@tanstack/ai-code-mode
Version:
Secure TypeScript Code Mode for TanStack AI agents to execute sandboxed tool orchestration programs.
77 lines (58 loc) • 2.6 kB
JavaScript
import { renderLazyCatalogEntry } from "@tanstack/ai";
import { toolsToBindings } from "./bindings/tool-to-binding.js";
import { generateTypeStubs } from "./type-generator/json-schema-to-ts.js";
function createCodeModeSystemPrompt(config) {
const { tools } = config;
const include = config.lazyToolsConfig?.includeDescription ?? "none";
const eagerTools = tools.filter((t) => !t.lazy);
const lazyTools = tools.filter((t) => t.lazy);
const bindings = toolsToBindings(eagerTools, "external_");
const typeStubs = generateTypeStubs(bindings);
const functionDocs = Object.entries(bindings).map(([name, binding]) => `- \`${name}(input)\`: ${binding.description}`).join("\n");
const discoverableSection = lazyTools.length > 0 ? `
These additional functions are available but not yet documented. Before calling \`external_<name>\` for any of them inside \`execute_typescript\`, call the \`discover_tools\` tool with their names to get full TypeScript signatures:
${lazyTools.map(
(t) => `- ${renderLazyCatalogEntry(`external_${t.name}`, t.description, include)}`
).join("\n")}` : "";
return `
You have access to \`execute_typescript\` which runs TypeScript code in a sandboxed environment.
Use \`execute_typescript\` when you need to:
- Process data with loops, conditionals, or complex logic
- Make multiple API calls in parallel (Promise.all)
- Transform, filter, or aggregate data
- Perform calculations or data analysis
For simple operations, prefer calling tools directly.
### Available External APIs
Inside your TypeScript code, you can call these async functions:
${functionDocs}
\`\`\`typescript
${typeStubs}
\`\`\`${discoverableSection}
\`\`\`typescript
// Fetch weather for multiple cities in parallel
const cities = ["Tokyo", "Paris", "NYC"];
const results = await Promise.all(
cities.map(city => external_fetchWeather({ location: city }))
);
// Find the warmest city
const warmest = results.reduce((prev, curr) =>
curr.temperature > prev.temperature ? curr : prev
);
return { warmestCity: warmest.location, temperature: warmest.temperature };
\`\`\`
- All \`external_*\` calls are async - always use \`await\`
- Return a value to pass results back to you
- Use \`console.log()\` for debugging (logs are captured)
- The sandbox is isolated - no network access or file system
- Each execution is independent (no shared state between calls)
`;
}
export {
createCodeModeSystemPrompt
};
//# sourceMappingURL=create-system-prompt.js.map