package modelloop import ( "fmt" "strings" "github.com/samcharles93/archie-core/internal/domain/agentrun" "github.com/samcharles93/archie-core/internal/prompts" ) // missionWithHistory puts the conversation of an earlier run of the stage // ahead of the mission, cut to fit the model's context window. agentloop has no // field for prior messages, so the conversation goes in the first user message // as text. func missionWithHistory(req agentrun.Request) string { entries := agentrun.BoundHistory(req.History, agentrun.HistoryBytes(req.ContextWindow)) if len(entries) == 0 { return req.Mission } var b strings.Builder b.WriteString("\n" + prompts.ResumeRun() + "\n\n") for _, e := range entries { switch e.Kind { case agentrun.TranscriptModel: if e.Text != "" { fmt.Fprintf(&b, "[you] %s\n", e.Text) } for _, call := range e.ToolCalls { fmt.Fprintf(&b, "[you called %s] %s\n", call.Name, call.Args) } case agentrun.TranscriptTool: if e.Error != "" { fmt.Fprintf(&b, "[%s failed] %s\n", e.Tool, e.Error) } else { fmt.Fprintf(&b, "[%s returned] %s\n", e.Tool, e.Output) } case agentrun.TranscriptUser: fmt.Fprintf(&b, "[feedback] %s\n", e.Text) case agentrun.TranscriptCompaction: fmt.Fprintf(&b, "[summary of earlier steps] %s\n", e.Text) } } b.WriteString("\n\n") return b.String() + req.Mission }