@cloudflare/codemode 0.3.7 → 0.4.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/LICENSE +21 -0
- package/README.md +251 -7
- package/dist/ai.d.ts +50 -7
- package/dist/ai.js +93 -5
- package/dist/ai.js.map +1 -1
- package/dist/base-B2amchZA.d.ts +239 -0
- package/dist/base-CHRR_a5j.js +156 -0
- package/dist/base-CHRR_a5j.js.map +1 -0
- package/dist/browser.d.ts +6 -6
- package/dist/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/{executor-BRm90XJT.d.ts → executor-BIs2dr7X.d.ts} +48 -10
- package/dist/index.d.ts +608 -8
- package/dist/index.js +1690 -17
- package/dist/index.js.map +1 -1
- package/dist/{json-schema-types-CVt4X9cw.js → json-schema-types-BGjW4KPy.js} +1 -1
- package/dist/{json-schema-types-CVt4X9cw.js.map → json-schema-types-BGjW4KPy.js.map} +1 -1
- package/dist/{json-schema-types-1OL3uao0.d.ts → json-schema-types-Cb2wClAd.d.ts} +1 -1
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +4 -3
- package/dist/mcp.js.map +1 -1
- package/dist/{resolve-DdyeEp3l.js → resolve-34dC47Il.js} +1 -1
- package/dist/{resolve-DdyeEp3l.js.map → resolve-34dC47Il.js.map} +1 -1
- package/dist/{resolve-DtfaKmms.d.ts → resolve-CBr_VoPl.d.ts} +4 -4
- package/dist/{run-code-5x8S5YWt.js → run-code-aKHcTgf9.js} +4 -4
- package/dist/run-code-aKHcTgf9.js.map +1 -0
- package/dist/{shared-TyFN6W7_.d.ts → shared-Ce-zCEBP.d.ts} +3 -3
- package/dist/{shared-xjl2irqK.js → shared-SMjV5yN4.js} +1 -1
- package/dist/{shared-xjl2irqK.js.map → shared-SMjV5yN4.js.map} +1 -1
- package/dist/tanstack-ai.d.ts +3 -3
- package/dist/tanstack-ai.js +4 -4
- package/dist/tanstack-ai.js.map +1 -1
- package/dist/vite.d.ts +7 -0
- package/dist/vite.js +122 -0
- package/dist/vite.js.map +1 -0
- package/package.json +19 -14
- package/dist/run-code-5x8S5YWt.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,53 @@
|
|
|
1
1
|
import {
|
|
2
|
-
c as
|
|
2
|
+
c as ExecuteResult,
|
|
3
3
|
i as ToolProvider,
|
|
4
|
+
l as Executor,
|
|
4
5
|
n as DynamicWorkerExecutorOptions,
|
|
5
|
-
o as
|
|
6
|
+
o as ConnectorBinding,
|
|
6
7
|
r as ToolDispatcher,
|
|
7
|
-
s as
|
|
8
|
-
t as DynamicWorkerExecutor
|
|
9
|
-
|
|
8
|
+
s as ExecuteOptions,
|
|
9
|
+
t as DynamicWorkerExecutor,
|
|
10
|
+
u as ResolvedProvider
|
|
11
|
+
} from "./executor-BIs2dr7X.js";
|
|
12
|
+
import { n as CodeOutput } from "./shared-Ce-zCEBP.js";
|
|
10
13
|
import {
|
|
11
14
|
i as jsonSchemaToType,
|
|
12
15
|
n as JsonSchemaToolDescriptors,
|
|
13
16
|
r as generateTypesFromJsonSchema,
|
|
14
17
|
t as JsonSchemaToolDescriptor
|
|
15
|
-
} from "./json-schema-types-
|
|
16
|
-
import {
|
|
18
|
+
} from "./json-schema-types-Cb2wClAd.js";
|
|
19
|
+
import {
|
|
20
|
+
a as DescribeOutput,
|
|
21
|
+
c as SearchOutput,
|
|
22
|
+
d as ToolExecuteContext,
|
|
23
|
+
i as ConnectorDescription,
|
|
24
|
+
l as SearchResult,
|
|
25
|
+
n as ConnectorTool,
|
|
26
|
+
o as ExecutionEndStatus,
|
|
27
|
+
r as ConnectorTools,
|
|
28
|
+
s as PassEndStatus,
|
|
29
|
+
t as CodemodeConnector,
|
|
30
|
+
u as ToolAnnotations
|
|
31
|
+
} from "./base-B2amchZA.js";
|
|
32
|
+
import { t as resolveProvider } from "./resolve-CBr_VoPl.js";
|
|
33
|
+
import { DurableObject } from "cloudflare:workers";
|
|
34
|
+
import { Tool } from "ai";
|
|
35
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
36
|
+
import { Tool as Tool$1 } from "@modelcontextprotocol/sdk/types.js";
|
|
17
37
|
|
|
38
|
+
//#region src/run-code.d.ts
|
|
39
|
+
declare function runCode({
|
|
40
|
+
code,
|
|
41
|
+
executor,
|
|
42
|
+
providers,
|
|
43
|
+
connectors
|
|
44
|
+
}: {
|
|
45
|
+
code: string;
|
|
46
|
+
executor: Executor;
|
|
47
|
+
providers: ResolvedProvider[];
|
|
48
|
+
connectors?: ConnectorBinding[];
|
|
49
|
+
}): Promise<CodeOutput>;
|
|
50
|
+
//#endregion
|
|
18
51
|
//#region src/utils.d.ts
|
|
19
52
|
/**
|
|
20
53
|
* Sanitize a tool name into a valid JavaScript identifier.
|
|
@@ -26,20 +59,587 @@ declare function sanitizeToolName(name: string): string;
|
|
|
26
59
|
//#region src/normalize.d.ts
|
|
27
60
|
declare function normalizeCode(code: string): string;
|
|
28
61
|
//#endregion
|
|
62
|
+
//#region src/truncate.d.ts
|
|
63
|
+
/**
|
|
64
|
+
* Result truncation utilities.
|
|
65
|
+
*
|
|
66
|
+
* Tool and sandbox results can be large enough to blow a model's context
|
|
67
|
+
* window. These cap the serialized size of a value while leaving small,
|
|
68
|
+
* structured results intact so the model can still reason over them. They are
|
|
69
|
+
* the default building blocks for a `transformResult` hook (see
|
|
70
|
+
* `createCodemodeRuntime`).
|
|
71
|
+
*/
|
|
72
|
+
type TruncateOptions = {
|
|
73
|
+
/**
|
|
74
|
+
* Maximum characters in the (serialized) output before truncation kicks in.
|
|
75
|
+
* Defaults to `maxTokens * 4`.
|
|
76
|
+
*/
|
|
77
|
+
maxChars?: number /** Token budget used to derive the default `maxChars`. Defaults to 6000. */;
|
|
78
|
+
maxTokens?: number;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Truncate a text response to a character budget, appending a marker that notes
|
|
82
|
+
* the original size so the model knows the output was clipped. Returns the
|
|
83
|
+
* input unchanged when it is within budget.
|
|
84
|
+
*/
|
|
85
|
+
declare function truncateResponse(
|
|
86
|
+
text: string,
|
|
87
|
+
options?: TruncateOptions
|
|
88
|
+
): string;
|
|
89
|
+
/**
|
|
90
|
+
* Truncate a structured result. Strings are truncated directly. Other values
|
|
91
|
+
* pass through unchanged when their JSON serialization is within budget; when
|
|
92
|
+
* oversized, the serialized form is truncated and returned as a string, so the
|
|
93
|
+
* model still gets a usable, bounded preview rather than a dropped result.
|
|
94
|
+
*
|
|
95
|
+
* Values that can't be serialized (cycles, bigint, `undefined`) are returned
|
|
96
|
+
* unchanged.
|
|
97
|
+
*/
|
|
98
|
+
declare function truncateResult(
|
|
99
|
+
value: unknown,
|
|
100
|
+
options?: TruncateOptions
|
|
101
|
+
): unknown;
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/snippet.d.ts
|
|
104
|
+
/**
|
|
105
|
+
* Codemode snippets.
|
|
106
|
+
*
|
|
107
|
+
* A snippet is a saved sandbox script — a reusable code pattern that already
|
|
108
|
+
* ran and worked. Snippets are durable: they live on the CodemodeRuntime
|
|
109
|
+
* facet, are addressable by name, and accumulate over time as the developer
|
|
110
|
+
* promotes working executions with `runtime.saveSnippet(name)`.
|
|
111
|
+
*
|
|
112
|
+
* Connectors provide raw capability. Snippets are recipes that worked. The
|
|
113
|
+
* model reuses them (`codemode.run`, `codemode.search`); the developer decides
|
|
114
|
+
* what is worth keeping.
|
|
115
|
+
*/
|
|
116
|
+
/** A saved, addressable sandbox script. */
|
|
117
|
+
interface Snippet {
|
|
118
|
+
/** Unique name. Appears in codemode.search and addresses codemode.run. */
|
|
119
|
+
name: string;
|
|
120
|
+
/** Short description for search/catalog. */
|
|
121
|
+
description: string;
|
|
122
|
+
/** The script — an async function source string, as written in the sandbox. */
|
|
123
|
+
code: string;
|
|
124
|
+
/** When the snippet was saved (epoch ms). */
|
|
125
|
+
savedAt: number;
|
|
126
|
+
/** Optional JSON Schema for the input passed to codemode.run(name, input). */
|
|
127
|
+
inputSchema?: unknown;
|
|
128
|
+
/**
|
|
129
|
+
* Connector names the source execution ran with. Recorded so a later
|
|
130
|
+
* `codemode.run` can verify the required connectors are still configured on
|
|
131
|
+
* the runtime, with a clear error when one is missing.
|
|
132
|
+
*/
|
|
133
|
+
connectors?: string[];
|
|
134
|
+
}
|
|
135
|
+
/** Options when promoting an execution's script to a saved snippet. */
|
|
136
|
+
interface SaveSnippetOptions {
|
|
137
|
+
description?: string;
|
|
138
|
+
inputSchema?: unknown;
|
|
139
|
+
/** Execution to take the code from (from the tool output or listExecutions). */
|
|
140
|
+
executionId: string;
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/runtime.d.ts
|
|
144
|
+
type ToolLogEntryState =
|
|
145
|
+
| "executing"
|
|
146
|
+
| "applied"
|
|
147
|
+
| "pending"
|
|
148
|
+
| "reverted"
|
|
149
|
+
| "error";
|
|
150
|
+
type ToolLogEntry = {
|
|
151
|
+
seq: number;
|
|
152
|
+
connector: string;
|
|
153
|
+
method: string;
|
|
154
|
+
args: unknown /** Recorded result for replay. Present once applied (never for ephemeral). */;
|
|
155
|
+
result?: unknown /** Whether this call required approval (vs. a read or step). */;
|
|
156
|
+
requiresApproval: boolean;
|
|
157
|
+
/**
|
|
158
|
+
* Ephemeral entries (`replay: "reexecute"` tools) re-execute on replay
|
|
159
|
+
* instead of replaying a recorded result; their result is never stored.
|
|
160
|
+
*/
|
|
161
|
+
ephemeral?: boolean;
|
|
162
|
+
state: ToolLogEntryState;
|
|
163
|
+
};
|
|
164
|
+
type ExecutionStatus =
|
|
165
|
+
| "running"
|
|
166
|
+
| "paused"
|
|
167
|
+
| "completed"
|
|
168
|
+
| "error"
|
|
169
|
+
| "rejected"
|
|
170
|
+
| "rolled_back";
|
|
171
|
+
type ExecutionState = {
|
|
172
|
+
id: string;
|
|
173
|
+
code: string;
|
|
174
|
+
status: ExecutionStatus;
|
|
175
|
+
log: ToolLogEntry[];
|
|
176
|
+
result?: unknown;
|
|
177
|
+
error?: string;
|
|
178
|
+
logs?: string[];
|
|
179
|
+
/**
|
|
180
|
+
* Connector names the execution was started with. Recorded so a later
|
|
181
|
+
* resume can verify the required connectors are still configured.
|
|
182
|
+
*/
|
|
183
|
+
connectors?: string[] /** Epoch ms the execution was created. */;
|
|
184
|
+
createdAt: number /** Epoch ms of the last state change. */;
|
|
185
|
+
updatedAt: number;
|
|
186
|
+
};
|
|
187
|
+
type PendingAction = {
|
|
188
|
+
executionId: string;
|
|
189
|
+
seq: number;
|
|
190
|
+
connector: string;
|
|
191
|
+
method: string;
|
|
192
|
+
args: unknown;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* The decision the runtime returns for a single tool call or step during a run.
|
|
196
|
+
* - "replay": return `result` without executing
|
|
197
|
+
* - "execute": execute, then report the result back via `recordResult`
|
|
198
|
+
* - "pause": stop the run (the binding throws the pause sentinel)
|
|
199
|
+
*/
|
|
200
|
+
type ToolDecision =
|
|
201
|
+
| {
|
|
202
|
+
kind: "replay";
|
|
203
|
+
result: unknown;
|
|
204
|
+
}
|
|
205
|
+
| {
|
|
206
|
+
kind: "execute";
|
|
207
|
+
seq: number;
|
|
208
|
+
}
|
|
209
|
+
| {
|
|
210
|
+
kind: "pause";
|
|
211
|
+
seq: number;
|
|
212
|
+
};
|
|
213
|
+
type BeginOptions = {
|
|
214
|
+
/** Terminal executions retained per runtime. Defaults to 50. */ maxExecutions?: number /** Connector names configured on the runtime starting this execution. */;
|
|
215
|
+
connectors?: string[];
|
|
216
|
+
};
|
|
217
|
+
/** Default number of terminal executions to retain per runtime. */
|
|
218
|
+
declare const DEFAULT_MAX_EXECUTIONS = 50;
|
|
219
|
+
/**
|
|
220
|
+
* Cap for any single serialized value stored in the durable log (args, a
|
|
221
|
+
* recorded result, the final result). SQLite rows in Durable Objects max out
|
|
222
|
+
* at ~2MB; 1MB leaves headroom for the rest of the row. Truncating stored
|
|
223
|
+
* values is never an option — replay would feed resumed code corrupted data —
|
|
224
|
+
* so a breach fails the run with a model-actionable error instead.
|
|
225
|
+
*/
|
|
226
|
+
declare const MAX_DURABLE_VALUE_BYTES = 1000000;
|
|
227
|
+
/** Default age after which a paused (awaiting-approval) run can be expired. */
|
|
228
|
+
declare const DEFAULT_PAUSED_TTL_MS: number;
|
|
229
|
+
declare class CodemodeRuntime extends DurableObject<unknown> {
|
|
230
|
+
#private;
|
|
231
|
+
constructor(ctx: DurableObjectState, env: unknown);
|
|
232
|
+
/**
|
|
233
|
+
* Begin a fresh execution. Returns the execution id. Prunes old terminal
|
|
234
|
+
* executions down to `maxExecutions` (newest kept). The connector names the
|
|
235
|
+
* runtime was created with are recorded on the execution so a later resume
|
|
236
|
+
* can verify they are still configured.
|
|
237
|
+
*/
|
|
238
|
+
begin(code: string, options?: BeginOptions): Promise<string>;
|
|
239
|
+
/**
|
|
240
|
+
* Resume an execution for a replay run. Only a `paused` run can be resumed:
|
|
241
|
+
* reviving a terminal run (completed/error/rejected/rolled_back) would
|
|
242
|
+
* re-offer a rejected action for approval or re-apply rolled-back side
|
|
243
|
+
* effects, and restarting an already-`running` run would race. Returns `null`
|
|
244
|
+
* (no state change) when the run is missing or not paused — the caller turns
|
|
245
|
+
* that into a clear error.
|
|
246
|
+
*/
|
|
247
|
+
resume(id: string): Promise<ExecutionState | null>;
|
|
248
|
+
/**
|
|
249
|
+
* Decide what to do with the next tool call or step. The host allocates and
|
|
250
|
+
* passes `seq` (not an in-memory cursor), so this is safe across hibernation
|
|
251
|
+
* and concurrent executions.
|
|
252
|
+
*
|
|
253
|
+
* If the execution is no longer "running" (already paused/terminal), every
|
|
254
|
+
* call gets a pause decision and nothing is recorded — so model code that
|
|
255
|
+
* swallows the pause sentinel can't drive further side effects.
|
|
256
|
+
*
|
|
257
|
+
* Replay: a log entry at `seq` must match connector + method + args
|
|
258
|
+
* (divergence is a hard error). Applied → replay its result (ephemeral
|
|
259
|
+
* entries re-execute instead). Pending → it was just approved, execute it.
|
|
260
|
+
* Executing (crashed mid-call) / reverted → treat as a fresh call.
|
|
261
|
+
*
|
|
262
|
+
* New call: step or read → execute (logged "executing" until recordResult).
|
|
263
|
+
* Approval-required → record pending, pause.
|
|
264
|
+
*/
|
|
265
|
+
decide(
|
|
266
|
+
executionId: string,
|
|
267
|
+
seq: number,
|
|
268
|
+
connector: string,
|
|
269
|
+
method: string,
|
|
270
|
+
args: unknown,
|
|
271
|
+
requiresApproval: boolean,
|
|
272
|
+
ephemeral?: boolean
|
|
273
|
+
): Promise<ToolDecision>;
|
|
274
|
+
/**
|
|
275
|
+
* Record the real result of an executed call or step. Ephemeral entries are
|
|
276
|
+
* marked applied but their result is never stored (they re-execute on
|
|
277
|
+
* replay).
|
|
278
|
+
*
|
|
279
|
+
* A non-ephemeral result that is too large / unserializable cannot live in
|
|
280
|
+
* the durable log (truncating it would corrupt replay), so the execution is
|
|
281
|
+
* marked failed with a model-actionable error — recorded on the execution
|
|
282
|
+
* rather than thrown, because a rejection across the facet RPC boundary
|
|
283
|
+
* would surface as an unhandled rejection on the host. The entry stays
|
|
284
|
+
* "executing" (its side effects already happened) and the run can make no
|
|
285
|
+
* further progress.
|
|
286
|
+
*/
|
|
287
|
+
recordResult(
|
|
288
|
+
executionId: string,
|
|
289
|
+
seq: number,
|
|
290
|
+
result: unknown
|
|
291
|
+
): Promise<void>;
|
|
292
|
+
/**
|
|
293
|
+
* Mark the run completed with a final result. Replay never needs the final
|
|
294
|
+
* result (a resume re-derives it), so an oversized/unserializable result is
|
|
295
|
+
* replaced with a placeholder note in the audit trail rather than failing a
|
|
296
|
+
* run that genuinely completed.
|
|
297
|
+
*/
|
|
298
|
+
complete(
|
|
299
|
+
executionId: string,
|
|
300
|
+
result: unknown,
|
|
301
|
+
logs?: string[]
|
|
302
|
+
): Promise<void>;
|
|
303
|
+
/** Mark the run errored. */
|
|
304
|
+
fail(executionId: string, error: string, logs?: string[]): Promise<void>;
|
|
305
|
+
/**
|
|
306
|
+
* List pending actions awaiting approval. With an `executionId`, scopes to
|
|
307
|
+
* that run; without one, aggregates across every **paused** run (newest
|
|
308
|
+
* first) — so an approval UI sees every awaiting-approval run, not just
|
|
309
|
+
* whichever happened to be started/resumed last. (Defaulting to a single
|
|
310
|
+
* "current" run would drop pending actions when multiple runs are in flight.)
|
|
311
|
+
*
|
|
312
|
+
* Only **paused** runs are considered: a non-paused run can retain a stale
|
|
313
|
+
* "pending" log entry (e.g. a resume diverged before reaching it, ending the
|
|
314
|
+
* run as `error` while the later entry stays `pending`). Such an entry isn't
|
|
315
|
+
* actionable — approving it is a no-op — so it must not clutter the queue.
|
|
316
|
+
*/
|
|
317
|
+
listPending(executionId?: string): Promise<PendingAction[]>;
|
|
318
|
+
/**
|
|
319
|
+
* Reject a pending action. Ends the execution with an error.
|
|
320
|
+
*
|
|
321
|
+
* Returns whether it actually terminated the run: `false` when the seq isn't
|
|
322
|
+
* pending (a stale/duplicate reject — e.g. the action was already handled by
|
|
323
|
+
* another tab), so the caller doesn't tear down a run that's still live.
|
|
324
|
+
*
|
|
325
|
+
* Rejection does NOT undo actions already applied earlier in the same run —
|
|
326
|
+
* call `rollback()` for that. See docs/codemode/approvals.md.
|
|
327
|
+
*/
|
|
328
|
+
reject(seq: number, executionId: string): Promise<boolean>;
|
|
329
|
+
/**
|
|
330
|
+
* Expire non-terminal runs whose last state change is older than
|
|
331
|
+
* `maxAgeMs`:
|
|
332
|
+
*
|
|
333
|
+
* - `paused` (awaiting approval) runs are marked **rejected**. A
|
|
334
|
+
* never-answered approval would otherwise live forever — paused runs are
|
|
335
|
+
* deliberately exempt from retention pruning.
|
|
336
|
+
* - `running` runs are marked **error**. A run only stays `running` with a
|
|
337
|
+
* stale `updated_at` when the host died mid-pass (every decide/record
|
|
338
|
+
* touches the row), and such a run can never be resumed — without expiry
|
|
339
|
+
* it would be unreclaimable and exempt from pruning forever.
|
|
340
|
+
*
|
|
341
|
+
* Returns the expired execution ids so the host can fire the connectors'
|
|
342
|
+
* `disposeExecution` for each. Each status flip is conditional on the
|
|
343
|
+
* status it observed, so an id is only returned (and disposed) when this
|
|
344
|
+
* call actually terminated the run.
|
|
345
|
+
*/
|
|
346
|
+
expirePaused(maxAgeMs?: number): Promise<string[]>;
|
|
347
|
+
/**
|
|
348
|
+
* Return applied connector actions (not steps) in reverse order. Reads are
|
|
349
|
+
* included but are harmless — the host's revertAction no-ops when a tool has
|
|
350
|
+
* no `revert`, and only entries that actually reverted are marked.
|
|
351
|
+
*/
|
|
352
|
+
actionsToRevert(executionId: string): Promise<ToolLogEntry[]>;
|
|
353
|
+
/** Mark an action reverted after the proxy tool has reverted it. */
|
|
354
|
+
markReverted(seq: number, executionId: string): Promise<void>;
|
|
355
|
+
/**
|
|
356
|
+
* Mark a run as rolled back once the proxy tool has finished reverting its
|
|
357
|
+
* actions, so the execution status reflects the rollback (rather than staying
|
|
358
|
+
* "completed"/"error" with some entries quietly flipped to "reverted").
|
|
359
|
+
*/
|
|
360
|
+
markRolledBack(executionId: string): Promise<void>;
|
|
361
|
+
getExecution(id: string): Promise<ExecutionState | null>;
|
|
362
|
+
/** List executions, newest first. Optionally cap the number returned. */
|
|
363
|
+
listExecutions(limit?: number): Promise<ExecutionState[]>;
|
|
364
|
+
/** Delete one execution. Returns whether it existed. */
|
|
365
|
+
deleteExecution(id: string): Promise<boolean>;
|
|
366
|
+
/**
|
|
367
|
+
* Delete terminal (completed/error) executions, keeping the newest `keep`.
|
|
368
|
+
* Running/paused executions are never deleted. Returns the count removed.
|
|
369
|
+
*/
|
|
370
|
+
pruneExecutions(keep?: number): Promise<number>;
|
|
371
|
+
/**
|
|
372
|
+
* Promote an execution's code to a saved, addressable snippet. This is the
|
|
373
|
+
* "save what ran" hook — the developer calls it (via runtime.saveSnippet)
|
|
374
|
+
* after a script proves useful, so the model can re-run it later with
|
|
375
|
+
* `codemode.run(name)`. The execution is named explicitly (its id comes from
|
|
376
|
+
* the tool's output or `listExecutions()`). The execution's recorded
|
|
377
|
+
* connector requirements carry over to the snippet so a later run can verify
|
|
378
|
+
* they are still configured.
|
|
379
|
+
*/
|
|
380
|
+
saveSnippet(name: string, options: SaveSnippetOptions): Promise<Snippet>;
|
|
381
|
+
getSnippet(name: string): Promise<Snippet | null>;
|
|
382
|
+
listSnippets(): Promise<Snippet[]>;
|
|
383
|
+
deleteSnippet(name: string): Promise<boolean>;
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region src/connectors/mcp.d.ts
|
|
387
|
+
interface McpConnectionLike {
|
|
388
|
+
name?: string;
|
|
389
|
+
client: Pick<Client, "callTool">;
|
|
390
|
+
instructions?: string;
|
|
391
|
+
tools?: Tool$1[];
|
|
392
|
+
fetchTools?: () => Promise<Tool$1[]>;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Connector backed by an MCP connection. Each MCP tool becomes one entry in
|
|
396
|
+
* the tools record, executing through `connection.client.callTool()`.
|
|
397
|
+
*
|
|
398
|
+
* Subclass and implement `createConnection()`. To mark a derived tool as
|
|
399
|
+
* requiring approval or to attach a revert, override the `tool(name, t)`
|
|
400
|
+
* decoration hook from the base class.
|
|
401
|
+
*/
|
|
402
|
+
declare abstract class McpConnector<
|
|
403
|
+
Env = unknown,
|
|
404
|
+
Props = unknown
|
|
405
|
+
> extends CodemodeConnector<Env, Props> {
|
|
406
|
+
#private;
|
|
407
|
+
protected abstract createConnection():
|
|
408
|
+
| Promise<McpConnectionLike>
|
|
409
|
+
| McpConnectionLike;
|
|
410
|
+
protected toolName(tool: Tool$1): string;
|
|
411
|
+
protected getConnection(): Promise<McpConnectionLike>;
|
|
412
|
+
protected fetchTools(): Promise<Tool$1[]>;
|
|
413
|
+
describe(): Promise<ConnectorDescription>;
|
|
414
|
+
protected tools(): Promise<ConnectorTools>;
|
|
415
|
+
}
|
|
416
|
+
//#endregion
|
|
417
|
+
//#region src/connectors/openapi.d.ts
|
|
418
|
+
type OpenApiRequestOptions = {
|
|
419
|
+
/** Path or URL to call, with path params already substituted. */ path: string;
|
|
420
|
+
method?: string /** Query parameters to append to the URL. */;
|
|
421
|
+
params?: Record<string, unknown>;
|
|
422
|
+
body?: unknown;
|
|
423
|
+
headers?: Record<string, string>;
|
|
424
|
+
};
|
|
425
|
+
/**
|
|
426
|
+
* Connector backed by an OpenAPI spec.
|
|
427
|
+
*
|
|
428
|
+
* The base reads the spec **once, host-side** and derives one typed tool per
|
|
429
|
+
* operation — so the model calls `api.get_repository({ owner, repo })` directly
|
|
430
|
+
* (discoverable via `codemode.search` / `codemode.describe`, with input types)
|
|
431
|
+
* instead of fetching the raw spec and hand-writing operation lookups on every
|
|
432
|
+
* run. Deriving on the host costs zero prompt tokens.
|
|
433
|
+
*
|
|
434
|
+
* Override two methods:
|
|
435
|
+
*
|
|
436
|
+
* - `spec()` returns the OpenAPI document (used to derive operations)
|
|
437
|
+
* - `request()` performs the authenticated HTTP call
|
|
438
|
+
*
|
|
439
|
+
* A low-level `request` tool is also exposed as an escape hatch for operations
|
|
440
|
+
* the model can't reach through a derived tool.
|
|
441
|
+
*
|
|
442
|
+
* The per-operation tool's input is a single object: top-level keys are the
|
|
443
|
+
* operation's path/query/header parameters, plus a `body` key when the
|
|
444
|
+
* operation has a JSON request body. The derived tool substitutes path params,
|
|
445
|
+
* then hands `request()` a clean `{ path, method, params, body, headers }`.
|
|
446
|
+
*
|
|
447
|
+
* (The sandbox-facing escape hatch is `request`, not `fetch`, because `fetch`
|
|
448
|
+
* is reserved by `WorkerEntrypoint`.)
|
|
449
|
+
*/
|
|
450
|
+
declare abstract class OpenApiConnector<
|
|
451
|
+
Env = unknown,
|
|
452
|
+
Props = unknown
|
|
453
|
+
> extends CodemodeConnector<Env, Props> {
|
|
454
|
+
protected abstract spec():
|
|
455
|
+
| Record<string, unknown>
|
|
456
|
+
| Promise<Record<string, unknown>>;
|
|
457
|
+
protected abstract request(options: OpenApiRequestOptions): Promise<unknown>;
|
|
458
|
+
/** Whether to also expose the raw `spec` document as a tool. Off by default. */
|
|
459
|
+
protected exposeSpec(): boolean;
|
|
460
|
+
protected tools(): Promise<ConnectorTools>;
|
|
461
|
+
}
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/proxy-tool.d.ts
|
|
464
|
+
type ProxyToolInput = {
|
|
465
|
+
code: string;
|
|
466
|
+
};
|
|
467
|
+
type ProxyToolOutput =
|
|
468
|
+
| {
|
|
469
|
+
status: "completed";
|
|
470
|
+
executionId: string;
|
|
471
|
+
result: unknown;
|
|
472
|
+
logs?: string[];
|
|
473
|
+
}
|
|
474
|
+
| {
|
|
475
|
+
status: "paused";
|
|
476
|
+
executionId: string;
|
|
477
|
+
pending: PendingAction[];
|
|
478
|
+
}
|
|
479
|
+
| {
|
|
480
|
+
status: "error";
|
|
481
|
+
executionId: string;
|
|
482
|
+
error: string;
|
|
483
|
+
logs?: string[];
|
|
484
|
+
};
|
|
485
|
+
/**
|
|
486
|
+
* Shape the final result before it is returned to the model. Runs on a
|
|
487
|
+
* completed run only (not on pause/error), after the raw result is recorded on
|
|
488
|
+
* the execution — so the audit trail keeps the full value while the model sees
|
|
489
|
+
* the transformed one. A common use is `truncateResult` to cap response size.
|
|
490
|
+
*/
|
|
491
|
+
type TransformResult = (result: unknown) => unknown | Promise<unknown>;
|
|
492
|
+
//#endregion
|
|
493
|
+
//#region src/runtime-handle.d.ts
|
|
494
|
+
type CreateCodemodeRuntimeOptions = {
|
|
495
|
+
ctx: DurableObjectState;
|
|
496
|
+
connectors: CodemodeConnector[];
|
|
497
|
+
executor: Executor;
|
|
498
|
+
/**
|
|
499
|
+
* Runtime name — the durable identity of this runtime's facet (its
|
|
500
|
+
* executions and snippets). Defaults to `"default"`. Use distinct names for
|
|
501
|
+
* runtimes that should keep separate histories. Adding or removing
|
|
502
|
+
* connectors does NOT change the identity: each execution/snippet records
|
|
503
|
+
* the connector names it needs, and resuming/re-running verifies they are
|
|
504
|
+
* still configured.
|
|
505
|
+
*/
|
|
506
|
+
name?: string;
|
|
507
|
+
/**
|
|
508
|
+
* How many terminal (completed/error) executions to retain per runtime.
|
|
509
|
+
* Older ones are pruned automatically when a new run begins. Running and
|
|
510
|
+
* paused executions are never pruned (see `expirePaused`). Defaults to 50.
|
|
511
|
+
*/
|
|
512
|
+
maxExecutions?: number;
|
|
513
|
+
/**
|
|
514
|
+
* Optionally reshape the model-facing result of a completed run before it is
|
|
515
|
+
* returned — e.g. `(r) => truncateResult(r)` to cap response size. The raw
|
|
516
|
+
* result is still recorded on the execution, so the audit trail is intact.
|
|
517
|
+
* Applies to both the initial run and a resume after approval.
|
|
518
|
+
*/
|
|
519
|
+
transformResult?: TransformResult;
|
|
520
|
+
};
|
|
521
|
+
type CodemodeRuntimeToolOptions = {
|
|
522
|
+
description?: string;
|
|
523
|
+
/**
|
|
524
|
+
* One-line hints rendered next to each connector in the default tool
|
|
525
|
+
* description (keyed by connector name). Ignored when a custom
|
|
526
|
+
* `description` is given.
|
|
527
|
+
*/
|
|
528
|
+
connectorHints?: Record<string, string>;
|
|
529
|
+
};
|
|
530
|
+
type CodemodeApproveOptions = {
|
|
531
|
+
/** Execution to approve and resume. Get it from `pending()` or the tool output. */ executionId: string;
|
|
532
|
+
};
|
|
533
|
+
type CodemodeRejectOptions = {
|
|
534
|
+
seq: number /** Execution to reject within. Get it from `pending()`. */;
|
|
535
|
+
executionId: string;
|
|
536
|
+
};
|
|
537
|
+
type CodemodeRollbackOptions = {
|
|
538
|
+
/** Execution to roll back. Get it from `executions()` or the tool output. */ executionId: string;
|
|
539
|
+
};
|
|
540
|
+
type CodemodeExpireOptions = {
|
|
541
|
+
/**
|
|
542
|
+
* Expire non-terminal runs whose last state change is older than this.
|
|
543
|
+
* Defaults to 24 hours.
|
|
544
|
+
*/
|
|
545
|
+
maxAgeMs?: number;
|
|
546
|
+
};
|
|
547
|
+
interface CodemodeRuntimeHandle {
|
|
548
|
+
tool(
|
|
549
|
+
options?: CodemodeRuntimeToolOptions
|
|
550
|
+
): Tool<ProxyToolInput, ProxyToolOutput>;
|
|
551
|
+
approve(options: CodemodeApproveOptions): Promise<ProxyToolOutput>;
|
|
552
|
+
/**
|
|
553
|
+
* Reject a pending action, ending the run. Returns whether the reject
|
|
554
|
+
* actually terminated it — `false` when the action was no longer pending
|
|
555
|
+
* (approved or rejected from elsewhere, or expired), in which case the run
|
|
556
|
+
* was NOT rejected and the action may have executed.
|
|
557
|
+
*/
|
|
558
|
+
reject(options: CodemodeRejectOptions): Promise<boolean>;
|
|
559
|
+
rollback(options: CodemodeRollbackOptions): Promise<void>;
|
|
560
|
+
pending(executionId?: string): Promise<PendingAction[]>;
|
|
561
|
+
/**
|
|
562
|
+
* Expire stale non-terminal runs (neither is ever auto-pruned), disposing
|
|
563
|
+
* per-execution connector resources: paused runs nobody approved are marked
|
|
564
|
+
* rejected; running runs whose host died mid-pass are marked error. Call
|
|
565
|
+
* from a recurring alarm/scheduled task. Returns the expired ids.
|
|
566
|
+
*/
|
|
567
|
+
expirePaused(options?: CodemodeExpireOptions): Promise<string[]>;
|
|
568
|
+
/** All executions, newest first — the audit trail. Optionally capped. */
|
|
569
|
+
executions(limit?: number): Promise<ExecutionState[]>;
|
|
570
|
+
/**
|
|
571
|
+
* Delete a single execution from the audit trail. Deleting a non-terminal
|
|
572
|
+
* execution also disposes its per-execution connector resources.
|
|
573
|
+
*/
|
|
574
|
+
deleteExecution(id: string): Promise<boolean>;
|
|
575
|
+
/** Prune terminal executions, keeping the newest `keep`. */
|
|
576
|
+
pruneExecutions(keep?: number): Promise<number>;
|
|
577
|
+
/** Promote an execution's script to a named, reusable snippet. */
|
|
578
|
+
saveSnippet(name: string, options: SaveSnippetOptions): Promise<Snippet>;
|
|
579
|
+
snippets(): Promise<Snippet[]>;
|
|
580
|
+
deleteSnippet(name: string): Promise<boolean>;
|
|
581
|
+
}
|
|
582
|
+
declare function createCodemodeRuntime(
|
|
583
|
+
options: CreateCodemodeRuntimeOptions
|
|
584
|
+
): CodemodeRuntimeHandle;
|
|
585
|
+
//#endregion
|
|
29
586
|
export {
|
|
587
|
+
type CodemodeApproveOptions,
|
|
588
|
+
CodemodeConnector,
|
|
589
|
+
type CodemodeExpireOptions,
|
|
590
|
+
type CodemodeRejectOptions,
|
|
591
|
+
type CodemodeRollbackOptions,
|
|
592
|
+
CodemodeRuntime,
|
|
593
|
+
type CodemodeRuntimeHandle,
|
|
594
|
+
type CodemodeRuntimeToolOptions,
|
|
595
|
+
type ConnectorBinding,
|
|
596
|
+
type ConnectorDescription,
|
|
597
|
+
type ConnectorTool,
|
|
598
|
+
type ConnectorTools,
|
|
599
|
+
type CreateCodemodeRuntimeOptions,
|
|
600
|
+
DEFAULT_MAX_EXECUTIONS,
|
|
601
|
+
DEFAULT_PAUSED_TTL_MS,
|
|
602
|
+
type DescribeOutput,
|
|
30
603
|
DynamicWorkerExecutor,
|
|
31
604
|
type DynamicWorkerExecutorOptions,
|
|
605
|
+
type ExecuteOptions,
|
|
32
606
|
type ExecuteResult,
|
|
607
|
+
type ExecutionEndStatus,
|
|
608
|
+
type ExecutionState,
|
|
609
|
+
type ExecutionStatus,
|
|
33
610
|
type Executor,
|
|
34
611
|
type JsonSchemaToolDescriptor,
|
|
35
612
|
type JsonSchemaToolDescriptors,
|
|
613
|
+
MAX_DURABLE_VALUE_BYTES,
|
|
614
|
+
type McpConnectionLike,
|
|
615
|
+
McpConnector,
|
|
616
|
+
OpenApiConnector,
|
|
617
|
+
type OpenApiRequestOptions,
|
|
618
|
+
type PassEndStatus,
|
|
619
|
+
type PendingAction,
|
|
620
|
+
type ProxyToolInput,
|
|
621
|
+
type ProxyToolOutput,
|
|
36
622
|
type ResolvedProvider,
|
|
623
|
+
type SaveSnippetOptions,
|
|
624
|
+
type SearchOutput,
|
|
625
|
+
type SearchResult,
|
|
626
|
+
type Snippet,
|
|
627
|
+
type ToolAnnotations,
|
|
628
|
+
type ToolDecision,
|
|
37
629
|
ToolDispatcher,
|
|
630
|
+
type ToolExecuteContext,
|
|
631
|
+
type ToolLogEntry,
|
|
38
632
|
type ToolProvider,
|
|
633
|
+
type TransformResult,
|
|
634
|
+
type TruncateOptions,
|
|
635
|
+
createCodemodeRuntime,
|
|
39
636
|
generateTypesFromJsonSchema,
|
|
40
637
|
jsonSchemaToType,
|
|
41
638
|
normalizeCode,
|
|
42
639
|
resolveProvider,
|
|
43
|
-
|
|
640
|
+
runCode,
|
|
641
|
+
sanitizeToolName,
|
|
642
|
+
truncateResponse,
|
|
643
|
+
truncateResult
|
|
44
644
|
};
|
|
45
645
|
//# sourceMappingURL=index.d.ts.map
|