@moinulmoin/eden-runtime-cloudflare 0.1.4 → 0.2.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 (47) hide show
  1. package/NOTICE +0 -31
  2. package/dist/eden-eve-host-worker.mjs +1 -9
  3. package/dist/eve-host-runtime.d.ts +0 -1
  4. package/dist/eve-host-runtime.js +0 -1
  5. package/dist/eve-host.d.ts +2 -1
  6. package/dist/eve-host.js +2 -8
  7. package/dist/index.d.ts +0 -18
  8. package/dist/index.js +0 -5
  9. package/package.json +2 -6
  10. package/dist/artifact-runtime.d.ts +0 -27
  11. package/dist/artifact-runtime.js +0 -14
  12. package/dist/http-host.d.ts +0 -6
  13. package/dist/http-host.js +0 -419
  14. package/dist/model-adapter-internal.d.ts +0 -12
  15. package/dist/model-adapter-internal.js +0 -172
  16. package/dist/model-adapter.d.ts +0 -9
  17. package/dist/model-adapter.js +0 -56
  18. package/dist/model-contracts.d.ts +0 -92
  19. package/dist/model-contracts.js +0 -2
  20. package/dist/model-normalizers.d.ts +0 -7
  21. package/dist/model-normalizers.js +0 -453
  22. package/dist/session-checkpoint.d.ts +0 -48
  23. package/dist/session-checkpoint.js +0 -300
  24. package/dist/session-identity.d.ts +0 -9
  25. package/dist/session-identity.js +0 -45
  26. package/dist/session-jobs.d.ts +0 -69
  27. package/dist/session-jobs.js +0 -427
  28. package/dist/session-journal.d.ts +0 -154
  29. package/dist/session-journal.js +0 -391
  30. package/dist/session-schema.d.ts +0 -21
  31. package/dist/session-schema.js +0 -247
  32. package/dist/session-state.d.ts +0 -165
  33. package/dist/session-state.js +0 -168
  34. package/dist/session.d.ts +0 -40
  35. package/dist/session.js +0 -792
  36. package/dist/test-execution.d.ts +0 -8
  37. package/dist/test-execution.js +0 -12
  38. package/dist/test-worker.d.ts +0 -12
  39. package/dist/test-worker.js +0 -214
  40. package/dist/tool-harness.d.ts +0 -31
  41. package/dist/tool-harness.js +0 -226
  42. package/dist/turn-runner-storage.d.ts +0 -28
  43. package/dist/turn-runner-storage.js +0 -583
  44. package/dist/turn-runner-types.d.ts +0 -112
  45. package/dist/turn-runner-types.js +0 -21
  46. package/dist/turn-runner.d.ts +0 -6
  47. package/dist/turn-runner.js +0 -370
