Files
archie/internal/gateway/prompt.go
T
sam b2d4f93105 fix(gateway): fix broken system prompt template and scrub real telegram ID from tests
Funcs() was passed a *strings.Replacer value instead of its Replace
method, panicking gateway's init(). Fixing that surfaced a second bug:
the template still referenced .Operator, removed from promptData by
5a92fc1, which made every render fail and silently fall back to a
generic hardcoded prompt.

Also replaces a real Telegram chat ID hardcoded across several test
fixtures with a placeholder value.
2026-08-29 00:31:47 +10:00

86 lines
2.4 KiB
Go

package gateway
import (
_ "embed"
"strings"
"text/template"
"time"
)
//go:embed templates/archie.md.tpl
var archiePromptTpl string
// ToolSummary is the capability metadata for one registered tool, as it is
// advertised to the model.
type ToolSummary struct {
Name string
Description string
}
// SystemPromptConfig holds the inputs for one rendered system prompt.
type SystemPromptConfig struct {
// Persona is the active persona prompt.
Persona string
// Tools is the complete set of tools available for this turn.
Tools []ToolSummary
// Channel names the communication channel.
Channel string
Model string
SessionID string
// Page is the dashboard route the operator is on right now (e.g.
// "/tasks"). It is supplied per message by the web channel; empty for
// non-web channels, which have no page context. Rendered in the <env>
// runtime-metadata block so the agent knows where the operator is
// looking and can point them somewhere relevant.
Page string
// Now stamps the prompt's date.
Now time.Time
}
// promptData is the template execution context. It exists so the template
// sees a pre-formatted date and pre-flattened tool metadata rather than
// calling methods during rendering.
type promptData struct {
SystemPromptConfig
Date string
}
var archiePromptTemplate = template.Must(
template.New("archie").
Funcs(template.FuncMap{"xml": strings.NewReplacer(
"&", "&amp;",
"<", "&lt;",
">", "&gt;",
).Replace}).
Parse(archiePromptTpl),
)
// BuildSystemPrompt renders the chat agent's system prompt.
func BuildSystemPrompt(cfg SystemPromptConfig) string {
tools := make([]ToolSummary, len(cfg.Tools))
for i, tool := range cfg.Tools {
tools[i] = ToolSummary{
Name: strings.Join(strings.Fields(tool.Name), " "),
Description: strings.Join(strings.Fields(tool.Description), " "),
}
}
data := promptData{
Persona: cfg.Persona,
Tools: tools,
Channel: cfg.Channel,
Model: cfg.Model,
SessionID: cfg.SessionID,
Page: cfg.Page,
Now: cfg.Now,
Date: cfg.Now.Format("2006-01-02"),
}
var buf strings.Builder
if err := archiePromptTemplate.Execute(&buf, data); err != nil {
tpl := "You are Archie, a coding and project assistant. " +
"Never claim a tool, file, memory, action or result you have not verified. " +
"Keep replies short and lead with the answer."
return tpl
}
return strings.TrimSpace(buf.String())
}