@agentclientprotocol/claude-agent-acp 0.102.0 → 0.102.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.
Files changed (206) hide show
  1. package/README.md +5 -1
  2. package/dist/acp-agent.d.ts +119 -439
  3. package/dist/acp-agent.d.ts.map +1 -1
  4. package/dist/acp-agent.js +380 -2412
  5. package/dist/acp-subagents.d.ts +1 -2
  6. package/dist/acp-subagents.d.ts.map +1 -1
  7. package/dist/auth-methods.d.ts +21 -0
  8. package/dist/auth-methods.d.ts.map +1 -0
  9. package/dist/auth-methods.js +68 -0
  10. package/dist/auth-status-reporter.d.ts +112 -0
  11. package/dist/auth-status-reporter.d.ts.map +1 -0
  12. package/dist/auth-status-reporter.js +253 -0
  13. package/dist/background-work.d.ts +12 -265
  14. package/dist/background-work.d.ts.map +1 -1
  15. package/dist/background-work.js +8 -558
  16. package/dist/client-system-prompt.d.ts +7 -0
  17. package/dist/client-system-prompt.d.ts.map +1 -1
  18. package/dist/client-system-prompt.js +24 -0
  19. package/dist/context-compaction.d.ts +20 -0
  20. package/dist/context-compaction.d.ts.map +1 -1
  21. package/dist/context-compaction.js +26 -0
  22. package/dist/context-window.d.ts +70 -0
  23. package/dist/context-window.d.ts.map +1 -0
  24. package/dist/context-window.js +132 -0
  25. package/dist/diff.d.ts +1 -1
  26. package/dist/diff.js +1 -1
  27. package/dist/elicitation.d.ts +66 -1
  28. package/dist/elicitation.d.ts.map +1 -1
  29. package/dist/elicitation.js +109 -0
  30. package/dist/file-change-audit.d.ts +4 -3
  31. package/dist/file-change-audit.d.ts.map +1 -1
  32. package/dist/file-change-audit.js +3 -2
  33. package/dist/mcp-oauth.d.ts +44 -0
  34. package/dist/mcp-oauth.d.ts.map +1 -0
  35. package/dist/mcp-oauth.js +142 -0
  36. package/dist/mcp-servers.d.ts +10 -0
  37. package/dist/mcp-servers.d.ts.map +1 -0
  38. package/dist/mcp-servers.js +29 -0
  39. package/dist/native-subagents.d.ts +18 -8
  40. package/dist/native-subagents.d.ts.map +1 -1
  41. package/dist/native-subagents.js +7 -35
  42. package/dist/permissions/presentation.d.ts +29 -0
  43. package/dist/permissions/presentation.d.ts.map +1 -1
  44. package/dist/permissions/presentation.js +38 -11
  45. package/dist/plans.d.ts +36 -6
  46. package/dist/plans.d.ts.map +1 -1
  47. package/dist/plans.js +49 -11
  48. package/dist/prompt-suggestions.d.ts +29 -0
  49. package/dist/prompt-suggestions.d.ts.map +1 -1
  50. package/dist/prompt-suggestions.js +45 -0
  51. package/dist/providers.d.ts +139 -0
  52. package/dist/providers.d.ts.map +1 -0
  53. package/dist/providers.js +286 -0
  54. package/dist/remote-control.d.ts +62 -0
  55. package/dist/remote-control.d.ts.map +1 -0
  56. package/dist/remote-control.js +78 -0
  57. package/dist/result-end.d.ts +39 -0
  58. package/dist/result-end.d.ts.map +1 -0
  59. package/dist/result-end.js +50 -0
  60. package/dist/serve.d.ts +7 -2
  61. package/dist/serve.d.ts.map +1 -1
  62. package/dist/serve.js +10 -5
  63. package/dist/session-config-ids.d.ts +1 -0
  64. package/dist/session-config-ids.d.ts.map +1 -1
  65. package/dist/session-config-ids.js +1 -0
  66. package/dist/session-config-options.d.ts +64 -0
  67. package/dist/session-config-options.d.ts.map +1 -0
  68. package/dist/session-config-options.js +162 -0
  69. package/dist/session-effort.d.ts +67 -0
  70. package/dist/session-effort.d.ts.map +1 -1
  71. package/dist/session-effort.js +98 -0
  72. package/dist/session-env.d.ts +22 -0
  73. package/dist/session-env.d.ts.map +1 -0
  74. package/dist/session-env.js +66 -0
  75. package/dist/session-fast-mode.d.ts +83 -0
  76. package/dist/session-fast-mode.d.ts.map +1 -0
  77. package/dist/session-fast-mode.js +142 -0
  78. package/dist/session-index/connection.d.ts +2 -5
  79. package/dist/session-index/connection.d.ts.map +1 -1
  80. package/dist/session-index/connection.js +3 -5
  81. package/dist/session-mode.d.ts +1 -0
  82. package/dist/session-mode.d.ts.map +1 -1
  83. package/dist/session-mode.js +1 -1
  84. package/dist/session-model.d.ts +41 -0
  85. package/dist/session-model.d.ts.map +1 -1
  86. package/dist/session-model.js +110 -45
  87. package/dist/session-notices.d.ts +44 -0
  88. package/dist/session-notices.d.ts.map +1 -1
  89. package/dist/session-notices.js +68 -0
  90. package/dist/session-titles.d.ts +34 -12
  91. package/dist/session-titles.d.ts.map +1 -1
  92. package/dist/session-titles.js +7 -6
  93. package/dist/session-usage.d.ts +125 -0
  94. package/dist/session-usage.d.ts.map +1 -0
  95. package/dist/session-usage.js +196 -0
  96. package/dist/streamed-text.d.ts +33 -0
  97. package/dist/streamed-text.d.ts.map +1 -0
  98. package/dist/streamed-text.js +71 -0
  99. package/dist/subagent-configuration.d.ts +32 -0
  100. package/dist/subagent-configuration.d.ts.map +1 -0
  101. package/dist/subagent-configuration.js +70 -0
  102. package/dist/subagent-replay.d.ts +18 -0
  103. package/dist/subagent-replay.d.ts.map +1 -1
  104. package/dist/subagent-replay.js +25 -15
  105. package/dist/tool-calls/client-capabilities.d.ts +8 -8
  106. package/dist/tool-calls/client-capabilities.js +4 -4
  107. package/dist/tool-calls/renderer.d.ts +4 -5
  108. package/dist/tool-calls/renderer.d.ts.map +1 -1
  109. package/dist/tool-calls/renderer.js +4 -5
  110. package/dist/turn-events.d.ts +6 -46
  111. package/dist/turn-events.d.ts.map +1 -1
  112. package/dist/v2/agent.d.ts +7 -11
  113. package/dist/v2/agent.d.ts.map +1 -1
  114. package/dist/v2/agent.js +751 -331
  115. package/dist/v2/air.d.ts +33 -0
  116. package/dist/v2/air.d.ts.map +1 -0
  117. package/dist/v2/air.js +29 -0
  118. package/dist/v2/auth.d.ts +97 -0
  119. package/dist/v2/auth.d.ts.map +1 -0
  120. package/dist/v2/auth.js +216 -0
  121. package/dist/v2/background.d.ts +27 -16
  122. package/dist/v2/background.d.ts.map +1 -1
  123. package/dist/v2/background.js +4 -32
  124. package/dist/v2/child-config.d.ts +55 -0
  125. package/dist/v2/child-config.d.ts.map +1 -0
  126. package/dist/v2/child-config.js +92 -0
  127. package/dist/v2/clear-context.d.ts +94 -0
  128. package/dist/v2/clear-context.d.ts.map +1 -0
  129. package/dist/v2/clear-context.js +176 -0
  130. package/dist/v2/commands.d.ts +46 -0
  131. package/dist/v2/commands.d.ts.map +1 -0
  132. package/dist/v2/commands.js +54 -0
  133. package/dist/v2/config.d.ts +187 -0
  134. package/dist/v2/config.d.ts.map +1 -0
  135. package/dist/v2/config.js +494 -0
  136. package/dist/v2/elicitation.d.ts +93 -0
  137. package/dist/v2/elicitation.d.ts.map +1 -0
  138. package/dist/v2/elicitation.js +160 -0
  139. package/dist/v2/file-changes.d.ts +61 -0
  140. package/dist/v2/file-changes.d.ts.map +1 -0
  141. package/dist/v2/file-changes.js +108 -0
  142. package/dist/v2/file-preview.d.ts +18 -0
  143. package/dist/v2/file-preview.d.ts.map +1 -0
  144. package/dist/v2/file-preview.js +29 -0
  145. package/dist/v2/initialize.d.ts +10 -24
  146. package/dist/v2/initialize.d.ts.map +1 -1
  147. package/dist/v2/initialize.js +4 -111
  148. package/dist/v2/outcome.d.ts +81 -0
  149. package/dist/v2/outcome.d.ts.map +1 -0
  150. package/dist/v2/outcome.js +165 -0
  151. package/dist/v2/permissions.d.ts +92 -0
  152. package/dist/v2/permissions.d.ts.map +1 -0
  153. package/dist/v2/permissions.js +162 -0
  154. package/dist/v2/prompt.d.ts +2 -42
  155. package/dist/v2/prompt.d.ts.map +1 -1
  156. package/dist/v2/prompt.js +2 -119
  157. package/dist/v2/remote-control.d.ts +41 -0
  158. package/dist/v2/remote-control.d.ts.map +1 -0
  159. package/dist/v2/remote-control.js +63 -0
  160. package/dist/v2/reports.d.ts +198 -0
  161. package/dist/v2/reports.d.ts.map +1 -0
  162. package/dist/v2/reports.js +487 -0
  163. package/dist/v2/restarts.d.ts +128 -0
  164. package/dist/v2/restarts.d.ts.map +1 -0
  165. package/dist/v2/restarts.js +348 -0
  166. package/dist/v2/session-requests.d.ts +20 -0
  167. package/dist/v2/session-requests.d.ts.map +1 -0
  168. package/dist/v2/session-requests.js +108 -0
  169. package/dist/v2/session-update.d.ts +2 -13
  170. package/dist/v2/session-update.d.ts.map +1 -1
  171. package/dist/v2/session-update.js +2 -22
  172. package/dist/v2/session.d.ts +340 -41
  173. package/dist/v2/session.d.ts.map +1 -1
  174. package/dist/v2/session.js +1223 -141
  175. package/dist/v2/setup.d.ts +49 -0
  176. package/dist/v2/setup.d.ts.map +1 -0
  177. package/dist/v2/setup.js +62 -0
  178. package/dist/v2/stopped-work.d.ts +123 -0
  179. package/dist/v2/stopped-work.d.ts.map +1 -0
  180. package/dist/v2/stopped-work.js +210 -0
  181. package/dist/v2/stream.d.ts +228 -0
  182. package/dist/v2/stream.d.ts.map +1 -0
  183. package/dist/v2/stream.js +506 -0
  184. package/dist/v2/subagent-replay.d.ts +115 -0
  185. package/dist/v2/subagent-replay.d.ts.map +1 -0
  186. package/dist/v2/subagent-replay.js +479 -0
  187. package/dist/v2/subscription-guard.d.ts +60 -0
  188. package/dist/v2/subscription-guard.d.ts.map +1 -0
  189. package/dist/v2/subscription-guard.js +84 -0
  190. package/dist/v2/tasks.d.ts +492 -0
  191. package/dist/v2/tasks.d.ts.map +1 -0
  192. package/dist/v2/tasks.js +1728 -0
  193. package/dist/v2/terminal.d.ts.map +1 -1
  194. package/dist/v2/terminal.js +6 -1
  195. package/dist/v2/tool-call.d.ts +3 -2
  196. package/dist/v2/tool-call.d.ts.map +1 -1
  197. package/dist/v2/update-translator.d.ts +36 -0
  198. package/dist/v2/update-translator.d.ts.map +1 -0
  199. package/dist/v2/update-translator.js +80 -0
  200. package/dist/workflow-progress.d.ts +23 -0
  201. package/dist/workflow-progress.d.ts.map +1 -1
  202. package/dist/workflow-progress.js +31 -8
  203. package/package.json +1 -1
  204. package/dist/v2/permission.d.ts +0 -28
  205. package/dist/v2/permission.d.ts.map +0 -1
  206. package/dist/v2/permission.js +0 -41
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Session setup of the v2 core, on the v1 core's rules: the query
3
+ * options that `_meta.claudeCode.options` and the agent's environment add,
4
+ * the models a session offers, and the updates that a `session/resume`
5
+ * sends before its response.
6
+ */
7
+ import type * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
8
+ import { type HookCallbackMatcher, type HookEvent, type ModelInfo, type Options, type Query } from "@anthropic-ai/claude-agent-sdk";
9
+ import type { Logger } from "../acp-agent.js";
10
+ import { type SessionModelState } from "../session-model.js";
11
+ import type { SettingsManager } from "../settings.js";
12
+ import type { SetupParams } from "./restarts.js";
13
+ import type { V2Session } from "./session.js";
14
+ /** The SDK options a client passes in `_meta.claudeCode.options`. */
15
+ export declare function clientOptions(meta: unknown): Options | undefined;
16
+ /** The core's hooks, after the client's own hooks of the same event. */
17
+ export declare function withClientHooks(client: Options["hooks"], own: Partial<Record<HookEvent, HookCallbackMatcher[]>>): Partial<Record<HookEvent, HookCallbackMatcher[]>>;
18
+ /**
19
+ * The models of a session, as the v1 core picks them: the SDK's models, with
20
+ * a resumed session's model row under its family alias (`aliasResumedModelRow`),
21
+ * narrowed by the settings' `availableModels` allowlist and then by the
22
+ * provider (`narrow`), with the current model from `ANTHROPIC_MODEL`, the
23
+ * settings' `model`, or the transcript of a resumed session
24
+ * (`getAvailableModels`, which sets it on the query when needed), and the
25
+ * current model's capabilities, also for a resumed model outside the
26
+ * allowlist (`withCurrentModelInfo`).
27
+ */
28
+ export declare function sessionModels(query: Query, sdkModels: ModelInfo[], settings: SettingsManager, narrow: (models: ModelInfo[]) => ModelInfo[], narrowedByProvider: boolean, resumed: {
29
+ model?: string;
30
+ } | undefined, sessionId: string, logger: Logger): Promise<{
31
+ models: SessionModelState;
32
+ modelInfos: ModelInfo[];
33
+ currentModelInfo: ModelInfo | undefined;
34
+ allowlistedModelInfo: ModelInfo | undefined;
35
+ }>;
36
+ /**
37
+ * The update that ends a replay, for a client that declares AIR's
38
+ * `loadReplayEnd` capability, as v1 sends it.
39
+ */
40
+ export declare function replayEndUpdate(capabilities: v2.ClientCapabilities | null | undefined): v2.SessionUpdate | undefined;
41
+ /**
42
+ * Starts OAuth for the setup request's MCP servers that need it, once the
43
+ * setup response is sent: v2 asks for no elicitation before it. The handler
44
+ * returns the response right after this call, and the SDK writes it before a
45
+ * timer runs. Nothing else orders the two. A restart calls it on the new
46
+ * query too: closing the old one withdrew the old one's OAuth runs.
47
+ */
48
+ export declare function afterSetupResponse(session: V2Session, params: SetupParams): void;
49
+ //# sourceMappingURL=setup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/v2/setup.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,KAAK,EAAE,MAAM,mDAAmD,CAAC;AAC7E,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,KAAK,EACX,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,MAAM,EAAkB,MAAM,iBAAiB,CAAC;AAO9D,OAAO,EAIL,KAAK,iBAAiB,EAEvB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,qEAAqE;AACrE,wBAAgB,aAAa,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,GAAG,SAAS,CAEhE;AAED,wEAAwE;AACxE,wBAAgB,eAAe,CAC7B,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,EACxB,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,mBAAmB,EAAE,CAAC,CAAC,GACrD,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,mBAAmB,EAAE,CAAC,CAAC,CAMnD;AAED;;;;;;;;;GASG;AACH,wBAAsB,aAAa,CACjC,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,SAAS,EAAE,EACtB,QAAQ,EAAE,eAAe,EACzB,MAAM,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,KAAK,SAAS,EAAE,EAC5C,kBAAkB,EAAE,OAAO,EAC3B,OAAO,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,EACvC,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;IACT,MAAM,EAAE,iBAAiB,CAAC;IAC1B,UAAU,EAAE,SAAS,EAAE,CAAC;IACxB,gBAAgB,EAAE,SAAS,GAAG,SAAS,CAAC;IACxC,oBAAoB,EAAE,SAAS,GAAG,SAAS,CAAC;CAC7C,CAAC,CAyBD;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,YAAY,EAAE,EAAE,CAAC,kBAAkB,GAAG,IAAI,GAAG,SAAS,GACrD,EAAE,CAAC,aAAa,GAAG,SAAS,CAO9B;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,GAAG,IAAI,CAOhF"}
@@ -0,0 +1,62 @@
1
+ import { AIR_LOAD_REPLAY_END_CAPABILITY, AIR_LOAD_REPLAY_END_KEY, clientSupportsAirCapability, withAirMeta, } from "../air-extension.js";
2
+ import { aliasResumedModelRow, applyAvailableModelsAllowlist, getAvailableModels, withCurrentModelInfo, } from "../session-model.js";
3
+ /** The SDK options a client passes in `_meta.claudeCode.options`. */
4
+ export function clientOptions(meta) {
5
+ return meta?.claudeCode?.options;
6
+ }
7
+ /** The core's hooks, after the client's own hooks of the same event. */
8
+ export function withClientHooks(client, own) {
9
+ const hooks = { ...client };
10
+ for (const [event, matchers] of Object.entries(own)) {
11
+ hooks[event] = [...(client?.[event] ?? []), ...matchers];
12
+ }
13
+ return hooks;
14
+ }
15
+ /**
16
+ * The models of a session, as the v1 core picks them: the SDK's models, with
17
+ * a resumed session's model row under its family alias (`aliasResumedModelRow`),
18
+ * narrowed by the settings' `availableModels` allowlist and then by the
19
+ * provider (`narrow`), with the current model from `ANTHROPIC_MODEL`, the
20
+ * settings' `model`, or the transcript of a resumed session
21
+ * (`getAvailableModels`, which sets it on the query when needed), and the
22
+ * current model's capabilities, also for a resumed model outside the
23
+ * allowlist (`withCurrentModelInfo`).
24
+ */
25
+ export async function sessionModels(query, sdkModels, settings, narrow, narrowedByProvider, resumed, sessionId, logger) {
26
+ const { availableModels, modelOverrides } = settings.getSettings();
27
+ // A resumed Fable 5.2 session lists Fable 5.2 as `fable`, as a new session does.
28
+ const listed = aliasResumedModelRow(sdkModels, resumed?.model);
29
+ const allowed = Array.isArray(availableModels)
30
+ ? applyAvailableModelsAllowlist(listed, availableModels, modelOverrides, settings.getManagedDeniedModels())
31
+ : listed;
32
+ const narrowed = narrow(allowed);
33
+ const models = await getAvailableModels(query, narrowed, sdkModels, settings, logger, resumed !== undefined, sessionId, resumed?.model, narrowedByProvider);
34
+ return { models, ...withCurrentModelInfo(narrowed, listed, models.currentModelId) };
35
+ }
36
+ /**
37
+ * The update that ends a replay, for a client that declares AIR's
38
+ * `loadReplayEnd` capability, as v1 sends it.
39
+ */
40
+ export function replayEndUpdate(capabilities) {
41
+ if (!clientSupportsAirCapability(capabilities, AIR_LOAD_REPLAY_END_CAPABILITY))
42
+ return undefined;
43
+ // No `title` and no `updatedAt`: `null` would clear them.
44
+ return {
45
+ sessionUpdate: "session_info_update",
46
+ _meta: withAirMeta(undefined, AIR_LOAD_REPLAY_END_KEY, true),
47
+ };
48
+ }
49
+ /**
50
+ * Starts OAuth for the setup request's MCP servers that need it, once the
51
+ * setup response is sent: v2 asks for no elicitation before it. The handler
52
+ * returns the response right after this call, and the SDK writes it before a
53
+ * timer runs. Nothing else orders the two. A restart calls it on the new
54
+ * query too: closing the old one withdrew the old one's OAuth runs.
55
+ */
56
+ export function afterSetupResponse(session, params) {
57
+ // The setup already rejected servers of an unknown transport.
58
+ const names = (params.mcpServers ?? []).flatMap((server) => typeof server.name === "string" ? [server.name] : []);
59
+ if (names.length === 0)
60
+ return;
61
+ setTimeout(() => session.elicitations.authenticateMcpServers(names), 0);
62
+ }
@@ -0,0 +1,123 @@
1
+ /**
2
+ * The work of an ACP v2 session that a `session/cancel` stopped before
3
+ * the client saw it, and the reach of the cancel's interrupt.
4
+ *
5
+ * A cancel answers every prompt that is not inserted yet with -32800, also
6
+ * one that went to Claude Code already. Claude Code can still run such a
7
+ * prompt, when it had taken it in before the interrupt arrived. The client
8
+ * was told the prompt never ran, so that run is dropped: its output, its
9
+ * states, and its permission requests, which are denied. So is a model cycle
10
+ * that Claude Code had started on its own (a `status: requesting` frame came)
11
+ * and that showed nothing yet when the cancel came while the session was
12
+ * idle.
13
+ *
14
+ * The interrupt stops whatever Claude Code runs when it arrives, which can be
15
+ * a cycle that the session reads only after the cancel. That cycle ends with
16
+ * an aborted result, which reports `cancelled`. Only the next result can be
17
+ * that cycle's after a cancel of an idle session, and the next two after a
18
+ * cancel of open work (the open cycle can end before the interrupt arrives).
19
+ * A prompt that a cancel did not abandon starting, or an idle that follows no
20
+ * result, ends that reach sooner: a later aborted result, such as of an
21
+ * interrupt from Claude Code's remote control, reports as it would without
22
+ * the cancel.
23
+ */
24
+ import type { SDKResultMessage } from "@anthropic-ai/claude-agent-sdk";
25
+ export declare class StoppedWork {
26
+ /** The prompts that a cancel answered with -32800 after they went to Claude Code. */
27
+ private readonly abandoned;
28
+ /** Whether Claude Code runs dropped work: its output goes nowhere. */
29
+ private dropping;
30
+ /** The abandoned prompt whose run is dropped, when a prompt's run is. */
31
+ private droppingFor;
32
+ /** The prompts that Claude Code took in since the last result. */
33
+ private readonly startedSinceResult;
34
+ /**
35
+ * Whether the late result of a dead turn may still come: Claude Code sent
36
+ * `cancelled` for a prompt that it had started, before any result. The v1
37
+ * core calls such a prompt a zombie.
38
+ */
39
+ private deadTurnResult;
40
+ /**
41
+ * The tool uses of the main conversation that the session read since the
42
+ * last result, each with whether it was dropped work's.
43
+ */
44
+ private readonly toolUses;
45
+ /** The `canUseTool` checks that wait for the session to read their tool use. */
46
+ private readonly toolUseWaiters;
47
+ /**
48
+ * How many of the next results a cancel's interrupt can end, until Claude
49
+ * Code shows what it stopped: 2 after a cancel of open work, whose result
50
+ * can come before the interrupt arrives and stops the next cycle, and 1
51
+ * after a cancel of an idle session (the cycle that Claude Code runs when
52
+ * it arrives).
53
+ */
54
+ private interruptReach;
55
+ /**
56
+ * Whether a model request started since the last result: a `status:
57
+ * requesting` frame, which starts each one.
58
+ */
59
+ modelRequestSinceResult: boolean;
60
+ /**
61
+ * A cancel: `pushed` are the prompts it answered that Claude Code has, and
62
+ * `foreground` whether the client sees work running. A cycle that Claude
63
+ * Code started while the client saw none is dropped.
64
+ */
65
+ cancelled(pushed: readonly string[], foreground: boolean): void;
66
+ /** The interrupt failed: Claude Code stops nothing. */
67
+ interruptFailed(): void;
68
+ /** A `status: requesting` frame, with the prompts that the request serves. */
69
+ requesting(prompts: readonly string[]): void;
70
+ /**
71
+ * Claude Code takes in the prompt `uuid` (its `started` frame or echo).
72
+ * Returns whether it is one that a cancel abandoned, which then adds
73
+ * nothing: its run is dropped, or, when work is open (`foreground`), Claude
74
+ * Code folded it into the work that the cancel stops. Another prompt's run
75
+ * is not dropped.
76
+ */
77
+ promptStarted(uuid: string, foreground: boolean): boolean;
78
+ /**
79
+ * Claude Code ended the prompt `uuid` (`completed`, or `cancelled` when
80
+ * `cancelled`). Only the end of the prompt whose run is dropped ends the
81
+ * drop: another abandoned prompt's `cancelled` frame, which the interrupt's
82
+ * `cancelQueued` sends, does not.
83
+ */
84
+ promptEnded(uuid: string, cancelled?: boolean): void;
85
+ /**
86
+ * Whether `message`, output of the main conversation, is dropped work's.
87
+ * Records the tool uses it asks to run.
88
+ */
89
+ drops(message: {
90
+ type: string;
91
+ message?: unknown;
92
+ event?: unknown;
93
+ }): boolean;
94
+ /**
95
+ * Whether `canUseTool` denies the tool use `toolUseId` of the agent
96
+ * `agentId` (undefined for the main conversation): dropped work asks to
97
+ * run it. Claude Code asks on its control channel after it streamed the
98
+ * tool use, and the session can read the stream later. So after a cancel,
99
+ * while dropped work may run, this waits until the session reads the tool
100
+ * use, for at most {@link TOOL_USE_WAIT_MS} or until `signal` aborts: the
101
+ * stream never shows some tool uses, such as SandboxNetworkAccess's.
102
+ */
103
+ dropsToolUse(toolUseId: string, agentId: string | undefined, signal: AbortSignal): Promise<boolean>;
104
+ /**
105
+ * A result that is not a placeholder. Returns whether it ends dropped work,
106
+ * or is the late result of a dead turn (see {@link promptEnded}), and
107
+ * whether it ends work that the interrupt stopped (an aborted result after
108
+ * a cancel).
109
+ */
110
+ result(result: SDKResultMessage): {
111
+ dropped: boolean;
112
+ interrupted: boolean;
113
+ };
114
+ /**
115
+ * Claude Code went idle, other than right after a result: what it ran has
116
+ * ended, and so has the interrupt's reach.
117
+ */
118
+ idle(): void;
119
+ /** Drops the run of the abandoned prompt `uuid`. */
120
+ private drop;
121
+ private endDropping;
122
+ }
123
+ //# sourceMappingURL=stopped-work.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stopped-work.d.ts","sourceRoot":"","sources":["../../src/v2/stopped-work.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,KAAK,EAGV,gBAAgB,EACjB,MAAM,gCAAgC,CAAC;AASxC,qBAAa,WAAW;IACtB,qFAAqF;IACrF,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,sEAAsE;IACtE,OAAO,CAAC,QAAQ,CAAS;IACzB,yEAAyE;IACzE,OAAO,CAAC,WAAW,CAAqB;IACxC,kEAAkE;IAClE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAqB;IACxD;;;;OAIG;IACH,OAAO,CAAC,cAAc,CAAS;IAC/B;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA8B;IACvD,gFAAgF;IAChF,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAsC;IACrE;;;;;;OAMG;IACH,OAAO,CAAC,cAAc,CAAK;IAC3B;;;OAGG;IACH,uBAAuB,UAAS;IAEhC;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE,UAAU,EAAE,OAAO,GAAG,IAAI;IAM/D,uDAAuD;IACvD,eAAe,IAAI,IAAI;IAIvB,8EAA8E;IAC9E,UAAU,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAM5C;;;;;;OAMG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,GAAG,OAAO;IAkBzD;;;;;OAKG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,UAAQ,GAAG,IAAI;IAOlD;;;OAGG;IACH,KAAK,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,OAAO;IAU7E;;;;;;;;OAQG;IACG,YAAY,CAChB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,GAAG,SAAS,EAC3B,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,OAAO,CAAC;IAwBnB;;;;;OAKG;IACH,MAAM,CAAC,MAAM,EAAE,gBAAgB,GAAG;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,WAAW,EAAE,OAAO,CAAA;KAAE;IAe5E;;;OAGG;IACH,IAAI,IAAI,IAAI;IASZ,oDAAoD;IACpD,OAAO,CAAC,IAAI;IAKZ,OAAO,CAAC,WAAW;CAIpB"}
@@ -0,0 +1,210 @@
1
+ import { isAbortedResult } from "../acp-agent.js";
2
+ /**
3
+ * How long `canUseTool` waits, after a cancel, for the session to read the
4
+ * tool use that it asks about, as on v1 (`TOOL_USE_WAIT_MS`).
5
+ */
6
+ const TOOL_USE_WAIT_MS = 2_000;
7
+ export class StoppedWork {
8
+ /** The prompts that a cancel answered with -32800 after they went to Claude Code. */
9
+ abandoned = new Set();
10
+ /** Whether Claude Code runs dropped work: its output goes nowhere. */
11
+ dropping = false;
12
+ /** The abandoned prompt whose run is dropped, when a prompt's run is. */
13
+ droppingFor;
14
+ /** The prompts that Claude Code took in since the last result. */
15
+ startedSinceResult = new Set();
16
+ /**
17
+ * Whether the late result of a dead turn may still come: Claude Code sent
18
+ * `cancelled` for a prompt that it had started, before any result. The v1
19
+ * core calls such a prompt a zombie.
20
+ */
21
+ deadTurnResult = false;
22
+ /**
23
+ * The tool uses of the main conversation that the session read since the
24
+ * last result, each with whether it was dropped work's.
25
+ */
26
+ toolUses = new Map();
27
+ /** The `canUseTool` checks that wait for the session to read their tool use. */
28
+ toolUseWaiters = new Map();
29
+ /**
30
+ * How many of the next results a cancel's interrupt can end, until Claude
31
+ * Code shows what it stopped: 2 after a cancel of open work, whose result
32
+ * can come before the interrupt arrives and stops the next cycle, and 1
33
+ * after a cancel of an idle session (the cycle that Claude Code runs when
34
+ * it arrives).
35
+ */
36
+ interruptReach = 0;
37
+ /**
38
+ * Whether a model request started since the last result: a `status:
39
+ * requesting` frame, which starts each one.
40
+ */
41
+ modelRequestSinceResult = false;
42
+ /**
43
+ * A cancel: `pushed` are the prompts it answered that Claude Code has, and
44
+ * `foreground` whether the client sees work running. A cycle that Claude
45
+ * Code started while the client saw none is dropped.
46
+ */
47
+ cancelled(pushed, foreground) {
48
+ for (const uuid of pushed)
49
+ this.abandoned.add(uuid);
50
+ if (!foreground && this.modelRequestSinceResult)
51
+ this.dropping = true;
52
+ this.interruptReach = foreground ? 2 : 1;
53
+ }
54
+ /** The interrupt failed: Claude Code stops nothing. */
55
+ interruptFailed() {
56
+ this.interruptReach = 0;
57
+ }
58
+ /** A `status: requesting` frame, with the prompts that the request serves. */
59
+ requesting(prompts) {
60
+ this.modelRequestSinceResult = true;
61
+ const abandoned = prompts.find((uuid) => this.abandoned.has(uuid));
62
+ if (abandoned !== undefined)
63
+ this.drop(abandoned);
64
+ }
65
+ /**
66
+ * Claude Code takes in the prompt `uuid` (its `started` frame or echo).
67
+ * Returns whether it is one that a cancel abandoned, which then adds
68
+ * nothing: its run is dropped, or, when work is open (`foreground`), Claude
69
+ * Code folded it into the work that the cancel stops. Another prompt's run
70
+ * is not dropped.
71
+ */
72
+ promptStarted(uuid, foreground) {
73
+ this.startedSinceResult.add(uuid);
74
+ if (!this.abandoned.has(uuid)) {
75
+ // Claude Code takes its prompts in order: the earlier ones ran or were
76
+ // dropped, and a dead turn's late result came before this prompt started.
77
+ // A prompt that a cancel did not abandon went to Claude Code after the
78
+ // cancel's interrupt, on the same stdin: the interrupt reached no
79
+ // further either.
80
+ this.abandoned.clear();
81
+ this.endDropping();
82
+ this.deadTurnResult = false;
83
+ this.interruptReach = 0;
84
+ return false;
85
+ }
86
+ if (!foreground)
87
+ this.drop(uuid);
88
+ return true;
89
+ }
90
+ /**
91
+ * Claude Code ended the prompt `uuid` (`completed`, or `cancelled` when
92
+ * `cancelled`). Only the end of the prompt whose run is dropped ends the
93
+ * drop: another abandoned prompt's `cancelled` frame, which the interrupt's
94
+ * `cancelQueued` sends, does not.
95
+ */
96
+ promptEnded(uuid, cancelled = false) {
97
+ // `cancelled` after `started` and before a result: the turn died, and its
98
+ // result can still come after this frame. That result ends nothing.
99
+ if (cancelled && this.startedSinceResult.delete(uuid))
100
+ this.deadTurnResult = true;
101
+ if (this.abandoned.delete(uuid) && this.droppingFor === uuid)
102
+ this.endDropping();
103
+ }
104
+ /**
105
+ * Whether `message`, output of the main conversation, is dropped work's.
106
+ * Records the tool uses it asks to run.
107
+ */
108
+ drops(message) {
109
+ for (const id of toolUseIds(message)) {
110
+ this.toolUses.set(id, this.dropping);
111
+ const waiters = this.toolUseWaiters.get(id);
112
+ this.toolUseWaiters.delete(id);
113
+ for (const waiter of waiters ?? [])
114
+ waiter();
115
+ }
116
+ return this.dropping;
117
+ }
118
+ /**
119
+ * Whether `canUseTool` denies the tool use `toolUseId` of the agent
120
+ * `agentId` (undefined for the main conversation): dropped work asks to
121
+ * run it. Claude Code asks on its control channel after it streamed the
122
+ * tool use, and the session can read the stream later. So after a cancel,
123
+ * while dropped work may run, this waits until the session reads the tool
124
+ * use, for at most {@link TOOL_USE_WAIT_MS} or until `signal` aborts: the
125
+ * stream never shows some tool uses, such as SandboxNetworkAccess's.
126
+ */
127
+ async dropsToolUse(toolUseId, agentId, signal) {
128
+ if (agentId !== undefined)
129
+ return false;
130
+ const read = this.toolUses.get(toolUseId);
131
+ if (read !== undefined)
132
+ return read;
133
+ if ((this.abandoned.size === 0 && !this.dropping) || signal.aborted)
134
+ return false;
135
+ await new Promise((resolve) => {
136
+ const waiters = this.toolUseWaiters.get(toolUseId) ?? new Set();
137
+ this.toolUseWaiters.set(toolUseId, waiters);
138
+ const done = () => {
139
+ clearTimeout(timer);
140
+ signal.removeEventListener("abort", done);
141
+ waiters.delete(done);
142
+ if (waiters.size === 0 && this.toolUseWaiters.get(toolUseId) === waiters) {
143
+ this.toolUseWaiters.delete(toolUseId);
144
+ }
145
+ resolve();
146
+ };
147
+ const timer = setTimeout(done, TOOL_USE_WAIT_MS);
148
+ signal.addEventListener("abort", done, { once: true });
149
+ waiters.add(done);
150
+ });
151
+ return this.toolUses.get(toolUseId) ?? this.dropping;
152
+ }
153
+ /**
154
+ * A result that is not a placeholder. Returns whether it ends dropped work,
155
+ * or is the late result of a dead turn (see {@link promptEnded}), and
156
+ * whether it ends work that the interrupt stopped (an aborted result after
157
+ * a cancel).
158
+ */
159
+ result(result) {
160
+ this.modelRequestSinceResult = false;
161
+ this.startedSinceResult.clear();
162
+ // Claude Code asks for a tool use before the result that ends its turn.
163
+ this.toolUses.clear();
164
+ // An aborted result shows what the interrupt stopped. Another one uses up
165
+ // one result of its reach (see `interruptReach`).
166
+ const aborted = this.interruptReach > 0 && isAbortedResult(result);
167
+ this.interruptReach = aborted ? 0 : Math.max(0, this.interruptReach - 1);
168
+ const dropped = this.dropping || this.deadTurnResult;
169
+ this.deadTurnResult = false;
170
+ this.endDropping();
171
+ return { dropped, interrupted: aborted && !dropped };
172
+ }
173
+ /**
174
+ * Claude Code went idle, other than right after a result: what it ran has
175
+ * ended, and so has the interrupt's reach.
176
+ */
177
+ idle() {
178
+ this.interruptReach = 0;
179
+ this.modelRequestSinceResult = false;
180
+ // A dead turn's result comes before Claude Code goes idle, or never.
181
+ this.deadTurnResult = false;
182
+ this.startedSinceResult.clear();
183
+ this.endDropping();
184
+ }
185
+ /** Drops the run of the abandoned prompt `uuid`. */
186
+ drop(uuid) {
187
+ this.dropping = true;
188
+ this.droppingFor = uuid;
189
+ }
190
+ endDropping() {
191
+ this.dropping = false;
192
+ this.droppingFor = undefined;
193
+ }
194
+ }
195
+ /** The ids of the tool uses in an assistant message, or in a streamed tool_use block's start. */
196
+ function toolUseIds(message) {
197
+ if (message.type === "assistant") {
198
+ const content = message.message.content;
199
+ return Array.isArray(content)
200
+ ? content.flatMap((block) => (block.type === "tool_use" ? [block.id] : []))
201
+ : [];
202
+ }
203
+ if (message.type === "stream_event") {
204
+ const event = message.event;
205
+ if (event.type === "content_block_start" && event.content_block.type === "tool_use") {
206
+ return [event.content_block.id];
207
+ }
208
+ }
209
+ return [];
210
+ }