internal/domain/workflow imported internal/agentexec for its protocol types, pulling the AI runtime into every binary that touches workflow types. The request, result and runner contract now live in internal/domain/agentrun; agentexec keeps the runtime. The model catalog cache moves from servicekit into modelcatalog and WaitForPeer into staterpc, so archie-state-store and archie-messaging no longer link the AI runtime.
67 lines
1.9 KiB
Go
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.ToolCallReporter) {
|
|
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 == "" || report == nil {
|
|
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(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 }
|