@vectorx/agent-runtime 0.6.2 → 0.8.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.
package/lib/agent.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type AI, type models } from "@vectorx/ai-sdk";
1
+ import { type AI, models as aiModels, type models } from "@vectorx/ai-sdk";
2
2
  import { type RcbContext } from "@vectorx/functions-framework";
3
3
  import type { CreateRecordPairResponse, GetAgentInfoResponse, GetConversationsResponse, GetHistoryMessagesResponse, KnowledgeBaseRetrieveResponse, QueryTasksResponse, UploadFileResponse } from "./agent-types";
4
4
  import { type CreateRecordPairParams, type GetHistoryMessagesParams, type KnowledgeSearchInput } from "./schema";
@@ -24,12 +24,16 @@ export declare class AgentRuntime {
24
24
  protected ai: AI;
25
25
  protected request: RcbContext["request"];
26
26
  protected logger: RcbContext["logger"];
27
+ private currentModelName?;
27
28
  get agentTag(): string;
28
29
  get agentId(): string;
29
30
  get version(): string;
30
31
  constructor(context: RcbContext);
32
+ private isModelRequest;
33
+ private getModelNameFromUrl;
34
+ private updateModelApmData;
31
35
  private createLoggedRequest;
32
- protected createModel(modelName: models.ModelName): models.ReActModel;
36
+ protected createModel(modelName: models.ModelName): aiModels.ReActModel;
33
37
  protected createRecordPair(params: CreateRecordPairParams): Promise<CreateRecordPairResponse>;
34
38
  protected getHistoryMessages(params: GetHistoryMessagesParams): Promise<GetHistoryMessagesResponse>;
35
39
  protected getConversations(): Promise<GetConversationsResponse>;
package/lib/agent.js CHANGED
@@ -97,12 +97,72 @@ class AgentRuntime {
97
97
  env: ai_sdk_1.AiSdkEnv.Cloud,
98
98
  });
99
99
  }
