@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.
- package/README.md +5 -1
- package/dist/acp-agent.d.ts +119 -439
- package/dist/acp-agent.d.ts.map +1 -1
- package/dist/acp-agent.js +380 -2412
- package/dist/acp-subagents.d.ts +1 -2
- package/dist/acp-subagents.d.ts.map +1 -1
- package/dist/auth-methods.d.ts +21 -0
- package/dist/auth-methods.d.ts.map +1 -0
- package/dist/auth-methods.js +68 -0
- package/dist/auth-status-reporter.d.ts +112 -0
- package/dist/auth-status-reporter.d.ts.map +1 -0
- package/dist/auth-status-reporter.js +253 -0
- package/dist/background-work.d.ts +12 -265
- package/dist/background-work.d.ts.map +1 -1
- package/dist/background-work.js +8 -558
- package/dist/client-system-prompt.d.ts +7 -0
- package/dist/client-system-prompt.d.ts.map +1 -1
- package/dist/client-system-prompt.js +24 -0
- package/dist/context-compaction.d.ts +20 -0
- package/dist/context-compaction.d.ts.map +1 -1
- package/dist/context-compaction.js +26 -0
- package/dist/context-window.d.ts +70 -0
- package/dist/context-window.d.ts.map +1 -0
- package/dist/context-window.js +132 -0
- package/dist/diff.d.ts +1 -1
- package/dist/diff.js +1 -1
- package/dist/elicitation.d.ts +66 -1
- package/dist/elicitation.d.ts.map +1 -1
- package/dist/elicitation.js +109 -0
- package/dist/file-change-audit.d.ts +4 -3
- package/dist/file-change-audit.d.ts.map +1 -1
- package/dist/file-change-audit.js +3 -2
- package/dist/mcp-oauth.d.ts +44 -0
- package/dist/mcp-oauth.d.ts.map +1 -0
- package/dist/mcp-oauth.js +142 -0
- package/dist/mcp-servers.d.ts +10 -0
- package/dist/mcp-servers.d.ts.map +1 -0
- package/dist/mcp-servers.js +29 -0
- package/dist/native-subagents.d.ts +18 -8
- package/dist/native-subagents.d.ts.map +1 -1
- package/dist/native-subagents.js +7 -35
- package/dist/permissions/presentation.d.ts +29 -0
- package/dist/permissions/presentation.d.ts.map +1 -1
- package/dist/permissions/presentation.js +38 -11
- package/dist/plans.d.ts +36 -6
- package/dist/plans.d.ts.map +1 -1
- package/dist/plans.js +49 -11
- package/dist/prompt-suggestions.d.ts +29 -0
- package/dist/prompt-suggestions.d.ts.map +1 -1
- package/dist/prompt-suggestions.js +45 -0
- package/dist/providers.d.ts +139 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +286 -0
- package/dist/remote-control.d.ts +62 -0
- package/dist/remote-control.d.ts.map +1 -0
- package/dist/remote-control.js +78 -0
- package/dist/result-end.d.ts +39 -0
- package/dist/result-end.d.ts.map +1 -0
- package/dist/result-end.js +50 -0
- package/dist/serve.d.ts +7 -2
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +10 -5
- package/dist/session-config-ids.d.ts +1 -0
- package/dist/session-config-ids.d.ts.map +1 -1
- package/dist/session-config-ids.js +1 -0
- package/dist/session-config-options.d.ts +64 -0
- package/dist/session-config-options.d.ts.map +1 -0
- package/dist/session-config-options.js +162 -0
- package/dist/session-effort.d.ts +67 -0
- package/dist/session-effort.d.ts.map +1 -1
- package/dist/session-effort.js +98 -0
- package/dist/session-env.d.ts +22 -0
- package/dist/session-env.d.ts.map +1 -0
- package/dist/session-env.js +66 -0
- package/dist/session-fast-mode.d.ts +83 -0
- package/dist/session-fast-mode.d.ts.map +1 -0
- package/dist/session-fast-mode.js +142 -0
- package/dist/session-index/connection.d.ts +2 -5
- package/dist/session-index/connection.d.ts.map +1 -1
- package/dist/session-index/connection.js +3 -5
- package/dist/session-mode.d.ts +1 -0
- package/dist/session-mode.d.ts.map +1 -1
- package/dist/session-mode.js +1 -1
- package/dist/session-model.d.ts +41 -0
- package/dist/session-model.d.ts.map +1 -1
- package/dist/session-model.js +110 -45
- package/dist/session-notices.d.ts +44 -0
- package/dist/session-notices.d.ts.map +1 -1
- package/dist/session-notices.js +68 -0
- package/dist/session-titles.d.ts +34 -12
- package/dist/session-titles.d.ts.map +1 -1
- package/dist/session-titles.js +7 -6
- package/dist/session-usage.d.ts +125 -0
- package/dist/session-usage.d.ts.map +1 -0
- package/dist/session-usage.js +196 -0
- package/dist/streamed-text.d.ts +33 -0
- package/dist/streamed-text.d.ts.map +1 -0
- package/dist/streamed-text.js +71 -0
- package/dist/subagent-configuration.d.ts +32 -0
- package/dist/subagent-configuration.d.ts.map +1 -0
- package/dist/subagent-configuration.js +70 -0
- package/dist/subagent-replay.d.ts +18 -0
- package/dist/subagent-replay.d.ts.map +1 -1
- package/dist/subagent-replay.js +25 -15
- package/dist/tool-calls/client-capabilities.d.ts +8 -8
- package/dist/tool-calls/client-capabilities.js +4 -4
- package/dist/tool-calls/renderer.d.ts +4 -5
- package/dist/tool-calls/renderer.d.ts.map +1 -1
- package/dist/tool-calls/renderer.js +4 -5
- package/dist/turn-events.d.ts +6 -46
- package/dist/turn-events.d.ts.map +1 -1
- package/dist/v2/agent.d.ts +7 -11
- package/dist/v2/agent.d.ts.map +1 -1
- package/dist/v2/agent.js +751 -331
- package/dist/v2/air.d.ts +33 -0
- package/dist/v2/air.d.ts.map +1 -0
- package/dist/v2/air.js +29 -0
- package/dist/v2/auth.d.ts +97 -0
- package/dist/v2/auth.d.ts.map +1 -0
- package/dist/v2/auth.js +216 -0
- package/dist/v2/background.d.ts +27 -16
- package/dist/v2/background.d.ts.map +1 -1
- package/dist/v2/background.js +4 -32
- package/dist/v2/child-config.d.ts +55 -0
- package/dist/v2/child-config.d.ts.map +1 -0
- package/dist/v2/child-config.js +92 -0
- package/dist/v2/clear-context.d.ts +94 -0
- package/dist/v2/clear-context.d.ts.map +1 -0
- package/dist/v2/clear-context.js +176 -0
- package/dist/v2/commands.d.ts +46 -0
- package/dist/v2/commands.d.ts.map +1 -0
- package/dist/v2/commands.js +54 -0
- package/dist/v2/config.d.ts +187 -0
- package/dist/v2/config.d.ts.map +1 -0
- package/dist/v2/config.js +494 -0
- package/dist/v2/elicitation.d.ts +93 -0
- package/dist/v2/elicitation.d.ts.map +1 -0
- package/dist/v2/elicitation.js +160 -0
- package/dist/v2/file-changes.d.ts +61 -0
- package/dist/v2/file-changes.d.ts.map +1 -0
- package/dist/v2/file-changes.js +108 -0
- package/dist/v2/file-preview.d.ts +18 -0
- package/dist/v2/file-preview.d.ts.map +1 -0
- package/dist/v2/file-preview.js +29 -0
- package/dist/v2/initialize.d.ts +10 -24
- package/dist/v2/initialize.d.ts.map +1 -1
- package/dist/v2/initialize.js +4 -111
- package/dist/v2/outcome.d.ts +81 -0
- package/dist/v2/outcome.d.ts.map +1 -0
- package/dist/v2/outcome.js +165 -0
- package/dist/v2/permissions.d.ts +92 -0
- package/dist/v2/permissions.d.ts.map +1 -0
- package/dist/v2/permissions.js +162 -0
- package/dist/v2/prompt.d.ts +2 -42
- package/dist/v2/prompt.d.ts.map +1 -1
- package/dist/v2/prompt.js +2 -119
- package/dist/v2/remote-control.d.ts +41 -0
- package/dist/v2/remote-control.d.ts.map +1 -0
- package/dist/v2/remote-control.js +63 -0
- package/dist/v2/reports.d.ts +198 -0
- package/dist/v2/reports.d.ts.map +1 -0
- package/dist/v2/reports.js +487 -0
- package/dist/v2/restarts.d.ts +128 -0
- package/dist/v2/restarts.d.ts.map +1 -0
- package/dist/v2/restarts.js +348 -0
- package/dist/v2/session-requests.d.ts +20 -0
- package/dist/v2/session-requests.d.ts.map +1 -0
- package/dist/v2/session-requests.js +108 -0
- package/dist/v2/session-update.d.ts +2 -13
- package/dist/v2/session-update.d.ts.map +1 -1
- package/dist/v2/session-update.js +2 -22
- package/dist/v2/session.d.ts +340 -41
- package/dist/v2/session.d.ts.map +1 -1
- package/dist/v2/session.js +1223 -141
- package/dist/v2/setup.d.ts +49 -0
- package/dist/v2/setup.d.ts.map +1 -0
- package/dist/v2/setup.js +62 -0
- package/dist/v2/stopped-work.d.ts +123 -0
- package/dist/v2/stopped-work.d.ts.map +1 -0
- package/dist/v2/stopped-work.js +210 -0
- package/dist/v2/stream.d.ts +228 -0
- package/dist/v2/stream.d.ts.map +1 -0
- package/dist/v2/stream.js +506 -0
- package/dist/v2/subagent-replay.d.ts +115 -0
- package/dist/v2/subagent-replay.d.ts.map +1 -0
- package/dist/v2/subagent-replay.js +479 -0
- package/dist/v2/subscription-guard.d.ts +60 -0
- package/dist/v2/subscription-guard.d.ts.map +1 -0
- package/dist/v2/subscription-guard.js +84 -0
- package/dist/v2/tasks.d.ts +492 -0
- package/dist/v2/tasks.d.ts.map +1 -0
- package/dist/v2/tasks.js +1728 -0
- package/dist/v2/terminal.d.ts.map +1 -1
- package/dist/v2/terminal.js +6 -1
- package/dist/v2/tool-call.d.ts +3 -2
- package/dist/v2/tool-call.d.ts.map +1 -1
- package/dist/v2/update-translator.d.ts +36 -0
- package/dist/v2/update-translator.d.ts.map +1 -0
- package/dist/v2/update-translator.js +80 -0
- package/dist/workflow-progress.d.ts +23 -0
- package/dist/workflow-progress.d.ts.map +1 -1
- package/dist/workflow-progress.js +31 -8
- package/package.json +1 -1
- package/dist/v2/permission.d.ts +0 -28
- package/dist/v2/permission.d.ts.map +0 -1
- 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
|
|
3
|
-
*
|
|
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,
|
|
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.
|
|
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",
|