MCPcopy
hub / github.com/TanStack/ai / codeModeWithSkills

Function codeModeWithSkills

packages/ai-code-mode-skills/src/code-mode-with-skills.ts:58–177  ·  view source on GitHub ↗
({
  config,
  adapter,
  skills,
  messages,
  skillsAsTools = true,
}: CodeModeWithSkillsOptions)

Source from the content-addressed store, hash-verified

56 * ```
57 */
58export 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',

Callers 3

runLiveTestFunction · 0.90
runSimulatedTestFunction · 0.90
runRegistryTestFunction · 0.90

Calls 12

selectRelevantSkillsFunction · 0.90
toolsToBindingsFunction · 0.90
createCodeModeToolFunction · 0.90
createToolRegistryFunction · 0.90
skillsToToolsFunction · 0.90
createSkillsSystemPromptFunction · 0.90
stripTypeScriptFunction · 0.85
createEventAwareBindingsFunction · 0.85
executeMethod · 0.45
disposeMethod · 0.45

Tested by

no test coverage detected