@codeany/open-agent-sdk 0.1.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.
Files changed (254) hide show
  1. package/README.md +144 -31
  2. package/dist/agent.d.ts +113 -0
  3. package/dist/agent.d.ts.map +1 -0
  4. package/dist/agent.js +446 -0
  5. package/dist/agent.js.map +1 -0
  6. package/dist/engine.d.ts +62 -0
  7. package/dist/engine.d.ts.map +1 -0
  8. package/dist/engine.js +494 -0
  9. package/dist/engine.js.map +1 -0
  10. package/dist/hooks.d.ts +111 -0
  11. package/dist/hooks.d.ts.map +1 -0
  12. package/dist/hooks.js +179 -0
  13. package/dist/hooks.js.map +1 -0
  14. package/dist/index.d.ts +66 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +133 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/mcp/client.d.ts +19 -0
  19. package/dist/mcp/client.d.ts.map +1 -0
  20. package/dist/mcp/client.js +126 -0
  21. package/dist/mcp/client.js.map +1 -0
  22. package/dist/providers/anthropic.d.ts +17 -0
  23. package/dist/providers/anthropic.d.ts.map +1 -0
  24. package/dist/providers/anthropic.js +47 -0
  25. package/dist/providers/anthropic.js.map +1 -0
  26. package/dist/providers/index.d.ts +20 -0
  27. package/dist/providers/index.d.ts.map +1 -0
  28. package/dist/providers/index.js +26 -0
  29. package/dist/providers/index.js.map +1 -0
  30. package/dist/providers/openai.d.ts +26 -0
  31. package/dist/providers/openai.d.ts.map +1 -0
  32. package/dist/providers/openai.js +212 -0
  33. package/dist/providers/openai.js.map +1 -0
  34. package/dist/providers/types.d.ts +84 -0
  35. package/dist/providers/types.d.ts.map +1 -0
  36. package/dist/providers/types.js +11 -0
  37. package/dist/providers/types.js.map +1 -0
  38. package/dist/sdk-mcp-server.d.ts +52 -0
  39. package/dist/sdk-mcp-server.d.ts.map +1 -0
  40. package/dist/sdk-mcp-server.js +57 -0
  41. package/dist/sdk-mcp-server.js.map +1 -0
  42. package/dist/session.d.ts +73 -0
  43. package/dist/session.d.ts.map +1 -0
  44. package/dist/session.js +159 -0
  45. package/dist/session.js.map +1 -0
  46. package/dist/skills/bundled/commit.d.ts +7 -0
  47. package/dist/skills/bundled/commit.d.ts.map +1 -0
  48. package/dist/skills/bundled/commit.js +35 -0
  49. package/dist/skills/bundled/commit.js.map +1 -0
  50. package/dist/skills/bundled/debug.d.ts +7 -0
  51. package/dist/skills/bundled/debug.d.ts.map +1 -0
  52. package/dist/skills/bundled/debug.js +46 -0
  53. package/dist/skills/bundled/debug.js.map +1 -0
  54. package/dist/skills/bundled/index.d.ts +11 -0
  55. package/dist/skills/bundled/index.d.ts.map +1 -0
  56. package/dist/skills/bundled/index.js +26 -0
  57. package/dist/skills/bundled/index.js.map +1 -0
  58. package/dist/skills/bundled/review.d.ts +7 -0
  59. package/dist/skills/bundled/review.d.ts.map +1 -0
  60. package/dist/skills/bundled/review.js +38 -0
  61. package/dist/skills/bundled/review.js.map +1 -0
  62. package/dist/skills/bundled/simplify.d.ts +8 -0
  63. package/dist/skills/bundled/simplify.d.ts.map +1 -0
  64. package/dist/skills/bundled/simplify.js +48 -0
  65. package/dist/skills/bundled/simplify.js.map +1 -0
  66. package/dist/skills/bundled/test.d.ts +7 -0
  67. package/dist/skills/bundled/test.d.ts.map +1 -0
  68. package/dist/skills/bundled/test.js +40 -0
  69. package/dist/skills/bundled/test.js.map +1 -0
  70. package/dist/skills/index.d.ts +7 -0
  71. package/dist/skills/index.d.ts.map +1 -0
  72. package/dist/skills/index.js +8 -0
  73. package/dist/skills/index.js.map +1 -0
  74. package/dist/skills/registry.d.ts +43 -0
  75. package/dist/skills/registry.d.ts.map +1 -0
  76. package/dist/skills/registry.js +111 -0
  77. package/dist/skills/registry.js.map +1 -0
  78. package/dist/skills/types.d.ts +83 -0
  79. package/dist/skills/types.d.ts.map +1 -0
  80. package/dist/skills/types.js +8 -0
  81. package/dist/skills/types.js.map +1 -0
  82. package/dist/tool-helper.d.ts +73 -0
  83. package/dist/tool-helper.d.ts.map +1 -0
  84. package/dist/tool-helper.js +86 -0
  85. package/dist/tool-helper.js.map +1 -0
  86. package/dist/tools/agent-tool.d.ts +17 -0
  87. package/dist/tools/agent-tool.d.ts.map +1 -0
  88. package/dist/tools/agent-tool.js +146 -0
  89. package/dist/tools/agent-tool.js.map +1 -0
  90. package/dist/tools/ask-user.d.ts +18 -0
  91. package/dist/tools/ask-user.d.ts.map +1 -0
  92. package/dist/tools/ask-user.js +72 -0
  93. package/dist/tools/ask-user.js.map +1 -0
  94. package/dist/tools/bash.d.ts +5 -0
  95. package/dist/tools/bash.d.ts.map +1 -0
  96. package/dist/tools/bash.js +67 -0
  97. package/dist/tools/bash.js.map +1 -0
  98. package/dist/tools/config-tool.d.ts +20 -0
  99. package/dist/tools/config-tool.d.ts.map +1 -0
  100. package/dist/tools/config-tool.js +83 -0
  101. package/dist/tools/config-tool.js.map +1 -0
  102. package/dist/tools/cron-tools.d.ts +33 -0
  103. package/dist/tools/cron-tools.d.ts.map +1 -0
  104. package/dist/tools/cron-tools.js +128 -0
  105. package/dist/tools/cron-tools.js.map +1 -0
  106. package/dist/tools/edit.d.ts +5 -0
  107. package/dist/tools/edit.d.ts.map +1 -0
  108. package/dist/tools/edit.js +70 -0
  109. package/dist/tools/edit.js.map +1 -0
  110. package/dist/tools/glob.d.ts +5 -0
  111. package/dist/tools/glob.d.ts.map +1 -0
  112. package/dist/tools/glob.js +75 -0
  113. package/dist/tools/glob.js.map +1 -0
  114. package/dist/tools/grep.d.ts +5 -0
  115. package/dist/tools/grep.d.ts.map +1 -0
  116. package/dist/tools/grep.js +168 -0
  117. package/dist/tools/grep.js.map +1 -0
  118. package/dist/tools/index.d.ts +45 -0
  119. package/dist/tools/index.d.ts.map +1 -0
  120. package/dist/tools/index.js +162 -0
  121. package/dist/tools/index.js.map +1 -0
  122. package/dist/tools/lsp-tool.d.ts +9 -0
  123. package/dist/tools/lsp-tool.d.ts.map +1 -0
  124. package/dist/tools/lsp-tool.js +137 -0
  125. package/dist/tools/lsp-tool.js.map +1 -0
  126. package/dist/tools/mcp-resource-tools.d.ts +14 -0
  127. package/dist/tools/mcp-resource-tools.d.ts.map +1 -0
  128. package/dist/tools/mcp-resource-tools.js +117 -0
  129. package/dist/tools/mcp-resource-tools.js.map +1 -0
  130. package/dist/tools/notebook-edit.d.ts +5 -0
  131. package/dist/tools/notebook-edit.d.ts.map +1 -0
  132. package/dist/tools/notebook-edit.js +85 -0
  133. package/dist/tools/notebook-edit.js.map +1 -0
  134. package/dist/tools/plan-tools.d.ts +12 -0
  135. package/dist/tools/plan-tools.d.ts.map +1 -0
  136. package/dist/tools/plan-tools.js +77 -0
  137. package/dist/tools/plan-tools.js.map +1 -0
  138. package/dist/tools/read.d.ts +5 -0
  139. package/dist/tools/read.d.ts.map +1 -0
  140. package/dist/tools/read.js +66 -0
  141. package/dist/tools/read.js.map +1 -0
  142. package/dist/tools/send-message.d.ts +31 -0
  143. package/dist/tools/send-message.d.ts.map +1 -0
  144. package/dist/tools/send-message.js +77 -0
  145. package/dist/tools/send-message.js.map +1 -0
  146. package/dist/tools/skill-tool.d.ts +9 -0
  147. package/dist/tools/skill-tool.d.ts.map +1 -0
  148. package/dist/tools/skill-tool.js +115 -0
  149. package/dist/tools/skill-tool.js.map +1 -0
  150. package/dist/tools/task-tools.d.ts +48 -0
  151. package/dist/tools/task-tools.d.ts.map +1 -0
  152. package/dist/tools/task-tools.js +242 -0
  153. package/dist/tools/task-tools.js.map +1 -0
  154. package/dist/tools/team-tools.d.ts +34 -0
  155. package/dist/tools/team-tools.d.ts.map +1 -0
  156. package/dist/tools/team-tools.js +103 -0
  157. package/dist/tools/team-tools.js.map +1 -0
  158. package/dist/tools/todo-tool.d.ts +22 -0
  159. package/dist/tools/todo-tool.d.ts.map +1 -0
  160. package/dist/tools/todo-tool.js +93 -0
  161. package/dist/tools/todo-tool.js.map +1 -0
  162. package/dist/tools/tool-search.d.ts +13 -0
  163. package/dist/tools/tool-search.d.ts.map +1 -0
  164. package/dist/tools/tool-search.js +76 -0
  165. package/dist/tools/tool-search.js.map +1 -0
  166. package/dist/tools/types.d.ts +29 -0
  167. package/dist/tools/types.d.ts.map +1 -0
  168. package/dist/tools/types.js +52 -0
  169. package/dist/tools/types.js.map +1 -0
  170. package/dist/tools/web-fetch.d.ts +5 -0
  171. package/dist/tools/web-fetch.d.ts.map +1 -0
  172. package/dist/tools/web-fetch.js +60 -0
  173. package/dist/tools/web-fetch.js.map +1 -0
  174. package/dist/tools/web-search.d.ts +5 -0
  175. package/dist/tools/web-search.d.ts.map +1 -0
  176. package/dist/tools/web-search.js +77 -0
  177. package/dist/tools/web-search.js.map +1 -0
  178. package/dist/tools/worktree-tools.d.ts +10 -0
  179. package/dist/tools/worktree-tools.d.ts.map +1 -0
  180. package/dist/tools/worktree-tools.js +130 -0
  181. package/dist/tools/worktree-tools.js.map +1 -0
  182. package/dist/tools/write.d.ts +5 -0
  183. package/dist/tools/write.d.ts.map +1 -0
  184. package/dist/tools/write.js +40 -0
  185. package/dist/tools/write.js.map +1 -0
  186. package/dist/types.d.ts +423 -0
  187. package/dist/types.d.ts.map +1 -0
  188. package/dist/types.js +5 -0
  189. package/dist/types.js.map +1 -0
  190. package/dist/utils/compact.d.ts +44 -0
  191. package/dist/utils/compact.d.ts.map +1 -0
  192. package/dist/utils/compact.js +162 -0
  193. package/dist/utils/compact.js.map +1 -0
  194. package/dist/utils/context.d.ts +35 -0
  195. package/dist/utils/context.d.ts.map +1 -0
  196. package/dist/utils/context.js +174 -0
  197. package/dist/utils/context.js.map +1 -0
  198. package/dist/utils/fileCache.d.ts +64 -0
  199. package/dist/utils/fileCache.d.ts.map +1 -0
  200. package/dist/utils/fileCache.js +117 -0
  201. package/dist/utils/fileCache.js.map +1 -0
  202. package/dist/utils/messages.d.ts +57 -0
  203. package/dist/utils/messages.d.ts.map +1 -0
  204. package/dist/utils/messages.js +152 -0
  205. package/dist/utils/messages.js.map +1 -0
  206. package/dist/utils/retry.d.ts +48 -0
  207. package/dist/utils/retry.d.ts.map +1 -0
  208. package/dist/utils/retry.js +111 -0
  209. package/dist/utils/retry.js.map +1 -0
  210. package/dist/utils/tokens.d.ts +57 -0
  211. package/dist/utils/tokens.d.ts.map +1 -0
  212. package/dist/utils/tokens.js +134 -0
  213. package/dist/utils/tokens.js.map +1 -0
  214. package/package.json +7 -1
  215. package/src/agent.ts +106 -15
  216. package/src/engine.ts +169 -59
  217. package/src/index.ts +51 -1
  218. package/src/providers/anthropic.ts +60 -0
  219. package/src/providers/index.ts +34 -0
  220. package/src/providers/openai.ts +315 -0
  221. package/src/providers/types.ts +85 -0
  222. package/src/session.ts +5 -5
  223. package/src/skills/bundled/commit.ts +38 -0
  224. package/src/skills/bundled/debug.ts +48 -0
  225. package/src/skills/bundled/index.ts +28 -0
  226. package/src/skills/bundled/review.ts +41 -0
  227. package/src/skills/bundled/simplify.ts +51 -0
  228. package/src/skills/bundled/test.ts +43 -0
  229. package/src/skills/index.ts +25 -0
  230. package/src/skills/registry.ts +133 -0
  231. package/src/skills/types.ts +99 -0
  232. package/src/tools/agent-tool.ts +13 -2
  233. package/src/tools/index.ts +8 -0
  234. package/src/tools/skill-tool.ts +133 -0
  235. package/src/tools/types.ts +7 -3
  236. package/src/types.ts +35 -8
  237. package/src/utils/compact.ts +18 -17
  238. package/src/utils/messages.ts +12 -13
  239. package/src/utils/tokens.ts +29 -6
  240. package/.env.example +0 -8
  241. package/examples/01-simple-query.ts +0 -43
  242. package/examples/02-multi-tool.ts +0 -44
  243. package/examples/03-multi-turn.ts +0 -39
  244. package/examples/04-prompt-api.ts +0 -29
  245. package/examples/05-custom-system-prompt.ts +0 -26
  246. package/examples/06-mcp-server.ts +0 -49
  247. package/examples/07-custom-tools.ts +0 -87
  248. package/examples/08-official-api-compat.ts +0 -38
  249. package/examples/09-subagents.ts +0 -48
  250. package/examples/10-permissions.ts +0 -40
  251. package/examples/11-custom-mcp-tools.ts +0 -101
  252. package/examples/web/index.html +0 -365
  253. package/examples/web/server.ts +0 -157
  254. package/tsconfig.json +0 -19
