Repository navigation
Expand file tree
/
Copy pathTaskTimeline.tsx
More file actions
486 lines (482 loc) · 16.9 KB
/
Copy pathTaskTimeline.tsx
File metadata and controls
486 lines (482 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import * as uiMessages from "./messages";
import AccountAttempts from "./AccountAttempts";
import type {
Account,
AccountAttempt,
AgentEvent,
Language,
Mutate,
Run,
Translate,
} from "./types";
import {
DateText,
eventDescription,
eventLabel,
eventText,
errorMessage,
statusLabel,
} from "./ui";
type Payload = Record<string, any>;
function payload(event: AgentEvent): Payload {
return event.payload && typeof event.payload === "object"
? (event.payload as Payload)
: {};
}
function toolText(value: unknown): string {
if (typeof value === "string") return value;
if (Array.isArray(value))
return value.map(toolText).filter(Boolean).join("\n");
if (value && typeof value === "object" && "text" in value)
return String(value.text);
return value == null ? "" : JSON.stringify(value, null, 2);
}
type Step = {
id: string;
kind: "reasoning" | "tool" | "message";
text: string;
name?: string;
input?: string;
status?: string;
itemId?: string;
phase?: string;
};
export function executionSteps(events: AgentEvent[]): Step[] {
const steps: Step[] = [];
const keyed = new Map<string, Step>();
for (const event of events) {
const p = payload(event);
if (event.kind === "output_truncated") {
steps.push({
id: event.id,
kind: "message",
text: typeof p.text === "string" ? p.text : "Progress truncated",
});
} else if (
event.kind === "reasoning_chunk" ||
event.kind === "reasoning_message"
) {
const id = `thought:${p.item_id ?? event.id}:${p.part ?? 0}`;
let step = keyed.get(id);
if (!step) {
step = { id, kind: "reasoning", text: "" };
keyed.set(id, step);
steps.push(step);
}
const text = typeof p.text === "string" ? p.text : "";
step.text = event.kind === "reasoning_message" ? text : step.text + text;
} else if (
["tool_call", "tool_result", "tool_output"].includes(event.kind)
) {
const item = p.item ?? {};
const id = `tool:${p.item_id ?? item.id ?? item.tool_use_id ?? event.id}`;
let step = keyed.get(id);
if (!step) {
step = { id, kind: "tool", text: "", status: "running" };
keyed.set(id, step);
steps.push(step);
}
const name = item.command ?? item.name ?? item.tool;
if (typeof name === "string") step.name = name;
if (item.input != null || item.arguments != null)
step.input = toolText(item.input ?? item.arguments);
if (event.kind === "tool_output")
step.text += typeof p.text === "string" ? p.text : "";
if (event.kind === "tool_result") {
const result =
item.aggregatedOutput ??
item.content ??
item.result ??
item.output ??
item.changes;
if (result != null) step.text = toolText(result);
step.status =
item.status === "declined"
? "declined"
: item.is_error ||
item.status === "failed" ||
(typeof item.exitCode === "number" && item.exitCode !== 0)
? "failed"
: "completed";
}
} else if (["agent_message_chunk", "agent_message"].includes(event.kind)) {
const itemId = typeof p.item_id === "string" ? p.item_id : undefined;
const id = itemId
? `message:${p.provider ?? "cli"}:${itemId}:${p.part ?? 0}`
: event.id;
let step = itemId ? keyed.get(id) : steps.at(-1);
if (step?.kind !== "message" || (!itemId && step.itemId)) {
step = { id, kind: "message", text: "", itemId };
keyed.set(id, step);
steps.push(step);
}
if (typeof p.phase === "string") step.phase = p.phase;
const text = eventText(event.payload);
step.text = event.kind === "agent_message" ? text : step.text + text;
}
}
return steps;
}
export function turnContent(events: AgentEvent[], storedAnswer: string) {
const steps = executionSteps(events);
const messages = steps.filter((step) => step.kind === "message");
let answer = storedAnswer;
// Older versions joined unphased Codex items into the saved result. Only
// separate them when the complete event groups reproduce that exact result.
if (
answer &&
messages.length > 1 &&
steps.at(-1)?.kind === "message" &&
messages.every((step) => !step.itemId && !step.phase) &&
messages.map((step) => step.text).join("\n") === answer
) {
answer = messages.at(-1)!.text;
}
const replyIds = new Set<string>();
if (answer) {
const suffix: Step[] = [];
for (const step of steps.slice().reverse()) {
if (step.kind !== "message") break;
suffix.unshift(step);
if (suffix.map((part) => part.text).join("\n") === answer) {
suffix.forEach((part) => replyIds.add(part.id));
break;
}
}
}
return {
answer,
steps: steps.filter(
(step) =>
!answer || (!replyIds.has(step.id) && step.phase !== "final_answer"),
),
};
}
export default function TaskTimeline({
runs,
events,
agentName,
accounts,
accountAttempts,
onConfigureAccount,
t,
lang,
busy,
mutate,
}: {
runs: Run[];
events: AgentEvent[];
agentName: string;
accounts?: Account[];
accountAttempts?: AccountAttempt[];
onConfigureAccount?: () => void;
t: Translate;
lang: Language;
busy: boolean;
mutate: Mutate;
}) {
const tasks = new Map(runs.map((r) => [r.id, { ...r }]));
const grouped = new Map<string, AgentEvent[]>();
const orphaned: AgentEvent[] = [];
const lastAccountChange = events
.filter((event) =>
[
"account_action_required",
"account_changed",
"account_switched",
"account_attempt",
].includes(event.kind),
)
.at(-1);
for (const event of events) {
const p = payload(event);
if (typeof p.run_id !== "string") {
orphaned.push(event);
continue;
}
const id = p.run_id;
if (!tasks.has(id))
tasks.set(id, {
id,
session_id: event.session_id,
agent_id: "",
project_id: event.project_id,
prompt: "",
status: "loading",
origin: "human",
depth: 0,
created_at: event.created_at,
updated_at: event.created_at,
});
const run = tasks.get(id)!;
if (event.kind === "user_message")
run.prompt =
typeof p.text === "string" ? p.text : eventText(event.payload);
if (
event.kind === "run_started" &&
["loading", "queued"].includes(run.status)
)
run.status = "running";
if (event.kind === "run_finished" && typeof p.status === "string")
run.status = p.status;
if (event.kind === "run_finished" && typeof p.error === "string")
run.error = p.error;
const group = grouped.get(id) ?? [];
group.push(event);
grouped.set(id, group);
}
return (
<div className="task-timeline">
{[...tasks.values()]
.sort((a, b) => a.created_at.localeCompare(b.created_at))
.map((run) => {
const runEvents = grouped.get(run.id) ?? [];
const modelEvent = runEvents
.filter((event) => event.kind === "model_info")
.at(-1);
const modelInfo = modelEvent ? payload(modelEvent) : {};
const answerEvent = runEvents
.slice()
.reverse()
.find((e) => e.kind === "assistant_message");
const { answer, steps } = turnContent(
runEvents,
run.result || (answerEvent ? eventText(answerEvent.payload) : ""),
);
const active = ["running", "queued"].includes(run.status);
const waiting =
runEvents.filter((e) => e.kind === "approval_required").length >
runEvents.filter((e) => e.kind === "approval_resolved").length;
const connection = runEvents
.filter((e) => e.kind === "transport_status")
.at(-1);
const reconnecting =
active &&
connection &&
payload(connection).status === "reconnecting";
const label =
run.status === "completed"
? run.work_task_id
? t(...uiMessages.tasktimeline_run_finished_3a0e76)
: t(...uiMessages.tasktimeline_task_completed_bdb582)
: run.status === "running"
? reconnecting
? t(
...uiMessages.tasktimeline_connection_lost_reconnecting_0274a0,
)
: waiting
? t(...uiMessages.tasktimeline_awaiting_approval_caad1d)
: t(...uiMessages.tasktimeline_task_running_0604a8)
: run.status === "queued"
? t(...uiMessages.tasktimeline_task_queued_b7058d)
: run.status === "failed"
? t(...uiMessages.tasktimeline_task_failed_e053ac)
: run.status === "loading"
? t(...uiMessages.tasktimeline_loading_task_5c1733)
: t(...uiMessages.tasktimeline_task_e8ed2b) +
" · " +
statusLabel(run.status, t);
return (
<article className="task-turn" key={run.id}>
{run.prompt && (
<div
className={`task-prompt ${run.origin === "human" ? "from-user" : "delegated"}`}
>
<div className="event-meta">
<span>
{run.origin === "human"
? t(...uiMessages.tasktimeline_you_d1c11b)
: t(...uiMessages.tasktimeline_delegated_task_b17737)}
</span>
<DateText date={run.created_at} lang={lang} />
</div>
<pre>{run.prompt}</pre>
</div>
)}
<div className="task-progress-row">
{/* Native toggles survive polling; changing this default on final
arrival collapses once, then the user can reopen it freely. */}
<details
className={`task-progress ${run.status}`}
open={
!answer && !["completed", "loading"].includes(run.status)
}
>
<summary>
<span
className={`task-indicator ${active && run.status === "running" ? "spinning" : ""}`}
>
{run.status === "completed" ? "✓" : active ? "◌" : "·"}
</span>
<span>{label}</span>
<span className="task-chevron">›</span>
{typeof modelInfo.model === "string" && (
<small
className="task-model"
title={t(
...uiMessages.tasktimeline_model_identifier_reported_by_the_cli_7e8617,
)}
>
{modelInfo.model}
{typeof modelInfo.effort === "string"
? ` · ${modelInfo.effort}`
: ""}
</small>
)}
</summary>
<div
className="task-steps"
role="log"
aria-label={t(
...uiMessages.tasktimeline_live_execution_ce86e3,
)}
>
{!steps.length && (
<p className="muted">
{run.status === "queued"
? t(
...uiMessages.tasktimeline_waiting_to_start_59a4fb,
)
: active
? t(
...uiMessages.tasktimeline_waiting_for_agent_output_470f31,
)
: t(
...uiMessages.tasktimeline_no_execution_details_a6e3d1,
)}
</p>
)}
{steps.map((step) =>
step.kind === "tool" ? (
<details
className="task-tool"
key={step.id}
open={step.status === "running" && active}
>
<summary>
<span>
{step.name ||
t(
...uiMessages.tasktimeline_tool_execution_bd5475,
)}
</span>
<small>
{step.status === "running" && !active
? statusLabel(
run.status === "completed"
? "interrupted"
: run.status,
t,
)
: statusLabel(step.status ?? "running", t)}
</small>
</summary>
{step.input && <pre>{step.input}</pre>}
{step.text && <pre>{step.text}</pre>}
</details>
) : (
<div className={`task-step ${step.kind}`} key={step.id}>
<small>
{step.kind === "reasoning"
? t(...uiMessages.tasktimeline_thinking_e891a0)
: step.phase === "final_answer"
? t(
...uiMessages.tasktimeline_composing_reply_a78a01,
)
: t(...uiMessages.tasktimeline_progress_cdc159)}
</small>
<pre>{step.text}</pre>
</div>
),
)}
</div>
</details>
{active && (
<button
type="button"
className="text-button task-cancel"
disabled={busy}
onClick={() =>
void mutate(
`/api/runs/${encodeURIComponent(run.id)}/cancel`,
{},
)
}
>
{t(...uiMessages.tasktimeline_cancel_68f563)}
</button>
)}
</div>
<AccountAttempts
accounts={accounts}
attempts={accountAttempts?.filter(
(attempt) => attempt.run_id === run.id,
)}
events={runEvents}
active={active}
actionRequired={
lastAccountChange?.kind === "account_action_required" &&
payload(lastAccountChange).run_id === run.id
}
onConfigureAccount={onConfigureAccount}
t={t}
/>
{run.error && (
<p className="inline-error" role="status">
{errorMessage(run.error, t)}
</p>
)}
{answer && (
<div
className="task-answer"
aria-label={t(...uiMessages.tasktimeline_agent_reply_8131a8)}
>
<div className="event-meta">
<span>{agentName}</span>
<span className="task-reply-label">
{t(...uiMessages.tasktimeline_reply_0754fa)}
</span>
</div>
<pre>{answer}</pre>
</div>
)}
</article>
);
})}
<AccountAttempts
accounts={accounts}
events={orphaned}
active={false}
actionRequired={
lastAccountChange?.kind === "account_action_required" &&
!payload(lastAccountChange).run_id
}
onConfigureAccount={onConfigureAccount}
t={t}
/>
{orphaned
.filter(
(e) =>
![
"run_started",
"run_finished",
"token_usage",
"account_attempt",
"account_switched",
"account_changed",
"account_action_required",
"account_waiting",
].includes(e.kind),
)
.map((event) => (
<article className="event" key={event.id}>
<div className="event-meta">
<span>{eventLabel(event.kind, t)}</span>
<DateText date={event.created_at} lang={lang} />
</div>
<pre>{eventDescription(event, t)}</pre>
</article>
))}
</div>
);
}