@pontia/claude-client 0.8.4 → 0.8.6
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/compaction.js +100 -0
- package/dist/src/host.js +31 -3
- package/dist/src/server.js +5 -0
- package/package.json +1 -1
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
export class Compaction {
|
|
3
|
+
emit;
|
|
4
|
+
active;
|
|
5
|
+
completed = new Set();
|
|
6
|
+
constructor(emit) {
|
|
7
|
+
this.emit = emit;
|
|
8
|
+
}
|
|
9
|
+
get busy() {
|
|
10
|
+
return this.active !== undefined;
|
|
11
|
+
}
|
|
12
|
+
request(operationId) {
|
|
13
|
+
const input = randomUUID();
|
|
14
|
+
this.active = { id: operationId, trigger: "manual", input, started: false };
|
|
15
|
+
return input;
|
|
16
|
+
}
|
|
17
|
+
accept(message) {
|
|
18
|
+
if ("parent_tool_use_id" in message && message.parent_tool_use_id)
|
|
19
|
+
return false;
|
|
20
|
+
if (message.type === "system" && message.subtype === "status") {
|
|
21
|
+
if (message.status === "compacting")
|
|
22
|
+
this.start();
|
|
23
|
+
if (message.compact_result === "failed" && this.active) {
|
|
24
|
+
this.fail(false, "Claude context compaction failed.");
|
|
25
|
+
if (!this.active.input)
|
|
26
|
+
this.finish();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (message.type === "system" && message.subtype === "compact_boundary") {
|
|
30
|
+
if (this.completed.has(message.uuid))
|
|
31
|
+
return true;
|
|
32
|
+
this.completed.add(message.uuid);
|
|
33
|
+
const operation = this.start();
|
|
34
|
+
operation.terminal = this.fact("completed", {
|
|
35
|
+
native_compaction_id: message.uuid,
|
|
36
|
+
tokens_before: tokens(message.compact_metadata.pre_tokens),
|
|
37
|
+
tokens_after: tokens(message.compact_metadata.post_tokens),
|
|
38
|
+
});
|
|
39
|
+
if (!operation.input)
|
|
40
|
+
this.finish();
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (!this.active?.input)
|
|
44
|
+
return false;
|
|
45
|
+
// /compact produces local user frames and a model-less result, not a Pontia Turn.
|
|
46
|
+
if (message.type === "result") {
|
|
47
|
+
const ids = message.user_message_uuids ?? [message.user_message_uuid];
|
|
48
|
+
if (!ids.includes(this.active.input) ||
|
|
49
|
+
ids.some((id) => id !== this.active.input) ||
|
|
50
|
+
(message.user_message_uuid && message.user_message_uuid !== this.active.input))
|
|
51
|
+
throw new Error("compaction_result_unassociated");
|
|
52
|
+
if (!this.active.terminal) {
|
|
53
|
+
const aborted = message.terminal_reason === "aborted_streaming" ||
|
|
54
|
+
message.terminal_reason === "aborted_tools";
|
|
55
|
+
this.fail(aborted, message.is_error
|
|
56
|
+
? "Claude context compaction failed."
|
|
57
|
+
: "Claude did not compact the context; there may be too few messages to summarize.");
|
|
58
|
+
}
|
|
59
|
+
this.finish();
|
|
60
|
+
}
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
closed(aborted) {
|
|
64
|
+
if (!this.active)
|
|
65
|
+
return;
|
|
66
|
+
this.fail(aborted, "Claude stopped before context compaction finished.");
|
|
67
|
+
this.finish();
|
|
68
|
+
}
|
|
69
|
+
start() {
|
|
70
|
+
this.active ??= { id: `compact_${randomUUID()}`, trigger: "unknown", started: false };
|
|
71
|
+
if (!this.active.started) {
|
|
72
|
+
this.active.started = true;
|
|
73
|
+
this.emit(this.fact("started"));
|
|
74
|
+
}
|
|
75
|
+
return this.active;
|
|
76
|
+
}
|
|
77
|
+
fact(phase, metadata = {}) {
|
|
78
|
+
return {
|
|
79
|
+
kind: "compaction",
|
|
80
|
+
phase,
|
|
81
|
+
data: {
|
|
82
|
+
operation_id: this.active.id,
|
|
83
|
+
trigger: this.active.trigger,
|
|
84
|
+
...metadata,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
fail(aborted, error) {
|
|
89
|
+
const operation = this.start();
|
|
90
|
+
operation.terminal ??= this.fact("failed", { aborted, error_message: error });
|
|
91
|
+
}
|
|
92
|
+
finish() {
|
|
93
|
+
const terminal = this.active.terminal;
|
|
94
|
+
this.active = undefined;
|
|
95
|
+
this.emit(terminal);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function tokens(value) {
|
|
99
|
+
return value !== undefined && Number.isSafeInteger(value) && value >= 0 ? value : null;
|
|
100
|
+
}
|
package/dist/src/host.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { query } from "@anthropic-ai/claude-agent-sdk";
|
|
2
|
+
import { Compaction } from "./compaction.js";
|
|
2
3
|
import { Input } from "./input.js";
|
|
3
4
|
import { Permissions } from "./permissions.js";
|
|
4
5
|
import { FactError, InputFacts } from "./facts.js";
|
|
@@ -16,6 +17,7 @@ export class Host {
|
|
|
16
17
|
emit;
|
|
17
18
|
createQuery;
|
|
18
19
|
input = new Input();
|
|
20
|
+
compaction;
|
|
19
21
|
permissions;
|
|
20
22
|
execution;
|
|
21
23
|
reader;
|
|
@@ -34,6 +36,7 @@ export class Host {
|
|
|
34
36
|
this.emit = emit;
|
|
35
37
|
this.createQuery = createQuery;
|
|
36
38
|
this.permissions = new Permissions(emit);
|
|
39
|
+
this.compaction = new Compaction(emit);
|
|
37
40
|
}
|
|
38
41
|
status() {
|
|
39
42
|
return {
|
|
@@ -49,13 +52,13 @@ export class Host {
|
|
|
49
52
|
isIdle() {
|
|
50
53
|
if (!this.acceptsControl())
|
|
51
54
|
throw new ControlError("rejected", "execution_unavailable");
|
|
52
|
-
return !this.current || this.current.finished;
|
|
55
|
+
return !this.compaction.busy && (!this.current || this.current.finished);
|
|
53
56
|
}
|
|
54
57
|
submit(id, text) {
|
|
55
58
|
if (!this.acceptsControl())
|
|
56
59
|
throw new ControlError("rejected", "execution_unavailable");
|
|
57
60
|
this.validateInput(id, text);
|
|
58
|
-
if (
|
|
61
|
+
if (!this.isIdle())
|
|
59
62
|
throw new ControlError("rejected", "input_busy");
|
|
60
63
|
this.usedInputs.add(id);
|
|
61
64
|
this.current = new InputFacts(id, this.emit);
|
|
@@ -89,6 +92,7 @@ export class Host {
|
|
|
89
92
|
: {}),
|
|
90
93
|
...(tools ? { tools } : {}),
|
|
91
94
|
...(policy?.disable_mcp ? { strictMcpConfig: true, mcpServers: {} } : {}),
|
|
95
|
+
...(policy?.disable_skills ? { extraArgs: { "disable-slash-commands": null } } : {}),
|
|
92
96
|
settingSources: ["user", "project", "local"],
|
|
93
97
|
settings: { env: { PONTIA_SESSION_ID: this.options.session_id } },
|
|
94
98
|
disallowedTools: [
|
|
@@ -117,6 +121,20 @@ export class Host {
|
|
|
117
121
|
}
|
|
118
122
|
}
|
|
119
123
|
}
|
|
124
|
+
compact(operationId) {
|
|
125
|
+
if (!this.acceptsControl() || !this.execution || !this.native)
|
|
126
|
+
throw new ControlError("rejected", "execution_unavailable");
|
|
127
|
+
if (!this.isIdle())
|
|
128
|
+
throw new ControlError("rejected", "input_busy");
|
|
129
|
+
const id = this.compaction.request(operationId);
|
|
130
|
+
this.input.push({
|
|
131
|
+
type: "user",
|
|
132
|
+
uuid: id,
|
|
133
|
+
session_id: this.native,
|
|
134
|
+
parent_tool_use_id: null,
|
|
135
|
+
message: { role: "user", content: "/compact" },
|
|
136
|
+
});
|
|
137
|
+
}
|
|
120
138
|
steer(target, id, text) {
|
|
121
139
|
if (!this.acceptsControl())
|
|
122
140
|
throw new ControlError("rejected", "execution_unavailable");
|
|
@@ -200,6 +218,7 @@ export class Host {
|
|
|
200
218
|
if (this.unavailable)
|
|
201
219
|
return;
|
|
202
220
|
this.unavailable = reason;
|
|
221
|
+
this.compaction.closed(false);
|
|
203
222
|
this.permissions.expire();
|
|
204
223
|
this.emit({ kind: "unavailable", reason, ...(result ? { result } : {}) });
|
|
205
224
|
}
|
|
@@ -222,6 +241,7 @@ export class Host {
|
|
|
222
241
|
}
|
|
223
242
|
}
|
|
224
243
|
if (this.closing) {
|
|
244
|
+
this.compaction.closed(true);
|
|
225
245
|
this.closed = true;
|
|
226
246
|
this.emit({ kind: "closed", history_anchor: this.current?.historyAnchor });
|
|
227
247
|
}
|
|
@@ -296,6 +316,13 @@ export class Host {
|
|
|
296
316
|
});
|
|
297
317
|
return;
|
|
298
318
|
}
|
|
319
|
+
if (message.type === "system" &&
|
|
320
|
+
(message.subtype === "status" || message.subtype === "compact_boundary")) {
|
|
321
|
+
if (!this.native || message.session_id !== this.native)
|
|
322
|
+
throw new Error("native_identity_mismatch");
|
|
323
|
+
this.compaction.accept(message);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
299
326
|
if (["stream_event", "assistant", "result", "user"].includes(message.type)) {
|
|
300
327
|
if (!this.native || message.session_id !== this.native)
|
|
301
328
|
throw new Error("native_identity_mismatch");
|
|
@@ -303,7 +330,8 @@ export class Host {
|
|
|
303
330
|
this.retrying = false;
|
|
304
331
|
this.emit({ kind: "retry", reason: null });
|
|
305
332
|
}
|
|
306
|
-
this.
|
|
333
|
+
if (!this.compaction.accept(message))
|
|
334
|
+
this.current?.accept(message);
|
|
307
335
|
}
|
|
308
336
|
}
|
|
309
337
|
}
|
package/dist/src/server.js
CHANGED
|
@@ -34,6 +34,7 @@ export function bootstrap(value) {
|
|
|
34
34
|
const policy = object(data.execution_policy);
|
|
35
35
|
if (typeof policy.yolo !== "boolean" ||
|
|
36
36
|
typeof policy.disable_mcp !== "boolean" ||
|
|
37
|
+
typeof policy.disable_skills !== "boolean" ||
|
|
37
38
|
!Array.isArray(policy.disabled_tools) ||
|
|
38
39
|
policy.disabled_tools.some((tool) => typeof tool !== "string" || !tool.trim()) ||
|
|
39
40
|
(policy.tools !== null &&
|
|
@@ -44,6 +45,7 @@ export function bootstrap(value) {
|
|
|
44
45
|
tools: policy.tools,
|
|
45
46
|
disabled_tools: policy.disabled_tools,
|
|
46
47
|
disable_mcp: policy.disable_mcp,
|
|
48
|
+
disable_skills: policy.disable_skills,
|
|
47
49
|
yolo: policy.yolo,
|
|
48
50
|
};
|
|
49
51
|
}
|
|
@@ -93,6 +95,9 @@ export async function serve(config, factory) {
|
|
|
93
95
|
case "steer":
|
|
94
96
|
host.steer(string(request.target_input_id), string(request.input_id), string(request.text));
|
|
95
97
|
break;
|
|
98
|
+
case "compact":
|
|
99
|
+
host.compact(string(request.operation_id));
|
|
100
|
+
break;
|
|
96
101
|
case "interrupt":
|
|
97
102
|
await host.interrupt(string(request.input_id));
|
|
98
103
|
break;
|