@pontia/claude-client 0.7.16 → 0.8.1
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/dist/src/host.js +62 -30
- package/dist/src/main.js +6 -1
- package/dist/src/models.js +28 -0
- package/dist/src/server.js +20 -5
- package/package.json +1 -1
package/dist/src/host.js
CHANGED
|
@@ -23,11 +23,12 @@ export class Host {
|
|
|
23
23
|
native;
|
|
24
24
|
unavailable;
|
|
25
25
|
retrying = false;
|
|
26
|
+
usagePending = false;
|
|
27
|
+
usageReading = false;
|
|
26
28
|
controlConnected = true;
|
|
27
29
|
closing = false;
|
|
28
30
|
closed = false;
|
|
29
31
|
usedInputs = new Set();
|
|
30
|
-
changingModel = false;
|
|
31
32
|
constructor(options, emit, createQuery = query) {
|
|
32
33
|
this.options = options;
|
|
33
34
|
this.emit = emit;
|
|
@@ -45,6 +46,11 @@ export class Host {
|
|
|
45
46
|
closed: this.closed,
|
|
46
47
|
};
|
|
47
48
|
}
|
|
49
|
+
isIdle() {
|
|
50
|
+
if (!this.acceptsControl())
|
|
51
|
+
throw new ControlError("rejected", "execution_unavailable");
|
|
52
|
+
return !this.current || this.current.finished;
|
|
53
|
+
}
|
|
48
54
|
submit(id, text) {
|
|
49
55
|
if (!this.acceptsControl())
|
|
50
56
|
throw new ControlError("rejected", "execution_unavailable");
|
|
@@ -61,8 +67,11 @@ export class Host {
|
|
|
61
67
|
message: { role: "user", content: text },
|
|
62
68
|
});
|
|
63
69
|
if (!this.execution) {
|
|
70
|
+
const policy = this.options.execution_policy;
|
|
71
|
+
const tools = policy?.tools;
|
|
64
72
|
const options = {
|
|
65
73
|
cwd: this.options.cwd,
|
|
74
|
+
...(this.options.model ? { model: this.options.model } : {}),
|
|
66
75
|
...(this.options.resume ? { resume: this.options.resume } : {}),
|
|
67
76
|
...(this.options.system_prompt
|
|
68
77
|
? {
|
|
@@ -74,13 +83,19 @@ export class Host {
|
|
|
74
83
|
}
|
|
75
84
|
: {}),
|
|
76
85
|
includePartialMessages: true,
|
|
77
|
-
permissionMode: "default",
|
|
86
|
+
permissionMode: policy?.yolo ? "bypassPermissions" : "default",
|
|
87
|
+
...(policy?.yolo ? { allowDangerouslySkipPermissions: true, sandbox: { enabled: false } } : {}),
|
|
88
|
+
...(tools ? { tools } : {}),
|
|
89
|
+
...(policy?.disable_mcp ? { strictMcpConfig: true, mcpServers: {} } : {}),
|
|
78
90
|
settingSources: ["user", "project", "local"],
|
|
79
91
|
settings: { env: { PONTIA_SESSION_ID: this.options.session_id } },
|
|
80
|
-
disallowedTools: ["AskUserQuestion"],
|
|
92
|
+
disallowedTools: ["AskUserQuestion", ...(policy?.disabled_tools ?? []),
|
|
93
|
+
...(policy?.disable_mcp ? ["mcp__*"] : [])],
|
|
81
94
|
canUseTool: (tool, input, context) => {
|
|
82
95
|
if (!this.acceptsControl())
|
|
83
96
|
return Promise.resolve({ behavior: "deny", message: "Tool approval is unavailable." });
|
|
97
|
+
if (policy?.yolo)
|
|
98
|
+
return Promise.resolve({ behavior: "allow", updatedInput: input });
|
|
84
99
|
return this.permissions.request(tool, input, context);
|
|
85
100
|
},
|
|
86
101
|
onElicitation: async () => ({ action: "decline" }),
|
|
@@ -122,33 +137,6 @@ export class Host {
|
|
|
122
137
|
if (this.usedInputs.has(id))
|
|
123
138
|
throw new ControlError("rejected", "input_already_submitted");
|
|
124
139
|
}
|
|
125
|
-
async listModels() {
|
|
126
|
-
if (!this.acceptsControl() || !this.execution || !this.native)
|
|
127
|
-
throw new ControlError("rejected", "execution_unavailable");
|
|
128
|
-
return (await this.execution.supportedModels()).map((model) => ({
|
|
129
|
-
id: model.value,
|
|
130
|
-
name: model.displayName,
|
|
131
|
-
description: model.description,
|
|
132
|
-
}));
|
|
133
|
-
}
|
|
134
|
-
async setModel(model) {
|
|
135
|
-
if (this.changingModel)
|
|
136
|
-
throw new ControlError("rejected", "model_change_busy");
|
|
137
|
-
this.changingModel = true;
|
|
138
|
-
try {
|
|
139
|
-
if (!(await this.listModels()).some((entry) => entry.id === model))
|
|
140
|
-
throw new ControlError("rejected", "model_unavailable");
|
|
141
|
-
if (!this.acceptsControl())
|
|
142
|
-
throw new ControlError("rejected", "execution_unavailable");
|
|
143
|
-
await this.execution.setModel(model);
|
|
144
|
-
if (!this.acceptsControl())
|
|
145
|
-
throw new ControlError("unknown", "model_change_unconfirmed");
|
|
146
|
-
this.emit({ kind: "model", model });
|
|
147
|
-
}
|
|
148
|
-
finally {
|
|
149
|
-
this.changingModel = false;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
140
|
async interrupt(id) {
|
|
153
141
|
if (this.closed ||
|
|
154
142
|
this.closing ||
|
|
@@ -215,6 +203,14 @@ export class Host {
|
|
|
215
203
|
for await (const message of execution) {
|
|
216
204
|
try {
|
|
217
205
|
this.message(message);
|
|
206
|
+
if (!this.unavailable &&
|
|
207
|
+
message.session_id === this.native &&
|
|
208
|
+
!("parent_tool_use_id" in message && message.parent_tool_use_id) &&
|
|
209
|
+
(message.type === "assistant" ||
|
|
210
|
+
message.type === "result" ||
|
|
211
|
+
(message.type === "system" &&
|
|
212
|
+
(message.subtype === "init" || message.subtype === "compact_boundary"))))
|
|
213
|
+
void this.refreshContextUsage(execution);
|
|
218
214
|
}
|
|
219
215
|
catch (error) {
|
|
220
216
|
this.fail(error instanceof Error ? error.message : "sdk_message_invalid", error instanceof FactError ? error.result : undefined);
|
|
@@ -232,6 +228,42 @@ export class Host {
|
|
|
232
228
|
this.fail("sdk_reader_failed");
|
|
233
229
|
}
|
|
234
230
|
}
|
|
231
|
+
async refreshContextUsage(execution) {
|
|
232
|
+
this.usagePending = true;
|
|
233
|
+
if (this.usageReading)
|
|
234
|
+
return;
|
|
235
|
+
this.usageReading = true;
|
|
236
|
+
try {
|
|
237
|
+
while (this.usagePending && !this.closing && !this.unavailable) {
|
|
238
|
+
this.usagePending = false;
|
|
239
|
+
const usage = await execution.getContextUsage({ detail: "summary" });
|
|
240
|
+
if (this.closing || this.unavailable)
|
|
241
|
+
return;
|
|
242
|
+
const used = usage.totalTokens;
|
|
243
|
+
const max = usage.rawMaxTokens;
|
|
244
|
+
if (!Number.isSafeInteger(used) || used < 0 || !Number.isSafeInteger(max) || max <= 0)
|
|
245
|
+
continue;
|
|
246
|
+
this.emit({
|
|
247
|
+
kind: "context_usage",
|
|
248
|
+
context_usage: {
|
|
249
|
+
used_tokens: used,
|
|
250
|
+
max_tokens: max,
|
|
251
|
+
remaining_tokens: Math.max(0, max - used),
|
|
252
|
+
usage_ratio: Math.min(1, used / max),
|
|
253
|
+
confidence: "estimated",
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
// Usage observation must not interrupt the task or expose SDK diagnostics.
|
|
260
|
+
if (!this.closing)
|
|
261
|
+
console.error("Claude context usage query failed");
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
this.usageReading = false;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
235
267
|
message(message) {
|
|
236
268
|
if (this.unavailable)
|
|
237
269
|
return;
|
package/dist/src/main.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import { bootstrap, serve } from "./server.js";
|
|
2
2
|
import { readHistory } from "./history.js";
|
|
3
|
+
import { listModels } from "./models.js";
|
|
3
4
|
import { preflight } from "./preflight.js";
|
|
4
5
|
async function main() {
|
|
5
6
|
if (process.argv[2] === "preflight") {
|
|
6
7
|
await preflight();
|
|
7
8
|
return;
|
|
8
9
|
}
|
|
10
|
+
if (process.argv[2] === "models") {
|
|
11
|
+
process.stdout.write(JSON.stringify(await listModels(process.cwd())) + "\n");
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
9
14
|
let input = Buffer.alloc(0);
|
|
10
15
|
// The parent supplies the connection capability privately, never in argv or a log.
|
|
11
16
|
for await (const chunk of process.stdin) {
|
|
@@ -30,7 +35,7 @@ async function main() {
|
|
|
30
35
|
throw new Error("bootstrap_missing");
|
|
31
36
|
}
|
|
32
37
|
main().catch((error) => {
|
|
33
|
-
if (process.argv[2] !== "preflight" && process.argv[2] !== "history") {
|
|
38
|
+
if (process.argv[2] !== "preflight" && process.argv[2] !== "history" && process.argv[2] !== "models") {
|
|
34
39
|
const systemCode = error instanceof Error && "code" in error ? error.code : undefined;
|
|
35
40
|
const code = typeof systemCode === "string" &&
|
|
36
41
|
["EACCES", "EADDRINUSE", "EINVAL", "ENOENT", "ENAMETOOLONG", "ENOTDIR", "EPERM"].includes(systemCode)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
+
import { Input } from "./input.js";
|
|
3
|
+
export async function modelOptions(execution) {
|
|
4
|
+
return (await execution.supportedModels()).map((model) => ({
|
|
5
|
+
id: model.value,
|
|
6
|
+
name: model.displayName,
|
|
7
|
+
description: model.description,
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
10
|
+
export async function listModels(cwd, createQuery = query) {
|
|
11
|
+
const input = new Input();
|
|
12
|
+
const execution = createQuery({
|
|
13
|
+
prompt: input,
|
|
14
|
+
options: {
|
|
15
|
+
cwd,
|
|
16
|
+
persistSession: false,
|
|
17
|
+
settingSources: ["user", "project", "local"],
|
|
18
|
+
stderr: () => { },
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
try {
|
|
22
|
+
return await modelOptions(execution);
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
input.end();
|
|
26
|
+
execution.close();
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/src/server.js
CHANGED
|
@@ -26,8 +26,26 @@ export function bootstrap(value) {
|
|
|
26
26
|
};
|
|
27
27
|
if (data.system_prompt !== undefined)
|
|
28
28
|
result.system_prompt = string(data.system_prompt);
|
|
29
|
+
if (data.model !== undefined)
|
|
30
|
+
result.model = string(data.model);
|
|
29
31
|
if (data.resume !== undefined)
|
|
30
32
|
result.resume = string(data.resume);
|
|
33
|
+
if (data.execution_policy !== undefined) {
|
|
34
|
+
const policy = object(data.execution_policy);
|
|
35
|
+
if (typeof policy.yolo !== "boolean" ||
|
|
36
|
+
typeof policy.disable_mcp !== "boolean" ||
|
|
37
|
+
!Array.isArray(policy.disabled_tools) ||
|
|
38
|
+
policy.disabled_tools.some((tool) => typeof tool !== "string" || !tool.trim()) ||
|
|
39
|
+
(policy.tools !== null &&
|
|
40
|
+
(!Array.isArray(policy.tools) || policy.tools.some((tool) => typeof tool !== "string" || !tool.trim()))))
|
|
41
|
+
throw new ControlError("rejected", "invalid_execution_policy");
|
|
42
|
+
result.execution_policy = {
|
|
43
|
+
tools: policy.tools,
|
|
44
|
+
disabled_tools: policy.disabled_tools,
|
|
45
|
+
disable_mcp: policy.disable_mcp,
|
|
46
|
+
yolo: policy.yolo,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
31
49
|
if (!/^[0-9a-f]{64}$/.test(result.token))
|
|
32
50
|
throw new ControlError("rejected", "invalid_token");
|
|
33
51
|
return result;
|
|
@@ -68,11 +86,6 @@ export async function serve(config, factory) {
|
|
|
68
86
|
const invoke = async (request) => {
|
|
69
87
|
try {
|
|
70
88
|
switch (request.method) {
|
|
71
|
-
case "list_models":
|
|
72
|
-
return { status: "accepted", models: await host.listModels() };
|
|
73
|
-
case "set_model":
|
|
74
|
-
await host.setModel(string(request.model));
|
|
75
|
-
break;
|
|
76
89
|
case "input":
|
|
77
90
|
host.submit(string(request.input_id), string(request.text));
|
|
78
91
|
break;
|
|
@@ -85,6 +98,8 @@ export async function serve(config, factory) {
|
|
|
85
98
|
case "permission":
|
|
86
99
|
host.resolvePermission(string(request.permission_id), string(request.option));
|
|
87
100
|
break;
|
|
101
|
+
case "is_idle":
|
|
102
|
+
return { status: "accepted", idle: host.isIdle() };
|
|
88
103
|
case "close":
|
|
89
104
|
await host.close();
|
|
90
105
|
break;
|