@elizaos/plugin-sql 1.6.0-beta.1 → 1.6.1-alpha.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.
@@ -6314,6 +6314,176 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
6314
6314
  return logs;
6315
6315
  });
6316
6316
  }
6317
+ async getAgentRunSummaries(params = {}) {
6318
+ const limit = Math.min(Math.max(params.limit ?? 20, 1), 100);
6319
+ const fromDate = typeof params.from === "number" ? new Date(params.from) : undefined;
6320
+ const toDate = typeof params.to === "number" ? new Date(params.to) : undefined;
6321
+ return this.withDatabase(async () => {
6322
+ const runMap = new Map;
6323
+ const conditions = [
6324
+ eq(logTable.type, "run_event"),
6325
+ sql`${logTable.body} ? 'runId'`,
6326
+ eq(roomTable.agentId, this.agentId)
6327
+ ];
6328
+ if (params.roomId) {
6329
+ conditions.push(eq(logTable.roomId, params.roomId));
6330
+ }
6331
+ if (fromDate) {
6332
+ conditions.push(gte(logTable.createdAt, fromDate));
6333
+ }
6334
+ if (toDate) {
6335
+ conditions.push(lte(logTable.createdAt, toDate));
6336
+ }
6337
+ const whereClause = and(...conditions);
6338
+ const eventLimit = Math.max(limit * 20, 200);
6339
+ const runEventRows = await this.db.select({
6340
+ runId: sql`(${logTable.body} ->> 'runId')`,
6341
+ status: sql`(${logTable.body} ->> 'status')`,
6342
+ messageId: sql`(${logTable.body} ->> 'messageId')`,
6343
+ rawBody: logTable.body,
6344
+ createdAt: logTable.createdAt,
6345
+ roomId: logTable.roomId,
6346
+ entityId: logTable.entityId
6347
+ }).from(logTable).innerJoin(roomTable, eq(roomTable.id, logTable.roomId)).where(whereClause).orderBy(desc(logTable.createdAt)).limit(eventLimit);
6348
+ for (const row of runEventRows) {
6349
+ const runId = row.runId;
6350
+ if (!runId)
6351
+ continue;
6352
+ const summary = runMap.get(runId) ?? {
6353
+ runId,
6354
+ status: "started",
6355
+ startedAt: null,
6356
+ endedAt: null,
6357
+ durationMs: null,
6358
+ messageId: undefined,
6359
+ roomId: undefined,
6360
+ entityId: undefined,
6361
+ metadata: {}
6362
+ };
6363
+ if (!summary.messageId && row.messageId) {
6364
+ summary.messageId = row.messageId;
6365
+ }
6366
+ if (!summary.roomId && row.roomId) {
6367
+ summary.roomId = row.roomId;
6368
+ }
6369
+ if (!summary.entityId && row.entityId) {
6370
+ summary.entityId = row.entityId;
6371
+ }
6372
+ const body = row.rawBody;
6373
+ if (body && typeof body === "object") {
6374
+ if (!summary.roomId && typeof body.roomId === "string") {
6375
+ summary.roomId = body.roomId;
6376
+ }
6377
+ if (!summary.entityId && typeof body.entityId === "string") {
6378
+ summary.entityId = body.entityId;
6379
+ }
6380
+ if (!summary.messageId && typeof body.messageId === "string") {
6381
+ summary.messageId = body.messageId;
6382
+ }
6383
+ if (!summary.metadata || Object.keys(summary.metadata).length === 0) {
6384
+ const metadata = body.metadata ?? undefined;
6385
+ summary.metadata = metadata ? { ...metadata } : {};
6386
+ }
6387
+ }
6388
+ const createdAt = row.createdAt instanceof Date ? row.createdAt : new Date(row.createdAt);
6389
+ const timestamp2 = createdAt.getTime();
6390
+ const eventStatus = row.status ?? body?.status;
6391
+ if (eventStatus === "started") {
6392
+ summary.startedAt = summary.startedAt === null ? timestamp2 : Math.min(summary.startedAt, timestamp2);
6393
+ } else if (eventStatus === "completed" || eventStatus === "timeout" || eventStatus === "error") {
6394
+ summary.status = eventStatus;
6395
+ summary.endedAt = timestamp2;
6396
+ if (summary.startedAt !== null) {
6397
+ summary.durationMs = Math.max(timestamp2 - summary.startedAt, 0);
6398
+ }
6399
+ }
6400
+ runMap.set(runId, summary);
6401
+ }
6402
+ let runs = Array.from(runMap.values());
6403
+ if (params.status && params.status !== "all") {
6404
+ runs = runs.filter((run) => run.status === params.status);
6405
+ }
6406
+ runs.sort((a, b) => (b.startedAt ?? 0) - (a.startedAt ?? 0));
6407
+ const total = runs.length;
6408
+ const limitedRuns = runs.slice(0, limit);
6409
+ const hasMore = total > limit;
6410
+ const runCounts = new Map;
6411
+ for (const run of limitedRuns) {
6412
+ runCounts.set(run.runId, { actions: 0, modelCalls: 0, errors: 0, evaluators: 0 });
6413
+ }
6414
+ const runIds = limitedRuns.map((run) => run.runId).filter(Boolean);
6415
+ if (runIds.length > 0) {
6416
+ const runIdArray = sql`array[${sql.join(runIds.map((id) => sql`${id}`), sql`, `)}]::text[]`;
6417
+ const actionSummary = await this.db.execute(sql`
6418
+ SELECT
6419
+ body->>'runId' as "runId",
6420
+ COUNT(*)::int as "actions",
6421
+ SUM(CASE WHEN COALESCE(body->'result'->>'success', 'true') = 'false' THEN 1 ELSE 0 END)::int as "errors",
6422
+ SUM(COALESCE((body->>'promptCount')::int, 0))::int as "modelCalls"
6423
+ FROM ${logTable}
6424
+ WHERE type = 'action'
6425
+ AND body->>'runId' = ANY(${runIdArray})
6426
+ GROUP BY body->>'runId'
6427
+ `);
6428
+ const actionRows = actionSummary.rows ?? [];
6429
+ for (const row of actionRows) {
6430
+ const counts = runCounts.get(row.runId);
6431
+ if (!counts)
6432
+ continue;
6433
+ counts.actions += Number(row.actions ?? 0);
6434
+ counts.errors += Number(row.errors ?? 0);
6435
+ counts.modelCalls += Number(row.modelCalls ?? 0);
6436
+ }
6437
+ const evaluatorSummary = await this.db.execute(sql`
6438
+ SELECT
6439
+ body->>'runId' as "runId",
6440
+ COUNT(*)::int as "evaluators"
6441
+ FROM ${logTable}
6442
+ WHERE type = 'evaluator'
6443
+ AND body->>'runId' = ANY(${runIdArray})
6444
+ GROUP BY body->>'runId'
6445
+ `);
6446
+ const evaluatorRows = evaluatorSummary.rows ?? [];
6447
+ for (const row of evaluatorRows) {
6448
+ const counts = runCounts.get(row.runId);
6449
+ if (!counts)
6450
+ continue;
6451
+ counts.evaluators += Number(row.evaluators ?? 0);
6452
+ }
6453
+ const genericSummary = await this.db.execute(sql`
6454
+ SELECT
6455
+ body->>'runId' as "runId",
6456
+ COUNT(*) FILTER (WHERE type LIKE 'useModel:%')::int as "modelLogs",
6457
+ COUNT(*) FILTER (WHERE type = 'embedding_event' AND body->>'status' = 'failed')::int as "embeddingErrors"
6458
+ FROM ${logTable}
6459
+ WHERE (type LIKE 'useModel:%' OR type = 'embedding_event')
6460
+ AND body->>'runId' = ANY(${runIdArray})
6461
+ GROUP BY body->>'runId'
6462
+ `);
6463
+ const genericRows = genericSummary.rows ?? [];
6464
+ for (const row of genericRows) {
6465
+ const counts = runCounts.get(row.runId);
6466
+ if (!counts)
6467
+ continue;
6468
+ counts.modelCalls += Number(row.modelLogs ?? 0);
6469
+ counts.errors += Number(row.embeddingErrors ?? 0);
6470
+ }
6471
+ }
6472
+ for (const run of limitedRuns) {
6473
+ run.counts = runCounts.get(run.runId) ?? {
6474
+ actions: 0,
6475
+ modelCalls: 0,
6476
+ errors: 0,
6477
+ evaluators: 0
6478
+ };
6479
+ }
6480
+ return {
6481
+ runs: limitedRuns,
6482
+ total,
6483
+ hasMore
6484
+ };
6485
+ });
6486
+ }
6317
6487
  async deleteLog(logId) {
6318
6488
  return this.withDatabase(async () => {
6319
6489
  await this.db.delete(logTable).where(eq(logTable.id, logId));
@@ -8861,5 +9031,5 @@ export {
8861
9031
  DatabaseMigrationService
8862
9032
  };
8863
9033
 
8864
- //# debugId=16FBE8772BAA544764756E2164756E21
9034
+ //# debugId=6BE7CE80F1F0063A64756E2164756E21
8865
9035
  //# sourceMappingURL=index.node.js.map