@zimtsui/typechat 0.0.93 → 0.0.95
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/DEVELOPMENT.md +0 -6
- package/README.md +26 -36
- package/assets/verbatim/instruction.md +27 -25
- package/assets/verbatim/quotation.handlebars +1 -1
- package/assets/verbatim/system.handlebars +1 -0
- package/build/adaptor.d.ts +5 -23
- package/build/adaptor.js +14 -25
- package/build/adaptor.js.map +1 -1
- package/build/api-types/anthropic/billing.d.ts +3 -3
- package/build/api-types/anthropic/billing.js +6 -6
- package/build/api-types/anthropic/billing.js.map +1 -1
- package/build/api-types/anthropic/tool-codec.d.ts +3 -3
- package/build/api-types/anthropic/tool-codec.js +15 -11
- package/build/api-types/anthropic/tool-codec.js.map +1 -1
- package/build/api-types/google/billing.d.ts +3 -3
- package/build/api-types/google/billing.js +7 -7
- package/build/api-types/google/billing.js.map +1 -1
- package/build/api-types/google/tool-codec.d.ts +3 -3
- package/build/api-types/google/tool-codec.js +16 -34
- package/build/api-types/google/tool-codec.js.map +1 -1
- package/build/api-types/openai-chatcompletions/billing.d.ts +5 -5
- package/build/api-types/openai-chatcompletions/billing.js +7 -7
- package/build/api-types/openai-chatcompletions/billing.js.map +1 -1
- package/build/api-types/openai-chatcompletions/tool-codec.d.ts +5 -5
- package/build/api-types/openai-chatcompletions/tool-codec.js +17 -13
- package/build/api-types/openai-chatcompletions/tool-codec.js.map +1 -1
- package/build/api-types/openai-responses/billing.d.ts +3 -3
- package/build/api-types/openai-responses/billing.js +6 -6
- package/build/api-types/openai-responses/billing.js.map +1 -1
- package/build/api-types/openai-responses/tool-codec.d.ts +3 -3
- package/build/api-types/openai-responses/tool-codec.js +16 -12
- package/build/api-types/openai-responses/tool-codec.js.map +1 -1
- package/build/assets.d.ts +1 -1
- package/build/assets.js +6 -5
- package/build/assets.js.map +1 -1
- package/build/compatible-engine/session.d.ts +4 -16
- package/build/compatible-engine/session.js +8 -15
- package/build/compatible-engine/session.js.map +1 -1
- package/build/compatible-engine/validation.d.ts +7 -7
- package/build/compatible-engine/validation.js +33 -34
- package/build/compatible-engine/validation.js.map +1 -1
- package/build/compatible-engine.d/anthropic/message-codec.d.ts +4 -3
- package/build/compatible-engine.d/anthropic/message-codec.js +16 -22
- package/build/compatible-engine.d/anthropic/message-codec.js.map +1 -1
- package/build/compatible-engine.d/anthropic/transport.d.ts +10 -3
- package/build/compatible-engine.d/anthropic/transport.js +105 -101
- package/build/compatible-engine.d/anthropic/transport.js.map +1 -1
- package/build/compatible-engine.d/anthropic.d.ts +2 -0
- package/build/compatible-engine.d/anthropic.js +9 -1
- package/build/compatible-engine.d/anthropic.js.map +1 -1
- package/build/compatible-engine.d/google/message-codec.d.ts +4 -3
- package/build/compatible-engine.d/google/message-codec.js +16 -22
- package/build/compatible-engine.d/google/message-codec.js.map +1 -1
- package/build/compatible-engine.d/google/transport.d.ts +10 -3
- package/build/compatible-engine.d/google/transport.js +32 -24
- package/build/compatible-engine.d/google/transport.js.map +1 -1
- package/build/compatible-engine.d/google.d.ts +2 -0
- package/build/compatible-engine.d/google.js +23 -2
- package/build/compatible-engine.d/google.js.map +1 -1
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.d.ts +6 -5
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js +17 -24
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js.map +1 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.d.ts +33 -3
- package/build/compatible-engine.d/openai-chatcompletions/transport.js +182 -6
- package/build/compatible-engine.d/openai-chatcompletions/transport.js.map +1 -1
- package/build/compatible-engine.d/openai-chatcompletions.d.ts +10 -2
- package/build/compatible-engine.d/openai-chatcompletions.js +35 -0
- package/build/compatible-engine.d/openai-chatcompletions.js.map +1 -1
- package/build/compatible-engine.d/openai-responses/message-codec.d.ts +4 -6
- package/build/compatible-engine.d/openai-responses/message-codec.js +70 -74
- package/build/compatible-engine.d/openai-responses/message-codec.js.map +1 -1
- package/build/compatible-engine.d/openai-responses/transport.d.ts +14 -8
- package/build/compatible-engine.d/openai-responses/transport.js +64 -64
- package/build/compatible-engine.d/openai-responses/transport.js.map +1 -1
- package/build/compatible-engine.d/openai-responses.d.ts +2 -0
- package/build/compatible-engine.d/openai-responses.js +10 -2
- package/build/compatible-engine.d/openai-responses.js.map +1 -1
- package/build/compatible-engine.d.ts +2 -1
- package/build/compatible-engine.js +3 -3
- package/build/compatible-engine.js.map +1 -1
- package/build/config.d.ts +17 -19
- package/build/config.js +2 -4
- package/build/config.js.map +1 -1
- package/build/endpoint-spec.d.ts +15 -15
- package/build/endpoint-spec.js +2 -2
- package/build/endpoint-spec.js.map +1 -1
- package/build/engine/session.d.ts +31 -0
- package/build/engine/session.js +19 -0
- package/build/engine/session.js.map +1 -1
- package/build/engine/validation.d.ts +1 -7
- package/build/engine.d.ts +37 -20
- package/build/engine.js +154 -87
- package/build/engine.js.map +1 -1
- package/build/function.d.ts +1 -1
- package/build/native-engines.d/google/message-codec.d.ts +6 -3
- package/build/native-engines.d/google/message-codec.js +23 -27
- package/build/native-engines.d/google/message-codec.js.map +1 -1
- package/build/native-engines.d/google/transport.d.ts +13 -3
- package/build/native-engines.d/google/transport.js +41 -27
- package/build/native-engines.d/google/transport.js.map +1 -1
- package/build/native-engines.d/google/validation.d.ts +5 -6
- package/build/native-engines.d/google/validation.js +3 -6
- package/build/native-engines.d/google/validation.js.map +1 -1
- package/build/native-engines.d/google.d.ts +2 -2
- package/build/native-engines.d/google.js +22 -4
- package/build/native-engines.d/google.js.map +1 -1
- package/build/native-engines.d/openai-responses/choice-codec.d.ts +1 -1
- package/build/native-engines.d/openai-responses/choice-codec.js +12 -12
- package/build/native-engines.d/openai-responses/choice-codec.js.map +1 -1
- package/build/native-engines.d/openai-responses/message-codec.d.ts +5 -3
- package/build/native-engines.d/openai-responses/message-codec.js +59 -48
- package/build/native-engines.d/openai-responses/message-codec.js.map +1 -1
- package/build/native-engines.d/openai-responses/transport.d.ts +14 -6
- package/build/native-engines.d/openai-responses/transport.js +56 -54
- package/build/native-engines.d/openai-responses/transport.js.map +1 -1
- package/build/native-engines.d/openai-responses/validation.d.ts +7 -7
- package/build/native-engines.d/openai-responses/validation.js +31 -32
- package/build/native-engines.d/openai-responses/validation.js.map +1 -1
- package/build/native-engines.d/openai-responses.d.ts +2 -2
- package/build/native-engines.d/openai-responses.js +9 -4
- package/build/native-engines.d/openai-responses.js.map +1 -1
- package/build/repetition.d.ts +2 -1
- package/build/repetition.js +7 -42
- package/build/repetition.js.map +1 -1
- package/build/telemetry.d.ts +1 -0
- package/build/telemetry.js +1 -0
- package/build/telemetry.js.map +1 -1
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/verbatim/codec/declarations.d.ts +0 -1
- package/build/verbatim/codec/declarations.js +1 -2
- package/build/verbatim/codec/declarations.js.map +1 -1
- package/build/verbatim/codec/helpers.d.ts +2 -0
- package/build/verbatim/codec/{escape.js → helpers.js} +5 -1
- package/build/verbatim/codec/helpers.js.map +1 -0
- package/build/verbatim/codec/quotation.d.ts +1 -2
- package/build/verbatim/codec/quotation.js +3 -4
- package/build/verbatim/codec/quotation.js.map +1 -1
- package/build/verbatim/codec/response.d.ts +0 -1
- package/build/verbatim/codec/response.js +1 -2
- package/build/verbatim/codec/response.js.map +1 -1
- package/build/verbatim/codec/{meta.d.ts → system.d.ts} +0 -1
- package/build/verbatim/codec/system.js +7 -0
- package/build/verbatim/codec/system.js.map +1 -0
- package/build/verbatim/codec.d.ts +1 -1
- package/build/verbatim/codec.js +1 -1
- package/build/verbatim/codec.js.map +1 -1
- package/package.json +8 -9
- package/assets/verbatim/meta.handlebars +0 -1
- package/build/compatible-engine.d/aliyun/transport.d.ts +0 -12
- package/build/compatible-engine.d/aliyun/transport.js +0 -7
- package/build/compatible-engine.d/aliyun/transport.js.map +0 -1
- package/build/compatible-engine.d/aliyun.d.ts +0 -21
- package/build/compatible-engine.d/aliyun.js +0 -38
- package/build/compatible-engine.d/aliyun.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.d.ts +0 -31
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js +0 -88
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/monolith.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.d.ts +0 -34
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js +0 -190
- package/build/compatible-engine.d/openai-chatcompletions/transport.d/stream.js.map +0 -1
- package/build/verbatim/codec/escape.d.ts +0 -1
- package/build/verbatim/codec/escape.js.map +0 -1
- package/build/verbatim/codec/meta.js +0 -8
- package/build/verbatim/codec/meta.js.map +0 -1
package/build/engine.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Agent, ProxyAgent } from 'undici';
|
|
2
2
|
import { env } from 'node:process';
|
|
3
3
|
import { loggers } from "./telemetry.js";
|
|
4
|
+
import * as SessionModule from "./engine/session.js";
|
|
4
5
|
export var Engine;
|
|
5
6
|
(function (Engine) {
|
|
6
7
|
class Instance {
|
|
@@ -12,138 +13,204 @@ export var Engine;
|
|
|
12
13
|
vdm;
|
|
13
14
|
throttle;
|
|
14
15
|
constructor(options) {
|
|
15
|
-
const proxyUrl = options.proxy || env.https_proxy || env.HTTPS_PROXY;
|
|
16
|
+
const proxyUrl = options.endpointSpec.proxy || env.https_proxy || env.HTTPS_PROXY;
|
|
16
17
|
const dispatcher = proxyUrl
|
|
17
18
|
? new ProxyAgent({
|
|
18
19
|
uri: proxyUrl,
|
|
19
|
-
headersTimeout:
|
|
20
|
+
headersTimeout: 10000,
|
|
20
21
|
bodyTimeout: 0,
|
|
21
22
|
})
|
|
22
23
|
: new Agent({
|
|
23
|
-
headersTimeout:
|
|
24
|
+
headersTimeout: 10000,
|
|
24
25
|
bodyTimeout: 0,
|
|
25
26
|
});
|
|
26
27
|
this.providerSpec = {
|
|
27
|
-
baseUrl: options.baseUrl,
|
|
28
|
-
apiKey: options.apiKey,
|
|
28
|
+
baseUrl: options.endpointSpec.baseUrl,
|
|
29
|
+
apiKey: options.endpointSpec.apiKey,
|
|
29
30
|
dispatcher,
|
|
31
|
+
retry: options.providerRetry ?? 2,
|
|
30
32
|
};
|
|
31
|
-
this.name = options.name;
|
|
33
|
+
this.name = options.endpointSpec.name;
|
|
32
34
|
this.inferenceParams = {
|
|
33
|
-
model: options.model,
|
|
34
|
-
additionalOptions: options.additionalOptions,
|
|
35
|
-
timeout: options.timeout,
|
|
36
|
-
parallelToolCall: options.parallelToolCall,
|
|
37
|
-
retry: options.
|
|
35
|
+
model: options.endpointSpec.model,
|
|
36
|
+
additionalOptions: options.endpointSpec.additionalOptions,
|
|
37
|
+
timeout: options.endpointSpec.timeout,
|
|
38
|
+
parallelToolCall: options.endpointSpec.parallelToolCall,
|
|
39
|
+
retry: options.inferenceRetry ?? 2,
|
|
38
40
|
};
|
|
39
41
|
this.pricing = {
|
|
40
|
-
inputPrice: options.inputPrice ?? 0,
|
|
41
|
-
outputPrice: options.outputPrice ?? 0,
|
|
42
|
-
cachePrice: options.cachePrice ?? options.inputPrice ?? 0,
|
|
42
|
+
inputPrice: options.endpointSpec.inputPrice ?? 0,
|
|
43
|
+
outputPrice: options.endpointSpec.outputPrice ?? 0,
|
|
44
|
+
cachePrice: options.endpointSpec.cachePrice ?? options.endpointSpec.inputPrice ?? 0,
|
|
43
45
|
};
|
|
44
46
|
this.fdm = options.functionDeclarationMap;
|
|
45
47
|
this.vdm = options.verbatimDeclarationMap;
|
|
46
48
|
this.throttle = options.throttle;
|
|
47
49
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
/**
|
|
51
|
+
* @throws {@link SyntaxError} 模型抽风
|
|
52
|
+
* @throws {@link Recoverable} 模型抽风但可恢复
|
|
53
|
+
* @throws {@link TypeError} 网络故障
|
|
54
|
+
*/
|
|
55
|
+
async infer(wfctx, session) {
|
|
56
|
+
const signalTimeout = this.inferenceParams.timeout ? AbortSignal.timeout(this.inferenceParams.timeout) : null;
|
|
57
|
+
const signals = [];
|
|
58
|
+
if (signalTimeout)
|
|
59
|
+
signals.push(signalTimeout);
|
|
60
|
+
if (wfctx.signal)
|
|
61
|
+
signals.push(wfctx.signal);
|
|
62
|
+
const signal = AbortSignal.any(signals);
|
|
63
|
+
try {
|
|
64
|
+
const aiMessage = await this.transport.fetch(wfctx, session, signal);
|
|
65
|
+
this.partsValidator.validate(aiMessage);
|
|
66
|
+
const rejection = this.structuringValidator.validate(aiMessage);
|
|
67
|
+
if (rejection)
|
|
68
|
+
throw new Recoverable(aiMessage, rejection);
|
|
69
|
+
return aiMessage;
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
if (signalTimeout?.aborted)
|
|
73
|
+
throw new InferenceTimeout(undefined, { cause: e });
|
|
74
|
+
else
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
52
77
|
}
|
|
53
78
|
/**
|
|
54
79
|
* @throws {@link InferenceTimeout} 推理超时
|
|
55
|
-
* @throws {@link
|
|
56
|
-
* @throws {@link
|
|
57
|
-
* @throws {@link CustomRetry} 自定义重试
|
|
80
|
+
* @throws {@link SyntaxError} 模型抽风
|
|
81
|
+
* @throws {@link TypeError} 网络故障
|
|
58
82
|
*/
|
|
59
|
-
async stateless(wfctx, session
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const signals = [];
|
|
63
|
-
if (signalTimeout)
|
|
64
|
-
signals.push(signalTimeout);
|
|
65
|
-
if (wfctx.signal)
|
|
66
|
-
signals.push(wfctx.signal);
|
|
67
|
-
const signal = AbortSignal.any(signals);
|
|
83
|
+
async stateless(wfctx, session) {
|
|
84
|
+
const middleware = this.compose(this.middlewares);
|
|
85
|
+
for (let retryProvider = 0, retryInference = 0;;)
|
|
68
86
|
try {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
if (rejection)
|
|
72
|
-
throw new ResponseInvalid.Recoverable();
|
|
73
|
-
if (await validate(response)) { }
|
|
74
|
-
else
|
|
75
|
-
throw new CustomRetry(undefined, { cause: response });
|
|
76
|
-
return response;
|
|
87
|
+
const next = () => this.infer(wfctx, session);
|
|
88
|
+
return await middleware(wfctx, session, next);
|
|
77
89
|
}
|
|
78
90
|
catch (e) {
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
else if (e instanceof CustomRetry) { } // 自定义重试
|
|
84
|
-
else
|
|
85
|
-
throw e;
|
|
86
|
-
if (retry < this.inferenceParams.retry)
|
|
91
|
+
if (e instanceof InferenceTimeout) { // 推理超时
|
|
92
|
+
if (retryInference < this.inferenceParams.retry) { }
|
|
93
|
+
else
|
|
94
|
+
throw e;
|
|
87
95
|
loggers.message.warn(e);
|
|
96
|
+
retryInference++;
|
|
97
|
+
}
|
|
98
|
+
else if (e instanceof SyntaxError) { // 模型抽风
|
|
99
|
+
if (retryInference < this.inferenceParams.retry) { }
|
|
100
|
+
else
|
|
101
|
+
throw e;
|
|
102
|
+
loggers.message.warn(e);
|
|
103
|
+
retryInference++;
|
|
104
|
+
}
|
|
105
|
+
else if (e instanceof TypeError) { // 网络故障
|
|
106
|
+
if (retryProvider < this.providerSpec.retry) { }
|
|
107
|
+
else
|
|
108
|
+
throw e;
|
|
109
|
+
loggers.message.warn(e);
|
|
110
|
+
retryProvider++;
|
|
111
|
+
}
|
|
88
112
|
else
|
|
89
113
|
throw e;
|
|
90
114
|
}
|
|
91
|
-
}
|
|
92
115
|
}
|
|
93
116
|
/**
|
|
117
|
+
* @throws {@link InferenceTimeout} 推理超时
|
|
118
|
+
* @throws {@link SyntaxError} 模型抽风
|
|
119
|
+
* @throws {@link TypeError} 网络故障
|
|
94
120
|
* @param session mutable
|
|
95
121
|
*/
|
|
96
|
-
async stateful(wfctx, session
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (signalTimeout)
|
|
101
|
-
signals.push(signalTimeout);
|
|
102
|
-
if (wfctx.signal)
|
|
103
|
-
signals.push(wfctx.signal);
|
|
104
|
-
const signal = AbortSignal.any(signals);
|
|
122
|
+
async stateful(wfctx, session) {
|
|
123
|
+
const middleware = this.compose(this.middlewares);
|
|
124
|
+
const statefulMiddleware = this.compose(this.statefulMiddlewares);
|
|
125
|
+
for (let retryProvider = 0, retryInference = 0;;)
|
|
105
126
|
try {
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
session.chatMessages.push(
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
else
|
|
114
|
-
throw new CustomRetry(undefined, { cause: response });
|
|
115
|
-
session.chatMessages.push(response);
|
|
116
|
-
return response;
|
|
127
|
+
const next = () => this.infer(wfctx, session);
|
|
128
|
+
const statefulNext = async () => {
|
|
129
|
+
const aiMessage = await middleware(wfctx, session, next);
|
|
130
|
+
session.chatMessages.push(aiMessage);
|
|
131
|
+
return aiMessage;
|
|
132
|
+
};
|
|
133
|
+
return await statefulMiddleware(wfctx, session, statefulNext);
|
|
117
134
|
}
|
|
118
135
|
catch (e) {
|
|
119
|
-
if (
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (
|
|
136
|
+
if (e instanceof InferenceTimeout) { // 推理超时
|
|
137
|
+
if (retryInference < this.inferenceParams.retry) { }
|
|
138
|
+
else
|
|
139
|
+
throw e;
|
|
140
|
+
loggers.message.warn(e);
|
|
141
|
+
retryInference++;
|
|
142
|
+
}
|
|
143
|
+
else if (e instanceof SyntaxError) { // 模型抽风
|
|
144
|
+
if (retryInference < this.inferenceParams.retry) { }
|
|
145
|
+
else
|
|
146
|
+
throw e;
|
|
147
|
+
loggers.message.warn(e);
|
|
148
|
+
retryInference++;
|
|
149
|
+
}
|
|
150
|
+
else if (e instanceof TypeError) { // 网络故障
|
|
151
|
+
if (retryProvider < this.providerSpec.retry) { }
|
|
152
|
+
else
|
|
153
|
+
throw e;
|
|
154
|
+
loggers.message.warn(e);
|
|
155
|
+
retryProvider++;
|
|
156
|
+
}
|
|
127
157
|
else
|
|
128
158
|
throw e;
|
|
129
|
-
loggers.message.warn(e);
|
|
130
159
|
}
|
|
131
|
-
|
|
160
|
+
}
|
|
161
|
+
middlewares = [];
|
|
162
|
+
use(middleware) {
|
|
163
|
+
const engine = this.clone();
|
|
164
|
+
engine.middlewares.push(middleware);
|
|
165
|
+
return engine;
|
|
166
|
+
}
|
|
167
|
+
statefulMiddlewares = [];
|
|
168
|
+
statefulUse(middleware) {
|
|
169
|
+
const engine = this.clone();
|
|
170
|
+
engine.statefulMiddlewares.push(middleware);
|
|
171
|
+
return engine;
|
|
172
|
+
}
|
|
173
|
+
compose(middlewares, i = 0) {
|
|
174
|
+
if (i < middlewares.length)
|
|
175
|
+
return async (wfctx, session, next) => {
|
|
176
|
+
const middleware = middlewares[i];
|
|
177
|
+
const nextMiddlewares = this.compose(middlewares, i + 1);
|
|
178
|
+
return middleware(wfctx, session, () => nextMiddlewares(wfctx, session, next));
|
|
179
|
+
};
|
|
180
|
+
else
|
|
181
|
+
return (wfctx, session, next) => next();
|
|
132
182
|
}
|
|
133
183
|
}
|
|
134
184
|
Engine.Instance = Instance;
|
|
185
|
+
Engine.RoleMessage = SessionModule.RoleMessage;
|
|
135
186
|
})(Engine || (Engine = {}));
|
|
136
187
|
export class InferenceTimeout extends Error {
|
|
137
188
|
}
|
|
138
|
-
export class
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
189
|
+
export class Recoverable extends SyntaxError {
|
|
190
|
+
response;
|
|
191
|
+
rejection;
|
|
192
|
+
constructor(response, rejection, ...rest) {
|
|
193
|
+
super(...rest);
|
|
194
|
+
this.response = response;
|
|
195
|
+
this.rejection = rejection;
|
|
196
|
+
}
|
|
197
|
+
resume() {
|
|
198
|
+
return this.response;
|
|
199
|
+
}
|
|
200
|
+
recover() {
|
|
201
|
+
return this.rejection;
|
|
146
202
|
}
|
|
147
|
-
|
|
148
|
-
|
|
203
|
+
static async recover(wfctx, session, next) {
|
|
204
|
+
try {
|
|
205
|
+
return await next();
|
|
206
|
+
}
|
|
207
|
+
catch (e) {
|
|
208
|
+
if (e instanceof Recoverable) { }
|
|
209
|
+
else
|
|
210
|
+
throw e;
|
|
211
|
+
session.chatMessages.push(e.resume(), e.recover());
|
|
212
|
+
throw e;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
149
216
|
//# sourceMappingURL=engine.js.map
|
package/build/engine.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAc,MAAM,QAAQ,CAAC;AACvD,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AA+
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAc,MAAM,QAAQ,CAAC;AACvD,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AAEnC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,KAAK,aAAa,MAAM,qBAAqB,CAAC;AA+BrD,MAAM,KAAW,MAAM,CAuNtB;AAvND,WAAiB,MAAM;IACnB,MAAsB,QAAQ;QAMhB,YAAY,CAAe;QAC3B,eAAe,CAAkB;QACpC,IAAI,CAAS;QACb,OAAO,CAAU;QACjB,GAAG,CAAM;QACT,GAAG,CAAM;QACN,QAAQ,CAAW;QAK7B,YAAmB,OAAiC;YAChD,MAAM,QAAQ,GAAG,OAAO,CAAC,YAAY,CAAC,KAAK,IAAI,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,WAAW,CAAC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EAAE,CAAC,IAAI,EAAE,CAAC;QAChD,CAAC;KACJ;IA3LqB,eAAQ,WA2L7B,CAAA;IAuBa,kBAAW,GAAG,aAAa,CAAC,WAAW,CAAC;AAI1D,CAAC,EAvNgB,MAAM,KAAN,MAAM,QAuNtB;AAED,MAAM,OAAO,gBAAiB,SAAQ,KAAK;CAAG;AAC9C,MAAM,OAAO,WAAwB,SAAQ,WAAW;IAEtC;IACA;IAFd,YACc,QAAa,EACb,SAAgB,EAC1B,GAAG,IAA+C;QAElD,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;QAJL,aAAQ,GAAR,QAAQ,CAAK;QACb,cAAS,GAAT,SAAS,CAAO;IAI9B,CAAC;IACM,MAAM;QACT,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;IACM,OAAO;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IAC1B,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,OAAO,CAGzB,KAAuB,EAAE,OAAgB,EAAE,IAAwB;QACjE,IAAI,CAAC;YACD,OAAO,MAAM,IAAI,EAAE,CAAC;QACxB,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,IAAI,CAAC,YAAY,WAAW,EAAE,CAAC,CAAA,CAAC;;gBAAM,MAAM,CAAC,CAAC;YAC9C,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;YACnD,MAAM,CAAC,CAAC;QACZ,CAAC;IACL,CAAC;CACJ"}
|
package/build/function.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Static, type TObject, type TSchema } from '
|
|
1
|
+
import { type Static, type TObject, type TSchema } from 'typebox';
|
|
2
2
|
declare const NOMINAL: unique symbol;
|
|
3
3
|
export interface Function<in out fd extends Function.Decl.Proto> {
|
|
4
4
|
(params: Static<fd['parameters']>): Promise<string>;
|
|
@@ -5,8 +5,11 @@ import { MessageCodec as CompatibleMessageCodec } from '../../compatible-engine.
|
|
|
5
5
|
import type { ToolCodec } from '../../api-types/google/tool-codec.ts';
|
|
6
6
|
import { Verbatim } from '../../verbatim.ts';
|
|
7
7
|
export declare class MessageCodec<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
8
|
-
protected
|
|
9
|
-
|
|
8
|
+
protected toolCodec: ToolCodec<fdm>;
|
|
9
|
+
protected compatibleMessageCodec: CompatibleMessageCodec<fdm, vdm>;
|
|
10
|
+
protected codeExecution: boolean;
|
|
11
|
+
protected vdm: vdm;
|
|
12
|
+
constructor(options: MessageCodec.Options<fdm, vdm>);
|
|
10
13
|
encodeAiMessage(aiMessage: RoleMessage.Ai.From<fdm, vdm>): Google.Content;
|
|
11
14
|
encodeUserMessage(userMessage: RoleMessage.User.From<fdm>): Google.Content;
|
|
12
15
|
encodeDeveloperMessage(developerMessage: RoleMessage.Developer): Google.Content;
|
|
@@ -14,7 +17,7 @@ export declare class MessageCodec<in out fdm extends Function.Decl.Map.Proto, in
|
|
|
14
17
|
decodeAiMessage(content: Google.Content): RoleMessage.Ai.From<fdm, vdm>;
|
|
15
18
|
}
|
|
16
19
|
export declare namespace MessageCodec {
|
|
17
|
-
interface
|
|
20
|
+
interface Options<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
18
21
|
toolCodec: ToolCodec<fdm>;
|
|
19
22
|
compatibleMessageCodec: CompatibleMessageCodec<fdm, vdm>;
|
|
20
23
|
codeExecution: boolean;
|
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import { ResponseInvalid } from "../../engine.js";
|
|
2
1
|
import { RoleMessage } from "./session.js";
|
|
3
2
|
import * as VerbatimCodec from "../../verbatim/codec.js";
|
|
4
3
|
export class MessageCodec {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
toolCodec;
|
|
5
|
+
compatibleMessageCodec;
|
|
6
|
+
codeExecution;
|
|
7
|
+
vdm;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.toolCodec = options.toolCodec;
|
|
10
|
+
this.compatibleMessageCodec = options.compatibleMessageCodec;
|
|
11
|
+
this.codeExecution = options.codeExecution;
|
|
12
|
+
this.vdm = options.vdm;
|
|
8
13
|
}
|
|
9
14
|
encodeAiMessage(aiMessage) {
|
|
10
15
|
return aiMessage.getRaw();
|
|
11
16
|
}
|
|
12
17
|
encodeUserMessage(userMessage) {
|
|
13
|
-
return this.
|
|
18
|
+
return this.compatibleMessageCodec.encodeUserMessage(userMessage);
|
|
14
19
|
}
|
|
15
20
|
encodeDeveloperMessage(developerMessage) {
|
|
16
|
-
return this.
|
|
21
|
+
return this.compatibleMessageCodec.encodeDeveloperMessage(developerMessage);
|
|
17
22
|
}
|
|
18
23
|
encodeChatMessages(chatMessages) {
|
|
19
24
|
return chatMessages.map(chatMessage => {
|
|
@@ -29,26 +34,18 @@ export class MessageCodec {
|
|
|
29
34
|
if (content.parts) { }
|
|
30
35
|
else
|
|
31
36
|
throw new Error();
|
|
32
|
-
const parts =
|
|
33
|
-
|
|
34
|
-
if (part.text)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
parts.push(new RoleMessage.Part.Text(part.text, vrs));
|
|
38
|
-
}
|
|
39
|
-
catch (e) {
|
|
40
|
-
if (e instanceof SyntaxError)
|
|
41
|
-
throw new ResponseInvalid('Invalid verbatim message', { cause: content });
|
|
42
|
-
else
|
|
43
|
-
throw e;
|
|
44
|
-
}
|
|
45
|
-
if (part.functionCall) {
|
|
46
|
-
parts.push(this.ctx.toolCodec.decodeFunctionCall(part.functionCall));
|
|
37
|
+
const parts = [];
|
|
38
|
+
for (const part of content.parts) {
|
|
39
|
+
if (part.text) {
|
|
40
|
+
const vrs = VerbatimCodec.Request.decode(part.text, this.vdm);
|
|
41
|
+
parts.push(new RoleMessage.Part.Text(part.text, vrs));
|
|
47
42
|
}
|
|
43
|
+
if (part.functionCall)
|
|
44
|
+
parts.push(this.toolCodec.decodeFunctionCall(part.functionCall));
|
|
48
45
|
if (part.executableCode) {
|
|
49
|
-
if (this.
|
|
46
|
+
if (this.codeExecution) { }
|
|
50
47
|
else
|
|
51
|
-
throw new
|
|
48
|
+
throw new SyntaxError('Unexpected code execution', { cause: content });
|
|
52
49
|
if (part.executableCode.code) { }
|
|
53
50
|
else
|
|
54
51
|
throw new Error();
|
|
@@ -58,16 +55,15 @@ export class MessageCodec {
|
|
|
58
55
|
parts.push(new RoleMessage.Ai.Part.ExecutableCode(part.executableCode.code, part.executableCode.language));
|
|
59
56
|
}
|
|
60
57
|
if (part.codeExecutionResult) {
|
|
61
|
-
if (this.
|
|
58
|
+
if (this.codeExecution) { }
|
|
62
59
|
else
|
|
63
|
-
throw new
|
|
60
|
+
throw new SyntaxError('Unexpected code execution result', { cause: content });
|
|
64
61
|
if (part.codeExecutionResult.outcome) { }
|
|
65
62
|
else
|
|
66
63
|
throw new Error();
|
|
67
64
|
parts.push(new RoleMessage.Ai.Part.CodeExecutionResult(part.codeExecutionResult.outcome, part.codeExecutionResult.output));
|
|
68
65
|
}
|
|
69
|
-
|
|
70
|
-
});
|
|
66
|
+
}
|
|
71
67
|
return new RoleMessage.Ai(parts, content);
|
|
72
68
|
}
|
|
73
69
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-codec.js","sourceRoot":"","sources":["../../../src/native-engines.d/google/message-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"message-codec.js","sourceRoot":"","sources":["../../../src/native-engines.d/google/message-codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgB,MAAM,cAAc,CAAC;AAMzD,OAAO,KAAK,aAAa,MAAM,yBAAyB,CAAC;AAIzD,MAAM,OAAO,YAAY;IAIX,SAAS,CAAiB;IAC1B,sBAAsB,CAAmC;IACzD,aAAa,CAAU;IACvB,GAAG,CAAM;IACnB,YAAmB,OA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IAAI,WAAW,CAAC,EAAE,CAAC,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;YAC/H,CAAC;QACL,CAAC;QACD,OAAO,IAAI,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC9C,CAAC;CACJ"}
|
|
@@ -10,13 +10,23 @@ import type { Verbatim } from '../../verbatim.ts';
|
|
|
10
10
|
import type { Structuring } from '../../compatible-engine/structuring.ts';
|
|
11
11
|
import type { Engine } from '../../engine.ts';
|
|
12
12
|
export declare class GoogleNativeTransport<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> implements Engine.Transport<RoleMessage.User.From<fdm>, RoleMessage.Ai.From<fdm, vdm>, RoleMessage.Developer, Session.From<fdm, vdm>> {
|
|
13
|
-
protected ctx: GoogleNativeTransport.Context<fdm, vdm>;
|
|
14
13
|
protected apiURL: URL;
|
|
15
|
-
|
|
14
|
+
protected inferenceParams: InferenceParams;
|
|
15
|
+
protected providerSpec: ProviderSpec;
|
|
16
|
+
protected fdm: fdm;
|
|
17
|
+
protected throttle: Throttle;
|
|
18
|
+
protected choice: Structuring.Choice.From<fdm, vdm>;
|
|
19
|
+
protected codeExecution: boolean;
|
|
20
|
+
protected urlContext: boolean;
|
|
21
|
+
protected googleSearch: boolean;
|
|
22
|
+
protected messageCodec: MessageCodec<fdm, vdm>;
|
|
23
|
+
protected toolCodec: ToolCodec<fdm>;
|
|
24
|
+
protected billing: Billing;
|
|
25
|
+
constructor(options: GoogleNativeTransport.Options<fdm, vdm>);
|
|
16
26
|
fetch(wfctx: InferenceContext, session: Session.From<fdm, vdm>, signal?: AbortSignal): Promise<RoleMessage.Ai.From<fdm, vdm>>;
|
|
17
27
|
}
|
|
18
28
|
export declare namespace GoogleNativeTransport {
|
|
19
|
-
interface
|
|
29
|
+
interface Options<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
20
30
|
inferenceParams: InferenceParams;
|
|
21
31
|
providerSpec: ProviderSpec;
|
|
22
32
|
fdm: fdm;
|
|
@@ -1,55 +1,69 @@
|
|
|
1
|
-
import { NetworkError, ResponseInvalid } from "../../engine.js";
|
|
2
1
|
import * as Google from '@google/genai';
|
|
3
2
|
import * as Undici from 'undici';
|
|
4
3
|
import { loggers } from "../../telemetry.js";
|
|
5
4
|
import * as ChoiceCodec from "../../compatible-engine.d/google/choice-codec.js";
|
|
6
5
|
import { MIMEType } from 'whatwg-mimetype';
|
|
7
6
|
export class GoogleNativeTransport {
|
|
8
|
-
ctx;
|
|
9
7
|
apiURL;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
inferenceParams;
|
|
9
|
+
providerSpec;
|
|
10
|
+
fdm;
|
|
11
|
+
throttle;
|
|
12
|
+
choice;
|
|
13
|
+
codeExecution;
|
|
14
|
+
urlContext;
|
|
15
|
+
googleSearch;
|
|
16
|
+
messageCodec;
|
|
17
|
+
toolCodec;
|
|
18
|
+
billing;
|
|
19
|
+
constructor(options) {
|
|
20
|
+
this.apiURL = new URL(`${options.providerSpec.baseUrl}/v1beta/models/${options.inferenceParams.model}:generateContent`);
|
|
21
|
+
this.inferenceParams = options.inferenceParams;
|
|
22
|
+
this.providerSpec = options.providerSpec;
|
|
23
|
+
this.fdm = options.fdm;
|
|
24
|
+
this.throttle = options.throttle;
|
|
25
|
+
this.choice = options.choice;
|
|
26
|
+
this.codeExecution = options.codeExecution;
|
|
27
|
+
this.urlContext = options.urlContext;
|
|
28
|
+
this.googleSearch = options.googleSearch;
|
|
29
|
+
this.messageCodec = options.messageCodec;
|
|
30
|
+
this.toolCodec = options.toolCodec;
|
|
31
|
+
this.billing = options.billing;
|
|
13
32
|
}
|
|
14
33
|
async fetch(wfctx, session, signal) {
|
|
15
|
-
const systemInstruction = session.developerMessage && this.
|
|
16
|
-
const contents = this.
|
|
17
|
-
await this.
|
|
18
|
-
const apifds = this.
|
|
34
|
+
const systemInstruction = session.developerMessage && this.messageCodec.encodeDeveloperMessage(session.developerMessage);
|
|
35
|
+
const contents = this.messageCodec.encodeChatMessages(session.chatMessages);
|
|
36
|
+
await this.throttle.requests(wfctx);
|
|
37
|
+
const apifds = this.toolCodec.encodeFunctionDeclarationMap(this.fdm);
|
|
19
38
|
const apiTools = [];
|
|
20
39
|
if (apifds.length)
|
|
21
40
|
apiTools.push({ functionDeclarations: apifds });
|
|
22
|
-
if (this.
|
|
41
|
+
if (this.urlContext)
|
|
23
42
|
apiTools.push({ urlContext: {} });
|
|
24
|
-
if (this.
|
|
43
|
+
if (this.googleSearch)
|
|
25
44
|
apiTools.push({ googleSearch: {} });
|
|
26
|
-
if (this.
|
|
45
|
+
if (this.codeExecution)
|
|
27
46
|
apiTools.push({ codeExecution: {} });
|
|
28
47
|
const apiToolConfig = {};
|
|
29
48
|
if (apifds.length)
|
|
30
|
-
apiToolConfig.functionCallingConfig = ChoiceCodec.encode(this.
|
|
49
|
+
apiToolConfig.functionCallingConfig = ChoiceCodec.encode(this.choice);
|
|
31
50
|
const reqbody = {
|
|
32
51
|
contents,
|
|
33
52
|
tools: apiTools.length ? apiTools : undefined,
|
|
34
53
|
toolConfig: apiToolConfig,
|
|
35
54
|
systemInstruction,
|
|
36
|
-
generationConfig: this.
|
|
55
|
+
generationConfig: this.inferenceParams.additionalOptions,
|
|
37
56
|
};
|
|
38
57
|
loggers.message.debug(reqbody);
|
|
39
58
|
const res = await Undici.fetch(this.apiURL, {
|
|
40
59
|
method: 'POST',
|
|
41
60
|
headers: {
|
|
42
61
|
'Content-Type': 'application/json',
|
|
43
|
-
'x-goog-api-key': this.
|
|
62
|
+
'x-goog-api-key': this.providerSpec.apiKey,
|
|
44
63
|
},
|
|
45
64
|
body: JSON.stringify(reqbody),
|
|
46
|
-
dispatcher: this.
|
|
65
|
+
dispatcher: this.providerSpec.dispatcher,
|
|
47
66
|
signal,
|
|
48
|
-
}).catch(e => {
|
|
49
|
-
if (e instanceof TypeError)
|
|
50
|
-
throw new NetworkError(undefined, { cause: e });
|
|
51
|
-
else
|
|
52
|
-
throw e;
|
|
53
67
|
});
|
|
54
68
|
loggers.message.debug(res);
|
|
55
69
|
if (res.ok) { }
|
|
@@ -69,12 +83,12 @@ export class GoogleNativeTransport {
|
|
|
69
83
|
const response = await res.json();
|
|
70
84
|
if (response.candidates?.[0]?.content?.parts?.length) { }
|
|
71
85
|
else
|
|
72
|
-
throw new
|
|
86
|
+
throw new SyntaxError('Content missing', { cause: response });
|
|
73
87
|
if (response.candidates[0].finishReason === Google.FinishReason.MAX_TOKENS)
|
|
74
|
-
throw new
|
|
88
|
+
throw new SyntaxError('Token limit exceeded.', { cause: response });
|
|
75
89
|
if (response.candidates[0].finishReason === Google.FinishReason.STOP) { }
|
|
76
90
|
else
|
|
77
|
-
throw new
|
|
91
|
+
throw new SyntaxError('Abnormal finish reason', { cause: response });
|
|
78
92
|
for (const part of response.candidates[0].content.parts) {
|
|
79
93
|
if (part.text)
|
|
80
94
|
loggers.inference.info(part.text);
|
|
@@ -83,9 +97,9 @@ export class GoogleNativeTransport {
|
|
|
83
97
|
}
|
|
84
98
|
if (response.usageMetadata) { }
|
|
85
99
|
else
|
|
86
|
-
throw new
|
|
87
|
-
wfctx.cost?.(this.
|
|
88
|
-
return this.
|
|
100
|
+
throw new SyntaxError('Usage metadata missing', { cause: response });
|
|
101
|
+
wfctx.cost?.(this.billing.charge(response.usageMetadata));
|
|
102
|
+
return this.messageCodec.decodeAiMessage(response.candidates[0].content);
|
|
89
103
|
}
|
|
90
104
|
}
|
|
91
105
|
//# sourceMappingURL=transport.js.map
|