agent-neckbeard 1.1.28 → 1.1.30
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/CHANGELOG.md +12 -0
- package/README.md +28 -30
- package/dist/archil.d.ts +73 -0
- package/dist/archil.js +673 -0
- package/dist/build.cjs +129 -5160
- package/dist/build.d.cts +9 -5
- package/dist/build.d.ts +9 -5
- package/dist/build.js +4 -6
- package/dist/{chunk-JF5YPOBY.js → chunk-FHNQ5DNA.js} +3235 -511
- package/dist/{chunk-M7XNBKG4.js → chunk-I5GIWADK.js} +29 -7
- package/dist/chunk-I5NULOJP.js +129 -0
- package/dist/chunk-PKUGXECR.js +13 -0
- package/dist/chunk-QA4L7ZI2.js +72 -0
- package/dist/{chunk-KG6SJM74.js → chunk-VHBDBB3N.js} +8 -5
- package/dist/cli.cjs +124 -5154
- package/dist/cli.js +7 -8
- package/dist/define-gdmBe9RA.d.ts +227 -0
- package/dist/durable.d.ts +53 -0
- package/dist/durable.js +92 -0
- package/dist/index.cjs +720 -833
- package/dist/index.d.cts +139 -78
- package/dist/index.d.ts +139 -78
- package/dist/index.js +12 -11
- package/dist/inside.d.ts +15 -0
- package/dist/inside.js +530 -0
- package/dist/{invoke-client-P2CPBYTF.js → invoke-client-QB2NTZ3L.js} +1 -2
- package/dist/pi-watch-legacy.d.ts +4503 -0
- package/dist/serve.cjs +9 -309
- package/dist/serve.d.cts +14 -1
- package/dist/serve.d.ts +14 -1
- package/dist/serve.js +3 -5
- package/docs/AGENT.md +95 -59
- package/docs/DAYTONA.md +2 -0
- package/docs/DOCKER.md +0 -1
- package/docs/E2B.md +1 -1
- package/docs/LEGACY.md +1 -8
- package/docs/MODAL.md +1 -1
- package/package.json +57 -23
- package/types/pi-watch.d.cts +68 -0
- package/dist/chunk-33IXN6MM.js +0 -21
- package/dist/chunk-6P5I7ZCH.js +0 -2552
- package/dist/chunk-M3FISP7J.js +0 -377
- package/dist/sandbox-capabilities-CFjnZ_kB.d.cts +0 -77
- package/dist/sandbox-capabilities-CFjnZ_kB.d.ts +0 -77
- package/dist/serve-Dan1HQKN.d.cts +0 -89
- package/dist/serve-Dan1HQKN.d.ts +0 -89
- package/dist/supervisor.cjs +0 -5731
- package/dist/supervisor.d.cts +0 -482
- package/dist/supervisor.d.ts +0 -482
- package/dist/supervisor.js +0 -3187
- package/docs/SUPERVISOR.md +0 -258
package/dist/chunk-M3FISP7J.js
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
// src/serve.ts
|
|
2
|
-
import { createServer } from "http";
|
|
3
|
-
|
|
4
|
-
// src/turn-protocol.ts
|
|
5
|
-
var TURN_PROTOCOL_VERSION = 2;
|
|
6
|
-
var MAX_TURN_REQUEST_BYTES = 4 * 1024 * 1024;
|
|
7
|
-
var MAX_TURN_ID_LENGTH = 200;
|
|
8
|
-
function isRecord(value) {
|
|
9
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
-
}
|
|
11
|
-
function parseTurnRecord(record) {
|
|
12
|
-
if (!isRecord(record)) throw new Error("Turn record must be an object.");
|
|
13
|
-
const { seq } = record;
|
|
14
|
-
if (typeof seq !== "number" || !Number.isSafeInteger(seq) || seq < 0) {
|
|
15
|
-
throw new Error("Turn record seq must be a non-negative integer.");
|
|
16
|
-
}
|
|
17
|
-
if (record.type === "event" && "event" in record) {
|
|
18
|
-
return { seq, type: "event", event: record.event };
|
|
19
|
-
}
|
|
20
|
-
if (record.type === "end" && (record.outcome === "completed" || record.outcome === "aborted" || record.outcome === "failed") && (record.error === void 0 || typeof record.error === "string")) {
|
|
21
|
-
return {
|
|
22
|
-
seq,
|
|
23
|
-
type: "end",
|
|
24
|
-
outcome: record.outcome,
|
|
25
|
-
...record.error === void 0 ? {} : { error: record.error }
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
throw new Error("Turn record has an unknown shape.");
|
|
29
|
-
}
|
|
30
|
-
function parseTurnHealth(value) {
|
|
31
|
-
if (!isRecord(value)) throw new Error("Health response must be an object.");
|
|
32
|
-
const { ok, protocolVersion, busy, turnId } = value;
|
|
33
|
-
if (ok !== true || typeof protocolVersion !== "number" || typeof busy !== "boolean" || turnId !== null && typeof turnId !== "string") {
|
|
34
|
-
throw new Error("Health response is not { ok: true, protocolVersion, busy, turnId }.");
|
|
35
|
-
}
|
|
36
|
-
return { ok, protocolVersion, busy, turnId };
|
|
37
|
-
}
|
|
38
|
-
function readAssistantMessageEnd(event) {
|
|
39
|
-
if (!isRecord(event) || event.type !== "message_end" || !isRecord(event.message)) return void 0;
|
|
40
|
-
const { role, content, stopReason, errorMessage } = event.message;
|
|
41
|
-
if (role !== "assistant") return void 0;
|
|
42
|
-
const text = (Array.isArray(content) ? content : []).flatMap((block) => isRecord(block) && block.type === "text" && typeof block.text === "string" ? [block.text] : []).join("\n").trim();
|
|
43
|
-
return {
|
|
44
|
-
text,
|
|
45
|
-
stopReason: typeof stopReason === "string" ? stopReason : void 0,
|
|
46
|
-
errorMessage: typeof errorMessage === "string" ? errorMessage : void 0
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function getPiTurnResult(records) {
|
|
50
|
-
let end;
|
|
51
|
-
let message;
|
|
52
|
-
for (const record of records) {
|
|
53
|
-
if (record.type === "end") {
|
|
54
|
-
end = record;
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
message = readAssistantMessageEnd(record.event) ?? message;
|
|
58
|
-
}
|
|
59
|
-
if (!end) return { completed: false, error: "The agent turn has not ended." };
|
|
60
|
-
if (end.outcome !== "completed") {
|
|
61
|
-
return { completed: false, error: end.error ?? `The agent turn was ${end.outcome}.` };
|
|
62
|
-
}
|
|
63
|
-
if (!message) return { completed: false, error: "The agent did not reply." };
|
|
64
|
-
if (message.stopReason !== "stop") {
|
|
65
|
-
return {
|
|
66
|
-
completed: false,
|
|
67
|
-
error: message.errorMessage?.trim() || `The agent stopped with ${message.stopReason ?? "no reason"}.`
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
return { completed: true, text: message.text || null };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// src/serve.ts
|
|
74
|
-
var HEARTBEAT_INTERVAL_MS = 15e3;
|
|
75
|
-
var DEFAULT_ABORT_GRACE_MS = 1e4;
|
|
76
|
-
var DEFAULT_PORT = 8080;
|
|
77
|
-
function toPiJsonEvent(event) {
|
|
78
|
-
if (!isRecord(event) || event.type !== "message_update") return event;
|
|
79
|
-
const { message, assistantMessageEvent } = event;
|
|
80
|
-
if (!isRecord(message) || message.role !== "assistant") {
|
|
81
|
-
throw new Error("message_update message is not an assistant message");
|
|
82
|
-
}
|
|
83
|
-
if (!isRecord(assistantMessageEvent) || typeof assistantMessageEvent.type !== "string") {
|
|
84
|
-
throw new Error("message_update has no assistantMessageEvent");
|
|
85
|
-
}
|
|
86
|
-
const { partial, ...delta } = assistantMessageEvent;
|
|
87
|
-
if (assistantMessageEvent.type === "toolcall_start") {
|
|
88
|
-
const { contentIndex } = assistantMessageEvent;
|
|
89
|
-
const content = isRecord(partial) && Array.isArray(partial.content) ? partial.content : [];
|
|
90
|
-
const toolCall = typeof contentIndex === "number" ? content[contentIndex] : void 0;
|
|
91
|
-
if (!isRecord(toolCall) || toolCall.type !== "toolCall" || typeof toolCall.id !== "string" || typeof toolCall.name !== "string") {
|
|
92
|
-
throw new Error(`toolcall_start content at index ${String(contentIndex)} is not a tool call`);
|
|
93
|
-
}
|
|
94
|
-
return {
|
|
95
|
-
type: "message_update",
|
|
96
|
-
usage: message.usage,
|
|
97
|
-
assistantMessageEvent: { ...delta, id: toolCall.id, toolName: toolCall.name }
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
return { type: "message_update", usage: message.usage, assistantMessageEvent: delta };
|
|
101
|
-
}
|
|
102
|
-
var RequestError = class extends Error {
|
|
103
|
-
constructor(status, message) {
|
|
104
|
-
super(message);
|
|
105
|
-
this.status = status;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
function sendJson(response, status, body) {
|
|
109
|
-
response.writeHead(status, { "content-type": "application/json" });
|
|
110
|
-
response.end(JSON.stringify(body));
|
|
111
|
-
}
|
|
112
|
-
async function readJsonBody(request) {
|
|
113
|
-
const chunks = [];
|
|
114
|
-
let size = 0;
|
|
115
|
-
for await (const chunk of request) {
|
|
116
|
-
size += chunk.byteLength;
|
|
117
|
-
if (size > MAX_TURN_REQUEST_BYTES) throw new RequestError(413, "Request body is too large.");
|
|
118
|
-
chunks.push(chunk);
|
|
119
|
-
}
|
|
120
|
-
try {
|
|
121
|
-
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
122
|
-
} catch {
|
|
123
|
-
throw new RequestError(400, "Request body must be JSON.");
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function parseStartTurn(body) {
|
|
127
|
-
if (!body || typeof body !== "object") throw new RequestError(400, "Request body must be an object.");
|
|
128
|
-
const { turnId, message, env } = body;
|
|
129
|
-
if (typeof turnId !== "string" || !turnId || turnId.length > MAX_TURN_ID_LENGTH) {
|
|
130
|
-
throw new RequestError(400, `turnId must be a non-empty string of at most ${MAX_TURN_ID_LENGTH} characters.`);
|
|
131
|
-
}
|
|
132
|
-
if (typeof message !== "string" || !message.trim()) {
|
|
133
|
-
throw new RequestError(400, "message must be a non-empty string.");
|
|
134
|
-
}
|
|
135
|
-
if (env !== void 0 && (!env || typeof env !== "object" || Array.isArray(env) || Object.entries(env).some(([name, value]) => !/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name) || typeof value !== "string"))) {
|
|
136
|
-
throw new RequestError(400, "env must map environment variable names to strings.");
|
|
137
|
-
}
|
|
138
|
-
return { turnId, message, ...env === void 0 ? {} : { env } };
|
|
139
|
-
}
|
|
140
|
-
async function serve(options) {
|
|
141
|
-
const toWireEvent = options.toWireEvent ?? toPiJsonEvent;
|
|
142
|
-
let sessionPromise;
|
|
143
|
-
let current;
|
|
144
|
-
let previous;
|
|
145
|
-
let currentSessionAbort;
|
|
146
|
-
const findTurn = (turnId) => current?.id === turnId ? current : previous?.id === turnId ? previous : void 0;
|
|
147
|
-
const append = (turn, record) => {
|
|
148
|
-
const json = JSON.stringify({ seq: turn.records.length, ...record });
|
|
149
|
-
turn.records.push(json);
|
|
150
|
-
for (const wake of turn.waiters) wake();
|
|
151
|
-
};
|
|
152
|
-
const end = (turn, outcome, error) => {
|
|
153
|
-
if (turn.ended) return;
|
|
154
|
-
append(turn, { type: "end", outcome, ...error === void 0 ? {} : { error } });
|
|
155
|
-
turn.ended = true;
|
|
156
|
-
};
|
|
157
|
-
const endAfterGrace = (turn, outcome, error) => {
|
|
158
|
-
if (turn.ended || turn.finishing || turn.forceEnd) return;
|
|
159
|
-
turn.forceEnd = setTimeout(() => end(turn, outcome, error), options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS);
|
|
160
|
-
};
|
|
161
|
-
const runTurn = async (turn, request) => {
|
|
162
|
-
const previousEnv = /* @__PURE__ */ new Map();
|
|
163
|
-
let unsubscribe;
|
|
164
|
-
let timedOut = false;
|
|
165
|
-
let session;
|
|
166
|
-
const timeoutMessage = `Turn exceeded ${options.maxTurnDurationMs}ms.`;
|
|
167
|
-
const timer = options.maxTurnDurationMs === void 0 ? void 0 : setTimeout(() => {
|
|
168
|
-
timedOut = true;
|
|
169
|
-
if (session) void session.abort().catch(() => void 0);
|
|
170
|
-
endAfterGrace(turn, "failed", timeoutMessage);
|
|
171
|
-
}, options.maxTurnDurationMs);
|
|
172
|
-
const finish = async (outcome, error) => {
|
|
173
|
-
turn.finishing = true;
|
|
174
|
-
if (timer) clearTimeout(timer);
|
|
175
|
-
clearTimeout(turn.forceEnd);
|
|
176
|
-
try {
|
|
177
|
-
await session?.afterTurn?.();
|
|
178
|
-
} catch {
|
|
179
|
-
}
|
|
180
|
-
end(turn, outcome, error);
|
|
181
|
-
};
|
|
182
|
-
try {
|
|
183
|
-
sessionPromise ??= options.createSession();
|
|
184
|
-
try {
|
|
185
|
-
session = await sessionPromise;
|
|
186
|
-
} catch (error) {
|
|
187
|
-
sessionPromise = void 0;
|
|
188
|
-
throw error;
|
|
189
|
-
}
|
|
190
|
-
if (timedOut) {
|
|
191
|
-
end(turn, "failed", timeoutMessage);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
const started = session;
|
|
195
|
-
for (const [name, value] of Object.entries(request.env ?? {})) {
|
|
196
|
-
previousEnv.set(name, process.env[name]);
|
|
197
|
-
process.env[name] = value;
|
|
198
|
-
}
|
|
199
|
-
unsubscribe = session.subscribe((event) => {
|
|
200
|
-
if (turn.ended) return;
|
|
201
|
-
try {
|
|
202
|
-
append(turn, { type: "event", event: toWireEvent(event) });
|
|
203
|
-
} catch (error) {
|
|
204
|
-
end(turn, "failed", `Could not convert an agent event: ${error instanceof Error ? error.message : String(error)}`);
|
|
205
|
-
void started.abort().catch(() => void 0);
|
|
206
|
-
}
|
|
207
|
-
});
|
|
208
|
-
currentSessionAbort = () => started.abort();
|
|
209
|
-
if (turn.abortRequested) {
|
|
210
|
-
end(turn, "aborted");
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
await started.prompt(request.message);
|
|
214
|
-
if (timedOut) await finish("failed", timeoutMessage);
|
|
215
|
-
else await finish(turn.abortRequested ? "aborted" : "completed");
|
|
216
|
-
} catch (error) {
|
|
217
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
218
|
-
if (timedOut) await finish("failed", timeoutMessage);
|
|
219
|
-
else await finish(turn.abortRequested ? "aborted" : "failed", turn.abortRequested ? void 0 : message);
|
|
220
|
-
} finally {
|
|
221
|
-
if (timer) clearTimeout(timer);
|
|
222
|
-
clearTimeout(turn.forceEnd);
|
|
223
|
-
unsubscribe?.();
|
|
224
|
-
currentSessionAbort = void 0;
|
|
225
|
-
for (const [name, value] of previousEnv) {
|
|
226
|
-
if (value === void 0) delete process.env[name];
|
|
227
|
-
else process.env[name] = value;
|
|
228
|
-
}
|
|
229
|
-
turn.settled = true;
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
const busy = () => Boolean(current && !current.settled);
|
|
233
|
-
const startTurn = async (request) => {
|
|
234
|
-
const existing = findTurn(request.turnId);
|
|
235
|
-
if (existing) return { status: 200, body: { turnId: existing.id, ended: existing.ended } };
|
|
236
|
-
if (current && !current.settled) {
|
|
237
|
-
return { status: 409, body: { error: "Another turn is running.", turnId: current.id } };
|
|
238
|
-
}
|
|
239
|
-
const turn = {
|
|
240
|
-
id: request.turnId,
|
|
241
|
-
records: [],
|
|
242
|
-
ended: false,
|
|
243
|
-
settled: false,
|
|
244
|
-
abortRequested: false,
|
|
245
|
-
finishing: false,
|
|
246
|
-
waiters: /* @__PURE__ */ new Set()
|
|
247
|
-
};
|
|
248
|
-
previous = current;
|
|
249
|
-
current = turn;
|
|
250
|
-
void runTurn(turn, request);
|
|
251
|
-
return { status: 202, body: { turnId: turn.id, ended: false } };
|
|
252
|
-
};
|
|
253
|
-
const streamTurn = async (turn, after, request, response) => {
|
|
254
|
-
response.writeHead(200, {
|
|
255
|
-
"content-type": "text/event-stream",
|
|
256
|
-
"cache-control": "no-cache, no-transform",
|
|
257
|
-
connection: "keep-alive",
|
|
258
|
-
"x-accel-buffering": "no"
|
|
259
|
-
});
|
|
260
|
-
response.write(": connected\n\n");
|
|
261
|
-
let closed = false;
|
|
262
|
-
let wake;
|
|
263
|
-
const onClose = () => {
|
|
264
|
-
closed = true;
|
|
265
|
-
wake?.();
|
|
266
|
-
};
|
|
267
|
-
request.on("close", onClose);
|
|
268
|
-
response.on("close", onClose);
|
|
269
|
-
const heartbeat = setInterval(() => {
|
|
270
|
-
if (!closed) response.write(": ping\n\n");
|
|
271
|
-
}, HEARTBEAT_INTERVAL_MS);
|
|
272
|
-
let next = turn.ended ? Math.min(after + 1, turn.records.length - 1) : after + 1;
|
|
273
|
-
try {
|
|
274
|
-
while (!closed) {
|
|
275
|
-
while (!closed && next < turn.records.length) {
|
|
276
|
-
const ok = response.write(`id: ${next}
|
|
277
|
-
data: ${turn.records[next]}
|
|
278
|
-
|
|
279
|
-
`);
|
|
280
|
-
next += 1;
|
|
281
|
-
if (!ok && !closed) {
|
|
282
|
-
let onDrain;
|
|
283
|
-
await new Promise((resolve) => {
|
|
284
|
-
wake = onDrain = resolve;
|
|
285
|
-
response.once("drain", resolve);
|
|
286
|
-
});
|
|
287
|
-
if (onDrain) response.off("drain", onDrain);
|
|
288
|
-
wake = void 0;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
if (closed || turn.ended && next >= turn.records.length) break;
|
|
292
|
-
await new Promise((resolve) => {
|
|
293
|
-
wake = resolve;
|
|
294
|
-
turn.waiters.add(resolve);
|
|
295
|
-
});
|
|
296
|
-
if (wake) turn.waiters.delete(wake);
|
|
297
|
-
wake = void 0;
|
|
298
|
-
}
|
|
299
|
-
} finally {
|
|
300
|
-
clearInterval(heartbeat);
|
|
301
|
-
request.off("close", onClose);
|
|
302
|
-
response.off("close", onClose);
|
|
303
|
-
if (!closed) response.end();
|
|
304
|
-
}
|
|
305
|
-
};
|
|
306
|
-
const server = createServer((request, response) => {
|
|
307
|
-
void (async () => {
|
|
308
|
-
const url = new URL(request.url ?? "/", "http://localhost");
|
|
309
|
-
const turnPath = /^\/turns\/([^/]+)\/(events|abort)$/u.exec(url.pathname);
|
|
310
|
-
if (request.method === "GET" && url.pathname === "/health") {
|
|
311
|
-
const health = {
|
|
312
|
-
ok: true,
|
|
313
|
-
protocolVersion: TURN_PROTOCOL_VERSION,
|
|
314
|
-
busy: busy(),
|
|
315
|
-
turnId: current?.id ?? null
|
|
316
|
-
};
|
|
317
|
-
return sendJson(response, 200, health);
|
|
318
|
-
}
|
|
319
|
-
if (request.method === "POST" && url.pathname === "/turns") {
|
|
320
|
-
const { status, body } = await startTurn(parseStartTurn(await readJsonBody(request)));
|
|
321
|
-
return sendJson(response, status, body);
|
|
322
|
-
}
|
|
323
|
-
if (turnPath) {
|
|
324
|
-
const turn = findTurn(decodeURIComponent(turnPath[1]));
|
|
325
|
-
if (!turn) return sendJson(response, 404, { error: "Unknown turn." });
|
|
326
|
-
if (request.method === "GET" && turnPath[2] === "events") {
|
|
327
|
-
const afterParam = url.searchParams.get("after");
|
|
328
|
-
const after = afterParam === null ? -1 : Number(afterParam);
|
|
329
|
-
if (!Number.isSafeInteger(after) || after < -1) {
|
|
330
|
-
return sendJson(response, 400, { error: "after must be an integer of at least -1." });
|
|
331
|
-
}
|
|
332
|
-
return streamTurn(turn, after, request, response);
|
|
333
|
-
}
|
|
334
|
-
if (request.method === "POST" && turnPath[2] === "abort") {
|
|
335
|
-
if (!turn.ended && !turn.finishing && !turn.abortRequested) {
|
|
336
|
-
turn.abortRequested = true;
|
|
337
|
-
if (turn === current) void currentSessionAbort?.().catch(() => void 0);
|
|
338
|
-
endAfterGrace(turn, "aborted");
|
|
339
|
-
}
|
|
340
|
-
return sendJson(response, 202, { turnId: turn.id, ended: turn.ended });
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
sendJson(response, 404, { error: "Not found." });
|
|
344
|
-
})().catch((error) => {
|
|
345
|
-
if (response.headersSent) {
|
|
346
|
-
response.destroy();
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
const status = error instanceof RequestError ? error.status : 500;
|
|
350
|
-
sendJson(response, status, { error: error instanceof Error ? error.message : String(error) });
|
|
351
|
-
});
|
|
352
|
-
});
|
|
353
|
-
const port = options.port ?? (Number(process.env.NECKBEARD_PORT) || DEFAULT_PORT);
|
|
354
|
-
await new Promise((resolve, reject) => {
|
|
355
|
-
server.once("error", reject);
|
|
356
|
-
server.listen(port, options.host ?? "0.0.0.0", () => {
|
|
357
|
-
server.off("error", reject);
|
|
358
|
-
resolve();
|
|
359
|
-
});
|
|
360
|
-
});
|
|
361
|
-
return {
|
|
362
|
-
port: server.address().port,
|
|
363
|
-
close: () => new Promise((resolve, reject) => {
|
|
364
|
-
server.closeAllConnections();
|
|
365
|
-
server.close((error) => error ? reject(error) : resolve());
|
|
366
|
-
})
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
export {
|
|
371
|
-
TURN_PROTOCOL_VERSION,
|
|
372
|
-
parseTurnRecord,
|
|
373
|
-
parseTurnHealth,
|
|
374
|
-
getPiTurnResult,
|
|
375
|
-
toPiJsonEvent,
|
|
376
|
-
serve
|
|
377
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/** File content accepted by the public sandbox client. */
|
|
2
|
-
type SandboxFileContent = string | ArrayBuffer | Uint8Array;
|
|
3
|
-
/** File content used by portable, generated sandbox artifacts. */
|
|
4
|
-
type PortableSandboxFileContent = string | Uint8Array;
|
|
5
|
-
interface SandboxCommandOptions {
|
|
6
|
-
cwd?: string;
|
|
7
|
-
envs?: Record<string, string>;
|
|
8
|
-
timeoutMs: number;
|
|
9
|
-
onStdout?: (data: string) => void;
|
|
10
|
-
onStderr?: (data: string) => void;
|
|
11
|
-
}
|
|
12
|
-
/** Result from running a sandbox command with captured output. */
|
|
13
|
-
interface SandboxCommandResult {
|
|
14
|
-
exitCode: number;
|
|
15
|
-
stdout: string;
|
|
16
|
-
stderr: string;
|
|
17
|
-
}
|
|
18
|
-
interface SandboxDetachedCommandOptions {
|
|
19
|
-
cwd?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Environment applied only when the requested payload is spawned. Detached
|
|
22
|
-
* lifecycle preparation and its persistent reaper use a fixed clean
|
|
23
|
-
* environment.
|
|
24
|
-
*/
|
|
25
|
-
envs?: Record<string, string>;
|
|
26
|
-
/**
|
|
27
|
-
* Stable semantic identity for this launch. Providers hash this value before
|
|
28
|
-
* using it in sandbox paths so retries can adopt an already-prepared process
|
|
29
|
-
* without exposing the raw identity.
|
|
30
|
-
*/
|
|
31
|
-
launchId?: string;
|
|
32
|
-
/**
|
|
33
|
-
* Runs after the provider has captured a birth-fenced process identity but
|
|
34
|
-
* before the sandbox process is allowed to execute the requested command.
|
|
35
|
-
*/
|
|
36
|
-
beforeRelease?: (command: SandboxDetachedCommand) => void | Promise<void>;
|
|
37
|
-
}
|
|
38
|
-
interface SandboxDetachedCommand {
|
|
39
|
-
/**
|
|
40
|
-
* Provider-owned opaque session identity. Pass the complete handle to
|
|
41
|
-
* follow-up APIs instead of separating this value from `commandId`.
|
|
42
|
-
*/
|
|
43
|
-
readonly sessionId: string;
|
|
44
|
-
/** Provider-owned command identity, when the underlying provider has one. */
|
|
45
|
-
readonly commandId?: string;
|
|
46
|
-
}
|
|
47
|
-
interface SandboxFilesystemCapabilities<TContent = SandboxFileContent> {
|
|
48
|
-
makeDir(path: string): Promise<void>;
|
|
49
|
-
writeFile(path: string, content: TContent): Promise<void>;
|
|
50
|
-
}
|
|
51
|
-
interface SandboxCommandCapabilities<TOptions = SandboxCommandOptions> {
|
|
52
|
-
runCommand(command: string, options: TOptions): Promise<SandboxCommandResult>;
|
|
53
|
-
}
|
|
54
|
-
type SupervisorSandboxCommandOptions = Pick<SandboxCommandOptions, 'cwd' | 'envs' | 'timeoutMs'>;
|
|
55
|
-
/** Detached launch contract used only by durable supervisor deployment. */
|
|
56
|
-
interface SupervisorSandboxDetachedCommandOptions extends SandboxDetachedCommandOptions {
|
|
57
|
-
/**
|
|
58
|
-
* Starts the supervisor with a clean initial environment and hydrates
|
|
59
|
-
* `envs` from an inherited, unlinked descriptor.
|
|
60
|
-
*/
|
|
61
|
-
environmentMode: 'supervisor-fd-v1';
|
|
62
|
-
/** Binds a replacement generation to the exact supervisor environment. */
|
|
63
|
-
environmentRevision: string;
|
|
64
|
-
/**
|
|
65
|
-
* Permit replacement only after the exact released process session is
|
|
66
|
-
* proven empty. Durable reconciliation requires this generation policy.
|
|
67
|
-
*/
|
|
68
|
-
restartReleasedWhenExited: true;
|
|
69
|
-
}
|
|
70
|
-
/** Detached launch capability used only by durable supervisor deployment. */
|
|
71
|
-
interface SupervisorSandboxDetachedCommandCapabilities {
|
|
72
|
-
startDetachedCommand(command: string, options: SupervisorSandboxDetachedCommandOptions): Promise<SandboxDetachedCommand>;
|
|
73
|
-
}
|
|
74
|
-
/** Minimal host-side capabilities needed to deploy a generated supervisor. */
|
|
75
|
-
type SupervisorSandboxCapabilities = SandboxFilesystemCapabilities<PortableSandboxFileContent> & SandboxCommandCapabilities<SupervisorSandboxCommandOptions> & SupervisorSandboxDetachedCommandCapabilities;
|
|
76
|
-
|
|
77
|
-
export type { SandboxFileContent as S, SandboxCommandOptions as a, SandboxCommandResult as b, SandboxDetachedCommandOptions as c, SandboxDetachedCommand as d, SupervisorSandboxCapabilities as e, SupervisorSandboxDetachedCommandOptions as f };
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/** File content accepted by the public sandbox client. */
|
|
2
|
-
type SandboxFileContent = string | ArrayBuffer | Uint8Array;
|
|
3
|
-
/** File content used by portable, generated sandbox artifacts. */
|
|
4
|
-
type PortableSandboxFileContent = string | Uint8Array;
|
|
5
|
-
interface SandboxCommandOptions {
|
|
6
|
-
cwd?: string;
|
|
7
|
-
envs?: Record<string, string>;
|
|
8
|
-
timeoutMs: number;
|
|
9
|
-
onStdout?: (data: string) => void;
|
|
10
|
-
onStderr?: (data: string) => void;
|
|
11
|
-
}
|
|
12
|
-
/** Result from running a sandbox command with captured output. */
|
|
13
|
-
interface SandboxCommandResult {
|
|
14
|
-
exitCode: number;
|
|
15
|
-
stdout: string;
|
|
16
|
-
stderr: string;
|
|
17
|
-
}
|
|
18
|
-
interface SandboxDetachedCommandOptions {
|
|
19
|
-
cwd?: string;
|
|
20
|
-
/**
|
|
21
|
-
* Environment applied only when the requested payload is spawned. Detached
|
|
22
|
-
* lifecycle preparation and its persistent reaper use a fixed clean
|
|
23
|
-
* environment.
|
|
24
|
-
*/
|
|
25
|
-
envs?: Record<string, string>;
|
|
26
|
-
/**
|
|
27
|
-
* Stable semantic identity for this launch. Providers hash this value before
|
|
28
|
-
* using it in sandbox paths so retries can adopt an already-prepared process
|
|
29
|
-
* without exposing the raw identity.
|
|
30
|
-
*/
|
|
31
|
-
launchId?: string;
|
|
32
|
-
/**
|
|
33
|
-
* Runs after the provider has captured a birth-fenced process identity but
|
|
34
|
-
* before the sandbox process is allowed to execute the requested command.
|
|
35
|
-
*/
|
|
36
|
-
beforeRelease?: (command: SandboxDetachedCommand) => void | Promise<void>;
|
|
37
|
-
}
|
|
38
|
-
interface SandboxDetachedCommand {
|
|
39
|
-
/**
|
|
40
|
-
* Provider-owned opaque session identity. Pass the complete handle to
|
|
41
|
-
* follow-up APIs instead of separating this value from `commandId`.
|
|
42
|
-
*/
|
|
43
|
-
readonly sessionId: string;
|
|
44
|
-
/** Provider-owned command identity, when the underlying provider has one. */
|
|
45
|
-
readonly commandId?: string;
|
|
46
|
-
}
|
|
47
|
-
interface SandboxFilesystemCapabilities<TContent = SandboxFileContent> {
|
|
48
|
-
makeDir(path: string): Promise<void>;
|
|
49
|
-
writeFile(path: string, content: TContent): Promise<void>;
|
|
50
|
-
}
|
|
51
|
-
interface SandboxCommandCapabilities<TOptions = SandboxCommandOptions> {
|
|
52
|
-
runCommand(command: string, options: TOptions): Promise<SandboxCommandResult>;
|
|
53
|
-
}
|
|
54
|
-
type SupervisorSandboxCommandOptions = Pick<SandboxCommandOptions, 'cwd' | 'envs' | 'timeoutMs'>;
|
|
55
|
-
/** Detached launch contract used only by durable supervisor deployment. */
|
|
56
|
-
interface SupervisorSandboxDetachedCommandOptions extends SandboxDetachedCommandOptions {
|
|
57
|
-
/**
|
|
58
|
-
* Starts the supervisor with a clean initial environment and hydrates
|
|
59
|
-
* `envs` from an inherited, unlinked descriptor.
|
|
60
|
-
*/
|
|
61
|
-
environmentMode: 'supervisor-fd-v1';
|
|
62
|
-
/** Binds a replacement generation to the exact supervisor environment. */
|
|
63
|
-
environmentRevision: string;
|
|
64
|
-
/**
|
|
65
|
-
* Permit replacement only after the exact released process session is
|
|
66
|
-
* proven empty. Durable reconciliation requires this generation policy.
|
|
67
|
-
*/
|
|
68
|
-
restartReleasedWhenExited: true;
|
|
69
|
-
}
|
|
70
|
-
/** Detached launch capability used only by durable supervisor deployment. */
|
|
71
|
-
interface SupervisorSandboxDetachedCommandCapabilities {
|
|
72
|
-
startDetachedCommand(command: string, options: SupervisorSandboxDetachedCommandOptions): Promise<SandboxDetachedCommand>;
|
|
73
|
-
}
|
|
74
|
-
/** Minimal host-side capabilities needed to deploy a generated supervisor. */
|
|
75
|
-
type SupervisorSandboxCapabilities = SandboxFilesystemCapabilities<PortableSandboxFileContent> & SandboxCommandCapabilities<SupervisorSandboxCommandOptions> & SupervisorSandboxDetachedCommandCapabilities;
|
|
76
|
-
|
|
77
|
-
export type { SandboxFileContent as S, SandboxCommandOptions as a, SandboxCommandResult as b, SandboxDetachedCommandOptions as c, SandboxDetachedCommand as d, SupervisorSandboxCapabilities as e, SupervisorSandboxDetachedCommandOptions as f };
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wire contract between `Agent` on the host and `serve()` in the sandbox. See docs/AGENT.md.
|
|
3
|
-
*/
|
|
4
|
-
declare const TURN_PROTOCOL_VERSION = 2;
|
|
5
|
-
/** One record of a turn's stream. `seq` starts at 0 and increases by one per record. */
|
|
6
|
-
type TurnRecord<TEvent = unknown> = {
|
|
7
|
-
seq: number;
|
|
8
|
-
type: 'event';
|
|
9
|
-
event: TEvent;
|
|
10
|
-
} | {
|
|
11
|
-
seq: number;
|
|
12
|
-
type: 'end';
|
|
13
|
-
outcome: TurnOutcome;
|
|
14
|
-
error?: string;
|
|
15
|
-
};
|
|
16
|
-
type TurnOutcome = 'completed' | 'aborted' | 'failed';
|
|
17
|
-
interface StartTurnRequest {
|
|
18
|
-
/** Caller-chosen, stable per turn, so a retried start never runs the turn twice. */
|
|
19
|
-
turnId: string;
|
|
20
|
-
message: string;
|
|
21
|
-
/** Set in the agent's `process.env` for this turn only, then restored. */
|
|
22
|
-
env?: Record<string, string>;
|
|
23
|
-
}
|
|
24
|
-
interface TurnHealth {
|
|
25
|
-
ok: true;
|
|
26
|
-
protocolVersion: number;
|
|
27
|
-
busy: boolean;
|
|
28
|
-
turnId: string | null;
|
|
29
|
-
}
|
|
30
|
-
type PiTurnResult = {
|
|
31
|
-
completed: true;
|
|
32
|
-
text: string | null;
|
|
33
|
-
} | {
|
|
34
|
-
completed: false;
|
|
35
|
-
error: string;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* How a turn of a Pi session ended, from its records: completed when they include its `end`
|
|
39
|
-
* record, the turn ended normally, and its last assistant message stopped on its own, with that
|
|
40
|
-
* message's text. Records without an `end` record have not shown how the turn ended.
|
|
41
|
-
*/
|
|
42
|
-
declare function getPiTurnResult(records: readonly TurnRecord[]): PiTurnResult;
|
|
43
|
-
|
|
44
|
-
/** The parts of a Pi `AgentSession` the bridge drives. */
|
|
45
|
-
interface ServedSession {
|
|
46
|
-
subscribe(listener: (event: unknown) => void): () => void;
|
|
47
|
-
prompt(text: string): Promise<void>;
|
|
48
|
-
abort(): Promise<void>;
|
|
49
|
-
/**
|
|
50
|
-
* Runs once each turn's outcome is known: outside its deadline, so it can't fail the turn, and
|
|
51
|
-
* before its end record, so a host that deletes the sandbox at the end loses nothing, as when
|
|
52
|
-
* sending the turn's trace. An abort that arrives meanwhile changes nothing. Keep it bounded; it
|
|
53
|
-
* delays the end record. Its errors are ignored.
|
|
54
|
-
*/
|
|
55
|
-
afterTurn?(): Promise<void>;
|
|
56
|
-
}
|
|
57
|
-
interface ServeOptions {
|
|
58
|
-
/** Creates the agent's session before its first turn; the session is reused for every later turn. */
|
|
59
|
-
createSession(): Promise<ServedSession>;
|
|
60
|
-
/** Converts a session event for the wire. Defaults to Pi's JSON event form. */
|
|
61
|
-
toWireEvent?: (event: unknown) => unknown;
|
|
62
|
-
/** Defaults to `NECKBEARD_PORT`, then 8080. */
|
|
63
|
-
port?: number;
|
|
64
|
-
host?: string;
|
|
65
|
-
/** Aborts a turn that runs longer, ending it `failed`. */
|
|
66
|
-
maxTurnDurationMs?: number;
|
|
67
|
-
/**
|
|
68
|
-
* How long an aborted or timed-out turn may take to stop before its end record is written anyway,
|
|
69
|
-
* since Pi waits for a running tool. The runtime stays busy until the prompt returns. Defaults to 10 seconds.
|
|
70
|
-
*/
|
|
71
|
-
abortGraceMs?: number;
|
|
72
|
-
}
|
|
73
|
-
interface ServeHandle {
|
|
74
|
-
port: number;
|
|
75
|
-
close(): Promise<void>;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Pi's JSON event form, which its JSON and RPC modes write: `message_update` drops the
|
|
79
|
-
* cumulative `partial` message, which the deltas rebuild, and keeps the usage.
|
|
80
|
-
*/
|
|
81
|
-
declare function toPiJsonEvent(event: unknown): unknown;
|
|
82
|
-
/**
|
|
83
|
-
* Serves a Pi session over HTTP for `Agent` on the host. Run it as the sandbox image's main
|
|
84
|
-
* process. One turn runs at a time, a turn keeps running when its stream disconnects, and a
|
|
85
|
-
* stream resumes from any `seq` of the current or previous turn.
|
|
86
|
-
*/
|
|
87
|
-
declare function serve(options: ServeOptions): Promise<ServeHandle>;
|
|
88
|
-
|
|
89
|
-
export { type PiTurnResult as P, type StartTurnRequest as S, type TurnRecord as T, type TurnHealth as a, type ServedSession as b, type ServeHandle as c, TURN_PROTOCOL_VERSION as d, type TurnOutcome as e, type ServeOptions as f, getPiTurnResult as g, serve as s, toPiJsonEvent as t };
|