Open History to see the last commit
1import { chat, maxIterations } from '@tanstack/ai'
2import {
3 createSkillManagementTools,
4 createSkillsSystemPrompt,
5 skillsToTools,
6} from '@tanstack/ai-code-mode-skills'
7import { maxTokensModelOptions } from './max-tokens-model-options'
8import type { AnyTextAdapter, AnyTool, SchemaInput } from '@tanstack/ai'
9import type { CodeModeTool, IsolateDriver } from '@tanstack/ai-code-mode'
10import type { SkillStorage, TrustStrategy } from '@tanstack/ai-code-mode-skills'
11
12export interface StructuredOutputOptions<TSchema extends SchemaInput> {
13 adapter: AnyTextAdapter
14 prompt: string
15 outputSchema: TSchema
16 codeMode: {
17 tool: AnyTool
18 systemPrompt: string
19 driver: IsolateDriver
20 codeTools: Array<CodeModeTool>
21 }
22 skills?: {
23 storage: SkillStorage
24 trustStrategy: TrustStrategy
25 timeout?: number
26 memoryLimit?: number
27 }
28 tools?: Array<AnyTool>
29 maxIterations?: number
30 maxTokens?: number
31}
32
33const SKILL_REGISTRATION_PROMPT = `## Skill Registration — MANDATORY
34
35After every successful \`execute_typescript\` call you MUST register the code as a reusable skill using \`register_skill\` — unless an identical skill already exists.
36
37Rules:
38- \`name\`: descriptive snake_case (e.g. \`get_average_product_price\`)
39- \`code\`: the TypeScript code, parameterised with an \`input\` variable where useful
40- \`inputSchema\` / \`outputSchema\`: valid JSON Schema **strings**
41- If a skill with the same name exists, skip registration
42
43This is not optional — skill registration is a core part of your workflow.`
44
45export async function structuredOutput<TSchema extends SchemaInput>(
46 options: StructuredOutputOptions<TSchema>,
47) {
48 const {
49 adapter,
50 prompt,
51 outputSchema,
52 codeMode,
53 skills,
54 tools = [],
55 maxIterations: maxIter = 10,
56 maxTokens: maxTok = 8192,
57 } = options
58
59 const skillGuidance = skills
60 ? `- If a skill tool matches what you need, call it directly — skill tools are faster and preferred over writing new code.
61- Use execute_typescript only for tasks not covered by existing skill tools. After successful execute_typescript calls, register the code as a reusable skill.`
62 : `- Use execute_typescript to gather the data you need. Chain multiple tool calls if needed.`
63
64 const systemPrompt = `${prompt}
65
66RULES:
67- Do NOT produce conversational text. No greetings, no narration. Only tool calls and the final structured response.
68${skillGuidance}`
69
70 let allTools: Array<AnyTool> = [codeMode.tool, ...tools]
71 const systemPrompts = [systemPrompt, codeMode.systemPrompt]
72
73 if (skills) {
74 const allSkills = await skills.storage.loadAll()
75 const skillIndex = await skills.storage.loadIndex()
76
77 if (allSkills.length > 0) {
78 const skillToolsList = skillsToTools({
79 skills: allSkills,
80 driver: codeMode.driver,
81 tools: codeMode.codeTools,
82 storage: skills.storage,
83 timeout: skills.timeout ?? 60000,
84 memoryLimit: skills.memoryLimit ?? 128,
85 })
86 allTools = [...allTools, ...skillToolsList]
87 }
88
89 const mgmtTools = createSkillManagementTools({
90 storage: skills.storage,
91 trustStrategy: skills.trustStrategy,
92 })
93 allTools = [...allTools, ...mgmtTools]
94
95 const libraryPrompt = createSkillsSystemPrompt({
96 selectedSkills: allSkills,
97 totalSkillCount: skillIndex.length,
98 skillsAsTools: true,
99 })
100 systemPrompts.push(libraryPrompt + '\n\n' + SKILL_REGISTRATION_PROMPT)
101 }
102
103 console.log(
104 '[StructuredOutput] Tools passed to chat:',
105 allTools.map((t) => t.name),
106 )
107
108 const result = await chat({
109 adapter,
110 messages: [{ role: 'user' as const, content: prompt }],
111 tools: allTools,
112 systemPrompts,
113 agentLoopStrategy: maxIterations(maxIter),
114 // Sampling lives in provider-native `modelOptions` now; map the generic
115 // cap to the resolved adapter's wire key.
116 modelOptions: maxTokensModelOptions(adapter, maxTok),
117 outputSchema,
118 })
119
120 return result
121}