@agentclientprotocol/claude-agent-acp 0.113.0 → 0.113.1-preview.10
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/README.md +5 -3
- package/dist/async-tasks.d.ts +27 -0
- package/dist/async-tasks.d.ts.map +1 -1
- package/dist/async-tasks.js +36 -0
- package/dist/claude-cli.d.ts +22 -16
- package/dist/claude-cli.d.ts.map +1 -1
- package/dist/claude-cli.js +40 -18
- package/dist/mcp-oauth.d.ts +3 -2
- package/dist/mcp-oauth.d.ts.map +1 -1
- package/dist/mcp-oauth.js +65 -27
- package/dist/resumed-session.d.ts +9 -2
- package/dist/resumed-session.d.ts.map +1 -1
- package/dist/resumed-session.js +51 -7
- package/dist/sdk-to-acp.js +4 -2
- package/dist/session-cursors.d.ts +1 -1
- package/dist/session-cursors.d.ts.map +1 -1
- package/dist/session-cursors.js +8 -2
- package/dist/session-index/service.d.ts.map +1 -1
- package/dist/session-index/service.js +9 -17
- package/dist/session-index/session-index.d.ts +1 -13
- package/dist/session-index/session-index.d.ts.map +1 -1
- package/dist/session-index/session-index.js +3 -29
- package/dist/session-index/transcript-scan.d.ts +17 -4
- package/dist/session-index/transcript-scan.d.ts.map +1 -1
- package/dist/session-index/transcript-scan.js +44 -3
- package/dist/session-mode.d.ts +38 -1
- package/dist/session-mode.d.ts.map +1 -1
- package/dist/session-mode.js +73 -15
- package/dist/session-read.d.ts +45 -0
- package/dist/session-read.d.ts.map +1 -0
- package/dist/session-read.js +300 -0
- package/dist/session-rewind.d.ts +6 -2
- package/dist/session-rewind.d.ts.map +1 -1
- package/dist/session-rewind.js +13 -4
- package/dist/session-ultracode.d.ts +14 -5
- package/dist/session-ultracode.d.ts.map +1 -1
- package/dist/session-ultracode.js +23 -11
- package/dist/startup-failure.d.ts +11 -1
- package/dist/startup-failure.d.ts.map +1 -1
- package/dist/startup-failure.js +33 -1
- package/dist/subagent-history.d.ts +9 -1
- package/dist/subagent-history.d.ts.map +1 -1
- package/dist/subagent-history.js +55 -13
- package/dist/v1/acp-agent.d.ts +8 -1
- package/dist/v1/acp-agent.d.ts.map +1 -1
- package/dist/v1/acp-agent.js +167 -87
- package/dist/v1/async-task-stop.d.ts +31 -0
- package/dist/v1/async-task-stop.d.ts.map +1 -1
- package/dist/v1/async-task-stop.js +41 -0
- package/dist/v1/session-read.d.ts +31 -0
- package/dist/v1/session-read.d.ts.map +1 -0
- package/dist/v1/session-read.js +62 -0
- package/dist/v2/agent.d.ts.map +1 -1
- package/dist/v2/agent.js +60 -25
- package/dist/v2/commands.d.ts +8 -3
- package/dist/v2/commands.d.ts.map +1 -1
- package/dist/v2/commands.js +4 -3
- package/dist/v2/config.d.ts +3 -3
- package/dist/v2/config.d.ts.map +1 -1
- package/dist/v2/config.js +11 -9
- package/dist/v2/session-read.d.ts +11 -0
- package/dist/v2/session-read.d.ts.map +1 -0
- package/dist/v2/session-read.js +28 -0
- package/dist/v2/session.d.ts.map +1 -1
- package/dist/v2/session.js +19 -2
- package/dist/v2/stream.d.ts +9 -13
- package/dist/v2/stream.d.ts.map +1 -1
- package/dist/v2/stream.js +10 -9
- package/dist/v2/subagent-replay.d.ts +16 -4
- package/dist/v2/subagent-replay.d.ts.map +1 -1
- package/dist/v2/subagent-replay.js +22 -10
- package/dist/v2/tasks.d.ts.map +1 -1
- package/dist/v2/tasks.js +5 -3
- package/package.json +8 -8
package/dist/session-mode.js
CHANGED
|
@@ -70,22 +70,19 @@ export class SessionModeManager {
|
|
|
70
70
|
if (!this.isAvailable(availableModes, effectiveMode)) {
|
|
71
71
|
this.options.logError(`permissions.defaultMode "${effectiveMode}" is not available in ` +
|
|
72
72
|
`this session; falling back to "default".`);
|
|
73
|
-
effectiveMode = "default";
|
|
74
|
-
await this.trySyncMode(query, effectiveMode, "Failed to sync clamped permissionMode to SDK:");
|
|
73
|
+
effectiveMode = await this.trySyncMode(query, availableModes, "default", "Failed to sync clamped permissionMode to SDK:");
|
|
75
74
|
}
|
|
76
75
|
else if (autoUnsupported && builtInDefault) {
|
|
77
76
|
// Like Claude Code: a built-in Auto default the model can't serve starts
|
|
78
77
|
// in Manual, without a warning. Nobody chose Auto, so don't escalate to
|
|
79
78
|
// acceptEdits.
|
|
80
|
-
effectiveMode = "default";
|
|
81
|
-
await this.trySyncMode(query, effectiveMode, "Failed to sync clamped permissionMode to SDK:");
|
|
79
|
+
effectiveMode = await this.trySyncMode(query, availableModes, "default", "Failed to sync clamped permissionMode to SDK:");
|
|
82
80
|
}
|
|
83
81
|
else if (autoUnsupported) {
|
|
84
82
|
this.options.logError(`permissions.defaultMode "auto" is not available for model ` +
|
|
85
83
|
`"${currentModelId}"; falling back to "${AUTO_MODE_FALLBACK}".`);
|
|
86
|
-
effectiveMode = AUTO_MODE_FALLBACK;
|
|
87
84
|
autoModeFallbackWarningPending = true;
|
|
88
|
-
await this.trySyncMode(query,
|
|
85
|
+
effectiveMode = await this.trySyncMode(query, availableModes, AUTO_MODE_FALLBACK, "Failed to sync clamped permissionMode to SDK:");
|
|
89
86
|
}
|
|
90
87
|
return {
|
|
91
88
|
modes: { currentModeId: effectiveMode, availableModes },
|
|
@@ -156,13 +153,13 @@ export class SessionModeManager {
|
|
|
156
153
|
}
|
|
157
154
|
session.modes = {
|
|
158
155
|
availableModes: session.modes.availableModes,
|
|
159
|
-
currentModeId: AUTO_MODE_FALLBACK,
|
|
156
|
+
currentModeId: await this.trySyncMode(session.query, session.modes.availableModes, AUTO_MODE_FALLBACK, `Failed to sync permissionMode to "${AUTO_MODE_FALLBACK}" after model switch invalidated "auto":`),
|
|
160
157
|
};
|
|
161
|
-
await this.trySyncMode(session.query, AUTO_MODE_FALLBACK, `Failed to sync permissionMode to "${AUTO_MODE_FALLBACK}" after model switch invalidated "auto":`);
|
|
162
158
|
return true;
|
|
163
159
|
}
|
|
164
160
|
/**
|
|
165
|
-
* Sets the mode on the query
|
|
161
|
+
* Sets the mode on the query, and returns the mode that is then in effect
|
|
162
|
+
* (see {@link setQueryMode}). `auto` on a model without Auto runs as its
|
|
166
163
|
* fallback, with the warning. With `deferWarning`, the warning waits for
|
|
167
164
|
* the next prompt (see {@link deferFallbackWarning}).
|
|
168
165
|
*/
|
|
@@ -172,9 +169,10 @@ export class SessionModeManager {
|
|
|
172
169
|
if (!this.isAvailable(session.modes.availableModes, requestedMode)) {
|
|
173
170
|
throw RequestError.invalidParams(undefined, `Mode ${modeId} is not available in this session`);
|
|
174
171
|
}
|
|
175
|
-
const
|
|
172
|
+
const askedMode = this.effectiveMode(session, requestedMode);
|
|
173
|
+
let modeInEffect;
|
|
176
174
|
try {
|
|
177
|
-
await
|
|
175
|
+
modeInEffect = await this.setQueryMode(session, askedMode);
|
|
178
176
|
}
|
|
179
177
|
catch (error) {
|
|
180
178
|
if (error instanceof Error) {
|
|
@@ -185,13 +183,38 @@ export class SessionModeManager {
|
|
|
185
183
|
// eslint-disable-next-line preserve-caught-error
|
|
186
184
|
throw new Error("Invalid Mode");
|
|
187
185
|
}
|
|
188
|
-
if (
|
|
186
|
+
if (askedMode !== requestedMode) {
|
|
189
187
|
if (deferWarning)
|
|
190
188
|
this.deferFallbackWarning(session);
|
|
191
189
|
else
|
|
192
190
|
await this.publishFallbackWarning(sessionId, session);
|
|
193
191
|
}
|
|
194
|
-
return
|
|
192
|
+
return modeInEffect;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Sets `mode` on the session's query, and returns the mode that Claude Code
|
|
196
|
+
* names as in effect in its answer (SDK 0.3.332). That is not always the
|
|
197
|
+
* mode asked for, so the session records the answer.
|
|
198
|
+
*
|
|
199
|
+
* Returns `mode` where the answer names no mode, as from a Claude Code
|
|
200
|
+
* before 2.1.332, or names one that the session does not offer: the
|
|
201
|
+
* session's current mode is always one of its `availableModes`. Rejects
|
|
202
|
+
* where Claude Code refuses the mode, e.g. "Cannot set permission mode to
|
|
203
|
+
* auto: auto mode disabled by settings".
|
|
204
|
+
*/
|
|
205
|
+
async setQueryMode(session, mode) {
|
|
206
|
+
return this.namedMode(session.modes.availableModes, mode, await session.query.setPermissionMode(mode));
|
|
207
|
+
}
|
|
208
|
+
/** The mode of Claude Code's `answer` to a request for `asked` (see {@link setQueryMode}). */
|
|
209
|
+
namedMode(availableModes, asked, answer) {
|
|
210
|
+
const named = answer?.mode;
|
|
211
|
+
if (named === undefined || named === asked)
|
|
212
|
+
return asked;
|
|
213
|
+
if (!this.isAvailable(availableModes, named)) {
|
|
214
|
+
this.options.logError(`Claude Code is in permission mode "${named}" after a request for "${asked}", and the session does not offer "${named}"; keeping "${asked}".`);
|
|
215
|
+
return asked;
|
|
216
|
+
}
|
|
217
|
+
return named;
|
|
195
218
|
}
|
|
196
219
|
async setSessionMode(params) {
|
|
197
220
|
const effectiveMode = await this.selectMode(params.sessionId, params.modeId);
|
|
@@ -207,6 +230,36 @@ export class SessionModeManager {
|
|
|
207
230
|
update: { sessionUpdate: "current_mode_update", currentModeId: mode },
|
|
208
231
|
});
|
|
209
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Records the mode that Claude Code reports for `session`, and tells the
|
|
235
|
+
* client. Claude Code reports the mode in the `init` frame at the start of
|
|
236
|
+
* each turn, and in a `status` frame after a change. That covers a change
|
|
237
|
+
* of its own: a session that starts in `auto` while settings disable Auto
|
|
238
|
+
* mode is in `default` a moment later, with a `status` frame or, when the
|
|
239
|
+
* change came before the answer to the initialize request, without one.
|
|
240
|
+
*
|
|
241
|
+
* Changes nothing for the mode already recorded, which is what a frame
|
|
242
|
+
* names after a change that the adapter asked for, and for a mode that the
|
|
243
|
+
* session does not offer, which is logged (see {@link setQueryMode}).
|
|
244
|
+
*
|
|
245
|
+
* Nothing ties a frame to the request that caused it. After two quick
|
|
246
|
+
* changes by the client, the first one's frame can arrive when the second
|
|
247
|
+
* mode is already recorded: the session then shows the first mode until
|
|
248
|
+
* the second frame arrives.
|
|
249
|
+
*/
|
|
250
|
+
async modeReported(sessionId, session, mode) {
|
|
251
|
+
// A frame of a query that the session no longer runs on is not its mode.
|
|
252
|
+
if (this.options.getSession(sessionId) !== session)
|
|
253
|
+
return;
|
|
254
|
+
if (session.modes.currentModeId === mode)
|
|
255
|
+
return;
|
|
256
|
+
if (!this.isAvailable(session.modes.availableModes, mode)) {
|
|
257
|
+
this.options.logError(`Claude Code reports permission mode "${mode}", which the session does not offer; keeping "${session.modes.currentModeId}".`);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
await this.publishCurrent(sessionId, mode);
|
|
261
|
+
await this.options.updateConfigOption(sessionId, MODE_CONFIG_ID, mode);
|
|
262
|
+
}
|
|
210
263
|
async publishFallbackWarning(sessionId, session) {
|
|
211
264
|
session.autoModeFallbackWarningPending = false;
|
|
212
265
|
if (session.autoModeFallbackWarningShown)
|
|
@@ -270,12 +323,17 @@ export class SessionModeManager {
|
|
|
270
323
|
throw RequestError.invalidParams(undefined, `Invalid Mode: ${modeId}`);
|
|
271
324
|
}
|
|
272
325
|
}
|
|
273
|
-
|
|
326
|
+
/**
|
|
327
|
+
* Sets `mode` on the query, and returns the mode then in effect (see
|
|
328
|
+
* {@link setQueryMode}). A failure is logged, and `mode` is returned.
|
|
329
|
+
*/
|
|
330
|
+
async trySyncMode(query, availableModes, mode, errorMessage) {
|
|
274
331
|
try {
|
|
275
|
-
await query.setPermissionMode(mode);
|
|
332
|
+
return this.namedMode(availableModes, mode, await query.setPermissionMode(mode));
|
|
276
333
|
}
|
|
277
334
|
catch (error) {
|
|
278
335
|
this.options.logError(errorMessage, error);
|
|
336
|
+
return mode;
|
|
279
337
|
}
|
|
280
338
|
}
|
|
281
339
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export declare const SESSION_READ_METHOD = "_session/read";
|
|
2
|
+
export declare const SESSION_READ_CAPABILITY: {
|
|
3
|
+
readonly supported: true;
|
|
4
|
+
};
|
|
5
|
+
export type SessionReadFrame = {
|
|
6
|
+
sessionId: string;
|
|
7
|
+
update: {
|
|
8
|
+
sessionUpdate: string;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
};
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
type ReadOptions = {
|
|
14
|
+
protocol: 1 | 2;
|
|
15
|
+
capabilities: unknown;
|
|
16
|
+
cursors: boolean;
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
isOpen(sessionId: string): boolean;
|
|
19
|
+
replay(sessionId: string, cwd: string | undefined): Promise<{
|
|
20
|
+
frames: SessionReadFrame[];
|
|
21
|
+
cwd: string;
|
|
22
|
+
historyEnd?: number;
|
|
23
|
+
title?: string;
|
|
24
|
+
}>;
|
|
25
|
+
send(frame: SessionReadFrame): Promise<void> | void;
|
|
26
|
+
};
|
|
27
|
+
/** The SDK follows parent links in these records, including the branch selected by rewind.json. */
|
|
28
|
+
export declare function readSessionTranscript(sessionId: string, cwd?: string): Promise<{
|
|
29
|
+
messages: import("@anthropic-ai/claude-agent-sdk").SessionMessage[];
|
|
30
|
+
cwd: string;
|
|
31
|
+
transcript: string;
|
|
32
|
+
title: string | undefined;
|
|
33
|
+
}>;
|
|
34
|
+
/** A read owns only a replay on this connection. Closing it invalidates the pending delivery. */
|
|
35
|
+
export declare class SessionReads {
|
|
36
|
+
private readonly active;
|
|
37
|
+
private readonly transitions;
|
|
38
|
+
/** Reserve a session before load, resume, close or delete can yield. */
|
|
39
|
+
transition<T>(sessionId: string | undefined, kind: "open" | "close", run: () => Promise<T>): Promise<T>;
|
|
40
|
+
read(params: unknown, options: ReadOptions): Promise<{
|
|
41
|
+
cursorId?: string;
|
|
42
|
+
}>;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
45
|
+
//# sourceMappingURL=session-read.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-read.d.ts","sourceRoot":"","sources":["../src/session-read.ts"],"names":[],"mappings":"AAsBA,eAAO,MAAM,mBAAmB,kBAAkB,CAAC;AACnD,eAAO,MAAM,uBAAuB;;CAA+B,CAAC;AAEpE,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C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC;QAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CA8GlF"}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { RequestError } from "@agentclientprotocol/sdk/unstable";
|
|
2
|
+
import { getSessionMessages } from "@anthropic-ai/claude-agent-sdk";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { open } from "node:fs/promises";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
8
|
+
import { claudeConfigDir, isSessionId } from "./paths.js";
|
|
9
|
+
import { findTranscript, readHistoryAcrossCompactions, readTranscriptEntriesOf, } from "./resumed-session.js";
|
|
10
|
+
import { LastReportedEntity } from "./session-cursors.js";
|
|
11
|
+
import { readHeadTailOf, readRecentCustomTitle, } from "./session-index/transcript-scan.js";
|
|
12
|
+
import { sanitizeTitle } from "./session-titles.js";
|
|
13
|
+
import { cutAtRewindPoint } from "./session-rewind.js";
|
|
14
|
+
export const SESSION_READ_METHOD = "_session/read";
|
|
15
|
+
export const SESSION_READ_CAPABILITY = { supported: true };
|
|
16
|
+
const request = z.object({
|
|
17
|
+
sessionId: z.string().trim().min(1),
|
|
18
|
+
_meta: z.record(z.string(), z.unknown()).nullish(),
|
|
19
|
+
cwd: z.string().refine(path.isAbsolute, "cwd must be an absolute path").nullish(),
|
|
20
|
+
replayFrom: z
|
|
21
|
+
.discriminatedUnion("type", [
|
|
22
|
+
z.object({ type: z.literal("start") }),
|
|
23
|
+
z.object({ type: z.literal("cursor"), cursorId: z.unknown().optional() }),
|
|
24
|
+
])
|
|
25
|
+
.nullish(),
|
|
26
|
+
});
|
|
27
|
+
/** The SDK follows parent links in these records, including the branch selected by rewind.json. */
|
|
28
|
+
export async function readSessionTranscript(sessionId, cwd) {
|
|
29
|
+
if (!isSessionId(sessionId)) {
|
|
30
|
+
throw RequestError.invalidParams({ sessionId }, "Read the root Claude session by its UUID.");
|
|
31
|
+
}
|
|
32
|
+
const transcript = await findTranscript(sessionId, { strict: true });
|
|
33
|
+
if (!transcript)
|
|
34
|
+
throw RequestError.sessionNotFound(sessionId);
|
|
35
|
+
const handle = await open(transcript, "r");
|
|
36
|
+
let headTail;
|
|
37
|
+
let records;
|
|
38
|
+
try {
|
|
39
|
+
const { size } = await handle.stat();
|
|
40
|
+
headTail = await readHeadTailOf(handle, size);
|
|
41
|
+
records = await readTranscriptEntriesOf(transcript, handle, size);
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
await handle.close();
|
|
45
|
+
}
|
|
46
|
+
const title = records[0]?.isSidechain === true
|
|
47
|
+
? undefined
|
|
48
|
+
: ((await readRecentCustomTitle(transcript, headTail, { strict: true })) ??
|
|
49
|
+
lastRecordedTitle(records, "custom-title", "customTitle") ??
|
|
50
|
+
lastRecordedTitle(records, "ai-title", "aiTitle"));
|
|
51
|
+
const dir = cwd ?? records.find((r) => typeof r.cwd === "string" && path.isAbsolute(r.cwd))?.cwd;
|
|
52
|
+
const effectiveCwd = typeof dir === "string" ? dir : process.cwd();
|
|
53
|
+
const options = {
|
|
54
|
+
dir: effectiveCwd,
|
|
55
|
+
sessionStore: { append: async () => { }, load: async () => records },
|
|
56
|
+
};
|
|
57
|
+
const chain = await getSessionMessages(sessionId, options);
|
|
58
|
+
const retained = await cutAtRewindPoint(sessionId, chain, undefined, { transcript });
|
|
59
|
+
const messages = await readHistoryAcrossCompactions(sessionId, retained, undefined, effectiveCwd, records);
|
|
60
|
+
return {
|
|
61
|
+
messages,
|
|
62
|
+
cwd: effectiveCwd,
|
|
63
|
+
transcript,
|
|
64
|
+
title: title && sanitizeTitle(title),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The `field` of the last `type` record of the snapshot, undefined when it is
|
|
69
|
+
* empty. The SDK reads the head and tail 64 KB windows instead, so a large
|
|
70
|
+
* tool result appended after the last `ai-title` would hide it from the read
|
|
71
|
+
* and change the title of an unchanged session.
|
|
72
|
+
*/
|
|
73
|
+
function lastRecordedTitle(records, type, field) {
|
|
74
|
+
for (let i = records.length - 1; i >= 0; i--) {
|
|
75
|
+
const record = records[i];
|
|
76
|
+
const value = record.type === type ? record[field] : undefined;
|
|
77
|
+
if (typeof value === "string")
|
|
78
|
+
return value || undefined;
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
function invalidCursor() {
|
|
83
|
+
return RequestError.invalidParams({ reason: "invalid_cursor" }, "This cursor cannot continue the replay. Read from start.");
|
|
84
|
+
}
|
|
85
|
+
function alreadyOpen(sessionId) {
|
|
86
|
+
return RequestError.invalidRequest({ reason: "session_already_open", sessionId }, "The session is open or changing on this connection. Retry after it closes.");
|
|
87
|
+
}
|
|
88
|
+
function json(value) {
|
|
89
|
+
return JSON.stringify(value, (_key, member) => member && typeof member === "object" && !Array.isArray(member)
|
|
90
|
+
? Object.fromEntries(Object.keys(member)
|
|
91
|
+
.sort()
|
|
92
|
+
.map((key) => [key, member[key]]))
|
|
93
|
+
: member);
|
|
94
|
+
}
|
|
95
|
+
function digest(value) {
|
|
96
|
+
return createHash("sha256").update(value).digest("base64url");
|
|
97
|
+
}
|
|
98
|
+
// A cursor carries the number of delivered updates per session. Its digest binds
|
|
99
|
+
// those counts to the rendering options, exact history prefixes and post-replay metadata.
|
|
100
|
+
const lengths = z.record(z.string(), z.int().nonnegative());
|
|
101
|
+
function decodeLengths(id) {
|
|
102
|
+
const match = /^read:v1:[\w-]{43}\.([\w-]+)$/.exec(id);
|
|
103
|
+
if (match) {
|
|
104
|
+
try {
|
|
105
|
+
return new Map(Object.entries(lengths.parse(JSON.parse(Buffer.from(match[1], "base64url").toString("utf8")))));
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
/* Invalid and expired cursors have the same recovery. */
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
throw invalidCursor();
|
|
112
|
+
}
|
|
113
|
+
function snapshot(frames, root, protocol, scope, previous, historyEnd = frames.length) {
|
|
114
|
+
const saved = previous ? decodeLengths(previous) : new Map();
|
|
115
|
+
const streams = new Map();
|
|
116
|
+
const entities = new LastReportedEntity();
|
|
117
|
+
const outgoing = [];
|
|
118
|
+
for (const [index, frame] of frames.entries()) {
|
|
119
|
+
const { sessionId, update } = frame;
|
|
120
|
+
// The renderer emits each child's final state on its parent after all messages.
|
|
121
|
+
// A later parent message precedes that same state in the next full replay.
|
|
122
|
+
const boundary = update.sessionUpdate === "replay_complete";
|
|
123
|
+
const key = index < historyEnd || boundary
|
|
124
|
+
? sessionId
|
|
125
|
+
: json([sessionId, update.sessionUpdate, update.sessionId ?? update.subagentSessionId]);
|
|
126
|
+
let stream = streams.get(key);
|
|
127
|
+
if (!stream) {
|
|
128
|
+
stream = {
|
|
129
|
+
count: 0,
|
|
130
|
+
history: createHash("sha256"),
|
|
131
|
+
prefix: digest(""),
|
|
132
|
+
};
|
|
133
|
+
streams.set(key, stream);
|
|
134
|
+
}
|
|
135
|
+
const before = saved.get(key);
|
|
136
|
+
if (boundary) {
|
|
137
|
+
if (before === undefined || sessionId === root || stream.count > before)
|
|
138
|
+
outgoing.push(frame);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
stream.history.update(json(frame) + "\n");
|
|
142
|
+
stream.count++;
|
|
143
|
+
if (stream.count === before)
|
|
144
|
+
stream.prefix = stream.history.copy().digest("base64url");
|
|
145
|
+
if (before === undefined || stream.count > before)
|
|
146
|
+
outgoing.push(frame);
|
|
147
|
+
entities.sending(frame, protocol);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
function cursor(prefix) {
|
|
151
|
+
const counts = new Map();
|
|
152
|
+
const hashes = new Map();
|
|
153
|
+
for (const [id, stream] of streams) {
|
|
154
|
+
const count = prefix ? saved.get(id) : stream.count;
|
|
155
|
+
if (count === undefined)
|
|
156
|
+
continue;
|
|
157
|
+
if (count > stream.count)
|
|
158
|
+
throw invalidCursor();
|
|
159
|
+
counts.set(id, count);
|
|
160
|
+
hashes.set(id, prefix ? stream.prefix : stream.history.copy().digest("base64url"));
|
|
161
|
+
}
|
|
162
|
+
const encoded = Buffer.from(json(Object.fromEntries(counts))).toString("base64url");
|
|
163
|
+
return "read:v1:" + digest(json([scope, encoded, Object.fromEntries(hashes)])) + "." + encoded;
|
|
164
|
+
}
|
|
165
|
+
if (previous && cursor(true) !== previous)
|
|
166
|
+
throw invalidCursor();
|
|
167
|
+
const anchor = entities.after(root);
|
|
168
|
+
if (previous && !anchor)
|
|
169
|
+
throw invalidCursor();
|
|
170
|
+
return { outgoing, anchor, cursorId: anchor ? cursor(false) : undefined };
|
|
171
|
+
}
|
|
172
|
+
/** A read owns only a replay on this connection. Closing it invalidates the pending delivery. */
|
|
173
|
+
export class SessionReads {
|
|
174
|
+
active = new Map();
|
|
175
|
+
transitions = new Map();
|
|
176
|
+
/** Reserve a session before load, resume, close or delete can yield. */
|
|
177
|
+
async transition(sessionId, kind, run) {
|
|
178
|
+
if (sessionId === undefined)
|
|
179
|
+
return run();
|
|
180
|
+
const read = this.active.get(sessionId);
|
|
181
|
+
if (read) {
|
|
182
|
+
if (kind === "open")
|
|
183
|
+
throw alreadyOpen(sessionId);
|
|
184
|
+
read.invalidated = true;
|
|
185
|
+
}
|
|
186
|
+
this.transitions.set(sessionId, (this.transitions.get(sessionId) ?? 0) + 1);
|
|
187
|
+
try {
|
|
188
|
+
return await run();
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
const remaining = this.transitions.get(sessionId) - 1;
|
|
192
|
+
if (remaining === 0)
|
|
193
|
+
this.transitions.delete(sessionId);
|
|
194
|
+
else
|
|
195
|
+
this.transitions.set(sessionId, remaining);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async read(params, options) {
|
|
199
|
+
const parsed = request.safeParse(params);
|
|
200
|
+
if (!parsed.success)
|
|
201
|
+
throw RequestError.invalidParams(undefined, parsed.error.issues[0]?.message);
|
|
202
|
+
const { sessionId, cwd, replayFrom } = parsed.data;
|
|
203
|
+
const previous = replayFrom?.type === "cursor" ? replayFrom.cursorId : undefined;
|
|
204
|
+
if (replayFrom?.type === "cursor" &&
|
|
205
|
+
(typeof previous !== "string" || !previous || !options.cursors))
|
|
206
|
+
throw invalidCursor();
|
|
207
|
+
const cursor = typeof previous === "string" ? previous : undefined;
|
|
208
|
+
if (this.active.has(sessionId))
|
|
209
|
+
throw alreadyOpen(sessionId);
|
|
210
|
+
const read = { invalidated: false };
|
|
211
|
+
const check = () => {
|
|
212
|
+
if (options.signal?.aborted)
|
|
213
|
+
throw RequestError.requestCancelled();
|
|
214
|
+
if (read.invalidated || this.transitions.has(sessionId) || options.isOpen(sessionId))
|
|
215
|
+
throw alreadyOpen(sessionId);
|
|
216
|
+
};
|
|
217
|
+
check();
|
|
218
|
+
this.active.set(sessionId, read);
|
|
219
|
+
const replay = async () => {
|
|
220
|
+
try {
|
|
221
|
+
const view = await options.replay(sessionId, cwd ?? undefined);
|
|
222
|
+
if (options.protocol === 1 && view.title)
|
|
223
|
+
view.frames.push({
|
|
224
|
+
sessionId,
|
|
225
|
+
update: { sessionUpdate: "session_info_update", title: view.title },
|
|
226
|
+
});
|
|
227
|
+
return view;
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
check();
|
|
231
|
+
if (error instanceof RequestError)
|
|
232
|
+
throw error;
|
|
233
|
+
const file = error?.path;
|
|
234
|
+
throw RequestError.internalError({
|
|
235
|
+
sessionId,
|
|
236
|
+
reason: "history_read_failed",
|
|
237
|
+
retryable: true,
|
|
238
|
+
...(file && { paths: [file] }),
|
|
239
|
+
}, `Could not read the history of ${sessionId}. Retry the read. ${String(error)}`);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
try {
|
|
243
|
+
const { frames, cwd: effectiveCwd, historyEnd, title } = await replay();
|
|
244
|
+
check();
|
|
245
|
+
let outgoing = frames;
|
|
246
|
+
let cursorId;
|
|
247
|
+
const useCursor = options.cursors && replayFrom != null;
|
|
248
|
+
if (useCursor) {
|
|
249
|
+
const scope = json([
|
|
250
|
+
sessionId,
|
|
251
|
+
effectiveCwd,
|
|
252
|
+
claudeConfigDir(),
|
|
253
|
+
options.protocol,
|
|
254
|
+
options.capabilities,
|
|
255
|
+
packageJson.version,
|
|
256
|
+
]);
|
|
257
|
+
const next = snapshot(frames, sessionId, options.protocol, scope, cursor, historyEnd);
|
|
258
|
+
outgoing = next.outgoing;
|
|
259
|
+
cursorId = next.cursorId;
|
|
260
|
+
if (cursorId && cursorId !== cursor) {
|
|
261
|
+
// A terminal can append or rewind between the reads of the root and a child.
|
|
262
|
+
const again = await replay();
|
|
263
|
+
check();
|
|
264
|
+
if (again.cwd !== effectiveCwd ||
|
|
265
|
+
again.historyEnd !== historyEnd ||
|
|
266
|
+
frames.length !== again.frames.length ||
|
|
267
|
+
frames.some((frame, i) => json(frame) !== json(again.frames[i]))) {
|
|
268
|
+
throw RequestError.internalError({ sessionId, retryable: true }, "History changed during the read. Retry with the last successful cursor.");
|
|
269
|
+
}
|
|
270
|
+
const end = outgoing.findIndex((frame) => frame.sessionId === sessionId && frame.update.sessionUpdate === "replay_complete");
|
|
271
|
+
outgoing.splice(end < 0 ? outgoing.length : end, 0, {
|
|
272
|
+
sessionId,
|
|
273
|
+
update: {
|
|
274
|
+
sessionUpdate: "cursor_update",
|
|
275
|
+
cursorId,
|
|
276
|
+
anchor: next.anchor,
|
|
277
|
+
capabilities: { read: {} },
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// On v2 the title is current session info, outside the replay cursor.
|
|
283
|
+
if (options.protocol === 2 && (title || useCursor)) {
|
|
284
|
+
outgoing.push({
|
|
285
|
+
sessionId,
|
|
286
|
+
update: { sessionUpdate: "session_info_update", title: title || null },
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
for (const frame of outgoing) {
|
|
290
|
+
check();
|
|
291
|
+
await options.send(frame);
|
|
292
|
+
}
|
|
293
|
+
check();
|
|
294
|
+
return cursorId ? { cursorId } : {};
|
|
295
|
+
}
|
|
296
|
+
finally {
|
|
297
|
+
this.active.delete(sessionId);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
package/dist/session-rewind.d.ts
CHANGED
|
@@ -97,7 +97,9 @@ export declare function loadRewindableHistory(sessionId: string, logger?: Logger
|
|
|
97
97
|
*/
|
|
98
98
|
export declare function cutAtRewindPoint<M extends {
|
|
99
99
|
uuid?: string | null;
|
|
100
|
-
}>(sessionId: string, stored: readonly M[], logger?: Logger
|
|
100
|
+
}>(sessionId: string, stored: readonly M[], logger?: Logger, options?: {
|
|
101
|
+
transcript: string;
|
|
102
|
+
}): Promise<M[]>;
|
|
101
103
|
/**
|
|
102
104
|
* The entry that a fork of the whole of `sessionId` ends at while a rewind is
|
|
103
105
|
* pending, or undefined when there is none: the newest leaf then still lies in
|
|
@@ -136,7 +138,9 @@ export declare function writeRewindPoint(sessionId: string, point: RewindPoint):
|
|
|
136
138
|
* parsed fails with an internal error: reading it as "no rewind" would show or
|
|
137
139
|
* resume the dropped turns. The file stays, so the client can retry.
|
|
138
140
|
*/
|
|
139
|
-
export declare function readRewindPoint(sessionId: string, logger?: Logger
|
|
141
|
+
export declare function readRewindPoint(sessionId: string, logger?: Logger, options?: {
|
|
142
|
+
transcript: string;
|
|
143
|
+
}): Promise<RewindPoint | undefined>;
|
|
140
144
|
/** Removes the record of the last rewind. */
|
|
141
145
|
export declare function clearRewindPoint(sessionId: string): Promise<void>;
|
|
142
146
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-rewind.d.ts","sourceRoot":"","sources":["../src/session-rewind.ts"],"names":[],"mappings":"AAsBA,OAAO,EAEL,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACvB,MAAM,gCAAgC,CAAC;AAaxC,eAAO,MAAM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AAM,MAAM,WAAW,GAAG;IACxB,sDAAsD;IACtD,eAAe,EAAE,MAAM,CAAC;IACxB,iFAAiF;IACjF,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC;AAQF,4CAA4C;AAC5C,wBAAsB,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAY3F;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE,GAC/B,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CA+BlC;AAED,6CAA6C;AAC7C,wBAAsB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGvE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,oBAAoB,CACxC,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAuB7B"}
|
package/dist/session-rewind.js
CHANGED
|
@@ -194,8 +194,8 @@ export async function loadRewindableHistory(sessionId, logger) {
|
|
|
194
194
|
* that a pending rewind dropped. Cut before reading across compactions: the
|
|
195
195
|
* newest leaf is the last retained message.
|
|
196
196
|
*/
|
|
197
|
-
export async function cutAtRewindPoint(sessionId, stored, logger) {
|
|
198
|
-
return activeSessionMessages(stored, await readRewindPoint(sessionId, logger));
|
|
197
|
+
export async function cutAtRewindPoint(sessionId, stored, logger, options) {
|
|
198
|
+
return activeSessionMessages(stored, await readRewindPoint(sessionId, logger, options));
|
|
199
199
|
}
|
|
200
200
|
/**
|
|
201
201
|
* The entry that a fork of the whole of `sessionId` ends at while a rewind is
|
|
@@ -272,9 +272,11 @@ export async function writeRewindPoint(sessionId, point) {
|
|
|
272
272
|
* parsed fails with an internal error: reading it as "no rewind" would show or
|
|
273
273
|
* resume the dropped turns. The file stays, so the client can retry.
|
|
274
274
|
*/
|
|
275
|
-
export async function readRewindPoint(sessionId, logger) {
|
|
275
|
+
export async function readRewindPoint(sessionId, logger, options) {
|
|
276
276
|
try {
|
|
277
|
-
const file =
|
|
277
|
+
const file = options
|
|
278
|
+
? path.join(path.dirname(options.transcript), sessionId, "rewind.json")
|
|
279
|
+
: await rewindPointFile(sessionId);
|
|
278
280
|
if (!file)
|
|
279
281
|
return undefined;
|
|
280
282
|
let point;
|
|
@@ -293,6 +295,13 @@ export async function readRewindPoint(sessionId, logger) {
|
|
|
293
295
|
return { resumeSessionAt, droppedPromptUuid };
|
|
294
296
|
}
|
|
295
297
|
catch (error) {
|
|
298
|
+
if (options) {
|
|
299
|
+
if (typeof error?.path === "string")
|
|
300
|
+
throw error;
|
|
301
|
+
throw Object.assign(new Error("Failed to read the rewind point", { cause: error }), {
|
|
302
|
+
path: path.join(path.dirname(options.transcript), sessionId, "rewind.json"),
|
|
303
|
+
});
|
|
304
|
+
}
|
|
296
305
|
logger?.error(`Failed to read the rewind point of session ${sessionId}:`, error);
|
|
297
306
|
throw RequestError.internalError(undefined, `Session ${sessionId} was rewound, but its rewind point cannot be read (${error instanceof Error ? error.message : String(error)}). Try again.`);
|
|
298
307
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SessionConfigOption, SetSessionConfigOptionRequest } from "@agentclientprotocol/sdk/unstable";
|
|
2
|
-
import type { ModelInfo, Query } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
+
import type { ModelInfo, Query, SDKSettingsParseError } from "@anthropic-ai/claude-agent-sdk";
|
|
3
3
|
export { ULTRACODE_CONFIG_ID } from "./session-config-ids.js";
|
|
4
4
|
/** Select-fallback values for clients without boolean config options. */
|
|
5
5
|
export declare const ULTRACODE_ON = "on";
|
|
@@ -14,10 +14,19 @@ export type UltracodeSettingsReport = {
|
|
|
14
14
|
* effect on the current model. */
|
|
15
15
|
requested?: boolean;
|
|
16
16
|
};
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
/** What the CLI's `get_settings` control response says about the session. */
|
|
18
|
+
export type SettingsReport = {
|
|
19
|
+
/** Undefined when the SDK has no `getSettings` or the request fails; callers
|
|
20
|
+
* then fall back to {@link modelSupportsUltracode}. */
|
|
21
|
+
ultracode: UltracodeSettingsReport | undefined;
|
|
22
|
+
/** Settings files that the CLI skipped, e.g. `{ file: "/home/u/.claude/settings.json",
|
|
23
|
+
* path: "", message: "Invalid or malformed JSON" }`. Empty when the CLI
|
|
24
|
+
* reports none or cannot be asked. */
|
|
25
|
+
settingsErrors: SDKSettingsParseError[];
|
|
26
|
+
};
|
|
27
|
+
/** Ask the CLI whether ultracode is available and requested for the session,
|
|
28
|
+
* and which settings files it skipped. */
|
|
29
|
+
export declare function readSettingsReport(query: Query): Promise<SettingsReport>;
|
|
21
30
|
/** The CLI supports ultracode on exactly the models that support `xhigh`
|
|
22
31
|
* effort (both use the same capability check), and `supportedEffortLevels`
|
|
23
32
|
* lists `xhigh` only for those models. E.g. Opus 4.7+, Sonnet 5+ and Haiku
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session-ultracode.d.ts","sourceRoot":"","sources":["../src/session-ultracode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EACnB,6BAA6B,EAC9B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;
|
|
1
|
+
{"version":3,"file":"session-ultracode.d.ts","sourceRoot":"","sources":["../src/session-ultracode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,mBAAmB,EACnB,6BAA6B,EAC9B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,qBAAqB,EAAE,MAAM,gCAAgC,CAAC;AAG9F,OAAO,EAAE,mBAAmB,E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