@agentclientprotocol/claude-agent-acp 0.102.1-preview.6 → 0.102.1-preview.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -64,6 +64,49 @@ export type ReplayChild = {
|
|
|
64
64
|
/** The model id of the latest response in its transcript. */
|
|
65
65
|
responseModel?: string;
|
|
66
66
|
};
|
|
67
|
+
export type SendMessageCall = {
|
|
68
|
+
toolUseId: string;
|
|
69
|
+
to: string;
|
|
70
|
+
message: string;
|
|
71
|
+
senderSessionId: string;
|
|
72
|
+
/** The child the replay delivered the message to. */
|
|
73
|
+
recipientSessionId?: string;
|
|
74
|
+
/** The replay reached the call before the delivery, so it stays a tool call. */
|
|
75
|
+
shownAsToolCall: boolean;
|
|
76
|
+
/** The sender's view of the message went out, at the call. */
|
|
77
|
+
outgoingSent: boolean;
|
|
78
|
+
};
|
|
79
|
+
/** A SubagentHandback call: its report, whether its result failed, and whether the replay sent it. */
|
|
80
|
+
type ReplayHandback = {
|
|
81
|
+
message: string;
|
|
82
|
+
failed: boolean;
|
|
83
|
+
sent: boolean;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* What both replays read from the transcripts before they send anything: the
|
|
87
|
+
* children that launches with metadata started, the launches without metadata
|
|
88
|
+
* (left out), the SendMessage calls, and the SubagentHandback calls. It sends
|
|
89
|
+
* nothing; `RfdSubagentReplay` (v1) and `src/v2/subagent-replay.ts` send the
|
|
90
|
+
* replay from it.
|
|
91
|
+
*/
|
|
92
|
+
export declare class ReplayTranscript<Child extends ReplayChild = ReplayChild> {
|
|
93
|
+
private readonly history;
|
|
94
|
+
/** The children by the id of the tool use that launched each. */
|
|
95
|
+
readonly children: Map<string, Child>;
|
|
96
|
+
readonly byAgentId: Map<string, Child>;
|
|
97
|
+
readonly sendMessages: SendMessageCall[];
|
|
98
|
+
/** Launches without metadata, which the replay leaves out. */
|
|
99
|
+
readonly omitted: Set<string>;
|
|
100
|
+
/** The SubagentHandback calls, by tool use id. */
|
|
101
|
+
readonly handbacks: Map<string, ReplayHandback>;
|
|
102
|
+
constructor(history: SubagentHistory);
|
|
103
|
+
/**
|
|
104
|
+
* Registers the launches, the launch results, and the SendMessage calls in
|
|
105
|
+
* `messages`, the transcript of the session `ownerSessionId`.
|
|
106
|
+
*/
|
|
107
|
+
scan(messages: readonly SessionMessage[], ownerSessionId: string): void;
|
|
108
|
+
private registerLaunch;
|
|
109
|
+
}
|
|
67
110
|
export declare class RfdSubagentReplay {
|
|
68
111
|
private readonly history;
|
|
69
112
|
private readonly publish;
|
|
@@ -76,16 +119,9 @@ export declare class RfdSubagentReplay {
|
|
|
76
119
|
private readonly mapsDuringReplay;
|
|
77
120
|
/** The working directory of the root session, which a child inherits unless it reports its own. */
|
|
78
121
|
private readonly rootCwd?;
|
|
79
|
-
|
|
80
|
-
private readonly children;
|
|
81
|
-
private readonly byAgentId;
|
|
82
|
-
private readonly sendMessages;
|
|
83
|
-
/** Launches without metadata, which the replay leaves out. */
|
|
84
|
-
private readonly omitted;
|
|
122
|
+
private readonly transcript;
|
|
85
123
|
/** Children whose transcript could not be read: only their association is replayed. */
|
|
86
124
|
private withoutHistory;
|
|
87
|
-
/** The SubagentHandback calls, with their report and whether their result failed. */
|
|
88
|
-
private readonly handbacks;
|
|
89
125
|
constructor(history: SubagentHistory, publish: Publish, models?: SubagentModels | undefined,
|
|
90
126
|
/**
|
|
91
127
|
* Whether the replay maps a model to the session's model list as it goes.
|
|
@@ -154,7 +190,7 @@ export declare class RfdSubagentReplay {
|
|
|
154
190
|
*/
|
|
155
191
|
taskNotification(taskId: string, status: string): boolean;
|
|
156
192
|
/**
|
|
157
|
-
* Sends the end of every announced child (see `
|
|
193
|
+
* Sends the end of every announced child (see `replayEndState`), and a notice when
|
|
158
194
|
* subagent history is missing.
|
|
159
195
|
*/
|
|
160
196
|
finish(supportsNotices: boolean, rootSessionId: string): Promise<void>;
|
|
@@ -174,7 +210,6 @@ export declare class RfdSubagentReplay {
|
|
|
174
210
|
* RFD requires after a reconnection.
|
|
175
211
|
*/
|
|
176
212
|
currentStates(live: (sessionId: string) => WorkState["state"] | undefined, liveConfiguration?: (sessionId: string) => SessionConfigOption[] | undefined): AcpSessionNotification[];
|
|
177
|
-
private registerLaunch;
|
|
178
213
|
/** The prompt of the launch, with the launch's id, to the parent and the child, as live. */
|
|
179
214
|
private sendPrompt;
|
|
180
215
|
/**
|
|
@@ -198,6 +233,16 @@ export declare function firstResponseModel(messages: readonly SessionMessage[]):
|
|
|
198
233
|
* names a stop is a cancellation, any other error a failure.
|
|
199
234
|
*/
|
|
200
235
|
export declare function replaySubagentTerminalState(block: Record<string, unknown>): "completed" | "failed" | "cancelled";
|
|
236
|
+
/**
|
|
237
|
+
* How `child`'s work ended: the state of its last task notification, else
|
|
238
|
+
* `cancelled` for a child the user stopped, else the result of its foreground
|
|
239
|
+
* launch, else `idle` with no stop reason.
|
|
240
|
+
*/
|
|
241
|
+
export declare function replayEndState(child: ReplayChild): WorkState;
|
|
242
|
+
/** The text of user message content: a string, or its text blocks. */
|
|
243
|
+
export declare function textOf(content: unknown): string;
|
|
244
|
+
/** Whether `dir` is an existing directory. */
|
|
245
|
+
export declare function isDirectory(dir: string): boolean;
|
|
201
246
|
/** A message that a SendMessage call delivered to the child. */
|
|
202
247
|
export declare function isDelivery(message: SessionMessage): boolean;
|
|
203
248
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subagent-replay.d.ts","sourceRoot":"","sources":["../src/subagent-replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AAC7E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAErE,OAAO,KAAK,EAAE,sBAAsB,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAOL,KAAK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;;;;;OAUG;YACW,YAAY;YAoBZ,WAAW;CAmB1B;AAED,8EAA8E;AAC9E,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,SAAS,cAAc,EAAE,GAAG,MAAM,GAAG,SAAS,CAO1F;AAED;;;GAGG;AACH,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,WAAW,GAAG,QAAQ,GAAG,WAAW,CAMtC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,WAAW,GAAG,SAAS,CAG5D;AA6BD,sEAAsE;AACtE,wBAAgB,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAS/C;AAED,8CAA8C;AAC9C,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAMhD;AAED,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAE3D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,CAAC,SAAS;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,kBAAkB,CAAC,EAAE,MAAM,CAAA;CAAE,EACtE,KAAK,EAAE,SAAS,CAAC,EAAE,EAAE,KAAK,EAAE,SAAS,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS,CAO7E;AAED,iGAAiG;AACjG,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,sBAAsB,CAAC,QAAQ,CAAC,EAAE;IAAE,aAAa,EAAE,QAAQ,CAAA;CAAE,CAAC,CASxE"}
|
package/dist/subagent-replay.js
CHANGED
|
@@ -1,36 +1,25 @@
|
|
|
1
1
|
import { statSync } from "node:fs";
|
|
2
2
|
import { childModelOption, HANDBACK_TOOL, handbackMessage, isNativeSubagentControlTool, launchReply, linkedSessionMessages, nativeSubagentState, responseModelEntry, rfdEndState, rfdLabels, sameDirectory, } from "./native-subagents.js";
|
|
3
3
|
import { noticeUpdate } from "./session-notices.js";
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* What both replays read from the transcripts before they send anything: the
|
|
6
|
+
* children that launches with metadata started, the launches without metadata
|
|
7
|
+
* (left out), the SendMessage calls, and the SubagentHandback calls. It sends
|
|
8
|
+
* nothing; `RfdSubagentReplay` (v1) and `src/v2/subagent-replay.ts` send the
|
|
9
|
+
* replay from it.
|
|
10
|
+
*/
|
|
11
|
+
export class ReplayTranscript {
|
|
5
12
|
history;
|
|
6
|
-
publish;
|
|
7
|
-
models;
|
|
8
|
-
mapsDuringReplay;
|
|
9
|
-
rootCwd;
|
|
10
13
|
/** The children by the id of the tool use that launched each. */
|
|
11
14
|
children = new Map();
|
|
12
15
|
byAgentId = new Map();
|
|
13
16
|
sendMessages = [];
|
|
14
17
|
/** Launches without metadata, which the replay leaves out. */
|
|
15
18
|
omitted = new Set();
|
|
16
|
-
/**
|
|
17
|
-
withoutHistory = 0;
|
|
18
|
-
/** The SubagentHandback calls, with their report and whether their result failed. */
|
|
19
|
+
/** The SubagentHandback calls, by tool use id. */
|
|
19
20
|
handbacks = new Map();
|
|
20
|
-
constructor(history
|
|
21
|
-
/**
|
|
22
|
-
* Whether the replay maps a model to the session's model list as it goes.
|
|
23
|
-
* A load lacks the list until the CLI started, so it maps only in
|
|
24
|
-
* {@link currentStates}, after the load response.
|
|
25
|
-
*/
|
|
26
|
-
mapsDuringReplay = true,
|
|
27
|
-
/** The working directory of the root session, which a child inherits unless it reports its own. */
|
|
28
|
-
rootCwd) {
|
|
21
|
+
constructor(history) {
|
|
29
22
|
this.history = history;
|
|
30
|
-
this.publish = publish;
|
|
31
|
-
this.models = models;
|
|
32
|
-
this.mapsDuringReplay = mapsDuringReplay;
|
|
33
|
-
this.rootCwd = rootCwd;
|
|
34
23
|
}
|
|
35
24
|
/**
|
|
36
25
|
* Registers the launches, the launch results, and the SendMessage calls in
|
|
@@ -75,9 +64,63 @@ export class RfdSubagentReplay {
|
|
|
75
64
|
}
|
|
76
65
|
}
|
|
77
66
|
}
|
|
67
|
+
registerLaunch(toolUseId, input, parentSessionId) {
|
|
68
|
+
if (this.children.has(toolUseId))
|
|
69
|
+
return;
|
|
70
|
+
const record = this.history.agents.get(toolUseId);
|
|
71
|
+
if (!record) {
|
|
72
|
+
this.omitted.add(toolUseId);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
// A child type adds only optional fields, which a new child leaves unset.
|
|
76
|
+
const child = {
|
|
77
|
+
launchId: toolUseId,
|
|
78
|
+
sessionId: record.agentId,
|
|
79
|
+
record,
|
|
80
|
+
parentSessionId,
|
|
81
|
+
input,
|
|
82
|
+
announced: false,
|
|
83
|
+
promptSent: false,
|
|
84
|
+
};
|
|
85
|
+
this.children.set(toolUseId, child);
|
|
86
|
+
this.byAgentId.set(record.agentId, child);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export class RfdSubagentReplay {
|
|
90
|
+
history;
|
|
91
|
+
publish;
|
|
92
|
+
models;
|
|
93
|
+
mapsDuringReplay;
|
|
94
|
+
rootCwd;
|
|
95
|
+
transcript;
|
|
96
|
+
/** Children whose transcript could not be read: only their association is replayed. */
|
|
97
|
+
withoutHistory = 0;
|
|
98
|
+
constructor(history, publish, models,
|
|
99
|
+
/**
|
|
100
|
+
* Whether the replay maps a model to the session's model list as it goes.
|
|
101
|
+
* A load lacks the list until the CLI started, so it maps only in
|
|
102
|
+
* {@link currentStates}, after the load response.
|
|
103
|
+
*/
|
|
104
|
+
mapsDuringReplay = true,
|
|
105
|
+
/** The working directory of the root session, which a child inherits unless it reports its own. */
|
|
106
|
+
rootCwd) {
|
|
107
|
+
this.history = history;
|
|
108
|
+
this.publish = publish;
|
|
109
|
+
this.models = models;
|
|
110
|
+
this.mapsDuringReplay = mapsDuringReplay;
|
|
111
|
+
this.rootCwd = rootCwd;
|
|
112
|
+
this.transcript = new ReplayTranscript(history);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Registers the launches, the launch results, and the SendMessage calls in
|
|
116
|
+
* `messages`, the transcript of the session `ownerSessionId`.
|
|
117
|
+
*/
|
|
118
|
+
scan(messages, ownerSessionId) {
|
|
119
|
+
this.transcript.scan(messages, ownerSessionId);
|
|
120
|
+
}
|
|
78
121
|
/** The child that the tool use `toolUseId` launched, when the replay shows it. */
|
|
79
122
|
childOfLaunch(toolUseId) {
|
|
80
|
-
return this.children.get(toolUseId);
|
|
123
|
+
return this.transcript.children.get(toolUseId);
|
|
81
124
|
}
|
|
82
125
|
/**
|
|
83
126
|
* Announces `child` on its parent, as live: running, with the parent's
|
|
@@ -115,12 +158,12 @@ export class RfdSubagentReplay {
|
|
|
115
158
|
*/
|
|
116
159
|
effectiveCwd(sessionId) {
|
|
117
160
|
const seen = new Set();
|
|
118
|
-
let child = this.byAgentId.get(sessionId);
|
|
161
|
+
let child = this.transcript.byAgentId.get(sessionId);
|
|
119
162
|
while (child && !seen.has(child.sessionId)) {
|
|
120
163
|
if (child.cwd)
|
|
121
164
|
return child.cwd;
|
|
122
165
|
seen.add(child.sessionId);
|
|
123
|
-
child = this.byAgentId.get(child.parentSessionId);
|
|
166
|
+
child = this.transcript.byAgentId.get(child.parentSessionId);
|
|
124
167
|
}
|
|
125
168
|
return this.rootCwd;
|
|
126
169
|
}
|
|
@@ -196,7 +239,7 @@ export class RfdSubagentReplay {
|
|
|
196
239
|
if (update.sessionUpdate !== "tool_call" && update.sessionUpdate !== "tool_call_update") {
|
|
197
240
|
return false;
|
|
198
241
|
}
|
|
199
|
-
const sendMessage = this.sendMessages.find((call) => call.toolUseId === update.toolCallId);
|
|
242
|
+
const sendMessage = this.transcript.sendMessages.find((call) => call.toolUseId === update.toolCallId);
|
|
200
243
|
if (sendMessage) {
|
|
201
244
|
if (sendMessage.recipientSessionId === undefined) {
|
|
202
245
|
sendMessage.shownAsToolCall = true;
|
|
@@ -210,7 +253,7 @@ export class RfdSubagentReplay {
|
|
|
210
253
|
}
|
|
211
254
|
return true;
|
|
212
255
|
}
|
|
213
|
-
const handback = owner ? this.handbacks.get(update.toolCallId) : undefined;
|
|
256
|
+
const handback = owner ? this.transcript.handbacks.get(update.toolCallId) : undefined;
|
|
214
257
|
if (owner && handback && !handback.failed) {
|
|
215
258
|
if (!handback.sent) {
|
|
216
259
|
handback.sent = true;
|
|
@@ -221,7 +264,7 @@ export class RfdSubagentReplay {
|
|
|
221
264
|
}
|
|
222
265
|
return true;
|
|
223
266
|
}
|
|
224
|
-
const child = this.children.get(update.toolCallId);
|
|
267
|
+
const child = this.transcript.children.get(update.toolCallId);
|
|
225
268
|
if (!child)
|
|
226
269
|
return false;
|
|
227
270
|
if (update.status === "completed" &&
|
|
@@ -239,18 +282,18 @@ export class RfdSubagentReplay {
|
|
|
239
282
|
* its task id is the agent id.
|
|
240
283
|
*/
|
|
241
284
|
taskNotification(taskId, status) {
|
|
242
|
-
const child = this.byAgentId.get(taskId);
|
|
285
|
+
const child = this.transcript.byAgentId.get(taskId);
|
|
243
286
|
if (!child)
|
|
244
287
|
return false;
|
|
245
288
|
child.notified = nativeSubagentState(status) ?? child.notified;
|
|
246
289
|
return true;
|
|
247
290
|
}
|
|
248
291
|
/**
|
|
249
|
-
* Sends the end of every announced child (see `
|
|
292
|
+
* Sends the end of every announced child (see `replayEndState`), and a notice when
|
|
250
293
|
* subagent history is missing.
|
|
251
294
|
*/
|
|
252
295
|
async finish(supportsNotices, rootSessionId) {
|
|
253
|
-
for (const child of [...this.children.values()].reverse()) {
|
|
296
|
+
for (const child of [...this.transcript.children.values()].reverse()) {
|
|
254
297
|
if (!child.announced)
|
|
255
298
|
continue;
|
|
256
299
|
await this.publish({
|
|
@@ -258,11 +301,11 @@ export class RfdSubagentReplay {
|
|
|
258
301
|
update: {
|
|
259
302
|
sessionUpdate: "subagent_update",
|
|
260
303
|
sessionId: child.sessionId,
|
|
261
|
-
state:
|
|
304
|
+
state: replayEndState(child),
|
|
262
305
|
},
|
|
263
306
|
});
|
|
264
307
|
}
|
|
265
|
-
const missing = this.omitted.size + this.withoutHistory;
|
|
308
|
+
const missing = this.transcript.omitted.size + this.withoutHistory;
|
|
266
309
|
if (missing > 0 && supportsNotices) {
|
|
267
310
|
await this.publish({ sessionId: rootSessionId, update: missingHistoryNotice(missing) });
|
|
268
311
|
}
|
|
@@ -283,11 +326,13 @@ export class RfdSubagentReplay {
|
|
|
283
326
|
* RFD requires after a reconnection.
|
|
284
327
|
*/
|
|
285
328
|
currentStates(live, liveConfiguration) {
|
|
286
|
-
return [...this.children.values()]
|
|
329
|
+
return [...this.transcript.children.values()]
|
|
287
330
|
.filter((child) => child.announced)
|
|
288
331
|
.flatMap((child) => {
|
|
289
332
|
const liveState = live(child.sessionId);
|
|
290
|
-
const state = liveState === undefined || liveState === "idle"
|
|
333
|
+
const state = liveState === undefined || liveState === "idle"
|
|
334
|
+
? replayEndState(child)
|
|
335
|
+
: { state: liveState };
|
|
291
336
|
// The session's model list may be known only now, after the load response.
|
|
292
337
|
const entry = responseModelEntry(child.responseModel, this.models);
|
|
293
338
|
const replayed = child.model ? JSON.stringify([childModelOption(child.model)]) : undefined;
|
|
@@ -316,26 +361,6 @@ export class RfdSubagentReplay {
|
|
|
316
361
|
];
|
|
317
362
|
});
|
|
318
363
|
}
|
|
319
|
-
registerLaunch(toolUseId, input, parentSessionId) {
|
|
320
|
-
if (this.children.has(toolUseId))
|
|
321
|
-
return;
|
|
322
|
-
const record = this.history.agents.get(toolUseId);
|
|
323
|
-
if (!record) {
|
|
324
|
-
this.omitted.add(toolUseId);
|
|
325
|
-
return;
|
|
326
|
-
}
|
|
327
|
-
const child = {
|
|
328
|
-
launchId: toolUseId,
|
|
329
|
-
sessionId: record.agentId,
|
|
330
|
-
record,
|
|
331
|
-
parentSessionId,
|
|
332
|
-
input,
|
|
333
|
-
announced: false,
|
|
334
|
-
promptSent: false,
|
|
335
|
-
};
|
|
336
|
-
this.children.set(toolUseId, child);
|
|
337
|
-
this.byAgentId.set(record.agentId, child);
|
|
338
|
-
}
|
|
339
364
|
/** The prompt of the launch, with the launch's id, to the parent and the child, as live. */
|
|
340
365
|
async sendPrompt(child, text) {
|
|
341
366
|
child.promptSent = true;
|
|
@@ -355,7 +380,7 @@ export class RfdSubagentReplay {
|
|
|
355
380
|
* call (see {@link toolCallUpdate}).
|
|
356
381
|
*/
|
|
357
382
|
async sendDelivery(child, message, text) {
|
|
358
|
-
const call = deliveredCall(this.sendMessages, [child.sessionId, child.record.name, child.input.name], text);
|
|
383
|
+
const call = deliveredCall(this.transcript.sendMessages, [child.sessionId, child.record.name, child.input.name], text);
|
|
359
384
|
if (call) {
|
|
360
385
|
call.recipientSessionId = child.sessionId;
|
|
361
386
|
await this.sendMessage(child, call.toolUseId, call.message, call.senderSessionId, {
|
|
@@ -408,7 +433,7 @@ export function replaySubagentTerminalState(block) {
|
|
|
408
433
|
* `cancelled` for a child the user stopped, else the result of its foreground
|
|
409
434
|
* launch, else `idle` with no stop reason.
|
|
410
435
|
*/
|
|
411
|
-
function
|
|
436
|
+
export function replayEndState(child) {
|
|
412
437
|
const end = child.notified ?? (child.record.stoppedByUser ? "cancelled" : child.launchEnd);
|
|
413
438
|
return end ? rfdEndState(end, undefined) : { state: "idle" };
|
|
414
439
|
}
|
|
@@ -438,7 +463,7 @@ function inputOf(block) {
|
|
|
438
463
|
: {};
|
|
439
464
|
}
|
|
440
465
|
/** The text of user message content: a string, or its text blocks. */
|
|
441
|
-
function textOf(content) {
|
|
466
|
+
export function textOf(content) {
|
|
442
467
|
if (typeof content === "string")
|
|
443
468
|
return content;
|
|
444
469
|
if (!Array.isArray(content))
|
|
@@ -449,7 +474,7 @@ function textOf(content) {
|
|
|
449
474
|
.join("\n");
|
|
450
475
|
}
|
|
451
476
|
/** Whether `dir` is an existing directory. */
|
|
452
|
-
function isDirectory(dir) {
|
|
477
|
+
export function isDirectory(dir) {
|
|
453
478
|
try {
|
|
454
479
|
return statSync(dir).isDirectory();
|
|
455
480
|
}
|
|
@@ -1,3 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The replay of a session's subagents, for `session/resume` with
|
|
3
|
+
* `replayFrom`, as the v1 core replays them (`RfdSubagentReplay`).
|
|
4
|
+
*
|
|
5
|
+
* Claude Code keeps each subagent's history in its own transcript, and its
|
|
6
|
+
* metadata file names the Agent or Task call that launched it (see
|
|
7
|
+
* `subagentHistory`). The replay announces each child right after that call,
|
|
8
|
+
* under its live session id (the agent id), with no capabilities: it cannot
|
|
9
|
+
* run again in this process. Then it sends the child's `model` (of its first
|
|
10
|
+
* response) and its history to the child session; a later response with
|
|
11
|
+
* another model sends it again. As live, the parent is not shown the launch
|
|
12
|
+
* call: its prompt is the parent's outgoing message, and a foreground child's
|
|
13
|
+
* answer its reply. A child's successful SubagentHandback call is its message
|
|
14
|
+
* to the parent instead. Unlike live, the replay does not tell runs apart, so
|
|
15
|
+
* a child that handed back any report gets no reply from its launch's result.
|
|
16
|
+
*
|
|
17
|
+
* A SendMessage call whose message the replay delivered to a child before it
|
|
18
|
+
* reached the call is that message too: the sender's outgoing view, at the
|
|
19
|
+
* call, and the child's incoming one, in the child's history. A child's
|
|
20
|
+
* history follows its launch, so a call to a child from its parent is always
|
|
21
|
+
* delivered first. Any other SendMessage call stays a tool call.
|
|
22
|
+
*
|
|
23
|
+
* Every child's end goes out on its parent at the end of the replay, and once
|
|
24
|
+
* on the child's own stream: v2 forbids replaying a child's past `running` as
|
|
25
|
+
* current activity. These states are history to the client, so each child's
|
|
26
|
+
* current state goes out again after the response ({@link afterResponse}).
|
|
27
|
+
* A child that the live session still tracks, on a resume of the active
|
|
28
|
+
* session, gets its current state from the resume snapshot instead
|
|
29
|
+
* (`SessionTasks.currentState`). After the response, a child whose
|
|
30
|
+
* configuration is not the `model` that the replay sent, such as a live child
|
|
31
|
+
* whose hooks reported its mode, gets it again.
|
|
32
|
+
*
|
|
33
|
+
* A launch without metadata has no known id, so it is not announced: the RFD
|
|
34
|
+
* forbids inventing one. Its Agent call stays an ordinary tool call of the
|
|
35
|
+
* parent, and a notice at the end says that subagent history is missing, as
|
|
36
|
+
* it does for a child whose transcript cannot be read.
|
|
37
|
+
*/
|
|
1
38
|
import type * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
2
39
|
import type { SessionConfigOption } from "@agentclientprotocol/sdk/unstable";
|
|
3
40
|
import type { SessionMessage } from "@anthropic-ai/claude-agent-sdk";
|
|
@@ -28,13 +65,7 @@ export declare class SubagentReplay {
|
|
|
28
65
|
/** The transcript of each child, by agent id. */
|
|
29
66
|
private readonly transcripts;
|
|
30
67
|
private readonly live;
|
|
31
|
-
|
|
32
|
-
private readonly children;
|
|
33
|
-
private readonly sendMessages;
|
|
34
|
-
/** The SubagentHandback calls, with their report and whether their result failed. */
|
|
35
|
-
private readonly handbacks;
|
|
36
|
-
/** Launches without metadata, which the replay leaves out. */
|
|
37
|
-
private readonly omitted;
|
|
68
|
+
private readonly transcript;
|
|
38
69
|
/** Children whose transcript could not be read: only their association is replayed. */
|
|
39
70
|
private withoutHistory;
|
|
40
71
|
private rootOutput;
|
|
@@ -68,8 +99,6 @@ export declare class SubagentReplay {
|
|
|
68
99
|
* Registers the launches, the launch results, the SendMessage calls and the
|
|
69
100
|
* SubagentHandback calls in `messages`, of the session `ownerSessionId`.
|
|
70
101
|
*/
|
|
71
|
-
private scan;
|
|
72
|
-
private registerLaunch;
|
|
73
102
|
/**
|
|
74
103
|
* A replayed update of a tool call of `output`, the session of `owner`
|
|
75
104
|
* when it is a child's. The client is not shown the launch of a replayed
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subagent-replay.d.ts","sourceRoot":"","sources":["../../src/v2/subagent-replay.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"subagent-replay.d.ts","sourceRoot":"","sources":["../../src/v2/subagent-replay.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG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OAAO,CAAC,UAAU,CAA4B;IAE9C,OAAO;IAUP;;;OAGG;WACU,IAAI,CACf,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,cAAc,EAAE,EAC1B,IAAI,GAAE,YAA+B,GACpC,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC;IAiBtC,0EAA0E;IAC1E,KAAK,CAAC,MAAM,EAAE,aAAa,EAAE,QAAQ,EAAE,cAAc,EAAE,GAAG,WAAW;IAMrE;;;OAGG;IACH,MAAM,IAAI,IAAI;IA0Bd;;;;;;;;OAQG;IACH,aAAa,IAAI,IAAI;IAQrB,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,YAAY;IAiBpB,OAAO,CAAC,UAAU;IAWlB;;;OAGG;IACH;;;;;;;OAOG;IACH,OAAO,CAAC,KAAK;IAyDb;;;;OAIG;IACH,OAAO,CAAC,SAAS;IAcjB,0FAA0F;IAC1F,OAAO,CAAC,QAAQ;IAiBhB,OAAO,CAAC,QAAQ;IAiChB,qFAAqF;IACrF,OAAO,CAAC,SAAS;IAcjB;;;;;;;OAOG;IACH,OAAO,CAAC,YAAY;IAgBpB;;;;;;;OAOG;IACH,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,UAAU;IAYlB,OAAO,CAAC,QAAQ;IAMhB,OAAO,CAAC,KAAK;CAKd"}
|
|
@@ -1,44 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
* The replay of a session's subagents, for `session/resume` with
|
|
3
|
-
* `replayFrom`, as the v1 core replays them (`RfdSubagentReplay`).
|
|
4
|
-
*
|
|
5
|
-
* Claude Code keeps each subagent's history in its own transcript, and its
|
|
6
|
-
* metadata file names the Agent or Task call that launched it (see
|
|
7
|
-
* `subagentHistory`). The replay announces each child right after that call,
|
|
8
|
-
* under its live session id (the agent id), with no capabilities: it cannot
|
|
9
|
-
* run again in this process. Then it sends the child's `model` (of its first
|
|
10
|
-
* response) and its history to the child session; a later response with
|
|
11
|
-
* another model sends it again. As live, the parent is not shown the launch
|
|
12
|
-
* call: its prompt is the parent's outgoing message, and a foreground child's
|
|
13
|
-
* answer its reply. A child's successful SubagentHandback call is its message
|
|
14
|
-
* to the parent instead. Unlike live, the replay does not tell runs apart, so
|
|
15
|
-
* a child that handed back any report gets no reply from its launch's result.
|
|
16
|
-
*
|
|
17
|
-
* A SendMessage call whose message the replay delivered to a child before it
|
|
18
|
-
* reached the call is that message too: the sender's outgoing view, at the
|
|
19
|
-
* call, and the child's incoming one, in the child's history. A child's
|
|
20
|
-
* history follows its launch, so a call to a child from its parent is always
|
|
21
|
-
* delivered first. Any other SendMessage call stays a tool call.
|
|
22
|
-
*
|
|
23
|
-
* Every child's end goes out on its parent at the end of the replay, and once
|
|
24
|
-
* on the child's own stream: v2 forbids replaying a child's past `running` as
|
|
25
|
-
* current activity. These states are history to the client, so each child's
|
|
26
|
-
* current state goes out again after the response ({@link afterResponse}).
|
|
27
|
-
* A child that the live session still tracks, on a resume of the active
|
|
28
|
-
* session, gets its current state from the resume snapshot instead
|
|
29
|
-
* (`SessionTasks.currentState`). After the response, a child whose
|
|
30
|
-
* configuration is not the `model` that the replay sent, such as a live child
|
|
31
|
-
* whose hooks reported its mode, gets it again.
|
|
32
|
-
*
|
|
33
|
-
* A launch without metadata has no known id, so it is not announced: the RFD
|
|
34
|
-
* forbids inventing one. Its Agent call stays an ordinary tool call of the
|
|
35
|
-
* parent, and a notice at the end says that subagent history is missing, as
|
|
36
|
-
* it does for a child whose transcript cannot be read.
|
|
37
|
-
*/
|
|
38
|
-
import * as fs from "node:fs";
|
|
39
|
-
import { childModelOption, HANDBACK_TOOL, handbackMessage, isNativeSubagentControlTool, launchReply, linkedSessionMessages, nativeSubagentState, responseModelEntry, rfdEndState, rfdLabels, } from "../native-subagents.js";
|
|
1
|
+
import { childModelOption, launchReply, linkedSessionMessages, nativeSubagentState, responseModelEntry, rfdLabels, } from "../native-subagents.js";
|
|
40
2
|
import { readSubagentMessages, subagentHistory, subagentLaunchIds, } from "../subagent-history.js";
|
|
41
|
-
import { deliveredCall, firstResponseModel, isDelivery, missingHistoryNotice,
|
|
3
|
+
import { deliveredCall, firstResponseModel, isDelivery, isDirectory, missingHistoryNotice, ReplayTranscript, replayEndState, textOf, } from "../subagent-replay.js";
|
|
42
4
|
import { taskNotificationsOf } from "../task-notifications.js";
|
|
43
5
|
import { SessionOutput } from "./stream.js";
|
|
44
6
|
import { sameDirectory } from "./tasks.js";
|
|
@@ -48,13 +10,7 @@ export class SubagentReplay {
|
|
|
48
10
|
history;
|
|
49
11
|
transcripts;
|
|
50
12
|
live;
|
|
51
|
-
|
|
52
|
-
children = new Map();
|
|
53
|
-
sendMessages = [];
|
|
54
|
-
/** The SubagentHandback calls, with their report and whether their result failed. */
|
|
55
|
-
handbacks = new Map();
|
|
56
|
-
/** Launches without metadata, which the replay leaves out. */
|
|
57
|
-
omitted = new Set();
|
|
13
|
+
transcript;
|
|
58
14
|
/** Children whose transcript could not be read: only their association is replayed. */
|
|
59
15
|
withoutHistory = 0;
|
|
60
16
|
rootOutput;
|
|
@@ -65,6 +21,7 @@ export class SubagentReplay {
|
|
|
65
21
|
this.history = history;
|
|
66
22
|
this.transcripts = transcripts;
|
|
67
23
|
this.live = live;
|
|
24
|
+
this.transcript = new ReplayTranscript(history);
|
|
68
25
|
}
|
|
69
26
|
/**
|
|
70
27
|
* Reads the subagent history that `messages`, the session's transcript,
|
|
@@ -89,7 +46,7 @@ export class SubagentReplay {
|
|
|
89
46
|
/** The hooks of the replay of the root session's history (`messages`). */
|
|
90
47
|
hooks(output, messages) {
|
|
91
48
|
this.rootOutput = output;
|
|
92
|
-
this.scan(messages, output.sessionId);
|
|
49
|
+
this.transcript.scan(messages, output.sessionId);
|
|
93
50
|
return this.ownerHooks(output, undefined);
|
|
94
51
|
}
|
|
95
52
|
/**
|
|
@@ -97,24 +54,24 @@ export class SubagentReplay {
|
|
|
97
54
|
* stream, and the notice when subagent history is missing.
|
|
98
55
|
*/
|
|
99
56
|
finish() {
|
|
100
|
-
const announced = [...this.children.values()].filter((child) => child.announced);
|
|
57
|
+
const announced = [...this.transcript.children.values()].filter((child) => child.announced);
|
|
101
58
|
for (const child of [...announced].reverse()) {
|
|
102
59
|
this.host.send({
|
|
103
60
|
sessionId: child.parentSessionId,
|
|
104
61
|
update: {
|
|
105
62
|
sessionUpdate: "subagent_update",
|
|
106
63
|
sessionId: child.sessionId,
|
|
107
|
-
state:
|
|
64
|
+
state: replayEndState(child),
|
|
108
65
|
},
|
|
109
66
|
});
|
|
110
67
|
}
|
|
111
68
|
for (const child of announced) {
|
|
112
69
|
this.host.send({
|
|
113
70
|
sessionId: child.sessionId,
|
|
114
|
-
update: { sessionUpdate: "state_update", ...
|
|
71
|
+
update: { sessionUpdate: "state_update", ...replayEndState(child) },
|
|
115
72
|
});
|
|
116
73
|
}
|
|
117
|
-
const missing = this.omitted.size + this.withoutHistory;
|
|
74
|
+
const missing = this.transcript.omitted.size + this.withoutHistory;
|
|
118
75
|
if (missing > 0) {
|
|
119
76
|
this.rootOutput?.emitV1([
|
|
120
77
|
{ sessionId: this.host.sessionId, update: missingHistoryNotice(missing) },
|
|
@@ -131,7 +88,7 @@ export class SubagentReplay {
|
|
|
131
88
|
* the session's model list.
|
|
132
89
|
*/
|
|
133
90
|
afterResponse() {
|
|
134
|
-
for (const child of this.children.values()) {
|
|
91
|
+
for (const child of this.transcript.children.values()) {
|
|
135
92
|
if (!child.announced)
|
|
136
93
|
continue;
|
|
137
94
|
if (!this.live.tracks(child.sessionId))
|
|
@@ -153,7 +110,7 @@ export class SubagentReplay {
|
|
|
153
110
|
]);
|
|
154
111
|
}
|
|
155
112
|
sendEndState(child) {
|
|
156
|
-
const state =
|
|
113
|
+
const state = replayEndState(child);
|
|
157
114
|
this.host.send({
|
|
158
115
|
sessionId: child.parentSessionId,
|
|
159
116
|
update: {
|
|
@@ -182,62 +139,6 @@ export class SubagentReplay {
|
|
|
182
139
|
* Registers the launches, the launch results, the SendMessage calls and the
|
|
183
140
|
* SubagentHandback calls in `messages`, of the session `ownerSessionId`.
|
|
184
141
|
*/
|
|
185
|
-
scan(messages, ownerSessionId) {
|
|
186
|
-
for (const message of messages) {
|
|
187
|
-
for (const block of contentBlocks(message)) {
|
|
188
|
-
if (block.type === "tool_use" && typeof block.id === "string") {
|
|
189
|
-
if (isNativeSubagentControlTool(block.name)) {
|
|
190
|
-
this.registerLaunch(block.id, inputOf(block), ownerSessionId);
|
|
191
|
-
}
|
|
192
|
-
else if (block.name === HANDBACK_TOOL) {
|
|
193
|
-
const report = handbackMessage(block.input);
|
|
194
|
-
if (report)
|
|
195
|
-
this.handbacks.set(block.id, { message: report, failed: false, sent: false });
|
|
196
|
-
}
|
|
197
|
-
else if (block.name === "SendMessage") {
|
|
198
|
-
const input = inputOf(block);
|
|
199
|
-
if (typeof input.to === "string" && typeof input.message === "string") {
|
|
200
|
-
this.sendMessages.push({
|
|
201
|
-
toolUseId: block.id,
|
|
202
|
-
to: input.to,
|
|
203
|
-
message: input.message,
|
|
204
|
-
senderSessionId: ownerSessionId,
|
|
205
|
-
shownAsToolCall: false,
|
|
206
|
-
outgoingSent: false,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
else if (block.type === "tool_result" && typeof block.tool_use_id === "string") {
|
|
212
|
-
const handback = this.handbacks.get(block.tool_use_id);
|
|
213
|
-
if (handback && block.is_error === true)
|
|
214
|
-
handback.failed = true;
|
|
215
|
-
const child = this.children.get(block.tool_use_id);
|
|
216
|
-
// A background launch answers at once; its result is not the end.
|
|
217
|
-
if (child && !child.record.background)
|
|
218
|
-
child.launchEnd = replaySubagentTerminalState(block);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
registerLaunch(launchId, input, parentSessionId) {
|
|
224
|
-
if (this.children.has(launchId))
|
|
225
|
-
return;
|
|
226
|
-
const record = this.history.agents.get(launchId);
|
|
227
|
-
if (!record) {
|
|
228
|
-
this.omitted.add(launchId);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
this.children.set(launchId, {
|
|
232
|
-
launchId,
|
|
233
|
-
sessionId: record.agentId,
|
|
234
|
-
record,
|
|
235
|
-
parentSessionId,
|
|
236
|
-
input,
|
|
237
|
-
announced: false,
|
|
238
|
-
promptSent: false,
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
142
|
/**
|
|
242
143
|
* A replayed update of a tool call of `output`, the session of `owner`
|
|
243
144
|
* when it is a child's. The client is not shown the launch of a replayed
|
|
@@ -251,7 +152,7 @@ export class SubagentReplay {
|
|
|
251
152
|
if (update.sessionUpdate !== "tool_call" && update.sessionUpdate !== "tool_call_update") {
|
|
252
153
|
return [notification];
|
|
253
154
|
}
|
|
254
|
-
const sendMessage = this.sendMessages.find((call) => call.toolUseId === update.toolCallId);
|
|
155
|
+
const sendMessage = this.transcript.sendMessages.find((call) => call.toolUseId === update.toolCallId);
|
|
255
156
|
if (sendMessage) {
|
|
256
157
|
if (sendMessage.recipientSessionId === undefined) {
|
|
257
158
|
sendMessage.shownAsToolCall = true;
|
|
@@ -263,7 +164,7 @@ export class SubagentReplay {
|
|
|
263
164
|
const [outgoing] = linkedSessionMessages(sendMessage.toolUseId, sendMessage.senderSessionId, sendMessage.recipientSessionId, sendMessage.message);
|
|
264
165
|
return [outgoing];
|
|
265
166
|
}
|
|
266
|
-
const handback = owner ? this.handbacks.get(update.toolCallId) : undefined;
|
|
167
|
+
const handback = owner ? this.transcript.handbacks.get(update.toolCallId) : undefined;
|
|
267
168
|
if (owner && handback && !handback.failed) {
|
|
268
169
|
if (handback.sent)
|
|
269
170
|
return [];
|
|
@@ -275,7 +176,7 @@ export class SubagentReplay {
|
|
|
275
176
|
this.outputOf(owner.parentSessionId)?.emitV1([incoming]);
|
|
276
177
|
return [];
|
|
277
178
|
}
|
|
278
|
-
const child = this.children.get(update.toolCallId);
|
|
179
|
+
const child = this.transcript.children.get(update.toolCallId);
|
|
279
180
|
if (!child)
|
|
280
181
|
return [notification];
|
|
281
182
|
if (update.status === "completed" &&
|
|
@@ -301,7 +202,7 @@ export class SubagentReplay {
|
|
|
301
202
|
return;
|
|
302
203
|
}
|
|
303
204
|
for (const notification of taskNotificationsOf(inner?.content)) {
|
|
304
|
-
const child = [...this.children.values()].find((c) => c.sessionId === notification.task_id);
|
|
205
|
+
const child = [...this.transcript.children.values()].find((c) => c.sessionId === notification.task_id);
|
|
305
206
|
if (child)
|
|
306
207
|
child.notified = nativeSubagentState(notification.status) ?? child.notified;
|
|
307
208
|
}
|
|
@@ -311,7 +212,7 @@ export class SubagentReplay {
|
|
|
311
212
|
if (message.type !== "assistant")
|
|
312
213
|
return;
|
|
313
214
|
for (const launchId of launchIdsOf(message)) {
|
|
314
|
-
const child = this.children.get(launchId);
|
|
215
|
+
const child = this.transcript.children.get(launchId);
|
|
315
216
|
if (!child || child.announced)
|
|
316
217
|
continue;
|
|
317
218
|
const messages = this.transcripts.get(child.sessionId) ?? [];
|
|
@@ -319,7 +220,7 @@ export class SubagentReplay {
|
|
|
319
220
|
this.withoutHistory++;
|
|
320
221
|
this.announce(child);
|
|
321
222
|
this.sendModel(child, firstResponseModel(messages));
|
|
322
|
-
this.scan(messages, child.sessionId);
|
|
223
|
+
this.transcript.scan(messages, child.sessionId);
|
|
323
224
|
child.output.replay(messages, this.ownerHooks(child.output, child));
|
|
324
225
|
if (!child.promptSent && typeof child.input.prompt === "string") {
|
|
325
226
|
this.sendPrompt(child, parent, child.input.prompt);
|
|
@@ -396,7 +297,7 @@ export class SubagentReplay {
|
|
|
396
297
|
* the delivered text.
|
|
397
298
|
*/
|
|
398
299
|
sendDelivery(child, message, text) {
|
|
399
|
-
const call = deliveredCall(this.sendMessages, [child.sessionId, child.record.name, child.input.name], text);
|
|
300
|
+
const call = deliveredCall(this.transcript.sendMessages, [child.sessionId, child.record.name, child.input.name], text);
|
|
400
301
|
if (call)
|
|
401
302
|
call.recipientSessionId = child.sessionId;
|
|
402
303
|
const messageId = call?.toolUseId ?? message.uuid;
|
|
@@ -424,56 +325,18 @@ export class SubagentReplay {
|
|
|
424
325
|
outputOf(sessionId) {
|
|
425
326
|
if (sessionId === this.host.sessionId)
|
|
426
327
|
return this.rootOutput;
|
|
427
|
-
return [...this.children.values()].find((child) => child.sessionId === sessionId)
|
|
328
|
+
return [...this.transcript.children.values()].find((child) => child.sessionId === sessionId)
|
|
329
|
+
?.output;
|
|
428
330
|
}
|
|
429
331
|
cwdOf(sessionId) {
|
|
430
|
-
const child = [...this.children.values()].find((c) => c.sessionId === sessionId);
|
|
332
|
+
const child = [...this.transcript.children.values()].find((c) => c.sessionId === sessionId);
|
|
431
333
|
if (!child)
|
|
432
334
|
return this.host.cwd;
|
|
433
335
|
return child.cwd ?? this.cwdOf(child.parentSessionId);
|
|
434
336
|
}
|
|
435
337
|
}
|
|
436
|
-
/**
|
|
437
|
-
* How a replayed child's work ended: the state of its last task
|
|
438
|
-
* notification, else a stop by the user, else its foreground launch's result.
|
|
439
|
-
*/
|
|
440
|
-
function endState(child) {
|
|
441
|
-
const end = child.notified ??
|
|
442
|
-
nativeSubagentState(child.record.stoppedByUser ? "cancelled" : child.launchEnd);
|
|
443
|
-
return end ? rfdEndState(end, undefined) : { state: "idle" };
|
|
444
|
-
}
|
|
445
338
|
function launchIdsOf(message) {
|
|
446
339
|
if (message.type !== "assistant")
|
|
447
340
|
return [];
|
|
448
341
|
return subagentLaunchIds(message.message?.content);
|
|
449
342
|
}
|
|
450
|
-
function contentBlocks(message) {
|
|
451
|
-
const content = message.message?.content;
|
|
452
|
-
return Array.isArray(content)
|
|
453
|
-
? content.filter((block) => typeof block === "object" && block !== null)
|
|
454
|
-
: [];
|
|
455
|
-
}
|
|
456
|
-
function inputOf(block) {
|
|
457
|
-
return typeof block.input === "object" && block.input !== null
|
|
458
|
-
? block.input
|
|
459
|
-
: {};
|
|
460
|
-
}
|
|
461
|
-
/** The text of user message content: a string, or its text blocks. */
|
|
462
|
-
function textOf(content) {
|
|
463
|
-
if (typeof content === "string")
|
|
464
|
-
return content;
|
|
465
|
-
if (!Array.isArray(content))
|
|
466
|
-
return "";
|
|
467
|
-
return content
|
|
468
|
-
.flatMap((block) => typeof block === "object" && block !== null && block.type === "text" ? [block.text] : [])
|
|
469
|
-
.filter((text) => typeof text === "string")
|
|
470
|
-
.join("\n");
|
|
471
|
-
}
|
|
472
|
-
function isDirectory(dir) {
|
|
473
|
-
try {
|
|
474
|
-
return fs.statSync(dir).isDirectory();
|
|
475
|
-
}
|
|
476
|
-
catch {
|
|
477
|
-
return false;
|
|
478
|
-
}
|
|
479
|
-
}
|
package/package.json
CHANGED