@agentscope-ai/agentscope 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/index.d.mts +234 -0
- package/dist/agent/index.d.ts +234 -0
- package/dist/agent/index.js +1412 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/index.mjs +1375 -0
- package/dist/agent/index.mjs.map +1 -0
- package/dist/base-BOx3UzOl.d.mts +41 -0
- package/dist/base-BoIps2RL.d.ts +41 -0
- package/dist/base-C7jwyH4Z.d.mts +52 -0
- package/dist/base-Cwi4bjze.d.ts +127 -0
- package/dist/base-DYlBMCy_.d.mts +127 -0
- package/dist/base-NX-knWOv.d.ts +52 -0
- package/dist/block-VsnHrllL.d.mts +48 -0
- package/dist/block-VsnHrllL.d.ts +48 -0
- package/dist/event/index.d.mts +181 -0
- package/dist/event/index.d.ts +181 -0
- package/dist/event/index.js +58 -0
- package/dist/event/index.js.map +1 -0
- package/dist/event/index.mjs +33 -0
- package/dist/event/index.mjs.map +1 -0
- package/dist/formatter/index.d.mts +187 -0
- package/dist/formatter/index.d.ts +187 -0
- package/dist/formatter/index.js +647 -0
- package/dist/formatter/index.js.map +1 -0
- package/dist/formatter/index.mjs +616 -0
- package/dist/formatter/index.mjs.map +1 -0
- package/dist/index-BTJDlKvQ.d.mts +195 -0
- package/dist/index-BcatlwXQ.d.ts +195 -0
- package/dist/index-CAxQAkiP.d.mts +21 -0
- package/dist/index-CAxQAkiP.d.ts +21 -0
- package/dist/mcp/index.d.mts +9 -0
- package/dist/mcp/index.d.ts +9 -0
- package/dist/mcp/index.js +432 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/index.mjs +408 -0
- package/dist/mcp/index.mjs.map +1 -0
- package/dist/message/index.d.mts +10 -0
- package/dist/message/index.d.ts +10 -0
- package/dist/message/index.js +67 -0
- package/dist/message/index.js.map +1 -0
- package/dist/message/index.mjs +37 -0
- package/dist/message/index.mjs.map +1 -0
- package/dist/message-CkN21KaY.d.mts +99 -0
- package/dist/message-CzLeTlua.d.ts +99 -0
- package/dist/model/index.d.mts +377 -0
- package/dist/model/index.d.ts +377 -0
- package/dist/model/index.js +1880 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model/index.mjs +1849 -0
- package/dist/model/index.mjs.map +1 -0
- package/dist/storage/index.d.mts +68 -0
- package/dist/storage/index.d.ts +68 -0
- package/dist/storage/index.js +250 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/index.mjs +212 -0
- package/dist/storage/index.mjs.map +1 -0
- package/dist/tool/index.d.mts +311 -0
- package/dist/tool/index.d.ts +311 -0
- package/dist/tool/index.js +1494 -0
- package/dist/tool/index.js.map +1 -0
- package/dist/tool/index.mjs +1447 -0
- package/dist/tool/index.mjs.map +1 -0
- package/dist/toolkit-CEpulFi0.d.ts +99 -0
- package/dist/toolkit-CGEZSZPa.d.mts +99 -0
- package/jest.config.js +11 -0
- package/package.json +92 -0
- package/src/_utils/common.ts +104 -0
- package/src/_utils/index.ts +1 -0
- package/src/agent/agent-base.ts +0 -0
- package/src/agent/agent.test.ts +1028 -0
- package/src/agent/agent.ts +1032 -0
- package/src/agent/index.ts +2 -0
- package/src/agent/interfaces.ts +23 -0
- package/src/agent/test-compression.ts +72 -0
- package/src/event/index.ts +250 -0
- package/src/formatter/base.ts +133 -0
- package/src/formatter/dashscope-chat-formatter.test.ts +372 -0
- package/src/formatter/dashscope-chat-formatter.ts +163 -0
- package/src/formatter/deepseek-chat-formatter.ts +130 -0
- package/src/formatter/index.ts +5 -0
- package/src/formatter/ollama-chat-formatter.ts +67 -0
- package/src/formatter/openai-chat-formatter.test.ts +263 -0
- package/src/formatter/openai-chat-formatter.ts +301 -0
- package/src/formatter/openai.md +767 -0
- package/src/mcp/base.ts +114 -0
- package/src/mcp/http.test.ts +303 -0
- package/src/mcp/http.ts +224 -0
- package/src/mcp/index.ts +2 -0
- package/src/mcp/stdio.test.ts +91 -0
- package/src/mcp/stdio.ts +119 -0
- package/src/message/block.ts +60 -0
- package/src/message/enums.ts +4 -0
- package/src/message/index.ts +12 -0
- package/src/message/message.test.ts +80 -0
- package/src/message/message.ts +131 -0
- package/src/model/base.ts +226 -0
- package/src/model/dashscope-model.test.ts +335 -0
- package/src/model/dashscope-model.ts +441 -0
- package/src/model/deepseek-model.test.ts +279 -0
- package/src/model/deepseek-model.ts +401 -0
- package/src/model/index.ts +7 -0
- package/src/model/ollama-model.test.ts +307 -0
- package/src/model/ollama-model.ts +356 -0
- package/src/model/openai-model.ts +327 -0
- package/src/model/response.ts +22 -0
- package/src/model/usage.ts +12 -0
- package/src/storage/base.ts +52 -0
- package/src/storage/file-system.test.ts +587 -0
- package/src/storage/file-system.ts +269 -0
- package/src/storage/index.ts +2 -0
- package/src/tool/base.ts +23 -0
- package/src/tool/bash.test.ts +174 -0
- package/src/tool/bash.ts +152 -0
- package/src/tool/edit.test.ts +83 -0
- package/src/tool/edit.ts +95 -0
- package/src/tool/glob.test.ts +63 -0
- package/src/tool/glob.ts +166 -0
- package/src/tool/grep.test.ts +74 -0
- package/src/tool/grep.ts +256 -0
- package/src/tool/index.ts +10 -0
- package/src/tool/read.test.ts +77 -0
- package/src/tool/read.ts +117 -0
- package/src/tool/response.ts +82 -0
- package/src/tool/task.test.ts +299 -0
- package/src/tool/task.ts +399 -0
- package/src/tool/toolkit.test.ts +636 -0
- package/src/tool/toolkit.ts +601 -0
- package/src/tool/write.test.ts +52 -0
- package/src/tool/write.ts +57 -0
- package/src/type/index.ts +52 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.cjs.json +11 -0
- package/tsconfig.esm.json +10 -0
- package/tsconfig.json +14 -0
- package/tsup.config.ts +20 -0
- package/typedoc.json +52 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { OpenAI } from 'openai';
|
|
2
|
+
import {
|
|
3
|
+
ChatCompletionMessageParam,
|
|
4
|
+
ChatCompletionToolChoiceOption,
|
|
5
|
+
} from 'openai/resources/chat/completions';
|
|
6
|
+
|
|
7
|
+
import { DataBlock, TextBlock, ThinkingBlock, ToolCallBlock } from '../message';
|
|
8
|
+
import { ToolChoice, ToolSchema } from '../type';
|
|
9
|
+
import { ChatModelBase, ChatModelOptions, ChatModelRequestOptions } from './base';
|
|
10
|
+
import { ChatResponse } from './response';
|
|
11
|
+
import { ChatUsage } from './usage';
|
|
12
|
+
import { OpenAIChatFormatter } from '../formatter';
|
|
13
|
+
|
|
14
|
+
interface OpenAIChatModelOptions extends ChatModelOptions {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
presetGenParams?: Record<string, unknown>;
|
|
17
|
+
baseURL?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The OpenAI API chat model.
|
|
22
|
+
*/
|
|
23
|
+
export class OpenAIChatModel extends ChatModelBase {
|
|
24
|
+
protected client: OpenAI;
|
|
25
|
+
protected presetGenParams: Record<string, unknown> | undefined;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Initializes a new instance of the OpenAIChatModel class.
|
|
29
|
+
* @param options
|
|
30
|
+
* @param options.modelName
|
|
31
|
+
* @param options.apiKey
|
|
32
|
+
* @param options.stream
|
|
33
|
+
* @param options.maxRetries
|
|
34
|
+
* @param options.fallbackModelName
|
|
35
|
+
* @param options.presetGenParams
|
|
36
|
+
* @param options.baseURL
|
|
37
|
+
* @param options.formatter
|
|
38
|
+
*/
|
|
39
|
+
constructor({
|
|
40
|
+
modelName,
|
|
41
|
+
apiKey,
|
|
42
|
+
stream = true,
|
|
43
|
+
maxRetries = 3,
|
|
44
|
+
fallbackModelName,
|
|
45
|
+
presetGenParams,
|
|
46
|
+
baseURL,
|
|
47
|
+
formatter,
|
|
48
|
+
}: OpenAIChatModelOptions) {
|
|
49
|
+
// If no formatter is provided, create a default OpenAIChatFormatter
|
|
50
|
+
const defaultFormatter = formatter || new OpenAIChatFormatter();
|
|
51
|
+
super({
|
|
52
|
+
modelName,
|
|
53
|
+
stream,
|
|
54
|
+
maxRetries,
|
|
55
|
+
fallbackModelName,
|
|
56
|
+
formatter: defaultFormatter,
|
|
57
|
+
} as ChatModelOptions);
|
|
58
|
+
|
|
59
|
+
this.client = new OpenAI({
|
|
60
|
+
apiKey: apiKey,
|
|
61
|
+
baseURL,
|
|
62
|
+
});
|
|
63
|
+
this.presetGenParams = presetGenParams;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Calls the OpenAI API with the given parameters.
|
|
68
|
+
*
|
|
69
|
+
* @param modelName - The name of the model to use.
|
|
70
|
+
* @param options - The chat model options.
|
|
71
|
+
* @returns A promise that resolves to either a ChatResponse or an AsyncGenerator of ChatResponses.
|
|
72
|
+
*/
|
|
73
|
+
async _callAPI(
|
|
74
|
+
modelName: string,
|
|
75
|
+
options: ChatModelRequestOptions<ChatCompletionMessageParam>
|
|
76
|
+
): Promise<ChatResponse | AsyncGenerator<ChatResponse, ChatResponse>> {
|
|
77
|
+
const startTime = Date.now();
|
|
78
|
+
|
|
79
|
+
if (this.stream) {
|
|
80
|
+
// Handle streaming response
|
|
81
|
+
const stream = await this.client.chat.completions.create({
|
|
82
|
+
model: modelName,
|
|
83
|
+
messages: options.messages,
|
|
84
|
+
tools: this._formatToolSchemas(options.tools),
|
|
85
|
+
tool_choice: this._formatToolChoice(options.toolChoice),
|
|
86
|
+
stream: true,
|
|
87
|
+
...(this.presetGenParams ?? {}),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
return this._parseOpenAIStreamedResponse(stream, startTime);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Handle non-streaming response
|
|
94
|
+
const response = await this.client.chat.completions.create({
|
|
95
|
+
model: modelName,
|
|
96
|
+
messages: options.messages,
|
|
97
|
+
tools: options.tools,
|
|
98
|
+
tool_choice: this._formatToolChoice(options.toolChoice),
|
|
99
|
+
stream: false,
|
|
100
|
+
...(this.presetGenParams ?? {}),
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const choice = response.choices[0];
|
|
104
|
+
const blocks: (TextBlock | ToolCallBlock | ThinkingBlock | DataBlock)[] = [];
|
|
105
|
+
|
|
106
|
+
// handling text block
|
|
107
|
+
if (choice.message.content) {
|
|
108
|
+
blocks.push({ id: crypto.randomUUID(), type: 'text', text: choice.message.content });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// handling tool calls
|
|
112
|
+
if (choice.message.tool_calls && Array.isArray(choice.message.tool_calls)) {
|
|
113
|
+
choice.message.tool_calls.forEach(toolCall => {
|
|
114
|
+
if (toolCall.type === 'function') {
|
|
115
|
+
blocks.push({
|
|
116
|
+
type: 'tool_call',
|
|
117
|
+
id: toolCall.id,
|
|
118
|
+
name: toolCall.function.name,
|
|
119
|
+
input: toolCall.function.arguments,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// handling usage
|
|
126
|
+
const usage = response.usage
|
|
127
|
+
? {
|
|
128
|
+
type: 'chat_usage',
|
|
129
|
+
inputTokens: response.usage.prompt_tokens,
|
|
130
|
+
outputTokens: response.usage.completion_tokens,
|
|
131
|
+
time: (Date.now() - startTime) / 1000,
|
|
132
|
+
}
|
|
133
|
+
: undefined;
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
type: 'chat',
|
|
137
|
+
id: response.id,
|
|
138
|
+
createdAt: new Date(response.created * 1000).toISOString(),
|
|
139
|
+
content: blocks,
|
|
140
|
+
usage,
|
|
141
|
+
} as ChatResponse;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Formats the tool choice for the API request.
|
|
146
|
+
*
|
|
147
|
+
* TODO: supports grouped tool choices.
|
|
148
|
+
*
|
|
149
|
+
* @param toolChoice - The tool choice option.
|
|
150
|
+
* @returns The formatted tool choice.
|
|
151
|
+
*/
|
|
152
|
+
_formatToolChoice(toolChoice?: ToolChoice): ChatCompletionToolChoiceOption {
|
|
153
|
+
if (toolChoice) {
|
|
154
|
+
// Directly return predefined options
|
|
155
|
+
if (toolChoice === 'none' || toolChoice === 'auto' || toolChoice === 'required') {
|
|
156
|
+
return toolChoice;
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
type: 'function',
|
|
160
|
+
function: {
|
|
161
|
+
name: toolChoice,
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return 'auto';
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Parses a streamed response from OpenAI API.
|
|
170
|
+
* An async generator that yields delta ChatResponse objects as they are received.
|
|
171
|
+
*
|
|
172
|
+
* @param stream - The OpenAI stream object.
|
|
173
|
+
* @param startTime - The start time of the request for usage calculation.
|
|
174
|
+
* @returns An async generator yielding delta ChatResponse objects, and returns the complete ChatResponse.
|
|
175
|
+
*/
|
|
176
|
+
async *_parseOpenAIStreamedResponse(
|
|
177
|
+
stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>,
|
|
178
|
+
startTime: number
|
|
179
|
+
): AsyncGenerator<ChatResponse, ChatResponse> {
|
|
180
|
+
let accText = '';
|
|
181
|
+
// Store accumulated input strings for each tool call
|
|
182
|
+
const accToolInputs: Map<string, string> = new Map();
|
|
183
|
+
// Store tool call metadata (id, name)
|
|
184
|
+
const toolCallMeta: Map<string, { id: string; name: string }> = new Map();
|
|
185
|
+
let lastUsage: ChatUsage | null = null;
|
|
186
|
+
let responseId = '';
|
|
187
|
+
let createdTimestamp = 0;
|
|
188
|
+
|
|
189
|
+
for await (const chunk of stream) {
|
|
190
|
+
if (!responseId && chunk.id) {
|
|
191
|
+
responseId = chunk.id;
|
|
192
|
+
}
|
|
193
|
+
if (!createdTimestamp && chunk.created) {
|
|
194
|
+
createdTimestamp = chunk.created;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (chunk.choices && chunk.choices.length > 0) {
|
|
198
|
+
const choice = chunk.choices[0];
|
|
199
|
+
|
|
200
|
+
// Delta data for this chunk
|
|
201
|
+
let deltaText = '';
|
|
202
|
+
const deltaToolCalls: Map<string, ToolCallBlock> = new Map();
|
|
203
|
+
|
|
204
|
+
if (choice.delta?.content) {
|
|
205
|
+
deltaText = choice.delta.content;
|
|
206
|
+
accText += deltaText;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (choice.delta?.tool_calls) {
|
|
210
|
+
choice.delta.tool_calls.forEach(toolCall => {
|
|
211
|
+
const index = toolCall.index.toString();
|
|
212
|
+
|
|
213
|
+
// Initialize metadata if not exists
|
|
214
|
+
if (!toolCallMeta.has(index)) {
|
|
215
|
+
toolCallMeta.set(index, { id: '', name: '' });
|
|
216
|
+
}
|
|
217
|
+
if (!accToolInputs.has(index)) {
|
|
218
|
+
accToolInputs.set(index, '');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Update the tool call id
|
|
222
|
+
if (toolCall.id) {
|
|
223
|
+
toolCallMeta.get(index)!.id = toolCall.id;
|
|
224
|
+
}
|
|
225
|
+
// Update the tool call name
|
|
226
|
+
if (toolCall.function?.name) {
|
|
227
|
+
toolCallMeta.get(index)!.name = toolCall.function.name;
|
|
228
|
+
}
|
|
229
|
+
// Update the tool call input
|
|
230
|
+
if (toolCall.function?.arguments) {
|
|
231
|
+
const deltaArgs = toolCall.function.arguments;
|
|
232
|
+
accToolInputs.set(index, accToolInputs.get(index)! + deltaArgs);
|
|
233
|
+
|
|
234
|
+
// Create delta tool call with incremental input
|
|
235
|
+
const meta = toolCallMeta.get(index)!;
|
|
236
|
+
deltaToolCalls.set(index, {
|
|
237
|
+
type: 'tool_call',
|
|
238
|
+
id: meta.id,
|
|
239
|
+
name: meta.name,
|
|
240
|
+
input: deltaArgs,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Create a delta ChatResponse object
|
|
247
|
+
const deltaBlocks = this._accDataToBlocks(deltaText, deltaToolCalls);
|
|
248
|
+
|
|
249
|
+
yield {
|
|
250
|
+
type: 'chat',
|
|
251
|
+
id: responseId || crypto.randomUUID(),
|
|
252
|
+
createdAt: createdTimestamp
|
|
253
|
+
? new Date(createdTimestamp * 1000).toISOString()
|
|
254
|
+
: new Date().toISOString(),
|
|
255
|
+
content: deltaBlocks,
|
|
256
|
+
usage: lastUsage,
|
|
257
|
+
} as ChatResponse;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Handle usage information (typically in the last chunk)
|
|
261
|
+
if (chunk.usage) {
|
|
262
|
+
lastUsage = {
|
|
263
|
+
type: 'chat_usage',
|
|
264
|
+
inputTokens: chunk.usage.prompt_tokens || 0,
|
|
265
|
+
outputTokens: chunk.usage.completion_tokens || 0,
|
|
266
|
+
time: (Date.now() - startTime) / 1000,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Build final tool calls with complete JSON strings
|
|
272
|
+
const finalToolCalls: Map<string, ToolCallBlock> = new Map();
|
|
273
|
+
toolCallMeta.forEach((meta, index) => {
|
|
274
|
+
finalToolCalls.set(index, {
|
|
275
|
+
type: 'tool_call',
|
|
276
|
+
id: meta.id,
|
|
277
|
+
name: meta.name,
|
|
278
|
+
input: accToolInputs.get(index) || '{}',
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const blocks = this._accDataToBlocks(accText, finalToolCalls);
|
|
283
|
+
return {
|
|
284
|
+
type: 'chat',
|
|
285
|
+
id: responseId || crypto.randomUUID(),
|
|
286
|
+
createdAt: createdTimestamp
|
|
287
|
+
? new Date(createdTimestamp * 1000).toISOString()
|
|
288
|
+
: new Date().toISOString(),
|
|
289
|
+
content: blocks,
|
|
290
|
+
usage: lastUsage,
|
|
291
|
+
} as ChatResponse;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Convert data into blocks
|
|
296
|
+
*
|
|
297
|
+
* @param text - The text response from the llm API
|
|
298
|
+
* @param toolCalls - The tool calls
|
|
299
|
+
* @returns An array of blocks
|
|
300
|
+
*/
|
|
301
|
+
_accDataToBlocks(
|
|
302
|
+
text: string,
|
|
303
|
+
toolCalls: Map<string, ToolCallBlock>
|
|
304
|
+
): (TextBlock | ToolCallBlock)[] {
|
|
305
|
+
const blocks: (TextBlock | ToolCallBlock)[] = [];
|
|
306
|
+
if (text) {
|
|
307
|
+
blocks.push({ id: crypto.randomUUID(), type: 'text', text: text });
|
|
308
|
+
}
|
|
309
|
+
// Push the tool calls into the blocks
|
|
310
|
+
if (toolCalls.size > 0) {
|
|
311
|
+
toolCalls.forEach(value => {
|
|
312
|
+
blocks.push(value);
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return blocks;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Format the tool schemas to the expected API format.
|
|
321
|
+
* @param tools
|
|
322
|
+
* @returns The formatted tool schemas.
|
|
323
|
+
*/
|
|
324
|
+
_formatToolSchemas(tools: ToolSchema[] | undefined): ToolSchema[] {
|
|
325
|
+
return tools || [];
|
|
326
|
+
}
|
|
327
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DataBlock, TextBlock, ThinkingBlock, ToolCallBlock } from '../message';
|
|
2
|
+
import { JSONSerializableObject } from '../type';
|
|
3
|
+
import { ChatUsage } from './usage';
|
|
4
|
+
|
|
5
|
+
export interface ChatResponse {
|
|
6
|
+
type: 'chat';
|
|
7
|
+
id: string;
|
|
8
|
+
createdAt: string;
|
|
9
|
+
content: Array<TextBlock | ToolCallBlock | ThinkingBlock | DataBlock>;
|
|
10
|
+
usage?: ChatUsage;
|
|
11
|
+
structuredContent?: Record<string, JSONSerializableObject>;
|
|
12
|
+
metadata?: Record<string, JSONSerializableObject>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface StructuredResponse {
|
|
16
|
+
type: 'structured';
|
|
17
|
+
id: string;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
content: Record<string, JSONSerializableObject>;
|
|
20
|
+
usage?: ChatUsage;
|
|
21
|
+
metadata?: Record<string, JSONSerializableObject>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { JSONSerializableObject } from '../type';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The usage structure for chat models.
|
|
5
|
+
*/
|
|
6
|
+
export interface ChatUsage {
|
|
7
|
+
type: 'chat_usage';
|
|
8
|
+
inputTokens: number;
|
|
9
|
+
outputTokens: number;
|
|
10
|
+
time: number;
|
|
11
|
+
metadata?: Record<string, JSONSerializableObject>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Msg } from '../message';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The complete agent state including both conversation context and metadata.
|
|
5
|
+
*/
|
|
6
|
+
export interface AgentState {
|
|
7
|
+
/**
|
|
8
|
+
* The conversation context (message history).
|
|
9
|
+
*/
|
|
10
|
+
context: Msg[];
|
|
11
|
+
/**
|
|
12
|
+
* Agent metadata (replyId, curIter, curSummary, etc.).
|
|
13
|
+
*/
|
|
14
|
+
metadata: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The base storage class that responsible for
|
|
19
|
+
* - loading/saving agent context and state, and
|
|
20
|
+
* - offloading compressed context to external storage for agentic search if needed
|
|
21
|
+
*/
|
|
22
|
+
export abstract class StorageBase {
|
|
23
|
+
/**
|
|
24
|
+
* Load the complete agent state including context and metadata.
|
|
25
|
+
* @param options.agentId - The agent identifier
|
|
26
|
+
* @returns The agent state with context and metadata
|
|
27
|
+
*/
|
|
28
|
+
abstract loadAgentState(options?: { agentId?: string }): Promise<AgentState>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Save the complete agent state including context and metadata.
|
|
32
|
+
* @param options.agentId - The agent identifier
|
|
33
|
+
* @param options.context - The conversation context to save
|
|
34
|
+
* @param options.metadata - The agent metadata to save
|
|
35
|
+
*/
|
|
36
|
+
abstract saveAgentState(options: {
|
|
37
|
+
agentId?: string;
|
|
38
|
+
context: Msg[];
|
|
39
|
+
metadata: Record<string, unknown>;
|
|
40
|
+
}): Promise<void>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Offload the compressed context to external storage for agentic search if needed.
|
|
44
|
+
* @param _options.msgs
|
|
45
|
+
* @param _options
|
|
46
|
+
* @returns The identifier or URL of the offloaded context, or undefined if offloading is not implemented or not needed
|
|
47
|
+
*/
|
|
48
|
+
async offloadContext(_options: { msgs: Msg[] }): Promise<string | undefined> {
|
|
49
|
+
console.log('Offloading context is not implemented for this storage. Skipping offloading.');
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|