@codeany/open-agent-sdk 0.2.0 → 0.2.1
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/dist/agent.d.ts +113 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +446 -0
- package/dist/agent.js.map +1 -0
- package/dist/engine.d.ts +62 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +494 -0
- package/dist/engine.js.map +1 -0
- package/dist/hooks.d.ts +111 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +179 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +133 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +19 -0
- package/dist/mcp/client.d.ts.map +1 -0
- package/dist/mcp/client.js +126 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/providers/anthropic.d.ts +17 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +47 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/index.d.ts +20 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +26 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai.d.ts +26 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +212 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/types.d.ts +84 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +11 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/sdk-mcp-server.d.ts +52 -0
- package/dist/sdk-mcp-server.d.ts.map +1 -0
- package/dist/sdk-mcp-server.js +57 -0
- package/dist/sdk-mcp-server.js.map +1 -0
- package/dist/session.d.ts +73 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +159 -0
- package/dist/session.js.map +1 -0
- package/dist/skills/bundled/commit.d.ts +7 -0
- package/dist/skills/bundled/commit.d.ts.map +1 -0
- package/dist/skills/bundled/commit.js +35 -0
- package/dist/skills/bundled/commit.js.map +1 -0
- package/dist/skills/bundled/debug.d.ts +7 -0
- package/dist/skills/bundled/debug.d.ts.map +1 -0
- package/dist/skills/bundled/debug.js +46 -0
- package/dist/skills/bundled/debug.js.map +1 -0
- package/dist/skills/bundled/index.d.ts +11 -0
- package/dist/skills/bundled/index.d.ts.map +1 -0
- package/dist/skills/bundled/index.js +26 -0
- package/dist/skills/bundled/index.js.map +1 -0
- package/dist/skills/bundled/review.d.ts +7 -0
- package/dist/skills/bundled/review.d.ts.map +1 -0
- package/dist/skills/bundled/review.js +38 -0
- package/dist/skills/bundled/review.js.map +1 -0
- package/dist/skills/bundled/simplify.d.ts +8 -0
- package/dist/skills/bundled/simplify.d.ts.map +1 -0
- package/dist/skills/bundled/simplify.js +48 -0
- package/dist/skills/bundled/simplify.js.map +1 -0
- package/dist/skills/bundled/test.d.ts +7 -0
- package/dist/skills/bundled/test.d.ts.map +1 -0
- package/dist/skills/bundled/test.js +40 -0
- package/dist/skills/bundled/test.js.map +1 -0
- package/dist/skills/index.d.ts +7 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +8 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/skills/registry.d.ts +43 -0
- package/dist/skills/registry.d.ts.map +1 -0
- package/dist/skills/registry.js +111 -0
- package/dist/skills/registry.js.map +1 -0
- package/dist/skills/types.d.ts +83 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +8 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/tool-helper.d.ts +73 -0
- package/dist/tool-helper.d.ts.map +1 -0
- package/dist/tool-helper.js +86 -0
- package/dist/tool-helper.js.map +1 -0
- package/dist/tools/agent-tool.d.ts +17 -0
- package/dist/tools/agent-tool.d.ts.map +1 -0
- package/dist/tools/agent-tool.js +146 -0
- package/dist/tools/agent-tool.js.map +1 -0
- package/dist/tools/ask-user.d.ts +18 -0
- package/dist/tools/ask-user.d.ts.map +1 -0
- package/dist/tools/ask-user.js +72 -0
- package/dist/tools/ask-user.js.map +1 -0
- package/dist/tools/bash.d.ts +5 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +67 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/config-tool.d.ts +20 -0
- package/dist/tools/config-tool.d.ts.map +1 -0
- package/dist/tools/config-tool.js +83 -0
- package/dist/tools/config-tool.js.map +1 -0
- package/dist/tools/cron-tools.d.ts +33 -0
- package/dist/tools/cron-tools.d.ts.map +1 -0
- package/dist/tools/cron-tools.js +128 -0
- package/dist/tools/cron-tools.js.map +1 -0
- package/dist/tools/edit.d.ts +5 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +70 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/glob.d.ts +5 -0
- package/dist/tools/glob.d.ts.map +1 -0
- package/dist/tools/glob.js +75 -0
- package/dist/tools/glob.js.map +1 -0
- package/dist/tools/grep.d.ts +5 -0
- package/dist/tools/grep.d.ts.map +1 -0
- package/dist/tools/grep.js +168 -0
- package/dist/tools/grep.js.map +1 -0
- package/dist/tools/index.d.ts +45 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +162 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/lsp-tool.d.ts +9 -0
- package/dist/tools/lsp-tool.d.ts.map +1 -0
- package/dist/tools/lsp-tool.js +137 -0
- package/dist/tools/lsp-tool.js.map +1 -0
- package/dist/tools/mcp-resource-tools.d.ts +14 -0
- package/dist/tools/mcp-resource-tools.d.ts.map +1 -0
- package/dist/tools/mcp-resource-tools.js +117 -0
- package/dist/tools/mcp-resource-tools.js.map +1 -0
- package/dist/tools/notebook-edit.d.ts +5 -0
- package/dist/tools/notebook-edit.d.ts.map +1 -0
- package/dist/tools/notebook-edit.js +85 -0
- package/dist/tools/notebook-edit.js.map +1 -0
- package/dist/tools/plan-tools.d.ts +12 -0
- package/dist/tools/plan-tools.d.ts.map +1 -0
- package/dist/tools/plan-tools.js +77 -0
- package/dist/tools/plan-tools.js.map +1 -0
- package/dist/tools/read.d.ts +5 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +66 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/send-message.d.ts +31 -0
- package/dist/tools/send-message.d.ts.map +1 -0
- package/dist/tools/send-message.js +77 -0
- package/dist/tools/send-message.js.map +1 -0
- package/dist/tools/skill-tool.d.ts +9 -0
- package/dist/tools/skill-tool.d.ts.map +1 -0
- package/dist/tools/skill-tool.js +115 -0
- package/dist/tools/skill-tool.js.map +1 -0
- package/dist/tools/task-tools.d.ts +48 -0
- package/dist/tools/task-tools.d.ts.map +1 -0
- package/dist/tools/task-tools.js +242 -0
- package/dist/tools/task-tools.js.map +1 -0
- package/dist/tools/team-tools.d.ts +34 -0
- package/dist/tools/team-tools.d.ts.map +1 -0
- package/dist/tools/team-tools.js +103 -0
- package/dist/tools/team-tools.js.map +1 -0
- package/dist/tools/todo-tool.d.ts +22 -0
- package/dist/tools/todo-tool.d.ts.map +1 -0
- package/dist/tools/todo-tool.js +93 -0
- package/dist/tools/todo-tool.js.map +1 -0
- package/dist/tools/tool-search.d.ts +13 -0
- package/dist/tools/tool-search.d.ts.map +1 -0
- package/dist/tools/tool-search.js +76 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/types.d.ts +29 -0
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/tools/types.js +52 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/tools/web-fetch.d.ts +5 -0
- package/dist/tools/web-fetch.d.ts.map +1 -0
- package/dist/tools/web-fetch.js +60 -0
- package/dist/tools/web-fetch.js.map +1 -0
- package/dist/tools/web-search.d.ts +5 -0
- package/dist/tools/web-search.d.ts.map +1 -0
- package/dist/tools/web-search.js +77 -0
- package/dist/tools/web-search.js.map +1 -0
- package/dist/tools/worktree-tools.d.ts +10 -0
- package/dist/tools/worktree-tools.d.ts.map +1 -0
- package/dist/tools/worktree-tools.js +130 -0
- package/dist/tools/worktree-tools.js.map +1 -0
- package/dist/tools/write.d.ts +5 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +40 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/types.d.ts +423 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/compact.d.ts +44 -0
- package/dist/utils/compact.d.ts.map +1 -0
- package/dist/utils/compact.js +162 -0
- package/dist/utils/compact.js.map +1 -0
- package/dist/utils/context.d.ts +35 -0
- package/dist/utils/context.d.ts.map +1 -0
- package/dist/utils/context.js +174 -0
- package/dist/utils/context.js.map +1 -0
- package/dist/utils/fileCache.d.ts +64 -0
- package/dist/utils/fileCache.d.ts.map +1 -0
- package/dist/utils/fileCache.js +117 -0
- package/dist/utils/fileCache.js.map +1 -0
- package/dist/utils/messages.d.ts +57 -0
- package/dist/utils/messages.d.ts.map +1 -0
- package/dist/utils/messages.js +152 -0
- package/dist/utils/messages.js.map +1 -0
- package/dist/utils/retry.d.ts +48 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +111 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/tokens.d.ts +57 -0
- package/dist/utils/tokens.d.ts.map +1 -0
- package/dist/utils/tokens.js +134 -0
- package/dist/utils/tokens.js.map +1 -0
- package/package.json +7 -1
- package/.env.example +0 -8
- package/examples/01-simple-query.ts +0 -43
- package/examples/02-multi-tool.ts +0 -44
- package/examples/03-multi-turn.ts +0 -39
- package/examples/04-prompt-api.ts +0 -29
- package/examples/05-custom-system-prompt.ts +0 -26
- package/examples/06-mcp-server.ts +0 -49
- package/examples/07-custom-tools.ts +0 -87
- package/examples/08-official-api-compat.ts +0 -38
- package/examples/09-subagents.ts +0 -48
- package/examples/10-permissions.ts +0 -40
- package/examples/11-custom-mcp-tools.ts +0 -101
- package/examples/12-skills.ts +0 -88
- package/examples/13-hooks.ts +0 -88
- package/examples/14-openai-compat.ts +0 -71
- package/examples/web/index.html +0 -365
- package/examples/web/server.ts +0 -157
- package/tsconfig.json +0 -19
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 7: Custom Tools
|
|
3
|
-
*
|
|
4
|
-
* Shows how to define and use custom tools alongside built-in tools.
|
|
5
|
-
*
|
|
6
|
-
* Run: npx tsx examples/07-custom-tools.ts
|
|
7
|
-
*/
|
|
8
|
-
import { createAgent, getAllBaseTools, defineTool } from '../src/index.js'
|
|
9
|
-
|
|
10
|
-
const weatherTool = defineTool({
|
|
11
|
-
name: 'GetWeather',
|
|
12
|
-
description: 'Get current weather for a city. Returns temperature and conditions.',
|
|
13
|
-
inputSchema: {
|
|
14
|
-
type: 'object',
|
|
15
|
-
properties: {
|
|
16
|
-
city: { type: 'string', description: 'City name (e.g., "Tokyo", "London")' },
|
|
17
|
-
},
|
|
18
|
-
required: ['city'],
|
|
19
|
-
},
|
|
20
|
-
isReadOnly: true,
|
|
21
|
-
isConcurrencySafe: true,
|
|
22
|
-
async call(input) {
|
|
23
|
-
const temps: Record<string, number> = {
|
|
24
|
-
tokyo: 22, london: 14, beijing: 25, 'new york': 18, paris: 16,
|
|
25
|
-
}
|
|
26
|
-
const temp = temps[input.city?.toLowerCase()] ?? 20
|
|
27
|
-
return `Weather in ${input.city}: ${temp}°C, partly cloudy`
|
|
28
|
-
},
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
const calculatorTool = defineTool({
|
|
32
|
-
name: 'Calculator',
|
|
33
|
-
description: 'Evaluate a mathematical expression. Use ** for exponentiation.',
|
|
34
|
-
inputSchema: {
|
|
35
|
-
type: 'object',
|
|
36
|
-
properties: {
|
|
37
|
-
expression: { type: 'string', description: 'Math expression (e.g., "42 * 17 + 3", "2 ** 10")' },
|
|
38
|
-
},
|
|
39
|
-
required: ['expression'],
|
|
40
|
-
},
|
|
41
|
-
isReadOnly: true,
|
|
42
|
-
isConcurrencySafe: true,
|
|
43
|
-
async call(input) {
|
|
44
|
-
try {
|
|
45
|
-
const result = Function(`'use strict'; return (${input.expression})`)()
|
|
46
|
-
return `${input.expression} = ${result}`
|
|
47
|
-
} catch (e: any) {
|
|
48
|
-
return { data: `Error: ${e.message}`, is_error: true }
|
|
49
|
-
}
|
|
50
|
-
},
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
async function main() {
|
|
54
|
-
console.log('--- Example 7: Custom Tools ---\n')
|
|
55
|
-
|
|
56
|
-
const builtinTools = getAllBaseTools()
|
|
57
|
-
const allTools = [...builtinTools, weatherTool, calculatorTool]
|
|
58
|
-
|
|
59
|
-
const agent = createAgent({
|
|
60
|
-
model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
|
|
61
|
-
maxTurns: 10,
|
|
62
|
-
tools: allTools,
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
console.log(`Loaded ${allTools.length} tools (${builtinTools.length} built-in + 2 custom)\n`)
|
|
66
|
-
|
|
67
|
-
for await (const event of agent.query(
|
|
68
|
-
'What is the weather in Tokyo and London? Also calculate 2**10 * 3. Be brief.',
|
|
69
|
-
)) {
|
|
70
|
-
const msg = event as any
|
|
71
|
-
if (msg.type === 'assistant') {
|
|
72
|
-
for (const block of msg.message?.content || []) {
|
|
73
|
-
if (block.type === 'tool_use') {
|
|
74
|
-
console.log(`[${block.name}] ${JSON.stringify(block.input)}`)
|
|
75
|
-
}
|
|
76
|
-
if (block.type === 'text' && block.text.trim()) {
|
|
77
|
-
console.log(`\n${block.text}`)
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (msg.type === 'result') {
|
|
82
|
-
console.log(`\n--- ${msg.subtype} ---`)
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
main().catch(console.error)
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 8: Official SDK-Compatible API
|
|
3
|
-
*
|
|
4
|
-
* Demonstrates the query() function with the same API pattern
|
|
5
|
-
* as open-agent-sdk. Drop-in compatible.
|
|
6
|
-
*
|
|
7
|
-
* Run: npx tsx examples/08-official-api-compat.ts
|
|
8
|
-
*/
|
|
9
|
-
import { query } from '../src/index.js'
|
|
10
|
-
|
|
11
|
-
async function main() {
|
|
12
|
-
console.log('--- Example 8: Official SDK-Compatible API ---\n')
|
|
13
|
-
|
|
14
|
-
// Standard SDK query pattern
|
|
15
|
-
for await (const message of query({
|
|
16
|
-
prompt: 'What files are in this directory? Be brief.',
|
|
17
|
-
options: {
|
|
18
|
-
allowedTools: ['Bash', 'Glob'],
|
|
19
|
-
permissionMode: 'bypassPermissions',
|
|
20
|
-
},
|
|
21
|
-
})) {
|
|
22
|
-
const msg = message as any
|
|
23
|
-
|
|
24
|
-
if (msg.type === 'assistant' && msg.message?.content) {
|
|
25
|
-
for (const block of msg.message.content) {
|
|
26
|
-
if ('text' in block && block.text) {
|
|
27
|
-
console.log(block.text)
|
|
28
|
-
} else if ('name' in block) {
|
|
29
|
-
console.log(`Tool: ${block.name}`)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
} else if (msg.type === 'result') {
|
|
33
|
-
console.log(`\nDone: ${msg.subtype}`)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
main().catch(console.error)
|
package/examples/09-subagents.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 9: Subagents
|
|
3
|
-
*
|
|
4
|
-
* Define specialized subagents that the main agent can delegate
|
|
5
|
-
* tasks to. Matches the official SDK's agents option.
|
|
6
|
-
*
|
|
7
|
-
* Run: npx tsx examples/09-subagents.ts
|
|
8
|
-
*/
|
|
9
|
-
import { query } from '../src/index.js'
|
|
10
|
-
|
|
11
|
-
async function main() {
|
|
12
|
-
console.log('--- Example 9: Subagents ---\n')
|
|
13
|
-
|
|
14
|
-
for await (const message of query({
|
|
15
|
-
prompt: 'Use the code-reviewer agent to review src/agent.ts',
|
|
16
|
-
options: {
|
|
17
|
-
allowedTools: ['Read', 'Glob', 'Grep', 'Agent'],
|
|
18
|
-
agents: {
|
|
19
|
-
'code-reviewer': {
|
|
20
|
-
description: 'Expert code reviewer for quality and security reviews.',
|
|
21
|
-
prompt:
|
|
22
|
-
'Analyze code quality and suggest improvements. Focus on ' +
|
|
23
|
-
'security, performance, and maintainability. Be concise.',
|
|
24
|
-
tools: ['Read', 'Glob', 'Grep'],
|
|
25
|
-
},
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
})) {
|
|
29
|
-
const msg = message as any
|
|
30
|
-
|
|
31
|
-
if (msg.type === 'assistant') {
|
|
32
|
-
for (const block of msg.message?.content || []) {
|
|
33
|
-
if ('text' in block && block.text?.trim()) {
|
|
34
|
-
console.log(block.text)
|
|
35
|
-
}
|
|
36
|
-
if ('name' in block) {
|
|
37
|
-
console.log(`[${block.name}] ${JSON.stringify(block.input || {}).slice(0, 80)}`)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (msg.type === 'result') {
|
|
43
|
-
console.log(`\n--- ${msg.subtype} ---`)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
main().catch(console.error)
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 10: Permissions and Allowed Tools
|
|
3
|
-
*
|
|
4
|
-
* Shows how to restrict which tools the agent can use.
|
|
5
|
-
* Creates a read-only agent that can analyze but not modify code.
|
|
6
|
-
*
|
|
7
|
-
* Run: npx tsx examples/10-permissions.ts
|
|
8
|
-
*/
|
|
9
|
-
import { query } from '../src/index.js'
|
|
10
|
-
|
|
11
|
-
async function main() {
|
|
12
|
-
console.log('--- Example 10: Read-Only Agent ---\n')
|
|
13
|
-
|
|
14
|
-
// Read-only agent: can only use Read, Glob, Grep
|
|
15
|
-
for await (const message of query({
|
|
16
|
-
prompt: 'Review the code in src/agent.ts for best practices. Be concise.',
|
|
17
|
-
options: {
|
|
18
|
-
allowedTools: ['Read', 'Glob', 'Grep'],
|
|
19
|
-
},
|
|
20
|
-
})) {
|
|
21
|
-
const msg = message as any
|
|
22
|
-
|
|
23
|
-
if (msg.type === 'assistant') {
|
|
24
|
-
for (const block of msg.message?.content || []) {
|
|
25
|
-
if ('text' in block && block.text?.trim()) {
|
|
26
|
-
console.log(block.text)
|
|
27
|
-
}
|
|
28
|
-
if ('name' in block) {
|
|
29
|
-
console.log(`[${block.name}]`)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (msg.type === 'result') {
|
|
35
|
-
console.log(`\n--- ${msg.subtype} ---`)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
main().catch(console.error)
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 11: Custom Tools with tool() + createSdkMcpServer()
|
|
3
|
-
*
|
|
4
|
-
* Shows the Zod-based tool() helper and in-process MCP server creation.
|
|
5
|
-
* This is the recommended way to add custom tools.
|
|
6
|
-
*
|
|
7
|
-
* Run: npx tsx examples/11-custom-mcp-tools.ts
|
|
8
|
-
*/
|
|
9
|
-
import { z } from 'zod'
|
|
10
|
-
import { query, tool, createSdkMcpServer } from '../src/index.js'
|
|
11
|
-
|
|
12
|
-
// Define tools using Zod schemas for type-safe input validation
|
|
13
|
-
const getTemperature = tool(
|
|
14
|
-
'get_temperature',
|
|
15
|
-
'Get the current temperature at a location',
|
|
16
|
-
{
|
|
17
|
-
city: z.string().describe('City name'),
|
|
18
|
-
unit: z.enum(['celsius', 'fahrenheit']).default('celsius').describe('Temperature unit'),
|
|
19
|
-
},
|
|
20
|
-
async ({ city, unit }) => {
|
|
21
|
-
// Mock weather data
|
|
22
|
-
const temps: Record<string, number> = {
|
|
23
|
-
tokyo: 22, london: 14, paris: 16, 'new york': 18, beijing: 25,
|
|
24
|
-
}
|
|
25
|
-
const tempC = temps[city.toLowerCase()] ?? 20
|
|
26
|
-
const temp = unit === 'fahrenheit' ? tempC * 9 / 5 + 32 : tempC
|
|
27
|
-
const symbol = unit === 'fahrenheit' ? '°F' : '°C'
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
content: [{ type: 'text' as const, text: `Temperature in ${city}: ${temp}${symbol}` }],
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
{ annotations: { readOnlyHint: true } },
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
const convertUnits = tool(
|
|
37
|
-
'convert_units',
|
|
38
|
-
'Convert between measurement units',
|
|
39
|
-
{
|
|
40
|
-
value: z.number().describe('Value to convert'),
|
|
41
|
-
from_unit: z.string().describe('Source unit'),
|
|
42
|
-
to_unit: z.string().describe('Target unit'),
|
|
43
|
-
},
|
|
44
|
-
async ({ value, from_unit, to_unit }) => {
|
|
45
|
-
const conversions: Record<string, Record<string, (v: number) => number>> = {
|
|
46
|
-
km: { miles: (v) => v * 0.621371, m: (v) => v * 1000 },
|
|
47
|
-
miles: { km: (v) => v * 1.60934, m: (v) => v * 1609.34 },
|
|
48
|
-
kg: { lbs: (v) => v * 2.20462, g: (v) => v * 1000 },
|
|
49
|
-
lbs: { kg: (v) => v * 0.453592, g: (v) => v * 453.592 },
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const fn = conversions[from_unit]?.[to_unit]
|
|
53
|
-
if (!fn) {
|
|
54
|
-
return {
|
|
55
|
-
content: [{ type: 'text' as const, text: `Cannot convert from ${from_unit} to ${to_unit}` }],
|
|
56
|
-
isError: true,
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const result = fn(value)
|
|
61
|
-
return {
|
|
62
|
-
content: [{ type: 'text' as const, text: `${value} ${from_unit} = ${result.toFixed(2)} ${to_unit}` }],
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
// Bundle tools into an in-process MCP server
|
|
68
|
-
const utilityServer = createSdkMcpServer({
|
|
69
|
-
name: 'utilities',
|
|
70
|
-
version: '1.0.0',
|
|
71
|
-
tools: [getTemperature, convertUnits],
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
async function main() {
|
|
75
|
-
console.log('--- Example 11: Custom MCP Tools (tool + createSdkMcpServer) ---\n')
|
|
76
|
-
|
|
77
|
-
for await (const message of query({
|
|
78
|
-
prompt: 'What is the temperature in Tokyo and Paris? Also convert 10 km to miles. Be brief.',
|
|
79
|
-
options: {
|
|
80
|
-
mcpServers: { utilities: utilityServer as any },
|
|
81
|
-
allowedTools: ['mcp__utilities__*'],
|
|
82
|
-
permissionMode: 'bypassPermissions',
|
|
83
|
-
},
|
|
84
|
-
})) {
|
|
85
|
-
const msg = message as any
|
|
86
|
-
|
|
87
|
-
if (msg.type === 'assistant' && msg.message?.content) {
|
|
88
|
-
for (const block of msg.message.content) {
|
|
89
|
-
if ('text' in block && block.text?.trim()) {
|
|
90
|
-
console.log(block.text)
|
|
91
|
-
} else if ('name' in block) {
|
|
92
|
-
console.log(`[${block.name}] ${JSON.stringify(block.input || {})}`)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
} else if (msg.type === 'result') {
|
|
96
|
-
console.log(`\nDone: ${msg.subtype} (cost: $${msg.total_cost_usd?.toFixed(4) || '0'})`)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
main().catch(console.error)
|
package/examples/12-skills.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 12: Skills
|
|
3
|
-
*
|
|
4
|
-
* Shows how to use the skill system: bundled skills, custom skills,
|
|
5
|
-
* and invoking skills programmatically.
|
|
6
|
-
*
|
|
7
|
-
* Run: npx tsx examples/12-skills.ts
|
|
8
|
-
*/
|
|
9
|
-
import {
|
|
10
|
-
createAgent,
|
|
11
|
-
registerSkill,
|
|
12
|
-
getAllSkills,
|
|
13
|
-
getUserInvocableSkills,
|
|
14
|
-
getSkill,
|
|
15
|
-
initBundledSkills,
|
|
16
|
-
} from '../src/index.js'
|
|
17
|
-
import type { SkillContentBlock } from '../src/index.js'
|
|
18
|
-
|
|
19
|
-
async function main() {
|
|
20
|
-
console.log('--- Example 12: Skills ---\n')
|
|
21
|
-
|
|
22
|
-
// Bundled skills are auto-initialized when creating an Agent,
|
|
23
|
-
// but you can also init them explicitly:
|
|
24
|
-
initBundledSkills()
|
|
25
|
-
|
|
26
|
-
// List all registered skills
|
|
27
|
-
const all = getAllSkills()
|
|
28
|
-
console.log(`Registered skills (${all.length}):`)
|
|
29
|
-
for (const skill of all) {
|
|
30
|
-
console.log(` - ${skill.name}: ${skill.description.slice(0, 80)}...`)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Register a custom skill
|
|
34
|
-
registerSkill({
|
|
35
|
-
name: 'explain',
|
|
36
|
-
description: 'Explain a concept or piece of code in simple terms.',
|
|
37
|
-
aliases: ['eli5'],
|
|
38
|
-
userInvocable: true,
|
|
39
|
-
async getPrompt(args): Promise<SkillContentBlock[]> {
|
|
40
|
-
return [{
|
|
41
|
-
type: 'text',
|
|
42
|
-
text: `Explain the following in simple, clear terms that a beginner could understand. Use analogies where helpful.\n\nTopic: ${args || 'Ask the user what they want explained.'}`,
|
|
43
|
-
}]
|
|
44
|
-
},
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
console.log(`\nAfter registering custom skill: ${getAllSkills().length} total`)
|
|
48
|
-
console.log(`User-invocable: ${getUserInvocableSkills().length}`)
|
|
49
|
-
|
|
50
|
-
// Get a specific skill
|
|
51
|
-
const commitSkill = getSkill('commit')
|
|
52
|
-
if (commitSkill) {
|
|
53
|
-
const blocks = await commitSkill.getPrompt('', { cwd: process.cwd() })
|
|
54
|
-
console.log(`\nCommit skill prompt (first 200 chars):`)
|
|
55
|
-
console.log(blocks[0]?.type === 'text' ? blocks[0].text.slice(0, 200) + '...' : '(non-text)')
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// Use skills with an agent - the model can invoke them via the Skill tool
|
|
59
|
-
console.log('\n--- Using skills with an agent ---\n')
|
|
60
|
-
|
|
61
|
-
const agent = createAgent({
|
|
62
|
-
model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
|
|
63
|
-
maxTurns: 5,
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
for await (const event of agent.query(
|
|
67
|
-
'Use the "explain" skill to explain what git rebase does.',
|
|
68
|
-
)) {
|
|
69
|
-
const msg = event as any
|
|
70
|
-
if (msg.type === 'assistant') {
|
|
71
|
-
for (const block of msg.message?.content || []) {
|
|
72
|
-
if (block.type === 'tool_use') {
|
|
73
|
-
console.log(`[Tool: ${block.name}] ${JSON.stringify(block.input)}`)
|
|
74
|
-
}
|
|
75
|
-
if (block.type === 'text' && block.text.trim()) {
|
|
76
|
-
console.log(block.text)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (msg.type === 'result') {
|
|
81
|
-
console.log(`\n--- ${msg.subtype} ---`)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
await agent.close()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
main().catch(console.error)
|
package/examples/13-hooks.ts
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 13: Hooks
|
|
3
|
-
*
|
|
4
|
-
* Shows how to use lifecycle hooks to intercept agent behavior.
|
|
5
|
-
* Hooks fire at key points: session start/end, before/after tool use,
|
|
6
|
-
* compaction, etc.
|
|
7
|
-
*
|
|
8
|
-
* Run: npx tsx examples/13-hooks.ts
|
|
9
|
-
*/
|
|
10
|
-
import { createAgent, createHookRegistry } from '../src/index.js'
|
|
11
|
-
import type { HookInput } from '../src/index.js'
|
|
12
|
-
|
|
13
|
-
async function main() {
|
|
14
|
-
console.log('--- Example 13: Hooks ---\n')
|
|
15
|
-
|
|
16
|
-
// Create a hook registry with custom handlers
|
|
17
|
-
const registry = createHookRegistry({
|
|
18
|
-
SessionStart: [{
|
|
19
|
-
handler: async (input: HookInput) => {
|
|
20
|
-
console.log(`[Hook] Session started: ${input.sessionId}`)
|
|
21
|
-
},
|
|
22
|
-
}],
|
|
23
|
-
PreToolUse: [{
|
|
24
|
-
handler: async (input: HookInput) => {
|
|
25
|
-
console.log(`[Hook] About to use tool: ${input.toolName}`)
|
|
26
|
-
// You can block a tool by returning { block: true }
|
|
27
|
-
// return { block: true, message: 'Tool blocked by hook' }
|
|
28
|
-
},
|
|
29
|
-
}],
|
|
30
|
-
PostToolUse: [{
|
|
31
|
-
handler: async (input: HookInput) => {
|
|
32
|
-
const output = typeof input.toolOutput === 'string'
|
|
33
|
-
? input.toolOutput.slice(0, 100)
|
|
34
|
-
: JSON.stringify(input.toolOutput).slice(0, 100)
|
|
35
|
-
console.log(`[Hook] Tool ${input.toolName} completed: ${output}...`)
|
|
36
|
-
},
|
|
37
|
-
}],
|
|
38
|
-
PostToolUseFailure: [{
|
|
39
|
-
handler: async (input: HookInput) => {
|
|
40
|
-
console.log(`[Hook] Tool ${input.toolName} FAILED: ${input.error}`)
|
|
41
|
-
},
|
|
42
|
-
}],
|
|
43
|
-
Stop: [{
|
|
44
|
-
handler: async () => {
|
|
45
|
-
console.log('[Hook] Agent loop completed')
|
|
46
|
-
},
|
|
47
|
-
}],
|
|
48
|
-
SessionEnd: [{
|
|
49
|
-
handler: async () => {
|
|
50
|
-
console.log('[Hook] Session ended')
|
|
51
|
-
},
|
|
52
|
-
}],
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
// Create agent with hook registry
|
|
56
|
-
// Note: For direct HookRegistry usage, we pass hooks via the engine config.
|
|
57
|
-
// The AgentOptions.hooks format also works (see below).
|
|
58
|
-
const agent = createAgent({
|
|
59
|
-
model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
|
|
60
|
-
maxTurns: 5,
|
|
61
|
-
// Alternative: use AgentOptions.hooks format
|
|
62
|
-
hooks: {
|
|
63
|
-
PreToolUse: [{
|
|
64
|
-
hooks: [async (input: any, toolUseId: string) => {
|
|
65
|
-
console.log(`[AgentHook] PreToolUse: ${input.toolName} (${toolUseId})`)
|
|
66
|
-
}],
|
|
67
|
-
}],
|
|
68
|
-
},
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
for await (const event of agent.query('What files are in the current directory? Be brief.')) {
|
|
72
|
-
const msg = event as any
|
|
73
|
-
if (msg.type === 'assistant') {
|
|
74
|
-
for (const block of msg.message?.content || []) {
|
|
75
|
-
if (block.type === 'text' && block.text.trim()) {
|
|
76
|
-
console.log(`\nAssistant: ${block.text.slice(0, 200)}`)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (msg.type === 'result') {
|
|
81
|
-
console.log(`\n--- ${msg.subtype} ---`)
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
await agent.close()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
main().catch(console.error)
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Example 14: OpenAI-Compatible Models
|
|
3
|
-
*
|
|
4
|
-
* Shows how to use the SDK with OpenAI's API or any OpenAI-compatible
|
|
5
|
-
* endpoint (e.g., DeepSeek, Qwen, vLLM, Ollama).
|
|
6
|
-
*
|
|
7
|
-
* Environment variables:
|
|
8
|
-
* CODEANY_API_KEY=sk-... # Your OpenAI API key
|
|
9
|
-
* CODEANY_BASE_URL=https://api.openai.com/v1 # Optional, defaults to OpenAI
|
|
10
|
-
* CODEANY_API_TYPE=openai-completions # Optional, auto-detected from model name
|
|
11
|
-
*
|
|
12
|
-
* Run: npx tsx examples/14-openai-compat.ts
|
|
13
|
-
*/
|
|
14
|
-
import { createAgent } from '../src/index.js'
|
|
15
|
-
|
|
16
|
-
async function main() {
|
|
17
|
-
console.log('--- Example 14: OpenAI-Compatible Models ---\n')
|
|
18
|
-
|
|
19
|
-
// Option 1: Explicit apiType
|
|
20
|
-
const agent = createAgent({
|
|
21
|
-
apiType: 'openai-completions',
|
|
22
|
-
model: process.env.CODEANY_MODEL || 'gpt-4o',
|
|
23
|
-
apiKey: process.env.CODEANY_API_KEY,
|
|
24
|
-
baseURL: process.env.CODEANY_BASE_URL || 'https://api.openai.com/v1',
|
|
25
|
-
maxTurns: 5,
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
console.log(`API Type: ${agent.getApiType()}`)
|
|
29
|
-
console.log(`Model: ${process.env.CODEANY_MODEL || 'gpt-4o'}\n`)
|
|
30
|
-
|
|
31
|
-
// Option 2: Auto-detected from model name (uncomment to try)
|
|
32
|
-
// const agent = createAgent({
|
|
33
|
-
// model: 'gpt-4o', // Auto-detects 'openai-completions'
|
|
34
|
-
// apiKey: process.env.CODEANY_API_KEY,
|
|
35
|
-
// })
|
|
36
|
-
|
|
37
|
-
// Option 3: DeepSeek example (uncomment to try)
|
|
38
|
-
// const agent = createAgent({
|
|
39
|
-
// model: 'deepseek-chat',
|
|
40
|
-
// apiKey: process.env.CODEANY_API_KEY,
|
|
41
|
-
// baseURL: 'https://api.deepseek.com/v1',
|
|
42
|
-
// })
|
|
43
|
-
|
|
44
|
-
// Option 4: Via environment variables only
|
|
45
|
-
// CODEANY_API_TYPE=openai-completions
|
|
46
|
-
// CODEANY_MODEL=gpt-4o
|
|
47
|
-
// CODEANY_API_KEY=sk-...
|
|
48
|
-
// CODEANY_BASE_URL=https://api.openai.com/v1
|
|
49
|
-
// const agent = createAgent()
|
|
50
|
-
|
|
51
|
-
for await (const event of agent.query('What is 2+2? Reply in one sentence.')) {
|
|
52
|
-
const msg = event as any
|
|
53
|
-
if (msg.type === 'assistant') {
|
|
54
|
-
for (const block of msg.message?.content || []) {
|
|
55
|
-
if (block.type === 'text' && block.text.trim()) {
|
|
56
|
-
console.log(`Assistant: ${block.text}`)
|
|
57
|
-
}
|
|
58
|
-
if (block.type === 'tool_use') {
|
|
59
|
-
console.log(`[Tool: ${block.name}] ${JSON.stringify(block.input)}`)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (msg.type === 'result') {
|
|
64
|
-
console.log(`\n--- ${msg.subtype} (${msg.usage?.input_tokens}+${msg.usage?.output_tokens} tokens) ---`)
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
await agent.close()
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
main().catch(console.error)
|