package modelloop import ( "context" "encoding/json" "strings" "sync" "time" "unicode/utf8" "github.com/samcharles93/ai-sdk/chat" "github.com/samcharles93/ai-sdk/core" "github.com/samcharles93/archie-core/internal/domain/agentrun" "github.com/samcharles93/archie-core/internal/webhookguard" ) // transcriptFieldLimit caps each recorded text field, in bytes. const transcriptFieldLimit = 32 << 10 // compactionPrefix starts the instruction agentloop sends to summarise the // history, which marks a model call as a compaction rather than a step. const compactionPrefix = "Compact the conversation history above" // transcript records one agent run's conversation through agentloop's model // and tool hooks. Tool hooks run concurrently, so entries are numbered and // reported under one lock to keep their order. type transcript struct { report func(agentrun.TranscriptEntry) mu sync.Mutex seq int prompted bool lastUser string started map[string]time.Time } func newTranscript(report func(agentrun.TranscriptEntry)) *transcript { return &transcript{report: report, started: map[string]time.Time{}} } func (t *transcript) add(entry agentrun.TranscriptEntry) { t.seq++ entry.Seq = t.seq t.report(entry) } func (t *transcript) ModelCallStarted(context.Context, chat.Request) {} func (t *transcript) ModelCallFinished(_ context.Context, req chat.Request, resp *chat.Response, err error, latency time.Duration, usage *chat.Usage) { t.mu.Lock() defer t.mu.Unlock() entry := agentrun.TranscriptEntry{Kind: agentrun.TranscriptModel, LatencyMS: latency.Milliseconds(), Usage: transcriptUsage(usage)} if isCompaction(req) { entry.Kind = agentrun.TranscriptCompaction } else { t.recordInput(req.Messages) } if err != nil { entry.Error = clip(err.Error(), nil) } if resp != nil { entry.Text = clip(resp.Content, &entry.TextBytes) entry.Reasoning = clip(reasoning(resp.Parts), &entry.ReasoningBytes) entry.FinishReason = resp.FinishReason for _, call := range resp.ToolCalls { recorded := agentrun.TranscriptToolCall{ID: call.ID, Name: call.Name} recorded.Args = clipArgs(call.Arguments, &recorded.ArgsBytes) entry.ToolCalls = append(entry.ToolCalls, recorded) } } t.add(entry) } // recordInput records the prompt on the first call, and after that each user // message the loop appended since the model last answered: gate feedback, // recovery nudges. Assistant and tool messages are recorded from the response // and the tool hooks instead. func (t *transcript) recordInput(messages []chat.Message) { if !t.prompted { t.prompted = true for _, message := range messages { t.recordMessage(message) } return } start := len(messages) for start > 0 && messages[start-1].Role == chat.RoleUser { start-- } for _, message := range messages[start:] { if message.Content != t.lastUser { t.recordMessage(message) } } } func (t *transcript) recordMessage(message chat.Message) { kind := agentrun.TranscriptUser switch message.Role { case chat.RoleSystem: kind = agentrun.TranscriptSystem case chat.RoleUser: t.lastUser = message.Content default: return } entry := agentrun.TranscriptEntry{Kind: kind} entry.Text = clip(message.Content, &entry.TextBytes) t.add(entry) } func (t *transcript) BeforeToolExecute(_ context.Context, call *core.ToolCall) (*core.Skip, error) { t.mu.Lock() t.started[call.ToolCallID] = time.Now() t.mu.Unlock() return nil, nil } func (t *transcript) AfterToolExecute(_ context.Context, call core.ToolCall, result *core.ToolResult) { t.mu.Lock() defer t.mu.Unlock() entry := agentrun.TranscriptEntry{Kind: agentrun.TranscriptTool, CallID: call.ToolCallID, Tool: call.ToolName} if started, ok := t.started[call.ToolCallID]; ok { entry.LatencyMS = time.Since(started).Milliseconds() delete(t.started, call.ToolCallID) } entry.Args = clipArgs(call.Input, &entry.ArgsBytes) entry.Output = clip(result.Output, &entry.OutputBytes) entry.Error = clip(result.Error, nil) t.add(entry) } func reasoning(parts chat.Parts) string { var texts []string for _, part := range parts { if thought, ok := part.(chat.ReasoningPart); ok && thought.Text != "" { texts = append(texts, thought.Text) } } return strings.Join(texts, "\n\n") } func isCompaction(req chat.Request) bool { n := len(req.Messages) return n > 0 && req.Messages[n-1].Role == chat.RoleUser && strings.HasPrefix(req.Messages[n-1].Content, compactionPrefix) } func transcriptUsage(usage *chat.Usage) *agentrun.Usage { if usage == nil { return nil } return &agentrun.Usage{ PromptTokens: usage.PromptTokens, CompletionTokens: usage.CompletionTokens, TotalTokens: usage.TotalTokens, CachedTokens: usage.CachedTokens, CacheCreationTokens: usage.CacheCreationTokens, } } // clipArgs redacts tool arguments by key when they are JSON, then clips them. func clipArgs(args string, full *int) string { if json.Valid([]byte(args)) { if redacted, err := webhookguard.RedactPayload([]byte(args)); err == nil { args = string(redacted) } } return clip(args, full) } // clip redacts secret-shaped spans and cuts text to transcriptFieldLimit on a // rune boundary, storing the original size in full when it cuts. func clip(text string, full *int) string { text = webhookguard.RedactText(text) if len(text) <= transcriptFieldLimit { return text } if full != nil { *full = len(text) } cut := transcriptFieldLimit for cut > 0 && !utf8.RuneStart(text[cut]) { cut-- } return text[:cut] }