@cloudflare/codemode 0.5.0 → 0.5.2
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/dist/index.d.ts +67 -10
- package/dist/index.js +33 -59
- package/dist/index.js.map +1 -1
- package/dist/mcp.d.ts +1 -1
- package/dist/mcp.js +18 -25
- package/dist/mcp.js.map +1 -1
- package/dist/truncate-Dd-VZZYQ.js +191 -0
- package/dist/truncate-Dd-VZZYQ.js.map +1 -0
- package/docs/runtime.md +3 -1
- package/package.json +3 -4
package/dist/index.d.ts
CHANGED
|
@@ -31,8 +31,6 @@ import {
|
|
|
31
31
|
} from "./base-DiFD41JY.js";
|
|
32
32
|
import { t as resolveProvider } from "./resolve-tqDt6Djo.js";
|
|
33
33
|
import { DurableObject } from "cloudflare:workers";
|
|
34
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
35
|
-
import { Tool } from "@modelcontextprotocol/sdk/types.js";
|
|
36
34
|
|
|
37
35
|
//#region src/run-code.d.ts
|
|
38
36
|
declare function runCode({
|
|
@@ -67,6 +65,13 @@ declare function normalizeCode(code: string): string;
|
|
|
67
65
|
* structured results intact so the model can still reason over them. They are
|
|
68
66
|
* the default building blocks for a `transformResult` hook (see
|
|
69
67
|
* `createCodemodeRuntime`).
|
|
68
|
+
*
|
|
69
|
+
* Structured values are truncated structurally: the output is always valid
|
|
70
|
+
* JSON of the same shape, with the largest values cut first. A truncated
|
|
71
|
+
* string ends with a marker, a truncated array ends with a marker element
|
|
72
|
+
* counting the dropped items, and an object that had to lose entries carries a
|
|
73
|
+
* marker entry naming the omitted keys. Every marker contains
|
|
74
|
+
* `--- TRUNCATED ---` so callers and models can find them.
|
|
70
75
|
*/
|
|
71
76
|
type TruncateOptions = {
|
|
72
77
|
/**
|
|
@@ -87,9 +92,10 @@ declare function truncateResponse(
|
|
|
87
92
|
): string;
|
|
88
93
|
/**
|
|
89
94
|
* Truncate a structured result. Strings are truncated directly. Other values
|
|
90
|
-
* pass through unchanged when their JSON serialization is
|
|
91
|
-
* oversized, the
|
|
92
|
-
*
|
|
95
|
+
* pass through unchanged (same reference) when their JSON serialization is
|
|
96
|
+
* within budget. When oversized, the value is shrunk structurally — largest
|
|
97
|
+
* values first, deepest last — so the model gets a bounded value that is still
|
|
98
|
+
* valid JSON of the original shape, with markers where content was cut.
|
|
93
99
|
*
|
|
94
100
|
* Values that can't be serialized (cycles, bigint, `undefined`) are returned
|
|
95
101
|
* unchanged.
|
|
@@ -383,12 +389,42 @@ declare class CodemodeRuntime extends DurableObject<unknown> {
|
|
|
383
389
|
}
|
|
384
390
|
//#endregion
|
|
385
391
|
//#region src/connectors/mcp.d.ts
|
|
392
|
+
type CallToolResult = {
|
|
393
|
+
toolResult?: unknown;
|
|
394
|
+
isError?: boolean;
|
|
395
|
+
structuredContent?: unknown;
|
|
396
|
+
content?: Array<{
|
|
397
|
+
type: string;
|
|
398
|
+
text?: string;
|
|
399
|
+
[key: string]: unknown;
|
|
400
|
+
}>;
|
|
401
|
+
};
|
|
402
|
+
type McpJsonSchema = {
|
|
403
|
+
type?: string;
|
|
404
|
+
properties?: Record<string, unknown>;
|
|
405
|
+
required?: string[];
|
|
406
|
+
[key: string]: unknown;
|
|
407
|
+
};
|
|
408
|
+
/** Structural tool shape shared by MCP SDK v1 and v2. */
|
|
409
|
+
type McpTool = {
|
|
410
|
+
name: string;
|
|
411
|
+
description?: string;
|
|
412
|
+
inputSchema: McpJsonSchema;
|
|
413
|
+
outputSchema?: McpJsonSchema;
|
|
414
|
+
[key: string]: unknown;
|
|
415
|
+
};
|
|
416
|
+
/** Structural boundary compatible with both the legacy and v2 MCP clients. */
|
|
386
417
|
interface McpConnectionLike {
|
|
387
418
|
name?: string;
|
|
388
|
-
client:
|
|
419
|
+
client: {
|
|
420
|
+
callTool(params: {
|
|
421
|
+
name: string;
|
|
422
|
+
arguments?: Record<string, unknown>;
|
|
423
|
+
}): Promise<CallToolResult>;
|
|
424
|
+
};
|
|
389
425
|
instructions?: string;
|
|
390
|
-
tools?:
|
|
391
|
-
fetchTools?: () => Promise<
|
|
426
|
+
tools?: McpTool[];
|
|
427
|
+
fetchTools?: () => Promise<McpTool[]>;
|
|
392
428
|
}
|
|
393
429
|
/**
|
|
394
430
|
* Connector backed by an MCP connection. Each MCP tool becomes one entry in
|
|
@@ -406,9 +442,9 @@ declare abstract class McpConnector<
|
|
|
406
442
|
protected abstract createConnection():
|
|
407
443
|
| Promise<McpConnectionLike>
|
|
408
444
|
| McpConnectionLike;
|
|
409
|
-
protected toolName(tool:
|
|
445
|
+
protected toolName(tool: McpTool): string;
|
|
410
446
|
protected getConnection(): Promise<McpConnectionLike>;
|
|
411
|
-
protected fetchTools(): Promise<
|
|
447
|
+
protected fetchTools(): Promise<McpTool[]>;
|
|
412
448
|
describe(): Promise<ConnectorDescription>;
|
|
413
449
|
protected tools(): Promise<ConnectorTools>;
|
|
414
450
|
}
|
|
@@ -532,7 +568,28 @@ type CodemodeTool = {
|
|
|
532
568
|
input: ProxyToolInput,
|
|
533
569
|
options: unknown
|
|
534
570
|
) => Promise<ProxyToolOutput>;
|
|
571
|
+
/**
|
|
572
|
+
* The AI SDK model-facing projection of the output: everything but `calls`.
|
|
573
|
+
* The full output (with the durable call log) stays on the persisted tool
|
|
574
|
+
* part for UIs; the model sees the transformed `result`, `logs`, `pending`
|
|
575
|
+
* or `error`. Without this the raw call log — every connector call's args
|
|
576
|
+
* and result — would ride along uncapped and defeat `transformResult`.
|
|
577
|
+
*/
|
|
578
|
+
toModelOutput: (options: { output: unknown }) => {
|
|
579
|
+
type: "json";
|
|
580
|
+
value: JSONValue;
|
|
581
|
+
};
|
|
535
582
|
};
|
|
583
|
+
/** Structural twin of the AI SDK's `JSONValue`; the root entry must not import `ai`. */
|
|
584
|
+
type JSONValue =
|
|
585
|
+
| string
|
|
586
|
+
| number
|
|
587
|
+
| boolean
|
|
588
|
+
| null
|
|
589
|
+
| JSONValue[]
|
|
590
|
+
| {
|
|
591
|
+
[key: string]: JSONValue;
|
|
592
|
+
};
|
|
536
593
|
//#endregion
|
|
537
594
|
//#region src/runtime-handle.d.ts
|
|
538
595
|
type CreateCodemodeRuntimeOptions = {
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { a as sanitizeToolName, n as jsonSchemaToType, s as normalizeCode, t as generateTypesFromJsonSchema } from "./json-schema-types-D_m9tVnI.js";
|
|
2
2
|
import { t as runCode } from "./run-code-Q3Jh6Dcu.js";
|
|
3
3
|
import { r as resolveProvider } from "./resolve-34dC47Il.js";
|
|
4
|
+
import { n as truncateResult, t as truncateResponse } from "./truncate-Dd-VZZYQ.js";
|
|
4
5
|
import { t as CodemodeConnector } from "./base-BqhlNCSH.js";
|
|
5
6
|
import { DurableObject, RpcTarget } from "cloudflare:workers";
|
|
6
7
|
//#region src/codec.ts
|
|
@@ -362,61 +363,6 @@ var DynamicWorkerExecutor = class {
|
|
|
362
363
|
}
|
|
363
364
|
};
|
|
364
365
|
//#endregion
|
|
365
|
-
//#region src/truncate.ts
|
|
366
|
-
/**
|
|
367
|
-
* Result truncation utilities.
|
|
368
|
-
*
|
|
369
|
-
* Tool and sandbox results can be large enough to blow a model's context
|
|
370
|
-
* window. These cap the serialized size of a value while leaving small,
|
|
371
|
-
* structured results intact so the model can still reason over them. They are
|
|
372
|
-
* the default building blocks for a `transformResult` hook (see
|
|
373
|
-
* `createCodemodeRuntime`).
|
|
374
|
-
*/
|
|
375
|
-
/** ~4 characters per token is a reasonable cross-model estimate. */
|
|
376
|
-
const CHARS_PER_TOKEN = 4;
|
|
377
|
-
const DEFAULT_MAX_TOKENS = 6e3;
|
|
378
|
-
const TRUNCATION_MARKER = "--- TRUNCATED ---";
|
|
379
|
-
function budget(options) {
|
|
380
|
-
const maxTokens = options?.maxTokens ?? DEFAULT_MAX_TOKENS;
|
|
381
|
-
return {
|
|
382
|
-
maxChars: options?.maxChars ?? maxTokens * CHARS_PER_TOKEN,
|
|
383
|
-
maxTokens
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Truncate a text response to a character budget, appending a marker that notes
|
|
388
|
-
* the original size so the model knows the output was clipped. Returns the
|
|
389
|
-
* input unchanged when it is within budget.
|
|
390
|
-
*/
|
|
391
|
-
function truncateResponse(text, options) {
|
|
392
|
-
const { maxChars, maxTokens } = budget(options);
|
|
393
|
-
if (text.length <= maxChars) return text;
|
|
394
|
-
const estimatedTokens = Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
395
|
-
return text.slice(0, maxChars) + `\n\n${TRUNCATION_MARKER}\nResponse was ~${estimatedTokens.toLocaleString()} tokens (limit: ${maxTokens.toLocaleString()}). Narrow the request to reduce response size.`;
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* Truncate a structured result. Strings are truncated directly. Other values
|
|
399
|
-
* pass through unchanged when their JSON serialization is within budget; when
|
|
400
|
-
* oversized, the serialized form is truncated and returned as a string, so the
|
|
401
|
-
* model still gets a usable, bounded preview rather than a dropped result.
|
|
402
|
-
*
|
|
403
|
-
* Values that can't be serialized (cycles, bigint, `undefined`) are returned
|
|
404
|
-
* unchanged.
|
|
405
|
-
*/
|
|
406
|
-
function truncateResult(value, options) {
|
|
407
|
-
if (typeof value === "string") return truncateResponse(value, options);
|
|
408
|
-
let serialized;
|
|
409
|
-
try {
|
|
410
|
-
serialized = JSON.stringify(value, null, 2);
|
|
411
|
-
} catch {
|
|
412
|
-
return value;
|
|
413
|
-
}
|
|
414
|
-
if (serialized === void 0) return value;
|
|
415
|
-
const { maxChars } = budget(options);
|
|
416
|
-
if (serialized.length <= maxChars) return value;
|
|
417
|
-
return truncateResponse(serialized, options);
|
|
418
|
-
}
|
|
419
|
-
//#endregion
|
|
420
366
|
//#region src/runtime.ts
|
|
421
367
|
/**
|
|
422
368
|
* CodemodeRuntime — durable execution engine, implemented as a DurableObject
|
|
@@ -999,14 +945,15 @@ var CodemodeRuntime = class extends DurableObject {
|
|
|
999
945
|
//#endregion
|
|
1000
946
|
//#region src/connectors/mcp.ts
|
|
1001
947
|
function unwrapMcpResult(result) {
|
|
1002
|
-
if (
|
|
948
|
+
if (result.toolResult !== void 0) return result.toolResult;
|
|
1003
949
|
if (result.isError) {
|
|
1004
950
|
const msg = result.content?.filter((c) => c.type === "text").map((c) => "text" in c ? c.text : "").join("\n") || "Tool call failed";
|
|
1005
951
|
throw new Error(msg);
|
|
1006
952
|
}
|
|
1007
953
|
if (result.structuredContent != null) return result.structuredContent;
|
|
1008
|
-
|
|
1009
|
-
|
|
954
|
+
const content = result.content;
|
|
955
|
+
if (!(content !== void 0 && content.length > 0 && content.every((c) => c.type === "text"))) return result;
|
|
956
|
+
const text = content.map((c) => c.text ?? "").join("\n");
|
|
1010
957
|
try {
|
|
1011
958
|
return JSON.parse(text);
|
|
1012
959
|
} catch {
|
|
@@ -1755,6 +1702,32 @@ async function applyTransform(transformResult, result) {
|
|
|
1755
1702
|
return result;
|
|
1756
1703
|
}
|
|
1757
1704
|
}
|
|
1705
|
+
/**
|
|
1706
|
+
* Project a tool output for the model: drop the audit-only `calls` log and
|
|
1707
|
+
* bound the sandbox `logs` the same way `truncateResult` bounds a result.
|
|
1708
|
+
* Never throws — a completed run must not fail at model assembly because its
|
|
1709
|
+
* value carried a BigInt or a cycle.
|
|
1710
|
+
*/
|
|
1711
|
+
function toModelOutput(output) {
|
|
1712
|
+
if (typeof output !== "object" || output === null || Array.isArray(output)) return {
|
|
1713
|
+
type: "json",
|
|
1714
|
+
value: toJSONValue(output)
|
|
1715
|
+
};
|
|
1716
|
+
const { calls: _calls, ...rest } = output;
|
|
1717
|
+
if (Array.isArray(rest.logs)) rest.logs = truncateResult(rest.logs);
|
|
1718
|
+
return {
|
|
1719
|
+
type: "json",
|
|
1720
|
+
value: toJSONValue(rest)
|
|
1721
|
+
};
|
|
1722
|
+
}
|
|
1723
|
+
function toJSONValue(value) {
|
|
1724
|
+
try {
|
|
1725
|
+
const serialized = JSON.stringify(value, (_key, v) => typeof v === "bigint" ? v.toString() : v);
|
|
1726
|
+
return serialized === void 0 ? null : JSON.parse(serialized);
|
|
1727
|
+
} catch (err) {
|
|
1728
|
+
return { error: `Result could not be serialized for the model: ${err instanceof Error ? err.message : String(err)}` };
|
|
1729
|
+
}
|
|
1730
|
+
}
|
|
1758
1731
|
function createProxyTool(options) {
|
|
1759
1732
|
const connectors = options.connectors;
|
|
1760
1733
|
validateConnectorNames(connectors);
|
|
@@ -1777,7 +1750,8 @@ function createProxyTool(options) {
|
|
|
1777
1750
|
maxExecutions: options.maxExecutions,
|
|
1778
1751
|
connectors: connectors.map((c) => c.name())
|
|
1779
1752
|
}), code, setup, runtime, options.executor, options.transformResult);
|
|
1780
|
-
}
|
|
1753
|
+
},
|
|
1754
|
+
toModelOutput: ({ output }) => toModelOutput(output)
|
|
1781
1755
|
};
|
|
1782
1756
|
}
|
|
1783
1757
|
/** Default runtime name when none is given. */
|