@moinulmoin/eden-runtime-cloudflare 0.1.0

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.
Files changed (48) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +42 -0
  3. package/dist/artifact-runtime.d.ts +27 -0
  4. package/dist/artifact-runtime.js +14 -0
  5. package/dist/eden-eve-host-worker.mjs +1960 -0
  6. package/dist/eve-host-runtime.d.ts +46 -0
  7. package/dist/eve-host-runtime.js +127 -0
  8. package/dist/eve-host.d.ts +166 -0
  9. package/dist/eve-host.js +495 -0
  10. package/dist/http-host.d.ts +6 -0
  11. package/dist/http-host.js +419 -0
  12. package/dist/index.d.ts +21 -0
  13. package/dist/index.js +7 -0
  14. package/dist/model-adapter-internal.d.ts +12 -0
  15. package/dist/model-adapter-internal.js +172 -0
  16. package/dist/model-adapter.d.ts +9 -0
  17. package/dist/model-adapter.js +56 -0
  18. package/dist/model-contracts.d.ts +92 -0
  19. package/dist/model-contracts.js +2 -0
  20. package/dist/model-normalizers.d.ts +7 -0
  21. package/dist/model-normalizers.js +453 -0
  22. package/dist/session-checkpoint.d.ts +48 -0
  23. package/dist/session-checkpoint.js +300 -0
  24. package/dist/session-identity.d.ts +9 -0
  25. package/dist/session-identity.js +45 -0
  26. package/dist/session-jobs.d.ts +69 -0
  27. package/dist/session-jobs.js +427 -0
  28. package/dist/session-journal.d.ts +154 -0
  29. package/dist/session-journal.js +391 -0
  30. package/dist/session-schema.d.ts +21 -0
  31. package/dist/session-schema.js +247 -0
  32. package/dist/session-state.d.ts +165 -0
  33. package/dist/session-state.js +168 -0
  34. package/dist/session.d.ts +40 -0
  35. package/dist/session.js +792 -0
  36. package/dist/test-execution.d.ts +8 -0
  37. package/dist/test-execution.js +12 -0
  38. package/dist/test-worker.d.ts +12 -0
  39. package/dist/test-worker.js +214 -0
  40. package/dist/tool-harness.d.ts +31 -0
  41. package/dist/tool-harness.js +226 -0
  42. package/dist/turn-runner-storage.d.ts +28 -0
  43. package/dist/turn-runner-storage.js +583 -0
  44. package/dist/turn-runner-types.d.ts +112 -0
  45. package/dist/turn-runner-types.js +21 -0
  46. package/dist/turn-runner.d.ts +6 -0
  47. package/dist/turn-runner.js +370 -0
  48. package/package.json +51 -0
