@cloudflare/codemode 0.5.1 → 0.5.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/dist/index.d.ts +37 -4
- package/dist/index.js +29 -56
- package/dist/index.js.map +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/index.md +2 -0
- package/docs/runtime.md +3 -1
- package/package.json +3 -4
package/dist/index.d.ts
CHANGED
|
@@ -65,6 +65,13 @@ declare function normalizeCode(code: string): string;
|
|
|
65
65
|
* structured results intact so the model can still reason over them. They are
|
|
66
66
|
* the default building blocks for a `transformResult` hook (see
|
|
67
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.
|
|
68
75
|
*/
|
|
69
76
|
type TruncateOptions = {
|
|
70
77
|
/**
|
|
@@ -85,9 +92,10 @@ declare function truncateResponse(
|
|
|
85
92
|
): string;
|
|
86
93
|
/**
|
|
87
94
|
* Truncate a structured result. Strings are truncated directly. Other values
|
|
88
|
-
* pass through unchanged when their JSON serialization is
|
|
89
|
-
* oversized, the
|
|
90
|
-
*
|
|
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.
|
|
91
99
|
*
|
|
92
100
|
* Values that can't be serialized (cycles, bigint, `undefined`) are returned
|
|
93
101
|
* unchanged.
|
|
@@ -526,7 +534,11 @@ type StandardSchemaIssue = {
|
|
|
526
534
|
type ProxyToolInputSchema = {
|
|
527
535
|
readonly "~standard": {
|
|
528
536
|
readonly version: 1;
|
|
529
|
-
readonly vendor: "@cloudflare/codemode"
|
|
537
|
+
readonly vendor: "@cloudflare/codemode" /** Lets Standard Schema consumers (e.g. TanStack AI) infer `{ code }`. */;
|
|
538
|
+
readonly types?: {
|
|
539
|
+
readonly input: ProxyToolInput;
|
|
540
|
+
readonly output: ProxyToolInput;
|
|
541
|
+
};
|
|
530
542
|
readonly validate: (value: unknown) =>
|
|
531
543
|
| {
|
|
532
544
|
readonly value: ProxyToolInput;
|
|
@@ -560,7 +572,28 @@ type CodemodeTool = {
|
|
|
560
572
|
input: ProxyToolInput,
|
|
561
573
|
options: unknown
|
|
562
574
|
) => Promise<ProxyToolOutput>;
|
|
575
|
+
/**
|
|
576
|
+
* The AI SDK model-facing projection of the output: everything but `calls`.
|
|
577
|
+
* The full output (with the durable call log) stays on the persisted tool
|
|
578
|
+
* part for UIs; the model sees the transformed `result`, `logs`, `pending`
|
|
579
|
+
* or `error`. Without this the raw call log — every connector call's args
|
|
580
|
+
* and result — would ride along uncapped and defeat `transformResult`.
|
|
581
|
+
*/
|
|
582
|
+
toModelOutput: (options: { output: unknown }) => {
|
|
583
|
+
type: "json";
|
|
584
|
+
value: JSONValue;
|
|
585
|
+
};
|
|
563
586
|
};
|
|
587
|
+
/** Structural twin of the AI SDK's `JSONValue`; the root entry must not import `ai`. */
|
|
588
|
+
type JSONValue =
|
|
589
|
+
| string
|
|
590
|
+
| number
|
|
591
|
+
| boolean
|
|
592
|
+
| null
|
|
593
|
+
| JSONValue[]
|
|
594
|
+
| {
|
|
595
|
+
[key: string]: JSONValue;
|
|
596
|
+
};
|
|
564
597
|
//#endregion
|
|
565
598
|
//#region src/runtime-handle.d.ts
|
|
566
599
|
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
|
|
@@ -1756,6 +1702,32 @@ async function applyTransform(transformResult, result) {
|
|
|
1756
1702
|
return result;
|
|
1757
1703
|
}
|
|
1758
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
|
+
}
|
|
1759
1731
|
function createProxyTool(options) {
|
|
1760
1732
|
const connectors = options.connectors;
|
|
1761
1733
|
validateConnectorNames(connectors);
|
|
@@ -1778,7 +1750,8 @@ function createProxyTool(options) {
|
|
|
1778
1750
|
maxExecutions: options.maxExecutions,
|
|
1779
1751
|
connectors: connectors.map((c) => c.name())
|
|
1780
1752
|
}), code, setup, runtime, options.executor, options.transformResult);
|
|
1781
|
-
}
|
|
1753
|
+
},
|
|
1754
|
+
toModelOutput: ({ output }) => toModelOutput(output)
|
|
1782
1755
|
};
|
|
1783
1756
|
}
|
|
1784
1757
|
/** Default runtime name when none is given. */
|