@zimtsui/brainswitch 0.0.40 → 0.0.42
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 +9 -20
- package/build/adaptor.d.ts +4 -2
- package/build/adaptor.js +27 -10
- package/build/adaptor.js.map +1 -1
- package/build/agentloop.d.ts +1 -1
- package/build/agentloop.js +2 -4
- package/build/agentloop.js.map +1 -1
- package/build/api-types/anthropic.d.ts +18 -22
- package/build/api-types/anthropic.js +81 -72
- package/build/api-types/anthropic.js.map +1 -1
- package/build/api-types/google.d.ts +13 -18
- package/build/api-types/google.js +32 -33
- package/build/api-types/google.js.map +1 -1
- package/build/api-types/openai-chat-completions.d.ts +17 -19
- package/build/api-types/openai-chat-completions.js +101 -85
- package/build/api-types/openai-chat-completions.js.map +1 -1
- package/build/api-types/openai-responses.d.ts +13 -13
- package/build/api-types/openai-responses.js +59 -51
- package/build/api-types/openai-responses.js.map +1 -1
- package/build/compatible-engine.d.ts +19 -24
- package/build/compatible-engine.js +54 -56
- package/build/compatible-engine.js.map +1 -1
- package/build/compatible-engines.d/aliyun.d.ts +38 -55
- package/build/compatible-engines.d/aliyun.js +91 -143
- package/build/compatible-engines.d/aliyun.js.map +1 -1
- package/build/compatible-engines.d/anthropic.d.ts +35 -60
- package/build/compatible-engines.d/anthropic.js +233 -282
- package/build/compatible-engines.d/anthropic.js.map +1 -1
- package/build/compatible-engines.d/google.d.ts +38 -59
- package/build/compatible-engines.d/google.js +172 -231
- package/build/compatible-engines.d/google.js.map +1 -1
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.d.ts +7 -14
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.js +64 -66
- package/build/compatible-engines.d/openai-chatcompletions.d/monolith.js.map +1 -1
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.d.ts +10 -16
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.js +169 -165
- package/build/compatible-engines.d/openai-chatcompletions.d/stream.js.map +1 -1
- package/build/compatible-engines.d/openai-chatcompletions.d.ts +12 -18
- package/build/compatible-engines.d/openai-chatcompletions.js +64 -64
- package/build/compatible-engines.d/openai-chatcompletions.js.map +1 -1
- package/build/compatible-engines.d/openai-responses.d.ts +46 -68
- package/build/compatible-engines.d/openai-responses.js +222 -274
- package/build/compatible-engines.d/openai-responses.js.map +1 -1
- package/build/engine.d.ts +10 -24
- package/build/engine.js +25 -38
- package/build/engine.js.map +1 -1
- package/build/exports.d.ts +1 -0
- package/build/exports.js +1 -0
- package/build/exports.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 +1 -10
- package/build/inference-context.js +0 -2
- package/build/inference-context.js.map +1 -1
- package/build/native-engines.d/google/agentloop.d.ts +1 -1
- package/build/native-engines.d/google/agentloop.js +2 -4
- package/build/native-engines.d/google/agentloop.js.map +1 -1
- package/build/native-engines.d/google/engine.d.ts +56 -73
- package/build/native-engines.d/google/engine.js +265 -295
- package/build/native-engines.d/google/engine.js.map +1 -1
- package/build/native-engines.d/google/exports.d.ts +3 -2
- package/build/native-engines.d/google/exports.js +3 -2
- package/build/native-engines.d/google/exports.js.map +1 -1
- package/build/native-engines.d/google/session.js +6 -3
- package/build/native-engines.d/google/session.js.map +1 -1
- package/build/native-engines.d/openai-responses/agentloop.d.ts +1 -1
- package/build/native-engines.d/openai-responses/agentloop.js +2 -5
- package/build/native-engines.d/openai-responses/agentloop.js.map +1 -1
- package/build/native-engines.d/openai-responses/engine.d.ts +59 -78
- package/build/native-engines.d/openai-responses/engine.js +285 -321
- package/build/native-engines.d/openai-responses/engine.js.map +1 -1
- package/build/native-engines.d/openai-responses/exports.d.ts +4 -2
- package/build/native-engines.d/openai-responses/exports.js +4 -2
- package/build/native-engines.d/openai-responses/exports.js.map +1 -1
- package/build/native-engines.d/openai-responses/session.js +21 -8
- package/build/native-engines.d/openai-responses/session.js.map +1 -1
- package/build/session.js +12 -5
- package/build/session.js.map +1 -1
- package/build/telemetry.d.ts +6 -0
- package/build/telemetry.js +3 -0
- package/build/telemetry.js.map +1 -0
- package/build/throttle.d.ts +1 -1
- package/build/throttle.js +6 -6
- package/build/throttle.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -68,8 +68,6 @@ export type Config = {
|
|
|
68
68
|
|
|
69
69
|
## 快速上手
|
|
70
70
|
|
|
71
|
-
下面演示:定义一个工具函数,先用 Google 做推理与工具调用,再在同一会话中切换到 OpenAI Responses 做最终的结构化总结。
|
|
72
|
-
|
|
73
71
|
```ts
|
|
74
72
|
import { Adaptor, agentloop, RoleMessage, Function, type InferenceContext, type Config, Session } from '@zimtsui/brainswitch';
|
|
75
73
|
import { Type } from '@sinclair/typebox';
|
|
@@ -81,28 +79,19 @@ import * as Presets from '@zimtsui/typelog/presets';
|
|
|
81
79
|
const config: Config = {
|
|
82
80
|
brainswitch: {
|
|
83
81
|
endpoints: {
|
|
84
|
-
'gpt-
|
|
85
|
-
name: 'GPT-
|
|
86
|
-
apiType: 'openai-chatcompletions',
|
|
87
|
-
baseUrl: 'https://api.openai.com/v1',
|
|
88
|
-
apiKey: process.env.OPENAI_API_KEY!,
|
|
89
|
-
model: 'gpt-4o-mini',
|
|
90
|
-
inputPrice: 5, outputPrice: 15, cachedPrice: 1,
|
|
91
|
-
rpm: 3000, timeout: 60_000,
|
|
92
|
-
},
|
|
93
|
-
'o4-mini': {
|
|
94
|
-
name: 'o4 mini',
|
|
82
|
+
'gpt-5-mini': {
|
|
83
|
+
name: 'GPT-5 mini',
|
|
95
84
|
apiType: 'openai-responses',
|
|
96
85
|
baseUrl: 'https://api.openai.com/v1',
|
|
97
86
|
apiKey: process.env.OPENAI_API_KEY!,
|
|
98
|
-
model: '
|
|
87
|
+
model: 'gpt-5-mini',
|
|
99
88
|
},
|
|
100
|
-
'gemini-
|
|
101
|
-
name: 'Gemini
|
|
89
|
+
'gemini-3-flash': {
|
|
90
|
+
name: 'Gemini 3 Flash',
|
|
102
91
|
apiType: 'google',
|
|
103
92
|
baseUrl: 'https://generativelanguage.googleapis.com',
|
|
104
93
|
apiKey: process.env.GOOGLE_API_KEY!,
|
|
105
|
-
model: 'gemini-
|
|
94
|
+
model: 'gemini-3-flash',
|
|
106
95
|
},
|
|
107
96
|
}
|
|
108
97
|
}
|
|
@@ -111,7 +100,7 @@ const config: Config = {
|
|
|
111
100
|
// 声明函数工具
|
|
112
101
|
const fdm = {
|
|
113
102
|
get_weather: {
|
|
114
|
-
description: '
|
|
103
|
+
description: '获取指定城市的天气',
|
|
115
104
|
paraschema: Type.Object({
|
|
116
105
|
city: Type.String(),
|
|
117
106
|
unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
|
|
@@ -131,7 +120,7 @@ type fdu = Function.Declaration.From<fdm>;
|
|
|
131
120
|
// 实现函数工具
|
|
132
121
|
export class Submission extends Error {
|
|
133
122
|
public constructor(public weather: string, public advice: string) {
|
|
134
|
-
super(
|
|
123
|
+
super();
|
|
135
124
|
}
|
|
136
125
|
}
|
|
137
126
|
const fnm: Function.Map<fdm> = {
|
|
@@ -165,7 +154,7 @@ const session: Session<fdu> = {
|
|
|
165
154
|
|
|
166
155
|
// 选择推理引擎
|
|
167
156
|
const adaptor = Adaptor.create(config);
|
|
168
|
-
const engine = adaptor.makeEngine('gpt-
|
|
157
|
+
const engine = adaptor.makeEngine('gpt-5-mini', fdm, Function.ToolChoice.REQUIRED);
|
|
169
158
|
|
|
170
159
|
// 使用 agentloop 驱动智能体循环,最多 8 轮对话
|
|
171
160
|
try {
|
package/build/adaptor.d.ts
CHANGED
|
@@ -3,11 +3,13 @@ import { Function } from './function.ts';
|
|
|
3
3
|
import { type CompatibleEngine } from './compatible-engine.ts';
|
|
4
4
|
import { OpenAIResponsesNativeEngine } from './native-engines.d/openai-responses/engine.ts';
|
|
5
5
|
import { GoogleNativeEngine } from './native-engines.d/google/engine.ts';
|
|
6
|
+
import { type Logger } from './telemetry.ts';
|
|
6
7
|
export declare class Adaptor {
|
|
7
8
|
config: Config;
|
|
8
|
-
|
|
9
|
+
logger: Logger;
|
|
10
|
+
static create(config: Config, logger: Logger): Adaptor;
|
|
9
11
|
private throttles;
|
|
10
|
-
protected constructor(config: Config);
|
|
12
|
+
protected constructor(config: Config, logger: Logger);
|
|
11
13
|
makeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, toolChoice?: Function.ToolChoice<fdm>, parallelToolCall?: boolean): CompatibleEngine<fdm>;
|
|
12
14
|
makeOpenAIResponsesNativeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, applyPatch?: boolean, toolChoice?: Function.ToolChoice<fdm>, parallelToolCall?: boolean): OpenAIResponsesNativeEngine<fdm>;
|
|
13
15
|
makeGoogleNativeEngine<fdm extends Function.Declaration.Map>(endpoint: string, functionDeclarationMap: fdm, toolChoice?: Function.ToolChoice<fdm>, codeExecution?: boolean, urlContext?: boolean, googleSearch?: boolean, parallelToolCall?: boolean): GoogleNativeEngine<fdm>;
|
package/build/adaptor.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
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
5
|
import { GoogleCompatibleEngine } from "./compatible-engines.d/google.js";
|
|
7
6
|
import { AliyunEngine } from "./compatible-engines.d/aliyun.js";
|
|
@@ -9,14 +8,17 @@ import { OpenAIResponsesCompatibleEngine } from "./compatible-engines.d/openai-r
|
|
|
9
8
|
import { AnthropicCompatibleEngine } from "./compatible-engines.d/anthropic.js";
|
|
10
9
|
import { OpenAIResponsesNativeEngine } from "./native-engines.d/openai-responses/engine.js";
|
|
11
10
|
import { GoogleNativeEngine } from "./native-engines.d/google/engine.js";
|
|
11
|
+
import {} from "./telemetry.js";
|
|
12
12
|
export class Adaptor {
|
|
13
13
|
config;
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
logger;
|
|
15
|
+
static create(config, logger) {
|
|
16
|
+
return new Adaptor(config, logger);
|
|
16
17
|
}
|
|
17
18
|
throttles = new Map();
|
|
18
|
-
constructor(config) {
|
|
19
|
+
constructor(config, logger) {
|
|
19
20
|
this.config = config;
|
|
21
|
+
this.logger = logger;
|
|
20
22
|
for (const endpointId in this.config.brainswitch.endpoints) {
|
|
21
23
|
const rpm = this.config.brainswitch.endpoints[endpointId].rpm ?? Number.POSITIVE_INFINITY;
|
|
22
24
|
this.throttles.set(endpointId, new Throttle(rpm));
|
|
@@ -24,15 +26,20 @@ export class Adaptor {
|
|
|
24
26
|
}
|
|
25
27
|
makeEngine(endpoint, functionDeclarationMap, toolChoice, parallelToolCall) {
|
|
26
28
|
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
27
|
-
|
|
29
|
+
if (endpointSpec) { }
|
|
30
|
+
else
|
|
31
|
+
throw new Error();
|
|
28
32
|
const throttle = this.throttles.get(endpoint);
|
|
29
|
-
|
|
33
|
+
if (throttle) { }
|
|
34
|
+
else
|
|
35
|
+
throw new Error();
|
|
30
36
|
const options = {
|
|
31
37
|
...endpointSpec,
|
|
32
38
|
functionDeclarationMap,
|
|
33
39
|
toolChoice,
|
|
34
40
|
parallelToolCall,
|
|
35
41
|
throttle,
|
|
42
|
+
logger: this.logger,
|
|
36
43
|
};
|
|
37
44
|
if (endpointSpec.apiType === 'openai-responses')
|
|
38
45
|
return OpenAIResponsesCompatibleEngine.create(options);
|
|
@@ -47,9 +54,13 @@ export class Adaptor {
|
|
|
47
54
|
}
|
|
48
55
|
makeOpenAIResponsesNativeEngine(endpoint, functionDeclarationMap, applyPatch, toolChoice, parallelToolCall) {
|
|
49
56
|
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
50
|
-
|
|
57
|
+
if (endpointSpec?.apiType === 'openai-responses') { }
|
|
58
|
+
else
|
|
59
|
+
throw new Error();
|
|
51
60
|
const throttle = this.throttles.get(endpoint);
|
|
52
|
-
|
|
61
|
+
if (throttle) { }
|
|
62
|
+
else
|
|
63
|
+
throw new Error();
|
|
53
64
|
const options = {
|
|
54
65
|
...endpointSpec,
|
|
55
66
|
functionDeclarationMap,
|
|
@@ -57,14 +68,19 @@ export class Adaptor {
|
|
|
57
68
|
parallelToolCall,
|
|
58
69
|
throttle,
|
|
59
70
|
applyPatch,
|
|
71
|
+
logger: this.logger,
|
|
60
72
|
};
|
|
61
73
|
return OpenAIResponsesNativeEngine.create(options);
|
|
62
74
|
}
|
|
63
75
|
makeGoogleNativeEngine(endpoint, functionDeclarationMap, toolChoice, codeExecution, urlContext, googleSearch, parallelToolCall) {
|
|
64
76
|
const endpointSpec = this.config.brainswitch.endpoints[endpoint];
|
|
65
|
-
|
|
77
|
+
if (endpointSpec?.apiType === 'google') { }
|
|
78
|
+
else
|
|
79
|
+
throw new Error();
|
|
66
80
|
const throttle = this.throttles.get(endpoint);
|
|
67
|
-
|
|
81
|
+
if (throttle) { }
|
|
82
|
+
else
|
|
83
|
+
throw new Error();
|
|
68
84
|
const options = {
|
|
69
85
|
...endpointSpec,
|
|
70
86
|
functionDeclarationMap,
|
|
@@ -74,6 +90,7 @@ export class Adaptor {
|
|
|
74
90
|
codeExecution,
|
|
75
91
|
urlContext,
|
|
76
92
|
googleSearch,
|
|
93
|
+
logger: this.logger,
|
|
77
94
|
};
|
|
78
95
|
return GoogleNativeEngine.create(options);
|
|
79
96
|
}
|
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,wBAA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 -5,7 +5,7 @@ import { type CompatibleEngine } from './compatible-engine.ts';
|
|
|
5
5
|
/**
|
|
6
6
|
* @param session mutable
|
|
7
7
|
*/
|
|
8
|
-
export declare function agentloop<fdm extends Function.Declaration.Map>(
|
|
8
|
+
export declare function agentloop<fdm extends Function.Declaration.Map>(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, engine: CompatibleEngine<fdm>, fnm: Function.Map<fdm>, limit?: number): AsyncGenerator<string, string, void>;
|
|
9
9
|
export declare namespace agentloop {
|
|
10
10
|
class FunctionCallLimitExceeded extends Error {
|
|
11
11
|
}
|
package/build/agentloop.js
CHANGED
|
@@ -2,13 +2,12 @@ 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
|
*/
|
|
9
|
-
export async function* agentloop(
|
|
8
|
+
export async function* agentloop(wfctx, session, engine, fnm, limit = Number.POSITIVE_INFINITY) {
|
|
10
9
|
for (let i = 0; i < limit; i++) {
|
|
11
|
-
const response = await engine.stateful(
|
|
10
|
+
const response = await engine.stateful(wfctx, session);
|
|
12
11
|
const fcs = response.getFunctionCalls();
|
|
13
12
|
if (!fcs.length)
|
|
14
13
|
return response.getOnlyText();
|
|
@@ -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":"A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AAC;gBACnD,MAAM,CAAC,GAAG,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC;gBACvB,IAAI,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,EAAE;oBAClB,OAAO,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAM;wBACjC,EAAE,EAAE,EAAE,CAAC,EAAE;wBACT,IAAI,EAAE,EAAE,CAAC,IAAI;wBACb,IAAI,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC;qBACM,CAAC,CAAC;gBAChD,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,33 +1,29 @@
|
|
|
1
1
|
import { Function } from '../function.ts';
|
|
2
|
-
import { Engine } from '../engine.ts';
|
|
2
|
+
import { type Engine } from '../engine.ts';
|
|
3
3
|
import Anthropic from '@anthropic-ai/sdk';
|
|
4
4
|
export declare namespace AnthropicEngine {
|
|
5
|
-
interface
|
|
5
|
+
interface Options<in out fdm extends Function.Declaration.Map> extends Engine.Options<fdm> {
|
|
6
|
+
}
|
|
7
|
+
interface OwnProps {
|
|
8
|
+
parallelToolCall: boolean;
|
|
6
9
|
anthropic: Anthropic;
|
|
7
|
-
|
|
10
|
+
}
|
|
11
|
+
namespace OwnProps {
|
|
12
|
+
function init<fdm extends Function.Declaration.Map>(this: Engine.Underhood<fdm>, options: Options<fdm>): OwnProps;
|
|
13
|
+
}
|
|
14
|
+
interface Underhood<in out fdm extends Function.Declaration.Map> extends Engine.Underhood<fdm>, OwnProps {
|
|
15
|
+
parallelToolCall: boolean;
|
|
8
16
|
convertFromFunctionCall(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Anthropic.ToolUseBlock;
|
|
9
17
|
convertToFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
10
18
|
convertFromFunctionResponse(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): Anthropic.ToolResultBlockParam;
|
|
11
19
|
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): Anthropic.Tool;
|
|
12
|
-
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>,
|
|
20
|
+
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>, parallelToolCall: boolean): Anthropic.ToolChoice;
|
|
13
21
|
calcCost(usage: Anthropic.Usage): number;
|
|
14
22
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
parallel: boolean;
|
|
22
|
-
constructor(instance: AnthropicEngine.Instance<fdm>, options: AnthropicEngine.Options<fdm>);
|
|
23
|
-
convertFromFunctionCall(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Anthropic.ToolUseBlock;
|
|
24
|
-
convertToFunctionCall(apifc: Anthropic.ToolUseBlock): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
25
|
-
convertFromFunctionResponse(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): Anthropic.ToolResultBlockParam;
|
|
26
|
-
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): Anthropic.Tool;
|
|
27
|
-
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>, parallel: boolean): Anthropic.ToolChoice;
|
|
28
|
-
calcCost(usage: Anthropic.Usage): number;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
interface Options<fdm extends Function.Declaration.Map> extends Engine.Options<fdm> {
|
|
32
|
-
}
|
|
23
|
+
function convertFromFunctionCall<fdm extends Function.Declaration.Map>(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Anthropic.ToolUseBlock;
|
|
24
|
+
function convertToFunctionCall<fdm extends Function.Declaration.Map>(this: Engine.Underhood<fdm>, apifc: Anthropic.ToolUseBlock): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
25
|
+
function convertFromFunctionResponse<fdm extends Function.Declaration.Map>(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): Anthropic.ToolResultBlockParam;
|
|
26
|
+
function convertFromFunctionDeclarationEntry<fdm extends Function.Declaration.Map>(fdentry: Function.Declaration.Entry.From<fdm>): Anthropic.Tool;
|
|
27
|
+
function convertFromToolChoice<fdm extends Function.Declaration.Map>(toolChoice: Function.ToolChoice<fdm>, parallelToolCall: boolean): Anthropic.ToolChoice;
|
|
28
|
+
function calcCost<fdm extends Function.Declaration.Map>(this: Engine.Underhood<fdm>, usage: Anthropic.Usage): number;
|
|
33
29
|
}
|
|
@@ -1,84 +1,93 @@
|
|
|
1
1
|
import { Function } from "../function.js";
|
|
2
|
-
import {
|
|
2
|
+
import { 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();
|
|
8
7
|
export var AnthropicEngine;
|
|
9
8
|
(function (AnthropicEngine) {
|
|
10
|
-
let
|
|
11
|
-
(function (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
37
|
-
}
|
|
38
|
-
})();
|
|
39
|
-
assert(ajv.validate(fditem.paraschema, args), new ResponseInvalid('Function call not conforming to schema', { cause: apifc }));
|
|
40
|
-
return Function.Call.create({
|
|
41
|
-
id: apifc.id,
|
|
42
|
-
name: apifc.name,
|
|
43
|
-
args,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
convertFromFunctionResponse(fr) {
|
|
47
|
-
assert(fr.id);
|
|
48
|
-
return {
|
|
49
|
-
type: 'tool_result',
|
|
50
|
-
tool_use_id: fr.id,
|
|
51
|
-
content: fr.text,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
convertFromFunctionDeclarationEntry(fdentry) {
|
|
55
|
-
return {
|
|
56
|
-
name: fdentry[0],
|
|
57
|
-
description: fdentry[1].description,
|
|
58
|
-
input_schema: fdentry[1].paraschema,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
convertFromToolChoice(toolChoice, parallel) {
|
|
62
|
-
if (toolChoice === Function.ToolChoice.NONE)
|
|
63
|
-
return { type: 'none' };
|
|
64
|
-
else if (toolChoice === Function.ToolChoice.REQUIRED)
|
|
65
|
-
return { type: 'any', disable_parallel_tool_use: !parallel };
|
|
66
|
-
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
67
|
-
return { type: 'auto', disable_parallel_tool_use: !parallel };
|
|
68
|
-
else {
|
|
69
|
-
assert(toolChoice.length === 1);
|
|
70
|
-
return { type: 'tool', name: toolChoice[0], disable_parallel_tool_use: !parallel };
|
|
71
|
-
}
|
|
9
|
+
let OwnProps;
|
|
10
|
+
(function (OwnProps) {
|
|
11
|
+
function init(options) {
|
|
12
|
+
return {
|
|
13
|
+
parallelToolCall: options.parallelToolCall ?? false,
|
|
14
|
+
anthropic: new Anthropic({
|
|
15
|
+
baseURL: this.baseUrl,
|
|
16
|
+
apiKey: this.apiKey,
|
|
17
|
+
fetchOptions: { dispatcher: this.proxyAgent },
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
OwnProps.init = init;
|
|
22
|
+
})(OwnProps = AnthropicEngine.OwnProps || (AnthropicEngine.OwnProps = {}));
|
|
23
|
+
function convertFromFunctionCall(fc) {
|
|
24
|
+
throw new Error('Anthropic compatible engine requires native function calls.');
|
|
25
|
+
}
|
|
26
|
+
AnthropicEngine.convertFromFunctionCall = convertFromFunctionCall;
|
|
27
|
+
function convertToFunctionCall(apifc) {
|
|
28
|
+
const fditem = this.fdm[apifc.name];
|
|
29
|
+
if (fditem) { }
|
|
30
|
+
else
|
|
31
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
32
|
+
const args = (() => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(apifc.input);
|
|
72
35
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
76
|
-
return this.instance.inputPrice * cacheMissTokenCount / 1e6 +
|
|
77
|
-
this.instance.cachedPrice * cacheHitTokenCount / 1e6 +
|
|
78
|
-
this.instance.outputPrice * usage.output_tokens / 1e6;
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
79
38
|
}
|
|
39
|
+
})();
|
|
40
|
+
if (ajv.validate(fditem.paraschema, args)) { }
|
|
41
|
+
else
|
|
42
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
43
|
+
return Function.Call.create({
|
|
44
|
+
id: apifc.id,
|
|
45
|
+
name: apifc.name,
|
|
46
|
+
args,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
AnthropicEngine.convertToFunctionCall = convertToFunctionCall;
|
|
50
|
+
function convertFromFunctionResponse(fr) {
|
|
51
|
+
if (fr.id) { }
|
|
52
|
+
else
|
|
53
|
+
throw new Error();
|
|
54
|
+
return {
|
|
55
|
+
type: 'tool_result',
|
|
56
|
+
tool_use_id: fr.id,
|
|
57
|
+
content: fr.text,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
AnthropicEngine.convertFromFunctionResponse = convertFromFunctionResponse;
|
|
61
|
+
function convertFromFunctionDeclarationEntry(fdentry) {
|
|
62
|
+
return {
|
|
63
|
+
name: fdentry[0],
|
|
64
|
+
description: fdentry[1].description,
|
|
65
|
+
input_schema: fdentry[1].paraschema,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
AnthropicEngine.convertFromFunctionDeclarationEntry = convertFromFunctionDeclarationEntry;
|
|
69
|
+
function convertFromToolChoice(toolChoice, parallelToolCall) {
|
|
70
|
+
if (toolChoice === Function.ToolChoice.NONE)
|
|
71
|
+
return { type: 'none' };
|
|
72
|
+
else if (toolChoice === Function.ToolChoice.REQUIRED)
|
|
73
|
+
return { type: 'any', disable_parallel_tool_use: !parallelToolCall };
|
|
74
|
+
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
75
|
+
return { type: 'auto', disable_parallel_tool_use: !parallelToolCall };
|
|
76
|
+
else {
|
|
77
|
+
if (toolChoice.length === 1) { }
|
|
78
|
+
else
|
|
79
|
+
throw new Error();
|
|
80
|
+
return { type: 'tool', name: toolChoice[0], disable_parallel_tool_use: !parallelToolCall };
|
|
80
81
|
}
|
|
81
|
-
|
|
82
|
-
|
|
82
|
+
}
|
|
83
|
+
AnthropicEngine.convertFromToolChoice = convertFromToolChoice;
|
|
84
|
+
function calcCost(usage) {
|
|
85
|
+
const cacheHitTokenCount = usage.cache_read_input_tokens || 0;
|
|
86
|
+
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
87
|
+
return this.inputPrice * cacheMissTokenCount / 1e6 +
|
|
88
|
+
this.cachePrice * cacheHitTokenCount / 1e6 +
|
|
89
|
+
this.outputPrice * usage.output_tokens / 1e6;
|
|
90
|
+
}
|
|
91
|
+
AnthropicEngine.calcCost = calcCost;
|
|
83
92
|
})(AnthropicEngine || (AnthropicEngine = {}));
|
|
84
93
|
//# sourceMappingURL=anthropic.js.map
|
|
@@ -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,
|
|
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,eAAe,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,SAAS,MAAM,mBAAmB,CAAC;AAC1C,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAgB,MAAM,mBAAmB,CAAC;AAEjD,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAItB,MAAM,KAAW,eAAe,CA8G/B;AA9GD,WAAiB,eAAe;IAO5B,IAAiB,QAAQ,CAcxB;IAdD,WAAiB,QAAQ;QACrB,SAAgB,IAAI,CAEhB,OAAqB;YAErB,OAAO;gBACH,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,KAAK;gBACnD,SAAS,EAAE,IAAI,SAAS,CAAC;oBACrB,OAAO,EAAE,IAAI,CAAC,OAAO;oBACrB,MAAM,EAAE,IAAI,CAAC,MAAM;oBACnB,YAAY,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE;iBAChD,CAAC;aACL,CAAC;QACN,CAAC;QAZe,aAAI,OAYnB,CAAA;IACL,CAAC,EAdgB,QA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,GAAG,KAAK,CAAC,YAAY,GAAG,kBAAkB,CAAC;QACpE,OAAO,IAAI,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YAC1C,IAAI,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YAC1C,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,aAAa,GAAG,GAAG,CAAC;IACzD,CAAC;IATe,wBAAQ,WASvB,CAAA;AAEL,CAAC,EA9GgB,eAAe,KAAf,eAAe,QA8G/B"}
|
|
@@ -1,28 +1,23 @@
|
|
|
1
|
-
import { Engine } from '../engine.ts';
|
|
1
|
+
import { type Engine } from '../engine.ts';
|
|
2
2
|
import { Function } from '../function.ts';
|
|
3
3
|
import * as Google from '@google/genai';
|
|
4
|
-
import { CompatibleEngine } from '../compatible-engine.ts';
|
|
5
4
|
export declare namespace GoogleEngine {
|
|
6
|
-
interface
|
|
7
|
-
|
|
5
|
+
interface Options<in out fdm extends Function.Declaration.Map> extends Engine.Options<fdm> {
|
|
6
|
+
}
|
|
7
|
+
interface OwnProps {
|
|
8
|
+
parallelToolCall: boolean;
|
|
9
|
+
}
|
|
10
|
+
namespace OwnProps {
|
|
11
|
+
function init<fdm extends Function.Declaration.Map>(options: Options<fdm>): OwnProps;
|
|
12
|
+
}
|
|
13
|
+
interface Underhood<in out fdm extends Function.Declaration.Map> extends Engine.Underhood<fdm>, OwnProps {
|
|
8
14
|
convertFromFunctionCall(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Google.FunctionCall;
|
|
9
15
|
convertToFunctionCall(googlefc: Google.FunctionCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
10
16
|
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
11
17
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class Instance<in out fdm extends Function.Declaration.Map> implements GoogleEngine.Base<fdm> {
|
|
16
|
-
protected instance: GoogleEngine.Instance<fdm>;
|
|
17
|
-
parallel: boolean;
|
|
18
|
-
constructor(instance: GoogleEngine.Instance<fdm>, options: CompatibleEngine.Options<fdm>);
|
|
19
|
-
convertFromFunctionCall(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Google.FunctionCall;
|
|
20
|
-
convertToFunctionCall(googlefc: Google.FunctionCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
21
|
-
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function convertFromFunctionDeclarationEntry<fdm extends Function.Declaration.Map>(fdentry: Function.Declaration.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
25
|
-
function convertToFunctionCall<fdm extends Function.Declaration.Map>(googlefc: Google.FunctionCall, fdm: fdm): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
18
|
+
function convertFromFunctionCall<fdm extends Function.Declaration.Map>(this: GoogleEngine.Underhood<fdm>, fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Google.FunctionCall;
|
|
19
|
+
function convertFromFunctionDeclarationEntry<fdm extends Function.Declaration.Map>(this: GoogleEngine.Underhood<fdm>, fdentry: Function.Declaration.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
20
|
+
function convertToFunctionCall<fdm extends Function.Declaration.Map>(this: GoogleEngine.Underhood<fdm>, googlefc: Google.FunctionCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
26
21
|
interface RestfulRequest {
|
|
27
22
|
contents: Google.Content[];
|
|
28
23
|
tools?: Google.Tool[];
|
|
@@ -1,38 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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
|
-
import { CompatibleEngine } from "../compatible-engine.js";
|
|
6
4
|
import Ajv from 'ajv';
|
|
7
5
|
const ajv = new Ajv();
|
|
8
6
|
export var GoogleEngine;
|
|
9
7
|
(function (GoogleEngine) {
|
|
10
|
-
let
|
|
11
|
-
(function (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
convertFromFunctionCall(fc) {
|
|
21
|
-
return {
|
|
22
|
-
id: fc.id,
|
|
23
|
-
name: fc.name,
|
|
24
|
-
args: fc.args,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
convertToFunctionCall(googlefc) {
|
|
28
|
-
return GoogleEngine.convertToFunctionCall(googlefc, this.instance.fdm);
|
|
29
|
-
}
|
|
30
|
-
convertFromFunctionDeclarationEntry(fdentry) {
|
|
31
|
-
return GoogleEngine.convertFromFunctionDeclarationEntry(fdentry);
|
|
32
|
-
}
|
|
8
|
+
let OwnProps;
|
|
9
|
+
(function (OwnProps) {
|
|
10
|
+
function init(options) {
|
|
11
|
+
const parallelToolCall = options.parallelToolCall ?? true;
|
|
12
|
+
if (parallelToolCall) { }
|
|
13
|
+
else
|
|
14
|
+
throw new Error('Google API requires parallel tool calls.');
|
|
15
|
+
return {
|
|
16
|
+
parallelToolCall,
|
|
17
|
+
};
|
|
33
18
|
}
|
|
34
|
-
|
|
35
|
-
})(
|
|
19
|
+
OwnProps.init = init;
|
|
20
|
+
})(OwnProps = GoogleEngine.OwnProps || (GoogleEngine.OwnProps = {}));
|
|
21
|
+
function convertFromFunctionCall(fc) {
|
|
22
|
+
return {
|
|
23
|
+
id: fc.id,
|
|
24
|
+
name: fc.name,
|
|
25
|
+
args: fc.args,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
GoogleEngine.convertFromFunctionCall = convertFromFunctionCall;
|
|
36
29
|
function convertFromFunctionDeclarationEntry(fdentry) {
|
|
37
30
|
const json = JSON.stringify(fdentry[1].paraschema);
|
|
38
31
|
const parsed = JSON.parse(json, (key, value) => {
|
|
@@ -62,11 +55,17 @@ export var GoogleEngine;
|
|
|
62
55
|
};
|
|
63
56
|
}
|
|
64
57
|
GoogleEngine.convertFromFunctionDeclarationEntry = convertFromFunctionDeclarationEntry;
|
|
65
|
-
function convertToFunctionCall(googlefc
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
58
|
+
function convertToFunctionCall(googlefc) {
|
|
59
|
+
if (googlefc.name) { }
|
|
60
|
+
else
|
|
61
|
+
throw new Error();
|
|
62
|
+
const fditem = this.fdm[googlefc.name];
|
|
63
|
+
if (fditem) { }
|
|
64
|
+
else
|
|
65
|
+
throw new ResponseInvalid('Unknown function call', { cause: googlefc });
|
|
66
|
+
if (ajv.validate(fditem.paraschema, googlefc.args)) { }
|
|
67
|
+
else
|
|
68
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: googlefc });
|
|
70
69
|
return Function.Call.create({
|
|
71
70
|
id: googlefc.id,
|
|
72
71
|
name: googlefc.name,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/api-types/google.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/api-types/google.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,eAAe,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAItB,MAAM,KAAW,YAAY,CAoF5B;AApFD,WAAiB,YAAY;IAMzB,IAAiB,QAAQ,CAQxB;IARD,WAAiB,QAAQ;QACrB,SAAgB,IAAI,CAAuC,OAAqB;YAC5E,MAAM,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,IAAI,CAAC;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