|
| 1 | +import { invoke } from '@tauri-apps/api/core'; |
| 2 | +import { useFileStore } from '../stores/fileStore'; |
| 3 | +import type { ToolDefinition, ToolResult } from './agent-tools'; |
| 4 | + |
| 5 | +export function getPlanToolDefinitions(): ToolDefinition[] { |
| 6 | + return [ |
| 7 | + { |
| 8 | + type: 'function', |
| 9 | + function: { |
| 10 | + name: 'web_search', |
| 11 | + description: 'Search the internet for information about boards, sensors, libraries, tutorials, datasheets, pinout diagrams, and best practices.', |
| 12 | + parameters: { |
| 13 | + type: 'object', |
| 14 | + properties: { |
| 15 | + query: { type: 'string', description: 'The search query. Be specific: include board name, component model, and what you need to know.' }, |
| 16 | + }, |
| 17 | + required: ['query'], |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + { |
| 22 | + type: 'function', |
| 23 | + function: { |
| 24 | + name: 'read_file', |
| 25 | + description: 'Read the contents of any file in the project.', |
| 26 | + parameters: { |
| 27 | + type: 'object', |
| 28 | + properties: { |
| 29 | + path: { type: 'string', description: 'Absolute path to the file' }, |
| 30 | + }, |
| 31 | + required: ['path'], |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + type: 'function', |
| 37 | + function: { |
| 38 | + name: 'list_directory', |
| 39 | + description: 'List files and folders in a directory.', |
| 40 | + parameters: { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + path: { type: 'string', description: 'Directory path to list' }, |
| 44 | + }, |
| 45 | + required: ['path'], |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + type: 'function', |
| 51 | + function: { |
| 52 | + name: 'get_directory_tree', |
| 53 | + description: 'Get the full directory tree structure.', |
| 54 | + parameters: { |
| 55 | + type: 'object', |
| 56 | + properties: { |
| 57 | + path: { type: 'string', description: 'Root directory path' }, |
| 58 | + depth: { type: 'number', description: 'Maximum depth (default 3)' }, |
| 59 | + }, |
| 60 | + required: ['path'], |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + type: 'function', |
| 66 | + function: { |
| 67 | + name: 'search_code', |
| 68 | + description: 'Search for text patterns in source files.', |
| 69 | + parameters: { |
| 70 | + type: 'object', |
| 71 | + properties: { |
| 72 | + path: { type: 'string', description: 'Root path to search in' }, |
| 73 | + pattern: { type: 'string', description: 'Text pattern to find' }, |
| 74 | + filePattern: { type: 'string', description: 'Optional file glob pattern (e.g. "*.cpp")' }, |
| 75 | + }, |
| 76 | + required: ['path', 'pattern'], |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + ]; |
| 81 | +} |
| 82 | + |
| 83 | +export async function executePlanTool(callId: string, name: string, args: Record<string, unknown>): Promise<ToolResult> { |
| 84 | + try { |
| 85 | + switch (name) { |
| 86 | + case 'web_search': { |
| 87 | + const results = await invoke<Array<{ title: string; url: string; snippet: string }>>('web_search', { |
| 88 | + query: args.query as string, |
| 89 | + }); |
| 90 | + const output = results.map((r, i) => `${i + 1}. **${r.title}**\n URL: ${r.url}\n ${r.snippet}`).join('\n\n'); |
| 91 | + return { callId, toolCallId: callId, success: true, output: output || 'No search results found.' }; |
| 92 | + } |
| 93 | + |
| 94 | + case 'read_file': { |
| 95 | + const root = useFileStore.getState().rootPath; |
| 96 | + const content = await invoke<string>('read_file', { path: args.path as string, root }); |
| 97 | + return { callId, toolCallId: callId, success: true, output: content }; |
| 98 | + } |
| 99 | + |
| 100 | + case 'list_directory': { |
| 101 | + const root = useFileStore.getState().rootPath; |
| 102 | + const entries = await invoke<Array<{ name: string; path: string; is_dir: boolean; is_file: boolean }>>('list_directory', { path: args.path as string, root }); |
| 103 | + const output = entries.map(e => `${e.is_dir ? '[DIR]' : '[FILE]'} ${e.name}`).join('\n'); |
| 104 | + return { callId, toolCallId: callId, success: true, output: output || 'Directory is empty.' }; |
| 105 | + } |
| 106 | + |
| 107 | + case 'get_directory_tree': { |
| 108 | + const root = useFileStore.getState().rootPath; |
| 109 | + const tree = await invoke<{ name: string; path: string; is_dir: boolean; children: unknown[] }>('get_directory_tree', { |
| 110 | + path: args.path as string, |
| 111 | + depth: (args.depth as number) || 3, |
| 112 | + root, |
| 113 | + }); |
| 114 | + const formatTree = (node: typeof tree, indent = 0): string => { |
| 115 | + const prefix = ' '.repeat(indent); |
| 116 | + let result = `${prefix}${node.is_dir ? '[DIR]' : '[FILE]'} ${node.name}\n`; |
| 117 | + if (node.children && Array.isArray(node.children)) { |
| 118 | + for (const child of node.children) { |
| 119 | + result += formatTree(child as typeof tree, indent + 1); |
| 120 | + } |
| 121 | + } |
| 122 | + return result; |
| 123 | + }; |
| 124 | + return { callId, toolCallId: callId, success: true, output: formatTree(tree) }; |
| 125 | + } |
| 126 | + |
| 127 | + case 'search_code': { |
| 128 | + const results = await invoke<Array<{ path: string; line_number: number; content: string }>>('grep_search', { |
| 129 | + root_path: args.path as string, |
| 130 | + pattern: args.pattern as string, |
| 131 | + file_pattern: args.filePattern as string | null, |
| 132 | + max_results: 50, |
| 133 | + }); |
| 134 | + const output = results.map(r => `${r.path}:${r.line_number}: ${r.content}`).join('\n'); |
| 135 | + return { callId, toolCallId: callId, success: true, output: output || 'No matches found.' }; |
| 136 | + } |
| 137 | + |
| 138 | + default: |
| 139 | + return { callId, toolCallId: callId, success: false, output: `Unknown tool: ${name}` }; |
| 140 | + } |
| 141 | + } catch (err) { |
| 142 | + const msg = err instanceof Error ? err.message : String(err); |
| 143 | + return { callId, toolCallId: callId, success: false, output: `Error: ${msg}` }; |
| 144 | + } |
| 145 | +} |
0 commit comments