@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.
- 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 +496 -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/{src/skills/bundled/debug.ts → dist/skills/bundled/debug.js} +20 -22
- 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/{src/skills/bundled/review.ts → dist/skills/bundled/review.js} +18 -21
- 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/{src/skills/bundled/simplify.ts → dist/skills/bundled/simplify.js} +16 -19
- 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/{src/skills/bundled/test.ts → dist/skills/bundled/test.js} +18 -21
- 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 +6 -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/src/agent.ts +0 -516
- package/src/engine.ts +0 -633
- package/src/hooks.ts +0 -261
- package/src/index.ts +0 -426
- package/src/mcp/client.ts +0 -150
- package/src/providers/anthropic.ts +0 -60
- package/src/providers/index.ts +0 -34
- package/src/providers/openai.ts +0 -315
- package/src/providers/types.ts +0 -85
- package/src/sdk-mcp-server.ts +0 -78
- package/src/session.ts +0 -227
- package/src/skills/bundled/commit.ts +0 -38
- package/src/skills/bundled/index.ts +0 -28
- package/src/skills/index.ts +0 -25
- package/src/skills/registry.ts +0 -133
- package/src/skills/types.ts +0 -99
- package/src/tool-helper.ts +0 -127
- package/src/tools/agent-tool.ts +0 -164
- package/src/tools/ask-user.ts +0 -79
- package/src/tools/bash.ts +0 -75
- package/src/tools/config-tool.ts +0 -89
- package/src/tools/cron-tools.ts +0 -153
- package/src/tools/edit.ts +0 -74
- package/src/tools/glob.ts +0 -77
- package/src/tools/grep.ts +0 -168
- package/src/tools/index.ts +0 -240
- package/src/tools/lsp-tool.ts +0 -163
- package/src/tools/mcp-resource-tools.ts +0 -125
- package/src/tools/notebook-edit.ts +0 -93
- package/src/tools/plan-tools.ts +0 -88
- package/src/tools/read.ts +0 -73
- package/src/tools/send-message.ts +0 -96
- package/src/tools/skill-tool.ts +0 -133
- package/src/tools/task-tools.ts +0 -290
- package/src/tools/team-tools.ts +0 -128
- package/src/tools/todo-tool.ts +0 -112
- package/src/tools/tool-search.ts +0 -87
- package/src/tools/types.ts +0 -66
- package/src/tools/web-fetch.ts +0 -66
- package/src/tools/web-search.ts +0 -86
- package/src/tools/worktree-tools.ts +0 -140
- package/src/tools/write.ts +0 -42
- package/src/types.ts +0 -486
- package/src/utils/compact.ts +0 -207
- package/src/utils/context.ts +0 -191
- package/src/utils/fileCache.ts +0 -148
- package/src/utils/messages.ts +0 -195
- package/src/utils/retry.ts +0 -140
- package/src/utils/tokens.ts +0 -145
- package/tsconfig.json +0 -19
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry Logic with Exponential Backoff
|
|
3
|
+
*
|
|
4
|
+
* Handles API retries for rate limits, overloaded servers,
|
|
5
|
+
* and transient failures.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Retry configuration.
|
|
9
|
+
*/
|
|
10
|
+
export interface RetryConfig {
|
|
11
|
+
maxRetries: number;
|
|
12
|
+
baseDelayMs: number;
|
|
13
|
+
maxDelayMs: number;
|
|
14
|
+
retryableStatusCodes: number[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Default retry configuration.
|
|
18
|
+
*/
|
|
19
|
+
export declare const DEFAULT_RETRY_CONFIG: RetryConfig;
|
|
20
|
+
/**
|
|
21
|
+
* Check if an error is retryable.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isRetryableError(err: any, config?: RetryConfig): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Calculate delay for exponential backoff.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getRetryDelay(attempt: number, config?: RetryConfig): number;
|
|
28
|
+
/**
|
|
29
|
+
* Execute a function with retries.
|
|
30
|
+
*/
|
|
31
|
+
export declare function withRetry<T>(fn: () => Promise<T>, config?: RetryConfig, abortSignal?: AbortSignal): Promise<T>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if an error is a "prompt too long" error.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isPromptTooLongError(err: any): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Check if error is an auth error.
|
|
38
|
+
*/
|
|
39
|
+
export declare function isAuthError(err: any): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Check if error is a rate limit error.
|
|
42
|
+
*/
|
|
43
|
+
export declare function isRateLimitError(err: any): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Format an API error for display.
|
|
46
|
+
*/
|
|
47
|
+
export declare function formatApiError(err: any): string;
|
|
48
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAA;IAClB,oBAAoB,EAAE,MAAM,EAAE,CAAA;CAC/B;AAED;;GAEG;AACH,eAAO,MAAM,oBAAoB,EAAE,WAKlC,CAAA;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,GAAE,WAAkC,GAAG,OAAO,CAgB9F;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,WAAkC,GAAG,MAAM,CAKjG;AAED;;GAEG;AACH,wBAAsB,SAAS,CAAC,CAAC,EAC/B,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,MAAM,GAAE,WAAkC,EAC1C,WAAW,CAAC,EAAE,WAAW,GACxB,OAAO,CAAC,CAAC,CAAC,CA4BZ;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAQtD;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAE7C;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAElD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAc/C"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry Logic with Exponential Backoff
|
|
3
|
+
*
|
|
4
|
+
* Handles API retries for rate limits, overloaded servers,
|
|
5
|
+
* and transient failures.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Default retry configuration.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_RETRY_CONFIG = {
|
|
11
|
+
maxRetries: 3,
|
|
12
|
+
baseDelayMs: 2000,
|
|
13
|
+
maxDelayMs: 30000,
|
|
14
|
+
retryableStatusCodes: [429, 500, 502, 503, 529],
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Check if an error is retryable.
|
|
18
|
+
*/
|
|
19
|
+
export function isRetryableError(err, config = DEFAULT_RETRY_CONFIG) {
|
|
20
|
+
if (err?.status && config.retryableStatusCodes.includes(err.status)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
// Network errors
|
|
24
|
+
if (err?.code === 'ECONNRESET' || err?.code === 'ETIMEDOUT' || err?.code === 'ECONNREFUSED') {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
// API overloaded
|
|
28
|
+
if (err?.error?.type === 'overloaded_error') {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Calculate delay for exponential backoff.
|
|
35
|
+
*/
|
|
36
|
+
export function getRetryDelay(attempt, config = DEFAULT_RETRY_CONFIG) {
|
|
37
|
+
const delay = config.baseDelayMs * Math.pow(2, attempt);
|
|
38
|
+
// Add jitter (±25%)
|
|
39
|
+
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
40
|
+
return Math.min(delay + jitter, config.maxDelayMs);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Execute a function with retries.
|
|
44
|
+
*/
|
|
45
|
+
export async function withRetry(fn, config = DEFAULT_RETRY_CONFIG, abortSignal) {
|
|
46
|
+
let lastError;
|
|
47
|
+
for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
|
|
48
|
+
if (abortSignal?.aborted) {
|
|
49
|
+
throw new Error('Aborted');
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return await fn();
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
lastError = err;
|
|
56
|
+
if (!isRetryableError(err, config)) {
|
|
57
|
+
throw err;
|
|
58
|
+
}
|
|
59
|
+
if (attempt === config.maxRetries) {
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
// Wait before retry
|
|
63
|
+
const delay = getRetryDelay(attempt, config);
|
|
64
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
throw lastError;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Check if an error is a "prompt too long" error.
|
|
71
|
+
*/
|
|
72
|
+
export function isPromptTooLongError(err) {
|
|
73
|
+
if (err?.status === 400) {
|
|
74
|
+
const message = err?.error?.error?.message || err?.message || '';
|
|
75
|
+
return message.includes('prompt is too long') ||
|
|
76
|
+
message.includes('max_tokens') ||
|
|
77
|
+
message.includes('context length');
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Check if error is an auth error.
|
|
83
|
+
*/
|
|
84
|
+
export function isAuthError(err) {
|
|
85
|
+
return err?.status === 401 || err?.status === 403;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Check if error is a rate limit error.
|
|
89
|
+
*/
|
|
90
|
+
export function isRateLimitError(err) {
|
|
91
|
+
return err?.status === 429;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Format an API error for display.
|
|
95
|
+
*/
|
|
96
|
+
export function formatApiError(err) {
|
|
97
|
+
if (isAuthError(err)) {
|
|
98
|
+
return 'Authentication failed. Check your CODEANY_API_KEY.';
|
|
99
|
+
}
|
|
100
|
+
if (isRateLimitError(err)) {
|
|
101
|
+
return 'Rate limit exceeded. Please retry after a short wait.';
|
|
102
|
+
}
|
|
103
|
+
if (err?.status === 529) {
|
|
104
|
+
return 'API overloaded. Please retry later.';
|
|
105
|
+
}
|
|
106
|
+
if (isPromptTooLongError(err)) {
|
|
107
|
+
return 'Prompt too long. Auto-compacting conversation...';
|
|
108
|
+
}
|
|
109
|
+
return `API error: ${err.message || err}`;
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH;;GAEG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C,UAAU,EAAE,CAAC;IACb,WAAW,EAAE,IAAI;IACjB,UAAU,EAAE,KAAK;IACjB,oBAAoB,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;CAChD,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAAQ,EAAE,SAAsB,oBAAoB;IACnF,IAAI,GAAG,EAAE,MAAM,IAAI,MAAM,CAAC,oBAAoB,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACpE,OAAO,IAAI,CAAA;IACb,CAAC;IAED,iBAAiB;IACjB,IAAI,GAAG,EAAE,IAAI,KAAK,YAAY,IAAI,GAAG,EAAE,IAAI,KAAK,WAAW,IAAI,GAAG,EAAE,IAAI,KAAK,cAAc,EAAE,CAAC;QAC5F,OAAO,IAAI,CAAA;IACb,CAAC;IAED,iBAAiB;IACjB,IAAI,GAAG,EAAE,KAAK,EAAE,IAAI,KAAK,kBAAkB,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa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+
* Token Estimation & Counting
|
|
3
|
+
*
|
|
4
|
+
* Provides rough token estimation (character-based) and
|
|
5
|
+
* API-based exact counting when available.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Rough token estimation: ~4 chars per token (conservative).
|
|
9
|
+
*/
|
|
10
|
+
export declare function estimateTokens(text: string): number;
|
|
11
|
+
/**
|
|
12
|
+
* Estimate tokens for a message array.
|
|
13
|
+
*/
|
|
14
|
+
export declare function estimateMessagesTokens(messages: Array<{
|
|
15
|
+
role: string;
|
|
16
|
+
content: any;
|
|
17
|
+
}>): number;
|
|
18
|
+
/**
|
|
19
|
+
* Estimate tokens for a system prompt.
|
|
20
|
+
*/
|
|
21
|
+
export declare function estimateSystemPromptTokens(systemPrompt: string): number;
|
|
22
|
+
/**
|
|
23
|
+
* Count tokens from API usage response.
|
|
24
|
+
*/
|
|
25
|
+
export declare function getTokenCountFromUsage(usage: {
|
|
26
|
+
input_tokens: number;
|
|
27
|
+
output_tokens: number;
|
|
28
|
+
cache_creation_input_tokens?: number;
|
|
29
|
+
cache_read_input_tokens?: number;
|
|
30
|
+
}): number;
|
|
31
|
+
/**
|
|
32
|
+
* Get the context window size for a model.
|
|
33
|
+
*/
|
|
34
|
+
export declare function getContextWindowSize(model: string): number;
|
|
35
|
+
/**
|
|
36
|
+
* Auto-compact buffer: trigger compaction when within this many tokens of the limit.
|
|
37
|
+
*/
|
|
38
|
+
export declare const AUTOCOMPACT_BUFFER_TOKENS = 13000;
|
|
39
|
+
/**
|
|
40
|
+
* Get the auto-compact threshold for a model.
|
|
41
|
+
*/
|
|
42
|
+
export declare function getAutoCompactThreshold(model: string): number;
|
|
43
|
+
/**
|
|
44
|
+
* Model pricing (USD per token).
|
|
45
|
+
*/
|
|
46
|
+
export declare const MODEL_PRICING: Record<string, {
|
|
47
|
+
input: number;
|
|
48
|
+
output: number;
|
|
49
|
+
}>;
|
|
50
|
+
/**
|
|
51
|
+
* Estimate cost from usage and model.
|
|
52
|
+
*/
|
|
53
|
+
export declare function estimateCost(model: string, usage: {
|
|
54
|
+
input_tokens: number;
|
|
55
|
+
output_tokens: number;
|
|
56
|
+
}): number;
|
|
57
|
+
//# sourceMappingURL=tokens.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.d.ts","sourceRoot":"","sources":["../../src/utils/tokens.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,KAAK,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,GAAG,CAAA;CAAE,CAAC,GAC9C,MAAM,CAmBR;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE;IAC5C,YAAY,EAAE,MAAM,CAAA;IACpB,aAAa,EAAE,MAAM,CAAA;IACrB,2BAA2B,CAAC,EAAE,MAAM,CAAA;IACpC,uBAAuB,CAAC,EAAE,MAAM,CAAA;CACjC,GAAG,MAAM,CAOT;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAuB1D;AAED;;GAEG;AACH,eAAO,MAAM,yBAAyB,QAAS,CAAA;AAE/C;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAuB3E,CAAA;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE;IAAE,YAAY,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAA;CAAE,GACrD,MAAM,CAMR"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token Estimation & Counting
|
|
3
|
+
*
|
|
4
|
+
* Provides rough token estimation (character-based) and
|
|
5
|
+
* API-based exact counting when available.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Rough token estimation: ~4 chars per token (conservative).
|
|
9
|
+
*/
|
|
10
|
+
export function estimateTokens(text) {
|
|
11
|
+
return Math.ceil(text.length / 4);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Estimate tokens for a message array.
|
|
15
|
+
*/
|
|
16
|
+
export function estimateMessagesTokens(messages) {
|
|
17
|
+
let total = 0;
|
|
18
|
+
for (const msg of messages) {
|
|
19
|
+
if (typeof msg.content === 'string') {
|
|
20
|
+
total += estimateTokens(msg.content);
|
|
21
|
+
}
|
|
22
|
+
else if (Array.isArray(msg.content)) {
|
|
23
|
+
for (const block of msg.content) {
|
|
24
|
+
if ('text' in block && typeof block.text === 'string') {
|
|
25
|
+
total += estimateTokens(block.text);
|
|
26
|
+
}
|
|
27
|
+
else if ('content' in block && typeof block.content === 'string') {
|
|
28
|
+
total += estimateTokens(block.content);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// tool_use, image, etc - rough estimate
|
|
32
|
+
total += estimateTokens(JSON.stringify(block));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return total;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Estimate tokens for a system prompt.
|
|
41
|
+
*/
|
|
42
|
+
export function estimateSystemPromptTokens(systemPrompt) {
|
|
43
|
+
return estimateTokens(systemPrompt);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Count tokens from API usage response.
|
|
47
|
+
*/
|
|
48
|
+
export function getTokenCountFromUsage(usage) {
|
|
49
|
+
return (usage.input_tokens +
|
|
50
|
+
usage.output_tokens +
|
|
51
|
+
(usage.cache_creation_input_tokens || 0) +
|
|
52
|
+
(usage.cache_read_input_tokens || 0));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the context window size for a model.
|
|
56
|
+
*/
|
|
57
|
+
export function getContextWindowSize(model) {
|
|
58
|
+
// Anthropic model context windows
|
|
59
|
+
if (model.includes('opus-4') && model.includes('1m'))
|
|
60
|
+
return 1_000_000;
|
|
61
|
+
if (model.includes('opus-4'))
|
|
62
|
+
return 200_000;
|
|
63
|
+
if (model.includes('sonnet-4'))
|
|
64
|
+
return 200_000;
|
|
65
|
+
if (model.includes('haiku-4'))
|
|
66
|
+
return 200_000;
|
|
67
|
+
if (model.includes('claude-3'))
|
|
68
|
+
return 200_000;
|
|
69
|
+
// OpenAI model context windows
|
|
70
|
+
if (model.includes('gpt-4o'))
|
|
71
|
+
return 128_000;
|
|
72
|
+
if (model.includes('gpt-4-turbo'))
|
|
73
|
+
return 128_000;
|
|
74
|
+
if (model.includes('gpt-4-1'))
|
|
75
|
+
return 1_000_000;
|
|
76
|
+
if (model.includes('gpt-4'))
|
|
77
|
+
return 128_000;
|
|
78
|
+
if (model.includes('gpt-3.5'))
|
|
79
|
+
return 16_385;
|
|
80
|
+
if (model.includes('o1'))
|
|
81
|
+
return 200_000;
|
|
82
|
+
if (model.includes('o3'))
|
|
83
|
+
return 200_000;
|
|
84
|
+
if (model.includes('o4'))
|
|
85
|
+
return 200_000;
|
|
86
|
+
// DeepSeek models
|
|
87
|
+
if (model.includes('deepseek'))
|
|
88
|
+
return 128_000;
|
|
89
|
+
// Default
|
|
90
|
+
return 200_000;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Auto-compact buffer: trigger compaction when within this many tokens of the limit.
|
|
94
|
+
*/
|
|
95
|
+
export const AUTOCOMPACT_BUFFER_TOKENS = 13_000;
|
|
96
|
+
/**
|
|
97
|
+
* Get the auto-compact threshold for a model.
|
|
98
|
+
*/
|
|
99
|
+
export function getAutoCompactThreshold(model) {
|
|
100
|
+
return getContextWindowSize(model) - AUTOCOMPACT_BUFFER_TOKENS;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Model pricing (USD per token).
|
|
104
|
+
*/
|
|
105
|
+
export const MODEL_PRICING = {
|
|
106
|
+
// Anthropic models
|
|
107
|
+
'claude-opus-4-6': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
|
|
108
|
+
'claude-opus-4-5': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
|
|
109
|
+
'claude-sonnet-4-6': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
|
|
110
|
+
'claude-sonnet-4-5': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
|
|
111
|
+
'claude-haiku-4-5': { input: 0.8 / 1_000_000, output: 4 / 1_000_000 },
|
|
112
|
+
'claude-3-5-sonnet': { input: 3 / 1_000_000, output: 15 / 1_000_000 },
|
|
113
|
+
'claude-3-5-haiku': { input: 0.8 / 1_000_000, output: 4 / 1_000_000 },
|
|
114
|
+
'claude-3-opus': { input: 15 / 1_000_000, output: 75 / 1_000_000 },
|
|
115
|
+
// OpenAI models
|
|
116
|
+
'gpt-4o': { input: 2.5 / 1_000_000, output: 10 / 1_000_000 },
|
|
117
|
+
'gpt-4o-mini': { input: 0.15 / 1_000_000, output: 0.6 / 1_000_000 },
|
|
118
|
+
'gpt-4-turbo': { input: 10 / 1_000_000, output: 30 / 1_000_000 },
|
|
119
|
+
'gpt-4-1': { input: 2 / 1_000_000, output: 8 / 1_000_000 },
|
|
120
|
+
'o1': { input: 15 / 1_000_000, output: 60 / 1_000_000 },
|
|
121
|
+
'o3': { input: 10 / 1_000_000, output: 40 / 1_000_000 },
|
|
122
|
+
'o4-mini': { input: 1.1 / 1_000_000, output: 4.4 / 1_000_000 },
|
|
123
|
+
// DeepSeek models
|
|
124
|
+
'deepseek-chat': { input: 0.27 / 1_000_000, output: 1.1 / 1_000_000 },
|
|
125
|
+
'deepseek-reasoner': { input: 0.55 / 1_000_000, output: 2.19 / 1_000_000 },
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Estimate cost from usage and model.
|
|
129
|
+
*/
|
|
130
|
+
export function estimateCost(model, usage) {
|
|
131
|
+
const pricing = Object.entries(MODEL_PRICING).find(([key]) => model.includes(key))?.[1] ?? { input: 3 / 1_000_000, output: 15 / 1_000_000 };
|
|
132
|
+
return usage.input_tokens * pricing.input + usage.output_tokens * pricing.output;
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../src/utils/tokens.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;AACnC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,sBAAsB,CACpC,QAA+C;IAE/C,IAAI,KAAK,GAAG,CAAC,CAAA;IACb,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,IAAI,OAAO,GAAG,CAAC,OAAO,KAAK,QAAQ,EAAE,CAAC;YACpC,KAAK,IAAI,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;QACtC,CAAC;aAAM,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC;YACtC,KAAK,MAAM,KAAK,IAAI,GAAG,CAAC,OAAO,EAAE,CAAC;gBAChC,IAAI,MAAM,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBACtD,KAAK,IAAI,cAAc,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;gBACrC,CAAC;qBAAM,IAAI,SAAS,IAAI,KAAK,IA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G,GAAG,SAAS,EAAE;IACrE,mBAAmB,EAAE,EAAE,KAAK,EAAE,IAAI,GAAG,SAAS,EAAE,MAAM,EAAE,IAAI,GAAG,SAAS,EAAE;CAC3E,CAAA;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAa,EACb,KAAsD;IAEtD,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAC3D,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CACpB,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,SAAS,EAAE,MAAM,EAAE,EAAE,GAAG,SAAS,EAAE,CAAA;IAE1D,OAAO,KAAK,CAAC,YAAY,GAAG,OAAO,CAAC,KAAK,GAAG,KAAK,CAAC,aAAa,GAAG,OAAO,CAAC,MAAM,CAAA;AAClF,CAAC"}
|
package/package.json
CHANGED
|
@@ -12,11 +12,16 @@
|
|
|
12
12
|
"bugs": {
|
|
13
13
|
"url": "https://github.com/codeany-ai/open-agent-sdk-typescript/issues"
|
|
14
14
|
},
|
|
15
|
-
"version": "0.2.
|
|
15
|
+
"version": "0.2.3",
|
|
16
16
|
"description": "Open-source Agent SDK. Runs the full agent loop in-process — no local CLI required. Deploy anywhere: cloud, serverless, Docker, CI/CD.",
|
|
17
17
|
"type": "module",
|
|
18
18
|
"main": "./dist/index.js",
|
|
19
19
|
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
20
25
|
"exports": {
|
|
21
26
|
".": {
|
|
22
27
|
"types": "./dist/index.d.ts",
|
package/.env.example
DELETED
|
@@ -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
|
-
})
|