@mastra/pg 0.17.8 → 1.0.0-beta.1
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/CHANGELOG.md +231 -23
- package/README.md +69 -20
- package/dist/index.cjs +276 -591
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +276 -591
- package/dist/index.js.map +1 -1
- package/dist/shared/config.d.ts +1 -0
- package/dist/shared/config.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +14 -45
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +16 -16
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/operations/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +5 -5
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +4 -10
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +34 -93
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/performance-indexes/performance-test.d.ts.map +1 -1
- package/dist/storage/test-utils.d.ts.map +1 -1
- package/dist/vector/index.d.ts +3 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +13 -15
- package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
- package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
- package/dist/storage/domains/traces/index.d.ts +0 -23
- package/dist/storage/domains/traces/index.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -5,10 +5,10 @@ import { Mutex } from 'async-mutex';
|
|
|
5
5
|
import * as pg from 'pg';
|
|
6
6
|
import xxhash from 'xxhash-wasm';
|
|
7
7
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
8
|
-
import { MastraStorage, StoreOperations, TABLE_SCHEMAS, TABLE_WORKFLOW_SNAPSHOT,
|
|
8
|
+
import { MastraStorage, StoreOperations, TABLE_SCHEMAS, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, TABLE_THREADS, TABLE_MESSAGES, TABLE_TRACES, TABLE_SCORERS, ScoresStorage, normalizePerPage, calculatePagination, WorkflowsStorage, MemoryStorage, TABLE_RESOURCES, ObservabilityStorage, safelyParseJSON } from '@mastra/core/storage';
|
|
9
9
|
import pgPromise from 'pg-promise';
|
|
10
10
|
import { MessageList } from '@mastra/core/agent';
|
|
11
|
-
import { saveScorePayloadSchema } from '@mastra/core/
|
|
11
|
+
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
12
12
|
|
|
13
13
|
// src/vector/index.ts
|
|
14
14
|
|
|
@@ -23,6 +23,9 @@ var isCloudSqlConfig = (cfg) => {
|
|
|
23
23
|
return "stream" in cfg || "password" in cfg && typeof cfg.password === "function";
|
|
24
24
|
};
|
|
25
25
|
var validateConfig = (name, config) => {
|
|
26
|
+
if (!config.id || typeof config.id !== "string" || config.id.trim() === "") {
|
|
27
|
+
throw new Error(`${name}: id must be provided and cannot be empty.`);
|
|
28
|
+
}
|
|
26
29
|
if (isConnectionStringConfig(config)) {
|
|
27
30
|
if (!config.connectionString || typeof config.connectionString !== "string" || config.connectionString.trim() === "") {
|
|
28
31
|
throw new Error(
|
|
@@ -403,7 +406,7 @@ var PgVector = class extends MastraVector {
|
|
|
403
406
|
constructor(config) {
|
|
404
407
|
try {
|
|
405
408
|
validateConfig("PgVector", config);
|
|
406
|
-
super();
|
|
409
|
+
super({ id: config.id });
|
|
407
410
|
this.schema = config.schemaName;
|
|
408
411
|
let poolConfig;
|
|
409
412
|
if (isConnectionStringConfig(config)) {
|
|
@@ -439,14 +442,7 @@ var PgVector = class extends MastraVector {
|
|
|
439
442
|
} else {
|
|
440
443
|
throw new Error("PgVector: invalid configuration provided");
|
|
441
444
|
}
|
|
442
|
-
|
|
443
|
-
const telemetry = this.__getTelemetry();
|
|
444
|
-
this.pool = telemetry?.traceClass(basePool, {
|
|
445
|
-
spanNamePrefix: "pg-vector",
|
|
446
|
-
attributes: {
|
|
447
|
-
"vector.type": "postgres"
|
|
448
|
-
}
|
|
449
|
-
}) ?? basePool;
|
|
445
|
+
this.pool = new pg.Pool(poolConfig);
|
|
450
446
|
this.cacheWarmupPromise = (async () => {
|
|
451
447
|
try {
|
|
452
448
|
const existingIndexes = await this.listIndexes();
|
|
@@ -723,7 +719,7 @@ var PgVector = class extends MastraVector {
|
|
|
723
719
|
const schemaCheck = await client.query(
|
|
724
720
|
`
|
|
725
721
|
SELECT EXISTS (
|
|
726
|
-
SELECT 1 FROM information_schema.schemata
|
|
722
|
+
SELECT 1 FROM information_schema.schemata
|
|
727
723
|
WHERE schema_name = $1
|
|
728
724
|
)
|
|
729
725
|
`,
|
|
@@ -920,8 +916,8 @@ var PgVector = class extends MastraVector {
|
|
|
920
916
|
const m = indexConfig.hnsw?.m ?? 8;
|
|
921
917
|
const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
|
|
922
918
|
indexSQL = `
|
|
923
|
-
CREATE INDEX IF NOT EXISTS ${vectorIndexName}
|
|
924
|
-
ON ${tableName}
|
|
919
|
+
CREATE INDEX IF NOT EXISTS ${vectorIndexName}
|
|
920
|
+
ON ${tableName}
|
|
925
921
|
USING hnsw (embedding ${metricOp})
|
|
926
922
|
WITH (
|
|
927
923
|
m = ${m},
|
|
@@ -1372,123 +1368,7 @@ function transformFromSqlRow({
|
|
|
1372
1368
|
return result;
|
|
1373
1369
|
}
|
|
1374
1370
|
|
|
1375
|
-
// src/storage/domains/
|
|
1376
|
-
function transformEvalRow(row) {
|
|
1377
|
-
let testInfoValue = null;
|
|
1378
|
-
if (row.test_info) {
|
|
1379
|
-
try {
|
|
1380
|
-
testInfoValue = typeof row.test_info === "string" ? JSON.parse(row.test_info) : row.test_info;
|
|
1381
|
-
} catch (e) {
|
|
1382
|
-
console.warn("Failed to parse test_info:", e);
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
return {
|
|
1386
|
-
agentName: row.agent_name,
|
|
1387
|
-
input: row.input,
|
|
1388
|
-
output: row.output,
|
|
1389
|
-
result: row.result,
|
|
1390
|
-
metricName: row.metric_name,
|
|
1391
|
-
instructions: row.instructions,
|
|
1392
|
-
testInfo: testInfoValue,
|
|
1393
|
-
globalRunId: row.global_run_id,
|
|
1394
|
-
runId: row.run_id,
|
|
1395
|
-
createdAt: row.created_atZ || row.created_at
|
|
1396
|
-
};
|
|
1397
|
-
}
|
|
1398
|
-
var LegacyEvalsPG = class extends LegacyEvalsStorage {
|
|
1399
|
-
client;
|
|
1400
|
-
schema;
|
|
1401
|
-
constructor({ client, schema }) {
|
|
1402
|
-
super();
|
|
1403
|
-
this.client = client;
|
|
1404
|
-
this.schema = schema;
|
|
1405
|
-
}
|
|
1406
|
-
/** @deprecated use getEvals instead */
|
|
1407
|
-
async getEvalsByAgentName(agentName, type) {
|
|
1408
|
-
try {
|
|
1409
|
-
const baseQuery = `SELECT * FROM ${getTableName({ indexName: TABLE_EVALS, schemaName: getSchemaName(this.schema) })} WHERE agent_name = $1`;
|
|
1410
|
-
const typeCondition = type === "test" ? " AND test_info IS NOT NULL AND test_info->>'testPath' IS NOT NULL" : type === "live" ? " AND (test_info IS NULL OR test_info->>'testPath' IS NULL)" : "";
|
|
1411
|
-
const query = `${baseQuery}${typeCondition} ORDER BY created_at DESC`;
|
|
1412
|
-
const rows = await this.client.manyOrNone(query, [agentName]);
|
|
1413
|
-
return rows?.map((row) => transformEvalRow(row)) ?? [];
|
|
1414
|
-
} catch (error) {
|
|
1415
|
-
if (error instanceof Error && error.message.includes("relation") && error.message.includes("does not exist")) {
|
|
1416
|
-
return [];
|
|
1417
|
-
}
|
|
1418
|
-
console.error("Failed to get evals for the specified agent: " + error?.message);
|
|
1419
|
-
throw error;
|
|
1420
|
-
}
|
|
1421
|
-
}
|
|
1422
|
-
async getEvals(options = {}) {
|
|
1423
|
-
const tableName = getTableName({ indexName: TABLE_EVALS, schemaName: getSchemaName(this.schema) });
|
|
1424
|
-
const { agentName, type, page = 0, perPage = 100, dateRange } = options;
|
|
1425
|
-
const fromDate = dateRange?.start;
|
|
1426
|
-
const toDate = dateRange?.end;
|
|
1427
|
-
const conditions = [];
|
|
1428
|
-
const queryParams = [];
|
|
1429
|
-
let paramIndex = 1;
|
|
1430
|
-
if (agentName) {
|
|
1431
|
-
conditions.push(`agent_name = $${paramIndex++}`);
|
|
1432
|
-
queryParams.push(agentName);
|
|
1433
|
-
}
|
|
1434
|
-
if (type === "test") {
|
|
1435
|
-
conditions.push(`(test_info IS NOT NULL AND test_info->>'testPath' IS NOT NULL)`);
|
|
1436
|
-
} else if (type === "live") {
|
|
1437
|
-
conditions.push(`(test_info IS NULL OR test_info->>'testPath' IS NULL)`);
|
|
1438
|
-
}
|
|
1439
|
-
if (fromDate) {
|
|
1440
|
-
conditions.push(`created_at >= $${paramIndex++}`);
|
|
1441
|
-
queryParams.push(fromDate);
|
|
1442
|
-
}
|
|
1443
|
-
if (toDate) {
|
|
1444
|
-
conditions.push(`created_at <= $${paramIndex++}`);
|
|
1445
|
-
queryParams.push(toDate);
|
|
1446
|
-
}
|
|
1447
|
-
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
1448
|
-
const countQuery = `SELECT COUNT(*) FROM ${tableName} ${whereClause}`;
|
|
1449
|
-
try {
|
|
1450
|
-
const countResult = await this.client.one(countQuery, queryParams);
|
|
1451
|
-
const total = parseInt(countResult.count, 10);
|
|
1452
|
-
const currentOffset = page * perPage;
|
|
1453
|
-
if (total === 0) {
|
|
1454
|
-
return {
|
|
1455
|
-
evals: [],
|
|
1456
|
-
total: 0,
|
|
1457
|
-
page,
|
|
1458
|
-
perPage,
|
|
1459
|
-
hasMore: false
|
|
1460
|
-
};
|
|
1461
|
-
}
|
|
1462
|
-
const dataQuery = `SELECT * FROM ${tableName} ${whereClause} ORDER BY created_at DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1463
|
-
const rows = await this.client.manyOrNone(dataQuery, [...queryParams, perPage, currentOffset]);
|
|
1464
|
-
return {
|
|
1465
|
-
evals: rows?.map((row) => transformEvalRow(row)) ?? [],
|
|
1466
|
-
total,
|
|
1467
|
-
page,
|
|
1468
|
-
perPage,
|
|
1469
|
-
hasMore: currentOffset + (rows?.length ?? 0) < total
|
|
1470
|
-
};
|
|
1471
|
-
} catch (error) {
|
|
1472
|
-
const mastraError = new MastraError(
|
|
1473
|
-
{
|
|
1474
|
-
id: "MASTRA_STORAGE_PG_STORE_GET_EVALS_FAILED",
|
|
1475
|
-
domain: ErrorDomain.STORAGE,
|
|
1476
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1477
|
-
details: {
|
|
1478
|
-
agentName: agentName || "all",
|
|
1479
|
-
type: type || "all",
|
|
1480
|
-
page,
|
|
1481
|
-
perPage
|
|
1482
|
-
}
|
|
1483
|
-
},
|
|
1484
|
-
error
|
|
1485
|
-
);
|
|
1486
|
-
this.logger?.error?.(mastraError.toString());
|
|
1487
|
-
this.logger?.trackException(mastraError);
|
|
1488
|
-
throw mastraError;
|
|
1489
|
-
}
|
|
1490
|
-
}
|
|
1491
|
-
};
|
|
1371
|
+
// src/storage/domains/memory/index.ts
|
|
1492
1372
|
var MemoryPG = class extends MemoryStorage {
|
|
1493
1373
|
client;
|
|
1494
1374
|
schema;
|
|
@@ -1549,40 +1429,27 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1549
1429
|
);
|
|
1550
1430
|
}
|
|
1551
1431
|
}
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
return (rows || []).map((thread) => ({
|
|
1566
|
-
...thread,
|
|
1567
|
-
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
1568
|
-
createdAt: thread.createdAt,
|
|
1569
|
-
updatedAt: thread.updatedAt
|
|
1570
|
-
}));
|
|
1571
|
-
} catch (error) {
|
|
1572
|
-
this.logger.error(`Error getting threads for resource ${resourceId}:`, error);
|
|
1573
|
-
return [];
|
|
1432
|
+
async listThreadsByResourceId(args) {
|
|
1433
|
+
const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
|
|
1434
|
+
if (page < 0) {
|
|
1435
|
+
throw new MastraError({
|
|
1436
|
+
id: "MASTRA_STORAGE_PG_STORE_INVALID_PAGE",
|
|
1437
|
+
domain: ErrorDomain.STORAGE,
|
|
1438
|
+
category: ErrorCategory.USER,
|
|
1439
|
+
text: "Page number must be non-negative",
|
|
1440
|
+
details: {
|
|
1441
|
+
resourceId,
|
|
1442
|
+
page
|
|
1443
|
+
}
|
|
1444
|
+
});
|
|
1574
1445
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
const {
|
|
1578
|
-
const orderBy = this.castThreadOrderBy(args.orderBy);
|
|
1579
|
-
const sortDirection = this.castThreadSortDirection(args.sortDirection);
|
|
1446
|
+
const { field, direction } = this.parseOrderBy(orderBy);
|
|
1447
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
1448
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1580
1449
|
try {
|
|
1581
1450
|
const tableName = getTableName({ indexName: TABLE_THREADS, schemaName: getSchemaName(this.schema) });
|
|
1582
1451
|
const baseQuery = `FROM ${tableName} WHERE "resourceId" = $1`;
|
|
1583
1452
|
const queryParams = [resourceId];
|
|
1584
|
-
const perPage = perPageInput !== void 0 ? perPageInput : 100;
|
|
1585
|
-
const currentOffset = page * perPage;
|
|
1586
1453
|
const countQuery = `SELECT COUNT(*) ${baseQuery}`;
|
|
1587
1454
|
const countResult = await this.client.one(countQuery, queryParams);
|
|
1588
1455
|
const total = parseInt(countResult.count, 10);
|
|
@@ -1591,12 +1458,13 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1591
1458
|
threads: [],
|
|
1592
1459
|
total: 0,
|
|
1593
1460
|
page,
|
|
1594
|
-
perPage,
|
|
1461
|
+
perPage: perPageForResponse,
|
|
1595
1462
|
hasMore: false
|
|
1596
1463
|
};
|
|
1597
1464
|
}
|
|
1598
|
-
const
|
|
1599
|
-
const
|
|
1465
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1466
|
+
const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "updatedAt" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`;
|
|
1467
|
+
const rows = await this.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
|
|
1600
1468
|
const threads = (rows || []).map((thread) => ({
|
|
1601
1469
|
...thread,
|
|
1602
1470
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
@@ -1608,13 +1476,13 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1608
1476
|
threads,
|
|
1609
1477
|
total,
|
|
1610
1478
|
page,
|
|
1611
|
-
perPage,
|
|
1612
|
-
hasMore:
|
|
1479
|
+
perPage: perPageForResponse,
|
|
1480
|
+
hasMore: perPageInput === false ? false : offset + perPage < total
|
|
1613
1481
|
};
|
|
1614
1482
|
} catch (error) {
|
|
1615
1483
|
const mastraError = new MastraError(
|
|
1616
1484
|
{
|
|
1617
|
-
id: "
|
|
1485
|
+
id: "MASTRA_STORAGE_PG_STORE_LIST_THREADS_BY_RESOURCE_ID_FAILED",
|
|
1618
1486
|
domain: ErrorDomain.STORAGE,
|
|
1619
1487
|
category: ErrorCategory.THIRD_PARTY,
|
|
1620
1488
|
details: {
|
|
@@ -1626,7 +1494,13 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1626
1494
|
);
|
|
1627
1495
|
this.logger?.error?.(mastraError.toString());
|
|
1628
1496
|
this.logger?.trackException(mastraError);
|
|
1629
|
-
return {
|
|
1497
|
+
return {
|
|
1498
|
+
threads: [],
|
|
1499
|
+
total: 0,
|
|
1500
|
+
page,
|
|
1501
|
+
perPage: perPageForResponse,
|
|
1502
|
+
hasMore: false
|
|
1503
|
+
};
|
|
1630
1504
|
}
|
|
1631
1505
|
}
|
|
1632
1506
|
async saveThread({ thread }) {
|
|
@@ -1760,11 +1634,9 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1760
1634
|
}
|
|
1761
1635
|
async _getIncludedMessages({
|
|
1762
1636
|
threadId,
|
|
1763
|
-
|
|
1764
|
-
orderByStatement
|
|
1637
|
+
include
|
|
1765
1638
|
}) {
|
|
1766
1639
|
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1767
|
-
const include = selectBy?.include;
|
|
1768
1640
|
if (!include) return null;
|
|
1769
1641
|
const unionQueries = [];
|
|
1770
1642
|
const params = [];
|
|
@@ -1779,7 +1651,7 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1779
1651
|
WITH ordered_messages AS (
|
|
1780
1652
|
SELECT
|
|
1781
1653
|
*,
|
|
1782
|
-
ROW_NUMBER() OVER (
|
|
1654
|
+
ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
|
|
1783
1655
|
FROM ${tableName}
|
|
1784
1656
|
WHERE thread_id = $${paramIdx}
|
|
1785
1657
|
)
|
|
@@ -1798,11 +1670,11 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1798
1670
|
SELECT 1 FROM ordered_messages target
|
|
1799
1671
|
WHERE target.id = $${paramIdx + 1}
|
|
1800
1672
|
AND (
|
|
1801
|
-
-- Get previous messages
|
|
1802
|
-
(m.row_num
|
|
1673
|
+
-- Get previous messages (messages that come BEFORE the target)
|
|
1674
|
+
(m.row_num < target.row_num AND m.row_num >= target.row_num - $${paramIdx + 2})
|
|
1803
1675
|
OR
|
|
1804
|
-
-- Get next messages
|
|
1805
|
-
(m.row_num
|
|
1676
|
+
-- Get next messages (messages that come AFTER the target)
|
|
1677
|
+
(m.row_num > target.row_num AND m.row_num <= target.row_num + $${paramIdx + 3})
|
|
1806
1678
|
)
|
|
1807
1679
|
)
|
|
1808
1680
|
) AS query_${paramIdx}
|
|
@@ -1839,72 +1711,8 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1839
1711
|
...normalized.type && normalized.type !== "v2" ? { type: normalized.type } : {}
|
|
1840
1712
|
};
|
|
1841
1713
|
}
|
|
1842
|
-
async
|
|
1843
|
-
|
|
1844
|
-
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
1845
|
-
const orderByStatement = `ORDER BY "createdAt" DESC`;
|
|
1846
|
-
const limit = resolveMessageLimit({ last: selectBy?.last, defaultLimit: 40 });
|
|
1847
|
-
try {
|
|
1848
|
-
if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
|
|
1849
|
-
let rows = [];
|
|
1850
|
-
const include = selectBy?.include || [];
|
|
1851
|
-
if (include?.length) {
|
|
1852
|
-
const includeMessages = await this._getIncludedMessages({ threadId, selectBy, orderByStatement });
|
|
1853
|
-
if (includeMessages) {
|
|
1854
|
-
rows.push(...includeMessages);
|
|
1855
|
-
}
|
|
1856
|
-
}
|
|
1857
|
-
const excludeIds = rows.map((m) => m.id);
|
|
1858
|
-
const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
|
|
1859
|
-
const excludeIdsParam = excludeIds.map((_, idx) => `$${idx + 2}`).join(", ");
|
|
1860
|
-
let query = `${selectStatement} FROM ${tableName} WHERE thread_id = $1
|
|
1861
|
-
${excludeIds.length ? `AND id NOT IN (${excludeIdsParam})` : ""}
|
|
1862
|
-
${orderByStatement}
|
|
1863
|
-
LIMIT $${excludeIds.length + 2}
|
|
1864
|
-
`;
|
|
1865
|
-
const queryParams = [threadId, ...excludeIds, limit];
|
|
1866
|
-
const remainingRows = await this.client.manyOrNone(query, queryParams);
|
|
1867
|
-
rows.push(...remainingRows);
|
|
1868
|
-
const fetchedMessages = (rows || []).map((row) => {
|
|
1869
|
-
const message = this.normalizeMessageRow(row);
|
|
1870
|
-
if (typeof message.content === "string") {
|
|
1871
|
-
try {
|
|
1872
|
-
message.content = JSON.parse(message.content);
|
|
1873
|
-
} catch {
|
|
1874
|
-
}
|
|
1875
|
-
}
|
|
1876
|
-
if (message.type === "v2") delete message.type;
|
|
1877
|
-
return message;
|
|
1878
|
-
});
|
|
1879
|
-
const sortedMessages = fetchedMessages.sort(
|
|
1880
|
-
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
|
|
1881
|
-
);
|
|
1882
|
-
return format === "v2" ? sortedMessages.map(
|
|
1883
|
-
(m) => ({ ...m, content: m.content || { format: 2, parts: [{ type: "text", text: "" }] } })
|
|
1884
|
-
) : sortedMessages;
|
|
1885
|
-
} catch (error) {
|
|
1886
|
-
const mastraError = new MastraError(
|
|
1887
|
-
{
|
|
1888
|
-
id: "MASTRA_STORAGE_PG_STORE_GET_MESSAGES_FAILED",
|
|
1889
|
-
domain: ErrorDomain.STORAGE,
|
|
1890
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
1891
|
-
details: {
|
|
1892
|
-
threadId,
|
|
1893
|
-
resourceId: resourceId ?? ""
|
|
1894
|
-
}
|
|
1895
|
-
},
|
|
1896
|
-
error
|
|
1897
|
-
);
|
|
1898
|
-
this.logger?.error?.(mastraError.toString());
|
|
1899
|
-
this.logger?.trackException(mastraError);
|
|
1900
|
-
return [];
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1903
|
-
async getMessagesById({
|
|
1904
|
-
messageIds,
|
|
1905
|
-
format
|
|
1906
|
-
}) {
|
|
1907
|
-
if (messageIds.length === 0) return [];
|
|
1714
|
+
async listMessagesById({ messageIds }) {
|
|
1715
|
+
if (messageIds.length === 0) return { messages: [] };
|
|
1908
1716
|
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
1909
1717
|
try {
|
|
1910
1718
|
const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
|
|
@@ -1918,12 +1726,11 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1918
1726
|
resultRows.map((row) => this.parseRow(row)),
|
|
1919
1727
|
"memory"
|
|
1920
1728
|
);
|
|
1921
|
-
|
|
1922
|
-
return list.get.all.v2();
|
|
1729
|
+
return { messages: list.get.all.db() };
|
|
1923
1730
|
} catch (error) {
|
|
1924
1731
|
const mastraError = new MastraError(
|
|
1925
1732
|
{
|
|
1926
|
-
id: "
|
|
1733
|
+
id: "MASTRA_STORAGE_PG_STORE_LIST_MESSAGES_BY_ID_FAILED",
|
|
1927
1734
|
domain: ErrorDomain.STORAGE,
|
|
1928
1735
|
category: ErrorCategory.THIRD_PARTY,
|
|
1929
1736
|
details: {
|
|
@@ -1934,102 +1741,138 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
1934
1741
|
);
|
|
1935
1742
|
this.logger?.error?.(mastraError.toString());
|
|
1936
1743
|
this.logger?.trackException(mastraError);
|
|
1937
|
-
return [];
|
|
1744
|
+
return { messages: [] };
|
|
1938
1745
|
}
|
|
1939
1746
|
}
|
|
1940
|
-
async
|
|
1941
|
-
const { threadId, resourceId,
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1747
|
+
async listMessages(args) {
|
|
1748
|
+
const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
|
|
1749
|
+
if (!threadId.trim()) {
|
|
1750
|
+
throw new MastraError(
|
|
1751
|
+
{
|
|
1752
|
+
id: "STORAGE_PG_LIST_MESSAGES_INVALID_THREAD_ID",
|
|
1753
|
+
domain: ErrorDomain.STORAGE,
|
|
1754
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
1755
|
+
details: { threadId }
|
|
1756
|
+
},
|
|
1757
|
+
new Error("threadId must be a non-empty string")
|
|
1758
|
+
);
|
|
1759
|
+
}
|
|
1760
|
+
if (page < 0) {
|
|
1761
|
+
throw new MastraError({
|
|
1762
|
+
id: "MASTRA_STORAGE_PG_STORE_INVALID_PAGE",
|
|
1763
|
+
domain: ErrorDomain.STORAGE,
|
|
1764
|
+
category: ErrorCategory.USER,
|
|
1765
|
+
text: "Page number must be non-negative",
|
|
1766
|
+
details: {
|
|
1767
|
+
threadId,
|
|
1768
|
+
page
|
|
1954
1769
|
}
|
|
1955
|
-
}
|
|
1956
|
-
|
|
1957
|
-
|
|
1770
|
+
});
|
|
1771
|
+
}
|
|
1772
|
+
const perPage = normalizePerPage(perPageInput, 40);
|
|
1773
|
+
const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
1774
|
+
try {
|
|
1775
|
+
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
1776
|
+
const orderByStatement = `ORDER BY "${field}" ${direction}`;
|
|
1777
|
+
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
1778
|
+
const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
|
|
1958
1779
|
const conditions = [`thread_id = $1`];
|
|
1959
1780
|
const queryParams = [threadId];
|
|
1960
1781
|
let paramIndex = 2;
|
|
1961
|
-
if (
|
|
1782
|
+
if (resourceId) {
|
|
1783
|
+
conditions.push(`"resourceId" = $${paramIndex++}`);
|
|
1784
|
+
queryParams.push(resourceId);
|
|
1785
|
+
}
|
|
1786
|
+
if (filter?.dateRange?.start) {
|
|
1962
1787
|
conditions.push(`"createdAt" >= $${paramIndex++}`);
|
|
1963
|
-
queryParams.push(
|
|
1788
|
+
queryParams.push(filter.dateRange.start);
|
|
1964
1789
|
}
|
|
1965
|
-
if (
|
|
1790
|
+
if (filter?.dateRange?.end) {
|
|
1966
1791
|
conditions.push(`"createdAt" <= $${paramIndex++}`);
|
|
1967
|
-
queryParams.push(
|
|
1792
|
+
queryParams.push(filter.dateRange.end);
|
|
1968
1793
|
}
|
|
1969
1794
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
1970
|
-
const tableName = getTableName({ indexName: TABLE_MESSAGES, schemaName: getSchemaName(this.schema) });
|
|
1971
1795
|
const countQuery = `SELECT COUNT(*) FROM ${tableName} ${whereClause}`;
|
|
1972
1796
|
const countResult = await this.client.one(countQuery, queryParams);
|
|
1973
1797
|
const total = parseInt(countResult.count, 10);
|
|
1974
|
-
|
|
1798
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
1799
|
+
const dataQuery = `${selectStatement} FROM ${tableName} ${whereClause} ${orderByStatement} LIMIT $${paramIndex++} OFFSET $${paramIndex++}`;
|
|
1800
|
+
const rows = await this.client.manyOrNone(dataQuery, [...queryParams, limitValue, offset]);
|
|
1801
|
+
const messages = [...rows || []];
|
|
1802
|
+
if (total === 0 && messages.length === 0 && (!include || include.length === 0)) {
|
|
1975
1803
|
return {
|
|
1976
1804
|
messages: [],
|
|
1977
1805
|
total: 0,
|
|
1978
1806
|
page,
|
|
1979
|
-
perPage,
|
|
1807
|
+
perPage: perPageForResponse,
|
|
1980
1808
|
hasMore: false
|
|
1981
1809
|
};
|
|
1982
1810
|
}
|
|
1983
|
-
const
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
try {
|
|
1993
|
-
return { ...message, content: JSON.parse(message.content) };
|
|
1994
|
-
} catch {
|
|
1995
|
-
return message;
|
|
1811
|
+
const messageIds = new Set(messages.map((m) => m.id));
|
|
1812
|
+
if (include && include.length > 0) {
|
|
1813
|
+
const includeMessages = await this._getIncludedMessages({ threadId, include });
|
|
1814
|
+
if (includeMessages) {
|
|
1815
|
+
for (const includeMsg of includeMessages) {
|
|
1816
|
+
if (!messageIds.has(includeMsg.id)) {
|
|
1817
|
+
messages.push(includeMsg);
|
|
1818
|
+
messageIds.add(includeMsg.id);
|
|
1819
|
+
}
|
|
1996
1820
|
}
|
|
1997
1821
|
}
|
|
1998
|
-
|
|
1999
|
-
|
|
1822
|
+
}
|
|
1823
|
+
const messagesWithParsedContent = messages.map((row) => this.parseRow(row));
|
|
2000
1824
|
const list = new MessageList().add(messagesWithParsedContent, "memory");
|
|
2001
|
-
|
|
1825
|
+
let finalMessages = list.get.all.db();
|
|
1826
|
+
finalMessages = finalMessages.sort((a, b) => {
|
|
1827
|
+
const aValue = field === "createdAt" ? new Date(a.createdAt).getTime() : a[field];
|
|
1828
|
+
const bValue = field === "createdAt" ? new Date(b.createdAt).getTime() : b[field];
|
|
1829
|
+
if (aValue == null && bValue == null) return a.id.localeCompare(b.id);
|
|
1830
|
+
if (aValue == null) return 1;
|
|
1831
|
+
if (bValue == null) return -1;
|
|
1832
|
+
if (aValue === bValue) {
|
|
1833
|
+
return a.id.localeCompare(b.id);
|
|
1834
|
+
}
|
|
1835
|
+
if (typeof aValue === "number" && typeof bValue === "number") {
|
|
1836
|
+
return direction === "ASC" ? aValue - bValue : bValue - aValue;
|
|
1837
|
+
}
|
|
1838
|
+
return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
|
|
1839
|
+
});
|
|
1840
|
+
const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
|
|
1841
|
+
const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
|
|
1842
|
+
const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
|
|
2002
1843
|
return {
|
|
2003
|
-
messages:
|
|
1844
|
+
messages: finalMessages,
|
|
2004
1845
|
total,
|
|
2005
1846
|
page,
|
|
2006
|
-
perPage,
|
|
2007
|
-
hasMore
|
|
1847
|
+
perPage: perPageForResponse,
|
|
1848
|
+
hasMore
|
|
2008
1849
|
};
|
|
2009
1850
|
} catch (error) {
|
|
2010
1851
|
const mastraError = new MastraError(
|
|
2011
1852
|
{
|
|
2012
|
-
id: "
|
|
1853
|
+
id: "MASTRA_STORAGE_PG_STORE_LIST_MESSAGES_FAILED",
|
|
2013
1854
|
domain: ErrorDomain.STORAGE,
|
|
2014
1855
|
category: ErrorCategory.THIRD_PARTY,
|
|
2015
1856
|
details: {
|
|
2016
1857
|
threadId,
|
|
2017
|
-
resourceId: resourceId ?? ""
|
|
2018
|
-
page
|
|
1858
|
+
resourceId: resourceId ?? ""
|
|
2019
1859
|
}
|
|
2020
1860
|
},
|
|
2021
1861
|
error
|
|
2022
1862
|
);
|
|
2023
1863
|
this.logger?.error?.(mastraError.toString());
|
|
2024
1864
|
this.logger?.trackException(mastraError);
|
|
2025
|
-
return {
|
|
1865
|
+
return {
|
|
1866
|
+
messages: [],
|
|
1867
|
+
total: 0,
|
|
1868
|
+
page,
|
|
1869
|
+
perPage: perPageForResponse,
|
|
1870
|
+
hasMore: false
|
|
1871
|
+
};
|
|
2026
1872
|
}
|
|
2027
1873
|
}
|
|
2028
|
-
async saveMessages({
|
|
2029
|
-
messages
|
|
2030
|
-
format
|
|
2031
|
-
}) {
|
|
2032
|
-
if (messages.length === 0) return messages;
|
|
1874
|
+
async saveMessages({ messages }) {
|
|
1875
|
+
if (messages.length === 0) return { messages: [] };
|
|
2033
1876
|
const threadId = messages[0]?.threadId;
|
|
2034
1877
|
if (!threadId) {
|
|
2035
1878
|
throw new MastraError({
|
|
@@ -2109,8 +1952,7 @@ var MemoryPG = class extends MemoryStorage {
|
|
|
2109
1952
|
return message;
|
|
2110
1953
|
});
|
|
2111
1954
|
const list = new MessageList().add(messagesWithParsedContent, "memory");
|
|
2112
|
-
|
|
2113
|
-
return list.get.all.v1();
|
|
1955
|
+
return { messages: list.get.all.db() };
|
|
2114
1956
|
} catch (error) {
|
|
2115
1957
|
throw new MastraError(
|
|
2116
1958
|
{
|
|
@@ -2342,13 +2184,13 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2342
2184
|
this.operations = operations;
|
|
2343
2185
|
this.schema = schema;
|
|
2344
2186
|
}
|
|
2345
|
-
get
|
|
2187
|
+
get tracingStrategy() {
|
|
2346
2188
|
return {
|
|
2347
2189
|
preferred: "batch-with-updates",
|
|
2348
2190
|
supported: ["batch-with-updates", "insert-only"]
|
|
2349
2191
|
};
|
|
2350
2192
|
}
|
|
2351
|
-
async
|
|
2193
|
+
async createSpan(span) {
|
|
2352
2194
|
try {
|
|
2353
2195
|
const startedAt = span.startedAt instanceof Date ? span.startedAt.toISOString() : span.startedAt;
|
|
2354
2196
|
const endedAt = span.endedAt instanceof Date ? span.endedAt.toISOString() : span.endedAt;
|
|
@@ -2360,11 +2202,11 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2360
2202
|
endedAtZ: endedAt
|
|
2361
2203
|
// Note: createdAt/updatedAt will be set by database triggers
|
|
2362
2204
|
};
|
|
2363
|
-
return this.operations.insert({ tableName:
|
|
2205
|
+
return this.operations.insert({ tableName: TABLE_SPANS, record });
|
|
2364
2206
|
} catch (error) {
|
|
2365
2207
|
throw new MastraError(
|
|
2366
2208
|
{
|
|
2367
|
-
id: "
|
|
2209
|
+
id: "PG_STORE_CREATE_SPAN_FAILED",
|
|
2368
2210
|
domain: ErrorDomain.STORAGE,
|
|
2369
2211
|
category: ErrorCategory.USER,
|
|
2370
2212
|
details: {
|
|
@@ -2378,10 +2220,10 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2378
2220
|
);
|
|
2379
2221
|
}
|
|
2380
2222
|
}
|
|
2381
|
-
async
|
|
2223
|
+
async getTrace(traceId) {
|
|
2382
2224
|
try {
|
|
2383
2225
|
const tableName = getTableName({
|
|
2384
|
-
indexName:
|
|
2226
|
+
indexName: TABLE_SPANS,
|
|
2385
2227
|
schemaName: getSchemaName(this.schema)
|
|
2386
2228
|
});
|
|
2387
2229
|
const spans = await this.client.manyOrNone(
|
|
@@ -2402,7 +2244,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2402
2244
|
traceId,
|
|
2403
2245
|
spans: spans.map(
|
|
2404
2246
|
(span) => transformFromSqlRow({
|
|
2405
|
-
tableName:
|
|
2247
|
+
tableName: TABLE_SPANS,
|
|
2406
2248
|
sqlRow: span
|
|
2407
2249
|
})
|
|
2408
2250
|
)
|
|
@@ -2410,7 +2252,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2410
2252
|
} catch (error) {
|
|
2411
2253
|
throw new MastraError(
|
|
2412
2254
|
{
|
|
2413
|
-
id: "
|
|
2255
|
+
id: "PG_STORE_GET_TRACE_FAILED",
|
|
2414
2256
|
domain: ErrorDomain.STORAGE,
|
|
2415
2257
|
category: ErrorCategory.USER,
|
|
2416
2258
|
details: {
|
|
@@ -2421,7 +2263,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2421
2263
|
);
|
|
2422
2264
|
}
|
|
2423
2265
|
}
|
|
2424
|
-
async
|
|
2266
|
+
async updateSpan({
|
|
2425
2267
|
spanId,
|
|
2426
2268
|
traceId,
|
|
2427
2269
|
updates
|
|
@@ -2435,14 +2277,14 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2435
2277
|
data.startedAt = data.startedAt.toISOString();
|
|
2436
2278
|
}
|
|
2437
2279
|
await this.operations.update({
|
|
2438
|
-
tableName:
|
|
2280
|
+
tableName: TABLE_SPANS,
|
|
2439
2281
|
keys: { spanId, traceId },
|
|
2440
2282
|
data
|
|
2441
2283
|
});
|
|
2442
2284
|
} catch (error) {
|
|
2443
2285
|
throw new MastraError(
|
|
2444
2286
|
{
|
|
2445
|
-
id: "
|
|
2287
|
+
id: "PG_STORE_UPDATE_SPAN_FAILED",
|
|
2446
2288
|
domain: ErrorDomain.STORAGE,
|
|
2447
2289
|
category: ErrorCategory.USER,
|
|
2448
2290
|
details: {
|
|
@@ -2454,7 +2296,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2454
2296
|
);
|
|
2455
2297
|
}
|
|
2456
2298
|
}
|
|
2457
|
-
async
|
|
2299
|
+
async getTracesPaginated({
|
|
2458
2300
|
filters,
|
|
2459
2301
|
pagination
|
|
2460
2302
|
}) {
|
|
@@ -2478,7 +2320,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2478
2320
|
name = `agent run: '${entityId}'`;
|
|
2479
2321
|
} else {
|
|
2480
2322
|
const error = new MastraError({
|
|
2481
|
-
id: "
|
|
2323
|
+
id: "PG_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
2482
2324
|
domain: ErrorDomain.STORAGE,
|
|
2483
2325
|
category: ErrorCategory.USER,
|
|
2484
2326
|
details: {
|
|
@@ -2498,7 +2340,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2498
2340
|
}
|
|
2499
2341
|
}
|
|
2500
2342
|
const tableName = getTableName({
|
|
2501
|
-
indexName:
|
|
2343
|
+
indexName: TABLE_SPANS,
|
|
2502
2344
|
schemaName: getSchemaName(this.schema)
|
|
2503
2345
|
});
|
|
2504
2346
|
try {
|
|
@@ -2538,7 +2380,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2538
2380
|
},
|
|
2539
2381
|
spans: spans.map(
|
|
2540
2382
|
(span) => transformFromSqlRow({
|
|
2541
|
-
tableName:
|
|
2383
|
+
tableName: TABLE_SPANS,
|
|
2542
2384
|
sqlRow: span
|
|
2543
2385
|
})
|
|
2544
2386
|
)
|
|
@@ -2546,7 +2388,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2546
2388
|
} catch (error) {
|
|
2547
2389
|
throw new MastraError(
|
|
2548
2390
|
{
|
|
2549
|
-
id: "
|
|
2391
|
+
id: "PG_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
2550
2392
|
domain: ErrorDomain.STORAGE,
|
|
2551
2393
|
category: ErrorCategory.USER
|
|
2552
2394
|
},
|
|
@@ -2554,7 +2396,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2554
2396
|
);
|
|
2555
2397
|
}
|
|
2556
2398
|
}
|
|
2557
|
-
async
|
|
2399
|
+
async batchCreateSpans(args) {
|
|
2558
2400
|
try {
|
|
2559
2401
|
const records = args.records.map((record) => {
|
|
2560
2402
|
const startedAt = record.startedAt instanceof Date ? record.startedAt.toISOString() : record.startedAt;
|
|
@@ -2569,13 +2411,13 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2569
2411
|
};
|
|
2570
2412
|
});
|
|
2571
2413
|
return this.operations.batchInsert({
|
|
2572
|
-
tableName:
|
|
2414
|
+
tableName: TABLE_SPANS,
|
|
2573
2415
|
records
|
|
2574
2416
|
});
|
|
2575
2417
|
} catch (error) {
|
|
2576
2418
|
throw new MastraError(
|
|
2577
2419
|
{
|
|
2578
|
-
id: "
|
|
2420
|
+
id: "PG_STORE_BATCH_CREATE_SPANS_FAILED",
|
|
2579
2421
|
domain: ErrorDomain.STORAGE,
|
|
2580
2422
|
category: ErrorCategory.USER
|
|
2581
2423
|
},
|
|
@@ -2583,10 +2425,10 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2583
2425
|
);
|
|
2584
2426
|
}
|
|
2585
2427
|
}
|
|
2586
|
-
async
|
|
2428
|
+
async batchUpdateSpans(args) {
|
|
2587
2429
|
try {
|
|
2588
2430
|
return this.operations.batchUpdate({
|
|
2589
|
-
tableName:
|
|
2431
|
+
tableName: TABLE_SPANS,
|
|
2590
2432
|
updates: args.records.map((record) => {
|
|
2591
2433
|
const data = {
|
|
2592
2434
|
...record.updates
|
|
@@ -2610,7 +2452,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2610
2452
|
} catch (error) {
|
|
2611
2453
|
throw new MastraError(
|
|
2612
2454
|
{
|
|
2613
|
-
id: "
|
|
2455
|
+
id: "PG_STORE_BATCH_UPDATE_SPANS_FAILED",
|
|
2614
2456
|
domain: ErrorDomain.STORAGE,
|
|
2615
2457
|
category: ErrorCategory.USER
|
|
2616
2458
|
},
|
|
@@ -2618,10 +2460,10 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2618
2460
|
);
|
|
2619
2461
|
}
|
|
2620
2462
|
}
|
|
2621
|
-
async
|
|
2463
|
+
async batchDeleteTraces(args) {
|
|
2622
2464
|
try {
|
|
2623
2465
|
const tableName = getTableName({
|
|
2624
|
-
indexName:
|
|
2466
|
+
indexName: TABLE_SPANS,
|
|
2625
2467
|
schemaName: getSchemaName(this.schema)
|
|
2626
2468
|
});
|
|
2627
2469
|
const placeholders = args.traceIds.map((_, i) => `$${i + 1}`).join(", ");
|
|
@@ -2629,7 +2471,7 @@ var ObservabilityPG = class extends ObservabilityStorage {
|
|
|
2629
2471
|
} catch (error) {
|
|
2630
2472
|
throw new MastraError(
|
|
2631
2473
|
{
|
|
2632
|
-
id: "
|
|
2474
|
+
id: "PG_STORE_BATCH_DELETE_TRACES_FAILED",
|
|
2633
2475
|
domain: ErrorDomain.STORAGE,
|
|
2634
2476
|
category: ErrorCategory.USER
|
|
2635
2477
|
},
|
|
@@ -2845,7 +2687,7 @@ var StoreOperationsPG = class extends StoreOperations {
|
|
|
2845
2687
|
schema,
|
|
2846
2688
|
ifNotExists: timeZColumnNames
|
|
2847
2689
|
});
|
|
2848
|
-
if (tableName ===
|
|
2690
|
+
if (tableName === TABLE_SPANS) {
|
|
2849
2691
|
await this.setupTimestampTriggers(tableName);
|
|
2850
2692
|
}
|
|
2851
2693
|
} catch (error) {
|
|
@@ -3231,37 +3073,31 @@ var StoreOperationsPG = class extends StoreOperations {
|
|
|
3231
3073
|
table: TABLE_TRACES,
|
|
3232
3074
|
columns: ["name", "startTime DESC"]
|
|
3233
3075
|
},
|
|
3234
|
-
// Composite index for evals (filter + sort)
|
|
3235
|
-
{
|
|
3236
|
-
name: `${schemaPrefix}mastra_evals_agent_name_created_at_idx`,
|
|
3237
|
-
table: TABLE_EVALS,
|
|
3238
|
-
columns: ["agent_name", "created_at DESC"]
|
|
3239
|
-
},
|
|
3240
3076
|
// Composite index for scores (filter + sort)
|
|
3241
3077
|
{
|
|
3242
3078
|
name: `${schemaPrefix}mastra_scores_trace_id_span_id_created_at_idx`,
|
|
3243
3079
|
table: TABLE_SCORERS,
|
|
3244
3080
|
columns: ["traceId", "spanId", "createdAt DESC"]
|
|
3245
3081
|
},
|
|
3246
|
-
//
|
|
3082
|
+
// Spans indexes for optimal trace querying
|
|
3247
3083
|
{
|
|
3248
3084
|
name: `${schemaPrefix}mastra_ai_spans_traceid_startedat_idx`,
|
|
3249
|
-
table:
|
|
3085
|
+
table: TABLE_SPANS,
|
|
3250
3086
|
columns: ["traceId", "startedAt DESC"]
|
|
3251
3087
|
},
|
|
3252
3088
|
{
|
|
3253
3089
|
name: `${schemaPrefix}mastra_ai_spans_parentspanid_startedat_idx`,
|
|
3254
|
-
table:
|
|
3090
|
+
table: TABLE_SPANS,
|
|
3255
3091
|
columns: ["parentSpanId", "startedAt DESC"]
|
|
3256
3092
|
},
|
|
3257
3093
|
{
|
|
3258
3094
|
name: `${schemaPrefix}mastra_ai_spans_name_idx`,
|
|
3259
|
-
table:
|
|
3095
|
+
table: TABLE_SPANS,
|
|
3260
3096
|
columns: ["name"]
|
|
3261
3097
|
},
|
|
3262
3098
|
{
|
|
3263
3099
|
name: `${schemaPrefix}mastra_ai_spans_spantype_startedat_idx`,
|
|
3264
|
-
table:
|
|
3100
|
+
table: TABLE_SPANS,
|
|
3265
3101
|
columns: ["spanType", "startedAt DESC"]
|
|
3266
3102
|
}
|
|
3267
3103
|
];
|
|
@@ -3482,7 +3318,7 @@ function transformScoreRow(row) {
|
|
|
3482
3318
|
metadata: safelyParseJSON(row.metadata),
|
|
3483
3319
|
output: safelyParseJSON(row.output),
|
|
3484
3320
|
additionalContext: safelyParseJSON(row.additionalContext),
|
|
3485
|
-
|
|
3321
|
+
requestContext: safelyParseJSON(row.requestContext),
|
|
3486
3322
|
entity: safelyParseJSON(row.entity),
|
|
3487
3323
|
createdAt: row.createdAtZ || row.createdAt,
|
|
3488
3324
|
updatedAt: row.updatedAtZ || row.updatedAt
|
|
@@ -3520,7 +3356,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3520
3356
|
);
|
|
3521
3357
|
}
|
|
3522
3358
|
}
|
|
3523
|
-
async
|
|
3359
|
+
async listScoresByScorerId({
|
|
3524
3360
|
scorerId,
|
|
3525
3361
|
pagination,
|
|
3526
3362
|
entityId,
|
|
@@ -3548,27 +3384,32 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3548
3384
|
`SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause}`,
|
|
3549
3385
|
queryParams
|
|
3550
3386
|
);
|
|
3387
|
+
const { page, perPage: perPageInput } = pagination;
|
|
3388
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
3389
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
3551
3390
|
if (total?.count === "0" || !total?.count) {
|
|
3552
3391
|
return {
|
|
3553
3392
|
pagination: {
|
|
3554
3393
|
total: 0,
|
|
3555
|
-
page
|
|
3556
|
-
perPage:
|
|
3394
|
+
page,
|
|
3395
|
+
perPage: perPageForResponse,
|
|
3557
3396
|
hasMore: false
|
|
3558
3397
|
},
|
|
3559
3398
|
scores: []
|
|
3560
3399
|
};
|
|
3561
3400
|
}
|
|
3401
|
+
const limitValue = perPageInput === false ? Number(total?.count) : perPage;
|
|
3402
|
+
const end = perPageInput === false ? Number(total?.count) : start + perPage;
|
|
3562
3403
|
const result = await this.client.manyOrNone(
|
|
3563
3404
|
`SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
|
|
3564
|
-
[...queryParams,
|
|
3405
|
+
[...queryParams, limitValue, start]
|
|
3565
3406
|
);
|
|
3566
3407
|
return {
|
|
3567
3408
|
pagination: {
|
|
3568
3409
|
total: Number(total?.count) || 0,
|
|
3569
|
-
page
|
|
3570
|
-
perPage:
|
|
3571
|
-
hasMore: Number(total?.count)
|
|
3410
|
+
page,
|
|
3411
|
+
perPage: perPageForResponse,
|
|
3412
|
+
hasMore: end < Number(total?.count)
|
|
3572
3413
|
},
|
|
3573
3414
|
scores: result.map(transformScoreRow)
|
|
3574
3415
|
};
|
|
@@ -3594,7 +3435,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3594
3435
|
domain: ErrorDomain.STORAGE,
|
|
3595
3436
|
category: ErrorCategory.USER,
|
|
3596
3437
|
details: {
|
|
3597
|
-
scorer: score.scorer.
|
|
3438
|
+
scorer: score.scorer.id,
|
|
3598
3439
|
entityId: score.entityId,
|
|
3599
3440
|
entityType: score.entityType,
|
|
3600
3441
|
traceId: score.traceId || "",
|
|
@@ -3614,7 +3455,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3614
3455
|
input,
|
|
3615
3456
|
output,
|
|
3616
3457
|
additionalContext,
|
|
3617
|
-
|
|
3458
|
+
requestContext,
|
|
3618
3459
|
entity,
|
|
3619
3460
|
...rest
|
|
3620
3461
|
} = parsedScore;
|
|
@@ -3630,7 +3471,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3630
3471
|
analyzeStepResult: analyzeStepResult ? JSON.stringify(analyzeStepResult) : null,
|
|
3631
3472
|
metadata: metadata ? JSON.stringify(metadata) : null,
|
|
3632
3473
|
additionalContext: additionalContext ? JSON.stringify(additionalContext) : null,
|
|
3633
|
-
|
|
3474
|
+
requestContext: requestContext ? JSON.stringify(requestContext) : null,
|
|
3634
3475
|
entity: entity ? JSON.stringify(entity) : null,
|
|
3635
3476
|
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3636
3477
|
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -3649,7 +3490,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3649
3490
|
);
|
|
3650
3491
|
}
|
|
3651
3492
|
}
|
|
3652
|
-
async
|
|
3493
|
+
async listScoresByRunId({
|
|
3653
3494
|
runId,
|
|
3654
3495
|
pagination
|
|
3655
3496
|
}) {
|
|
@@ -3658,27 +3499,32 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3658
3499
|
`SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "runId" = $1`,
|
|
3659
3500
|
[runId]
|
|
3660
3501
|
);
|
|
3502
|
+
const { page, perPage: perPageInput } = pagination;
|
|
3503
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
3504
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
3661
3505
|
if (total?.count === "0" || !total?.count) {
|
|
3662
3506
|
return {
|
|
3663
3507
|
pagination: {
|
|
3664
3508
|
total: 0,
|
|
3665
|
-
page
|
|
3666
|
-
perPage:
|
|
3509
|
+
page,
|
|
3510
|
+
perPage: perPageForResponse,
|
|
3667
3511
|
hasMore: false
|
|
3668
3512
|
},
|
|
3669
3513
|
scores: []
|
|
3670
3514
|
};
|
|
3671
3515
|
}
|
|
3516
|
+
const limitValue = perPageInput === false ? Number(total?.count) : perPage;
|
|
3517
|
+
const end = perPageInput === false ? Number(total?.count) : start + perPage;
|
|
3672
3518
|
const result = await this.client.manyOrNone(
|
|
3673
3519
|
`SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "runId" = $1 LIMIT $2 OFFSET $3`,
|
|
3674
|
-
[runId,
|
|
3520
|
+
[runId, limitValue, start]
|
|
3675
3521
|
);
|
|
3676
3522
|
return {
|
|
3677
3523
|
pagination: {
|
|
3678
3524
|
total: Number(total?.count) || 0,
|
|
3679
|
-
page
|
|
3680
|
-
perPage:
|
|
3681
|
-
hasMore: Number(total?.count)
|
|
3525
|
+
page,
|
|
3526
|
+
perPage: perPageForResponse,
|
|
3527
|
+
hasMore: end < Number(total?.count)
|
|
3682
3528
|
},
|
|
3683
3529
|
scores: result.map(transformScoreRow)
|
|
3684
3530
|
};
|
|
@@ -3693,7 +3539,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3693
3539
|
);
|
|
3694
3540
|
}
|
|
3695
3541
|
}
|
|
3696
|
-
async
|
|
3542
|
+
async listScoresByEntityId({
|
|
3697
3543
|
entityId,
|
|
3698
3544
|
entityType,
|
|
3699
3545
|
pagination
|
|
@@ -3703,27 +3549,32 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3703
3549
|
`SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "entityId" = $1 AND "entityType" = $2`,
|
|
3704
3550
|
[entityId, entityType]
|
|
3705
3551
|
);
|
|
3552
|
+
const { page, perPage: perPageInput } = pagination;
|
|
3553
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
3554
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
3706
3555
|
if (total?.count === "0" || !total?.count) {
|
|
3707
3556
|
return {
|
|
3708
3557
|
pagination: {
|
|
3709
3558
|
total: 0,
|
|
3710
|
-
page
|
|
3711
|
-
perPage:
|
|
3559
|
+
page,
|
|
3560
|
+
perPage: perPageForResponse,
|
|
3712
3561
|
hasMore: false
|
|
3713
3562
|
},
|
|
3714
3563
|
scores: []
|
|
3715
3564
|
};
|
|
3716
3565
|
}
|
|
3566
|
+
const limitValue = perPageInput === false ? Number(total?.count) : perPage;
|
|
3567
|
+
const end = perPageInput === false ? Number(total?.count) : start + perPage;
|
|
3717
3568
|
const result = await this.client.manyOrNone(
|
|
3718
3569
|
`SELECT * FROM ${getTableName({ indexName: TABLE_SCORERS, schemaName: this.schema })} WHERE "entityId" = $1 AND "entityType" = $2 LIMIT $3 OFFSET $4`,
|
|
3719
|
-
[entityId, entityType,
|
|
3570
|
+
[entityId, entityType, limitValue, start]
|
|
3720
3571
|
);
|
|
3721
3572
|
return {
|
|
3722
3573
|
pagination: {
|
|
3723
3574
|
total: Number(total?.count) || 0,
|
|
3724
|
-
page
|
|
3725
|
-
perPage:
|
|
3726
|
-
hasMore: Number(total?.count)
|
|
3575
|
+
page,
|
|
3576
|
+
perPage: perPageForResponse,
|
|
3577
|
+
hasMore: end < Number(total?.count)
|
|
3727
3578
|
},
|
|
3728
3579
|
scores: result.map(transformScoreRow)
|
|
3729
3580
|
};
|
|
@@ -3738,7 +3589,7 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3738
3589
|
);
|
|
3739
3590
|
}
|
|
3740
3591
|
}
|
|
3741
|
-
async
|
|
3592
|
+
async listScoresBySpan({
|
|
3742
3593
|
traceId,
|
|
3743
3594
|
spanId,
|
|
3744
3595
|
pagination
|
|
@@ -3750,18 +3601,23 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3750
3601
|
[traceId, spanId]
|
|
3751
3602
|
);
|
|
3752
3603
|
const total = Number(countSQLResult?.count ?? 0);
|
|
3604
|
+
const { page, perPage: perPageInput } = pagination;
|
|
3605
|
+
const perPage = normalizePerPage(perPageInput, 100);
|
|
3606
|
+
const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
|
|
3607
|
+
const limitValue = perPageInput === false ? total : perPage;
|
|
3608
|
+
const end = perPageInput === false ? total : start + perPage;
|
|
3753
3609
|
const result = await this.client.manyOrNone(
|
|
3754
3610
|
`SELECT * FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2 ORDER BY "createdAt" DESC LIMIT $3 OFFSET $4`,
|
|
3755
|
-
[traceId, spanId,
|
|
3611
|
+
[traceId, spanId, limitValue, start]
|
|
3756
3612
|
);
|
|
3757
|
-
const hasMore =
|
|
3758
|
-
const scores = result.
|
|
3613
|
+
const hasMore = end < total;
|
|
3614
|
+
const scores = result.map((row) => transformScoreRow(row)) ?? [];
|
|
3759
3615
|
return {
|
|
3760
3616
|
scores,
|
|
3761
3617
|
pagination: {
|
|
3762
3618
|
total,
|
|
3763
|
-
page
|
|
3764
|
-
perPage:
|
|
3619
|
+
page,
|
|
3620
|
+
perPage: perPageForResponse,
|
|
3765
3621
|
hasMore
|
|
3766
3622
|
}
|
|
3767
3623
|
};
|
|
@@ -3777,141 +3633,6 @@ var ScoresPG = class extends ScoresStorage {
|
|
|
3777
3633
|
}
|
|
3778
3634
|
}
|
|
3779
3635
|
};
|
|
3780
|
-
var TracesPG = class extends TracesStorage {
|
|
3781
|
-
client;
|
|
3782
|
-
operations;
|
|
3783
|
-
schema;
|
|
3784
|
-
constructor({
|
|
3785
|
-
client,
|
|
3786
|
-
operations,
|
|
3787
|
-
schema
|
|
3788
|
-
}) {
|
|
3789
|
-
super();
|
|
3790
|
-
this.client = client;
|
|
3791
|
-
this.operations = operations;
|
|
3792
|
-
this.schema = schema;
|
|
3793
|
-
}
|
|
3794
|
-
async getTraces(args) {
|
|
3795
|
-
if (args.fromDate || args.toDate) {
|
|
3796
|
-
args.dateRange = {
|
|
3797
|
-
start: args.fromDate,
|
|
3798
|
-
end: args.toDate
|
|
3799
|
-
};
|
|
3800
|
-
}
|
|
3801
|
-
try {
|
|
3802
|
-
const result = await this.getTracesPaginated(args);
|
|
3803
|
-
return result.traces;
|
|
3804
|
-
} catch (error) {
|
|
3805
|
-
throw new MastraError(
|
|
3806
|
-
{
|
|
3807
|
-
id: "MASTRA_STORAGE_PG_STORE_GET_TRACES_FAILED",
|
|
3808
|
-
domain: ErrorDomain.STORAGE,
|
|
3809
|
-
category: ErrorCategory.THIRD_PARTY
|
|
3810
|
-
},
|
|
3811
|
-
error
|
|
3812
|
-
);
|
|
3813
|
-
}
|
|
3814
|
-
}
|
|
3815
|
-
async getTracesPaginated(args) {
|
|
3816
|
-
const { name, scope, page = 0, perPage = 100, attributes, filters, dateRange } = args;
|
|
3817
|
-
const fromDate = dateRange?.start;
|
|
3818
|
-
const toDate = dateRange?.end;
|
|
3819
|
-
const currentOffset = page * perPage;
|
|
3820
|
-
const queryParams = [];
|
|
3821
|
-
const conditions = [];
|
|
3822
|
-
let paramIndex = 1;
|
|
3823
|
-
if (name) {
|
|
3824
|
-
conditions.push(`name LIKE $${paramIndex++}`);
|
|
3825
|
-
queryParams.push(`${name}%`);
|
|
3826
|
-
}
|
|
3827
|
-
if (scope) {
|
|
3828
|
-
conditions.push(`scope = $${paramIndex++}`);
|
|
3829
|
-
queryParams.push(scope);
|
|
3830
|
-
}
|
|
3831
|
-
if (attributes) {
|
|
3832
|
-
Object.entries(attributes).forEach(([key, value]) => {
|
|
3833
|
-
const parsedKey = parseFieldKey(key);
|
|
3834
|
-
conditions.push(`attributes->>'${parsedKey}' = $${paramIndex++}`);
|
|
3835
|
-
queryParams.push(value);
|
|
3836
|
-
});
|
|
3837
|
-
}
|
|
3838
|
-
if (filters) {
|
|
3839
|
-
Object.entries(filters).forEach(([key, value]) => {
|
|
3840
|
-
const parsedKey = parseFieldKey(key);
|
|
3841
|
-
conditions.push(`"${parsedKey}" = $${paramIndex++}`);
|
|
3842
|
-
queryParams.push(value);
|
|
3843
|
-
});
|
|
3844
|
-
}
|
|
3845
|
-
if (fromDate) {
|
|
3846
|
-
conditions.push(`"createdAt" >= $${paramIndex++}`);
|
|
3847
|
-
queryParams.push(fromDate);
|
|
3848
|
-
}
|
|
3849
|
-
if (toDate) {
|
|
3850
|
-
conditions.push(`"createdAt" <= $${paramIndex++}`);
|
|
3851
|
-
queryParams.push(toDate);
|
|
3852
|
-
}
|
|
3853
|
-
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
3854
|
-
try {
|
|
3855
|
-
const countResult = await this.client.oneOrNone(
|
|
3856
|
-
`SELECT COUNT(*) FROM ${getTableName({ indexName: TABLE_TRACES, schemaName: getSchemaName(this.schema) })} ${whereClause}`,
|
|
3857
|
-
queryParams
|
|
3858
|
-
);
|
|
3859
|
-
const total = Number(countResult?.count ?? 0);
|
|
3860
|
-
if (total === 0) {
|
|
3861
|
-
return {
|
|
3862
|
-
traces: [],
|
|
3863
|
-
total: 0,
|
|
3864
|
-
page,
|
|
3865
|
-
perPage,
|
|
3866
|
-
hasMore: false
|
|
3867
|
-
};
|
|
3868
|
-
}
|
|
3869
|
-
const dataResult = await this.client.manyOrNone(
|
|
3870
|
-
`SELECT * FROM ${getTableName({ indexName: TABLE_TRACES, schemaName: getSchemaName(this.schema) })} ${whereClause} ORDER BY "startTime" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
|
|
3871
|
-
[...queryParams, perPage, currentOffset]
|
|
3872
|
-
);
|
|
3873
|
-
const traces = dataResult.map((row) => ({
|
|
3874
|
-
id: row.id,
|
|
3875
|
-
parentSpanId: row.parentSpanId,
|
|
3876
|
-
traceId: row.traceId,
|
|
3877
|
-
name: row.name,
|
|
3878
|
-
scope: row.scope,
|
|
3879
|
-
kind: row.kind,
|
|
3880
|
-
status: safelyParseJSON(row.status),
|
|
3881
|
-
events: safelyParseJSON(row.events),
|
|
3882
|
-
links: safelyParseJSON(row.links),
|
|
3883
|
-
attributes: safelyParseJSON(row.attributes),
|
|
3884
|
-
startTime: row.startTime,
|
|
3885
|
-
endTime: row.endTime,
|
|
3886
|
-
other: safelyParseJSON(row.other),
|
|
3887
|
-
createdAt: row.createdAtZ || row.createdAt
|
|
3888
|
-
}));
|
|
3889
|
-
return {
|
|
3890
|
-
traces,
|
|
3891
|
-
total,
|
|
3892
|
-
page,
|
|
3893
|
-
perPage,
|
|
3894
|
-
hasMore: currentOffset + traces.length < total
|
|
3895
|
-
};
|
|
3896
|
-
} catch (error) {
|
|
3897
|
-
throw new MastraError(
|
|
3898
|
-
{
|
|
3899
|
-
id: "MASTRA_STORAGE_PG_STORE_GET_TRACES_PAGINATED_FAILED",
|
|
3900
|
-
domain: ErrorDomain.STORAGE,
|
|
3901
|
-
category: ErrorCategory.THIRD_PARTY
|
|
3902
|
-
},
|
|
3903
|
-
error
|
|
3904
|
-
);
|
|
3905
|
-
}
|
|
3906
|
-
}
|
|
3907
|
-
async batchTraceInsert({ records }) {
|
|
3908
|
-
this.logger.debug("Batch inserting traces", { count: records.length });
|
|
3909
|
-
await this.operations.batchInsert({
|
|
3910
|
-
tableName: TABLE_TRACES,
|
|
3911
|
-
records
|
|
3912
|
-
});
|
|
3913
|
-
}
|
|
3914
|
-
};
|
|
3915
3636
|
function parseWorkflowRun(row) {
|
|
3916
3637
|
let parsedSnapshot = row.snapshot;
|
|
3917
3638
|
if (typeof parsedSnapshot === "string") {
|
|
@@ -3949,7 +3670,7 @@ var WorkflowsPG = class extends WorkflowsStorage {
|
|
|
3949
3670
|
// runId,
|
|
3950
3671
|
// stepId,
|
|
3951
3672
|
// result,
|
|
3952
|
-
//
|
|
3673
|
+
// requestContext,
|
|
3953
3674
|
}) {
|
|
3954
3675
|
throw new Error("Method not implemented.");
|
|
3955
3676
|
}
|
|
@@ -4052,13 +3773,14 @@ var WorkflowsPG = class extends WorkflowsStorage {
|
|
|
4052
3773
|
);
|
|
4053
3774
|
}
|
|
4054
3775
|
}
|
|
4055
|
-
async
|
|
3776
|
+
async listWorkflowRuns({
|
|
4056
3777
|
workflowName,
|
|
4057
3778
|
fromDate,
|
|
4058
3779
|
toDate,
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
resourceId
|
|
3780
|
+
perPage,
|
|
3781
|
+
page,
|
|
3782
|
+
resourceId,
|
|
3783
|
+
status
|
|
4062
3784
|
} = {}) {
|
|
4063
3785
|
try {
|
|
4064
3786
|
const conditions = [];
|
|
@@ -4069,6 +3791,11 @@ var WorkflowsPG = class extends WorkflowsStorage {
|
|
|
4069
3791
|
values.push(workflowName);
|
|
4070
3792
|
paramIndex++;
|
|
4071
3793
|
}
|
|
3794
|
+
if (status) {
|
|
3795
|
+
conditions.push(`snapshot::jsonb ->> 'status' = $${paramIndex}`);
|
|
3796
|
+
values.push(status);
|
|
3797
|
+
paramIndex++;
|
|
3798
|
+
}
|
|
4072
3799
|
if (resourceId) {
|
|
4073
3800
|
const hasResourceId = await this.operations.hasColumn(TABLE_WORKFLOW_SNAPSHOT, "resourceId");
|
|
4074
3801
|
if (hasResourceId) {
|
|
@@ -4091,20 +3818,23 @@ var WorkflowsPG = class extends WorkflowsStorage {
|
|
|
4091
3818
|
}
|
|
4092
3819
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
4093
3820
|
let total = 0;
|
|
4094
|
-
|
|
3821
|
+
const usePagination = typeof perPage === "number" && typeof page === "number";
|
|
3822
|
+
if (usePagination) {
|
|
4095
3823
|
const countResult = await this.client.one(
|
|
4096
3824
|
`SELECT COUNT(*) as count FROM ${getTableName({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: this.schema })} ${whereClause}`,
|
|
4097
3825
|
values
|
|
4098
3826
|
);
|
|
4099
3827
|
total = Number(countResult.count);
|
|
4100
3828
|
}
|
|
3829
|
+
const normalizedPerPage = usePagination ? normalizePerPage(perPage, Number.MAX_SAFE_INTEGER) : 0;
|
|
3830
|
+
const offset = usePagination ? page * normalizedPerPage : void 0;
|
|
4101
3831
|
const query = `
|
|
4102
3832
|
SELECT * FROM ${getTableName({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: this.schema })}
|
|
4103
3833
|
${whereClause}
|
|
4104
3834
|
ORDER BY "createdAt" DESC
|
|
4105
|
-
${
|
|
3835
|
+
${usePagination ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ""}
|
|
4106
3836
|
`;
|
|
4107
|
-
const queryValues =
|
|
3837
|
+
const queryValues = usePagination ? [...values, normalizedPerPage, offset] : values;
|
|
4108
3838
|
const result = await this.client.manyOrNone(query, queryValues);
|
|
4109
3839
|
const runs = (result || []).map((row) => {
|
|
4110
3840
|
return parseWorkflowRun(row);
|
|
@@ -4113,7 +3843,7 @@ var WorkflowsPG = class extends WorkflowsStorage {
|
|
|
4113
3843
|
} catch (error) {
|
|
4114
3844
|
throw new MastraError(
|
|
4115
3845
|
{
|
|
4116
|
-
id: "
|
|
3846
|
+
id: "MASTRA_STORAGE_PG_STORE_LIST_WORKFLOW_RUNS_FAILED",
|
|
4117
3847
|
domain: ErrorDomain.STORAGE,
|
|
4118
3848
|
category: ErrorCategory.THIRD_PARTY,
|
|
4119
3849
|
details: {
|
|
@@ -4137,10 +3867,11 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4137
3867
|
constructor(config) {
|
|
4138
3868
|
try {
|
|
4139
3869
|
validateConfig("PostgresStore", config);
|
|
4140
|
-
super({ name: "PostgresStore" });
|
|
3870
|
+
super({ id: config.id, name: "PostgresStore" });
|
|
4141
3871
|
this.schema = config.schemaName || "public";
|
|
4142
3872
|
if (isConnectionStringConfig(config)) {
|
|
4143
3873
|
this.#config = {
|
|
3874
|
+
id: config.id,
|
|
4144
3875
|
connectionString: config.connectionString,
|
|
4145
3876
|
max: config.max,
|
|
4146
3877
|
idleTimeoutMillis: config.idleTimeoutMillis,
|
|
@@ -4149,11 +3880,13 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4149
3880
|
} else if (isCloudSqlConfig(config)) {
|
|
4150
3881
|
this.#config = {
|
|
4151
3882
|
...config,
|
|
3883
|
+
id: config.id,
|
|
4152
3884
|
max: config.max,
|
|
4153
3885
|
idleTimeoutMillis: config.idleTimeoutMillis
|
|
4154
3886
|
};
|
|
4155
3887
|
} else if (isHostConfig(config)) {
|
|
4156
3888
|
this.#config = {
|
|
3889
|
+
id: config.id,
|
|
4157
3890
|
host: config.host,
|
|
4158
3891
|
port: config.port,
|
|
4159
3892
|
database: config.database,
|
|
@@ -4190,17 +3923,13 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4190
3923
|
this.#db = this.#pgp(this.#config);
|
|
4191
3924
|
const operations = new StoreOperationsPG({ client: this.#db, schemaName: this.schema });
|
|
4192
3925
|
const scores = new ScoresPG({ client: this.#db, operations, schema: this.schema });
|
|
4193
|
-
const traces = new TracesPG({ client: this.#db, operations, schema: this.schema });
|
|
4194
3926
|
const workflows = new WorkflowsPG({ client: this.#db, operations, schema: this.schema });
|
|
4195
|
-
const legacyEvals = new LegacyEvalsPG({ client: this.#db, schema: this.schema });
|
|
4196
3927
|
const memory = new MemoryPG({ client: this.#db, schema: this.schema, operations });
|
|
4197
3928
|
const observability = new ObservabilityPG({ client: this.#db, operations, schema: this.schema });
|
|
4198
3929
|
this.stores = {
|
|
4199
3930
|
operations,
|
|
4200
3931
|
scores,
|
|
4201
|
-
traces,
|
|
4202
3932
|
workflows,
|
|
4203
|
-
legacyEvals,
|
|
4204
3933
|
memory,
|
|
4205
3934
|
observability
|
|
4206
3935
|
};
|
|
@@ -4241,30 +3970,11 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4241
3970
|
hasColumn: true,
|
|
4242
3971
|
createTable: true,
|
|
4243
3972
|
deleteMessages: true,
|
|
4244
|
-
|
|
3973
|
+
observabilityInstance: true,
|
|
4245
3974
|
indexManagement: true,
|
|
4246
|
-
|
|
3975
|
+
listScoresBySpan: true
|
|
4247
3976
|
};
|
|
4248
3977
|
}
|
|
4249
|
-
/** @deprecated use getEvals instead */
|
|
4250
|
-
async getEvalsByAgentName(agentName, type) {
|
|
4251
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
4252
|
-
}
|
|
4253
|
-
async getEvals(options = {}) {
|
|
4254
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
4255
|
-
}
|
|
4256
|
-
/**
|
|
4257
|
-
* @deprecated use getTracesPaginated instead
|
|
4258
|
-
*/
|
|
4259
|
-
async getTraces(args) {
|
|
4260
|
-
return this.stores.traces.getTraces(args);
|
|
4261
|
-
}
|
|
4262
|
-
async getTracesPaginated(args) {
|
|
4263
|
-
return this.stores.traces.getTracesPaginated(args);
|
|
4264
|
-
}
|
|
4265
|
-
async batchTraceInsert({ records }) {
|
|
4266
|
-
return this.stores.traces.batchTraceInsert({ records });
|
|
4267
|
-
}
|
|
4268
3978
|
async createTable({
|
|
4269
3979
|
tableName,
|
|
4270
3980
|
schema
|
|
@@ -4299,15 +4009,6 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4299
4009
|
async getThreadById({ threadId }) {
|
|
4300
4010
|
return this.stores.memory.getThreadById({ threadId });
|
|
4301
4011
|
}
|
|
4302
|
-
/**
|
|
4303
|
-
* @deprecated use getThreadsByResourceIdPaginated instead
|
|
4304
|
-
*/
|
|
4305
|
-
async getThreadsByResourceId(args) {
|
|
4306
|
-
return this.stores.memory.getThreadsByResourceId(args);
|
|
4307
|
-
}
|
|
4308
|
-
async getThreadsByResourceIdPaginated(args) {
|
|
4309
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
4310
|
-
}
|
|
4311
4012
|
async saveThread({ thread }) {
|
|
4312
4013
|
return this.stores.memory.saveThread({ thread });
|
|
4313
4014
|
}
|
|
@@ -4321,17 +4022,8 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4321
4022
|
async deleteThread({ threadId }) {
|
|
4322
4023
|
return this.stores.memory.deleteThread({ threadId });
|
|
4323
4024
|
}
|
|
4324
|
-
async
|
|
4325
|
-
return this.stores.memory.
|
|
4326
|
-
}
|
|
4327
|
-
async getMessagesById({
|
|
4328
|
-
messageIds,
|
|
4329
|
-
format
|
|
4330
|
-
}) {
|
|
4331
|
-
return this.stores.memory.getMessagesById({ messageIds, format });
|
|
4332
|
-
}
|
|
4333
|
-
async getMessagesPaginated(args) {
|
|
4334
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
4025
|
+
async listMessagesById({ messageIds }) {
|
|
4026
|
+
return this.stores.memory.listMessagesById({ messageIds });
|
|
4335
4027
|
}
|
|
4336
4028
|
async saveMessages(args) {
|
|
4337
4029
|
return this.stores.memory.saveMessages(args);
|
|
@@ -4365,9 +4057,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4365
4057
|
runId,
|
|
4366
4058
|
stepId,
|
|
4367
4059
|
result,
|
|
4368
|
-
|
|
4060
|
+
requestContext
|
|
4369
4061
|
}) {
|
|
4370
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result,
|
|
4062
|
+
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
4371
4063
|
}
|
|
4372
4064
|
async updateWorkflowState({
|
|
4373
4065
|
workflowName,
|
|
@@ -4390,15 +4082,8 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4390
4082
|
}) {
|
|
4391
4083
|
return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
4392
4084
|
}
|
|
4393
|
-
async
|
|
4394
|
-
|
|
4395
|
-
fromDate,
|
|
4396
|
-
toDate,
|
|
4397
|
-
limit,
|
|
4398
|
-
offset,
|
|
4399
|
-
resourceId
|
|
4400
|
-
} = {}) {
|
|
4401
|
-
return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
|
|
4085
|
+
async listWorkflowRuns(args = {}) {
|
|
4086
|
+
return this.stores.workflows.listWorkflowRuns(args);
|
|
4402
4087
|
}
|
|
4403
4088
|
async getWorkflowRunById({
|
|
4404
4089
|
runId,
|
|
@@ -4410,9 +4095,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4410
4095
|
this.pgp.end();
|
|
4411
4096
|
}
|
|
4412
4097
|
/**
|
|
4413
|
-
*
|
|
4098
|
+
* Tracing / Observability
|
|
4414
4099
|
*/
|
|
4415
|
-
async
|
|
4100
|
+
async createSpan(span) {
|
|
4416
4101
|
if (!this.stores.observability) {
|
|
4417
4102
|
throw new MastraError({
|
|
4418
4103
|
id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
@@ -4421,9 +4106,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4421
4106
|
text: "Observability storage is not initialized"
|
|
4422
4107
|
});
|
|
4423
4108
|
}
|
|
4424
|
-
return this.stores.observability.
|
|
4109
|
+
return this.stores.observability.createSpan(span);
|
|
4425
4110
|
}
|
|
4426
|
-
async
|
|
4111
|
+
async updateSpan({
|
|
4427
4112
|
spanId,
|
|
4428
4113
|
traceId,
|
|
4429
4114
|
updates
|
|
@@ -4436,9 +4121,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4436
4121
|
text: "Observability storage is not initialized"
|
|
4437
4122
|
});
|
|
4438
4123
|
}
|
|
4439
|
-
return this.stores.observability.
|
|
4124
|
+
return this.stores.observability.updateSpan({ spanId, traceId, updates });
|
|
4440
4125
|
}
|
|
4441
|
-
async
|
|
4126
|
+
async getTrace(traceId) {
|
|
4442
4127
|
if (!this.stores.observability) {
|
|
4443
4128
|
throw new MastraError({
|
|
4444
4129
|
id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
@@ -4447,9 +4132,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4447
4132
|
text: "Observability storage is not initialized"
|
|
4448
4133
|
});
|
|
4449
4134
|
}
|
|
4450
|
-
return this.stores.observability.
|
|
4135
|
+
return this.stores.observability.getTrace(traceId);
|
|
4451
4136
|
}
|
|
4452
|
-
async
|
|
4137
|
+
async getTracesPaginated(args) {
|
|
4453
4138
|
if (!this.stores.observability) {
|
|
4454
4139
|
throw new MastraError({
|
|
4455
4140
|
id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
@@ -4458,9 +4143,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4458
4143
|
text: "Observability storage is not initialized"
|
|
4459
4144
|
});
|
|
4460
4145
|
}
|
|
4461
|
-
return this.stores.observability.
|
|
4146
|
+
return this.stores.observability.getTracesPaginated(args);
|
|
4462
4147
|
}
|
|
4463
|
-
async
|
|
4148
|
+
async batchCreateSpans(args) {
|
|
4464
4149
|
if (!this.stores.observability) {
|
|
4465
4150
|
throw new MastraError({
|
|
4466
4151
|
id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
@@ -4469,9 +4154,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4469
4154
|
text: "Observability storage is not initialized"
|
|
4470
4155
|
});
|
|
4471
4156
|
}
|
|
4472
|
-
return this.stores.observability.
|
|
4157
|
+
return this.stores.observability.batchCreateSpans(args);
|
|
4473
4158
|
}
|
|
4474
|
-
async
|
|
4159
|
+
async batchUpdateSpans(args) {
|
|
4475
4160
|
if (!this.stores.observability) {
|
|
4476
4161
|
throw new MastraError({
|
|
4477
4162
|
id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
@@ -4480,9 +4165,9 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4480
4165
|
text: "Observability storage is not initialized"
|
|
4481
4166
|
});
|
|
4482
4167
|
}
|
|
4483
|
-
return this.stores.observability.
|
|
4168
|
+
return this.stores.observability.batchUpdateSpans(args);
|
|
4484
4169
|
}
|
|
4485
|
-
async
|
|
4170
|
+
async batchDeleteTraces(args) {
|
|
4486
4171
|
if (!this.stores.observability) {
|
|
4487
4172
|
throw new MastraError({
|
|
4488
4173
|
id: "PG_STORE_OBSERVABILITY_NOT_INITIALIZED",
|
|
@@ -4491,7 +4176,7 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4491
4176
|
text: "Observability storage is not initialized"
|
|
4492
4177
|
});
|
|
4493
4178
|
}
|
|
4494
|
-
return this.stores.observability.
|
|
4179
|
+
return this.stores.observability.batchDeleteTraces(args);
|
|
4495
4180
|
}
|
|
4496
4181
|
/**
|
|
4497
4182
|
* Scorers
|
|
@@ -4499,41 +4184,41 @@ var PostgresStore = class extends MastraStorage {
|
|
|
4499
4184
|
async getScoreById({ id }) {
|
|
4500
4185
|
return this.stores.scores.getScoreById({ id });
|
|
4501
4186
|
}
|
|
4502
|
-
async
|
|
4187
|
+
async listScoresByScorerId({
|
|
4503
4188
|
scorerId,
|
|
4504
4189
|
pagination,
|
|
4505
4190
|
entityId,
|
|
4506
4191
|
entityType,
|
|
4507
4192
|
source
|
|
4508
4193
|
}) {
|
|
4509
|
-
return this.stores.scores.
|
|
4194
|
+
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
4510
4195
|
}
|
|
4511
4196
|
async saveScore(score) {
|
|
4512
4197
|
return this.stores.scores.saveScore(score);
|
|
4513
4198
|
}
|
|
4514
|
-
async
|
|
4199
|
+
async listScoresByRunId({
|
|
4515
4200
|
runId,
|
|
4516
4201
|
pagination
|
|
4517
4202
|
}) {
|
|
4518
|
-
return this.stores.scores.
|
|
4203
|
+
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
4519
4204
|
}
|
|
4520
|
-
async
|
|
4205
|
+
async listScoresByEntityId({
|
|
4521
4206
|
entityId,
|
|
4522
4207
|
entityType,
|
|
4523
4208
|
pagination
|
|
4524
4209
|
}) {
|
|
4525
|
-
return this.stores.scores.
|
|
4210
|
+
return this.stores.scores.listScoresByEntityId({
|
|
4526
4211
|
entityId,
|
|
4527
4212
|
entityType,
|
|
4528
4213
|
pagination
|
|
4529
4214
|
});
|
|
4530
4215
|
}
|
|
4531
|
-
async
|
|
4216
|
+
async listScoresBySpan({
|
|
4532
4217
|
traceId,
|
|
4533
4218
|
spanId,
|
|
4534
4219
|
pagination
|
|
4535
4220
|
}) {
|
|
4536
|
-
return this.stores.scores.
|
|
4221
|
+
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
4537
4222
|
}
|
|
4538
4223
|
};
|
|
4539
4224
|
|