@moinulmoin/eden-runtime-cloudflare 0.1.5 → 0.2.1
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/NOTICE +0 -31
- package/dist/eden-eve-host-worker.mjs +130 -10
- package/dist/eve-host-runtime.d.ts +13 -3
- package/dist/eve-host-runtime.js +47 -3
- package/dist/eve-host.d.ts +6 -1
- package/dist/eve-host.js +20 -11
- package/dist/index.d.ts +0 -18
- package/dist/index.js +0 -5
- package/package.json +2 -6
- package/dist/artifact-runtime.d.ts +0 -27
- package/dist/artifact-runtime.js +0 -14
- package/dist/http-host.d.ts +0 -6
- package/dist/http-host.js +0 -419
- package/dist/model-adapter-internal.d.ts +0 -12
- package/dist/model-adapter-internal.js +0 -172
- package/dist/model-adapter.d.ts +0 -9
- package/dist/model-adapter.js +0 -56
- package/dist/model-contracts.d.ts +0 -92
- package/dist/model-contracts.js +0 -2
- package/dist/model-normalizers.d.ts +0 -7
- package/dist/model-normalizers.js +0 -453
- package/dist/session-checkpoint.d.ts +0 -48
- package/dist/session-checkpoint.js +0 -300
- package/dist/session-identity.d.ts +0 -9
- package/dist/session-identity.js +0 -45
- package/dist/session-jobs.d.ts +0 -69
- package/dist/session-jobs.js +0 -427
- package/dist/session-journal.d.ts +0 -154
- package/dist/session-journal.js +0 -391
- package/dist/session-schema.d.ts +0 -21
- package/dist/session-schema.js +0 -247
- package/dist/session-state.d.ts +0 -165
- package/dist/session-state.js +0 -168
- package/dist/session.d.ts +0 -40
- package/dist/session.js +0 -792
- package/dist/test-execution.d.ts +0 -8
- package/dist/test-execution.js +0 -12
- package/dist/test-worker.d.ts +0 -12
- package/dist/test-worker.js +0 -214
- package/dist/tool-harness.d.ts +0 -31
- package/dist/tool-harness.js +0 -226
- package/dist/turn-runner-storage.d.ts +0 -28
- package/dist/turn-runner-storage.js +0 -583
- package/dist/turn-runner-types.d.ts +0 -112
- package/dist/turn-runner-types.js +0 -21
- package/dist/turn-runner.d.ts +0 -6
- package/dist/turn-runner.js +0 -370
package/dist/http-host.js
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
import { EDEN_VERSIONS, } from "@moinulmoin/eden-definitions";
|
|
2
|
-
import { createOpaqueMessageId, createOpaqueSessionId, createOpaqueTurnId, createSessionObjectName, isOpaqueSessionId, } from "./session-identity.js";
|
|
3
|
-
import { readConfiguredEdenArtifact } from "./artifact-runtime.js";
|
|
4
|
-
import { SESSION_SCHEMA_VERSION } from "./session-schema.js";
|
|
5
|
-
const HEALTH_PATH = "/eden/v1/health";
|
|
6
|
-
const INFO_PATH = "/eden/v1/info";
|
|
7
|
-
const SESSION_PATH = "/eden/v1/session";
|
|
8
|
-
const STREAM_SUFFIX = "/stream";
|
|
9
|
-
const MAX_CREATE_BODY_BYTES = 1_024;
|
|
10
|
-
const MAX_COMMAND_BODY_BYTES = 32 * 1_024;
|
|
11
|
-
const MAX_MESSAGE_BYTES = 16 * 1_024;
|
|
12
|
-
const MAX_JSON_DEPTH = 32;
|
|
13
|
-
const FIXED_PRINCIPAL = "principal:test";
|
|
14
|
-
const UNSUPPORTED_IDENTITY_HEADERS = new Set([
|
|
15
|
-
"x-eden-principal",
|
|
16
|
-
"x-eden-tenant",
|
|
17
|
-
"x-eden-identity",
|
|
18
|
-
"x-eden-session-id",
|
|
19
|
-
"x-eden-object-name",
|
|
20
|
-
"x-durable-object-id",
|
|
21
|
-
]);
|
|
22
|
-
function jsonResponse(body, status = 200, headers = {}) {
|
|
23
|
-
const responseHeaders = new Headers(headers);
|
|
24
|
-
responseHeaders.set("content-type", "application/json; charset=utf-8");
|
|
25
|
-
responseHeaders.set("cache-control", "no-store");
|
|
26
|
-
return new Response(JSON.stringify(body), { status, headers: responseHeaders });
|
|
27
|
-
}
|
|
28
|
-
function errorResponse(code, status) {
|
|
29
|
-
return jsonResponse({
|
|
30
|
-
code,
|
|
31
|
-
message: code === "unauthorized"
|
|
32
|
-
? "Authorization is required."
|
|
33
|
-
: code === "not_found"
|
|
34
|
-
? "Resource was not found."
|
|
35
|
-
: code === "conflict"
|
|
36
|
-
? "The session cannot accept this operation."
|
|
37
|
-
: code === "internal_error"
|
|
38
|
-
? "The request could not be completed."
|
|
39
|
-
: "The request is invalid.",
|
|
40
|
-
}, status);
|
|
41
|
-
}
|
|
42
|
-
function constantTimeEqual(left, right) {
|
|
43
|
-
const leftBytes = new TextEncoder().encode(left);
|
|
44
|
-
const rightBytes = new TextEncoder().encode(right);
|
|
45
|
-
const length = Math.max(leftBytes.length, rightBytes.length);
|
|
46
|
-
let difference = leftBytes.length ^ rightBytes.length;
|
|
47
|
-
for (let index = 0; index < length; index += 1) {
|
|
48
|
-
difference |= (leftBytes[index] ?? 0) ^ (rightBytes[index] ?? 0);
|
|
49
|
-
}
|
|
50
|
-
return difference === 0;
|
|
51
|
-
}
|
|
52
|
-
function isAuthorized(request, configuredBearer) {
|
|
53
|
-
if (configuredBearer === undefined || configuredBearer.length === 0) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
const header = request.headers.get("authorization");
|
|
57
|
-
if (header === null || !header.startsWith("Bearer "))
|
|
58
|
-
return false;
|
|
59
|
-
const bearer = header.slice("Bearer ".length);
|
|
60
|
-
if (bearer.length === 0 || bearer.includes(" "))
|
|
61
|
-
return false;
|
|
62
|
-
return constantTimeEqual(bearer, configuredBearer);
|
|
63
|
-
}
|
|
64
|
-
function hasUnsupportedIdentityHeader(request) {
|
|
65
|
-
let unsupported = false;
|
|
66
|
-
request.headers.forEach((_value, key) => {
|
|
67
|
-
if (UNSUPPORTED_IDENTITY_HEADERS.has(key.toLowerCase())) {
|
|
68
|
-
unsupported = true;
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
return unsupported;
|
|
72
|
-
}
|
|
73
|
-
function contentTypeIsJson(request) {
|
|
74
|
-
const contentType = request.headers.get("content-type");
|
|
75
|
-
if (contentType === null)
|
|
76
|
-
return false;
|
|
77
|
-
return contentType.split(";", 1)[0]?.trim().toLowerCase() === "application/json";
|
|
78
|
-
}
|
|
79
|
-
async function readBoundedBody(request, maximumBytes) {
|
|
80
|
-
const declaredLength = request.headers.get("content-length");
|
|
81
|
-
if (declaredLength !== null) {
|
|
82
|
-
const parsedLength = Number(declaredLength);
|
|
83
|
-
if (!Number.isSafeInteger(parsedLength) ||
|
|
84
|
-
parsedLength < 0 ||
|
|
85
|
-
parsedLength > maximumBytes) {
|
|
86
|
-
return { status: "too_large" };
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
const reader = request.body?.getReader();
|
|
90
|
-
if (reader === undefined) {
|
|
91
|
-
return { status: "invalid" };
|
|
92
|
-
}
|
|
93
|
-
const chunks = [];
|
|
94
|
-
let total = 0;
|
|
95
|
-
try {
|
|
96
|
-
while (true) {
|
|
97
|
-
const next = await reader.read();
|
|
98
|
-
if (next.done)
|
|
99
|
-
break;
|
|
100
|
-
total += next.value.byteLength;
|
|
101
|
-
if (total > maximumBytes) {
|
|
102
|
-
await reader.cancel();
|
|
103
|
-
return { status: "too_large" };
|
|
104
|
-
}
|
|
105
|
-
chunks.push(next.value);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
catch {
|
|
109
|
-
return { status: "invalid" };
|
|
110
|
-
}
|
|
111
|
-
const bytes = new Uint8Array(total);
|
|
112
|
-
let offset = 0;
|
|
113
|
-
for (const chunk of chunks) {
|
|
114
|
-
bytes.set(chunk, offset);
|
|
115
|
-
offset += chunk.byteLength;
|
|
116
|
-
}
|
|
117
|
-
try {
|
|
118
|
-
return {
|
|
119
|
-
status: "ok",
|
|
120
|
-
value: JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)),
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
return { status: "invalid" };
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function parseObject(value) {
|
|
128
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
129
|
-
return undefined;
|
|
130
|
-
}
|
|
131
|
-
return value;
|
|
132
|
-
}
|
|
133
|
-
function exceedsJsonDepth(value, depth = 0, seen = new WeakSet()) {
|
|
134
|
-
if (depth > MAX_JSON_DEPTH)
|
|
135
|
-
return true;
|
|
136
|
-
if (typeof value !== "object" || value === null)
|
|
137
|
-
return false;
|
|
138
|
-
if (seen.has(value))
|
|
139
|
-
return true;
|
|
140
|
-
seen.add(value);
|
|
141
|
-
try {
|
|
142
|
-
if (Array.isArray(value)) {
|
|
143
|
-
return value.some((item) => exceedsJsonDepth(item, depth + 1, seen));
|
|
144
|
-
}
|
|
145
|
-
return Object.values(value).some((item) => exceedsJsonDepth(item, depth + 1, seen));
|
|
146
|
-
}
|
|
147
|
-
finally {
|
|
148
|
-
// Keep cycle tracking path-local so repeated references in separate
|
|
149
|
-
// branches remain valid JSON while true cycles still fail closed.
|
|
150
|
-
seen.delete(value);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
function hasOnlyKeys(value, keys) {
|
|
154
|
-
const allowed = new Set(keys);
|
|
155
|
-
return Object.keys(value).every((key) => allowed.has(key));
|
|
156
|
-
}
|
|
157
|
-
function validateCreateBody(value) {
|
|
158
|
-
const body = parseObject(value);
|
|
159
|
-
return body !== undefined && Object.keys(body).length === 0;
|
|
160
|
-
}
|
|
161
|
-
function validateCommandBody(value) {
|
|
162
|
-
const body = parseObject(value);
|
|
163
|
-
if (body === undefined ||
|
|
164
|
-
!hasOnlyKeys(body, ["message"]) ||
|
|
165
|
-
typeof body.message !== "string") {
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
return (body.message.trim().length > 0 &&
|
|
169
|
-
new TextEncoder().encode(body.message).byteLength <= MAX_MESSAGE_BYTES);
|
|
170
|
-
}
|
|
171
|
-
function readStreamQuery(url) {
|
|
172
|
-
const allowed = new Set(["startIndex", "follow"]);
|
|
173
|
-
const queryKeys = [];
|
|
174
|
-
url.searchParams.forEach((_value, key) => queryKeys.push(key));
|
|
175
|
-
for (const key of queryKeys) {
|
|
176
|
-
if (!allowed.has(key))
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
179
|
-
const startValues = url.searchParams.getAll("startIndex");
|
|
180
|
-
if (startValues.length > 1)
|
|
181
|
-
return undefined;
|
|
182
|
-
const rawStartIndex = startValues[0] ?? "0";
|
|
183
|
-
const startIndex = Number(rawStartIndex);
|
|
184
|
-
if (!Number.isSafeInteger(startIndex) ||
|
|
185
|
-
startIndex < 0 ||
|
|
186
|
-
rawStartIndex !== String(startIndex)) {
|
|
187
|
-
return undefined;
|
|
188
|
-
}
|
|
189
|
-
const followValues = url.searchParams.getAll("follow");
|
|
190
|
-
if (followValues.length > 1)
|
|
191
|
-
return undefined;
|
|
192
|
-
const rawFollow = followValues[0];
|
|
193
|
-
if (rawFollow !== undefined && rawFollow !== "true" && rawFollow !== "false") {
|
|
194
|
-
return undefined;
|
|
195
|
-
}
|
|
196
|
-
return {
|
|
197
|
-
startIndex,
|
|
198
|
-
follow: rawFollow !== "false",
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
function hasQuery(url) {
|
|
202
|
-
let present = false;
|
|
203
|
-
url.searchParams.forEach(() => {
|
|
204
|
-
present = true;
|
|
205
|
-
});
|
|
206
|
-
return present;
|
|
207
|
-
}
|
|
208
|
-
function sessionIdFromPath(pathname) {
|
|
209
|
-
if (!pathname.startsWith(`${SESSION_PATH}/`))
|
|
210
|
-
return undefined;
|
|
211
|
-
const suffix = pathname.slice(`${SESSION_PATH}/`.length);
|
|
212
|
-
if (suffix.length === 0 || suffix.includes("/"))
|
|
213
|
-
return undefined;
|
|
214
|
-
let decoded;
|
|
215
|
-
try {
|
|
216
|
-
decoded = decodeURIComponent(suffix);
|
|
217
|
-
}
|
|
218
|
-
catch {
|
|
219
|
-
return undefined;
|
|
220
|
-
}
|
|
221
|
-
return isOpaqueSessionId(decoded) ? decoded : undefined;
|
|
222
|
-
}
|
|
223
|
-
function sessionStub(env, sessionId) {
|
|
224
|
-
if (env.EDEN_SESSIONS === undefined)
|
|
225
|
-
return undefined;
|
|
226
|
-
return env.EDEN_SESSIONS.get(env.EDEN_SESSIONS.idFromName(createSessionObjectName(sessionId)));
|
|
227
|
-
}
|
|
228
|
-
async function initializeSession(env, sessionId) {
|
|
229
|
-
const stub = sessionStub(env, sessionId);
|
|
230
|
-
if (stub === undefined)
|
|
231
|
-
return false;
|
|
232
|
-
const response = await stub.fetch("https://eden/_eden/initialize", {
|
|
233
|
-
method: "POST",
|
|
234
|
-
headers: { "content-type": "application/json" },
|
|
235
|
-
body: JSON.stringify({
|
|
236
|
-
sessionId,
|
|
237
|
-
ownerPrincipal: FIXED_PRINCIPAL,
|
|
238
|
-
versions: EDEN_VERSIONS,
|
|
239
|
-
}),
|
|
240
|
-
});
|
|
241
|
-
await response.arrayBuffer();
|
|
242
|
-
return response.status === 200 || response.status === 201;
|
|
243
|
-
}
|
|
244
|
-
async function acceptCommand(env, sessionId, request, turnId, messageId) {
|
|
245
|
-
const stub = sessionStub(env, sessionId);
|
|
246
|
-
if (stub === undefined)
|
|
247
|
-
return errorResponse("internal_error", 500);
|
|
248
|
-
let response;
|
|
249
|
-
try {
|
|
250
|
-
response = await stub.fetch("https://eden/_eden/accept", {
|
|
251
|
-
method: "POST",
|
|
252
|
-
headers: { "content-type": "application/json" },
|
|
253
|
-
body: JSON.stringify({
|
|
254
|
-
sessionId,
|
|
255
|
-
ownerPrincipal: FIXED_PRINCIPAL,
|
|
256
|
-
turnId,
|
|
257
|
-
messageId,
|
|
258
|
-
message: request.message,
|
|
259
|
-
}),
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
catch {
|
|
263
|
-
return errorResponse("internal_error", 500);
|
|
264
|
-
}
|
|
265
|
-
const status = response.status;
|
|
266
|
-
await response.arrayBuffer();
|
|
267
|
-
if (status === 200 || status === 202) {
|
|
268
|
-
return jsonResponse({ sessionId, turnId, status: "accepted" }, 202);
|
|
269
|
-
}
|
|
270
|
-
if (status === 404)
|
|
271
|
-
return errorResponse("not_found", 404);
|
|
272
|
-
if (status === 409)
|
|
273
|
-
return errorResponse("conflict", 409);
|
|
274
|
-
if (status === 400)
|
|
275
|
-
return errorResponse("invalid_request", 400);
|
|
276
|
-
return errorResponse("internal_error", 500);
|
|
277
|
-
}
|
|
278
|
-
async function streamEvents(env, sessionId, query) {
|
|
279
|
-
const stub = sessionStub(env, sessionId);
|
|
280
|
-
if (stub === undefined)
|
|
281
|
-
return errorResponse("internal_error", 500);
|
|
282
|
-
let response;
|
|
283
|
-
try {
|
|
284
|
-
response = await stub.fetch("https://eden/_eden/stream", {
|
|
285
|
-
method: "POST",
|
|
286
|
-
headers: { "content-type": "application/json" },
|
|
287
|
-
body: JSON.stringify({
|
|
288
|
-
sessionId,
|
|
289
|
-
ownerPrincipal: FIXED_PRINCIPAL,
|
|
290
|
-
startIndex: query.startIndex,
|
|
291
|
-
follow: query.follow,
|
|
292
|
-
}),
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
catch {
|
|
296
|
-
return errorResponse("internal_error", 500);
|
|
297
|
-
}
|
|
298
|
-
if (response.status === 404) {
|
|
299
|
-
await response.arrayBuffer();
|
|
300
|
-
return errorResponse("not_found", 404);
|
|
301
|
-
}
|
|
302
|
-
if (response.status !== 200) {
|
|
303
|
-
await response.arrayBuffer();
|
|
304
|
-
return errorResponse("internal_error", 500);
|
|
305
|
-
}
|
|
306
|
-
return new Response(response.body, {
|
|
307
|
-
status: 200,
|
|
308
|
-
headers: {
|
|
309
|
-
"content-type": "application/x-ndjson; charset=utf-8",
|
|
310
|
-
"cache-control": "no-store",
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
async function handleCreate(request, env) {
|
|
315
|
-
if (request.method !== "POST") {
|
|
316
|
-
return errorResponse("not_found", 404);
|
|
317
|
-
}
|
|
318
|
-
if (hasQuery(new URL(request.url))) {
|
|
319
|
-
return errorResponse("invalid_request", 400);
|
|
320
|
-
}
|
|
321
|
-
if (!contentTypeIsJson(request))
|
|
322
|
-
return errorResponse("invalid_request", 400);
|
|
323
|
-
const body = await readBoundedBody(request, MAX_CREATE_BODY_BYTES);
|
|
324
|
-
if (body.status !== "ok" ||
|
|
325
|
-
exceedsJsonDepth(body.value) ||
|
|
326
|
-
!validateCreateBody(body.value)) {
|
|
327
|
-
return errorResponse(body.status === "too_large" ? "request_too_large" : "invalid_request", 400);
|
|
328
|
-
}
|
|
329
|
-
const sessionId = createOpaqueSessionId();
|
|
330
|
-
if (!(await initializeSession(env, sessionId))) {
|
|
331
|
-
return errorResponse("internal_error", 500);
|
|
332
|
-
}
|
|
333
|
-
return jsonResponse({
|
|
334
|
-
sessionId,
|
|
335
|
-
status: "new",
|
|
336
|
-
versions: EDEN_VERSIONS,
|
|
337
|
-
sqliteSchemaVersion: SESSION_SCHEMA_VERSION,
|
|
338
|
-
}, 201);
|
|
339
|
-
}
|
|
340
|
-
async function handleCommand(request, env, sessionId) {
|
|
341
|
-
if (request.method !== "POST") {
|
|
342
|
-
return errorResponse("not_found", 404);
|
|
343
|
-
}
|
|
344
|
-
if (hasQuery(new URL(request.url))) {
|
|
345
|
-
return errorResponse("invalid_request", 400);
|
|
346
|
-
}
|
|
347
|
-
if (!contentTypeIsJson(request))
|
|
348
|
-
return errorResponse("invalid_request", 400);
|
|
349
|
-
const body = await readBoundedBody(request, MAX_COMMAND_BODY_BYTES);
|
|
350
|
-
if (body.status !== "ok" ||
|
|
351
|
-
exceedsJsonDepth(body.value) ||
|
|
352
|
-
!validateCommandBody(body.value)) {
|
|
353
|
-
return errorResponse(body.status === "too_large" ? "request_too_large" : "invalid_request", 400);
|
|
354
|
-
}
|
|
355
|
-
return acceptCommand(env, sessionId, body.value, createOpaqueTurnId(), createOpaqueMessageId());
|
|
356
|
-
}
|
|
357
|
-
async function handleStream(request, env, sessionId) {
|
|
358
|
-
if (request.method !== "GET")
|
|
359
|
-
return errorResponse("not_found", 404);
|
|
360
|
-
const query = readStreamQuery(new URL(request.url));
|
|
361
|
-
if (query === undefined)
|
|
362
|
-
return errorResponse("invalid_request", 400);
|
|
363
|
-
return streamEvents(env, sessionId, query);
|
|
364
|
-
}
|
|
365
|
-
export async function handleEdenRequest(request, env) {
|
|
366
|
-
if (!isAuthorized(request, env.EDEN_BEARER_SECRET)) {
|
|
367
|
-
return errorResponse("unauthorized", 401);
|
|
368
|
-
}
|
|
369
|
-
if (hasUnsupportedIdentityHeader(request)) {
|
|
370
|
-
return errorResponse("invalid_request", 400);
|
|
371
|
-
}
|
|
372
|
-
const url = new URL(request.url);
|
|
373
|
-
if (url.pathname === HEALTH_PATH) {
|
|
374
|
-
return request.method === "GET"
|
|
375
|
-
? jsonResponse({ status: "ok" })
|
|
376
|
-
: errorResponse("not_found", 404);
|
|
377
|
-
}
|
|
378
|
-
if (url.pathname === INFO_PATH) {
|
|
379
|
-
const generation = readConfiguredEdenArtifact()?.generation;
|
|
380
|
-
return request.method === "GET"
|
|
381
|
-
? jsonResponse({
|
|
382
|
-
service: "eden",
|
|
383
|
-
protocol: EDEN_VERSIONS.protocol,
|
|
384
|
-
versions: EDEN_VERSIONS,
|
|
385
|
-
sqliteSchemaVersion: SESSION_SCHEMA_VERSION,
|
|
386
|
-
...(generation === undefined
|
|
387
|
-
? {}
|
|
388
|
-
: {
|
|
389
|
-
generation: {
|
|
390
|
-
generationId: generation.generationId,
|
|
391
|
-
bundleDigest: generation.bundleDigest,
|
|
392
|
-
manifestVersion: generation.manifestVersion,
|
|
393
|
-
runtimeVersion: generation.runtimeVersion,
|
|
394
|
-
agentBundleVersion: generation.agentBundleVersion,
|
|
395
|
-
protocolVersion: generation.protocolVersion,
|
|
396
|
-
schemaVersion: generation.schemaVersion,
|
|
397
|
-
toolNames: generation.toolNames,
|
|
398
|
-
},
|
|
399
|
-
}),
|
|
400
|
-
})
|
|
401
|
-
: errorResponse("not_found", 404);
|
|
402
|
-
}
|
|
403
|
-
if (url.pathname === SESSION_PATH) {
|
|
404
|
-
return handleCreate(request, env);
|
|
405
|
-
}
|
|
406
|
-
if (url.pathname.endsWith(STREAM_SUFFIX)) {
|
|
407
|
-
const basePath = url.pathname.slice(0, -STREAM_SUFFIX.length);
|
|
408
|
-
const streamSessionId = sessionIdFromPath(basePath);
|
|
409
|
-
if (streamSessionId === undefined) {
|
|
410
|
-
return errorResponse("not_found", 404);
|
|
411
|
-
}
|
|
412
|
-
return handleStream(request, env, streamSessionId);
|
|
413
|
-
}
|
|
414
|
-
const sessionId = sessionIdFromPath(url.pathname);
|
|
415
|
-
if (sessionId === undefined)
|
|
416
|
-
return errorResponse("not_found", 404);
|
|
417
|
-
return handleCommand(request, env, sessionId);
|
|
418
|
-
}
|
|
419
|
-
//# sourceMappingURL=http-host.js.map
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type EdenModelAdapter, type EdenModelAdapterCall } from "./model-adapter.js";
|
|
2
|
-
interface InternalWorkersAIOptions {
|
|
3
|
-
readonly binding: unknown;
|
|
4
|
-
readonly modelId: string;
|
|
5
|
-
readonly gatewayId: string;
|
|
6
|
-
}
|
|
7
|
-
/** Internal only. The public runtime contract never accepts a binding. */
|
|
8
|
-
export declare function createWorkersAIModelProvider(options: InternalWorkersAIOptions): EdenModelAdapterCall;
|
|
9
|
-
/** Internal only. The public runtime contract never accepts a binding. */
|
|
10
|
-
export declare function createWorkersAIModelAdapter(options: InternalWorkersAIOptions): EdenModelAdapter;
|
|
11
|
-
export {};
|
|
12
|
-
//# sourceMappingURL=model-adapter-internal.d.ts.map
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { createModelAdapter, } from "./model-adapter.js";
|
|
2
|
-
function isRecord(value) {
|
|
3
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
-
}
|
|
5
|
-
function providerMessage(message) {
|
|
6
|
-
if (typeof message.content === "string") {
|
|
7
|
-
return { role: message.role, content: message.content };
|
|
8
|
-
}
|
|
9
|
-
if (message.role === "assistant") {
|
|
10
|
-
const text = message.content
|
|
11
|
-
.filter((part) => part.type === "text")
|
|
12
|
-
.map((part) => part.text)
|
|
13
|
-
.join("");
|
|
14
|
-
const toolCalls = message.content
|
|
15
|
-
.filter((part) => part.type === "tool-call")
|
|
16
|
-
.map((part) => ({
|
|
17
|
-
id: part.callId,
|
|
18
|
-
type: "function",
|
|
19
|
-
function: {
|
|
20
|
-
name: part.toolName,
|
|
21
|
-
arguments: JSON.stringify(part.input),
|
|
22
|
-
},
|
|
23
|
-
}));
|
|
24
|
-
return {
|
|
25
|
-
role: message.role,
|
|
26
|
-
content: text.length === 0 ? null : text,
|
|
27
|
-
...(toolCalls.length === 0 ? {} : { tool_calls: toolCalls }),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
if (message.role === "tool") {
|
|
31
|
-
return {
|
|
32
|
-
role: "tool",
|
|
33
|
-
content: message.content
|
|
34
|
-
.filter((part) => part.type === "tool-result")
|
|
35
|
-
.map((part) => JSON.stringify(part.output))
|
|
36
|
-
.join("\n"),
|
|
37
|
-
tool_call_id: message.content.find((part) => part.type === "tool-result")?.callId,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
return {
|
|
41
|
-
role: message.role,
|
|
42
|
-
content: message.content
|
|
43
|
-
.filter((part) => part.type === "text")
|
|
44
|
-
.map((part) => part.text)
|
|
45
|
-
.join(""),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
function providerTools(tools) {
|
|
49
|
-
if (tools === undefined)
|
|
50
|
-
return undefined;
|
|
51
|
-
return tools.map((tool) => ({
|
|
52
|
-
type: "function",
|
|
53
|
-
function: {
|
|
54
|
-
name: tool.name,
|
|
55
|
-
description: tool.description,
|
|
56
|
-
parameters: isRecord(tool.inputSchema)
|
|
57
|
-
? tool.inputSchema
|
|
58
|
-
: { type: "object", properties: {} },
|
|
59
|
-
},
|
|
60
|
-
}));
|
|
61
|
-
}
|
|
62
|
-
function providerToolChoice(choice) {
|
|
63
|
-
if (choice === undefined || choice === "auto" || choice === "none") {
|
|
64
|
-
return choice;
|
|
65
|
-
}
|
|
66
|
-
if (choice === "required")
|
|
67
|
-
return "required";
|
|
68
|
-
return {
|
|
69
|
-
type: "function",
|
|
70
|
-
function: { name: choice.toolName },
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
function parseToolInput(value) {
|
|
74
|
-
if (typeof value !== "string")
|
|
75
|
-
return value;
|
|
76
|
-
try {
|
|
77
|
-
return JSON.parse(value);
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
return {};
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
function normalizeWorkersAIResult(value) {
|
|
84
|
-
if (!isRecord(value))
|
|
85
|
-
return {};
|
|
86
|
-
const choices = Array.isArray(value.choices) ? value.choices : [];
|
|
87
|
-
const choice = isRecord(choices[0]) ? choices[0] : {};
|
|
88
|
-
const message = isRecord(choice.message) ? choice.message : {};
|
|
89
|
-
const toolCalls = Array.isArray(message.tool_calls)
|
|
90
|
-
? message.tool_calls
|
|
91
|
-
.filter(isRecord)
|
|
92
|
-
.map((call, index) => {
|
|
93
|
-
const functionValue = isRecord(call.function) ? call.function : {};
|
|
94
|
-
return {
|
|
95
|
-
callId: typeof call.id === "string" ? call.id : `call_${index + 1}`,
|
|
96
|
-
toolName: typeof functionValue.name === "string"
|
|
97
|
-
? functionValue.name
|
|
98
|
-
: "",
|
|
99
|
-
input: parseToolInput(functionValue.arguments),
|
|
100
|
-
};
|
|
101
|
-
})
|
|
102
|
-
: [];
|
|
103
|
-
const usage = isRecord(value.usage)
|
|
104
|
-
? {
|
|
105
|
-
...(typeof value.usage.prompt_tokens === "number"
|
|
106
|
-
? { inputTokens: value.usage.prompt_tokens }
|
|
107
|
-
: typeof value.usage.input_tokens === "number"
|
|
108
|
-
? { inputTokens: value.usage.input_tokens }
|
|
109
|
-
: {}),
|
|
110
|
-
...(typeof value.usage.completion_tokens === "number"
|
|
111
|
-
? { outputTokens: value.usage.completion_tokens }
|
|
112
|
-
: typeof value.usage.output_tokens === "number"
|
|
113
|
-
? { outputTokens: value.usage.output_tokens }
|
|
114
|
-
: {}),
|
|
115
|
-
...(typeof value.usage.total_tokens === "number"
|
|
116
|
-
? { totalTokens: value.usage.total_tokens }
|
|
117
|
-
: {}),
|
|
118
|
-
}
|
|
119
|
-
: undefined;
|
|
120
|
-
return {
|
|
121
|
-
text: typeof message.content === "string" ? message.content : "",
|
|
122
|
-
calls: toolCalls,
|
|
123
|
-
results: [],
|
|
124
|
-
finishReason: choice.finish_reason,
|
|
125
|
-
...(usage === undefined ? {} : { usage }),
|
|
126
|
-
};
|
|
127
|
-
}
|
|
128
|
-
function createWorkersAIProvider(options) {
|
|
129
|
-
return {
|
|
130
|
-
call: async (request) => {
|
|
131
|
-
const modelId = request.modelId ?? options.modelId;
|
|
132
|
-
const generationOptions = request.options;
|
|
133
|
-
const tools = providerTools(request.tools);
|
|
134
|
-
const inputs = {
|
|
135
|
-
messages: request.messages.map(providerMessage),
|
|
136
|
-
...(tools === undefined
|
|
137
|
-
? {}
|
|
138
|
-
: { tools }),
|
|
139
|
-
...(request.toolChoice === undefined
|
|
140
|
-
? {}
|
|
141
|
-
: { tool_choice: providerToolChoice(request.toolChoice) }),
|
|
142
|
-
...(generationOptions?.temperature === undefined
|
|
143
|
-
? {}
|
|
144
|
-
: { temperature: generationOptions.temperature }),
|
|
145
|
-
...(generationOptions?.maxOutputTokens === undefined
|
|
146
|
-
? {}
|
|
147
|
-
: { max_tokens: generationOptions.maxOutputTokens }),
|
|
148
|
-
...(generationOptions?.thinking === false
|
|
149
|
-
? { reasoning_effort: null }
|
|
150
|
-
: {}),
|
|
151
|
-
};
|
|
152
|
-
const binding = options.binding;
|
|
153
|
-
const response = await binding.run(modelId, inputs, {
|
|
154
|
-
gateway: { id: options.gatewayId },
|
|
155
|
-
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
156
|
-
});
|
|
157
|
-
if (response instanceof Response) {
|
|
158
|
-
return normalizeWorkersAIResult(await response.json());
|
|
159
|
-
}
|
|
160
|
-
return normalizeWorkersAIResult(response);
|
|
161
|
-
},
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
/** Internal only. The public runtime contract never accepts a binding. */
|
|
165
|
-
export function createWorkersAIModelProvider(options) {
|
|
166
|
-
return createWorkersAIProvider(options).call;
|
|
167
|
-
}
|
|
168
|
-
/** Internal only. The public runtime contract never accepts a binding. */
|
|
169
|
-
export function createWorkersAIModelAdapter(options) {
|
|
170
|
-
return createModelAdapter(createWorkersAIModelProvider(options));
|
|
171
|
-
}
|
|
172
|
-
//# sourceMappingURL=model-adapter-internal.js.map
|
package/dist/model-adapter.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { EdenModelOutcome, EdenModelAdapterRequest, EdenModelRequest } from "./model-contracts.js";
|
|
2
|
-
export type { EdenModelCorrelation, EdenModelFailure, EdenModelFailureCode, EdenModelFinishReason, EdenModelMessage, EdenModelMessagePart, EdenModelOptions, EdenModelOutcome, EdenModelAdapterRequest, EdenModelRequest, EdenModelResult, EdenModelRole, EdenModelToolCall, EdenModelToolChoice, EdenModelToolDefinition, EdenModelToolResult, EdenModelUsage, } from "./model-contracts.js";
|
|
3
|
-
export { normalizeEdenJsonValue, normalizeModelFailure, normalizeModelMessages, normalizeModelResult, } from "./model-normalizers.js";
|
|
4
|
-
export type EdenModelAdapterCall = (request: EdenModelAdapterRequest) => Promise<unknown>;
|
|
5
|
-
export interface EdenModelAdapter {
|
|
6
|
-
readonly call: (request: EdenModelRequest) => Promise<EdenModelOutcome>;
|
|
7
|
-
}
|
|
8
|
-
export declare function createModelAdapter(provider: EdenModelAdapterCall): EdenModelAdapter;
|
|
9
|
-
//# sourceMappingURL=model-adapter.d.ts.map
|
package/dist/model-adapter.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { normalizeModelFailure, normalizeModelMessages, normalizeModelResult, } from "./model-normalizers.js";
|
|
2
|
-
export { normalizeEdenJsonValue, normalizeModelFailure, normalizeModelMessages, normalizeModelResult, } from "./model-normalizers.js";
|
|
3
|
-
function normalizeToolDefinition(tool) {
|
|
4
|
-
return {
|
|
5
|
-
name: tool.name,
|
|
6
|
-
description: tool.description,
|
|
7
|
-
inputSchema: tool.inputSchema,
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
function providerRequest(request) {
|
|
11
|
-
const messages = normalizeModelMessages(request.messages);
|
|
12
|
-
const tools = request.tools === undefined
|
|
13
|
-
? undefined
|
|
14
|
-
: request.tools.map(normalizeToolDefinition);
|
|
15
|
-
return {
|
|
16
|
-
messages,
|
|
17
|
-
...(request.modelId === undefined ? {} : { modelId: request.modelId }),
|
|
18
|
-
...(tools === undefined ? {} : { tools }),
|
|
19
|
-
...(request.toolChoice === undefined
|
|
20
|
-
? {}
|
|
21
|
-
: { toolChoice: request.toolChoice }),
|
|
22
|
-
...(request.options === undefined ? {} : { options: request.options }),
|
|
23
|
-
...(request.signal === undefined ? {} : { signal: request.signal }),
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
export function createModelAdapter(provider) {
|
|
27
|
-
return {
|
|
28
|
-
async call(request) {
|
|
29
|
-
const correlation = request.correlation;
|
|
30
|
-
try {
|
|
31
|
-
const value = await provider(providerRequest(request));
|
|
32
|
-
let result;
|
|
33
|
-
try {
|
|
34
|
-
result = normalizeModelResult(value, correlation);
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
return {
|
|
38
|
-
status: "error",
|
|
39
|
-
error: normalizeModelFailure(error, correlation, "model_result_invalid"),
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
return { status: "ok", result };
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
const code = request.signal?.aborted
|
|
46
|
-
? "model_call_aborted"
|
|
47
|
-
: "model_call_failed";
|
|
48
|
-
return {
|
|
49
|
-
status: "error",
|
|
50
|
-
error: normalizeModelFailure(error, correlation, code),
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
//# sourceMappingURL=model-adapter.js.map
|