@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.
- package/README.md +5 -1
- package/dist/acp-agent.d.ts +93 -622
- package/dist/acp-agent.d.ts.map +1 -1
- package/dist/acp-agent.js +369 -3474
- package/dist/acp-client.d.ts +28 -0
- package/dist/acp-client.d.ts.map +1 -0
- package/dist/acp-client.js +1 -0
- 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/lib.d.ts +5 -1
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +3 -1
- package/dist/logger.d.ts +10 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +1 -0
- 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/message-filters.d.ts +43 -0
- package/dist/message-filters.d.ts.map +1 -0
- package/dist/message-filters.js +197 -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/new-session-meta.d.ts +40 -0
- package/dist/new-session-meta.d.ts.map +1 -0
- package/dist/new-session-meta.js +1 -0
- 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/prompt-suggestions.d.ts +29 -0
- package/dist/prompt-suggestions.d.ts.map +1 -1
- package/dist/prompt-suggestions.js +45 -0
- package/dist/prompt-to-claude.d.ts +10 -0
- package/dist/prompt-to-claude.d.ts.map +1 -0
- package/dist/prompt-to-claude.js +120 -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 +68 -0
- package/dist/result-end.d.ts.map +1 -0
- package/dist/result-end.js +84 -0
- package/dist/sdk-to-acp.d.ts +93 -0
- package/dist/sdk-to-acp.d.ts.map +1 -0
- package/dist/sdk-to-acp.js +635 -0
- package/dist/serve.d.ts +9 -3
- 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 +5 -6
- 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/slash-commands.d.ts +10 -0
- package/dist/slash-commands.d.ts.map +1 -0
- package/dist/slash-commands.js +99 -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/utils.d.ts +1 -1
- package/dist/utils.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 +166 -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 +1224 -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 +229 -0
- package/dist/v2/stream.d.ts.map +1 -0
- package/dist/v2/stream.js +507 -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,160 @@
|
|
|
1
|
+
import { answerAskUserQuestion, answerMcpElicitation, answerUserDialog, elicitUnlessWithdrawn, REFUSAL_FALLBACK_DIALOG_KIND, } from "../elicitation.js";
|
|
2
|
+
import { startMcpServerAuthentication } from "../mcp-oauth.js";
|
|
3
|
+
export class SessionElicitations {
|
|
4
|
+
host;
|
|
5
|
+
/** Whether Claude Code takes `annotations[question].unanswered` (`ask_user_question_unanswered_v1`). */
|
|
6
|
+
reportUnanswered = false;
|
|
7
|
+
/**
|
|
8
|
+
* Aborted by `session/close`: stops the MCP OAuth runs. A query that ended
|
|
9
|
+
* on its own fails their next `mcpServerStatus`, which ends them too.
|
|
10
|
+
*/
|
|
11
|
+
closed = new AbortController();
|
|
12
|
+
/** The accepted URL requests that wait for their `elicitation_complete`, by elicitation id. */
|
|
13
|
+
awaitingCompletion = new Map();
|
|
14
|
+
/** The elicitation ids that Claude Code reported complete in the open turn. */
|
|
15
|
+
completedIds = new Set();
|
|
16
|
+
constructor(host) {
|
|
17
|
+
this.host = host;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The query options: the elicitation callbacks for the modes the client
|
|
21
|
+
* renders. Without form elicitation AskUserQuestion is disallowed, and the
|
|
22
|
+
* refusal fallback is not declared, so Claude Code ends the turn with the
|
|
23
|
+
* refusal instead.
|
|
24
|
+
*/
|
|
25
|
+
queryOptions() {
|
|
26
|
+
const { support, sessionId, logger } = this.host;
|
|
27
|
+
return {
|
|
28
|
+
...(support.form || support.url
|
|
29
|
+
? {
|
|
30
|
+
onElicitation: async (request, { signal }) => answerMcpElicitation(request, {
|
|
31
|
+
sessionId,
|
|
32
|
+
support,
|
|
33
|
+
logger,
|
|
34
|
+
elicit: this.elicit(await this.host.target(undefined), signal, true),
|
|
35
|
+
}),
|
|
36
|
+
}
|
|
37
|
+
: {}),
|
|
38
|
+
...(support.form
|
|
39
|
+
? {
|
|
40
|
+
onUserDialog: async (request, { signal }) => answerUserDialog(request, {
|
|
41
|
+
sessionId,
|
|
42
|
+
logger,
|
|
43
|
+
elicit: this.elicit(await this.host.target(undefined), signal),
|
|
44
|
+
}),
|
|
45
|
+
supportedDialogKinds: [REFUSAL_FALLBACK_DIALOG_KIND],
|
|
46
|
+
}
|
|
47
|
+
: { disallowedTools: ["AskUserQuestion"] }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** `canUseTool` that asks AskUserQuestion's questions, and leaves the other tools to `permissions`. */
|
|
51
|
+
canUseTool(permissions) {
|
|
52
|
+
return async (toolName, toolInput, options) => {
|
|
53
|
+
if (toolName !== "AskUserQuestion" || !this.host.support.form) {
|
|
54
|
+
return permissions(toolName, toolInput, options);
|
|
55
|
+
}
|
|
56
|
+
const target = await this.host.target(options.agentID);
|
|
57
|
+
return answerAskUserQuestion(toolInput, {
|
|
58
|
+
sessionId: target.sessionId,
|
|
59
|
+
toolUseID: options.toolUseID,
|
|
60
|
+
airClient: this.host.airClient,
|
|
61
|
+
reportUnanswered: () => this.reportUnanswered,
|
|
62
|
+
logger: this.host.logger,
|
|
63
|
+
elicit: this.elicit(target, options.signal),
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** A `system/init` of the session: it lists Claude Code's capabilities. */
|
|
68
|
+
init(capabilities) {
|
|
69
|
+
if (capabilities?.includes("ask_user_question_unanswered_v1"))
|
|
70
|
+
this.reportUnanswered = true;
|
|
71
|
+
}
|
|
72
|
+
/** Claude Code's `elicitation_complete`: a URL elicitation of an MCP server finished. */
|
|
73
|
+
completed(elicitationId) {
|
|
74
|
+
this.completedIds.add(elicitationId);
|
|
75
|
+
for (const release of this.awaitingCompletion.get(elicitationId) ?? [])
|
|
76
|
+
release();
|
|
77
|
+
if (!this.host.support.url || this.closed.signal.aborted)
|
|
78
|
+
return;
|
|
79
|
+
this.host.completeElicitation({ elicitationId }).catch((error) => {
|
|
80
|
+
this.host.logger.error(`Failed to complete elicitation: ${error}`);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Starts OAuth in the background for the servers of `serverNames` that
|
|
85
|
+
* Claude Code reports as `needs-auth`. Call it only once the setup
|
|
86
|
+
* response is sent: v2 asks for no elicitation before it.
|
|
87
|
+
*/
|
|
88
|
+
authenticateMcpServers(serverNames) {
|
|
89
|
+
startMcpServerAuthentication({
|
|
90
|
+
urlElicitation: this.host.support.url,
|
|
91
|
+
createElicitation: (request, signal) => this.host.createElicitation(request, signal),
|
|
92
|
+
completeElicitation: (params) => this.host.completeElicitation(params),
|
|
93
|
+
logger: this.host.logger,
|
|
94
|
+
}, {
|
|
95
|
+
key: this,
|
|
96
|
+
sessionId: this.host.sessionId,
|
|
97
|
+
currentQuery: () => (this.closed.signal.aborted ? undefined : this.host.query()),
|
|
98
|
+
closed: this.closed.signal,
|
|
99
|
+
}, new Set(serverNames));
|
|
100
|
+
}
|
|
101
|
+
/** The session closes: the OAuth runs stop, and withdraw their elicitations. */
|
|
102
|
+
close() {
|
|
103
|
+
this.closed.abort();
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The open turn ended: its accepted URL requests no longer hold it. A
|
|
107
|
+
* server that never reports its completion holds only its turn.
|
|
108
|
+
*/
|
|
109
|
+
turnEnded() {
|
|
110
|
+
for (const releases of [...this.awaitingCompletion.values()]) {
|
|
111
|
+
for (const release of [...releases])
|
|
112
|
+
release();
|
|
113
|
+
}
|
|
114
|
+
this.completedIds.clear();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Sends an elicitation as an open request of `target`. With
|
|
118
|
+
* `holdAcceptedUrl`, an accepted URL request then stays open (see the
|
|
119
|
+
* module doc).
|
|
120
|
+
*/
|
|
121
|
+
elicit(target, toolSignal, holdAcceptedUrl = false) {
|
|
122
|
+
return (request) => target.openRequest(toolSignal, async (signal) => {
|
|
123
|
+
const outcome = await elicitUnlessWithdrawn(signal, (signal) => this.host.createElicitation(request, signal));
|
|
124
|
+
const elicitationId = request.elicitationId;
|
|
125
|
+
if (holdAcceptedUrl &&
|
|
126
|
+
request.mode === "url" &&
|
|
127
|
+
typeof elicitationId === "string" &&
|
|
128
|
+
"response" in outcome &&
|
|
129
|
+
outcome.response.action === "accept") {
|
|
130
|
+
// Opened before this request settles, so the state stays
|
|
131
|
+
// `requires_action`. Claude Code gets the answer at once: the MCP
|
|
132
|
+
// server waits for the user's interaction itself.
|
|
133
|
+
void target.openRequest(toolSignal, (signal) => this.untilCompleted(elicitationId, signal));
|
|
134
|
+
}
|
|
135
|
+
return outcome;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Resolves once Claude Code reports the elicitation `elicitationId`
|
|
140
|
+
* complete, `signal` aborts (the tool use ended, or a cancel or close
|
|
141
|
+
* withdrew it), or the turn ends.
|
|
142
|
+
*/
|
|
143
|
+
untilCompleted(elicitationId, signal) {
|
|
144
|
+
if (this.completedIds.has(elicitationId) || signal.aborted)
|
|
145
|
+
return Promise.resolve();
|
|
146
|
+
return new Promise((resolve) => {
|
|
147
|
+
const releases = this.awaitingCompletion.get(elicitationId) ?? new Set();
|
|
148
|
+
this.awaitingCompletion.set(elicitationId, releases);
|
|
149
|
+
const release = () => {
|
|
150
|
+
signal.removeEventListener("abort", release);
|
|
151
|
+
releases.delete(release);
|
|
152
|
+
if (releases.size === 0)
|
|
153
|
+
this.awaitingCompletion.delete(elicitationId);
|
|
154
|
+
resolve();
|
|
155
|
+
};
|
|
156
|
+
signal.addEventListener("abort", release, { once: true });
|
|
157
|
+
releases.add(release);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AIR's agent file-change report (`agentFileChangeReport`) on a v2
|
|
3
|
+
* session: a `session/prompt` asks for it in `_meta.jetbrains.air`, and the
|
|
4
|
+
* turn that serves the prompt ends with one `session_info_update` that lists
|
|
5
|
+
* the files it changed.
|
|
6
|
+
*
|
|
7
|
+
* v1 sends the report before the prompt's response, which comes at the
|
|
8
|
+
* turn's end. A v2 prompt is answered at its insertion, so the report goes
|
|
9
|
+
* out at the end of the turn that the prompt started or joined, before the
|
|
10
|
+
* turn's idle. Building the report is shared with v1 (`file-change-audit.ts`):
|
|
11
|
+
* a dry-run rewind of the prompt's checkpoint, and the file-tool edits of
|
|
12
|
+
* subagents, which no checkpoint holds.
|
|
13
|
+
*/
|
|
14
|
+
import type { Query, SDKMessage } from "@anthropic-ai/claude-agent-sdk";
|
|
15
|
+
import { createNativeFileChangeReporter } from "../file-change-audit.js";
|
|
16
|
+
export declare class TurnFileChanges {
|
|
17
|
+
private readonly reporter;
|
|
18
|
+
/** The reports of the prompts that are not inserted yet, by prompt uuid. */
|
|
19
|
+
private readonly pending;
|
|
20
|
+
/** The reports of the prompts that the open turn serves. */
|
|
21
|
+
private readonly turn;
|
|
22
|
+
/** The tool uses of subagents in the open turn, for their file edits. */
|
|
23
|
+
private subagentToolUses;
|
|
24
|
+
private constructor();
|
|
25
|
+
/**
|
|
26
|
+
* The reports of a session, or undefined when the client did not declare
|
|
27
|
+
* `agentFileChangeReport`. `publish` sends one report's `_meta`.
|
|
28
|
+
*/
|
|
29
|
+
static create(supported: boolean, options: Parameters<typeof createNativeFileChangeReporter>[0]): TurnFileChanges | undefined;
|
|
30
|
+
/** A `session/prompt` came: its `_meta` may ask for a report. */
|
|
31
|
+
requested(promptUuid: string, meta: unknown): void;
|
|
32
|
+
/** The prompt failed before Claude Code inserted it: -32800 for a cancel, or another error. */
|
|
33
|
+
promptFailed(promptUuid: string, error: unknown): void;
|
|
34
|
+
/** Claude Code inserted the prompt: the open turn serves it. */
|
|
35
|
+
promptStarted(promptUuid: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* The open turn goes on in a fresh context (`clear-context.ts`), with the
|
|
38
|
+
* plan's prompt `planUuid`. `/clear` reset Claude Code's file history, so
|
|
39
|
+
* the checkpoints of the turn's own prompts are gone: its reports read the
|
|
40
|
+
* plan prompt's checkpoint. v1 sends the plan with the turn's prompt uuid
|
|
41
|
+
* instead (`clear-context-coordinator.ts`).
|
|
42
|
+
*/
|
|
43
|
+
contextCleared(planUuid: string): void;
|
|
44
|
+
/** A message of the query: a subagent's file-tool edits go into the open turn's reports. */
|
|
45
|
+
message(message: SDKMessage): void;
|
|
46
|
+
/**
|
|
47
|
+
* The open turn ends with `stopReason`. A turn that ended normally reads
|
|
48
|
+
* each report from its prompt's checkpoint, which takes up to
|
|
49
|
+
* `AGENT_FILE_CHANGE_REPORT_TIMEOUT_MS`: the returned promise resolves once
|
|
50
|
+
* every report went out, and the turn's idle waits for it. A cancelled or
|
|
51
|
+
* failed turn sends its reports `unavailable` at once.
|
|
52
|
+
*/
|
|
53
|
+
turnEnding(stopReason: string, query: Pick<Query, "rewindFiles"> | undefined): Promise<void> | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* The open turn closed. The reports that {@link turnEnding} did not read,
|
|
56
|
+
* such as of a turn that ended without a result, go out `unavailable`:
|
|
57
|
+
* `cancelled` after a cancel, else `providerError`.
|
|
58
|
+
*/
|
|
59
|
+
turnClosed(cancelled: boolean): void;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=file-changes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-changes.d.ts","sourceRoot":"","sources":["../../src/v2/file-changes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAExE,OAAO,EACL,8BAA8B,EAK/B,MAAM,yBAAyB,CAAC;AAIjC,qBAAa,eAAe;IAQN,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAP7C,4EAA4E;IAC5E,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiD;IACzE,4DAA4D;IAC5D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAoC;IACzD,yEAAyE;IACzE,OAAO,CAAC,gBAAgB,CAAgB;IAExC,OAAO;IAEP;;;OAGG;IACH,MAAM,CAAC,MAAM,CACX,SAAS,EAAE,OAAO,EAClB,OAAO,EAAE,UAAU,CAAC,OAAO,8BAA8B,CAAC,CAAC,CAAC,CAAC,GAC5D,eAAe,GAAG,SAAS;IAI9B,iEAAiE;IACjE,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI;IAKlD,+FAA+F;IAC/F,YAAY,CAAC,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;IAStD,gEAAgE;IAChE,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAOvC;;;;;;OAMG;IACH,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAItC,4FAA4F;IAC5F,OAAO,CAAC,OAAO,EAAE,UAAU,GAAG,IAAI;IAoBlC;;;;;;OAMG;IACH,UAAU,CACR,UAAU,EAAE,MAAM,EAClB,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,aAAa,CAAC,GAAG,SAAS,GAC5C,OAAO,CAAC,IAAI,CAAC,GAAG,SAAS;IAW5B;;;;OAIG;IACH,UAAU,CAAC,SAAS,EAAE,OAAO,GAAG,IAAI;CAKrC"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { RequestError } from "@agentclientprotocol/sdk/unstable";
|
|
2
|
+
import { createNativeFileChangeReporter, recordSubagentFileEdits, } from "../file-change-audit.js";
|
|
3
|
+
export class TurnFileChanges {
|
|
4
|
+
reporter;
|
|
5
|
+
/** The reports of the prompts that are not inserted yet, by prompt uuid. */
|
|
6
|
+
pending = new Map();
|
|
7
|
+
/** The reports of the prompts that the open turn serves. */
|
|
8
|
+
turn = [];
|
|
9
|
+
/** The tool uses of subagents in the open turn, for their file edits. */
|
|
10
|
+
subagentToolUses = {};
|
|
11
|
+
constructor(reporter) {
|
|
12
|
+
this.reporter = reporter;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The reports of a session, or undefined when the client did not declare
|
|
16
|
+
* `agentFileChangeReport`. `publish` sends one report's `_meta`.
|
|
17
|
+
*/
|
|
18
|
+
static create(supported, options) {
|
|
19
|
+
return supported ? new TurnFileChanges(createNativeFileChangeReporter(options)) : undefined;
|
|
20
|
+
}
|
|
21
|
+
/** A `session/prompt` came: its `_meta` may ask for a report. */
|
|
22
|
+
requested(promptUuid, meta) {
|
|
23
|
+
const fileChangeReport = this.reporter.request(meta);
|
|
24
|
+
if (fileChangeReport)
|
|
25
|
+
this.pending.set(promptUuid, { promptUuid, fileChangeReport });
|
|
26
|
+
}
|
|
27
|
+
/** The prompt failed before Claude Code inserted it: -32800 for a cancel, or another error. */
|
|
28
|
+
promptFailed(promptUuid, error) {
|
|
29
|
+
const report = this.pending.get(promptUuid);
|
|
30
|
+
if (!report)
|
|
31
|
+
return;
|
|
32
|
+
this.pending.delete(promptUuid);
|
|
33
|
+
const cancelled = error instanceof RequestError && error.code === RequestError.requestCancelled().code;
|
|
34
|
+
this.reporter.finish(report.fileChangeReport, cancelled ? "cancelled" : "providerError");
|
|
35
|
+
}
|
|
36
|
+
/** Claude Code inserted the prompt: the open turn serves it. */
|
|
37
|
+
promptStarted(promptUuid) {
|
|
38
|
+
const report = this.pending.get(promptUuid);
|
|
39
|
+
if (!report)
|
|
40
|
+
return;
|
|
41
|
+
this.pending.delete(promptUuid);
|
|
42
|
+
this.turn.push(report);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The open turn goes on in a fresh context (`clear-context.ts`), with the
|
|
46
|
+
* plan's prompt `planUuid`. `/clear` reset Claude Code's file history, so
|
|
47
|
+
* the checkpoints of the turn's own prompts are gone: its reports read the
|
|
48
|
+
* plan prompt's checkpoint. v1 sends the plan with the turn's prompt uuid
|
|
49
|
+
* instead (`clear-context-coordinator.ts`).
|
|
50
|
+
*/
|
|
51
|
+
contextCleared(planUuid) {
|
|
52
|
+
for (const report of this.turn)
|
|
53
|
+
report.promptUuid = planUuid;
|
|
54
|
+
}
|
|
55
|
+
/** A message of the query: a subagent's file-tool edits go into the open turn's reports. */
|
|
56
|
+
message(message) {
|
|
57
|
+
if (this.turn.length === 0)
|
|
58
|
+
return;
|
|
59
|
+
if (message.type !== "assistant" && message.type !== "user")
|
|
60
|
+
return;
|
|
61
|
+
const parent = message.parent_tool_use_id;
|
|
62
|
+
if (parent === null)
|
|
63
|
+
return;
|
|
64
|
+
const content = message.message.content;
|
|
65
|
+
if (!Array.isArray(content))
|
|
66
|
+
return;
|
|
67
|
+
if (message.type === "assistant") {
|
|
68
|
+
for (const block of content) {
|
|
69
|
+
if (block.type === "tool_use") {
|
|
70
|
+
this.subagentToolUses[block.id] = { name: block.name, input: block.input };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
for (const report of this.turn) {
|
|
76
|
+
recordSubagentFileEdits(report, parent, content, this.subagentToolUses);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The open turn ends with `stopReason`. A turn that ended normally reads
|
|
81
|
+
* each report from its prompt's checkpoint, which takes up to
|
|
82
|
+
* `AGENT_FILE_CHANGE_REPORT_TIMEOUT_MS`: the returned promise resolves once
|
|
83
|
+
* every report went out, and the turn's idle waits for it. A cancelled or
|
|
84
|
+
* failed turn sends its reports `unavailable` at once.
|
|
85
|
+
*/
|
|
86
|
+
turnEnding(stopReason, query) {
|
|
87
|
+
if (this.turn.length === 0)
|
|
88
|
+
return undefined;
|
|
89
|
+
if (stopReason === "cancelled" || stopReason === "error" || !query) {
|
|
90
|
+
this.turnClosed(stopReason === "cancelled");
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const reports = this.turn.splice(0);
|
|
94
|
+
this.subagentToolUses = {};
|
|
95
|
+
return Promise.all(reports.map((report) => this.reporter.report(report, query))).then(() => { });
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The open turn closed. The reports that {@link turnEnding} did not read,
|
|
99
|
+
* such as of a turn that ended without a result, go out `unavailable`:
|
|
100
|
+
* `cancelled` after a cancel, else `providerError`.
|
|
101
|
+
*/
|
|
102
|
+
turnClosed(cancelled) {
|
|
103
|
+
const reason = cancelled ? "cancelled" : "providerError";
|
|
104
|
+
for (const report of this.turn.splice(0))
|
|
105
|
+
this.reporter.finish(report.fileChangeReport, reason);
|
|
106
|
+
this.subagentToolUses = {};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The PreToolUse hook that shows an Edit's or Write's exact patch before the
|
|
3
|
+
* tool runs, also when no permission request shows it.
|
|
4
|
+
*
|
|
5
|
+
* A v2 diff cannot hold the input's snippet, so without it an auto-approved
|
|
6
|
+
* Edit would show its change only once it is done. Claude Code runs the tool
|
|
7
|
+
* only after the hook returns, so the file is still as it was, and the patch
|
|
8
|
+
* is the one a permission request for the tool would show. A patch whose tool
|
|
9
|
+
* call is not published yet waits for it (`SessionOutput.preview`). The hook
|
|
10
|
+
* makes no permission decision, and a failure only leaves the preview out.
|
|
11
|
+
*/
|
|
12
|
+
import type { HookCallbackMatcher } from "@anthropic-ai/claude-agent-sdk";
|
|
13
|
+
import type { Logger } from "../logger.js";
|
|
14
|
+
import type { PermissionHost } from "./permissions.js";
|
|
15
|
+
/** The session of a tool use (the root's, or a subagent's), and the tool uses that a cancel drops. */
|
|
16
|
+
export type PreviewHost = Pick<PermissionHost, "target" | "dropsToolUse">;
|
|
17
|
+
export declare function fileChangePreviewHook(host: PreviewHost, logger: Logger): HookCallbackMatcher;
|
|
18
|
+
//# sourceMappingURL=file-preview.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-preview.d.ts","sourceRoot":"","sources":["../../src/v2/file-preview.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAE3C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,sGAAsG;AACtG,MAAM,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,EAAE,QAAQ,GAAG,cAAc,CAAC,CAAC;AAE1E,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,mBAAmB,CAsB5F"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { previewPatchContent } from "../diff.js";
|
|
2
|
+
export function fileChangePreviewHook(host, logger) {
|
|
3
|
+
return {
|
|
4
|
+
matcher: "Edit|Write",
|
|
5
|
+
hooks: [
|
|
6
|
+
async (input, toolUseID, { signal }) => {
|
|
7
|
+
if (input.hook_event_name !== "PreToolUse" || !toolUseID)
|
|
8
|
+
return { continue: true };
|
|
9
|
+
try {
|
|
10
|
+
// Work that the client was told never ran shows nothing, as in `canUseTool`.
|
|
11
|
+
if (await host.dropsToolUse(toolUseID, input.agent_id, signal))
|
|
12
|
+
return { continue: true };
|
|
13
|
+
const target = await host.target(input.agent_id);
|
|
14
|
+
// The work a cancel stopped shows nothing, as in `canUseTool`.
|
|
15
|
+
if (target.cancelling())
|
|
16
|
+
return { continue: true };
|
|
17
|
+
const toolInput = (input.tool_input ?? {});
|
|
18
|
+
const content = await previewPatchContent(input.tool_name, toolInput, target.cwd, "v2");
|
|
19
|
+
if (content)
|
|
20
|
+
target.output.preview(toolUseID, content);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
logger.error(`Failed to preview ${toolUseID}:`, error);
|
|
24
|
+
}
|
|
25
|
+
return { continue: true };
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
}
|
package/dist/v2/initialize.d.ts
CHANGED
|
@@ -1,36 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* The agent decides its capabilities and auth methods in v1 terms. The v2
|
|
6
|
-
* request becomes the v1 request that the agent reads, and the v1 response of
|
|
7
|
-
* the agent becomes the v2 response, so both protocol versions advertise from
|
|
8
|
-
* one source.
|
|
9
|
-
*/
|
|
10
|
-
import { type InitializeRequest, type InitializeResponse } from "@agentclientprotocol/sdk/unstable";
|
|
11
|
-
import * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
12
|
-
/** The v1 `initialize` request that a v2 client means. */
|
|
13
|
-
export declare function v1InitializeRequest(request: v2.InitializeRequest): InitializeRequest;
|
|
14
|
-
/**
|
|
15
|
-
* The v2 `initialize` response for the v1 response of the agent.
|
|
16
|
-
*
|
|
17
|
-
* It does not advertise what the v2 surface does not serve yet: the
|
|
18
|
-
* extensions of {@link UNSERVED_EXTENSION_KEYS}, and the AIR extensions of
|
|
19
|
-
* {@link withoutUnservedAirExtensions}.
|
|
2
|
+
* What the ACP v2 `initialize` response advertises: the session capabilities,
|
|
3
|
+
* and the v2 form of the auth methods, which the shared auth modules build in
|
|
4
|
+
* v1 terms (`auth-methods.ts`).
|
|
20
5
|
*/
|
|
21
|
-
|
|
6
|
+
import type { AuthMethod } from "@agentclientprotocol/sdk/unstable";
|
|
7
|
+
import type * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
22
8
|
/**
|
|
23
|
-
* The session capabilities
|
|
24
|
-
* method of the agent.
|
|
9
|
+
* The session capabilities that the v2 core serves.
|
|
25
10
|
*
|
|
26
11
|
* - The session baseline: `session/new`, `list`, `resume` (with replay),
|
|
27
12
|
* `close`, `prompt`, `cancel`, and `update`.
|
|
28
13
|
* - `session/delete`, `session/fork` (which returns a session the client can
|
|
29
14
|
* prompt at once), and `additionalDirectories`.
|
|
30
|
-
* - The prompt content and MCP transports that the
|
|
31
|
-
*
|
|
32
|
-
* v1 baseline; v2 has no SSE transport.
|
|
15
|
+
* - The prompt content and MCP transports that the v1 `initialize` lists too:
|
|
16
|
+
* images, embedded context, and HTTP servers. v2 has no SSE transport.
|
|
33
17
|
* - `systemPrompt`, in `append` and `override` mode.
|
|
34
18
|
*/
|
|
35
19
|
export declare const V2_SESSION_CAPABILITIES: v2.SessionCapabilities;
|
|
20
|
+
/** v2 names the id of a method `methodId`, requires `type`, and lists `env` as name and value pairs. */
|
|
21
|
+
export declare function v2AuthMethod(method: AuthMethod): v2.AuthMethod;
|
|
36
22
|
//# sourceMappingURL=initialize.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialize.d.ts","sourceRoot":"","sources":["../../src/v2/initialize.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"initialize.d.ts","sourceRoot":"","sources":["../../src/v2/initialize.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,KAAK,KAAK,EAAE,MAAM,mDAAmD,CAAC;AAG7E;;;;;;;;;;GAUG;AACH,eAAO,MAAM,uBAAuB,EAAE,EAAE,CAAC,mBAOxC,CAAC;AAEF,wGAAwG;AACxG,wBAAgB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,EAAE,CAAC,UAAU,CAW9D"}
|
package/dist/v2/initialize.js
CHANGED
|
@@ -1,120 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The ACP v2 `initialize` handshake, expressed through the v1 handshake of
|
|
3
|
-
* `ClaudeAcpAgent`.
|
|
4
|
-
*
|
|
5
|
-
* The agent decides its capabilities and auth methods in v1 terms. The v2
|
|
6
|
-
* request becomes the v1 request that the agent reads, and the v1 response of
|
|
7
|
-
* the agent becomes the v2 response, so both protocol versions advertise from
|
|
8
|
-
* one source.
|
|
9
|
-
*/
|
|
10
|
-
import { PROTOCOL_VERSION as V1_PROTOCOL_VERSION, } from "@agentclientprotocol/sdk/unstable";
|
|
11
|
-
import * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
12
|
-
import { AIR_ASYNC_TASKS_CAPABILITY, AIR_GOAL_KEY } from "../air-extension.js";
|
|
13
1
|
import { SYSTEM_PROMPT_CAPABILITY } from "../client-system-prompt.js";
|
|
14
|
-
/** The v1 `initialize` request that a v2 client means. */
|
|
15
|
-
export function v1InitializeRequest(request) {
|
|
16
|
-
const capabilities = request.capabilities;
|
|
17
|
-
return {
|
|
18
|
-
protocolVersion: V1_PROTOCOL_VERSION,
|
|
19
|
-
clientInfo: request.info,
|
|
20
|
-
clientCapabilities: {
|
|
21
|
-
// v2 has no client file system and no client terminals: the agent
|
|
22
|
-
// reads, writes, and runs commands itself.
|
|
23
|
-
fs: { readTextFile: false, writeTextFile: false },
|
|
24
|
-
terminal: false,
|
|
25
|
-
// Every v2 client handles boolean config options, notices, and
|
|
26
|
-
// compaction updates; v1 asks for a marker. With them, the agent reports
|
|
27
|
-
// advisories as notices rather than as transcript messages, and a
|
|
28
|
-
// compaction as a compaction entity rather than as a "Compact
|
|
29
|
-
// conversation" tool call, also on replay.
|
|
30
|
-
session: { configOptions: { boolean: {} }, notices: {}, compaction: {} },
|
|
31
|
-
// Every v2 client takes `plan_update`, with the draft's markdown and file
|
|
32
|
-
// plans, and `plan_removed`; v1 asks for the `plan` marker. With it, the
|
|
33
|
-
// agent shows an approved ExitPlanMode plan as a markdown plan.
|
|
34
|
-
plan: {},
|
|
35
|
-
// Subagent updates are part of v2's baseline session model (the subagents
|
|
36
|
-
// RFD), so every v2 client takes them; v1 asks for the `subagents`
|
|
37
|
-
// capability. With it, the agent reports each Agent or Task subagent as a
|
|
38
|
-
// child session.
|
|
39
|
-
subagents: {},
|
|
40
|
-
// v1 marks terminal auth support with a boolean, v2 with an object.
|
|
41
|
-
auth: {
|
|
42
|
-
terminal: capabilities?.auth?.terminal != null,
|
|
43
|
-
...(capabilities?.auth?._meta != null ? { _meta: capabilities.auth._meta } : {}),
|
|
44
|
-
},
|
|
45
|
-
...(capabilities?.elicitation != null ? { elicitation: capabilities.elicitation } : {}),
|
|
46
|
-
...(capabilities?._meta != null ? { _meta: capabilities._meta } : {}),
|
|
47
|
-
},
|
|
48
|
-
...(request._meta != null ? { _meta: request._meta } : {}),
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* The top-level `_meta` keys of the agent's v1 `initialize` response that
|
|
53
|
-
* advertise an extension the v2 surface does not serve yet: `steering`, for
|
|
54
|
-
* `_session/steering`.
|
|
55
|
-
*/
|
|
56
|
-
const UNSERVED_EXTENSION_KEYS = ["steering"];
|
|
57
|
-
/**
|
|
58
|
-
* The v2 `initialize` response for the v1 response of the agent.
|
|
59
|
-
*
|
|
60
|
-
* It does not advertise what the v2 surface does not serve yet: the
|
|
61
|
-
* extensions of {@link UNSERVED_EXTENSION_KEYS}, and the AIR extensions of
|
|
62
|
-
* {@link withoutUnservedAirExtensions}.
|
|
63
|
-
*/
|
|
64
|
-
export function v2InitializeResponse(response) {
|
|
65
|
-
if (!response.agentInfo) {
|
|
66
|
-
throw new Error("ACP v2 requires agentInfo, and the agent reported none");
|
|
67
|
-
}
|
|
68
|
-
const meta = response.agentCapabilities?._meta;
|
|
69
|
-
const topMeta = withoutUnservedAirExtensions(Object.fromEntries(Object.entries(response._meta ?? {}).filter(([key]) => !UNSERVED_EXTENSION_KEYS.includes(key))));
|
|
70
|
-
return {
|
|
71
|
-
protocolVersion: v2.PROTOCOL_VERSION,
|
|
72
|
-
info: response.agentInfo,
|
|
73
|
-
// v1 `auth.logout` has no v2 counterpart: a v2 agent that advertises auth
|
|
74
|
-
// methods must serve `auth/logout`.
|
|
75
|
-
capabilities: {
|
|
76
|
-
session: V2_SESSION_CAPABILITIES,
|
|
77
|
-
// `providers/list`, `set`, and `disable`, which the agent serves for
|
|
78
|
-
// every client.
|
|
79
|
-
providers: {},
|
|
80
|
-
...(meta != null ? { _meta: meta } : {}),
|
|
81
|
-
},
|
|
82
|
-
authMethods: (response.authMethods ?? []).map(v2AuthMethod),
|
|
83
|
-
...(Object.keys(topMeta).length > 0 ? { _meta: topMeta } : {}),
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* `meta` without the AIR extensions whose methods the v2 surface does not
|
|
88
|
-
* register: the `asyncTasks` capability (`_session/async_task/stop`) and the
|
|
89
|
-
* `goal` entry (`_session/goal`) of `_meta.jetbrains.air`.
|
|
90
|
-
*/
|
|
91
|
-
function withoutUnservedAirExtensions(meta) {
|
|
92
|
-
const jetbrains = asRecord(meta.jetbrains);
|
|
93
|
-
const air = asRecord(jetbrains.air);
|
|
94
|
-
if (Object.keys(air).length === 0)
|
|
95
|
-
return meta;
|
|
96
|
-
const served = Object.fromEntries(Object.entries(air).filter(([key]) => key !== AIR_GOAL_KEY));
|
|
97
|
-
if (Array.isArray(served.capabilities)) {
|
|
98
|
-
served.capabilities = served.capabilities.filter((capability) => capability !== AIR_ASYNC_TASKS_CAPABILITY);
|
|
99
|
-
}
|
|
100
|
-
return { ...meta, jetbrains: { ...jetbrains, air: served } };
|
|
101
|
-
}
|
|
102
|
-
function asRecord(value) {
|
|
103
|
-
return value && typeof value === "object" && !Array.isArray(value)
|
|
104
|
-
? value
|
|
105
|
-
: {};
|
|
106
|
-
}
|
|
107
2
|
/**
|
|
108
|
-
* The session capabilities
|
|
109
|
-
* method of the agent.
|
|
3
|
+
* The session capabilities that the v2 core serves.
|
|
110
4
|
*
|
|
111
5
|
* - The session baseline: `session/new`, `list`, `resume` (with replay),
|
|
112
6
|
* `close`, `prompt`, `cancel`, and `update`.
|
|
113
7
|
* - `session/delete`, `session/fork` (which returns a session the client can
|
|
114
8
|
* prompt at once), and `additionalDirectories`.
|
|
115
|
-
* - The prompt content and MCP transports that the
|
|
116
|
-
*
|
|
117
|
-
* v1 baseline; v2 has no SSE transport.
|
|
9
|
+
* - The prompt content and MCP transports that the v1 `initialize` lists too:
|
|
10
|
+
* images, embedded context, and HTTP servers. v2 has no SSE transport.
|
|
118
11
|
* - `systemPrompt`, in `append` and `override` mode.
|
|
119
12
|
*/
|
|
120
13
|
export const V2_SESSION_CAPABILITIES = {
|
|
@@ -126,7 +19,7 @@ export const V2_SESSION_CAPABILITIES = {
|
|
|
126
19
|
systemPrompt: SYSTEM_PROMPT_CAPABILITY,
|
|
127
20
|
};
|
|
128
21
|
/** v2 names the id of a method `methodId`, requires `type`, and lists `env` as name and value pairs. */
|
|
129
|
-
function v2AuthMethod(method) {
|
|
22
|
+
export function v2AuthMethod(method) {
|
|
130
23
|
if ("type" in method && method.type === "terminal") {
|
|
131
24
|
const { id, env, ...terminal } = method;
|
|
132
25
|
return {
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type * as v2 from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
2
|
+
import type { SDKAssistantMessage, SDKAssistantMessageError, SDKResultMessage } from "@anthropic-ai/claude-agent-sdk";
|
|
3
|
+
import { quotaMeta } from "../session-usage.js";
|
|
4
|
+
/** A result that ended a turn, with what the turn spent. */
|
|
5
|
+
export type TurnEnd = {
|
|
6
|
+
result: SDKResultMessage;
|
|
7
|
+
usage: v2.Usage;
|
|
8
|
+
_meta: ReturnType<typeof quotaMeta>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The idle members this core reports. Typed as those members, so the checker
|
|
12
|
+
* matches each stop reason to its own, which it cannot against the whole
|
|
13
|
+
* union with its custom and unknown members.
|
|
14
|
+
*/
|
|
15
|
+
export type IdleOutcome = Extract<v2.IdleStateUpdate, {
|
|
16
|
+
stopReason: "end_turn" | "max_tokens" | "max_turn_requests" | "refusal" | "cancelled" | "error";
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* The stop reason, usage and `_meta.quota` of an idle, from the result that
|
|
20
|
+
* ended the turn, and what its assistant messages reported (`report`).
|
|
21
|
+
* `prompted`: a prompt started the turn. Without a result, such as when an
|
|
22
|
+
* interrupt stopped the work before its result, a prompt's turn reports zero
|
|
23
|
+
* usage and a zero quota, and a cycle that Claude Code started on its own
|
|
24
|
+
* reports neither. A failed turn reports its error instead.
|
|
25
|
+
*/
|
|
26
|
+
export declare function idleOutcome(end: TurnEnd | undefined, cancelled: boolean, report?: TurnReport, prompted?: boolean): IdleOutcome;
|
|
27
|
+
/**
|
|
28
|
+
* What the assistant messages of the open turn reported about its end: the
|
|
29
|
+
* error kind of a failed API request, Claude Code's sign-in message, and a
|
|
30
|
+
* refusal's explanation. Read from the main conversation's messages.
|
|
31
|
+
*/
|
|
32
|
+
export declare class TurnReport {
|
|
33
|
+
errorKind: SDKAssistantMessageError | undefined;
|
|
34
|
+
/** Claude Code showed its sign-in message ("Not logged in · Please run /login"). */
|
|
35
|
+
loginRequired: boolean;
|
|
36
|
+
/** The explanation of the turn's refusal. */
|
|
37
|
+
refusalExplanation: string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* An assistant message of the main conversation. Returns whether it is
|
|
40
|
+
* Claude Code's sign-in message, which the client is not shown: its
|
|
41
|
+
* `/login` instruction is for Claude Code's terminal, and the turn's idle
|
|
42
|
+
* fails with `authRequired` instead.
|
|
43
|
+
*/
|
|
44
|
+
assistant(message: SDKAssistantMessage): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Claude Code's `model_refusal_no_fallback`: the explanation when the
|
|
47
|
+
* refused message carried none (older CLIs, gateways that drop
|
|
48
|
+
* `stop_details`). One for a turn that no user prompt started
|
|
49
|
+
* (`refused_user_message_uuid: null`) is not this turn's.
|
|
50
|
+
*/
|
|
51
|
+
refusalWithoutFallback(message: {
|
|
52
|
+
api_refusal_explanation?: string | null;
|
|
53
|
+
refused_user_message_uuid?: string | null;
|
|
54
|
+
content: string;
|
|
55
|
+
}): void;
|
|
56
|
+
reset(): void;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Whether `result` is a placeholder: Claude Code answers background-task
|
|
60
|
+
* notifications that queued up with one model call, and gives each but the
|
|
61
|
+
* last a result with `num_turns: 0` before that call runs. The cycle goes on
|
|
62
|
+
* to the last one's result.
|
|
63
|
+
*/
|
|
64
|
+
export declare function isPlaceholderResult(result: SDKResultMessage): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* What each result spent. `result.modelUsage` is a running total of the
|
|
67
|
+
* query, so a result's rows are its increment over the previous reading. A
|
|
68
|
+
* resumed query's first reading continues the totals of its transcript, so
|
|
69
|
+
* that result's row is its own `usage`, under the model of the last
|
|
70
|
+
* assistant message (see `resumedFirstResultModelUsage`).
|
|
71
|
+
*/
|
|
72
|
+
export declare class ResultUsage {
|
|
73
|
+
private reading;
|
|
74
|
+
private model;
|
|
75
|
+
constructor(resumed: boolean);
|
|
76
|
+
/** An assistant message of the main conversation, by `model`. */
|
|
77
|
+
assistant(model: string | undefined): void;
|
|
78
|
+
/** What `result` spent. Every result moves the reading on, also a placeholder. */
|
|
79
|
+
record(result: SDKResultMessage): TurnEnd;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=outcome.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outcome.d.ts","sourceRoot":"","sources":["../../src/v2/outcome.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,KAAK,EAAE,MAAM,mDAAmD,CAAC;AAC7E,OAAO,KAAK,EACV,mBAAmB,EACnB,wBAAwB,EACxB,gBAAgB,EACjB,MAAM,gCAAgC,CAAC;AAKxC,OAAO,EAIL,SAAS,EAKV,MAAM,qBAAqB,CAAC;AAE7B,4DAA4D;AAC5D,MAAM,MAAM,OAAO,GAAG;IACpB,MAAM,EAAE,gBAAgB,CAAC;IACzB,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC;IAChB,KAAK,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC;CACrC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,WAAW,GAAG,OAAO,CAC/B,EAAE,CAAC,eAAe,EAClB;IACE,UAAU,EAAE,UAAU,GAAG,YAAY,GAAG,mBAAmB,GAAG,SAAS,GAAG,WAAW,GAAG,OAAO,CAAC;CACjG,CACF,CAAC;AAUF;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,SAAS,EAAE,OAAO,EAClB,MAAM,CAAC,EAAE,UAAU,EACnB,QAAQ,UAAQ,GACf,WAAW,CAyBb;AAaD;;;;GAIG;AACH,qBAAa,UAAU;IACrB,SAAS,EAAE,wBAAwB,GAAG,SAAS,CAAC;IAChD,oFAAoF;IACpF,aAAa,UAAS;IACtB,6CAA6C;IAC7C,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IAEvC;;;;;OAKG;IACH,SAAS,CAAC,OAAO,EAAE,mBAAmB,GAAG,OAAO;IAWhD;;;;;OAKG;IACH,sBAAsB,CAAC,OAAO,EAAE;QAC9B,uBAAuB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QACxC,yBAAyB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC1C,OAAO,EAAE,MAAM,CAAC;KACjB,GAAG,IAAI;IAKR,KAAK,IAAI,IAAI;CAKd;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAOrE;AAED;;;;;;GAMG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,KAAK,CAAuB;gBAExB,OAAO,EAAE,OAAO;IAI5B,iEAAiE;IACjE,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI;IAI1C,kFAAkF;IAClF,MAAM,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO;CAuB1C"}
|