@zimtsui/brainswitch 0.0.41 → 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/build/adaptor.d.ts +4 -2
- package/build/adaptor.js +9 -3
- package/build/adaptor.js.map +1 -1
- 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 +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 +230 -296
- 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 +165 -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 +64 -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 +169 -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 +46 -68
- package/build/compatible-engines.d/openai-responses.js +218 -281
- 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/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 +56 -73
- package/build/native-engines.d/google/engine.js +262 -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 +59 -78
- package/build/native-engines.d/openai-responses/engine.js +281 -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 +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
|
@@ -5,104 +5,109 @@ import { Ajv } from 'ajv';
|
|
|
5
5
|
const ajv = new Ajv();
|
|
6
6
|
export var OpenAIChatCompletionsEngine;
|
|
7
7
|
(function (OpenAIChatCompletionsEngine) {
|
|
8
|
-
let
|
|
9
|
-
(function (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
catch (e) {
|
|
40
|
-
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
41
|
-
}
|
|
42
|
-
})();
|
|
43
|
-
if (ajv.validate(fditem.paraschema, args)) { }
|
|
44
|
-
else
|
|
45
|
-
throw new ResponseInvalid('Invalid function arguments', { cause: apifc });
|
|
46
|
-
return Function.Call.create({
|
|
47
|
-
id: apifc.id,
|
|
48
|
-
name: apifc.function.name,
|
|
49
|
-
args,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
convertFromFunctionResponse(fr) {
|
|
53
|
-
if (fr.id) { }
|
|
54
|
-
else
|
|
55
|
-
throw new Error();
|
|
56
|
-
return {
|
|
57
|
-
role: 'tool',
|
|
58
|
-
tool_call_id: fr.id,
|
|
59
|
-
content: fr.text,
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
convertFromFunctionDeclarationEntry(fdentry) {
|
|
63
|
-
return {
|
|
64
|
-
type: 'function',
|
|
65
|
-
function: {
|
|
66
|
-
name: fdentry[0],
|
|
67
|
-
description: fdentry[1].description,
|
|
68
|
-
strict: true,
|
|
69
|
-
parameters: fdentry[1].paraschema,
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
convertFromToolChoice(toolChoice) {
|
|
74
|
-
if (toolChoice === Function.ToolChoice.NONE)
|
|
75
|
-
return 'none';
|
|
76
|
-
else if (toolChoice === Function.ToolChoice.REQUIRED)
|
|
77
|
-
return 'required';
|
|
78
|
-
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
79
|
-
return 'auto';
|
|
80
|
-
else {
|
|
81
|
-
if (toolChoice.length === 1) { }
|
|
82
|
-
else
|
|
83
|
-
throw new Error();
|
|
84
|
-
return { type: 'function', function: { name: toolChoice[0] } };
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
calcCost(usage) {
|
|
88
|
-
const cacheHitTokenCount = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
89
|
-
const cacheMissTokenCount = usage.prompt_tokens - cacheHitTokenCount;
|
|
90
|
-
return this.instance.inputPrice * cacheMissTokenCount / 1e6 +
|
|
91
|
-
this.instance.cachedPrice * cacheHitTokenCount / 1e6 +
|
|
92
|
-
this.instance.outputPrice * usage.completion_tokens / 1e6;
|
|
93
|
-
}
|
|
94
|
-
extractContent(completionContent) {
|
|
95
|
-
return completionContent;
|
|
8
|
+
let OwnProps;
|
|
9
|
+
(function (OwnProps) {
|
|
10
|
+
function init(options) {
|
|
11
|
+
return {
|
|
12
|
+
parallelToolCall: options.parallelToolCall ?? false,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
OwnProps.init = init;
|
|
16
|
+
})(OwnProps = OpenAIChatCompletionsEngine.OwnProps || (OpenAIChatCompletionsEngine.OwnProps = {}));
|
|
17
|
+
function convertFromFunctionCall(fc) {
|
|
18
|
+
if (fc.id) { }
|
|
19
|
+
else
|
|
20
|
+
throw new Error();
|
|
21
|
+
return {
|
|
22
|
+
id: fc.id,
|
|
23
|
+
type: 'function',
|
|
24
|
+
function: {
|
|
25
|
+
name: fc.name,
|
|
26
|
+
arguments: JSON.stringify(fc.args),
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
OpenAIChatCompletionsEngine.convertFromFunctionCall = convertFromFunctionCall;
|
|
31
|
+
function convertToFunctionCall(apifc) {
|
|
32
|
+
const fditem = this.fdm[apifc.function.name];
|
|
33
|
+
if (fditem) { }
|
|
34
|
+
else
|
|
35
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
36
|
+
const args = (() => {
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(apifc.function.arguments);
|
|
96
39
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
throw new ResponseInvalid('Token limit exceeded.', { cause: completion });
|
|
100
|
-
if (['stop', 'tool_calls'].includes(finishReason)) { }
|
|
101
|
-
else
|
|
102
|
-
throw new ResponseInvalid('Abnormal finish reason', { cause: finishReason });
|
|
40
|
+
catch (e) {
|
|
41
|
+
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
103
42
|
}
|
|
43
|
+
})();
|
|
44
|
+
if (ajv.validate(fditem.paraschema, args)) { }
|
|
45
|
+
else
|
|
46
|
+
throw new ResponseInvalid('Invalid function arguments', { cause: apifc });
|
|
47
|
+
return Function.Call.create({
|
|
48
|
+
id: apifc.id,
|
|
49
|
+
name: apifc.function.name,
|
|
50
|
+
args,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
OpenAIChatCompletionsEngine.convertToFunctionCall = convertToFunctionCall;
|
|
54
|
+
function convertFromFunctionResponse(fr) {
|
|
55
|
+
if (fr.id) { }
|
|
56
|
+
else
|
|
57
|
+
throw new Error();
|
|
58
|
+
return {
|
|
59
|
+
role: 'tool',
|
|
60
|
+
tool_call_id: fr.id,
|
|
61
|
+
content: fr.text,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
OpenAIChatCompletionsEngine.convertFromFunctionResponse = convertFromFunctionResponse;
|
|
65
|
+
function convertFromFunctionDeclarationEntry(fdentry) {
|
|
66
|
+
return {
|
|
67
|
+
type: 'function',
|
|
68
|
+
function: {
|
|
69
|
+
name: fdentry[0],
|
|
70
|
+
description: fdentry[1].description,
|
|
71
|
+
strict: true,
|
|
72
|
+
parameters: fdentry[1].paraschema,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
OpenAIChatCompletionsEngine.convertFromFunctionDeclarationEntry = convertFromFunctionDeclarationEntry;
|
|
77
|
+
function convertFromToolChoice(toolChoice) {
|
|
78
|
+
if (toolChoice === Function.ToolChoice.NONE)
|
|
79
|
+
return 'none';
|
|
80
|
+
else if (toolChoice === Function.ToolChoice.REQUIRED)
|
|
81
|
+
return 'required';
|
|
82
|
+
else if (toolChoice === Function.ToolChoice.AUTO)
|
|
83
|
+
return 'auto';
|
|
84
|
+
else {
|
|
85
|
+
if (toolChoice.length === 1) { }
|
|
86
|
+
else
|
|
87
|
+
throw new Error();
|
|
88
|
+
return { type: 'function', function: { name: toolChoice[0] } };
|
|
104
89
|
}
|
|
105
|
-
|
|
106
|
-
|
|
90
|
+
}
|
|
91
|
+
OpenAIChatCompletionsEngine.convertFromToolChoice = convertFromToolChoice;
|
|
92
|
+
function calcCost(usage) {
|
|
93
|
+
const cacheHitTokenCount = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
94
|
+
const cacheMissTokenCount = usage.prompt_tokens - cacheHitTokenCount;
|
|
95
|
+
return this.inputPrice * cacheMissTokenCount / 1e6 +
|
|
96
|
+
this.cachePrice * cacheHitTokenCount / 1e6 +
|
|
97
|
+
this.outputPrice * usage.completion_tokens / 1e6;
|
|
98
|
+
}
|
|
99
|
+
OpenAIChatCompletionsEngine.calcCost = calcCost;
|
|
100
|
+
function extractContent(completionContent) {
|
|
101
|
+
return completionContent;
|
|
102
|
+
}
|
|
103
|
+
OpenAIChatCompletionsEngine.extractContent = extractContent;
|
|
104
|
+
function handleFinishReason(completion, finishReason) {
|
|
105
|
+
if (finishReason === 'length')
|
|
106
|
+
throw new ResponseInvalid('Token limit exceeded.', { cause: completion });
|
|
107
|
+
if (['stop', 'tool_calls'].includes(finishReason)) { }
|
|
108
|
+
else
|
|
109
|
+
throw new ResponseInvalid('Abnormal finish reason', { cause: finishReason });
|
|
110
|
+
}
|
|
111
|
+
OpenAIChatCompletionsEngine.handleFinishReason = handleFinishReason;
|
|
107
112
|
})(OpenAIChatCompletionsEngine || (OpenAIChatCompletionsEngine = {}));
|
|
108
113
|
//# sourceMappingURL=openai-chat-completions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-chat-completions.js","sourceRoot":"","sources":["../../src/api-types/openai-chat-completions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAe,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAG1B,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAGtB,MAAM,KAAW,2BAA2B,
|
|
1
|
+
{"version":3,"file":"openai-chat-completions.js","sourceRoot":"","sources":["../../src/api-types/openai-chat-completions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAe,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,GAAG,EAAE,MAAM,KAAK,CAAC;AAG1B,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAGtB,MAAM,KAAW,2BAA2B,CA2H3C;AA3HD,WAAiB,2BAA2B;IAMxC,IAAiB,QAAQ,CAMxB;IAND,WAAiB,QAAQ;QACrB,SAAgB,IAAI,CAAuC,OAAqB;YAC5E,OAAO;gBACH,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,KAAK;aACtD,CAAC;QACN,CAAC;QAJe,aAAI,OAInB,CAAA;IACL,CAAC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AU,CAAC,CAAC,CAAE,EAAE,EAAE,CAAC;QACpE,CAAC;IACL,CAAC;IAVe,iDAAqB,wBAUpC,CAAA;IAED,SAAgB,QAAQ,CAEpB,KAA6B;QAE7B,MAAM,kBAAkB,GAAG,KAAK,CAAC,qBAAqB,EAAE,aAAa,IAAI,CAAC,CAAC;QAC3E,MAAM,mBAAmB,GAAG,KAAK,CAAC,aAAa,GAAG,kBAAkB,CAAC;QACrE,OAAO,IAAI,CAAC,UAAU,GAAG,mBAAmB,GAAG,GAAG;YAC1C,IAAI,CAAC,UAAU,GAAG,kBAAkB,GAAG,GAAG;YAC1C,IAAI,CAAC,WAAW,GAAG,KAAK,CAAC,iBAAiB,GAAG,GAAG,CAAC;IAC7D,CAAC;IATe,oCAAQ,WASvB,CAAA;IAED,SAAgB,cAAc,CAAC,iBAAyB;QACpD,OAAO,iBAAiB,CAAC;IAC7B,CAAC;IAFe,0CAAc,iBAE7B,CAAA;IAED,SAAgB,kBAAkB,CAAC,UAAiC,EAAE,YAA2D;QAC7H,IAAI,YAAY,KAAK,QAAQ;YACzB,MAAM,IAAI,eAAe,CAAC,uBAAuB,EAAE,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC;QAC9E,IAAI,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC,CAAA,CAAC;;YAChD,MAAM,IAAI,eAAe,CAAC,wBAAwB,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC,CAAC;IACtF,CAAC;IALe,8CAAkB,qBAKjC,CAAA;AACL,CAAC,EA3HgB,2BAA2B,KAA3B,2BAA2B,QA2H3C"}
|
|
@@ -2,22 +2,22 @@ import OpenAI from 'openai';
|
|
|
2
2
|
import { Function } from '../function.ts';
|
|
3
3
|
import { type Engine } from '../engine.ts';
|
|
4
4
|
export declare namespace OpenAIResponsesEngine {
|
|
5
|
-
interface
|
|
5
|
+
interface Options<in out fdm extends Function.Declaration.Map> extends Engine.Options<fdm> {
|
|
6
|
+
}
|
|
7
|
+
interface OwnProps {
|
|
8
|
+
parallelToolCall: boolean;
|
|
9
|
+
}
|
|
10
|
+
namespace OwnProps {
|
|
11
|
+
function init<fdm extends Function.Declaration.Map>(options: Options<fdm>): OwnProps;
|
|
12
|
+
}
|
|
13
|
+
interface Underhood<in out fdm extends Function.Declaration.Map> extends Engine.Underhood<fdm>, OwnProps {
|
|
6
14
|
convertFromFunctionResponse(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInputItem.FunctionCallOutput;
|
|
7
15
|
calcCost(usage: OpenAI.Responses.ResponseUsage): number;
|
|
8
16
|
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): OpenAI.Responses.FunctionTool;
|
|
9
17
|
convertToFunctionCall(apifc: OpenAI.Responses.ResponseFunctionToolCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
10
18
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
protected instance: OpenAIResponsesEngine.Instance<fdm>;
|
|
16
|
-
constructor(instance: OpenAIResponsesEngine.Instance<fdm>);
|
|
17
|
-
convertFromFunctionResponse(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInputItem.FunctionCallOutput;
|
|
18
|
-
calcCost(usage: OpenAI.Responses.ResponseUsage): number;
|
|
19
|
-
convertFromFunctionDeclarationEntry(fdentry: Function.Declaration.Entry.From<fdm>): OpenAI.Responses.FunctionTool;
|
|
20
|
-
convertToFunctionCall(apifc: OpenAI.Responses.ResponseFunctionToolCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
19
|
+
function convertFromFunctionResponse<fdm extends Function.Declaration.Map>(fr: Function.Response.Distributive<Function.Declaration.From<fdm>>): OpenAI.Responses.ResponseInputItem.FunctionCallOutput;
|
|
20
|
+
function calcCost<fdm extends Function.Declaration.Map>(this: Engine.Underhood<fdm>, usage: OpenAI.Responses.ResponseUsage): number;
|
|
21
|
+
function convertFromFunctionDeclarationEntry<fdm extends Function.Declaration.Map>(fdentry: Function.Declaration.Entry.From<fdm>): OpenAI.Responses.FunctionTool;
|
|
22
|
+
function convertToFunctionCall<fdm extends Function.Declaration.Map>(this: Engine.Underhood<fdm>, apifc: OpenAI.Responses.ResponseFunctionToolCall): Function.Call.Distributive<Function.Declaration.From<fdm>>;
|
|
23
23
|
}
|
|
@@ -6,63 +6,66 @@ import {} from "../engine.js";
|
|
|
6
6
|
const ajv = new Ajv();
|
|
7
7
|
export var OpenAIResponsesEngine;
|
|
8
8
|
(function (OpenAIResponsesEngine) {
|
|
9
|
-
let
|
|
10
|
-
(function (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
9
|
+
let OwnProps;
|
|
10
|
+
(function (OwnProps) {
|
|
11
|
+
function init(options) {
|
|
12
|
+
return {
|
|
13
|
+
parallelToolCall: options.parallelToolCall ?? false,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
OwnProps.init = init;
|
|
17
|
+
})(OwnProps = OpenAIResponsesEngine.OwnProps || (OpenAIResponsesEngine.OwnProps = {}));
|
|
18
|
+
function convertFromFunctionResponse(fr) {
|
|
19
|
+
if (fr.id) { }
|
|
20
|
+
else
|
|
21
|
+
throw new Error();
|
|
22
|
+
return {
|
|
23
|
+
type: 'function_call_output',
|
|
24
|
+
call_id: fr.id,
|
|
25
|
+
output: fr.text,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
OpenAIResponsesEngine.convertFromFunctionResponse = convertFromFunctionResponse;
|
|
29
|
+
function calcCost(usage) {
|
|
30
|
+
const cacheHitTokenCount = usage.input_tokens_details.cached_tokens;
|
|
31
|
+
const cacheMissTokenCount = usage.input_tokens - cacheHitTokenCount;
|
|
32
|
+
return this.inputPrice * cacheMissTokenCount / 1e6 +
|
|
33
|
+
this.cachePrice * cacheHitTokenCount / 1e6 +
|
|
34
|
+
this.outputPrice * usage.output_tokens / 1e6;
|
|
35
|
+
}
|
|
36
|
+
OpenAIResponsesEngine.calcCost = calcCost;
|
|
37
|
+
function convertFromFunctionDeclarationEntry(fdentry) {
|
|
38
|
+
return {
|
|
39
|
+
name: fdentry[0],
|
|
40
|
+
description: fdentry[1].description,
|
|
41
|
+
parameters: fdentry[1].paraschema,
|
|
42
|
+
strict: true,
|
|
43
|
+
type: 'function',
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
OpenAIResponsesEngine.convertFromFunctionDeclarationEntry = convertFromFunctionDeclarationEntry;
|
|
47
|
+
function convertToFunctionCall(apifc) {
|
|
48
|
+
const fditem = this.fdm[apifc.name];
|
|
49
|
+
if (fditem) { }
|
|
50
|
+
else
|
|
51
|
+
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
52
|
+
const args = (() => {
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(apifc.arguments);
|
|
41
55
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (fditem) { }
|
|
45
|
-
else
|
|
46
|
-
throw new ResponseInvalid('Unknown function call', { cause: apifc });
|
|
47
|
-
const args = (() => {
|
|
48
|
-
try {
|
|
49
|
-
return JSON.parse(apifc.arguments);
|
|
50
|
-
}
|
|
51
|
-
catch (e) {
|
|
52
|
-
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
53
|
-
}
|
|
54
|
-
})();
|
|
55
|
-
if (ajv.validate(fditem.paraschema, args)) { }
|
|
56
|
-
else
|
|
57
|
-
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
58
|
-
return Function.Call.create({
|
|
59
|
-
id: apifc.call_id,
|
|
60
|
-
name: apifc.name,
|
|
61
|
-
args,
|
|
62
|
-
});
|
|
56
|
+
catch (e) {
|
|
57
|
+
return new ResponseInvalid('Invalid JSON of function call', { cause: apifc });
|
|
63
58
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
})();
|
|
60
|
+
if (ajv.validate(fditem.paraschema, args)) { }
|
|
61
|
+
else
|
|
62
|
+
throw new ResponseInvalid('Function call not conforming to schema', { cause: apifc });
|
|
63
|
+
return Function.Call.create({
|
|
64
|
+
id: apifc.call_id,
|
|
65
|
+
name: apifc.name,
|
|
66
|
+
args,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
OpenAIResponsesEngine.convertToFunctionCall = convertToFunctionCall;
|
|
67
70
|
})(OpenAIResponsesEngine || (OpenAIResponsesEngine = {}));
|
|
68
71
|
//# sourceMappingURL=openai-responses.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai-responses.js","sourceRoot":"","sources":["../../src/api-types/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,GAAG,MAAM,KAAK,CAAC;AACtB,OAAO,EAAe,MAAM,cAAc,CAAC;AAE3C,MAAM,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;AAItB,MAAM,KAAW,qBAAqB,
|
|
1
|
+
{"version":3,"file":"openai-responses.js","sourceRoot":"","sources":["../../src/api-types/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,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
|
@@ -4,16 +4,16 @@ import { type InferenceContext } from './inference-context.ts';
|
|
|
4
4
|
import { type Engine } from './engine.ts';
|
|
5
5
|
export interface CompatibleEngine<in out fdm extends Function.Declaration.Map> extends Engine {
|
|
6
6
|
/**
|
|
7
|
-
* @throws {@link
|
|
7
|
+
* @throws {@link USER_ABORTION} 用户中止
|
|
8
8
|
* @throws {@link InferenceTimeout} 推理超时
|
|
9
9
|
* @throws {@link ResponseInvalid} 模型抽风
|
|
10
10
|
* @throws {TypeError} 网络故障
|
|
11
11
|
*/
|
|
12
|
-
stateless(
|
|
12
|
+
stateless(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
13
13
|
/**
|
|
14
14
|
* @param session mutable
|
|
15
15
|
*/
|
|
16
|
-
stateful(
|
|
16
|
+
stateful(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
17
17
|
appendUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
18
18
|
/**
|
|
19
19
|
* @param session mutable
|
|
@@ -21,36 +21,31 @@ export interface CompatibleEngine<in out fdm extends Function.Declaration.Map> e
|
|
|
21
21
|
pushUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
22
22
|
}
|
|
23
23
|
export declare namespace CompatibleEngine {
|
|
24
|
-
interface Options<in out fdm extends Function.Declaration.Map> extends Engine.Options<fdm>,
|
|
24
|
+
interface Options<in out fdm extends Function.Declaration.Map> extends Engine.Options<fdm>, Options.Tools<fdm> {
|
|
25
25
|
}
|
|
26
26
|
namespace Options {
|
|
27
27
|
interface Tools<in out fdm extends Function.Declaration.Map> extends Engine.Options.Tools<fdm> {
|
|
28
28
|
toolChoice?: Function.ToolChoice<fdm>;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
interface
|
|
31
|
+
interface OwnProps<in out fdm extends Function.Declaration.Map> {
|
|
32
32
|
toolChoice: Function.ToolChoice<fdm>;
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
}
|
|
34
|
+
namespace OwnProps {
|
|
35
|
+
function init<fdm extends Function.Declaration.Map>(options: CompatibleEngine.Options<fdm>): OwnProps<fdm>;
|
|
36
|
+
}
|
|
37
|
+
interface Underhood<in out fdm extends Function.Declaration.Map> extends Engine.Underhood<fdm>, CompatibleEngine<fdm>, OwnProps<fdm> {
|
|
38
|
+
parallelToolCall: boolean;
|
|
39
|
+
fetch(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>, signal?: AbortSignal): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
40
|
+
stateless(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
41
|
+
stateful(wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
35
42
|
appendUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
36
43
|
pushUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
37
44
|
validateToolCallsByToolChoice(toolCalls: Function.Call.Distributive<Function.Declaration.From<fdm>>[]): void;
|
|
38
45
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
class Instance<in out fdm extends Function.Declaration.Map> implements CompatibleEngine.Base<fdm> {
|
|
45
|
-
protected instance: CompatibleEngine.Instance<fdm>;
|
|
46
|
-
toolChoice: Function.ToolChoice<fdm>;
|
|
47
|
-
constructor(instance: CompatibleEngine.Instance<fdm>, options: CompatibleEngine.Options<fdm>);
|
|
48
|
-
stateless(ctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
49
|
-
stateful(ctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
50
|
-
appendUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
51
|
-
pushUserMessage(session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
52
|
-
validateToolCallsByToolChoice(toolCalls: Function.Call.Distributive<Function.Declaration.From<fdm>>[]): void;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function validateToolCallsByToolChoice<fdm extends Function.Declaration.Map>(toolCalls: Function.Call.Distributive<Function.Declaration.From<fdm>>[], toolChoice: Function.ToolChoice<fdm>): void;
|
|
46
|
+
function stateless<fdm extends Function.Declaration.Map>(this: CompatibleEngine.Underhood<fdm>, wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
47
|
+
function stateful<fdm extends Function.Declaration.Map>(this: CompatibleEngine.Underhood<fdm>, wfctx: InferenceContext, session: Session<Function.Declaration.From<fdm>>): Promise<RoleMessage.Ai<Function.Declaration.From<fdm>>>;
|
|
48
|
+
function appendUserMessage<fdm extends Function.Declaration.Map>(this: CompatibleEngine.Underhood<fdm>, session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
49
|
+
function pushUserMessage<fdm extends Function.Declaration.Map>(this: CompatibleEngine.Underhood<fdm>, session: Session<Function.Declaration.From<fdm>>, message: RoleMessage.User<Function.Declaration.From<fdm>>): Session<Function.Declaration.From<fdm>>;
|
|
50
|
+
function validateToolCallsByToolChoice<fdm extends Function.Declaration.Map>(this: CompatibleEngine.Underhood<fdm>, toolCalls: Function.Call.Distributive<Function.Declaration.From<fdm>>[]): void;
|
|
56
51
|
}
|
|
@@ -1,67 +1,65 @@
|
|
|
1
1
|
import { RoleMessage } from "./session.js";
|
|
2
2
|
import { Function } from "./function.js";
|
|
3
3
|
import {} from "./inference-context.js";
|
|
4
|
-
import {
|
|
4
|
+
import { USER_ABORTION, InferenceTimeout, ResponseInvalid } from "./engine.js";
|
|
5
5
|
export var CompatibleEngine;
|
|
6
6
|
(function (CompatibleEngine) {
|
|
7
|
-
let
|
|
8
|
-
(function (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
if (ctx.signal?.aborted)
|
|
28
|
-
throw new UserAbortion(); // 用户中止
|
|
29
|
-
else if (signalTimeout?.aborted)
|
|
30
|
-
e = new InferenceTimeout(undefined, { cause: e }); // 推理超时
|
|
31
|
-
else if (e instanceof ResponseInvalid) { } // 模型抽风
|
|
32
|
-
else if (e instanceof TypeError) { } // 网络故障
|
|
33
|
-
else
|
|
34
|
-
throw e;
|
|
35
|
-
if (retry < 3)
|
|
36
|
-
ctx.logger.message?.warn(e);
|
|
37
|
-
else
|
|
38
|
-
throw e;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
async stateful(ctx, session) {
|
|
43
|
-
const response = await this.stateless(ctx, session);
|
|
44
|
-
session.chatMessages.push(response);
|
|
45
|
-
return response;
|
|
46
|
-
}
|
|
47
|
-
appendUserMessage(session, message) {
|
|
48
|
-
return {
|
|
49
|
-
...session,
|
|
50
|
-
chatMessages: [...session.chatMessages, message],
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
pushUserMessage(session, message) {
|
|
54
|
-
session.chatMessages.push(message);
|
|
55
|
-
return session;
|
|
7
|
+
let OwnProps;
|
|
8
|
+
(function (OwnProps) {
|
|
9
|
+
function init(options) {
|
|
10
|
+
return {
|
|
11
|
+
toolChoice: options.toolChoice ?? Function.ToolChoice.AUTO,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
OwnProps.init = init;
|
|
15
|
+
})(OwnProps = CompatibleEngine.OwnProps || (CompatibleEngine.OwnProps = {}));
|
|
16
|
+
async function stateless(wfctx, session) {
|
|
17
|
+
for (let retry = 0;; retry++) {
|
|
18
|
+
const signalTimeout = this.timeout ? AbortSignal.timeout(this.timeout) : undefined;
|
|
19
|
+
const signal = wfctx.signal && signalTimeout ? AbortSignal.any([
|
|
20
|
+
wfctx.signal,
|
|
21
|
+
signalTimeout,
|
|
22
|
+
]) : wfctx.signal || signalTimeout;
|
|
23
|
+
try {
|
|
24
|
+
return await this.fetch(wfctx, session, signal);
|
|
56
25
|
}
|
|
57
|
-
|
|
58
|
-
|
|
26
|
+
catch (e) {
|
|
27
|
+
if (wfctx.signal?.aborted)
|
|
28
|
+
throw USER_ABORTION; // 用户中止
|
|
29
|
+
else if (signalTimeout?.aborted)
|
|
30
|
+
e = new InferenceTimeout(undefined, { cause: e }); // 推理超时
|
|
31
|
+
else if (e instanceof ResponseInvalid) { } // 模型抽风
|
|
32
|
+
else if (e instanceof TypeError) { } // 网络故障
|
|
33
|
+
else
|
|
34
|
+
throw e;
|
|
35
|
+
if (retry < 3)
|
|
36
|
+
this.logger.message?.warn(e);
|
|
37
|
+
else
|
|
38
|
+
throw e;
|
|
59
39
|
}
|
|
60
40
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
function
|
|
64
|
-
|
|
41
|
+
}
|
|
42
|
+
CompatibleEngine.stateless = stateless;
|
|
43
|
+
async function stateful(wfctx, session) {
|
|
44
|
+
const response = await this.stateless(wfctx, session);
|
|
45
|
+
session.chatMessages.push(response);
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
CompatibleEngine.stateful = stateful;
|
|
49
|
+
function appendUserMessage(session, message) {
|
|
50
|
+
return {
|
|
51
|
+
...session,
|
|
52
|
+
chatMessages: [...session.chatMessages, message],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
CompatibleEngine.appendUserMessage = appendUserMessage;
|
|
56
|
+
function pushUserMessage(session, message) {
|
|
57
|
+
session.chatMessages.push(message);
|
|
58
|
+
return session;
|
|
59
|
+
}
|
|
60
|
+
CompatibleEngine.pushUserMessage = pushUserMessage;
|
|
61
|
+
function validateToolCallsByToolChoice(toolCalls) {
|
|
62
|
+
Function.Call.validate(toolCalls, this.toolChoice, new ResponseInvalid('Invalid function call', { cause: toolCalls }));
|
|
65
63
|
}
|
|
66
64
|
CompatibleEngine.validateToolCallsByToolChoice = validateToolCallsByToolChoice;
|
|
67
65
|
})(CompatibleEngine || (CompatibleEngine = {}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compatible-engine.js","sourceRoot":"","sources":["../src/compatible-engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgB,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAyB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"compatible-engine.js","sourceRoot":"","sources":["../src/compatible-engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgB,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAyB,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,eAAe,EAAe,MAAM,aAAa,CAAC;AAuB5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