App images have no templates/ checkout, so GetDocTemplate fell back to a two-section stub and prelim_design lost its real contract. Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package template
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"chat-agent-system/templates"
|
|
)
|
|
|
|
func findProjectRoot() string {
|
|
wd, _ := os.Getwd()
|
|
dir := wd
|
|
for i := 0; i < 10; i++ {
|
|
if _, err := os.Stat(filepath.Join(dir, "templates", "docs")); err == nil {
|
|
return dir
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
break
|
|
}
|
|
dir = parent
|
|
}
|
|
return wd
|
|
}
|
|
|
|
func GetDocTemplate(name string) (string, error) {
|
|
name = strings.TrimSuffix(name, ".md")
|
|
if name == "" || strings.Contains(name, "..") || strings.ContainsAny(name, `/\`) {
|
|
return "", fmt.Errorf("read doc template: invalid name %q", name)
|
|
}
|
|
if b, err := templates.DocTemplates.ReadFile("docs/" + name + ".md"); err == nil && len(strings.TrimSpace(string(b))) > 0 {
|
|
return string(b), nil
|
|
}
|
|
root := findProjectRoot()
|
|
path := filepath.Join(root, "templates", "docs", name+".md")
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read doc template %s: %w", name, err)
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
func GetAgentInstruction(role string) (string, error) {
|
|
role = strings.TrimSuffix(role, ".md")
|
|
root := findProjectRoot()
|
|
path := filepath.Join(root, "configs", "agents", role+".md")
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read agent instruction %s: %w", role, err)
|
|
}
|
|
|
|
inst := string(b)
|
|
writingPath := filepath.Join(root, "configs", "shared", "writing.md")
|
|
if wb, err := os.ReadFile(writingPath); err == nil {
|
|
inst += "\n\n" + string(wb)
|
|
}
|
|
return inst, nil
|
|
}
|