Wires the durable-memory engine into the chat turn: prepareTurn resolves a
per-channel Subject (BotUser as AgentID, TurnRunnerConfig.UserIdentity for
the user), runs a scope-only Query over Subject.Scopes(), and renders the
result into a new <memory purpose="durable_context" trust="data"> block
between <tools> and <env>, following the same untrusted-data framing as
those two blocks. The read is synchronous, total, and never surfaces an
error: a nil engine, a failing Query, or a panicking provider all degrade to
no memory block (recovered in gateway.renderMemory).
UserIdentity is supplied per channel by the composition
(userIdentityResolver in internal/app/archied): telegram and email resolve
their native SenderID, the dashboard ("web") and webhook resolve nothing, so
a webhook's route-path SenderID can never become a user identity and the
dashboard's shared bearer token can never impersonate a person.
Fixes the composition-ordering bug that left every turn runner holding a nil
memory engine: setupMemoryAll now runs before setupGateways in main.go, and
before setupGatewayChat in gateway.go's startGatewayRuntime, so
boot.memoryStore() has a live engine at every turn-runner construction site.
Named constants bound the read: memoryRecordsPerScope per scope, and
memoryBlockByteCap on the rendered block, which drops (and logs) trailing
records rather than truncating one mid-line.
126 lines
4.4 KiB
Go
126 lines
4.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
|
|
}
|
|
|
|
// RepoEnv is the runtime-metadata summary of one repository under
|
|
// management, rendered into the chat prompt's <env> block so the agent knows
|
|
// which checkouts it is responsible for without probing the filesystem. It is
|
|
// built only from live configuration; it never names a repository the daemon
|
|
// cannot confirm.
|
|
type RepoEnv struct {
|
|
// FullName is the configured owner/name.
|
|
FullName string
|
|
// Forge is the forge host the repository lives on (forge.host). Empty
|
|
// means no forge is configured for the active identity.
|
|
Forge string
|
|
// DefaultBranch is the branch PRs target (repos[].base, "main" when
|
|
// unset) -- the branch the daemon actually uses, not a guess.
|
|
DefaultBranch 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
|
|
// Workspace is the directory the chat agent's file and shell tools are
|
|
// rooted at (chat.workspace). Empty means the agent has no rooted
|
|
// workspace: a real fact the prompt must say explicitly ("unknown") so the
|
|
// agent does not guess a path the daemon never granted it.
|
|
Workspace string
|
|
// Repos lists the repositories under management with their forge host and
|
|
// default branch. Rendered from live configuration (repos entries) so the
|
|
// agent knows its scope without probing the filesystem.
|
|
Repos []RepoEnv
|
|
// Operator is the display name of the person this deployment assists
|
|
// (chat.operator). Rendered so the agent knows the identity context it
|
|
// serves under; empty means unconfigured and must be said explicitly.
|
|
Operator string
|
|
// Memory is the pre-rendered <memory> block body: the durable records the
|
|
// read path recalled for this turn's resolved subject, already bounded by
|
|
// the per-scope record limit and the render byte cap. Empty when there is
|
|
// nothing to recall, or when the read degraded (engine failure, panic,
|
|
// unresolved identity beyond global/agent scope) -- the template omits
|
|
// the block entirely in that case, exactly like Tools.
|
|
Memory string
|
|
}
|
|
|
|
// 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(
|
|
"&", "&",
|
|
"<", "<",
|
|
">", ">",
|
|
).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"),
|
|
Workspace: cfg.Workspace,
|
|
Repos: cfg.Repos,
|
|
Operator: cfg.Operator,
|
|
Memory: cfg.Memory,
|
|
}
|
|
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())
|
|
}
|