@@ -0,0 +1,792 @@
1
+ import { DurableObject } from "cloudflare:workers";
2
+ import { readConfiguredEdenArtifact } from "./artifact-runtime.js";
3
+ import { createWorkersAIModelAdapter } from "./model-adapter-internal.js";
4
+ import { readConfiguredEdenTestModel } from "./test-execution.js";
5
+ import { applySessionMigrations, readAppliedSessionSchemaVersion, SESSION_SCHEMA_TABLES, } from "./session-schema.js";
6
+ import { isOpaqueMessageId, isOpaqueTurnId, sessionIdFromObjectName, } from "./session-identity.js";
7
+ import { commitSessionTransaction, readJournalEventsPage, readLatestJournalCursor, } from "./session-journal.js";
8
+ import { enqueueRecoveryJob, inspectRecoveryJobs, MAX_RECOVERY_JOBS_PER_ALARM, nextRecoveryJobDueAt, processRecoveryJobs, quarantineLegacyCompletedUnknownActions, recoverRecoveryJob, } from "./session-jobs.js";
9
+ import { runBoundedTurn } from "./turn-runner.js";
10
+ const BOUNDED_TURN_JOB_KIND = "bounded-turn";
11
+ const BOUNDED_TURN_RECOVERY_ACTION = "run-bounded-turn";
12
+ const BOUNDED_TURN_START_DELAY_MS = 100;
13
+ const MAX_EVENT_LINE_BYTES = 131_072;
14
+ function configuredTurn() {
15
+ const configured = readConfiguredEdenArtifact();
16
+ if (configured === undefined)
17
+ return undefined;
18
+ const toolName = configured.generation.toolNames[0];
19
+ if (toolName === undefined)
20
+ return undefined;
21
+ const tool = configured.artifact.tools[toolName];
22
+ if (tool === undefined)
23
+ return undefined;
24
+ const toolInputSchema = configured.artifact.toolSchemas[toolName];
25
+ if (toolInputSchema === undefined)
26
+ return undefined;
27
+ return {
28
+ agent: configured.artifact.agent,
29
+ instructions: configured.artifact.instructions,
30
+ generation: configured.generation,
31
+ toolName,
32
+ tool,
33
+ toolInputSchema,
34
+ bundleIdentity: configured.generation.generationId,
35
+ };
36
+ }
37
+ function isTerminalEvent(type) {
38
+ return type === "session.waiting" || type === "session.failed";
39
+ }
40
+ function eventLine(event) {
41
+ const serialized = `${JSON.stringify(event)}\n`;
42
+ const bytes = new TextEncoder().encode(serialized);
43
+ if (bytes.byteLength > MAX_EVENT_LINE_BYTES) {
44
+ throw new Error("Stream event exceeds the 128 KiB limit");
45
+ }
46
+ return bytes;
47
+ }
48
+ function delay(milliseconds) {
49
+ return new Promise((resolve) => {
50
+ setTimeout(resolve, milliseconds);
51
+ });
52
+ }
53
+ function jsonResponse(body, status = 200) {
54
+ return new Response(JSON.stringify(body), {
55
+ status,
56
+ headers: { "content-type": "application/json; charset=utf-8" },
57
+ });
58
+ }
59
+ function parseObject(value) {
60
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
61
+ return null;
62
+ }
63
+ return value;
64
+ }
65
+ function isVersionSet(value) {
66
+ const object = parseObject(value);
67
+ return (object !== null &&
68
+ typeof object.runtime === "string" &&
69
+ typeof object.agentBundle === "string" &&
70
+ typeof object.manifest === "string" &&
71
+ typeof object.protocol === "string" &&
72
+ typeof object.schema === "number" &&
73
+ Number.isSafeInteger(object.schema));
74
+ }
75
+ function versionResponse(row, installedSchemaVersion) {
76
+ return {
77
+ sessionId: row.session_id,
78
+ ownerPrincipal: row.owner_principal,
79
+ status: row.status,
80
+ runtimeVersion: row.runtime_version,
81
+ agentBundleVersion: row.agent_bundle_version,
82
+ manifestVersion: row.manifest_version,
83
+ protocolVersion: row.protocol_version,
84
+ schemaVersion: installedSchemaVersion,
85
+ artifactSchemaVersion: row.artifact_schema_version,
86
+ };
87
+ }
88
+ function artifactVersionsFromMeta(row) {
89
+ return {
90
+ runtime: row.runtime_version,
91
+ agentBundle: row.agent_bundle_version,
92
+ manifest: row.manifest_version,
93
+ protocol: row.protocol_version,
94
+ schema: row.artifact_schema_version,
95
+ };
96
+ }
97
+ function readSessionMeta(sql) {
98
+ const rows = sql
99
+ .exec(`SELECT session_id, owner_principal, status, runtime_version,
100
+ agent_bundle_version, manifest_version, protocol_version, schema_version,
101
+ artifact_schema_version
102
+ FROM session_meta
103
+ LIMIT 1`)
104
+ .toArray();
105
+ return rows[0] ?? null;
106
+ }
107
+ function readTables(sql) {
108
+ const placeholders = SESSION_SCHEMA_TABLES.map(() => "?").join(", ");
109
+ const rows = sql
110
+ .exec(`SELECT name FROM sqlite_master
111
+ WHERE type = 'table' AND name IN (${placeholders})
112
+ ORDER BY name`, ...SESSION_SCHEMA_TABLES)
113
+ .toArray();
114
+ return rows.map((row) => row.name);
115
+ }
116
+ function readMigrations(sql) {
117
+ return sql
118
+ .exec("SELECT version, name FROM schema_migrations ORDER BY version")
119
+ .toArray();
120
+ }
121
+ function readInitialization(request) {
122
+ return request
123
+ .json()
124
+ .then((value) => {
125
+ const object = parseObject(value);
126
+ if (object === null ||
127
+ typeof object.sessionId !== "string" ||
128
+ typeof object.ownerPrincipal !== "string" ||
129
+ !isVersionSet(object.versions)) {
130
+ return null;
131
+ }
132
+ return {
133
+ sessionId: object.sessionId,
134
+ ownerPrincipal: object.ownerPrincipal,
135
+ versions: object.versions,
136
+ };
137
+ })
138
+ .catch(() => null);
139
+ }
140
+ function readJsonObject(request) {
141
+ return request
142
+ .json()
143
+ .then(parseObject)
144
+ .catch(() => null);
145
+ }
146
+ function readEventStreamRequest(request) {
147
+ return readJsonObject(request).then((body) => {
148
+ if (body === null ||
149
+ !hasOnlyKeys(body, [
150
+ "sessionId",
151
+ "ownerPrincipal",
152
+ "startIndex",
153
+ "follow",
154
+ ]) ||
155
+ typeof body.sessionId !== "string" ||
156
+ typeof body.ownerPrincipal !== "string" ||
157
+ typeof body.startIndex !== "number" ||
158
+ !Number.isSafeInteger(body.startIndex) ||
159
+ body.startIndex < 0 ||
160
+ typeof body.follow !== "boolean") {
161
+ return null;
162
+ }
163
+ return {
164
+ sessionId: body.sessionId,
165
+ ownerPrincipal: body.ownerPrincipal,
166
+ startIndex: body.startIndex,
167
+ follow: body.follow,
168
+ };
169
+ });
170
+ }
171
+ function hasOnlyKeys(value, keys) {
172
+ const allowed = new Set(keys);
173
+ return Object.keys(value).every((key) => allowed.has(key));
174
+ }
175
+ function readOwnerPrincipal(body) {
176
+ return typeof body.ownerPrincipal === "string"
177
+ ? body.ownerPrincipal
178
+ : null;
179
+ }
180
+ function isRecoveryJobInput(value) {
181
+ const object = parseObject(value);
182
+ return (object !== null &&
183
+ typeof object.jobId === "string" &&
184
+ typeof object.kind === "string" &&
185
+ typeof object.dueAt === "number" &&
186
+ typeof object.recoveryAction === "string" &&
187
+ (object.maxAttempts === undefined || typeof object.maxAttempts === "number"));
188
+ }
189
+ function jsonJobResponse(body, status = 200) {
190
+ return jsonResponse(body, status);
191
+ }
192
+ export class EdenSession extends DurableObject {
193
+ initialized;
194
+ constructor(ctx, env) {
195
+ super(ctx, env);
196
+ this.initialized = ctx.blockConcurrencyWhile(async () => {
197
+ applySessionMigrations(ctx.storage);
198
+ const sessionId = sessionIdFromObjectName(ctx.id.name ?? "");
199
+ if (sessionId !== null) {
200
+ quarantineLegacyCompletedUnknownActions(ctx.storage, sessionId);
201
+ }
202
+ });
203
+ }
204
+ async fetch(request) {
205
+ await this.initialized;
206
+ const url = new URL(request.url);
207
+ if (url.pathname === "/_eden/schema" && request.method === "GET") {
208
+ return this.schemaResponse();
209
+ }
210
+ if (url.pathname === "/_eden/events" && request.method === "GET") {
211
+ return this.eventsResponse(url);
212
+ }
213
+ if (url.pathname === "/_eden/read-events" &&
214
+ request.method === "POST") {
215
+ return this.readEventsResponse(request);
216
+ }
217
+ if (url.pathname === "/_eden/stream" && request.method === "POST") {
218
+ return this.streamEventsResponse(request);
219
+ }
220
+ if (url.pathname === "/_eden/accept" && request.method === "POST") {
221
+ return this.acceptCommandResponse(request);
222
+ }
223
+ if (url.pathname === "/_eden/jobs" && request.method === "GET") {
224
+ return this.recoveryJobsResponse(url);
225
+ }
226
+ if (url.pathname === "/_eden/jobs" && request.method === "POST") {
227
+ return this.enqueueRecoveryJobResponse(request);
228
+ }
229
+ const recoverPrefix = "/_eden/jobs/";
230
+ if (url.pathname.startsWith(recoverPrefix) &&
231
+ url.pathname.endsWith("/recover") &&
232
+ request.method === "POST") {
233
+ let jobId;
234
+ try {
235
+ jobId = decodeURIComponent(url.pathname.slice(recoverPrefix.length, -"/recover".length));
236
+ }
237
+ catch {
238
+ return jsonResponse({ code: "invalid_recovery_operation", message: "Invalid recovery operation" }, 400);
239
+ }
240
+ return this.recoverRecoveryJobResponse(request, jobId);
241
+ }
242
+ if (url.pathname === "/_eden/initialize" &&
243
+ request.method === "POST") {
244
+ return this.initialize(request);
245
+ }
246
+ return jsonResponse({ code: "not_found", message: "Not found" }, 404);
247
+ }
248
+ async alarm() {
249
+ await this.initialized;
250
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
251
+ if (sessionId === null)
252
+ return;
253
+ try {
254
+ await processRecoveryJobs(this.ctx.storage, sessionId, async (job) => this.executeRecoveryJob(job), {
255
+ limit: MAX_RECOVERY_JOBS_PER_ALARM,
256
+ });
257
+ }
258
+ finally {
259
+ await this.rearmRecoveryAlarm(sessionId);
260
+ }
261
+ }
262
+ async enqueueRecoveryJob(input) {
263
+ await this.initialized;
264
+ const sessionId = this.requireSessionId();
265
+ const result = enqueueRecoveryJob(this.ctx.storage, sessionId, input);
266
+ await this.rearmRecoveryAlarm(sessionId);
267
+ return result;
268
+ }
269
+ inspectRecoveryJobs(options = {}) {
270
+ const sessionId = this.requireSessionId();
271
+ return inspectRecoveryJobs(this.ctx.storage, sessionId, options);
272
+ }
273
+ async recoverRecoveryJob(jobId, input = {}) {
274
+ await this.initialized;
275
+ const sessionId = this.requireSessionId();
276
+ const result = recoverRecoveryJob(this.ctx.storage, sessionId, jobId, input.recoveryAction, input.dueAt);
277
+ await this.rearmRecoveryAlarm(sessionId);
278
+ return result;
279
+ }
280
+ schemaResponse() {
281
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
282
+ if (sessionId === null) {
283
+ return jsonResponse({ code: "session_mapping_invalid", message: "Session mapping unavailable" }, 500);
284
+ }
285
+ const sql = this.ctx.storage.sql;
286
+ const meta = readSessionMeta(sql);
287
+ const installedSchemaVersion = readAppliedSessionSchemaVersion(sql);
288
+ return jsonResponse({
289
+ sessionId,
290
+ tables: readTables(sql),
291
+ appliedMigrations: readMigrations(sql),
292
+ installedSchemaVersion,
293
+ sessionMeta: meta === null ? null : versionResponse(meta, installedSchemaVersion),
294
+ });
295
+ }
296
+ eventsResponse(url) {
297
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
298
+ if (sessionId === null) {
299
+ return jsonResponse({ code: "session_mapping_invalid", message: "Session mapping unavailable" }, 500);
300
+ }
301
+ const rawStartIndex = url.searchParams.get("startIndex") ?? "0";
302
+ const startIndex = Number(rawStartIndex);
303
+ if (!Number.isSafeInteger(startIndex) ||
304
+ startIndex < 0 ||
305
+ rawStartIndex !== String(startIndex)) {
306
+ return jsonResponse({ code: "invalid_start_index", message: "Invalid event start index" }, 400);
307
+ }
308
+ const latestCursor = readLatestJournalCursor(this.ctx.storage.sql, sessionId);
309
+ const events = readJournalEventsPage(this.ctx.storage.sql, sessionId, startIndex, { endIndex: latestCursor });
310
+ return jsonResponse({
311
+ sessionId,
312
+ startIndex,
313
+ latestCursor,
314
+ nextCursor: events.at(-1)?.streamIndex !== undefined &&
315
+ (events.at(-1)?.streamIndex ?? latestCursor) < latestCursor
316
+ ? events.at(-1)?.streamIndex ?? null
317
+ : null,
318
+ events,
319
+ });
320
+ }
321
+ async initialize(request) {
322
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
323
+ const input = await readInitialization(request);
324
+ if (sessionId === null || input === null || input.sessionId !== sessionId) {
325
+ return jsonResponse({ code: "invalid_session_initialization", message: "Invalid session initialization" }, 400);
326
+ }
327
+ const sql = this.ctx.storage.sql;
328
+ const installedSchemaVersion = readAppliedSessionSchemaVersion(sql);
329
+ const existing = readSessionMeta(sql);
330
+ if (existing !== null) {
331
+ if (existing.owner_principal !== input.ownerPrincipal ||
332
+ existing.runtime_version !== input.versions.runtime ||
333
+ existing.agent_bundle_version !== input.versions.agentBundle ||
334
+ existing.manifest_version !== input.versions.manifest ||
335
+ existing.protocol_version !== input.versions.protocol ||
336
+ existing.schema_version !== installedSchemaVersion ||
337
+ existing.artifact_schema_version !== input.versions.schema) {
338
+ return jsonResponse({ code: "session_initialization_conflict", message: "Session already initialized" }, 409);
339
+ }
340
+ return jsonResponse({
341
+ sessionId,
342
+ status: existing.status,
343
+ versions: artifactVersionsFromMeta(existing),
344
+ });
345
+ }
346
+ const timestamp = new Date().toISOString();
347
+ try {
348
+ commitSessionTransaction(this.ctx.storage, sessionId, (journal) => {
349
+ sql.exec(`INSERT INTO session_meta (
350
+ session_id, owner_principal, status, created_at, updated_at,
351
+ runtime_version, agent_bundle_version, manifest_version,
352
+ protocol_version, schema_version, artifact_schema_version
353
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, sessionId, input.ownerPrincipal, "new", timestamp, timestamp, input.versions.runtime, input.versions.agentBundle, input.versions.manifest, input.versions.protocol, installedSchemaVersion, input.versions.schema);
354
+ journal.appendEvent({
355
+ type: "session.started",
356
+ data: {
357
+ sessionId,
358
+ status: "new",
359
+ versions: input.versions,
360
+ },
361
+ committedAt: timestamp,
362
+ });
363
+ });
364
+ }
365
+ catch (error) {
366
+ const durable = readSessionMeta(sql);
367
+ if (durable !== null) {
368
+ return jsonResponse({
369
+ sessionId,
370
+ status: durable.status,
371
+ versions: artifactVersionsFromMeta(durable),
372
+ });
373
+ }
374
+ throw error;
375
+ }
376
+ return jsonResponse({
377
+ sessionId,
378
+ status: "new",
379
+ versions: input.versions,
380
+ }, 201);
381
+ }
382
+ async acceptCommandResponse(request) {
383
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
384
+ const body = await readJsonObject(request);
385
+ if (sessionId === null ||
386
+ body === null ||
387
+ !hasOnlyKeys(body, [
388
+ "sessionId",
389
+ "ownerPrincipal",
390
+ "turnId",
391
+ "messageId",
392
+ "message",
393
+ ]) ||
394
+ body.sessionId !== sessionId ||
395
+ readOwnerPrincipal(body) === null ||
396
+ typeof body.turnId !== "string" ||
397
+ !isOpaqueTurnId(body.turnId) ||
398
+ typeof body.messageId !== "string" ||
399
+ !isOpaqueMessageId(body.messageId) ||
400
+ typeof body.message !== "string") {
401
+ return jsonResponse({ code: "invalid_command", message: "Invalid session command" }, 400);
402
+ }
403
+ const message = body.message;
404
+ if (message.trim().length === 0 ||
405
+ new TextEncoder().encode(message).byteLength > 16 * 1024) {
406
+ return jsonResponse({ code: "invalid_command", message: "Invalid session command" }, 400);
407
+ }
408
+ const sql = this.ctx.storage.sql;
409
+ const meta = readSessionMeta(sql);
410
+ if (meta === null || meta.owner_principal !== body.ownerPrincipal) {
411
+ return jsonResponse({ code: "session_not_found", message: "Session was not found" }, 404);
412
+ }
413
+ const existing = sql
414
+ .exec(`SELECT turns.turn_id, turns.status, messages.message_id,
415
+ messages.content AS message
416
+ FROM turns
417
+ LEFT JOIN messages
418
+ ON messages.turn_id = turns.turn_id
419
+ AND messages.role = 'user'
420
+ WHERE turns.session_id = ? AND turns.turn_id = ?
421
+ LIMIT 1`, sessionId, body.turnId)
422
+ .toArray()[0];
423
+ if (existing !== undefined) {
424
+ if (existing.status === "accepted" &&
425
+ existing.message_id === body.messageId &&
426
+ existing.message === message) {
427
+ return jsonResponse({
428
+ sessionId,
429
+ turnId: body.turnId,
430
+ messageId: body.messageId,
431
+ status: "accepted",
432
+ }, 202);
433
+ }
434
+ return jsonResponse({ code: "session_conflict", message: "Session command conflicts with durable state" }, 409);
435
+ }
436
+ const timestamp = new Date().toISOString();
437
+ try {
438
+ commitSessionTransaction(this.ctx.storage, sessionId, (journal) => {
439
+ journal.insertTurn({
440
+ turnId: body.turnId,
441
+ status: "accepted",
442
+ acceptedAt: timestamp,
443
+ });
444
+ journal.insertMessage({
445
+ messageId: body.messageId,
446
+ turnId: body.turnId,
447
+ role: "user",
448
+ content: message,
449
+ createdAt: timestamp,
450
+ });
451
+ journal.setSessionStatus("running", timestamp);
452
+ journal.appendEvent({
453
+ type: "turn.started",
454
+ turnId: body.turnId,
455
+ data: { turnId: body.turnId },
456
+ committedAt: timestamp,
457
+ });
458
+ journal.appendEvent({
459
+ type: "message.received",
460
+ turnId: body.turnId,
461
+ data: {
462
+ messageId: body.messageId,
463
+ role: "user",
464
+ },
465
+ committedAt: timestamp,
466
+ });
467
+ journal.insertJob({
468
+ jobId: `turn:${body.turnId}`,
469
+ kind: BOUNDED_TURN_JOB_KIND,
470
+ dueAt: Date.now() + BOUNDED_TURN_START_DELAY_MS,
471
+ recoveryAction: BOUNDED_TURN_RECOVERY_ACTION,
472
+ createdAt: timestamp,
473
+ });
474
+ });
475
+ }
476
+ catch {
477
+ const durable = sql
478
+ .exec(`SELECT turns.status, messages.message_id,
479
+ messages.content AS message
480
+ FROM turns
481
+ LEFT JOIN messages
482
+ ON messages.turn_id = turns.turn_id
483
+ AND messages.role = 'user'
484
+ WHERE turns.session_id = ? AND turns.turn_id = ?
485
+ LIMIT 1`, sessionId, body.turnId)
486
+ .toArray()[0];
487
+ if (durable?.status === "accepted" &&
488
+ durable.message_id === body.messageId &&
489
+ durable.message === message) {
490
+ return jsonResponse({
491
+ sessionId,
492
+ turnId: body.turnId,
493
+ messageId: body.messageId,
494
+ status: "accepted",
495
+ }, 202);
496
+ }
497
+ return jsonResponse({ code: "session_conflict", message: "Session command could not be accepted" }, 409);
498
+ }
499
+ await this.rearmRecoveryAlarm(sessionId);
500
+ return jsonResponse({
501
+ sessionId,
502
+ turnId: body.turnId,
503
+ messageId: body.messageId,
504
+ status: "accepted",
505
+ }, 202);
506
+ }
507
+ async readEventsResponse(request) {
508
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
509
+ const body = await readJsonObject(request);
510
+ if (sessionId === null ||
511
+ body === null ||
512
+ !hasOnlyKeys(body, [
513
+ "sessionId",
514
+ "ownerPrincipal",
515
+ "startIndex",
516
+ "follow",
517
+ ]) ||
518
+ body.sessionId !== sessionId ||
519
+ readOwnerPrincipal(body) === null ||
520
+ typeof body.startIndex !== "number" ||
521
+ !Number.isSafeInteger(body.startIndex) ||
522
+ body.startIndex < 0 ||
523
+ typeof body.follow !== "boolean") {
524
+ return jsonResponse({ code: "invalid_event_read", message: "Invalid event read" }, 400);
525
+ }
526
+ const meta = readSessionMeta(this.ctx.storage.sql);
527
+ if (meta === null || meta.owner_principal !== body.ownerPrincipal) {
528
+ return jsonResponse({ code: "session_not_found", message: "Session was not found" }, 404);
529
+ }
530
+ const latestCursor = readLatestJournalCursor(this.ctx.storage.sql, sessionId);
531
+ const events = readJournalEventsPage(this.ctx.storage.sql, sessionId, body.startIndex, { endIndex: latestCursor });
532
+ return jsonResponse({
533
+ sessionId,
534
+ startIndex: body.startIndex,
535
+ latestCursor,
536
+ nextCursor: events.at(-1)?.streamIndex !== undefined &&
537
+ (events.at(-1)?.streamIndex ?? latestCursor) < latestCursor
538
+ ? events.at(-1)?.streamIndex ?? null
539
+ : null,
540
+ events,
541
+ });
542
+ }
543
+ async streamEventsResponse(request) {
544
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
545
+ const body = await readEventStreamRequest(request);
546
+ if (sessionId === null ||
547
+ body === null ||
548
+ body.sessionId !== sessionId) {
549
+ return jsonResponse({ code: "invalid_event_read", message: "Invalid event read" }, 400);
550
+ }
551
+ const meta = readSessionMeta(this.ctx.storage.sql);
552
+ if (meta === null || meta.owner_principal !== body.ownerPrincipal) {
553
+ return jsonResponse({ code: "session_not_found", message: "Session was not found" }, 404);
554
+ }
555
+ const sql = this.ctx.storage.sql;
556
+ const highWater = body.follow ? null : readLatestJournalCursor(sql, sessionId);
557
+ let cursor = body.startIndex;
558
+ let cancelled = false;
559
+ const stream = new ReadableStream({
560
+ start: (controller) => {
561
+ const pump = async () => {
562
+ try {
563
+ while (!cancelled) {
564
+ const events = readJournalEventsPage(sql, sessionId, cursor, {
565
+ ...(highWater === null ? {} : { endIndex: highWater }),
566
+ });
567
+ let emitted = false;
568
+ for (const event of events) {
569
+ controller.enqueue(eventLine(event));
570
+ cursor = event.streamIndex;
571
+ emitted = true;
572
+ if (body.follow && isTerminalEvent(event.type)) {
573
+ controller.close();
574
+ return;
575
+ }
576
+ }
577
+ if (!body.follow) {
578
+ if (cursor >= (highWater ?? cursor)) {
579
+ controller.close();
580
+ return;
581
+ }
582
+ }
583
+ else if (!emitted) {
584
+ const current = readSessionMeta(sql);
585
+ if (current?.status === "waiting" ||
586
+ current?.status === "failed" ||
587
+ current?.status === "completed") {
588
+ controller.close();
589
+ return;
590
+ }
591
+ }
592
+ await delay(10);
593
+ }
594
+ }
595
+ catch (error) {
596
+ if (!cancelled)
597
+ controller.error(error);
598
+ }
599
+ };
600
+ this.ctx.waitUntil(pump());
601
+ },
602
+ cancel: () => {
603
+ cancelled = true;
604
+ },
605
+ });
606
+ return new Response(stream, {
607
+ status: 200,
608
+ headers: {
609
+ "content-type": "application/x-ndjson; charset=utf-8",
610
+ "cache-control": "no-store",
611
+ },
612
+ });
613
+ }
614
+ requireSessionId() {
615
+ const sessionId = sessionIdFromObjectName(this.ctx.id.name ?? "");
616
+ if (sessionId === null) {
617
+ throw new Error("Session mapping unavailable");
618
+ }
619
+ return sessionId;
620
+ }
621
+ async recoveryJobsResponse(url) {
622
+ const allowed = new Set(["cursor", "limit"]);
623
+ let hasUnknownQuery = false;
624
+ url.searchParams.forEach((_value, key) => {
625
+ if (!allowed.has(key))
626
+ hasUnknownQuery = true;
627
+ });
628
+ if (hasUnknownQuery) {
629
+ return jsonResponse({ code: "invalid_recovery_inspection", message: "Invalid recovery inspection" }, 400);
630
+ }
631
+ const rawLimit = url.searchParams.get("limit");
632
+ const rawCursor = url.searchParams.get("cursor");
633
+ let limitValue;
634
+ let cursorValue;
635
+ if (rawLimit !== null) {
636
+ const limit = Number(rawLimit);
637
+ if (!Number.isSafeInteger(limit) ||
638
+ limit < 1 ||
639
+ rawLimit !== String(limit)) {
640
+ return jsonResponse({ code: "invalid_recovery_inspection", message: "Invalid recovery inspection" }, 400);
641
+ }
642
+ limitValue = limit;
643
+ }
644
+ if (rawCursor !== null) {
645
+ if (rawCursor.length === 0) {
646
+ return jsonResponse({ code: "invalid_recovery_inspection", message: "Invalid recovery inspection" }, 400);
647
+ }
648
+ cursorValue = rawCursor;
649
+ }
650
+ try {
651
+ const options = {
652
+ ...(limitValue === undefined ? {} : { limit: limitValue }),
653
+ ...(cursorValue === undefined ? {} : { cursor: cursorValue }),
654
+ };
655
+ return jsonResponse(this.inspectRecoveryJobs(options));
656
+ }
657
+ catch {
658
+ return jsonResponse({ code: "invalid_recovery_inspection", message: "Invalid recovery inspection" }, 400);
659
+ }
660
+ }
661
+ async enqueueRecoveryJobResponse(request) {
662
+ const body = await readJsonObject(request);
663
+ if (!isRecoveryJobInput(body)) {
664
+ return jsonResponse({ code: "invalid_recovery_job", message: "Invalid recovery job" }, 400);
665
+ }
666
+ try {
667
+ const result = await this.enqueueRecoveryJob(body);
668
+ return jsonJobResponse(result, result.status === "scheduled" ? 201 : 200);
669
+ }
670
+ catch {
671
+ return jsonResponse({
672
+ code: "invalid_recovery_job",
673
+ message: "Invalid recovery job",
674
+ }, 400);
675
+ }
676
+ }
677
+ async recoverRecoveryJobResponse(request, jobId) {
678
+ const body = await readJsonObject(request);
679
+ if (body === null) {
680
+ return jsonResponse({ code: "invalid_recovery_operation", message: "Invalid recovery operation" }, 400);
681
+ }
682
+ if (body.recoveryAction !== undefined &&
683
+ typeof body.recoveryAction !== "string") {
684
+ return jsonResponse({ code: "invalid_recovery_operation", message: "Invalid recovery operation" }, 400);
685
+ }
686
+ if (body.dueAt !== undefined &&
687
+ (typeof body.dueAt !== "number" || !Number.isSafeInteger(body.dueAt))) {
688
+ return jsonResponse({ code: "invalid_recovery_operation", message: "Invalid recovery operation" }, 400);
689
+ }
690
+ try {
691
+ const result = await this.recoverRecoveryJob(jobId, {
692
+ ...(body.recoveryAction === undefined
693
+ ? {}
694
+ : { recoveryAction: body.recoveryAction }),
695
+ ...(body.dueAt === undefined ? {} : { dueAt: body.dueAt }),
696
+ });
697
+ return jsonJobResponse(result);
698
+ }
699
+ catch {
700
+ return jsonResponse({
701
+ code: "invalid_recovery_operation",
702
+ message: "Invalid recovery operation",
703
+ }, 400);
704
+ }
705
+ }
706
+ async rearmRecoveryAlarm(sessionId) {
707
+ const nextDueAt = nextRecoveryJobDueAt(this.ctx.storage.sql, sessionId);
708
+ if (nextDueAt === null) {
709
+ await this.ctx.storage.deleteAlarm();
710
+ return;
711
+ }
712
+ await this.ctx.storage.setAlarm(nextDueAt);
713
+ }
714
+ async executeRecoveryJob(job) {
715
+ switch (job.recoveryAction) {
716
+ case "mark-complete":
717
+ if (job.kind === BOUNDED_TURN_JOB_KIND) {
718
+ throw new Error("Bounded turn recovery action is invalid");
719
+ }
720
+ return;
721
+ case "always-fail":
722
+ case "fail":
723
+ if (job.kind === BOUNDED_TURN_JOB_KIND) {
724
+ throw new Error("Bounded turn recovery action is invalid");
725
+ }
726
+ throw new Error("Configured recovery action failed");
727
+ case BOUNDED_TURN_RECOVERY_ACTION:
728
+ if (job.kind !== BOUNDED_TURN_JOB_KIND) {
729
+ throw new Error("Bounded turn recovery action is invalid");
730
+ }
731
+ await this.executeBoundedTurnJob(job);
732
+ return;
733
+ default:
734
+ throw new Error("Unknown recovery action");
735
+ }
736
+ }
737
+ async executeBoundedTurnJob(job) {
738
+ const sessionId = this.requireSessionId();
739
+ if (job.recoveryAction !== BOUNDED_TURN_RECOVERY_ACTION) {
740
+ throw new Error("Bounded turn recovery action is invalid");
741
+ }
742
+ const turn = this.ctx.storage.sql
743
+ .exec(`SELECT turns.turn_id, messages.message_id, messages.content AS message
744
+ FROM turns
745
+ INNER JOIN messages
746
+ ON messages.session_id = turns.session_id
747
+ AND messages.turn_id = turns.turn_id
748
+ AND messages.role = 'user'
749
+ WHERE turns.session_id = ? AND turns.turn_id = ?
750
+ LIMIT 1`, sessionId, job.jobId.slice("turn:".length))
751
+ .toArray()[0];
752
+ if (turn === undefined) {
753
+ throw new Error("Bounded turn input is missing");
754
+ }
755
+ const configured = configuredTurn();
756
+ if (configured === undefined) {
757
+ throw new Error("Configured agent artifact is unavailable");
758
+ }
759
+ const runtimeEnvironment = this.env;
760
+ const liveModel = runtimeEnvironment.AI !== undefined
761
+ ? createWorkersAIModelAdapter({
762
+ binding: runtimeEnvironment.AI,
763
+ modelId: configured.agent.model,
764
+ gatewayId: "default",
765
+ })
766
+ : undefined;
767
+ const testModel = configured.generation.executionMode === "local"
768
+ ? readConfiguredEdenTestModel()
769
+ : undefined;
770
+ const model = testModel ?? liveModel;
771
+ if (model === undefined) {
772
+ throw new Error("Configured model binding is unavailable");
773
+ }
774
+ await runBoundedTurn(this.ctx.storage, {
775
+ sessionId,
776
+ turnId: turn.turn_id,
777
+ messageId: turn.message_id,
778
+ message: turn.message,
779
+ model,
780
+ modelId: configured.agent.model,
781
+ ...(configured.agent.options === undefined
782
+ ? {}
783
+ : { modelOptions: configured.agent.options }),
784
+ systemPrompt: configured.instructions,
785
+ toolName: configured.toolName,
786
+ tool: configured.tool,
787
+ toolInputSchema: configured.toolInputSchema,
788
+ bundleIdentity: configured.bundleIdentity,
789
+ });
790
+ }
791
+ }
792
+ //# sourceMappingURL=session.js.map