@agentclientprotocol/claude-agent-acp 0.114.1-preview.1 → 0.114.1-preview.2
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.
- package/dist/resumed-session.d.ts.map +1 -1
- package/dist/resumed-session.js +8 -2
- package/dist/session-read.d.ts.map +1 -1
- package/dist/session-read.js +6 -4
- package/dist/subagent-history.d.ts.map +1 -1
- package/dist/subagent-history.js +42 -5
- package/dist/transcript-branches.d.ts +44 -0
- package/dist/transcript-branches.d.ts.map +1 -0
- package/dist/transcript-branches.js +228 -0
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resumed-session.d.ts","sourceRoot":"","sources":["../src/resumed-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,cAAc,EAEnB,KAAK,iBAAiB,EACvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAyB,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"resumed-session.d.ts","sourceRoot":"","sources":["../src/resumed-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,cAAc,EAEnB,KAAK,iBAAiB,EACvB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAyB,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAW1E,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IAClC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG;IACnC,QAAQ,CAAC,EAAE,cAAc,EAAE,CAAC;IAC5B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF;;;iDAGiD;AACjD,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,cAAc,EAAE,GAAG,MAAM,GAAG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iB,EAAE,CAAC,CAgB9B"}
|
package/dist/resumed-session.js
CHANGED
|
@@ -5,6 +5,7 @@ import { isCompactSummaryMessage } from "./context-compaction.js";
|
|
|
5
5
|
import { projectsRoot } from "./paths.js";
|
|
6
6
|
import { errorCode } from "./session-index/project-dirs.js";
|
|
7
7
|
import { SessionTiming } from "./session-timing.js";
|
|
8
|
+
import { withReplyBranches } from "./transcript-branches.js";
|
|
8
9
|
/** The size of one backward read of a transcript. */
|
|
9
10
|
const TAIL_CHUNK_BYTES = 64 * 1024;
|
|
10
11
|
/** Return the concrete model recorded by the last real assistant response.
|
|
@@ -82,10 +83,15 @@ export async function readHistoryAcrossCompactions(sessionId, messages, logger,
|
|
|
82
83
|
return messages;
|
|
83
84
|
try {
|
|
84
85
|
const entries = (snapshot ?? (await loadFullSessionHistory(sessionId, dir))).map(linkCompactBoundary);
|
|
85
|
-
const
|
|
86
|
+
const read = (records) => getSessionMessages(sessionId, {
|
|
86
87
|
dir,
|
|
87
|
-
sessionStore: { append: async () => { }, load: async () =>
|
|
88
|
+
sessionStore: { append: async () => { }, load: async () => records },
|
|
88
89
|
});
|
|
90
|
+
// `_session/read` gives `messages` with the branches of each model reply,
|
|
91
|
+
// and passes its records: the history before it gets them too.
|
|
92
|
+
const history = snapshot
|
|
93
|
+
? await withReplyBranches(entries, await read(entries), read)
|
|
94
|
+
: await read(entries);
|
|
89
95
|
const leaf = messages.at(-1)?.uuid;
|
|
90
96
|
const end = history.findIndex((message) => message.uuid === leaf);
|
|
91
97
|
if (end < 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-read.d.ts","sourceRoot":"","sources":["../src/session-read.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"session-read.d.ts","sourceRoot":"","sources":["../src/session-read.ts"],"names":[],"mappings":"AAyBA,eAAO,MAAM,mBAAmB,kBAAkB,CAAC;AACnD,eAAO,MAAM,uBAAuB;;CAA+B,CAAC;AAEpE,MAAM,MAAM,g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YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA+C;IAEtE,OAAO,CAAC,QAAQ,CAAC,WAAW,CAA6B;IAEzD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAiC;IAE7D,wEAAwE;IAClE,UAAU,CAAC,CAAC,EAChB,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,IAAI,EAAE,MAAM,GAAG,OAAO,EACtB,GAAG,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACpB,OAAO,CAAC,CAAC,CAAC;IAkBP,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAoKlF"}
|
package/dist/session-read.js
CHANGED
|
@@ -11,6 +11,7 @@ import { LastReportedEntity } from "./session-cursors.js";
|
|
|
11
11
|
import { readHeadTailOf, readRecentCustomTitle, readSidecarTitle, } from "./session-index/transcript-scan.js";
|
|
12
12
|
import { sanitizeTitle } from "./session-titles.js";
|
|
13
13
|
import { activeSessionMessages, readRewindPoint } from "./session-rewind.js";
|
|
14
|
+
import { withReplyBranches } from "./transcript-branches.js";
|
|
14
15
|
export const SESSION_READ_METHOD = "_session/read";
|
|
15
16
|
export const SESSION_READ_CAPABILITY = { supported: true };
|
|
16
17
|
const request = z.object({
|
|
@@ -107,11 +108,12 @@ export async function readSessionTranscript(sessionId, cwd, observeStorage = fal
|
|
|
107
108
|
lastRecordedTitle(records, "ai-title", "aiTitle"));
|
|
108
109
|
const dir = cwd ?? records.find((r) => typeof r.cwd === "string" && path.isAbsolute(r.cwd))?.cwd;
|
|
109
110
|
const effectiveCwd = typeof dir === "string" ? dir : process.cwd();
|
|
110
|
-
const
|
|
111
|
+
const read = (entries) => getSessionMessages(sessionId, {
|
|
111
112
|
dir: effectiveCwd,
|
|
112
|
-
sessionStore: { append: async () => { }, load: async () =>
|
|
113
|
-
};
|
|
114
|
-
|
|
113
|
+
sessionStore: { append: async () => { }, load: async () => entries },
|
|
114
|
+
});
|
|
115
|
+
// A poll must see the earlier history as a prefix (see `withReplyBranches`).
|
|
116
|
+
const chain = await withReplyBranches(records, await read(records), read);
|
|
115
117
|
const rewind = await readRewindPoint(sessionId, undefined, { transcript });
|
|
116
118
|
const retained = activeSessionMessages(chain, rewind);
|
|
117
119
|
const messages = await readHistoryAcrossCompactions(sessionId, retained, undefined, effectiveCwd, records);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subagent-history.d.ts","sourceRoot":"","sources":["../src/subagent-history.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,cAAc,
|
|
1
|
+
{"version":3,"file":"subagent-history.d.ts","sourceRoot":"","sources":["../src/subagent-history.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,cAAc,EAEpB,MAAM,gCAAgC,CAAC;AAKxC,OAAO,EAA+B,KAAK,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAI7F,0DAA0D;AAC1D,MAAM,WAAW,cAAc;IAC7B,oDAAoD;IACpD,OAAO,EAAE,MAAM,CAAC;IAChB,8CAA8C;IAC9C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,uDAAuD;IACvD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,iDAAiD;IACjD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,uDAAuD;IACvD,UAAU,EAAE,OAAO,CAAC;IACpB,mCAAmC;IACnC,aAAa,EAAE,OAAO,CAAC;IACvB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,uDAAuD;AACvD,MAAM,WAAW,eAAe;IAC9B,qEAAqE;IACrE,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACpC;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,gEAAgE;IAChE,SAAS,CAAC,EAAE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-5,6 +5,7 @@ import path from "node:path";
|
|
|
5
5
|
import readline from "node:readline";
|
|
6
6
|
import { isNativeSubagentControlTool } from "./native-subagents.js";
|
|
7
7
|
import { findTranscript, readTranscriptEntries } from "./resumed-session.js";
|
|
8
|
+
import { withReplyBranches } from "./transcript-branches.js";
|
|
8
9
|
/**
|
|
9
10
|
* Finds the subagent transcripts of a session.
|
|
10
11
|
*
|
|
@@ -248,16 +249,53 @@ export async function readSubagentMessages(sessionId, agentId, history, logger)
|
|
|
248
249
|
try {
|
|
249
250
|
if (history.dir) {
|
|
250
251
|
const messages = await getSubagentMessages(sessionId, agentId, { dir: history.dir });
|
|
251
|
-
if (messages.length > 0)
|
|
252
|
-
return stripForkPrefix(messages);
|
|
252
|
+
if (messages.length > 0) {
|
|
253
|
+
return stripForkPrefix(await withSubagentBranches(sessionId, agentId, history, messages));
|
|
254
|
+
}
|
|
253
255
|
}
|
|
254
|
-
|
|
256
|
+
const messages = await getSubagentMessages(sessionId, agentId);
|
|
257
|
+
return stripForkPrefix(await withSubagentBranches(sessionId, agentId, history, messages));
|
|
255
258
|
}
|
|
256
259
|
catch (error) {
|
|
257
260
|
logger.error(`Failed to read the history of subagent ${agentId}:`, error);
|
|
258
261
|
return [];
|
|
259
262
|
}
|
|
260
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* The chain of the subagent `agentId` in its transcript records `entries`,
|
|
266
|
+
* with the branches of its model replies (see {@link withReplyBranches}).
|
|
267
|
+
*/
|
|
268
|
+
async function readSubagentChain(sessionId, agentId, entries) {
|
|
269
|
+
const read = (records) => getSubagentMessages(sessionId, agentId, {
|
|
270
|
+
sessionStore: { append: async () => { }, load: async () => records },
|
|
271
|
+
});
|
|
272
|
+
return withReplyBranches(entries, await read(entries), read);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* `messages`, the chain that the SDK read from the files of the subagent
|
|
276
|
+
* `agentId`, with the branches of its model replies. It is `messages` when
|
|
277
|
+
* the transcript in `history.directory` cannot be read.
|
|
278
|
+
*/
|
|
279
|
+
async function withSubagentBranches(sessionId, agentId, history, messages) {
|
|
280
|
+
if (messages.length === 0 || !history.directory)
|
|
281
|
+
return messages;
|
|
282
|
+
let entries;
|
|
283
|
+
try {
|
|
284
|
+
entries = await readTranscriptEntries(path.join(history.directory, `agent-${agentId}.jsonl`));
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return messages;
|
|
288
|
+
}
|
|
289
|
+
const read = async (records) => (await getSubagentMessages(sessionId, agentId, {
|
|
290
|
+
sessionStore: { append: async () => { }, load: async () => records },
|
|
291
|
+
})).map((message) => ({
|
|
292
|
+
...message,
|
|
293
|
+
// The SDK reads these fields from agent-*.meta.json only with its filesystem store.
|
|
294
|
+
parent_tool_use_id: messages[0].parent_tool_use_id,
|
|
295
|
+
parent_agent_id: messages[0].parent_agent_id,
|
|
296
|
+
}));
|
|
297
|
+
return withReplyBranches(entries, messages, read);
|
|
298
|
+
}
|
|
261
299
|
/** Whether a filesystem error means a file does not exist. */
|
|
262
300
|
function isMissing(error) {
|
|
263
301
|
return error?.code === "ENOENT";
|
|
@@ -276,8 +314,7 @@ export async function readSubagentMessagesOrFail(sessionId, agentId, history, la
|
|
|
276
314
|
const file = path.join(history.directory, `agent-${agentId}.jsonl`);
|
|
277
315
|
try {
|
|
278
316
|
const entries = await readTranscriptEntries(file);
|
|
279
|
-
const
|
|
280
|
-
const messages = await getSubagentMessages(sessionId, agentId, { sessionStore });
|
|
317
|
+
const messages = await readSubagentChain(sessionId, agentId, entries);
|
|
281
318
|
// The SDK reads these fields from agent-*.meta.json only with its filesystem store.
|
|
282
319
|
const parent = history.agents.get(launchId)?.parentAgentId ?? null;
|
|
283
320
|
return stripForkPrefix(messages.map((message) => ({
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SessionMessage, SessionStoreEntry } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
+
/**
|
|
3
|
+
* The history of a transcript with each record of a model reply's branches,
|
|
4
|
+
* in file order. `chain` is what the SDK read from `entries`, and `read`
|
|
5
|
+
* reads the history from other entries in the same way.
|
|
6
|
+
*
|
|
7
|
+
* Claude Code writes each tool_use block of one model reply as its own
|
|
8
|
+
* record, and parents the result of each call on the record of that call. A
|
|
9
|
+
* record that comes with a result, such as the text of a Skill, has that
|
|
10
|
+
* result as parent. So parallel calls make one branch per call. The SDK reads
|
|
11
|
+
* one chain, from the last record back by `parentUuid`, and adds some of the
|
|
12
|
+
* other records of the reply after the last chain record of the reply. So a
|
|
13
|
+
* new record can move a record of the reply or drop it from the history:
|
|
14
|
+
*
|
|
15
|
+
* - a third tool_use of the reply, with the second result as parent, moves
|
|
16
|
+
* the first result after it;
|
|
17
|
+
* - the second result drops a record whose parent is the first result.
|
|
18
|
+
*
|
|
19
|
+
* The earlier history is then no prefix of the later one, and a record that
|
|
20
|
+
* a client got is gone. Here each reply of the chain keeps every record that
|
|
21
|
+
* grows from its records, up to the next reply, in file order, as Claude
|
|
22
|
+
* Code appended them. A record whose branch leads to another reply is an
|
|
23
|
+
* other history, such as the prompt before a rewind, and stays out. A
|
|
24
|
+
* record that Claude Code wrote after the next record of the chain, or after
|
|
25
|
+
* the last record of the chain, stays out too: it would go before a record
|
|
26
|
+
* that a client already got. A later read adds a record of the second kind
|
|
27
|
+
* when a record after it comes.
|
|
28
|
+
*
|
|
29
|
+
* The SDK still converts and selects the records: they go back to `read`
|
|
30
|
+
* with the `parentUuid` of each record of a changed reply set to the record
|
|
31
|
+
* before it in file order. The result is `chain` when no reply changes, or
|
|
32
|
+
* when `read` drops a message of `chain` or gives another order.
|
|
33
|
+
*/
|
|
34
|
+
export declare function withReplyBranches(entries: readonly SessionStoreEntry[], chain: SessionMessage[], read: (entries: SessionStoreEntry[]) => Promise<SessionMessage[]>): Promise<SessionMessage[]>;
|
|
35
|
+
/**
|
|
36
|
+
* The record order of {@link withReplyBranches} for the SDK chain `chain`
|
|
37
|
+
* (its uuids), and the entries to read it from. Undefined when the chain
|
|
38
|
+
* already has that order.
|
|
39
|
+
*/
|
|
40
|
+
export declare function replyBranchOrder(entries: readonly SessionStoreEntry[], chain: readonly string[]): {
|
|
41
|
+
order: string[];
|
|
42
|
+
entries: SessionStoreEntry[];
|
|
43
|
+
} | undefined;
|
|
44
|
+
//# sourceMappingURL=transcript-branches.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcript-branches.d.ts","sourceRoot":"","sources":["../src/transcript-branches.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAWxF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,SAAS,iBAAiB,EAAE,EACrC,KAAK,EAAE,cAAc,EAAE,EACvB,IAAI,EAAE,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,OAAO,CAAC,cAAc,EAAE,CAAC,GAChE,OAAO,CAAC,cAAc,EAAE,CAAC,CAkB3B;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,SAAS,iBAAiB,EAAE,EACrC,KAAK,EAAE,SAAS,MAAM,EAAE,GACvB;IAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,iBAAiB,EAAE,CAAA;CAAE,GAAG,SAAS,CAuI/D"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The history of a transcript with each record of a model reply's branches,
|
|
3
|
+
* in file order. `chain` is what the SDK read from `entries`, and `read`
|
|
4
|
+
* reads the history from other entries in the same way.
|
|
5
|
+
*
|
|
6
|
+
* Claude Code writes each tool_use block of one model reply as its own
|
|
7
|
+
* record, and parents the result of each call on the record of that call. A
|
|
8
|
+
* record that comes with a result, such as the text of a Skill, has that
|
|
9
|
+
* result as parent. So parallel calls make one branch per call. The SDK reads
|
|
10
|
+
* one chain, from the last record back by `parentUuid`, and adds some of the
|
|
11
|
+
* other records of the reply after the last chain record of the reply. So a
|
|
12
|
+
* new record can move a record of the reply or drop it from the history:
|
|
13
|
+
*
|
|
14
|
+
* - a third tool_use of the reply, with the second result as parent, moves
|
|
15
|
+
* the first result after it;
|
|
16
|
+
* - the second result drops a record whose parent is the first result.
|
|
17
|
+
*
|
|
18
|
+
* The earlier history is then no prefix of the later one, and a record that
|
|
19
|
+
* a client got is gone. Here each reply of the chain keeps every record that
|
|
20
|
+
* grows from its records, up to the next reply, in file order, as Claude
|
|
21
|
+
* Code appended them. A record whose branch leads to another reply is an
|
|
22
|
+
* other history, such as the prompt before a rewind, and stays out. A
|
|
23
|
+
* record that Claude Code wrote after the next record of the chain, or after
|
|
24
|
+
* the last record of the chain, stays out too: it would go before a record
|
|
25
|
+
* that a client already got. A later read adds a record of the second kind
|
|
26
|
+
* when a record after it comes.
|
|
27
|
+
*
|
|
28
|
+
* The SDK still converts and selects the records: they go back to `read`
|
|
29
|
+
* with the `parentUuid` of each record of a changed reply set to the record
|
|
30
|
+
* before it in file order. The result is `chain` when no reply changes, or
|
|
31
|
+
* when `read` drops a message of `chain` or gives another order.
|
|
32
|
+
*/
|
|
33
|
+
export async function withReplyBranches(entries, chain, read) {
|
|
34
|
+
const plan = replyBranchOrder(entries, chain.map((message) => message.uuid));
|
|
35
|
+
if (!plan)
|
|
36
|
+
return chain;
|
|
37
|
+
const history = await read(plan.entries);
|
|
38
|
+
// The SDK leaves out some records, such as a meta prompt of the main thread.
|
|
39
|
+
// The result must keep each message of `chain`, in the planned order.
|
|
40
|
+
const kept = new Set(history.map((message) => message.uuid));
|
|
41
|
+
if (chain.some((message) => !kept.has(message.uuid)))
|
|
42
|
+
return chain;
|
|
43
|
+
let at = 0;
|
|
44
|
+
for (const message of history) {
|
|
45
|
+
while (at < plan.order.length && plan.order[at] !== message.uuid)
|
|
46
|
+
at++;
|
|
47
|
+
if (at === plan.order.length)
|
|
48
|
+
return chain;
|
|
49
|
+
at++;
|
|
50
|
+
}
|
|
51
|
+
return history;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The record order of {@link withReplyBranches} for the SDK chain `chain`
|
|
55
|
+
* (its uuids), and the entries to read it from. Undefined when the chain
|
|
56
|
+
* already has that order.
|
|
57
|
+
*/
|
|
58
|
+
export function replyBranchOrder(entries, chain) {
|
|
59
|
+
const records = entries;
|
|
60
|
+
const byUuid = new Map();
|
|
61
|
+
const position = new Map();
|
|
62
|
+
const children = new Map();
|
|
63
|
+
const replies = new Map();
|
|
64
|
+
records.forEach((record, index) => {
|
|
65
|
+
if (typeof record.uuid !== "string")
|
|
66
|
+
return;
|
|
67
|
+
if (!byUuid.has(record.uuid) && typeof record.parentUuid === "string") {
|
|
68
|
+
const siblings = children.get(record.parentUuid);
|
|
69
|
+
if (siblings)
|
|
70
|
+
siblings.push(record.uuid);
|
|
71
|
+
else
|
|
72
|
+
children.set(record.parentUuid, [record.uuid]);
|
|
73
|
+
}
|
|
74
|
+
if (!byUuid.has(record.uuid)) {
|
|
75
|
+
const id = replyId(record);
|
|
76
|
+
if (id !== undefined) {
|
|
77
|
+
const members = replies.get(id);
|
|
78
|
+
if (members)
|
|
79
|
+
members.push(record.uuid);
|
|
80
|
+
else
|
|
81
|
+
replies.set(id, [record.uuid]);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
byUuid.set(record.uuid, record);
|
|
85
|
+
if (!position.has(record.uuid))
|
|
86
|
+
position.set(record.uuid, index);
|
|
87
|
+
});
|
|
88
|
+
const inChain = new Set(chain);
|
|
89
|
+
const order = [];
|
|
90
|
+
/** The records of each changed reply, of every type, in file order. */
|
|
91
|
+
const relinked = [];
|
|
92
|
+
const done = new Set();
|
|
93
|
+
for (let at = 0; at < chain.length;) {
|
|
94
|
+
const record = byUuid.get(chain[at]);
|
|
95
|
+
const id = record && replyId(record);
|
|
96
|
+
if (!record || id === undefined || done.has(id)) {
|
|
97
|
+
order.push(chain[at]);
|
|
98
|
+
at++;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
done.add(id);
|
|
102
|
+
const region = growth(record, id);
|
|
103
|
+
let end = at;
|
|
104
|
+
while (end < chain.length && region.has(chain[end]))
|
|
105
|
+
end++;
|
|
106
|
+
const run = chain.slice(at, end);
|
|
107
|
+
// A record that Claude Code wrote after the next record of the chain would
|
|
108
|
+
// go before a record that a client already got, so it stays out. So does a
|
|
109
|
+
// record after the last one of the chain: the SDK ends the history at the
|
|
110
|
+
// record that it chose, and a later read adds the record after it.
|
|
111
|
+
// The SDK can move an earlier record of the run to its end, so the newest
|
|
112
|
+
// record of the run sets the limit, not the last one.
|
|
113
|
+
const limit = end < chain.length
|
|
114
|
+
? position.get(chain[end])
|
|
115
|
+
: Math.max(...run.map((uuid) => position.get(uuid))) + 1;
|
|
116
|
+
const sorted = [...region]
|
|
117
|
+
.filter((uuid) => inChain.has(uuid) || position.get(uuid) < limit)
|
|
118
|
+
.sort((a, b) => position.get(a) - position.get(b));
|
|
119
|
+
const messages = sorted.filter((uuid) => isMessage(byUuid.get(uuid)));
|
|
120
|
+
// A record of the reply that the chain has at another place: keep the SDK order.
|
|
121
|
+
const inRun = new Set(run);
|
|
122
|
+
const inMessages = new Set(messages);
|
|
123
|
+
if (messages.some((uuid) => inChain.has(uuid) && !inRun.has(uuid)))
|
|
124
|
+
return undefined;
|
|
125
|
+
if (run.some((uuid) => !inMessages.has(uuid)))
|
|
126
|
+
return undefined;
|
|
127
|
+
order.push(...messages);
|
|
128
|
+
if (messages.length !== run.length || messages.some((uuid, index) => uuid !== run[index])) {
|
|
129
|
+
relinked.push({ region: sorted, next: chain[end] });
|
|
130
|
+
}
|
|
131
|
+
at = end;
|
|
132
|
+
}
|
|
133
|
+
if (relinked.length === 0)
|
|
134
|
+
return undefined;
|
|
135
|
+
const replaced = new Map();
|
|
136
|
+
const relink = (uuid, parentUuid) => {
|
|
137
|
+
const record = byUuid.get(uuid);
|
|
138
|
+
replaced.set(record, { ...(replaced.get(record) ?? record), parentUuid });
|
|
139
|
+
};
|
|
140
|
+
for (const { region, next } of relinked) {
|
|
141
|
+
for (let index = 1; index < region.length; index++)
|
|
142
|
+
relink(region[index], region[index - 1]);
|
|
143
|
+
if (next !== undefined)
|
|
144
|
+
relink(next, region.at(-1));
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
order,
|
|
148
|
+
entries: records.map((record) => replaced.get(record) ?? record),
|
|
149
|
+
};
|
|
150
|
+
/**
|
|
151
|
+
* The records of the reply `id` of `first`, and each record of the same
|
|
152
|
+
* agent that grows from them up to the next reply. A record outside the
|
|
153
|
+
* chain whose branch reaches another reply is left out, with its branch.
|
|
154
|
+
*/
|
|
155
|
+
function growth(first, id) {
|
|
156
|
+
const region = new Set(replies.get(id).filter((uuid) => sameAgent(byUuid.get(uuid), first)));
|
|
157
|
+
const queue = [...region];
|
|
158
|
+
/** Whether the branch of each visited record reaches a record of another reply. */
|
|
159
|
+
const leads = new Map();
|
|
160
|
+
const leadsElsewhere = (start) => {
|
|
161
|
+
// A walk with a stack: a branch can be thousands of records long.
|
|
162
|
+
const stack = [[start, false]];
|
|
163
|
+
const entered = new Set();
|
|
164
|
+
while (stack.length > 0) {
|
|
165
|
+
const [uuid, expanded] = stack.pop();
|
|
166
|
+
if (expanded) {
|
|
167
|
+
leads.set(uuid, (children.get(uuid) ?? []).some((child) => leads.get(child) === true));
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
// A record seen before, or a cycle of `parentUuid` links.
|
|
171
|
+
if (leads.has(uuid) || entered.has(uuid))
|
|
172
|
+
continue;
|
|
173
|
+
entered.add(uuid);
|
|
174
|
+
const other = replyId(byUuid.get(uuid));
|
|
175
|
+
if (other !== undefined && other !== id) {
|
|
176
|
+
leads.set(uuid, true);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
stack.push([uuid, true]);
|
|
180
|
+
for (const child of children.get(uuid) ?? [])
|
|
181
|
+
stack.push([child, false]);
|
|
182
|
+
}
|
|
183
|
+
return leads.get(start) === true;
|
|
184
|
+
};
|
|
185
|
+
while (queue.length > 0) {
|
|
186
|
+
for (const child of children.get(queue.shift()) ?? []) {
|
|
187
|
+
if (region.has(child))
|
|
188
|
+
continue;
|
|
189
|
+
const record = byUuid.get(child);
|
|
190
|
+
const other = replyId(record);
|
|
191
|
+
if (other !== undefined && other !== id)
|
|
192
|
+
continue;
|
|
193
|
+
if (!sameAgent(record, first))
|
|
194
|
+
continue;
|
|
195
|
+
// A prompt outside the chain is a prompt that the user left, such as
|
|
196
|
+
// the prompt before an edit or a rewind.
|
|
197
|
+
if (!inChain.has(child) && (isPrompt(record) || leadsElsewhere(child)))
|
|
198
|
+
continue;
|
|
199
|
+
region.add(child);
|
|
200
|
+
queue.push(child);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return region;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/** The `message.id` of the model reply of an assistant record, or undefined. */
|
|
207
|
+
function replyId(record) {
|
|
208
|
+
if (record.type !== "assistant")
|
|
209
|
+
return undefined;
|
|
210
|
+
const id = record.message?.id;
|
|
211
|
+
return typeof id === "string" ? id : undefined;
|
|
212
|
+
}
|
|
213
|
+
/** Whether `record` is a prompt: a user record with no tool result that is not a meta record. */
|
|
214
|
+
function isPrompt(record) {
|
|
215
|
+
if (record.type !== "user" || record.isMeta === true)
|
|
216
|
+
return false;
|
|
217
|
+
const content = record.message?.content;
|
|
218
|
+
return !(Array.isArray(content) &&
|
|
219
|
+
content.some((block) => block?.type === "tool_result"));
|
|
220
|
+
}
|
|
221
|
+
/** Whether the SDK gives `record` as a message of the history. */
|
|
222
|
+
function isMessage(record) {
|
|
223
|
+
return record.type === "user" || record.type === "assistant";
|
|
224
|
+
}
|
|
225
|
+
/** Whether two records belong to one thread: the main thread or one subagent. */
|
|
226
|
+
function sameAgent(a, b) {
|
|
227
|
+
return (a.isSidechain === true) === (b.isSidechain === true) && a.agentId === b.agentId;
|
|
228
|
+
}
|
package/package.json
CHANGED