@codeany/open-agent-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +8 -0
- package/LICENSE +21 -0
- package/README.md +388 -0
- package/examples/01-simple-query.ts +43 -0
- package/examples/02-multi-tool.ts +44 -0
- package/examples/03-multi-turn.ts +39 -0
- package/examples/04-prompt-api.ts +29 -0
- package/examples/05-custom-system-prompt.ts +26 -0
- package/examples/06-mcp-server.ts +49 -0
- package/examples/07-custom-tools.ts +87 -0
- package/examples/08-official-api-compat.ts +38 -0
- package/examples/09-subagents.ts +48 -0
- package/examples/10-permissions.ts +40 -0
- package/examples/11-custom-mcp-tools.ts +101 -0
- package/examples/web/index.html +365 -0
- package/examples/web/server.ts +157 -0
- package/package.json +60 -0
- package/src/agent.ts +425 -0
- package/src/engine.ts +520 -0
- package/src/hooks.ts +261 -0
- package/src/index.ts +376 -0
- package/src/mcp/client.ts +150 -0
- package/src/sdk-mcp-server.ts +78 -0
- package/src/session.ts +227 -0
- package/src/tool-helper.ts +127 -0
- package/src/tools/agent-tool.ts +153 -0
- package/src/tools/ask-user.ts +79 -0
- package/src/tools/bash.ts +75 -0
- package/src/tools/config-tool.ts +89 -0
- package/src/tools/cron-tools.ts +153 -0
- package/src/tools/edit.ts +74 -0
- package/src/tools/glob.ts +77 -0
- package/src/tools/grep.ts +168 -0
- package/src/tools/index.ts +232 -0
- package/src/tools/lsp-tool.ts +163 -0
- package/src/tools/mcp-resource-tools.ts +125 -0
- package/src/tools/notebook-edit.ts +93 -0
- package/src/tools/plan-tools.ts +88 -0
- package/src/tools/read.ts +73 -0
- package/src/tools/send-message.ts +96 -0
- package/src/tools/task-tools.ts +290 -0
- package/src/tools/team-tools.ts +128 -0
- package/src/tools/todo-tool.ts +112 -0
- package/src/tools/tool-search.ts +87 -0
- package/src/tools/types.ts +62 -0
- package/src/tools/web-fetch.ts +66 -0
- package/src/tools/web-search.ts +86 -0
- package/src/tools/worktree-tools.ts +140 -0
- package/src/tools/write.ts +42 -0
- package/src/types.ts +459 -0
- package/src/utils/compact.ts +206 -0
- package/src/utils/context.ts +191 -0
- package/src/utils/fileCache.ts +148 -0
- package/src/utils/messages.ts +196 -0
- package/src/utils/retry.ts +140 -0
- package/src/utils/tokens.ts +122 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentTool - Spawn subagents for parallel/delegated work
|
|
3
|
+
*
|
|
4
|
+
* Supports built-in agents (Explore, Plan) and custom agent definitions.
|
|
5
|
+
* Agents run as nested query loops with their own context and tool sets.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ToolDefinition, ToolContext, ToolResult, AgentDefinition } from '../types.js'
|
|
9
|
+
import { QueryEngine } from '../engine.js'
|
|
10
|
+
import { getAllBaseTools, filterTools } from './index.js'
|
|
11
|
+
import { toApiTool } from './types.js'
|
|
12
|
+
|
|
13
|
+
// Store for registered agent definitions
|
|
14
|
+
let registeredAgents: Record<string, AgentDefinition> = {}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Register agent definitions for the AgentTool to use.
|
|
18
|
+
*/
|
|
19
|
+
export function registerAgents(agents: Record<string, AgentDefinition>): void {
|
|
20
|
+
registeredAgents = { ...registeredAgents, ...agents }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Clear registered agents.
|
|
25
|
+
*/
|
|
26
|
+
export function clearAgents(): void {
|
|
27
|
+
registeredAgents = {}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Built-in agent definitions.
|
|
32
|
+
*/
|
|
33
|
+
const BUILTIN_AGENTS: Record<string, AgentDefinition> = {
|
|
34
|
+
Explore: {
|
|
35
|
+
description: 'Fast agent for exploring codebases. Use for finding files, searching code, and answering questions about the codebase.',
|
|
36
|
+
prompt: 'You are a codebase exploration agent. Search through files and code to answer questions. Be thorough but efficient. Use Glob to find files, Grep to search content, and Read to examine files.',
|
|
37
|
+
tools: ['Read', 'Glob', 'Grep', 'Bash'],
|
|
38
|
+
},
|
|
39
|
+
Plan: {
|
|
40
|
+
description: 'Software architect agent for designing implementation plans. Returns step-by-step plans and identifies critical files.',
|
|
41
|
+
prompt: 'You are a software architect. Design implementation plans for the given task. Identify critical files, consider trade-offs, and provide step-by-step plans. Use search tools to understand the codebase before planning.',
|
|
42
|
+
tools: ['Read', 'Glob', 'Grep', 'Bash'],
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const AgentTool: ToolDefinition = {
|
|
47
|
+
name: 'Agent',
|
|
48
|
+
description: 'Launch a subagent to handle complex, multi-step tasks autonomously. Subagents have their own context and can run specialized tool sets.',
|
|
49
|
+
inputSchema: {
|
|
50
|
+
type: 'object',
|
|
51
|
+
properties: {
|
|
52
|
+
prompt: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
description: 'The task for the agent to perform',
|
|
55
|
+
},
|
|
56
|
+
description: {
|
|
57
|
+
type: 'string',
|
|
58
|
+
description: 'A short (3-5 word) description of the task',
|
|
59
|
+
},
|
|
60
|
+
subagent_type: {
|
|
61
|
+
type: 'string',
|
|
62
|
+
description: 'The type of agent to use (e.g., "Explore", "Plan", or a custom agent name)',
|
|
63
|
+
},
|
|
64
|
+
model: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
description: 'Optional model override for this agent',
|
|
67
|
+
},
|
|
68
|
+
name: {
|
|
69
|
+
type: 'string',
|
|
70
|
+
description: 'Name for the spawned agent',
|
|
71
|
+
},
|
|
72
|
+
run_in_background: {
|
|
73
|
+
type: 'boolean',
|
|
74
|
+
description: 'Whether to run in background',
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
required: ['prompt', 'description'],
|
|
78
|
+
},
|
|
79
|
+
isReadOnly: () => false,
|
|
80
|
+
isConcurrencySafe: () => false,
|
|
81
|
+
isEnabled: () => true,
|
|
82
|
+
async prompt() {
|
|
83
|
+
return 'Launch a subagent to handle complex tasks autonomously.'
|
|
84
|
+
},
|
|
85
|
+
async call(input: any, context: ToolContext): Promise<ToolResult> {
|
|
86
|
+
const agentType = input.subagent_type || 'general-purpose'
|
|
87
|
+
|
|
88
|
+
// Find agent definition
|
|
89
|
+
const agentDef = registeredAgents[agentType] || BUILTIN_AGENTS[agentType]
|
|
90
|
+
|
|
91
|
+
// Determine tools for subagent
|
|
92
|
+
let tools = getAllBaseTools()
|
|
93
|
+
if (agentDef?.tools) {
|
|
94
|
+
tools = filterTools(tools, agentDef.tools)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Remove AgentTool from subagent to prevent infinite recursion
|
|
98
|
+
tools = tools.filter(t => t.name !== 'Agent')
|
|
99
|
+
|
|
100
|
+
// Build system prompt
|
|
101
|
+
const systemPrompt = agentDef?.prompt ||
|
|
102
|
+
'You are a helpful assistant. Complete the given task using the available tools.'
|
|
103
|
+
|
|
104
|
+
// Create subagent engine
|
|
105
|
+
const engine = new QueryEngine({
|
|
106
|
+
cwd: context.cwd,
|
|
107
|
+
model: input.model || process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
|
|
108
|
+
tools,
|
|
109
|
+
systemPrompt,
|
|
110
|
+
maxTurns: agentDef?.maxTurns || 10,
|
|
111
|
+
maxTokens: 16384,
|
|
112
|
+
canUseTool: async () => ({ behavior: 'allow' }),
|
|
113
|
+
includePartialMessages: false,
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// Run the subagent
|
|
117
|
+
let resultText = ''
|
|
118
|
+
let toolCalls: string[] = []
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
for await (const event of engine.submitMessage(input.prompt)) {
|
|
122
|
+
if (event.type === 'assistant') {
|
|
123
|
+
for (const block of event.message.content) {
|
|
124
|
+
if ('text' in block && block.text) {
|
|
125
|
+
resultText = block.text
|
|
126
|
+
}
|
|
127
|
+
if ('name' in block) {
|
|
128
|
+
toolCalls.push(block.name as string)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
} catch (err: any) {
|
|
134
|
+
return {
|
|
135
|
+
type: 'tool_result',
|
|
136
|
+
tool_use_id: '',
|
|
137
|
+
content: `Subagent error: ${err.message}`,
|
|
138
|
+
is_error: true,
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const output = resultText || '(Subagent completed with no text output)'
|
|
143
|
+
const toolSummary = toolCalls.length > 0
|
|
144
|
+
? `\n[Tools used: ${toolCalls.join(', ')}]`
|
|
145
|
+
: ''
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
type: 'tool_result',
|
|
149
|
+
tool_use_id: '',
|
|
150
|
+
content: output + toolSummary,
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AskUserQuestionTool - Interactive user questions
|
|
3
|
+
*
|
|
4
|
+
* In SDK mode, returns a permission_request event and waits
|
|
5
|
+
* for the consumer to provide an answer.
|
|
6
|
+
* In non-interactive mode, returns a default or denies.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ToolDefinition, ToolResult } from '../types.js'
|
|
10
|
+
|
|
11
|
+
// Callback for handling user questions (set by the agent)
|
|
12
|
+
let questionHandler: ((question: string, options?: string[]) => Promise<string>) | null = null
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Set the question handler for AskUserQuestion.
|
|
16
|
+
*/
|
|
17
|
+
export function setQuestionHandler(
|
|
18
|
+
handler: (question: string, options?: string[]) => Promise<string>,
|
|
19
|
+
): void {
|
|
20
|
+
questionHandler = handler
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Clear the question handler.
|
|
25
|
+
*/
|
|
26
|
+
export function clearQuestionHandler(): void {
|
|
27
|
+
questionHandler = null
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const AskUserQuestionTool: ToolDefinition = {
|
|
31
|
+
name: 'AskUserQuestion',
|
|
32
|
+
description: 'Ask the user a question and wait for their response. Use when you need clarification or input from the user.',
|
|
33
|
+
inputSchema: {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
question: { type: 'string', description: 'The question to ask the user' },
|
|
37
|
+
options: {
|
|
38
|
+
type: 'array',
|
|
39
|
+
items: { type: 'string' },
|
|
40
|
+
description: 'Optional list of choices for the user',
|
|
41
|
+
},
|
|
42
|
+
allow_multiselect: {
|
|
43
|
+
type: 'boolean',
|
|
44
|
+
description: 'Whether to allow multiple selections (for options)',
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
required: ['question'],
|
|
48
|
+
},
|
|
49
|
+
isReadOnly: () => true,
|
|
50
|
+
isConcurrencySafe: () => false,
|
|
51
|
+
isEnabled: () => true,
|
|
52
|
+
async prompt() { return 'Ask the user a question.' },
|
|
53
|
+
async call(input: any): Promise<ToolResult> {
|
|
54
|
+
if (questionHandler) {
|
|
55
|
+
try {
|
|
56
|
+
const answer = await questionHandler(input.question, input.options)
|
|
57
|
+
return {
|
|
58
|
+
type: 'tool_result',
|
|
59
|
+
tool_use_id: '',
|
|
60
|
+
content: answer,
|
|
61
|
+
}
|
|
62
|
+
} catch (err: any) {
|
|
63
|
+
return {
|
|
64
|
+
type: 'tool_result',
|
|
65
|
+
tool_use_id: '',
|
|
66
|
+
content: `User declined to answer: ${err.message}`,
|
|
67
|
+
is_error: true,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Non-interactive: return informative message
|
|
73
|
+
return {
|
|
74
|
+
type: 'tool_result',
|
|
75
|
+
tool_use_id: '',
|
|
76
|
+
content: `[Non-interactive mode] Question: ${input.question}${input.options ? `\nOptions: ${input.options.join(', ')}` : ''}\n\nNo user available to answer. Proceeding with best judgment.`,
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BashTool - Execute shell commands
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { spawn } from 'child_process'
|
|
6
|
+
import { defineTool } from './types.js'
|
|
7
|
+
|
|
8
|
+
export const BashTool = defineTool({
|
|
9
|
+
name: 'Bash',
|
|
10
|
+
description: 'Execute a bash command and return its output. Use for running shell commands, scripts, and system operations.',
|
|
11
|
+
inputSchema: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
command: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'The bash command to execute',
|
|
17
|
+
},
|
|
18
|
+
timeout: {
|
|
19
|
+
type: 'number',
|
|
20
|
+
description: 'Optional timeout in milliseconds (max 600000, default 120000)',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
required: ['command'],
|
|
24
|
+
},
|
|
25
|
+
isReadOnly: false,
|
|
26
|
+
isConcurrencySafe: false,
|
|
27
|
+
async call(input, context) {
|
|
28
|
+
const { command, timeout: userTimeout } = input
|
|
29
|
+
const timeoutMs = Math.min(userTimeout || 120000, 600000)
|
|
30
|
+
|
|
31
|
+
return new Promise<string>((resolve) => {
|
|
32
|
+
const chunks: Buffer[] = []
|
|
33
|
+
const errChunks: Buffer[] = []
|
|
34
|
+
|
|
35
|
+
const proc = spawn('bash', ['-c', command], {
|
|
36
|
+
cwd: context.cwd,
|
|
37
|
+
env: { ...process.env },
|
|
38
|
+
timeout: timeoutMs,
|
|
39
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
proc.stdout?.on('data', (data: Buffer) => chunks.push(data))
|
|
43
|
+
proc.stderr?.on('data', (data: Buffer) => errChunks.push(data))
|
|
44
|
+
|
|
45
|
+
if (context.abortSignal) {
|
|
46
|
+
context.abortSignal.addEventListener('abort', () => {
|
|
47
|
+
proc.kill('SIGTERM')
|
|
48
|
+
}, { once: true })
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
proc.on('close', (code) => {
|
|
52
|
+
const stdout = Buffer.concat(chunks).toString('utf-8')
|
|
53
|
+
const stderr = Buffer.concat(errChunks).toString('utf-8')
|
|
54
|
+
|
|
55
|
+
let output = ''
|
|
56
|
+
if (stdout) output += stdout
|
|
57
|
+
if (stderr) output += (output ? '\n' : '') + stderr
|
|
58
|
+
if (code !== 0 && code !== null) {
|
|
59
|
+
output += `\nExit code: ${code}`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Truncate very large outputs
|
|
63
|
+
if (output.length > 100000) {
|
|
64
|
+
output = output.slice(0, 50000) + '\n...(truncated)...\n' + output.slice(-50000)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
resolve(output || '(no output)')
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
proc.on('error', (err) => {
|
|
71
|
+
resolve(`Error executing command: ${err.message}`)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
})
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ConfigTool - Dynamic configuration management
|
|
3
|
+
*
|
|
4
|
+
* Get/set global configuration and session settings.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ToolDefinition, ToolResult } from '../types.js'
|
|
8
|
+
|
|
9
|
+
// In-memory config store
|
|
10
|
+
const configStore = new Map<string, unknown>()
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Get a config value.
|
|
14
|
+
*/
|
|
15
|
+
export function getConfig(key: string): unknown {
|
|
16
|
+
return configStore.get(key)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Set a config value.
|
|
21
|
+
*/
|
|
22
|
+
export function setConfig(key: string, value: unknown): void {
|
|
23
|
+
configStore.set(key, value)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Clear all config.
|
|
28
|
+
*/
|
|
29
|
+
export function clearConfig(): void {
|
|
30
|
+
configStore.clear()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const ConfigTool: ToolDefinition = {
|
|
34
|
+
name: 'Config',
|
|
35
|
+
description: 'Get or set configuration values. Supports session-scoped settings.',
|
|
36
|
+
inputSchema: {
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
action: {
|
|
40
|
+
type: 'string',
|
|
41
|
+
enum: ['get', 'set', 'list'],
|
|
42
|
+
description: 'Operation to perform',
|
|
43
|
+
},
|
|
44
|
+
key: { type: 'string', description: 'Config key' },
|
|
45
|
+
value: { description: 'Config value (for set)' },
|
|
46
|
+
},
|
|
47
|
+
required: ['action'],
|
|
48
|
+
},
|
|
49
|
+
isReadOnly: () => false,
|
|
50
|
+
isConcurrencySafe: () => true,
|
|
51
|
+
isEnabled: () => true,
|
|
52
|
+
async prompt() { return 'Manage configuration settings.' },
|
|
53
|
+
async call(input: any): Promise<ToolResult> {
|
|
54
|
+
switch (input.action) {
|
|
55
|
+
case 'get': {
|
|
56
|
+
if (!input.key) {
|
|
57
|
+
return { type: 'tool_result', tool_use_id: '', content: 'key required for get', is_error: true }
|
|
58
|
+
}
|
|
59
|
+
const value = configStore.get(input.key)
|
|
60
|
+
return {
|
|
61
|
+
type: 'tool_result',
|
|
62
|
+
tool_use_id: '',
|
|
63
|
+
content: value !== undefined ? JSON.stringify(value) : `Config key "${input.key}" not found`,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
case 'set': {
|
|
67
|
+
if (!input.key) {
|
|
68
|
+
return { type: 'tool_result', tool_use_id: '', content: 'key required for set', is_error: true }
|
|
69
|
+
}
|
|
70
|
+
configStore.set(input.key, input.value)
|
|
71
|
+
return {
|
|
72
|
+
type: 'tool_result',
|
|
73
|
+
tool_use_id: '',
|
|
74
|
+
content: `Config set: ${input.key} = ${JSON.stringify(input.value)}`,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
case 'list': {
|
|
78
|
+
const entries = Array.from(configStore.entries())
|
|
79
|
+
if (entries.length === 0) {
|
|
80
|
+
return { type: 'tool_result', tool_use_id: '', content: 'No config values set.' }
|
|
81
|
+
}
|
|
82
|
+
const lines = entries.map(([k, v]) => `${k} = ${JSON.stringify(v)}`)
|
|
83
|
+
return { type: 'tool_result', tool_use_id: '', content: lines.join('\n') }
|
|
84
|
+
}
|
|
85
|
+
default:
|
|
86
|
+
return { type: 'tool_result', tool_use_id: '', content: `Unknown action: ${input.action}`, is_error: true }
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cron/Scheduling Tools
|
|
3
|
+
*
|
|
4
|
+
* CronCreate, CronDelete, CronList - Schedule recurring tasks.
|
|
5
|
+
* RemoteTrigger - Manage remote scheduled agent triggers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ToolDefinition, ToolResult } from '../types.js'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Cron job definition.
|
|
12
|
+
*/
|
|
13
|
+
export interface CronJob {
|
|
14
|
+
id: string
|
|
15
|
+
name: string
|
|
16
|
+
schedule: string // cron expression
|
|
17
|
+
command: string
|
|
18
|
+
enabled: boolean
|
|
19
|
+
createdAt: string
|
|
20
|
+
lastRunAt?: string
|
|
21
|
+
nextRunAt?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// In-memory cron store
|
|
25
|
+
const cronStore = new Map<string, CronJob>()
|
|
26
|
+
let cronCounter = 0
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get all cron jobs.
|
|
30
|
+
*/
|
|
31
|
+
export function getAllCronJobs(): CronJob[] {
|
|
32
|
+
return Array.from(cronStore.values())
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Clear all cron jobs.
|
|
37
|
+
*/
|
|
38
|
+
export function clearCronJobs(): void {
|
|
39
|
+
cronStore.clear()
|
|
40
|
+
cronCounter = 0
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const CronCreateTool: ToolDefinition = {
|
|
44
|
+
name: 'CronCreate',
|
|
45
|
+
description: 'Create a scheduled recurring task (cron job). Supports cron expressions for scheduling.',
|
|
46
|
+
inputSchema: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {
|
|
49
|
+
name: { type: 'string', description: 'Job name' },
|
|
50
|
+
schedule: { type: 'string', description: 'Cron expression (e.g., "*/5 * * * *" for every 5 minutes)' },
|
|
51
|
+
command: { type: 'string', description: 'Command or prompt to execute' },
|
|
52
|
+
},
|
|
53
|
+
required: ['name', 'schedule', 'command'],
|
|
54
|
+
},
|
|
55
|
+
isReadOnly: () => false,
|
|
56
|
+
isConcurrencySafe: () => true,
|
|
57
|
+
isEnabled: () => true,
|
|
58
|
+
async prompt() { return 'Create a scheduled cron job.' },
|
|
59
|
+
async call(input: any): Promise<ToolResult> {
|
|
60
|
+
const id = `cron_${++cronCounter}`
|
|
61
|
+
const job: CronJob = {
|
|
62
|
+
id,
|
|
63
|
+
name: input.name,
|
|
64
|
+
schedule: input.schedule,
|
|
65
|
+
command: input.command,
|
|
66
|
+
enabled: true,
|
|
67
|
+
createdAt: new Date().toISOString(),
|
|
68
|
+
}
|
|
69
|
+
cronStore.set(id, job)
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
type: 'tool_result',
|
|
73
|
+
tool_use_id: '',
|
|
74
|
+
content: `Cron job created: ${id} "${job.name}" schedule="${job.schedule}"`,
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const CronDeleteTool: ToolDefinition = {
|
|
80
|
+
name: 'CronDelete',
|
|
81
|
+
description: 'Delete a scheduled cron job.',
|
|
82
|
+
inputSchema: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
id: { type: 'string', description: 'Cron job ID to delete' },
|
|
86
|
+
},
|
|
87
|
+
required: ['id'],
|
|
88
|
+
},
|
|
89
|
+
isReadOnly: () => false,
|
|
90
|
+
isConcurrencySafe: () => true,
|
|
91
|
+
isEnabled: () => true,
|
|
92
|
+
async prompt() { return 'Delete a cron job.' },
|
|
93
|
+
async call(input: any): Promise<ToolResult> {
|
|
94
|
+
if (!cronStore.has(input.id)) {
|
|
95
|
+
return { type: 'tool_result', tool_use_id: '', content: `Cron job not found: ${input.id}`, is_error: true }
|
|
96
|
+
}
|
|
97
|
+
cronStore.delete(input.id)
|
|
98
|
+
return { type: 'tool_result', tool_use_id: '', content: `Cron job deleted: ${input.id}` }
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const CronListTool: ToolDefinition = {
|
|
103
|
+
name: 'CronList',
|
|
104
|
+
description: 'List all scheduled cron jobs.',
|
|
105
|
+
inputSchema: { type: 'object', properties: {} },
|
|
106
|
+
isReadOnly: () => true,
|
|
107
|
+
isConcurrencySafe: () => true,
|
|
108
|
+
isEnabled: () => true,
|
|
109
|
+
async prompt() { return 'List cron jobs.' },
|
|
110
|
+
async call(): Promise<ToolResult> {
|
|
111
|
+
const jobs = getAllCronJobs()
|
|
112
|
+
if (jobs.length === 0) {
|
|
113
|
+
return { type: 'tool_result', tool_use_id: '', content: 'No cron jobs scheduled.' }
|
|
114
|
+
}
|
|
115
|
+
const lines = jobs.map(j =>
|
|
116
|
+
`[${j.id}] ${j.enabled ? '✓' : '✗'} "${j.name}" schedule="${j.schedule}" command="${j.command.slice(0, 50)}"`
|
|
117
|
+
)
|
|
118
|
+
return { type: 'tool_result', tool_use_id: '', content: lines.join('\n') }
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export const RemoteTriggerTool: ToolDefinition = {
|
|
123
|
+
name: 'RemoteTrigger',
|
|
124
|
+
description: 'Manage remote scheduled agent triggers. Supports list, get, create, update, and run operations.',
|
|
125
|
+
inputSchema: {
|
|
126
|
+
type: 'object',
|
|
127
|
+
properties: {
|
|
128
|
+
action: {
|
|
129
|
+
type: 'string',
|
|
130
|
+
enum: ['list', 'get', 'create', 'update', 'run'],
|
|
131
|
+
description: 'Operation to perform',
|
|
132
|
+
},
|
|
133
|
+
id: { type: 'string', description: 'Trigger ID (for get/update/run)' },
|
|
134
|
+
name: { type: 'string', description: 'Trigger name (for create)' },
|
|
135
|
+
schedule: { type: 'string', description: 'Cron schedule (for create/update)' },
|
|
136
|
+
prompt: { type: 'string', description: 'Agent prompt (for create/update)' },
|
|
137
|
+
},
|
|
138
|
+
required: ['action'],
|
|
139
|
+
},
|
|
140
|
+
isReadOnly: () => false,
|
|
141
|
+
isConcurrencySafe: () => true,
|
|
142
|
+
isEnabled: () => true,
|
|
143
|
+
async prompt() { return 'Manage remote agent triggers.' },
|
|
144
|
+
async call(input: any): Promise<ToolResult> {
|
|
145
|
+
// RemoteTrigger operations are typically handled by the remote backend
|
|
146
|
+
// In standalone SDK mode, we provide a stub implementation
|
|
147
|
+
return {
|
|
148
|
+
type: 'tool_result',
|
|
149
|
+
tool_use_id: '',
|
|
150
|
+
content: `RemoteTrigger ${input.action}: This feature requires a connected remote backend. In standalone SDK mode, use CronCreate/CronList/CronDelete for local scheduling.`,
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FileEditTool - Precise string replacement in files
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { readFile, writeFile } from 'fs/promises'
|
|
6
|
+
import { resolve } from 'path'
|
|
7
|
+
import { defineTool } from './types.js'
|
|
8
|
+
|
|
9
|
+
export const FileEditTool = defineTool({
|
|
10
|
+
name: 'Edit',
|
|
11
|
+
description: 'Perform exact string replacements in files. The old_string must match exactly (including whitespace and indentation). Use replace_all to change every occurrence.',
|
|
12
|
+
inputSchema: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
properties: {
|
|
15
|
+
file_path: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
description: 'The absolute path to the file to modify',
|
|
18
|
+
},
|
|
19
|
+
old_string: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: 'The exact text to find and replace',
|
|
22
|
+
},
|
|
23
|
+
new_string: {
|
|
24
|
+
type: 'string',
|
|
25
|
+
description: 'The replacement text',
|
|
26
|
+
},
|
|
27
|
+
replace_all: {
|
|
28
|
+
type: 'boolean',
|
|
29
|
+
description: 'Replace all occurrences (default false)',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
required: ['file_path', 'old_string', 'new_string'],
|
|
33
|
+
},
|
|
34
|
+
isReadOnly: false,
|
|
35
|
+
isConcurrencySafe: false,
|
|
36
|
+
async call(input, context) {
|
|
37
|
+
const filePath = resolve(context.cwd, input.file_path)
|
|
38
|
+
const { old_string, new_string, replace_all } = input
|
|
39
|
+
|
|
40
|
+
if (old_string === new_string) {
|
|
41
|
+
return { data: 'Error: old_string and new_string are identical', is_error: true }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
let content = await readFile(filePath, 'utf-8')
|
|
46
|
+
|
|
47
|
+
if (!content.includes(old_string)) {
|
|
48
|
+
return { data: `Error: old_string not found in ${filePath}. Make sure it matches exactly including whitespace.`, is_error: true }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!replace_all) {
|
|
52
|
+
// Check uniqueness
|
|
53
|
+
const count = content.split(old_string).length - 1
|
|
54
|
+
if (count > 1) {
|
|
55
|
+
return {
|
|
56
|
+
data: `Error: old_string appears ${count} times in the file. Provide more context to make it unique, or set replace_all: true.`,
|
|
57
|
+
is_error: true,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
content = content.replace(old_string, new_string)
|
|
61
|
+
} else {
|
|
62
|
+
content = content.split(old_string).join(new_string)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
await writeFile(filePath, content, 'utf-8')
|
|
66
|
+
return `File edited: ${filePath}`
|
|
67
|
+
} catch (err: any) {
|
|
68
|
+
if (err.code === 'ENOENT') {
|
|
69
|
+
return { data: `Error: File not found: ${filePath}`, is_error: true }
|
|
70
|
+
}
|
|
71
|
+
return { data: `Error editing file: ${err.message}`, is_error: true }
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GlobTool - File pattern matching
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { resolve } from 'path'
|
|
6
|
+
import { defineTool } from './types.js'
|
|
7
|
+
|
|
8
|
+
export const GlobTool = defineTool({
|
|
9
|
+
name: 'Glob',
|
|
10
|
+
description: 'Find files matching a glob pattern. Returns matching file paths sorted by modification time. Supports patterns like "**/*.ts", "src/**/*.js".',
|
|
11
|
+
inputSchema: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
properties: {
|
|
14
|
+
pattern: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
description: 'The glob pattern to match files against',
|
|
17
|
+
},
|
|
18
|
+
path: {
|
|
19
|
+
type: 'string',
|
|
20
|
+
description: 'The directory to search in (defaults to cwd)',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
required: ['pattern'],
|
|
24
|
+
},
|
|
25
|
+
isReadOnly: true,
|
|
26
|
+
isConcurrencySafe: true,
|
|
27
|
+
async call(input, context) {
|
|
28
|
+
const searchDir = input.path ? resolve(context.cwd, input.path) : context.cwd
|
|
29
|
+
const { pattern } = input
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
// Use Node.js glob (available in Node 22+) or fall back to bash find
|
|
33
|
+
const { glob } = await import('fs/promises')
|
|
34
|
+
|
|
35
|
+
// @ts-ignore - glob is available in Node 22+
|
|
36
|
+
if (typeof glob === 'function') {
|
|
37
|
+
const matches: string[] = []
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
for await (const entry of glob(pattern, { cwd: searchDir })) {
|
|
40
|
+
matches.push(entry)
|
|
41
|
+
if (matches.length >= 500) break
|
|
42
|
+
}
|
|
43
|
+
if (matches.length === 0) {
|
|
44
|
+
return `No files matching pattern "${pattern}" in ${searchDir}`
|
|
45
|
+
}
|
|
46
|
+
return matches.join('\n')
|
|
47
|
+
}
|
|
48
|
+
} catch {
|
|
49
|
+
// Fall through to bash-based approach
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Fallback: use bash find/glob
|
|
53
|
+
const { spawn } = await import('child_process')
|
|
54
|
+
return new Promise<string>((resolvePromise) => {
|
|
55
|
+
// Use bash glob expansion or find
|
|
56
|
+
const cmd = `shopt -s globstar nullglob 2>/dev/null; cd ${JSON.stringify(searchDir)} && ls -1d ${pattern} 2>/dev/null | head -500`
|
|
57
|
+
const proc = spawn('bash', ['-c', cmd], {
|
|
58
|
+
cwd: searchDir,
|
|
59
|
+
timeout: 30000,
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const chunks: Buffer[] = []
|
|
63
|
+
proc.stdout?.on('data', (d: Buffer) => chunks.push(d))
|
|
64
|
+
proc.on('close', () => {
|
|
65
|
+
const result = Buffer.concat(chunks).toString('utf-8').trim()
|
|
66
|
+
if (!result) {
|
|
67
|
+
resolvePromise(`No files matching pattern "${pattern}" in ${searchDir}`)
|
|
68
|
+
} else {
|
|
69
|
+
resolvePromise(result)
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
proc.on('error', () => {
|
|
73
|
+
resolvePromise(`Error searching for files with pattern "${pattern}"`)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
},
|
|
77
|
+
})
|