Skip to content

Commit f481070

Browse files
refactor(utils): create helper to define tool parameters
1 parent 0542d3d commit f481070

1 file changed

Lines changed: 81 additions & 108 deletions

File tree

src/utils/tools.ts

Lines changed: 81 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,138 +5,111 @@ import { promisify } from 'node:util';
55

66
const execAsync = promisify(exec);
77

8-
// Tool definitions for Ollama API
9-
export const TOOLS = [
10-
{
8+
/**
9+
* Helper to define tool parameters
10+
*/
11+
function defineTool(
12+
name: string,
13+
description: string,
14+
params: Record<string, { type: string; description: string }>,
15+
required: string[],
16+
) {
17+
return {
1118
type: 'function' as const,
1219
function: {
13-
name: 'read_file',
14-
description: 'Read the contents of a file at the specified path',
20+
name,
21+
description,
1522
parameters: {
1623
type: 'object',
17-
properties: {
18-
path: {
19-
type: 'string',
20-
description: 'The path to the file to read',
21-
},
22-
},
23-
required: ['path'],
24+
properties: params,
25+
required,
2426
},
2527
},
26-
},
28+
};
29+
}
2730

28-
{
29-
type: 'function' as const,
30-
function: {
31-
name: 'write_file',
32-
description: 'Write content to a file at the specified path',
33-
parameters: {
34-
type: 'object',
35-
properties: {
36-
path: {
37-
type: 'string',
38-
description: 'The path to the file to write',
39-
},
40-
content: {
41-
type: 'string',
42-
description: 'The content to write to the file',
43-
},
44-
},
45-
required: ['path', 'content'],
46-
},
31+
/**
32+
* Tool definitions for Ollama API
33+
*/
34+
export const TOOLS = [
35+
defineTool(
36+
'read_file',
37+
'Read the contents of a file at the specified path',
38+
{
39+
path: { type: 'string', description: 'The path to the file to read' },
4740
},
48-
},
41+
['path'],
42+
),
4943

50-
{
51-
type: 'function' as const,
52-
function: {
53-
name: 'run_shell',
54-
description: 'Execute a shell command',
55-
parameters: {
56-
type: 'object',
57-
properties: {
58-
command: {
59-
type: 'string',
60-
description: 'The shell command to execute',
61-
},
62-
},
63-
required: ['command'],
44+
defineTool(
45+
'write_file',
46+
'Write content to a file at the specified path',
47+
{
48+
path: { type: 'string', description: 'The path to the file to write' },
49+
content: {
50+
type: 'string',
51+
description: 'The content to write to the file',
6452
},
6553
},
66-
},
54+
['path', 'content'],
55+
),
6756

68-
{
69-
type: 'function' as const,
70-
function: {
71-
name: 'list_dir',
72-
description: 'List the contents of a directory',
73-
parameters: {
74-
type: 'object',
75-
properties: {
76-
path: {
77-
type: 'string',
78-
description: 'The path to the directory to list',
79-
},
80-
},
81-
required: ['path'],
57+
defineTool(
58+
'run_shell',
59+
'Execute a shell command',
60+
{
61+
command: { type: 'string', description: 'The shell command to execute' },
62+
},
63+
['command'],
64+
),
65+
66+
defineTool(
67+
'list_dir',
68+
'List the contents of a directory',
69+
{
70+
path: {
71+
type: 'string',
72+
description: 'The path to the directory to list',
8273
},
8374
},
84-
},
75+
['path'],
76+
),
8577

86-
{
87-
type: 'function' as const,
88-
function: {
89-
name: 'grep_search',
90-
description: 'Search for a pattern in files within a directory',
91-
parameters: {
92-
type: 'object',
93-
properties: {
94-
pattern: {
95-
type: 'string',
96-
description: 'The regex pattern to search for',
97-
},
98-
path: {
99-
type: 'string',
100-
description: 'The directory path to search in',
101-
},
102-
},
103-
required: ['pattern', 'path'],
78+
defineTool(
79+
'grep_search',
80+
'Search for a pattern in files within a directory',
81+
{
82+
pattern: {
83+
type: 'string',
84+
description: 'The regex pattern to search for',
10485
},
86+
path: { type: 'string', description: 'The directory path to search in' },
10587
},
106-
},
88+
['pattern', 'path'],
89+
),
10790

108-
{
109-
type: 'function' as const,
110-
function: {
111-
name: 'view_range',
112-
description: 'View a specific range of lines from a file',
113-
parameters: {
114-
type: 'object',
115-
properties: {
116-
path: {
117-
type: 'string',
118-
description: 'The path to the file',
119-
},
120-
start: {
121-
type: 'number',
122-
description: 'The starting line number (1-indexed)',
123-
},
124-
end: {
125-
type: 'number',
126-
description: 'The ending line number (inclusive)',
127-
},
128-
},
129-
required: ['path', 'start', 'end'],
91+
defineTool(
92+
'view_range',
93+
'View a specific range of lines from a file',
94+
{
95+
path: { type: 'string', description: 'The path to the file' },
96+
start: {
97+
type: 'number',
98+
description: 'The starting line number (1-indexed)',
99+
},
100+
end: {
101+
type: 'number',
102+
description: 'The ending line number (inclusive)',
130103
},
131104
},
132-
},
105+
['path', 'start', 'end'],
106+
),
133107
];
134108

135-
// Tools that require approval in smart mode
109+
// for smart mode
136110
export const TOOLS_REQUIRING_APPROVAL = new Set(['write_file', 'run_shell']);
137111

138-
// Tool execution result
139-
export interface ToolExecutionResult {
112+
interface ToolExecutionResult {
140113
content: string;
141114
error?: string;
142115
}

0 commit comments

Comments
 (0)