@codeany/open-agent-sdk 0.2.2 → 0.2.3

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.
Files changed (280) hide show
  1. package/dist/agent.d.ts +113 -0
  2. package/dist/agent.d.ts.map +1 -0
  3. package/dist/agent.js +446 -0
  4. package/dist/agent.js.map +1 -0
  5. package/dist/engine.d.ts +62 -0
  6. package/dist/engine.d.ts.map +1 -0
  7. package/dist/engine.js +496 -0
  8. package/dist/engine.js.map +1 -0
  9. package/dist/hooks.d.ts +111 -0
  10. package/dist/hooks.d.ts.map +1 -0
  11. package/dist/hooks.js +179 -0
  12. package/dist/hooks.js.map +1 -0
  13. package/dist/index.d.ts +66 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +133 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/mcp/client.d.ts +19 -0
  18. package/dist/mcp/client.d.ts.map +1 -0
  19. package/dist/mcp/client.js +126 -0
  20. package/dist/mcp/client.js.map +1 -0
  21. package/dist/providers/anthropic.d.ts +17 -0
  22. package/dist/providers/anthropic.d.ts.map +1 -0
  23. package/dist/providers/anthropic.js +47 -0
  24. package/dist/providers/anthropic.js.map +1 -0
  25. package/dist/providers/index.d.ts +20 -0
  26. package/dist/providers/index.d.ts.map +1 -0
  27. package/dist/providers/index.js +26 -0
  28. package/dist/providers/index.js.map +1 -0
  29. package/dist/providers/openai.d.ts +26 -0
  30. package/dist/providers/openai.d.ts.map +1 -0
  31. package/dist/providers/openai.js +212 -0
  32. package/dist/providers/openai.js.map +1 -0
  33. package/dist/providers/types.d.ts +84 -0
  34. package/dist/providers/types.d.ts.map +1 -0
  35. package/dist/providers/types.js +11 -0
  36. package/dist/providers/types.js.map +1 -0
  37. package/dist/sdk-mcp-server.d.ts +52 -0
  38. package/dist/sdk-mcp-server.d.ts.map +1 -0
  39. package/dist/sdk-mcp-server.js +57 -0
  40. package/dist/sdk-mcp-server.js.map +1 -0
  41. package/dist/session.d.ts +73 -0
  42. package/dist/session.d.ts.map +1 -0
  43. package/dist/session.js +159 -0
  44. package/dist/session.js.map +1 -0
  45. package/dist/skills/bundled/commit.d.ts +7 -0
  46. package/dist/skills/bundled/commit.d.ts.map +1 -0
  47. package/dist/skills/bundled/commit.js +35 -0
  48. package/dist/skills/bundled/commit.js.map +1 -0
  49. package/dist/skills/bundled/debug.d.ts +7 -0
  50. package/dist/skills/bundled/debug.d.ts.map +1 -0
  51. package/{src/skills/bundled/debug.ts → dist/skills/bundled/debug.js} +20 -22
  52. package/dist/skills/bundled/debug.js.map +1 -0
  53. package/dist/skills/bundled/index.d.ts +11 -0
  54. package/dist/skills/bundled/index.d.ts.map +1 -0
  55. package/dist/skills/bundled/index.js +26 -0
  56. package/dist/skills/bundled/index.js.map +1 -0
  57. package/dist/skills/bundled/review.d.ts +7 -0
  58. package/dist/skills/bundled/review.d.ts.map +1 -0
  59. package/{src/skills/bundled/review.ts → dist/skills/bundled/review.js} +18 -21
  60. package/dist/skills/bundled/review.js.map +1 -0
  61. package/dist/skills/bundled/simplify.d.ts +8 -0
  62. package/dist/skills/bundled/simplify.d.ts.map +1 -0
  63. package/{src/skills/bundled/simplify.ts → dist/skills/bundled/simplify.js} +16 -19
  64. package/dist/skills/bundled/simplify.js.map +1 -0
  65. package/dist/skills/bundled/test.d.ts +7 -0
  66. package/dist/skills/bundled/test.d.ts.map +1 -0
  67. package/{src/skills/bundled/test.ts → dist/skills/bundled/test.js} +18 -21
  68. package/dist/skills/bundled/test.js.map +1 -0
  69. package/dist/skills/index.d.ts +7 -0
  70. package/dist/skills/index.d.ts.map +1 -0
  71. package/dist/skills/index.js +8 -0
  72. package/dist/skills/index.js.map +1 -0
  73. package/dist/skills/registry.d.ts +43 -0
  74. package/dist/skills/registry.d.ts.map +1 -0
  75. package/dist/skills/registry.js +111 -0
  76. package/dist/skills/registry.js.map +1 -0
  77. package/dist/skills/types.d.ts +83 -0
  78. package/dist/skills/types.d.ts.map +1 -0
  79. package/dist/skills/types.js +8 -0
  80. package/dist/skills/types.js.map +1 -0
  81. package/dist/tool-helper.d.ts +73 -0
  82. package/dist/tool-helper.d.ts.map +1 -0
  83. package/dist/tool-helper.js +86 -0
  84. package/dist/tool-helper.js.map +1 -0
  85. package/dist/tools/agent-tool.d.ts +17 -0
  86. package/dist/tools/agent-tool.d.ts.map +1 -0
  87. package/dist/tools/agent-tool.js +146 -0
  88. package/dist/tools/agent-tool.js.map +1 -0
  89. package/dist/tools/ask-user.d.ts +18 -0
  90. package/dist/tools/ask-user.d.ts.map +1 -0
  91. package/dist/tools/ask-user.js +72 -0
  92. package/dist/tools/ask-user.js.map +1 -0
  93. package/dist/tools/bash.d.ts +5 -0
  94. package/dist/tools/bash.d.ts.map +1 -0
  95. package/dist/tools/bash.js +67 -0
  96. package/dist/tools/bash.js.map +1 -0
  97. package/dist/tools/config-tool.d.ts +20 -0
  98. package/dist/tools/config-tool.d.ts.map +1 -0
  99. package/dist/tools/config-tool.js +83 -0
  100. package/dist/tools/config-tool.js.map +1 -0
  101. package/dist/tools/cron-tools.d.ts +33 -0
  102. package/dist/tools/cron-tools.d.ts.map +1 -0
  103. package/dist/tools/cron-tools.js +128 -0
  104. package/dist/tools/cron-tools.js.map +1 -0
  105. package/dist/tools/edit.d.ts +5 -0
  106. package/dist/tools/edit.d.ts.map +1 -0
  107. package/dist/tools/edit.js +70 -0
  108. package/dist/tools/edit.js.map +1 -0
  109. package/dist/tools/glob.d.ts +5 -0
  110. package/dist/tools/glob.d.ts.map +1 -0
  111. package/dist/tools/glob.js +75 -0
  112. package/dist/tools/glob.js.map +1 -0
  113. package/dist/tools/grep.d.ts +5 -0
  114. package/dist/tools/grep.d.ts.map +1 -0
  115. package/dist/tools/grep.js +168 -0
  116. package/dist/tools/grep.js.map +1 -0
  117. package/dist/tools/index.d.ts +45 -0
  118. package/dist/tools/index.d.ts.map +1 -0
  119. package/dist/tools/index.js +162 -0
  120. package/dist/tools/index.js.map +1 -0
  121. package/dist/tools/lsp-tool.d.ts +9 -0
  122. package/dist/tools/lsp-tool.d.ts.map +1 -0
  123. package/dist/tools/lsp-tool.js +137 -0
  124. package/dist/tools/lsp-tool.js.map +1 -0
  125. package/dist/tools/mcp-resource-tools.d.ts +14 -0
  126. package/dist/tools/mcp-resource-tools.d.ts.map +1 -0
  127. package/dist/tools/mcp-resource-tools.js +117 -0
  128. package/dist/tools/mcp-resource-tools.js.map +1 -0
  129. package/dist/tools/notebook-edit.d.ts +5 -0
  130. package/dist/tools/notebook-edit.d.ts.map +1 -0
  131. package/dist/tools/notebook-edit.js +85 -0
  132. package/dist/tools/notebook-edit.js.map +1 -0
  133. package/dist/tools/plan-tools.d.ts +12 -0
  134. package/dist/tools/plan-tools.d.ts.map +1 -0
  135. package/dist/tools/plan-tools.js +77 -0
  136. package/dist/tools/plan-tools.js.map +1 -0
  137. package/dist/tools/read.d.ts +5 -0
  138. package/dist/tools/read.d.ts.map +1 -0
  139. package/dist/tools/read.js +66 -0
  140. package/dist/tools/read.js.map +1 -0
  141. package/dist/tools/send-message.d.ts +31 -0
  142. package/dist/tools/send-message.d.ts.map +1 -0
  143. package/dist/tools/send-message.js +77 -0
  144. package/dist/tools/send-message.js.map +1 -0
  145. package/dist/tools/skill-tool.d.ts +9 -0
  146. package/dist/tools/skill-tool.d.ts.map +1 -0
  147. package/dist/tools/skill-tool.js +115 -0
  148. package/dist/tools/skill-tool.js.map +1 -0
  149. package/dist/tools/task-tools.d.ts +48 -0
  150. package/dist/tools/task-tools.d.ts.map +1 -0
  151. package/dist/tools/task-tools.js +242 -0
  152. package/dist/tools/task-tools.js.map +1 -0
  153. package/dist/tools/team-tools.d.ts +34 -0
  154. package/dist/tools/team-tools.d.ts.map +1 -0
  155. package/dist/tools/team-tools.js +103 -0
  156. package/dist/tools/team-tools.js.map +1 -0
  157. package/dist/tools/todo-tool.d.ts +22 -0
  158. package/dist/tools/todo-tool.d.ts.map +1 -0
  159. package/dist/tools/todo-tool.js +93 -0
  160. package/dist/tools/todo-tool.js.map +1 -0
  161. package/dist/tools/tool-search.d.ts +13 -0
  162. package/dist/tools/tool-search.d.ts.map +1 -0
  163. package/dist/tools/tool-search.js +76 -0
  164. package/dist/tools/tool-search.js.map +1 -0
  165. package/dist/tools/types.d.ts +29 -0
  166. package/dist/tools/types.d.ts.map +1 -0
  167. package/dist/tools/types.js +52 -0
  168. package/dist/tools/types.js.map +1 -0
  169. package/dist/tools/web-fetch.d.ts +5 -0
  170. package/dist/tools/web-fetch.d.ts.map +1 -0
  171. package/dist/tools/web-fetch.js +60 -0
  172. package/dist/tools/web-fetch.js.map +1 -0
  173. package/dist/tools/web-search.d.ts +5 -0
  174. package/dist/tools/web-search.d.ts.map +1 -0
  175. package/dist/tools/web-search.js +77 -0
  176. package/dist/tools/web-search.js.map +1 -0
  177. package/dist/tools/worktree-tools.d.ts +10 -0
  178. package/dist/tools/worktree-tools.d.ts.map +1 -0
  179. package/dist/tools/worktree-tools.js +130 -0
  180. package/dist/tools/worktree-tools.js.map +1 -0
  181. package/dist/tools/write.d.ts +5 -0
  182. package/dist/tools/write.d.ts.map +1 -0
  183. package/dist/tools/write.js +40 -0
  184. package/dist/tools/write.js.map +1 -0
  185. package/dist/types.d.ts +423 -0
  186. package/dist/types.d.ts.map +1 -0
  187. package/dist/types.js +5 -0
  188. package/dist/types.js.map +1 -0
  189. package/dist/utils/compact.d.ts +44 -0
  190. package/dist/utils/compact.d.ts.map +1 -0
  191. package/dist/utils/compact.js +162 -0
  192. package/dist/utils/compact.js.map +1 -0
  193. package/dist/utils/context.d.ts +35 -0
  194. package/dist/utils/context.d.ts.map +1 -0
  195. package/dist/utils/context.js +174 -0
  196. package/dist/utils/context.js.map +1 -0
  197. package/dist/utils/fileCache.d.ts +64 -0
  198. package/dist/utils/fileCache.d.ts.map +1 -0
  199. package/dist/utils/fileCache.js +117 -0
  200. package/dist/utils/fileCache.js.map +1 -0
  201. package/dist/utils/messages.d.ts +57 -0
  202. package/dist/utils/messages.d.ts.map +1 -0
  203. package/dist/utils/messages.js +152 -0
  204. package/dist/utils/messages.js.map +1 -0
  205. package/dist/utils/retry.d.ts +48 -0
  206. package/dist/utils/retry.d.ts.map +1 -0
  207. package/dist/utils/retry.js +111 -0
  208. package/dist/utils/retry.js.map +1 -0
  209. package/dist/utils/tokens.d.ts +57 -0
  210. package/dist/utils/tokens.d.ts.map +1 -0
  211. package/dist/utils/tokens.js +134 -0
  212. package/dist/utils/tokens.js.map +1 -0
  213. package/package.json +6 -1
  214. package/.env.example +0 -8
  215. package/examples/01-simple-query.ts +0 -43
  216. package/examples/02-multi-tool.ts +0 -44
  217. package/examples/03-multi-turn.ts +0 -39
  218. package/examples/04-prompt-api.ts +0 -29
  219. package/examples/05-custom-system-prompt.ts +0 -26
  220. package/examples/06-mcp-server.ts +0 -49
  221. package/examples/07-custom-tools.ts +0 -87
  222. package/examples/08-official-api-compat.ts +0 -38
  223. package/examples/09-subagents.ts +0 -48
  224. package/examples/10-permissions.ts +0 -40
  225. package/examples/11-custom-mcp-tools.ts +0 -101
  226. package/examples/12-skills.ts +0 -88
  227. package/examples/13-hooks.ts +0 -88
  228. package/examples/14-openai-compat.ts +0 -71
  229. package/examples/web/index.html +0 -365
  230. package/examples/web/server.ts +0 -157
  231. package/src/agent.ts +0 -516
  232. package/src/engine.ts +0 -633
  233. package/src/hooks.ts +0 -261
  234. package/src/index.ts +0 -426
  235. package/src/mcp/client.ts +0 -150
  236. package/src/providers/anthropic.ts +0 -60
  237. package/src/providers/index.ts +0 -34
  238. package/src/providers/openai.ts +0 -315
  239. package/src/providers/types.ts +0 -85
  240. package/src/sdk-mcp-server.ts +0 -78
  241. package/src/session.ts +0 -227
  242. package/src/skills/bundled/commit.ts +0 -38
  243. package/src/skills/bundled/index.ts +0 -28
  244. package/src/skills/index.ts +0 -25
  245. package/src/skills/registry.ts +0 -133
  246. package/src/skills/types.ts +0 -99
  247. package/src/tool-helper.ts +0 -127
  248. package/src/tools/agent-tool.ts +0 -164
  249. package/src/tools/ask-user.ts +0 -79
  250. package/src/tools/bash.ts +0 -75
  251. package/src/tools/config-tool.ts +0 -89
  252. package/src/tools/cron-tools.ts +0 -153
  253. package/src/tools/edit.ts +0 -74
  254. package/src/tools/glob.ts +0 -77
  255. package/src/tools/grep.ts +0 -168
  256. package/src/tools/index.ts +0 -240
  257. package/src/tools/lsp-tool.ts +0 -163
  258. package/src/tools/mcp-resource-tools.ts +0 -125
  259. package/src/tools/notebook-edit.ts +0 -93
  260. package/src/tools/plan-tools.ts +0 -88
  261. package/src/tools/read.ts +0 -73
  262. package/src/tools/send-message.ts +0 -96
  263. package/src/tools/skill-tool.ts +0 -133
  264. package/src/tools/task-tools.ts +0 -290
  265. package/src/tools/team-tools.ts +0 -128
  266. package/src/tools/todo-tool.ts +0 -112
  267. package/src/tools/tool-search.ts +0 -87
  268. package/src/tools/types.ts +0 -66
  269. package/src/tools/web-fetch.ts +0 -66
  270. package/src/tools/web-search.ts +0 -86
  271. package/src/tools/worktree-tools.ts +0 -140
  272. package/src/tools/write.ts +0 -42
  273. package/src/types.ts +0 -486
  274. package/src/utils/compact.ts +0 -207
  275. package/src/utils/context.ts +0 -191
  276. package/src/utils/fileCache.ts +0 -148
  277. package/src/utils/messages.ts +0 -195
  278. package/src/utils/retry.ts +0 -140
  279. package/src/utils/tokens.ts +0 -145
  280. package/tsconfig.json +0 -19
