@ai-sdk/code-mode 1.0.77 → 1.0.80
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/approval-continuation.js +48 -74
- package/dist/approval-continuation.js.map +1 -1
- package/dist/approval.js +12 -23
- package/dist/approval.js.map +1 -1
- package/dist/code-mode-tool.js +39 -51
- package/dist/code-mode-tool.js.map +1 -1
- package/dist/continuation-capability.js +74 -105
- package/dist/continuation-capability.js.map +1 -1
- package/dist/direct-tool-call.js +9 -3
- package/dist/direct-tool-call.js.map +1 -1
- package/dist/errors.js +109 -96
- package/dist/errors.js.map +1 -1
- package/dist/host-interrupt.js +7 -12
- package/dist/host-interrupt.js.map +1 -1
- package/dist/index.d.ts +172 -163
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -61
- package/dist/interrupt-continuation.js +54 -90
- package/dist/interrupt-continuation.js.map +1 -1
- package/dist/run-code-mode.js +292 -494
- package/dist/run-code-mode.js.map +1 -1
- package/dist/tool-invocation.js +98 -176
- package/dist/tool-invocation.js.map +1 -1
- package/dist/tool-prompt.js +254 -392
- package/dist/tool-prompt.js.map +1 -1
- package/dist/types.js +1 -1
- package/dist/utils/serialization.js +23 -39
- package/dist/utils/serialization.js.map +1 -1
- package/package.json +6 -6
- package/dist/index.js.map +0 -1
- package/dist/types.js.map +0 -1
package/dist/tool-prompt.js
CHANGED
|
@@ -1,468 +1,330 @@
|
|
|
1
1
|
import { asSchema } from "ai";
|
|
2
|
+
//#region src/tool-prompt.ts
|
|
2
3
|
const MAX_SCHEMA_DEPTH = 8;
|
|
3
4
|
const MAX_COMPACT_OBJECT_TYPE_LENGTH = 120;
|
|
4
5
|
function buildCodeModeToolDescription(tools, toolDiscovery = "description") {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const { typeBlock, exampleBlock } = renderToolCatalog(tools);
|
|
22
|
-
sections.push("", "Tools:", typeBlock);
|
|
23
|
-
if (exampleBlock.length > 0) {
|
|
24
|
-
sections.push(exampleBlock);
|
|
25
|
-
}
|
|
26
|
-
return sections.join("\n");
|
|
6
|
+
const sections = [
|
|
7
|
+
"Execute code-mode TypeScript in an isolated sandbox.",
|
|
8
|
+
"",
|
|
9
|
+
"Put the full program in `js`; top-level `await`/`return` work. Return a JSON-serializable result.",
|
|
10
|
+
"Call host tools only as async `tools.name(input)`; await each or use `Promise.all` for independent calls.",
|
|
11
|
+
toolDiscovery === "description" ? "Use exact names/types below. `JSON.parse`/`JSON.stringify` are available." : "Use exact names/types from the latest capability update. `JSON.parse`/`JSON.stringify` are available.",
|
|
12
|
+
"Fetch: `fetch` is not available."
|
|
13
|
+
];
|
|
14
|
+
if (toolDiscovery === "conversation") {
|
|
15
|
+
sections.push("", "Tools:", "The current host-tool API is provided in \"Code mode capability update\" user messages. Follow the latest catalog and ignore earlier catalogs.");
|
|
16
|
+
return sections.join("\n");
|
|
17
|
+
}
|
|
18
|
+
const { typeBlock, exampleBlock } = renderToolCatalog(tools);
|
|
19
|
+
sections.push("", "Tools:", typeBlock);
|
|
20
|
+
if (exampleBlock.length > 0) sections.push(exampleBlock);
|
|
21
|
+
return sections.join("\n");
|
|
27
22
|
}
|
|
28
23
|
function buildCodeModeToolCatalogMessage(tools) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
return sections.join("\n");
|
|
24
|
+
const { typeBlock, exampleBlock } = renderToolCatalog(tools);
|
|
25
|
+
const sections = [
|
|
26
|
+
"Code mode capability update.",
|
|
27
|
+
"",
|
|
28
|
+
"This catalog replaces all previous code mode capability catalogs. Only the tools listed below are currently available through `tools`.",
|
|
29
|
+
"",
|
|
30
|
+
"Tools:",
|
|
31
|
+
typeBlock
|
|
32
|
+
];
|
|
33
|
+
if (exampleBlock.length > 0) sections.push(exampleBlock);
|
|
34
|
+
return sections.join("\n");
|
|
42
35
|
}
|
|
43
36
|
function renderToolCatalog(tools) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
37
|
+
const toolEntries = Object.entries(tools);
|
|
38
|
+
return {
|
|
39
|
+
typeBlock: toolEntries.length === 0 ? "No host tools. Do not call `tools.*`." : [
|
|
40
|
+
"```ts",
|
|
41
|
+
"declare const tools: {",
|
|
42
|
+
...toolEntries.flatMap(renderToolType),
|
|
43
|
+
"};",
|
|
44
|
+
"```"
|
|
45
|
+
].join("\n"),
|
|
46
|
+
exampleBlock: toolEntries.length === 0 ? "" : [
|
|
47
|
+
"",
|
|
48
|
+
"Tool call examples:",
|
|
49
|
+
"```ts",
|
|
50
|
+
...renderToolExamples(toolEntries),
|
|
51
|
+
"```"
|
|
52
|
+
].join("\n")
|
|
53
|
+
};
|
|
60
54
|
}
|
|
61
55
|
function renderToolType([toolName, tool]) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
lines.push(
|
|
72
|
-
` ${formatObjectKey(toolName)}: (input: ${indentType(inputType, 2)}) => Promise<${indentType(outputType, 2)}>;`
|
|
73
|
-
);
|
|
74
|
-
return lines;
|
|
56
|
+
const schema = resolveInputSchema(tool);
|
|
57
|
+
const inputType = schema === void 0 ? "unknown" : schemaToType(schema, schema);
|
|
58
|
+
const outputSchema = resolveOutputSchema(tool);
|
|
59
|
+
const outputType = outputSchema === void 0 ? "unknown" : schemaToType(outputSchema, outputSchema);
|
|
60
|
+
const description = firstString(tool.description, tool.title);
|
|
61
|
+
const lines = [];
|
|
62
|
+
if (description !== void 0) lines.push(` /** ${toComment(description)} */`);
|
|
63
|
+
lines.push(` ${formatObjectKey(toolName)}: (input: ${indentType(inputType, 2)}) => Promise<${indentType(outputType, 2)}>;`);
|
|
64
|
+
return lines;
|
|
75
65
|
}
|
|
76
66
|
function renderToolExamples(toolEntries) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
"return {",
|
|
96
|
-
...toolEntries.map(([toolName, tool], index) => {
|
|
97
|
-
const projection = renderToolProjection(variableNames[index], tool);
|
|
98
|
-
return ` ${formatObjectKey(toolName)}: ${projection},`;
|
|
99
|
-
}),
|
|
100
|
-
"};"
|
|
101
|
-
];
|
|
67
|
+
if (toolEntries.length === 1) {
|
|
68
|
+
const toolEntry = toolEntries[0];
|
|
69
|
+
const projection = renderToolProjection("result", toolEntry[1]);
|
|
70
|
+
return [`const result = await ${renderToolExampleCall(toolEntry)};`, `return ${projection};`];
|
|
71
|
+
}
|
|
72
|
+
const variableNames = uniqueToolVariableNames(toolEntries.map(([toolName]) => toolName));
|
|
73
|
+
const calls = toolEntries.map((toolEntry) => ` ${renderToolExampleCall(toolEntry)},`);
|
|
74
|
+
return [
|
|
75
|
+
`const [${variableNames.join(", ")}] = await Promise.all([`,
|
|
76
|
+
...calls,
|
|
77
|
+
"]);",
|
|
78
|
+
"return {",
|
|
79
|
+
...toolEntries.map(([toolName, tool], index) => {
|
|
80
|
+
const projection = renderToolProjection(variableNames[index], tool);
|
|
81
|
+
return ` ${formatObjectKey(toolName)}: ${projection},`;
|
|
82
|
+
}),
|
|
83
|
+
"};"
|
|
84
|
+
];
|
|
102
85
|
}
|
|
103
86
|
function renderToolExampleCall([toolName, tool]) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
87
|
+
const schema = resolveInputSchema(tool);
|
|
88
|
+
const input = firstInputExample(tool) ?? (schema === void 0 ? {} : sampleFromSchema(schema, schema));
|
|
89
|
+
return `tools${formatPropertyAccess(toolName)}(${JSON.stringify(input)})`;
|
|
107
90
|
}
|
|
108
91
|
function renderToolProjection(variableName, tool) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
return `{ ${formatObjectKey(field)}: ${variableName}${formatPropertyAccess(field)} }`;
|
|
92
|
+
const field = firstObjectProperty(resolveOutputSchema(tool));
|
|
93
|
+
if (field === void 0) return variableName;
|
|
94
|
+
return `{ ${formatObjectKey(field)}: ${variableName}${formatPropertyAccess(field)} }`;
|
|
114
95
|
}
|
|
115
96
|
function firstObjectProperty(schema) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
}
|
|
119
|
-
return Object.entries(schema.properties).find(
|
|
120
|
-
(entry) => isRecord(entry[1])
|
|
121
|
-
)?.[0];
|
|
97
|
+
if (schema === void 0 || !isRecord(schema.properties)) return;
|
|
98
|
+
return Object.entries(schema.properties).find((entry) => isRecord(entry[1]))?.[0];
|
|
122
99
|
}
|
|
123
100
|
function resolveInputSchema(tool) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return void 0;
|
|
132
|
-
}
|
|
101
|
+
try {
|
|
102
|
+
const jsonSchema = asSchema(tool.inputSchema).jsonSchema;
|
|
103
|
+
if (isPromiseLike(jsonSchema) || !isRecord(jsonSchema)) return;
|
|
104
|
+
return jsonSchema;
|
|
105
|
+
} catch {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
133
108
|
}
|
|
134
109
|
function resolveOutputSchema(tool) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return jsonSchema;
|
|
145
|
-
} catch {
|
|
146
|
-
return void 0;
|
|
147
|
-
}
|
|
110
|
+
try {
|
|
111
|
+
const outputSchema = tool.outputSchema;
|
|
112
|
+
if (outputSchema === void 0) return;
|
|
113
|
+
const jsonSchema = asSchema(outputSchema).jsonSchema;
|
|
114
|
+
if (isPromiseLike(jsonSchema) || !isRecord(jsonSchema)) return;
|
|
115
|
+
return jsonSchema;
|
|
116
|
+
} catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
148
119
|
}
|
|
149
120
|
function schemaToType(schema, root) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
121
|
+
return schemaToTypeInner(schema, {
|
|
122
|
+
root,
|
|
123
|
+
seenRefs: /* @__PURE__ */ new Set(),
|
|
124
|
+
depth: 0
|
|
125
|
+
});
|
|
155
126
|
}
|
|
156
127
|
function schemaToTypeInner(schema, context) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const allOf = schemaArray(schema.allOf);
|
|
186
|
-
if (allOf !== void 0) {
|
|
187
|
-
return allOf.map((part) => schemaToTypeInner(part, nextContext(context))).join(" & ");
|
|
188
|
-
}
|
|
189
|
-
const type = schema.type;
|
|
190
|
-
if (Array.isArray(type)) {
|
|
191
|
-
return union(
|
|
192
|
-
type.map((part) => schemaToTypeInner({ ...schema, type: part }, context))
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
if (type === "object" || isRecord(schema.properties)) {
|
|
196
|
-
return objectType(schema, context);
|
|
197
|
-
}
|
|
198
|
-
if (type === "array" || schema.items !== void 0) {
|
|
199
|
-
return arrayType(schema, context);
|
|
200
|
-
}
|
|
201
|
-
if (type === "string") {
|
|
202
|
-
return "string";
|
|
203
|
-
}
|
|
204
|
-
if (type === "number" || type === "integer") {
|
|
205
|
-
return "number";
|
|
206
|
-
}
|
|
207
|
-
if (type === "boolean") {
|
|
208
|
-
return "boolean";
|
|
209
|
-
}
|
|
210
|
-
if (type === "null") {
|
|
211
|
-
return "null";
|
|
212
|
-
}
|
|
213
|
-
return "unknown";
|
|
128
|
+
if (context.depth > MAX_SCHEMA_DEPTH) return "unknown";
|
|
129
|
+
const ref = firstString(schema.$ref);
|
|
130
|
+
if (ref !== void 0) {
|
|
131
|
+
const resolved = resolveRef(ref, context.root, context.seenRefs);
|
|
132
|
+
return resolved === void 0 ? "unknown" : schemaToTypeInner(resolved, nextContext(context));
|
|
133
|
+
}
|
|
134
|
+
const constValue = schema.const;
|
|
135
|
+
if (constValue !== void 0) return literalType(constValue);
|
|
136
|
+
const enumValues = asArray(schema.enum);
|
|
137
|
+
if (enumValues !== void 0) return union(enumValues.map(literalType));
|
|
138
|
+
const oneOf = schemaArray(schema.oneOf);
|
|
139
|
+
if (oneOf !== void 0) return union(oneOf.map((part) => schemaToTypeInner(part, nextContext(context))));
|
|
140
|
+
const anyOf = schemaArray(schema.anyOf);
|
|
141
|
+
if (anyOf !== void 0) return union(anyOf.map((part) => schemaToTypeInner(part, nextContext(context))));
|
|
142
|
+
const allOf = schemaArray(schema.allOf);
|
|
143
|
+
if (allOf !== void 0) return allOf.map((part) => schemaToTypeInner(part, nextContext(context))).join(" & ");
|
|
144
|
+
const type = schema.type;
|
|
145
|
+
if (Array.isArray(type)) return union(type.map((part) => schemaToTypeInner({
|
|
146
|
+
...schema,
|
|
147
|
+
type: part
|
|
148
|
+
}, context)));
|
|
149
|
+
if (type === "object" || isRecord(schema.properties)) return objectType(schema, context);
|
|
150
|
+
if (type === "array" || schema.items !== void 0) return arrayType(schema, context);
|
|
151
|
+
if (type === "string") return "string";
|
|
152
|
+
if (type === "number" || type === "integer") return "number";
|
|
153
|
+
if (type === "boolean") return "boolean";
|
|
154
|
+
if (type === "null") return "null";
|
|
155
|
+
return "unknown";
|
|
214
156
|
}
|
|
215
157
|
function objectType(schema, context) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
` ${formatObjectKey(name)}${required.has(name) ? "" : "?"}: ${indentType(
|
|
239
|
-
schemaToTypeInner(value, nextContext(context)),
|
|
240
|
-
2
|
|
241
|
-
)};`
|
|
242
|
-
);
|
|
243
|
-
return propertyLines;
|
|
244
|
-
});
|
|
245
|
-
const additionalProperties = schema.additionalProperties;
|
|
246
|
-
if (isRecord(additionalProperties)) {
|
|
247
|
-
lines.push(
|
|
248
|
-
` [key: string]: ${schemaToTypeInner(additionalProperties, nextContext(context))};`
|
|
249
|
-
);
|
|
250
|
-
} else if (additionalProperties === true) {
|
|
251
|
-
lines.push(" [key: string]: unknown;");
|
|
252
|
-
}
|
|
253
|
-
const compact = compactObjectType(lines);
|
|
254
|
-
if (compact !== void 0) {
|
|
255
|
-
return compact;
|
|
256
|
-
}
|
|
257
|
-
return `{
|
|
258
|
-
${lines.join("\n")}
|
|
259
|
-
}`;
|
|
158
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
159
|
+
const required = new Set(asArray(schema.required)?.filter(isString));
|
|
160
|
+
const entries = Object.entries(properties).filter((entry) => isRecord(entry[1]));
|
|
161
|
+
if (entries.length === 0) {
|
|
162
|
+
const additionalProperties = schema.additionalProperties;
|
|
163
|
+
if (isRecord(additionalProperties)) return `Record<string, ${schemaToTypeInner(additionalProperties, nextContext(context))}>`;
|
|
164
|
+
if (additionalProperties === true) return "Record<string, unknown>";
|
|
165
|
+
return "{}";
|
|
166
|
+
}
|
|
167
|
+
const lines = entries.flatMap(([name, value]) => {
|
|
168
|
+
const propertyLines = [];
|
|
169
|
+
const description = firstString(value.description);
|
|
170
|
+
if (description !== void 0) propertyLines.push(` /** ${toComment(description)} */`);
|
|
171
|
+
propertyLines.push(` ${formatObjectKey(name)}${required.has(name) ? "" : "?"}: ${indentType(schemaToTypeInner(value, nextContext(context)), 2)};`);
|
|
172
|
+
return propertyLines;
|
|
173
|
+
});
|
|
174
|
+
const additionalProperties = schema.additionalProperties;
|
|
175
|
+
if (isRecord(additionalProperties)) lines.push(` [key: string]: ${schemaToTypeInner(additionalProperties, nextContext(context))};`);
|
|
176
|
+
else if (additionalProperties === true) lines.push(" [key: string]: unknown;");
|
|
177
|
+
const compact = compactObjectType(lines);
|
|
178
|
+
if (compact !== void 0) return compact;
|
|
179
|
+
return `{\n${lines.join("\n")}\n}`;
|
|
260
180
|
}
|
|
261
181
|
function arrayType(schema, context) {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const itemType = schemaToTypeInner(items, nextContext(context));
|
|
270
|
-
return needsParentheses(itemType) || itemType.includes("\n") ? `Array<${itemType}>` : `${itemType}[]`;
|
|
271
|
-
}
|
|
272
|
-
return "unknown[]";
|
|
182
|
+
const items = schema.items;
|
|
183
|
+
if (Array.isArray(items)) return `[${items.map((item) => isRecord(item) ? schemaToTypeInner(item, nextContext(context)) : "unknown").join(", ")}]`;
|
|
184
|
+
if (isRecord(items)) {
|
|
185
|
+
const itemType = schemaToTypeInner(items, nextContext(context));
|
|
186
|
+
return needsParentheses(itemType) || itemType.includes("\n") ? `Array<${itemType}>` : `${itemType}[]`;
|
|
187
|
+
}
|
|
188
|
+
return "unknown[]";
|
|
273
189
|
}
|
|
274
190
|
function sampleFromSchema(schema, root) {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
191
|
+
return sampleFromSchemaInner(schema, {
|
|
192
|
+
root,
|
|
193
|
+
seenRefs: /* @__PURE__ */ new Set(),
|
|
194
|
+
depth: 0
|
|
195
|
+
});
|
|
280
196
|
}
|
|
281
197
|
function sampleFromSchemaInner(schema, context) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
if (type === "array" || schema.items !== void 0) {
|
|
325
|
-
const items = Array.isArray(schema.items) ? schema.items[0] : schema.items;
|
|
326
|
-
return [
|
|
327
|
-
isRecord(items) ? sampleFromSchemaInner(items, nextContext(context)) : null
|
|
328
|
-
];
|
|
329
|
-
}
|
|
330
|
-
if (type === "number" || type === "integer") {
|
|
331
|
-
return 1;
|
|
332
|
-
}
|
|
333
|
-
if (type === "boolean") {
|
|
334
|
-
return true;
|
|
335
|
-
}
|
|
336
|
-
if (type === "null") {
|
|
337
|
-
return null;
|
|
338
|
-
}
|
|
339
|
-
if (type === "string" || type === void 0) {
|
|
340
|
-
const examples = asArray(schema.examples);
|
|
341
|
-
if (examples?.[0] !== void 0) {
|
|
342
|
-
return examples[0];
|
|
343
|
-
}
|
|
344
|
-
const format = firstString(schema.format);
|
|
345
|
-
if (format === "uri" || format === "url") {
|
|
346
|
-
return "https://example.com";
|
|
347
|
-
}
|
|
348
|
-
if (format === "date-time") {
|
|
349
|
-
return "2026-01-01T00:00:00.000Z";
|
|
350
|
-
}
|
|
351
|
-
if (format === "date") {
|
|
352
|
-
return "2026-01-01";
|
|
353
|
-
}
|
|
354
|
-
return "string";
|
|
355
|
-
}
|
|
356
|
-
return null;
|
|
198
|
+
if (context.depth > MAX_SCHEMA_DEPTH) return null;
|
|
199
|
+
if (schema.default !== void 0) return schema.default;
|
|
200
|
+
const ref = firstString(schema.$ref);
|
|
201
|
+
if (ref !== void 0) {
|
|
202
|
+
const resolved = resolveRef(ref, context.root, context.seenRefs);
|
|
203
|
+
return resolved === void 0 ? null : sampleFromSchemaInner(resolved, nextContext(context));
|
|
204
|
+
}
|
|
205
|
+
if (schema.const !== void 0) return schema.const;
|
|
206
|
+
const enumValues = asArray(schema.enum);
|
|
207
|
+
if (enumValues !== void 0) return enumValues[0] ?? null;
|
|
208
|
+
const oneOf = schemaArray(schema.oneOf);
|
|
209
|
+
if (oneOf !== void 0) return sampleFromSchemaInner(oneOf[0] ?? {}, nextContext(context));
|
|
210
|
+
const anyOf = schemaArray(schema.anyOf);
|
|
211
|
+
if (anyOf !== void 0) return sampleFromSchemaInner(anyOf[0] ?? {}, nextContext(context));
|
|
212
|
+
const type = schema.type;
|
|
213
|
+
if (Array.isArray(type)) return sampleFromSchemaInner({
|
|
214
|
+
...schema,
|
|
215
|
+
type: type.find((part) => part !== "null") ?? type[0]
|
|
216
|
+
}, context);
|
|
217
|
+
if (type === "object" || isRecord(schema.properties)) {
|
|
218
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
219
|
+
return Object.fromEntries(Object.entries(properties).filter((entry) => isRecord(entry[1])).map(([name, value]) => [name, sampleFromSchemaInner(value, nextContext(context))]));
|
|
220
|
+
}
|
|
221
|
+
if (type === "array" || schema.items !== void 0) {
|
|
222
|
+
const items = Array.isArray(schema.items) ? schema.items[0] : schema.items;
|
|
223
|
+
return [isRecord(items) ? sampleFromSchemaInner(items, nextContext(context)) : null];
|
|
224
|
+
}
|
|
225
|
+
if (type === "number" || type === "integer") return 1;
|
|
226
|
+
if (type === "boolean") return true;
|
|
227
|
+
if (type === "null") return null;
|
|
228
|
+
if (type === "string" || type === void 0) {
|
|
229
|
+
const examples = asArray(schema.examples);
|
|
230
|
+
if (examples?.[0] !== void 0) return examples[0];
|
|
231
|
+
const format = firstString(schema.format);
|
|
232
|
+
if (format === "uri" || format === "url") return "https://example.com";
|
|
233
|
+
if (format === "date-time") return "2026-01-01T00:00:00.000Z";
|
|
234
|
+
if (format === "date") return "2026-01-01";
|
|
235
|
+
return "string";
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
357
238
|
}
|
|
358
239
|
function firstInputExample(tool) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
return isRecord(first) && "input" in first ? first.input : void 0;
|
|
240
|
+
const first = asArray(tool.inputExamples)?.[0];
|
|
241
|
+
return isRecord(first) && "input" in first ? first.input : void 0;
|
|
362
242
|
}
|
|
363
243
|
function resolveRef(ref, root, seenRefs) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
return current[part];
|
|
373
|
-
}, root);
|
|
374
|
-
return isRecord(value) ? value : void 0;
|
|
244
|
+
if (!ref.startsWith("#/") || seenRefs.has(ref)) return;
|
|
245
|
+
seenRefs.add(ref);
|
|
246
|
+
const value = ref.slice(2).split("/").map((part) => part.replace(/~1/g, "/").replace(/~0/g, "~")).reduce((current, part) => {
|
|
247
|
+
if (!isRecord(current)) return;
|
|
248
|
+
return current[part];
|
|
249
|
+
}, root);
|
|
250
|
+
return isRecord(value) ? value : void 0;
|
|
375
251
|
}
|
|
376
252
|
function nextContext(context) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
253
|
+
return {
|
|
254
|
+
...context,
|
|
255
|
+
depth: context.depth + 1
|
|
256
|
+
};
|
|
381
257
|
}
|
|
382
258
|
function union(types) {
|
|
383
|
-
|
|
384
|
-
|
|
259
|
+
const unique = [...new Set(types)];
|
|
260
|
+
return unique.length === 0 ? "unknown" : unique.join(" | ");
|
|
385
261
|
}
|
|
386
262
|
function literalType(value) {
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (typeof value === "number" || typeof value === "boolean" || value === null) {
|
|
391
|
-
return String(value);
|
|
392
|
-
}
|
|
393
|
-
return "unknown";
|
|
263
|
+
if (typeof value === "string") return JSON.stringify(value);
|
|
264
|
+
if (typeof value === "number" || typeof value === "boolean" || value === null) return String(value);
|
|
265
|
+
return "unknown";
|
|
394
266
|
}
|
|
395
267
|
function indentType(type, spaces) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const padding = " ".repeat(spaces);
|
|
400
|
-
return type.replaceAll("\n", `
|
|
401
|
-
${padding}`);
|
|
268
|
+
if (!type.includes("\n")) return type;
|
|
269
|
+
const padding = " ".repeat(spaces);
|
|
270
|
+
return type.replaceAll("\n", `\n${padding}`);
|
|
402
271
|
}
|
|
403
272
|
function needsParentheses(type) {
|
|
404
|
-
|
|
273
|
+
return type.includes(" | ") || type.includes(" & ");
|
|
405
274
|
}
|
|
406
275
|
function formatObjectKey(key) {
|
|
407
|
-
|
|
276
|
+
return isIdentifier(key) ? key : JSON.stringify(key);
|
|
408
277
|
}
|
|
409
278
|
function formatPropertyAccess(key) {
|
|
410
|
-
|
|
279
|
+
return isIdentifier(key) ? `.${key}` : `[${JSON.stringify(key)}]`;
|
|
411
280
|
}
|
|
412
281
|
function uniqueToolVariableNames(toolNames) {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
282
|
+
const seen = /* @__PURE__ */ new Map();
|
|
283
|
+
return toolNames.map((toolName) => {
|
|
284
|
+
const base = toIdentifier(toolName);
|
|
285
|
+
const count = seen.get(base) ?? 0;
|
|
286
|
+
seen.set(base, count + 1);
|
|
287
|
+
return count === 0 ? base : `${base}${count + 1}`;
|
|
288
|
+
});
|
|
420
289
|
}
|
|
421
290
|
function toIdentifier(value) {
|
|
422
|
-
|
|
423
|
-
|
|
291
|
+
const identifier = value.replace(/[^A-Za-z0-9_$]/g, "_").replace(/^[^A-Za-z_$]+/, "");
|
|
292
|
+
return identifier.length === 0 ? "tool" : identifier;
|
|
424
293
|
}
|
|
425
294
|
function isIdentifier(value) {
|
|
426
|
-
|
|
295
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value);
|
|
427
296
|
}
|
|
428
297
|
function compactObjectType(lines) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
return void 0;
|
|
435
|
-
}
|
|
436
|
-
const compact = `{ ${fields.join(" ")} }`;
|
|
437
|
-
return compact.length <= MAX_COMPACT_OBJECT_TYPE_LENGTH ? compact : void 0;
|
|
298
|
+
if (lines.length === 0 || lines.some((line) => line.includes("/**"))) return;
|
|
299
|
+
const fields = lines.map((line) => line.trim());
|
|
300
|
+
if (fields.some((field) => field.includes("\n"))) return;
|
|
301
|
+
const compact = `{ ${fields.join(" ")} }`;
|
|
302
|
+
return compact.length <= MAX_COMPACT_OBJECT_TYPE_LENGTH ? compact : void 0;
|
|
438
303
|
}
|
|
439
304
|
function toComment(value) {
|
|
440
|
-
|
|
305
|
+
return value.replaceAll("*/", "* /").replace(/\s+/g, " ").trim();
|
|
441
306
|
}
|
|
442
307
|
function firstString(...values) {
|
|
443
|
-
|
|
308
|
+
return values.find(isString);
|
|
444
309
|
}
|
|
445
310
|
function schemaArray(value) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
}
|
|
450
|
-
return values;
|
|
311
|
+
const values = asArray(value);
|
|
312
|
+
if (values === void 0 || !values.every(isRecord)) return;
|
|
313
|
+
return values;
|
|
451
314
|
}
|
|
452
315
|
function asArray(value) {
|
|
453
|
-
|
|
316
|
+
return Array.isArray(value) ? value : void 0;
|
|
454
317
|
}
|
|
455
318
|
function isRecord(value) {
|
|
456
|
-
|
|
319
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
457
320
|
}
|
|
458
321
|
function isString(value) {
|
|
459
|
-
|
|
322
|
+
return typeof value === "string";
|
|
460
323
|
}
|
|
461
324
|
function isPromiseLike(value) {
|
|
462
|
-
|
|
325
|
+
return isRecord(value) && typeof value.then === "function";
|
|
463
326
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
};
|
|
327
|
+
//#endregion
|
|
328
|
+
export { buildCodeModeToolCatalogMessage, buildCodeModeToolDescription };
|
|
329
|
+
|
|
468
330
|
//# sourceMappingURL=tool-prompt.js.map
|