@cloudflare/codemode 0.4.1 → 0.4.3
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/README.md +13 -11
- package/dist/ai.d.ts +2 -2
- package/dist/ai.js +3 -3
- package/dist/{base-CHRR_a5j.js → base-BqhlNCSH.js} +2 -2
- package/dist/{base-CHRR_a5j.js.map → base-BqhlNCSH.js.map} +1 -1
- package/dist/browser.d.ts +2 -2
- package/dist/browser.js +4 -4
- package/dist/browser.js.map +1 -1
- package/dist/{executor-BIs2dr7X.d.ts → executor-DkMGH_7-.d.ts} +2 -2
- package/dist/index.d.ts +54 -12
- package/dist/index.js +79 -27
- package/dist/index.js.map +1 -1
- package/dist/{json-schema-types-BGjW4KPy.js → json-schema-types-D_m9tVnI.js} +2 -2
- package/dist/{json-schema-types-BGjW4KPy.js.map → json-schema-types-D_m9tVnI.js.map} +1 -1
- package/dist/mcp.d.ts +35 -2
- package/dist/mcp.js +3 -3
- package/dist/mcp.js.map +1 -1
- package/dist/{resolve-CBr_VoPl.d.ts → resolve-tqDt6Djo.d.ts} +2 -2
- package/dist/{run-code-aKHcTgf9.js → run-code-Q3Jh6Dcu.js} +2 -2
- package/dist/{run-code-aKHcTgf9.js.map → run-code-Q3Jh6Dcu.js.map} +1 -1
- package/dist/{shared-Ce-zCEBP.d.ts → shared-BOF2IKt-.d.ts} +2 -2
- package/dist/tanstack-ai.d.ts +3 -3
- package/dist/tanstack-ai.js +2 -2
- package/dist/vite.js +19 -91
- package/dist/vite.js.map +1 -1
- package/docs/approvals.md +169 -0
- package/docs/connectors.md +343 -0
- package/docs/index.md +192 -0
- package/docs/runtime.md +263 -0
- package/docs/snippets.md +96 -0
- package/docs/vite-plugin.md +60 -0
- package/package.json +22 -4
package/dist/index.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { a as sanitizeToolName, n as jsonSchemaToType, s as normalizeCode, t as generateTypesFromJsonSchema } from "./json-schema-types-
|
|
2
|
-
import { t as runCode } from "./run-code-
|
|
1
|
+
import { a as sanitizeToolName, n as jsonSchemaToType, s as normalizeCode, t as generateTypesFromJsonSchema } from "./json-schema-types-D_m9tVnI.js";
|
|
2
|
+
import { t as runCode } from "./run-code-Q3Jh6Dcu.js";
|
|
3
3
|
import { r as resolveProvider } from "./resolve-34dC47Il.js";
|
|
4
|
-
import { t as CodemodeConnector } from "./base-
|
|
4
|
+
import { t as CodemodeConnector } from "./base-BqhlNCSH.js";
|
|
5
5
|
import { DurableObject, RpcTarget } from "cloudflare:workers";
|
|
6
|
-
import { tool } from "ai";
|
|
7
|
-
import { z } from "zod";
|
|
8
6
|
//#region src/codec.ts
|
|
9
7
|
/**
|
|
10
8
|
* Host-side value codec.
|
|
@@ -99,6 +97,26 @@ function parseForStorage(json) {
|
|
|
99
97
|
*/
|
|
100
98
|
const CONNECTOR_CONTROL_KEY = "__codemode_control__";
|
|
101
99
|
const PAUSE_SENTINEL_LITERAL = "__CODEMODE_PAUSE__";
|
|
100
|
+
/**
|
|
101
|
+
* Best-effort synchronous disposal of a disposable resource.
|
|
102
|
+
*
|
|
103
|
+
* Dynamically-loaded Workers and the RPC `Fetcher` stubs returned by
|
|
104
|
+
* `getEntrypoint()` own native handles. Left to GC, they can be finalized
|
|
105
|
+
* after the isolate's destruction queue has closed — under the
|
|
106
|
+
* `@cloudflare/vitest-pool-workers` teardown matrix this trips a fatal
|
|
107
|
+
* workerd assertion ("tried to defer destruction during isolate shutdown")
|
|
108
|
+
* that kills the worker. Disposing them eagerly releases the handle while the
|
|
109
|
+
* isolate is still alive. Disposal must never mask the execution result, so
|
|
110
|
+
* failures are swallowed.
|
|
111
|
+
*/
|
|
112
|
+
function disposeQuietly(resource) {
|
|
113
|
+
if (typeof resource !== "object" || resource === null || !(Symbol.dispose in resource)) return;
|
|
114
|
+
const dispose = resource[Symbol.dispose];
|
|
115
|
+
if (typeof dispose !== "function") return;
|
|
116
|
+
try {
|
|
117
|
+
dispose.call(resource);
|
|
118
|
+
} catch {}
|
|
119
|
+
}
|
|
102
120
|
const SANDBOX_CODEC = String.raw`
|
|
103
121
|
const __CODEMODE_BINARY_TAG = "__codemode_binary_v1__";
|
|
104
122
|
function __bytesToBase64(bytes) {
|
|
@@ -164,6 +182,7 @@ var ToolDispatcher = class extends RpcTarget {
|
|
|
164
182
|
}
|
|
165
183
|
}
|
|
166
184
|
};
|
|
185
|
+
const DEFAULT_DYNAMIC_WORKER_EXECUTION_TIMEOUT_MS = 6e4;
|
|
167
186
|
/**
|
|
168
187
|
* Executes code in an isolated Cloudflare Worker via WorkerLoader.
|
|
169
188
|
* Tool calls are dispatched via Workers RPC — the host passes
|
|
@@ -190,7 +209,7 @@ var DynamicWorkerExecutor = class {
|
|
|
190
209
|
#bindings;
|
|
191
210
|
constructor(options) {
|
|
192
211
|
this.#loader = options.loader;
|
|
193
|
-
this.#timeout = options.timeout ??
|
|
212
|
+
this.#timeout = options.timeout ?? DEFAULT_DYNAMIC_WORKER_EXECUTION_TIMEOUT_MS;
|
|
194
213
|
this.#globalOutbound = options.globalOutbound ?? null;
|
|
195
214
|
const { "executor.js": _, ...safeModules } = options.modules ?? {};
|
|
196
215
|
this.#modules = safeModules;
|
|
@@ -207,7 +226,7 @@ var DynamicWorkerExecutor = class {
|
|
|
207
226
|
} else providers = providersOrFns;
|
|
208
227
|
const normalized = normalizeCode(code);
|
|
209
228
|
const timeoutMs = this.#timeout;
|
|
210
|
-
const RESERVED_NAMES = new Set([
|
|
229
|
+
const RESERVED_NAMES = /* @__PURE__ */ new Set([
|
|
211
230
|
"__dispatchers",
|
|
212
231
|
"__connectors",
|
|
213
232
|
"__logs",
|
|
@@ -310,7 +329,7 @@ var DynamicWorkerExecutor = class {
|
|
|
310
329
|
for (const connector of connectors) connectorBindings[connector.name] = connector.binding;
|
|
311
330
|
const env = { ...this.#bindings };
|
|
312
331
|
const hasEnv = Object.keys(env).length > 0;
|
|
313
|
-
const
|
|
332
|
+
const worker = this.#loader.load({
|
|
314
333
|
compatibilityDate: "2025-06-01",
|
|
315
334
|
compatibilityFlags: ["nodejs_compat"],
|
|
316
335
|
mainModule: "executor.js",
|
|
@@ -320,16 +339,26 @@ var DynamicWorkerExecutor = class {
|
|
|
320
339
|
},
|
|
321
340
|
globalOutbound: this.#globalOutbound,
|
|
322
341
|
env: hasEnv ? env : void 0
|
|
323
|
-
})
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
342
|
+
});
|
|
343
|
+
try {
|
|
344
|
+
const entrypoint = worker.getEntrypoint();
|
|
345
|
+
try {
|
|
346
|
+
const response = await entrypoint.evaluate(dispatchers, connectorBindings);
|
|
347
|
+
if (response.error) return {
|
|
348
|
+
result: void 0,
|
|
349
|
+
error: response.error,
|
|
350
|
+
logs: response.logs
|
|
351
|
+
};
|
|
352
|
+
return {
|
|
353
|
+
result: response.result,
|
|
354
|
+
logs: response.logs
|
|
355
|
+
};
|
|
356
|
+
} finally {
|
|
357
|
+
disposeQuietly(entrypoint);
|
|
358
|
+
}
|
|
359
|
+
} finally {
|
|
360
|
+
disposeQuietly(worker);
|
|
361
|
+
}
|
|
333
362
|
}
|
|
334
363
|
};
|
|
335
364
|
//#endregion
|
|
@@ -1117,7 +1146,7 @@ var OpenApiConnector = class extends CodemodeConnector {
|
|
|
1117
1146
|
return tools;
|
|
1118
1147
|
}
|
|
1119
1148
|
};
|
|
1120
|
-
const RESERVED_TOOL_NAMES = new Set(["request", "spec"]);
|
|
1149
|
+
const RESERVED_TOOL_NAMES = /* @__PURE__ */ new Set(["request", "spec"]);
|
|
1121
1150
|
const operationCache = /* @__PURE__ */ new WeakMap();
|
|
1122
1151
|
function deriveOperations(spec) {
|
|
1123
1152
|
if (!spec || typeof spec !== "object") return [];
|
|
@@ -1433,7 +1462,24 @@ function describeTarget(target, descriptions, snippets) {
|
|
|
1433
1462
|
* in-memory cursor, so runs are safe across hibernation and can run
|
|
1434
1463
|
* concurrently without clobbering one another.
|
|
1435
1464
|
*/
|
|
1436
|
-
const
|
|
1465
|
+
const proxyJsonSchema = {
|
|
1466
|
+
type: "object",
|
|
1467
|
+
properties: { code: { type: "string" } },
|
|
1468
|
+
required: ["code"],
|
|
1469
|
+
additionalProperties: false
|
|
1470
|
+
};
|
|
1471
|
+
const proxySchema = { "~standard": {
|
|
1472
|
+
version: 1,
|
|
1473
|
+
vendor: "@cloudflare/codemode",
|
|
1474
|
+
validate: (value) => {
|
|
1475
|
+
if (typeof value === "object" && value !== null && "code" in value && typeof value.code === "string") return { value: { code: value.code } };
|
|
1476
|
+
return { issues: [{ message: "Expected an object with a string code property" }] };
|
|
1477
|
+
},
|
|
1478
|
+
jsonSchema: {
|
|
1479
|
+
input: (_options) => proxyJsonSchema,
|
|
1480
|
+
output: (_options) => proxyJsonSchema
|
|
1481
|
+
}
|
|
1482
|
+
} };
|
|
1437
1483
|
const STEP_PRELUDE = String.raw`
|
|
1438
1484
|
codemode.step = async (name, fn) => {
|
|
1439
1485
|
const decision = await codemode.__stepDecide(name);
|
|
@@ -1638,12 +1684,14 @@ async function runPass(executionId, code, setup, runtime, executor, transformRes
|
|
|
1638
1684
|
threw = err;
|
|
1639
1685
|
}
|
|
1640
1686
|
const execution = await runtime.getExecution(executionId);
|
|
1687
|
+
const calls = execution?.log;
|
|
1641
1688
|
if (execution?.status === "paused") {
|
|
1642
1689
|
await endPass("paused");
|
|
1643
1690
|
return {
|
|
1644
1691
|
status: "paused",
|
|
1645
1692
|
executionId,
|
|
1646
|
-
pending: await runtime.listPending(executionId)
|
|
1693
|
+
pending: await runtime.listPending(executionId),
|
|
1694
|
+
calls
|
|
1647
1695
|
};
|
|
1648
1696
|
}
|
|
1649
1697
|
if (execution?.status === "error") {
|
|
@@ -1651,7 +1699,8 @@ async function runPass(executionId, code, setup, runtime, executor, transformRes
|
|
|
1651
1699
|
return {
|
|
1652
1700
|
status: "error",
|
|
1653
1701
|
executionId,
|
|
1654
|
-
error: execution.error ?? "Codemode execution failed"
|
|
1702
|
+
error: execution.error ?? "Codemode execution failed",
|
|
1703
|
+
calls
|
|
1655
1704
|
};
|
|
1656
1705
|
}
|
|
1657
1706
|
if (threw) {
|
|
@@ -1661,7 +1710,8 @@ async function runPass(executionId, code, setup, runtime, executor, transformRes
|
|
|
1661
1710
|
return {
|
|
1662
1711
|
status: "error",
|
|
1663
1712
|
executionId,
|
|
1664
|
-
error: message
|
|
1713
|
+
error: message,
|
|
1714
|
+
calls
|
|
1665
1715
|
};
|
|
1666
1716
|
}
|
|
1667
1717
|
const result = output?.result;
|
|
@@ -1671,7 +1721,8 @@ async function runPass(executionId, code, setup, runtime, executor, transformRes
|
|
|
1671
1721
|
status: "completed",
|
|
1672
1722
|
executionId,
|
|
1673
1723
|
result: await applyTransform(transformResult, result),
|
|
1674
|
-
logs: output?.logs
|
|
1724
|
+
logs: output?.logs,
|
|
1725
|
+
calls
|
|
1675
1726
|
};
|
|
1676
1727
|
} finally {
|
|
1677
1728
|
if (!ended) await endPass("error");
|
|
@@ -1709,7 +1760,7 @@ function createProxyTool(options) {
|
|
|
1709
1760
|
function getSetup() {
|
|
1710
1761
|
return setupPromise ??= loadSetup(connectors);
|
|
1711
1762
|
}
|
|
1712
|
-
return
|
|
1763
|
+
return {
|
|
1713
1764
|
description: buildDescription(connectors, options.description, options.connectorHints),
|
|
1714
1765
|
inputSchema: proxySchema,
|
|
1715
1766
|
execute: async ({ code }) => {
|
|
@@ -1724,7 +1775,7 @@ function createProxyTool(options) {
|
|
|
1724
1775
|
connectors: connectors.map((c) => c.name())
|
|
1725
1776
|
}), code, setup, runtime, options.executor, options.transformResult);
|
|
1726
1777
|
}
|
|
1727
|
-
}
|
|
1778
|
+
};
|
|
1728
1779
|
}
|
|
1729
1780
|
/** Default runtime name when none is given. */
|
|
1730
1781
|
const DEFAULT_RUNTIME_NAME = "default";
|
|
@@ -1742,7 +1793,8 @@ function runtimeFacetName(name = DEFAULT_RUNTIME_NAME) {
|
|
|
1742
1793
|
}
|
|
1743
1794
|
function getRuntime(ctx, name) {
|
|
1744
1795
|
const facetCtx = ctx;
|
|
1745
|
-
const runtimeClass = facetCtx.exports?.CodemodeRuntime
|
|
1796
|
+
const runtimeClass = facetCtx.exports?.CodemodeRuntime;
|
|
1797
|
+
if (!runtimeClass) throw new Error("CodemodeRuntime is not exported from this Worker entry. Add the @cloudflare/codemode/vite plugin to your Vite config, or manually export { CodemodeRuntime } from \"@cloudflare/codemode\".");
|
|
1746
1798
|
return facetCtx.facets.get(runtimeFacetName(name), () => ({ class: runtimeClass }));
|
|
1747
1799
|
}
|
|
1748
1800
|
/** Internal: the runtime handle uses this to reach the facet. Not public API. */
|