@@ -1,453 +0,0 @@
1
- /*
2
- * Modified derivative of portable Eve concepts. Eve 0.31.3 reference commit:
3
- * 0b102bc90e7cf2c3e294f6ca3af86c307d449b1a. See repository NOTICE and LICENSE.
4
- */
5
- const MAX_MODEL_STRING_BYTES = 65_536;
6
- const MAX_MODEL_JSON_DEPTH = 32;
7
- const MAX_MODEL_CALLS = 32;
8
- const MAX_NORMALIZED_JSON_BYTES = 65_536;
9
- function isRecord(value) {
10
- return typeof value === "object" && value !== null && !Array.isArray(value);
11
- }
12
- function boundedString(value) {
13
- if (typeof value !== "string")
14
- return undefined;
15
- if (new TextEncoder().encode(value).byteLength > MAX_MODEL_STRING_BYTES) {
16
- return undefined;
17
- }
18
- return value;
19
- }
20
- function safeIdentifier(value) {
21
- const identifier = boundedString(value);
22
- if (identifier === undefined || identifier.trim().length === 0) {
23
- return undefined;
24
- }
25
- return identifier;
26
- }
27
- function normalizeCorrelation(correlation) {
28
- const requestId = safeIdentifier(correlation.requestId) ?? "unknown";
29
- const sessionId = safeIdentifier(correlation.sessionId);
30
- const turnId = safeIdentifier(correlation.turnId);
31
- const stepId = safeIdentifier(correlation.stepId);
32
- return {
33
- requestId,
34
- ...(sessionId === undefined ? {} : { sessionId }),
35
- ...(turnId === undefined ? {} : { turnId }),
36
- ...(stepId === undefined ? {} : { stepId }),
37
- };
38
- }
39
- function normalizeJsonValue(value, depth = 0, active = new WeakSet()) {
40
- if (depth > MAX_MODEL_JSON_DEPTH)
41
- return undefined;
42
- if (value === null)
43
- return null;
44
- if (typeof value === "string" || typeof value === "boolean") {
45
- return boundedString(value) === undefined && typeof value === "string"
46
- ? undefined
47
- : value;
48
- }
49
- if (typeof value === "number") {
50
- return Number.isFinite(value) ? value : undefined;
51
- }
52
- if (Array.isArray(value)) {
53
- if (active.has(value))
54
- return undefined;
55
- active.add(value);
56
- try {
57
- const output = [];
58
- for (const item of value) {
59
- const normalized = normalizeJsonValue(item, depth + 1, active);
60
- if (normalized === undefined)
61
- return undefined;
62
- output.push(normalized);
63
- }
64
- return output;
65
- }
66
- finally {
67
- active.delete(value);
68
- }
69
- }
70
- if (!isRecord(value))
71
- return undefined;
72
- const prototype = Object.getPrototypeOf(value);
73
- if (prototype !== Object.prototype && prototype !== null)
74
- return undefined;
75
- if (active.has(value))
76
- return undefined;
77
- active.add(value);
78
- try {
79
- const output = {};
80
- for (const [key, item] of Object.entries(value)) {
81
- const normalized = normalizeJsonValue(item, depth + 1, active);
82
- if (normalized === undefined)
83
- return undefined;
84
- output[key] = normalized;
85
- }
86
- return output;
87
- }
88
- finally {
89
- active.delete(value);
90
- }
91
- }
92
- function parseJsonValue(value) {
93
- if (typeof value !== "string")
94
- return normalizeJsonValue(value);
95
- try {
96
- return normalizeJsonValue(JSON.parse(value));
97
- }
98
- catch {
99
- return undefined;
100
- }
101
- }
102
- export function normalizeEdenJsonValue(value) {
103
- const normalized = normalizeJsonValue(value);
104
- if (normalized === undefined)
105
- return undefined;
106
- const serialized = JSON.stringify(normalized);
107
- if (serialized === undefined ||
108
- new TextEncoder().encode(serialized).byteLength > MAX_NORMALIZED_JSON_BYTES) {
109
- return undefined;
110
- }
111
- return normalized;
112
- }
113
- function normalizeToolCall(value) {
114
- if (!isRecord(value))
115
- return undefined;
116
- const callId = safeIdentifier(value.callId ?? value.toolCallId ?? value.id);
117
- const toolName = safeIdentifier(value.toolName ?? value.name);
118
- const rawInput = "input" in value
119
- ? value.input
120
- : "args" in value
121
- ? value.args
122
- : value.arguments;
123
- const input = parseJsonValue(rawInput);
124
- if (callId === undefined || toolName === undefined || input === undefined) {
125
- return undefined;
126
- }
127
- return { callId, toolName, input };
128
- }
129
- function normalizeToolOutput(value) {
130
- if (isRecord(value) && typeof value.type === "string") {
131
- if (value.type === "execution-denied") {
132
- return {
133
- error: {
134
- code: "tool_execution_denied",
135
- message: "Tool execution was denied.",
136
- },
137
- };
138
- }
139
- if (value.type === "text") {
140
- const text = boundedString(value.value);
141
- if (text === undefined)
142
- return undefined;
143
- return text;
144
- }
145
- if (value.type === "error-text") {
146
- return {
147
- error: {
148
- code: "tool_result_error",
149
- message: "Tool result was not available.",
150
- },
151
- };
152
- }
153
- if (value.type === "json")
154
- return parseJsonValue(value.value);
155
- if (value.type === "error-json") {
156
- return {
157
- error: {
158
- code: "tool_result_error",
159
- message: "Tool result was not available.",
160
- },
161
- };
162
- }
163
- if (value.type === "content")
164
- return parseJsonValue(value.value);
165
- return undefined;
166
- }
167
- return parseJsonValue(value);
168
- }
169
- function normalizeToolResult(value) {
170
- if (!isRecord(value))
171
- return undefined;
172
- const callId = safeIdentifier(value.callId ?? value.toolCallId ?? value.id);
173
- const toolName = safeIdentifier(value.toolName ?? value.name);
174
- const rawOutput = "output" in value ? value.output : value.result;
175
- const output = normalizeToolOutput(rawOutput);
176
- if (callId === undefined || toolName === undefined || output === undefined) {
177
- return undefined;
178
- }
179
- return { callId, toolName, output };
180
- }
181
- function contentParts(value) {
182
- if (!Array.isArray(value))
183
- return [];
184
- return value.filter(isRecord);
185
- }
186
- function normalizeMessagePart(value) {
187
- if (value.type === "text") {
188
- const text = boundedString(value.text);
189
- return text === undefined ? undefined : { type: "text", text };
190
- }
191
- if (value.type === "tool-call") {
192
- const call = normalizeToolCall(value);
193
- return call === undefined ? undefined : { type: "tool-call", ...call };
194
- }
195
- if (value.type === "tool-result") {
196
- const result = normalizeToolResult(value);
197
- return result === undefined ? undefined : { type: "tool-result", ...result };
198
- }
199
- return undefined;
200
- }
201
- function normalizeMessage(value) {
202
- if (!isRecord(value))
203
- return undefined;
204
- const role = value.role;
205
- if (role !== "system" &&
206
- role !== "user" &&
207
- role !== "assistant" &&
208
- role !== "tool") {
209
- return undefined;
210
- }
211
- if (typeof value.content === "string") {
212
- const content = boundedString(value.content);
213
- return content === undefined ? undefined : { role, content };
214
- }
215
- const parts = contentParts(value.content)
216
- .map(normalizeMessagePart)
217
- .filter((part) => part !== undefined)
218
- .slice(0, MAX_MODEL_CALLS);
219
- if (role === "tool") {
220
- return { role, content: parts.filter((part) => part.type === "tool-result") };
221
- }
222
- if (role === "system" || role === "user") {
223
- const text = parts
224
- .filter((part) => part.type === "text")
225
- .map((part) => part.text)
226
- .join("");
227
- return { role, content: text };
228
- }
229
- return { role, content: parts };
230
- }
231
- export function normalizeModelMessages(messages) {
232
- return messages
233
- .map(normalizeMessage)
234
- .filter((message) => message !== undefined);
235
- }
236
- function normalizeUsage(value) {
237
- if (!isRecord(value))
238
- return undefined;
239
- const inputTokens = safeTokenCount(value.inputTokens) ??
240
- safeTokenCount(value.promptTokens) ??
241
- safeTokenCount(value.inputTokenCount);
242
- const outputTokens = safeTokenCount(value.outputTokens) ??
243
- safeTokenCount(value.completionTokens) ??
244
- safeTokenCount(value.outputTokenCount);
245
- const totalTokens = safeTokenCount(value.totalTokens) ??
246
- safeTokenCount(value.totalTokenCount) ??
247
- (inputTokens !== undefined && outputTokens !== undefined
248
- ? inputTokens + outputTokens
249
- : undefined);
250
- if (inputTokens === undefined &&
251
- outputTokens === undefined &&
252
- totalTokens === undefined) {
253
- return undefined;
254
- }
255
- return {
256
- ...(inputTokens === undefined ? {} : { inputTokens }),
257
- ...(outputTokens === undefined ? {} : { outputTokens }),
258
- ...(totalTokens === undefined ? {} : { totalTokens }),
259
- };
260
- }
261
- function safeTokenCount(value) {
262
- return typeof value === "number" &&
263
- Number.isSafeInteger(value) &&
264
- value >= 0
265
- ? value
266
- : undefined;
267
- }
268
- function normalizeFinishReason(value, hasCalls, hasText) {
269
- const rawValue = typeof value === "string"
270
- ? value
271
- : isRecord(value) && typeof value.unified === "string"
272
- ? value.unified
273
- : undefined;
274
- const raw = rawValue === "tool_calls"
275
- ? "tool-calls"
276
- : rawValue === "content_filter"
277
- ? "content-filter"
278
- : rawValue === "max_tokens"
279
- ? "length"
280
- : rawValue;
281
- switch (raw) {
282
- case "stop":
283
- case "length":
284
- case "content-filter":
285
- case "tool-calls":
286
- case "error":
287
- case "other":
288
- return raw;
289
- default:
290
- return hasCalls ? "tool-calls" : hasText ? "stop" : "other";
291
- }
292
- }
293
- function resultContentParts(value) {
294
- return contentParts(value.content);
295
- }
296
- function uniqueCalls(values) {
297
- const seen = new Set();
298
- const unique = [];
299
- for (const value of values) {
300
- if (seen.has(value.callId))
301
- continue;
302
- seen.add(value.callId);
303
- unique.push(value);
304
- if (unique.length >= MAX_MODEL_CALLS)
305
- break;
306
- }
307
- return unique;
308
- }
309
- function uniqueResults(values) {
310
- const seen = new Set();
311
- const unique = [];
312
- for (const value of values) {
313
- if (seen.has(value.callId))
314
- continue;
315
- seen.add(value.callId);
316
- unique.push(value);
317
- if (unique.length >= MAX_MODEL_CALLS)
318
- break;
319
- }
320
- return unique;
321
- }
322
- function normalizedToolCalls(value, field) {
323
- if (value === undefined)
324
- return [];
325
- if (!Array.isArray(value)) {
326
- throw new Error(`Model ${field} is invalid`);
327
- }
328
- return value.map((item) => {
329
- const call = normalizeToolCall(item);
330
- if (call === undefined) {
331
- throw new Error(`Model ${field} contains an invalid tool call`);
332
- }
333
- return call;
334
- });
335
- }
336
- function normalizedToolResults(value, field) {
337
- if (value === undefined)
338
- return [];
339
- if (!Array.isArray(value)) {
340
- throw new Error(`Model ${field} is invalid`);
341
- }
342
- return value.map((item) => {
343
- const result = normalizeToolResult(item);
344
- if (result === undefined) {
345
- throw new Error(`Model ${field} contains an invalid tool result`);
346
- }
347
- return result;
348
- });
349
- }
350
- export function normalizeModelResult(providerResult, correlation) {
351
- if (!isRecord(providerResult)) {
352
- throw new Error("Model result is not an object");
353
- }
354
- const parts = resultContentParts(providerResult);
355
- let text;
356
- if (providerResult.text !== undefined) {
357
- if (typeof providerResult.text !== "string") {
358
- throw new Error("Model text is invalid");
359
- }
360
- const normalizedText = boundedString(providerResult.text);
361
- if (normalizedText === undefined) {
362
- throw new Error("Model text exceeds the bounded output limit");
363
- }
364
- text = normalizedText;
365
- }
366
- else {
367
- const textParts = parts.filter((part) => part.type === "text");
368
- const normalizedTextParts = textParts.map((part) => {
369
- if (typeof part.text !== "string") {
370
- throw new Error("Model text part is invalid");
371
- }
372
- const normalizedText = boundedString(part.text);
373
- if (normalizedText === undefined) {
374
- throw new Error("Model text exceeds the bounded output limit");
375
- }
376
- return normalizedText;
377
- });
378
- const joinedText = normalizedTextParts.join("");
379
- if (boundedString(joinedText) === undefined) {
380
- throw new Error("Model text exceeds the bounded output limit");
381
- }
382
- text = joinedText;
383
- }
384
- const calls = uniqueCalls([
385
- ...normalizedToolCalls(providerResult.toolCalls, "tool calls"),
386
- ...normalizedToolCalls(providerResult.calls, "calls"),
387
- ...parts
388
- .filter((part) => part.type === "tool-call")
389
- .map((part) => {
390
- const call = normalizeToolCall(part);
391
- if (call === undefined) {
392
- throw new Error("Model content contains an invalid tool call");
393
- }
394
- return call;
395
- }),
396
- ]);
397
- const results = uniqueResults([
398
- ...normalizedToolResults(providerResult.toolResults, "tool results"),
399
- ...normalizedToolResults(providerResult.results, "results"),
400
- ...parts
401
- .filter((part) => part.type === "tool-result")
402
- .map((part) => {
403
- const result = normalizeToolResult(part);
404
- if (result === undefined) {
405
- throw new Error("Model content contains an invalid tool result");
406
- }
407
- return result;
408
- }),
409
- ]);
410
- const usage = normalizeUsage(providerResult.usage);
411
- return {
412
- text,
413
- calls,
414
- results,
415
- finishReason: normalizeFinishReason(providerResult.finishReason ?? providerResult.rawFinishReason, calls.length > 0, text.length > 0),
416
- ...(usage === undefined ? {} : { usage }),
417
- correlation: normalizeCorrelation(correlation),
418
- };
419
- }
420
- function statusCode(value) {
421
- if (!isRecord(value))
422
- return undefined;
423
- const candidate = value.statusCode ?? value.status;
424
- return typeof candidate === "number" && Number.isSafeInteger(candidate)
425
- ? candidate
426
- : undefined;
427
- }
428
- function isRetryableStatus(value) {
429
- return (value === 408 ||
430
- value === 425 ||
431
- value === 429 ||
432
- (value !== undefined && value >= 500 && value <= 599));
433
- }
434
- function isRetryableError(error, status) {
435
- if (isRecord(error) && typeof error.isRetryable === "boolean") {
436
- return error.isRetryable;
437
- }
438
- return isRetryableStatus(status);
439
- }
440
- export function normalizeModelFailure(error, correlation, code = "model_call_failed") {
441
- const status = statusCode(error);
442
- return {
443
- code,
444
- message: code === "model_call_aborted"
445
- ? "Model call was aborted."
446
- : code === "model_result_invalid"
447
- ? "Model result was invalid."
448
- : "Model call failed.",
449
- retryable: code === "model_result_invalid" ? false : isRetryableError(error, status),
450
- correlation: normalizeCorrelation(correlation),
451
- };
452
- }
453
- //# sourceMappingURL=model-normalizers.js.map
@@ -1,48 +0,0 @@
1
- import type { EdenJsonValue, EdenStepPhase } from "@moinulmoin/eden-definitions";
2
- import { type EdenSessionStorage } from "./session-journal.js";
3
- export declare const MAX_CHECKPOINT_ATTEMPTS = 3;
4
- export interface StableEffectIdentity {
5
- readonly sessionId: string;
6
- readonly turnId: string;
7
- readonly logicalStep: string;
8
- readonly callId: string;
9
- readonly toolName: string;
10
- readonly bundleIdentity: string;
11
- }
12
- export declare function createStableEffectIdempotencyKey(identity: StableEffectIdentity): string;
13
- export declare const createEffectIdempotencyKey: typeof createStableEffectIdempotencyKey;
14
- export interface CheckpointRequest extends StableEffectIdentity {
15
- readonly stepId: string;
16
- readonly phase: EdenStepPhase;
17
- readonly effectId: string;
18
- readonly input: EdenJsonValue;
19
- }
20
- export interface PreparedCheckpoint {
21
- readonly request: CheckpointRequest;
22
- readonly idempotencyKey: string;
23
- readonly attemptCount: number;
24
- }
25
- export type CheckpointPreparation = {
26
- readonly status: "execute";
27
- readonly prepared: PreparedCheckpoint;
28
- readonly attemptCount: number;
29
- } | {
30
- readonly status: "replayed";
31
- readonly output: EdenJsonValue;
32
- } | {
33
- readonly status: "exhausted";
34
- readonly attemptCount: number;
35
- };
36
- export type CheckpointCommitResult = {
37
- readonly status: "committed" | "replayed";
38
- readonly output: EdenJsonValue;
39
- } | {
40
- readonly status: "stale";
41
- } | {
42
- readonly status: "exhausted";
43
- };
44
- export declare function prepareCheckpointAttempt(storage: EdenSessionStorage, request: CheckpointRequest): CheckpointPreparation;
45
- export declare function commitCheckpointResult(storage: EdenSessionStorage, prepared: PreparedCheckpoint, output: EdenJsonValue, completedAt?: string): CheckpointCommitResult;
46
- export declare function markCheckpointRetryable(storage: EdenSessionStorage, prepared: PreparedCheckpoint, updatedAt?: string): boolean;
47
- export declare function reenterCheckpoint<TOutput extends EdenJsonValue>(storage: EdenSessionStorage, request: CheckpointRequest, execute: (prepared: PreparedCheckpoint) => TOutput | Promise<TOutput>): Promise<CheckpointCommitResult>;
48
- //# sourceMappingURL=session-checkpoint.d.ts.map