@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
|
@@ -3,38 +3,36 @@ import { Engine } from '../engine.ts';
|
|
|
3
3
|
import { RoleMessage, type Session, type ChatMessage } from '../session.ts';
|
|
4
4
|
import { Function } from '../function.ts';
|
|
5
5
|
import * as Google from '@google/genai';
|
|
6
|
+
import * as Undici from 'undici';
|
|
6
7
|
import { type InferenceContext } from '../inference-context.ts';
|
|
7
8
|
import { GoogleEngine } from '../api-types/google.ts';
|
|
9
|
+
import { Throttle } from '../throttle.ts';
|
|
10
|
+
import { type Logger } from '../telemetry.ts';
|
|
8
11
|
export declare namespace GoogleCompatibleEngine {
|
|
9
|
-
interface
|
|
12
|
+
interface Options<in out fdm extends Function.Declaration.Map> extends CompatibleEngine.Options<fdm>, GoogleEngine.Options<fdm> {
|
|
13
|
+
}
|
|
14
|
+
interface OwnProps<in out fdm extends Function.Declaration.Map> {
|
|
15
|
+
apiURL: URL;
|
|
16
|
+
}
|
|
17
|
+
namespace OwnProps {
|
|
18
|
+
function init<fdm extends Function.Declaration.Map>(this: Engine.Underhood<fdm>, options: Options<fdm>): OwnProps<fdm>;
|
|
19
|
+
}
|
|
20
|
+
interface Underhood<in out fdm extends Function.Declaration.Map> extends GoogleEngine.Underhood<fdm>, CompatibleEngine.Underhood<fdm>, OwnProps<fdm> {
|
|
10
21
|
convertFromUserMessage(userMessage: RoleMessage.User<Function.Declaration.From<fdm>>): Google.Content;
|
|
11
22
|
convertFromAiMessage(aiMessage: RoleMessage.Ai<Function.Declaration.From<fdm>>): Google.Content;
|
|
12
23
|
convertFromDeveloperMessage(developerMessage: RoleMessage.Developer): Google.Content;
|
|
13
24
|
convertFromChatMessages(chatMessages: ChatMessage<Function.Declaration.From<fdm>>[]): Google.Content[];
|
|
14
25
|
convertToAiMessage(content: Google.Content): GoogleCompatibleEngine.Message.Ai<Function.Declaration.From<fdm>>;
|
|
15
26
|
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>): Google.FunctionCallingConfig;
|
|
16
|
-
fetch(
|
|
17
|
-
}
|
|
18
|
-
interface Instance<in out fdm extends Function.Declaration.Map> extends GoogleEngine.Instance<fdm>, CompatibleEngine.Instance<fdm>, GoogleCompatibleEngine.Base<fdm> {
|
|
19
|
-
}
|
|
20
|
-
namespace Base {
|
|
21
|
-
class Instance<in out fdm extends Function.Declaration.Map> implements GoogleCompatibleEngine.Base<fdm> {
|
|
22
|
-
protected instance: GoogleCompatibleEngine.Instance<fdm>;
|
|
23
|
-
protected apiURL: URL;
|
|
24
|
-
constructor(instance: GoogleCompatibleEngine.Instance<fdm>);
|
|
25
|
-
convertFromUserMessage(userMessage: RoleMessage.User<Function.Declaration.From<fdm>>): Google.Content;
|
|
26
|
-
convertFromAiMessage(aiMessage: RoleMessage.Ai<Function.Declaration.From<fdm>>): Google.Content;
|
|
27
|
-
convertFromDeveloperMessage(developerMessage: RoleMessage.Developer): Google.Content;
|
|
28
|
-
convertFromChatMessages(chatMessages: ChatMessage<Function.Declaration.From<fdm>>[]): Google.Content[];
|
|
29
|
-
convertToAiMessage(content: Google.Content): GoogleCompatibleEngine.Message.Ai<Function.Declaration.From<fdm>>;
|
|
30
|
-
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>): Google.FunctionCallingConfig;
|
|
31
|
-
fetch(ctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, signal?: AbortSignal): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
32
|
-
}
|
|
27
|
+
fetch(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, signal?: AbortSignal): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
33
28
|
}
|
|
29
|
+
function convertFromAiMessage<fdm extends Function.Declaration.Map>(this: GoogleCompatibleEngine.Underhood<fdm>, aiMessage: RoleMessage.Ai<Function.Declaration.From<fdm>>): Google.Content;
|
|
30
|
+
function convertFromChatMessages<fdm extends Function.Declaration.Map>(this: GoogleCompatibleEngine.Underhood<fdm>, chatMessages: ChatMessage<Function.Declaration.From<fdm>>[]): Google.Content[];
|
|
31
|
+
function fetch<fdm extends Function.Declaration.Map>(this: GoogleCompatibleEngine.Underhood<fdm>, wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, signal?: AbortSignal): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
34
32
|
function convertFromUserMessage<fdm extends Function.Declaration.Map>(userMessage: RoleMessage.User<Function.Declaration.From<fdm>>): Google.Content;
|
|
35
33
|
function convertFromDeveloperMessage(developerMessage: RoleMessage.Developer): Google.Content;
|
|
36
34
|
function convertFromToolChoice<fdm extends Function.Declaration.Map>(toolChoice: Function.ToolChoice<fdm>): Google.FunctionCallingConfig;
|
|
37
|
-
function convertToAiMessage<fdm extends Function.Declaration.Map>(content: Google.Content
|
|
35
|
+
function convertToAiMessage<fdm extends Function.Declaration.Map>(this: GoogleCompatibleEngine.Underhood<fdm>, content: Google.Content): GoogleCompatibleEngine.Message.Ai<Function.Declaration.From<fdm>>;
|
|
38
36
|
namespace Message {
|
|
39
37
|
type Ai<fdu extends Function.Declaration> = Ai.Instance<fdu>;
|
|
40
38
|
namespace Ai {
|
|
@@ -48,47 +46,30 @@ export declare namespace GoogleCompatibleEngine {
|
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
}
|
|
51
|
-
class Instance<in out fdm extends Function.Declaration.Map> implements GoogleCompatibleEngine.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
class Instance<in out fdm extends Function.Declaration.Map> implements GoogleCompatibleEngine.Underhood<fdm> {
|
|
50
|
+
baseUrl: string;
|
|
51
|
+
apiKey: string;
|
|
52
|
+
model: string;
|
|
53
|
+
name: string;
|
|
54
|
+
inputPrice: number;
|
|
55
|
+
outputPrice: number;
|
|
56
|
+
cachePrice: number;
|
|
57
|
+
fdm: fdm;
|
|
58
|
+
additionalOptions?: Record<string, unknown>;
|
|
59
|
+
throttle: Throttle;
|
|
60
|
+
timeout?: number;
|
|
61
|
+
maxTokens?: number;
|
|
62
|
+
proxyAgent?: Undici.ProxyAgent;
|
|
63
|
+
logger: Logger;
|
|
64
|
+
toolChoice: Function.ToolChoice<fdm>;
|
|
65
|
+
parallelToolCall: boolean;
|
|
66
|
+
apiURL: URL;
|
|
56
67
|
constructor(options: GoogleCompatibleEngine.Options<fdm>);
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
get apiKey(): string;
|
|
60
|
-
set apiKey(value: string);
|
|
61
|
-
get model(): string;
|
|
62
|
-
set model(value: string);
|
|
63
|
-
get name(): string;
|
|
64
|
-
set name(value: string);
|
|
65
|
-
get inputPrice(): number;
|
|
66
|
-
set inputPrice(value: number);
|
|
67
|
-
get outputPrice(): number;
|
|
68
|
-
set outputPrice(value: number);
|
|
69
|
-
get cachedPrice(): number;
|
|
70
|
-
set cachedPrice(value: number);
|
|
71
|
-
get fdm(): fdm;
|
|
72
|
-
set fdm(value: fdm);
|
|
73
|
-
get additionalOptions(): Record<string, unknown> | undefined;
|
|
74
|
-
set additionalOptions(value: Record<string, unknown> | undefined);
|
|
75
|
-
get throttle(): import("../throttle.ts").Throttle;
|
|
76
|
-
set throttle(value: import("../throttle.ts").Throttle);
|
|
77
|
-
get timeout(): number | undefined;
|
|
78
|
-
set timeout(value: number | undefined);
|
|
79
|
-
get maxTokens(): number | undefined;
|
|
80
|
-
set maxTokens(value: number | undefined);
|
|
81
|
-
get proxyAgent(): import("undici").ProxyAgent | undefined;
|
|
82
|
-
set proxyAgent(value: import("undici").ProxyAgent | undefined);
|
|
83
|
-
get toolChoice(): Function.ToolChoice<fdm>;
|
|
84
|
-
set toolChoice(value: Function.ToolChoice<fdm>);
|
|
85
|
-
stateless(ctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm, Extract<keyof fdm, string>>>>;
|
|
86
|
-
stateful(ctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm, Extract<keyof fdm, string>>>>;
|
|
68
|
+
stateless(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm, Extract<keyof fdm, string>>>>;
|
|
69
|
+
stateful(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm, Extract<keyof fdm, string>>>>;
|
|
87
70
|
appendUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm, Extract<keyof fdm, string>>>;
|
|
88
71
|
pushUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm, Extract<keyof fdm, string>>>;
|
|
89
72
|
validateToolCallsByToolChoice(toolCalls: Function.Call.Distributive<Function.Declaration.From<fdm>>[]): void;
|
|
90
|
-
get parallel(): boolean;
|
|
91
|
-
set parallel(value: boolean);
|
|
92
73
|
convertFromFunctionCall(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): Google.FunctionCall;
|
|
93
74
|
convertToFunctionCall(googlefc: Google.FunctionCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
94
75
|
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): Google.FunctionDeclaration;
|
|
@@ -98,9 +79,7 @@ export declare namespace GoogleCompatibleEngine {
|
|
|
98
79
|
convertFromChatMessages(chatMessages: ChatMessage<Function.Declaration.From<fdm>>[]): Google.Content[];
|
|
99
80
|
convertToAiMessage(content: Google.Content): GoogleCompatibleEngine.Message.Ai<Function.Declaration.From<fdm>>;
|
|
100
81
|
convertFromToolChoice(toolChoice: Function.ToolChoice<fdm>): Google.FunctionCallingConfig;
|
|
101
|
-
fetch(
|
|
102
|
-
}
|
|
103
|
-
interface Options<in out fdm extends Function.Declaration.Map> extends CompatibleEngine.Options<fdm> {
|
|
82
|
+
fetch(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, signal?: AbortSignal): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
104
83
|
}
|
|
105
84
|
function create<fdm extends Function.Declaration.Map>(options: CompatibleEngine.Options<fdm>): CompatibleEngine<fdm>;
|
|
106
85
|
}
|
|
@@ -1,124 +1,123 @@
|
|
|
1
1
|
import { CompatibleEngine } from "../compatible-engine.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Engine, ResponseInvalid } from "../engine.js";
|
|
3
3
|
import { RoleMessage } from "../session.js";
|
|
4
4
|
import { Function } from "../function.js";
|
|
5
5
|
import * as Google from '@google/genai';
|
|
6
|
-
import
|
|
7
|
-
import { fetch } from 'undici';
|
|
6
|
+
import * as Undici from 'undici';
|
|
8
7
|
import {} from "../inference-context.js";
|
|
9
8
|
import { GoogleEngine } from "../api-types/google.js";
|
|
9
|
+
import { Throttle } from "../throttle.js";
|
|
10
|
+
import {} from "../telemetry.js";
|
|
10
11
|
export var GoogleCompatibleEngine;
|
|
11
12
|
(function (GoogleCompatibleEngine) {
|
|
12
|
-
let
|
|
13
|
-
(function (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return Google.createPartFromText(part.text);
|
|
31
|
-
else if (part instanceof Function.Call) {
|
|
32
|
-
assert(part.args instanceof Object);
|
|
33
|
-
return Google.createPartFromFunctionCall(part.name, part.args);
|
|
34
|
-
}
|
|
35
|
-
else
|
|
36
|
-
throw new Error();
|
|
37
|
-
});
|
|
38
|
-
return Google.createModelContent(parts);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
convertFromDeveloperMessage(developerMessage) {
|
|
42
|
-
return GoogleCompatibleEngine.convertFromDeveloperMessage(developerMessage);
|
|
43
|
-
}
|
|
44
|
-
convertFromChatMessages(chatMessages) {
|
|
45
|
-
return chatMessages.map(chatMessage => {
|
|
46
|
-
if (chatMessage instanceof RoleMessage.User.Instance)
|
|
47
|
-
return this.convertFromUserMessage(chatMessage);
|
|
48
|
-
else if (chatMessage instanceof RoleMessage.Ai.Instance)
|
|
49
|
-
return this.convertFromAiMessage(chatMessage);
|
|
13
|
+
let OwnProps;
|
|
14
|
+
(function (OwnProps) {
|
|
15
|
+
function init(options) {
|
|
16
|
+
return {
|
|
17
|
+
apiURL: new URL(`${this.baseUrl}/v1beta/models/${this.model}:generateContent`),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
OwnProps.init = init;
|
|
21
|
+
})(OwnProps = GoogleCompatibleEngine.OwnProps || (GoogleCompatibleEngine.OwnProps = {}));
|
|
22
|
+
function convertFromAiMessage(aiMessage) {
|
|
23
|
+
if (aiMessage instanceof GoogleCompatibleEngine.Message.Ai.Instance)
|
|
24
|
+
return aiMessage.getRaw();
|
|
25
|
+
else {
|
|
26
|
+
const parts = aiMessage.getParts().map(part => {
|
|
27
|
+
if (part instanceof RoleMessage.Part.Text.Instance)
|
|
28
|
+
return Google.createPartFromText(part.text);
|
|
29
|
+
else if (part instanceof Function.Call) {
|
|
30
|
+
if (part.args instanceof Object) { }
|
|
50
31
|
else
|
|
51
32
|
throw new Error();
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
convertToAiMessage(content) {
|
|
55
|
-
return GoogleCompatibleEngine.convertToAiMessage(content, this.instance.fdm);
|
|
56
|
-
}
|
|
57
|
-
convertFromToolChoice(toolChoice) {
|
|
58
|
-
return GoogleCompatibleEngine.convertFromToolChoice(toolChoice);
|
|
59
|
-
}
|
|
60
|
-
async fetch(ctx, session, signal) {
|
|
61
|
-
const systemInstruction = session.developerMessage && this.instance.convertFromDeveloperMessage(session.developerMessage);
|
|
62
|
-
const contents = this.instance.convertFromChatMessages(session.chatMessages);
|
|
63
|
-
await this.instance.throttle.requests(ctx);
|
|
64
|
-
const fdentries = Object.entries(this.instance.fdm);
|
|
65
|
-
const tools = fdentries.map(fdentry => this.instance.convertFromFunctionDeclarationEntry(fdentry));
|
|
66
|
-
const reqbody = {
|
|
67
|
-
contents,
|
|
68
|
-
tools: tools.length ? [{
|
|
69
|
-
functionDeclarations: tools,
|
|
70
|
-
}] : undefined,
|
|
71
|
-
toolConfig: tools.length ? {
|
|
72
|
-
functionCallingConfig: this.instance.convertFromToolChoice(this.instance.toolChoice),
|
|
73
|
-
} : undefined,
|
|
74
|
-
systemInstruction,
|
|
75
|
-
generationConfig: this.instance.maxTokens || this.instance.additionalOptions ? {
|
|
76
|
-
maxOutputTokens: this.instance.maxTokens ?? undefined,
|
|
77
|
-
...this.instance.additionalOptions,
|
|
78
|
-
} : undefined,
|
|
79
|
-
};
|
|
80
|
-
ctx.logger.message?.trace(reqbody);
|
|
81
|
-
const res = await fetch(this.apiURL, {
|
|
82
|
-
method: 'POST',
|
|
83
|
-
headers: new Headers({
|
|
84
|
-
'Content-Type': 'application/json',
|
|
85
|
-
'x-goog-api-key': this.instance.apiKey,
|
|
86
|
-
}),
|
|
87
|
-
body: JSON.stringify(reqbody),
|
|
88
|
-
dispatcher: this.instance.proxyAgent,
|
|
89
|
-
signal,
|
|
90
|
-
});
|
|
91
|
-
ctx.logger.message?.trace(res);
|
|
92
|
-
assert(res.ok, new Error(undefined, { cause: res }));
|
|
93
|
-
const response = await res.json();
|
|
94
|
-
assert(response.candidates?.[0]?.content?.parts?.length, new ResponseInvalid('Content missing', { cause: response }));
|
|
95
|
-
if (response.candidates[0].finishReason === Google.FinishReason.MAX_TOKENS)
|
|
96
|
-
throw new ResponseInvalid('Token limit exceeded.', { cause: response });
|
|
97
|
-
assert(response.candidates[0].finishReason === Google.FinishReason.STOP, new ResponseInvalid('Abnormal finish reason', { cause: response }));
|
|
98
|
-
for (const part of response.candidates[0].content.parts) {
|
|
99
|
-
if (part.text)
|
|
100
|
-
ctx.logger.inference?.debug(part.text + '\n');
|
|
101
|
-
if (part.functionCall)
|
|
102
|
-
ctx.logger.message?.debug(part.functionCall);
|
|
33
|
+
return Google.createPartFromFunctionCall(part.name, part.args);
|
|
103
34
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const cacheMissTokenCount = response.usageMetadata.promptTokenCount - cacheHitTokenCount;
|
|
109
|
-
const thinkingTokenCount = response.usageMetadata.thoughtsTokenCount ?? 0;
|
|
110
|
-
const cost = this.instance.inputPrice * cacheMissTokenCount / 1e6 +
|
|
111
|
-
this.instance.cachedPrice * cacheHitTokenCount / 1e6 +
|
|
112
|
-
this.instance.outputPrice * candidatesTokenCount / 1e6 +
|
|
113
|
-
this.instance.outputPrice * thinkingTokenCount / 1e6;
|
|
114
|
-
ctx.logger.cost?.(cost);
|
|
115
|
-
const aiMessage = this.instance.convertToAiMessage(response.candidates[0].content);
|
|
116
|
-
this.instance.validateToolCallsByToolChoice(aiMessage.getFunctionCalls());
|
|
117
|
-
return aiMessage;
|
|
118
|
-
}
|
|
35
|
+
else
|
|
36
|
+
throw new Error();
|
|
37
|
+
});
|
|
38
|
+
return Google.createModelContent(parts);
|
|
119
39
|
}
|
|
120
|
-
|
|
121
|
-
|
|
40
|
+
}
|
|
41
|
+
GoogleCompatibleEngine.convertFromAiMessage = convertFromAiMessage;
|
|
42
|
+
function convertFromChatMessages(chatMessages) {
|
|
43
|
+
return chatMessages.map(chatMessage => {
|
|
44
|
+
if (chatMessage instanceof RoleMessage.User.Instance)
|
|
45
|
+
return this.convertFromUserMessage(chatMessage);
|
|
46
|
+
else if (chatMessage instanceof RoleMessage.Ai.Instance)
|
|
47
|
+
return this.convertFromAiMessage(chatMessage);
|
|
48
|
+
else
|
|
49
|
+
throw new Error();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
GoogleCompatibleEngine.convertFromChatMessages = convertFromChatMessages;
|
|
53
|
+
async function fetch(wfctx, session, signal) {
|
|
54
|
+
const systemInstruction = session.developerMessage && this.convertFromDeveloperMessage(session.developerMessage);
|
|
55
|
+
const contents = this.convertFromChatMessages(session.chatMessages);
|
|
56
|
+
await this.throttle.requests(wfctx);
|
|
57
|
+
const fdentries = Object.entries(this.fdm);
|
|
58
|
+
const tools = fdentries.map(fdentry => this.convertFromFunctionDeclarationEntry(fdentry));
|
|
59
|
+
const reqbody = {
|
|
60
|
+
contents,
|
|
61
|
+
tools: tools.length ? [{
|
|
62
|
+
functionDeclarations: tools,
|
|
63
|
+
}] : undefined,
|
|
64
|
+
toolConfig: tools.length ? {
|
|
65
|
+
functionCallingConfig: this.convertFromToolChoice(this.toolChoice),
|
|
66
|
+
} : undefined,
|
|
67
|
+
systemInstruction,
|
|
68
|
+
generationConfig: this.maxTokens || this.additionalOptions ? {
|
|
69
|
+
maxOutputTokens: this.maxTokens ?? undefined,
|
|
70
|
+
...this.additionalOptions,
|
|
71
|
+
} : undefined,
|
|
72
|
+
};
|
|
73
|
+
this.logger.message?.trace(reqbody);
|
|
74
|
+
const res = await Undici.fetch(this.apiURL, {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: new Headers({
|
|
77
|
+
'Content-Type': 'application/json',
|
|
78
|
+
'x-goog-api-key': this.apiKey,
|
|
79
|
+
}),
|
|
80
|
+
body: JSON.stringify(reqbody),
|
|
81
|
+
dispatcher: this.proxyAgent,
|
|
82
|
+
signal,
|
|
83
|
+
});
|
|
84
|
+
this.logger.message?.trace(res);
|
|
85
|
+
if (res.ok) { }
|
|
86
|
+
else
|
|
87
|
+
throw new Error(undefined, { cause: res });
|
|
88
|
+
const response = await res.json();
|
|
89
|
+
if (response.candidates?.[0]?.content?.parts?.length) { }
|
|
90
|
+
else
|
|
91
|
+
throw new ResponseInvalid('Content missing', { cause: response });
|
|
92
|
+
if (response.candidates[0].finishReason === Google.FinishReason.MAX_TOKENS)
|
|
93
|
+
throw new ResponseInvalid('Token limit exceeded.', { cause: response });
|
|
94
|
+
if (response.candidates[0].finishReason === Google.FinishReason.STOP) { }
|
|
95
|
+
else
|
|
96
|
+
throw new ResponseInvalid('Abnormal finish reason', { cause: response });
|
|
97
|
+
for (const part of response.candidates[0].content.parts) {
|
|
98
|
+
if (part.text)
|
|
99
|
+
this.logger.inference?.debug(part.text + '\n');
|
|
100
|
+
if (part.functionCall)
|
|
101
|
+
this.logger.message?.debug(part.functionCall);
|
|
102
|
+
}
|
|
103
|
+
if (response.usageMetadata?.promptTokenCount) { }
|
|
104
|
+
else
|
|
105
|
+
throw new Error('Prompt token count absent', { cause: response });
|
|
106
|
+
this.logger.message?.debug(response.usageMetadata);
|
|
107
|
+
const candidatesTokenCount = response.usageMetadata.candidatesTokenCount ?? 0;
|
|
108
|
+
const cacheHitTokenCount = response.usageMetadata.cachedContentTokenCount ?? 0;
|
|
109
|
+
const cacheMissTokenCount = response.usageMetadata.promptTokenCount - cacheHitTokenCount;
|
|
110
|
+
const thinkingTokenCount = response.usageMetadata.thoughtsTokenCount ?? 0;
|
|
111
|
+
const cost = this.inputPrice * cacheMissTokenCount / 1e6 +
|
|
112
|
+
this.cachePrice * cacheHitTokenCount / 1e6 +
|
|
113
|
+
this.outputPrice * candidatesTokenCount / 1e6 +
|
|
114
|
+
this.outputPrice * thinkingTokenCount / 1e6;
|
|
115
|
+
wfctx.cost?.(cost);
|
|
116
|
+
const aiMessage = this.convertToAiMessage(response.candidates[0].content);
|
|
117
|
+
this.validateToolCallsByToolChoice(aiMessage.getFunctionCalls());
|
|
118
|
+
return aiMessage;
|
|
119
|
+
}
|
|
120
|
+
GoogleCompatibleEngine.fetch = fetch;
|
|
122
121
|
function convertFromUserMessage(userMessage) {
|
|
123
122
|
const parts = userMessage.getParts().map(part => {
|
|
124
123
|
if (part instanceof RoleMessage.Part.Text.Instance)
|
|
@@ -149,15 +148,19 @@ export var GoogleCompatibleEngine;
|
|
|
149
148
|
return { mode: Google.FunctionCallingConfigMode.ANY, allowedFunctionNames: [...toolChoice] };
|
|
150
149
|
}
|
|
151
150
|
GoogleCompatibleEngine.convertFromToolChoice = convertFromToolChoice;
|
|
152
|
-
function convertToAiMessage(content
|
|
153
|
-
|
|
151
|
+
function convertToAiMessage(content) {
|
|
152
|
+
if (content.parts) { }
|
|
153
|
+
else
|
|
154
|
+
throw new Error();
|
|
154
155
|
return GoogleCompatibleEngine.Message.Ai.create(content.parts.flatMap(part => {
|
|
155
156
|
const parts = [];
|
|
156
|
-
|
|
157
|
+
if (part.functionCall || part.text) { }
|
|
158
|
+
else
|
|
159
|
+
throw new ResponseInvalid('Unknown content part', { cause: content });
|
|
157
160
|
if (part.text)
|
|
158
161
|
parts.push(RoleMessage.Part.Text.create(part.text));
|
|
159
162
|
if (part.functionCall)
|
|
160
|
-
parts.push(
|
|
163
|
+
parts.push(this.convertToFunctionCall(part.functionCall));
|
|
161
164
|
return parts;
|
|
162
165
|
}), content);
|
|
163
166
|
}
|
|
@@ -185,150 +188,88 @@ export var GoogleCompatibleEngine;
|
|
|
185
188
|
})(Ai = Message.Ai || (Message.Ai = {}));
|
|
186
189
|
})(Message = GoogleCompatibleEngine.Message || (GoogleCompatibleEngine.Message = {}));
|
|
187
190
|
class Instance {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
191
|
+
baseUrl;
|
|
192
|
+
apiKey;
|
|
193
|
+
model;
|
|
194
|
+
name;
|
|
195
|
+
inputPrice;
|
|
196
|
+
outputPrice;
|
|
197
|
+
cachePrice;
|
|
198
|
+
fdm;
|
|
199
|
+
additionalOptions;
|
|
200
|
+
throttle;
|
|
201
|
+
timeout;
|
|
202
|
+
maxTokens;
|
|
203
|
+
proxyAgent;
|
|
204
|
+
logger;
|
|
205
|
+
toolChoice;
|
|
206
|
+
parallelToolCall;
|
|
207
|
+
apiURL;
|
|
192
208
|
constructor(options) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return
|
|
218
|
-
}
|
|
219
|
-
set name(value) {
|
|
220
|
-
this.engineBase.name = value;
|
|
221
|
-
}
|
|
222
|
-
get inputPrice() {
|
|
223
|
-
return this.engineBase.inputPrice;
|
|
224
|
-
}
|
|
225
|
-
set inputPrice(value) {
|
|
226
|
-
this.engineBase.inputPrice = value;
|
|
227
|
-
}
|
|
228
|
-
get outputPrice() {
|
|
229
|
-
return this.engineBase.outputPrice;
|
|
230
|
-
}
|
|
231
|
-
set outputPrice(value) {
|
|
232
|
-
this.engineBase.outputPrice = value;
|
|
233
|
-
}
|
|
234
|
-
get cachedPrice() {
|
|
235
|
-
return this.engineBase.cachedPrice;
|
|
236
|
-
}
|
|
237
|
-
set cachedPrice(value) {
|
|
238
|
-
this.engineBase.cachedPrice = value;
|
|
239
|
-
}
|
|
240
|
-
get fdm() {
|
|
241
|
-
return this.engineBase.fdm;
|
|
242
|
-
}
|
|
243
|
-
set fdm(value) {
|
|
244
|
-
this.engineBase.fdm = value;
|
|
245
|
-
}
|
|
246
|
-
get additionalOptions() {
|
|
247
|
-
return this.engineBase.additionalOptions;
|
|
248
|
-
}
|
|
249
|
-
set additionalOptions(value) {
|
|
250
|
-
this.engineBase.additionalOptions = value;
|
|
251
|
-
}
|
|
252
|
-
get throttle() {
|
|
253
|
-
return this.engineBase.throttle;
|
|
254
|
-
}
|
|
255
|
-
set throttle(value) {
|
|
256
|
-
this.engineBase.throttle = value;
|
|
257
|
-
}
|
|
258
|
-
get timeout() {
|
|
259
|
-
return this.engineBase.timeout;
|
|
260
|
-
}
|
|
261
|
-
set timeout(value) {
|
|
262
|
-
this.engineBase.timeout = value;
|
|
263
|
-
}
|
|
264
|
-
get maxTokens() {
|
|
265
|
-
return this.engineBase.maxTokens;
|
|
266
|
-
}
|
|
267
|
-
set maxTokens(value) {
|
|
268
|
-
this.engineBase.maxTokens = value;
|
|
269
|
-
}
|
|
270
|
-
get proxyAgent() {
|
|
271
|
-
return this.engineBase.proxyAgent;
|
|
272
|
-
}
|
|
273
|
-
set proxyAgent(value) {
|
|
274
|
-
this.engineBase.proxyAgent = value;
|
|
275
|
-
}
|
|
276
|
-
get toolChoice() {
|
|
277
|
-
return this.compatibleEngineBase.toolChoice;
|
|
278
|
-
}
|
|
279
|
-
set toolChoice(value) {
|
|
280
|
-
this.compatibleEngineBase.toolChoice = value;
|
|
281
|
-
}
|
|
282
|
-
stateless(ctx, session) {
|
|
283
|
-
return this.compatibleEngineBase.stateless(ctx, session);
|
|
284
|
-
}
|
|
285
|
-
stateful(ctx, session) {
|
|
286
|
-
return this.compatibleEngineBase.stateful(ctx, session);
|
|
209
|
+
({
|
|
210
|
+
baseUrl: this.baseUrl,
|
|
211
|
+
apiKey: this.apiKey,
|
|
212
|
+
model: this.model,
|
|
213
|
+
name: this.name,
|
|
214
|
+
inputPrice: this.inputPrice,
|
|
215
|
+
outputPrice: this.outputPrice,
|
|
216
|
+
cachePrice: this.cachePrice,
|
|
217
|
+
fdm: this.fdm,
|
|
218
|
+
additionalOptions: this.additionalOptions,
|
|
219
|
+
throttle: this.throttle,
|
|
220
|
+
timeout: this.timeout,
|
|
221
|
+
maxTokens: this.maxTokens,
|
|
222
|
+
proxyAgent: this.proxyAgent,
|
|
223
|
+
logger: this.logger,
|
|
224
|
+
} = (Engine.OwnProps.init).call(this, options));
|
|
225
|
+
({ toolChoice: this.toolChoice } = (CompatibleEngine.OwnProps.init).call(this, options));
|
|
226
|
+
({ parallelToolCall: this.parallelToolCall } = (GoogleEngine.OwnProps.init).call(this, options));
|
|
227
|
+
({ apiURL: this.apiURL } = (GoogleCompatibleEngine.OwnProps.init).call(this, options));
|
|
228
|
+
}
|
|
229
|
+
stateless(wfctx, session) {
|
|
230
|
+
return (CompatibleEngine.stateless).call(this, wfctx, session);
|
|
231
|
+
}
|
|
232
|
+
stateful(wfctx, session) {
|
|
233
|
+
return (CompatibleEngine.stateful).call(this, wfctx, session);
|
|
287
234
|
}
|
|
288
235
|
appendUserMessage(session, message) {
|
|
289
|
-
return
|
|
236
|
+
return (CompatibleEngine.appendUserMessage).call(this, session, message);
|
|
290
237
|
}
|
|
291
238
|
pushUserMessage(session, message) {
|
|
292
|
-
return
|
|
239
|
+
return (CompatibleEngine.pushUserMessage).call(this, session, message);
|
|
293
240
|
}
|
|
294
241
|
validateToolCallsByToolChoice(toolCalls) {
|
|
295
|
-
return
|
|
296
|
-
}
|
|
297
|
-
get parallel() {
|
|
298
|
-
return this.googleEngineBase.parallel;
|
|
299
|
-
}
|
|
300
|
-
set parallel(value) {
|
|
301
|
-
this.googleEngineBase.parallel = value;
|
|
242
|
+
return (CompatibleEngine.validateToolCallsByToolChoice).call(this, toolCalls);
|
|
302
243
|
}
|
|
303
244
|
convertFromFunctionCall(fc) {
|
|
304
|
-
return
|
|
245
|
+
return (GoogleEngine.convertFromFunctionCall).call(this, fc);
|
|
305
246
|
}
|
|
306
247
|
convertToFunctionCall(googlefc) {
|
|
307
|
-
return
|
|
248
|
+
return (GoogleEngine.convertToFunctionCall).call(this, googlefc);
|
|
308
249
|
}
|
|
309
250
|
convertFromFunctionDeclarationEntry(fdentry) {
|
|
310
|
-
return
|
|
251
|
+
return (GoogleEngine.convertFromFunctionDeclarationEntry).call(this, fdentry);
|
|
311
252
|
}
|
|
312
253
|
convertFromUserMessage(userMessage) {
|
|
313
|
-
return
|
|
254
|
+
return (GoogleCompatibleEngine.convertFromUserMessage).call(this, userMessage);
|
|
314
255
|
}
|
|
315
256
|
convertFromAiMessage(aiMessage) {
|
|
316
|
-
return
|
|
257
|
+
return (GoogleCompatibleEngine.convertFromAiMessage).call(this, aiMessage);
|
|
317
258
|
}
|
|
318
259
|
convertFromDeveloperMessage(developerMessage) {
|
|
319
|
-
return
|
|
260
|
+
return (GoogleCompatibleEngine.convertFromDeveloperMessage).call(this, developerMessage);
|
|
320
261
|
}
|
|
321
262
|
convertFromChatMessages(chatMessages) {
|
|
322
|
-
return
|
|
263
|
+
return (GoogleCompatibleEngine.convertFromChatMessages).call(this, chatMessages);
|
|
323
264
|
}
|
|
324
265
|
convertToAiMessage(content) {
|
|
325
|
-
return
|
|
266
|
+
return (GoogleCompatibleEngine.convertToAiMessage).call(this, content);
|
|
326
267
|
}
|
|
327
268
|
convertFromToolChoice(toolChoice) {
|
|
328
|
-
return
|
|
269
|
+
return (GoogleCompatibleEngine.convertFromToolChoice).call(this, toolChoice);
|
|
329
270
|
}
|
|
330
|
-
fetch(
|
|
331
|
-
return
|
|
271
|
+
fetch(wfctx, session, signal) {
|
|
272
|
+
return (GoogleCompatibleEngine.fetch).call(this, wfctx, session, signal);
|
|
332
273
|
}
|
|
333
274
|
}
|
|
334
275
|
GoogleCompatibleEngine.Instance = Instance;
|