on main help sign in
fix(template): for coder so we handle memory files correctly
abb654ekujtimiihoxha
internal/agent/prompt/prompt.go+28−18
@@ -23,13 +23,14 @@ type Prompt struct {
23}
23}
24
24
25type PromptDat struct {
25type PromptDat struct {
26− Provider string
26+ Provider string
27− Model string
27+ Model string
28− Config config.Config
28+ Config config.Config
29− WorkingDir string
29+ WorkingDir string
30− IsGitRepo bool
30+ IsGitRepo bool
31− Platform string
31+ Platform string
32− Date string
32+ Date string
33+ ContextFiles []ContextFile
33}
34}
34
35
35type ContextFile struct {
36type ContextFile struct {
@@ -70,7 +71,7 @@ func NewPrompt(name, promptTemplate string, opts ...Option) (*Prompt, error) {
70}
71}
71
72
72func (p *Prompt) Build(provider, model string, cfg config.Config) (string, error) {
73func (p *Prompt) Build(provider, model string, cfg config.Config) (string, error) {
73− t, err := template.New(p.name).Funcs(p.funcMap(cfg)).Parse(p.template)
74+ t, err := template.New(p.name).Parse(p.template)
74 if err != nil {
75 if err != nil {
75 return "", fmt.Errorf("parsing template: %w", err)
76 return "", fmt.Errorf("parsing template: %w", err)
76 }
77 }
@@ -82,15 +83,6 @@ func (p *Prompt) Build(provider, model string, cfg config.Config) (string, error
82 return sb.String(), nil
83 return sb.String(), nil
83}
84}
84
85
85−func (p *Prompt) funcMap(cfg config.Config) template.FuncMap {
86− return template.FuncMap{
87− "contextFiles": func(path string) []ContextFile {
88− path = expandPath(path, cfg)
89− return processContextPath(path, cfg)
90− },
91− }
92−}
93−
94func processFile(filePath string) *ContextFile {
86func processFile(filePath string) *ContextFile {
95 content, err := os.ReadFile(filePath)
87 content, err := os.ReadFile(filePath)
96 if err != nil {
88 if err != nil {
@@ -155,7 +147,20 @@ func (p *Prompt) promptData(provider, model string, cfg config.Config) PromptDat
155 if p.platform != "" {
147 if p.platform != "" {
156 platform = p.platform
148 platform = p.platform
157 }
149 }
150+
151+ files := map[string][]ContextFile{}
152+
153+ for _, pth := range cfg.Options.ContextPaths {
154+ expanded := expandPath(pth, cfg)
155+ pathKey := strings.ToLower(expanded)
156+ if _, ok := files[pathKey]; ok {
157+ continue
158+ }
159+ content := processContextPath(expanded, cfg)
160+ files[pathKey] = content
161+ }
162+
158− return PromptDat{
163+ data := PromptDat{
159 Provider: provider,
164 Provider: provider,
160 Model: model,
165 Model: model,
161 Config: cfg,
166 Config: cfg,
@@ -164,6 +169,11 @@ func (p *Prompt) promptData(provider, model string, cfg config.Config) PromptDat
164 Platform: platform,
169 Platform: platform,
165 Date: p.now().Format("1/2/2006"),
170 Date: p.now().Format("1/2/2006"),
166 }
171 }
172+
173+ for _, contextFiles := range files {
174+ data.ContextFiles = append(data.ContextFiles, contextFiles...)
175+ }
176+ return data
167}
177}
168
178
169func isGitRepo(dir string) bool {
179func isGitRepo(dir string) bool {
internal/agent/templates/coder.md.tpl+2−5
@@ -124,14 +124,11 @@ Tools that support it will also include useful diagnostics such as linting and t
124- Take necessary actions to fix the issues.
124- Take necessary actions to fix the issues.
125- You should ignore diagnostics of files that you did not change or are not related or caused by your changes unless the user explicitly asks you to fix them.
125- You should ignore diagnostics of files that you did not change or are not related or caused by your changes unless the user explicitly asks you to fix them.
126</lsp>
126</lsp>
127−{{end}}{{if gt (len .Config.Options.ContextPaths) 0}}
127+{{end}}{{if .ContextFiles}}
128−<memory>
128+<memory>{{range .ContextFiles}}
129−{{range .Config.Options.ContextPaths}}
130−{{range contextFiles .}}
131<file path="{{.Path}}">
129<file path="{{.Path}}">
132{{.Content}}
130{{.Content}}
133</file>
131</file>
134{{end}}
132{{end}}
135−{{end}}
136</memory>
133</memory>
137{{end}}
134{{end}}
internal/app/app.go−1
@@ -33,7 +33,6 @@ type App struct {
33 History history.Service
33 History history.Service
34 Permissions permission.Service
34 Permissions permission.Service
35
35
36− // TODO: (kujtim) remove this when fully implemented
37 AgentCoordinator agent.Coordinator
36 AgentCoordinator agent.Coordinator
38
37
39 LSPClients *csync.Map[string, *lsp.Client]
38 LSPClients *csync.Map[string, *lsp.Client]