@vectorx/agent-runtime 0.1.0

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.
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AgentDriver = void 0;
13
+ const codes_1 = require("./codes");
14
+ const schema_1 = require("./schema");
15
+ const agent_helper_1 = require("./utils/agent-helper");
16
+ const integration_response_1 = require("./utils/integration-response");
17
+ class AgentDriver {
18
+ static run(event, context, agent) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ return new AgentDriver(agent).run(event, context);
21
+ });
22
+ }
23
+ constructor(agent) {
24
+ this.agent = agent;
25
+ }
26
+ run(event, context) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ if (!context.httpContext) {
29
+ return (0, integration_response_1.createIntegrationResponse)(400, codes_1.CODES.INVALID_HTTP_CONTEXT, "Invalid HTTP context");
30
+ }
31
+ const apiFuncs = {
32
+ "POST:send-message": this.sendMessageFunc.bind(this),
33
+ "GET:messages": this.getHistoryMessagesFunc.bind(this),
34
+ "GET:info": this.getAgentInfoFunc.bind(this),
35
+ "GET:conversations": this.getConversationsFunc.bind(this),
36
+ "GET:health": () => __awaiter(this, void 0, void 0, function* () { return ({ message: integration_response_1.OK_MESSAGE, timestamp: new Date().toISOString() }); }),
37
+ };
38
+ const httpMethod = context.httpContext.httpMethod;
39
+ const apiName = (0, agent_helper_1.parseApiName)(context.httpContext.url);
40
+ if (!apiName) {
41
+ return (0, integration_response_1.createIntegrationResponse)(404, codes_1.CODES.AGENT_API_NOT_FOUND, `Invalid url : ${context.httpContext.url}`);
42
+ }
43
+ const apiFunc = apiFuncs[`${httpMethod}:${apiName}`];
44
+ if (apiFunc) {
45
+ return apiFunc(event, context);
46
+ }
47
+ return (0, integration_response_1.createIntegrationResponse)(404, codes_1.CODES.AGENT_API_NOT_FOUND, `Agent API ${httpMethod}:${apiName} has not been supported`);
48
+ });
49
+ }
50
+ sendMessageFunc(event, context) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ try {
53
+ const parsedEvent = schema_1.sendMessageInputSchema.passthrough().parse(event);
54
+ return yield this.agent.sendMessage(parsedEvent);
55
+ }
56
+ catch (e) {
57
+ return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'sendMessage' failed, message: ${e}`);
58
+ }
59
+ });
60
+ }
61
+ getHistoryMessagesFunc(event, context) {
62
+ return __awaiter(this, void 0, void 0, function* () {
63
+ try {
64
+ const query = (0, agent_helper_1.parseQueryFromCtx)(context);
65
+ const parsedEvent = schema_1.getHistoryMessagesParamsSchema.passthrough().parse(query);
66
+ return yield this.agent.getHistoryMessages(parsedEvent);
67
+ }
68
+ catch (e) {
69
+ return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'getHistoryMessages' failed, message: ${e}`);
70
+ }
71
+ });
72
+ }
73
+ getAgentInfoFunc() {
74
+ return __awaiter(this, void 0, void 0, function* () {
75
+ try {
76
+ return yield this.agent.getAgentInfo();
77
+ }
78
+ catch (e) {
79
+ return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'getAgentInfo' failed, message: ${e}`);
80
+ }
81
+ });
82
+ }
83
+ getConversationsFunc() {
84
+ return __awaiter(this, void 0, void 0, function* () {
85
+ try {
86
+ return yield this.agent.getConversations();
87
+ }
88
+ catch (e) {
89
+ return (0, integration_response_1.createIntegrationResponse)(500, codes_1.CODES.AGENT_API_ERROR, `Agent API 'getConversations' failed, message: ${e}`);
90
+ }
91
+ });
92
+ }
93
+ }
94
+ exports.AgentDriver = AgentDriver;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/agent.js ADDED
@@ -0,0 +1,577 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.AgentRuntime = exports.AgentEventType = exports.AgentType = void 0;
13
+ const ai_sdk_1 = require("@vectorx/ai-sdk");
14
+ const functions_framework_1 = require("@vectorx/functions-framework");
15
+ const schema_1 = require("./schema");
16
+ const sse_sender_1 = require("./sse-sender");
17
+ const agent_helper_1 = require("./utils/agent-helper");
18
+ var AgentType;
19
+ (function (AgentType) {
20
+ AgentType["AI_SDK_REQUEST_START"] = "AI_SDK_REQUEST_START";
21
+ AgentType["AI_SDK_REQUEST_SUCCESS"] = "AI_SDK_REQUEST_SUCCESS";
22
+ AgentType["AI_SDK_REQUEST_ERROR"] = "AI_SDK_REQUEST_ERROR";
23
+ AgentType["AI_SDK_CREATE_MODEL"] = "AI_SDK_CREATE_MODEL";
24
+ AgentType["AI_SDK_CREATE_RECORD_PAIR"] = "AI_SDK_CREATE_RECORD_PAIR";
25
+ AgentType["AI_SDK_GET_HISTORY_MESSAGES"] = "AI_SDK_GET_HISTORY_MESSAGES";
26
+ AgentType["AI_SDK_GET_CONVERSATIONS"] = "AI_SDK_GET_CONVERSATIONS";
27
+ AgentType["Ai_SDK_POST_KNOWLEDGE_BASE"] = "Ai_SDK_POST_KNOWLEDGE_BASE";
28
+ })(AgentType || (exports.AgentType = AgentType = {}));
29
+ var AgentEventType;
30
+ (function (AgentEventType) {
31
+ AgentEventType["FRAMEWORK_EVENT"] = "FRAMEWORK_EVENT";
32
+ AgentEventType["HTTP_ACCESS"] = "HTTP_ACCESS";
33
+ })(AgentEventType || (exports.AgentEventType = AgentEventType = {}));
34
+ const safeJsonStringify = (obj) => {
35
+ try {
36
+ return JSON.stringify(obj);
37
+ }
38
+ catch (error) {
39
+ return JSON.stringify({ error: "Failed to stringify object" });
40
+ }
41
+ };
42
+ class AgentRuntime {
43
+ get agentTag() {
44
+ const url = this.context.httpContext.url;
45
+ return (0, agent_helper_1.parseAgentTag)(url);
46
+ }
47
+ get agentId() {
48
+ const url = this.context.httpContext.url;
49
+ return (0, agent_helper_1.parseAgentId)(url);
50
+ }
51
+ get version() {
52
+ return "0.0.1【TODO: 从 context header 中获取22222版本号】";
53
+ }
54
+ constructor(context) {
55
+ this.context = context;
56
+ this.sseSender = new sse_sender_1.SSESender(context);
57
+ this.request = context.request;
58
+ this.logger = context.logger;
59
+ const loggedRequest = this.createLoggedRequest(this.request, this.logger);
60
+ this.ai = (0, ai_sdk_1.createAi)({
61
+ request: loggedRequest,
62
+ getBaseUrl: () => this.context.baseUrl,
63
+ env: ai_sdk_1.AiSdkEnv.Cloud,
64
+ });
65
+ }
66
+ createLoggedRequest(originalRequest, logger) {
67
+ return {
68
+ get: (options) => __awaiter(this, void 0, void 0, function* () {
69
+ const startTime = Date.now();
70
+ const requestStartLog = safeJsonStringify({
71
+ type: AgentType.AI_SDK_REQUEST_START,
72
+ method: "GET",
73
+ url: options.url,
74
+ headers: options.headers,
75
+ data: options.data,
76
+ eventId: this.context.eventID,
77
+ timestamp: new Date().toISOString(),
78
+ });
79
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
80
+ try {
81
+ const response = yield originalRequest.get(options);
82
+ const requestSuccessLog = safeJsonStringify({
83
+ type: AgentType.AI_SDK_REQUEST_SUCCESS,
84
+ method: "GET",
85
+ url: options.url,
86
+ status: response.statusCode,
87
+ duration: `${Date.now() - startTime}ms`,
88
+ eventId: this.context.eventID,
89
+ timestamp: new Date().toISOString(),
90
+ });
91
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
92
+ return response;
93
+ }
94
+ catch (error) {
95
+ const requestErrorLog = safeJsonStringify({
96
+ type: AgentType.AI_SDK_REQUEST_ERROR,
97
+ method: "GET",
98
+ url: options.url,
99
+ error: error instanceof Error ? error.message : "请求失败",
100
+ stack: error instanceof Error ? error.stack : undefined,
101
+ duration: `${Date.now() - startTime}ms`,
102
+ eventId: this.context.eventID,
103
+ timestamp: new Date().toISOString(),
104
+ });
105
+ logger.logAccesslog(functions_framework_1.LogLevel.ERROR, requestErrorLog);
106
+ throw error;
107
+ }
108
+ }),
109
+ post: (options) => __awaiter(this, void 0, void 0, function* () {
110
+ const startTime = Date.now();
111
+ const requestStartLog = safeJsonStringify({
112
+ type: AgentEventType.FRAMEWORK_EVENT,
113
+ scene: AgentType.AI_SDK_REQUEST_START,
114
+ method: "POST",
115
+ url: options.url,
116
+ headers: options.headers,
117
+ data: options.data,
118
+ eventId: this.context.eventID,
119
+ timestamp: new Date().toISOString(),
120
+ });
121
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
122
+ try {
123
+ const response = yield originalRequest.post(options);
124
+ const requestSuccessLog = safeJsonStringify({
125
+ type: AgentEventType.FRAMEWORK_EVENT,
126
+ scene: AgentType.AI_SDK_REQUEST_SUCCESS,
127
+ method: "POST",
128
+ url: options.url,
129
+ status: response.statusCode,
130
+ duration: `${Date.now() - startTime}ms`,
131
+ eventId: this.context.eventID,
132
+ timestamp: new Date().toISOString(),
133
+ });
134
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
135
+ return response;
136
+ }
137
+ catch (error) {
138
+ const requestErrorLog = safeJsonStringify({
139
+ type: AgentEventType.FRAMEWORK_EVENT,
140
+ scene: AgentType.AI_SDK_REQUEST_ERROR,
141
+ method: "POST",
142
+ url: options.url,
143
+ error: error instanceof Error ? error.message : "请求失败",
144
+ stack: error instanceof Error ? error.stack : undefined,
145
+ duration: `${Date.now() - startTime}ms`,
146
+ eventId: this.context.eventID,
147
+ timestamp: new Date().toISOString(),
148
+ });
149
+ logger.logAccesslog(functions_framework_1.LogLevel.ERROR, requestErrorLog);
150
+ throw error;
151
+ }
152
+ }),
153
+ put: (options) => __awaiter(this, void 0, void 0, function* () {
154
+ const startTime = Date.now();
155
+ const requestStartLog = safeJsonStringify({
156
+ type: AgentEventType.FRAMEWORK_EVENT,
157
+ scene: AgentType.AI_SDK_REQUEST_START,
158
+ method: "PUT",
159
+ url: options.url,
160
+ headers: options.headers,
161
+ data: options.data,
162
+ eventId: this.context.eventID,
163
+ timestamp: new Date().toISOString(),
164
+ });
165
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
166
+ try {
167
+ const response = yield originalRequest.put(options);
168
+ const requestSuccessLog = safeJsonStringify({
169
+ type: AgentEventType.FRAMEWORK_EVENT,
170
+ scene: AgentType.AI_SDK_REQUEST_SUCCESS,
171
+ method: "PUT",
172
+ url: options.url,
173
+ status: response.statusCode,
174
+ duration: `${Date.now() - startTime}ms`,
175
+ eventId: this.context.eventID,
176
+ timestamp: new Date().toISOString(),
177
+ });
178
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
179
+ return response;
180
+ }
181
+ catch (error) {
182
+ const requestErrorLog = safeJsonStringify({
183
+ type: AgentEventType.FRAMEWORK_EVENT,
184
+ scene: AgentType.AI_SDK_REQUEST_ERROR,
185
+ method: "PUT",
186
+ url: options.url,
187
+ error: error instanceof Error ? error.message : "请求失败",
188
+ stack: error instanceof Error ? error.stack : undefined,
189
+ duration: `${Date.now() - startTime}ms`,
190
+ eventId: this.context.eventID,
191
+ timestamp: new Date().toISOString(),
192
+ });
193
+ logger.logAccesslog(functions_framework_1.LogLevel.ERROR, requestErrorLog);
194
+ throw error;
195
+ }
196
+ }),
197
+ upload: (options) => __awaiter(this, void 0, void 0, function* () {
198
+ const startTime = Date.now();
199
+ const requestStartLog = safeJsonStringify({
200
+ type: AgentEventType.FRAMEWORK_EVENT,
201
+ scene: AgentType.AI_SDK_REQUEST_START,
202
+ method: "UPLOAD",
203
+ url: options.url,
204
+ headers: options.headers,
205
+ eventId: this.context.eventID,
206
+ timestamp: new Date().toISOString(),
207
+ });
208
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
209
+ try {
210
+ const response = yield originalRequest.upload(options);
211
+ const requestSuccessLog = safeJsonStringify({
212
+ type: AgentEventType.FRAMEWORK_EVENT,
213
+ scene: AgentType.AI_SDK_REQUEST_SUCCESS,
214
+ method: "UPLOAD",
215
+ url: options.url,
216
+ status: response.statusCode,
217
+ duration: `${Date.now() - startTime}ms`,
218
+ eventId: this.context.eventID,
219
+ timestamp: new Date().toISOString(),
220
+ });
221
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
222
+ return response;
223
+ }
224
+ catch (error) {
225
+ const requestErrorLog = safeJsonStringify({
226
+ type: AgentEventType.FRAMEWORK_EVENT,
227
+ scene: AgentType.AI_SDK_REQUEST_ERROR,
228
+ method: "UPLOAD",
229
+ url: options.url,
230
+ error: error instanceof Error ? error.message : "请求失败",
231
+ stack: error instanceof Error ? error.stack : undefined,
232
+ duration: `${Date.now() - startTime}ms`,
233
+ eventId: this.context.eventID,
234
+ timestamp: new Date().toISOString(),
235
+ });
236
+ logger.logAccesslog(functions_framework_1.LogLevel.ERROR, requestErrorLog);
237
+ throw error;
238
+ }
239
+ }),
240
+ download: (options) => __awaiter(this, void 0, void 0, function* () {
241
+ const startTime = Date.now();
242
+ const requestStartLog = safeJsonStringify({
243
+ type: AgentEventType.FRAMEWORK_EVENT,
244
+ scene: AgentType.AI_SDK_REQUEST_START,
245
+ method: "DOWNLOAD",
246
+ url: options.url,
247
+ headers: options.headers,
248
+ eventId: this.context.eventID,
249
+ timestamp: new Date().toISOString(),
250
+ });
251
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
252
+ try {
253
+ const response = yield originalRequest.download(options);
254
+ const requestSuccessLog = safeJsonStringify({
255
+ type: AgentEventType.FRAMEWORK_EVENT,
256
+ scene: AgentType.AI_SDK_REQUEST_SUCCESS,
257
+ method: "DOWNLOAD",
258
+ url: options.url,
259
+ duration: `${Date.now() - startTime}ms`,
260
+ eventId: this.context.eventID,
261
+ timestamp: new Date().toISOString(),
262
+ });
263
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
264
+ return response;
265
+ }
266
+ catch (error) {
267
+ const requestErrorLog = safeJsonStringify({
268
+ type: AgentEventType.FRAMEWORK_EVENT,
269
+ scene: AgentType.AI_SDK_REQUEST_ERROR,
270
+ method: "DOWNLOAD",
271
+ url: options.url,
272
+ error: error instanceof Error ? error.message : "请求失败",
273
+ stack: error instanceof Error ? error.stack : undefined,
274
+ duration: `${Date.now() - startTime}ms`,
275
+ eventId: this.context.eventID,
276
+ timestamp: new Date().toISOString(),
277
+ });
278
+ logger.logAccesslog(functions_framework_1.LogLevel.ERROR, requestErrorLog);
279
+ throw error;
280
+ }
281
+ }),
282
+ fetch: (options) => __awaiter(this, void 0, void 0, function* () {
283
+ const startTime = Date.now();
284
+ const requestStartLog = safeJsonStringify({
285
+ type: AgentEventType.FRAMEWORK_EVENT,
286
+ scene: AgentType.AI_SDK_REQUEST_START,
287
+ method: "FETCH",
288
+ url: options.url,
289
+ headers: options.headers,
290
+ data: safeJsonStringify(options.body),
291
+ eventId: this.context.eventID,
292
+ timestamp: new Date().toISOString(),
293
+ });
294
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
295
+ try {
296
+ const response = yield originalRequest.fetch(options);
297
+ const requestSuccessLog = safeJsonStringify({
298
+ type: AgentEventType.FRAMEWORK_EVENT,
299
+ scene: AgentType.AI_SDK_REQUEST_SUCCESS,
300
+ method: "FETCH",
301
+ url: options.url,
302
+ status: response.statusCode,
303
+ duration: `${Date.now() - startTime}ms`,
304
+ eventId: this.context.eventID,
305
+ timestamp: new Date().toISOString(),
306
+ });
307
+ logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
308
+ return response;
309
+ }
310
+ catch (error) {
311
+ const requestErrorLog = safeJsonStringify({
312
+ type: AgentEventType.FRAMEWORK_EVENT,
313
+ scene: AgentType.AI_SDK_REQUEST_ERROR,
314
+ method: "FETCH",
315
+ url: options.url,
316
+ error: error instanceof Error ? error.message : "请求失败",
317
+ stack: error instanceof Error ? error.stack : undefined,
318
+ duration: `${Date.now() - startTime}ms`,
319
+ eventId: this.context.eventID,
320
+ timestamp: new Date().toISOString(),
321
+ });
322
+ logger.logAccesslog(functions_framework_1.LogLevel.ERROR, requestErrorLog);
323
+ throw error;
324
+ }
325
+ }),
326
+ };
327
+ }
328
+ createModel(modelName) {
329
+ const createModelLog = safeJsonStringify({
330
+ type: AgentEventType.FRAMEWORK_EVENT,
331
+ scene: AgentType.AI_SDK_CREATE_MODEL,
332
+ modelName: modelName,
333
+ eventId: this.context.eventID,
334
+ timestamp: new Date().toISOString(),
335
+ });
336
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, createModelLog);
337
+ return this.ai.createModel(modelName);
338
+ }
339
+ createRecordPair(params) {
340
+ return __awaiter(this, void 0, void 0, function* () {
341
+ try {
342
+ const validatedParams = schema_1.createRecordPairParamsSchema.passthrough().parse(params);
343
+ const { conversation_id, user_input, assistant_output } = validatedParams;
344
+ const createRecordPairStartLog = safeJsonStringify({
345
+ type: AgentEventType.FRAMEWORK_EVENT,
346
+ scene: AgentType.AI_SDK_CREATE_RECORD_PAIR,
347
+ method: "POST",
348
+ url: `${this.context.baseUrl}/conversation/save`,
349
+ headers: {
350
+ "Content-Type": "application/json",
351
+ },
352
+ data: {
353
+ conversation_id,
354
+ user_input: JSON.stringify(user_input),
355
+ assistant_output: JSON.stringify(assistant_output),
356
+ },
357
+ eventId: this.context.eventID,
358
+ timestamp: new Date().toISOString(),
359
+ });
360
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, createRecordPairStartLog);
361
+ const response = yield this.request.post({
362
+ headers: {
363
+ "Content-Type": "application/json",
364
+ },
365
+ url: `${this.context.baseUrl}/conversation/save`,
366
+ data: {
367
+ conversation_id,
368
+ user_input: JSON.stringify(user_input),
369
+ assistant_output: JSON.stringify(assistant_output),
370
+ },
371
+ });
372
+ const createRecordPairEndLog = safeJsonStringify({
373
+ type: AgentEventType.FRAMEWORK_EVENT,
374
+ scene: AgentType.AI_SDK_CREATE_RECORD_PAIR,
375
+ method: "POST",
376
+ url: `${this.context.baseUrl}/conversation/save`,
377
+ status: response.status,
378
+ data: response.data,
379
+ eventId: this.context.eventID,
380
+ timestamp: new Date().toISOString(),
381
+ });
382
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, createRecordPairEndLog);
383
+ return response.data;
384
+ }
385
+ catch (error) {
386
+ const createRecordPairErrorLog = safeJsonStringify({
387
+ type: AgentEventType.FRAMEWORK_EVENT,
388
+ scene: AgentType.AI_SDK_CREATE_RECORD_PAIR,
389
+ method: "POST",
390
+ url: `${this.context.baseUrl}/conversation/save`,
391
+ error: error instanceof Error ? error.message : "创建对话对失败",
392
+ stack: error instanceof Error ? error.stack : undefined,
393
+ eventId: this.context.eventID,
394
+ timestamp: new Date().toISOString(),
395
+ });
396
+ this.logger.logAccesslog(functions_framework_1.LogLevel.ERROR, createRecordPairErrorLog);
397
+ return {
398
+ code: -1,
399
+ msg: error instanceof Error ? error.message : "创建对话对失败",
400
+ };
401
+ }
402
+ });
403
+ }
404
+ getHistoryMessages(params) {
405
+ return __awaiter(this, void 0, void 0, function* () {
406
+ try {
407
+ const validatedParams = schema_1.getHistoryMessagesParamsSchema.passthrough().parse(params);
408
+ const getHistoryMessagesStartLog = safeJsonStringify({
409
+ type: AgentEventType.FRAMEWORK_EVENT,
410
+ scene: AgentType.AI_SDK_GET_HISTORY_MESSAGES,
411
+ method: "GET",
412
+ url: `${this.context.baseUrl}/conversation/history`,
413
+ data: validatedParams,
414
+ eventId: this.context.eventID,
415
+ timestamp: new Date().toISOString(),
416
+ });
417
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, getHistoryMessagesStartLog);
418
+ const response = yield this.request.get({
419
+ url: `${this.context.baseUrl}/conversation/history`,
420
+ data: validatedParams,
421
+ });
422
+ const getHistoryMessagesEndLog = safeJsonStringify({
423
+ type: AgentEventType.FRAMEWORK_EVENT,
424
+ scene: AgentType.AI_SDK_GET_HISTORY_MESSAGES,
425
+ method: "GET",
426
+ url: `${this.context.baseUrl}/conversation/history`,
427
+ status: response.status,
428
+ data: response.data,
429
+ eventId: this.context.eventID,
430
+ timestamp: new Date().toISOString(),
431
+ });
432
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, getHistoryMessagesEndLog);
433
+ return response.data;
434
+ }
435
+ catch (error) {
436
+ const getHistoryMessagesErrorLog = safeJsonStringify({
437
+ type: AgentEventType.FRAMEWORK_EVENT,
438
+ scene: AgentType.AI_SDK_GET_HISTORY_MESSAGES,
439
+ method: "GET",
440
+ url: `${this.context.baseUrl}/conversation/history`,
441
+ error: error instanceof Error ? error.message : "获取历史消息失败",
442
+ stack: error instanceof Error ? error.stack : undefined,
443
+ eventId: this.context.eventID,
444
+ timestamp: new Date().toISOString(),
445
+ });
446
+ this.logger.logAccesslog(functions_framework_1.LogLevel.ERROR, getHistoryMessagesErrorLog);
447
+ return {
448
+ code: -1,
449
+ msg: error instanceof Error ? error.message : "获取历史消息失败",
450
+ data: {
451
+ message_list: [],
452
+ conversation_id: "",
453
+ participant_info_map: {},
454
+ next_cursor: 0,
455
+ has_more: false,
456
+ },
457
+ };
458
+ }
459
+ });
460
+ }
461
+ getConversations() {
462
+ return __awaiter(this, void 0, void 0, function* () {
463
+ try {
464
+ const getConversationsStartLog = safeJsonStringify({
465
+ type: AgentEventType.FRAMEWORK_EVENT,
466
+ scene: AgentType.AI_SDK_GET_CONVERSATIONS,
467
+ method: "GET",
468
+ url: `${this.context.baseUrl}/conversation/list`,
469
+ eventId: this.context.eventID,
470
+ timestamp: new Date().toISOString(),
471
+ });
472
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, getConversationsStartLog);
473
+ const response = yield this.request.get({
474
+ url: `${this.context.baseUrl}/conversation/list`,
475
+ });
476
+ const getConversationsEndLog = safeJsonStringify({
477
+ type: AgentEventType.FRAMEWORK_EVENT,
478
+ scene: AgentType.AI_SDK_GET_CONVERSATIONS,
479
+ method: "GET",
480
+ url: `${this.context.baseUrl}/conversation/list`,
481
+ status: response.status,
482
+ data: response.data,
483
+ eventId: this.context.eventID,
484
+ timestamp: new Date().toISOString(),
485
+ });
486
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, getConversationsEndLog);
487
+ return response.data;
488
+ }
489
+ catch (error) {
490
+ const getConversationsErrorLog = safeJsonStringify({
491
+ type: AgentEventType.FRAMEWORK_EVENT,
492
+ scene: AgentType.AI_SDK_GET_CONVERSATIONS,
493
+ method: "GET",
494
+ url: `${this.context.baseUrl}/conversation/list`,
495
+ error: error instanceof Error ? error.message : "获取会话列表失败",
496
+ stack: error instanceof Error ? error.stack : undefined,
497
+ eventId: this.context.eventID,
498
+ timestamp: new Date().toISOString(),
499
+ });
500
+ this.logger.logAccesslog(functions_framework_1.LogLevel.ERROR, getConversationsErrorLog);
501
+ return {
502
+ code: -1,
503
+ msg: error instanceof Error ? error.message : "获取会话列表失败",
504
+ data: {
505
+ conversations: [],
506
+ },
507
+ };
508
+ }
509
+ });
510
+ }
511
+ getAgentInfo() {
512
+ return __awaiter(this, void 0, void 0, function* () {
513
+ const response = yield this.request.get({
514
+ url: `${this.context.baseUrl}/agent/info`,
515
+ });
516
+ return response.data;
517
+ });
518
+ }
519
+ knowledgeBaseRetrieve(params) {
520
+ return __awaiter(this, void 0, void 0, function* () {
521
+ try {
522
+ const validatedParams = schema_1.knowledgeSearchInputSchema.passthrough().parse(params);
523
+ const knowledgeBaseRetrieveStartLog = safeJsonStringify({
524
+ type: AgentEventType.FRAMEWORK_EVENT,
525
+ scene: AgentType.Ai_SDK_POST_KNOWLEDGE_BASE,
526
+ method: "POST",
527
+ url: `${this.context.baseUrl}/rag/retrieve`,
528
+ data: validatedParams,
529
+ eventId: this.context.eventID,
530
+ timestamp: new Date().toISOString(),
531
+ });
532
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, knowledgeBaseRetrieveStartLog);
533
+ const response = yield this.request.post({
534
+ headers: {
535
+ "Content-Type": "application/json",
536
+ },
537
+ url: `${this.context.baseUrl}/rag/retrieve`,
538
+ data: validatedParams,
539
+ });
540
+ const knowledgeBaseRetrieveEndLog = safeJsonStringify({
541
+ type: AgentEventType.FRAMEWORK_EVENT,
542
+ scene: AgentType.Ai_SDK_POST_KNOWLEDGE_BASE,
543
+ method: "POST",
544
+ url: `${this.context.baseUrl}/rag/retrieve`,
545
+ status: response.status,
546
+ data: response.data,
547
+ eventId: this.context.eventID,
548
+ timestamp: new Date().toISOString(),
549
+ });
550
+ this.logger.logAccesslog(functions_framework_1.LogLevel.INFO, knowledgeBaseRetrieveEndLog);
551
+ return response.data;
552
+ }
553
+ catch (error) {
554
+ const knowledgeBaseRetrieveErrorLog = safeJsonStringify({
555
+ type: AgentEventType.FRAMEWORK_EVENT,
556
+ scene: AgentType.Ai_SDK_POST_KNOWLEDGE_BASE,
557
+ method: "POST",
558
+ url: `${this.context.baseUrl}/rag/retrieve`,
559
+ error: error instanceof Error ? error.message : "知识库检索失败",
560
+ stack: error instanceof Error ? error.stack : undefined,
561
+ eventId: this.context.eventID,
562
+ timestamp: new Date().toISOString(),
563
+ });
564
+ this.logger.logAccesslog(functions_framework_1.LogLevel.ERROR, knowledgeBaseRetrieveErrorLog);
565
+ return {
566
+ code: -1,
567
+ msg: error instanceof Error ? error.message : "知识库检索失败",
568
+ data: {
569
+ knowledge_results: [],
570
+ total_tokens: 0,
571
+ },
572
+ };
573
+ }
574
+ });
575
+ }
576
+ }
577
+ exports.AgentRuntime = AgentRuntime;