UNPKG

@tanstack/ai-code-mode

Version:

Code Mode for TanStack AI - LLM-driven code execution in secure sandboxes

67 lines (51 loc) 1.94 kB
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 bindings = toolsToBindings(tools, "external_"); const typeStubs = generateTypeStubs(bindings); const functionDocs = Object.entries(bindings).map(([name, binding]) => { const doc = `- \`${name}(input)\`: ${binding.description}`; return doc; }).join("\n"); return `## Code Execution Tool You have access to \`execute_typescript\` which runs TypeScript code in a sandboxed environment. ### When to Use 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} ### Type Definitions \`\`\`typescript ${typeStubs} \`\`\` ### Example \`\`\`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 }; \`\`\` ### Important Notes - 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