({
config,
adapter,
skills,
messages,
skillsAsTools = true,
}: CodeModeWithSkillsOptions)
| 56 | * ``` |
| 57 | */ |
| 58 | export async function codeModeWithSkills({ |
| 59 | config, |
| 60 | adapter, |
| 61 | skills, |
| 62 | messages, |
| 63 | skillsAsTools = true, |
| 64 | }: CodeModeWithSkillsOptions): Promise<CodeModeWithSkillsResult> { |
| 65 | const { storage, maxSkillsInContext = 5 } = skills |
| 66 | |
| 67 | // 1. Load the skill index (lightweight metadata only) |
| 68 | const skillIndex = await storage.loadIndex() |
| 69 | |
| 70 | // 2. Use adapter to select relevant skills based on transcript |
| 71 | const selectedSkills = await selectRelevantSkills({ |
| 72 | adapter, |
| 73 | messages, |
| 74 | skillIndex, |
| 75 | maxSkills: maxSkillsInContext, |
| 76 | storage, |
| 77 | }) |
| 78 | |
| 79 | // Pre-compute bindings from base tools (shared across skill executions) |
| 80 | const baseBindings = toolsToBindings(config.tools, 'external_') |
| 81 | |
| 82 | // 3. Create the execute_typescript tool with dynamic skill bindings |
| 83 | const codeModeTool = createCodeModeTool({ |
| 84 | ...config, |
| 85 | // Dynamic skill bindings - fetched at execution time |
| 86 | getSkillBindings: async () => { |
| 87 | // Get all skills from storage (includes newly registered ones) |
| 88 | const allSkills = await storage.loadAll() |
| 89 | // Convert to bindings with skill_ prefix |
| 90 | const skillBindings: Record<string, ToolBinding> = {} |
| 91 | for (const skill of allSkills) { |
| 92 | // Create a simple binding that executes the skill code |
| 93 | skillBindings[`skill_${skill.name}`] = { |
| 94 | name: `skill_${skill.name}`, |
| 95 | description: skill.description, |
| 96 | inputSchema: skill.inputSchema, |
| 97 | outputSchema: skill.outputSchema, |
| 98 | execute: async (input: unknown) => { |
| 99 | // This is a simplified execution - the full skillToTool handles events |
| 100 | const wrappedCode = `const input = ${JSON.stringify(input)};\n${skill.code}` |
| 101 | const { stripTypeScript, createEventAwareBindings } = |
| 102 | await import('@tanstack/ai-code-mode') |
| 103 | const strippedCode = await stripTypeScript(wrappedCode) |
| 104 | const context = await config.driver.createContext({ |
| 105 | bindings: createEventAwareBindings(baseBindings, () => {}), |
| 106 | timeout: config.timeout, |
| 107 | ...(config.memoryLimit !== undefined && { |
| 108 | memoryLimit: config.memoryLimit, |
| 109 | }), |
| 110 | }) |
| 111 | try { |
| 112 | const result = await context.execute(strippedCode) |
| 113 | if (!result.success) { |
| 114 | throw new Error( |
| 115 | result.error?.message || 'Skill execution failed', |
no test coverage detected