Files
archie/internal/agentexec/harness_output_pi.go
T
sam 6f10ecc04f feat(agentexec): record each agent run's transcript and show it per step (#1718)
The loop runner hooks every model call and tool call and reports ordered,
redacted, clipped entries. Each is persisted as an agent_transcript task
event through InsertEvent, left out of the timeline read, and served per
attempt at /api/tasks/{id}/transcript. The step run view shows it with Trace.
2026-11-01 22:09:53 +11:00

67 lines
1.9 KiB
Go

package agentexec
import (
"encoding/json"
"strings"
"github.com/samcharles93/archie-core/internal/domain/agentrun"
)
// piHarnessOutput reads `--mode json` from Pi and OMP, which share the
// event stream. Usage is summed from each assistant message_end, so an
// interrupted run still reports what it spent.
type piHarnessOutput struct {
sessionID string
usage agentrun.Usage
}
func (o *piHarnessOutput) Line(line []byte, report agentrun.Reporter) {
var event struct {
Type string `json:"type"`
ID string `json:"id"`
ToolName string `json:"toolName"`
IsError bool `json:"isError"`
Result struct {
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
} `json:"result"`
Message struct {
Role string `json:"role"`
Usage *struct {
Input int `json:"input"`
Output int `json:"output"`
CacheRead int `json:"cacheRead"`
CacheWrite int `json:"cacheWrite"`
} `json:"usage"`
} `json:"message"`
}
if json.Unmarshal(line, &event) != nil {
return
}
switch event.Type {
case "session":
o.sessionID = event.ID
case "message_end":
// Pi's input excludes cache reads and writes, as Claude's does.
if u := event.Message.Usage; event.Message.Role == "assistant" && u != nil {
o.usage = addUsage(o.usage, usageFromFields(u.Input, u.Output, u.CacheRead, u.CacheWrite))
}
case "tool_execution_end":
if event.ToolName == "" {
return
}
texts := make([]string, 0, len(event.Result.Content))
for _, block := range event.Result.Content {
if block.Type == "text" {
texts = append(texts, block.Text)
}
}
report.ReportToolCall(agentrun.ToolCallReport{Tool: event.ToolName, Detail: ClipToolCallDetail(strings.Join(texts, "\n")), Failed: event.IsError})
}
}
func (o *piHarnessOutput) SessionID() string { return o.sessionID }
func (o *piHarnessOutput) Usage() agentrun.Usage { return o.usage }