@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,494 @@
1
+ /**
2
+ * The config options of a v2 session: its permission mode, model,
3
+ * effort, Fast mode, ultracode and main-thread agent. Claude Code owns them
4
+ * all. The session sets each on the query when the client picks a value, and
5
+ * follows the changes it learns of (an approved plan's mode, a model switch
6
+ * that the adapter did not make, the Fast mode state that Claude Code
7
+ * reports).
8
+ *
9
+ * The options are built and changed by the v1 core's rules, from the shared
10
+ * modules (`session-config-options.ts`, `session-effort.ts`,
11
+ * `session-fast-mode.ts`, `session-ultracode.ts`, `session-model.ts`,
12
+ * `session-mode.ts`). Fast mode and ultracode are boolean options: every v2
13
+ * client takes them. For an AIR client that declares `recommendedValue`, the
14
+ * model and effort options carry no `default` row and name a recommendation
15
+ * (`_meta.jetbrains.air.recommendedValue`), and the effort they show is set
16
+ * on the query at the start and at each model switch, as on v1.
17
+ *
18
+ * The mode follows `SessionModeManager`: a model whose `ModelInfo` does not
19
+ * report `supportsAutoMode` runs `auto` as `acceptEdits`, with a notice,
20
+ * whether the client picks `auto`, an answer to ExitPlanMode does, or a model
21
+ * switch leaves the session in it.
22
+ *
23
+ * The changes of one session run one at a time, in the order they came, as
24
+ * the v1 core's `serializeConfigurationChange` runs them: a change awaits
25
+ * Claude Code between its steps, and a model switch rebuilds every option at
26
+ * its end, so a change that ran meanwhile would be lost.
27
+ */
28
+ import { RequestError, } from "@agentclientprotocol/sdk/unstable";
29
+ import { DEFAULT_AGENT_ID } from "../session-config-ids.js";
30
+ import { AGENT_CONFIG_ID, buildConfigOptions, configOptionValue, discoverCustomAgents, } from "../session-config-options.js";
31
+ import { EFFORT_CONFIG_ID, effortFlagSettings, effortForModelSwitch, settleEffortAfterModelSwitch, settingsEffortForModel, } from "../session-effort.js";
32
+ import { createFastModeConfigOption, FAST_MODE_CONFIG_ID, fastModeStateChange, fastModeStateEnabled, normalizeFastModeDisabledReason, } from "../session-fast-mode.js";
33
+ import { MODEL_CONFIG_ID, modelDisplayName, modelEntryFor, resolveModelPreference, } from "../session-model.js";
34
+ import { MODE_CONFIG_ID, SessionModeManager, } from "../session-mode.js";
35
+ import { noticeUpdate } from "../session-notices.js";
36
+ import { createUltracodeConfigOption, readUltracodeSettings, ultracodeSupported, ultracodeWorkflowsEnabled, } from "../session-ultracode.js";
37
+ import { v1SetSessionConfigOptionRequest, v2ConfigOptions } from "./session-requests.js";
38
+ /** Every v2 client takes boolean config options. */
39
+ const BOOLEAN_OPTIONS = true;
40
+ export class SessionConfig {
41
+ sessionId;
42
+ modelInfos;
43
+ effortSettings;
44
+ recommendedValue;
45
+ notify;
46
+ logger;
47
+ session;
48
+ rules;
49
+ agents;
50
+ currentAgent;
51
+ /** Whether the client picked the agent (`session/set_config_option`), which a restart restores. */
52
+ agentSelected = false;
53
+ effort;
54
+ fastModeEnabled;
55
+ fastModeDisabledReason;
56
+ ultracodeEnabled;
57
+ ultracodeWorkflowsEnabled;
58
+ /** The end of the last change (see the module doc). */
59
+ changes = Promise.resolve();
60
+ constructor(sessionId, query, modes, models,
61
+ /** What Claude Code reports of each model: its descriptions and capabilities. */
62
+ modelInfos, state, effortSettings,
63
+ /** The client declared AIR's `recommendedValue` (see the module doc). */
64
+ recommendedValue,
65
+ /** Sends an update of the session, such as the Auto mode fallback's notice. */
66
+ notify, logger, autoModeFallbackWarningPending) {
67
+ this.sessionId = sessionId;
68
+ this.modelInfos = modelInfos;
69
+ this.effortSettings = effortSettings;
70
+ this.recommendedValue = recommendedValue;
71
+ this.notify = notify;
72
+ this.logger = logger;
73
+ this.session = {
74
+ query,
75
+ modes,
76
+ models,
77
+ modelInfos,
78
+ configOptions: state.configOptions,
79
+ autoModeFallbackWarningPending,
80
+ };
81
+ this.agents = state.agents;
82
+ this.currentAgent = state.currentAgent;
83
+ this.effort = state.effort;
84
+ this.fastModeEnabled = state.fastModeEnabled;
85
+ this.fastModeDisabledReason = state.fastModeDisabledReason;
86
+ this.ultracodeEnabled = state.ultracodeEnabled;
87
+ this.ultracodeWorkflowsEnabled = state.ultracodeWorkflowsEnabled;
88
+ this.rules = new SessionModeManager({
89
+ getSession: () => this.session,
90
+ sessionEndedMessage: "The session ended",
91
+ updateConfigOption: async () => { },
92
+ sessionUpdate: async (notification) => notify(notification),
93
+ // Every v2 client takes `notice` updates.
94
+ supportsNotices: () => true,
95
+ logError: (...args) => logger.error(...args),
96
+ });
97
+ }
98
+ /**
99
+ * The config of a session that starts in `mode`, as the v1 core starts one
100
+ * (`ClaudeAcpAgent.createSession`): a mode the model cannot serve falls
101
+ * back, and the fallback's notice goes out with the first prompt. The
102
+ * custom agents are discovered, Fast mode follows the reported state, and
103
+ * ultracode follows Claude Code's `get_settings` report, or the settings
104
+ * without one. The client's `effort` pins the effort when the model offers
105
+ * it.
106
+ */
107
+ static async start(sessionId, query, mode, models, modelInfos, currentModelInfo, setup, notify, logger) {
108
+ const rules = new SessionModeManager({
109
+ getSession: () => undefined,
110
+ sessionEndedMessage: "",
111
+ updateConfigOption: async () => { },
112
+ sessionUpdate: async () => { },
113
+ logError: (...args) => logger.error(...args),
114
+ });
115
+ const { modes, autoModeFallbackWarningPending } = await rules.initialize({
116
+ query,
117
+ requestedMode: mode.mode,
118
+ builtInDefault: mode.builtInDefault,
119
+ currentModelInfo,
120
+ currentModelId: models.currentModelId,
121
+ allowBypass: mode.allowBypass,
122
+ allowAuto: mode.allowAuto,
123
+ });
124
+ const agents = await discoverCustomAgents(query);
125
+ // Only a listed agent is selected: another would leave the option's
126
+ // value outside its own entries.
127
+ const requestedAgent = setup.client?.agent;
128
+ const currentAgent = requestedAgent && agents.some((agent) => agent.name === requestedAgent)
129
+ ? requestedAgent
130
+ : DEFAULT_AGENT_ID;
131
+ const fastModeEnabled = setup.fastMode.state !== undefined && fastModeStateEnabled(setup.fastMode.state);
132
+ const fastModeDisabledReason = fastModeEnabled
133
+ ? undefined
134
+ : normalizeFastModeDisabledReason(setup.fastMode.disabledReason);
135
+ const settings = setup.effortSettings();
136
+ const ultracodeReport = await readUltracodeSettings(query);
137
+ const ultracodeEnabled = ultracodeReport?.requested ?? settings.ultracode === true;
138
+ const workflowsEnabled = ultracodeWorkflowsEnabled(ultracodeReport, currentModelInfo);
139
+ const clientEffort = setup.client?.effort;
140
+ const configOptions = buildConfigOptions(modes, models, modelInfos, clientEffort ?? settingsEffortForModel(settings, currentModelInfo), agents, currentAgent, {
141
+ supported: currentModelInfo?.supportsFastMode ?? false,
142
+ enabled: fastModeEnabled,
143
+ useBooleanOption: BOOLEAN_OPTIONS,
144
+ disabledReason: fastModeDisabledReason,
145
+ }, { useRecommendedValue: setup.recommendedValue }, {
146
+ supported: ultracodeReport?.available ?? ultracodeSupported(workflowsEnabled, currentModelInfo),
147
+ enabled: ultracodeEnabled,
148
+ useBooleanOption: BOOLEAN_OPTIONS,
149
+ });
150
+ const shownEffort = configOptions.find((option) => option.id === EFFORT_CONFIG_ID)?.currentValue;
151
+ // The client's effort is a pin when the model offers it.
152
+ const pinned = clientEffort !== undefined && shownEffort === clientEffort ? clientEffort : undefined;
153
+ // With `recommendedValue`, the shown effort is a concrete level, which
154
+ // Claude Code's own default need not be: it is set on the query, as on
155
+ // v1. A failure fails the session's start, as on v1.
156
+ const applied = setup.recommendedValue && typeof shownEffort === "string" ? shownEffort : pinned;
157
+ if (setup.recommendedValue && typeof shownEffort === "string") {
158
+ await query.applyFlagSettings(effortFlagSettings(shownEffort, ultracodeEnabled));
159
+ }
160
+ return new SessionConfig(sessionId, query, modes, models, modelInfos, {
161
+ agents,
162
+ currentAgent,
163
+ effort: { pinned, applied },
164
+ fastModeEnabled,
165
+ fastModeDisabledReason,
166
+ ultracodeEnabled,
167
+ ultracodeWorkflowsEnabled: workflowsEnabled,
168
+ configOptions,
169
+ }, setup.effortSettings, setup.recommendedValue, notify, logger, autoModeFallbackWarningPending);
170
+ }
171
+ /** The options, in v2 form. */
172
+ options() {
173
+ return v2ConfigOptions(this.session.configOptions);
174
+ }
175
+ /** The entries of the `model` option, for the `model` of a subagent's session. */
176
+ subagentModels() {
177
+ return {
178
+ entry: (model) => modelEntryFor(this.session.configOptions, this.modelInfos, model),
179
+ name: (model) => modelDisplayName(this.modelInfos, model),
180
+ };
181
+ }
182
+ /** The current model, and what Claude Code reports of it. */
183
+ currentModel() {
184
+ const id = this.session.models.currentModelId;
185
+ return { id, info: this.modelInfos.find((model) => model.value === id) };
186
+ }
187
+ availableModeIds() {
188
+ return this.session.modes.availableModes.map((mode) => mode.id);
189
+ }
190
+ /** The session's modes, for the name of a subagent's mode. */
191
+ availableModes() {
192
+ return this.session.modes.availableModes;
193
+ }
194
+ /** The mode the session left when it entered plan mode, while it is in plan. */
195
+ prePlanMode() {
196
+ return this.session.prePlanMode;
197
+ }
198
+ /** A prompt goes out: the notice of a fallback at the session's start goes with the first. */
199
+ async promptSent() {
200
+ if (this.session.autoModeFallbackWarningPending) {
201
+ await this.rules.publishFallbackWarning(this.sessionId, this.session);
202
+ }
203
+ }
204
+ /**
205
+ * Sets the value of `params` on the query, and records it once Claude Code
206
+ * took it. The value is checked against the option as the session
207
+ * advertised it (`configOptionValue`): a select takes an `id` of its
208
+ * entries (or a model alias), a boolean takes a boolean.
209
+ */
210
+ set(params) {
211
+ return this.serialized(() => this.apply(params));
212
+ }
213
+ /**
214
+ * Sets the choices of `previous`, the config of the query that this one
215
+ * replaced (see `restarts.ts`), as the v1 core's `restoreSessionChoices`
216
+ * does: the model, then the mode, which the model decides, and its
217
+ * pre-plan mode, then ultracode, Fast mode, the effort pin (after
218
+ * ultracode, which its flag carries) and the agent the client picked. A
219
+ * value the new query refuses is logged.
220
+ */
221
+ restoreFrom(previous) {
222
+ return this.serialized(async () => {
223
+ const restore = async (choice, apply) => {
224
+ try {
225
+ await apply();
226
+ }
227
+ catch (error) {
228
+ this.logger.error(`Could not restore ${choice} after the query restart:`, error);
229
+ }
230
+ };
231
+ const select = (configId, value) => restore(`${configId} "${value}"`, () => this.apply({ sessionId: this.sessionId, configId, type: "id", value }));
232
+ const model = previous.session.models.currentModelId;
233
+ if (this.session.models.currentModelId !== model)
234
+ await select(MODEL_CONFIG_ID, model);
235
+ // After the model: the start falls back from `auto` on a model without Auto.
236
+ const mode = previous.session.modes.currentModeId;
237
+ if (this.session.modes.currentModeId !== mode) {
238
+ await select(MODE_CONFIG_ID, mode);
239
+ if (this.session.modes.currentModeId === mode) {
240
+ this.session.autoModeFallbackWarningPending = false;
241
+ }
242
+ }
243
+ if (this.session.modes.currentModeId === "plan") {
244
+ this.session.prePlanMode = previous.session.prePlanMode;
245
+ }
246
+ // The user's intent, not the option: a model without support hides it.
247
+ if (this.ultracodeEnabled !== previous.ultracodeEnabled) {
248
+ await restore("ultracode", () => this.setUltracode(previous.ultracodeEnabled));
249
+ }
250
+ if (this.fastModeEnabled !== previous.fastModeEnabled) {
251
+ await restore("Fast mode", () => this.setFastMode(previous.fastModeEnabled));
252
+ }
253
+ if (previous.effort.pinned !== undefined) {
254
+ await select(EFFORT_CONFIG_ID, previous.effort.pinned);
255
+ }
256
+ // Even when unchanged: Default must clear an agent that the setup's
257
+ // `_meta` picked. Default is valid without an `agent` option too.
258
+ if (previous.agentSelected) {
259
+ if (previous.currentAgent === DEFAULT_AGENT_ID) {
260
+ await restore("Default agent", () => this.selectAgent(DEFAULT_AGENT_ID));
261
+ }
262
+ else {
263
+ await select(AGENT_CONFIG_ID, previous.currentAgent);
264
+ }
265
+ }
266
+ });
267
+ }
268
+ serialized(change) {
269
+ const result = this.changes.then(change);
270
+ this.changes = result.then(() => { }, () => { });
271
+ return result;
272
+ }
273
+ async apply(params) {
274
+ const request = v1SetSessionConfigOptionRequest(params);
275
+ const option = this.session.configOptions.find((entry) => entry.id === request.configId);
276
+ if (!option) {
277
+ throw RequestError.invalidParams({ configId: request.configId }, `Unknown config option: ${request.configId}`);
278
+ }
279
+ const selected = configOptionValue(option, request, this.modelInfos);
280
+ if ("enabled" in selected) {
281
+ if (request.configId === FAST_MODE_CONFIG_ID)
282
+ await this.setFastMode(selected.enabled);
283
+ else
284
+ await this.setUltracode(selected.enabled);
285
+ return;
286
+ }
287
+ const value = selected.value;
288
+ switch (request.configId) {
289
+ case MODE_CONFIG_ID:
290
+ await this.applyMode(value);
291
+ return;
292
+ case MODEL_CONFIG_ID:
293
+ await this.session.query.setModel(value);
294
+ await this.recordModel(value);
295
+ return;
296
+ case EFFORT_CONFIG_ID:
297
+ // Applied first, so a rejected control request leaves the option as it was.
298
+ await this.session.query.applyFlagSettings(effortFlagSettings(value, this.ultracodeEnabled));
299
+ this.setCurrentValue(EFFORT_CONFIG_ID, value);
300
+ // "Default" hands effort back to the CLI's per-model settings: it un-pins.
301
+ this.effort =
302
+ value !== "default"
303
+ ? { pinned: value, applied: value }
304
+ : { pinned: undefined, applied: undefined };
305
+ return;
306
+ case AGENT_CONFIG_ID:
307
+ await this.selectAgent(value);
308
+ return;
309
+ default:
310
+ throw RequestError.invalidParams({ configId: request.configId }, `Unknown config option: ${request.configId}`);
311
+ }
312
+ }
313
+ /**
314
+ * Sets the mode `requested` on the query, and records it once Claude Code
315
+ * took it: `auto` that the model cannot serve runs as its fallback, with a
316
+ * notice.
317
+ */
318
+ enterMode(requested) {
319
+ return this.serialized(() => this.applyMode(requested));
320
+ }
321
+ /** The mode that `requested` runs as on the current model (see the module doc). */
322
+ effectiveMode(requested) {
323
+ return this.rules.effectiveMode(this.session, requested);
324
+ }
325
+ async applyMode(requested) {
326
+ const mode = this.rules.effectiveMode(this.session, requested);
327
+ await this.session.query.setPermissionMode(mode);
328
+ this.rules.syncConfig(this.session, mode);
329
+ if (mode !== requested)
330
+ await this.rules.publishFallbackWarning(this.sessionId, this.session);
331
+ }
332
+ /** Claude Code switched the mode itself. Returns whether the option changed. */
333
+ modeChanged(mode) {
334
+ if (this.session.modes.currentModeId === mode || !this.availableModeIds().includes(mode)) {
335
+ return false;
336
+ }
337
+ this.rules.syncConfig(this.session, mode);
338
+ return true;
339
+ }
340
+ /**
341
+ * Claude Code switched the model without the adapter: a typed `/model`, or
342
+ * a refusal fallback. Returns whether the options changed. A model that the
343
+ * option does not list is recorded by its id, which the option then lists.
344
+ */
345
+ modelSwitched(model) {
346
+ return this.serialized(async () => {
347
+ const value = resolveModelPreference(this.modelInfos, model)?.value ?? model;
348
+ if (this.session.models.currentModelId === value)
349
+ return false;
350
+ await this.recordModel(value);
351
+ return true;
352
+ });
353
+ }
354
+ /**
355
+ * Claude Code reported a Fast mode state (`system`/init, a result). Returns
356
+ * whether the option changed. Without a Fast mode option the model does not
357
+ * support it, and the state says nothing of the user's intent, which stays
358
+ * for a model that does (see `fastModeStateChange`).
359
+ */
360
+ fastModeReported(state, reason) {
361
+ if (!this.session.configOptions.some((option) => option.id === FAST_MODE_CONFIG_ID)) {
362
+ return false;
363
+ }
364
+ const change = fastModeStateChange({ enabled: this.fastModeEnabled, disabledReason: this.fastModeDisabledReason }, state, reason);
365
+ if (!change)
366
+ return false;
367
+ this.fastModeEnabled = change.enabled;
368
+ this.fastModeDisabledReason = change.disabledReason;
369
+ this.refreshFastModeOption();
370
+ if (change.notice) {
371
+ this.notify({ sessionId: this.sessionId, update: noticeUpdate(change.notice.notice) });
372
+ }
373
+ return true;
374
+ }
375
+ /**
376
+ * An allowed permission answer, with an `auto` mode that the model cannot
377
+ * serve set as its fallback (see the module doc).
378
+ */
379
+ async permissionAllowed(result) {
380
+ const { permissionResult, fallbackApplied } = this.rules.applyPermissionFallback(this.session, result);
381
+ if (fallbackApplied)
382
+ await this.rules.publishFallbackWarning(this.sessionId, this.session);
383
+ return permissionResult;
384
+ }
385
+ /**
386
+ * `null` clears the flag layer back to the standard Claude Code agent. The
387
+ * change takes effect on the next turn. Applied first, so a rejected
388
+ * control request leaves the option as it was.
389
+ */
390
+ async selectAgent(value) {
391
+ await this.session.query.applyFlagSettings({
392
+ agent: value === DEFAULT_AGENT_ID ? null : value,
393
+ });
394
+ this.currentAgent = value;
395
+ this.agentSelected = true;
396
+ this.setCurrentValue(AGENT_CONFIG_ID, value);
397
+ }
398
+ /** Applied first, so a rejected control request leaves the option as it was. */
399
+ async setFastMode(enabled) {
400
+ await this.session.query.applyFlagSettings({ fastMode: enabled });
401
+ this.fastModeEnabled = enabled;
402
+ this.refreshFastModeOption();
403
+ }
404
+ /**
405
+ * An explicit boolean leaves the effort level alone (`null` would reset the
406
+ * flag layer). Applied first, so a rejected control request leaves the
407
+ * option as it was.
408
+ */
409
+ async setUltracode(enabled) {
410
+ await this.session.query.applyFlagSettings({ ultracode: enabled });
411
+ this.ultracodeEnabled = enabled;
412
+ this.replaceOption(createUltracodeConfigOption(enabled, BOOLEAN_OPTIONS));
413
+ }
414
+ refreshFastModeOption() {
415
+ this.replaceOption(createFastModeConfigOption(this.fastModeEnabled, BOOLEAN_OPTIONS, this.fastModeDisabledReason));
416
+ }
417
+ replaceOption(option) {
418
+ this.session.configOptions = this.session.configOptions.map((entry) => entry.id === option.id ? option : entry);
419
+ }
420
+ setCurrentValue(configId, value) {
421
+ this.session.configOptions = this.session.configOptions.map((entry) => entry.id === configId && typeof entry.currentValue === "string"
422
+ ? { ...entry, currentValue: value }
423
+ : entry);
424
+ }
425
+ /**
426
+ * Records the model `value`, as the v1 core does (`applyConfigOptionValue`):
427
+ * an `auto` mode it cannot serve falls back, and the options are rebuilt
428
+ * for it. Effort follows `effortForModelSwitch`; Fast mode and ultracode
429
+ * show only on a model that supports them, with the user's retained
430
+ * choice.
431
+ */
432
+ async recordModel(value) {
433
+ const info = this.modelInfos.find((model) => model.value === value);
434
+ this.session.models = { ...this.session.models, currentModelId: value };
435
+ const modeDowngraded = await this.rules.reconcileForModel(this.session, info);
436
+ // `model_not_allowed` described the model the session left. The other
437
+ // reasons are account- or environment-scoped and stay true.
438
+ if (this.fastModeDisabledReason === "model_not_allowed") {
439
+ this.fastModeDisabledReason = undefined;
440
+ }
441
+ const displayed = this.session.configOptions.find((option) => option.id === EFFORT_CONFIG_ID)?.currentValue;
442
+ const effortSwitch = effortForModelSwitch({
443
+ state: this.effort,
444
+ displayed: typeof displayed === "string" ? displayed : undefined,
445
+ newModelInfo: info,
446
+ modelId: value,
447
+ settings: this.effortSettings(),
448
+ useRecommendedValue: this.recommendedValue,
449
+ });
450
+ const options = buildConfigOptions(this.session.modes, this.session.models, this.modelInfos, effortSwitch.seed, this.agents, this.currentAgent, {
451
+ supported: info?.supportsFastMode ?? false,
452
+ enabled: this.fastModeEnabled,
453
+ useBooleanOption: BOOLEAN_OPTIONS,
454
+ disabledReason: this.fastModeDisabledReason,
455
+ }, { useRecommendedValue: this.recommendedValue }, {
456
+ supported: ultracodeSupported(this.ultracodeWorkflowsEnabled, info),
457
+ enabled: this.ultracodeEnabled,
458
+ useBooleanOption: BOOLEAN_OPTIONS,
459
+ });
460
+ const effort = await settleEffortAfterModelSwitch(effortSwitch, options.find((option) => option.id === EFFORT_CONFIG_ID), this.ultracodeEnabled, this.recommendedValue, (settings) => this.session.query.applyFlagSettings(settings));
461
+ this.effort = effort.state;
462
+ this.session.configOptions = options.flatMap((option) => option.id !== EFFORT_CONFIG_ID ? [option] : effort.option ? [effort.option] : []);
463
+ // The model switch already happened: a failed effort sync only logs.
464
+ if ("error" in effort) {
465
+ this.logger.error(`Session ${this.sessionId}: failed to synchronize effort after the switch to ${value}`, effort.error);
466
+ }
467
+ if (modeDowngraded)
468
+ await this.rules.publishFallbackWarning(this.sessionId, this.session);
469
+ }
470
+ }
471
+ /**
472
+ * The PostModelSwitch hook: a switch of the root's model that the adapter
473
+ * did not make (`source` other than `sdk`, its own `setModel`, and `resume`,
474
+ * which the setup read already) updates the `model` option. A subagent's
475
+ * switch (`agent_id`) is not the session's.
476
+ */
477
+ export function modelSwitchHook(switched) {
478
+ return {
479
+ hooks: [
480
+ async (input) => {
481
+ if (input.hook_event_name === "PostModelSwitch" &&
482
+ !input.agent_id &&
483
+ input.source !== "sdk" &&
484
+ input.source !== "resume") {
485
+ // After the hook's answer: Claude Code waits for it, and the
486
+ // session's control requests share its channel.
487
+ const model = input.to_model;
488
+ setImmediate(() => switched(model));
489
+ }
490
+ return { continue: true };
491
+ },
492
+ ],
493
+ };
494
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Claude Code's requests for user input that are not permission checks, as
3
+ * ACP v2 elicitations of one session:
4
+ *
5
+ * - AskUserQuestion: a form, on the session of the agent that asks (the root
6
+ * or a subagent's child session), with the question's `toolCallId`.
7
+ * - An MCP server's elicitation (`onElicitation`): a form or a URL. A mode
8
+ * that the client did not advertise is answered `cancel`.
9
+ * - The refusal fallback's consent prompt (`onUserDialog`): a form.
10
+ * - MCP OAuth: a URL, sent after the setup response (see `mcp-oauth.ts`).
11
+ *
12
+ * The first three are open requests of their session while they wait
13
+ * (`requires_action`), and a cancel or close withdraws them with
14
+ * `$/cancel_request`, as it does a permission request. An MCP server's URL
15
+ * request that the user accepts stays open until Claude Code reports it
16
+ * complete: `accept` only consents to open the URL, and the work waits for
17
+ * the user to finish there. Building the elicitations and reading the
18
+ * answers is shared with v1 (`elicitation.ts`).
19
+ */
20
+ import type { CompleteElicitationNotification, CreateElicitationRequest, CreateElicitationResponse } from "@agentclientprotocol/sdk/unstable";
21
+ import type { CanUseTool, Options, Query } from "@anthropic-ai/claude-agent-sdk";
22
+ import type { Logger } from "../logger.js";
23
+ import { type ElicitationSupport } from "../elicitation.js";
24
+ import type { RequestTarget } from "./tasks.js";
25
+ /** What the elicitations need of the connection and the session. */
26
+ export interface ElicitationHost {
27
+ readonly sessionId: string;
28
+ readonly logger: Logger;
29
+ /** The modes that the client advertised in `initialize`. */
30
+ readonly support: ElicitationSupport;
31
+ /** The client is JetBrains AIR: AskUserQuestion's "Other" fields carry AIR's custom answer marker. */
32
+ readonly airClient: boolean;
33
+ createElicitation(request: CreateElicitationRequest, signal: AbortSignal): Promise<CreateElicitationResponse>;
34
+ completeElicitation(params: CompleteElicitationNotification): Promise<void>;
35
+ /** The session of the agent `agentId` (see `V2Session.target`). */
36
+ target(agentId: string | undefined): Promise<RequestTarget>;
37
+ /** The session's query, once it started. */
38
+ query(): Query | undefined;
39
+ }
40
+ export declare class SessionElicitations {
41
+ private readonly host;
42
+ /** Whether Claude Code takes `annotations[question].unanswered` (`ask_user_question_unanswered_v1`). */
43
+ private reportUnanswered;
44
+ /**
45
+ * Aborted by `session/close`: stops the MCP OAuth runs. A query that ended
46
+ * on its own fails their next `mcpServerStatus`, which ends them too.
47
+ */
48
+ private readonly closed;
49
+ /** The accepted URL requests that wait for their `elicitation_complete`, by elicitation id. */
50
+ private readonly awaitingCompletion;
51
+ /** The elicitation ids that Claude Code reported complete in the open turn. */
52
+ private readonly completedIds;
53
+ constructor(host: ElicitationHost);
54
+ /**
55
+ * The query options: the elicitation callbacks for the modes the client
56
+ * renders. Without form elicitation AskUserQuestion is disallowed, and the
57
+ * refusal fallback is not declared, so Claude Code ends the turn with the
58
+ * refusal instead.
59
+ */
60
+ queryOptions(): Pick<Options, "onElicitation" | "onUserDialog" | "supportedDialogKinds" | "disallowedTools">;
61
+ /** `canUseTool` that asks AskUserQuestion's questions, and leaves the other tools to `permissions`. */
62
+ canUseTool(permissions: CanUseTool): CanUseTool;
63
+ /** A `system/init` of the session: it lists Claude Code's capabilities. */
64
+ init(capabilities: readonly string[] | undefined): void;
65
+ /** Claude Code's `elicitation_complete`: a URL elicitation of an MCP server finished. */
66
+ completed(elicitationId: string): void;
67
+ /**
68
+ * Starts OAuth in the background for the servers of `serverNames` that
69
+ * Claude Code reports as `needs-auth`. Call it only once the setup
70
+ * response is sent: v2 asks for no elicitation before it.
71
+ */
72
+ authenticateMcpServers(serverNames: readonly string[]): void;
73
+ /** The session closes: the OAuth runs stop, and withdraw their elicitations. */
74
+ close(): void;
75
+ /**
76
+ * The open turn ended: its accepted URL requests no longer hold it. A
77
+ * server that never reports its completion holds only its turn.
78
+ */
79
+ turnEnded(): void;
80
+ /**
81
+ * Sends an elicitation as an open request of `target`. With
82
+ * `holdAcceptedUrl`, an accepted URL request then stays open (see the
83
+ * module doc).
84
+ */
85
+ private elicit;
86
+ /**
87
+ * Resolves once Claude Code reports the elicitation `elicitationId`
88
+ * complete, `signal` aborts (the tool use ended, or a cancel or close
89
+ * withdrew it), or the turn ends.
90
+ */
91
+ private untilCompleted;
92
+ }
93
+ //# sourceMappingURL=elicitation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"elicitation.d.ts","sourceRoot":"","sources":["../../src/v2/elicitation.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,EACV,+BAA+B,EAC/B,wBAAwB,EACxB,yBAAyB,EAC1B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,gCAAgC,CAAC;AACjF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAOL,KAAK,kBAAkB,EACxB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD,oEAAoE;AACpE,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,EAAE,kBAAkB,CAAC;IACrC,sGAAsG;IACtG,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAC;IAC5B,iBAAiB,CACf,OAAO,EAAE,wBAAwB,EACjC,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,yBAAyB,CAAC,CAAC;IACtC,mBAAmB,CAAC,MAAM,EAAE,+BAA+B,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5E,mEAAmE;IACnE,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAC5D,4CAA4C;IAC5C,KAAK,IAAI,KAAK,GAAG,SAAS,CAAC;CAC5B;AAED,qBAAa,mBAAmB;IAalB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAZjC,wGAAwG;IACxG,OAAO,CAAC,gBAAgB,CAAS;IACjC;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAyB;IAChD,+FAA+F;IAC/F,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAsC;IACzE,+EAA+E;IAC/E,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAqB;gBAErB,IAAI,EAAE,eAAe;IAElD;;;;;OAKG;IACH,YAAY,IAAI,IAAI,CAClB,OAAO,EACP,eAAe,GAAG,cAAc,GAAG,sBAAsB,GAAG,iBAAiB,CAC9E;IA4BD,uGAAuG;IACvG,UAAU,CAAC,WAAW,EAAE,UAAU,GAAG,UAAU;IAiB/C,2EAA2E;IAC3E,IAAI,CAAC,YAAY,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,GAAG,IAAI;IAIvD,yFAAyF;IACzF,SAAS,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI;IAStC;;;;OAIG;IACH,sBAAsB,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI;IAkB5D,gFAAgF;IAChF,KAAK,IAAI,IAAI;IAIb;;;OAGG;IACH,SAAS,IAAI,IAAI;IAOjB;;;;OAIG;IACH,OAAO,CAAC,MAAM;IAyBd;;;;OAIG;IACH,OAAO,CAAC,cAAc;CAevB"}