@zimtsui/brainswitch 0.0.1 → 0.0.2
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 +98 -83
- package/build/adaptor.d.ts +1 -1
- package/build/adaptor.js +8 -8
- package/build/adaptor.js.map +1 -1
- package/build/agentloop.d.ts +1 -1
- package/build/agentloop.js +21 -21
- package/build/agentloop.js.map +1 -1
- package/build/api-types/base.d.ts +0 -2
- package/build/api-types/base.js +0 -2
- package/build/api-types/base.js.map +1 -1
- package/build/api-types/google-base.d.ts +15 -3
- package/build/api-types/google-base.js +33 -13
- package/build/api-types/google-base.js.map +1 -1
- package/build/api-types/google-rest.d.ts +13 -9
- package/build/api-types/google-rest.js +102 -89
- package/build/api-types/google-rest.js.map +1 -1
- package/build/api-types/huggingface-cerebras-qwen3-thinking.d.ts +9 -7
- package/build/api-types/huggingface-cerebras-qwen3-thinking.js +46 -41
- package/build/api-types/huggingface-cerebras-qwen3-thinking.js.map +1 -1
- package/build/api-types/openai-chatcompletions-base.d.ts +2 -6
- package/build/api-types/openai-chatcompletions-base.js +5 -12
- package/build/api-types/openai-chatcompletions-base.js.map +1 -1
- package/build/api-types/openai-chatcompletions-monolith-base.d.ts +1 -1
- package/build/api-types/openai-chatcompletions-monolith-base.js +12 -16
- package/build/api-types/openai-chatcompletions-monolith-base.js.map +1 -1
- package/build/api-types/openai-chatcompletions-stream-base.d.ts +1 -1
- package/build/api-types/openai-chatcompletions-stream-base.js +13 -16
- package/build/api-types/openai-chatcompletions-stream-base.js.map +1 -1
- package/build/api-types/openai-chatcompletions.d.ts +4 -2
- package/build/api-types/openai-chatcompletions.js +9 -4
- package/build/api-types/openai-chatcompletions.js.map +1 -1
- package/build/api-types/openai-responses.d.ts +35 -21
- package/build/api-types/openai-responses.js +211 -188
- package/build/api-types/openai-responses.js.map +1 -1
- package/build/api-types/openrouter-monolith.d.ts +8 -8
- package/build/api-types/openrouter-monolith.js +39 -44
- package/build/api-types/openrouter-monolith.js.map +1 -1
- package/build/api-types/openrouter-stream.d.ts +10 -10
- package/build/api-types/openrouter-stream.js +49 -54
- package/build/api-types/openrouter-stream.js.map +1 -1
- package/build/api-types/qwen.d.ts +5 -3
- package/build/api-types/qwen.js +11 -6
- package/build/api-types/qwen.js.map +1 -1
- package/build/config.js.map +1 -1
- package/build/endpoint-spec.js.map +1 -1
- package/build/engine.d.ts +1 -1
- package/build/engine.js +2 -2
- package/build/engine.js.map +1 -1
- package/build/function.d.ts +14 -2
- package/build/function.js +12 -0
- package/build/function.js.map +1 -1
- package/build/session.d.ts +105 -38
- package/build/session.js +212 -60
- package/build/session.js.map +1 -1
- package/build/throttle.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/build/test.d.ts +0 -1
- package/build/test.js +0 -23
- package/build/test.js.map +0 -1
|
@@ -2,9 +2,9 @@ import { RoleMessage } from "../session.js";
|
|
|
2
2
|
import { Function } from "../function.js";
|
|
3
3
|
import OpenAI from 'openai';
|
|
4
4
|
import assert from 'node:assert';
|
|
5
|
-
import { OpenAIChatCompletionsAPIBase
|
|
5
|
+
import { OpenAIChatCompletionsAPIBase } from "./openai-chatcompletions-base.js";
|
|
6
6
|
import {} from "../inference-context.js";
|
|
7
|
-
import {
|
|
7
|
+
import { TransientError } from "./base.js";
|
|
8
8
|
export class OpenAIChatCompletionsStreamAPIBase extends OpenAIChatCompletionsAPIBase {
|
|
9
9
|
makeStreamParams(session) {
|
|
10
10
|
return {
|
|
@@ -30,8 +30,6 @@ export class OpenAIChatCompletionsStreamAPIBase extends OpenAIChatCompletionsAPI
|
|
|
30
30
|
return this.convertToFunctionCall(apifc);
|
|
31
31
|
}
|
|
32
32
|
async stream(ctx, session, retry = 0) {
|
|
33
|
-
if (retry > 2)
|
|
34
|
-
throw new RetryLimitError();
|
|
35
33
|
const signalTimeout = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
36
34
|
const signal = ctx.signal && signalTimeout ? AbortSignal.any([
|
|
37
35
|
ctx.signal,
|
|
@@ -42,7 +40,8 @@ export class OpenAIChatCompletionsStreamAPIBase extends OpenAIChatCompletionsAPI
|
|
|
42
40
|
await this.throttle.requests(ctx);
|
|
43
41
|
await this.throttle.inputTokens(this.tokenize(params), ctx);
|
|
44
42
|
try {
|
|
45
|
-
const stream = await this.client.chat.completions.create(params, { signal })
|
|
43
|
+
const stream = await this.client.chat.completions.create(params, { signal })
|
|
44
|
+
.catch(e => Promise.reject(new TransientError(undefined, { cause: e })));
|
|
46
45
|
let chunk = null;
|
|
47
46
|
let usage = { completion_tokens: 0, prompt_tokens: 0, total_tokens: 0 };
|
|
48
47
|
let finishReason = null;
|
|
@@ -76,7 +75,7 @@ export class OpenAIChatCompletionsStreamAPIBase extends OpenAIChatCompletionsAPI
|
|
|
76
75
|
ctx.logger.inference?.debug(deltaText);
|
|
77
76
|
usage = newUsage;
|
|
78
77
|
}
|
|
79
|
-
assert(finishReason && ['stop', 'tool_calls'].includes(finishReason), new
|
|
78
|
+
assert(finishReason && ['stop', 'tool_calls'].includes(finishReason), new TransientError('Invalid finish reason', { cause: finishReason }));
|
|
80
79
|
ctx.logger.inference?.debug('\n');
|
|
81
80
|
assert(usage);
|
|
82
81
|
if (toolCalls.length)
|
|
@@ -85,25 +84,23 @@ export class OpenAIChatCompletionsStreamAPIBase extends OpenAIChatCompletionsAPI
|
|
|
85
84
|
ctx.logger.cost?.(cost);
|
|
86
85
|
const fcs = toolCalls.map(apifc => this.convertToFunctionCallFromDelta(apifc));
|
|
87
86
|
this.validateFunctionCallByToolChoice(fcs);
|
|
88
|
-
return
|
|
89
|
-
|
|
87
|
+
return RoleMessage.AI.create([
|
|
88
|
+
RoleMessage.Part.Text.create(this.extractContent(text)),
|
|
90
89
|
...fcs,
|
|
91
90
|
]);
|
|
92
91
|
}
|
|
93
92
|
catch (e) {
|
|
94
93
|
if (ctx.signal?.aborted)
|
|
95
94
|
throw new DOMException(undefined, 'AbortError');
|
|
96
|
-
if (
|
|
97
|
-
else if (e instanceof
|
|
98
|
-
else if (e instanceof OpenAI.APIUserAbortError) { } // 推理超时
|
|
99
|
-
else if (e instanceof OpenAI.BadRequestError) {
|
|
100
|
-
ctx.logger.message?.warn(params);
|
|
101
|
-
}
|
|
95
|
+
else if (signalTimeout?.aborted) { } // 推理超时
|
|
96
|
+
else if (e instanceof TransientError) { } // 模型抽风
|
|
102
97
|
else
|
|
103
98
|
throw e;
|
|
104
99
|
ctx.logger.message?.warn(e);
|
|
105
|
-
|
|
106
|
-
|
|
100
|
+
if (retry < 3)
|
|
101
|
+
return this.stream(ctx, session, retry + 1);
|
|
102
|
+
else
|
|
103
|
+
throw e;
|
|
107
104
|
}
|
|
108
105
|
}
|
|
109
106
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-chatcompletions-stream-base.js","sourceRoot":"","sources":["../../src/api-types/openai-chatcompletions-stream-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,4BAA4B,EAAE,
|
|
1
|
+
{"version":3,"file":"openai-chatcompletions-stream-base.js","sourceRoot":"","sources":["../../src/api-types/openai-chatcompletions-stream-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgB,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,4BAA4B,EAAE,MAAM,kCAAkC,CAAC;AAChF,OAAO,EAAyB,MAAM,yBAAyB,CAAC;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,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OpenAIChatCompletionsMonolithAPIBase } from './openai-chatcompletions-monolith-base.ts';
|
|
4
|
-
export declare
|
|
5
|
-
|
|
4
|
+
export declare namespace OpenAIChatCompletionsAPI {
|
|
5
|
+
function makeEngine<fdm extends Function.Declaration.Map = {}>(options: Engine.Options<fdm>): Engine<Function.Declaration.From<fdm>>;
|
|
6
|
+
class Constructor<in out fdm extends Function.Declaration.Map = {}> extends OpenAIChatCompletionsMonolithAPIBase<fdm> {
|
|
7
|
+
}
|
|
6
8
|
}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { Engine } from "../engine.js";
|
|
2
2
|
import { Function } from "../function.js";
|
|
3
3
|
import { OpenAIChatCompletionsMonolithAPIBase } from "./openai-chatcompletions-monolith-base.js";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export var OpenAIChatCompletionsAPI;
|
|
5
|
+
(function (OpenAIChatCompletionsAPI) {
|
|
6
|
+
function makeEngine(options) {
|
|
7
|
+
const api = new Constructor(options);
|
|
7
8
|
return api.monolith.bind(api);
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
OpenAIChatCompletionsAPI.makeEngine = makeEngine;
|
|
11
|
+
class Constructor extends OpenAIChatCompletionsMonolithAPIBase {
|
|
12
|
+
}
|
|
13
|
+
OpenAIChatCompletionsAPI.Constructor = Constructor;
|
|
14
|
+
})(OpenAIChatCompletionsAPI || (OpenAIChatCompletionsAPI = {}));
|
|
10
15
|
//# sourceMappingURL=openai-chatcompletions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-chatcompletions.js","sourceRoot":"","sources":["../../src/api-types/openai-chatcompletions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,oCAAoC,EAAE,MAAM,2CAA2C,CAAC;AAGjG,MAAM,
|
|
1
|
+
{"version":3,"file":"openai-chatcompletions.js","sourceRoot":"","sources":["../../src/api-types/openai-chatcompletions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,oCAAoC,EAAE,MAAM,2CAA2C,CAAC;AAGjG,MAAM,KAAW,wBAAwB,CAOxC;AAPD,WAAiB,wBAAwB;IACxC,SAAgB,UAAU,CAA4C,OAA4B;QACjG,MAAM,GAAG,GAAG,IAAI,WAAW,CAAM,OAAO,CAAC,CAAC;QAC1C,OAAO,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC/B,CAAC;IAHe,mCAAU,aAGzB,CAAA;IAED,MAAa,WAA8D,SAAQ,oCAAyC;KAAG;IAAlH,oCAAW,cAAuG,CAAA;AAChI,CAAC,EAPgB,wBAAwB,KAAxB,wBAAwB,QAOxC"}
|
|
@@ -5,26 +5,40 @@ import { Engine } from '../engine.ts';
|
|
|
5
5
|
import { type InferenceContext } from '../inference-context.ts';
|
|
6
6
|
import OpenAI from 'openai';
|
|
7
7
|
import { ProxyAgent } from 'undici';
|
|
8
|
-
export declare
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
export declare namespace OpenAIResponsesAPI {
|
|
9
|
+
function makeEngine<fdm extends Function.Declaration.Map = {}>(options: Engine.Options<fdm>): Engine<Function.Declaration.From<fdm>>;
|
|
10
|
+
class Constructor<in out fdm extends Function.Declaration.Map = {}> extends APIBase<fdm> {
|
|
11
|
+
protected client: OpenAI;
|
|
12
|
+
protected proxyAgent?: ProxyAgent;
|
|
13
|
+
constructor(options: Engine.Options<fdm>);
|
|
14
|
+
protected convertFromFunctionCall(fc: Function.Call.Distributive<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseFunctionToolCall;
|
|
15
|
+
protected convertToFunctionCall(apifc: OpenAI.Responses.ResponseFunctionToolCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
16
|
+
protected convertFromFunctionResponse(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInputItem.FunctionCallOutput;
|
|
17
|
+
protected convertFromUserMessage(userMessage: RoleMessage.User<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInput;
|
|
18
|
+
protected convertFromAIMessage(aiMessage: RoleMessage.AI<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInput;
|
|
19
|
+
protected convertFromChatMessage(chatMessage: ChatMessage<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInput;
|
|
20
|
+
protected convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): OpenAI.Responses.FunctionTool;
|
|
21
|
+
protected makeMonolithParams(session: Session<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseCreateParamsNonStreaming;
|
|
22
|
+
protected convertToAIMessage(output: OpenAI.Responses.ResponseOutputItem[]): OpenAIResponsesAIMessage<Function.Declaration.From<fdm>>;
|
|
23
|
+
protected validateFunctionCallByToolChoice(functionCalls: Function.Call.Distributive<Function.Declaration.From<fdm>>[]): void;
|
|
24
|
+
protected calcCost(usage: OpenAI.Responses.ResponseUsage): number;
|
|
25
|
+
protected tokenize(params: OpenAI.Responses.ResponseCreateParams): number;
|
|
26
|
+
monolith(ctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, retry?: number): Promise<RoleMessage.AI<Function.Declaration.From<fdm>>>;
|
|
27
|
+
}
|
|
26
28
|
}
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
export type OpenAIResponsesAIMessage<fdu extends Function.Declaration> = OpenAIResponsesAIMessage.Constructor<fdu>;
|
|
30
|
+
export declare namespace OpenAIResponsesAIMessage {
|
|
31
|
+
function create<fdu extends Function.Declaration>(parts: RoleMessage.AI.Part<fdu>[], raw: OpenAI.Responses.ResponseOutputItem[]): OpenAIResponsesAIMessage<fdu>;
|
|
32
|
+
const NOMINAL: unique symbol;
|
|
33
|
+
class Constructor<out fdu extends Function.Declaration> extends RoleMessage.AI.Constructor<fdu> {
|
|
34
|
+
raw: OpenAI.Responses.ResponseOutputItem[];
|
|
35
|
+
readonly [NOMINAL]: void;
|
|
36
|
+
constructor(parts: RoleMessage.AI.Part<fdu>[], raw: OpenAI.Responses.ResponseOutputItem[]);
|
|
37
|
+
}
|
|
38
|
+
interface Snapshot<in out fdu extends Function.Declaration = never> {
|
|
39
|
+
parts: RoleMessage.AI.Part.Snapshot<fdu>[];
|
|
40
|
+
raw: OpenAI.Responses.ResponseOutputItem[];
|
|
41
|
+
}
|
|
42
|
+
function restore<fdu extends Function.Declaration>(snapshot: Snapshot<fdu>): OpenAIResponsesAIMessage<fdu>;
|
|
43
|
+
function capture<fdu extends Function.Declaration>(message: OpenAIResponsesAIMessage<fdu>): Snapshot<fdu>;
|
|
30
44
|
}
|
|
@@ -5,212 +5,235 @@ import { Engine } from "../engine.js";
|
|
|
5
5
|
import {} from "../inference-context.js";
|
|
6
6
|
import OpenAI from 'openai';
|
|
7
7
|
import assert from 'node:assert';
|
|
8
|
-
import { TransientError
|
|
8
|
+
import { TransientError } from "./base.js";
|
|
9
9
|
import { ProxyAgent } from 'undici';
|
|
10
10
|
import Ajv from 'ajv';
|
|
11
11
|
const ajv = new Ajv();
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const api = new OpenAIResponsesAPI(options);
|
|
12
|
+
export var OpenAIResponsesAPI;
|
|
13
|
+
(function (OpenAIResponsesAPI) {
|
|
14
|
+
function makeEngine(options) {
|
|
15
|
+
const api = new OpenAIResponsesAPI.Constructor(options);
|
|
17
16
|
return api.monolith.bind(api);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
args,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
else if (part instanceof Function.Response)
|
|
74
|
-
return this.convertFromFunctionResponse(part);
|
|
75
|
-
else
|
|
76
|
-
throw new Error();
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
convertFromAIMessage(aiMessage) {
|
|
80
|
-
if (aiMessage instanceof OpenAIResponsesAIMessage)
|
|
81
|
-
return aiMessage.raw;
|
|
82
|
-
else {
|
|
83
|
-
return aiMessage.parts.map(part => {
|
|
84
|
-
if (part instanceof RoleMessage.Text)
|
|
18
|
+
OpenAIResponsesAPI.makeEngine = makeEngine;
|
|
19
|
+
class Constructor extends APIBase {
|
|
20
|
+
client;
|
|
21
|
+
proxyAgent;
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super(options);
|
|
24
|
+
this.proxyAgent = options.proxy ? new ProxyAgent(options.proxy) : undefined;
|
|
25
|
+
this.client = new OpenAI({
|
|
26
|
+
baseURL: this.baseUrl,
|
|
27
|
+
apiKey: this.apiKey,
|
|
28
|
+
fetchOptions: {
|
|
29
|
+
dispatcher: this.proxyAgent,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
convertFromFunctionCall(fc) {
|
|
34
|
+
assert(fc.id);
|
|
35
|
+
return {
|
|
36
|
+
type: 'function_call',
|
|
37
|
+
call_id: fc.id,
|
|
38
|
+
name: fc.name,
|
|
39
|
+
arguments: JSON.stringify(fc.args),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
convertToFunctionCall(apifc) {
|
|
43
|
+
const fditem = this.functionDeclarationMap[apifc.name];
|
|
44
|
+
assert(fditem, new TransientError('Invalid function call', { cause: apifc }));
|
|
45
|
+
const args = (() => {
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(apifc.arguments);
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
return new TransientError('Invalid function call', { cause: apifc });
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
assert(ajv.validate(fditem.paraschema, args), new TransientError('Invalid function call', { cause: apifc }));
|
|
54
|
+
return Function.Call.create({
|
|
55
|
+
id: apifc.call_id,
|
|
56
|
+
name: apifc.name,
|
|
57
|
+
args,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
convertFromFunctionResponse(fr) {
|
|
61
|
+
assert(fr.id);
|
|
62
|
+
return {
|
|
63
|
+
type: 'function_call_output',
|
|
64
|
+
call_id: fr.id,
|
|
65
|
+
output: fr.text,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
convertFromUserMessage(userMessage) {
|
|
69
|
+
return userMessage.parts.map(part => {
|
|
70
|
+
if (part instanceof RoleMessage.Part.Text.Constructor)
|
|
85
71
|
return {
|
|
86
|
-
|
|
72
|
+
type: 'message',
|
|
73
|
+
role: 'user',
|
|
87
74
|
content: part.text,
|
|
88
75
|
};
|
|
89
|
-
else if (part instanceof Function.
|
|
90
|
-
return this.
|
|
76
|
+
else if (part instanceof Function.Response)
|
|
77
|
+
return this.convertFromFunctionResponse(part);
|
|
91
78
|
else
|
|
92
79
|
throw new Error();
|
|
93
80
|
});
|
|
94
81
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
type: 'function',
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
makeMonolithParams(session) {
|
|
114
|
-
return {
|
|
115
|
-
model: this.model,
|
|
116
|
-
include: ['reasoning.encrypted_content'],
|
|
117
|
-
store: false,
|
|
118
|
-
input: session.chatMessages.flatMap(chatMessage => this.convertFromChatMessage(chatMessage)),
|
|
119
|
-
instructions: session.developerMessage?.getOnlyText(),
|
|
120
|
-
tools: Object.keys(this.functionDeclarationMap).length
|
|
121
|
-
? Object.entries(this.functionDeclarationMap).map(fdentry => this.convertFromFunctionDeclarationEntry(fdentry)) : undefined,
|
|
122
|
-
tool_choice: Object.keys(this.functionDeclarationMap).length ? 'required' : undefined,
|
|
123
|
-
parallel_tool_calls: Object.keys(this.functionDeclarationMap).length ? false : undefined,
|
|
124
|
-
...this.customOptions,
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
convertToAIMessage(output) {
|
|
128
|
-
const parts = output.flatMap((item) => {
|
|
129
|
-
if (item.type === 'message') {
|
|
130
|
-
assert(item.content.every(part => part.type === 'output_text'));
|
|
131
|
-
return [new RoleMessage.Text(item.content.map(part => part.text).join(''))];
|
|
82
|
+
convertFromAIMessage(aiMessage) {
|
|
83
|
+
if (aiMessage instanceof OpenAIResponsesAIMessage.Constructor)
|
|
84
|
+
return aiMessage.raw;
|
|
85
|
+
else {
|
|
86
|
+
return aiMessage.parts.map(part => {
|
|
87
|
+
if (part instanceof RoleMessage.Part.Text.Constructor)
|
|
88
|
+
return {
|
|
89
|
+
role: 'assistant',
|
|
90
|
+
content: part.text,
|
|
91
|
+
};
|
|
92
|
+
else if (part instanceof Function.Call)
|
|
93
|
+
return this.convertFromFunctionCall(part);
|
|
94
|
+
else
|
|
95
|
+
throw new Error();
|
|
96
|
+
});
|
|
132
97
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
return
|
|
98
|
+
}
|
|
99
|
+
convertFromChatMessage(chatMessage) {
|
|
100
|
+
if (chatMessage instanceof RoleMessage.User.Constructor)
|
|
101
|
+
return this.convertFromUserMessage(chatMessage);
|
|
102
|
+
else if (chatMessage instanceof RoleMessage.AI.Constructor)
|
|
103
|
+
return this.convertFromAIMessage(chatMessage);
|
|
137
104
|
else
|
|
138
105
|
throw new Error();
|
|
139
|
-
});
|
|
140
|
-
return new OpenAIResponsesAIMessage(parts, output);
|
|
141
|
-
}
|
|
142
|
-
validateFunctionCallByToolChoice(functionCalls) {
|
|
143
|
-
// https://community.openai.com/t/function-call-with-finish-reason-of-stop/437226/7
|
|
144
|
-
if (this.toolChoice === Function.ToolChoice.REQUIRED)
|
|
145
|
-
assert(functionCalls.length, new TransientError());
|
|
146
|
-
else if (this.toolChoice instanceof Array)
|
|
147
|
-
for (const fc of functionCalls)
|
|
148
|
-
assert(this.toolChoice.includes(fc.name), new TransientError());
|
|
149
|
-
else if (this.toolChoice === Function.ToolChoice.NONE)
|
|
150
|
-
assert(!functionCalls.length, new TransientError());
|
|
151
|
-
}
|
|
152
|
-
calcCost(usage) {
|
|
153
|
-
const cacheHitTokenCount = usage.input_tokens_details.cached_tokens;
|
|
154
|
-
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
155
|
-
return this.inputPrice * cacheMissTokenCount / 1e6 +
|
|
156
|
-
this.cachedPrice * cacheHitTokenCount / 1e6 +
|
|
157
|
-
this.outputPrice * usage.output_tokens / 1e6;
|
|
158
|
-
}
|
|
159
|
-
tokenize(params) {
|
|
160
|
-
return JSON.stringify(params).length;
|
|
161
|
-
}
|
|
162
|
-
async monolith(ctx, session, retry = 0) {
|
|
163
|
-
if (retry > 2)
|
|
164
|
-
throw new RetryLimitError();
|
|
165
|
-
const signalTimeout = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
166
|
-
const signal = ctx.signal && signalTimeout ? AbortSignal.any([
|
|
167
|
-
ctx.signal,
|
|
168
|
-
signalTimeout,
|
|
169
|
-
]) : ctx.signal || signalTimeout;
|
|
170
|
-
const params = this.makeMonolithParams(session);
|
|
171
|
-
ctx.logger.message?.trace(params);
|
|
172
|
-
await this.throttle.requests(ctx);
|
|
173
|
-
await this.throttle.inputTokens(this.tokenize(params), ctx);
|
|
174
|
-
try {
|
|
175
|
-
const response = await this.client.responses.create(params, { signal });
|
|
176
|
-
ctx.logger.message?.trace(response);
|
|
177
|
-
const aiMessage = this.convertToAIMessage(response.output);
|
|
178
|
-
const text = aiMessage.getText();
|
|
179
|
-
if (text)
|
|
180
|
-
ctx.logger.inference?.debug(text + '\n');
|
|
181
|
-
const apifcs = response.output.filter(item => item.type === 'function_call');
|
|
182
|
-
for (const apifc of apifcs)
|
|
183
|
-
ctx.logger.message?.debug(apifc);
|
|
184
|
-
assert(response.usage);
|
|
185
|
-
const cost = this.calcCost(response.usage);
|
|
186
|
-
ctx.logger.cost?.(cost);
|
|
187
|
-
ctx.logger.message?.debug(response.usage);
|
|
188
|
-
this.throttle.outputTokens(response.usage.output_tokens);
|
|
189
|
-
const functionCalls = aiMessage.getFunctionCalls();
|
|
190
|
-
this.validateFunctionCallByToolChoice(functionCalls);
|
|
191
|
-
return aiMessage;
|
|
192
106
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
107
|
+
convertFromFunctionDeclarationEntry(fdentry) {
|
|
108
|
+
return {
|
|
109
|
+
name: fdentry[0],
|
|
110
|
+
description: fdentry[1].description,
|
|
111
|
+
parameters: fdentry[1].paraschema,
|
|
112
|
+
strict: true,
|
|
113
|
+
type: 'function',
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
makeMonolithParams(session) {
|
|
117
|
+
return {
|
|
118
|
+
model: this.model,
|
|
119
|
+
include: ['reasoning.encrypted_content'],
|
|
120
|
+
store: false,
|
|
121
|
+
input: session.chatMessages.flatMap(chatMessage => this.convertFromChatMessage(chatMessage)),
|
|
122
|
+
instructions: session.developerMessage?.getOnlyText(),
|
|
123
|
+
tools: Object.keys(this.functionDeclarationMap).length
|
|
124
|
+
? Object.entries(this.functionDeclarationMap).map(fdentry => this.convertFromFunctionDeclarationEntry(fdentry)) : undefined,
|
|
125
|
+
tool_choice: Object.keys(this.functionDeclarationMap).length ? 'required' : undefined,
|
|
126
|
+
parallel_tool_calls: Object.keys(this.functionDeclarationMap).length ? false : undefined,
|
|
127
|
+
...this.customOptions,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
convertToAIMessage(output) {
|
|
131
|
+
const parts = output.flatMap((item) => {
|
|
132
|
+
if (item.type === 'message') {
|
|
133
|
+
assert(item.content.every(part => part.type === 'output_text'));
|
|
134
|
+
return [RoleMessage.Part.Text.create(item.content.map(part => part.text).join(''))];
|
|
135
|
+
}
|
|
136
|
+
else if (item.type === 'function_call')
|
|
137
|
+
return [this.convertToFunctionCall(item)];
|
|
138
|
+
else if (item.type === 'reasoning')
|
|
139
|
+
return [];
|
|
140
|
+
else
|
|
141
|
+
throw new Error();
|
|
142
|
+
});
|
|
143
|
+
return OpenAIResponsesAIMessage.create(parts, output);
|
|
144
|
+
}
|
|
145
|
+
validateFunctionCallByToolChoice(functionCalls) {
|
|
146
|
+
// https://community.openai.com/t/function-call-with-finish-reason-of-stop/437226/7
|
|
147
|
+
if (this.toolChoice === Function.ToolChoice.REQUIRED)
|
|
148
|
+
assert(functionCalls.length, new TransientError());
|
|
149
|
+
else if (this.toolChoice instanceof Array)
|
|
150
|
+
for (const fc of functionCalls)
|
|
151
|
+
assert(this.toolChoice.includes(fc.name), new TransientError());
|
|
152
|
+
else if (this.toolChoice === Function.ToolChoice.NONE)
|
|
153
|
+
assert(!functionCalls.length, new TransientError());
|
|
154
|
+
}
|
|
155
|
+
calcCost(usage) {
|
|
156
|
+
const cacheHitTokenCount = usage.input_tokens_details.cached_tokens;
|
|
157
|
+
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
158
|
+
return this.inputPrice * cacheMissTokenCount / 1e6 +
|
|
159
|
+
this.cachedPrice * cacheHitTokenCount / 1e6 +
|
|
160
|
+
this.outputPrice * usage.output_tokens / 1e6;
|
|
161
|
+
}
|
|
162
|
+
tokenize(params) {
|
|
163
|
+
return JSON.stringify(params).length;
|
|
164
|
+
}
|
|
165
|
+
async monolith(ctx, session, retry = 0) {
|
|
166
|
+
const signalTimeout = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
167
|
+
const signal = ctx.signal && signalTimeout ? AbortSignal.any([
|
|
168
|
+
ctx.signal,
|
|
169
|
+
signalTimeout,
|
|
170
|
+
]) : ctx.signal || signalTimeout;
|
|
171
|
+
const params = this.makeMonolithParams(session);
|
|
172
|
+
ctx.logger.message?.trace(params);
|
|
173
|
+
await this.throttle.requests(ctx);
|
|
174
|
+
await this.throttle.inputTokens(this.tokenize(params), ctx);
|
|
175
|
+
try {
|
|
176
|
+
const response = await this.client.responses.create(params, { signal })
|
|
177
|
+
.catch(e => Promise.reject(new TransientError(undefined, { cause: e })));
|
|
178
|
+
ctx.logger.message?.trace(response);
|
|
179
|
+
const aiMessage = this.convertToAIMessage(response.output);
|
|
180
|
+
const text = aiMessage.getText();
|
|
181
|
+
if (text)
|
|
182
|
+
ctx.logger.inference?.debug(text + '\n');
|
|
183
|
+
const apifcs = response.output.filter(item => item.type === 'function_call');
|
|
184
|
+
for (const apifc of apifcs)
|
|
185
|
+
ctx.logger.message?.debug(apifc);
|
|
186
|
+
assert(response.usage);
|
|
187
|
+
const cost = this.calcCost(response.usage);
|
|
188
|
+
ctx.logger.cost?.(cost);
|
|
189
|
+
ctx.logger.message?.debug(response.usage);
|
|
190
|
+
this.throttle.outputTokens(response.usage.output_tokens);
|
|
191
|
+
const functionCalls = aiMessage.getFunctionCalls();
|
|
192
|
+
this.validateFunctionCallByToolChoice(functionCalls);
|
|
193
|
+
return aiMessage;
|
|
200
194
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
195
|
+
catch (e) {
|
|
196
|
+
if (ctx.signal?.aborted)
|
|
197
|
+
throw new DOMException(undefined, 'AbortError');
|
|
198
|
+
else if (signalTimeout?.aborted) { } // 推理超时
|
|
199
|
+
else if (e instanceof TransientError) { } // 模型抽风
|
|
200
|
+
else
|
|
201
|
+
throw e;
|
|
202
|
+
ctx.logger.message?.warn(e);
|
|
203
|
+
if (retry < 3)
|
|
204
|
+
return await this.monolith(ctx, session, retry + 1);
|
|
205
|
+
else
|
|
206
|
+
throw e;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
OpenAIResponsesAPI.Constructor = Constructor;
|
|
211
|
+
})(OpenAIResponsesAPI || (OpenAIResponsesAPI = {}));
|
|
212
|
+
export var OpenAIResponsesAIMessage;
|
|
213
|
+
(function (OpenAIResponsesAIMessage) {
|
|
214
|
+
function create(parts, raw) {
|
|
215
|
+
return new Constructor(parts, raw);
|
|
216
|
+
}
|
|
217
|
+
OpenAIResponsesAIMessage.create = create;
|
|
218
|
+
OpenAIResponsesAIMessage.NOMINAL = Symbol();
|
|
219
|
+
class Constructor extends RoleMessage.AI.Constructor {
|
|
220
|
+
raw;
|
|
221
|
+
constructor(parts, raw) {
|
|
222
|
+
super(parts);
|
|
223
|
+
this.raw = raw;
|
|
206
224
|
}
|
|
207
225
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
226
|
+
OpenAIResponsesAIMessage.Constructor = Constructor;
|
|
227
|
+
function restore(snapshot) {
|
|
228
|
+
return new Constructor(RoleMessage.AI.restore(snapshot.parts).parts, snapshot.raw);
|
|
229
|
+
}
|
|
230
|
+
OpenAIResponsesAIMessage.restore = restore;
|
|
231
|
+
function capture(message) {
|
|
232
|
+
return {
|
|
233
|
+
parts: RoleMessage.AI.capture(message),
|
|
234
|
+
raw: message.raw,
|
|
235
|
+
};
|
|
214
236
|
}
|
|
215
|
-
|
|
237
|
+
OpenAIResponsesAIMessage.capture = capture;
|
|
238
|
+
})(OpenAIResponsesAIMessage || (OpenAIResponsesAIMessage = {}));
|
|
216
239
|
//# sourceMappingURL=openai-responses.js.map
|