@zimtsui/brainswitch 0.0.55 → 0.0.57
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/DEVELOPMENT.md +13 -14
- package/README.md +110 -62
- package/assets/verbatim/declarations.handlebars +11 -0
- package/assets/verbatim/instruction.md +49 -0
- package/build/adaptor.d.ts +38 -7
- package/build/adaptor.js +35 -37
- package/build/adaptor.js.map +1 -1
- package/build/api-types/anthropic/billing.d.ts +12 -0
- package/build/api-types/anthropic/billing.js +14 -0
- package/build/api-types/anthropic/billing.js.map +1 -0
- package/build/api-types/anthropic/tool-codec.d.ts +16 -0
- package/build/api-types/anthropic/tool-codec.js +58 -0
- package/build/api-types/anthropic/tool-codec.js.map +1 -0
- package/build/api-types/google/billing.d.ts +12 -0
- package/build/api-types/google/billing.js +23 -0
- package/build/api-types/google/billing.js.map +1 -0
- package/build/api-types/google/restful-request.d.ts +8 -0
- package/build/api-types/google/restful-request.js +1 -0
- package/build/api-types/google/restful-request.js.map +1 -0
- package/build/api-types/google/tool-codec.d.ts +16 -0
- package/build/api-types/{google.js → google/tool-codec.js} +32 -29
- package/build/api-types/google/tool-codec.js.map +1 -0
- package/build/api-types/openai-chatcompletions/billing.d.ts +12 -0
- package/build/api-types/openai-chatcompletions/billing.js +14 -0
- package/build/api-types/openai-chatcompletions/billing.js.map +1 -0
- package/build/api-types/openai-chatcompletions/tool-codec.d.ts +17 -0
- package/build/api-types/openai-chatcompletions/tool-codec.js +80 -0
- package/build/api-types/openai-chatcompletions/tool-codec.js.map +1 -0
- package/build/api-types/openai-responses/billing.d.ts +12 -0
- package/build/api-types/openai-responses/billing.js +14 -0
- package/build/api-types/openai-responses/billing.js.map +1 -0
- package/build/api-types/openai-responses/tool-codec.d.ts +16 -0
- package/build/api-types/openai-responses/tool-codec.js +76 -0
- package/build/api-types/openai-responses/tool-codec.js.map +1 -0
- package/build/assets.d.ts +7 -0
- package/build/assets.js +8 -0
- package/build/assets.js.map +1 -0
- package/build/compatible-engine/session.d.ts +67 -0
- package/build/compatible-engine/session.js +92 -0
- package/build/compatible-engine/session.js.map +1 -0
- package/build/compatible-engine/structuring.d.ts +36 -0
- package/build/compatible-engine/structuring.js +34 -0
- package/build/compatible-engine/structuring.js.map +1 -0
- package/build/compatible-engine/validation.d.ts +21 -0
- package/build/compatible-engine/validation.js +82 -0
- package/build/compatible-engine/validation.js.map +1 -0
- package/build/compatible-engine.d/agentloop.d.ts +12 -0
- package/build/compatible-engine.d/agentloop.js +52 -0
- package/build/compatible-engine.d/agentloop.js.map +1 -0
- package/build/compatible-engine.d/aliyun/transport.d.ts +12 -0
- package/build/compatible-engine.d/aliyun/transport.js +7 -0
- package/build/compatible-engine.d/aliyun/transport.js.map +1 -0
- package/build/compatible-engine.d/aliyun.d.ts +24 -0
- package/build/compatible-engine.d/aliyun.js +46 -0
- package/build/compatible-engine.d/aliyun.js.map +1 -0
- package/build/compatible-engine.d/anthropic/choice-codec.d.ts +5 -0
- package/build/compatible-engine.d/anthropic/choice-codec.js +26 -0
- package/build/compatible-engine.d/anthropic/choice-codec.js.map +1 -0
- package/build/compatible-engine.d/anthropic/message-codec.d.ts +36 -0
- package/build/compatible-engine.d/anthropic/message-codec.js +86 -0
- package/build/compatible-engine.d/anthropic/message-codec.js.map +1 -0
- package/build/compatible-engine.d/anthropic/transport.d.ts +34 -0
- package/build/compatible-engine.d/anthropic/transport.js +150 -0
- package/build/compatible-engine.d/anthropic/transport.js.map +1 -0
- package/build/compatible-engine.d/anthropic.d.ts +26 -0
- package/build/compatible-engine.d/anthropic.js +46 -0
- package/build/compatible-engine.d/anthropic.js.map +1 -0
- package/build/compatible-engine.d/google/choice-codec.d.ts +5 -0
- package/build/compatible-engine.d/google/choice-codec.js +27 -0
- package/build/compatible-engine.d/google/choice-codec.js.map +1 -0
- package/build/compatible-engine.d/google/message-codec.d.ts +36 -0
- package/build/compatible-engine.d/google/message-codec.js +94 -0
- package/build/compatible-engine.d/google/message-codec.js.map +1 -0
- package/build/compatible-engine.d/google/transport.d.ts +30 -0
- package/build/compatible-engine.d/google/transport.js +78 -0
- package/build/compatible-engine.d/google/transport.js.map +1 -0
- package/build/compatible-engine.d/google.d.ts +26 -0
- package/build/compatible-engine.d/google.js +50 -0
- package/build/compatible-engine.d/google.js.map +1 -0
- package/build/compatible-engine.d/openai-chatcompletions/choice-codec.d.ts +5 -0
- package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js +31 -0
- package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js.map +1 -0
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.d.ts +24 -0
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js +71 -0
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js.map +1 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.d.ts +38 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js +77 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js.map +1 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.d.ts +37 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js +190 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js.map +1 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.d.ts +10 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.js +11 -0
- package/build/compatible-engine.d/openai-chatcompletions/transport.js.map +1 -0
- package/build/compatible-engine.d/openai-chatcompletions.d.ts +17 -0
- package/build/compatible-engine.d/openai-chatcompletions.js +14 -0
- package/build/compatible-engine.d/openai-chatcompletions.js.map +1 -0
- package/build/compatible-engine.d/openai-responses/choice-codec.d.ts +5 -0
- package/build/compatible-engine.d/openai-responses/choice-codec.js +30 -0
- package/build/compatible-engine.d/openai-responses/choice-codec.js.map +1 -0
- package/build/compatible-engine.d/openai-responses/message-codec.d.ts +36 -0
- package/build/compatible-engine.d/openai-responses/message-codec.js +96 -0
- package/build/compatible-engine.d/openai-responses/message-codec.js.map +1 -0
- package/build/compatible-engine.d/openai-responses/transport.d.ts +35 -0
- package/build/compatible-engine.d/openai-responses/transport.js +80 -0
- package/build/compatible-engine.d/openai-responses/transport.js.map +1 -0
- package/build/compatible-engine.d/openai-responses.d.ts +26 -0
- package/build/compatible-engine.d/openai-responses.js +46 -0
- package/build/compatible-engine.d/openai-responses.js.map +1 -0
- package/build/compatible-engine.d.ts +17 -46
- package/build/compatible-engine.js +20 -58
- package/build/compatible-engine.js.map +1 -1
- package/build/config.d.ts +1 -0
- package/build/config.js +0 -1
- package/build/config.js.map +1 -1
- package/build/endpoint-spec.d.ts +1 -0
- package/build/endpoint-spec.js +2 -1
- package/build/endpoint-spec.js.map +1 -1
- package/build/engine/session.d.ts +7 -0
- package/build/engine/session.js +1 -0
- package/build/engine/session.js.map +1 -0
- package/build/engine/transport.d.ts +5 -0
- package/build/engine/transport.js +1 -0
- package/build/engine/transport.js.map +1 -0
- package/build/engine/validation.d.ts +9 -0
- package/build/engine/validation.js +1 -0
- package/build/engine/validation.js.map +1 -0
- package/build/engine.d.ts +60 -29
- package/build/engine.js +86 -19
- package/build/engine.js.map +1 -1
- package/build/exports.d.ts +4 -3
- package/build/exports.js +4 -3
- package/build/exports.js.map +1 -1
- package/build/function.d.ts +79 -54
- package/build/function.js +30 -46
- package/build/function.js.map +1 -1
- package/build/inference-context.d.ts +2 -2
- package/build/inference-context.js +0 -1
- package/build/inference-context.js.map +1 -1
- package/build/native-engines.d/google/message-codec.d.ts +23 -0
- package/build/native-engines.d/google/message-codec.js +67 -0
- package/build/native-engines.d/google/message-codec.js.map +1 -0
- package/build/native-engines.d/google/session.d.ts +45 -51
- package/build/native-engines.d/google/session.js +84 -80
- package/build/native-engines.d/google/session.js.map +1 -1
- package/build/native-engines.d/google/transport.d.ts +34 -0
- package/build/native-engines.d/google/transport.js +81 -0
- package/build/native-engines.d/google/transport.js.map +1 -0
- package/build/native-engines.d/google/validation.d.ts +22 -0
- package/build/native-engines.d/google/validation.js +20 -0
- package/build/native-engines.d/google/validation.js.map +1 -0
- package/build/native-engines.d/google.d/agentloop.d.ts +12 -0
- package/build/native-engines.d/google.d/agentloop.js +57 -0
- package/build/native-engines.d/google.d/agentloop.js.map +1 -0
- package/build/native-engines.d/google.d/exports.d.ts +3 -0
- package/build/native-engines.d/google.d/exports.js +4 -0
- package/build/native-engines.d/google.d/exports.js.map +1 -0
- package/build/native-engines.d/google.d.ts +42 -0
- package/build/native-engines.d/google.js +83 -0
- package/build/native-engines.d/google.js.map +1 -0
- package/build/native-engines.d/openai-responses/choice-codec.d.ts +5 -0
- package/build/native-engines.d/openai-responses/choice-codec.js +30 -0
- package/build/native-engines.d/openai-responses/choice-codec.js.map +1 -0
- package/build/native-engines.d/openai-responses/message-codec.d.ts +23 -0
- package/build/native-engines.d/openai-responses/message-codec.js +75 -0
- package/build/native-engines.d/openai-responses/message-codec.js.map +1 -0
- package/build/native-engines.d/openai-responses/session.d.ts +47 -44
- package/build/native-engines.d/openai-responses/session.js +73 -103
- package/build/native-engines.d/openai-responses/session.js.map +1 -1
- package/build/native-engines.d/openai-responses/structuring.d.ts +19 -0
- package/build/native-engines.d/openai-responses/structuring.js +19 -0
- package/build/native-engines.d/openai-responses/structuring.js.map +1 -0
- package/build/native-engines.d/openai-responses/tool.d.ts +14 -11
- package/build/native-engines.d/openai-responses/tool.js +1 -12
- package/build/native-engines.d/openai-responses/tool.js.map +1 -1
- package/build/native-engines.d/openai-responses/transport.d.ts +35 -0
- package/build/native-engines.d/openai-responses/transport.js +84 -0
- package/build/native-engines.d/openai-responses/transport.js.map +1 -0
- package/build/native-engines.d/openai-responses/validation.d.ts +20 -0
- package/build/native-engines.d/openai-responses/validation.js +80 -0
- package/build/native-engines.d/openai-responses/validation.js.map +1 -0
- package/build/native-engines.d/openai-responses.d/agentloop.d.ts +12 -0
- package/build/native-engines.d/openai-responses.d/agentloop.js +61 -0
- package/build/native-engines.d/openai-responses.d/agentloop.js.map +1 -0
- package/build/native-engines.d/{google → openai-responses.d}/exports.d.ts +1 -2
- package/build/native-engines.d/{google → openai-responses.d}/exports.js +1 -2
- package/build/native-engines.d/openai-responses.d/exports.js.map +1 -0
- package/build/native-engines.d/openai-responses.d.ts +44 -0
- package/build/native-engines.d/openai-responses.js +78 -0
- package/build/native-engines.d/openai-responses.js.map +1 -0
- package/build/throttle.d.ts +5 -4
- package/build/throttle.js +17 -14
- package/build/throttle.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/verbatim/codec/declarations.d.ts +2 -0
- package/build/verbatim/codec/declarations.js +22 -0
- package/build/verbatim/codec/declarations.js.map +1 -0
- package/build/verbatim/codec/request.d.ts +7 -0
- package/build/verbatim/codec/request.js +53 -0
- package/build/verbatim/codec/request.js.map +1 -0
- package/build/verbatim/codec.d.ts +2 -0
- package/build/verbatim/codec.js +3 -0
- package/build/verbatim/codec.js.map +1 -0
- package/build/verbatim.d.ts +74 -0
- package/build/verbatim.js +17 -0
- package/build/verbatim.js.map +1 -0
- package/examples/agent.ts +65 -0
- package/examples/chat.ts +24 -0
- package/examples/config.ts +24 -0
- package/examples/tsconfig.json +34 -0
- package/examples/verbatim.ts +44 -0
- package/package.json +10 -9
- package/build/agentloop.d.ts +0 -12
- package/build/agentloop.js +0 -43
- package/build/agentloop.js.map +0 -1
- package/build/api-types/anthropic.d.ts +0 -29
- package/build/api-types/anthropic.js +0 -93
- package/build/api-types/anthropic.js.map +0 -1
- package/build/api-types/google.d.ts +0 -28
- package/build/api-types/google.js.map +0 -1
- package/build/api-types/openai-chat-completions.d.ts +0 -31
- package/build/api-types/openai-chat-completions.js +0 -113
- package/build/api-types/openai-chat-completions.js.map +0 -1
- package/build/api-types/openai-responses.d.ts +0 -23
- package/build/api-types/openai-responses.js +0 -71
- package/build/api-types/openai-responses.js.map +0 -1
- package/build/compatible-engines.d/aliyun.d.ts +0 -69
- package/build/compatible-engines.d/aliyun.js +0 -149
- package/build/compatible-engines.d/aliyun.js.map +0 -1
- package/build/compatible-engines.d/anthropic.d.ts +0 -78
- package/build/compatible-engines.d/anthropic.js +0 -312
- package/build/compatible-engines.d/anthropic.js.map +0 -1
- package/build/compatible-engines.d/google.d.ts +0 -83
- package/build/compatible-engines.d/google.js +0 -279
- package/build/compatible-engines.d/google.js.map +0 -1
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.d.ts +0 -15
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.js +0 -75
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.js.map +0 -1
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.d.ts +0 -21
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.js +0 -186
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.js.map +0 -1
- package/build/compatible-engines.d/openai-chatcompletions.d.ts +0 -27
- package/build/compatible-engines.d/openai-chatcompletions.js +0 -76
- package/build/compatible-engines.d/openai-chatcompletions.js.map +0 -1
- package/build/compatible-engines.d/openai-responses.d.ts +0 -93
- package/build/compatible-engines.d/openai-responses.js +0 -300
- package/build/compatible-engines.d/openai-responses.js.map +0 -1
- package/build/frontmatter.d.ts +0 -5
- package/build/frontmatter.js +0 -13
- package/build/frontmatter.js.map +0 -1
- package/build/native-engines.d/google/agentloop.d.ts +0 -12
- package/build/native-engines.d/google/agentloop.js +0 -42
- package/build/native-engines.d/google/agentloop.js.map +0 -1
- package/build/native-engines.d/google/engine.d.ts +0 -110
- package/build/native-engines.d/google/engine.js +0 -314
- package/build/native-engines.d/google/engine.js.map +0 -1
- package/build/native-engines.d/google/exports.js.map +0 -1
- package/build/native-engines.d/openai-responses/agentloop.d.ts +0 -13
- package/build/native-engines.d/openai-responses/agentloop.js +0 -53
- package/build/native-engines.d/openai-responses/agentloop.js.map +0 -1
- package/build/native-engines.d/openai-responses/engine.d.ts +0 -112
- package/build/native-engines.d/openai-responses/engine.js +0 -344
- package/build/native-engines.d/openai-responses/engine.js.map +0 -1
- package/build/native-engines.d/openai-responses/exports.d.ts +0 -7
- package/build/native-engines.d/openai-responses/exports.js +0 -8
- package/build/native-engines.d/openai-responses/exports.js.map +0 -1
- package/build/session.d.ts +0 -77
- package/build/session.js +0 -137
- package/build/session.js.map +0 -1
- package/build/test.d.ts +0 -1
- package/build/test.js +0 -198
- package/build/test.js.map +0 -1
package/DEVELOPMENT.md
CHANGED
|
@@ -3,21 +3,20 @@
|
|
|
3
3
|
```mermaid
|
|
4
4
|
classDiagram
|
|
5
5
|
|
|
6
|
-
OpenAIResponsesEngine <|.. OpenAIResponsesNativeEngine
|
|
7
|
-
|
|
8
|
-
CompatibleEngine <|.. OpenAIResponsesCompatibleEngine
|
|
9
|
-
OpenAIResponsesEngine <|.. OpenAIResponsesCompatibleEngine
|
|
10
|
-
Engine <|.. OpenAIResponsesEngine
|
|
11
|
-
|
|
12
6
|
Engine <|.. CompatibleEngine
|
|
13
7
|
|
|
14
|
-
Engine <|..
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
OpenAIChatCompletionsCompatibleEngine <|.. OpenAIChatCompletionsCompatibleMonolithEngine
|
|
18
|
-
OpenAIChatCompletionsCompatibleEngine <|.. OpenAIChatCompletionsCompatibleStreamEngine
|
|
19
|
-
```
|
|
8
|
+
Engine <|.. OpenAIResponsesNativeEngine
|
|
9
|
+
OpenAIResponsesNativeEngine o--> OpenAIResponsesNativeHelpers
|
|
10
|
+
OpenAIResponsesNativeHelpers o--> OpenAIResponsesHelpers
|
|
20
11
|
|
|
21
|
-
|
|
12
|
+
CompatibleEngine <|.. AliyunEngine
|
|
13
|
+
OpenAIChatCompletionsCompatibleHelpers o--> OpenAIChatCompletionsHelpers
|
|
14
|
+
OpenAIChatCompletionsCompatibleMonolithHelpers o--> OpenAIChatCompletionsCompatibleHelpers
|
|
15
|
+
OpenAIChatCompletionsCompatibleStreamHelpers o--> OpenAIChatCompletionsCompatibleHelpers
|
|
16
|
+
AliyunEngine o--> OpenAIChatCompletionsCompatibleStreamHelpers
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
CompatibleEngine <|.. OpenAICompatibleEngine
|
|
19
|
+
OpenAICompatibleEngine o--> OpenAIResponsesCompatibleHelpers
|
|
20
|
+
OpenAIResponsesCompatibleHelpers o--> OpenAIResponsesHelpers
|
|
21
|
+
|
|
22
|
+
```
|
package/README.md
CHANGED
|
@@ -4,13 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@zimtsui/brainswitch)
|
|
6
6
|
|
|
7
|
-
Brainswitch
|
|
8
|
-
|
|
9
|
-
## Motivation
|
|
10
|
-
|
|
11
|
-
大多数 LLM 的[聊天模板](https://huggingface.co/docs/transformers/en/chat_templating) ChatML 原生不支持[严格函数调用](https://platform.openai.com/docs/guides/function-calling#strict-mode),在批处理 AI 工作流中难以达到生产级可靠性。如果仅使用 OpenAI 等支持严格函数调用的服务商,那么可选的模型型号会大幅受限。
|
|
12
|
-
|
|
13
|
-
Brainswitch 支持在一次会话中途切换模型并保持对话上下文,包括 OpenAI、Google、Anthropic 的深度思考模型[交替思考](https://platform.claude.com/docs/en/build-with-claude/extended-thinking#interleaved-thinking)的加密思考内容,这样即可在会话的大量推理阶段使用最合适的模型生成自然语言结果,在最后的总结阶段切换成支持严格函数调用的模型进行结构化提交。
|
|
7
|
+
Brainswitch 是一个强类型的 LLM 推理 API 适配器。
|
|
14
8
|
|
|
15
9
|
## 支持服务商 API 类型
|
|
16
10
|
|
|
@@ -40,47 +34,14 @@ npm install @zimtsui/brainswitch
|
|
|
40
34
|
## 配置
|
|
41
35
|
|
|
42
36
|
```ts
|
|
43
|
-
|
|
44
|
-
brainswitch: {
|
|
45
|
-
endpoints: Record<string, {
|
|
46
|
-
baseUrl: string;
|
|
47
|
-
apiKey: string;
|
|
48
|
-
model: string;
|
|
49
|
-
name: string;
|
|
50
|
-
apiType:
|
|
51
|
-
| 'openai-responses'
|
|
52
|
-
| 'google'
|
|
53
|
-
| 'aliyun'
|
|
54
|
-
| 'anthropic'
|
|
55
|
-
;
|
|
56
|
-
proxy?: string;
|
|
57
|
-
inputPrice?: number; // CNY per MToken
|
|
58
|
-
outputPrice?: number; // CNY per MToken
|
|
59
|
-
cachePrice?: number; // CNY per MToken
|
|
60
|
-
rpm?: number; // Requests per minute
|
|
61
|
-
timeout?: number; // Time limit in milliseconds
|
|
62
|
-
maxTokens?: number; // Maximum number of generated tokens
|
|
63
|
-
additionalOptions?: Record<string, unknown>;
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## 快速上手
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { Adaptor, agentloop, RoleMessage, Function, type InferenceContext, type Config, Session } from '@zimtsui/brainswitch';
|
|
73
|
-
import { Type } from '@sinclair/typebox';
|
|
74
|
-
import { RWLock } from '@zimtsui/coroutine-locks';
|
|
75
|
-
import { Channel } from '@zimtsui/typelog';
|
|
76
|
-
import * as Presets from '@zimtsui/typelog/presets';
|
|
37
|
+
import { type Config } from '@zimtsui/brainswitch';
|
|
77
38
|
|
|
78
39
|
// 配置推理服务商 API 接入点
|
|
79
|
-
const config: Config = {
|
|
40
|
+
export const config: Config = {
|
|
80
41
|
brainswitch: {
|
|
81
42
|
endpoints: {
|
|
82
|
-
'gpt-5-mini': {
|
|
83
|
-
name: 'GPT-5 mini',
|
|
43
|
+
'gpt-5.4-mini': {
|
|
44
|
+
name: 'GPT-5.4 mini',
|
|
84
45
|
apiType: 'openai-responses',
|
|
85
46
|
baseUrl: 'https://api.openai.com/v1',
|
|
86
47
|
apiKey: process.env.OPENAI_API_KEY!,
|
|
@@ -96,26 +57,63 @@ const config: Config = {
|
|
|
96
57
|
}
|
|
97
58
|
}
|
|
98
59
|
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## 对话
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { Adaptor, RoleMessage, type Session } from '@zimtsui/brainswitch';
|
|
66
|
+
import { config } from './config.ts';
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// 创建会话
|
|
70
|
+
const session: Session<never, never> = {
|
|
71
|
+
developerMessage: new RoleMessage.Developer([
|
|
72
|
+
RoleMessage.Part.Text.paragraph('You are a helpful assistant.'),
|
|
73
|
+
]),
|
|
74
|
+
chatMessages: [
|
|
75
|
+
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('Hello!') ]),
|
|
76
|
+
],
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// 选择推理引擎
|
|
80
|
+
const adaptor = Adaptor.create(config);
|
|
81
|
+
const engine = adaptor.makeCompatibleEngine<{}, {}>({
|
|
82
|
+
endpoint: 'gpt-5.4-mini',
|
|
83
|
+
functionDeclarationMap: {},
|
|
84
|
+
verbatimDeclarationMap: {},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const response = await engine.stateless({}, session);
|
|
88
|
+
console.log(response.getText());
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 智能体
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
import { Adaptor, agentloop, RoleMessage, Function, type Session, Structuring } from '@zimtsui/brainswitch';
|
|
95
|
+
import { Type } from '@sinclair/typebox';
|
|
96
|
+
import { config } from './config.ts';
|
|
99
97
|
|
|
100
98
|
// 声明函数工具
|
|
101
99
|
const fdm = {
|
|
102
100
|
get_weather: {
|
|
103
101
|
description: '获取指定城市的天气',
|
|
104
|
-
|
|
102
|
+
parameters: Type.Object({
|
|
105
103
|
city: Type.String(),
|
|
106
104
|
unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
|
|
107
105
|
}),
|
|
108
106
|
},
|
|
109
107
|
submit_result: {
|
|
110
108
|
description: '提交最终结果',
|
|
111
|
-
|
|
109
|
+
parameters: Type.Object({
|
|
112
110
|
weather: Type.String(),
|
|
113
111
|
advice: Type.String(),
|
|
114
112
|
}),
|
|
115
113
|
},
|
|
116
|
-
} satisfies Function.
|
|
114
|
+
} satisfies Function.Decl.Map.Proto;
|
|
117
115
|
type fdm = typeof fdm;
|
|
118
|
-
type fdu = Function.
|
|
116
|
+
type fdu = Function.Decl.From<fdm>;
|
|
119
117
|
|
|
120
118
|
// 实现函数工具
|
|
121
119
|
export class Submission {
|
|
@@ -131,34 +129,84 @@ const fnm: Function.Map<fdm> = {
|
|
|
131
129
|
},
|
|
132
130
|
};
|
|
133
131
|
|
|
134
|
-
// 初始化工作流上下文
|
|
135
|
-
const ctx: InferenceContext = {
|
|
136
|
-
busy: new RWLock(),
|
|
137
|
-
cost(deltaCost) {
|
|
138
|
-
console.log((-deltaCost).toFixed(2));
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
|
|
142
132
|
// 创建会话
|
|
143
|
-
const session: Session<fdu> = {
|
|
144
|
-
developerMessage: RoleMessage.Developer
|
|
145
|
-
RoleMessage.Part.Text.
|
|
133
|
+
const session: Session<fdu, never> = {
|
|
134
|
+
developerMessage: new RoleMessage.Developer([
|
|
135
|
+
RoleMessage.Part.Text.paragraph('你的工作是为用户查询天气,并给出穿衣建议。调用工具提交最终结果'),
|
|
146
136
|
]),
|
|
147
137
|
chatMessages: [
|
|
148
|
-
RoleMessage.User
|
|
138
|
+
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请查询现在北京的天气,并给穿衣建议。') ]),
|
|
149
139
|
],
|
|
150
140
|
};
|
|
151
141
|
|
|
152
142
|
// 选择推理引擎
|
|
153
143
|
const adaptor = Adaptor.create(config);
|
|
154
|
-
const engine = adaptor.
|
|
144
|
+
const engine = adaptor.makeCompatibleEngine<fdm, {}>({
|
|
145
|
+
endpoint: 'gpt-5.4-mini',
|
|
146
|
+
functionDeclarationMap: fdm,
|
|
147
|
+
verbatimDeclarationMap: {},
|
|
148
|
+
structuringChoice: Structuring.Choice.FCall.REQUIRED,
|
|
149
|
+
});
|
|
155
150
|
|
|
156
151
|
// 使用 agentloop 驱动智能体循环,最多 8 轮对话
|
|
157
152
|
try {
|
|
158
|
-
for await (const text of agentloop(
|
|
153
|
+
for await (const text of agentloop({}, session, engine, fnm, 8)) console.log(text);
|
|
159
154
|
} catch (e) {
|
|
160
155
|
if (e instanceof Submission) {} else throw e;
|
|
161
156
|
console.log(e.weather);
|
|
162
157
|
console.log(e.advice);
|
|
163
158
|
}
|
|
164
159
|
```
|
|
160
|
+
|
|
161
|
+
### XML Verbatim 频道
|
|
162
|
+
|
|
163
|
+
When a LLM outputs structured data in JSON format, if there are too many special characters in the parameters (for example, a large Markdown document containing a lot of LaTeX math formulas), the LLM is prone to make mistakes in JSON escaping.
|
|
164
|
+
|
|
165
|
+
XML Verbatim Channel is designed to avoid escaping in structured output of large text.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import { Adaptor, RoleMessage, type Session, Structuring, Verbatim } from '@zimtsui/brainswitch';
|
|
169
|
+
import Assets from '@zimtsui/brainswitch/assets';
|
|
170
|
+
import * as Codec from '@zimtsui/brainswitch/codec';
|
|
171
|
+
import { config } from './config.ts';
|
|
172
|
+
|
|
173
|
+
// 声明 XML Verbatim 频道
|
|
174
|
+
const vdm = {
|
|
175
|
+
bash: {
|
|
176
|
+
description: '执行 Bash 命令',
|
|
177
|
+
parameters: {
|
|
178
|
+
command: {
|
|
179
|
+
description: 'Bash 命令',
|
|
180
|
+
mimeType: 'text/plain',
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
} satisfies Verbatim.Decl.Map.Proto;
|
|
185
|
+
type vdm = typeof vdm;
|
|
186
|
+
type vdu = Verbatim.Decl.From<vdm>;
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
// 创建会话
|
|
190
|
+
const session: Session<never, vdu> = {
|
|
191
|
+
developerMessage: new RoleMessage.Developer([
|
|
192
|
+
Assets.verbatim.instruction,
|
|
193
|
+
RoleMessage.Part.Text.paragraph('# Available Verbatim Channels'),
|
|
194
|
+
RoleMessage.Part.Text.paragraph(Codec.Declarations.encode(vdm)),
|
|
195
|
+
]),
|
|
196
|
+
chatMessages: [
|
|
197
|
+
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请使用 Bash 命令查询当前系统时间。') ]),
|
|
198
|
+
],
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
// 选择推理引擎
|
|
202
|
+
const adaptor = Adaptor.create(config);
|
|
203
|
+
const engine = adaptor.makeCompatibleEngine<{}, vdm>({
|
|
204
|
+
endpoint: 'gpt-5.4-mini',
|
|
205
|
+
functionDeclarationMap: {},
|
|
206
|
+
verbatimDeclarationMap: vdm,
|
|
207
|
+
structuringChoice: Structuring.Choice.VRequest.ANYONE,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const response = await engine.stateless({}, session);
|
|
211
|
+
console.log(response.getOnlyVerbatimRequest().args.command);
|
|
212
|
+
```
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{{#each declarations}}
|
|
2
|
+
<verbatim:declaration name="{{this.name}}">
|
|
3
|
+
<verbatim:description>{{this.description}}</verbatim:description>
|
|
4
|
+
{{#each this.parameters}}
|
|
5
|
+
<verbatim:parameter name="{{this.name}}">
|
|
6
|
+
<verbatim:description>{{this.description}}</verbatim:description>
|
|
7
|
+
<verbatim:mime-type>{{this.mimeType}}</verbatim:mime-type>
|
|
8
|
+
</verbatim:parameter>
|
|
9
|
+
{{/each}}
|
|
10
|
+
</verbatim:declaration>
|
|
11
|
+
{{/each}}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# XML Verbatim Channel
|
|
2
|
+
|
|
3
|
+
## Motivation
|
|
4
|
+
|
|
5
|
+
When a LLM outputs structured data in JSON format, if there are too many special characters in the parameters (for example, a large Markdown document containing a lot of LaTeX math formulas), the LLM is prone to make mistakes in JSON escaping.
|
|
6
|
+
|
|
7
|
+
XML Verbatim Channel is designed to avoid escaping in structured output of large text.
|
|
8
|
+
|
|
9
|
+
## Declaration
|
|
10
|
+
|
|
11
|
+
The system or the user will declare all available XML Verbatim Channels in the form of
|
|
12
|
+
|
|
13
|
+
<verbatim:declaration name="CHANNEL_1_NAME">
|
|
14
|
+
<verbatim:description>DESCRIPTION_OF_CHANNEL_1</verbatim:description>
|
|
15
|
+
<verbatim:parameter name="PARAMETER_1_NAME">
|
|
16
|
+
<verbatim:description>DESCRIPTION_OF_PARAMETER_1</verbatim:description>
|
|
17
|
+
<verbatim:mime-type>VALUE_MIME_TYPE_OF_PARAMETER_1</verbatim:mime-type>
|
|
18
|
+
</verbatim:parameter>
|
|
19
|
+
<verbatim:parameter name="PARAMETER_2_NAME">
|
|
20
|
+
<verbatim:description>DESCRIPTION_OF_PARAMETER_2</verbatim:description>
|
|
21
|
+
<verbatim:mime-type>VALUE_MIME_TYPE_OF_PARAMETER_2</verbatim:mime-type>
|
|
22
|
+
</verbatim:parameter>
|
|
23
|
+
</verbatim:declaration>
|
|
24
|
+
|
|
25
|
+
## Request
|
|
26
|
+
|
|
27
|
+
You can make a request through the channel CHANNEL_1_NAME in the form of
|
|
28
|
+
|
|
29
|
+
<verbatim:request name="CHANNEL_1_NAME">
|
|
30
|
+
<verbatim:argument name="PARAMETER_1_NAME"><![CDATA[PARAMETER_1_VALUE]]></verbatim:argument>
|
|
31
|
+
<verbatim:argument name="PARAMETER_2_NAME"><![CDATA[PARAMETER_2_VALUE]]></verbatim:argument>
|
|
32
|
+
</verbatim:request>
|
|
33
|
+
|
|
34
|
+
- All parameters are required
|
|
35
|
+
- All arguments must be wrapped in CDATA
|
|
36
|
+
|
|
37
|
+
# Available XML Verbatim Channels
|
|
38
|
+
|
|
39
|
+
<verbatim:declaration name="submit">
|
|
40
|
+
<verbatim:description>提交文章</verbatim:description>
|
|
41
|
+
<verbatim:parameter name="title">
|
|
42
|
+
<verbatim:description>文章标题</verbatim:description>
|
|
43
|
+
<verbatim:mime-type>text/plain</verbatim:mime-type>
|
|
44
|
+
</verbatim:parameter>
|
|
45
|
+
<verbatim:parameter name="body">
|
|
46
|
+
<verbatim:description>文章内容</verbatim:description>
|
|
47
|
+
<verbatim:mime-type>text/markdown;dialect=gfm+tex_math_dollars</verbatim:mime-type>
|
|
48
|
+
</verbatim:parameter>
|
|
49
|
+
</verbatim:declaration>
|
package/build/adaptor.d.ts
CHANGED
|
@@ -1,14 +1,45 @@
|
|
|
1
|
-
import { Config } from '
|
|
1
|
+
import { Config } from './config.ts';
|
|
2
2
|
import { Function } from './function.ts';
|
|
3
3
|
import { type CompatibleEngine } from './compatible-engine.ts';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { Throttle } from './throttle.ts';
|
|
5
|
+
import { OpenAIResponsesNativeEngine } from './native-engines.d/openai-responses.ts';
|
|
6
|
+
import { GoogleNativeEngine } from './native-engines.d/google.ts';
|
|
7
|
+
import type { Verbatim } from './verbatim.ts';
|
|
8
|
+
import type { Structuring } from './compatible-engine/structuring.ts';
|
|
9
|
+
import type { Structuring as OpenAIResponsesNativeStructuring } from './native-engines.d/openai-responses/structuring.ts';
|
|
6
10
|
export declare class Adaptor {
|
|
7
11
|
config: Config;
|
|
8
12
|
static create(config: Config): Adaptor;
|
|
9
|
-
|
|
13
|
+
protected throttles: Map<string, Throttle>;
|
|
10
14
|
protected constructor(config: Config);
|
|
11
|
-
|
|
12
|
-
makeOpenAIResponsesNativeEngine<fdm extends Function.
|
|
13
|
-
makeGoogleNativeEngine<fdm extends Function.
|
|
15
|
+
makeCompatibleEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.CompatibleEngine.Options<fdm, vdm>): CompatibleEngine<fdm, vdm>;
|
|
16
|
+
makeOpenAIResponsesNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.OpenAIResponsesNativeEngineOptions<fdm, vdm>): OpenAIResponsesNativeEngine<fdm, vdm>;
|
|
17
|
+
makeGoogleNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.GoogleNativeEngineOptions<fdm, vdm>): GoogleNativeEngine<fdm, vdm>;
|
|
18
|
+
}
|
|
19
|
+
export declare namespace Adaptor {
|
|
20
|
+
namespace CompatibleEngine {
|
|
21
|
+
interface Options<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
22
|
+
endpoint: string;
|
|
23
|
+
functionDeclarationMap: fdm;
|
|
24
|
+
verbatimDeclarationMap: vdm;
|
|
25
|
+
structuringChoice?: Structuring.Choice.From<fdm, vdm>;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
interface OpenAIResponsesNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
29
|
+
endpoint: string;
|
|
30
|
+
functionDeclarationMap: fdm;
|
|
31
|
+
verbatimDeclarationMap: vdm;
|
|
32
|
+
applyPatch?: boolean;
|
|
33
|
+
structuringChoice?: OpenAIResponsesNativeStructuring.Choice.From<fdm, vdm>;
|
|
34
|
+
}
|
|
35
|
+
interface GoogleNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
36
|
+
endpoint: string;
|
|
37
|
+
functionDeclarationMap: fdm;
|
|
38
|
+
verbatimDeclarationMap: vdm;
|
|
39
|
+
structuringChoice?: Structuring.Choice.From<fdm, vdm>;
|
|
40
|
+
codeExecution?: boolean;
|
|
41
|
+
urlContext?: boolean;
|
|
42
|
+
googleSearch?: boolean;
|
|
43
|
+
parallelToolCall?: boolean;
|
|
44
|
+
}
|
|
14
45
|
}
|
package/build/adaptor.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import { Config } from '#config';
|
|
2
|
-
import { Function } from "./function.js";
|
|
3
|
-
import {} from "./compatible-engine.js";
|
|
4
1
|
import { Throttle } from "./throttle.js";
|
|
5
|
-
import { GoogleCompatibleEngine } from "./compatible-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses
|
|
10
|
-
import { GoogleNativeEngine } from "./native-engines.d/google
|
|
2
|
+
import { GoogleCompatibleEngine } from "./compatible-engine.d/google.js";
|
|
3
|
+
import { OpenAIResponsesCompatibleEngine } from "./compatible-engine.d/openai-responses.js";
|
|
4
|
+
import { AnthropicCompatibleEngine } from "./compatible-engine.d/anthropic.js";
|
|
5
|
+
import { AliyunCompatibleEngine } from "./compatible-engine.d/aliyun.js";
|
|
6
|
+
import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses.js";
|
|
7
|
+
import { GoogleNativeEngine } from "./native-engines.d/google.js";
|
|
11
8
|
export class Adaptor {
|
|
12
9
|
config;
|
|
13
10
|
static create(config) {
|
|
@@ -21,72 +18,73 @@ export class Adaptor {
|
|
|
21
18
|
this.throttles.set(endpointId, new Throttle(rpm));
|
|
22
19
|
}
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
21
|
+
makeCompatibleEngine(adaptorOptions) {
|
|
22
|
+
const endpointSpec = this.config.brainswitch.endpoints[adaptorOptions.endpoint];
|
|
26
23
|
if (endpointSpec) { }
|
|
27
24
|
else
|
|
28
25
|
throw new Error();
|
|
29
|
-
const throttle = this.throttles.get(endpoint);
|
|
26
|
+
const throttle = this.throttles.get(adaptorOptions.endpoint);
|
|
30
27
|
if (throttle) { }
|
|
31
28
|
else
|
|
32
29
|
throw new Error();
|
|
33
30
|
const options = {
|
|
34
31
|
...endpointSpec,
|
|
35
|
-
functionDeclarationMap,
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
functionDeclarationMap: adaptorOptions.functionDeclarationMap,
|
|
33
|
+
verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
|
|
34
|
+
structuringChoice: adaptorOptions.structuringChoice,
|
|
38
35
|
throttle,
|
|
39
36
|
};
|
|
40
37
|
if (endpointSpec.apiType === 'openai-responses')
|
|
41
|
-
return OpenAIResponsesCompatibleEngine.
|
|
38
|
+
return new OpenAIResponsesCompatibleEngine.Instance(options);
|
|
42
39
|
else if (endpointSpec.apiType === 'google')
|
|
43
|
-
return GoogleCompatibleEngine.
|
|
40
|
+
return new GoogleCompatibleEngine.Instance(options);
|
|
44
41
|
else if (endpointSpec.apiType === 'aliyun')
|
|
45
|
-
return
|
|
42
|
+
return new AliyunCompatibleEngine.Instance(options);
|
|
46
43
|
else if (endpointSpec.apiType === 'anthropic')
|
|
47
|
-
return AnthropicCompatibleEngine.
|
|
44
|
+
return new AnthropicCompatibleEngine.Instance(options);
|
|
48
45
|
else
|
|
49
46
|
throw new Error();
|
|
50
47
|
}
|
|
51
|
-
makeOpenAIResponsesNativeEngine(
|
|
52
|
-
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
48
|
+
makeOpenAIResponsesNativeEngine(adaptorOptions) {
|
|
49
|
+
const endpointSpec = this.config.brainswitch.endpoints[adaptorOptions.endpoint];
|
|
53
50
|
if (endpointSpec?.apiType === 'openai-responses') { }
|
|
54
51
|
else
|
|
55
52
|
throw new Error();
|
|
56
|
-
const throttle = this.throttles.get(endpoint);
|
|
53
|
+
const throttle = this.throttles.get(adaptorOptions.endpoint);
|
|
57
54
|
if (throttle) { }
|
|
58
55
|
else
|
|
59
56
|
throw new Error();
|
|
60
57
|
const options = {
|
|
61
58
|
...endpointSpec,
|
|
62
|
-
functionDeclarationMap,
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
functionDeclarationMap: adaptorOptions.functionDeclarationMap,
|
|
60
|
+
verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
|
|
61
|
+
structuringChoice: adaptorOptions.structuringChoice,
|
|
65
62
|
throttle,
|
|
66
|
-
applyPatch,
|
|
63
|
+
applyPatch: adaptorOptions.applyPatch,
|
|
67
64
|
};
|
|
68
|
-
return OpenAIResponsesNativeEngine.
|
|
65
|
+
return new OpenAIResponsesNativeEngine.Instance(options);
|
|
69
66
|
}
|
|
70
|
-
makeGoogleNativeEngine(
|
|
71
|
-
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
67
|
+
makeGoogleNativeEngine(adaptorOptions) {
|
|
68
|
+
const endpointSpec = this.config.brainswitch.endpoints[adaptorOptions.endpoint];
|
|
72
69
|
if (endpointSpec?.apiType === 'google') { }
|
|
73
70
|
else
|
|
74
71
|
throw new Error();
|
|
75
|
-
const throttle = this.throttles.get(endpoint);
|
|
72
|
+
const throttle = this.throttles.get(adaptorOptions.endpoint);
|
|
76
73
|
if (throttle) { }
|
|
77
74
|
else
|
|
78
75
|
throw new Error();
|
|
79
76
|
const options = {
|
|
80
77
|
...endpointSpec,
|
|
81
|
-
functionDeclarationMap,
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
functionDeclarationMap: adaptorOptions.functionDeclarationMap,
|
|
79
|
+
verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
|
|
80
|
+
structuringChoice: adaptorOptions.structuringChoice,
|
|
81
|
+
parallelToolCall: adaptorOptions.parallelToolCall,
|
|
84
82
|
throttle,
|
|
85
|
-
codeExecution,
|
|
86
|
-
urlContext,
|
|
87
|
-
googleSearch,
|
|
83
|
+
codeExecution: adaptorOptions.codeExecution,
|
|
84
|
+
urlContext: adaptorOptions.urlContext,
|
|
85
|
+
googleSearch: adaptorOptions.googleSearch,
|
|
88
86
|
};
|
|
89
|
-
return GoogleNativeEngine.
|
|
87
|
+
return new GoogleNativeEngine.Instance(options);
|
|
90
88
|
}
|
|
91
89
|
}
|
|
92
90
|
//# sourceMappingURL=adaptor.js.map
|
package/build/adaptor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,QAAQ,EAAE,MAA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,EAAE,cAAc,CAAC,sBAAsB;YAC7D,iBAAiB,EAAE,cAAc,CAAC,iBAAiB;YACnD,QAAQ;YACR,UAAU,EAAE,cAAc,CAAC,UAAU;SACxC,CAAC;QACF,OAAO,IAAI,2BAA2B,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC7D,CAAC;IAEM,sBAAsB,CAG3B,cAA2D;QACzD,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAChF,IAAI,YAAY,EAAE,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QAClE,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAC7D,IAAI,QAAQ,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACxC,MAAM,OAAO,GAAyC;YAClD,GAAG,YAAY;YACf,sBAAsB,EAAE,cAAc,CAAC,sBAAsB;YAC7D,sBAAsB,EAAE,cAAc,CAAC,sBAAsB;YAC7D,iBAAiB,EAAE,cAAc,CAAC,iBAAiB;YACnD,gBAAgB,EAAE,cAAc,CAAC,gBAAgB;YACjD,QAAQ;YACR,aAAa,EAAE,cAAc,CAAC,aAAa;YAC3C,UAAU,EAAE,cAAc,CAAC,UAAU;YACrC,YAAY,EAAE,cAAc,CAAC,YAAY;SAC5C,CAAC;QACF,OAAO,IAAI,kBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;CACJ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Pricing } from '../../engine.ts';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
export declare class Billing {
|
|
4
|
+
protected ctx: Billing.Context;
|
|
5
|
+
constructor(ctx: Billing.Context);
|
|
6
|
+
charge(usage: Anthropic.Usage): number;
|
|
7
|
+
}
|
|
8
|
+
export declare namespace Billing {
|
|
9
|
+
interface Context {
|
|
10
|
+
pricing: Pricing;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export class Billing {
|
|
2
|
+
ctx;
|
|
3
|
+
constructor(ctx) {
|
|
4
|
+
this.ctx = ctx;
|
|
5
|
+
}
|
|
6
|
+
charge(usage) {
|
|
7
|
+
const cacheHitTokenCount = usage.cache_read_input_tokens || 0;
|
|
8
|
+
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
9
|
+
return (this.ctx.pricing.inputPrice * cacheMissTokenCount / 1e6 +
|
|
10
|
+
this.ctx.pricing.cachePrice * cacheHitTokenCount / 1e6 +
|
|
11
|
+
this.ctx.pricing.outputPrice * usage.output_tokens / 1e6);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=billing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,OAAO;IACa;IAA7B,YAA6B,GAAoB;QAApB,QAAG,GAAH,GAAG,CAAiB;IAAG,CAAC;IAE9C,MAAM,CAAC,KAAsB;QAChC,MAAM,kBAAkB,GAAG,KAAK,CAAC,uBAAuB,IAAI,CAAC,CAAC;QAC9D,MAAM,mBAAmB,GAAG,KAAK,CAAC,YAAY,GAAG,kBAAkB,CAAC;QACpE,OAAO,CACH,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YACvD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YACtD,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,aAAa,GAAG,GAAG,CAC3D,CAAC;IACN,CAAC;CACJ"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Function } from '../../function.ts';
|
|
2
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
+
export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
|
|
4
|
+
protected ctx: ToolCodec.Context<fdm>;
|
|
5
|
+
constructor(ctx: ToolCodec.Context<fdm>);
|
|
6
|
+
encodeFunctionCall(fc: Function.Call.From<fdm>): Anthropic.ToolUseBlock;
|
|
7
|
+
decodeFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.From<fdm>;
|
|
8
|
+
encodeFunctionResponse(fr: Function.Response.From<fdm>): Anthropic.ToolResultBlockParam;
|
|
9
|
+
protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): Anthropic.Tool;
|
|
10
|
+
encodeFunctionDeclarationMap(fdm: fdm): Anthropic.Tool[];
|
|
11
|
+
}
|
|
12
|
+
export declare namespace ToolCodec {
|
|
13
|
+
interface Context<in out fdm extends Function.Decl.Map.Proto> {
|
|
14
|
+
fdm: fdm;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Function } from "../../function.js";
|
|
2
|
+
import { ResponseInvalid } from "../../engine.js";
|
|
3
|
+
import Ajv from 'ajv';
|
|
4
|
+
const ajv = new Ajv();
|
|
5
|
+
export class ToolCodec {
|
|
6
|
+
ctx;
|
|
7
|
+
constructor(ctx) {
|
|
8
|
+
this.ctx = ctx;
|
|
9
|
+
}
|
|
10
|
+
encodeFunctionCall(fc) {
|
|
11
|
+
throw new Error('Anthropic compatible engine requires native function calls.');
|
|
12
|
+
}
|
|
13
|
+
decodeFunctionCall(apifc) {
|
|
14
|
+
const fditem = this.ctx.fdm[apifc.name];
|
|
15
|
+
if (fditem) { }
|
|
16
|
+
else
|
|
17
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
18
|
+
if (ajv.validate(fditem.parameters, apifc.input)) { }
|
|
19
|
+
else
|
|
20
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
21
|
+
return Function.Call.of({
|
|
22
|
+
id: apifc.id,
|
|
23
|
+
name: apifc.name,
|
|
24
|
+
args: apifc.input,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
encodeFunctionResponse(fr) {
|
|
28
|
+
if (fr.id) { }
|
|
29
|
+
else
|
|
30
|
+
throw new Error();
|
|
31
|
+
if (fr instanceof Function.Response.Successful)
|
|
32
|
+
return {
|
|
33
|
+
type: 'tool_result',
|
|
34
|
+
tool_use_id: fr.id,
|
|
35
|
+
content: fr.text,
|
|
36
|
+
};
|
|
37
|
+
else if (fr instanceof Function.Response.Failed)
|
|
38
|
+
return {
|
|
39
|
+
type: 'tool_result',
|
|
40
|
+
tool_use_id: fr.id,
|
|
41
|
+
content: fr.error,
|
|
42
|
+
};
|
|
43
|
+
else
|
|
44
|
+
throw new Error();
|
|
45
|
+
}
|
|
46
|
+
encodeFunctionDeclarationEntry(fdentry) {
|
|
47
|
+
return {
|
|
48
|
+
name: fdentry[0],
|
|
49
|
+
description: fdentry[1].description,
|
|
50
|
+
input_schema: fdentry[1].parameters,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
encodeFunctionDeclarationMap(fdm) {
|
|
54
|
+
const fdentries = Object.entries(fdm);
|
|
55
|
+
return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=tool-codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-codec.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/tool-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,GAAG,MAAM,KAAK,CAAC;AAG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,OAAO;gBACH,IAAI,EAAE,aAAa;gBACnB,WAAW,EAAE,EAAE,CAAC,EAAE;gBAClB,OAAO,EAAE,EAAE,CAAC,KAAK;aACpB,CAAC;;YACD,MAAM,IAAI,KAAK,EAAE,CAAC;IAC3B,CAAC;IAES,8BAA8B,CACpC,OAAsC;QAEtC,OAAO;YACH,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;YAChB,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,WAAW;YACnC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,UAAqB;SACjD,CAAC;IACN,CAAC;IAEM,4BAA4B,CAC/B,GAAQ;QAER,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAoC,CAAC;QAEzE,OAAO,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,CAAC,8BAA8B,CAAC,OAAO,CAAC,CAAC,CAAC;IAClF,CAAC;CAEJ"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as Google from '@google/genai';
|
|
2
|
+
import type { Pricing } from '../../engine.ts';
|
|
3
|
+
export declare class Billing {
|
|
4
|
+
protected ctx: Billing.Context;
|
|
5
|
+
constructor(ctx: Billing.Context);
|
|
6
|
+
charge(usageMetadata: Google.GenerateContentResponseUsageMetadata): number;
|
|
7
|
+
}
|
|
8
|
+
export declare namespace Billing {
|
|
9
|
+
interface Context {
|
|
10
|
+
pricing: Pricing;
|
|
11
|
+
}
|
|
12
|
+
}
|