@@ -5,14 +5,13 @@
5
5
  * synthetic placeholders, and content processing.
6
6
  */
7
7
 
8
- import type Anthropic from '@anthropic-ai/sdk'
9
8
  import type { Message, UserMessage, AssistantMessage, TokenUsage } from '../types.js'
10
9
 
11
10
  /**
12
11
  * Create a user message.
13
12
  */
14
13
  export function createUserMessage(
15
- content: string | Anthropic.ContentBlockParam[],
14
+ content: string | any[],
16
15
  options?: {
17
16
  uuid?: string
18
17
  isMeta?: boolean
@@ -34,7 +33,7 @@ export function createUserMessage(
34
33
  * Create an assistant message.
35
34
  */
36
35
  export function createAssistantMessage(
37
- content: Anthropic.ContentBlock[],
36
+ content: any[],
38
37
  usage?: TokenUsage,
39
38
  ): AssistantMessage {
40
39
  return {
@@ -55,9 +54,9 @@ export function createAssistantMessage(
55
54
  * and fixes tool result pairing.
56
55
  */
57
56
  export function normalizeMessagesForAPI(
58
- messages: Anthropic.MessageParam[],
59
- ): Anthropic.MessageParam[] {
60
- const normalized: Anthropic.MessageParam[] = []
57
+ messages: Array<{ role: string; content: any }>,
58
+ ): Array<{ role: string; content: any }> {
59
+ const normalized: Array<{ role: string; content: any }> = []
61
60
 
62
61
  for (let i = 0; i < messages.length; i++) {
63
62
  const msg = messages[i]
@@ -96,9 +95,9 @@ export function normalizeMessagesForAPI(
96
95
  * matching tool_use in the previous assistant message.
97
96
  */
98
97
  function fixToolResultPairing(
99
- messages: Anthropic.MessageParam[],
100
- ): Anthropic.MessageParam[] {
101
- const result: Anthropic.MessageParam[] = []
98
+ messages: Array<{ role: string; content: any }>,
99
+ ): Array<{ role: string; content: any }> {
100
+ const result: Array<{ role: string; content: any }> = []
102
101
 
103
102
  for (let i = 0; i < messages.length; i++) {
104
103
  const msg = messages[i]
@@ -145,8 +144,8 @@ function fixToolResultPairing(
145
144
  * Strip images from messages (for compaction).
146
145
  */
147
146
  export function stripImagesFromMessages(
148
- messages: Anthropic.MessageParam[],
149
- ): Anthropic.MessageParam[] {
147
+ messages: Array<{ role: string; content: any }>,
148
+ ): Array<{ role: string; content: any }> {
150
149
  return messages.map((msg) => {
151
150
  if (typeof msg.content === 'string') return msg
152
151
  if (!Array.isArray(msg.content)) return msg
@@ -166,7 +165,7 @@ export function stripImagesFromMessages(
166
165
  * Extract text from message content blocks.
167
166
  */
168
167
  export function extractTextFromContent(
169
- content: Anthropic.ContentBlock[] | string,
168
+ content: any[] | string,
170
169
  ): string {
171
170
  if (typeof content === 'string') return content
172
171
 
@@ -179,7 +178,7 @@ export function extractTextFromContent(
179
178
  /**
180
179
  * Create a system message for compact boundary.
181
180
  */
182
- export function createCompactBoundaryMessage(): Anthropic.MessageParam {
181
+ export function createCompactBoundaryMessage(): { role: string; content: string } {
183
182
  return {
184
183
  role: 'user',
185
184
  content: '[Previous context has been summarized above. Continuing conversation.]',
@@ -5,8 +5,6 @@
5
5
  * API-based exact counting when available.
6
6
  */
7
7
 
8
- import Anthropic from '@anthropic-ai/sdk'
9
-
10
8
  /**
11
9
  * Rough token estimation: ~4 chars per token (conservative).
12
10
  */
@@ -18,7 +16,7 @@ export function estimateTokens(text: string): number {
18
16
  * Estimate tokens for a message array.
19
17
  */
20
18
  export function estimateMessagesTokens(
21
- messages: Anthropic.MessageParam[],
19
+ messages: Array<{ role: string; content: any }>,
22
20
  ): number {
23
21
  let total = 0
24
22
  for (const msg of messages) {
@@ -68,15 +66,26 @@ export function getTokenCountFromUsage(usage: {
68
66
  * Get the context window size for a model.
69
67
  */
70
68
  export function getContextWindowSize(model: string): number {
71
- // Model context windows
69
+ // Anthropic model context windows
72
70
  if (model.includes('opus-4') && model.includes('1m')) return 1_000_000
73
71
  if (model.includes('opus-4')) return 200_000
74
72
  if (model.includes('sonnet-4')) return 200_000
75
73
  if (model.includes('haiku-4')) return 200_000
76
-
77
-
78
74
  if (model.includes('claude-3')) return 200_000
79
75
 
76
+ // OpenAI model context windows
77
+ if (model.includes('gpt-4o')) return 128_000
78
+ if (model.includes('gpt-4-turbo')) return 128_000
79
+ if (model.includes('gpt-4-1')) return 1_000_000
80
+ if (model.includes('gpt-4')) return 128_000
81
+ if (model.includes('gpt-3.5')) return 16_385
82
+ if (model.includes('o1')) return 200_000
83
+ if (model.includes('o3')) return 200_000
84
+ if (model.includes('o4')) return 200_000
85
+
86
+ // DeepSeek models
87
+ if (model.includes('deepseek')) return 128_000
88
+
80
89
  // Default
81
90
  return 200_000
82
91
  }
@@ -97,6 +106,7 @@ export function getAutoCompactThreshold(model: string): number {
97
106
  * Model pricing (USD per token).
98
107
  */
99
108
  export const MODEL_PRICING: Record<string, { input: number; output: number }> = {
109
+ // Anthropic models
100
110
  'claude-opus-4-6': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
101
111
  'claude-opus-4-5': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
102
112
  'claude-sonnet-4-6': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
@@ -105,6 +115,19 @@ export const MODEL_PRICING: Record<string, { input: number; output: number }> =
105
115
  'claude-3-5-sonnet': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
106
116
  'claude-3-5-haiku': { input: 0.8 / 1_000_000, output: 4 / 1_000_000 },
107
117
  'claude-3-opus': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
118
+
119
+ // OpenAI models
120
+ 'gpt-4o': { input: 2.5 / 1_000_000, output: 10 / 1_000_000 },
121
+ 'gpt-4o-mini': { input: 0.15 / 1_000_000, output: 0.6 / 1_000_000 },
122
+ 'gpt-4-turbo': { input: 10 / 1_000_000, output: 30 / 1_000_000 },
123
+ 'gpt-4-1': { input: 2 / 1_000_000, output: 8 / 1_000_000 },
124
+ 'o1': { input: 15 / 1_000_000, output: 60 / 1_000_000 },
125
+ 'o3': { input: 10 / 1_000_000, output: 40 / 1_000_000 },
126
+ 'o4-mini': { input: 1.1 / 1_000_000, output: 4.4 / 1_000_000 },
127
+
128
+ // DeepSeek models
129
+ 'deepseek-chat': { input: 0.27 / 1_000_000, output: 1.1 / 1_000_000 },
130
+ 'deepseek-reasoner': { input: 0.55 / 1_000_000, output: 2.19 / 1_000_000 },
108
131
  }
109
132
 
110
133
  /**
package/.env.example DELETED
@@ -1,8 +0,0 @@
1
- # Required: your LLM API key
2
- CODEANY_API_KEY=sk-or-...
3
-
4
- # Optional: override model
5
- # CODEANY_MODEL=anthropic/claude-sonnet-4
6
-
7
- # Optional: custom API endpoint (e.g. third-party proxy)
8
- # CODEANY_BASE_URL=https://openrouter.ai/api
@@ -1,43 +0,0 @@
1
- /**
2
- * Example 1: Simple Query with Streaming
3
- *
4
- * Demonstrates the basic createAgent() + query() flow with
5
- * real-time event streaming.
6
- *
7
- * Run: npx tsx examples/01-simple-query.ts
8
- */
9
- import { createAgent } from '../src/index.js'
10
-
11
- async function main() {
12
- console.log('--- Example 1: Simple Query ---\n')
13
-
14
- const agent = createAgent({
15
- model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
16
- maxTurns: 10,
17
- })
18
-
19
- for await (const event of agent.query(
20
- 'Read package.json and tell me the project name and version in one sentence.',
21
- )) {
22
- const msg = event as any
23
-
24
- if (msg.type === 'assistant') {
25
- // Print tool calls
26
- for (const block of msg.message?.content || []) {
27
- if (block.type === 'tool_use') {
28
- console.log(`[Tool] ${block.name}(${JSON.stringify(block.input).slice(0, 80)})`)
29
- }
30
- if (block.type === 'text') {
31
- console.log(`\nAssistant: ${block.text}`)
32
- }
33
- }
34
- }
35
-
36
- if (msg.type === 'result') {
37
- console.log(`\n--- Result: ${msg.subtype} ---`)
38
- console.log(`Tokens: ${msg.usage?.input_tokens} in / ${msg.usage?.output_tokens} out`)
39
- }
40
- }
41
- }
42
-
43
- main().catch(console.error)
@@ -1,44 +0,0 @@
1
- /**
2
- * Example 2: Multi-Tool Orchestration
3
- *
4
- * The agent autonomously uses Glob, Bash, and Read tools to
5
- * accomplish a multi-step task.
6
- *
7
- * Run: npx tsx examples/02-multi-tool.ts
8
- */
9
- import { createAgent } from '../src/index.js'
10
-
11
- async function main() {
12
- console.log('--- Example 2: Multi-Tool Orchestration ---\n')
13
-
14
- const agent = createAgent({
15
- model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
16
- maxTurns: 15,
17
- })
18
-
19
- for await (const event of agent.query(
20
- 'Do these steps: ' +
21
- '1) Use Glob to find all .ts files in src/ (pattern "src/*.ts"). ' +
22
- '2) Use Bash to count lines in src/agent.ts with `wc -l`. ' +
23
- '3) Give a brief summary.',
24
- )) {
25
- const msg = event as any
26
-
27
- if (msg.type === 'assistant') {
28
- for (const block of msg.message?.content || []) {
29
- if (block.type === 'tool_use') {
30
- console.log(`[${block.name}] ${JSON.stringify(block.input).slice(0, 100)}`)
31
- }
32
- if (block.type === 'text' && block.text.trim()) {
33
- console.log(`\n${block.text}`)
34
- }
35
- }
36
- }
37
-
38
- if (msg.type === 'result') {
39
- console.log(`\n--- ${msg.subtype} | ${msg.usage?.input_tokens}/${msg.usage?.output_tokens} tokens ---`)
40
- }
41
- }
42
- }
43
-
44
- main().catch(console.error)
@@ -1,39 +0,0 @@
1
- /**
2
- * Example 3: Multi-Turn Conversation
3
- *
4
- * Demonstrates session persistence across multiple turns.
5
- * The agent remembers context from previous interactions.
6
- *
7
- * Run: npx tsx examples/03-multi-turn.ts
8
- */
9
- import { createAgent } from '../src/index.js'
10
-
11
- async function main() {
12
- console.log('--- Example 3: Multi-Turn Conversation ---\n')
13
-
14
- const agent = createAgent({
15
- model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
16
- maxTurns: 5,
17
- })
18
-
19
- // Turn 1: Create a file
20
- console.log('> Turn 1: Create a file')
21
- const r1 = await agent.prompt(
22
- 'Use Bash to run: echo "Hello Open Agent SDK" > /tmp/oas-test.txt. Confirm briefly.',
23
- )
24
- console.log(` ${r1.text}\n`)
25
-
26
- // Turn 2: Read back (should remember context)
27
- console.log('> Turn 2: Read the file back')
28
- const r2 = await agent.prompt('Read the file you just created and tell me its contents.')
29
- console.log(` ${r2.text}\n`)
30
-
31
- // Turn 3: Clean up
32
- console.log('> Turn 3: Cleanup')
33
- const r3 = await agent.prompt('Delete that file with Bash. Confirm.')
34
- console.log(` ${r3.text}\n`)
35
-
36
- console.log(`Session history: ${agent.getMessages().length} messages`)
37
- }
38
-
39
- main().catch(console.error)
@@ -1,29 +0,0 @@
1
- /**
2
- * Example 4: Simple Prompt API
3
- *
4
- * Uses the blocking prompt() method for quick one-shot queries.
5
- * No need to iterate over streaming events.
6
- *
7
- * Run: npx tsx examples/04-prompt-api.ts
8
- */
9
- import { createAgent } from '../src/index.js'
10
-
11
- async function main() {
12
- console.log('--- Example 4: Simple Prompt API ---\n')
13
-
14
- const agent = createAgent({
15
- model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
16
- maxTurns: 5,
17
- })
18
-
19
- const result = await agent.prompt(
20
- 'Use Bash to run `node --version` and `npm --version`, then tell me the versions.',
21
- )
22
-
23
- console.log(`Answer: ${result.text}`)
24
- console.log(`Turns: ${result.num_turns}`)
25
- console.log(`Tokens: ${result.usage.input_tokens} in / ${result.usage.output_tokens} out`)
26
- console.log(`Duration: ${result.duration_ms}ms`)
27
- }
28
-
29
- main().catch(console.error)
@@ -1,26 +0,0 @@
1
- /**
2
- * Example 5: Custom System Prompt
3
- *
4
- * Shows how to customize the agent's behavior with a system prompt.
5
- *
6
- * Run: npx tsx examples/05-custom-system-prompt.ts
7
- */
8
- import { createAgent } from '../src/index.js'
9
-
10
- async function main() {
11
- console.log('--- Example 5: Custom System Prompt ---\n')
12
-
13
- const agent = createAgent({
14
- model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
15
- maxTurns: 5,
16
- systemPrompt:
17
- 'You are a senior code reviewer. When asked to review code, focus on: ' +
18
- '1) Security issues, 2) Performance concerns, 3) Maintainability. ' +
19
- 'Be concise and use bullet points.',
20
- })
21
-
22
- const result = await agent.prompt('Read src/agent.ts and give a brief code review.')
23
- console.log(result.text)
24
- }
25
-
26
- main().catch(console.error)
@@ -1,49 +0,0 @@
1
- /**
2
- * Example 6: MCP Server Integration
3
- *
4
- * Connects to an MCP (Model Context Protocol) server and uses
5
- * its tools through the agent. This example uses the filesystem
6
- * MCP server as a demonstration.
7
- *
8
- * Prerequisites:
9
- * npm install -g @modelcontextprotocol/server-filesystem
10
- *
11
- * Run: npx tsx examples/06-mcp-server.ts
12
- */
13
- import { createAgent } from '../src/index.js'
14
-
15
- async function main() {
16
- console.log('--- Example 6: MCP Server Integration ---\n')
17
-
18
- const agent = createAgent({
19
- model: process.env.CODEANY_MODEL || 'claude-sonnet-4-6',
20
- maxTurns: 10,
21
- mcpServers: {
22
- filesystem: {
23
- command: 'npx',
24
- args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp'],
25
- },
26
- },
27
- })
28
-
29
- console.log('Connecting to MCP filesystem server...\n')
30
-
31
- const result = await agent.prompt(
32
- 'Use the filesystem MCP tools to list files in /tmp. Be brief.',
33
- )
34
-
35
- console.log(`Answer: ${result.text}`)
36
- console.log(`Turns: ${result.num_turns}`)
37
-
38
- await agent.close()
39
- }
40
-
41
- main().catch(e => {
42
- console.error('Error:', e.message)
43
- if (e.message.includes('ENOENT') || e.message.includes('not found')) {
44
- console.error(
45
- '\nMCP server not found. Install it with:\n' +
46
- ' npm install -g @modelcontextprotocol/server-filesystem\n',
47
- )
48
- }
49
- })
@@ -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)
@@ -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)