browser-use 0.2.0 → 0.3.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.
- package/README.md +295 -686
- package/dist/actor/element.d.ts +19 -0
- package/dist/actor/element.js +46 -0
- package/dist/actor/index.d.ts +4 -0
- package/dist/actor/index.js +4 -0
- package/dist/actor/mouse.d.ts +19 -0
- package/dist/actor/mouse.js +39 -0
- package/dist/actor/page.d.ts +29 -0
- package/dist/actor/page.js +88 -0
- package/dist/actor/utils.d.ts +4 -0
- package/dist/actor/utils.js +35 -0
- package/dist/agent/cloud-events.d.ts +18 -0
- package/dist/agent/cloud-events.js +65 -2
- package/dist/agent/gif.d.ts +1 -0
- package/dist/agent/gif.js +24 -2
- package/dist/agent/judge.d.ts +17 -0
- package/dist/agent/judge.js +197 -0
- package/dist/agent/message-manager/service.d.ts +12 -4
- package/dist/agent/message-manager/service.js +205 -39
- package/dist/agent/message-manager/utils.js +0 -1
- package/dist/agent/message-manager/views.d.ts +4 -0
- package/dist/agent/message-manager/views.js +11 -7
- package/dist/agent/prompts.d.ts +24 -3
- package/dist/agent/prompts.js +274 -59
- package/dist/agent/service.d.ts +99 -41
- package/dist/agent/service.js +2266 -472
- package/dist/agent/variable-detector.d.ts +12 -0
- package/dist/agent/variable-detector.js +211 -0
- package/dist/agent/views.d.ts +237 -18
- package/dist/agent/views.js +446 -33
- package/dist/browser/cloud/cloud.d.ts +20 -0
- package/dist/browser/cloud/cloud.js +129 -0
- package/dist/browser/cloud/index.d.ts +2 -0
- package/dist/browser/cloud/index.js +2 -0
- package/dist/browser/cloud/views.d.ts +41 -0
- package/dist/browser/cloud/views.js +35 -0
- package/dist/browser/events.d.ts +345 -0
- package/dist/browser/events.js +566 -0
- package/dist/browser/extensions.js +17 -17
- package/dist/browser/index.d.ts +4 -0
- package/dist/browser/index.js +4 -0
- package/dist/browser/profile.d.ts +8 -2
- package/dist/browser/profile.js +79 -12
- package/dist/browser/session-manager.d.ts +85 -0
- package/dist/browser/session-manager.js +208 -0
- package/dist/browser/session.d.ts +100 -8
- package/dist/browser/session.js +1097 -58
- package/dist/browser/types.d.ts +0 -2
- package/dist/browser/views.d.ts +39 -0
- package/dist/browser/views.js +32 -0
- package/dist/browser/watchdogs/aboutblank-watchdog.d.ts +12 -0
- package/dist/browser/watchdogs/aboutblank-watchdog.js +131 -0
- package/dist/browser/watchdogs/base.d.ts +21 -0
- package/dist/browser/watchdogs/base.js +81 -0
- package/dist/browser/watchdogs/cdp-session-watchdog.d.ts +14 -0
- package/dist/browser/watchdogs/cdp-session-watchdog.js +177 -0
- package/dist/browser/watchdogs/crash-watchdog.d.ts +38 -0
- package/dist/browser/watchdogs/crash-watchdog.js +296 -0
- package/dist/browser/watchdogs/default-action-watchdog.d.ts +49 -0
- package/dist/browser/watchdogs/default-action-watchdog.js +212 -0
- package/dist/browser/watchdogs/dom-watchdog.d.ts +8 -0
- package/dist/browser/watchdogs/dom-watchdog.js +31 -0
- package/dist/browser/watchdogs/downloads-watchdog.d.ts +77 -0
- package/dist/browser/watchdogs/downloads-watchdog.js +409 -0
- package/dist/browser/watchdogs/har-recording-watchdog.d.ts +19 -0
- package/dist/browser/watchdogs/har-recording-watchdog.js +317 -0
- package/dist/browser/watchdogs/index.d.ts +15 -0
- package/dist/browser/watchdogs/index.js +15 -0
- package/dist/browser/watchdogs/local-browser-watchdog.d.ts +10 -0
- package/dist/browser/watchdogs/local-browser-watchdog.js +32 -0
- package/dist/browser/watchdogs/permissions-watchdog.d.ts +8 -0
- package/dist/browser/watchdogs/permissions-watchdog.js +73 -0
- package/dist/browser/watchdogs/popups-watchdog.d.ts +13 -0
- package/dist/browser/watchdogs/popups-watchdog.js +77 -0
- package/dist/browser/watchdogs/recording-watchdog.d.ts +27 -0
- package/dist/browser/watchdogs/recording-watchdog.js +249 -0
- package/dist/browser/watchdogs/screenshot-watchdog.d.ts +6 -0
- package/dist/browser/watchdogs/screenshot-watchdog.js +13 -0
- package/dist/browser/watchdogs/security-watchdog.d.ts +10 -0
- package/dist/browser/watchdogs/security-watchdog.js +84 -0
- package/dist/browser/watchdogs/storage-state-watchdog.d.ts +24 -0
- package/dist/browser/watchdogs/storage-state-watchdog.js +288 -0
- package/dist/cli.d.ts +7 -2
- package/dist/cli.js +182 -25
- package/dist/code-use/formatting.d.ts +3 -0
- package/dist/code-use/formatting.js +18 -0
- package/dist/code-use/index.d.ts +6 -0
- package/dist/code-use/index.js +6 -0
- package/dist/code-use/namespace.d.ts +5 -0
- package/dist/code-use/namespace.js +81 -0
- package/dist/code-use/notebook-export.d.ts +3 -0
- package/dist/code-use/notebook-export.js +56 -0
- package/dist/code-use/service.d.ts +24 -0
- package/dist/code-use/service.js +104 -0
- package/dist/code-use/utils.d.ts +4 -0
- package/dist/code-use/utils.js +98 -0
- package/dist/code-use/views.d.ts +108 -0
- package/dist/code-use/views.js +165 -0
- package/dist/config.d.ts +13 -0
- package/dist/config.js +69 -3
- package/dist/controller/registry/service.d.ts +10 -1
- package/dist/controller/registry/service.js +266 -10
- package/dist/controller/registry/views.d.ts +4 -1
- package/dist/controller/registry/views.js +25 -2
- package/dist/controller/service.d.ts +10 -1
- package/dist/controller/service.js +1807 -268
- package/dist/controller/views.d.ts +78 -155
- package/dist/controller/views.js +61 -12
- package/dist/dom/history-tree-processor/service.d.ts +5 -0
- package/dist/dom/history-tree-processor/service.js +169 -14
- package/dist/dom/history-tree-processor/view.d.ts +7 -1
- package/dist/dom/history-tree-processor/view.js +10 -1
- package/dist/dom/markdown-extractor.d.ts +37 -0
- package/dist/dom/markdown-extractor.js +345 -0
- package/dist/dom/service.d.ts +3 -1
- package/dist/dom/service.js +76 -0
- package/dist/dom/views.d.ts +1 -0
- package/dist/dom/views.js +45 -0
- package/dist/event-bus.d.ts +107 -7
- package/dist/event-bus.js +313 -10
- package/dist/exceptions.d.ts +0 -3
- package/dist/exceptions.js +0 -7
- package/dist/filesystem/file-system.d.ts +18 -0
- package/dist/filesystem/file-system.js +503 -42
- package/dist/index.d.ts +7 -0
- package/dist/index.js +6 -0
- package/dist/integrations/gmail/actions.d.ts +3 -3
- package/dist/integrations/gmail/actions.js +4 -4
- package/dist/llm/anthropic/chat.d.ts +18 -1
- package/dist/llm/anthropic/chat.js +123 -55
- package/dist/llm/anthropic/serializer.d.ts +2 -0
- package/dist/llm/anthropic/serializer.js +81 -9
- package/dist/llm/aws/chat-anthropic.d.ts +17 -0
- package/dist/llm/aws/chat-anthropic.js +126 -26
- package/dist/llm/aws/chat-bedrock.d.ts +28 -1
- package/dist/llm/aws/chat-bedrock.js +161 -34
- package/dist/llm/aws/serializer.d.ts +13 -1
- package/dist/llm/aws/serializer.js +56 -17
- package/dist/llm/azure/chat.d.ts +53 -2
- package/dist/llm/azure/chat.js +366 -54
- package/dist/llm/base.d.ts +2 -0
- package/dist/llm/browser-use/chat.d.ts +40 -0
- package/dist/llm/browser-use/chat.js +305 -0
- package/dist/llm/browser-use/index.d.ts +1 -0
- package/dist/llm/browser-use/index.js +1 -0
- package/dist/llm/cerebras/chat.d.ts +39 -0
- package/dist/llm/cerebras/chat.js +178 -0
- package/dist/llm/cerebras/index.d.ts +2 -0
- package/dist/llm/cerebras/index.js +2 -0
- package/dist/llm/cerebras/serializer.d.ts +7 -0
- package/dist/llm/cerebras/serializer.js +82 -0
- package/dist/llm/deepseek/chat.d.ts +19 -2
- package/dist/llm/deepseek/chat.js +138 -25
- package/dist/llm/google/chat.d.ts +46 -2
- package/dist/llm/google/chat.js +267 -64
- package/dist/llm/google/serializer.d.ts +9 -1
- package/dist/llm/google/serializer.js +141 -34
- package/dist/llm/groq/chat.d.ts +21 -2
- package/dist/llm/groq/chat.js +125 -26
- package/dist/llm/groq/parser.js +3 -1
- package/dist/llm/mistral/chat.d.ts +43 -0
- package/dist/llm/mistral/chat.js +154 -0
- package/dist/llm/mistral/index.d.ts +2 -0
- package/dist/llm/mistral/index.js +2 -0
- package/dist/llm/mistral/schema.d.ts +8 -0
- package/dist/llm/mistral/schema.js +27 -0
- package/dist/llm/models.d.ts +2 -0
- package/dist/llm/models.js +317 -0
- package/dist/llm/ollama/chat.d.ts +13 -1
- package/dist/llm/ollama/chat.js +110 -19
- package/dist/llm/ollama/serializer.d.ts +1 -0
- package/dist/llm/ollama/serializer.js +34 -12
- package/dist/llm/openai/chat.d.ts +16 -0
- package/dist/llm/openai/chat.js +94 -44
- package/dist/llm/openai/like.d.ts +5 -3
- package/dist/llm/openai/like.js +7 -3
- package/dist/llm/openai/responses-serializer.d.ts +18 -0
- package/dist/llm/openai/responses-serializer.js +72 -0
- package/dist/llm/openrouter/chat.d.ts +28 -2
- package/dist/llm/openrouter/chat.js +115 -29
- package/dist/llm/schema.d.ts +11 -1
- package/dist/llm/schema.js +81 -1
- package/dist/llm/vercel/chat.d.ts +50 -0
- package/dist/llm/vercel/chat.js +276 -0
- package/dist/llm/vercel/index.d.ts +1 -0
- package/dist/llm/vercel/index.js +1 -0
- package/dist/llm/vercel/serializer.d.ts +5 -0
- package/dist/llm/vercel/serializer.js +7 -0
- package/dist/llm/views.d.ts +2 -1
- package/dist/llm/views.js +3 -1
- package/dist/logging-config.d.ts +2 -0
- package/dist/logging-config.js +82 -29
- package/dist/mcp/client.d.ts +10 -5
- package/dist/mcp/client.js +14 -9
- package/dist/mcp/controller.d.ts +42 -3
- package/dist/mcp/controller.js +56 -31
- package/dist/mcp/server.d.ts +14 -0
- package/dist/mcp/server.js +255 -52
- package/dist/observability.js +10 -4
- package/dist/sandbox/index.d.ts +2 -0
- package/dist/sandbox/index.js +2 -0
- package/dist/sandbox/sandbox.d.ts +19 -0
- package/dist/sandbox/sandbox.js +140 -0
- package/dist/sandbox/views.d.ts +67 -0
- package/dist/sandbox/views.js +121 -0
- package/dist/skill-cli/index.d.ts +3 -0
- package/dist/skill-cli/index.js +3 -0
- package/dist/skill-cli/protocol.d.ts +30 -0
- package/dist/skill-cli/protocol.js +48 -0
- package/dist/skill-cli/server.d.ts +11 -0
- package/dist/skill-cli/server.js +85 -0
- package/dist/skill-cli/sessions.d.ts +24 -0
- package/dist/skill-cli/sessions.js +47 -0
- package/dist/skills/index.d.ts +3 -0
- package/dist/skills/index.js +3 -0
- package/dist/skills/service.d.ts +27 -0
- package/dist/skills/service.js +266 -0
- package/dist/skills/utils.d.ts +6 -0
- package/dist/skills/utils.js +53 -0
- package/dist/skills/views.d.ts +40 -0
- package/dist/skills/views.js +10 -0
- package/dist/sync/auth.js +8 -3
- package/dist/sync/service.d.ts +6 -6
- package/dist/sync/service.js +54 -89
- package/dist/telemetry/views.d.ts +20 -6
- package/dist/telemetry/views.js +23 -5
- package/dist/tokens/custom-pricing.d.ts +2 -0
- package/dist/tokens/custom-pricing.js +22 -0
- package/dist/tokens/index.d.ts +2 -0
- package/dist/tokens/index.js +2 -0
- package/dist/tokens/mappings.d.ts +1 -0
- package/dist/tokens/mappings.js +3 -0
- package/dist/tokens/service.js +27 -8
- package/dist/tools/extraction/index.d.ts +2 -0
- package/dist/tools/extraction/index.js +2 -0
- package/dist/tools/extraction/schema-utils.d.ts +6 -0
- package/dist/tools/extraction/schema-utils.js +237 -0
- package/dist/tools/extraction/views.d.ts +7 -0
- package/dist/tools/index.d.ts +5 -0
- package/dist/tools/index.js +5 -0
- package/dist/tools/registry/index.d.ts +2 -0
- package/dist/tools/registry/index.js +2 -0
- package/dist/tools/registry/service.d.ts +1 -0
- package/dist/tools/registry/service.js +1 -0
- package/dist/tools/registry/views.d.ts +1 -0
- package/dist/tools/registry/views.js +1 -0
- package/dist/tools/service.d.ts +2 -0
- package/dist/tools/service.js +1 -0
- package/dist/tools/utils.d.ts +2 -0
- package/dist/tools/utils.js +57 -0
- package/dist/tools/views.d.ts +1 -0
- package/dist/tools/views.js +1 -0
- package/dist/utils.d.ts +10 -1
- package/dist/utils.js +70 -3
- package/package.json +87 -26
- package/dist/dom/playground/process-dom.js +0 -5
- package/dist/dom/playground/test-accessibility.d.ts +0 -44
- package/dist/dom/playground/test-accessibility.js +0 -111
- /package/dist/{dom/playground/process-dom.d.ts → tools/extraction/views.js} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -14,11 +14,14 @@ export * from './screenshots/service.js';
|
|
|
14
14
|
export * from './controller/views.js';
|
|
15
15
|
export * from './controller/registry/service.js';
|
|
16
16
|
export * from './controller/service.js';
|
|
17
|
+
export { Tools } from './tools/service.js';
|
|
18
|
+
export type { ToolsOptions, ToolsActParams } from './tools/service.js';
|
|
17
19
|
export * from './filesystem/file-system.js';
|
|
18
20
|
export * from './agent/views.js';
|
|
19
21
|
export * from './telemetry/views.js';
|
|
20
22
|
export * from './telemetry/service.js';
|
|
21
23
|
export * from './llm/messages.js';
|
|
24
|
+
export * from './llm/models.js';
|
|
22
25
|
export * from './llm/views.js';
|
|
23
26
|
export * from './llm/base.js';
|
|
24
27
|
export * from './llm/exceptions.js';
|
|
@@ -29,3 +32,7 @@ export * from './agent/prompts.js';
|
|
|
29
32
|
export * from './agent/message-manager/service.js';
|
|
30
33
|
export * from './agent/service.js';
|
|
31
34
|
export * from './agent/message-manager/utils.js';
|
|
35
|
+
export * from './skills/index.js';
|
|
36
|
+
export * from './sandbox/index.js';
|
|
37
|
+
export * from './code-use/index.js';
|
|
38
|
+
export * from './skill-cli/index.js';
|
package/dist/index.js
CHANGED
|
@@ -16,11 +16,13 @@ export * from './screenshots/service.js';
|
|
|
16
16
|
export * from './controller/views.js';
|
|
17
17
|
export * from './controller/registry/service.js';
|
|
18
18
|
export * from './controller/service.js';
|
|
19
|
+
export { Tools } from './tools/service.js';
|
|
19
20
|
export * from './filesystem/file-system.js';
|
|
20
21
|
export * from './agent/views.js';
|
|
21
22
|
export * from './telemetry/views.js';
|
|
22
23
|
export * from './telemetry/service.js';
|
|
23
24
|
export * from './llm/messages.js';
|
|
25
|
+
export * from './llm/models.js';
|
|
24
26
|
export * from './llm/views.js';
|
|
25
27
|
export * from './llm/base.js';
|
|
26
28
|
export * from './llm/exceptions.js';
|
|
@@ -31,3 +33,7 @@ export * from './agent/prompts.js';
|
|
|
31
33
|
export * from './agent/message-manager/service.js';
|
|
32
34
|
export * from './agent/service.js';
|
|
33
35
|
export * from './agent/message-manager/utils.js';
|
|
36
|
+
export * from './skills/index.js';
|
|
37
|
+
export * from './sandbox/index.js';
|
|
38
|
+
export * from './code-use/index.js';
|
|
39
|
+
export * from './skill-cli/index.js';
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* Defines agent actions for Gmail integration including 2FA code retrieval,
|
|
4
4
|
* email reading, and authentication management.
|
|
5
5
|
*/
|
|
6
|
-
import type { Controller } from '../../controller/service.js';
|
|
7
6
|
import { GmailService } from './service.js';
|
|
7
|
+
import type { Tools } from '../../tools/service.js';
|
|
8
8
|
/**
|
|
9
|
-
* Register Gmail actions with the provided
|
|
9
|
+
* Register Gmail actions with the provided tools registry
|
|
10
10
|
*/
|
|
11
|
-
export declare function registerGmailActions(
|
|
11
|
+
export declare function registerGmailActions(tools: Tools, gmailService?: GmailService | null, accessToken?: string | null): Tools;
|
|
12
12
|
export declare const register_gmail_actions: typeof registerGmailActions;
|
|
@@ -25,9 +25,9 @@ const GetRecentEmailsParamsSchema = z.object({
|
|
|
25
25
|
.describe('Maximum number of emails to retrieve (1-50, default: 3)'),
|
|
26
26
|
});
|
|
27
27
|
/**
|
|
28
|
-
* Register Gmail actions with the provided
|
|
28
|
+
* Register Gmail actions with the provided tools registry
|
|
29
29
|
*/
|
|
30
|
-
export function registerGmailActions(
|
|
30
|
+
export function registerGmailActions(tools, gmailService, accessToken) {
|
|
31
31
|
// Use provided service or create a new one with access token if provided
|
|
32
32
|
if (gmailService) {
|
|
33
33
|
_gmailService = gmailService;
|
|
@@ -39,7 +39,7 @@ export function registerGmailActions(controller, gmailService, accessToken) {
|
|
|
39
39
|
_gmailService = new GmailService();
|
|
40
40
|
}
|
|
41
41
|
// Register get_recent_emails action
|
|
42
|
-
|
|
42
|
+
tools.registry.action('Get recent emails from the mailbox with a keyword to retrieve verification codes, OTP, 2FA tokens, magic links, or any recent email content. Keep your query a single keyword.', GetRecentEmailsParamsSchema)(async (params) => {
|
|
43
43
|
try {
|
|
44
44
|
if (!_gmailService) {
|
|
45
45
|
throw new Error('Gmail service not initialized');
|
|
@@ -107,7 +107,7 @@ export function registerGmailActions(controller, gmailService, accessToken) {
|
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
109
|
});
|
|
110
|
-
return
|
|
110
|
+
return tools;
|
|
111
111
|
}
|
|
112
112
|
// Backward compatibility export
|
|
113
113
|
export const register_gmail_actions = registerGmailActions;
|
|
@@ -1,14 +1,24 @@
|
|
|
1
|
+
import { type ClientOptions } from '@anthropic-ai/sdk';
|
|
1
2
|
import type { BaseChatModel, ChatInvokeOptions } from '../base.js';
|
|
2
3
|
import { ChatInvokeCompletion } from '../views.js';
|
|
3
4
|
import { type Message } from '../messages.js';
|
|
4
5
|
export interface ChatAnthropicOptions {
|
|
5
6
|
model?: string;
|
|
6
7
|
apiKey?: string;
|
|
8
|
+
authToken?: string;
|
|
7
9
|
baseURL?: string;
|
|
10
|
+
timeout?: number;
|
|
8
11
|
maxTokens?: number;
|
|
9
12
|
temperature?: number | null;
|
|
10
13
|
topP?: number | null;
|
|
14
|
+
seed?: number | null;
|
|
11
15
|
maxRetries?: number;
|
|
16
|
+
defaultHeaders?: Record<string, string>;
|
|
17
|
+
defaultQuery?: Record<string, string | undefined>;
|
|
18
|
+
fetchImplementation?: ClientOptions['fetch'];
|
|
19
|
+
fetchOptions?: ClientOptions['fetchOptions'];
|
|
20
|
+
removeMinItemsFromSchema?: boolean;
|
|
21
|
+
removeDefaultsFromSchema?: boolean;
|
|
12
22
|
}
|
|
13
23
|
export declare class ChatAnthropic implements BaseChatModel {
|
|
14
24
|
model: string;
|
|
@@ -17,9 +27,16 @@ export declare class ChatAnthropic implements BaseChatModel {
|
|
|
17
27
|
private maxTokens;
|
|
18
28
|
private temperature;
|
|
19
29
|
private topP;
|
|
20
|
-
|
|
30
|
+
private seed;
|
|
31
|
+
private removeMinItemsFromSchema;
|
|
32
|
+
private removeDefaultsFromSchema;
|
|
33
|
+
constructor(options?: string | ChatAnthropicOptions);
|
|
21
34
|
get name(): string;
|
|
22
35
|
get model_name(): string;
|
|
36
|
+
private getModelParams;
|
|
37
|
+
private getZodSchemaCandidate;
|
|
38
|
+
private parseOutput;
|
|
39
|
+
private getTextCompletion;
|
|
23
40
|
private getUsage;
|
|
24
41
|
ainvoke(messages: Message[], output_format?: undefined, options?: ChatInvokeOptions): Promise<ChatInvokeCompletion<string>>;
|
|
25
42
|
ainvoke<T>(messages: Message[], output_format: {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
-
import { zodToJsonSchema } from 'zod-to-json-schema';
|
|
1
|
+
import Anthropic, { APIConnectionError, APIError, RateLimitError, } from '@anthropic-ai/sdk';
|
|
3
2
|
import { ChatInvokeCompletion } from '../views.js';
|
|
4
|
-
import { SystemMessage } from '../messages.js';
|
|
5
3
|
import { AnthropicMessageSerializer } from './serializer.js';
|
|
6
4
|
import { ModelProviderError, ModelRateLimitError } from '../exceptions.js';
|
|
5
|
+
import { SchemaOptimizer, zodSchemaToJsonSchema } from '../schema.js';
|
|
7
6
|
export class ChatAnthropic {
|
|
8
7
|
model;
|
|
9
8
|
provider = 'anthropic';
|
|
@@ -11,16 +10,29 @@ export class ChatAnthropic {
|
|
|
11
10
|
maxTokens;
|
|
12
11
|
temperature;
|
|
13
12
|
topP;
|
|
13
|
+
seed;
|
|
14
|
+
removeMinItemsFromSchema;
|
|
15
|
+
removeDefaultsFromSchema;
|
|
14
16
|
constructor(options = {}) {
|
|
15
|
-
const
|
|
17
|
+
const normalizedOptions = typeof options === 'string' ? { model: options } : options;
|
|
18
|
+
const { model = 'claude-sonnet-4-20250514', apiKey = process.env.ANTHROPIC_API_KEY, authToken = process.env.ANTHROPIC_AUTH_TOKEN, baseURL, timeout, maxTokens = 8192, temperature = null, topP = null, seed = null, maxRetries = 10, defaultHeaders, defaultQuery, fetchImplementation, fetchOptions, removeMinItemsFromSchema = false, removeDefaultsFromSchema = false, } = normalizedOptions;
|
|
16
19
|
this.model = model;
|
|
17
20
|
this.maxTokens = maxTokens;
|
|
18
21
|
this.temperature = temperature;
|
|
19
22
|
this.topP = topP;
|
|
23
|
+
this.seed = seed;
|
|
24
|
+
this.removeMinItemsFromSchema = removeMinItemsFromSchema;
|
|
25
|
+
this.removeDefaultsFromSchema = removeDefaultsFromSchema;
|
|
20
26
|
this.client = new Anthropic({
|
|
21
27
|
apiKey,
|
|
28
|
+
authToken,
|
|
22
29
|
baseURL,
|
|
30
|
+
timeout,
|
|
23
31
|
maxRetries,
|
|
32
|
+
defaultHeaders,
|
|
33
|
+
defaultQuery,
|
|
34
|
+
...(fetchImplementation ? { fetch: fetchImplementation } : {}),
|
|
35
|
+
...(fetchOptions ? { fetchOptions } : {}),
|
|
24
36
|
});
|
|
25
37
|
}
|
|
26
38
|
get name() {
|
|
@@ -29,6 +41,54 @@ export class ChatAnthropic {
|
|
|
29
41
|
get model_name() {
|
|
30
42
|
return this.model;
|
|
31
43
|
}
|
|
44
|
+
getModelParams() {
|
|
45
|
+
const modelParams = {};
|
|
46
|
+
if (this.temperature !== null) {
|
|
47
|
+
modelParams.temperature = this.temperature;
|
|
48
|
+
}
|
|
49
|
+
if (this.topP !== null) {
|
|
50
|
+
modelParams.top_p = this.topP;
|
|
51
|
+
}
|
|
52
|
+
if (this.seed !== null) {
|
|
53
|
+
modelParams.seed = this.seed;
|
|
54
|
+
}
|
|
55
|
+
return modelParams;
|
|
56
|
+
}
|
|
57
|
+
getZodSchemaCandidate(output_format) {
|
|
58
|
+
const output = output_format;
|
|
59
|
+
if (output &&
|
|
60
|
+
typeof output === 'object' &&
|
|
61
|
+
typeof output.safeParse === 'function' &&
|
|
62
|
+
typeof output.parse === 'function') {
|
|
63
|
+
return output;
|
|
64
|
+
}
|
|
65
|
+
if (output &&
|
|
66
|
+
typeof output === 'object' &&
|
|
67
|
+
output.schema &&
|
|
68
|
+
typeof output.schema.safeParse === 'function' &&
|
|
69
|
+
typeof output.schema.parse === 'function') {
|
|
70
|
+
return output.schema;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
parseOutput(output_format, payload) {
|
|
75
|
+
const output = output_format;
|
|
76
|
+
if (output &&
|
|
77
|
+
typeof output === 'object' &&
|
|
78
|
+
output.schema &&
|
|
79
|
+
typeof output.schema.parse === 'function') {
|
|
80
|
+
return output.schema.parse(payload);
|
|
81
|
+
}
|
|
82
|
+
return output.parse(payload);
|
|
83
|
+
}
|
|
84
|
+
getTextCompletion(response) {
|
|
85
|
+
const textBlock = response.content.find((block) => block.type === 'text');
|
|
86
|
+
if (textBlock && textBlock.type === 'text') {
|
|
87
|
+
return textBlock.text;
|
|
88
|
+
}
|
|
89
|
+
const firstBlock = response.content[0];
|
|
90
|
+
return firstBlock ? String(firstBlock) : '';
|
|
91
|
+
}
|
|
32
92
|
getUsage(response) {
|
|
33
93
|
const cacheReadTokens = response.usage.cache_read_input_tokens ?? 0;
|
|
34
94
|
const cacheCreationTokens = response.usage.cache_creation_input_tokens ?? 0;
|
|
@@ -43,84 +103,92 @@ export class ChatAnthropic {
|
|
|
43
103
|
}
|
|
44
104
|
async ainvoke(messages, output_format, options = {}) {
|
|
45
105
|
const serializer = new AnthropicMessageSerializer();
|
|
46
|
-
const [anthropicMessages] = serializer.serializeMessages(messages);
|
|
47
|
-
const
|
|
48
|
-
const system = systemMessage ? systemMessage.text : undefined;
|
|
106
|
+
const [anthropicMessages, systemPrompt] = serializer.serializeMessages(messages);
|
|
107
|
+
const zodSchemaCandidate = this.getZodSchemaCandidate(output_format);
|
|
49
108
|
let tools = undefined;
|
|
50
109
|
let toolChoice = undefined;
|
|
51
|
-
if (output_format &&
|
|
52
|
-
// Assuming output_format is a Zod schema wrapper
|
|
110
|
+
if (output_format && zodSchemaCandidate) {
|
|
53
111
|
try {
|
|
54
|
-
const
|
|
112
|
+
const rawJsonSchema = zodSchemaToJsonSchema(zodSchemaCandidate, {
|
|
55
113
|
name: 'Response',
|
|
56
114
|
target: 'jsonSchema7',
|
|
57
115
|
});
|
|
116
|
+
const optimizedJsonSchema = SchemaOptimizer.createOptimizedJsonSchema(rawJsonSchema, {
|
|
117
|
+
removeMinItems: this.removeMinItemsFromSchema,
|
|
118
|
+
removeDefaults: this.removeDefaultsFromSchema,
|
|
119
|
+
});
|
|
120
|
+
delete optimizedJsonSchema.title;
|
|
121
|
+
const toolName = output_format?.name || 'response';
|
|
58
122
|
tools = [
|
|
59
123
|
{
|
|
60
|
-
name:
|
|
61
|
-
description:
|
|
62
|
-
input_schema:
|
|
124
|
+
name: toolName,
|
|
125
|
+
description: `Extract information in the format of ${toolName}`,
|
|
126
|
+
input_schema: optimizedJsonSchema,
|
|
127
|
+
cache_control: { type: 'ephemeral' },
|
|
63
128
|
},
|
|
64
129
|
];
|
|
65
|
-
toolChoice = { type: 'tool', name:
|
|
130
|
+
toolChoice = { type: 'tool', name: toolName };
|
|
66
131
|
}
|
|
67
132
|
catch (e) {
|
|
68
133
|
console.warn('Failed to convert output_format to JSON schema for Anthropic', e);
|
|
69
134
|
}
|
|
70
135
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
136
|
+
const requestPayload = {
|
|
137
|
+
model: this.model,
|
|
138
|
+
max_tokens: this.maxTokens,
|
|
139
|
+
messages: anthropicMessages,
|
|
140
|
+
...this.getModelParams(),
|
|
141
|
+
};
|
|
142
|
+
if (systemPrompt !== undefined) {
|
|
143
|
+
requestPayload.system = systemPrompt;
|
|
78
144
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (index === tools.length - 1) {
|
|
83
|
-
return {
|
|
84
|
-
...tool,
|
|
85
|
-
cache_control: { type: 'ephemeral' },
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return tool;
|
|
89
|
-
});
|
|
145
|
+
if (tools?.length) {
|
|
146
|
+
requestPayload.tools = tools;
|
|
147
|
+
requestPayload.tool_choice = toolChoice;
|
|
90
148
|
}
|
|
91
149
|
try {
|
|
92
|
-
const response = await this.client.messages.create({
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
150
|
+
const response = await this.client.messages.create(requestPayload, options.signal ? { signal: options.signal } : undefined);
|
|
151
|
+
let completion = this.getTextCompletion(response);
|
|
152
|
+
if (output_format) {
|
|
153
|
+
const toolUseBlock = response.content.find((block) => block.type === 'tool_use');
|
|
154
|
+
if (toolUseBlock && toolUseBlock.type === 'tool_use') {
|
|
155
|
+
try {
|
|
156
|
+
completion = this.parseOutput(output_format, toolUseBlock.input);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
if (typeof toolUseBlock.input === 'string') {
|
|
160
|
+
completion = this.parseOutput(output_format, JSON.parse(toolUseBlock.input));
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else if (tools?.length) {
|
|
168
|
+
throw new ModelProviderError('Expected tool use in response but none found', 502, this.model);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
completion = this.parseOutput(output_format, completion);
|
|
172
|
+
}
|
|
106
173
|
}
|
|
107
174
|
else {
|
|
108
|
-
|
|
109
|
-
const textBlock = response.content.find((block) => block.type === 'text');
|
|
110
|
-
completion = textBlock ? textBlock.text : '';
|
|
175
|
+
completion = this.getTextCompletion(response);
|
|
111
176
|
}
|
|
112
177
|
const usage = this.getUsage(response);
|
|
113
|
-
|
|
178
|
+
const stopReason = response.stop_reason ?? null;
|
|
179
|
+
return new ChatInvokeCompletion(completion, usage, null, null, stopReason);
|
|
114
180
|
}
|
|
115
181
|
catch (error) {
|
|
116
|
-
|
|
117
|
-
if (error?.status === 429) {
|
|
182
|
+
if (error instanceof RateLimitError || error?.status === 429) {
|
|
118
183
|
throw new ModelRateLimitError(error?.message ?? 'Rate limit exceeded', 429, this.model);
|
|
119
184
|
}
|
|
120
|
-
if (error
|
|
121
|
-
throw new ModelProviderError(error?.message ?? '
|
|
185
|
+
if (error instanceof APIConnectionError) {
|
|
186
|
+
throw new ModelProviderError(error?.message ?? 'Connection error', 502, this.model);
|
|
187
|
+
}
|
|
188
|
+
if (error instanceof APIError) {
|
|
189
|
+
throw new ModelProviderError(error?.message ?? 'Anthropic API error', error?.status ?? 502, this.model);
|
|
122
190
|
}
|
|
123
|
-
throw new ModelProviderError(error?.message ?? String(error), error?.status ??
|
|
191
|
+
throw new ModelProviderError(error?.message ?? String(error), error?.status ?? 502, this.model);
|
|
124
192
|
}
|
|
125
193
|
}
|
|
126
194
|
}
|
|
@@ -58,6 +58,8 @@ export declare class AnthropicMessageSerializer {
|
|
|
58
58
|
* Parse base64 data URL to extract media type and data
|
|
59
59
|
*/
|
|
60
60
|
private _parseBase64Url;
|
|
61
|
+
private _cloneMessage;
|
|
62
|
+
private _cloneContent;
|
|
61
63
|
/**
|
|
62
64
|
* Clean cache settings so only the last cache=true message remains cached
|
|
63
65
|
*
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* - Subsequent: 1,000 tokens @ $3/M + 9,000 tokens @ $0.30/M = $0.0057
|
|
17
17
|
* - Savings: 81% cost reduction
|
|
18
18
|
*/
|
|
19
|
-
import { AssistantMessage, ContentPartImageParam, ContentPartTextParam, UserMessage, SystemMessage, } from '../messages.js';
|
|
19
|
+
import { AssistantMessage, ContentPartImageParam, ContentPartRefusalParam, ContentPartTextParam, FunctionCall, ImageURL, ToolCall, UserMessage, SystemMessage, } from '../messages.js';
|
|
20
20
|
export class AnthropicMessageSerializer {
|
|
21
21
|
/**
|
|
22
22
|
* Serialize a list of messages, extracting any system message
|
|
@@ -26,7 +26,7 @@ export class AnthropicMessageSerializer {
|
|
|
26
26
|
*/
|
|
27
27
|
serializeMessages(messages) {
|
|
28
28
|
// Make deep copies to avoid modifying originals
|
|
29
|
-
const messagesCopy = messages.map((
|
|
29
|
+
const messagesCopy = messages.map((message) => this._cloneMessage(message));
|
|
30
30
|
// Separate system messages from normal messages
|
|
31
31
|
const normalMessages = [];
|
|
32
32
|
let systemMessage = null;
|
|
@@ -79,7 +79,7 @@ export class AnthropicMessageSerializer {
|
|
|
79
79
|
content.push({
|
|
80
80
|
type: 'text',
|
|
81
81
|
text: message.content,
|
|
82
|
-
...(message.cache
|
|
82
|
+
...(message.cache && !message.tool_calls?.length
|
|
83
83
|
? { cache_control: this._serializeCacheControl(true) }
|
|
84
84
|
: {}),
|
|
85
85
|
});
|
|
@@ -99,11 +99,18 @@ export class AnthropicMessageSerializer {
|
|
|
99
99
|
message.tool_calls.forEach((toolCall, idx, arr) => {
|
|
100
100
|
const isLastToolCall = idx === arr.length - 1;
|
|
101
101
|
const useCache = message.cache && isLastToolCall;
|
|
102
|
+
let toolInput;
|
|
103
|
+
try {
|
|
104
|
+
toolInput = JSON.parse(toolCall.functionCall.arguments);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
toolInput = { arguments: toolCall.functionCall.arguments };
|
|
108
|
+
}
|
|
102
109
|
content.push({
|
|
103
110
|
type: 'tool_use',
|
|
104
111
|
id: toolCall.id,
|
|
105
112
|
name: toolCall.functionCall.name,
|
|
106
|
-
input:
|
|
113
|
+
input: toolInput,
|
|
107
114
|
...(useCache
|
|
108
115
|
? { cache_control: this._serializeCacheControl(true) }
|
|
109
116
|
: {}),
|
|
@@ -120,9 +127,19 @@ export class AnthropicMessageSerializer {
|
|
|
120
127
|
: {}),
|
|
121
128
|
});
|
|
122
129
|
}
|
|
130
|
+
const normalizedContent = (() => {
|
|
131
|
+
if (message.cache || content.length > 1) {
|
|
132
|
+
return content;
|
|
133
|
+
}
|
|
134
|
+
const first = content[0];
|
|
135
|
+
if (first && first.type === 'text' && !first.cache_control) {
|
|
136
|
+
return first.text;
|
|
137
|
+
}
|
|
138
|
+
return content;
|
|
139
|
+
})();
|
|
123
140
|
return {
|
|
124
141
|
role: 'assistant',
|
|
125
|
-
content:
|
|
142
|
+
content: normalizedContent,
|
|
126
143
|
};
|
|
127
144
|
}
|
|
128
145
|
throw new Error(`Unknown message type or unhandled role: ${message.role}`);
|
|
@@ -238,7 +255,7 @@ export class AnthropicMessageSerializer {
|
|
|
238
255
|
if (!header || !data) {
|
|
239
256
|
throw new Error(`Invalid base64 URL format: ${url}`);
|
|
240
257
|
}
|
|
241
|
-
let mediaType = header.split(';')[0]?.replace('data:', '') || 'image/
|
|
258
|
+
let mediaType = header.split(';')[0]?.replace('data:', '') || 'image/jpeg';
|
|
242
259
|
// Ensure it's a supported media type
|
|
243
260
|
const supportedTypes = [
|
|
244
261
|
'image/jpeg',
|
|
@@ -247,11 +264,66 @@ export class AnthropicMessageSerializer {
|
|
|
247
264
|
'image/webp',
|
|
248
265
|
];
|
|
249
266
|
if (!supportedTypes.includes(mediaType)) {
|
|
250
|
-
// Default to
|
|
251
|
-
mediaType = 'image/
|
|
267
|
+
// Default to jpeg if not recognized
|
|
268
|
+
mediaType = 'image/jpeg';
|
|
252
269
|
}
|
|
253
270
|
return [mediaType, data];
|
|
254
271
|
}
|
|
272
|
+
_cloneMessage(message) {
|
|
273
|
+
if (message instanceof UserMessage) {
|
|
274
|
+
const clone = new UserMessage(this._cloneContent(message.content), message.name);
|
|
275
|
+
clone.cache = message.cache;
|
|
276
|
+
return clone;
|
|
277
|
+
}
|
|
278
|
+
if (message instanceof SystemMessage) {
|
|
279
|
+
const clone = new SystemMessage(typeof message.content === 'string'
|
|
280
|
+
? message.content
|
|
281
|
+
: message.content.map((part) => new ContentPartTextParam(part.text)), message.name);
|
|
282
|
+
clone.cache = message.cache;
|
|
283
|
+
return clone;
|
|
284
|
+
}
|
|
285
|
+
if (message instanceof AssistantMessage) {
|
|
286
|
+
const clone = new AssistantMessage({
|
|
287
|
+
content: message.content === null
|
|
288
|
+
? null
|
|
289
|
+
: typeof message.content === 'string'
|
|
290
|
+
? message.content
|
|
291
|
+
: message.content.map((part) => {
|
|
292
|
+
if (part instanceof ContentPartTextParam) {
|
|
293
|
+
return new ContentPartTextParam(part.text);
|
|
294
|
+
}
|
|
295
|
+
if (part instanceof ContentPartRefusalParam) {
|
|
296
|
+
return new ContentPartRefusalParam(part.refusal);
|
|
297
|
+
}
|
|
298
|
+
if (part instanceof ContentPartImageParam) {
|
|
299
|
+
return new ContentPartImageParam(new ImageURL(part.image_url.url, part.image_url.detail, part.image_url.media_type));
|
|
300
|
+
}
|
|
301
|
+
return part;
|
|
302
|
+
}),
|
|
303
|
+
tool_calls: message.tool_calls
|
|
304
|
+
? message.tool_calls.map((toolCall) => new ToolCall(toolCall.id, new FunctionCall(toolCall.functionCall.name, toolCall.functionCall.arguments)))
|
|
305
|
+
: null,
|
|
306
|
+
refusal: message.refusal,
|
|
307
|
+
});
|
|
308
|
+
clone.cache = message.cache;
|
|
309
|
+
return clone;
|
|
310
|
+
}
|
|
311
|
+
return message;
|
|
312
|
+
}
|
|
313
|
+
_cloneContent(content) {
|
|
314
|
+
if (typeof content === 'string') {
|
|
315
|
+
return content;
|
|
316
|
+
}
|
|
317
|
+
return content.map((part) => {
|
|
318
|
+
if (part instanceof ContentPartTextParam) {
|
|
319
|
+
return new ContentPartTextParam(part.text);
|
|
320
|
+
}
|
|
321
|
+
if (part instanceof ContentPartRefusalParam) {
|
|
322
|
+
return new ContentPartRefusalParam(part.refusal);
|
|
323
|
+
}
|
|
324
|
+
return new ContentPartImageParam(new ImageURL(part.image_url.url, part.image_url.detail, part.image_url.media_type));
|
|
325
|
+
});
|
|
326
|
+
}
|
|
255
327
|
/**
|
|
256
328
|
* Clean cache settings so only the last cache=true message remains cached
|
|
257
329
|
*
|
|
@@ -263,7 +335,7 @@ export class AnthropicMessageSerializer {
|
|
|
263
335
|
return messages;
|
|
264
336
|
}
|
|
265
337
|
// Create deep copies to avoid modifying originals
|
|
266
|
-
const cleanedMessages = messages.map((msg) => (
|
|
338
|
+
const cleanedMessages = messages.map((msg) => this._cloneMessage(msg));
|
|
267
339
|
// Find the last message with cache=true
|
|
268
340
|
let lastCacheIndex = -1;
|
|
269
341
|
for (let i = cleanedMessages.length - 1; i >= 0; i--) {
|
|
@@ -26,6 +26,14 @@ export interface ChatAnthropicBedrockConfig {
|
|
|
26
26
|
model?: string;
|
|
27
27
|
/** AWS region, defaults to us-east-1 */
|
|
28
28
|
region?: string;
|
|
29
|
+
/** AWS access key ID */
|
|
30
|
+
awsAccessKeyId?: string;
|
|
31
|
+
/** AWS secret access key */
|
|
32
|
+
awsSecretAccessKey?: string;
|
|
33
|
+
/** AWS session token */
|
|
34
|
+
awsSessionToken?: string;
|
|
35
|
+
/** Retry attempts */
|
|
36
|
+
maxRetries?: number;
|
|
29
37
|
/** Maximum tokens to generate */
|
|
30
38
|
max_tokens?: number;
|
|
31
39
|
/** Temperature for sampling (0-1) */
|
|
@@ -36,6 +44,10 @@ export interface ChatAnthropicBedrockConfig {
|
|
|
36
44
|
top_k?: number | null;
|
|
37
45
|
/** Stop sequences */
|
|
38
46
|
stop_sequences?: string[] | null;
|
|
47
|
+
/** Remove minItems from schema for provider compatibility */
|
|
48
|
+
removeMinItemsFromSchema?: boolean;
|
|
49
|
+
/** Remove default from schema for provider compatibility */
|
|
50
|
+
removeDefaultsFromSchema?: boolean;
|
|
39
51
|
}
|
|
40
52
|
export declare class ChatAnthropicBedrock implements BaseChatModel {
|
|
41
53
|
model: string;
|
|
@@ -46,10 +58,15 @@ export declare class ChatAnthropicBedrock implements BaseChatModel {
|
|
|
46
58
|
private top_p;
|
|
47
59
|
private top_k;
|
|
48
60
|
private stop_sequences;
|
|
61
|
+
private removeMinItemsFromSchema;
|
|
62
|
+
private removeDefaultsFromSchema;
|
|
49
63
|
constructor(config?: ChatAnthropicBedrockConfig);
|
|
50
64
|
get name(): string;
|
|
51
65
|
get model_name(): string;
|
|
52
66
|
private _getInferenceParams;
|
|
67
|
+
private getZodSchemaCandidate;
|
|
68
|
+
private parseOutput;
|
|
69
|
+
private getTextCompletion;
|
|
53
70
|
ainvoke(messages: Message[], output_format?: undefined, options?: ChatInvokeOptions): Promise<ChatInvokeCompletion<string>>;
|
|
54
71
|
ainvoke<T>(messages: Message[], output_format: {
|
|
55
72
|
parse: (input: string) => T;
|