@@ -1,87 +0,0 @@
1
- /**
2
- * ToolSearchTool - Discover deferred/lazy-loaded tools
3
- *
4
- * Allows the model to search for tools that haven't been loaded yet.
5
- * Supports keyword search and exact name selection.
6
- */
7
-
8
- import type { ToolDefinition, ToolResult } from '../types.js'
9
-
10
- // Registry of deferred tools (set by the agent)
11
- let deferredTools: ToolDefinition[] = []
12
-
13
- /**
14
- * Set deferred tools available for search.
15
- */
16
- export function setDeferredTools(tools: ToolDefinition[]): void {
17
- deferredTools = tools
18
- }
19
-
20
- export const ToolSearchTool: ToolDefinition = {
21
- name: 'ToolSearch',
22
- description: 'Search for additional tools that may be available but not yet loaded. Use keyword search or exact name selection.',
23
- inputSchema: {
24
- type: 'object',
25
- properties: {
26
- query: {
27
- type: 'string',
28
- description: 'Search query. Use "select:ToolName" for exact match or keywords for search.',
29
- },
30
- max_results: {
31
- type: 'number',
32
- description: 'Maximum results to return (default: 5)',
33
- },
34
- },
35
- required: ['query'],
36
- },
37
- isReadOnly: () => true,
38
- isConcurrencySafe: () => true,
39
- isEnabled: () => true,
40
- async prompt() { return 'Search for available tools.' },
41
- async call(input: any): Promise<ToolResult> {
42
- const { query, max_results = 5 } = input
43
-
44
- if (deferredTools.length === 0) {
45
- return {
46
- type: 'tool_result',
47
- tool_use_id: '',
48
- content: 'No deferred tools available.',
49
- }
50
- }
51
-
52
- let matches: ToolDefinition[]
53
-
54
- if (query.startsWith('select:')) {
55
- // Exact name selection
56
- const names = query.slice(7).split(',').map((n: string) => n.trim())
57
- matches = deferredTools.filter(t => names.includes(t.name))
58
- } else {
59
- // Keyword search
60
- const keywords: string[] = query.toLowerCase().split(/\s+/)
61
- matches = deferredTools
62
- .filter(t => {
63
- const searchText = `${t.name} ${t.description}`.toLowerCase()
64
- return keywords.some((kw: string) => searchText.includes(kw))
65
- })
66
- .slice(0, max_results)
67
- }
68
-
69
- if (matches.length === 0) {
70
- return {
71
- type: 'tool_result',
72
- tool_use_id: '',
73
- content: `No tools found matching "${query}"`,
74
- }
75
- }
76
-
77
- const lines = matches.map(t =>
78
- `- ${t.name}: ${t.description.slice(0, 200)}`
79
- )
80
-
81
- return {
82
- type: 'tool_result',
83
- tool_use_id: '',
84
- content: `Found ${matches.length} tool(s):\n${lines.join('\n')}`,
85
- }
86
- },
87
- }
@@ -1,66 +0,0 @@
1
- /**
2
- * Tool interface and helper utilities
3
- */
4
-
5
- import type { ToolDefinition, ToolInputSchema, ToolContext, ToolResult } from '../types.js'
6
-
7
- /**
8
- * Helper to create a tool definition with sensible defaults.
9
- */
10
- export function defineTool(config: {
11
- name: string
12
- description: string
13
- inputSchema: ToolInputSchema
14
- call: (input: any, context: ToolContext) => Promise<string | { data: string; is_error?: boolean }>
15
- isReadOnly?: boolean
16
- isConcurrencySafe?: boolean
17
- prompt?: string | ((context: ToolContext) => Promise<string>)
18
- }): ToolDefinition {
19
- return {
20
- name: config.name,
21
- description: config.description,
22
- inputSchema: config.inputSchema,
23
- isReadOnly: () => config.isReadOnly ?? false,
24
- isConcurrencySafe: () => config.isConcurrencySafe ?? false,
25
- isEnabled: () => true,
26
- prompt: typeof config.prompt === 'function'
27
- ? config.prompt
28
- : async (_context: ToolContext) => (config.prompt as string) ?? config.description,
29
- async call(input: any, context: ToolContext): Promise<ToolResult> {
30
- try {
31
- const result = await config.call(input, context)
32
- const output = typeof result === 'string' ? result : result.data
33
- const isError = typeof result === 'object' && result.is_error
34
- return {
35
- type: 'tool_result',
36
- tool_use_id: '', // filled by engine
37
- content: output,
38
- is_error: isError || false,
39
- }
40
- } catch (err: any) {
41
- return {
42
- type: 'tool_result',
43
- tool_use_id: '',
44
- content: `Error: ${err.message}`,
45
- is_error: true,
46
- }
47
- }
48
- },
49
- }
50
- }
51
-
52
- /**
53
- * Convert a ToolDefinition to API-compatible tool format.
54
- * Returns the normalized tool format used by providers.
55
- */
56
- export function toApiTool(tool: ToolDefinition): {
57
- name: string
58
- description: string
59
- input_schema: ToolInputSchema
60
- } {
61
- return {
62
- name: tool.name,
63
- description: tool.description,
64
- input_schema: tool.inputSchema,
65
- }
66
- }
@@ -1,66 +0,0 @@
1
- /**
2
- * WebFetchTool - Fetch web content
3
- */
4
-
5
- import { defineTool } from './types.js'
6
-
7
- export const WebFetchTool = defineTool({
8
- name: 'WebFetch',
9
- description: 'Fetch content from a URL and return it as text. Supports HTML pages, JSON APIs, and plain text. Strips HTML tags for readability.',
10
- inputSchema: {
11
- type: 'object',
12
- properties: {
13
- url: {
14
- type: 'string',
15
- description: 'The URL to fetch content from',
16
- },
17
- headers: {
18
- type: 'object',
19
- description: 'Optional HTTP headers',
20
- },
21
- },
22
- required: ['url'],
23
- },
24
- isReadOnly: true,
25
- isConcurrencySafe: true,
26
- async call(input, _context) {
27
- const { url, headers } = input
28
-
29
- try {
30
- const response = await fetch(url, {
31
- headers: {
32
- 'User-Agent': 'Mozilla/5.0 (compatible; AgentSDK/1.0)',
33
- ...headers,
34
- },
35
- signal: AbortSignal.timeout(30000),
36
- })
37
-
38
- if (!response.ok) {
39
- return { data: `HTTP ${response.status}: ${response.statusText}`, is_error: true }
40
- }
41
-
42
- const contentType = response.headers.get('content-type') || ''
43
- let text = await response.text()
44
-
45
- // Strip HTML tags for readability
46
- if (contentType.includes('text/html')) {
47
- // Remove script and style blocks
48
- text = text.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
49
- text = text.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
50
- // Remove HTML tags
51
- text = text.replace(/<[^>]+>/g, ' ')
52
- // Clean up whitespace
53
- text = text.replace(/\s+/g, ' ').trim()
54
- }
55
-
56
- // Truncate very large responses
57
- if (text.length > 100000) {
58
- text = text.slice(0, 100000) + '\n...(truncated)'
59
- }
60
-
61
- return text || '(empty response)'
62
- } catch (err: any) {
63
- return { data: `Error fetching ${url}: ${err.message}`, is_error: true }
64
- }
65
- },
66
- })
@@ -1,86 +0,0 @@
1
- /**
2
- * WebSearchTool - Web search (via web fetch of search engines)
3
- */
4
-
5
- import { defineTool } from './types.js'
6
-
7
- export const WebSearchTool = defineTool({
8
- name: 'WebSearch',
9
- description: 'Search the web for information. Returns search results with titles, URLs, and snippets.',
10
- inputSchema: {
11
- type: 'object',
12
- properties: {
13
- query: {
14
- type: 'string',
15
- description: 'The search query',
16
- },
17
- num_results: {
18
- type: 'number',
19
- description: 'Number of results to return (default: 5)',
20
- },
21
- },
22
- required: ['query'],
23
- },
24
- isReadOnly: true,
25
- isConcurrencySafe: true,
26
- async call(input, _context) {
27
- const { query } = input
28
-
29
- try {
30
- // Use DuckDuckGo HTML search as a free fallback
31
- const encoded = encodeURIComponent(query)
32
- const url = `https://html.duckduckgo.com/html/?q=${encoded}`
33
-
34
- const response = await fetch(url, {
35
- headers: {
36
- 'User-Agent': 'Mozilla/5.0 (compatible; AgentSDK/1.0)',
37
- },
38
- signal: AbortSignal.timeout(15000),
39
- })
40
-
41
- if (!response.ok) {
42
- return { data: `Search failed: HTTP ${response.status}`, is_error: true }
43
- }
44
-
45
- const html = await response.text()
46
-
47
- // Parse search results from DuckDuckGo HTML
48
- const results: string[] = []
49
- const resultRegex = /<a rel="nofollow" class="result__a" href="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi
50
- const snippetRegex = /<a class="result__snippet"[^>]*>([\s\S]*?)<\/a>/gi
51
-
52
- let match
53
- const links: Array<{ title: string; url: string }> = []
54
-
55
- while ((match = resultRegex.exec(html)) !== null) {
56
- const href = match[1]
57
- const title = match[2].replace(/<[^>]+>/g, '').trim()
58
- if (href && title && !href.includes('duckduckgo.com')) {
59
- links.push({ title, url: href })
60
- }
61
- }
62
-
63
- const snippets: string[] = []
64
- while ((match = snippetRegex.exec(html)) !== null) {
65
- snippets.push(match[1].replace(/<[^>]+>/g, '').trim())
66
- }
67
-
68
- const numResults = Math.min(input.num_results || 5, links.length)
69
- for (let i = 0; i < numResults; i++) {
70
- const link = links[i]
71
- if (!link) continue
72
- let entry = `${i + 1}. ${link.title}\n ${link.url}`
73
- if (snippets[i]) {
74
- entry += `\n ${snippets[i]}`
75
- }
76
- results.push(entry)
77
- }
78
-
79
- return results.length > 0
80
- ? results.join('\n\n')
81
- : `No results found for "${query}"`
82
- } catch (err: any) {
83
- return { data: `Search error: ${err.message}`, is_error: true }
84
- }
85
- },
86
- })
@@ -1,140 +0,0 @@
1
- /**
2
- * Git Worktree Tools
3
- *
4
- * EnterWorktree / ExitWorktree - Isolated git worktree environments
5
- * for parallel work without affecting the main working tree.
6
- */
7
-
8
- import { execSync } from 'child_process'
9
- import { existsSync } from 'fs'
10
- import { join } from 'path'
11
- import type { ToolDefinition, ToolResult } from '../types.js'
12
-
13
- // Track active worktrees
14
- const activeWorktrees = new Map<string, { path: string; branch: string; originalCwd: string }>()
15
-
16
- export const EnterWorktreeTool: ToolDefinition = {
17
- name: 'EnterWorktree',
18
- description: 'Create an isolated git worktree for parallel work. The agent will work in the worktree without affecting the main working tree.',
19
- inputSchema: {
20
- type: 'object',
21
- properties: {
22
- branch: { type: 'string', description: 'Branch name for the worktree (auto-generated if not provided)' },
23
- path: { type: 'string', description: 'Path for the worktree (auto-generated if not provided)' },
24
- },
25
- },
26
- isReadOnly: () => false,
27
- isConcurrencySafe: () => false,
28
- isEnabled: () => true,
29
- async prompt() { return 'Create an isolated git worktree for parallel work.' },
30
- async call(input: any, context: { cwd: string }): Promise<ToolResult> {
31
- try {
32
- // Check if we're in a git repo
33
- execSync('git rev-parse --git-dir', { cwd: context.cwd, encoding: 'utf-8' })
34
-
35
- const branch = input.branch || `worktree-${Date.now()}`
36
- const worktreePath = input.path || join(context.cwd, '..', `.worktree-${branch}`)
37
-
38
- // Create the branch if it doesn't exist
39
- try {
40
- execSync(`git branch ${branch}`, { cwd: context.cwd, encoding: 'utf-8', stdio: 'pipe' })
41
- } catch {
42
- // Branch might already exist
43
- }
44
-
45
- // Create worktree
46
- execSync(`git worktree add ${JSON.stringify(worktreePath)} ${branch}`, {
47
- cwd: context.cwd,
48
- encoding: 'utf-8',
49
- })
50
-
51
- const id = crypto.randomUUID()
52
- activeWorktrees.set(id, {
53
- path: worktreePath,
54
- branch,
55
- originalCwd: context.cwd,
56
- })
57
-
58
- return {
59
- type: 'tool_result',
60
- tool_use_id: '',
61
- content: `Worktree created:\n ID: ${id}\n Path: ${worktreePath}\n Branch: ${branch}\n\nYou are now working in the isolated worktree.`,
62
- }
63
- } catch (err: any) {
64
- return {
65
- type: 'tool_result',
66
- tool_use_id: '',
67
- content: `Error creating worktree: ${err.message}`,
68
- is_error: true,
69
- }
70
- }
71
- },
72
- }
73
-
74
- export const ExitWorktreeTool: ToolDefinition = {
75
- name: 'ExitWorktree',
76
- description: 'Exit and optionally remove a git worktree. Use "keep" to preserve changes or "remove" to clean up.',
77
- inputSchema: {
78
- type: 'object',
79
- properties: {
80
- id: { type: 'string', description: 'Worktree ID' },
81
- action: {
82
- type: 'string',
83
- enum: ['keep', 'remove'],
84
- description: 'Whether to keep or remove the worktree (default: remove)',
85
- },
86
- },
87
- required: ['id'],
88
- },
89
- isReadOnly: () => false,
90
- isConcurrencySafe: () => false,
91
- isEnabled: () => true,
92
- async prompt() { return 'Exit a git worktree.' },
93
- async call(input: any): Promise<ToolResult> {
94
- const worktree = activeWorktrees.get(input.id)
95
- if (!worktree) {
96
- return {
97
- type: 'tool_result',
98
- tool_use_id: '',
99
- content: `Worktree not found: ${input.id}`,
100
- is_error: true,
101
- }
102
- }
103
-
104
- const action = input.action || 'remove'
105
-
106
- try {
107
- if (action === 'remove') {
108
- execSync(`git worktree remove ${JSON.stringify(worktree.path)} --force`, {
109
- cwd: worktree.originalCwd,
110
- encoding: 'utf-8',
111
- })
112
- // Clean up branch
113
- try {
114
- execSync(`git branch -D ${worktree.branch}`, {
115
- cwd: worktree.originalCwd,
116
- encoding: 'utf-8',
117
- stdio: 'pipe',
118
- })
119
- } catch {
120
- // Branch might have commits
121
- }
122
- }
123
-
124
- activeWorktrees.delete(input.id)
125
-
126
- return {
127
- type: 'tool_result',
128
- tool_use_id: '',
129
- content: `Worktree ${action === 'remove' ? 'removed' : 'kept'}: ${worktree.path}`,
130
- }
131
- } catch (err: any) {
132
- return {
133
- type: 'tool_result',
134
- tool_use_id: '',
135
- content: `Error: ${err.message}`,
136
- is_error: true,
137
- }
138
- }
139
- },
140
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * FileWriteTool - Write/create files
3
- */
4
-
5
- import { writeFile, mkdir } from 'fs/promises'
6
- import { resolve, dirname } from 'path'
7
- import { defineTool } from './types.js'
8
-
9
- export const FileWriteTool = defineTool({
10
- name: 'Write',
11
- description: 'Write content to a file. Creates the file if it does not exist, or overwrites if it does. Creates parent directories as needed.',
12
- inputSchema: {
13
- type: 'object',
14
- properties: {
15
- file_path: {
16
- type: 'string',
17
- description: 'The absolute path to the file to write',
18
- },
19
- content: {
20
- type: 'string',
21
- description: 'The content to write to the file',
22
- },
23
- },
24
- required: ['file_path', 'content'],
25
- },
26
- isReadOnly: false,
27
- isConcurrencySafe: false,
28
- async call(input, context) {
29
- const filePath = resolve(context.cwd, input.file_path)
30
-
31
- try {
32
- await mkdir(dirname(filePath), { recursive: true })
33
- await writeFile(filePath, input.content, 'utf-8')
34
-
35
- const lines = input.content.split('\n').length
36
- const bytes = Buffer.byteLength(input.content, 'utf-8')
37
- return `File written: ${filePath} (${lines} lines, ${bytes} bytes)`
38
- } catch (err: any) {
39
- return { data: `Error writing file: ${err.message}`, is_error: true }
40
- }
41
- },
42
- })