- configs package embeds agents/ + shared/ prompt assets; templates package embeds xlsx/ (cdo_quote.xlsx) - template.GetAgentInstructionFile: disk-first with embedded fallback for instructions and writing.md (dev behavior unchanged) - quote.CalculateAndFill: embedded xlsx fallback when the on-disk template walk misses (single-binary deployments) - presence test guards the embedded asset set - verified: vet + template/quote/bizdev/squad/arch green
115 lines
3.4 KiB
Go
115 lines
3.4 KiB
Go
package template
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"chat-agent-system/configs"
|
|
"chat-agent-system/internal/workflowdef"
|
|
"chat-agent-system/templates"
|
|
)
|
|
|
|
func findProjectRoot() string {
|
|
wd, _ := os.Getwd()
|
|
candidates := []string{wd}
|
|
dir := wd
|
|
for i := 0; i < 10; i++ {
|
|
candidates = append(candidates, dir)
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
break
|
|
}
|
|
dir = parent
|
|
}
|
|
if env := strings.TrimSpace(os.Getenv("APP_ROOT")); env != "" {
|
|
candidates = append(candidates, env)
|
|
}
|
|
// Image WORKDIR is /app; Dockerfile copies templates/configs here.
|
|
candidates = append(candidates, "/app")
|
|
for _, root := range candidates {
|
|
if root == "" {
|
|
continue
|
|
}
|
|
if _, err := os.Stat(filepath.Join(root, "templates", "docs")); err == nil {
|
|
return root
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
for _, candidate := range templateNames(name) {
|
|
if b, err := templates.DocTemplates.ReadFile("docs/" + candidate + ".md"); err == nil && len(strings.TrimSpace(string(b))) > 0 {
|
|
return string(b), nil
|
|
}
|
|
root := findProjectRoot()
|
|
path := filepath.Join(root, "templates", "docs", candidate+".md")
|
|
b, err := os.ReadFile(path)
|
|
if err == nil && len(strings.TrimSpace(string(b))) > 0 {
|
|
return string(b), nil
|
|
}
|
|
}
|
|
return "", fmt.Errorf("read doc template %s: file not found", name)
|
|
}
|
|
|
|
func templateNames(name string) []string {
|
|
names := []string{name}
|
|
if canonical := workflowdef.CanonicalTaskKey(name); canonical != name {
|
|
names = append(names, canonical)
|
|
}
|
|
if legacy := workflowdef.ImplementationTaskKey(name); legacy != name {
|
|
names = append(names, legacy)
|
|
}
|
|
return names
|
|
}
|
|
|
|
func GetAgentInstruction(role string) (string, error) {
|
|
role = strings.TrimSuffix(role, ".md")
|
|
if role == "" || strings.Contains(role, "..") || strings.ContainsAny(role, `/\`) {
|
|
return "", fmt.Errorf("read agent instruction: invalid role %q", role)
|
|
}
|
|
return GetAgentInstructionFile(filepath.Join("configs", "agents", role+".md"))
|
|
}
|
|
|
|
// GetAgentInstructionFile 读取 configs/agents/ 下指定相对路径的指令文件,
|
|
// 并自动追加 configs/shared/writing.md。rel 必须落在 configs/agents/ 内,拒绝 ".."。
|
|
func GetAgentInstructionFile(rel string) (string, error) {
|
|
rel = filepath.ToSlash(strings.TrimSpace(rel))
|
|
if rel == "" || strings.Contains(rel, "..") {
|
|
return "", fmt.Errorf("read agent instruction file: invalid path %q", rel)
|
|
}
|
|
if !strings.HasPrefix(rel, "configs/agents/") {
|
|
return "", fmt.Errorf("read agent instruction file: must be under configs/agents/: %q", rel)
|
|
}
|
|
root := findProjectRoot()
|
|
path := filepath.Join(root, filepath.FromSlash(rel))
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
// U10a:磁盘缺失(单文件部署)时回退内嵌 configs 资产。
|
|
eb, eerr := configs.Assets.ReadFile(strings.TrimPrefix(rel, "configs/"))
|
|
if eerr != nil {
|
|
return "", fmt.Errorf("read agent instruction file %s: %w", rel, err)
|
|
}
|
|
b = eb
|
|
}
|
|
|
|
inst := string(b)
|
|
writingPath := filepath.Join(root, "configs", "shared", "writing.md")
|
|
wb, werr := os.ReadFile(writingPath)
|
|
if werr != nil {
|
|
if eb, eerr := configs.Assets.ReadFile("shared/writing.md"); eerr == nil {
|
|
wb = eb
|
|
}
|
|
}
|
|
if wb != nil {
|
|
inst += "\n\n" + string(wb)
|
|
}
|
|
return inst, nil
|
|
}
|