@nyte-ai/plugin 0.0.0-stage → 0.0.18
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/LICENSE +21 -0
- package/THIRD-PARTY-NOTICES.md +800 -0
- package/dist/chunks/billing-0y7dfkp2.js +146 -0
- package/dist/chunks/billing-2v3g7rd9.js +190 -0
- package/dist/chunks/billing-3j77ss7y.js +86 -0
- package/dist/chunks/billing-3scqmkzb.js +559 -0
- package/dist/chunks/billing-6z2kgtyt.js +51 -0
- package/dist/chunks/billing-8rjk35gr.js +444 -0
- package/dist/chunks/billing-9mfedmsh.js +373 -0
- package/dist/chunks/billing-9nn3yp18.js +176 -0
- package/dist/chunks/billing-9qc0eqh4.js +174 -0
- package/dist/chunks/billing-a9vqn66p.js +351 -0
- package/dist/chunks/billing-bv5zr32b.js +369 -0
- package/dist/chunks/billing-cy1rg619.js +302 -0
- package/dist/chunks/billing-ejx2hv52.js +323 -0
- package/dist/chunks/billing-h13r0byk.js +80 -0
- package/dist/chunks/billing-j643d12p.js +197 -0
- package/dist/chunks/billing-k2p1t25n.js +109 -0
- package/dist/chunks/billing-kn3d30b4.js +276 -0
- package/dist/chunks/billing-mvc33pv6.js +36 -0
- package/dist/chunks/billing-s4rrspsk.js +7 -0
- package/dist/chunks/billing-svrhh5a5.js +307 -0
- package/dist/chunks/billing-sxc70w72.js +161 -0
- package/dist/chunks/billing-t1qrxe16.js +41 -0
- package/dist/chunks/billing-tzr3cebf.js +20 -0
- package/dist/chunks/billing-wmcbg6fw.js +52 -0
- package/dist/chunks/billing-wmkxkypm.js +527 -0
- package/dist/chunks/billing-ye3wk5r3.js +205 -0
- package/dist/chunks/billing-zfpm2rt0.js +29 -0
- package/dist/examples/claude-code/LICENSE +21 -0
- package/dist/examples/claude-code/billing.d.ts +7 -0
- package/dist/examples/claude-code/billing.js +11 -0
- package/dist/examples/claude-code/claude-cli.d.ts +35 -0
- package/dist/examples/claude-code/claude-cli.js +15 -0
- package/dist/examples/claude-code/env.d.ts +1 -0
- package/dist/examples/claude-code/env.js +6 -0
- package/dist/examples/claude-code/index.d.ts +24 -0
- package/dist/examples/claude-code/index.js +149 -0
- package/dist/examples/claude-code/inert-mcp.d.ts +1 -0
- package/dist/examples/claude-code/inert-mcp.js +6 -0
- package/dist/examples/claude-code/model-catalog.d.ts +27 -0
- package/dist/examples/claude-code/model-catalog.js +12 -0
- package/dist/examples/claude-code/process.d.ts +22 -0
- package/dist/examples/claude-code/process.js +25 -0
- package/dist/examples/claude-code/replay.d.ts +4 -0
- package/dist/examples/claude-code/replay.js +11 -0
- package/dist/examples/claude-code/stream.d.ts +3 -0
- package/dist/examples/claude-code/stream.js +16 -0
- package/dist/examples/claude-code/transcript.d.ts +83 -0
- package/dist/examples/claude-code/transcript.js +23 -0
- package/dist/examples/claude-code/turn-relay.d.ts +48 -0
- package/dist/examples/claude-code/turn-relay.js +9 -0
- package/dist/examples/claude-code/wire.d.ts +372 -0
- package/dist/examples/claude-code/wire.js +18 -0
- package/dist/src/acp/connection.d.ts +24 -0
- package/dist/src/acp/connection.js +8 -0
- package/dist/src/acp/index.d.ts +54 -0
- package/dist/src/acp/index.js +1004 -0
- package/dist/src/acp/registry.d.ts +18 -0
- package/dist/src/acp/registry.js +8 -0
- package/dist/src/acp/thread.d.ts +46 -0
- package/dist/src/acp/thread.js +7 -0
- package/dist/src/acp/transcript.d.ts +8 -0
- package/dist/src/acp/transcript.js +11 -0
- package/dist/src/acp/translate.d.ts +33 -0
- package/dist/src/acp/translate.js +20 -0
- package/dist/src/approvals/approvals.d.ts +189 -0
- package/dist/src/approvals/approvals.js +143 -0
- package/dist/src/approvals/schema.d.ts +169 -0
- package/dist/src/approvals/schema.js +18 -0
- package/dist/src/bash-description.d.ts +30 -0
- package/dist/src/bash-description.js +18 -0
- package/dist/src/browser-tools.d.ts +25 -0
- package/dist/src/browser-tools.js +246 -0
- package/dist/src/canvas/canvas-compile.d.ts +48 -0
- package/dist/src/canvas/canvas-compile.js +8 -0
- package/dist/src/canvas/canvas-libraries.d.ts +32 -0
- package/dist/src/canvas/canvas-libraries.js +6 -0
- package/dist/src/canvas/canvas-preview.d.ts +107 -0
- package/dist/src/canvas/canvas-preview.js +12 -0
- package/dist/src/canvas/canvas-protocol.d.ts +184 -0
- package/dist/src/canvas/canvas-protocol.js +75 -0
- package/dist/src/canvas/canvas.d.ts +50 -0
- package/dist/src/canvas/canvas.js +221 -0
- package/dist/src/canvas/schema.d.ts +47 -0
- package/dist/src/canvas/schema.js +8 -0
- package/dist/src/claude-code-sdk/index.d.ts +35 -0
- package/dist/src/claude-code-sdk/index.js +368 -0
- package/dist/src/claude-code-sdk/session.d.ts +48 -0
- package/dist/src/claude-code-sdk/session.js +7 -0
- package/dist/src/claude-code-sdk/transcript.d.ts +19 -0
- package/dist/src/claude-code-sdk/transcript.js +15 -0
- package/dist/src/claude-code-sdk/translate.d.ts +18 -0
- package/dist/src/claude-code-sdk/translate.js +20 -0
- package/dist/src/client/mount.d.ts +44 -0
- package/dist/src/client/mount.js +252 -0
- package/dist/src/client/notifications.d.ts +35 -0
- package/dist/src/client/notifications.js +26 -0
- package/dist/src/client/presentation.d.ts +336 -0
- package/dist/src/client/presentation.js +373 -0
- package/dist/src/client/transcript-tui.d.ts +23 -0
- package/dist/src/client/transcript-tui.js +20 -0
- package/dist/src/client/transcript.d.ts +23 -0
- package/dist/src/client/transcript.js +51 -0
- package/dist/src/client.d.ts +53 -0
- package/dist/src/client.js +22 -0
- package/dist/src/codemode-runtime.d.ts +11 -0
- package/dist/src/codemode-runtime.js +10 -0
- package/dist/src/codemode-worker.d.ts +1 -0
- package/dist/src/codemode-worker.js +2 -0
- package/dist/src/codemode.d.ts +21 -0
- package/dist/src/codemode.js +170 -0
- package/dist/src/core.d.ts +43 -0
- package/dist/src/core.js +11 -0
- package/dist/src/openai-astra-context.d.ts +23 -0
- package/dist/src/openai-astra-context.js +14 -0
- package/dist/src/openai-compaction.d.ts +15 -0
- package/dist/src/openai-compaction.js +55 -0
- package/dist/src/question/client/questions-tui.d.ts +9 -0
- package/dist/src/question/client/questions-tui.js +19 -0
- package/dist/src/question/client/questions.d.ts +9 -0
- package/dist/src/question/client/questions.js +160 -0
- package/dist/src/question/question.d.ts +63 -0
- package/dist/src/question/question.js +9 -0
- package/dist/src/question/schema.d.ts +101 -0
- package/dist/src/question/schema.js +14 -0
- package/dist/src/rename.d.ts +78 -0
- package/dist/src/rename.js +78 -0
- package/dist/src/telemetry.d.ts +36 -0
- package/dist/src/telemetry.js +84 -0
- package/dist/src/web-search.d.ts +78 -0
- package/dist/src/web-search.js +219 -0
- package/package.json +190 -3
- package/README.md +0 -3
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BACKGROUND_TOOL2,
|
|
3
|
+
NYTE_SERVER2,
|
|
4
|
+
NYTE_TOOL_PREFIX2,
|
|
5
|
+
emptyMessage3,
|
|
6
|
+
mirrorCall2,
|
|
7
|
+
Translator3
|
|
8
|
+
} from "./billing-ye3wk5r3.js";
|
|
9
|
+
|
|
10
|
+
// src/claude-code-sdk/session.ts
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
12
|
+
import { createSdkMcpServer } from "@anthropic-ai/claude-agent-sdk";
|
|
13
|
+
import { createAssistantMessageEventStream } from "@nyte-ai/ai";
|
|
14
|
+
import { Type } from "typebox";
|
|
15
|
+
import { Value } from "typebox/value";
|
|
16
|
+
var ALLOW = { behavior: "allow" };
|
|
17
|
+
var SKIPPED = "Nyte did not run this tool call.";
|
|
18
|
+
var toolUseId = Type.String({ minLength: 1 });
|
|
19
|
+
var toolInput = Type.Record(Type.String(), Type.Unknown());
|
|
20
|
+
var APPROVAL_HOOK_SECONDS = 7 * 24 * 60 * 60;
|
|
21
|
+
|
|
22
|
+
class Inbox {
|
|
23
|
+
#items = [];
|
|
24
|
+
#waiter;
|
|
25
|
+
#ended = false;
|
|
26
|
+
has(match) {
|
|
27
|
+
return this.#items.some(match);
|
|
28
|
+
}
|
|
29
|
+
push(item) {
|
|
30
|
+
this.#items.push(item);
|
|
31
|
+
this.#wake();
|
|
32
|
+
}
|
|
33
|
+
clear() {
|
|
34
|
+
this.#items.length = 0;
|
|
35
|
+
}
|
|
36
|
+
end() {
|
|
37
|
+
this.#ended = true;
|
|
38
|
+
this.#wake();
|
|
39
|
+
}
|
|
40
|
+
async next() {
|
|
41
|
+
while (this.#items.length === 0 && !this.#ended)
|
|
42
|
+
await new Promise((resolve) => this.#waiter = resolve);
|
|
43
|
+
return this.#items.shift();
|
|
44
|
+
}
|
|
45
|
+
async* [Symbol.asyncIterator]() {
|
|
46
|
+
for (;; ) {
|
|
47
|
+
const item = await this.next();
|
|
48
|
+
if (item === undefined)
|
|
49
|
+
return;
|
|
50
|
+
yield item;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
#wake() {
|
|
54
|
+
const waiter = this.#waiter;
|
|
55
|
+
this.#waiter = undefined;
|
|
56
|
+
waiter?.();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function toolError(text) {
|
|
60
|
+
return { content: [{ type: "text", text }], isError: true };
|
|
61
|
+
}
|
|
62
|
+
function reply(message) {
|
|
63
|
+
return {
|
|
64
|
+
content: message.content.map((block) => block.type === "text" ? { type: "text", text: block.text } : { type: "image", data: block.data, mimeType: block.mimeType }),
|
|
65
|
+
isError: message.isError
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function resultText(content) {
|
|
69
|
+
if (!Array.isArray(content))
|
|
70
|
+
return content ?? "";
|
|
71
|
+
return content.map((block) => block.type === "text" ? block.text : `[${block.type} content]`).join(`
|
|
72
|
+
`);
|
|
73
|
+
}
|
|
74
|
+
function storePath(key) {
|
|
75
|
+
return key.subpath === undefined ? key.sessionId : `${key.sessionId}/${key.subpath}`;
|
|
76
|
+
}
|
|
77
|
+
function memoryStore(session, history) {
|
|
78
|
+
const files = new Map([[session, [...history]]]);
|
|
79
|
+
return {
|
|
80
|
+
append: async (key, entries) => {
|
|
81
|
+
files.set(storePath(key), [...files.get(storePath(key)) ?? [], ...entries]);
|
|
82
|
+
},
|
|
83
|
+
load: async (key) => files.get(storePath(key)) ?? null
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
class ClaudeCodeSession2 {
|
|
88
|
+
key;
|
|
89
|
+
known = [];
|
|
90
|
+
#prompts = new Inbox;
|
|
91
|
+
#turn = new Inbox;
|
|
92
|
+
#results = new Map;
|
|
93
|
+
#questions = new Map;
|
|
94
|
+
#asking = new Set;
|
|
95
|
+
#changes = new Set;
|
|
96
|
+
#replies = new Map;
|
|
97
|
+
#gates = new Map;
|
|
98
|
+
#permits = new Map;
|
|
99
|
+
#approve;
|
|
100
|
+
#server;
|
|
101
|
+
#query;
|
|
102
|
+
#tools;
|
|
103
|
+
#order = [];
|
|
104
|
+
#settings;
|
|
105
|
+
#background = [];
|
|
106
|
+
#notes = [];
|
|
107
|
+
#idle = true;
|
|
108
|
+
#held = false;
|
|
109
|
+
#stderr = "";
|
|
110
|
+
#failure;
|
|
111
|
+
constructor(launch) {
|
|
112
|
+
this.key = launch.key;
|
|
113
|
+
this.#settings = { effort: launch.effort, ultracode: launch.ultracode };
|
|
114
|
+
this.#tools = launch.tools;
|
|
115
|
+
this.#approve = launch.approve;
|
|
116
|
+
const session = launch.session;
|
|
117
|
+
this.#server = createSdkMcpServer({ name: NYTE_SERVER2, tools: [] });
|
|
118
|
+
this.#server.instance.server.registerCapabilities({ tools: { listChanged: true } });
|
|
119
|
+
this.#server.instance.server.fallbackRequestHandler = async (request, extra) => {
|
|
120
|
+
if (request.method === "tools/list")
|
|
121
|
+
return {
|
|
122
|
+
tools: this.#tools.map((tool) => ({
|
|
123
|
+
name: tool.name,
|
|
124
|
+
description: tool.description,
|
|
125
|
+
inputSchema: { ...tool.parameters, type: "object" }
|
|
126
|
+
}))
|
|
127
|
+
};
|
|
128
|
+
if (request.method !== "tools/call")
|
|
129
|
+
throw new Error(`Nyte does not serve ${request.method}.`);
|
|
130
|
+
const { _meta: meta } = extra;
|
|
131
|
+
const id = meta?.["claudecode/toolUseId"];
|
|
132
|
+
return Value.Check(toolUseId, id) ? await this.#call(id, extra.signal) : toolError("Claude Code sent no tool use id for this Nyte tool call.");
|
|
133
|
+
};
|
|
134
|
+
const options = {
|
|
135
|
+
model: launch.model,
|
|
136
|
+
cwd: launch.cwd,
|
|
137
|
+
systemPrompt: { type: "preset", preset: "claude_code" },
|
|
138
|
+
includePartialMessages: true,
|
|
139
|
+
mcpServers: { [NYTE_SERVER2]: this.#server },
|
|
140
|
+
permissionMode: "default",
|
|
141
|
+
canUseTool: async (name, input, { signal, toolUseID, agentID }) => {
|
|
142
|
+
if (name.startsWith(NYTE_TOOL_PREFIX2))
|
|
143
|
+
return agentID === undefined ? ALLOW : { behavior: "deny", message: "Only the main agent can use Nyte's tools." };
|
|
144
|
+
if (name === "AskUserQuestion") {
|
|
145
|
+
if (agentID !== undefined)
|
|
146
|
+
return { behavior: "deny", message: "Only the main agent can ask the user questions." };
|
|
147
|
+
const answers = await this.#question(toolUseID, signal);
|
|
148
|
+
return answers === null ? { behavior: "deny", message: "The user did not answer." } : { behavior: "allow", updatedInput: { ...input, answers } };
|
|
149
|
+
}
|
|
150
|
+
return await this.#permit(toolUseID, name, input, agentID === undefined, signal);
|
|
151
|
+
},
|
|
152
|
+
hooks: {
|
|
153
|
+
PreToolUse: [
|
|
154
|
+
{
|
|
155
|
+
hooks: [(input, _id, { signal }) => this.#beforeTool(input, signal)],
|
|
156
|
+
timeout: APPROVAL_HOOK_SECONDS
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
},
|
|
160
|
+
thinking: launch.thinking ? { type: "adaptive", display: "summarized" } : { type: "disabled" },
|
|
161
|
+
env: {
|
|
162
|
+
...process.env,
|
|
163
|
+
...launch.env,
|
|
164
|
+
CLAUDE_AGENT_SDK_CLIENT_APP: "nyte",
|
|
165
|
+
CLAUDE_CODE_EMIT_SESSION_STATE_EVENTS: "1"
|
|
166
|
+
},
|
|
167
|
+
stderr: (chunk) => this.#stderr = (this.#stderr + chunk).slice(-4096)
|
|
168
|
+
};
|
|
169
|
+
if (launch.effort !== undefined)
|
|
170
|
+
options.effort = launch.effort;
|
|
171
|
+
if (launch.ultracode)
|
|
172
|
+
options.settings = { ultracode: true, enableWorkflows: true };
|
|
173
|
+
if (launch.history.length > 0) {
|
|
174
|
+
options.resume = session;
|
|
175
|
+
options.sessionStore = memoryStore(session, launch.history);
|
|
176
|
+
}
|
|
177
|
+
this.#query = launch.query({ prompt: this.#prompts, options });
|
|
178
|
+
this.#pump();
|
|
179
|
+
}
|
|
180
|
+
get closed() {
|
|
181
|
+
return this.#failure !== undefined;
|
|
182
|
+
}
|
|
183
|
+
async configure(effort, ultracode) {
|
|
184
|
+
if (this.#settings.effort === effort && this.#settings.ultracode === ultracode)
|
|
185
|
+
return;
|
|
186
|
+
this.#settings = { effort, ultracode };
|
|
187
|
+
await this.#query.applyFlagSettings({
|
|
188
|
+
effortLevel: effort ?? null,
|
|
189
|
+
ultracode: ultracode ? true : null,
|
|
190
|
+
enableWorkflows: ultracode ? true : null
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
expose(tools) {
|
|
194
|
+
if (JSON.stringify(tools) === JSON.stringify(this.#tools))
|
|
195
|
+
return;
|
|
196
|
+
this.#tools = tools;
|
|
197
|
+
this.#server.instance.sendToolListChanged();
|
|
198
|
+
}
|
|
199
|
+
deliver(messages) {
|
|
200
|
+
for (const message of messages) {
|
|
201
|
+
if (message.role !== "toolResult")
|
|
202
|
+
continue;
|
|
203
|
+
const id = message.toolCallId;
|
|
204
|
+
if (this.#replies.has(id))
|
|
205
|
+
this.#reply(id).resolve(reply(message));
|
|
206
|
+
else
|
|
207
|
+
this.#gates.get(id)?.resolve({
|
|
208
|
+
behavior: "deny",
|
|
209
|
+
message: message.content.flatMap((block) => block.type === "text" ? [block.text] : []).join(`
|
|
210
|
+
`) || SKIPPED
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
recording(id) {
|
|
215
|
+
const position = this.#order.findIndex((known) => known === id);
|
|
216
|
+
for (const [index, earlier] of this.#order.entries()) {
|
|
217
|
+
if (index > position)
|
|
218
|
+
break;
|
|
219
|
+
this.#gate(earlier).resolve(index === position ? ALLOW : { behavior: "deny", message: SKIPPED });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
send(content) {
|
|
223
|
+
if (this.#idle)
|
|
224
|
+
this.#turn.clear();
|
|
225
|
+
this.#idle = false;
|
|
226
|
+
this.#held = false;
|
|
227
|
+
this.#prompts.push({
|
|
228
|
+
type: "user",
|
|
229
|
+
message: { role: "user", content: [...content] },
|
|
230
|
+
parent_tool_use_id: null,
|
|
231
|
+
origin: { kind: "human" }
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
respond(model, signal) {
|
|
235
|
+
const stream = createAssistantMessageEventStream();
|
|
236
|
+
const output = emptyMessage3(model);
|
|
237
|
+
const translator = new Translator3(model, output, stream);
|
|
238
|
+
const abort = () => this.close(new Error("Request was aborted"));
|
|
239
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
240
|
+
(async () => {
|
|
241
|
+
try {
|
|
242
|
+
stream.push({ type: "start", partial: output });
|
|
243
|
+
let failure;
|
|
244
|
+
for (;; ) {
|
|
245
|
+
const item = await this.#turn.next();
|
|
246
|
+
if (item === undefined)
|
|
247
|
+
throw this.#failure ?? new Error("Claude Code exited.");
|
|
248
|
+
if (item.type === "system") {
|
|
249
|
+
if (item.state !== "idle" || !this.#held)
|
|
250
|
+
continue;
|
|
251
|
+
this.#held = false;
|
|
252
|
+
this.#idle = true;
|
|
253
|
+
if (output.stopReason === "pending")
|
|
254
|
+
output.stopReason = "stop";
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
if (item.type === "result" && this.#held)
|
|
258
|
+
continue;
|
|
259
|
+
if (item.type === "assistant") {
|
|
260
|
+
if (item.error !== undefined)
|
|
261
|
+
failure = item.message.content.flatMap((block) => block.type === "text" ? [block.text] : []).join(`
|
|
262
|
+
`);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (item.type === "result") {
|
|
266
|
+
if (item.subtype !== "success")
|
|
267
|
+
throw new Error(failure ?? (item.errors.join(`
|
|
268
|
+
`) || `Claude Code failed: ${item.subtype}`));
|
|
269
|
+
if (item.is_error)
|
|
270
|
+
throw new Error(failure ?? item.result);
|
|
271
|
+
this.#idle = true;
|
|
272
|
+
if (output.stopReason === "pending")
|
|
273
|
+
output.stopReason = "stop";
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
this.#held = false;
|
|
277
|
+
const stop = translator.apply(item.event);
|
|
278
|
+
if (stop === "tool_use") {
|
|
279
|
+
if (output.content.some((block) => block.type === "toolCall"))
|
|
280
|
+
break;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
if (stop !== undefined && this.#background.length > 0) {
|
|
284
|
+
translator.append(mirrorCall2(`claude-code-background-${randomUUID()}`, BACKGROUND_TOOL2, {
|
|
285
|
+
tasks: [...this.#background]
|
|
286
|
+
}));
|
|
287
|
+
output.stopReason = "toolUse";
|
|
288
|
+
this.#held = true;
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (output.stopReason === "error")
|
|
293
|
+
throw new Error(output.errorMessage ?? "Claude Code failed.");
|
|
294
|
+
if (output.stopReason === "aborted" || output.stopReason === "pending")
|
|
295
|
+
throw new Error("Claude Code ended without a response.");
|
|
296
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
297
|
+
} catch (error) {
|
|
298
|
+
output.stopReason = signal?.aborted === true ? "aborted" : "error";
|
|
299
|
+
output.errorMessage = error instanceof Error ? error.message : String(error);
|
|
300
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
301
|
+
this.close(error instanceof Error ? error : new Error(String(error)));
|
|
302
|
+
} finally {
|
|
303
|
+
signal?.removeEventListener("abort", abort);
|
|
304
|
+
stream.end();
|
|
305
|
+
}
|
|
306
|
+
})();
|
|
307
|
+
return stream;
|
|
308
|
+
}
|
|
309
|
+
get asking() {
|
|
310
|
+
return this.#asking.size > 0;
|
|
311
|
+
}
|
|
312
|
+
answer(id, answers) {
|
|
313
|
+
this.#pending(id).resolve(answers);
|
|
314
|
+
}
|
|
315
|
+
async result(id, signal) {
|
|
316
|
+
await this.#until(() => this.#results.has(id), signal);
|
|
317
|
+
const output = this.#results.get(id);
|
|
318
|
+
if (output === undefined)
|
|
319
|
+
throw this.#failure ?? new Error("Claude Code is no longer running this turn.");
|
|
320
|
+
return output;
|
|
321
|
+
}
|
|
322
|
+
async background(signal) {
|
|
323
|
+
await this.#until(() => this.#turn.has((item) => item.type === "stream_event" || item.type === "system"), signal);
|
|
324
|
+
if (this.#failure !== undefined)
|
|
325
|
+
throw this.#failure;
|
|
326
|
+
const notes = this.#notes.splice(0);
|
|
327
|
+
return notes.length === 0 ? "Background work finished." : notes.join(`
|
|
328
|
+
|
|
329
|
+
`);
|
|
330
|
+
}
|
|
331
|
+
close(reason = new Error("Claude Code closed.")) {
|
|
332
|
+
if (this.#failure !== undefined)
|
|
333
|
+
return;
|
|
334
|
+
this.#failure = reason;
|
|
335
|
+
for (const pending of this.#questions.values())
|
|
336
|
+
pending.resolve(null);
|
|
337
|
+
for (const pending of this.#replies.values())
|
|
338
|
+
pending.resolve(toolError(reason.message));
|
|
339
|
+
for (const pending of this.#gates.values())
|
|
340
|
+
pending.resolve({ behavior: "deny", message: reason.message });
|
|
341
|
+
this.#prompts.end();
|
|
342
|
+
this.#turn.end();
|
|
343
|
+
this.#query.close();
|
|
344
|
+
this.#changed();
|
|
345
|
+
}
|
|
346
|
+
#pending(id) {
|
|
347
|
+
const known = this.#questions.get(id);
|
|
348
|
+
if (known !== undefined)
|
|
349
|
+
return known;
|
|
350
|
+
const created = Promise.withResolvers();
|
|
351
|
+
this.#questions.set(id, created);
|
|
352
|
+
return created;
|
|
353
|
+
}
|
|
354
|
+
async#question(id, signal) {
|
|
355
|
+
const pending = this.#pending(id);
|
|
356
|
+
const cancel = () => this.answer(id, null);
|
|
357
|
+
this.#asking.add(id);
|
|
358
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
359
|
+
try {
|
|
360
|
+
return await pending.promise;
|
|
361
|
+
} finally {
|
|
362
|
+
this.#asking.delete(id);
|
|
363
|
+
signal.removeEventListener("abort", cancel);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
#reply(id) {
|
|
367
|
+
const known = this.#replies.get(id);
|
|
368
|
+
if (known !== undefined)
|
|
369
|
+
return known;
|
|
370
|
+
const created = Promise.withResolvers();
|
|
371
|
+
this.#replies.set(id, created);
|
|
372
|
+
return created;
|
|
373
|
+
}
|
|
374
|
+
#gate(id) {
|
|
375
|
+
const known = this.#gates.get(id);
|
|
376
|
+
if (known !== undefined)
|
|
377
|
+
return known;
|
|
378
|
+
const created = Promise.withResolvers();
|
|
379
|
+
this.#gates.set(id, created);
|
|
380
|
+
return created;
|
|
381
|
+
}
|
|
382
|
+
async#call(id, signal) {
|
|
383
|
+
const pending = this.#reply(id);
|
|
384
|
+
const cancel = () => pending.resolve(toolError("Request was aborted"));
|
|
385
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
386
|
+
try {
|
|
387
|
+
return await pending.promise;
|
|
388
|
+
} finally {
|
|
389
|
+
this.#replies.delete(id);
|
|
390
|
+
signal.removeEventListener("abort", cancel);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
#permit(id, name, input, main, signal) {
|
|
394
|
+
const known = this.#permits.get(id);
|
|
395
|
+
if (known !== undefined)
|
|
396
|
+
return known;
|
|
397
|
+
const decided = (async () => {
|
|
398
|
+
if (main) {
|
|
399
|
+
const recorded = await this.#recorded(id, signal);
|
|
400
|
+
if (recorded.behavior === "deny")
|
|
401
|
+
return recorded;
|
|
402
|
+
}
|
|
403
|
+
return this.#approve(name, input, signal);
|
|
404
|
+
})();
|
|
405
|
+
this.#permits.set(id, decided);
|
|
406
|
+
return decided;
|
|
407
|
+
}
|
|
408
|
+
async#beforeTool(input, signal) {
|
|
409
|
+
if (input.hook_event_name !== "PreToolUse" || input.tool_name.startsWith(NYTE_TOOL_PREFIX2) || input.tool_name === "AskUserQuestion")
|
|
410
|
+
return {};
|
|
411
|
+
const decided = await this.#permit(input.tool_use_id, input.tool_name, Value.Check(toolInput, input.tool_input) ? input.tool_input : {}, input.agent_id === undefined, signal);
|
|
412
|
+
return decided.behavior === "allow" ? {} : {
|
|
413
|
+
hookSpecificOutput: {
|
|
414
|
+
hookEventName: "PreToolUse",
|
|
415
|
+
permissionDecision: "deny",
|
|
416
|
+
permissionDecisionReason: decided.message
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
async#recorded(id, signal) {
|
|
421
|
+
const pending = this.#gate(id);
|
|
422
|
+
const cancel = () => pending.resolve({ behavior: "deny", message: "Request was aborted" });
|
|
423
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
424
|
+
try {
|
|
425
|
+
return await pending.promise;
|
|
426
|
+
} finally {
|
|
427
|
+
signal.removeEventListener("abort", cancel);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async#until(ready, signal) {
|
|
431
|
+
while (!ready() && this.#failure === undefined) {
|
|
432
|
+
signal.throwIfAborted();
|
|
433
|
+
await new Promise((resolve) => {
|
|
434
|
+
const done = () => {
|
|
435
|
+
this.#changes.delete(done);
|
|
436
|
+
signal.removeEventListener("abort", done);
|
|
437
|
+
resolve();
|
|
438
|
+
};
|
|
439
|
+
this.#changes.add(done);
|
|
440
|
+
signal.addEventListener("abort", done, { once: true });
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
if (signal.aborted) {
|
|
444
|
+
this.close(new Error("Request was aborted"));
|
|
445
|
+
signal.throwIfAborted();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
async#pump() {
|
|
449
|
+
try {
|
|
450
|
+
for await (const message of this.#query)
|
|
451
|
+
this.#route(message);
|
|
452
|
+
this.close(new Error(`Claude Code exited.${this.#detail()}`));
|
|
453
|
+
} catch (error) {
|
|
454
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
455
|
+
this.close(new Error(`${message}${this.#detail()}`, { cause: error }));
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
#detail() {
|
|
459
|
+
const tail = this.#stderr.trim().slice(-500);
|
|
460
|
+
return tail === "" ? "" : ` (${tail})`;
|
|
461
|
+
}
|
|
462
|
+
#route(message) {
|
|
463
|
+
switch (message.type) {
|
|
464
|
+
case "stream_event":
|
|
465
|
+
if (message.parent_tool_use_id !== null)
|
|
466
|
+
break;
|
|
467
|
+
this.#track(message.event);
|
|
468
|
+
this.#turn.push(message);
|
|
469
|
+
break;
|
|
470
|
+
case "assistant":
|
|
471
|
+
if (message.parent_tool_use_id === null)
|
|
472
|
+
this.#turn.push(message);
|
|
473
|
+
break;
|
|
474
|
+
case "result":
|
|
475
|
+
this.#turn.push(message);
|
|
476
|
+
break;
|
|
477
|
+
case "user": {
|
|
478
|
+
const content = message.message.content;
|
|
479
|
+
if (message.parent_tool_use_id !== null || !Array.isArray(content))
|
|
480
|
+
break;
|
|
481
|
+
for (const block of content)
|
|
482
|
+
if (block.type === "tool_result")
|
|
483
|
+
this.#results.set(block.tool_use_id, {
|
|
484
|
+
text: resultText(block.content),
|
|
485
|
+
isError: block.is_error === true
|
|
486
|
+
});
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
case "system":
|
|
490
|
+
if (message.subtype === "background_tasks_changed")
|
|
491
|
+
this.#background = message.tasks.flatMap((task) => task.ambient === true ? [] : [task.description]);
|
|
492
|
+
else if (message.subtype === "task_notification")
|
|
493
|
+
this.#notes.push(message.summary);
|
|
494
|
+
else if (message.subtype === "session_state_changed" && message.state === "idle")
|
|
495
|
+
this.#turn.push(message);
|
|
496
|
+
break;
|
|
497
|
+
default:
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
this.#changed();
|
|
501
|
+
}
|
|
502
|
+
#track(event) {
|
|
503
|
+
if (event.type === "message_start") {
|
|
504
|
+
for (const id of this.#order) {
|
|
505
|
+
this.#gates.delete(id);
|
|
506
|
+
this.#permits.delete(id);
|
|
507
|
+
}
|
|
508
|
+
this.#order = [];
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (event.type !== "content_block_start" || event.content_block.type !== "tool_use")
|
|
512
|
+
return;
|
|
513
|
+
const id = event.content_block.id;
|
|
514
|
+
if (event.content_block.name.startsWith(NYTE_TOOL_PREFIX2)) {
|
|
515
|
+
this.#reply(id);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
this.#order.push(id);
|
|
519
|
+
this.#gate(id);
|
|
520
|
+
}
|
|
521
|
+
#changed() {
|
|
522
|
+
for (const change of this.#changes)
|
|
523
|
+
change();
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export { ClaudeCodeSession2 };
|