@zimtsui/brainswitch 0.0.56 → 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/README.md +109 -54
- package/assets/verbatim/declarations.handlebars +11 -0
- package/assets/verbatim/instruction.md +49 -0
- package/build/adaptor.d.ts +40 -9
- package/build/adaptor.js +36 -38
- package/build/adaptor.js.map +1 -1
- package/build/api-types/anthropic/billing.d.ts +5 -5
- package/build/api-types/anthropic/billing.js +1 -2
- package/build/api-types/anthropic/billing.js.map +1 -1
- package/build/api-types/anthropic/tool-codec.d.ts +11 -12
- package/build/api-types/anthropic/tool-codec.js +27 -42
- package/build/api-types/anthropic/tool-codec.js.map +1 -1
- package/build/api-types/google/billing.d.ts +5 -5
- package/build/api-types/google/billing.js +2 -3
- package/build/api-types/google/billing.js.map +1 -1
- package/build/api-types/google/restful-request.d.ts +1 -1
- package/build/api-types/google/restful-request.js +0 -1
- package/build/api-types/google/restful-request.js.map +1 -1
- package/build/api-types/google/tool-codec.d.ts +11 -11
- package/build/api-types/google/tool-codec.js +21 -19
- package/build/api-types/google/tool-codec.js.map +1 -1
- package/build/api-types/{openai-chatcompletion → openai-chatcompletions}/billing.d.ts +2 -3
- package/build/api-types/{openai-chatcompletion → openai-chatcompletions}/billing.js +0 -2
- 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-chatcompletion → openai-chatcompletions}/tool-codec.js +26 -32
- package/build/api-types/openai-chatcompletions/tool-codec.js.map +1 -0
- package/build/api-types/openai-responses/billing.d.ts +5 -5
- package/build/api-types/openai-responses/billing.js +1 -2
- package/build/api-types/openai-responses/billing.js.map +1 -1
- package/build/api-types/openai-responses/tool-codec.d.ts +11 -11
- package/build/api-types/openai-responses/tool-codec.js +36 -32
- package/build/api-types/openai-responses/tool-codec.js.map +1 -1
- 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.d → compatible-engine.d}/google/transport.js +22 -18
- 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.d/openai-chatcompletions.d → compatible-engine.d/openai-chatcompletions/transport.d}/monolith.js +25 -22
- 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.d → compatible-engine.d}/openai-responses/transport.js +21 -29
- 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 +22 -0
- package/build/compatible-engine.js +29 -0
- package/build/compatible-engine.js.map +1 -0
- package/build/config.d.ts +1 -0
- package/build/config.js +1 -2
- 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/{session.js.map → engine/session.js.map} +1 -1
- 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 +49 -40
- package/build/engine.js +88 -70
- package/build/engine.js.map +1 -1
- package/build/exports.d.ts +14 -14
- package/build/exports.js +14 -14
- package/build/exports.js.map +1 -1
- package/build/function.d.ts +79 -66
- package/build/function.js +30 -47
- 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 +18 -16
- package/build/native-engines.d/google/message-codec.js +31 -33
- package/build/native-engines.d/google/message-codec.js.map +1 -1
- package/build/native-engines.d/google/session.d.ts +44 -51
- package/build/native-engines.d/google/session.js +85 -81
- package/build/native-engines.d/google/session.js.map +1 -1
- package/build/native-engines.d/google/transport.d.ts +22 -19
- package/build/native-engines.d/google/transport.js +13 -13
- package/build/native-engines.d/google/transport.js.map +1 -1
- 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 +19 -17
- package/build/native-engines.d/openai-responses/message-codec.js +31 -25
- package/build/native-engines.d/openai-responses/message-codec.js.map +1 -1
- package/build/native-engines.d/openai-responses/session.d.ts +47 -45
- package/build/native-engines.d/openai-responses/session.js +75 -105
- 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 +15 -12
- 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 +24 -25
- package/build/native-engines.d/openai-responses/transport.js +21 -41
- package/build/native-engines.d/openai-responses/transport.js.map +1 -1
- 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/openai-responses.d/exports.d.ts +4 -0
- package/build/native-engines.d/openai-responses.d/exports.js +5 -0
- 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 +6 -5
- 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/{test/src → examples}/tsconfig.json +6 -2
- 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/openai-chatcompletion/billing.js.map +0 -1
- package/build/api-types/openai-chatcompletion/tool-codec.d.ts +0 -18
- package/build/api-types/openai-chatcompletion/tool-codec.js.map +0 -1
- package/build/compatible/engine.d/aliyun.d.ts +0 -23
- package/build/compatible/engine.d/aliyun.js +0 -44
- package/build/compatible/engine.d/aliyun.js.map +0 -1
- package/build/compatible/engine.d/anthropic.d.ts +0 -23
- package/build/compatible/engine.d/anthropic.js +0 -41
- package/build/compatible/engine.d/anthropic.js.map +0 -1
- package/build/compatible/engine.d/google.d.ts +0 -23
- package/build/compatible/engine.d/google.js +0 -45
- package/build/compatible/engine.d/google.js.map +0 -1
- package/build/compatible/engine.d/openai-responses.d.ts +0 -23
- package/build/compatible/engine.d/openai-responses.js +0 -41
- package/build/compatible/engine.d/openai-responses.js.map +0 -1
- package/build/compatible/engine.d.ts +0 -18
- package/build/compatible/engine.js +0 -21
- package/build/compatible/engine.js.map +0 -1
- package/build/compatible/session.d.ts +0 -78
- package/build/compatible/session.js +0 -138
- package/build/compatible/session.js.map +0 -1
- package/build/compatible/tool-call-validator.d.ts +0 -11
- package/build/compatible/tool-call-validator.js +0 -12
- package/build/compatible/tool-call-validator.js.map +0 -1
- package/build/compatible.d/aliyun/transport.d.ts +0 -11
- package/build/compatible.d/aliyun/transport.js +0 -9
- package/build/compatible.d/aliyun/transport.js.map +0 -1
- package/build/compatible.d/anthropic/message-codec.d.ts +0 -31
- package/build/compatible.d/anthropic/message-codec.js +0 -88
- package/build/compatible.d/anthropic/message-codec.js.map +0 -1
- package/build/compatible.d/anthropic/transport.d.ts +0 -31
- package/build/compatible.d/anthropic/transport.js +0 -137
- package/build/compatible.d/anthropic/transport.js.map +0 -1
- package/build/compatible.d/google/message-codec.d.ts +0 -31
- package/build/compatible.d/google/message-codec.js +0 -98
- package/build/compatible.d/google/message-codec.js.map +0 -1
- package/build/compatible.d/google/transport.d.ts +0 -28
- package/build/compatible.d/google/transport.js.map +0 -1
- package/build/compatible.d/openai-chatcompletions/message-codec.d.ts +0 -19
- package/build/compatible.d/openai-chatcompletions/message-codec.js +0 -66
- package/build/compatible.d/openai-chatcompletions/message-codec.js.map +0 -1
- package/build/compatible.d/openai-chatcompletions/transport.d.ts +0 -9
- package/build/compatible.d/openai-chatcompletions/transport.js +0 -25
- package/build/compatible.d/openai-chatcompletions/transport.js.map +0 -1
- package/build/compatible.d/openai-chatcompletions.d/monolith.d.ts +0 -36
- package/build/compatible.d/openai-chatcompletions.d/monolith.js.map +0 -1
- package/build/compatible.d/openai-chatcompletions.d/stream.d.ts +0 -35
- package/build/compatible.d/openai-chatcompletions.d/stream.js +0 -196
- package/build/compatible.d/openai-chatcompletions.d/stream.js.map +0 -1
- package/build/compatible.d/openai-responses/message-codec.d.ts +0 -32
- package/build/compatible.d/openai-responses/message-codec.js +0 -105
- package/build/compatible.d/openai-responses/message-codec.js.map +0 -1
- package/build/compatible.d/openai-responses/transport.d.ts +0 -33
- package/build/compatible.d/openai-responses/transport.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 -35
- package/build/native-engines.d/google/engine.js +0 -73
- package/build/native-engines.d/google/engine.js.map +0 -1
- package/build/native-engines.d/google/exports.d.ts +0 -5
- package/build/native-engines.d/google/exports.js +0 -6
- 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 -32
- package/build/native-engines.d/openai-responses/engine.js +0 -63
- package/build/native-engines.d/openai-responses/engine.js.map +0 -1
- package/build/native-engines.d/openai-responses/exports.d.ts +0 -6
- package/build/native-engines.d/openai-responses/exports.js +0 -7
- package/build/native-engines.d/openai-responses/exports.js.map +0 -1
- package/build/native-engines.d/openai-responses/tool-call-validator.d.ts +0 -12
- package/build/native-engines.d/openai-responses/tool-call-validator.js +0 -33
- package/build/native-engines.d/openai-responses/tool-call-validator.js.map +0 -1
- package/build/session.d.ts +0 -7
- package/test/build/test-codecs.d.ts +0 -1
- package/test/build/test-codecs.js +0 -230
- package/test/build/test-codecs.js.map +0 -1
- package/test/build/test-compatible-engine.d.ts +0 -1
- package/test/build/test-compatible-engine.js +0 -27
- package/test/build/test-compatible-engine.js.map +0 -1
- package/test/build/test-compatible-engines.d.ts +0 -1
- package/test/build/test-compatible-engines.js +0 -112
- package/test/build/test-compatible-engines.js.map +0 -1
- package/test/build/test-engine.d.ts +0 -1
- package/test/build/test-engine.js +0 -97
- package/test/build/test-engine.js.map +0 -1
- package/test/build/test-helpers.d.ts +0 -46
- package/test/build/test-helpers.js +0 -45
- package/test/build/test-helpers.js.map +0 -1
- package/test/build/test-native-engines.d.ts +0 -1
- package/test/build/test-native-engines.js +0 -64
- package/test/build/test-native-engines.js.map +0 -1
- package/test/build/test.d.ts +0 -5
- package/test/build/test.js +0 -6
- package/test/build/test.js.map +0 -1
- package/test/build/tsconfig.tsbuildinfo +0 -1
- package/test/src/test-codecs.ts +0 -256
- package/test/src/test-compatible-engine.ts +0 -45
- package/test/src/test-compatible-engines.ts +0 -129
- package/test/src/test-engine.ts +0 -144
- package/test/src/test-helpers.ts +0 -53
- package/test/src/test-native-engines.ts +0 -75
- package/test/src/test.ts +0 -5
- /package/build/{session.js → engine/session.js} +0 -0
package/README.md
CHANGED
|
@@ -34,46 +34,14 @@ npm install @zimtsui/brainswitch
|
|
|
34
34
|
## 配置
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
|
-
|
|
38
|
-
brainswitch: {
|
|
39
|
-
endpoints: Record<string, {
|
|
40
|
-
baseUrl: string;
|
|
41
|
-
apiKey: string;
|
|
42
|
-
model: string;
|
|
43
|
-
name: string;
|
|
44
|
-
apiType:
|
|
45
|
-
| 'openai-responses'
|
|
46
|
-
| 'google'
|
|
47
|
-
| 'aliyun'
|
|
48
|
-
| 'anthropic'
|
|
49
|
-
;
|
|
50
|
-
proxy?: string;
|
|
51
|
-
inputPrice?: number; // CNY per MToken
|
|
52
|
-
outputPrice?: number; // CNY per MToken
|
|
53
|
-
cachePrice?: number; // CNY per MToken
|
|
54
|
-
rpm?: number; // Requests per minute
|
|
55
|
-
timeout?: number; // Time limit in milliseconds
|
|
56
|
-
maxTokens?: number; // Maximum number of generated tokens
|
|
57
|
-
additionalOptions?: Record<string, unknown>;
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## 快速上手
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
import { Adaptor, agentloop, RoleMessage, Function, type InferenceContext, type Config, type Session } from '@zimtsui/brainswitch';
|
|
67
|
-
import { Type } from '@sinclair/typebox';
|
|
68
|
-
import { RWLock } from '@zimtsui/coroutine-locks';
|
|
69
|
-
|
|
37
|
+
import { type Config } from '@zimtsui/brainswitch';
|
|
70
38
|
|
|
71
39
|
// 配置推理服务商 API 接入点
|
|
72
|
-
const config: Config = {
|
|
40
|
+
export const config: Config = {
|
|
73
41
|
brainswitch: {
|
|
74
42
|
endpoints: {
|
|
75
|
-
'gpt-5-mini': {
|
|
76
|
-
name: 'GPT-5 mini',
|
|
43
|
+
'gpt-5.4-mini': {
|
|
44
|
+
name: 'GPT-5.4 mini',
|
|
77
45
|
apiType: 'openai-responses',
|
|
78
46
|
baseUrl: 'https://api.openai.com/v1',
|
|
79
47
|
apiKey: process.env.OPENAI_API_KEY!,
|
|
@@ -89,25 +57,63 @@ const config: Config = {
|
|
|
89
57
|
}
|
|
90
58
|
}
|
|
91
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';
|
|
92
97
|
|
|
93
98
|
// 声明函数工具
|
|
94
99
|
const fdm = {
|
|
95
100
|
get_weather: {
|
|
96
101
|
description: '获取指定城市的天气',
|
|
97
|
-
|
|
102
|
+
parameters: Type.Object({
|
|
98
103
|
city: Type.String(),
|
|
99
104
|
unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
|
|
100
105
|
}),
|
|
101
106
|
},
|
|
102
107
|
submit_result: {
|
|
103
108
|
description: '提交最终结果',
|
|
104
|
-
|
|
109
|
+
parameters: Type.Object({
|
|
105
110
|
weather: Type.String(),
|
|
106
111
|
advice: Type.String(),
|
|
107
112
|
}),
|
|
108
113
|
},
|
|
109
|
-
} satisfies Function.
|
|
114
|
+
} satisfies Function.Decl.Map.Proto;
|
|
110
115
|
type fdm = typeof fdm;
|
|
116
|
+
type fdu = Function.Decl.From<fdm>;
|
|
111
117
|
|
|
112
118
|
// 实现函数工具
|
|
113
119
|
export class Submission {
|
|
@@ -123,35 +129,84 @@ const fnm: Function.Map<fdm> = {
|
|
|
123
129
|
},
|
|
124
130
|
};
|
|
125
131
|
|
|
126
|
-
// 初始化工作流上下文
|
|
127
|
-
const wfctx: InferenceContext = {
|
|
128
|
-
busy: new RWLock(),
|
|
129
|
-
cost(deltaCost) {
|
|
130
|
-
console.log((-deltaCost).toFixed(2));
|
|
131
|
-
},
|
|
132
|
-
signal: null,
|
|
133
|
-
};
|
|
134
|
-
|
|
135
132
|
// 创建会话
|
|
136
|
-
const session: Session<
|
|
137
|
-
developerMessage: RoleMessage.Developer
|
|
138
|
-
RoleMessage.Part.Text.
|
|
133
|
+
const session: Session<fdu, never> = {
|
|
134
|
+
developerMessage: new RoleMessage.Developer([
|
|
135
|
+
RoleMessage.Part.Text.paragraph('你的工作是为用户查询天气,并给出穿衣建议。调用工具提交最终结果'),
|
|
139
136
|
]),
|
|
140
137
|
chatMessages: [
|
|
141
|
-
RoleMessage.User
|
|
138
|
+
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请查询现在北京的天气,并给穿衣建议。') ]),
|
|
142
139
|
],
|
|
143
140
|
};
|
|
144
141
|
|
|
145
142
|
// 选择推理引擎
|
|
146
143
|
const adaptor = Adaptor.create(config);
|
|
147
|
-
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
|
+
});
|
|
148
150
|
|
|
149
151
|
// 使用 agentloop 驱动智能体循环,最多 8 轮对话
|
|
150
152
|
try {
|
|
151
|
-
for await (const text of agentloop(
|
|
153
|
+
for await (const text of agentloop({}, session, engine, fnm, 8)) console.log(text);
|
|
152
154
|
} catch (e) {
|
|
153
155
|
if (e instanceof Submission) {} else throw e;
|
|
154
156
|
console.log(e.weather);
|
|
155
157
|
console.log(e.advice);
|
|
156
158
|
}
|
|
157
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 '
|
|
2
|
-
import { Function } from '
|
|
3
|
-
import { type CompatibleEngine } from '
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { Config } from './config.ts';
|
|
2
|
+
import { Function } from './function.ts';
|
|
3
|
+
import { type CompatibleEngine } from './compatible-engine.ts';
|
|
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 {
|
|
2
|
-
import {
|
|
3
|
-
import {} from
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { AliyunEngine } from '#@/compatible/engine.d/aliyun.ts';
|
|
9
|
-
import { OpenAIResponsesNativeEngine } from '#@/native-engines.d/openai-responses/engine.ts';
|
|
10
|
-
import { GoogleNativeEngine } from '#@/native-engines.d/google/engine.ts';
|
|
1
|
+
import { Throttle } from "./throttle.js";
|
|
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 new OpenAIResponsesCompatibleEngine(options);
|
|
38
|
+
return new OpenAIResponsesCompatibleEngine.Instance(options);
|
|
42
39
|
else if (endpointSpec.apiType === 'google')
|
|
43
|
-
return new GoogleCompatibleEngine(options);
|
|
40
|
+
return new GoogleCompatibleEngine.Instance(options);
|
|
44
41
|
else if (endpointSpec.apiType === 'aliyun')
|
|
45
|
-
return new
|
|
42
|
+
return new AliyunCompatibleEngine.Instance(options);
|
|
46
43
|
else if (endpointSpec.apiType === 'anthropic')
|
|
47
|
-
return new AnthropicCompatibleEngine(options);
|
|
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 new OpenAIResponsesNativeEngine(options);
|
|
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 new GoogleNativeEngine(options);
|
|
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":"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"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { Pricing } from '
|
|
1
|
+
import type { Pricing } from '../../engine.ts';
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
-
export declare class
|
|
4
|
-
protected ctx:
|
|
5
|
-
constructor(ctx:
|
|
3
|
+
export declare class Billing {
|
|
4
|
+
protected ctx: Billing.Context;
|
|
5
|
+
constructor(ctx: Billing.Context);
|
|
6
6
|
charge(usage: Anthropic.Usage): number;
|
|
7
7
|
}
|
|
8
|
-
export declare namespace
|
|
8
|
+
export declare namespace Billing {
|
|
9
9
|
interface Context {
|
|
10
10
|
pricing: Pricing;
|
|
11
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"
|
|
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"}
|
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import { Function } from '
|
|
1
|
+
import { Function } from '../../function.ts';
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
|
-
export declare class
|
|
4
|
-
protected ctx:
|
|
5
|
-
constructor(ctx:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
protected
|
|
10
|
-
|
|
11
|
-
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>, parallelToolCall: boolean): Anthropic.ToolChoice;
|
|
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[];
|
|
12
11
|
}
|
|
13
|
-
export declare namespace
|
|
14
|
-
interface Context<in out fdm extends Function.
|
|
12
|
+
export declare namespace ToolCodec {
|
|
13
|
+
interface Context<in out fdm extends Function.Decl.Map.Proto> {
|
|
15
14
|
fdm: fdm;
|
|
16
15
|
}
|
|
17
16
|
}
|
|
@@ -1,73 +1,58 @@
|
|
|
1
|
-
import { Function } from
|
|
2
|
-
import { ResponseInvalid } from
|
|
3
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
1
|
+
import { Function } from "../../function.js";
|
|
2
|
+
import { ResponseInvalid } from "../../engine.js";
|
|
4
3
|
import Ajv from 'ajv';
|
|
5
|
-
import {} from '@sinclair/typebox';
|
|
6
4
|
const ajv = new Ajv();
|
|
7
|
-
export class
|
|
5
|
+
export class ToolCodec {
|
|
8
6
|
ctx;
|
|
9
7
|
constructor(ctx) {
|
|
10
8
|
this.ctx = ctx;
|
|
11
9
|
}
|
|
12
|
-
|
|
10
|
+
encodeFunctionCall(fc) {
|
|
13
11
|
throw new Error('Anthropic compatible engine requires native function calls.');
|
|
14
12
|
}
|
|
15
|
-
|
|
13
|
+
decodeFunctionCall(apifc) {
|
|
16
14
|
const fditem = this.ctx.fdm[apifc.name];
|
|
17
15
|
if (fditem) { }
|
|
18
16
|
else
|
|
19
17
|
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
return JSON.parse(apifc.input);
|
|
23
|
-
}
|
|
24
|
-
catch {
|
|
25
|
-
throw new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
26
|
-
}
|
|
27
|
-
})();
|
|
28
|
-
if (ajv.validate(fditem.paraschema, args)) { }
|
|
18
|
+
if (ajv.validate(fditem.parameters, apifc.input)) { }
|
|
29
19
|
else
|
|
30
20
|
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
31
|
-
return Function.Call.
|
|
21
|
+
return Function.Call.of({
|
|
32
22
|
id: apifc.id,
|
|
33
23
|
name: apifc.name,
|
|
34
|
-
args,
|
|
24
|
+
args: apifc.input,
|
|
35
25
|
});
|
|
36
26
|
}
|
|
37
|
-
|
|
27
|
+
encodeFunctionResponse(fr) {
|
|
38
28
|
if (fr.id) { }
|
|
39
29
|
else
|
|
40
30
|
throw new Error();
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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();
|
|
46
45
|
}
|
|
47
|
-
|
|
46
|
+
encodeFunctionDeclarationEntry(fdentry) {
|
|
48
47
|
return {
|
|
49
48
|
name: fdentry[0],
|
|
50
49
|
description: fdentry[1].description,
|
|
51
|
-
input_schema: fdentry[1].
|
|
50
|
+
input_schema: fdentry[1].parameters,
|
|
52
51
|
};
|
|
53
52
|
}
|
|
54
|
-
|
|
53
|
+
encodeFunctionDeclarationMap(fdm) {
|
|
55
54
|
const fdentries = Object.entries(fdm);
|
|
56
|
-
return fdentries.map(fdentry => this.
|
|
57
|
-
}
|
|
58
|
-
convertFromToolChoice(toolChoice, parallelToolCall) {
|
|
59
|
-
if (toolChoice === Function.ToolChoice.NONE)
|
|
60
|
-
return { type: 'none' };
|
|
61
|
-
else if (toolChoice === Function.ToolChoice.REQUIRED)
|
|
62
|
-
return { type: 'any', disable_parallel_tool_use: !parallelToolCall };
|
|
63
|
-
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
64
|
-
return { type: 'auto', disable_parallel_tool_use: !parallelToolCall };
|
|
65
|
-
else {
|
|
66
|
-
if (toolChoice.length === 1) { }
|
|
67
|
-
else
|
|
68
|
-
throw new Error();
|
|
69
|
-
return { type: 'tool', name: toolChoice[0], disable_parallel_tool_use: !parallelToolCall };
|
|
70
|
-
}
|
|
55
|
+
return fdentries.map(fdentry => this.encodeFunctionDeclarationEntry(fdentry));
|
|
71
56
|
}
|
|
72
57
|
}
|
|
73
58
|
//# sourceMappingURL=tool-codec.js.map
|