@moinulmoin/eden-runtime-cloudflare 0.1.5 → 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.
- package/NOTICE +0 -31
- package/dist/eden-eve-host-worker.mjs +1 -9
- package/dist/eve-host-runtime.d.ts +0 -1
- package/dist/eve-host-runtime.js +0 -1
- package/dist/eve-host.d.ts +2 -1
- package/dist/eve-host.js +2 -8
- 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/test-execution.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { EdenModelAdapter } from "./model-adapter.js";
|
|
2
|
-
/**
|
|
3
|
-
* Deterministic model injection is intentionally test-worker-only.
|
|
4
|
-
* Production wrappers must use the Workers AI binding instead.
|
|
5
|
-
*/
|
|
6
|
-
export declare function configureEdenTestModel(model: EdenModelAdapter): void;
|
|
7
|
-
export declare function readConfiguredEdenTestModel(): EdenModelAdapter | undefined;
|
|
8
|
-
//# sourceMappingURL=test-execution.d.ts.map
|
package/dist/test-execution.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
let configuredTestModel;
|
|
2
|
-
/**
|
|
3
|
-
* Deterministic model injection is intentionally test-worker-only.
|
|
4
|
-
* Production wrappers must use the Workers AI binding instead.
|
|
5
|
-
*/
|
|
6
|
-
export function configureEdenTestModel(model) {
|
|
7
|
-
configuredTestModel = model;
|
|
8
|
-
}
|
|
9
|
-
export function readConfiguredEdenTestModel() {
|
|
10
|
-
return configuredTestModel;
|
|
11
|
-
}
|
|
12
|
-
//# sourceMappingURL=test-execution.js.map
|
package/dist/test-worker.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { EdenSession } from "./session.js";
|
|
2
|
-
import { type EdenWorkerEnvironment } from "./http-host.js";
|
|
3
|
-
import "./model-adapter-internal.js";
|
|
4
|
-
import { configureEdenArtifact } from "./artifact-runtime.js";
|
|
5
|
-
export { EdenSession };
|
|
6
|
-
export { readConfiguredEdenArtifact, } from "./artifact-runtime.js";
|
|
7
|
-
export { configureEdenArtifact };
|
|
8
|
-
declare const _default: {
|
|
9
|
-
fetch(request: Request, env: EdenWorkerEnvironment): Promise<Response>;
|
|
10
|
-
};
|
|
11
|
-
export default _default;
|
|
12
|
-
//# sourceMappingURL=test-worker.d.ts.map
|
package/dist/test-worker.js
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import { EdenSession } from "./session.js";
|
|
2
|
-
import { handleEdenRequest } from "./http-host.js";
|
|
3
|
-
import { createModelAdapter, } from "./model-adapter.js";
|
|
4
|
-
import "./model-adapter-internal.js";
|
|
5
|
-
import { configureEdenTestModel } from "./test-execution.js";
|
|
6
|
-
import { configureEdenArtifact, readConfiguredEdenArtifact, } from "./artifact-runtime.js";
|
|
7
|
-
import { isOpaqueSessionId } from "./session-identity.js";
|
|
8
|
-
export { EdenSession };
|
|
9
|
-
export { readConfiguredEdenArtifact, } from "./artifact-runtime.js";
|
|
10
|
-
export { configureEdenArtifact };
|
|
11
|
-
const TEST_TOOL_INPUT_SCHEMA = {
|
|
12
|
-
type: "object",
|
|
13
|
-
properties: {
|
|
14
|
-
query: { type: "string" },
|
|
15
|
-
},
|
|
16
|
-
required: ["query"],
|
|
17
|
-
additionalProperties: false,
|
|
18
|
-
};
|
|
19
|
-
const TEST_TOOL_STANDARD_SCHEMA = {
|
|
20
|
-
"~standard": {
|
|
21
|
-
version: 1,
|
|
22
|
-
vendor: "eden-test-configured-artifact",
|
|
23
|
-
validate(value) {
|
|
24
|
-
if (typeof value !== "object" ||
|
|
25
|
-
value === null ||
|
|
26
|
-
Array.isArray(value) ||
|
|
27
|
-
typeof value.query !== "string") {
|
|
28
|
-
return {
|
|
29
|
-
issues: [{ message: "query must be a string", path: ["query"] }],
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
return {
|
|
33
|
-
value: {
|
|
34
|
-
query: value.query.trim(),
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
const testToolExecutionCounts = new Map();
|
|
41
|
-
const testToolInvocationCounts = new Map();
|
|
42
|
-
const TEST_INVOCATION_PATH = "/__test/tool-invocations/";
|
|
43
|
-
function testJsonResponse(body, status = 200) {
|
|
44
|
-
return new Response(JSON.stringify(body), {
|
|
45
|
-
status,
|
|
46
|
-
headers: {
|
|
47
|
-
"content-type": "application/json; charset=utf-8",
|
|
48
|
-
"cache-control": "no-store",
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function readTestBearer(request) {
|
|
53
|
-
const header = request.headers.get("authorization");
|
|
54
|
-
if (header === null || !header.startsWith("Bearer "))
|
|
55
|
-
return undefined;
|
|
56
|
-
const value = header.slice("Bearer ".length);
|
|
57
|
-
return value.length === 0 || value.includes(" ") ? undefined : value;
|
|
58
|
-
}
|
|
59
|
-
function constantTimeTestBearerEqual(left, right) {
|
|
60
|
-
const leftBytes = new TextEncoder().encode(left);
|
|
61
|
-
const rightBytes = new TextEncoder().encode(right);
|
|
62
|
-
const length = Math.max(leftBytes.length, rightBytes.length);
|
|
63
|
-
let difference = leftBytes.length ^ rightBytes.length;
|
|
64
|
-
for (let index = 0; index < length; index += 1) {
|
|
65
|
-
difference |= (leftBytes[index] ?? 0) ^ (rightBytes[index] ?? 0);
|
|
66
|
-
}
|
|
67
|
-
return difference === 0;
|
|
68
|
-
}
|
|
69
|
-
function testInvocationResponse(request, env) {
|
|
70
|
-
const url = new URL(request.url);
|
|
71
|
-
if (!url.pathname.startsWith(TEST_INVOCATION_PATH))
|
|
72
|
-
return undefined;
|
|
73
|
-
if (request.method !== "GET") {
|
|
74
|
-
return testJsonResponse({ code: "not_found", message: "Resource was not found." }, 404);
|
|
75
|
-
}
|
|
76
|
-
const bearer = readTestBearer(request);
|
|
77
|
-
if (env.EDEN_BEARER_SECRET === undefined ||
|
|
78
|
-
bearer === undefined ||
|
|
79
|
-
!constantTimeTestBearerEqual(bearer, env.EDEN_BEARER_SECRET)) {
|
|
80
|
-
return testJsonResponse({ code: "unauthorized", message: "Authorization is required." }, 401);
|
|
81
|
-
}
|
|
82
|
-
const sessionId = url.pathname.slice(TEST_INVOCATION_PATH.length);
|
|
83
|
-
if (!isOpaqueSessionId(sessionId) || sessionId.includes("/")) {
|
|
84
|
-
return testJsonResponse({ code: "not_found", message: "Resource was not found." }, 404);
|
|
85
|
-
}
|
|
86
|
-
return testJsonResponse({
|
|
87
|
-
sessionId,
|
|
88
|
-
count: testToolInvocationCounts.get(sessionId) ?? 0,
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
const TEST_TOOL = {
|
|
92
|
-
description: "Return the configured test-worker artifact result.",
|
|
93
|
-
inputSchema: TEST_TOOL_STANDARD_SCHEMA,
|
|
94
|
-
execute(input, context) {
|
|
95
|
-
testToolInvocationCounts.set(context.sessionId, (testToolInvocationCounts.get(context.sessionId) ?? 0) + 1);
|
|
96
|
-
if (input.query === "interrupted") {
|
|
97
|
-
throw new Error("deterministic uncommitted interruption: provider secret sentinel binding sentinel-binding");
|
|
98
|
-
}
|
|
99
|
-
const executionCount = input.query === "replay"
|
|
100
|
-
? (testToolExecutionCounts.get(context.idempotencyKey) ?? 0) + 1
|
|
101
|
-
: undefined;
|
|
102
|
-
if (executionCount !== undefined) {
|
|
103
|
-
testToolExecutionCounts.set(context.idempotencyKey, executionCount);
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
source: "test-worker-configured-artifact",
|
|
107
|
-
query: input.query,
|
|
108
|
-
...(executionCount === undefined ? {} : { executionCount }),
|
|
109
|
-
};
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
// This deterministic model is injected only by the Workers test entrypoint.
|
|
113
|
-
// Remote generated wrappers select the Workers AI adapter instead.
|
|
114
|
-
const TEST_MODEL = createModelAdapter(async (request) => {
|
|
115
|
-
const configured = readConfiguredEdenArtifact();
|
|
116
|
-
const toolName = configured?.generation.toolNames[0];
|
|
117
|
-
const tool = toolName === undefined
|
|
118
|
-
? undefined
|
|
119
|
-
: configured?.artifact.tools[toolName];
|
|
120
|
-
const toolInputSchema = toolName === undefined
|
|
121
|
-
? undefined
|
|
122
|
-
: configured?.artifact.toolSchemas[toolName];
|
|
123
|
-
if (request.messages.some((message) => message.role === "tool")) {
|
|
124
|
-
const toolResult = request.messages
|
|
125
|
-
.flatMap((message) => typeof message.content === "string" ? [] : message.content)
|
|
126
|
-
.find((part) => part.type === "tool-result");
|
|
127
|
-
if (toolResult?.type !== "tool-result" ||
|
|
128
|
-
toolName === undefined ||
|
|
129
|
-
toolResult.toolName !== toolName ||
|
|
130
|
-
toolResult.output === undefined) {
|
|
131
|
-
throw new Error("Configured test artifact result was not provided");
|
|
132
|
-
}
|
|
133
|
-
return {
|
|
134
|
-
text: "configured-artifact-final-response: ✓",
|
|
135
|
-
finishReason: "stop",
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
const requestedTool = request.tools?.[0];
|
|
139
|
-
if (configured === undefined ||
|
|
140
|
-
request.messages[0]?.role !== "system" ||
|
|
141
|
-
request.messages[0]?.content !== configured.artifact.instructions ||
|
|
142
|
-
request.modelId !== configured.artifact.agent.model ||
|
|
143
|
-
request.options?.thinking !== configured.artifact.agent.options?.thinking ||
|
|
144
|
-
request.options?.maxOutputTokens !==
|
|
145
|
-
configured.artifact.agent.options?.maxOutputTokens ||
|
|
146
|
-
toolName === undefined ||
|
|
147
|
-
tool === undefined ||
|
|
148
|
-
requestedTool?.name !== toolName ||
|
|
149
|
-
requestedTool.description !== tool.description ||
|
|
150
|
-
JSON.stringify(requestedTool.inputSchema) !== JSON.stringify(toolInputSchema)) {
|
|
151
|
-
throw new Error("Configured artifact did not reach the bounded model runner");
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
toolCalls: [
|
|
155
|
-
{
|
|
156
|
-
toolCallId: `call_${toolName}`,
|
|
157
|
-
toolName,
|
|
158
|
-
input: toolName === "greet"
|
|
159
|
-
? { name: " Eden " }
|
|
160
|
-
: request.messages.some((message) => message.role === "user" &&
|
|
161
|
-
typeof message.content === "string" &&
|
|
162
|
-
message.content.includes("invalid"))
|
|
163
|
-
? { query: 42 }
|
|
164
|
-
: request.messages.some((message) => message.role === "user" &&
|
|
165
|
-
typeof message.content === "string" &&
|
|
166
|
-
message.content.includes("interrupted"))
|
|
167
|
-
? { query: " interrupted " }
|
|
168
|
-
: request.messages.some((message) => message.role === "user" &&
|
|
169
|
-
typeof message.content === "string" &&
|
|
170
|
-
message.content.includes("completed-replay"))
|
|
171
|
-
? { query: " replay " }
|
|
172
|
-
: { query: " eden " },
|
|
173
|
-
},
|
|
174
|
-
],
|
|
175
|
-
finishReason: "tool-calls",
|
|
176
|
-
};
|
|
177
|
-
});
|
|
178
|
-
const TEST_ARTIFACT = Object.freeze({
|
|
179
|
-
agent: Object.freeze({
|
|
180
|
-
model: "test/configured-model",
|
|
181
|
-
options: Object.freeze({
|
|
182
|
-
maxOutputTokens: 321,
|
|
183
|
-
thinking: false,
|
|
184
|
-
}),
|
|
185
|
-
}),
|
|
186
|
-
instructions: "Configured test-worker instructions.\n",
|
|
187
|
-
tools: Object.freeze({
|
|
188
|
-
configured_lookup: TEST_TOOL,
|
|
189
|
-
}),
|
|
190
|
-
toolSchemas: Object.freeze({
|
|
191
|
-
configured_lookup: TEST_TOOL_INPUT_SCHEMA,
|
|
192
|
-
}),
|
|
193
|
-
});
|
|
194
|
-
configureEdenArtifact(TEST_ARTIFACT, {
|
|
195
|
-
generationId: "configured-test-generation",
|
|
196
|
-
bundleDigest: "configured-test-bundle",
|
|
197
|
-
manifestVersion: "configured-test-manifest",
|
|
198
|
-
runtimeVersion: "configured-test-runtime",
|
|
199
|
-
agentBundleVersion: "configured-test-agent-bundle",
|
|
200
|
-
protocolVersion: "configured-test-protocol",
|
|
201
|
-
schemaVersion: 1,
|
|
202
|
-
toolNames: ["configured_lookup"],
|
|
203
|
-
executionMode: "local",
|
|
204
|
-
});
|
|
205
|
-
configureEdenTestModel(TEST_MODEL);
|
|
206
|
-
export default {
|
|
207
|
-
fetch(request, env) {
|
|
208
|
-
const testInvocation = testInvocationResponse(request, env);
|
|
209
|
-
if (testInvocation !== undefined)
|
|
210
|
-
return Promise.resolve(testInvocation);
|
|
211
|
-
return handleEdenRequest(request, env);
|
|
212
|
-
},
|
|
213
|
-
};
|
|
214
|
-
//# sourceMappingURL=test-worker.js.map
|
package/dist/tool-harness.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { EdenJsonValue, EdenToolDefinition } from "@moinulmoin/eden-definitions";
|
|
2
|
-
import { type CheckpointRequest } from "./session-checkpoint.js";
|
|
3
|
-
import { type EdenSessionStorage } from "./session-journal.js";
|
|
4
|
-
export interface EdenToolHarnessRequest<TInput, TOutput extends EdenJsonValue = EdenJsonValue> extends Omit<CheckpointRequest, "input"> {
|
|
5
|
-
readonly input: EdenJsonValue;
|
|
6
|
-
readonly tool: EdenToolDefinition<TInput, TOutput>;
|
|
7
|
-
readonly signal?: AbortSignal;
|
|
8
|
-
}
|
|
9
|
-
export type EdenToolFailureCode = "tool_input_invalid" | "tool_output_invalid";
|
|
10
|
-
export interface EdenToolFailure {
|
|
11
|
-
readonly code: EdenToolFailureCode;
|
|
12
|
-
readonly message: string;
|
|
13
|
-
readonly retryable: false;
|
|
14
|
-
}
|
|
15
|
-
export type EdenToolHarnessResult<TOutput extends EdenJsonValue = EdenJsonValue> = {
|
|
16
|
-
readonly status: "committed" | "replayed";
|
|
17
|
-
readonly output: TOutput;
|
|
18
|
-
readonly idempotencyKey: string;
|
|
19
|
-
readonly attemptCount: number;
|
|
20
|
-
} | {
|
|
21
|
-
readonly status: "failed";
|
|
22
|
-
readonly error: EdenToolFailure;
|
|
23
|
-
readonly idempotencyKey: string;
|
|
24
|
-
readonly attemptCount: number;
|
|
25
|
-
} | {
|
|
26
|
-
readonly status: "stale" | "exhausted";
|
|
27
|
-
readonly idempotencyKey: string;
|
|
28
|
-
readonly attemptCount: number;
|
|
29
|
-
};
|
|
30
|
-
export declare function executeTypedTool<TInput, TOutput extends EdenJsonValue = EdenJsonValue>(storage: EdenSessionStorage, request: EdenToolHarnessRequest<TInput, TOutput>): Promise<EdenToolHarnessResult<TOutput>>;
|
|
31
|
-
//# sourceMappingURL=tool-harness.d.ts.map
|
package/dist/tool-harness.js
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import { normalizeEdenJsonValue } from "./model-normalizers.js";
|
|
2
|
-
import { commitCheckpointResult, createStableEffectIdempotencyKey, markCheckpointRetryable, prepareCheckpointAttempt, } from "./session-checkpoint.js";
|
|
3
|
-
import { commitSessionTransaction, } from "./session-journal.js";
|
|
4
|
-
const TOOL_INPUT_FAILURE = Object.freeze({
|
|
5
|
-
code: "tool_input_invalid",
|
|
6
|
-
message: "Tool input failed schema validation.",
|
|
7
|
-
retryable: false,
|
|
8
|
-
});
|
|
9
|
-
const TOOL_OUTPUT_FAILURE = Object.freeze({
|
|
10
|
-
code: "tool_output_invalid",
|
|
11
|
-
message: "Tool output was not JSON-compatible.",
|
|
12
|
-
retryable: false,
|
|
13
|
-
});
|
|
14
|
-
function isRecord(value) {
|
|
15
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
16
|
-
}
|
|
17
|
-
function hasOwn(value, key) {
|
|
18
|
-
return Object.prototype.hasOwnProperty.call(value, key);
|
|
19
|
-
}
|
|
20
|
-
function validIssuePath(value) {
|
|
21
|
-
return (value === undefined ||
|
|
22
|
-
(Array.isArray(value) &&
|
|
23
|
-
value.every((segment) => typeof segment === "string" || typeof segment === "number")));
|
|
24
|
-
}
|
|
25
|
-
function validIssue(value) {
|
|
26
|
-
return (isRecord(value) &&
|
|
27
|
-
typeof value.message === "string" &&
|
|
28
|
-
value.message.trim().length > 0 &&
|
|
29
|
-
validIssuePath(value.path));
|
|
30
|
-
}
|
|
31
|
-
function parseSchemaResult(value) {
|
|
32
|
-
if (!isRecord(value))
|
|
33
|
-
return undefined;
|
|
34
|
-
const hasValue = hasOwn(value, "value");
|
|
35
|
-
const hasIssues = hasOwn(value, "issues");
|
|
36
|
-
if (hasValue && !hasIssues) {
|
|
37
|
-
return { value: value.value };
|
|
38
|
-
}
|
|
39
|
-
if (!hasValue &&
|
|
40
|
-
hasIssues &&
|
|
41
|
-
Array.isArray(value.issues) &&
|
|
42
|
-
value.issues.length > 0 &&
|
|
43
|
-
value.issues.every(validIssue)) {
|
|
44
|
-
return {
|
|
45
|
-
issues: value.issues,
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
async function validateToolInput(schema, input) {
|
|
51
|
-
const descriptor = schema?.["~standard"];
|
|
52
|
-
if (descriptor === undefined ||
|
|
53
|
-
descriptor.version !== 1 ||
|
|
54
|
-
typeof descriptor.vendor !== "string" ||
|
|
55
|
-
descriptor.vendor.trim().length === 0 ||
|
|
56
|
-
typeof descriptor.validate !== "function") {
|
|
57
|
-
return { status: "invalid" };
|
|
58
|
-
}
|
|
59
|
-
let rawResult;
|
|
60
|
-
try {
|
|
61
|
-
rawResult = await descriptor.validate(input);
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
return { status: "invalid" };
|
|
65
|
-
}
|
|
66
|
-
const result = parseSchemaResult(rawResult);
|
|
67
|
-
if (result === undefined || "issues" in result) {
|
|
68
|
-
return { status: "invalid" };
|
|
69
|
-
}
|
|
70
|
-
return { status: "valid", value: result.value };
|
|
71
|
-
}
|
|
72
|
-
function contextFor(request, prepared) {
|
|
73
|
-
return Object.freeze({
|
|
74
|
-
sessionId: request.sessionId,
|
|
75
|
-
turnId: request.turnId,
|
|
76
|
-
callId: request.callId,
|
|
77
|
-
toolName: request.toolName,
|
|
78
|
-
idempotencyKey: prepared.idempotencyKey,
|
|
79
|
-
signal: request.signal ?? new AbortController().signal,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
function now() {
|
|
83
|
-
return new Date().toISOString();
|
|
84
|
-
}
|
|
85
|
-
function failTool(storage, request, prepared, toolFailure) {
|
|
86
|
-
const committed = commitSessionTransaction(storage, request.sessionId, (journal) => {
|
|
87
|
-
const timestamp = now();
|
|
88
|
-
const errorId = toolFailure.code === "tool_input_invalid"
|
|
89
|
-
? `err_tool_input_${request.stepId}`
|
|
90
|
-
: `err_tool_output_${request.stepId}`;
|
|
91
|
-
const step = storage.sql
|
|
92
|
-
.exec(`SELECT status, attempt_count
|
|
93
|
-
FROM steps
|
|
94
|
-
WHERE session_id = ? AND step_id = ?`, request.sessionId, request.stepId)
|
|
95
|
-
.toArray()[0];
|
|
96
|
-
const effect = storage.sql
|
|
97
|
-
.exec(`SELECT status
|
|
98
|
-
FROM effects
|
|
99
|
-
WHERE session_id = ? AND effect_id = ?`, request.sessionId, request.effectId)
|
|
100
|
-
.toArray()[0];
|
|
101
|
-
if (step === undefined ||
|
|
102
|
-
effect === undefined ||
|
|
103
|
-
step.status !== "running" ||
|
|
104
|
-
step.attempt_count !== prepared.attemptCount ||
|
|
105
|
-
effect.status !== "running") {
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
journal.failEffect({
|
|
109
|
-
effectId: request.effectId,
|
|
110
|
-
error: {
|
|
111
|
-
code: toolFailure.code,
|
|
112
|
-
message: toolFailure.message,
|
|
113
|
-
retryable: toolFailure.retryable,
|
|
114
|
-
},
|
|
115
|
-
updatedAt: timestamp,
|
|
116
|
-
});
|
|
117
|
-
journal.updateStep({
|
|
118
|
-
stepId: request.stepId,
|
|
119
|
-
status: "failed",
|
|
120
|
-
attemptCount: prepared.attemptCount,
|
|
121
|
-
resultRef: null,
|
|
122
|
-
errorId,
|
|
123
|
-
updatedAt: timestamp,
|
|
124
|
-
completedAt: timestamp,
|
|
125
|
-
});
|
|
126
|
-
journal.recordError({
|
|
127
|
-
errorId,
|
|
128
|
-
turnId: request.turnId,
|
|
129
|
-
stepId: request.stepId,
|
|
130
|
-
code: toolFailure.code,
|
|
131
|
-
message: toolFailure.message,
|
|
132
|
-
retryable: toolFailure.retryable,
|
|
133
|
-
createdAt: timestamp,
|
|
134
|
-
});
|
|
135
|
-
journal.appendEvent({
|
|
136
|
-
type: "step.failed",
|
|
137
|
-
turnId: request.turnId,
|
|
138
|
-
stepId: request.stepId,
|
|
139
|
-
data: {
|
|
140
|
-
stepId: request.stepId,
|
|
141
|
-
code: toolFailure.code,
|
|
142
|
-
message: toolFailure.message,
|
|
143
|
-
retryable: toolFailure.retryable,
|
|
144
|
-
},
|
|
145
|
-
committedAt: timestamp,
|
|
146
|
-
});
|
|
147
|
-
return true;
|
|
148
|
-
});
|
|
149
|
-
if (!committed) {
|
|
150
|
-
return {
|
|
151
|
-
status: "stale",
|
|
152
|
-
idempotencyKey: prepared.idempotencyKey,
|
|
153
|
-
attemptCount: prepared.attemptCount,
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
return {
|
|
157
|
-
status: "failed",
|
|
158
|
-
error: toolFailure,
|
|
159
|
-
idempotencyKey: prepared.idempotencyKey,
|
|
160
|
-
attemptCount: prepared.attemptCount,
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
function resultFromPreparation(storage, request, preparation, idempotencyKey) {
|
|
164
|
-
if (preparation.status === "replayed") {
|
|
165
|
-
const row = storage.sql
|
|
166
|
-
.exec(`SELECT attempt_count
|
|
167
|
-
FROM steps
|
|
168
|
-
WHERE session_id = ? AND step_id = ?`, request.sessionId, request.stepId)
|
|
169
|
-
.toArray()[0];
|
|
170
|
-
return {
|
|
171
|
-
status: "replayed",
|
|
172
|
-
output: preparation.output,
|
|
173
|
-
idempotencyKey,
|
|
174
|
-
attemptCount: row?.attempt_count ?? 0,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
if (preparation.status === "exhausted") {
|
|
178
|
-
return {
|
|
179
|
-
status: "exhausted",
|
|
180
|
-
idempotencyKey,
|
|
181
|
-
attemptCount: preparation.attemptCount,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
return undefined;
|
|
185
|
-
}
|
|
186
|
-
export async function executeTypedTool(storage, request) {
|
|
187
|
-
const idempotencyKey = createStableEffectIdempotencyKey(request);
|
|
188
|
-
const preparation = prepareCheckpointAttempt(storage, request);
|
|
189
|
-
const preparedResult = resultFromPreparation(storage, request, preparation, idempotencyKey);
|
|
190
|
-
if (preparedResult !== undefined)
|
|
191
|
-
return preparedResult;
|
|
192
|
-
if (preparation.status !== "execute") {
|
|
193
|
-
throw new Error("Tool checkpoint preparation was incomplete");
|
|
194
|
-
}
|
|
195
|
-
const validated = await validateToolInput(request.tool.inputSchema, request.input);
|
|
196
|
-
if (validated.status === "invalid") {
|
|
197
|
-
return failTool(storage, request, preparation.prepared, TOOL_INPUT_FAILURE);
|
|
198
|
-
}
|
|
199
|
-
let output;
|
|
200
|
-
try {
|
|
201
|
-
output = await request.tool.execute(validated.value, contextFor(request, preparation.prepared));
|
|
202
|
-
}
|
|
203
|
-
catch (error) {
|
|
204
|
-
markCheckpointRetryable(storage, preparation.prepared);
|
|
205
|
-
throw error;
|
|
206
|
-
}
|
|
207
|
-
const normalizedOutput = normalizeEdenJsonValue(output);
|
|
208
|
-
if (normalizedOutput === undefined) {
|
|
209
|
-
return failTool(storage, request, preparation.prepared, TOOL_OUTPUT_FAILURE);
|
|
210
|
-
}
|
|
211
|
-
const committed = commitCheckpointResult(storage, preparation.prepared, normalizedOutput);
|
|
212
|
-
if (committed.status === "committed" || committed.status === "replayed") {
|
|
213
|
-
return {
|
|
214
|
-
status: committed.status,
|
|
215
|
-
output: committed.output,
|
|
216
|
-
idempotencyKey,
|
|
217
|
-
attemptCount: preparation.attemptCount,
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
return {
|
|
221
|
-
status: committed.status,
|
|
222
|
-
idempotencyKey,
|
|
223
|
-
attemptCount: preparation.attemptCount,
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
//# sourceMappingURL=tool-harness.js.map
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { EdenEvent, EdenEventType, EdenJsonValue } from "@moinulmoin/eden-definitions";
|
|
2
|
-
import type { EdenModelResult } from "./model-adapter.js";
|
|
3
|
-
import { type EdenSessionStorage, type EdenSqlStorage } from "./session-journal.js";
|
|
4
|
-
import { type BeginTurnResult, type EdenBoundedTurnRequest, type StepPreparation, type StoredFailure, type TurnIdentity, type TurnMessageIdentity } from "./turn-runner-types.js";
|
|
5
|
-
export declare function readProjection(sql: EdenSqlStorage, sessionId: string, key: string): EdenModelResult | undefined;
|
|
6
|
-
export declare function deliverNewEvents(storage: EdenSessionStorage, sessionId: string, cursor: number, onEvent: ((event: EdenEvent<EdenEventType>) => void | Promise<void>) | undefined): Promise<number>;
|
|
7
|
-
export declare function beginTurn<TInput, TOutput extends EdenJsonValue>(storage: EdenSessionStorage, request: EdenBoundedTurnRequest<TInput, TOutput>): BeginTurnResult;
|
|
8
|
-
export declare function ensureModelToolStep(storage: EdenSessionStorage, request: TurnIdentity): void;
|
|
9
|
-
export declare function prepareModelAttempt(storage: EdenSessionStorage, request: TurnIdentity): StepPreparation;
|
|
10
|
-
export declare function commitModelProjection(storage: EdenSessionStorage, request: TurnIdentity, attemptCount: number, result: EdenModelResult): boolean;
|
|
11
|
-
export declare function recordStepFailure(storage: EdenSessionStorage, request: TurnIdentity, stepId: string, input: StoredFailure): {
|
|
12
|
-
readonly failure: StoredFailure;
|
|
13
|
-
readonly stepWasAlreadyFailed: boolean;
|
|
14
|
-
};
|
|
15
|
-
export declare function recordTurnFailure(storage: EdenSessionStorage, request: TurnIdentity, input: StoredFailure): void;
|
|
16
|
-
export declare function completeModelToolStep(storage: EdenSessionStorage, request: TurnIdentity): void;
|
|
17
|
-
export declare function persistToolMessage(storage: EdenSessionStorage, request: TurnIdentity, call: {
|
|
18
|
-
readonly callId: string;
|
|
19
|
-
}, output: EdenJsonValue): void;
|
|
20
|
-
export declare function prepareFinalStep(storage: EdenSessionStorage, request: TurnIdentity): {
|
|
21
|
-
readonly status: "execute";
|
|
22
|
-
readonly attemptCount: number;
|
|
23
|
-
} | {
|
|
24
|
-
readonly status: "failed";
|
|
25
|
-
readonly failure: StoredFailure;
|
|
26
|
-
};
|
|
27
|
-
export declare function commitFinalResponse(storage: EdenSessionStorage, request: TurnMessageIdentity, content: string, attemptCount: number): void;
|
|
28
|
-
//# sourceMappingURL=turn-runner-storage.d.ts.map
|