Stream Claude Code subagent activity inline in the transcript
This commit is contained in:
@@ -580,6 +580,15 @@ func transcriptLine(ev api.Event, root string) string {
|
||||
p := ev.Payload
|
||||
text := func(k string) string { return fmt.Sprint(p[k]) }
|
||||
rel := func(s string) string { return strings.ReplaceAll(s, root+"/", "") }
|
||||
if p["parent_tool_use_id"] != nil {
|
||||
switch ev.Kind {
|
||||
case "agent.message":
|
||||
return "subagent: " + text("text")
|
||||
case "agent.tool_call":
|
||||
return "subagent tool " + text("name")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
switch ev.Kind {
|
||||
case "user.message":
|
||||
if ev.Actor == "byre" {
|
||||
|
||||
@@ -757,12 +757,19 @@ func (m *Manager) handle(ctx context.Context, l *live, ev claude.Event) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parent, err := ev.ParentToolUseID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, b := range blocks {
|
||||
err = m.handleAssistantBlock(ctx, l, b)
|
||||
err = m.handleAssistantBlock(ctx, l, b, parent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if parent != "" {
|
||||
return nil
|
||||
}
|
||||
usage, err := ev.ContextUsage()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -778,6 +785,10 @@ func (m *Manager) handle(ctx context.Context, l *live, ev claude.Event) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parent, err := ev.ParentToolUseID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, b := range blocks {
|
||||
if b.Type != "tool_result" {
|
||||
continue
|
||||
@@ -786,11 +797,11 @@ func (m *Manager) handle(ctx context.Context, l *live, ev claude.Event) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = m.emit(ctx, l, agentActor, "agent.tool_result", map[string]any{
|
||||
err = m.emit(ctx, l, agentActor, "agent.tool_result", withParent(map[string]any{
|
||||
"tool_use_id": b.ToolUseID,
|
||||
"is_error": b.IsError,
|
||||
"content": content,
|
||||
})
|
||||
}, parent))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -943,30 +954,37 @@ func (m *Manager) updateContext(ctx context.Context, l *live, used, window int)
|
||||
return m.emit(ctx, l, agentActor, "session.context", map[string]any{"model": l.contextModel, "used": used, "window": window})
|
||||
}
|
||||
|
||||
func (m *Manager) handleAssistantBlock(ctx context.Context, l *live, b claude.Block) error {
|
||||
func (m *Manager) handleAssistantBlock(ctx context.Context, l *live, b claude.Block, parent string) error {
|
||||
switch b.Type {
|
||||
case "text":
|
||||
return m.emit(ctx, l, agentActor, "agent.message", map[string]any{"text": b.Text})
|
||||
return m.emit(ctx, l, agentActor, "agent.message", withParent(map[string]any{"text": b.Text}, parent))
|
||||
|
||||
case "thinking":
|
||||
if b.Thinking == "" {
|
||||
return nil
|
||||
}
|
||||
return m.emit(ctx, l, agentActor, "agent.thinking", map[string]any{"text": b.Thinking})
|
||||
return m.emit(ctx, l, agentActor, "agent.thinking", withParent(map[string]any{"text": b.Thinking}, parent))
|
||||
|
||||
case "tool_use":
|
||||
input, err := decode(b.Input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.emit(ctx, l, agentActor, "agent.tool_call", map[string]any{
|
||||
return m.emit(ctx, l, agentActor, "agent.tool_call", withParent(map[string]any{
|
||||
"id": b.ID,
|
||||
"name": b.Name,
|
||||
"input": input,
|
||||
})
|
||||
}, parent))
|
||||
}
|
||||
|
||||
return m.emit(ctx, l, agentActor, "agent.vendor", store.ToPayload(b))
|
||||
return m.emit(ctx, l, agentActor, "agent.vendor", withParent(store.ToPayload(b), parent))
|
||||
}
|
||||
|
||||
func withParent(payload map[string]any, parent string) map[string]any {
|
||||
if parent != "" {
|
||||
payload["parent_tool_use_id"] = parent
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func (m *Manager) emit(ctx context.Context, l *live, actor, kind string, payload map[string]any) error {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package agents
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
"g.fc.run/misc/byre/internal/claude"
|
||||
"g.fc.run/misc/byre/internal/store"
|
||||
)
|
||||
|
||||
func TestSubagentEvents(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
st, err := store.Open(filepath.Join(t.TempDir(), "byre.db"), func(api.Event) {})
|
||||
if err != nil {
|
||||
t.Fatalf("open store: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { st.Close() })
|
||||
|
||||
now := time.Now().UTC()
|
||||
_, err = st.EnsureUser(ctx, "alice", "Alice")
|
||||
if err != nil {
|
||||
t.Fatalf("ensure user: %v", err)
|
||||
}
|
||||
err = st.CreateProject(ctx, api.Project{Id: "p1", Name: "demo", RepoPath: "/nonexistent", DefaultBranch: "main", Remote: "none", CreatedBy: "alice", CreatedAt: now})
|
||||
if err != nil {
|
||||
t.Fatalf("create project: %v", err)
|
||||
}
|
||||
err = st.CreateTask(ctx, api.Task{Id: "t1", ProjectId: "p1", Title: "t", Branch: "b", WorktreePath: "/nonexistent", BaseRef: "x", Status: api.TaskStatusActive, CreatedBy: "alice", CreatedAt: now}, "alice")
|
||||
if err != nil {
|
||||
t.Fatalf("create task: %v", err)
|
||||
}
|
||||
|
||||
m := New(st, DefaultLimits, t.TempDir(), t.TempDir())
|
||||
l := &live{id: "s1", dir: t.TempDir(), vendor: api.Claude, taskID: "t1", projectID: "p1"}
|
||||
|
||||
feed := func(typ, raw string) {
|
||||
t.Helper()
|
||||
err := m.handle(ctx, l, claude.Event{Type: typ, Raw: []byte(raw)})
|
||||
if err != nil {
|
||||
t.Fatalf("handle %s: %v", raw, err)
|
||||
}
|
||||
}
|
||||
|
||||
feed("assistant", `{"type":"assistant","message":{"model":"m","content":[{"type":"tool_use","id":"toolu_agent","name":"Agent","input":{"subagent_type":"general-purpose","description":"d","prompt":"p"}}],"usage":{"input_tokens":2,"cache_read_input_tokens":100,"output_tokens":17}},"parent_tool_use_id":null}`)
|
||||
feed("assistant", `{"type":"assistant","message":{"model":"m","content":[{"type":"tool_use","id":"toolu_bash","name":"Bash","input":{"command":"ls"}}],"usage":{"input_tokens":2,"cache_creation_input_tokens":33494,"output_tokens":20}},"parent_tool_use_id":"toolu_agent","agent_id":"a1"}`)
|
||||
feed("user", `{"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_bash","type":"tool_result","content":"adsb-tools","is_error":false}]},"parent_tool_use_id":"toolu_agent","agent_id":"a1"}`)
|
||||
feed("assistant", `{"type":"assistant","message":{"model":"m","content":[{"type":"text","text":"The first entry is adsb-tools."}],"usage":{"input_tokens":2,"output_tokens":3}},"parent_tool_use_id":"toolu_agent","agent_id":"a1"}`)
|
||||
|
||||
events, err := st.TaskEvents(ctx, "t1", 0, 100)
|
||||
if err != nil {
|
||||
t.Fatalf("task events: %v", err)
|
||||
}
|
||||
got := []string{}
|
||||
for _, ev := range events {
|
||||
if ev.AgentSessionId == nil {
|
||||
continue
|
||||
}
|
||||
parent, _ := ev.Payload["parent_tool_use_id"].(string)
|
||||
got = append(got, ev.Kind+" "+parent)
|
||||
}
|
||||
want := []string{
|
||||
"agent.tool_call ",
|
||||
"session.context ",
|
||||
"agent.tool_call toolu_agent",
|
||||
"agent.tool_result toolu_agent",
|
||||
"agent.message toolu_agent",
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("want %q, got %q", want, got)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("want %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
if l.contextUsed != 119 {
|
||||
t.Fatalf("subagent usage changed the session's context: %d", l.contextUsed)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user