@zimtsui/brainswitch 0.0.39 → 0.0.41
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 +6 -0
- package/README.md +10 -23
- package/build/adaptor.d.ts +3 -1
- package/build/adaptor.js +40 -15
- package/build/adaptor.js.map +1 -1
- package/build/agentloop.js +0 -2
- package/build/agentloop.js.map +1 -1
- package/build/api-types/anthropic.js +14 -13
- package/build/api-types/anthropic.js.map +1 -1
- package/build/api-types/google.d.ts +8 -0
- package/build/api-types/google.js +42 -31
- package/build/api-types/google.js.map +1 -1
- package/build/api-types/openai-chat-completions.d.ts +1 -1
- package/build/api-types/openai-chat-completions.js +23 -12
- package/build/api-types/openai-chat-completions.js.map +1 -1
- package/build/api-types/openai-responses.js +9 -4
- package/build/api-types/openai-responses.js.map +1 -1
- package/build/compatible-engine.d.ts +1 -0
- package/build/compatible-engine.js +5 -1
- package/build/compatible-engine.js.map +1 -1
- package/build/compatible-engines.d/aliyun.js.map +1 -0
- package/build/{compatible-engines → compatible-engines.d}/anthropic.js +27 -10
- package/build/compatible-engines.d/anthropic.js.map +1 -0
- package/build/{compatible-engines/google/restful-engine.d.ts → compatible-engines.d/google.d.ts} +41 -21
- package/build/{compatible-engines/google/restful-engine.js → compatible-engines.d/google.js} +137 -23
- package/build/compatible-engines.d/google.js.map +1 -0
- package/build/{compatible-engines → compatible-engines.d}/openai-chatcompletions.d/monolith.js +9 -4
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.js.map +1 -0
- package/build/{compatible-engines → compatible-engines.d}/openai-chatcompletions.d/stream.js +30 -11
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.js.map +1 -0
- package/build/{compatible-engines → compatible-engines.d}/openai-chatcompletions.js +6 -3
- package/build/compatible-engines.d/openai-chatcompletions.js.map +1 -0
- package/build/{compatible-engines → compatible-engines.d}/openai-responses.js +18 -7
- package/build/compatible-engines.d/openai-responses.js.map +1 -0
- package/build/config.d.ts +1 -1
- package/build/endpoint-spec.d.ts +1 -1
- package/build/endpoint-spec.js +0 -3
- package/build/endpoint-spec.js.map +1 -1
- package/build/frontmatter.js +2 -2
- package/build/frontmatter.js.map +1 -1
- package/build/function.js +9 -4
- package/build/function.js.map +1 -1
- package/build/inference-context.d.ts +2 -2
- package/build/inference-context.js +1 -1
- package/build/inference-context.js.map +1 -1
- package/build/native-engines.d/google/agentloop.d.ts +12 -0
- package/build/native-engines.d/google/agentloop.js +42 -0
- package/build/native-engines.d/google/agentloop.js.map +1 -0
- package/build/native-engines.d/google/engine.d.ts +129 -0
- package/build/native-engines.d/google/engine.js +363 -0
- package/build/native-engines.d/google/engine.js.map +1 -0
- package/build/native-engines.d/google/exports.d.ts +4 -0
- package/build/native-engines.d/google/exports.js +5 -0
- package/build/native-engines.d/google/exports.js.map +1 -0
- package/build/native-engines.d/google/session.d.ts +64 -0
- package/build/native-engines.d/google/session.js +97 -0
- package/build/native-engines.d/google/session.js.map +1 -0
- package/build/{native-engines → native-engines.d}/openai-responses/agentloop.d.ts +2 -2
- package/build/{native-engines → native-engines.d}/openai-responses/agentloop.js +2 -6
- package/build/native-engines.d/openai-responses/agentloop.js.map +1 -0
- package/build/{native-engines → native-engines.d}/openai-responses/engine.d.ts +1 -1
- package/build/{native-engines → native-engines.d}/openai-responses/engine.js +49 -32
- package/build/native-engines.d/openai-responses/engine.js.map +1 -0
- package/build/native-engines.d/openai-responses/exports.js.map +1 -0
- package/build/{native-engines → native-engines.d}/openai-responses/session.d.ts +9 -9
- package/build/{native-engines → native-engines.d}/openai-responses/session.js +22 -9
- package/build/native-engines.d/openai-responses/session.js.map +1 -0
- package/build/native-engines.d/openai-responses/tool.js.map +1 -0
- package/build/session.d.ts +16 -16
- package/build/session.js +16 -9
- package/build/session.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -5
- package/build/compatible-engines/aliyun.js.map +0 -1
- package/build/compatible-engines/anthropic.js.map +0 -1
- package/build/compatible-engines/google/engine.d.ts +0 -43
- package/build/compatible-engines/google/engine.js +0 -138
- package/build/compatible-engines/google/engine.js.map +0 -1
- package/build/compatible-engines/google/restful-engine.js.map +0 -1
- package/build/compatible-engines/openai-chatcompletions.d/default.d.ts +0 -71
- package/build/compatible-engines/openai-chatcompletions.d/default.js +0 -185
- package/build/compatible-engines/openai-chatcompletions.d/default.js.map +0 -1
- package/build/compatible-engines/openai-chatcompletions.d/monolith.js.map +0 -1
- package/build/compatible-engines/openai-chatcompletions.d/stream.js.map +0 -1
- package/build/compatible-engines/openai-chatcompletions.js.map +0 -1
- package/build/compatible-engines/openai-responses.js.map +0 -1
- package/build/native-engines/openai-responses/agentloop.js.map +0 -1
- package/build/native-engines/openai-responses/engine.js.map +0 -1
- package/build/native-engines/openai-responses/exports.js.map +0 -1
- package/build/native-engines/openai-responses/session.js.map +0 -1
- package/build/native-engines/openai-responses/tool.js.map +0 -1
- /package/build/{compatible-engines → compatible-engines.d}/aliyun.d.ts +0 -0
- /package/build/{compatible-engines → compatible-engines.d}/aliyun.js +0 -0
- /package/build/{compatible-engines → compatible-engines.d}/anthropic.d.ts +0 -0
- /package/build/{compatible-engines → compatible-engines.d}/openai-chatcompletions.d/monolith.d.ts +0 -0
- /package/build/{compatible-engines → compatible-engines.d}/openai-chatcompletions.d/stream.d.ts +0 -0
- /package/build/{compatible-engines → compatible-engines.d}/openai-chatcompletions.d.ts +0 -0
- /package/build/{compatible-engines → compatible-engines.d}/openai-responses.d.ts +0 -0
- /package/build/{native-engines → native-engines.d}/openai-responses/exports.d.ts +0 -0
- /package/build/{native-engines → native-engines.d}/openai-responses/exports.js +0 -0
- /package/build/{native-engines → native-engines.d}/openai-responses/tool.d.ts +0 -0
- /package/build/{native-engines → native-engines.d}/openai-responses/tool.js +0 -0
package/DEVELOPMENT.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
## Architecture Diagram
|
|
2
|
+
|
|
1
3
|
```mermaid
|
|
2
4
|
classDiagram
|
|
3
5
|
|
|
@@ -15,3 +17,7 @@ CompatibleEngine <|.. OpenAIChatCompletionsCompatibleEngine
|
|
|
15
17
|
OpenAIChatCompletionsCompatibleEngine <|.. OpenAIChatCompletionsCompatibleMonolithEngine
|
|
16
18
|
OpenAIChatCompletionsCompatibleEngine <|.. OpenAIChatCompletionsCompatibleStreamEngine
|
|
17
19
|
```
|
|
20
|
+
|
|
21
|
+
## 继承
|
|
22
|
+
|
|
23
|
+
用组合代替继承以实现多重继承的效果。
|
package/README.md
CHANGED
|
@@ -14,10 +14,9 @@ Brainswitch 支持在一次会话中途切换模型并保持对话上下文,
|
|
|
14
14
|
|
|
15
15
|
## 支持服务商 API 类型
|
|
16
16
|
|
|
17
|
-
- OpenAI Chat Completions
|
|
18
17
|
- OpenAI Responses
|
|
19
18
|
- Google
|
|
20
|
-
-
|
|
19
|
+
- 阿里云
|
|
21
20
|
- Anthropic
|
|
22
21
|
|
|
23
22
|
## 安装
|
|
@@ -49,7 +48,6 @@ export type Config = {
|
|
|
49
48
|
model: string;
|
|
50
49
|
name: string;
|
|
51
50
|
apiType:
|
|
52
|
-
| 'openai-chatcompletions'
|
|
53
51
|
| 'openai-responses'
|
|
54
52
|
| 'google'
|
|
55
53
|
| 'aliyun'
|
|
@@ -70,8 +68,6 @@ export type Config = {
|
|
|
70
68
|
|
|
71
69
|
## 快速上手
|
|
72
70
|
|
|
73
|
-
下面演示:定义一个工具函数,先用 Google 做推理与工具调用,再在同一会话中切换到 OpenAI Responses 做最终的结构化总结。
|
|
74
|
-
|
|
75
71
|
```ts
|
|
76
72
|
import { Adaptor, agentloop, RoleMessage, Function, type InferenceContext, type Config, Session } from '@zimtsui/brainswitch';
|
|
77
73
|
import { Type } from '@sinclair/typebox';
|
|
@@ -83,28 +79,19 @@ import * as Presets from '@zimtsui/typelog/presets';
|
|
|
83
79
|
const config: Config = {
|
|
84
80
|
brainswitch: {
|
|
85
81
|
endpoints: {
|
|
86
|
-
'gpt-
|
|
87
|
-
name: 'GPT-
|
|
88
|
-
apiType: 'openai-chatcompletions',
|
|
89
|
-
baseUrl: 'https://api.openai.com/v1',
|
|
90
|
-
apiKey: process.env.OPENAI_API_KEY!,
|
|
91
|
-
model: 'gpt-4o-mini',
|
|
92
|
-
inputPrice: 5, outputPrice: 15, cachedPrice: 1,
|
|
93
|
-
rpm: 3000, timeout: 60_000,
|
|
94
|
-
},
|
|
95
|
-
'o4-mini': {
|
|
96
|
-
name: 'o4 mini',
|
|
82
|
+
'gpt-5-mini': {
|
|
83
|
+
name: 'GPT-5 mini',
|
|
97
84
|
apiType: 'openai-responses',
|
|
98
85
|
baseUrl: 'https://api.openai.com/v1',
|
|
99
86
|
apiKey: process.env.OPENAI_API_KEY!,
|
|
100
|
-
model: '
|
|
87
|
+
model: 'gpt-5-mini',
|
|
101
88
|
},
|
|
102
|
-
'gemini-
|
|
103
|
-
name: 'Gemini
|
|
89
|
+
'gemini-3-flash': {
|
|
90
|
+
name: 'Gemini 3 Flash',
|
|
104
91
|
apiType: 'google',
|
|
105
92
|
baseUrl: 'https://generativelanguage.googleapis.com',
|
|
106
93
|
apiKey: process.env.GOOGLE_API_KEY!,
|
|
107
|
-
model: 'gemini-
|
|
94
|
+
model: 'gemini-3-flash',
|
|
108
95
|
},
|
|
109
96
|
}
|
|
110
97
|
}
|
|
@@ -113,7 +100,7 @@ const config: Config = {
|
|
|
113
100
|
// 声明函数工具
|
|
114
101
|
const fdm = {
|
|
115
102
|
get_weather: {
|
|
116
|
-
description: '
|
|
103
|
+
description: '获取指定城市的天气',
|
|
117
104
|
paraschema: Type.Object({
|
|
118
105
|
city: Type.String(),
|
|
119
106
|
unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
|
|
@@ -133,7 +120,7 @@ type fdu = Function.Declaration.From<fdm>;
|
|
|
133
120
|
// 实现函数工具
|
|
134
121
|
export class Submission extends Error {
|
|
135
122
|
public constructor(public weather: string, public advice: string) {
|
|
136
|
-
super(
|
|
123
|
+
super();
|
|
137
124
|
}
|
|
138
125
|
}
|
|
139
126
|
const fnm: Function.Map<fdm> = {
|
|
@@ -167,7 +154,7 @@ const session: Session<fdu> = {
|
|
|
167
154
|
|
|
168
155
|
// 选择推理引擎
|
|
169
156
|
const adaptor = Adaptor.create(config);
|
|
170
|
-
const engine = adaptor.makeEngine('gpt-
|
|
157
|
+
const engine = adaptor.makeEngine('gpt-5-mini', fdm, Function.ToolChoice.REQUIRED);
|
|
171
158
|
|
|
172
159
|
// 使用 agentloop 驱动智能体循环,最多 8 轮对话
|
|
173
160
|
try {
|
package/build/adaptor.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Config } from '#config';
|
|
2
2
|
import { Function } from './function.ts';
|
|
3
3
|
import { type CompatibleEngine } from './compatible-engine.ts';
|
|
4
|
-
import { OpenAIResponsesNativeEngine } from './native-engines/openai-responses/engine.ts';
|
|
4
|
+
import { OpenAIResponsesNativeEngine } from './native-engines.d/openai-responses/engine.ts';
|
|
5
|
+
import { GoogleNativeEngine } from './native-engines.d/google/engine.ts';
|
|
5
6
|
export declare class Adaptor {
|
|
6
7
|
config: Config;
|
|
7
8
|
static create(config: Config): Adaptor;
|
|
@@ -9,4 +10,5 @@ export declare class Adaptor {
|
|
|
9
10
|
protected constructor(config: Config);
|
|
10
11
|
makeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, toolChoice?: Function.ToolChoice<fdm>, parallelToolCall?: boolean): CompatibleEngine<fdm>;
|
|
11
12
|
makeOpenAIResponsesNativeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, applyPatch?: boolean, toolChoice?: Function.ToolChoice<fdm>, parallelToolCall?: boolean): OpenAIResponsesNativeEngine<fdm>;
|
|
13
|
+
makeGoogleNativeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, toolChoice?: Function.ToolChoice<fdm>, codeExecution?: boolean, urlContext?: boolean, googleSearch?: boolean, parallelToolCall?: boolean): GoogleNativeEngine<fdm>;
|
|
12
14
|
}
|
package/build/adaptor.js
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { Config } from '#config';
|
|
2
2
|
import { Function } from "./function.js";
|
|
3
3
|
import {} from "./compatible-engine.js";
|
|
4
|
-
import assert from 'node:assert';
|
|
5
4
|
import { Throttle } from "./throttle.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
5
|
+
import { GoogleCompatibleEngine } from "./compatible-engines.d/google.js";
|
|
6
|
+
import { AliyunEngine } from "./compatible-engines.d/aliyun.js";
|
|
7
|
+
import { OpenAIResponsesCompatibleEngine } from "./compatible-engines.d/openai-responses.js";
|
|
8
|
+
import { AnthropicCompatibleEngine } from "./compatible-engines.d/anthropic.js";
|
|
9
|
+
import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses/engine.js";
|
|
10
|
+
import { GoogleNativeEngine } from "./native-engines.d/google/engine.js";
|
|
12
11
|
export class Adaptor {
|
|
13
12
|
config;
|
|
14
13
|
static create(config) {
|
|
@@ -24,9 +23,13 @@ export class Adaptor {
|
|
|
24
23
|
}
|
|
25
24
|
makeEngine(endpoint, functionDeclarationMap, toolChoice, parallelToolCall) {
|
|
26
25
|
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
27
|
-
|
|
26
|
+
if (endpointSpec) { }
|
|
27
|
+
else
|
|
28
|
+
throw new Error();
|
|
28
29
|
const throttle = this.throttles.get(endpoint);
|
|
29
|
-
|
|
30
|
+
if (throttle) { }
|
|
31
|
+
else
|
|
32
|
+
throw new Error();
|
|
30
33
|
const options = {
|
|
31
34
|
...endpointSpec,
|
|
32
35
|
functionDeclarationMap,
|
|
@@ -36,10 +39,8 @@ export class Adaptor {
|
|
|
36
39
|
};
|
|
37
40
|
if (endpointSpec.apiType === 'openai-responses')
|
|
38
41
|
return OpenAIResponsesCompatibleEngine.create(options);
|
|
39
|
-
else if (endpointSpec.apiType === 'openai-chatcompletions')
|
|
40
|
-
return OpenAIChatCompletionsCompatibleDefaultEngine.create(options);
|
|
41
42
|
else if (endpointSpec.apiType === 'google')
|
|
42
|
-
return
|
|
43
|
+
return GoogleCompatibleEngine.create(options);
|
|
43
44
|
else if (endpointSpec.apiType === 'aliyun')
|
|
44
45
|
return AliyunEngine.create(options);
|
|
45
46
|
else if (endpointSpec.apiType === 'anthropic')
|
|
@@ -49,9 +50,13 @@ export class Adaptor {
|
|
|
49
50
|
}
|
|
50
51
|
makeOpenAIResponsesNativeEngine(endpoint, functionDeclarationMap, applyPatch, toolChoice, parallelToolCall) {
|
|
51
52
|
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
52
|
-
|
|
53
|
+
if (endpointSpec?.apiType === 'openai-responses') { }
|
|
54
|
+
else
|
|
55
|
+
throw new Error();
|
|
53
56
|
const throttle = this.throttles.get(endpoint);
|
|
54
|
-
|
|
57
|
+
if (throttle) { }
|
|
58
|
+
else
|
|
59
|
+
throw new Error();
|
|
55
60
|
const options = {
|
|
56
61
|
...endpointSpec,
|
|
57
62
|
functionDeclarationMap,
|
|
@@ -60,8 +65,28 @@ export class Adaptor {
|
|
|
60
65
|
throttle,
|
|
61
66
|
applyPatch,
|
|
62
67
|
};
|
|
63
|
-
assert(endpointSpec.apiType === 'openai-responses');
|
|
64
68
|
return OpenAIResponsesNativeEngine.create(options);
|
|
65
69
|
}
|
|
70
|
+
makeGoogleNativeEngine(endpoint, functionDeclarationMap, toolChoice, codeExecution, urlContext, googleSearch, parallelToolCall) {
|
|
71
|
+
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
72
|
+
if (endpointSpec?.apiType === 'google') { }
|
|
73
|
+
else
|
|
74
|
+
throw new Error();
|
|
75
|
+
const throttle = this.throttles.get(endpoint);
|
|
76
|
+
if (throttle) { }
|
|
77
|
+
else
|
|
78
|
+
throw new Error();
|
|
79
|
+
const options = {
|
|
80
|
+
...endpointSpec,
|
|
81
|
+
functionDeclarationMap,
|
|
82
|
+
toolChoice,
|
|
83
|
+
parallelToolCall,
|
|
84
|
+
throttle,
|
|
85
|
+
codeExecution,
|
|
86
|
+
urlContext,
|
|
87
|
+
googleSearch,
|
|
88
|
+
};
|
|
89
|
+
return GoogleNativeEngine.create(options);
|
|
90
|
+
}
|
|
66
91
|
}
|
|
67
92
|
//# 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":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAyB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,
|
|
1
|
+
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAyB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,kCAAkC,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,kCAAkC,CAAC;AAChE,OAAO,EAAE,+BAA+B,EAAE,MAAM,4CAA4C,CAAC;AAC7F,OAAO,EAAE,yBAAyB,EAAE,MAAM,qCAAqC,CAAC;AAChF,OAAO,EAAE,2BAA2B,EAAE,MAAM,+CAA+C,CAAC;AAC5F,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAGzE,MAAM,OAAO,OAAO;IAMa;IALtB,MAAM,CAAC,MAAM,CAAC,MAAc;QAC/B,OAAO,IAAI,O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AAQ,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACxC,MAAM,OAAO,GAAoC;YAC7C,GAAG,YAAY;YACf,sBAAsB;YACtB,UAAU;YACV,gBAAgB;YAChB,QAAQ;YACR,aAAa;YACb,UAAU;YACV,YAAY;SACf,CAAC;QACF,OAAO,kBAAkB,CAAC,MAAM,CAAM,OAAO,CAAC,CAAC;IACnD,CAAC;CACJ"}
|
package/build/agentloop.js
CHANGED
|
@@ -2,7 +2,6 @@ import {} from "./inference-context.js";
|
|
|
2
2
|
import { RoleMessage } from "./session.js";
|
|
3
3
|
import { Function } from "./function.js";
|
|
4
4
|
import {} from "./compatible-engine.js";
|
|
5
|
-
import assert from 'node:assert';
|
|
6
5
|
/**
|
|
7
6
|
* @param session mutable
|
|
8
7
|
*/
|
|
@@ -20,7 +19,6 @@ export async function* agentloop(ctx, session, engine, fnm, limit = Number.POSIT
|
|
|
20
19
|
else if (part instanceof Function.Call) {
|
|
21
20
|
const fc = part;
|
|
22
21
|
const f = fnm[fc.name];
|
|
23
|
-
assert(f);
|
|
24
22
|
pfrs.push((async () => {
|
|
25
23
|
return Function.Response.create({
|
|
26
24
|
id: fc.id,
|
package/build/agentloop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentloop.js","sourceRoot":"","sources":["../src/agentloop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAgB,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAyB,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"agentloop.js","sourceRoot":"","sources":["../src/agentloop.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAgB,MAAM,cAAc,CAAC;AACzD,OAAO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BAChD,CAAC,CAAC,EAAE,CAAC,CAAC;YACV,CAAC;;gBAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QAC7B,CAAC;QACD,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,CAAC,eAAe,CAAC,OAAO,EAAE,WAAW,CAAC,IAAI,CAAC,MAAM,CAAM,GAAG,CAAC,CAAC,CAAC;IACvE,CAAC;IACD,MAAM,IAAI,SAAS,CAAC,yBAAyB,CAAC,+BAA+B,CAAC,CAAC;AACnF,CAAC;AAED,WAAiB,SAAS;IACtB,MAAa,yBAA0B,SAAQ,KAAK;KAAG;IAA1C,mCAAyB,4BAAiB,CAAA;AAC3D,CAAC,EAFgB,SAAS,KAAT,SAAS,QAEzB"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Function } from "../function.js";
|
|
2
2
|
import { Engine, ResponseInvalid } from "../engine.js";
|
|
3
3
|
import Anthropic from '@anthropic-ai/sdk';
|
|
4
|
-
import assert from 'node:assert';
|
|
5
4
|
import Ajv from 'ajv';
|
|
6
5
|
import {} from '@sinclair/typebox';
|
|
7
6
|
const ajv = new Ajv();
|
|
@@ -23,26 +22,24 @@ export var AnthropicEngine;
|
|
|
23
22
|
});
|
|
24
23
|
}
|
|
25
24
|
convertFromFunctionCall(fc) {
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
type: 'tool_use',
|
|
29
|
-
id: fc.id,
|
|
30
|
-
name: fc.name,
|
|
31
|
-
input: fc.args,
|
|
32
|
-
};
|
|
25
|
+
throw new Error('Anthropic compatible engine requires native function calls.');
|
|
33
26
|
}
|
|
34
27
|
convertToFunctionCall(apifc) {
|
|
35
28
|
const fditem = this.instance.fdm[apifc.name];
|
|
36
|
-
|
|
29
|
+
if (fditem) { }
|
|
30
|
+
else
|
|
31
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
37
32
|
const args = (() => {
|
|
38
33
|
try {
|
|
39
34
|
return JSON.parse(apifc.input);
|
|
40
35
|
}
|
|
41
36
|
catch (e) {
|
|
42
|
-
|
|
37
|
+
throw new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
43
38
|
}
|
|
44
39
|
})();
|
|
45
|
-
|
|
40
|
+
if (ajv.validate(fditem.paraschema, args)) { }
|
|
41
|
+
else
|
|
42
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
46
43
|
return Function.Call.create({
|
|
47
44
|
id: apifc.id,
|
|
48
45
|
name: apifc.name,
|
|
@@ -50,7 +47,9 @@ export var AnthropicEngine;
|
|
|
50
47
|
});
|
|
51
48
|
}
|
|
52
49
|
convertFromFunctionResponse(fr) {
|
|
53
|
-
|
|
50
|
+
if (fr.id) { }
|
|
51
|
+
else
|
|
52
|
+
throw new Error();
|
|
54
53
|
return {
|
|
55
54
|
type: 'tool_result',
|
|
56
55
|
tool_use_id: fr.id,
|
|
@@ -72,7 +71,9 @@ export var AnthropicEngine;
|
|
|
72
71
|
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
73
72
|
return { type: 'auto', disable_parallel_tool_use: !parallel };
|
|
74
73
|
else {
|
|
75
|
-
|
|
74
|
+
if (toolChoice.length === 1) { }
|
|
75
|
+
else
|
|
76
|
+
throw new Error();
|
|
76
77
|
return { type: 'tool', name: toolChoice[0], disable_parallel_tool_use: !parallel };
|
|
77
78
|
}
|
|
78
79
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/api-types/anthropic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/api-types/anthropic.ts"],"names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|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
function convertFromFunctionDeclarationEntry<fdm extends Function.Declaration.Map>(fdentry: Function.Declaration.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
24
25
|
function convertToFunctionCall<fdm extends Function.Declaration.Map>(googlefc: Google.FunctionCall, fdm: fdm): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
26
|
+
interface RestfulRequest {
|
|
27
|
+
contents: Google.Content[];
|
|
28
|
+
tools?: Google.Tool[];
|
|
29
|
+
toolConfig?: Google.ToolConfig;
|
|
30
|
+
systemInstruction?: Google.Content;
|
|
31
|
+
generationConfig?: Google.GenerationConfig;
|
|
32
|
+
}
|
|
25
33
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Engine, ResponseInvalid } from "../engine.js";
|
|
2
2
|
import { Function } from "../function.js";
|
|
3
3
|
import * as Google from '@google/genai';
|
|
4
|
-
import assert from 'node:assert';
|
|
5
4
|
import { CompatibleEngine } from "../compatible-engine.js";
|
|
6
5
|
import Ajv from 'ajv';
|
|
7
6
|
const ajv = new Ajv();
|
|
@@ -15,7 +14,9 @@ export var GoogleEngine;
|
|
|
15
14
|
constructor(instance, options) {
|
|
16
15
|
this.instance = instance;
|
|
17
16
|
this.parallel = options.parallelToolCall ?? true;
|
|
18
|
-
|
|
17
|
+
if (this.parallel) { }
|
|
18
|
+
else
|
|
19
|
+
throw new Error('Google API requires parallel tool calls.');
|
|
19
20
|
}
|
|
20
21
|
convertFromFunctionCall(fc) {
|
|
21
22
|
return {
|
|
@@ -28,41 +29,51 @@ export var GoogleEngine;
|
|
|
28
29
|
return GoogleEngine.convertToFunctionCall(googlefc, this.instance.fdm);
|
|
29
30
|
}
|
|
30
31
|
convertFromFunctionDeclarationEntry(fdentry) {
|
|
31
|
-
|
|
32
|
-
const parsed = JSON.parse(json, (key, value) => {
|
|
33
|
-
if (key === 'type' && typeof value === 'string') {
|
|
34
|
-
if (value === 'string')
|
|
35
|
-
return Google.Type.STRING;
|
|
36
|
-
else if (value === 'number')
|
|
37
|
-
return Google.Type.NUMBER;
|
|
38
|
-
else if (value === 'boolean')
|
|
39
|
-
return Google.Type.BOOLEAN;
|
|
40
|
-
else if (value === 'object')
|
|
41
|
-
return Google.Type.OBJECT;
|
|
42
|
-
else if (value === 'array')
|
|
43
|
-
return Google.Type.ARRAY;
|
|
44
|
-
else
|
|
45
|
-
throw new Error();
|
|
46
|
-
}
|
|
47
|
-
else if (key === 'additionalProperties' && typeof value === 'boolean')
|
|
48
|
-
return;
|
|
49
|
-
else
|
|
50
|
-
return value;
|
|
51
|
-
});
|
|
52
|
-
return {
|
|
53
|
-
name: fdentry[0],
|
|
54
|
-
description: fdentry[1].description,
|
|
55
|
-
parameters: parsed,
|
|
56
|
-
};
|
|
32
|
+
return GoogleEngine.convertFromFunctionDeclarationEntry(fdentry);
|
|
57
33
|
}
|
|
58
34
|
}
|
|
59
35
|
Base.Instance = Instance;
|
|
60
36
|
})(Base = GoogleEngine.Base || (GoogleEngine.Base = {}));
|
|
37
|
+
function convertFromFunctionDeclarationEntry(fdentry) {
|
|
38
|
+
const json = JSON.stringify(fdentry[1].paraschema);
|
|
39
|
+
const parsed = JSON.parse(json, (key, value) => {
|
|
40
|
+
if (key === 'type' && typeof value === 'string') {
|
|
41
|
+
if (value === 'string')
|
|
42
|
+
return Google.Type.STRING;
|
|
43
|
+
else if (value === 'number')
|
|
44
|
+
return Google.Type.NUMBER;
|
|
45
|
+
else if (value === 'boolean')
|
|
46
|
+
return Google.Type.BOOLEAN;
|
|
47
|
+
else if (value === 'object')
|
|
48
|
+
return Google.Type.OBJECT;
|
|
49
|
+
else if (value === 'array')
|
|
50
|
+
return Google.Type.ARRAY;
|
|
51
|
+
else
|
|
52
|
+
throw new Error();
|
|
53
|
+
}
|
|
54
|
+
else if (key === 'additionalProperties' && typeof value === 'boolean')
|
|
55
|
+
return;
|
|
56
|
+
else
|
|
57
|
+
return value;
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
name: fdentry[0],
|
|
61
|
+
description: fdentry[1].description,
|
|
62
|
+
parameters: parsed,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
GoogleEngine.convertFromFunctionDeclarationEntry = convertFromFunctionDeclarationEntry;
|
|
61
66
|
function convertToFunctionCall(googlefc, fdm) {
|
|
62
|
-
|
|
67
|
+
if (googlefc.name) { }
|
|
68
|
+
else
|
|
69
|
+
throw new Error();
|
|
63
70
|
const fditem = fdm[googlefc.name];
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
if (fditem) { }
|
|
72
|
+
else
|
|
73
|
+
throw new ResponseInvalid('Unknown function call', { cause: googlefc });
|
|
74
|
+
if (ajv.validate(fditem.paraschema, googlefc.args)) { }
|
|
75
|
+
else
|
|
76
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: googlefc });
|
|
66
77
|
return Function.Call.create({
|
|
67
78
|
id: googlefc.id,
|
|
68
79
|
name: googlefc.name,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/api-types/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/api-types/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,GAAG,GAAG,IAAI,GAAG,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jD,MAAM,IAAI,eAAe,CAAC,wCAAwC,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC9F,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;YACxB,EAAE,EAAE,QAAQ,CAAC,EAAE;YACf,IAAI,EAAE,QAAQ,CAAC,IAAI;YACnB,IAAI,EAAE,QAAQ,CAAC,IAAI;SAC0C,CAAC,CAAC;IACvE,CAAC;IAbe,kCAAqB,wBAapC,CAAA;AAUL,CAAC,EA1FgB,YAAY,KAAZ,YAAY,QA0F5B"}
|
|
@@ -24,7 +24,7 @@ export declare namespace OpenAIChatCompletionsEngine {
|
|
|
24
24
|
convertToFunctionCall(apifc: OpenAI.ChatCompletionMessageFunctionToolCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
25
25
|
convertFromFunctionResponse(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): OpenAI.ChatCompletionToolMessageParam;
|
|
26
26
|
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): OpenAI.ChatCompletionTool;
|
|
27
|
-
convertFromToolChoice(
|
|
27
|
+
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>): OpenAI.ChatCompletionToolChoiceOption;
|
|
28
28
|
calcCost(usage: OpenAI.CompletionUsage): number;
|
|
29
29
|
extractContent(completionContent: string): string;
|
|
30
30
|
handleFinishReason(completion: OpenAI.ChatCompletion, finishReason: OpenAI.ChatCompletion.Choice['finish_reason']): void;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ResponseInvalid } from "../engine.js";
|
|
2
2
|
import { Function } from "../function.js";
|
|
3
3
|
import OpenAI from 'openai';
|
|
4
|
-
import assert from 'node:assert';
|
|
5
4
|
import { Ajv } from 'ajv';
|
|
6
5
|
const ajv = new Ajv();
|
|
7
6
|
export var OpenAIChatCompletionsEngine;
|
|
@@ -16,7 +15,9 @@ export var OpenAIChatCompletionsEngine;
|
|
|
16
15
|
this.parallel = options.parallelToolCall ?? false;
|
|
17
16
|
}
|
|
18
17
|
convertFromFunctionCall(fc) {
|
|
19
|
-
|
|
18
|
+
if (fc.id) { }
|
|
19
|
+
else
|
|
20
|
+
throw new Error();
|
|
20
21
|
return {
|
|
21
22
|
id: fc.id,
|
|
22
23
|
type: 'function',
|
|
@@ -28,7 +29,9 @@ export var OpenAIChatCompletionsEngine;
|
|
|
28
29
|
}
|
|
29
30
|
convertToFunctionCall(apifc) {
|
|
30
31
|
const fditem = this.instance.fdm[apifc.function.name];
|
|
31
|
-
|
|
32
|
+
if (fditem) { }
|
|
33
|
+
else
|
|
34
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
32
35
|
const args = (() => {
|
|
33
36
|
try {
|
|
34
37
|
return JSON.parse(apifc.function.arguments);
|
|
@@ -37,7 +40,9 @@ export var OpenAIChatCompletionsEngine;
|
|
|
37
40
|
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
38
41
|
}
|
|
39
42
|
})();
|
|
40
|
-
|
|
43
|
+
if (ajv.validate(fditem.paraschema, args)) { }
|
|
44
|
+
else
|
|
45
|
+
throw new ResponseInvalid('Invalid function arguments', { cause: apifc });
|
|
41
46
|
return Function.Call.create({
|
|
42
47
|
id: apifc.id,
|
|
43
48
|
name: apifc.function.name,
|
|
@@ -45,7 +50,9 @@ export var OpenAIChatCompletionsEngine;
|
|
|
45
50
|
});
|
|
46
51
|
}
|
|
47
52
|
convertFromFunctionResponse(fr) {
|
|
48
|
-
|
|
53
|
+
if (fr.id) { }
|
|
54
|
+
else
|
|
55
|
+
throw new Error();
|
|
49
56
|
return {
|
|
50
57
|
role: 'tool',
|
|
51
58
|
tool_call_id: fr.id,
|
|
@@ -63,16 +70,18 @@ export var OpenAIChatCompletionsEngine;
|
|
|
63
70
|
},
|
|
64
71
|
};
|
|
65
72
|
}
|
|
66
|
-
convertFromToolChoice(
|
|
67
|
-
if (
|
|
73
|
+
convertFromToolChoice(toolChoice) {
|
|
74
|
+
if (toolChoice === Function.ToolChoice.NONE)
|
|
68
75
|
return 'none';
|
|
69
|
-
else if (
|
|
76
|
+
else if (toolChoice === Function.ToolChoice.REQUIRED)
|
|
70
77
|
return 'required';
|
|
71
|
-
else if (
|
|
78
|
+
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
72
79
|
return 'auto';
|
|
73
80
|
else {
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
if (toolChoice.length === 1) { }
|
|
82
|
+
else
|
|
83
|
+
throw new Error();
|
|
84
|
+
return { type: 'function', function: { name: toolChoice[0] } };
|
|
76
85
|
}
|
|
77
86
|
}
|
|
78
87
|
calcCost(usage) {
|
|
@@ -88,7 +97,9 @@ export var OpenAIChatCompletionsEngine;
|
|
|
88
97
|
handleFinishReason(completion, finishReason) {
|
|
89
98
|
if (finishReason === 'length')
|
|
90
99
|
throw new ResponseInvalid('Token limit exceeded.', { cause: completion });
|
|
91
|
-
|
|
100
|
+
if (['stop', 'tool_calls'].includes(finishReason)) { }
|
|
101
|
+
else
|
|
102
|
+
throw new ResponseInvalid('Abnormal finish reason', { cause: finishReason });
|
|
92
103
|
}
|
|
93
104
|
}
|
|
94
105
|
Base.Instance = Instance;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-chat-completions.js","sourceRoot":"","sources":["../../src/api-types/openai-chat-completions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAe,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,
|
|
1
|
+
{"version":3,"file":"openai-chat-completions.js","sourceRoot":"","sources":["../../src/api-types/openai-chat-completions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAe,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,MAAM,MAAM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B,IAAI,GAAJ,gCAAI,KAAJ,gCAAI,QA6FpB;AACL,CAAC,EAjHgB,2BAA2B,KAA3B,2BAA2B,QAiH3C"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
1
|
import OpenAI from 'openai';
|
|
3
2
|
import { Function } from "../function.js";
|
|
4
3
|
import { ResponseInvalid } from "../engine.js";
|
|
@@ -15,7 +14,9 @@ export var OpenAIResponsesEngine;
|
|
|
15
14
|
this.instance = instance;
|
|
16
15
|
}
|
|
17
16
|
convertFromFunctionResponse(fr) {
|
|
18
|
-
|
|
17
|
+
if (fr.id) { }
|
|
18
|
+
else
|
|
19
|
+
throw new Error();
|
|
19
20
|
return {
|
|
20
21
|
type: 'function_call_output',
|
|
21
22
|
call_id: fr.id,
|
|
@@ -40,7 +41,9 @@ export var OpenAIResponsesEngine;
|
|
|
40
41
|
}
|
|
41
42
|
convertToFunctionCall(apifc) {
|
|
42
43
|
const fditem = this.instance.fdm[apifc.name];
|
|
43
|
-
|
|
44
|
+
if (fditem) { }
|
|
45
|
+
else
|
|
46
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
44
47
|
const args = (() => {
|
|
45
48
|
try {
|
|
46
49
|
return JSON.parse(apifc.arguments);
|
|
@@ -49,7 +52,9 @@ export var OpenAIResponsesEngine;
|
|
|
49
52
|
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
50
53
|
}
|
|
51
54
|
})();
|
|
52
|
-
|
|
55
|
+
if (ajv.validate(fditem.paraschema, args)) { }
|
|
56
|
+
else
|
|
57
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
53
58
|
return Function.Call.create({
|
|
54
59
|
id: apifc.call_id,
|
|
55
60
|
name: apifc.name,
|