|
| 1 | +/** |
| 2 | + * Example: Using createSkillTool with AI SDK ToolLoopAgent |
| 3 | + * |
| 4 | + * This example demonstrates how to create an AI agent with skills |
| 5 | + * that can process CSV and text files using bash tools. |
| 6 | + * |
| 7 | + * Run with: npx tsx examples/skills-tool/index.ts |
| 8 | + */ |
| 9 | + |
| 10 | +import path from "node:path"; |
| 11 | +import { ToolLoopAgent } from "ai"; |
| 12 | +import { |
| 13 | + createBashTool, |
| 14 | + experimental_createSkillTool as createSkillTool, |
| 15 | +} from "../../src/index.js"; |
| 16 | + |
| 17 | +async function main() { |
| 18 | + // Discover skills and get files to upload |
| 19 | + const { loadSkill, skills, files, instructions } = await createSkillTool({ |
| 20 | + skillsDirectory: path.join(import.meta.dirname, "skills"), |
| 21 | + }); |
| 22 | + |
| 23 | + console.log("Available skills:"); |
| 24 | + for (const skill of skills) { |
| 25 | + console.log(` - ${skill.name}: ${skill.description}`); |
| 26 | + } |
| 27 | + console.log(""); |
| 28 | + |
| 29 | + // Create bash tool with skill files |
| 30 | + const { tools } = await createBashTool({ |
| 31 | + files, |
| 32 | + extraInstructions: instructions, |
| 33 | + }); |
| 34 | + |
| 35 | + // Create the agent with skills |
| 36 | + const agent = new ToolLoopAgent({ |
| 37 | + model: "anthropic/claude-haiku-4.5", |
| 38 | + tools: { |
| 39 | + loadSkill, |
| 40 | + bash: tools.bash, |
| 41 | + }, |
| 42 | + instructions: `You are a data processing assistant with access to skills. |
| 43 | +Use loadSkill to discover how to use a skill, then use bash to run its scripts. |
| 44 | +Skills are located at ./skills/<skill-name>/.`, |
| 45 | + onStepFinish: ({ toolCalls, toolResults }) => { |
| 46 | + if (toolCalls && toolCalls.length > 0) { |
| 47 | + for (const call of toolCalls) { |
| 48 | + console.log(`Tool: ${call.toolName}`); |
| 49 | + if (call.toolName === "loadSkill" && "input" in call) { |
| 50 | + const input = call.input as { skillName: string }; |
| 51 | + console.log(` Loading skill: ${input.skillName}`); |
| 52 | + } else if (call.toolName === "bash" && "input" in call) { |
| 53 | + const input = call.input as { command: string }; |
| 54 | + console.log(` Command: ${input.command}`); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + if (toolResults && toolResults.length > 0) { |
| 59 | + for (const result of toolResults) { |
| 60 | + if (result.toolName === "bash" && "output" in result) { |
| 61 | + const output = result.output as { |
| 62 | + stdout: string; |
| 63 | + exitCode: number; |
| 64 | + }; |
| 65 | + if (output.stdout) { |
| 66 | + console.log(` Output:\n${output.stdout.slice(0, 500)}`); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + console.log(""); |
| 71 | + } |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + // Example prompt - the AI will discover and use skills as needed |
| 76 | + const prompt = ` |
| 77 | + I have a CSV file with sales data. Here's the content: |
| 78 | +
|
| 79 | + date,product,quantity,price,region |
| 80 | + 2024-01-15,Widget A,100,29.99,North |
| 81 | + 2024-01-15,Widget B,50,49.99,South |
| 82 | + 2024-01-16,Widget A,75,29.99,East |
| 83 | + 2024-01-16,Widget C,200,19.99,North |
| 84 | + 2024-01-17,Widget B,30,49.99,West |
| 85 | + 2024-01-17,Widget A,150,29.99,North |
| 86 | +
|
| 87 | + Please: |
| 88 | + 1. First, write this data to a file called sales.csv |
| 89 | + 2. Use the csv skill to analyze the file |
| 90 | + 3. Filter to show only North region sales |
| 91 | + 4. Sort by quantity (highest first) |
| 92 | + `; |
| 93 | + |
| 94 | + console.log("Sending prompt to agent...\n"); |
| 95 | + |
| 96 | + const result = await agent.generate({ prompt }); |
| 97 | + |
| 98 | + console.log("\n=== Final Response ===\n"); |
| 99 | + console.log(result.text); |
| 100 | + |
| 101 | + console.log("\n=== Agent Stats ==="); |
| 102 | + console.log(`Steps: ${result.steps.length}`); |
| 103 | + console.log(`Total tokens: ${result.usage.totalTokens}`); |
| 104 | +} |
| 105 | + |
| 106 | +main().catch(console.error); |
0 commit comments