@cloudflare/codemode 0.1.0 → 0.1.1
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/CHANGELOG.md +20 -0
- package/README.md +2 -2
- package/dist/ai.js +13 -4
- package/dist/ai.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/types-CpkEgXwN.js +392 -0
- package/dist/types-CpkEgXwN.js.map +1 -0
- package/package.json +2 -2
- package/src/tests/schema-conversion.test.ts +1068 -0
- package/src/tests/types.test.ts +275 -0
- package/src/tool.ts +25 -4
- package/src/types.ts +527 -52
- package/dist/types-B9g5T2nd.js +0 -138
- package/dist/types-B9g5T2nd.js.map +0 -1
package/dist/types-B9g5T2nd.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { createAuxiliaryTypeStore, createTypeAlias, printNode, zodToTs } from "zod-to-ts";
|
|
2
|
-
|
|
3
|
-
//#region src/types.ts
|
|
4
|
-
const JS_RESERVED = new Set([
|
|
5
|
-
"abstract",
|
|
6
|
-
"arguments",
|
|
7
|
-
"await",
|
|
8
|
-
"boolean",
|
|
9
|
-
"break",
|
|
10
|
-
"byte",
|
|
11
|
-
"case",
|
|
12
|
-
"catch",
|
|
13
|
-
"char",
|
|
14
|
-
"class",
|
|
15
|
-
"const",
|
|
16
|
-
"continue",
|
|
17
|
-
"debugger",
|
|
18
|
-
"default",
|
|
19
|
-
"delete",
|
|
20
|
-
"do",
|
|
21
|
-
"double",
|
|
22
|
-
"else",
|
|
23
|
-
"enum",
|
|
24
|
-
"eval",
|
|
25
|
-
"export",
|
|
26
|
-
"extends",
|
|
27
|
-
"false",
|
|
28
|
-
"final",
|
|
29
|
-
"finally",
|
|
30
|
-
"float",
|
|
31
|
-
"for",
|
|
32
|
-
"function",
|
|
33
|
-
"goto",
|
|
34
|
-
"if",
|
|
35
|
-
"implements",
|
|
36
|
-
"import",
|
|
37
|
-
"in",
|
|
38
|
-
"instanceof",
|
|
39
|
-
"int",
|
|
40
|
-
"interface",
|
|
41
|
-
"let",
|
|
42
|
-
"long",
|
|
43
|
-
"native",
|
|
44
|
-
"new",
|
|
45
|
-
"null",
|
|
46
|
-
"package",
|
|
47
|
-
"private",
|
|
48
|
-
"protected",
|
|
49
|
-
"public",
|
|
50
|
-
"return",
|
|
51
|
-
"short",
|
|
52
|
-
"static",
|
|
53
|
-
"super",
|
|
54
|
-
"switch",
|
|
55
|
-
"synchronized",
|
|
56
|
-
"this",
|
|
57
|
-
"throw",
|
|
58
|
-
"throws",
|
|
59
|
-
"transient",
|
|
60
|
-
"true",
|
|
61
|
-
"try",
|
|
62
|
-
"typeof",
|
|
63
|
-
"undefined",
|
|
64
|
-
"var",
|
|
65
|
-
"void",
|
|
66
|
-
"volatile",
|
|
67
|
-
"while",
|
|
68
|
-
"with",
|
|
69
|
-
"yield"
|
|
70
|
-
]);
|
|
71
|
-
/**
|
|
72
|
-
* Sanitize a tool name into a valid JavaScript identifier.
|
|
73
|
-
* Replaces hyphens, dots, and spaces with `_`, strips other invalid chars,
|
|
74
|
-
* prefixes digit-leading names with `_`, and appends `_` to JS reserved words.
|
|
75
|
-
*/
|
|
76
|
-
function sanitizeToolName(name) {
|
|
77
|
-
if (!name) return "_";
|
|
78
|
-
let sanitized = name.replace(/[-.\s]/g, "_");
|
|
79
|
-
sanitized = sanitized.replace(/[^a-zA-Z0-9_$]/g, "");
|
|
80
|
-
if (!sanitized) return "_";
|
|
81
|
-
if (/^[0-9]/.test(sanitized)) sanitized = "_" + sanitized;
|
|
82
|
-
if (JS_RESERVED.has(sanitized)) sanitized = sanitized + "_";
|
|
83
|
-
return sanitized;
|
|
84
|
-
}
|
|
85
|
-
function toCamelCase(str) {
|
|
86
|
-
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()).replace(/^[a-z]/, (letter) => letter.toUpperCase());
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Extract field descriptions from a Zod object schema's `.shape`, if available.
|
|
90
|
-
* Returns an array of `@param input.fieldName - description` lines.
|
|
91
|
-
*/
|
|
92
|
-
function extractParamDescriptions(schema) {
|
|
93
|
-
const descriptions = [];
|
|
94
|
-
const shape = schema.shape;
|
|
95
|
-
if (!shape || typeof shape !== "object") return descriptions;
|
|
96
|
-
for (const [fieldName, fieldSchema] of Object.entries(shape)) {
|
|
97
|
-
const desc = fieldSchema.description;
|
|
98
|
-
if (desc) descriptions.push(`@param input.${fieldName} - ${desc}`);
|
|
99
|
-
}
|
|
100
|
-
return descriptions;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Generate TypeScript type definitions from tool descriptors or an AI SDK ToolSet.
|
|
104
|
-
* These types can be included in tool descriptions to help LLMs write correct code.
|
|
105
|
-
*/
|
|
106
|
-
function generateTypes(tools) {
|
|
107
|
-
let availableTools = "";
|
|
108
|
-
let availableTypes = "";
|
|
109
|
-
const auxiliaryTypeStore = createAuxiliaryTypeStore();
|
|
110
|
-
for (const [toolName, tool] of Object.entries(tools)) {
|
|
111
|
-
const inputSchema = "inputSchema" in tool ? tool.inputSchema : tool.parameters;
|
|
112
|
-
const outputSchema = "outputSchema" in tool ? tool.outputSchema : void 0;
|
|
113
|
-
const description = tool.description;
|
|
114
|
-
const safeName = sanitizeToolName(toolName);
|
|
115
|
-
const inputType = printNode(createTypeAlias(zodToTs(inputSchema, { auxiliaryTypeStore }).node, `${toCamelCase(safeName)}Input`));
|
|
116
|
-
const outputType = outputSchema ? printNode(createTypeAlias(zodToTs(outputSchema, { auxiliaryTypeStore }).node, `${toCamelCase(safeName)}Output`)) : `type ${toCamelCase(safeName)}Output = unknown`;
|
|
117
|
-
availableTypes += `\n${inputType.trim()}`;
|
|
118
|
-
availableTypes += `\n${outputType.trim()}`;
|
|
119
|
-
const paramDescs = extractParamDescriptions(inputSchema);
|
|
120
|
-
const jsdocLines = [];
|
|
121
|
-
if (description?.trim()) jsdocLines.push(description.trim());
|
|
122
|
-
else jsdocLines.push(toolName);
|
|
123
|
-
for (const pd of paramDescs) jsdocLines.push(pd);
|
|
124
|
-
const jsdocBody = jsdocLines.map((l) => `\t * ${l}`).join("\n");
|
|
125
|
-
availableTools += `\n\t/**\n${jsdocBody}\n\t */`;
|
|
126
|
-
availableTools += `\n\t${safeName}: (input: ${toCamelCase(safeName)}Input) => Promise<${toCamelCase(safeName)}Output>;`;
|
|
127
|
-
availableTools += "\n";
|
|
128
|
-
}
|
|
129
|
-
availableTools = `\ndeclare const codemode: {${availableTools}}`;
|
|
130
|
-
return `
|
|
131
|
-
${availableTypes}
|
|
132
|
-
${availableTools}
|
|
133
|
-
`.trim();
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
//#endregion
|
|
137
|
-
export { sanitizeToolName as n, generateTypes as t };
|
|
138
|
-
//# sourceMappingURL=types-B9g5T2nd.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-B9g5T2nd.js","names":["printNodeZodToTs"],"sources":["../src/types.ts"],"sourcesContent":["import {\n zodToTs,\n printNode as printNodeZodToTs,\n createTypeAlias,\n createAuxiliaryTypeStore\n} from \"zod-to-ts\";\nimport type { ZodType } from \"zod\";\nimport type { ToolSet } from \"ai\";\n\nconst JS_RESERVED = new Set([\n \"abstract\",\n \"arguments\",\n \"await\",\n \"boolean\",\n \"break\",\n \"byte\",\n \"case\",\n \"catch\",\n \"char\",\n \"class\",\n \"const\",\n \"continue\",\n \"debugger\",\n \"default\",\n \"delete\",\n \"do\",\n \"double\",\n \"else\",\n \"enum\",\n \"eval\",\n \"export\",\n \"extends\",\n \"false\",\n \"final\",\n \"finally\",\n \"float\",\n \"for\",\n \"function\",\n \"goto\",\n \"if\",\n \"implements\",\n \"import\",\n \"in\",\n \"instanceof\",\n \"int\",\n \"interface\",\n \"let\",\n \"long\",\n \"native\",\n \"new\",\n \"null\",\n \"package\",\n \"private\",\n \"protected\",\n \"public\",\n \"return\",\n \"short\",\n \"static\",\n \"super\",\n \"switch\",\n \"synchronized\",\n \"this\",\n \"throw\",\n \"throws\",\n \"transient\",\n \"true\",\n \"try\",\n \"typeof\",\n \"undefined\",\n \"var\",\n \"void\",\n \"volatile\",\n \"while\",\n \"with\",\n \"yield\"\n]);\n\n/**\n * Sanitize a tool name into a valid JavaScript identifier.\n * Replaces hyphens, dots, and spaces with `_`, strips other invalid chars,\n * prefixes digit-leading names with `_`, and appends `_` to JS reserved words.\n */\nexport function sanitizeToolName(name: string): string {\n if (!name) return \"_\";\n\n // Replace common separators with underscores\n let sanitized = name.replace(/[-.\\s]/g, \"_\");\n\n // Strip any remaining non-identifier characters\n sanitized = sanitized.replace(/[^a-zA-Z0-9_$]/g, \"\");\n\n if (!sanitized) return \"_\";\n\n // Prefix with _ if starts with a digit\n if (/^[0-9]/.test(sanitized)) {\n sanitized = \"_\" + sanitized;\n }\n\n // Append _ to reserved words\n if (JS_RESERVED.has(sanitized)) {\n sanitized = sanitized + \"_\";\n }\n\n return sanitized;\n}\n\nfunction toCamelCase(str: string) {\n return str\n .replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())\n .replace(/^[a-z]/, (letter) => letter.toUpperCase());\n}\n\n/**\n * Extract field descriptions from a Zod object schema's `.shape`, if available.\n * Returns an array of `@param input.fieldName - description` lines.\n */\nfunction extractParamDescriptions(schema: ZodType): string[] {\n const descriptions: string[] = [];\n const shape = (schema as { shape?: Record<string, ZodType> }).shape;\n if (!shape || typeof shape !== \"object\") return descriptions;\n\n for (const [fieldName, fieldSchema] of Object.entries(shape)) {\n const desc = (fieldSchema as { description?: string }).description;\n if (desc) {\n descriptions.push(`@param input.${fieldName} - ${desc}`);\n }\n }\n return descriptions;\n}\n\nexport interface ToolDescriptor {\n description?: string;\n inputSchema: ZodType;\n outputSchema?: ZodType;\n execute?: (args: unknown) => Promise<unknown>;\n}\n\nexport type ToolDescriptors = Record<string, ToolDescriptor>;\n\n/**\n * Generate TypeScript type definitions from tool descriptors or an AI SDK ToolSet.\n * These types can be included in tool descriptions to help LLMs write correct code.\n */\nexport function generateTypes(tools: ToolDescriptors | ToolSet): string {\n let availableTools = \"\";\n let availableTypes = \"\";\n\n const auxiliaryTypeStore = createAuxiliaryTypeStore();\n\n for (const [toolName, tool] of Object.entries(tools)) {\n // Handle both our ToolDescriptor and AI SDK Tool types\n const inputSchema =\n \"inputSchema\" in tool ? tool.inputSchema : tool.parameters;\n const outputSchema = \"outputSchema\" in tool ? tool.outputSchema : undefined;\n const description = tool.description;\n\n const safeName = sanitizeToolName(toolName);\n\n const inputType = printNodeZodToTs(\n createTypeAlias(\n zodToTs(inputSchema as ZodType, { auxiliaryTypeStore }).node,\n `${toCamelCase(safeName)}Input`\n )\n );\n\n const outputType = outputSchema\n ? printNodeZodToTs(\n createTypeAlias(\n zodToTs(outputSchema as ZodType, { auxiliaryTypeStore }).node,\n `${toCamelCase(safeName)}Output`\n )\n )\n : `type ${toCamelCase(safeName)}Output = unknown`;\n\n availableTypes += `\\n${inputType.trim()}`;\n availableTypes += `\\n${outputType.trim()}`;\n\n // Build JSDoc comment with description and param descriptions\n const paramDescs = extractParamDescriptions(inputSchema as ZodType);\n const jsdocLines: string[] = [];\n if (description?.trim()) {\n jsdocLines.push(description.trim());\n } else {\n jsdocLines.push(toolName);\n }\n for (const pd of paramDescs) {\n jsdocLines.push(pd);\n }\n\n const jsdocBody = jsdocLines.map((l) => `\\t * ${l}`).join(\"\\n\");\n availableTools += `\\n\\t/**\\n${jsdocBody}\\n\\t */`;\n availableTools += `\\n\\t${safeName}: (input: ${toCamelCase(safeName)}Input) => Promise<${toCamelCase(safeName)}Output>;`;\n availableTools += \"\\n\";\n }\n\n availableTools = `\\ndeclare const codemode: {${availableTools}}`;\n\n return `\n${availableTypes}\n${availableTools}\n `.trim();\n}\n"],"mappings":";;;AASA,MAAM,cAAc,IAAI,IAAI;CAC1B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;C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