-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathmapping.ts
More file actions
617 lines (592 loc) · 17.9 KB
/
Copy pathmapping.ts
File metadata and controls
617 lines (592 loc) · 17.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import type {
SessionNotification,
ToolCallContent,
ToolCallLocation,
} from "@agentclientprotocol/sdk";
import { mcpToolKey, posthogToolMeta } from "@posthog/shared";
import { APP_SERVER_NOTIFICATIONS } from "./protocol";
import { readTokenUsage } from "./token-usage";
/**
* Translates a native app-server notification into an ACP SessionNotification.
* Streamed text maps to chunks; tool-like items map to `tool_call`/`tool_call_update`.
* Agent-message and reasoning items are dropped — their deltas already streamed.
*/
export function mapAppServerNotification(
sessionId: string,
method: string,
params: unknown,
): SessionNotification | null {
switch (method) {
case APP_SERVER_NOTIFICATIONS.AGENT_MESSAGE_DELTA: {
const delta = readStringField(params, "delta");
if (!delta) return null;
return {
sessionId,
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: delta },
},
};
}
case APP_SERVER_NOTIFICATIONS.REASONING_TEXT_DELTA:
case APP_SERVER_NOTIFICATIONS.REASONING_SUMMARY_TEXT_DELTA: {
const delta = readStringField(params, "delta");
if (!delta) return null;
return {
sessionId,
update: {
sessionUpdate: "agent_thought_chunk",
content: { type: "text", text: delta },
},
};
}
// Plan-mode proposal streaming as agent prose (codex strips it from agentMessage deltas).
case APP_SERVER_NOTIFICATIONS.PLAN_DELTA: {
const delta = readStringField(params, "delta");
if (!delta) return null;
return {
sessionId,
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: delta },
},
};
}
case APP_SERVER_NOTIFICATIONS.TOKEN_USAGE_UPDATED: {
// Context indicator: renderer reads `used`/`size`; detailed breakdown comes via `_posthog/usage_update`.
const usage = readTokenUsage(params);
if (!usage) return null;
// `usage_update` is a PostHog-convention update, not in the ACP union.
return {
sessionId,
update: {
sessionUpdate: "usage_update",
used: usage.used,
...(usage.size != null ? { size: usage.size } : {}),
},
} as unknown as SessionNotification;
}
case APP_SERVER_NOTIFICATIONS.TURN_PLAN_UPDATED: {
const plan = (
params as { plan?: Array<{ step?: string; status?: string }> }
)?.plan;
if (!Array.isArray(plan)) return null;
return {
sessionId,
update: {
sessionUpdate: "plan",
entries: plan.map((s) => ({
content: s.step ?? "",
priority: "medium",
status: mapPlanStatus(s.status),
})),
},
} as unknown as SessionNotification;
}
case APP_SERVER_NOTIFICATIONS.ITEM_STARTED:
case APP_SERVER_NOTIFICATIONS.ITEM_COMPLETED: {
const item = readItem(params);
if (!item) return null;
return mapItem(
sessionId,
item,
method === APP_SERVER_NOTIFICATIONS.ITEM_COMPLETED,
);
}
case APP_SERVER_NOTIFICATIONS.COMMAND_OUTPUT_DELTA: {
const itemId = readStringField(params, "itemId");
const delta = readStringField(params, "delta");
if (!itemId || !delta) return null;
return toolOutputChunk(sessionId, itemId, delta);
}
case APP_SERVER_NOTIFICATIONS.TERMINAL_INTERACTION: {
const itemId = readStringField(params, "itemId");
const stdin = readStringField(params, "stdin");
if (!itemId || !stdin) return null;
return toolOutputChunk(sessionId, itemId, stdin);
}
case APP_SERVER_NOTIFICATIONS.FILE_CHANGE_PATCH_UPDATED: {
const itemId = readStringField(params, "itemId");
if (!itemId) return null;
const changes = (params as { changes?: AppServerItem["changes"] })
?.changes;
const content = diffContent(changes);
if (!content) return null;
return {
sessionId,
update: {
sessionUpdate: "tool_call_update",
toolCallId: itemId,
status: "in_progress",
content,
},
};
}
default:
return null;
}
}
/** A streamed text chunk on an in-progress tool call; the renderer appends successive single-chunk updates. */
function toolOutputChunk(
sessionId: string,
toolCallId: string,
text: string,
): SessionNotification {
return {
sessionId,
update: {
sessionUpdate: "tool_call_update",
toolCallId,
status: "in_progress",
content: [{ type: "content", content: { type: "text", text } }],
},
};
}
function mapPlanStatus(
status: string | undefined,
): "pending" | "in_progress" | "completed" {
if (status === "inProgress") return "in_progress";
if (status === "completed") return "completed";
return "pending";
}
/**
* Extracts {oldText,newText} from a unified diff so a codex `fileChange` renders as an ACP diff.
* Cosmetic limit: a content line whose payload begins with "-- "/"++ " is misread as a header and dropped.
*/
export function parseUnifiedDiff(diff: string): {
oldText: string;
newText: string;
} {
const oldLines: string[] = [];
const newLines: string[] = [];
for (const line of diff.split("\n")) {
// Skip diff/hunk metadata; match trailing space on ---/+++ so content lines like "++i;" aren't dropped.
if (
line.startsWith("@@") ||
line.startsWith("diff ") ||
line.startsWith("index ") ||
line.startsWith("--- ") ||
line.startsWith("+++ ") ||
line.startsWith("\\ ")
) {
continue;
}
if (line.startsWith("-")) oldLines.push(line.slice(1));
else if (line.startsWith("+")) newLines.push(line.slice(1));
else {
const ctx = line.startsWith(" ") ? line.slice(1) : line;
oldLines.push(ctx);
newLines.push(ctx);
}
}
return { oldText: oldLines.join("\n"), newText: newLines.join("\n") };
}
export type AppServerItem = {
type?: string;
id?: string;
command?: string;
cwd?: string;
commandActions?: Array<{ type?: string; path?: string } | string>;
server?: string;
tool?: string;
namespace?: string | null;
contentItems?: unknown;
query?: string;
status?: string;
arguments?: unknown;
aggregatedOutput?: string | null;
changes?: Array<{ path?: string; diff?: string; kind?: unknown }>;
result?: { content?: unknown } | null;
error?: { message?: string } | null;
// Present on message/reasoning items replayed from thread history.
text?: string;
content?: unknown;
senderThreadId?: string;
receiverThreadIds?: string[];
prompt?: string | null;
model?: string | null;
reasoningEffort?: string | null;
agentsStates?: Record<
string,
{ status?: string; message?: string | null } | undefined
>;
};
function mcpResultText(
result: AppServerItem["result"],
error: AppServerItem["error"],
): string | null {
if (error?.message) return error.message;
const content = result?.content;
if (!Array.isArray(content)) return null;
const text = content
.filter(
(c) =>
c && typeof c === "object" && (c as { type?: string }).type === "text",
)
.map((c) => (c as { text?: string }).text ?? "")
.filter(Boolean)
.join("\n");
return text || null;
}
function dynamicToolText(items: unknown): string | null {
if (!Array.isArray(items)) return null;
const text = items
.filter(
(c) =>
c &&
typeof c === "object" &&
(c as { type?: string }).type === "inputText",
)
.map((c) => (c as { text?: string }).text ?? "")
.filter(Boolean)
.join("\n");
return text || null;
}
/**
* Re-renders a persisted `ThreadItem` as the ACP updates a live stream would have produced,
* so a reattaching host shows the full transcript. Tool items collapse to one completed
* `tool_call`; reasoning is not replayed.
*/
export function mapHistoryItem(
sessionId: string,
item: AppServerItem,
): SessionNotification[] {
switch (item.type) {
case "userMessage":
return userMessageChunks(sessionId, item.content);
case "agentMessage":
return item.text
? [
{
sessionId,
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: item.text },
},
},
]
: [];
case "reasoning":
return [];
// Replay the proposed plan as agent prose so a reattached host still shows it.
case "plan":
return item.text
? [
{
sessionId,
update: {
sessionUpdate: "agent_message_chunk",
content: { type: "text", text: item.text },
},
},
]
: [];
default: {
const tool = describeTool(item);
if (!tool || !item.id) return [];
const content = completedContent(item, tool);
return [
{
sessionId,
update: {
sessionUpdate: "tool_call",
toolCallId: item.id,
title: tool.title,
kind: tool.kind,
status: mapStatus(item.status),
...(tool.rawInput !== undefined ? { rawInput: tool.rawInput } : {}),
...(tool.locations?.length ? { locations: tool.locations } : {}),
...(item.type === "collabAgentToolCall"
? {
_meta: posthogToolMeta({
toolName: collabAgentToolName(item.tool),
}),
}
: tool.mcp
? {
_meta: posthogToolMeta({
toolName: mcpToolKey(tool.mcp),
mcp: tool.mcp,
}),
}
: {}),
...(content ? { content } : {}),
},
},
];
}
}
}
/** Replays a persisted `userMessage`'s text inputs; historical image attachments aren't re-rendered. */
function userMessageChunks(
sessionId: string,
content: unknown,
): SessionNotification[] {
if (!Array.isArray(content)) return [];
const out: SessionNotification[] = [];
for (const block of content) {
if (
block &&
typeof block === "object" &&
(block as { type?: string }).type === "text"
) {
const text = (block as { text?: string }).text;
if (typeof text === "string" && text) {
out.push({
sessionId,
update: {
sessionUpdate: "user_message_chunk",
content: { type: "text", text },
},
});
}
}
}
return out;
}
type ToolDescriptor = {
title: string;
kind: "execute" | "edit" | "fetch" | "other" | "read" | "search";
rawInput?: unknown;
output?: string | null;
locations?: ToolCallLocation[];
/** Originating MCP server + tool, surfaced on `_meta.posthog` so the renderer routes MCP rendering. */
mcp?: { server: string; tool: string };
};
/** Classify a shell command by its actions so read-only commands render as read/search, not execute. */
function commandKind(
actions: AppServerItem["commandActions"],
): "read" | "search" | "execute" {
if (!actions?.length) return "execute";
const types = actions.map((a) => (typeof a === "string" ? a : a?.type));
if (types.every((t) => t === "read")) return "read";
if (types.every((t) => t === "search" || t === "listFiles")) return "search";
return "execute";
}
function describeTool(item: AppServerItem): ToolDescriptor | null {
switch (item.type) {
case "commandExecution":
return {
title: item.command ?? "Run command",
kind: commandKind(item.commandActions),
output: item.aggregatedOutput ?? null,
locations: commandLocations(item),
};
case "fileChange": {
const paths = changePaths(item.changes);
return {
title: fileChangeTitle(paths),
kind: "edit",
locations: paths.map((path) => ({ path })),
};
}
case "mcpToolCall":
return {
title: `${item.server ?? "mcp"}/${item.tool ?? "tool"}`,
kind: "other",
rawInput: item.arguments,
output: mcpResultText(item.result, item.error),
mcp: { server: item.server ?? "mcp", tool: item.tool ?? "tool" },
};
case "dynamicToolCall":
return {
title: item.namespace
? `${item.namespace}/${item.tool ?? "tool"}`
: (item.tool ?? "tool"),
kind: "other",
rawInput: item.arguments,
output: dynamicToolText(item.contentItems),
};
case "collabAgentToolCall":
if (item.tool === "wait" || item.tool === "closeAgent") {
return null;
}
return {
title: collabAgentTitle(item),
kind: "other",
rawInput: {
...(item.prompt ? { prompt: item.prompt } : {}),
...(item.receiverThreadIds?.length
? { receiverThreadIds: item.receiverThreadIds }
: {}),
...(item.model ? { model: item.model } : {}),
...(item.reasoningEffort
? { reasoningEffort: item.reasoningEffort }
: {}),
},
};
case "webSearch":
return { title: item.query ?? "Web search", kind: "fetch" };
default:
return null;
}
}
function collabAgentTitle(item: AppServerItem): string {
switch (item.tool) {
case "spawnAgent":
return item.prompt
? item.prompt.split("\n", 1)[0].slice(0, 120)
: "Spawn subagent";
case "sendInput":
return "Message subagent";
case "resumeAgent":
return "Resume subagent";
case "wait":
return "Wait for subagents";
case "closeAgent":
return "Close subagent";
default:
return "Subagent";
}
}
function collabAgentToolName(tool: string | undefined): string {
switch (tool) {
case "spawnAgent":
return "spawn_agent";
case "sendInput":
return "send_input";
case "resumeAgent":
return "resume_agent";
case "wait":
return "wait_agent";
case "closeAgent":
return "close_agent";
default:
return "subagent";
}
}
/** Distinct, non-empty changed paths for a fileChange item, order-preserved. */
export function changePaths(changes: AppServerItem["changes"]): string[] {
if (!changes?.length) return [];
const seen = new Set<string>();
const paths: string[] = [];
for (const change of changes) {
const path = change?.path;
if (path && !seen.has(path)) {
seen.add(path);
paths.push(path);
}
}
return paths;
}
function fileChangeTitle(paths: string[]): string {
if (!paths.length) return "Edit files";
if (paths.length === 1) return paths[0];
return `${paths[0]} (+${paths.length - 1} more)`;
}
/** Clickable locations for a commandExecution: action paths, else the cwd as a fallback. */
function commandLocations(item: AppServerItem): ToolCallLocation[] | undefined {
const paths: string[] = [];
const seen = new Set<string>();
for (const action of item.commandActions ?? []) {
const path = typeof action === "string" ? undefined : action?.path;
if (path && !seen.has(path)) {
seen.add(path);
paths.push(path);
}
}
if (!paths.length && item.cwd) paths.push(item.cwd);
if (!paths.length) return undefined;
return paths.map((path) => ({ path }));
}
function mapItem(
sessionId: string,
item: AppServerItem,
completed: boolean,
): SessionNotification | null {
const tool = describeTool(item);
if (!tool || !item.id) {
return null;
}
if (!completed) {
return {
sessionId,
update: {
sessionUpdate: "tool_call",
toolCallId: item.id,
title: tool.title,
kind: tool.kind,
status: "in_progress",
...(tool.rawInput !== undefined ? { rawInput: tool.rawInput } : {}),
...(tool.locations?.length ? { locations: tool.locations } : {}),
...(item.type === "collabAgentToolCall"
? {
_meta: posthogToolMeta({
toolName: collabAgentToolName(item.tool),
}),
}
: tool.mcp
? {
_meta: posthogToolMeta({
toolName: mcpToolKey(tool.mcp),
mcp: tool.mcp,
}),
}
: {}),
},
};
}
const content = completedContent(item, tool);
return {
sessionId,
update: {
sessionUpdate: "tool_call_update",
toolCallId: item.id,
status: mapStatus(item.status),
...(content ? { content } : {}),
},
};
}
function completedContent(
item: AppServerItem,
tool: ToolDescriptor,
): ToolCallContent[] | undefined {
if (item.type === "fileChange") {
const diffs = diffContent(item.changes);
if (diffs) return diffs;
}
if (tool.output) {
return [{ type: "content", content: { type: "text", text: tool.output } }];
}
return undefined;
}
/** Maps a fileChange's `changes[]` to ACP `diff` content blocks. */
export function diffContent(
changes: AppServerItem["changes"],
): ToolCallContent[] | undefined {
if (!changes?.length) return undefined;
const diffs = changes
.filter((c) => c?.diff)
.map(
(c) =>
({
type: "diff",
path: c.path,
...parseUnifiedDiff(c.diff ?? ""),
}) as unknown as ToolCallContent,
);
return diffs.length ? diffs : undefined;
}
function mapStatus(
status: string | undefined,
): "completed" | "failed" | "in_progress" {
if (status === "completed") return "completed";
if (status === "failed" || status === "declined") return "failed";
return "in_progress";
}
function readItem(params: unknown): AppServerItem | null {
if (params && typeof params === "object" && "item" in params) {
const item = (params as Record<string, unknown>).item;
if (item && typeof item === "object") {
return item as AppServerItem;
}
}
return null;
}
function readStringField(params: unknown, key: string): string | null {
if (params && typeof params === "object" && key in params) {
const value = (params as Record<string, unknown>)[key];
return typeof value === "string" ? value : null;
}
return null;
}