agent-neckbeard 1.1.28 → 1.1.29
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 +5 -0
- package/README.md +3 -3
- package/dist/build.cjs +170 -15
- package/dist/build.d.cts +1 -1
- package/dist/build.d.ts +1 -1
- package/dist/build.js +3 -3
- package/dist/{chunk-JF5YPOBY.js → chunk-BPXEIXPM.js} +25 -1
- package/dist/{chunk-M7XNBKG4.js → chunk-FSBTLBB5.js} +1 -1
- package/dist/{chunk-M3FISP7J.js → chunk-YFG4EKZM.js} +146 -15
- package/dist/cli.cjs +170 -15
- package/dist/cli.js +3 -3
- package/dist/index.cjs +170 -15
- package/dist/index.d.cts +12 -3
- package/dist/index.d.ts +12 -3
- package/dist/index.js +2 -2
- package/dist/{serve-Dan1HQKN.d.ts → serve-Du2NCz9_.d.cts} +24 -2
- package/dist/{serve-Dan1HQKN.d.cts → serve-Du2NCz9_.d.ts} +24 -2
- package/dist/serve.cjs +143 -13
- package/dist/serve.d.cts +1 -1
- package/dist/serve.d.ts +1 -1
- package/dist/serve.js +1 -1
- package/docs/AGENT.md +39 -7
- package/package.json +1 -1
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { createServer } from "http";
|
|
3
3
|
|
|
4
4
|
// src/turn-protocol.ts
|
|
5
|
-
var TURN_PROTOCOL_VERSION =
|
|
5
|
+
var TURN_PROTOCOL_VERSION = 3;
|
|
6
6
|
var MAX_TURN_REQUEST_BYTES = 4 * 1024 * 1024;
|
|
7
7
|
var MAX_TURN_ID_LENGTH = 200;
|
|
8
8
|
function isRecord(value) {
|
|
@@ -17,12 +17,13 @@ function parseTurnRecord(record) {
|
|
|
17
17
|
if (record.type === "event" && "event" in record) {
|
|
18
18
|
return { seq, type: "event", event: record.event };
|
|
19
19
|
}
|
|
20
|
-
if (record.type === "end" && (record.outcome === "completed" || record.outcome === "aborted" || record.outcome === "failed") && (record.error === void 0 || typeof record.error === "string")) {
|
|
20
|
+
if (record.type === "end" && (record.outcome === "completed" || record.outcome === "aborted" || record.outcome === "failed") && (record.error === void 0 || typeof record.error === "string") && (record.undeliveredSteerIds === void 0 || Array.isArray(record.undeliveredSteerIds) && record.undeliveredSteerIds.every((id) => typeof id === "string"))) {
|
|
21
21
|
return {
|
|
22
22
|
seq,
|
|
23
23
|
type: "end",
|
|
24
24
|
outcome: record.outcome,
|
|
25
|
-
...record.error === void 0 ? {} : { error: record.error }
|
|
25
|
+
...record.error === void 0 ? {} : { error: record.error },
|
|
26
|
+
...record.undeliveredSteerIds === void 0 ? {} : { undeliveredSteerIds: record.undeliveredSteerIds }
|
|
26
27
|
};
|
|
27
28
|
}
|
|
28
29
|
throw new Error("Turn record has an unknown shape.");
|
|
@@ -137,6 +138,17 @@ function parseStartTurn(body) {
|
|
|
137
138
|
}
|
|
138
139
|
return { turnId, message, ...env === void 0 ? {} : { env } };
|
|
139
140
|
}
|
|
141
|
+
function parseSteerTurn(body) {
|
|
142
|
+
if (!body || typeof body !== "object") throw new RequestError(400, "Request body must be an object.");
|
|
143
|
+
const { steerId, message } = body;
|
|
144
|
+
if (typeof steerId !== "string" || !steerId || steerId.length > MAX_TURN_ID_LENGTH) {
|
|
145
|
+
throw new RequestError(400, `steerId must be a non-empty string of at most ${MAX_TURN_ID_LENGTH} characters.`);
|
|
146
|
+
}
|
|
147
|
+
if (typeof message !== "string" || !message.trim()) {
|
|
148
|
+
throw new RequestError(400, "message must be a non-empty string.");
|
|
149
|
+
}
|
|
150
|
+
return { steerId, message };
|
|
151
|
+
}
|
|
140
152
|
async function serve(options) {
|
|
141
153
|
const toWireEvent = options.toWireEvent ?? toPiJsonEvent;
|
|
142
154
|
let sessionPromise;
|
|
@@ -149,14 +161,85 @@ async function serve(options) {
|
|
|
149
161
|
turn.records.push(json);
|
|
150
162
|
for (const wake of turn.waiters) wake();
|
|
151
163
|
};
|
|
164
|
+
const reclaimSteers = (turn) => {
|
|
165
|
+
let left;
|
|
166
|
+
try {
|
|
167
|
+
left = turn.session?.clearQueue?.().steering.length ?? 0;
|
|
168
|
+
} catch {
|
|
169
|
+
left = turn.queuedSteers.length;
|
|
170
|
+
sessionPromise = void 0;
|
|
171
|
+
}
|
|
172
|
+
const leftover = left > 0 ? turn.queuedSteers.slice(-left) : [];
|
|
173
|
+
for (const steer of turn.queuedSteers) steer.state = leftover.includes(steer) ? "undelivered" : "delivered";
|
|
174
|
+
turn.queuedSteers = [];
|
|
175
|
+
turn.undeliveredSteers.push(...leftover);
|
|
176
|
+
};
|
|
152
177
|
const end = (turn, outcome, error) => {
|
|
153
|
-
if (turn.
|
|
154
|
-
|
|
155
|
-
turn.
|
|
178
|
+
if (turn.ending) return turn.ending;
|
|
179
|
+
turn.closed = true;
|
|
180
|
+
turn.sessionReady();
|
|
181
|
+
let written;
|
|
182
|
+
turn.ending = new Promise((resolve) => {
|
|
183
|
+
written = resolve;
|
|
184
|
+
});
|
|
185
|
+
reclaimSteers(turn);
|
|
186
|
+
void (async () => {
|
|
187
|
+
let timer;
|
|
188
|
+
const graceMs = turn.stopBy === void 0 ? options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS : Math.max(0, turn.stopBy - Date.now());
|
|
189
|
+
await Promise.race([
|
|
190
|
+
turn.steering,
|
|
191
|
+
new Promise((resolve) => {
|
|
192
|
+
timer = setTimeout(resolve, graceMs);
|
|
193
|
+
})
|
|
194
|
+
]);
|
|
195
|
+
clearTimeout(timer);
|
|
196
|
+
reclaimSteers(turn);
|
|
197
|
+
const undeliveredSteerIds = turn.undeliveredSteers.filter((steer) => !steer.late).map((steer) => steer.id);
|
|
198
|
+
append(turn, {
|
|
199
|
+
type: "end",
|
|
200
|
+
outcome,
|
|
201
|
+
...error === void 0 ? {} : { error },
|
|
202
|
+
...undeliveredSteerIds.length > 0 ? { undeliveredSteerIds } : {}
|
|
203
|
+
});
|
|
204
|
+
turn.ended = true;
|
|
205
|
+
written();
|
|
206
|
+
})();
|
|
207
|
+
return turn.ending;
|
|
208
|
+
};
|
|
209
|
+
const acceptSteer = (turn, id, message) => {
|
|
210
|
+
const steer = { id, state: "pending", late: false, settled: Promise.resolve() };
|
|
211
|
+
steer.settled = turn.steering.then(async () => {
|
|
212
|
+
const session = turn.session;
|
|
213
|
+
if (turn.closed || !session?.steer || !session.clearQueue) {
|
|
214
|
+
steer.state = "undelivered";
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
if (await session.steer(message) === "handled") {
|
|
219
|
+
steer.state = "handled";
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
steer.state = "queued";
|
|
223
|
+
turn.queuedSteers.push(steer);
|
|
224
|
+
if (turn.closed) {
|
|
225
|
+
reclaimSteers(turn);
|
|
226
|
+
steer.late = turn.undeliveredSteers.includes(steer);
|
|
227
|
+
}
|
|
228
|
+
} catch (error) {
|
|
229
|
+
steer.state = "failed";
|
|
230
|
+
steer.error = error;
|
|
231
|
+
turn.steers.delete(id);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
turn.steering = steer.settled;
|
|
235
|
+
turn.steers.set(id, steer);
|
|
236
|
+
return steer;
|
|
156
237
|
};
|
|
157
238
|
const endAfterGrace = (turn, outcome, error) => {
|
|
158
|
-
if (turn.
|
|
159
|
-
|
|
239
|
+
if (turn.ending || turn.finishing || turn.forceEnd) return;
|
|
240
|
+
const graceMs = options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS;
|
|
241
|
+
turn.stopBy = Date.now() + graceMs;
|
|
242
|
+
turn.forceEnd = setTimeout(() => void end(turn, outcome, error), graceMs);
|
|
160
243
|
};
|
|
161
244
|
const runTurn = async (turn, request) => {
|
|
162
245
|
const previousEnv = /* @__PURE__ */ new Map();
|
|
@@ -166,18 +249,21 @@ async function serve(options) {
|
|
|
166
249
|
const timeoutMessage = `Turn exceeded ${options.maxTurnDurationMs}ms.`;
|
|
167
250
|
const timer = options.maxTurnDurationMs === void 0 ? void 0 : setTimeout(() => {
|
|
168
251
|
timedOut = true;
|
|
252
|
+
turn.closed = true;
|
|
253
|
+
reclaimSteers(turn);
|
|
169
254
|
if (session) void session.abort().catch(() => void 0);
|
|
170
255
|
endAfterGrace(turn, "failed", timeoutMessage);
|
|
171
256
|
}, options.maxTurnDurationMs);
|
|
172
257
|
const finish = async (outcome, error) => {
|
|
173
258
|
turn.finishing = true;
|
|
259
|
+
turn.closed = true;
|
|
174
260
|
if (timer) clearTimeout(timer);
|
|
175
261
|
clearTimeout(turn.forceEnd);
|
|
176
262
|
try {
|
|
177
263
|
await session?.afterTurn?.();
|
|
178
264
|
} catch {
|
|
179
265
|
}
|
|
180
|
-
end(turn, outcome, error);
|
|
266
|
+
await end(turn, outcome, error);
|
|
181
267
|
};
|
|
182
268
|
try {
|
|
183
269
|
sessionPromise ??= options.createSession();
|
|
@@ -188,26 +274,34 @@ async function serve(options) {
|
|
|
188
274
|
throw error;
|
|
189
275
|
}
|
|
190
276
|
if (timedOut) {
|
|
191
|
-
end(turn, "failed", timeoutMessage);
|
|
277
|
+
await end(turn, "failed", timeoutMessage);
|
|
192
278
|
return;
|
|
193
279
|
}
|
|
194
280
|
const started = session;
|
|
281
|
+
turn.session = session;
|
|
195
282
|
for (const [name, value] of Object.entries(request.env ?? {})) {
|
|
196
283
|
previousEnv.set(name, process.env[name]);
|
|
197
284
|
process.env[name] = value;
|
|
198
285
|
}
|
|
199
286
|
unsubscribe = session.subscribe((event) => {
|
|
200
|
-
if (turn.
|
|
287
|
+
if (turn.ending) return;
|
|
201
288
|
try {
|
|
202
289
|
append(turn, { type: "event", event: toWireEvent(event) });
|
|
203
290
|
} catch (error) {
|
|
204
|
-
end(turn, "failed", `Could not convert an agent event: ${error instanceof Error ? error.message : String(error)}`);
|
|
291
|
+
void end(turn, "failed", `Could not convert an agent event: ${error instanceof Error ? error.message : String(error)}`);
|
|
205
292
|
void started.abort().catch(() => void 0);
|
|
206
293
|
}
|
|
207
294
|
});
|
|
208
295
|
currentSessionAbort = () => started.abort();
|
|
296
|
+
turn.sessionReady();
|
|
297
|
+
if (!turn.closed) await turn.steering;
|
|
298
|
+
if (turn.ending) return;
|
|
299
|
+
if (timedOut) {
|
|
300
|
+
await end(turn, "failed", timeoutMessage);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
209
303
|
if (turn.abortRequested) {
|
|
210
|
-
end(turn, "aborted");
|
|
304
|
+
await end(turn, "aborted");
|
|
211
305
|
return;
|
|
212
306
|
}
|
|
213
307
|
await started.prompt(request.message);
|
|
@@ -220,8 +314,13 @@ async function serve(options) {
|
|
|
220
314
|
} finally {
|
|
221
315
|
if (timer) clearTimeout(timer);
|
|
222
316
|
clearTimeout(turn.forceEnd);
|
|
317
|
+
turn.closed = true;
|
|
318
|
+
turn.sessionReady();
|
|
319
|
+
await turn.ending;
|
|
223
320
|
unsubscribe?.();
|
|
224
321
|
currentSessionAbort = void 0;
|
|
322
|
+
await turn.steering;
|
|
323
|
+
reclaimSteers(turn);
|
|
225
324
|
for (const [name, value] of previousEnv) {
|
|
226
325
|
if (value === void 0) delete process.env[name];
|
|
227
326
|
else process.env[name] = value;
|
|
@@ -236,12 +335,21 @@ async function serve(options) {
|
|
|
236
335
|
if (current && !current.settled) {
|
|
237
336
|
return { status: 409, body: { error: "Another turn is running.", turnId: current.id } };
|
|
238
337
|
}
|
|
338
|
+
let sessionReady;
|
|
239
339
|
const turn = {
|
|
240
340
|
id: request.turnId,
|
|
241
341
|
records: [],
|
|
242
342
|
ended: false,
|
|
243
343
|
settled: false,
|
|
244
344
|
abortRequested: false,
|
|
345
|
+
steers: /* @__PURE__ */ new Map(),
|
|
346
|
+
queuedSteers: [],
|
|
347
|
+
undeliveredSteers: [],
|
|
348
|
+
steering: new Promise((resolve) => {
|
|
349
|
+
sessionReady = resolve;
|
|
350
|
+
}),
|
|
351
|
+
sessionReady: () => sessionReady(),
|
|
352
|
+
closed: false,
|
|
245
353
|
finishing: false,
|
|
246
354
|
waiters: /* @__PURE__ */ new Set()
|
|
247
355
|
};
|
|
@@ -306,7 +414,7 @@ data: ${turn.records[next]}
|
|
|
306
414
|
const server = createServer((request, response) => {
|
|
307
415
|
void (async () => {
|
|
308
416
|
const url = new URL(request.url ?? "/", "http://localhost");
|
|
309
|
-
const turnPath = /^\/turns\/([^/]+)\/(events|abort)$/u.exec(url.pathname);
|
|
417
|
+
const turnPath = /^\/turns\/([^/]+)\/(events|abort|steer)$/u.exec(url.pathname);
|
|
310
418
|
if (request.method === "GET" && url.pathname === "/health") {
|
|
311
419
|
const health = {
|
|
312
420
|
ok: true,
|
|
@@ -332,13 +440,36 @@ data: ${turn.records[next]}
|
|
|
332
440
|
return streamTurn(turn, after, request, response);
|
|
333
441
|
}
|
|
334
442
|
if (request.method === "POST" && turnPath[2] === "abort") {
|
|
335
|
-
if (!turn.
|
|
443
|
+
if (!turn.ending && !turn.finishing && !turn.abortRequested) {
|
|
336
444
|
turn.abortRequested = true;
|
|
445
|
+
turn.closed = true;
|
|
446
|
+
reclaimSteers(turn);
|
|
337
447
|
if (turn === current) void currentSessionAbort?.().catch(() => void 0);
|
|
338
448
|
endAfterGrace(turn, "aborted");
|
|
339
449
|
}
|
|
340
450
|
return sendJson(response, 202, { turnId: turn.id, ended: turn.ended });
|
|
341
451
|
}
|
|
452
|
+
if (request.method === "POST" && turnPath[2] === "steer") {
|
|
453
|
+
const { steerId, message } = parseSteerTurn(await readJsonBody(request));
|
|
454
|
+
const known = turn.steers.get(steerId);
|
|
455
|
+
if (!known && (turn !== current || turn.closed)) {
|
|
456
|
+
return sendJson(response, 409, { error: "The turn is no longer running.", turnId: turn.id });
|
|
457
|
+
}
|
|
458
|
+
const steer = known ?? acceptSteer(turn, steerId, message);
|
|
459
|
+
await steer.settled;
|
|
460
|
+
if (steer.state === "failed") throw steer.error;
|
|
461
|
+
if (steer.state === "undelivered" && !turn.closed) {
|
|
462
|
+
return sendJson(response, 501, { error: "The session does not take steering messages." });
|
|
463
|
+
}
|
|
464
|
+
if (steer.late || steer.state === "undelivered") {
|
|
465
|
+
return sendJson(response, 409, { error: "The turn is no longer running.", turnId: turn.id });
|
|
466
|
+
}
|
|
467
|
+
return sendJson(response, known ? 200 : 202, {
|
|
468
|
+
turnId: turn.id,
|
|
469
|
+
steerId,
|
|
470
|
+
...steer.state === "handled" ? { handled: true } : {}
|
|
471
|
+
});
|
|
472
|
+
}
|
|
342
473
|
}
|
|
343
474
|
sendJson(response, 404, { error: "Not found." });
|
|
344
475
|
})().catch((error) => {
|
package/dist/cli.cjs
CHANGED
|
@@ -3416,7 +3416,7 @@ var ModalSandboxHandle = class _ModalSandboxHandle {
|
|
|
3416
3416
|
};
|
|
3417
3417
|
|
|
3418
3418
|
// src/turn-protocol.ts
|
|
3419
|
-
var TURN_PROTOCOL_VERSION =
|
|
3419
|
+
var TURN_PROTOCOL_VERSION = 3;
|
|
3420
3420
|
var MAX_TURN_REQUEST_BYTES = 4 * 1024 * 1024;
|
|
3421
3421
|
var MAX_TURN_ID_LENGTH = 200;
|
|
3422
3422
|
function isRecord2(value) {
|
|
@@ -3431,12 +3431,13 @@ function parseTurnRecord(record) {
|
|
|
3431
3431
|
if (record.type === "event" && "event" in record) {
|
|
3432
3432
|
return { seq, type: "event", event: record.event };
|
|
3433
3433
|
}
|
|
3434
|
-
if (record.type === "end" && (record.outcome === "completed" || record.outcome === "aborted" || record.outcome === "failed") && (record.error === void 0 || typeof record.error === "string")) {
|
|
3434
|
+
if (record.type === "end" && (record.outcome === "completed" || record.outcome === "aborted" || record.outcome === "failed") && (record.error === void 0 || typeof record.error === "string") && (record.undeliveredSteerIds === void 0 || Array.isArray(record.undeliveredSteerIds) && record.undeliveredSteerIds.every((id) => typeof id === "string"))) {
|
|
3435
3435
|
return {
|
|
3436
3436
|
seq,
|
|
3437
3437
|
type: "end",
|
|
3438
3438
|
outcome: record.outcome,
|
|
3439
|
-
...record.error === void 0 ? {} : { error: record.error }
|
|
3439
|
+
...record.error === void 0 ? {} : { error: record.error },
|
|
3440
|
+
...record.undeliveredSteerIds === void 0 ? {} : { undeliveredSteerIds: record.undeliveredSteerIds }
|
|
3440
3441
|
};
|
|
3441
3442
|
}
|
|
3442
3443
|
throw new Error("Turn record has an unknown shape.");
|
|
@@ -4436,6 +4437,25 @@ var Agent = class {
|
|
|
4436
4437
|
throw new ExecutionError(`Aborting turn ${turnId} failed with ${response.status}.`);
|
|
4437
4438
|
}
|
|
4438
4439
|
}
|
|
4440
|
+
/**
|
|
4441
|
+
* Queues a message for a running turn, which Pi takes after its current tool calls. Resolves to
|
|
4442
|
+
* true once the session has queued it: it is then delivered into the turn, or named in the end
|
|
4443
|
+
* record's `undeliveredSteerIds`. Resolves to false when the turn can no longer take it, or the
|
|
4444
|
+
* runtime no longer has it, so the caller can send the message as a turn of its own. Throws when
|
|
4445
|
+
* queueing failed or the request did; calling again with the same `steerId` never queues it twice.
|
|
4446
|
+
*/
|
|
4447
|
+
async steer(sandboxId, turnId, steer, signal) {
|
|
4448
|
+
const response = await this.request(sandboxId, "POST", `/turns/${encodeURIComponent(turnId)}/steer`, steer, signal);
|
|
4449
|
+
if (response.status === 200 || response.status === 202) {
|
|
4450
|
+
await response.body?.cancel();
|
|
4451
|
+
return true;
|
|
4452
|
+
}
|
|
4453
|
+
if (response.status === 409 || response.status === 404) {
|
|
4454
|
+
await response.body?.cancel();
|
|
4455
|
+
return false;
|
|
4456
|
+
}
|
|
4457
|
+
throw new ExecutionError(`Steering turn ${turnId} failed with ${response.status}: ${await response.text()}`);
|
|
4458
|
+
}
|
|
4439
4459
|
async health(sandboxId, signal) {
|
|
4440
4460
|
const response = await this.request(sandboxId, "GET", "/health", void 0, signal);
|
|
4441
4461
|
if (!response.ok) throw new ExecutionError(`Health check of sandbox ${sandboxId} returned ${response.status}.`);
|
|
@@ -4790,6 +4810,17 @@ function parseStartTurn(body) {
|
|
|
4790
4810
|
}
|
|
4791
4811
|
return { turnId, message, ...env === void 0 ? {} : { env } };
|
|
4792
4812
|
}
|
|
4813
|
+
function parseSteerTurn(body) {
|
|
4814
|
+
if (!body || typeof body !== "object") throw new RequestError(400, "Request body must be an object.");
|
|
4815
|
+
const { steerId, message } = body;
|
|
4816
|
+
if (typeof steerId !== "string" || !steerId || steerId.length > MAX_TURN_ID_LENGTH) {
|
|
4817
|
+
throw new RequestError(400, `steerId must be a non-empty string of at most ${MAX_TURN_ID_LENGTH} characters.`);
|
|
4818
|
+
}
|
|
4819
|
+
if (typeof message !== "string" || !message.trim()) {
|
|
4820
|
+
throw new RequestError(400, "message must be a non-empty string.");
|
|
4821
|
+
}
|
|
4822
|
+
return { steerId, message };
|
|
4823
|
+
}
|
|
4793
4824
|
async function serve(options) {
|
|
4794
4825
|
const toWireEvent = options.toWireEvent ?? toPiJsonEvent;
|
|
4795
4826
|
let sessionPromise;
|
|
@@ -4802,14 +4833,85 @@ async function serve(options) {
|
|
|
4802
4833
|
turn.records.push(json);
|
|
4803
4834
|
for (const wake of turn.waiters) wake();
|
|
4804
4835
|
};
|
|
4836
|
+
const reclaimSteers = (turn) => {
|
|
4837
|
+
let left;
|
|
4838
|
+
try {
|
|
4839
|
+
left = turn.session?.clearQueue?.().steering.length ?? 0;
|
|
4840
|
+
} catch {
|
|
4841
|
+
left = turn.queuedSteers.length;
|
|
4842
|
+
sessionPromise = void 0;
|
|
4843
|
+
}
|
|
4844
|
+
const leftover = left > 0 ? turn.queuedSteers.slice(-left) : [];
|
|
4845
|
+
for (const steer of turn.queuedSteers) steer.state = leftover.includes(steer) ? "undelivered" : "delivered";
|
|
4846
|
+
turn.queuedSteers = [];
|
|
4847
|
+
turn.undeliveredSteers.push(...leftover);
|
|
4848
|
+
};
|
|
4805
4849
|
const end = (turn, outcome, error) => {
|
|
4806
|
-
if (turn.
|
|
4807
|
-
|
|
4808
|
-
turn.
|
|
4850
|
+
if (turn.ending) return turn.ending;
|
|
4851
|
+
turn.closed = true;
|
|
4852
|
+
turn.sessionReady();
|
|
4853
|
+
let written;
|
|
4854
|
+
turn.ending = new Promise((resolve2) => {
|
|
4855
|
+
written = resolve2;
|
|
4856
|
+
});
|
|
4857
|
+
reclaimSteers(turn);
|
|
4858
|
+
void (async () => {
|
|
4859
|
+
let timer;
|
|
4860
|
+
const graceMs = turn.stopBy === void 0 ? options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS : Math.max(0, turn.stopBy - Date.now());
|
|
4861
|
+
await Promise.race([
|
|
4862
|
+
turn.steering,
|
|
4863
|
+
new Promise((resolve2) => {
|
|
4864
|
+
timer = setTimeout(resolve2, graceMs);
|
|
4865
|
+
})
|
|
4866
|
+
]);
|
|
4867
|
+
clearTimeout(timer);
|
|
4868
|
+
reclaimSteers(turn);
|
|
4869
|
+
const undeliveredSteerIds = turn.undeliveredSteers.filter((steer) => !steer.late).map((steer) => steer.id);
|
|
4870
|
+
append(turn, {
|
|
4871
|
+
type: "end",
|
|
4872
|
+
outcome,
|
|
4873
|
+
...error === void 0 ? {} : { error },
|
|
4874
|
+
...undeliveredSteerIds.length > 0 ? { undeliveredSteerIds } : {}
|
|
4875
|
+
});
|
|
4876
|
+
turn.ended = true;
|
|
4877
|
+
written();
|
|
4878
|
+
})();
|
|
4879
|
+
return turn.ending;
|
|
4880
|
+
};
|
|
4881
|
+
const acceptSteer = (turn, id, message) => {
|
|
4882
|
+
const steer = { id, state: "pending", late: false, settled: Promise.resolve() };
|
|
4883
|
+
steer.settled = turn.steering.then(async () => {
|
|
4884
|
+
const session = turn.session;
|
|
4885
|
+
if (turn.closed || !session?.steer || !session.clearQueue) {
|
|
4886
|
+
steer.state = "undelivered";
|
|
4887
|
+
return;
|
|
4888
|
+
}
|
|
4889
|
+
try {
|
|
4890
|
+
if (await session.steer(message) === "handled") {
|
|
4891
|
+
steer.state = "handled";
|
|
4892
|
+
return;
|
|
4893
|
+
}
|
|
4894
|
+
steer.state = "queued";
|
|
4895
|
+
turn.queuedSteers.push(steer);
|
|
4896
|
+
if (turn.closed) {
|
|
4897
|
+
reclaimSteers(turn);
|
|
4898
|
+
steer.late = turn.undeliveredSteers.includes(steer);
|
|
4899
|
+
}
|
|
4900
|
+
} catch (error) {
|
|
4901
|
+
steer.state = "failed";
|
|
4902
|
+
steer.error = error;
|
|
4903
|
+
turn.steers.delete(id);
|
|
4904
|
+
}
|
|
4905
|
+
});
|
|
4906
|
+
turn.steering = steer.settled;
|
|
4907
|
+
turn.steers.set(id, steer);
|
|
4908
|
+
return steer;
|
|
4809
4909
|
};
|
|
4810
4910
|
const endAfterGrace = (turn, outcome, error) => {
|
|
4811
|
-
if (turn.
|
|
4812
|
-
|
|
4911
|
+
if (turn.ending || turn.finishing || turn.forceEnd) return;
|
|
4912
|
+
const graceMs = options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS;
|
|
4913
|
+
turn.stopBy = Date.now() + graceMs;
|
|
4914
|
+
turn.forceEnd = setTimeout(() => void end(turn, outcome, error), graceMs);
|
|
4813
4915
|
};
|
|
4814
4916
|
const runTurn = async (turn, request) => {
|
|
4815
4917
|
const previousEnv = /* @__PURE__ */ new Map();
|
|
@@ -4819,18 +4921,21 @@ async function serve(options) {
|
|
|
4819
4921
|
const timeoutMessage = `Turn exceeded ${options.maxTurnDurationMs}ms.`;
|
|
4820
4922
|
const timer = options.maxTurnDurationMs === void 0 ? void 0 : setTimeout(() => {
|
|
4821
4923
|
timedOut = true;
|
|
4924
|
+
turn.closed = true;
|
|
4925
|
+
reclaimSteers(turn);
|
|
4822
4926
|
if (session) void session.abort().catch(() => void 0);
|
|
4823
4927
|
endAfterGrace(turn, "failed", timeoutMessage);
|
|
4824
4928
|
}, options.maxTurnDurationMs);
|
|
4825
4929
|
const finish = async (outcome, error) => {
|
|
4826
4930
|
turn.finishing = true;
|
|
4931
|
+
turn.closed = true;
|
|
4827
4932
|
if (timer) clearTimeout(timer);
|
|
4828
4933
|
clearTimeout(turn.forceEnd);
|
|
4829
4934
|
try {
|
|
4830
4935
|
await session?.afterTurn?.();
|
|
4831
4936
|
} catch {
|
|
4832
4937
|
}
|
|
4833
|
-
end(turn, outcome, error);
|
|
4938
|
+
await end(turn, outcome, error);
|
|
4834
4939
|
};
|
|
4835
4940
|
try {
|
|
4836
4941
|
sessionPromise ??= options.createSession();
|
|
@@ -4841,26 +4946,34 @@ async function serve(options) {
|
|
|
4841
4946
|
throw error;
|
|
4842
4947
|
}
|
|
4843
4948
|
if (timedOut) {
|
|
4844
|
-
end(turn, "failed", timeoutMessage);
|
|
4949
|
+
await end(turn, "failed", timeoutMessage);
|
|
4845
4950
|
return;
|
|
4846
4951
|
}
|
|
4847
4952
|
const started = session;
|
|
4953
|
+
turn.session = session;
|
|
4848
4954
|
for (const [name, value] of Object.entries(request.env ?? {})) {
|
|
4849
4955
|
previousEnv.set(name, process.env[name]);
|
|
4850
4956
|
process.env[name] = value;
|
|
4851
4957
|
}
|
|
4852
4958
|
unsubscribe = session.subscribe((event) => {
|
|
4853
|
-
if (turn.
|
|
4959
|
+
if (turn.ending) return;
|
|
4854
4960
|
try {
|
|
4855
4961
|
append(turn, { type: "event", event: toWireEvent(event) });
|
|
4856
4962
|
} catch (error) {
|
|
4857
|
-
end(turn, "failed", `Could not convert an agent event: ${error instanceof Error ? error.message : String(error)}`);
|
|
4963
|
+
void end(turn, "failed", `Could not convert an agent event: ${error instanceof Error ? error.message : String(error)}`);
|
|
4858
4964
|
void started.abort().catch(() => void 0);
|
|
4859
4965
|
}
|
|
4860
4966
|
});
|
|
4861
4967
|
currentSessionAbort = () => started.abort();
|
|
4968
|
+
turn.sessionReady();
|
|
4969
|
+
if (!turn.closed) await turn.steering;
|
|
4970
|
+
if (turn.ending) return;
|
|
4971
|
+
if (timedOut) {
|
|
4972
|
+
await end(turn, "failed", timeoutMessage);
|
|
4973
|
+
return;
|
|
4974
|
+
}
|
|
4862
4975
|
if (turn.abortRequested) {
|
|
4863
|
-
end(turn, "aborted");
|
|
4976
|
+
await end(turn, "aborted");
|
|
4864
4977
|
return;
|
|
4865
4978
|
}
|
|
4866
4979
|
await started.prompt(request.message);
|
|
@@ -4873,8 +4986,13 @@ async function serve(options) {
|
|
|
4873
4986
|
} finally {
|
|
4874
4987
|
if (timer) clearTimeout(timer);
|
|
4875
4988
|
clearTimeout(turn.forceEnd);
|
|
4989
|
+
turn.closed = true;
|
|
4990
|
+
turn.sessionReady();
|
|
4991
|
+
await turn.ending;
|
|
4876
4992
|
unsubscribe?.();
|
|
4877
4993
|
currentSessionAbort = void 0;
|
|
4994
|
+
await turn.steering;
|
|
4995
|
+
reclaimSteers(turn);
|
|
4878
4996
|
for (const [name, value] of previousEnv) {
|
|
4879
4997
|
if (value === void 0) delete process.env[name];
|
|
4880
4998
|
else process.env[name] = value;
|
|
@@ -4889,12 +5007,21 @@ async function serve(options) {
|
|
|
4889
5007
|
if (current && !current.settled) {
|
|
4890
5008
|
return { status: 409, body: { error: "Another turn is running.", turnId: current.id } };
|
|
4891
5009
|
}
|
|
5010
|
+
let sessionReady;
|
|
4892
5011
|
const turn = {
|
|
4893
5012
|
id: request.turnId,
|
|
4894
5013
|
records: [],
|
|
4895
5014
|
ended: false,
|
|
4896
5015
|
settled: false,
|
|
4897
5016
|
abortRequested: false,
|
|
5017
|
+
steers: /* @__PURE__ */ new Map(),
|
|
5018
|
+
queuedSteers: [],
|
|
5019
|
+
undeliveredSteers: [],
|
|
5020
|
+
steering: new Promise((resolve2) => {
|
|
5021
|
+
sessionReady = resolve2;
|
|
5022
|
+
}),
|
|
5023
|
+
sessionReady: () => sessionReady(),
|
|
5024
|
+
closed: false,
|
|
4898
5025
|
finishing: false,
|
|
4899
5026
|
waiters: /* @__PURE__ */ new Set()
|
|
4900
5027
|
};
|
|
@@ -4959,7 +5086,7 @@ data: ${turn.records[next]}
|
|
|
4959
5086
|
const server = (0, import_node_http.createServer)((request, response) => {
|
|
4960
5087
|
void (async () => {
|
|
4961
5088
|
const url = new URL(request.url ?? "/", "http://localhost");
|
|
4962
|
-
const turnPath = /^\/turns\/([^/]+)\/(events|abort)$/u.exec(url.pathname);
|
|
5089
|
+
const turnPath = /^\/turns\/([^/]+)\/(events|abort|steer)$/u.exec(url.pathname);
|
|
4963
5090
|
if (request.method === "GET" && url.pathname === "/health") {
|
|
4964
5091
|
const health = {
|
|
4965
5092
|
ok: true,
|
|
@@ -4985,13 +5112,36 @@ data: ${turn.records[next]}
|
|
|
4985
5112
|
return streamTurn(turn, after, request, response);
|
|
4986
5113
|
}
|
|
4987
5114
|
if (request.method === "POST" && turnPath[2] === "abort") {
|
|
4988
|
-
if (!turn.
|
|
5115
|
+
if (!turn.ending && !turn.finishing && !turn.abortRequested) {
|
|
4989
5116
|
turn.abortRequested = true;
|
|
5117
|
+
turn.closed = true;
|
|
5118
|
+
reclaimSteers(turn);
|
|
4990
5119
|
if (turn === current) void currentSessionAbort?.().catch(() => void 0);
|
|
4991
5120
|
endAfterGrace(turn, "aborted");
|
|
4992
5121
|
}
|
|
4993
5122
|
return sendJson(response, 202, { turnId: turn.id, ended: turn.ended });
|
|
4994
5123
|
}
|
|
5124
|
+
if (request.method === "POST" && turnPath[2] === "steer") {
|
|
5125
|
+
const { steerId, message } = parseSteerTurn(await readJsonBody(request));
|
|
5126
|
+
const known = turn.steers.get(steerId);
|
|
5127
|
+
if (!known && (turn !== current || turn.closed)) {
|
|
5128
|
+
return sendJson(response, 409, { error: "The turn is no longer running.", turnId: turn.id });
|
|
5129
|
+
}
|
|
5130
|
+
const steer = known ?? acceptSteer(turn, steerId, message);
|
|
5131
|
+
await steer.settled;
|
|
5132
|
+
if (steer.state === "failed") throw steer.error;
|
|
5133
|
+
if (steer.state === "undelivered" && !turn.closed) {
|
|
5134
|
+
return sendJson(response, 501, { error: "The session does not take steering messages." });
|
|
5135
|
+
}
|
|
5136
|
+
if (steer.late || steer.state === "undelivered") {
|
|
5137
|
+
return sendJson(response, 409, { error: "The turn is no longer running.", turnId: turn.id });
|
|
5138
|
+
}
|
|
5139
|
+
return sendJson(response, known ? 200 : 202, {
|
|
5140
|
+
turnId: turn.id,
|
|
5141
|
+
steerId,
|
|
5142
|
+
...steer.state === "handled" ? { handled: true } : {}
|
|
5143
|
+
});
|
|
5144
|
+
}
|
|
4995
5145
|
}
|
|
4996
5146
|
sendJson(response, 404, { error: "Not found." });
|
|
4997
5147
|
})().catch((error) => {
|
|
@@ -5125,6 +5275,9 @@ var DefinedAgent = class {
|
|
|
5125
5275
|
abort(sandboxId, turnId, signal) {
|
|
5126
5276
|
return this.agent.abort(sandboxId, turnId, signal);
|
|
5127
5277
|
}
|
|
5278
|
+
steer(sandboxId, turnId, steer, signal) {
|
|
5279
|
+
return this.agent.steer(sandboxId, turnId, steer, signal);
|
|
5280
|
+
}
|
|
5128
5281
|
health(sandboxId, signal) {
|
|
5129
5282
|
return this.agent.health(sandboxId, signal);
|
|
5130
5283
|
}
|
|
@@ -5214,6 +5367,8 @@ async function createPiSession(config) {
|
|
|
5214
5367
|
subscribe: (listener) => session.subscribe(listener),
|
|
5215
5368
|
prompt: (text) => session.prompt(text),
|
|
5216
5369
|
abort: () => session.abort(),
|
|
5370
|
+
steer: (text) => session.steer(text),
|
|
5371
|
+
clearQueue: () => session.clearQueue(),
|
|
5217
5372
|
afterTurn: () => braintrust.flush()
|
|
5218
5373
|
};
|
|
5219
5374
|
}
|
package/dist/cli.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
import {
|
|
3
3
|
buildAgent,
|
|
4
4
|
pushAgent
|
|
5
|
-
} from "./chunk-
|
|
6
|
-
import "./chunk-
|
|
5
|
+
} from "./chunk-FSBTLBB5.js";
|
|
6
|
+
import "./chunk-BPXEIXPM.js";
|
|
7
7
|
import "./chunk-6P5I7ZCH.js";
|
|
8
|
-
import "./chunk-
|
|
8
|
+
import "./chunk-YFG4EKZM.js";
|
|
9
9
|
import "./chunk-KG6SJM74.js";
|
|
10
10
|
import "./chunk-33IXN6MM.js";
|
|
11
11
|
|