Skip to content

Commit 834d23f

Browse files
author
liuzeming
committed
feat: add CallTraceBar for real-time call tracing in TUI
Add a collapsible CallTraceBar component that provides real-time visibility into all calls during a session: - User messages with expandable text - LLM calls showing model, tokens, cost, and duration - Tool calls with expandable input/output - Plugin operations (agent calls, subtasks) Components: - call-trace.tsx: Trace context provider with categorized traces - session/index.tsx: CallTraceBar integration into session view - Tests for trace categorization and integration Closes anomalyco#20068
1 parent 14f9e21 commit 834d23f

4 files changed

Lines changed: 1892 additions & 180 deletions

File tree

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
import { createSimpleContext } from "./helper"
2+
import { useSync } from "./sync"
3+
import type {
4+
Part,
5+
ToolPart,
6+
StepFinishPart,
7+
AgentPart,
8+
SubtaskPart,
9+
TextPart,
10+
UserMessage,
11+
AssistantMessage,
12+
Message,
13+
} from "@opencode-ai/sdk/v2"
14+
15+
export type TraceSource = "llm" | "tool" | "omo" | "unknown"
16+
export type TraceStatus = "pending" | "running" | "completed" | "error"
17+
export type TraceCategory = "user" | "opencode" | "llm" | "plugin"
18+
19+
export interface CallTraceItem {
20+
id: string
21+
type: string
22+
source: TraceSource
23+
category: TraceCategory
24+
name: string
25+
component: string
26+
startTime: number
27+
endTime?: number
28+
duration?: number
29+
status: TraceStatus
30+
metadata?: Record<string, unknown>
31+
providerID?: string
32+
modelID?: string
33+
tokens?: { input: number; output: number }
34+
cost?: number
35+
toolName?: string
36+
input?: string
37+
output?: string
38+
agentName?: string
39+
description?: string
40+
sessionID?: string
41+
messageID?: string
42+
}
43+
44+
export function partToTrace(part: Part): CallTraceItem | null {
45+
const base = {
46+
id: part.id,
47+
component: part.type,
48+
sessionID: part.sessionID,
49+
messageID: part.messageID,
50+
}
51+
52+
if (part.type === "tool") {
53+
const tool = part as ToolPart
54+
const state = tool.state
55+
const status: TraceStatus =
56+
state.status === "pending"
57+
? "pending"
58+
: state.status === "running"
59+
? "running"
60+
: state.status === "error"
61+
? "error"
62+
: "completed"
63+
64+
return {
65+
...base,
66+
type: "tool",
67+
source: "tool",
68+
category: "opencode",
69+
name: tool.tool,
70+
toolName: tool.tool,
71+
status,
72+
startTime: "time" in state ? state.time.start : Date.now(),
73+
endTime: "time" in state && "end" in state.time ? state.time.end : undefined,
74+
duration: "time" in state && "end" in state.time ? state.time.end - state.time.start : undefined,
75+
input: "input" in state ? JSON.stringify(state.input) : undefined,
76+
output: "output" in state ? state.output : undefined,
77+
}
78+
}
79+
80+
if (part.type === "agent") {
81+
const agent = part as AgentPart
82+
return {
83+
...base,
84+
type: "omo",
85+
source: "omo",
86+
category: "plugin",
87+
name: agent.name,
88+
agentName: agent.name,
89+
status: "completed",
90+
startTime: Date.now(),
91+
}
92+
}
93+
94+
if (part.type === "subtask") {
95+
const subtask = part as SubtaskPart
96+
return {
97+
...base,
98+
type: "omo",
99+
source: "omo",
100+
category: "plugin",
101+
name: subtask.agent,
102+
agentName: subtask.agent,
103+
description: subtask.description,
104+
status: "completed",
105+
startTime: Date.now(),
106+
input: subtask.prompt,
107+
}
108+
}
109+
110+
return null
111+
}
112+
113+
export function messageToLLMTrace(msg: Message, userText?: string, assistantText?: string): CallTraceItem | null {
114+
if (msg.role !== "assistant") return null
115+
const assistant = msg as AssistantMessage
116+
117+
return {
118+
id: `llm-${assistant.id}`,
119+
type: "llm",
120+
source: "llm",
121+
category: "llm",
122+
name: `${assistant.providerID}/${assistant.modelID}`,
123+
component: "llm",
124+
status: "completed",
125+
startTime: assistant.time.created,
126+
endTime: assistant.time.completed,
127+
duration: assistant.time.completed ? assistant.time.completed - assistant.time.created : undefined,
128+
providerID: assistant.providerID,
129+
modelID: assistant.modelID,
130+
tokens: assistant.tokens ? { input: assistant.tokens.input, output: assistant.tokens.output } : undefined,
131+
cost: assistant.cost,
132+
input: userText,
133+
output: assistantText,
134+
sessionID: assistant.sessionID,
135+
messageID: assistant.id,
136+
}
137+
}
138+
139+
export interface CategorizedTraces {
140+
user: CallTraceItem[]
141+
opencode: CallTraceItem[]
142+
llm: CallTraceItem[]
143+
plugin: CallTraceItem[]
144+
}
145+
146+
export const { use: useCallTrace, provider: CallTraceProvider } = createSimpleContext({
147+
name: "CallTrace",
148+
init: () => {
149+
const sync = useSync()
150+
151+
const traces = (sessionID: string): CallTraceItem[] => {
152+
const messages = sync.data.message[sessionID] ?? []
153+
const result: CallTraceItem[] = []
154+
155+
for (let i = 0; i < messages.length; i++) {
156+
const msg = messages[i]
157+
const parts = sync.data.part[msg.id] ?? []
158+
159+
if (msg.role === "user") {
160+
const textParts = parts.filter((p) => p.type === "text") as TextPart[]
161+
for (const tp of textParts) {
162+
if (!tp.synthetic) {
163+
result.push({
164+
id: tp.id,
165+
type: "user",
166+
source: "unknown",
167+
category: "user",
168+
name: "User",
169+
component: "text",
170+
status: "completed",
171+
startTime: tp.time?.start ?? Date.now(),
172+
input: tp.text,
173+
messageID: msg.id,
174+
sessionID: tp.sessionID,
175+
})
176+
}
177+
}
178+
}
179+
180+
if (msg.role === "assistant") {
181+
const textParts = parts.filter((p) => p.type === "text" && !p.synthetic && !p.ignored) as TextPart[]
182+
const assistantText = textParts.map((tp) => tp.text).join("")
183+
184+
let userText = ""
185+
if (i > 0 && messages[i - 1].role === "user") {
186+
const prevParts = sync.data.part[messages[i - 1].id] ?? []
187+
const prevTextParts = prevParts.filter((p) => p.type === "text" && !p.synthetic && !p.ignored) as TextPart[]
188+
userText = prevTextParts.map((tp) => tp.text).join("")
189+
}
190+
191+
const llmTrace = messageToLLMTrace(msg, userText, assistantText)
192+
if (llmTrace) result.push(llmTrace)
193+
}
194+
195+
for (const part of parts) {
196+
if (part.type === "tool" || part.type === "agent" || part.type === "subtask") {
197+
const trace = partToTrace(part)
198+
if (trace) result.push(trace)
199+
}
200+
}
201+
}
202+
203+
return result
204+
}
205+
206+
const categorized = (sessionID: string): CategorizedTraces => {
207+
const all = traces(sessionID)
208+
return {
209+
user: all.filter((t) => t.category === "user"),
210+
opencode: all.filter((t) => t.category === "opencode"),
211+
llm: all.filter((t) => t.category === "llm"),
212+
plugin: all.filter((t) => t.category === "plugin"),
213+
}
214+
}
215+
216+
return {
217+
traces,
218+
categorized,
219+
}
220+
},
221+
})

0 commit comments

Comments
 (0)