@agentclientprotocol/claude-agent-acp 0.102.1-preview.1 → 0.102.1-preview.3

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 (229) hide show
  1. package/README.md +5 -1
  2. package/dist/acp-agent.d.ts +93 -622
  3. package/dist/acp-agent.d.ts.map +1 -1
  4. package/dist/acp-agent.js +369 -3474
  5. package/dist/acp-client.d.ts +28 -0
  6. package/dist/acp-client.d.ts.map +1 -0
  7. package/dist/acp-client.js +1 -0
  8. package/dist/acp-subagents.d.ts +1 -2
  9. package/dist/acp-subagents.d.ts.map +1 -1
  10. package/dist/auth-methods.d.ts +21 -0
  11. package/dist/auth-methods.d.ts.map +1 -0
  12. package/dist/auth-methods.js +68 -0
  13. package/dist/auth-status-reporter.d.ts +112 -0
  14. package/dist/auth-status-reporter.d.ts.map +1 -0
  15. package/dist/auth-status-reporter.js +253 -0
  16. package/dist/background-work.d.ts +12 -265
  17. package/dist/background-work.d.ts.map +1 -1
  18. package/dist/background-work.js +8 -558
  19. package/dist/client-system-prompt.d.ts +7 -0
  20. package/dist/client-system-prompt.d.ts.map +1 -1
  21. package/dist/client-system-prompt.js +24 -0
  22. package/dist/context-compaction.d.ts +20 -0
  23. package/dist/context-compaction.d.ts.map +1 -1
  24. package/dist/context-compaction.js +26 -0
  25. package/dist/context-window.d.ts +70 -0
  26. package/dist/context-window.d.ts.map +1 -0
  27. package/dist/context-window.js +132 -0
  28. package/dist/diff.d.ts +1 -1
  29. package/dist/diff.js +1 -1
  30. package/dist/elicitation.d.ts +66 -1
  31. package/dist/elicitation.d.ts.map +1 -1
  32. package/dist/elicitation.js +109 -0
  33. package/dist/file-change-audit.d.ts +4 -3
  34. package/dist/file-change-audit.d.ts.map +1 -1
  35. package/dist/file-change-audit.js +3 -2
  36. package/dist/lib.d.ts +5 -1
  37. package/dist/lib.d.ts.map +1 -1
  38. package/dist/lib.js +3 -1
  39. package/dist/logger.d.ts +10 -0
  40. package/dist/logger.d.ts.map +1 -0
  41. package/dist/logger.js +1 -0
  42. package/dist/mcp-oauth.d.ts +44 -0
  43. package/dist/mcp-oauth.d.ts.map +1 -0
  44. package/dist/mcp-oauth.js +142 -0
  45. package/dist/mcp-servers.d.ts +10 -0
  46. package/dist/mcp-servers.d.ts.map +1 -0
  47. package/dist/mcp-servers.js +29 -0
  48. package/dist/message-filters.d.ts +43 -0
  49. package/dist/message-filters.d.ts.map +1 -0
  50. package/dist/message-filters.js +197 -0
  51. package/dist/native-subagents.d.ts +18 -8
  52. package/dist/native-subagents.d.ts.map +1 -1
  53. package/dist/native-subagents.js +7 -35
  54. package/dist/new-session-meta.d.ts +40 -0
  55. package/dist/new-session-meta.d.ts.map +1 -0
  56. package/dist/new-session-meta.js +1 -0
  57. package/dist/permissions/presentation.d.ts +29 -0
  58. package/dist/permissions/presentation.d.ts.map +1 -1
  59. package/dist/permissions/presentation.js +38 -11
  60. package/dist/prompt-suggestions.d.ts +29 -0
  61. package/dist/prompt-suggestions.d.ts.map +1 -1
  62. package/dist/prompt-suggestions.js +45 -0
  63. package/dist/prompt-to-claude.d.ts +10 -0
  64. package/dist/prompt-to-claude.d.ts.map +1 -0
  65. package/dist/prompt-to-claude.js +120 -0
  66. package/dist/providers.d.ts +139 -0
  67. package/dist/providers.d.ts.map +1 -0
  68. package/dist/providers.js +286 -0
  69. package/dist/remote-control.d.ts +62 -0
  70. package/dist/remote-control.d.ts.map +1 -0
  71. package/dist/remote-control.js +78 -0
  72. package/dist/result-end.d.ts +68 -0
  73. package/dist/result-end.d.ts.map +1 -0
  74. package/dist/result-end.js +84 -0
  75. package/dist/sdk-to-acp.d.ts +93 -0
  76. package/dist/sdk-to-acp.d.ts.map +1 -0
  77. package/dist/sdk-to-acp.js +635 -0
  78. package/dist/serve.d.ts +9 -3
  79. package/dist/serve.d.ts.map +1 -1
  80. package/dist/serve.js +10 -5
  81. package/dist/session-config-ids.d.ts +1 -0
  82. package/dist/session-config-ids.d.ts.map +1 -1
  83. package/dist/session-config-ids.js +1 -0
  84. package/dist/session-config-options.d.ts +64 -0
  85. package/dist/session-config-options.d.ts.map +1 -0
  86. package/dist/session-config-options.js +162 -0
  87. package/dist/session-effort.d.ts +67 -0
  88. package/dist/session-effort.d.ts.map +1 -1
  89. package/dist/session-effort.js +98 -0
  90. package/dist/session-env.d.ts +22 -0
  91. package/dist/session-env.d.ts.map +1 -0
  92. package/dist/session-env.js +66 -0
  93. package/dist/session-fast-mode.d.ts +83 -0
  94. package/dist/session-fast-mode.d.ts.map +1 -0
  95. package/dist/session-fast-mode.js +142 -0
  96. package/dist/session-index/connection.d.ts +5 -6
  97. package/dist/session-index/connection.d.ts.map +1 -1
  98. package/dist/session-index/connection.js +3 -5
  99. package/dist/session-mode.d.ts +1 -0
  100. package/dist/session-mode.d.ts.map +1 -1
  101. package/dist/session-mode.js +1 -1
  102. package/dist/session-model.d.ts +41 -0
  103. package/dist/session-model.d.ts.map +1 -1
  104. package/dist/session-model.js +110 -45
  105. package/dist/session-notices.d.ts +44 -0
  106. package/dist/session-notices.d.ts.map +1 -1
  107. package/dist/session-notices.js +68 -0
  108. package/dist/session-titles.d.ts +34 -12
  109. package/dist/session-titles.d.ts.map +1 -1
  110. package/dist/session-titles.js +7 -6
  111. package/dist/session-usage.d.ts +125 -0
  112. package/dist/session-usage.d.ts.map +1 -0
  113. package/dist/session-usage.js +196 -0
  114. package/dist/slash-commands.d.ts +10 -0
  115. package/dist/slash-commands.d.ts.map +1 -0
  116. package/dist/slash-commands.js +99 -0
  117. package/dist/streamed-text.d.ts +33 -0
  118. package/dist/streamed-text.d.ts.map +1 -0
  119. package/dist/streamed-text.js +71 -0
  120. package/dist/subagent-configuration.d.ts +32 -0
  121. package/dist/subagent-configuration.d.ts.map +1 -0
  122. package/dist/subagent-configuration.js +70 -0
  123. package/dist/subagent-replay.d.ts +18 -0
  124. package/dist/subagent-replay.d.ts.map +1 -1
  125. package/dist/subagent-replay.js +25 -15
  126. package/dist/tool-calls/client-capabilities.d.ts +8 -8
  127. package/dist/tool-calls/client-capabilities.js +4 -4
  128. package/dist/tool-calls/renderer.d.ts +4 -5
  129. package/dist/tool-calls/renderer.d.ts.map +1 -1
  130. package/dist/tool-calls/renderer.js +4 -5
  131. package/dist/turn-events.d.ts +6 -46
  132. package/dist/turn-events.d.ts.map +1 -1
  133. package/dist/utils.d.ts +1 -1
  134. package/dist/utils.d.ts.map +1 -1
  135. package/dist/v2/agent.d.ts +7 -11
  136. package/dist/v2/agent.d.ts.map +1 -1
  137. package/dist/v2/agent.js +751 -331
  138. package/dist/v2/air.d.ts +33 -0
  139. package/dist/v2/air.d.ts.map +1 -0
  140. package/dist/v2/air.js +29 -0
  141. package/dist/v2/auth.d.ts +97 -0
  142. package/dist/v2/auth.d.ts.map +1 -0
  143. package/dist/v2/auth.js +216 -0
  144. package/dist/v2/background.d.ts +27 -16
  145. package/dist/v2/background.d.ts.map +1 -1
  146. package/dist/v2/background.js +4 -32
  147. package/dist/v2/child-config.d.ts +55 -0
  148. package/dist/v2/child-config.d.ts.map +1 -0
  149. package/dist/v2/child-config.js +92 -0
  150. package/dist/v2/clear-context.d.ts +94 -0
  151. package/dist/v2/clear-context.d.ts.map +1 -0
  152. package/dist/v2/clear-context.js +176 -0
  153. package/dist/v2/commands.d.ts +46 -0
  154. package/dist/v2/commands.d.ts.map +1 -0
  155. package/dist/v2/commands.js +54 -0
  156. package/dist/v2/config.d.ts +187 -0
  157. package/dist/v2/config.d.ts.map +1 -0
  158. package/dist/v2/config.js +494 -0
  159. package/dist/v2/elicitation.d.ts +93 -0
  160. package/dist/v2/elicitation.d.ts.map +1 -0
  161. package/dist/v2/elicitation.js +160 -0
  162. package/dist/v2/file-changes.d.ts +61 -0
  163. package/dist/v2/file-changes.d.ts.map +1 -0
  164. package/dist/v2/file-changes.js +108 -0
  165. package/dist/v2/file-preview.d.ts +18 -0
  166. package/dist/v2/file-preview.d.ts.map +1 -0
  167. package/dist/v2/file-preview.js +29 -0
  168. package/dist/v2/initialize.d.ts +10 -24
  169. package/dist/v2/initialize.d.ts.map +1 -1
  170. package/dist/v2/initialize.js +4 -111
  171. package/dist/v2/outcome.d.ts +81 -0
  172. package/dist/v2/outcome.d.ts.map +1 -0
  173. package/dist/v2/outcome.js +166 -0
  174. package/dist/v2/permissions.d.ts +92 -0
  175. package/dist/v2/permissions.d.ts.map +1 -0
  176. package/dist/v2/permissions.js +162 -0
  177. package/dist/v2/prompt.d.ts +2 -42
  178. package/dist/v2/prompt.d.ts.map +1 -1
  179. package/dist/v2/prompt.js +2 -119
  180. package/dist/v2/remote-control.d.ts +41 -0
  181. package/dist/v2/remote-control.d.ts.map +1 -0
  182. package/dist/v2/remote-control.js +63 -0
  183. package/dist/v2/reports.d.ts +198 -0
  184. package/dist/v2/reports.d.ts.map +1 -0
  185. package/dist/v2/reports.js +487 -0
  186. package/dist/v2/restarts.d.ts +128 -0
  187. package/dist/v2/restarts.d.ts.map +1 -0
  188. package/dist/v2/restarts.js +348 -0
  189. package/dist/v2/session-requests.d.ts +20 -0
  190. package/dist/v2/session-requests.d.ts.map +1 -0
  191. package/dist/v2/session-requests.js +108 -0
  192. package/dist/v2/session-update.d.ts +2 -13
  193. package/dist/v2/session-update.d.ts.map +1 -1
  194. package/dist/v2/session-update.js +2 -22
  195. package/dist/v2/session.d.ts +340 -41
  196. package/dist/v2/session.d.ts.map +1 -1
  197. package/dist/v2/session.js +1224 -141
  198. package/dist/v2/setup.d.ts +49 -0
  199. package/dist/v2/setup.d.ts.map +1 -0
  200. package/dist/v2/setup.js +62 -0
  201. package/dist/v2/stopped-work.d.ts +123 -0
  202. package/dist/v2/stopped-work.d.ts.map +1 -0
  203. package/dist/v2/stopped-work.js +210 -0
  204. package/dist/v2/stream.d.ts +229 -0
  205. package/dist/v2/stream.d.ts.map +1 -0
  206. package/dist/v2/stream.js +507 -0
  207. package/dist/v2/subagent-replay.d.ts +115 -0
  208. package/dist/v2/subagent-replay.d.ts.map +1 -0
  209. package/dist/v2/subagent-replay.js +479 -0
  210. package/dist/v2/subscription-guard.d.ts +60 -0
  211. package/dist/v2/subscription-guard.d.ts.map +1 -0
  212. package/dist/v2/subscription-guard.js +84 -0
  213. package/dist/v2/tasks.d.ts +492 -0
  214. package/dist/v2/tasks.d.ts.map +1 -0
  215. package/dist/v2/tasks.js +1728 -0
  216. package/dist/v2/terminal.d.ts.map +1 -1
  217. package/dist/v2/terminal.js +6 -1
  218. package/dist/v2/tool-call.d.ts +3 -2
  219. package/dist/v2/tool-call.d.ts.map +1 -1
  220. package/dist/v2/update-translator.d.ts +36 -0
  221. package/dist/v2/update-translator.d.ts.map +1 -0
  222. package/dist/v2/update-translator.js +80 -0
  223. package/dist/workflow-progress.d.ts +23 -0
  224. package/dist/workflow-progress.d.ts.map +1 -1
  225. package/dist/workflow-progress.js +31 -8
  226. package/package.json +1 -1
  227. package/dist/v2/permission.d.ts +0 -28
  228. package/dist/v2/permission.d.ts.map +0 -1
  229. package/dist/v2/permission.js +0 -41
