@zimtsui/typechat 0.0.93 → 0.0.95
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 +0 -6
- package/README.md +26 -36
- package/assets/verbatim/instruction.md +27 -25
- package/assets/verbatim/quotation.handlebars +1 -1
- package/assets/verbatim/system.handlebars +1 -0
- package/build/adaptor.d.ts +5 -23
- package/build/adaptor.js +14 -25
- package/build/adaptor.js.map +1 -1
- package/build/api-types/anthropic/billing.d.ts +3 -3
- package/build/api-types/anthropic/billing.js +6 -6
- package/build/api-types/anthropic/billing.js.map +1 -1
- package/build/api-types/anthropic/tool-codec.d.ts +3 -3
- package/build/api-types/anthropic/tool-codec.js +15 -11
- package/build/api-types/anthropic/tool-codec.js.map +1 -1
- package/build/api-types/google/billing.d.ts +3 -3
- package/build/api-types/google/billing.js +7 -7
- package/build/api-types/google/billing.js.map +1 -1
- package/build/api-types/google/tool-codec.d.ts +3 -3
- package/build/api-types/google/tool-codec.js +16 -34
- package/build/api-types/google/tool-codec.js.map +1 -1
- package/build/api-types/openai-chatcompletions/billing.d.ts +5 -5
- package/build/api-types/openai-chatcompletions/billing.js +7 -7
- package/build/api-types/openai-chatcompletions/billing.js.map +1 -1
- package/build/api-types/openai-chatcompletions/tool-codec.d.ts +5 -5
- package/build/api-types/openai-chatcompletions/tool-codec.js +17 -13
- package/build/api-types/openai-chatcompletions/tool-codec.js.map +1 -1
- package/build/api-types/openai-responses/billing.d.ts +3 -3
- package/build/api-types/openai-responses/billing.js +6 -6
- package/build/api-types/openai-responses/billing.js.map +1 -1
- package/build/api-types/openai-responses/tool-codec.d.ts +3 -3
- package/build/api-types/openai-responses/tool-codec.js +16 -12
- package/build/api-types/openai-responses/tool-codec.js.map +1 -1
- package/build/assets.d.ts +1 -1
- package/build/assets.js +6 -5
- package/build/assets.js.map +1 -1
- package/build/compatible-engine/session.d.ts +4 -16
- package/build/compatible-engine/session.js +8 -15
- package/build/compatible-engine/session.js.map +1 -1
- package/build/compatible-engine/validation.d.ts +7 -7
- package/build/compatible-engine/validation.js +33 -34
- package/build/compatible-engine/validation.js.map +1 -1
- package/build/compatible-engine.d/anthropic/message-codec.d.ts +4 -3
- package/build/compatible-engine.d/anthropic/message-codec.js +16 -22
- package/build/compatible-engine.d/anthropic/message-codec.js.map +1 -1
- package/build/compatible-engine.d/anthropic/transport.d.ts +10 -3
- package/build/compatible-engine.d/anthropic/transport.js +105 -101
- package/build/compatible-engine.d/anthropic/transport.js.map +1 -1
- package/build/compatible-engine.d/anthropic.d.ts +2 -0
- package/build/compatible-engine.d/anthropic.js +9 -1
- package/build/compatible-engine.d/anthropic.js.map +1 -1
- package/build/compatible-engine.d/google/message-codec.d.ts +4 -3
- package/build/compatible-engine.d/google/message-codec.js +16 -22
- package/build/compatible-engine.d/google/message-codec.js.map +1 -1
- package/build/compatible-engine.d/google/transport.d.ts +10 -3
- package/build/compatible-engine.d/google/transport.js +32 -24
- package/build/compatible-engine.d/google/transport.js.map +1 -1
- package/build/compatible-engine.d/google.d.ts +2 -0
- package/build/compatible-engine.d/google.js +23 -2
- package/build/compatible-engine.d/google.js.map +1 -1
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.d.ts +6 -5
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js +17 -24
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js.map +1 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.d.ts +33 -3
- package/build/compatible-engine.d/openai-chatcompletions/transport.js +182 -6
- package/build/compatible-engine.d/openai-chatcompletions/transport.js.map +1 -1
- package/build/compatible-engine.d/openai-chatcompletions.d.ts +10 -2
- package/build/compatible-engine.d/openai-chatcompletions.js +35 -0
- package/build/compatible-engine.d/openai-chatcompletions.js.map +1 -1
- package/build/compatible-engine.d/openai-responses/message-codec.d.ts +4 -6
- package/build/compatible-engine.d/openai-responses/message-codec.js +70 -74
- package/build/compatible-engine.d/openai-responses/message-codec.js.map +1 -1
- package/build/compatible-engine.d/openai-responses/transport.d.ts +14 -8
- package/build/compatible-engine.d/openai-responses/transport.js +64 -64
- package/build/compatible-engine.d/openai-responses/transport.js.map +1 -1
- package/build/compatible-engine.d/openai-responses.d.ts +2 -0
- package/build/compatible-engine.d/openai-responses.js +10 -2
- package/build/compatible-engine.d/openai-responses.js.map +1 -1
- package/build/compatible-engine.d.ts +2 -1
- package/build/compatible-engine.js +3 -3
- package/build/compatible-engine.js.map +1 -1
- package/build/config.d.ts +17 -19
- package/build/config.js +2 -4
- package/build/config.js.map +1 -1
- package/build/endpoint-spec.d.ts +15 -15
- package/build/endpoint-spec.js +2 -2
- package/build/endpoint-spec.js.map +1 -1
- package/build/engine/session.d.ts +31 -0
- package/build/engine/session.js +19 -0
- package/build/engine/session.js.map +1 -1
- package/build/engine/validation.d.ts +1 -7
- package/build/engine.d.ts +37 -20
- package/build/engine.js +154 -87
- package/build/engine.js.map +1 -1
- package/build/function.d.ts +1 -1
- package/build/native-engines.d/google/message-codec.d.ts +6 -3
- package/build/native-engines.d/google/message-codec.js +23 -27
- package/build/native-engines.d/google/message-codec.js.map +1 -1
- package/build/native-engines.d/google/transport.d.ts +13 -3
- package/build/native-engines.d/google/transport.js +41 -27
- package/build/native-engines.d/google/transport.js.map +1 -1
- package/build/native-engines.d/google/validation.d.ts +5 -6
- package/build/native-engines.d/google/validation.js +3 -6
- package/build/native-engines.d/google/validation.js.map +1 -1
- package/build/native-engines.d/google.d.ts +2 -2
- package/build/native-engines.d/google.js +22 -4
- package/build/native-engines.d/google.js.map +1 -1
- package/build/native-engines.d/openai-responses/choice-codec.d.ts +1 -1
- package/build/native-engines.d/openai-responses/choice-codec.js +12 -12
- package/build/native-engines.d/openai-responses/choice-codec.js.map +1 -1
- package/build/native-engines.d/openai-responses/message-codec.d.ts +5 -3
- package/build/native-engines.d/openai-responses/message-codec.js +59 -48
- package/build/native-engines.d/openai-responses/message-codec.js.map +1 -1
- package/build/native-engines.d/openai-responses/transport.d.ts +14 -6
- package/build/native-engines.d/openai-responses/transport.js +56 -54
- package/build/native-engines.d/openai-responses/transport.js.map +1 -1
- package/build/native-engines.d/openai-responses/validation.d.ts +7 -7
- package/build/native-engines.d/openai-responses/validation.js +31 -32
- package/build/native-engines.d/openai-responses/validation.js.map +1 -1
- package/build/native-engines.d/openai-responses.d.ts +2 -2
- package/build/native-engines.d/openai-responses.js +9 -4
- package/build/native-engines.d/openai-responses.js.map +1 -1
- package/build/repetition.d.ts +2 -1
- package/build/repetition.js +7 -42
- package/build/repetition.js.map +1 -1
- package/build/telemetry.d.ts +1 -0
- package/build/telemetry.js +1 -0
- package/build/telemetry.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/verbatim/codec/declarations.d.ts +0 -1
- package/build/verbatim/codec/declarations.js +1 -2
- package/build/verbatim/codec/declarations.js.map +1 -1
- package/build/verbatim/codec/helpers.d.ts +2 -0
- package/build/verbatim/codec/{escape.js → helpers.js} +5 -1
- package/build/verbatim/codec/helpers.js.map +1 -0
- package/build/verbatim/codec/quotation.d.ts +1 -2
- package/build/verbatim/codec/quotation.js +3 -4
- package/build/verbatim/codec/quotation.js.map +1 -1
- package/build/verbatim/codec/response.d.ts +0 -1
- package/build/verbatim/codec/response.js +1 -2
- package/build/verbatim/codec/response.js.map +1 -1
- package/build/verbatim/codec/{meta.d.ts → system.d.ts} +0 -1
- package/build/verbatim/codec/system.js +7 -0
- package/build/verbatim/codec/system.js.map +1 -0
- package/build/verbatim/codec.d.ts +1 -1
- package/build/verbatim/codec.js +1 -1
- package/build/verbatim/codec.js.map +1 -1
- package/package.json +8 -9
- package/assets/verbatim/meta.handlebars +0 -1
- package/build/compatible-engine.d/aliyun/transport.d.ts +0 -12
- package/build/compatible-engine.d/aliyun/transport.js +0 -7
- package/build/compatible-engine.d/aliyun/transport.js.map +0 -1
- package/build/compatible-engine.d/aliyun.d.ts +0 -21
- package/build/compatible-engine.d/aliyun.js +0 -38
- package/build/compatible-engine.d/aliyun.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.d.ts +0 -31
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js +0 -88
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.d.ts +0 -34
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js +0 -190
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js.map +0 -1
- package/build/verbatim/codec/escape.d.ts +0 -1
- package/build/verbatim/codec/escape.js.map +0 -1
- package/build/verbatim/codec/meta.js +0 -8
- package/build/verbatim/codec/meta.js.map +0 -1
package/DEVELOPMENT.md
CHANGED
|
@@ -9,12 +9,6 @@ Engine <|.. OpenAIResponsesNativeEngine
|
|
|
9
9
|
OpenAIResponsesNativeEngine o--> OpenAIResponsesNativeHelpers
|
|
10
10
|
OpenAIResponsesNativeHelpers o--> OpenAIResponsesHelpers
|
|
11
11
|
|
|
12
|
-
CompatibleEngine <|.. AliyunEngine
|
|
13
|
-
OpenAIChatCompletionsCompatibleHelpers o--> OpenAIChatCompletionsHelpers
|
|
14
|
-
OpenAIChatCompletionsCompatibleMonolithHelpers o--> OpenAIChatCompletionsCompatibleHelpers
|
|
15
|
-
OpenAIChatCompletionsCompatibleStreamHelpers o--> OpenAIChatCompletionsCompatibleHelpers
|
|
16
|
-
AliyunEngine o--> OpenAIChatCompletionsCompatibleStreamHelpers
|
|
17
|
-
|
|
18
12
|
CompatibleEngine <|.. OpenAICompatibleEngine
|
|
19
13
|
OpenAICompatibleEngine o--> OpenAIResponsesCompatibleHelpers
|
|
20
14
|
OpenAIResponsesCompatibleHelpers o--> OpenAIResponsesHelpers
|
package/README.md
CHANGED
|
@@ -2,51 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@zimtsui/typechat)
|
|
4
4
|
|
|
5
|
-
TypeChat 是一个强类型的 LLM
|
|
5
|
+
TypeChat 是一个强类型的 LLM 推理服务商 API 适配器。
|
|
6
6
|
|
|
7
7
|
## 支持服务商 API 类型
|
|
8
8
|
|
|
9
|
+
- OpenAI Chat Completions
|
|
9
10
|
- OpenAI Responses
|
|
10
11
|
- Google
|
|
11
|
-
- 阿里云 OpenAI Chat Completions Compatible
|
|
12
12
|
- Anthropic
|
|
13
13
|
|
|
14
|
-
## 核心概念
|
|
15
|
-
|
|
16
|
-
- `Session`:会话状态。
|
|
17
|
-
- `InferenceContext`:工作流环境,包含 [Typelemetry](https://github.com/zimtsui/typelemetry) Logger、`AbortSignal`、用户防止并发过载的[读写锁](https://github.com/zimtsui/typelocks)。
|
|
18
|
-
- `Engine`:推理引擎,从一个会话状态生成下一个会话状态。
|
|
19
|
-
- `Endpoint`:代表一家服务商的一个模型的 API 端点。
|
|
20
|
-
- `Adaptor`:Engine 工厂。
|
|
21
|
-
- `RoleMessage`:三类角色消息 `Developer`、`User`、`AI`,消息由 `Text/Function.Call/Response` 片段组成。
|
|
22
|
-
- `Function.Declaration.Map`:函数工具声明集合,使用 [JSON Schema](https://json-schema.org/) 描述函数参数。
|
|
23
|
-
|
|
24
14
|
## 配置
|
|
25
15
|
|
|
26
16
|
```ts
|
|
27
17
|
import { type Config } from '@zimtsui/typechat';
|
|
28
18
|
|
|
19
|
+
|
|
29
20
|
// 配置推理服务商 API 接入点
|
|
30
21
|
export const config: Config = {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
'
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
'
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
}
|
|
22
|
+
endpoints: {
|
|
23
|
+
'gpt-5.4-mini': {
|
|
24
|
+
name: 'GPT-5.4 mini',
|
|
25
|
+
apiType: 'openai-responses',
|
|
26
|
+
baseUrl: 'https://api.openai.com/v1',
|
|
27
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
28
|
+
model: 'gpt-5.4-mini',
|
|
29
|
+
},
|
|
30
|
+
'gemini-3-flash': {
|
|
31
|
+
name: 'Gemini 3 Flash',
|
|
32
|
+
apiType: 'google',
|
|
33
|
+
baseUrl: 'https://generativelanguage.googleapis.com',
|
|
34
|
+
apiKey: process.env.GOOGLE_API_KEY!,
|
|
35
|
+
model: 'gemini-3-flash',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
};
|
|
50
39
|
```
|
|
51
40
|
|
|
52
41
|
## 对话
|
|
@@ -82,7 +71,7 @@ console.log(response.getText());
|
|
|
82
71
|
|
|
83
72
|
```ts
|
|
84
73
|
import { Adaptor, agentloop, RoleMessage, Function, type Session, Structuring } from '@zimtsui/typechat';
|
|
85
|
-
import { Type } from '
|
|
74
|
+
import { Type } from 'typebox';
|
|
86
75
|
import { config } from './config.ts';
|
|
87
76
|
|
|
88
77
|
// 声明函数工具
|
|
@@ -148,17 +137,18 @@ try {
|
|
|
148
137
|
}
|
|
149
138
|
```
|
|
150
139
|
|
|
151
|
-
### XML
|
|
140
|
+
### XML 逐字频道
|
|
152
141
|
|
|
153
|
-
When a LLM outputs structured data in JSON format, if there are too many special characters in
|
|
142
|
+
When a LLM outputs structured data in JSON format (e.g., legacy LLM function calling), if there are too many special characters in a string property (e.g., a large LaTeX document, or a complex shell command), the LLM is prone to make mistakes in JSON escaping.
|
|
154
143
|
|
|
155
|
-
XML Verbatim Channel is designed to avoid escaping in
|
|
144
|
+
XML Verbatim Channel is designed to avoid escaping in LLM messages.
|
|
156
145
|
|
|
157
146
|
```ts
|
|
158
147
|
import { Adaptor, RoleMessage, type Session, Structuring, Verbatim } from '@zimtsui/typechat';
|
|
159
148
|
import Assets from '@zimtsui/typechat/assets';
|
|
160
149
|
import * as Codec from '@zimtsui/typechat/codec';
|
|
161
150
|
import { config } from './config.ts';
|
|
151
|
+
import { MIMEType } from 'whatwg-mimetype';
|
|
162
152
|
|
|
163
153
|
// 声明 XML Verbatim 频道
|
|
164
154
|
const vdm = {
|
|
@@ -167,7 +157,7 @@ const vdm = {
|
|
|
167
157
|
parameters: {
|
|
168
158
|
command: {
|
|
169
159
|
description: 'Bash 命令',
|
|
170
|
-
mimeType: 'text/plain',
|
|
160
|
+
mimeType: new MIMEType('text/plain'),
|
|
171
161
|
required: true as const,
|
|
172
162
|
},
|
|
173
163
|
},
|
|
@@ -2,57 +2,59 @@
|
|
|
2
2
|
|
|
3
3
|
## Motivation
|
|
4
4
|
|
|
5
|
-
When a LLM outputs structured data in JSON format, if there are too many special characters in
|
|
5
|
+
When a LLM outputs structured data in JSON format (e.g., legacy LLM function calling), if there are too many special characters in a string property (e.g., a large LaTeX document, or a complex shell command), the LLM is prone to make mistakes in JSON escaping.
|
|
6
6
|
|
|
7
|
-
XML Verbatim Channel is designed to avoid escaping in
|
|
7
|
+
XML Verbatim Channel is designed to avoid escaping in LLM messages.
|
|
8
8
|
|
|
9
|
-
## Declaration
|
|
9
|
+
## Declaration of Channels
|
|
10
10
|
|
|
11
11
|
The LLM system/user message is expected to declare all available XML Verbatim Channels in the form of
|
|
12
12
|
|
|
13
|
-
<verbatim:declaration name="
|
|
14
|
-
<verbatim:description>
|
|
15
|
-
<verbatim:parameter name="
|
|
16
|
-
<verbatim:description>
|
|
17
|
-
<verbatim:mime-type>
|
|
18
|
-
<verbatim:required>
|
|
13
|
+
<verbatim:declaration name="NAME OF CHANNEL">
|
|
14
|
+
<verbatim:description>DESCRIPTION OF CHANNEL</verbatim:description>
|
|
15
|
+
<verbatim:parameter name="NAME OF PARAMETER 1">
|
|
16
|
+
<verbatim:description>DESCRIPTION OF THIS PARAMETER</verbatim:description>
|
|
17
|
+
<verbatim:mime-type>MIME TYPE OF THIS PARAMETER</verbatim:mime-type>
|
|
18
|
+
<verbatim:required>WHETHER THIS PARAMETER IS REQUIRED</verbatim:required>
|
|
19
19
|
</verbatim:parameter>
|
|
20
|
-
<verbatim:parameter name="
|
|
21
|
-
<verbatim:description>
|
|
22
|
-
<verbatim:mime-type>
|
|
23
|
-
<verbatim:required>
|
|
20
|
+
<verbatim:parameter name="NAME OF PARAMETER 2">
|
|
21
|
+
<verbatim:description>DESCRIPTION OF THIS PARAMETER</verbatim:description>
|
|
22
|
+
<verbatim:mime-type>MIME TYPE OF THIS PARAMETER</verbatim:mime-type>
|
|
23
|
+
<verbatim:required>WHETHER THIS PARAMETER IS REQUIRED</verbatim:required>
|
|
24
24
|
</verbatim:parameter>
|
|
25
25
|
</verbatim:declaration>
|
|
26
26
|
|
|
27
|
-
## Request
|
|
27
|
+
## Request through Channels
|
|
28
28
|
|
|
29
29
|
You can make a request through a channel in the form of
|
|
30
30
|
|
|
31
|
-
<verbatim:request name="
|
|
32
|
-
<verbatim:parameter name="
|
|
33
|
-
<verbatim:parameter name="
|
|
31
|
+
<verbatim:request name="NAME OF CHANNEL">
|
|
32
|
+
<verbatim:parameter name="NAME OF PARAMETER 1"><![CDATA[ARGUMENT OF THIS PARAMETER]]></verbatim:parameter>
|
|
33
|
+
<verbatim:parameter name="NAME OF PARAMETER 2"><![CDATA[ARGUMENT OF THIS PARAMETER]]></verbatim:parameter>
|
|
34
34
|
</verbatim:request>
|
|
35
35
|
|
|
36
36
|
- The only attribute of <verbatim:request> is `name`. Additional attributes will be ignored.
|
|
37
37
|
- The only attribute of <verbatim:parameter> is `name`. Additional attributes will be ignored.
|
|
38
38
|
- All arguments must be wrapped in CDATA.
|
|
39
39
|
|
|
40
|
-
## Response
|
|
40
|
+
## Response from Channels
|
|
41
41
|
|
|
42
|
-
The LLM
|
|
42
|
+
The LLM user message may contain the responses of your request in the form of
|
|
43
43
|
|
|
44
|
-
<verbatim:response name="
|
|
44
|
+
<verbatim:response name="NAME OF CHANNEL"><![CDATA[RESPONSE]]></verbatim:response>
|
|
45
45
|
|
|
46
46
|
Not all requests have a response.
|
|
47
47
|
|
|
48
|
-
## Quotation
|
|
48
|
+
## Verbatim Quotation
|
|
49
49
|
|
|
50
50
|
The LLM system/user message may contain verbatim quotations in the form of
|
|
51
51
|
|
|
52
|
-
<verbatim:quotation><![CDATA[QUOTATION]]></verbatim:quotation>
|
|
52
|
+
<verbatim:quotation author="AUTHOR OF THIS QUOTATION"><![CDATA[QUOTATION]]></verbatim:quotation>
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
The attribute `author` is optional.
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
## System Information
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
The LLM system/user message may contain system information from the AI agent framework in the form of
|
|
59
|
+
|
|
60
|
+
<verbatim:system></verbatim:system>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<verbatim:quotation mime-type="{{XmlAttr this.mimeType}}"><![CDATA[{{{this.text}}}]]></verbatim:quotation>
|
|
1
|
+
<verbatim:quotation{{#if (isDefined this.author)}} author="{{XmlAttr this.author}}"{{/if}} mime-type="{{XmlAttr this.mimeType}}"><![CDATA[{{{this.text}}}]]></verbatim:quotation>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<verbatim:system>{{this.text}}</verbatim:system>
|
package/build/adaptor.d.ts
CHANGED
|
@@ -5,41 +5,23 @@ import { Throttle } from './throttle.ts';
|
|
|
5
5
|
import { OpenAIResponsesNativeEngine } from './native-engines.d/openai-responses.ts';
|
|
6
6
|
import { GoogleNativeEngine } from './native-engines.d/google.ts';
|
|
7
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';
|
|
10
8
|
export declare class Adaptor {
|
|
11
9
|
config: Config;
|
|
12
10
|
static create(config: Config): Adaptor;
|
|
13
11
|
protected throttles: Map<string, Throttle>;
|
|
14
12
|
protected constructor(config: Config);
|
|
15
|
-
makeCompatibleEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.
|
|
13
|
+
makeCompatibleEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.CompatibleEngineOptions<fdm, vdm>): CompatibleEngine<fdm, vdm>;
|
|
16
14
|
makeOpenAIResponsesNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.OpenAIResponsesNativeEngineOptions<fdm, vdm>): OpenAIResponsesNativeEngine<fdm, vdm>;
|
|
17
15
|
makeGoogleNativeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.GoogleNativeEngineOptions<fdm, vdm>): GoogleNativeEngine<fdm, vdm>;
|
|
18
16
|
}
|
|
19
17
|
export declare namespace Adaptor {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
endpoint: string;
|
|
23
|
-
functionDeclarationMap: fdm;
|
|
24
|
-
verbatimDeclarationMap: vdm;
|
|
25
|
-
structuringChoice?: Structuring.Choice.From<fdm, vdm>;
|
|
26
|
-
}
|
|
18
|
+
interface CompatibleEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> extends Omit<CompatibleEngine.Options<fdm, vdm>, 'endpointSpec' | 'throttle'> {
|
|
19
|
+
endpoint: string;
|
|
27
20
|
}
|
|
28
|
-
interface OpenAIResponsesNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
21
|
+
interface OpenAIResponsesNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> extends Omit<OpenAIResponsesNativeEngine.Options<fdm, vdm>, 'endpointSpec' | 'throttle'> {
|
|
29
22
|
endpoint: string;
|
|
30
|
-
functionDeclarationMap: fdm;
|
|
31
|
-
verbatimDeclarationMap: vdm;
|
|
32
|
-
applyPatch?: boolean;
|
|
33
|
-
structuringChoice?: OpenAIResponsesNativeStructuring.Choice.From<fdm, vdm>;
|
|
34
23
|
}
|
|
35
|
-
interface GoogleNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
24
|
+
interface GoogleNativeEngineOptions<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> extends Omit<GoogleNativeEngine.Options<fdm, vdm>, 'endpointSpec' | 'throttle'> {
|
|
36
25
|
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
26
|
}
|
|
45
27
|
}
|
package/build/adaptor.js
CHANGED
|
@@ -2,9 +2,9 @@ import { Throttle } from "./throttle.js";
|
|
|
2
2
|
import { GoogleCompatibleEngine } from "./compatible-engine.d/google.js";
|
|
3
3
|
import { OpenAIResponsesCompatibleEngine } from "./compatible-engine.d/openai-responses.js";
|
|
4
4
|
import { AnthropicCompatibleEngine } from "./compatible-engine.d/anthropic.js";
|
|
5
|
-
import { AliyunCompatibleEngine } from "./compatible-engine.d/aliyun.js";
|
|
6
5
|
import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses.js";
|
|
7
6
|
import { GoogleNativeEngine } from "./native-engines.d/google.js";
|
|
7
|
+
import { OpenAIChatCompletionsCompatibleEngine } from "./compatible-engine.d/openai-chatcompletions.js";
|
|
8
8
|
export class Adaptor {
|
|
9
9
|
config;
|
|
10
10
|
static create(config) {
|
|
@@ -13,13 +13,13 @@ export class Adaptor {
|
|
|
13
13
|
throttles = new Map();
|
|
14
14
|
constructor(config) {
|
|
15
15
|
this.config = config;
|
|
16
|
-
for (const endpointId in this.config.
|
|
17
|
-
const rpm = this.config.
|
|
16
|
+
for (const endpointId in this.config.endpoints) {
|
|
17
|
+
const rpm = this.config.endpoints[endpointId].rpm ?? Number.POSITIVE_INFINITY;
|
|
18
18
|
this.throttles.set(endpointId, new Throttle(rpm));
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
makeCompatibleEngine(adaptorOptions) {
|
|
22
|
-
const endpointSpec = this.config.
|
|
22
|
+
const endpointSpec = this.config.endpoints[adaptorOptions.endpoint];
|
|
23
23
|
if (endpointSpec) { }
|
|
24
24
|
else
|
|
25
25
|
throw new Error();
|
|
@@ -28,25 +28,23 @@ export class Adaptor {
|
|
|
28
28
|
else
|
|
29
29
|
throw new Error();
|
|
30
30
|
const options = {
|
|
31
|
-
...
|
|
32
|
-
|
|
33
|
-
verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
|
|
34
|
-
structuringChoice: adaptorOptions.structuringChoice,
|
|
31
|
+
...adaptorOptions,
|
|
32
|
+
endpointSpec,
|
|
35
33
|
throttle,
|
|
36
34
|
};
|
|
37
35
|
if (endpointSpec.apiType === 'openai-responses')
|
|
38
36
|
return new OpenAIResponsesCompatibleEngine.Instance(options);
|
|
39
37
|
else if (endpointSpec.apiType === 'google')
|
|
40
38
|
return new GoogleCompatibleEngine.Instance(options);
|
|
41
|
-
else if (endpointSpec.apiType === 'aliyun')
|
|
42
|
-
return new AliyunCompatibleEngine.Instance(options);
|
|
43
39
|
else if (endpointSpec.apiType === 'anthropic')
|
|
44
40
|
return new AnthropicCompatibleEngine.Instance(options);
|
|
41
|
+
else if (endpointSpec.apiType === 'openai-chatcompletions')
|
|
42
|
+
return new OpenAIChatCompletionsCompatibleEngine.Instance(options);
|
|
45
43
|
else
|
|
46
44
|
throw new Error();
|
|
47
45
|
}
|
|
48
46
|
makeOpenAIResponsesNativeEngine(adaptorOptions) {
|
|
49
|
-
const endpointSpec = this.config.
|
|
47
|
+
const endpointSpec = this.config.endpoints[adaptorOptions.endpoint];
|
|
50
48
|
if (endpointSpec?.apiType === 'openai-responses') { }
|
|
51
49
|
else
|
|
52
50
|
throw new Error();
|
|
@@ -55,17 +53,14 @@ export class Adaptor {
|
|
|
55
53
|
else
|
|
56
54
|
throw new Error();
|
|
57
55
|
const options = {
|
|
58
|
-
...
|
|
59
|
-
|
|
60
|
-
verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
|
|
61
|
-
structuringChoice: adaptorOptions.structuringChoice,
|
|
56
|
+
...adaptorOptions,
|
|
57
|
+
endpointSpec,
|
|
62
58
|
throttle,
|
|
63
|
-
applyPatch: adaptorOptions.applyPatch,
|
|
64
59
|
};
|
|
65
60
|
return new OpenAIResponsesNativeEngine.Instance(options);
|
|
66
61
|
}
|
|
67
62
|
makeGoogleNativeEngine(adaptorOptions) {
|
|
68
|
-
const endpointSpec = this.config.
|
|
63
|
+
const endpointSpec = this.config.endpoints[adaptorOptions.endpoint];
|
|
69
64
|
if (endpointSpec?.apiType === 'google') { }
|
|
70
65
|
else
|
|
71
66
|
throw new Error();
|
|
@@ -74,15 +69,9 @@ export class Adaptor {
|
|
|
74
69
|
else
|
|
75
70
|
throw new Error();
|
|
76
71
|
const options = {
|
|
77
|
-
...
|
|
78
|
-
|
|
79
|
-
verbatimDeclarationMap: adaptorOptions.verbatimDeclarationMap,
|
|
80
|
-
structuringChoice: adaptorOptions.structuringChoice,
|
|
81
|
-
parallelToolCall: adaptorOptions.parallelToolCall,
|
|
72
|
+
...adaptorOptions,
|
|
73
|
+
endpointSpec,
|
|
82
74
|
throttle,
|
|
83
|
-
codeExecution: adaptorOptions.codeExecution,
|
|
84
|
-
urlContext: adaptorOptions.urlContext,
|
|
85
|
-
googleSearch: adaptorOptions.googleSearch,
|
|
86
75
|
};
|
|
87
76
|
return new GoogleNativeEngine.Instance(options);
|
|
88
77
|
}
|
package/build/adaptor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],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Q,CAAC,CAAC;QAC7D,IAAI,QAAQ,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACxC,MAAM,OAAO,GAAkD;YAC3D,GAAG,cAAc;YACjB,YAAY;YACZ,QAAQ;SACX,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,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QACpE,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,cAAc;YACjB,YAAY;YACZ,QAAQ;SACX,CAAC;QACF,OAAO,IAAI,kBAAkB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;CACJ"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Pricing } from '../../engine.ts';
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
3
|
export declare class Billing {
|
|
4
|
-
protected
|
|
5
|
-
constructor(
|
|
4
|
+
protected pricing: Pricing;
|
|
5
|
+
constructor(options: Billing.Options);
|
|
6
6
|
charge(usage: Anthropic.Usage): number;
|
|
7
7
|
}
|
|
8
8
|
export declare namespace Billing {
|
|
9
|
-
interface
|
|
9
|
+
interface Options {
|
|
10
10
|
pricing: Pricing;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
export class Billing {
|
|
2
|
-
|
|
3
|
-
constructor(
|
|
4
|
-
this.
|
|
2
|
+
pricing;
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.pricing = options.pricing;
|
|
5
5
|
}
|
|
6
6
|
charge(usage) {
|
|
7
7
|
const cacheHitTokenCount = usage.cache_read_input_tokens || 0;
|
|
8
8
|
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
9
|
-
return (this.
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
9
|
+
return (this.pricing.inputPrice * cacheMissTokenCount / 1e6 +
|
|
10
|
+
this.pricing.cachePrice * cacheHitTokenCount / 1e6 +
|
|
11
|
+
this.pricing.outputPrice * usage.output_tokens / 1e6);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
//# sourceMappingURL=billing.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,OAAO;
|
|
1
|
+
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/anthropic/billing.ts"],"names":[],"mappings":"AAIA,MAAM,OAAO,OAAO;IACN,OAAO,CAAU;IAC3B,YAAmB,OAAwB;QACvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IACnC,CAAC;IAEM,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,OAAO,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YACnD,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YAClD,IAAI,CAAC,OAAO,CAAC,WAAW,GAAG,KAAK,CAAC,aAAa,GAAG,GAAG,CACvD,CAAC;IACN,CAAC;CACJ"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Function } from '../../function.ts';
|
|
2
2
|
import Anthropic from '@anthropic-ai/sdk';
|
|
3
3
|
export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
|
|
4
|
-
protected
|
|
5
|
-
constructor(
|
|
4
|
+
protected fdm: fdm;
|
|
5
|
+
constructor(options: ToolCodec.Options<fdm>);
|
|
6
6
|
encodeFunctionCall(fc: Function.Call.From<fdm>): Anthropic.ToolUseBlock;
|
|
7
7
|
decodeFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.From<fdm>;
|
|
8
8
|
encodeFunctionResponse(fr: Function.Response.From<fdm>): Anthropic.ToolResultBlockParam;
|
|
@@ -10,7 +10,7 @@ export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
|
|
|
10
10
|
encodeFunctionDeclarationMap(fdm: fdm): Anthropic.Tool[];
|
|
11
11
|
}
|
|
12
12
|
export declare namespace ToolCodec {
|
|
13
|
-
interface
|
|
13
|
+
interface Options<in out fdm extends Function.Decl.Map.Proto> {
|
|
14
14
|
fdm: fdm;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
import { Function } from "../../function.js";
|
|
2
|
-
import {
|
|
3
|
-
import Ajv from 'ajv';
|
|
4
|
-
const ajv = new Ajv();
|
|
2
|
+
import { Parse, ParseError } from 'typebox/schema';
|
|
5
3
|
export class ToolCodec {
|
|
6
|
-
|
|
7
|
-
constructor(
|
|
8
|
-
this.
|
|
4
|
+
fdm;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
this.fdm = options.fdm;
|
|
9
7
|
}
|
|
10
8
|
encodeFunctionCall(fc) {
|
|
11
9
|
throw new Error('Anthropic compatible engine requires native function calls.');
|
|
12
10
|
}
|
|
13
11
|
decodeFunctionCall(apifc) {
|
|
14
|
-
const fditem = this.
|
|
12
|
+
const fditem = this.fdm[apifc.name];
|
|
15
13
|
if (fditem) { }
|
|
16
14
|
else
|
|
17
|
-
throw new
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
throw new SyntaxError('Unknown function call', { cause: apifc });
|
|
16
|
+
try {
|
|
17
|
+
Parse(fditem.parameters, apifc.input);
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
if (e instanceof ParseError)
|
|
21
|
+
throw new SyntaxError('Invalid arguments of function call.', { cause: e });
|
|
22
|
+
else
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
21
25
|
return Function.Call.of({
|
|
22
26
|
id: apifc.id,
|
|
23
27
|
name: apifc.name,
|
|
@@ -1 +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;
|
|
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;AAG7C,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAInD,MAAM,OAAO,SAAS;IACR,GAAG,CAAM;IACnB,YAAmB,OAA+B;QAC9C,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;IAC3B,CAAC;IAEM,kBAAkB,CACrB,EAA2B;QAE3B,MAAM,IAAI,KAAK,CAAC,6DAA6D,CAAC,CAAC;IACnF,CAAC;IAEM,kBAAkB,CACrB,KAA6B;QAE7B,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACpC,IAAI,MAAM,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,WAAW,CAAC,uBAAuB,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;Q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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"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import * as Google from '@google/genai';
|
|
2
2
|
import type { Pricing } from '../../engine.ts';
|
|
3
3
|
export declare class Billing {
|
|
4
|
-
protected
|
|
5
|
-
constructor(
|
|
4
|
+
protected pricing: Pricing;
|
|
5
|
+
constructor(options: Billing.Options);
|
|
6
6
|
charge(usageMetadata: Google.GenerateContentResponseUsageMetadata): number;
|
|
7
7
|
}
|
|
8
8
|
export declare namespace Billing {
|
|
9
|
-
interface
|
|
9
|
+
interface Options {
|
|
10
10
|
pricing: Pricing;
|
|
11
11
|
}
|
|
12
12
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { loggers } from "../../telemetry.js";
|
|
2
2
|
export class Billing {
|
|
3
|
-
|
|
4
|
-
constructor(
|
|
5
|
-
this.
|
|
3
|
+
pricing;
|
|
4
|
+
constructor(options) {
|
|
5
|
+
this.pricing = options.pricing;
|
|
6
6
|
}
|
|
7
7
|
charge(usageMetadata) {
|
|
8
8
|
loggers.message.info(usageMetadata);
|
|
@@ -13,10 +13,10 @@ export class Billing {
|
|
|
13
13
|
const cacheHitTokenCount = usageMetadata.cachedContentTokenCount ?? 0;
|
|
14
14
|
const cacheMissTokenCount = usageMetadata.promptTokenCount - cacheHitTokenCount;
|
|
15
15
|
const thinkingTokenCount = usageMetadata.thoughtsTokenCount ?? 0;
|
|
16
|
-
const cost = this.
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
16
|
+
const cost = this.pricing.inputPrice * cacheMissTokenCount / 1e6 +
|
|
17
|
+
this.pricing.cachePrice * cacheHitTokenCount / 1e6 +
|
|
18
|
+
this.pricing.outputPrice * candidatesTokenCount / 1e6 +
|
|
19
|
+
this.pricing.outputPrice * thinkingTokenCount / 1e6;
|
|
20
20
|
return cost;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/google/billing.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAG7C,MAAM,OAAO,OAAO;
|
|
1
|
+
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../../../src/api-types/google/billing.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAG7C,MAAM,OAAO,OAAO;IACN,OAAO,CAAU;IAC3B,YAAmB,OAAwB;QACvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;IACnC,CAAC;IAEM,MAAM,CAAC,aAA0D;QACpE,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QAEpC,IAAI,aAAa,CAAC,gBAAgB,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,CAAC,6BAA6B,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;QACrH,MAAM,oBAAoB,GAAG,aAAa,CAAC,oBAAoB,IAAI,CAAC,CAAC;QACrE,MAAM,kBAAkB,GAAG,aAAa,CAAC,uBAAuB,IAAI,CAAC,CAAC;QACtE,MAAM,mBAAmB,GAAG,aAAa,CAAC,gBAAgB,GAAG,kBAAkB,CAAC;QAChF,MAAM,kBAAkB,GAAG,aAAa,CAAC,kBAAkB,IAAI,CAAC,CAAC;QACjE,MAAM,IAAI,GACN,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YACnD,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YAClD,IAAI,CAAC,OAAO,CAAC,WAAW,GAAG,oBAAoB,GAAG,GAAG;YACrD,IAAI,CAAC,OAAO,CAAC,WAAW,GAAG,kBAAkB,GAAG,GAAG,CAAC;QACxD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Function } from '../../function.ts';
|
|
2
2
|
import * as Google from '@google/genai';
|
|
3
3
|
export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
|
|
4
|
-
protected
|
|
5
|
-
constructor(
|
|
4
|
+
protected fdm: fdm;
|
|
5
|
+
constructor(options: ToolCodec.Options<fdm>);
|
|
6
6
|
encodeFunctionCall(fc: Function.Call.From<fdm>): Google.FunctionCall;
|
|
7
7
|
encodeFunctionDeclarationMap(fdm: fdm): Google.FunctionDeclaration[];
|
|
8
8
|
protected encodeFunctionDeclarationEntry(fdentry: Function.Decl.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
@@ -10,7 +10,7 @@ export declare class ToolCodec<in out fdm extends Function.Decl.Map.Proto> {
|
|
|
10
10
|
encodeFunctionResponse(fr: Function.Response.From<fdm>): Google.Part;
|
|
11
11
|
}
|
|
12
12
|
export declare namespace ToolCodec {
|
|
13
|
-
interface
|
|
13
|
+
interface Options<in out fdm extends Function.Decl.Map.Proto> {
|
|
14
14
|
fdm: fdm;
|
|
15
15
|
}
|
|
16
16
|
}
|