@zimtsui/typechat 0.0.98 → 0.0.100
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 +10 -6
- 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
package/DEVELOPMENT.md
CHANGED
|
@@ -3,14 +3,9 @@
|
|
|
3
3
|
```mermaid
|
|
4
4
|
classDiagram
|
|
5
5
|
|
|
6
|
-
Engine
|
|
6
|
+
Engine <|-- NativeEngine
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
OpenAIResponsesNativeEngine o--> OpenAIResponsesNativeHelpers
|
|
10
|
-
OpenAIResponsesNativeHelpers o--> OpenAIResponsesHelpers
|
|
11
|
-
|
|
12
|
-
CompatibleEngine <|.. OpenAICompatibleEngine
|
|
13
|
-
OpenAICompatibleEngine o--> OpenAIResponsesCompatibleHelpers
|
|
14
|
-
OpenAIResponsesCompatibleHelpers o--> OpenAIResponsesHelpers
|
|
8
|
+
NativeRoleMessage --|> RoleMessage
|
|
15
9
|
|
|
10
|
+
NativeEngine --> NativeRoleMessage
|
|
16
11
|
```
|
package/README.md
CHANGED
|
@@ -6,188 +6,16 @@ TypeChat 是一个强类型的 LLM 推理服务商 API 适配器。
|
|
|
6
6
|
|
|
7
7
|
## 支持服务商 API 类型
|
|
8
8
|
|
|
9
|
-
- OpenAI
|
|
9
|
+
- OpenAI ChatCompletions
|
|
10
10
|
- OpenAI Responses
|
|
11
11
|
- Google
|
|
12
12
|
- Anthropic
|
|
13
13
|
|
|
14
|
-
## 配置
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// 配置推理服务商 API 接入点
|
|
21
|
-
export const config: Config = {
|
|
22
|
-
endpoints: {
|
|
23
|
-
'gpt-5.4-mini': {
|
|
24
|
-
name: 'GPT-5.4 mini',
|
|
25
|
-
apiType: 'openai-responses',
|
|
26
|
-
baseUrl: 'https://api.openai.com/v1',
|
|
27
|
-
apiKey: process.env.OPENAI_API_KEY!,
|
|
28
|
-
model: 'gpt-5.4-mini',
|
|
29
|
-
},
|
|
30
|
-
'gemini-3-flash': {
|
|
31
|
-
name: 'Gemini 3 Flash',
|
|
32
|
-
apiType: 'google',
|
|
33
|
-
baseUrl: 'https://generativelanguage.googleapis.com',
|
|
34
|
-
apiKey: process.env.GOOGLE_API_KEY!,
|
|
35
|
-
model: 'gemini-3-flash',
|
|
36
|
-
},
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## 对话
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
import { Adaptor, RoleMessage, type Session } from '@zimtsui/typechat';
|
|
45
|
-
import { config } from './config.ts';
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// 创建会话
|
|
49
|
-
const session: Session<never, never> = {
|
|
50
|
-
developerMessage: new RoleMessage.Developer([
|
|
51
|
-
RoleMessage.Part.Text.paragraph('You are a helpful assistant.'),
|
|
52
|
-
]),
|
|
53
|
-
chatMessages: [
|
|
54
|
-
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('Hello!') ]),
|
|
55
|
-
],
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// 选择推理引擎
|
|
59
|
-
const adaptor = Adaptor.create(config);
|
|
60
|
-
const engine = adaptor.makeCompatibleEngine<{}, {}>({
|
|
61
|
-
endpoint: 'gpt-5.4-mini',
|
|
62
|
-
functionDeclarationMap: {},
|
|
63
|
-
verbatimDeclarationMap: {},
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
const response = await engine.stateless({}, session);
|
|
67
|
-
console.log(response.getText());
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
## 智能体
|
|
71
|
-
|
|
72
|
-
```ts
|
|
73
|
-
import { Adaptor, agentloop, RoleMessage, Function, type Session, Structuring } from '@zimtsui/typechat';
|
|
74
|
-
import { Type } from 'typebox';
|
|
75
|
-
import { config } from './config.ts';
|
|
76
|
-
|
|
77
|
-
// 声明函数工具
|
|
78
|
-
const fdm = {
|
|
79
|
-
get_weather: {
|
|
80
|
-
description: '获取指定城市的天气',
|
|
81
|
-
parameters: Type.Object({
|
|
82
|
-
city: Type.String(),
|
|
83
|
-
unit: Type.Optional(Type.Union([Type.Literal('C'), Type.Literal('F')]))
|
|
84
|
-
}),
|
|
85
|
-
},
|
|
86
|
-
submit_result: {
|
|
87
|
-
description: '提交最终结果',
|
|
88
|
-
parameters: Type.Object({
|
|
89
|
-
weather: Type.String(),
|
|
90
|
-
advice: Type.String(),
|
|
91
|
-
}),
|
|
92
|
-
},
|
|
93
|
-
} satisfies Function.Decl.Map.Proto;
|
|
94
|
-
type fdm = typeof fdm;
|
|
95
|
-
type fdu = Function.Decl.From<fdm>;
|
|
96
|
-
|
|
97
|
-
// 实现函数工具
|
|
98
|
-
export class Submission {
|
|
99
|
-
public constructor(public weather: string, public advice: string) {}
|
|
100
|
-
}
|
|
101
|
-
const fnm: Function.Map<fdm> = {
|
|
102
|
-
async get_weather({ city, unit }) {
|
|
103
|
-
const data = { city, unit: unit ?? 'C', temperature: 26, sky: 'sunny' };
|
|
104
|
-
return JSON.stringify(data);
|
|
105
|
-
},
|
|
106
|
-
async submit_result({ weather, advice }) {
|
|
107
|
-
throw new Submission(weather, advice);
|
|
108
|
-
},
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
// 创建会话
|
|
112
|
-
const session: Session<fdu, never> = {
|
|
113
|
-
developerMessage: new RoleMessage.Developer([
|
|
114
|
-
RoleMessage.Part.Text.paragraph('你的工作是为用户查询天气,并给出穿衣建议。调用工具提交最终结果'),
|
|
115
|
-
]),
|
|
116
|
-
chatMessages: [
|
|
117
|
-
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请查询现在北京的天气,并给穿衣建议。') ]),
|
|
118
|
-
],
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
// 选择推理引擎
|
|
122
|
-
const adaptor = Adaptor.create(config);
|
|
123
|
-
const engine = adaptor.makeCompatibleEngine<fdm, {}>({
|
|
124
|
-
endpoint: 'gpt-5.4-mini',
|
|
125
|
-
functionDeclarationMap: fdm,
|
|
126
|
-
verbatimDeclarationMap: {},
|
|
127
|
-
structuringChoice: Structuring.Choice.FCall.REQUIRED,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
// 使用 agentloop 驱动智能体循环,最多 8 轮对话
|
|
131
|
-
try {
|
|
132
|
-
for await (const text of agentloop({}, session, engine, fnm, 8)) console.log(text);
|
|
133
|
-
} catch (e) {
|
|
134
|
-
if (e instanceof Submission) {} else throw e;
|
|
135
|
-
console.log(e.weather);
|
|
136
|
-
console.log(e.advice);
|
|
137
|
-
}
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
### XML 逐字频道
|
|
14
|
+
## [配置](./examples/config.ts)
|
|
15
|
+
## [对话](./examples/chat.ts)
|
|
16
|
+
## [智能体](./examples/agent.ts)
|
|
17
|
+
### [XML 逐字频道](./examples/verbatim.ts)
|
|
141
18
|
|
|
142
19
|
When a LLM outputs structured data in JSON format (e.g., legacy LLM function calling), if there are too many special characters in a string property (e.g., a large LaTeX document, or a complex shell command), the LLM is prone to make mistakes in JSON escaping.
|
|
143
20
|
|
|
144
21
|
XML Verbatim Channel is designed to avoid escaping in LLM messages.
|
|
145
|
-
|
|
146
|
-
```ts
|
|
147
|
-
import { Adaptor, RoleMessage, type Session, Structuring, Verbatim } from '@zimtsui/typechat';
|
|
148
|
-
import Assets from '@zimtsui/typechat/assets';
|
|
149
|
-
import * as Codec from '@zimtsui/typechat/codec';
|
|
150
|
-
import { config } from './config.ts';
|
|
151
|
-
import { MIMEType } from 'whatwg-mimetype';
|
|
152
|
-
|
|
153
|
-
// 声明 XML Verbatim 频道
|
|
154
|
-
const vdm = {
|
|
155
|
-
bash: {
|
|
156
|
-
description: '执行 Bash 命令',
|
|
157
|
-
parameters: {
|
|
158
|
-
command: {
|
|
159
|
-
description: 'Bash 命令',
|
|
160
|
-
mimeType: new MIMEType('text/plain'),
|
|
161
|
-
required: true as const,
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
},
|
|
165
|
-
} satisfies Verbatim.Decl.Map.Proto;
|
|
166
|
-
type vdm = typeof vdm;
|
|
167
|
-
type vdu = Verbatim.Decl.From<vdm>;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
// 创建会话
|
|
171
|
-
const session: Session<never, vdu> = {
|
|
172
|
-
developerMessage: new RoleMessage.Developer([
|
|
173
|
-
RoleMessage.Part.Text.paragraph(Assets.verbatim.instruction),
|
|
174
|
-
RoleMessage.Part.Text.paragraph('# Available Verbatim Channels'),
|
|
175
|
-
RoleMessage.Part.Text.paragraph(Codec.Declarations.encode(vdm)),
|
|
176
|
-
]),
|
|
177
|
-
chatMessages: [
|
|
178
|
-
new RoleMessage.User([ RoleMessage.Part.Text.paragraph('请使用 Bash 命令查询当前系统时间。') ]),
|
|
179
|
-
],
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
// 选择推理引擎
|
|
183
|
-
const adaptor = Adaptor.create(config);
|
|
184
|
-
const engine = adaptor.makeCompatibleEngine<{}, vdm>({
|
|
185
|
-
endpoint: 'gpt-5.4-mini',
|
|
186
|
-
functionDeclarationMap: {},
|
|
187
|
-
verbatimDeclarationMap: vdm,
|
|
188
|
-
structuringChoice: Structuring.Choice.VRequest.ANYONE,
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
const response = await engine.stateless({}, session);
|
|
192
|
-
console.log(response.getOnlyVerbatimRequest().args.command);
|
|
193
|
-
```
|
|
@@ -29,19 +29,21 @@ The LLM system/user message is expected to declare all available XML Verbatim Ch
|
|
|
29
29
|
You can make a request through a channel in the form of
|
|
30
30
|
|
|
31
31
|
<verbatim:request name="NAME OF CHANNEL">
|
|
32
|
-
<verbatim:parameter name="NAME OF PARAMETER 1"><![CDATA[ARGUMENT OF THIS PARAMETER]]></verbatim:parameter>
|
|
33
|
-
<verbatim:parameter name="NAME OF PARAMETER 2"><![CDATA[ARGUMENT OF THIS PARAMETER]]></verbatim:parameter>
|
|
32
|
+
<verbatim:parameter name="NAME OF PARAMETER 1"><![CDATA[VERBATIM ARGUMENT OF THIS PARAMETER]]></verbatim:parameter>
|
|
33
|
+
<verbatim:parameter name="NAME OF PARAMETER 2"><![CDATA[VERBATIM ARGUMENT OF THIS PARAMETER]]></verbatim:parameter>
|
|
34
34
|
</verbatim:request>
|
|
35
35
|
|
|
36
36
|
- The only attribute of <verbatim:request> is `name`. Additional attributes will be ignored.
|
|
37
37
|
- The only attribute of <verbatim:parameter> is `name`. Additional attributes will be ignored.
|
|
38
|
-
- All arguments must be wrapped in CDATA.
|
|
38
|
+
- All arguments must be wrapped in CDATA. The CDATA sections will be extracted not by standard XML parsers, but by regular expressions instead. Therefore, they can directly contain `]]>`, which is not allowed in standard XML.
|
|
39
39
|
|
|
40
40
|
## Response from Channels
|
|
41
41
|
|
|
42
|
-
The LLM user message may contain the responses
|
|
42
|
+
The LLM user message may contain the responses to your request in the form of
|
|
43
43
|
|
|
44
|
-
<verbatim:response name="NAME OF CHANNEL"><![CDATA[RESPONSE]]></verbatim:response>
|
|
44
|
+
<verbatim:response name="NAME OF CHANNEL"><![CDATA[VERBATIM RESPONSE]]></verbatim:response>
|
|
45
|
+
|
|
46
|
+
The CDATA section may directly contain `]]>`, which is not allowed in standard XML.
|
|
45
47
|
|
|
46
48
|
Not all requests have a response.
|
|
47
49
|
|
|
@@ -49,7 +51,9 @@ Not all requests have a response.
|
|
|
49
51
|
|
|
50
52
|
The LLM system/user message may contain verbatim quotations in the form of
|
|
51
53
|
|
|
52
|
-
<verbatim:quotation author="AUTHOR OF THIS QUOTATION"><![CDATA[QUOTATION]]></verbatim:quotation>
|
|
54
|
+
<verbatim:quotation author="AUTHOR OF THIS QUOTATION"><![CDATA[VERBATIM QUOTATION]]></verbatim:quotation>
|
|
55
|
+
|
|
56
|
+
The CDATA section may directly contain `]]>`, which is not allowed in standard XML.
|
|
53
57
|
|
|
54
58
|
The attribute `author` is optional.
|
|
55
59
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<verbatim:response name="{{this.name}}" mime-type="{{XmlAttr this.mimeType}}"><![CDATA[{{{this.text}}}]]></verbatim:response>
|
|
1
|
+
<verbatim:response name="{{AssertXmlAttr this.name}}" mime-type="{{XmlAttr this.mimeType}}"><![CDATA[{{{this.text}}}]]></verbatim:response>
|
package/build/adaptor.d.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
import { Config } from './config.ts';
|
|
2
2
|
import { Function } from './function.ts';
|
|
3
|
-
import { type CompatibleEngine } from './compatible-engine.ts';
|
|
4
3
|
import { Throttle } from './throttle.ts';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { GoogleEngine } from './engines/google.ts';
|
|
5
|
+
import { OpenAIResponsesEngine } from './engines/openai-responses.ts';
|
|
7
6
|
import type { Verbatim } from './verbatim.ts';
|
|
7
|
+
import { Engine } from './engine.ts';
|
|
8
8
|
export declare class Adaptor {
|
|
9
9
|
config: Config;
|
|
10
10
|
static create(config: Config): Adaptor;
|
|
11
11
|
protected throttles: Map<string, Throttle>;
|
|
12
12
|
protected constructor(config: Config);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
makeEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.Params<fdm, vdm>): Engine<fdm, vdm>;
|
|
14
|
+
makeGoogleEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.GoogleParams<fdm, vdm>): GoogleEngine<fdm, vdm>;
|
|
15
|
+
makeOpenAIResponsesEngine<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto>(adaptorOptions: Adaptor.OpenAIResponsesParams<fdm, vdm>): OpenAIResponsesEngine<fdm, vdm>;
|
|
16
16
|
}
|
|
17
17
|
export declare namespace Adaptor {
|
|
18
|
-
interface
|
|
18
|
+
interface Params<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> extends Omit<Engine.Options<fdm, vdm>, 'endpointSpec' | 'throttle'> {
|
|
19
19
|
endpoint: string;
|
|
20
20
|
}
|
|
21
|
-
interface
|
|
21
|
+
interface GoogleParams<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> extends Omit<GoogleEngine.Options<fdm, vdm>, 'endpointSpec' | 'throttle'> {
|
|
22
22
|
endpoint: string;
|
|
23
23
|
}
|
|
24
|
-
interface
|
|
24
|
+
interface OpenAIResponsesParams<in out fdm extends Function.Decl.Map.Proto, in out vdm extends Verbatim.Decl.Map.Proto> extends Omit<OpenAIResponsesEngine.Options<fdm, vdm>, 'endpointSpec' | 'throttle'> {
|
|
25
25
|
endpoint: string;
|
|
26
26
|
}
|
|
27
27
|
}
|
package/build/adaptor.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import { Throttle } from "./throttle.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { GoogleNativeEngine } from "./native-engines.d/google.js";
|
|
7
|
-
import { OpenAIChatCompletionsCompatibleEngine } from "./compatible-engine.d/openai-chatcompletions.js";
|
|
2
|
+
import { GoogleEngine } from "./engines/google.js";
|
|
3
|
+
import { OpenAIResponsesEngine } from "./engines/openai-responses.js";
|
|
4
|
+
import { OpenAIChatCompletionsEngine } from "./engines/openai-chatcompletions.js";
|
|
5
|
+
import { AnthropicEngine } from "./engines/anthropic.js";
|
|
8
6
|
export class Adaptor {
|
|
9
7
|
config;
|
|
10
8
|
static create(config) {
|
|
@@ -18,7 +16,7 @@ export class Adaptor {
|
|
|
18
16
|
this.throttles.set(endpointId, new Throttle(rpm));
|
|
19
17
|
}
|
|
20
18
|
}
|
|
21
|
-
|
|
19
|
+
makeEngine(adaptorOptions) {
|
|
22
20
|
const endpointSpec = this.config.endpoints[adaptorOptions.endpoint];
|
|
23
21
|
if (endpointSpec) { }
|
|
24
22
|
else
|
|
@@ -33,19 +31,19 @@ export class Adaptor {
|
|
|
33
31
|
throttle,
|
|
34
32
|
};
|
|
35
33
|
if (endpointSpec.apiType === 'openai-responses')
|
|
36
|
-
return
|
|
34
|
+
return OpenAIResponsesEngine.create(options);
|
|
37
35
|
else if (endpointSpec.apiType === 'google')
|
|
38
|
-
return
|
|
36
|
+
return GoogleEngine.create(options);
|
|
39
37
|
else if (endpointSpec.apiType === 'anthropic')
|
|
40
|
-
return
|
|
38
|
+
return AnthropicEngine.create(options);
|
|
41
39
|
else if (endpointSpec.apiType === 'openai-chatcompletions')
|
|
42
|
-
return
|
|
40
|
+
return OpenAIChatCompletionsEngine.create(options);
|
|
43
41
|
else
|
|
44
42
|
throw new Error();
|
|
45
43
|
}
|
|
46
|
-
|
|
44
|
+
makeGoogleEngine(adaptorOptions) {
|
|
47
45
|
const endpointSpec = this.config.endpoints[adaptorOptions.endpoint];
|
|
48
|
-
if (endpointSpec?.apiType === '
|
|
46
|
+
if (endpointSpec?.apiType === 'google') { }
|
|
49
47
|
else
|
|
50
48
|
throw new Error();
|
|
51
49
|
const throttle = this.throttles.get(adaptorOptions.endpoint);
|
|
@@ -57,11 +55,11 @@ export class Adaptor {
|
|
|
57
55
|
endpointSpec,
|
|
58
56
|
throttle,
|
|
59
57
|
};
|
|
60
|
-
return new
|
|
58
|
+
return new GoogleEngine.Instance(options);
|
|
61
59
|
}
|
|
62
|
-
|
|
60
|
+
makeOpenAIResponsesEngine(adaptorOptions) {
|
|
63
61
|
const endpointSpec = this.config.endpoints[adaptorOptions.endpoint];
|
|
64
|
-
if (endpointSpec?.apiType === '
|
|
62
|
+
if (endpointSpec?.apiType === 'openai-responses') { }
|
|
65
63
|
else
|
|
66
64
|
throw new Error();
|
|
67
65
|
const throttle = this.throttles.get(adaptorOptions.endpoint);
|
|
@@ -73,7 +71,7 @@ export class Adaptor {
|
|
|
73
71
|
endpointSpec,
|
|
74
72
|
throttle,
|
|
75
73
|
};
|
|
76
|
-
return new
|
|
74
|
+
return new OpenAIResponsesEngine.Instance(options);
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
77
|
//# sourceMappingURL=adaptor.js.map
|
package/build/adaptor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"adaptor.js","sourceRoot":"","sources":["../src/adaptor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAClF,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAKzD,MAAM,OAAO,OAAO;IAMa;IALtB,MAAM,CAAC,MAAM,CAAC,MAAc;QAC/B,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;IAES,SAAS,GAAG,IAAI,GAAG,EAAoB,CAAC;IAClD,YAA6B,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;QACvC,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;YAC7C,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,CAAE,CAAC,GAAG,IAAI,MAAM,CAAC,iBAAiB,CAAC;YAC/E,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,UAAU,EAAE,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;QACtD,CAAC;IACL,CAAC;IAEM,UAAU,CAGf,cAAwC;QACtC,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QACpE,IAAI,YAAY,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QAC5C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,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AuD;QACrD,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QACpE,IAAI,YAAY,EAAE,OAAO,KAAK,kBAAkB,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QAC5E,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAC7D,IAAI,QAAQ,EAAE,CAAC,CAAA,CAAC;;YAAM,MAAM,IAAI,KAAK,EAAE,CAAC;QACxC,MAAM,OAAO,GAA4C;YACrD,GAAG,cAAc;YACjB,YAAY;YACZ,QAAQ;SACX,CAAC;QACF,OAAO,IAAI,qBAAqB,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IACvD,CAAC;CACJ"}
|
package/build/endpoint-spec.d.ts
CHANGED
package/build/endpoint-spec.js
CHANGED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Function } from '../function.ts';
|
|
2
|
+
import { Verbatim } from '../verbatim.ts';
|
|
3
|
+
declare const NOMINAL: unique symbol;
|
|
4
|
+
export declare namespace RoleMessage {
|
|
5
|
+
class Developer {
|
|
6
|
+
protected parts: unknown[];
|
|
7
|
+
protected [NOMINAL]: never;
|
|
8
|
+
constructor(parts: unknown[]);
|
|
9
|
+
allTextParts(): boolean;
|
|
10
|
+
getParts(): unknown[];
|
|
11
|
+
getOnlyTextParts(): RoleMessage.Developer.Part.Text[];
|
|
12
|
+
getTextParts(): RoleMessage.Developer.Part.Text[];
|
|
13
|
+
getText(): string;
|
|
14
|
+
}
|
|
15
|
+
namespace Developer {
|
|
16
|
+
namespace Part {
|
|
17
|
+
class Text {
|
|
18
|
+
text: string;
|
|
19
|
+
protected [NOMINAL]: never;
|
|
20
|
+
static paragraph(text: string): Text;
|
|
21
|
+
constructor(text: string);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
class Ai<out fdu extends Function.Decl.Proto, out vdu extends Verbatim.Decl.Proto> {
|
|
26
|
+
protected parts: unknown[];
|
|
27
|
+
protected [NOMINAL]: never;
|
|
28
|
+
constructor(parts: unknown[]);
|
|
29
|
+
getParts(): unknown[];
|
|
30
|
+
allChat(): boolean;
|
|
31
|
+
getTextParts(): RoleMessage.Ai.Part.Text<vdu>[];
|
|
32
|
+
getChat(): string;
|
|
33
|
+
getText(): string;
|
|
34
|
+
getFunctionCalls(): Function.Call.Of<fdu>[];
|
|
35
|
+
getVerbatimRequests(): Verbatim.Request.Of<vdu>[];
|
|
36
|
+
getOnlyFunctionCall(): Function.Call.Of<fdu>;
|
|
37
|
+
getOnlyVerbatimRequest(): Verbatim.Request.Of<vdu>;
|
|
38
|
+
}
|
|
39
|
+
namespace Ai {
|
|
40
|
+
type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = RoleMessage.Ai<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
|
|
41
|
+
namespace Part {
|
|
42
|
+
class Text<out vdu extends Verbatim.Decl.Proto> {
|
|
43
|
+
text: string;
|
|
44
|
+
vreqs: Verbatim.Request.Of<vdu>[];
|
|
45
|
+
protected [NOMINAL]: never;
|
|
46
|
+
static paragraph(text: string): Text<never>;
|
|
47
|
+
constructor(text: string, vreqs: Verbatim.Request.Of<vdu>[]);
|
|
48
|
+
}
|
|
49
|
+
namespace Text {
|
|
50
|
+
type From<vdm extends Verbatim.Decl.Map.Proto> = Text<Verbatim.Decl.From<vdm>>;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
class User<out fdu extends Function.Decl.Proto> {
|
|
55
|
+
protected parts: unknown[];
|
|
56
|
+
protected [NOMINAL]: never;
|
|
57
|
+
constructor(parts: unknown[]);
|
|
58
|
+
getParts(): unknown[];
|
|
59
|
+
getFunctionResponses(): Function.Response.Of<fdu>[];
|
|
60
|
+
getOnlyFunctionResponse(): Function.Response.Of<fdu>;
|
|
61
|
+
getTextParts(): RoleMessage.User.Part.Text[];
|
|
62
|
+
}
|
|
63
|
+
namespace User {
|
|
64
|
+
type From<fdm extends Function.Decl.Map.Proto> = RoleMessage.User<Function.Decl.From<fdm>>;
|
|
65
|
+
namespace Part {
|
|
66
|
+
class Text {
|
|
67
|
+
text: string;
|
|
68
|
+
protected [NOMINAL]: never;
|
|
69
|
+
static paragraph(text: string): Text;
|
|
70
|
+
constructor(text: string);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Function } from "../function.js";
|
|
2
|
+
const NOMINAL = Symbol();
|
|
3
|
+
export var RoleMessage;
|
|
4
|
+
(function (RoleMessage) {
|
|
5
|
+
class Developer {
|
|
6
|
+
parts;
|
|
7
|
+
constructor(parts) {
|
|
8
|
+
this.parts = parts;
|
|
9
|
+
}
|
|
10
|
+
allTextParts() {
|
|
11
|
+
return this.parts.every(part => part instanceof RoleMessage.Developer.Part.Text);
|
|
12
|
+
}
|
|
13
|
+
getParts() {
|
|
14
|
+
return this.parts;
|
|
15
|
+
}
|
|
16
|
+
getOnlyTextParts() {
|
|
17
|
+
if (this.allTextParts()) { }
|
|
18
|
+
else
|
|
19
|
+
throw new Error();
|
|
20
|
+
return this.getTextParts();
|
|
21
|
+
}
|
|
22
|
+
getTextParts() {
|
|
23
|
+
const textParts = [];
|
|
24
|
+
for (const part of this.parts)
|
|
25
|
+
if (part instanceof RoleMessage.Developer.Part.Text) {
|
|
26
|
+
const textPart = part;
|
|
27
|
+
textParts.push(textPart);
|
|
28
|
+
}
|
|
29
|
+
return textParts;
|
|
30
|
+
}
|
|
31
|
+
getText() {
|
|
32
|
+
return this.getTextParts().map(part => part.text).join('');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
RoleMessage.Developer = Developer;
|
|
36
|
+
(function (Developer) {
|
|
37
|
+
let Part;
|
|
38
|
+
(function (Part) {
|
|
39
|
+
class Text {
|
|
40
|
+
text;
|
|
41
|
+
static paragraph(text) {
|
|
42
|
+
return new Text(text.trimEnd() + '\n\n');
|
|
43
|
+
}
|
|
44
|
+
constructor(text) {
|
|
45
|
+
this.text = text;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
Part.Text = Text;
|
|
49
|
+
})(Part = Developer.Part || (Developer.Part = {}));
|
|
50
|
+
})(Developer = RoleMessage.Developer || (RoleMessage.Developer = {}));
|
|
51
|
+
class Ai {
|
|
52
|
+
parts;
|
|
53
|
+
constructor(parts) {
|
|
54
|
+
this.parts = parts;
|
|
55
|
+
}
|
|
56
|
+
getParts() {
|
|
57
|
+
return this.parts;
|
|
58
|
+
}
|
|
59
|
+
allChat() {
|
|
60
|
+
return this.parts.every(part => part instanceof RoleMessage.Ai.Part.Text && !part.vreqs.length);
|
|
61
|
+
}
|
|
62
|
+
getTextParts() {
|
|
63
|
+
return this.parts.filter(part => part instanceof RoleMessage.Ai.Part.Text);
|
|
64
|
+
}
|
|
65
|
+
getChat() {
|
|
66
|
+
return this.getTextParts().filter(part => !part.vreqs.length).map(part => part.text).join('');
|
|
67
|
+
}
|
|
68
|
+
getText() {
|
|
69
|
+
return this.getTextParts().map(part => part.text).join('');
|
|
70
|
+
}
|
|
71
|
+
getFunctionCalls() {
|
|
72
|
+
const fcalls = [];
|
|
73
|
+
for (const part of this.parts)
|
|
74
|
+
if (part instanceof Function.Call) {
|
|
75
|
+
const fcall = part;
|
|
76
|
+
fcalls.push(fcall);
|
|
77
|
+
}
|
|
78
|
+
return fcalls;
|
|
79
|
+
}
|
|
80
|
+
getVerbatimRequests() {
|
|
81
|
+
return this.getTextParts().flatMap(part => part.vreqs);
|
|
82
|
+
}
|
|
83
|
+
getOnlyFunctionCall() {
|
|
84
|
+
const fcalls = this.getFunctionCalls();
|
|
85
|
+
if (fcalls.length === 1) { }
|
|
86
|
+
else
|
|
87
|
+
throw new Error();
|
|
88
|
+
return fcalls[0];
|
|
89
|
+
}
|
|
90
|
+
getOnlyVerbatimRequest() {
|
|
91
|
+
const vreqs = this.getVerbatimRequests();
|
|
92
|
+
if (vreqs.length === 1) { }
|
|
93
|
+
else
|
|
94
|
+
throw new Error();
|
|
95
|
+
return vreqs[0];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
RoleMessage.Ai = Ai;
|
|
99
|
+
(function (Ai) {
|
|
100
|
+
let Part;
|
|
101
|
+
(function (Part) {
|
|
102
|
+
class Text {
|
|
103
|
+
text;
|
|
104
|
+
vreqs;
|
|
105
|
+
static paragraph(text) {
|
|
106
|
+
return new RoleMessage.Ai.Part.Text(text.trimEnd() + '\n\n', []);
|
|
107
|
+
}
|
|
108
|
+
constructor(text, vreqs) {
|
|
109
|
+
this.text = text;
|
|
110
|
+
this.vreqs = vreqs;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
Part.Text = Text;
|
|
114
|
+
})(Part = Ai.Part || (Ai.Part = {}));
|
|
115
|
+
})(Ai = RoleMessage.Ai || (RoleMessage.Ai = {}));
|
|
116
|
+
class User {
|
|
117
|
+
parts;
|
|
118
|
+
constructor(parts) {
|
|
119
|
+
this.parts = parts;
|
|
120
|
+
}
|
|
121
|
+
getParts() {
|
|
122
|
+
return this.parts;
|
|
123
|
+
}
|
|
124
|
+
getFunctionResponses() {
|
|
125
|
+
const fress = [];
|
|
126
|
+
for (const part of this.parts)
|
|
127
|
+
if (part instanceof Function.Response) {
|
|
128
|
+
const fres = part;
|
|
129
|
+
fress.push(fres);
|
|
130
|
+
}
|
|
131
|
+
return fress;
|
|
132
|
+
}
|
|
133
|
+
getOnlyFunctionResponse() {
|
|
134
|
+
if (this.parts.length === 1) { }
|
|
135
|
+
else
|
|
136
|
+
throw new Error();
|
|
137
|
+
const part = this.parts[0];
|
|
138
|
+
if (part instanceof Function.Response) { }
|
|
139
|
+
else
|
|
140
|
+
throw new Error();
|
|
141
|
+
const fres = part;
|
|
142
|
+
return fres;
|
|
143
|
+
}
|
|
144
|
+
getTextParts() {
|
|
145
|
+
return this.parts.filter(part => part instanceof RoleMessage.User.Part.Text);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
RoleMessage.User = User;
|
|
149
|
+
(function (User) {
|
|
150
|
+
let Part;
|
|
151
|
+
(function (Part) {
|
|
152
|
+
class Text {
|
|
153
|
+
text;
|
|
154
|
+
static paragraph(text) {
|
|
155
|
+
return new RoleMessage.User.Part.Text(text.trimEnd() + '\n\n');
|
|
156
|
+
}
|
|
157
|
+
constructor(text) {
|
|
158
|
+
this.text = text;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
Part.Text = Text;
|
|
162
|
+
})(Part = User.Part || (User.Part = {}));
|
|
163
|
+
})(User = RoleMessage.User || (RoleMessage.User = {}));
|
|
164
|
+
})(RoleMessage || (RoleMessage = {}));
|
|
165
|
+
//# sourceMappingURL=message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message.js","sourceRoot":"","sources":["../../src/engine/message.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAG1C,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC;AAGzB,MAAM,KAAW,WAAW,CAkK3B;AAlKD,WAAiB,WAAW;IAExB,MAAa,SAAS;QAGW;QAA7B,YAA6B,KAAgB;YAAhB,UAAK,GAAL,KAAK,CAAW;QAAG,CAAC;QAC1C,YAAY;YACf,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,YAAY,WAAW,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrF,CAAC;QACM,QAAQ;YACX,OAAO,IAAI,CAAC,KAAK,CAAC;QACtB,CAAC;QACM,gBAAgB;YACnB,IAAI,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC,CAAA,CAAC;;gBAAM,MAAM,IA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
|
@@ -0,0 +1,11 @@
|
|
|
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 interface Middleware<in out fdu extends Function.Decl.Proto, in out vdu extends Verbatim.Decl.Proto> {
|
|
7
|
+
(wfctx: InferenceContext, session: Session<fdu, vdu>, next: () => Promise<RoleMessage.Ai<fdu, vdu>>): Promise<RoleMessage.Ai<fdu, vdu>>;
|
|
8
|
+
}
|
|
9
|
+
export declare namespace Middleware {
|
|
10
|
+
type From<fdm extends Function.Decl.Map.Proto, vdm extends Verbatim.Decl.Map.Proto> = Middleware<Function.Decl.From<fdm>, Verbatim.Decl.From<vdm>>;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=middleware.js.map
|