@@ -0,0 +1,348 @@
1
+ /**
2
+ * Restarts of a v2 session's query: a new
3
+ * query resumes the same Claude session under the same id, and takes over
4
+ * from the old one between turns.
5
+ *
6
+ * Claude Code reads the LLM route (the env of a provider or a gateway login)
7
+ * and its account once, when its process starts. So a query must start again
8
+ * when either changes:
9
+ *
10
+ * - The route: `providers/set`, `providers/disable`, and an `auth/logout`
11
+ * that drops a provider or a gateway login. Each live session restarts at
12
+ * its next turn boundary: no turn is open, no prompt of the session waits
13
+ * to go to Claude Code or in its queue, and no subagent runs, background
14
+ * ones included. A session at a turn boundary restarts right away, and the
15
+ * request that changed the route answers once it did. A busy session's
16
+ * restart is due at its next turn boundary, which the request does not
17
+ * wait for: until then the session keeps its query, and its prompts go to
18
+ * that query under the old route (the provider RFD lets an Agent leave
19
+ * running sessions on it). That restart starts under the route in force
20
+ * when it runs. A session whose query start read the route before the
21
+ * change, and that became live after it, restarts the same way once its
22
+ * setup response went out.
23
+ * A session that cannot start again stops: its requests then fail with
24
+ * what happened, until a resume starts it again.
25
+ * - The additional directories: a resume of the active session with other
26
+ * ones restarts it as for a new route, and does not wait for it. Claude
27
+ * Code reads them once, when its process starts, and the SDK has no call
28
+ * that changes them on a running query.
29
+ * - The account, under `--hide-claude-auth` (see `subscription-guard.ts`):
30
+ * the next prompt that finds the session at a turn boundary restarts it
31
+ * first, so the guard judges the new account. If the restart fails, such
32
+ * as when the guard refuses the account, the prompt fails with its error
33
+ * and the session stays as it was, for a retry after a sign-in.
34
+ *
35
+ * The new query starts before the old one ends, so a failed start leaves the
36
+ * session as it was. The old one ends at its next turn boundary after that,
37
+ * so work that started on it during the start, such as a turn that Claude
38
+ * Code started on its own, is not cut. Its background commands end with it
39
+ * (their terminals exit), as when a query ends. The new query starts without
40
+ * the effort of the setup's `_meta`, and the client's choices are set again
41
+ * on it (`SessionConfig.restoreFrom`), which then reports its options in a
42
+ * `config_option_update`. Ending the old query withdraws its MCP OAuth
43
+ * runs, and the new one starts OAuth for the setup's MCP servers that still
44
+ * need it, as a setup does. One restart of a session runs at
45
+ * a time: a route restart waits for an account restart that runs, as for a
46
+ * busy session.
47
+ *
48
+ * Prompts wait only for a restart that runs, from the new query's start to
49
+ * the old one's end, and go to the new query. A `session/cancel` or
50
+ * `session/close` answers them with -32800, and stops the work that runs on
51
+ * the old query while the restart waits for it.
52
+ */
53
+ import { RequestError } from "@agentclientprotocol/sdk/unstable";
54
+ import { withoutEffort } from "../session-effort.js";
55
+ import { afterSetupResponse } from "./setup.js";
56
+ export class SessionRestarts {
57
+ host;
58
+ /** The setup request of each live session. */
59
+ setups = new Map();
60
+ /** The sessions whose account may have changed: their next prompt at a turn boundary restarts them. */
61
+ accountChanged = new Set();
62
+ /** The route restarts that wait for the session's turn boundary. Prompts do not wait for them. */
63
+ reroutes = new Map();
64
+ /** The restarts that run: from the new query's start to the old one's end. */
65
+ swaps = new Map();
66
+ /** The session that each running restart replaces. */
67
+ retiring = new Map();
68
+ /** The sessions that `session/close` closes: a restart that runs does not track them again. */
69
+ closingSessions = new Set();
70
+ /**
71
+ * The sessions whose start read the route before a change that
72
+ * {@link reroute} did not find them live for: they restart once their
73
+ * setup answered ({@link setupAnswered}).
74
+ */
75
+ staleRoutes = new Set();
76
+ /** The prompts that wait for a restart, each with what rejects it. */
77
+ held = new Map();
78
+ constructor(host) {
79
+ this.host = host;
80
+ }
81
+ /**
82
+ * The session started from `params`, or a resume of the active session
83
+ * brought new ones. `staleRoute`: the start read the route before a route
84
+ * change came.
85
+ */
86
+ track(sessionId, params, { staleRoute = false } = {}) {
87
+ if (this.closingSessions.has(sessionId))
88
+ return;
89
+ this.setups.set(sessionId, params);
90
+ // A restart's start: the change found the session it replaces live, and
91
+ // its reroute runs again after this restart.
92
+ if (staleRoute && !this.swaps.has(sessionId))
93
+ this.staleRoutes.add(sessionId);
94
+ }
95
+ /**
96
+ * The setup response of `sessionId` goes out: a route restart that its
97
+ * start missed runs from then, as for a live session (see {@link reroute}).
98
+ * Not before: the restart's updates must not precede the response.
99
+ */
100
+ setupAnswered(sessionId) {
101
+ if (!this.staleRoutes.delete(sessionId))
102
+ return;
103
+ // The handler returns the response right after this call, and the SDK
104
+ // writes it before a timer runs. Nothing else orders the two.
105
+ setTimeout(() => {
106
+ if (this.setups.has(sessionId))
107
+ void this.rerouteSession(sessionId);
108
+ }, 0);
109
+ }
110
+ /**
111
+ * A resume of the active session brought other additional directories:
112
+ * the session restarts with `params`, as for a new route (see the module
113
+ * doc). Does not wait for it.
114
+ */
115
+ rootsChanged(sessionId, params) {
116
+ this.track(sessionId, params);
117
+ void this.rerouteSession(sessionId);
118
+ }
119
+ /**
120
+ * The session is no longer active, such as when its query ended. A route
121
+ * restart that is due does not run.
122
+ */
123
+ forget(sessionId) {
124
+ this.setups.delete(sessionId);
125
+ this.accountChanged.delete(sessionId);
126
+ this.staleRoutes.delete(sessionId);
127
+ }
128
+ /**
129
+ * The route changed: restarts every live session at its next turn
130
+ * boundary. Resolves once each session that is at one now restarted, or
131
+ * was closed or lost. Their restarts start in this call, so a prompt that
132
+ * comes after it waits for them (`beforePrompt`). The busy sessions'
133
+ * restarts are only due: a failure of one stops its session later
134
+ * (`RestartHost.lost`).
135
+ */
136
+ async reroute() {
137
+ await Promise.all([...this.setups.keys()].map((sessionId) => this.rerouteSession(sessionId)));
138
+ }
139
+ /** The session's account may have changed: its next prompt at a turn boundary restarts it. */
140
+ accountMayHaveChanged(sessionId) {
141
+ if (this.setups.has(sessionId))
142
+ this.accountChanged.add(sessionId);
143
+ }
144
+ /**
145
+ * What a prompt of `sessionId` waits for before it goes to the session: a
146
+ * restart that runs, or an account restart that is due. `undefined` when
147
+ * none is: the prompt goes on in the same call, so prompts keep their
148
+ * order. A route restart that waits for a turn boundary does not hold it:
149
+ * the prompt goes to the current query.
150
+ */
151
+ beforePrompt(sessionId) {
152
+ const running = this.swaps.get(sessionId);
153
+ if (running)
154
+ return this.hold(sessionId, running);
155
+ const session = this.host.active(sessionId);
156
+ if (this.accountChanged.has(sessionId) && session?.atTurnBoundary(true)) {
157
+ return this.hold(sessionId, this.swap(sessionId, session, { keepOnFailure: true }));
158
+ }
159
+ return undefined;
160
+ }
161
+ /**
162
+ * Whether a restart runs: its new query starts MCP OAuth once it took
163
+ * over, so the setup response of the session it replaces need not.
164
+ */
165
+ restarting(sessionId) {
166
+ return this.swaps.has(sessionId);
167
+ }
168
+ /** What a request other than a prompt waits for: a restart that runs. */
169
+ settled(sessionId) {
170
+ return this.swaps.get(sessionId)?.catch(() => { });
171
+ }
172
+ /**
173
+ * `session/cancel`: the prompts that wait for a restart fail with -32800,
174
+ * and work that runs on the query that a restart replaces stops too (the
175
+ * restart waits for it).
176
+ */
177
+ cancelled(sessionId) {
178
+ this.release(sessionId, RequestError.requestCancelled());
179
+ const retiring = this.retiring.get(sessionId);
180
+ // Until the new query started, the old one is the active session, which the cancel stops.
181
+ if (retiring && retiring !== this.host.active(sessionId))
182
+ void retiring.cancel();
183
+ }
184
+ /**
185
+ * `session/close`: the prompts that wait fail with -32800, and a route
186
+ * restart that waits for a turn boundary is dropped (the closed session
187
+ * reaches none). Resolves once a restart that runs is done, so the close
188
+ * ends the session that it started.
189
+ */
190
+ async closing(sessionId) {
191
+ this.closingSessions.add(sessionId);
192
+ this.forget(sessionId);
193
+ this.release(sessionId, RequestError.requestCancelled());
194
+ // The close stops the old query's work too, which the restart waits for.
195
+ await this.retiring.get(sessionId)?.close();
196
+ await this.swaps.get(sessionId)?.catch(() => { });
197
+ }
198
+ /** `session/close` closed the session. */
199
+ closed(sessionId) {
200
+ this.closingSessions.delete(sessionId);
201
+ this.forget(sessionId);
202
+ }
203
+ /** Waits for the restarts that run, so no new query starts after the agent's sessions closed. */
204
+ async dispose() {
205
+ for (const sessionId of [...this.setups.keys(), ...this.retiring.keys()]) {
206
+ this.closingSessions.add(sessionId);
207
+ this.forget(sessionId);
208
+ this.release(sessionId, RequestError.requestCancelled());
209
+ }
210
+ await Promise.all([...this.retiring.values()].map((session) => session.close()));
211
+ await Promise.all([...this.swaps.values()].map((swap) => swap.catch(() => { })));
212
+ }
213
+ /**
214
+ * Restarts the session for a new route. At a turn boundary, with no restart
215
+ * that runs, the restart starts in this call, and the result resolves once
216
+ * it is done. Otherwise it is due at the next turn boundary, and the result
217
+ * resolves at once.
218
+ */
219
+ async rerouteSession(sessionId) {
220
+ const failed = (error) => {
221
+ this.host.logger.error(`Session ${sessionId}: could not restart for the new route`, error);
222
+ };
223
+ const session = this.host.active(sessionId);
224
+ if (session && !this.swaps.has(sessionId) && session.atTurnBoundary(true)) {
225
+ // A restart due at the boundary that came has not resumed yet: this
226
+ // one runs in its place.
227
+ this.reroutes.delete(sessionId);
228
+ await this.swap(sessionId, session, { keepOnFailure: false }).catch(failed);
229
+ return;
230
+ }
231
+ const waiting = this.reroutes.get(sessionId);
232
+ // One that waits starts under the route in force when it runs. One that
233
+ // started may have read the route before this change: it runs again.
234
+ if (waiting && !waiting.started)
235
+ return;
236
+ const reroute = { done: Promise.resolve(), started: false };
237
+ const current = () => this.reroutes.get(sessionId) === reroute;
238
+ reroute.done = (async () => {
239
+ await waiting?.done;
240
+ let session;
241
+ for (;;) {
242
+ // After an account restart that runs, on the session it started.
243
+ await this.swaps.get(sessionId)?.catch(() => { });
244
+ session = this.host.active(sessionId);
245
+ if (!current() || !session)
246
+ return;
247
+ // `false` once the session closed.
248
+ if (!(await session.turnBoundary(true)) || !current())
249
+ return;
250
+ // A restart can start at the boundary too, and a prompt that it held
251
+ // can end the boundary again before this resumes: wait again then.
252
+ if (!this.swaps.has(sessionId) &&
253
+ this.host.active(sessionId) === session &&
254
+ session.atTurnBoundary(true)) {
255
+ break;
256
+ }
257
+ }
258
+ if (!this.setups.has(sessionId))
259
+ return;
260
+ reroute.started = true;
261
+ await this.swap(sessionId, session, { keepOnFailure: false });
262
+ })()
263
+ .catch(failed)
264
+ .finally(() => {
265
+ if (current())
266
+ this.reroutes.delete(sessionId);
267
+ });
268
+ this.reroutes.set(sessionId, reroute);
269
+ }
270
+ /**
271
+ * Starts the new query, then ends the old one and sets the client's choices
272
+ * on the new one. With `keepOnFailure`, a failed start rejects and leaves
273
+ * `previous` active. Otherwise the session is lost.
274
+ */
275
+ swap(sessionId, previous, { keepOnFailure }) {
276
+ const run = (async () => {
277
+ const params = this.setups.get(sessionId);
278
+ if (!params)
279
+ return;
280
+ this.host.logger.log(`Restarting the query of session ${sessionId}`);
281
+ let session;
282
+ try {
283
+ // The client's effort pin is restored below instead.
284
+ session = await this.host.start(sessionId, withoutEffort(params));
285
+ }
286
+ catch (error) {
287
+ if (keepOnFailure)
288
+ throw error;
289
+ this.host.logger.error(`Session ${sessionId}: could not start again`, error);
290
+ await this.host.lost(sessionId, previous);
291
+ return;
292
+ }
293
+ // The new query read the account in force.
294
+ this.accountChanged.delete(sessionId);
295
+ session.reports.restartedFrom(previous.reports);
296
+ // Work that started on the old query while the new one started, such
297
+ // as a turn that Claude Code started on its own, ends first. New
298
+ // prompts wait for this restart meanwhile (`beforePrompt`).
299
+ await previous.turnBoundary(true);
300
+ await previous.retire();
301
+ // A close during the restart closes the new session next.
302
+ if (this.closingSessions.has(sessionId))
303
+ return;
304
+ await session.config.restoreFrom(previous.config);
305
+ this.host.send({
306
+ sessionId,
307
+ update: { sessionUpdate: "config_option_update", configOptions: session.config.options() },
308
+ });
309
+ // Closing the old query withdrew its MCP OAuth runs. The servers of a
310
+ // resume that came during the restart are tracked by now.
311
+ afterSetupResponse(session, this.setups.get(sessionId) ?? params);
312
+ })().finally(() => {
313
+ if (this.swaps.get(sessionId) === run)
314
+ this.swaps.delete(sessionId);
315
+ if (this.retiring.get(sessionId) === previous)
316
+ this.retiring.delete(sessionId);
317
+ });
318
+ this.retiring.set(sessionId, previous);
319
+ this.swaps.set(sessionId, run);
320
+ return run;
321
+ }
322
+ /** Waits for `work`, unless a cancel or close of the session comes first. */
323
+ hold(sessionId, work) {
324
+ return new Promise((resolve, reject) => {
325
+ let holds = this.held.get(sessionId);
326
+ if (!holds)
327
+ this.held.set(sessionId, (holds = new Set()));
328
+ const settle = (error) => {
329
+ if (!holds.delete(settle))
330
+ return;
331
+ if (holds.size === 0 && this.held.get(sessionId) === holds)
332
+ this.held.delete(sessionId);
333
+ if (error === undefined)
334
+ resolve();
335
+ else
336
+ reject(error);
337
+ };
338
+ holds.add(settle);
339
+ work.then(() => settle(), (error) => settle(error ?? new Error("The restart failed")));
340
+ });
341
+ }
342
+ release(sessionId, error) {
343
+ const holds = this.held.get(sessionId);
344
+ this.held.delete(sessionId);
345
+ for (const settle of [...(holds ?? [])])
346
+ settle(error);
347
+ }
348
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The v1 form of the v2 session setup and config requests, which the shared
3
+ * session modules take (`sdkMcpServers`, `forkSession`, `configOptionValue`),
4
+ * and the v2 form of the config options and commands that they build.
5
+ */
6
+ import { type AvailableCommand, type ForkSessionRequest, type NewSessionRequest, type SessionConfigOption, type SetSessionConfigOptionRequest } from "@agentclientprotocol/sdk/unstable";
7
+ import * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
8
+ export declare function v1NewSessionRequest(params: v2.NewSessionRequest): NewSessionRequest;
9
+ /** The v1 request of a v2 `session/fork`. */
10
+ export declare function v1ForkSessionRequest(params: v2.ForkSessionRequest): ForkSessionRequest;
11
+ /** v2 tags a select value with `type: "id"`; v1 leaves it untagged. */
12
+ export declare function v1SetSessionConfigOptionRequest(params: v2.SetSessionConfigOptionRequest): SetSessionConfigOptionRequest;
13
+ /** v2 names the id of an option `configId` and the id of a select group `groupId`. */
14
+ export declare function v2ConfigOptions(options: SessionConfigOption[]): v2.SessionConfigOption[];
15
+ /**
16
+ * v2 tags each command with `type: "command"` and its free-text input with
17
+ * `type: "text"`. A receiver drops a command without the `type` tag.
18
+ */
19
+ export declare function v2AvailableCommands(commands: AvailableCommand[]): v2.AvailableCommand[];
20
+ //# sourceMappingURL=session-requests.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-requests.d.ts","sourceRoot":"","sources":["../../src/v2/session-requests.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EAEvB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EAGxB,KAAK,6BAA6B,EAEnC,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,mDAAmD,CAAC;AAIxE,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,EAAE,CAAC,iBAAiB,GAAG,iBAAiB,CAQnF;AAED,6CAA6C;AAC7C,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,EAAE,CAAC,kBAAkB,GAAG,kBAAkB,CAOtF;AAaD,uEAAuE;AACvE,wBAAgB,+BAA+B,CAC7C,MAAM,EAAE,EAAE,CAAC,6BAA6B,GACvC,6BAA6B,CAgB/B;AAED,sFAAsF;AACtF,wBAAgB,eAAe,CAAC,OAAO,EAAE,mBAAmB,EAAE,GAAG,EAAE,CAAC,mBAAmB,EAAE,CAQxF;AAWD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,EAAE,CAAC,gBAAgB,EAAE,CAMvF"}
@@ -0,0 +1,108 @@
1
+ /**
2
+ * The v1 form of the v2 session setup and config requests, which the shared
3
+ * session modules take (`sdkMcpServers`, `forkSession`, `configOptionValue`),
4
+ * and the v2 form of the config options and commands that they build.
5
+ */
6
+ import { RequestError, } from "@agentclientprotocol/sdk/unstable";
7
+ import * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
8
+ import { V2_SESSION_CAPABILITIES } from "./initialize.js";
9
+ import { v1ContentBlock } from "./prompt.js";
10
+ export function v1NewSessionRequest(params) {
11
+ const { mcpServers, systemPrompt, ...request } = params;
12
+ // v1 requires the list; v2 treats an omitted list as an empty one.
13
+ return {
14
+ ...request,
15
+ mcpServers: v1McpServers(mcpServers) ?? [],
16
+ ...v1SystemPromptField(systemPrompt),
17
+ };
18
+ }
19
+ /** The v1 request of a v2 `session/fork`. */
20
+ export function v1ForkSessionRequest(params) {
21
+ const { mcpServers, systemPrompt, ...request } = params;
22
+ return {
23
+ ...request,
24
+ ...(mcpServers ? { mcpServers: v1McpServers(mcpServers) } : {}),
25
+ ...v1SystemPromptField(systemPrompt),
26
+ };
27
+ }
28
+ /**
29
+ * The v1 `systemPrompt`. Its content converts like prompt content: a v2
30
+ * annotation audience can be any string, and v1 takes only `user` or `assistant`.
31
+ */
32
+ function v1SystemPromptField(systemPrompt) {
33
+ if (systemPrompt == null)
34
+ return {};
35
+ return { systemPrompt: { ...systemPrompt, content: systemPrompt.content.map(v1ContentBlock) } };
36
+ }
37
+ /** v2 tags a select value with `type: "id"`; v1 leaves it untagged. */
38
+ export function v1SetSessionConfigOptionRequest(params) {
39
+ const request = {
40
+ sessionId: params.sessionId,
41
+ configId: params.configId,
42
+ ...(params._meta != null ? { _meta: params._meta } : {}),
43
+ };
44
+ if (v2.SetSessionConfigOptionRequest.isId(params)) {
45
+ return { ...request, value: params.value };
46
+ }
47
+ if (v2.SetSessionConfigOptionRequest.isBoolean(params)) {
48
+ return { ...request, type: "boolean", value: params.value };
49
+ }
50
+ throw RequestError.invalidParams({ type: params.type }, `Config option values of type ${params.type} are not supported`);
51
+ }
52
+ /** v2 names the id of an option `configId` and the id of a select group `groupId`. */
53
+ export function v2ConfigOptions(options) {
54
+ return options.map((option) => {
55
+ const { id, ...rest } = option;
56
+ if (rest.type === "select") {
57
+ return { ...rest, configId: id, options: v2SelectOptions(rest.options) };
58
+ }
59
+ return { ...rest, configId: id };
60
+ });
61
+ }
62
+ function v2SelectOptions(options) {
63
+ if (!isGrouped(options))
64
+ return options;
65
+ return options.map(({ group, ...selectGroup }) => ({ ...selectGroup, groupId: group }));
66
+ }
67
+ function isGrouped(options) {
68
+ return options.some((entry) => "group" in entry);
69
+ }
70
+ /**
71
+ * v2 tags each command with `type: "command"` and its free-text input with
72
+ * `type: "text"`. A receiver drops a command without the `type` tag.
73
+ */
74
+ export function v2AvailableCommands(commands) {
75
+ return commands.map(({ input, ...command }) => ({
76
+ type: "command",
77
+ ...command,
78
+ ...(input != null ? { input: { type: "text", ...input } } : {}),
79
+ }));
80
+ }
81
+ /**
82
+ * The v1 list of the MCP servers of a v2 setup request, or undefined for no
83
+ * list. A request with two servers of the same `name`, or with a transport
84
+ * that the agent does not advertise in `session.mcp` (such as `acp` or a
85
+ * custom `_` type), fails with invalid params (-32602): `sdkMcpServers` would
86
+ * otherwise drop the server without telling the client.
87
+ */
88
+ function v1McpServers(servers) {
89
+ if (servers == null)
90
+ return undefined;
91
+ v2.assertSupportedMcpServers(servers, V2_SESSION_CAPABILITIES.mcp);
92
+ return servers.map(v1McpServer);
93
+ }
94
+ /**
95
+ * v2 requires a `type` on every transport and makes the empty lists optional.
96
+ * `sdkMcpServers` recognizes a v1 stdio server by its missing `type`, so the
97
+ * v2 tag is dropped. {@link v1McpServers} has already rejected every other transport.
98
+ */
99
+ function v1McpServer(server) {
100
+ if (v2.McpServer.isStdio(server)) {
101
+ const { name, command, args, env, _meta } = server;
102
+ return { name, command, args: args ?? [], env: env ?? [], ...(_meta != null ? { _meta } : {}) };
103
+ }
104
+ if (v2.McpServer.isHttp(server)) {
105
+ return { ...server, headers: server.headers ?? [] };
106
+ }
107
+ throw new Error(`Unreachable: MCP server of type ${server.type} passed the transport check`);
108
+ }
@@ -1,6 +1,6 @@
1
1
  /**
2
- * The v2 form of the session updates that `ClaudeAcpAgent` sends as v1
3
- * updates.
2
+ * The v2 form of the v1 session updates that the shared SDK-to-ACP mapping
3
+ * produces (`toAcpNotifications`).
4
4
  */
5
5
  import * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
6
6
  import type { AcpSessionNotification } from "../acp-subagents.js";
@@ -12,15 +12,4 @@ import type { AcpSessionNotification } from "../acp-subagents.js";
12
12
  * an error rather than a silently missing update.
13
13
  */
14
14
  export declare function v2SessionUpdate(update: AcpSessionNotification["update"]): v2.SessionUpdate | undefined;
15
- /**
16
- * The child's own `state_update` for a v1 `subagent_update` that reports the
17
- * child's work state, or `undefined` when it reports none.
18
- *
19
- * v1 reports a child's work state only on its parent, as the `state` of the
20
- * `subagent_update`. v2 reports it on the child's own stream, as for any
21
- * session, and mirrors it on the parent (the subagents RFD). The child's copy
22
- * goes right after the parent's: never before the announcement, and both
23
- * copies of a state before any later state.
24
- */
25
- export declare function v2ChildStateUpdate(update: AcpSessionNotification["update"]): v2.UpdateSessionNotification | undefined;
26
15
  //# sourceMappingURL=session-update.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"session-update.d.ts","sourceRoot":"","sources":["../../src/v2/session-update.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,MAAM,mDAAmD,CAAC;AACxE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAKlE;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,sBAAsB,CAAC,QAAQ,CAAC,GACvC,EAAE,CAAC,aAAa,GAAG,SAAS,CAwD9B;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,sBAAsB,CAAC,QAAQ,CAAC,GACvC,EAAE,CAAC,yBAAyB,GAAG,SAAS,CAM1C"}
1
+ {"version":3,"file":"session-update.d.ts","sourceRoot":"","sources":["../../src/v2/session-update.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,KAAK,EAAE,MAAM,mDAAmD,CAAC;AACxE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAKlE;;;;;;GAMG;AACH,wBAAgB,eAAe,CAC7B,MAAM,EAAE,sBAAsB,CAAC,QAAQ,CAAC,GACvC,EAAE,CAAC,aAAa,GAAG,SAAS,CAsD9B"}
@@ -1,4 +1,4 @@
1
- import { v2AvailableCommands, v2ConfigOptions } from "./session.js";
1
+ import { v2AvailableCommands, v2ConfigOptions } from "./session-requests.js";
2
2
  import { checklistPlanOperation } from "../plans.js";
3
3
  import { v2ToolCallUpdate } from "./tool-call.js";
4
4
  /**
@@ -44,9 +44,7 @@ export function v2SessionUpdate(update) {
44
44
  // v1's draft plan operations have v2's shape.
45
45
  return update;
46
46
  case "subagent_update": {
47
- // The subagents RFD gives it the same shape in v1 and v2. One that
48
- // reports a state also has a copy on the child's stream (see
49
- // `v2ChildStateUpdate`).
47
+ // The subagents RFD gives it the same shape in v1 and v2.
50
48
  const { state, ...association } = update;
51
49
  return { ...association, ...(state != null ? { state: v2SubagentState(state) } : {}) };
52
50
  }
@@ -63,24 +61,6 @@ export function v2SessionUpdate(update) {
63
61
  throw new Error(`The ACP v2 surface does not translate ${update.sessionUpdate} session updates yet`);
64
62
  }
65
63
  }
66
- /**
67
- * The child's own `state_update` for a v1 `subagent_update` that reports the
68
- * child's work state, or `undefined` when it reports none.
69
- *
70
- * v1 reports a child's work state only on its parent, as the `state` of the
71
- * `subagent_update`. v2 reports it on the child's own stream, as for any
72
- * session, and mirrors it on the parent (the subagents RFD). The child's copy
73
- * goes right after the parent's: never before the announcement, and both
74
- * copies of a state before any later state.
75
- */
76
- export function v2ChildStateUpdate(update) {
77
- if (update.sessionUpdate !== "subagent_update" || update.state == null)
78
- return undefined;
79
- return {
80
- sessionId: update.sessionId,
81
- update: { sessionUpdate: "state_update", ...v2SubagentState(update.state) },
82
- };
83
- }
84
64
  /** The work states of the subagents RFD, and the stop reasons of its `idle`. */
85
65
  const SUBAGENT_STATES = new Set([
86
66
  "running",