@zimtsui/typechat 0.0.98 → 0.0.99
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 +3 -8
- package/README.md +5 -177
- package/assets/verbatim/instruction.md +1 -1
- package/assets/verbatim/response.handlebars +1 -1
- package/build/adaptor.d.ts +9 -9
- package/build/adaptor.js +15 -17
- package/build/adaptor.js.map +1 -1
- package/build/endpoint-spec.d.ts +1 -1
- package/build/endpoint-spec.js +1 -1
- package/build/engine/message.d.ts +75 -0
- package/build/engine/message.js +165 -0
- package/build/engine/message.js.map +1 -0
- package/build/engine/middleware.d.ts +11 -0
- package/build/engine/middleware.js +1 -0
- package/build/engine/middleware.js.map +1 -0
- package/build/engine/parts-validator.d.ts +9 -0
- package/build/engine/parts-validator.js +11 -0
- package/build/engine/parts-validator.js.map +1 -0
- package/build/engine/recoverable.d.ts +13 -0
- package/build/engine/recoverable.js +29 -0
- package/build/engine/recoverable.js.map +1 -0
- package/build/engine/session.d.ts +8 -32
- package/build/engine/session.js +0 -19
- package/build/engine/session.js.map +1 -1
- package/build/engine/structuring-validator.d.ts +15 -0
- package/build/engine/structuring-validator.js +64 -0
- package/build/engine/structuring-validator.js.map +1 -0
- package/build/engine/transport.d.ts +6 -3
- package/build/engine.d.ts +40 -39
- package/build/engine.js +100 -64
- package/build/engine.js.map +1 -1
- package/build/engines/anthropic/billing.js.map +1 -0
- package/build/engines/anthropic/message-codec.d.ts +22 -0
- package/build/engines/anthropic/message-codec.js +79 -0
- package/build/engines/anthropic/message-codec.js.map +1 -0
- package/build/engines/anthropic/message.d.ts +22 -0
- package/build/engines/anthropic/message.js +25 -0
- package/build/engines/anthropic/message.js.map +1 -0
- package/build/{compatible-engine.d/anthropic/choice-codec.d.ts → engines/anthropic/structuring-choice-codec.d.ts} +2 -2
- package/build/engines/anthropic/structuring-choice-codec.js +23 -0
- package/build/engines/anthropic/structuring-choice-codec.js.map +1 -0
- package/build/{api-types → engines}/anthropic/tool-codec.d.ts +5 -4
- package/build/{api-types → engines}/anthropic/tool-codec.js +16 -14
- package/build/engines/anthropic/tool-codec.js.map +1 -0
- package/build/engines/anthropic/transport.d.ts +37 -0
- package/build/{compatible-engine.d → engines}/anthropic/transport.js +8 -12
- package/build/engines/anthropic/transport.js.map +1 -0
- package/build/engines/anthropic.d.ts +22 -0
- package/build/engines/anthropic.js +46 -0
- package/build/engines/anthropic.js.map +1 -0
- package/build/engines/google/billing.js.map +1 -0
- package/build/engines/google/message-codec.d.ts +24 -0
- package/build/engines/google/message-codec.js +113 -0
- package/build/engines/google/message-codec.js.map +1 -0
- package/build/engines/google/message.d.ts +37 -0
- package/build/engines/google/message.js +58 -0
- package/build/engines/google/message.js.map +1 -0
- package/build/engines/google/restful-request.js.map +1 -0
- package/build/{compatible-engine.d/google/choice-codec.d.ts → engines/google/structuring-choice-codec.d.ts} +2 -2
- package/build/engines/google/structuring-choice-codec.js +23 -0
- package/build/engines/google/structuring-choice-codec.js.map +1 -0
- package/build/{api-types → engines}/google/tool-codec.d.ts +5 -4
- package/build/{api-types → engines}/google/tool-codec.js +15 -13
- package/build/engines/google/tool-codec.js.map +1 -0
- package/build/{native-engines.d → engines}/google/transport.d.ts +13 -14
- package/build/{native-engines.d → engines}/google/transport.js +18 -13
- package/build/engines/google/transport.js.map +1 -0
- package/build/engines/google.d.ts +41 -0
- package/build/engines/google.js +151 -0
- package/build/engines/google.js.map +1 -0
- package/build/engines/openai-chatcompletions/billing.js.map +1 -0
- package/build/engines/openai-chatcompletions/message-codec.d.ts +22 -0
- package/build/engines/openai-chatcompletions/message-codec.js +80 -0
- package/build/engines/openai-chatcompletions/message-codec.js.map +1 -0
- package/build/{compatible-engine.d/openai-chatcompletions/choice-codec.d.ts → engines/openai-chatcompletions/structuring-choice-codec.d.ts} +2 -2
- package/build/engines/openai-chatcompletions/structuring-choice-codec.js +22 -0
- package/build/engines/openai-chatcompletions/structuring-choice-codec.js.map +1 -0
- package/build/engines/openai-chatcompletions/tool-codec.d.ts +17 -0
- package/build/{api-types → engines}/openai-chatcompletions/tool-codec.js +19 -17
- package/build/engines/openai-chatcompletions/tool-codec.js.map +1 -0
- package/build/{compatible-engine.d → engines}/openai-chatcompletions/transport.d.ts +13 -15
- package/build/{compatible-engine.d → engines}/openai-chatcompletions/transport.js +8 -18
- package/build/engines/openai-chatcompletions/transport.js.map +1 -0
- package/build/engines/openai-chatcompletions.d.ts +22 -0
- package/build/engines/openai-chatcompletions.js +48 -0
- package/build/engines/openai-chatcompletions.js.map +1 -0
- package/build/engines/openai-responses/billing.js.map +1 -0
- package/build/engines/openai-responses/message-codec.d.ts +24 -0
- package/build/{compatible-engine.d → engines}/openai-responses/message-codec.js +55 -51
- package/build/engines/openai-responses/message-codec.js.map +1 -0
- package/build/engines/openai-responses/message.d.ts +25 -0
- package/build/engines/openai-responses/message.js +43 -0
- package/build/engines/openai-responses/message.js.map +1 -0
- package/build/engines/openai-responses/structuring-choice-codec.d.ts +3 -0
- package/build/engines/openai-responses/structuring-choice-codec.js +22 -0
- package/build/engines/openai-responses/structuring-choice-codec.js.map +1 -0
- package/build/engines/openai-responses/structuring-validator.d.ts +12 -0
- package/build/engines/openai-responses/structuring-validator.js +64 -0
- package/build/engines/openai-responses/structuring-validator.js.map +1 -0
- package/build/engines/openai-responses/tool-codec.d.ts +17 -0
- package/build/{api-types → engines}/openai-responses/tool-codec.js +19 -17
- package/build/engines/openai-responses/tool-codec.js.map +1 -0
- package/build/{native-engines.d → engines}/openai-responses/tool.d.ts +1 -3
- package/build/{native-engines.d → engines}/openai-responses/tool.js +3 -3
- package/build/engines/openai-responses/tool.js.map +1 -0
- package/build/{compatible-engine.d → engines}/openai-responses/transport.d.ts +16 -12
- package/build/{native-engines.d → engines}/openai-responses/transport.js +11 -9
- package/build/engines/openai-responses/transport.js.map +1 -0
- package/build/engines/openai-responses.d.ts +41 -0
- package/build/engines/openai-responses.js +134 -0
- package/build/engines/openai-responses.js.map +1 -0
- package/build/exports.d.ts +5 -5
- package/build/exports.js +5 -5
- package/build/exports.js.map +1 -1
- package/build/function.d.ts +7 -7
- package/build/function.js +19 -19
- package/build/function.js.map +1 -1
- package/build/repetition.d.ts +2 -2
- package/build/repetition.js +21 -20
- package/build/repetition.js.map +1 -1
- package/build/structuring-choice.d.ts +15 -0
- package/build/structuring-choice.js +18 -0
- package/build/structuring-choice.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/verbatim/codec/declarations.js +0 -2
- package/build/verbatim/codec/declarations.js.map +1 -1
- package/build/verbatim/codec/quotation.js +0 -3
- package/build/verbatim/codec/quotation.js.map +1 -1
- package/build/verbatim/codec/request.js +4 -4
- package/build/verbatim/codec/request.js.map +1 -1
- package/build/verbatim/codec/response.js +0 -3
- package/build/verbatim/codec/response.js.map +1 -1
- package/package.json +2 -4
- package/build/api-types/anthropic/billing.js.map +0 -1
- package/build/api-types/anthropic/tool-codec.js.map +0 -1
- package/build/api-types/google/billing.js.map +0 -1
- package/build/api-types/google/restful-request.js.map +0 -1
- package/build/api-types/google/tool-codec.js.map +0 -1
- package/build/api-types/openai-chatcompletions/billing.js.map +0 -1
- package/build/api-types/openai-chatcompletions/tool-codec.d.ts +0 -16
- package/build/api-types/openai-chatcompletions/tool-codec.js.map +0 -1
- package/build/api-types/openai-responses/billing.js.map +0 -1
- package/build/api-types/openai-responses/tool-codec.d.ts +0 -16
- package/build/api-types/openai-responses/tool-codec.js.map +0 -1
- package/build/compatible-engine/session.d.ts +0 -56
- package/build/compatible-engine/session.js +0 -85
- package/build/compatible-engine/session.js.map +0 -1
- package/build/compatible-engine/structuring.d.ts +0 -36
- package/build/compatible-engine/structuring.js +0 -34
- package/build/compatible-engine/structuring.js.map +0 -1
- package/build/compatible-engine/validation.d.ts +0 -28
- package/build/compatible-engine/validation.js +0 -104
- package/build/compatible-engine/validation.js.map +0 -1
- package/build/compatible-engine.d/agentloop.d.ts +0 -12
- package/build/compatible-engine.d/agentloop.js +0 -52
- package/build/compatible-engine.d/agentloop.js.map +0 -1
- package/build/compatible-engine.d/anthropic/choice-codec.js +0 -27
- package/build/compatible-engine.d/anthropic/choice-codec.js.map +0 -1
- package/build/compatible-engine.d/anthropic/message-codec.d.ts +0 -34
- package/build/compatible-engine.d/anthropic/message-codec.js +0 -85
- package/build/compatible-engine.d/anthropic/message-codec.js.map +0 -1
- package/build/compatible-engine.d/anthropic/transport.d.ts +0 -38
- package/build/compatible-engine.d/anthropic/transport.js.map +0 -1
- package/build/compatible-engine.d/anthropic.d.ts +0 -25
- package/build/compatible-engine.d/anthropic.js +0 -47
- package/build/compatible-engine.d/anthropic.js.map +0 -1
- package/build/compatible-engine.d/google/choice-codec.js +0 -27
- package/build/compatible-engine.d/google/choice-codec.js.map +0 -1
- package/build/compatible-engine.d/google/message-codec.d.ts +0 -34
- package/build/compatible-engine.d/google/message-codec.js +0 -95
- package/build/compatible-engine.d/google/message-codec.js.map +0 -1
- package/build/compatible-engine.d/google/transport.d.ts +0 -35
- package/build/compatible-engine.d/google/transport.js +0 -97
- package/build/compatible-engine.d/google/transport.js.map +0 -1
- package/build/compatible-engine.d/google.d.ts +0 -25
- package/build/compatible-engine.d/google.js +0 -64
- package/build/compatible-engine.d/google.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js +0 -31
- package/build/compatible-engine.d/openai-chatcompletions/choice-codec.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.d.ts +0 -22
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js +0 -68
- package/build/compatible-engine.d/openai-chatcompletions/message-codec.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions/transport.js.map +0 -1
- package/build/compatible-engine.d/openai-chatcompletions.d.ts +0 -25
- package/build/compatible-engine.d/openai-chatcompletions.js +0 -49
- package/build/compatible-engine.d/openai-chatcompletions.js.map +0 -1
- package/build/compatible-engine.d/openai-responses/choice-codec.d.ts +0 -5
- package/build/compatible-engine.d/openai-responses/choice-codec.js +0 -30
- package/build/compatible-engine.d/openai-responses/choice-codec.js.map +0 -1
- package/build/compatible-engine.d/openai-responses/message-codec.d.ts +0 -36
- package/build/compatible-engine.d/openai-responses/message-codec.js.map +0 -1
- package/build/compatible-engine.d/openai-responses/transport.js +0 -91
- package/build/compatible-engine.d/openai-responses/transport.js.map +0 -1
- package/build/compatible-engine.d/openai-responses.d.ts +0 -25
- package/build/compatible-engine.d/openai-responses.js +0 -47
- package/build/compatible-engine.d/openai-responses.js.map +0 -1
- package/build/compatible-engine.d.ts +0 -27
- package/build/compatible-engine.js +0 -34
- package/build/compatible-engine.js.map +0 -1
- package/build/engine/validation.d.ts +0 -6
- package/build/engine/validation.js +0 -1
- package/build/engine/validation.js.map +0 -1
- package/build/native-engines.d/google/message-codec.d.ts +0 -26
- package/build/native-engines.d/google/message-codec.js +0 -70
- package/build/native-engines.d/google/message-codec.js.map +0 -1
- package/build/native-engines.d/google/session.d.ts +0 -58
- package/build/native-engines.d/google/session.js +0 -101
- package/build/native-engines.d/google/session.js.map +0 -1
- package/build/native-engines.d/google/transport.js.map +0 -1
- package/build/native-engines.d/google/validation.d.ts +0 -28
- package/build/native-engines.d/google/validation.js +0 -26
- package/build/native-engines.d/google/validation.js.map +0 -1
- package/build/native-engines.d/google.d/agentloop.d.ts +0 -12
- package/build/native-engines.d/google.d/agentloop.js +0 -57
- package/build/native-engines.d/google.d/agentloop.js.map +0 -1
- package/build/native-engines.d/google.d/exports.d.ts +0 -2
- package/build/native-engines.d/google.d/exports.js +0 -3
- package/build/native-engines.d/google.d/exports.js.map +0 -1
- package/build/native-engines.d/google.d.ts +0 -44
- package/build/native-engines.d/google.js +0 -100
- package/build/native-engines.d/google.js.map +0 -1
- package/build/native-engines.d/openai-responses/choice-codec.d.ts +0 -5
- package/build/native-engines.d/openai-responses/choice-codec.js +0 -30
- package/build/native-engines.d/openai-responses/choice-codec.js.map +0 -1
- package/build/native-engines.d/openai-responses/message-codec.d.ts +0 -25
- package/build/native-engines.d/openai-responses/message-codec.js +0 -99
- package/build/native-engines.d/openai-responses/message-codec.js.map +0 -1
- package/build/native-engines.d/openai-responses/session.d.ts +0 -61
- package/build/native-engines.d/openai-responses/session.js +0 -86
- package/build/native-engines.d/openai-responses/session.js.map +0 -1
- package/build/native-engines.d/openai-responses/structuring.d.ts +0 -19
- package/build/native-engines.d/openai-responses/structuring.js +0 -19
- package/build/native-engines.d/openai-responses/structuring.js.map +0 -1
- package/build/native-engines.d/openai-responses/tool.js.map +0 -1
- package/build/native-engines.d/openai-responses/transport.d.ts +0 -40
- package/build/native-engines.d/openai-responses/transport.js.map +0 -1
- package/build/native-engines.d/openai-responses/validation.d.ts +0 -28
- package/build/native-engines.d/openai-responses/validation.js +0 -101
- package/build/native-engines.d/openai-responses/validation.js.map +0 -1
- package/build/native-engines.d/openai-responses.d/agentloop.d.ts +0 -12
- package/build/native-engines.d/openai-responses.d/agentloop.js +0 -61
- package/build/native-engines.d/openai-responses.d/agentloop.js.map +0 -1
- package/build/native-engines.d/openai-responses.d/exports.d.ts +0 -2
- package/build/native-engines.d/openai-responses.d/exports.js +0 -3
- package/build/native-engines.d/openai-responses.d/exports.js.map +0 -1
- package/build/native-engines.d/openai-responses.d.ts +0 -46
- package/build/native-engines.d/openai-responses.js +0 -82
- package/build/native-engines.d/openai-responses.js.map +0 -1
- /package/build/{api-types → engines}/anthropic/billing.d.ts +0 -0
- /package/build/{api-types → engines}/anthropic/billing.js +0 -0
- /package/build/{api-types → engines}/google/billing.d.ts +0 -0
- /package/build/{api-types → engines}/google/billing.js +0 -0
- /package/build/{api-types → engines}/google/restful-request.d.ts +0 -0
- /package/build/{api-types → engines}/google/restful-request.js +0 -0
- /package/build/{api-types → engines}/openai-chatcompletions/billing.d.ts +0 -0
- /package/build/{api-types → engines}/openai-chatcompletions/billing.js +0 -0
- /package/build/{api-types → engines}/openai-responses/billing.d.ts +0 -0
- /package/build/{api-types → engines}/openai-responses/billing.js +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parts-validator.js","sourceRoot":"","sources":["../../src/engine/parts-validator.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAI/C,MAAM,OAAO,cAAc;IAKhB,QAAQ,CACX,OAAiC;QAEjC,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,WAAW,CAAC,gBAAgB,CAAC,CAAC;QAC/E,IAAI,WAAW,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;YAAE,MAAM,IAAI,WAAW,CAAC,WAAW,CAAC,CAAC;IAC3E,CAAC;CAEJ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Function } from '../function.ts';
|
|
2
|
+
import { type InferenceContext } from '../inference-context.ts';
|
|
3
|
+
import { Session } from './session.ts';
|
|
4
|
+
import { RoleMessage } from './message.ts';
|
|
5
|
+
import { Verbatim } from '../verbatim.ts';
|
|
6
|
+
export declare class Recoverable<in out fdu extends Function.Decl.Proto, in out vdu extends Verbatim.Decl.Proto> extends SyntaxError {
|
|
7
|
+
protected response: RoleMessage.Ai<fdu, vdu>;
|
|
8
|
+
protected rejection: RoleMessage.User<never>;
|
|
9
|
+
constructor(response: RoleMessage.Ai<fdu, vdu>, rejection: RoleMessage.User<never>, ...rest: ConstructorParameters<typeof SyntaxError>);
|
|
10
|
+
resume(): RoleMessage.Ai<fdu, vdu>;
|
|
11
|
+
recover(): RoleMessage.User<never>;
|
|
12
|
+
static recover<fdu extends Function.Decl.Proto, vdu extends Verbatim.Decl.Proto>(wfctx: InferenceContext, session: Session<fdu, vdu>, next: () => Promise<RoleMessage.Ai<fdu, vdu>>): Promise<RoleMessage.Ai<fdu, vdu>>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class Recoverable extends SyntaxError {
|
|
2
|
+
response;
|
|
3
|
+
rejection;
|
|
4
|
+
constructor(response, rejection, ...rest) {
|
|
5
|
+
super(...rest);
|
|
6
|
+
this.response = response;
|
|
7
|
+
this.rejection = rejection;
|
|
8
|
+
}
|
|
9
|
+
resume() {
|
|
10
|
+
return this.response;
|
|
11
|
+
}
|
|
12
|
+
recover() {
|
|
13
|
+
return this.rejection;
|
|
14
|
+
}
|
|
15
|
+
static async recover(wfctx, session, next) {
|
|
16
|
+
try {
|
|
17
|
+
return await next();
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
if (e instanceof Recoverable) { }
|
|
21
|
+
else
|
|
22
|
+
throw e;
|
|
23
|
+
const recoverable = e;
|
|
24
|
+
session.chatMessages.push(recoverable.resume(), recoverable.recover());
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=recoverable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recoverable.js","sourceRoot":"","sources":["../../src/engine/recoverable.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,WAGX,SAAQ,WAAW;IAEH;IACA;IAFd,YACc,QAAkC,EAClC,SAAkC,EAC5C,GAAG,IAA+C;QAElD,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC;QAJL,aAAQ,GAAR,QAAQ,CAA0B;QAClC,cAAS,GAAT,SAAS,CAAyB;IAIhD,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,CAIvB,KAAuB,EACvB,OAA0B,EAC1B,IAA6C;QAE7C,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,MAAM,WAAW,GAAG,CAA0B,CAAC;YAC/C,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,EAAE,WAAW,CAAC,OAAO,EAAE,CAAC,CAAC;YACvE,MAAM,CAAC,CAAC;QACZ,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1,38 +1,14 @@
|
|
|
1
1
|
import { Function } from '../function.ts';
|
|
2
2
|
import { Verbatim } from '../verbatim.ts';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
developerMessage?: devm;
|
|
3
|
+
import { RoleMessage } from './message.ts';
|
|
4
|
+
export interface Session<in out fdu extends Function.Decl.Proto, in out vdu extends Verbatim.Decl.Proto> {
|
|
5
|
+
chatMessages: Session.ChatMessage<fdu, vdu>[];
|
|
6
|
+
developerMessage?: RoleMessage.Developer;
|
|
8
7
|
}
|
|
9
8
|
export declare namespace Session {
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
class Text<out vdu extends Verbatim.Decl.Proto> {
|
|
15
|
-
text: string;
|
|
16
|
-
vrs: Verbatim.Request.Of<vdu>[];
|
|
17
|
-
static paragraph(text: string): Text<never>;
|
|
18
|
-
protected [NOMINAL]: never;
|
|
19
|
-
constructor(text: string, vrs: Verbatim.Request.Of<vdu>[]);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
namespace Ai {
|
|
23
|
-
type Part<fdu extends Function.Decl.Proto, vdu extends Verbatim.Decl.Proto> = RoleMessage.Part.Text<vdu> | Function.Call.Of<fdu>;
|
|
24
|
-
namespace Part {
|
|
25
|
-
type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = RoleMessage.Ai.Part<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
namespace User {
|
|
29
|
-
type Part<fdu extends Function.Decl.Proto> = RoleMessage.Part.Text<never> | Function.Response.Of<fdu> | Media;
|
|
30
|
-
namespace Part {
|
|
31
|
-
type From<fdm extends Function.Decl.Map.Proto> = RoleMessage.User.Part<Function.Decl.From<fdm>>;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
namespace Developer {
|
|
35
|
-
type Part = RoleMessage.Part.Text<never>;
|
|
9
|
+
type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = Session<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
|
|
10
|
+
type ChatMessage<fdu extends Function.Decl.Proto, vdu extends Verbatim.Decl.Proto> = RoleMessage.User<fdu> | RoleMessage.Ai<fdu, vdu>;
|
|
11
|
+
namespace ChatMessage {
|
|
12
|
+
type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = ChatMessage<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
|
|
36
13
|
}
|
|
37
14
|
}
|
|
38
|
-
export {};
|
package/build/engine/session.js
CHANGED
|
@@ -1,20 +1 @@
|
|
|
1
|
-
const NOMINAL = Symbol();
|
|
2
|
-
export var RoleMessage;
|
|
3
|
-
(function (RoleMessage) {
|
|
4
|
-
let Part;
|
|
5
|
-
(function (Part) {
|
|
6
|
-
class Text {
|
|
7
|
-
text;
|
|
8
|
-
vrs;
|
|
9
|
-
static paragraph(text) {
|
|
10
|
-
return new RoleMessage.Part.Text(text.trimEnd() + '\n\n', []);
|
|
11
|
-
}
|
|
12
|
-
constructor(text, vrs) {
|
|
13
|
-
this.text = text;
|
|
14
|
-
this.vrs = vrs;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
Part.Text = Text;
|
|
18
|
-
})(Part = RoleMessage.Part || (RoleMessage.Part = {}));
|
|
19
|
-
})(RoleMessage || (RoleMessage = {}));
|
|
20
1
|
//# sourceMappingURL=session.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/engine/session.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/engine/session.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StructuringChoice } from '../structuring-choice.ts';
|
|
2
|
+
import { Function } from '../function.ts';
|
|
3
|
+
import { Verbatim } from '../verbatim.ts';
|
|
4
|
+
import { RoleMessage } from './message.ts';
|
|
5
|
+
export declare class StructuringValidator<in out fdu extends Function.Decl.Proto, in out vdu extends Verbatim.Decl.Proto> {
|
|
6
|
+
protected structuringChoice: StructuringChoice;
|
|
7
|
+
constructor(options: StructuringValidator.Options);
|
|
8
|
+
validate(aiMessage: RoleMessage.Ai<fdu, vdu>): RoleMessage.User<never> | void;
|
|
9
|
+
}
|
|
10
|
+
export declare namespace StructuringValidator {
|
|
11
|
+
type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = StructuringValidator<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
|
|
12
|
+
interface Options {
|
|
13
|
+
structuringChoice: StructuringChoice;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { StructuringChoice } from "../structuring-choice.js";
|
|
2
|
+
import { RoleMessage } from "./message.js";
|
|
3
|
+
import * as VerbatimCodec from "../verbatim/codec.js";
|
|
4
|
+
export class StructuringValidator {
|
|
5
|
+
structuringChoice;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.structuringChoice = options.structuringChoice;
|
|
8
|
+
}
|
|
9
|
+
validate(aiMessage) {
|
|
10
|
+
const fcalls = aiMessage.getFunctionCalls();
|
|
11
|
+
const vreqs = aiMessage.getVerbatimRequests();
|
|
12
|
+
if (this.structuringChoice === StructuringChoice.TCall.REQUIRED) {
|
|
13
|
+
if (!fcalls.length)
|
|
14
|
+
throw new SyntaxError('Function call required.');
|
|
15
|
+
}
|
|
16
|
+
else if (this.structuringChoice === StructuringChoice.TCall.ANYONE) {
|
|
17
|
+
if (!fcalls.length)
|
|
18
|
+
throw new SyntaxError('Function call required.');
|
|
19
|
+
if (fcalls.length > 1)
|
|
20
|
+
throw new SyntaxError('Only 1 function call allowed.');
|
|
21
|
+
}
|
|
22
|
+
else if (this.structuringChoice === StructuringChoice.VRequest.REQUIRED) {
|
|
23
|
+
if (!vreqs.length)
|
|
24
|
+
return new RoleMessage.User([
|
|
25
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Verbatim request required, but not found. Check your output format.`)),
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
else if (this.structuringChoice === StructuringChoice.VRequest.ANYONE) {
|
|
29
|
+
if (!vreqs.length)
|
|
30
|
+
return new RoleMessage.User([
|
|
31
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Verbatim request required, but not found. Check your output format.`)),
|
|
32
|
+
]);
|
|
33
|
+
if (vreqs.length > 1)
|
|
34
|
+
return new RoleMessage.User([
|
|
35
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Only 1 verbatim request allowed, but multiple found.`)),
|
|
36
|
+
]);
|
|
37
|
+
}
|
|
38
|
+
else if (this.structuringChoice === StructuringChoice.REQUIRED) {
|
|
39
|
+
if (fcalls.length + vreqs.length) { }
|
|
40
|
+
else
|
|
41
|
+
return new RoleMessage.User([
|
|
42
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Either function call or verbatim request required, but none found. Check your output format.`)),
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
45
|
+
else if (this.structuringChoice === StructuringChoice.ANYONE) {
|
|
46
|
+
if (fcalls.length + vreqs.length) { }
|
|
47
|
+
else
|
|
48
|
+
return new RoleMessage.User([
|
|
49
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Either 1 function call or 1 verbatim request required, but none found. Check your output format.`)),
|
|
50
|
+
]);
|
|
51
|
+
if (fcalls.length + vreqs.length > 1)
|
|
52
|
+
return new RoleMessage.User([
|
|
53
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Either Only 1 function call or only 1 verbatim request allowed, but multiple found.`)),
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
else if (this.structuringChoice === StructuringChoice.NONE) {
|
|
57
|
+
if (fcalls.length + vreqs.length)
|
|
58
|
+
return new RoleMessage.User([
|
|
59
|
+
RoleMessage.User.Part.Text.paragraph(VerbatimCodec.System.encode(`Error: Neither function call nor verbatim request allowed.`)),
|
|
60
|
+
]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=structuring-validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structuring-validator.js","sourceRoot":"","sources":["../../src/engine/structuring-validator.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { Function } from '../function.ts';
|
|
1
2
|
import { type InferenceContext } from '../inference-context.ts';
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { Session } from './session.ts';
|
|
4
|
+
import { RoleMessage } from './message.ts';
|
|
5
|
+
import { Verbatim } from '../verbatim.ts';
|
|
6
|
+
export interface Transport<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
7
|
+
fetch(wfctx: InferenceContext, session: Session.From<fdm, vdm>, signal?: AbortSignal): Promise<RoleMessage.Ai.From<fdm, vdm>>;
|
|
5
8
|
}
|
package/build/engine.d.ts
CHANGED
|
@@ -4,99 +4,100 @@ import { Throttle } from './throttle.ts';
|
|
|
4
4
|
import { Dispatcher } from 'undici';
|
|
5
5
|
import { type InferenceContext } from './inference-context.ts';
|
|
6
6
|
import * as SessionModule from './engine/session.ts';
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
7
|
+
import * as MessageModule from './engine/message.ts';
|
|
8
|
+
import { Verbatim } from './verbatim.ts';
|
|
9
|
+
import { PartsValidator } from './engine/parts-validator.ts';
|
|
10
|
+
import { Recoverable } from './engine/recoverable.ts';
|
|
11
|
+
import { Middleware } from './engine/middleware.ts';
|
|
12
|
+
import * as StructuringValidatorModule from './engine/structuring-validator.ts';
|
|
13
|
+
import * as EngineTransportModule from './engine/transport.ts';
|
|
14
|
+
import { StructuringChoice } from './structuring-choice.ts';
|
|
10
15
|
export interface Pricing {
|
|
11
16
|
inputPrice: number;
|
|
12
17
|
cachePrice: number;
|
|
13
18
|
outputPrice: number;
|
|
14
19
|
}
|
|
15
|
-
export interface
|
|
20
|
+
export interface ProviderSpecs {
|
|
16
21
|
baseUrl: string;
|
|
17
22
|
apiKey: string;
|
|
18
23
|
dispatcher: Dispatcher;
|
|
19
24
|
retry: number;
|
|
20
25
|
}
|
|
21
|
-
export interface
|
|
26
|
+
export interface InferenceOptions {
|
|
22
27
|
model: string;
|
|
23
28
|
additionalOptions?: Record<string, unknown>;
|
|
24
29
|
parallelToolCall?: boolean;
|
|
25
30
|
timeout?: number;
|
|
26
31
|
retry: number;
|
|
27
32
|
}
|
|
28
|
-
export type Engine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto
|
|
33
|
+
export type Engine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = Engine.Instance<fdm, vdm>;
|
|
29
34
|
export declare namespace Engine {
|
|
30
|
-
abstract class Instance<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto
|
|
31
|
-
protected
|
|
32
|
-
protected
|
|
35
|
+
abstract class Instance<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
36
|
+
protected providerSpecs: ProviderSpecs;
|
|
37
|
+
protected inferenceOptions: InferenceOptions;
|
|
33
38
|
name: string;
|
|
34
39
|
pricing: Pricing;
|
|
35
40
|
fdm: fdm;
|
|
36
41
|
vdm: vdm;
|
|
37
42
|
protected throttle: Throttle;
|
|
38
|
-
protected
|
|
39
|
-
protected
|
|
40
|
-
protected
|
|
43
|
+
protected structuringChoice: StructuringChoice;
|
|
44
|
+
protected structuringValidator: Engine.StructuringValidator.From<fdm, vdm>;
|
|
45
|
+
protected partsValidator: PartsValidator.From<fdm, vdm>;
|
|
46
|
+
protected abstract transport: Engine.Transport<fdm, vdm>;
|
|
41
47
|
constructor(options: Engine.Options<fdm, vdm>);
|
|
42
48
|
/**
|
|
49
|
+
* @throws {@link InferenceTimeout} 推理超时
|
|
43
50
|
* @throws {@link SyntaxError} 模型抽风
|
|
44
51
|
* @throws {@link Recoverable} 模型抽风但可恢复
|
|
45
52
|
* @throws {@link TypeError} 网络故障
|
|
46
53
|
*/
|
|
47
|
-
protected infer(wfctx: InferenceContext, session:
|
|
54
|
+
protected infer(wfctx: InferenceContext, session: Session.From<fdm, vdm>): Promise<RoleMessage.Ai.From<fdm, vdm>>;
|
|
48
55
|
/**
|
|
49
56
|
* @throws {@link InferenceTimeout} 推理超时
|
|
50
57
|
* @throws {@link SyntaxError} 模型抽风
|
|
51
58
|
* @throws {@link TypeError} 网络故障
|
|
52
59
|
*/
|
|
53
|
-
stateless(wfctx: InferenceContext, session:
|
|
60
|
+
stateless(wfctx: InferenceContext, session: Session.From<fdm, vdm>): Promise<RoleMessage.Ai.From<fdm, vdm>>;
|
|
54
61
|
/**
|
|
55
62
|
* @throws {@link InferenceTimeout} 推理超时
|
|
56
63
|
* @throws {@link SyntaxError} 模型抽风
|
|
57
64
|
* @throws {@link TypeError} 网络故障
|
|
58
65
|
* @param session mutable
|
|
59
66
|
*/
|
|
60
|
-
stateful(wfctx: InferenceContext, session:
|
|
61
|
-
abstract
|
|
67
|
+
stateful(wfctx: InferenceContext, session: Session.From<fdm, vdm>): Promise<RoleMessage.Ai.From<fdm, vdm>>;
|
|
68
|
+
abstract clone(): Engine<fdm, vdm>;
|
|
69
|
+
protected middlewaresStateless: Middleware.From<fdm, vdm>[];
|
|
70
|
+
useStateless(middleware: Middleware.From<fdm, vdm>): Engine<fdm, vdm>;
|
|
71
|
+
protected middlewaresStateful: Middleware.From<fdm, vdm>[];
|
|
72
|
+
useStateful(middleware: Middleware.From<fdm, vdm>): Engine<fdm, vdm>;
|
|
73
|
+
protected compose(middlewares: Middleware.From<fdm, vdm>[]): Middleware.From<fdm, vdm>;
|
|
62
74
|
/**
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
abstract clone(): Engine<fdm, vdm, userm, aim, devm, session>;
|
|
67
|
-
protected middlewares: Middleware<userm, aim, devm, session>[];
|
|
68
|
-
use(middleware: Middleware<userm, aim, devm, session>): Engine<fdm, vdm, userm, aim, devm, session>;
|
|
69
|
-
protected statefulMiddlewares: Middleware<userm, aim, devm, session>[];
|
|
70
|
-
statefulUse(middleware: Middleware<userm, aim, devm, session>): ReturnType<typeof this['clone']>;
|
|
71
|
-
protected compose(middlewares: Middleware<userm, aim, devm, session>[], i?: number): Middleware<userm, aim, devm, session>;
|
|
75
|
+
* @param session mutable
|
|
76
|
+
*/
|
|
77
|
+
agentloop(wfctx: InferenceContext, session: Session.From<fdm, vdm>, fnm: Function.Map<fdm>, vhm: Verbatim.Handler.Map<vdm>, limit?: number): AsyncGenerator<string, string, void>;
|
|
72
78
|
}
|
|
73
|
-
|
|
74
|
-
(wfctx: InferenceContext, session: session, next: () => Promise<aim>): Promise<aim>;
|
|
79
|
+
class FunctionCallLimitExceeded extends Error {
|
|
75
80
|
}
|
|
76
81
|
interface Options<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> {
|
|
77
82
|
throttle: Throttle;
|
|
78
83
|
endpointSpec: EndpointSpec;
|
|
79
84
|
functionDeclarationMap: fdm;
|
|
80
85
|
verbatimDeclarationMap: vdm;
|
|
86
|
+
structuringChoice?: StructuringChoice;
|
|
81
87
|
providerRetry?: number;
|
|
82
88
|
inferenceRetry?: number;
|
|
83
89
|
}
|
|
90
|
+
interface Create {
|
|
91
|
+
<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(options: Engine.Options<fdm, vdm>): Engine<fdm, vdm>;
|
|
92
|
+
}
|
|
93
|
+
export import Transport = EngineTransportModule.Transport;
|
|
94
|
+
export import StructuringValidator = StructuringValidatorModule.StructuringValidator;
|
|
84
95
|
export import Session = SessionModule.Session;
|
|
85
|
-
export import RoleMessage =
|
|
86
|
-
export import StructuringValidator = ValidationModule.StructuringValidator;
|
|
87
|
-
export import PartsValidator = ValidationModule.PartsValidator;
|
|
88
|
-
export import Transport = TransportModule.Transport;
|
|
96
|
+
export import RoleMessage = MessageModule.RoleMessage;
|
|
89
97
|
}
|
|
90
98
|
export declare class InferenceTimeout extends Error {
|
|
91
99
|
}
|
|
92
|
-
export
|
|
93
|
-
protected response: aim;
|
|
94
|
-
protected rejection: userm;
|
|
95
|
-
constructor(response: aim, rejection: userm, ...rest: ConstructorParameters<typeof SyntaxError>);
|
|
96
|
-
resume(): aim;
|
|
97
|
-
recover(): userm;
|
|
98
|
-
static recover<userm, aim, devm, session extends Engine.Session<userm, aim, devm>>(wfctx: InferenceContext, session: session, next: () => Promise<aim>): Promise<aim>;
|
|
99
|
-
}
|
|
100
|
+
export { Recoverable, Middleware };
|
|
100
101
|
declare global {
|
|
101
102
|
export namespace NodeJS {
|
|
102
103
|
interface ProcessEnv {
|
package/build/engine.js
CHANGED
|
@@ -1,17 +1,25 @@
|
|
|
1
|
+
import { Function } from "./function.js";
|
|
1
2
|
import { Agent, ProxyAgent } from 'undici';
|
|
2
3
|
import { env } from 'node:process';
|
|
3
4
|
import { loggers } from "./telemetry.js";
|
|
4
|
-
import * as
|
|
5
|
+
import * as MessageModule from "./engine/message.js";
|
|
6
|
+
import { PartsValidator } from "./engine/parts-validator.js";
|
|
7
|
+
import { Recoverable } from "./engine/recoverable.js";
|
|
8
|
+
import * as StructuringValidatorModule from "./engine/structuring-validator.js";
|
|
9
|
+
import { StructuringChoice } from "./structuring-choice.js";
|
|
5
10
|
export var Engine;
|
|
6
11
|
(function (Engine) {
|
|
7
12
|
class Instance {
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
providerSpecs;
|
|
14
|
+
inferenceOptions;
|
|
10
15
|
name;
|
|
11
16
|
pricing;
|
|
12
17
|
fdm;
|
|
13
18
|
vdm;
|
|
14
19
|
throttle;
|
|
20
|
+
structuringChoice;
|
|
21
|
+
structuringValidator;
|
|
22
|
+
partsValidator;
|
|
15
23
|
constructor(options) {
|
|
16
24
|
const proxyUrl = options.endpointSpec.proxy || env.https_proxy || env.HTTPS_PROXY;
|
|
17
25
|
const dispatcher = proxyUrl
|
|
@@ -24,14 +32,14 @@ export var Engine;
|
|
|
24
32
|
headersTimeout: 10000,
|
|
25
33
|
bodyTimeout: 0,
|
|
26
34
|
});
|
|
27
|
-
this.
|
|
35
|
+
this.providerSpecs = {
|
|
28
36
|
baseUrl: options.endpointSpec.baseUrl,
|
|
29
37
|
apiKey: options.endpointSpec.apiKey,
|
|
30
38
|
dispatcher,
|
|
31
39
|
retry: options.providerRetry ?? 2,
|
|
32
40
|
};
|
|
33
41
|
this.name = options.endpointSpec.name;
|
|
34
|
-
this.
|
|
42
|
+
this.inferenceOptions = {
|
|
35
43
|
model: options.endpointSpec.model,
|
|
36
44
|
additionalOptions: options.endpointSpec.additionalOptions,
|
|
37
45
|
timeout: options.endpointSpec.timeout,
|
|
@@ -45,15 +53,19 @@ export var Engine;
|
|
|
45
53
|
};
|
|
46
54
|
this.fdm = options.functionDeclarationMap;
|
|
47
55
|
this.vdm = options.verbatimDeclarationMap;
|
|
56
|
+
this.structuringChoice = options.structuringChoice ?? StructuringChoice.AUTO;
|
|
48
57
|
this.throttle = options.throttle;
|
|
58
|
+
this.partsValidator = new PartsValidator();
|
|
59
|
+
this.structuringValidator = new Engine.StructuringValidator({ structuringChoice: this.structuringChoice });
|
|
49
60
|
}
|
|
50
61
|
/**
|
|
62
|
+
* @throws {@link InferenceTimeout} 推理超时
|
|
51
63
|
* @throws {@link SyntaxError} 模型抽风
|
|
52
64
|
* @throws {@link Recoverable} 模型抽风但可恢复
|
|
53
65
|
* @throws {@link TypeError} 网络故障
|
|
54
66
|
*/
|
|
55
67
|
async infer(wfctx, session) {
|
|
56
|
-
const signalTimeout = this.
|
|
68
|
+
const signalTimeout = this.inferenceOptions.timeout ? AbortSignal.timeout(this.inferenceOptions.timeout) : null;
|
|
57
69
|
const signals = [];
|
|
58
70
|
if (signalTimeout)
|
|
59
71
|
signals.push(signalTimeout);
|
|
@@ -81,29 +93,28 @@ export var Engine;
|
|
|
81
93
|
* @throws {@link TypeError} 网络故障
|
|
82
94
|
*/
|
|
83
95
|
async stateless(wfctx, session) {
|
|
84
|
-
const middleware = this.compose(this.
|
|
96
|
+
const middleware = this.compose(this.middlewaresStateless);
|
|
85
97
|
for (let retryProvider = 0, retryInference = 0;;)
|
|
86
98
|
try {
|
|
87
|
-
|
|
88
|
-
return await middleware(wfctx, session, next);
|
|
99
|
+
return await middleware(wfctx, session, () => this.infer(wfctx, session));
|
|
89
100
|
}
|
|
90
101
|
catch (e) {
|
|
91
102
|
if (e instanceof InferenceTimeout) { // 推理超时
|
|
92
|
-
if (retryInference < this.
|
|
103
|
+
if (retryInference < this.inferenceOptions.retry) { }
|
|
93
104
|
else
|
|
94
105
|
throw e;
|
|
95
106
|
loggers.message.warn(e);
|
|
96
107
|
retryInference++;
|
|
97
108
|
}
|
|
98
109
|
else if (e instanceof SyntaxError) { // 模型抽风
|
|
99
|
-
if (retryInference < this.
|
|
110
|
+
if (retryInference < this.inferenceOptions.retry) { }
|
|
100
111
|
else
|
|
101
112
|
throw e;
|
|
102
113
|
loggers.message.warn(e);
|
|
103
114
|
retryInference++;
|
|
104
115
|
}
|
|
105
116
|
else if (e instanceof TypeError) { // 网络故障
|
|
106
|
-
if (retryProvider < this.
|
|
117
|
+
if (retryProvider < this.providerSpecs.retry) { }
|
|
107
118
|
else
|
|
108
119
|
throw e;
|
|
109
120
|
loggers.message.warn(e);
|
|
@@ -120,35 +131,33 @@ export var Engine;
|
|
|
120
131
|
* @param session mutable
|
|
121
132
|
*/
|
|
122
133
|
async stateful(wfctx, session) {
|
|
123
|
-
const middleware = this.compose(this.
|
|
124
|
-
const statefulMiddleware = this.compose(this.statefulMiddlewares);
|
|
134
|
+
const middleware = this.compose(this.middlewaresStateful);
|
|
125
135
|
for (let retryProvider = 0, retryInference = 0;;)
|
|
126
136
|
try {
|
|
127
|
-
const next = () =>
|
|
128
|
-
|
|
129
|
-
const aiMessage = await middleware(wfctx, session, next);
|
|
137
|
+
const next = async () => {
|
|
138
|
+
const aiMessage = await this.infer(wfctx, session);
|
|
130
139
|
session.chatMessages.push(aiMessage);
|
|
131
140
|
return aiMessage;
|
|
132
141
|
};
|
|
133
|
-
return await
|
|
142
|
+
return await middleware(wfctx, session, next);
|
|
134
143
|
}
|
|
135
144
|
catch (e) {
|
|
136
145
|
if (e instanceof InferenceTimeout) { // 推理超时
|
|
137
|
-
if (retryInference < this.
|
|
146
|
+
if (retryInference < this.inferenceOptions.retry) { }
|
|
138
147
|
else
|
|
139
148
|
throw e;
|
|
140
149
|
loggers.message.warn(e);
|
|
141
150
|
retryInference++;
|
|
142
151
|
}
|
|
143
152
|
else if (e instanceof SyntaxError) { // 模型抽风
|
|
144
|
-
if (retryInference < this.
|
|
153
|
+
if (retryInference < this.inferenceOptions.retry) { }
|
|
145
154
|
else
|
|
146
155
|
throw e;
|
|
147
156
|
loggers.message.warn(e);
|
|
148
157
|
retryInference++;
|
|
149
158
|
}
|
|
150
159
|
else if (e instanceof TypeError) { // 网络故障
|
|
151
|
-
if (retryProvider < this.
|
|
160
|
+
if (retryProvider < this.providerSpecs.retry) { }
|
|
152
161
|
else
|
|
153
162
|
throw e;
|
|
154
163
|
loggers.message.warn(e);
|
|
@@ -158,59 +167,86 @@ export var Engine;
|
|
|
158
167
|
throw e;
|
|
159
168
|
}
|
|
160
169
|
}
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
middlewaresStateless = [];
|
|
171
|
+
useStateless(middleware) {
|
|
163
172
|
const engine = this.clone();
|
|
164
|
-
engine.
|
|
173
|
+
engine.middlewaresStateless.push(middleware);
|
|
165
174
|
return engine;
|
|
166
175
|
}
|
|
167
|
-
|
|
168
|
-
|
|
176
|
+
middlewaresStateful = [];
|
|
177
|
+
useStateful(middleware) {
|
|
169
178
|
const engine = this.clone();
|
|
170
|
-
engine.
|
|
179
|
+
engine.middlewaresStateful.push(middleware);
|
|
171
180
|
return engine;
|
|
172
181
|
}
|
|
173
|
-
compose(middlewares
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
+
compose(middlewares) {
|
|
183
|
+
let composed = (wfctx, session, next) => next();
|
|
184
|
+
for (const middleware of middlewares.slice().reverse()) {
|
|
185
|
+
const nextMiddleware = composed;
|
|
186
|
+
composed = (wfctx, session, next) => middleware(wfctx, session, () => nextMiddleware(wfctx, session, next));
|
|
187
|
+
}
|
|
188
|
+
return composed;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @param session mutable
|
|
192
|
+
*/
|
|
193
|
+
async *agentloop(wfctx, session, fnm, vhm, limit = Number.POSITIVE_INFINITY) {
|
|
194
|
+
for (let i = 0; i < limit; i++) {
|
|
195
|
+
const response = await this.stateful(wfctx, session);
|
|
196
|
+
if (response.allChat())
|
|
197
|
+
return response.getChat();
|
|
198
|
+
const pfress = [];
|
|
199
|
+
const pvress = [];
|
|
200
|
+
for (const part of response.getParts()) {
|
|
201
|
+
if (part instanceof Engine.RoleMessage.Ai.Part.Text) {
|
|
202
|
+
const textPart = part;
|
|
203
|
+
yield textPart.text;
|
|
204
|
+
for (const vreq of textPart.vreqs) {
|
|
205
|
+
const vh = vhm[vreq.name];
|
|
206
|
+
pvress.push((async () => new Engine.RoleMessage.User.Part.Text(await vh.call(vhm, vreq.args)))());
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (part instanceof Function.Call) {
|
|
210
|
+
const fcall = part;
|
|
211
|
+
const f = fnm[fcall.name];
|
|
212
|
+
pfress.push((async () => {
|
|
213
|
+
try {
|
|
214
|
+
return Function.Response.Successful.of({
|
|
215
|
+
id: fcall.id,
|
|
216
|
+
name: fcall.name,
|
|
217
|
+
text: await f.call(fnm, fcall.args),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
if (e instanceof Function.Error) { }
|
|
222
|
+
else
|
|
223
|
+
throw e;
|
|
224
|
+
return Function.Response.Failed.of({
|
|
225
|
+
id: fcall.id,
|
|
226
|
+
name: fcall.name,
|
|
227
|
+
error: e.message,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
})());
|
|
231
|
+
}
|
|
232
|
+
else
|
|
233
|
+
throw new Error();
|
|
234
|
+
}
|
|
235
|
+
const fress = await Promise.all(pfress);
|
|
236
|
+
const vress = await Promise.all(pvress);
|
|
237
|
+
session.chatMessages.push(new Engine.RoleMessage.User([...fress, ...vress]));
|
|
238
|
+
}
|
|
239
|
+
throw new Engine.FunctionCallLimitExceeded('Function call limit exceeded.');
|
|
182
240
|
}
|
|
183
241
|
}
|
|
184
242
|
Engine.Instance = Instance;
|
|
185
|
-
|
|
243
|
+
class FunctionCallLimitExceeded extends Error {
|
|
244
|
+
}
|
|
245
|
+
Engine.FunctionCallLimitExceeded = FunctionCallLimitExceeded;
|
|
246
|
+
Engine.StructuringValidator = StructuringValidatorModule.StructuringValidator;
|
|
247
|
+
Engine.RoleMessage = MessageModule.RoleMessage;
|
|
186
248
|
})(Engine || (Engine = {}));
|
|
187
249
|
export class InferenceTimeout extends Error {
|
|
188
250
|
}
|
|
189
|
-
export
|
|
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;
|
|
202
|
-
}
|
|
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
|
-
}
|
|
251
|
+
export { Recoverable };
|
|
216
252
|
//# sourceMappingURL=engine.js.map
|