@codeany/open-agent-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.env.example +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +388 -0
  4. package/examples/01-simple-query.ts +43 -0
  5. package/examples/02-multi-tool.ts +44 -0
  6. package/examples/03-multi-turn.ts +39 -0
  7. package/examples/04-prompt-api.ts +29 -0
  8. package/examples/05-custom-system-prompt.ts +26 -0
  9. package/examples/06-mcp-server.ts +49 -0
  10. package/examples/07-custom-tools.ts +87 -0
  11. package/examples/08-official-api-compat.ts +38 -0
  12. package/examples/09-subagents.ts +48 -0
  13. package/examples/10-permissions.ts +40 -0
  14. package/examples/11-custom-mcp-tools.ts +101 -0
  15. package/examples/web/index.html +365 -0
  16. package/examples/web/server.ts +157 -0
  17. package/package.json +60 -0
  18. package/src/agent.ts +425 -0
  19. package/src/engine.ts +520 -0
  20. package/src/hooks.ts +261 -0
  21. package/src/index.ts +376 -0
  22. package/src/mcp/client.ts +150 -0
  23. package/src/sdk-mcp-server.ts +78 -0
  24. package/src/session.ts +227 -0
  25. package/src/tool-helper.ts +127 -0
  26. package/src/tools/agent-tool.ts +153 -0
  27. package/src/tools/ask-user.ts +79 -0
  28. package/src/tools/bash.ts +75 -0
  29. package/src/tools/config-tool.ts +89 -0
  30. package/src/tools/cron-tools.ts +153 -0
  31. package/src/tools/edit.ts +74 -0
  32. package/src/tools/glob.ts +77 -0
  33. package/src/tools/grep.ts +168 -0
  34. package/src/tools/index.ts +232 -0
  35. package/src/tools/lsp-tool.ts +163 -0
  36. package/src/tools/mcp-resource-tools.ts +125 -0
  37. package/src/tools/notebook-edit.ts +93 -0
  38. package/src/tools/plan-tools.ts +88 -0
  39. package/src/tools/read.ts +73 -0
  40. package/src/tools/send-message.ts +96 -0
  41. package/src/tools/task-tools.ts +290 -0
  42. package/src/tools/team-tools.ts +128 -0
  43. package/src/tools/todo-tool.ts +112 -0
  44. package/src/tools/tool-search.ts +87 -0
  45. package/src/tools/types.ts +62 -0
  46. package/src/tools/web-fetch.ts +66 -0
  47. package/src/tools/web-search.ts +86 -0
  48. package/src/tools/worktree-tools.ts +140 -0
  49. package/src/tools/write.ts +42 -0
  50. package/src/types.ts +459 -0
  51. package/src/utils/compact.ts +206 -0
  52. package/src/utils/context.ts +191 -0
  53. package/src/utils/fileCache.ts +148 -0
  54. package/src/utils/messages.ts +196 -0
  55. package/src/utils/retry.ts +140 -0
  56. package/src/utils/tokens.ts +122 -0
  57. package/tsconfig.json +19 -0
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Token Estimation & Counting
3
+ *
4
+ * Provides rough token estimation (character-based) and
5
+ * API-based exact counting when available.
6
+ */
7
+
8
+ import Anthropic from '@anthropic-ai/sdk'
9
+
10
+ /**
11
+ * Rough token estimation: ~4 chars per token (conservative).
12
+ */
13
+ export function estimateTokens(text: string): number {
14
+ return Math.ceil(text.length / 4)
15
+ }
16
+
17
+ /**
18
+ * Estimate tokens for a message array.
19
+ */
20
+ export function estimateMessagesTokens(
21
+ messages: Anthropic.MessageParam[],
22
+ ): number {
23
+ let total = 0
24
+ for (const msg of messages) {
25
+ if (typeof msg.content === 'string') {
26
+ total += estimateTokens(msg.content)
27
+ } else if (Array.isArray(msg.content)) {
28
+ for (const block of msg.content) {
29
+ if ('text' in block && typeof block.text === 'string') {
30
+ total += estimateTokens(block.text)
31
+ } else if ('content' in block && typeof block.content === 'string') {
32
+ total += estimateTokens(block.content)
33
+ } else {
34
+ // tool_use, image, etc - rough estimate
35
+ total += estimateTokens(JSON.stringify(block))
36
+ }
37
+ }
38
+ }
39
+ }
40
+ return total
41
+ }
42
+
43
+ /**
44
+ * Estimate tokens for a system prompt.
45
+ */
46
+ export function estimateSystemPromptTokens(systemPrompt: string): number {
47
+ return estimateTokens(systemPrompt)
48
+ }
49
+
50
+ /**
51
+ * Count tokens from API usage response.
52
+ */
53
+ export function getTokenCountFromUsage(usage: {
54
+ input_tokens: number
55
+ output_tokens: number
56
+ cache_creation_input_tokens?: number
57
+ cache_read_input_tokens?: number
58
+ }): number {
59
+ return (
60
+ usage.input_tokens +
61
+ usage.output_tokens +
62
+ (usage.cache_creation_input_tokens || 0) +
63
+ (usage.cache_read_input_tokens || 0)
64
+ )
65
+ }
66
+
67
+ /**
68
+ * Get the context window size for a model.
69
+ */
70
+ export function getContextWindowSize(model: string): number {
71
+ // Model context windows
72
+ if (model.includes('opus-4') && model.includes('1m')) return 1_000_000
73
+ if (model.includes('opus-4')) return 200_000
74
+ if (model.includes('sonnet-4')) return 200_000
75
+ if (model.includes('haiku-4')) return 200_000
76
+
77
+
78
+ if (model.includes('claude-3')) return 200_000
79
+
80
+ // Default
81
+ return 200_000
82
+ }
83
+
84
+ /**
85
+ * Auto-compact buffer: trigger compaction when within this many tokens of the limit.
86
+ */
87
+ export const AUTOCOMPACT_BUFFER_TOKENS = 13_000
88
+
89
+ /**
90
+ * Get the auto-compact threshold for a model.
91
+ */
92
+ export function getAutoCompactThreshold(model: string): number {
93
+ return getContextWindowSize(model) - AUTOCOMPACT_BUFFER_TOKENS
94
+ }
95
+
96
+ /**
97
+ * Model pricing (USD per token).
98
+ */
99
+ export const MODEL_PRICING: Record<string, { input: number; output: number }> = {
100
+ 'claude-opus-4-6': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
101
+ 'claude-opus-4-5': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
102
+ 'claude-sonnet-4-6': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
103
+ 'claude-sonnet-4-5': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
104
+ 'claude-haiku-4-5': { input: 0.8 / 1_000_000, output: 4 / 1_000_000 },
105
+ 'claude-3-5-sonnet': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
106
+ 'claude-3-5-haiku': { input: 0.8 / 1_000_000, output: 4 / 1_000_000 },
107
+ 'claude-3-opus': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
108
+ }
109
+
110
+ /**
111
+ * Estimate cost from usage and model.
112
+ */
113
+ export function estimateCost(
114
+ model: string,
115
+ usage: { input_tokens: number; output_tokens: number },
116
+ ): number {
117
+ const pricing = Object.entries(MODEL_PRICING).find(([key]) =>
118
+ model.includes(key),
119
+ )?.[1] ?? { input: 3 / 1_000_000, output: 15 / 1_000_000 }
120
+
121
+ return usage.input_tokens * pricing.input + usage.output_tokens * pricing.output
122
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "outDir": "./dist",
7
+ "rootDir": "./src",
8
+ "declaration": true,
9
+ "declarationMap": true,
10
+ "sourceMap": true,
11
+ "strict": true,
12
+ "esModuleInterop": true,
13
+ "skipLibCheck": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "resolveJsonModule": true
16
+ },
17
+ "include": ["src/**/*"],
18
+ "exclude": ["node_modules", "dist", "examples"]
19
+ }