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