100
+ isModelRequest(url) {
101
+ if (!url)
102
+ return false;
103
+ return ai_sdk_1.models.isModelRequestUrl(url);
104
+ }
105
+ getModelNameFromUrl(url) {
106
+ if (!url)
107
+ return undefined;
108
+ try {
109
+ const urlObj = new URL(url);
110
+ const fullPath = urlObj.pathname;
111
+ for (const [modelName, { domain, paths }] of ai_sdk_1.models.MODEL_REQUEST_PATTERNS) {
112
+ if (domain !== "*" && urlObj.origin !== domain) {
113
+ continue;
114
+ }
115
+ for (const path of paths) {
116
+ if (fullPath.includes(path) || fullPath.endsWith(path)) {
117
+ return modelName;
118
+ }
119
+ }
120
+ }
121
+ return undefined;
122
+ }
123
+ catch (_a) {
124
+ for (const [modelName, { paths }] of ai_sdk_1.models.MODEL_REQUEST_PATTERNS) {
125
+ for (const path of paths) {
126
+ if (url.includes(path)) {
127
+ return modelName;
128
+ }
129
+ }
130
+ }
131
+ return undefined;
132
+ }
133
+ }
134
+ updateModelApmData(url, statusCode, rt) {
135
+ if (!this.isModelRequest(url)) {
136
+ (0, functions_framework_1.updateApmMeasurementData)({
137
+ kit_model_name: undefined,
138
+ kit_model_service_url: undefined,
139
+ kit_model_status_code: undefined,
140
+ kit_model_rt: undefined,
141
+ });
142
+ return;
143
+ }
144
+ const modelName = this.currentModelName || this.getModelNameFromUrl(url);
145
+ const updates = {
146
+ kit_model_service_url: url,
147
+ };
148
+ if (modelName) {
149
+ updates.kit_model_name = modelName;
150
+ }
151
+ if (statusCode !== undefined) {
152
+ updates.kit_model_status_code = statusCode;
153
+ }
154
+ if (rt !== undefined && rt > 0) {
155
+ updates.kit_model_rt = rt;
156
+ }
157
+ (0, functions_framework_1.updateApmMeasurementData)(updates);
158
+ }
100
159
  createLoggedRequest(originalRequest, logger) {
101
160
  return {
102
161
  get: (options) => __awaiter(this, void 0, void 0, function* () {
103
162
  const startTime = Date.now();
104
163
  const requestStartLog = safeJsonStringify({
105
- type: AgentType.AI_SDK_REQUEST_START,
164
+ type: AgentEventType.FRAMEWORK_EVENT,
165
+ scene: AgentType.AI_SDK_REQUEST_START,
106
166
  method: "GET",
107
167
  url: options.url,
108
168
  headers: options.headers,
@@ -113,12 +173,14 @@ class AgentRuntime {
113
173
  logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
114
174
  try {
115
175
  const response = yield originalRequest.get(options);
176
+ const duration = `${Date.now() - startTime}ms`;
116
177
  const requestSuccessLog = safeJsonStringify({
117
- type: AgentType.AI_SDK_REQUEST_SUCCESS,
178
+ type: AgentEventType.FRAMEWORK_EVENT,
179
+ scene: AgentType.AI_SDK_REQUEST_SUCCESS,
118
180
  method: "GET",
119
181
  url: options.url,
120
182
  status: response.statusCode,
121
- duration: `${Date.now() - startTime}ms`,
183
+ duration: duration,
122
184
  eventId: this.context.eventID,
123
185
  timestamp: new Date().toISOString(),
124
186
  });
@@ -126,13 +188,15 @@ class AgentRuntime {
126
188
  return response;
127
189
  }
128
190
  catch (error) {
191
+ const duration = `${Date.now() - startTime}ms`;
129
192
  const requestErrorLog = safeJsonStringify({
130
- type: AgentType.AI_SDK_REQUEST_ERROR,
193
+ type: AgentEventType.FRAMEWORK_EVENT,
194
+ scene: AgentType.AI_SDK_REQUEST_ERROR,
131
195
  method: "GET",
132
196
  url: options.url,
133
197
  error: error instanceof Error ? error.message : "请求失败",
134
198
  stack: error instanceof Error ? error.stack : undefined,
135
- duration: `${Date.now() - startTime}ms`,
199
+ duration: duration,
136
200
  eventId: this.context.eventID,
137
201
  timestamp: new Date().toISOString(),
138
202
  });
@@ -155,13 +219,14 @@ class AgentRuntime {
155
219
  logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
156
220
  try {
157
221
  const response = yield originalRequest.post(options);
222
+ const duration = `${Date.now() - startTime}ms`;
158
223
  const requestSuccessLog = safeJsonStringify({
159
224
  type: AgentEventType.FRAMEWORK_EVENT,
160
225
  scene: AgentType.AI_SDK_REQUEST_SUCCESS,
161
226
  method: "POST",
162
227
  url: options.url,
163
228
  status: response.statusCode,
164
- duration: `${Date.now() - startTime}ms`,
229
+ duration: duration,
165
230
  eventId: this.context.eventID,
166
231
  timestamp: new Date().toISOString(),
167
232
  });
@@ -169,6 +234,7 @@ class AgentRuntime {
169
234
  return response;
170
235
  }
171
236
  catch (error) {
237
+ const duration = `${Date.now() - startTime}ms`;
172
238
  const requestErrorLog = safeJsonStringify({
173
239
  type: AgentEventType.FRAMEWORK_EVENT,
174
240
  scene: AgentType.AI_SDK_REQUEST_ERROR,
@@ -176,7 +242,7 @@ class AgentRuntime {
176
242
  url: options.url,
177
243
  error: error instanceof Error ? error.message : "请求失败",
178
244
  stack: error instanceof Error ? error.stack : undefined,
179
- duration: `${Date.now() - startTime}ms`,
245
+ duration: duration,
180
246
  eventId: this.context.eventID,
181
247
  timestamp: new Date().toISOString(),
182
248
  });
@@ -199,13 +265,14 @@ class AgentRuntime {
199
265
  logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
200
266
  try {
201
267
  const response = yield originalRequest.put(options);
268
+ const duration = `${Date.now() - startTime}ms`;
202
269
  const requestSuccessLog = safeJsonStringify({
203
270
  type: AgentEventType.FRAMEWORK_EVENT,
204
271
  scene: AgentType.AI_SDK_REQUEST_SUCCESS,
205
272
  method: "PUT",
206
273
  url: options.url,
207
274
  status: response.statusCode,
208
- duration: `${Date.now() - startTime}ms`,
275
+ duration: duration,
209
276
  eventId: this.context.eventID,
210
277
  timestamp: new Date().toISOString(),
211
278
  });
@@ -213,6 +280,7 @@ class AgentRuntime {
213
280
  return response;
214
281
  }
215
282
  catch (error) {
283
+ const duration = `${Date.now() - startTime}ms`;
216
284
  const requestErrorLog = safeJsonStringify({
217
285
  type: AgentEventType.FRAMEWORK_EVENT,
218
286
  scene: AgentType.AI_SDK_REQUEST_ERROR,
@@ -220,7 +288,7 @@ class AgentRuntime {
220
288
  url: options.url,
221
289
  error: error instanceof Error ? error.message : "请求失败",
222
290
  stack: error instanceof Error ? error.stack : undefined,
223
- duration: `${Date.now() - startTime}ms`,
291
+ duration: duration,
224
292
  eventId: this.context.eventID,
225
293
  timestamp: new Date().toISOString(),
226
294
  });
@@ -242,13 +310,14 @@ class AgentRuntime {
242
310
  logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
243
311
  try {
244
312
  const response = yield originalRequest.upload(options);
313
+ const duration = `${Date.now() - startTime}ms`;
245
314
  const requestSuccessLog = safeJsonStringify({
246
315
  type: AgentEventType.FRAMEWORK_EVENT,
247
316
  scene: AgentType.AI_SDK_REQUEST_SUCCESS,
248
317
  method: "UPLOAD",
249
318
  url: options.url,
250
319
  status: response.statusCode,
251
- duration: `${Date.now() - startTime}ms`,
320
+ duration: duration,
252
321
  eventId: this.context.eventID,
253
322
  timestamp: new Date().toISOString(),
254
323
  });
@@ -256,6 +325,7 @@ class AgentRuntime {
256
325
  return response;
257
326
  }
258
327
  catch (error) {
328
+ const duration = `${Date.now() - startTime}ms`;
259
329
  const requestErrorLog = safeJsonStringify({
260
330
  type: AgentEventType.FRAMEWORK_EVENT,
261
331
  scene: AgentType.AI_SDK_REQUEST_ERROR,
@@ -263,7 +333,7 @@ class AgentRuntime {
263
333
  url: options.url,
264
334
  error: error instanceof Error ? error.message : "请求失败",
265
335
  stack: error instanceof Error ? error.stack : undefined,
266
- duration: `${Date.now() - startTime}ms`,
336
+ duration: duration,
267
337
  eventId: this.context.eventID,
268
338
  timestamp: new Date().toISOString(),
269
339
  });
@@ -285,12 +355,15 @@ class AgentRuntime {
285
355
  logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
286
356
  try {
287
357
  const response = yield originalRequest.download(options);
358
+ const duration = `${Date.now() - startTime}ms`;
359
+ const statusCode = response === null || response === void 0 ? void 0 : response.statusCode;
288
360
  const requestSuccessLog = safeJsonStringify({
289
361
  type: AgentEventType.FRAMEWORK_EVENT,
290
362
  scene: AgentType.AI_SDK_REQUEST_SUCCESS,
291
363
  method: "DOWNLOAD",
292
364
  url: options.url,
293
- duration: `${Date.now() - startTime}ms`,
365
+ status: statusCode,
366
+ duration: duration,
294
367
  eventId: this.context.eventID,
295
368
  timestamp: new Date().toISOString(),
296
369
  });
@@ -298,6 +371,7 @@ class AgentRuntime {
298
371
  return response;
299
372
  }
300
373
  catch (error) {
374
+ const duration = `${Date.now() - startTime}ms`;
301
375
  const requestErrorLog = safeJsonStringify({
302
376
  type: AgentEventType.FRAMEWORK_EVENT,
303
377
  scene: AgentType.AI_SDK_REQUEST_ERROR,
@@ -305,7 +379,7 @@ class AgentRuntime {
305
379
  url: options.url,
306
380
  error: error instanceof Error ? error.message : "请求失败",
307
381
  stack: error instanceof Error ? error.stack : undefined,
308
- duration: `${Date.now() - startTime}ms`,
382
+ duration: duration,
309
383
  eventId: this.context.eventID,
310
384
  timestamp: new Date().toISOString(),
311
385
  });
@@ -328,20 +402,33 @@ class AgentRuntime {
328
402
  logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestStartLog);
329
403
  try {
330
404
  const response = yield originalRequest.fetch(options);
331
- const requestSuccessLog = safeJsonStringify({
405
+ const rt = Date.now() - startTime;
406
+ const duration = `${rt}ms`;
407
+ const isSuccess = (response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300;
408
+ const logData = {
332
409
  type: AgentEventType.FRAMEWORK_EVENT,
333
- scene: AgentType.AI_SDK_REQUEST_SUCCESS,
410
+ scene: isSuccess ? AgentType.AI_SDK_REQUEST_SUCCESS : AgentType.AI_SDK_REQUEST_ERROR,
334
411
  method: "FETCH",
335
412
  url: options.url,
336
- status: response.statusCode,
337
- duration: `${Date.now() - startTime}ms`,
413
+ duration: duration,
338
414
  eventId: this.context.eventID,
339
415
  timestamp: new Date().toISOString(),
340
- });
341
- logger.logAccesslog(functions_framework_1.LogLevel.INFO, requestSuccessLog);
416
+ };
417
+ if (isSuccess) {
418
+ logData.status = response.statusCode;
419
+ }
420
+ else {
421
+ logData.error = `Request failed with status code: ${response === null || response === void 0 ? void 0 : response.statusCode}`;
422
+ }
423
+ this.updateModelApmData(options.url, response === null || response === void 0 ? void 0 : response.statusCode, rt);
424
+ const requestLog = safeJsonStringify(logData);
425
+ logger.logAccesslog(isSuccess ? functions_framework_1.LogLevel.INFO : functions_framework_1.LogLevel.ERROR, requestLog);
342
426
  return response;
343
427
  }
344
428
  catch (error) {
429
+ const rt = Date.now() - startTime;
430
+ const duration = `${rt}ms`;
431
+ this.updateModelApmData(options.url, undefined, rt);
345
432
  const requestErrorLog = safeJsonStringify({
346
433
  type: AgentEventType.FRAMEWORK_EVENT,
347
434
  scene: AgentType.AI_SDK_REQUEST_ERROR,
@@ -349,7 +436,7 @@ class AgentRuntime {
349
436
  url: options.url,
350
437
  error: error instanceof Error ? error.message : "请求失败",
351
438
  stack: error instanceof Error ? error.stack : undefined,
352
- duration: `${Date.now() - startTime}ms`,
439
+ duration: duration,
353
440
  eventId: this.context.eventID,
354
441
  timestamp: new Date().toISOString(),
355
442
  });
@@ -360,6 +447,10 @@ class AgentRuntime {
360
447
  };
361
448
  }
362
449
  createModel(modelName) {
450
+ this.currentModelName = modelName;
451
+ (0, functions_framework_1.updateApmMeasurementData)({
452
+ kit_model_name: modelName,
453
+ });
363
454
  const createModelLog = safeJsonStringify({
364
455
  type: AgentEventType.FRAMEWORK_EVENT,
365
456
  scene: AgentType.AI_SDK_CREATE_MODEL,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectorx/agent-runtime",
3
- "version": "0.6.2",
3
+ "version": "0.8.0",
4
4
  "description": "Cloud AI agent runtime",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -20,8 +20,8 @@
20
20
  "node": ">=18.0.0"
21
21
  },
22
22
  "dependencies": {
23
- "@vectorx/ai-sdk": "0.6.2",
24
- "@vectorx/functions-framework": "0.6.2",
23
+ "@vectorx/ai-sdk": "0.8.0",
24
+ "@vectorx/functions-framework": "0.8.0",
25
25
  "form-data": "^4.0.0",
26
26
  "langfuse": "^3.38.4",
27
27
  "query-string": "^6.14.1",