@mastra/pg 1.9.1 → 1.9.2-alpha.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 +18 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +430 -81
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +431 -83
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +25 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/domains/observability/index.d.ts +2 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +2 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -4280,6 +4280,307 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
|
|
|
4280
4280
|
};
|
|
4281
4281
|
}
|
|
4282
4282
|
};
|
|
4283
|
+
function getSchemaName3(schema) {
|
|
4284
|
+
return schema ? `"${schema}"` : '"public"';
|
|
4285
|
+
}
|
|
4286
|
+
function getTableName3(schemaName) {
|
|
4287
|
+
const quoted = `"${storage.TABLE_BACKGROUND_TASKS}"`;
|
|
4288
|
+
return schemaName ? `${schemaName}.${quoted}` : quoted;
|
|
4289
|
+
}
|
|
4290
|
+
function serializeJson(v) {
|
|
4291
|
+
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
4292
|
+
return v ?? null;
|
|
4293
|
+
}
|
|
4294
|
+
function parseJson(v) {
|
|
4295
|
+
if (typeof v === "string") {
|
|
4296
|
+
try {
|
|
4297
|
+
return JSON.parse(v);
|
|
4298
|
+
} catch {
|
|
4299
|
+
return v;
|
|
4300
|
+
}
|
|
4301
|
+
}
|
|
4302
|
+
return v ?? void 0;
|
|
4303
|
+
}
|
|
4304
|
+
function rowToTask(row) {
|
|
4305
|
+
return {
|
|
4306
|
+
id: row.id,
|
|
4307
|
+
status: row.status,
|
|
4308
|
+
toolName: row.tool_name,
|
|
4309
|
+
toolCallId: row.tool_call_id,
|
|
4310
|
+
args: parseJson(row.args),
|
|
4311
|
+
agentId: row.agent_id,
|
|
4312
|
+
threadId: row.thread_id ?? void 0,
|
|
4313
|
+
resourceId: row.resource_id ?? void 0,
|
|
4314
|
+
runId: row.run_id,
|
|
4315
|
+
result: parseJson(row.result),
|
|
4316
|
+
error: parseJson(row.error),
|
|
4317
|
+
retryCount: Number(row.retry_count),
|
|
4318
|
+
maxRetries: Number(row.max_retries),
|
|
4319
|
+
timeoutMs: Number(row.timeout_ms),
|
|
4320
|
+
createdAt: row.createdAt instanceof Date ? row.createdAt : new Date(row.createdAt),
|
|
4321
|
+
startedAt: row.startedAt ? row.startedAt instanceof Date ? row.startedAt : new Date(row.startedAt) : void 0,
|
|
4322
|
+
completedAt: row.completedAt ? row.completedAt instanceof Date ? row.completedAt : new Date(row.completedAt) : void 0
|
|
4323
|
+
};
|
|
4324
|
+
}
|
|
4325
|
+
var BackgroundTasksPG = class _BackgroundTasksPG extends storage.BackgroundTasksStorage {
|
|
4326
|
+
#db;
|
|
4327
|
+
#schema;
|
|
4328
|
+
#skipDefaultIndexes;
|
|
4329
|
+
#indexes;
|
|
4330
|
+
static MANAGED_TABLES = [storage.TABLE_BACKGROUND_TASKS];
|
|
4331
|
+
constructor(config) {
|
|
4332
|
+
super();
|
|
4333
|
+
const { client, schemaName, skipDefaultIndexes, indexes } = resolvePgConfig(config);
|
|
4334
|
+
this.#db = new PgDB({ client, schemaName, skipDefaultIndexes });
|
|
4335
|
+
this.#schema = schemaName || "public";
|
|
4336
|
+
this.#skipDefaultIndexes = skipDefaultIndexes;
|
|
4337
|
+
this.#indexes = indexes?.filter((idx) => _BackgroundTasksPG.MANAGED_TABLES.includes(idx.table));
|
|
4338
|
+
}
|
|
4339
|
+
async init() {
|
|
4340
|
+
await this.#db.createTable({
|
|
4341
|
+
tableName: storage.TABLE_BACKGROUND_TASKS,
|
|
4342
|
+
schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS]
|
|
4343
|
+
});
|
|
4344
|
+
await this.createDefaultIndexes();
|
|
4345
|
+
await this.createCustomIndexes();
|
|
4346
|
+
}
|
|
4347
|
+
static getDefaultIndexDefs(schemaPrefix) {
|
|
4348
|
+
return [
|
|
4349
|
+
{
|
|
4350
|
+
name: `${schemaPrefix}mastra_bg_tasks_status_created_at_idx`,
|
|
4351
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
4352
|
+
columns: ["status", "createdAt"]
|
|
4353
|
+
},
|
|
4354
|
+
{
|
|
4355
|
+
name: `${schemaPrefix}mastra_bg_tasks_agent_status_idx`,
|
|
4356
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
4357
|
+
columns: ["agent_id", "status"]
|
|
4358
|
+
},
|
|
4359
|
+
{
|
|
4360
|
+
name: `${schemaPrefix}mastra_bg_tasks_thread_idx`,
|
|
4361
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
4362
|
+
columns: ["thread_id", "createdAt"]
|
|
4363
|
+
},
|
|
4364
|
+
{
|
|
4365
|
+
name: `${schemaPrefix}mastra_bg_tasks_tool_call_idx`,
|
|
4366
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
4367
|
+
columns: ["tool_call_id"]
|
|
4368
|
+
}
|
|
4369
|
+
];
|
|
4370
|
+
}
|
|
4371
|
+
static getExportDDL(schemaName) {
|
|
4372
|
+
const statements = [];
|
|
4373
|
+
const parsedSchema = schemaName ? utils.parseSqlIdentifier(schemaName, "schema name") : "";
|
|
4374
|
+
const schemaPrefix = parsedSchema && parsedSchema !== "public" ? `${parsedSchema}_` : "";
|
|
4375
|
+
statements.push(
|
|
4376
|
+
generateTableSQL({
|
|
4377
|
+
tableName: storage.TABLE_BACKGROUND_TASKS,
|
|
4378
|
+
schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS],
|
|
4379
|
+
schemaName,
|
|
4380
|
+
includeAllConstraints: true
|
|
4381
|
+
})
|
|
4382
|
+
);
|
|
4383
|
+
for (const idx of _BackgroundTasksPG.getDefaultIndexDefs(schemaPrefix)) {
|
|
4384
|
+
statements.push(generateIndexSQL(idx, schemaName));
|
|
4385
|
+
}
|
|
4386
|
+
return statements;
|
|
4387
|
+
}
|
|
4388
|
+
getDefaultIndexDefinitions() {
|
|
4389
|
+
const schemaPrefix = this.#schema !== "public" ? `${this.#schema}_` : "";
|
|
4390
|
+
return _BackgroundTasksPG.getDefaultIndexDefs(schemaPrefix);
|
|
4391
|
+
}
|
|
4392
|
+
async createDefaultIndexes() {
|
|
4393
|
+
if (this.#skipDefaultIndexes) return;
|
|
4394
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
4395
|
+
try {
|
|
4396
|
+
await this.#db.createIndex(indexDef);
|
|
4397
|
+
} catch (error) {
|
|
4398
|
+
this.logger?.warn?.(`Failed to create index ${indexDef.name}:`, error);
|
|
4399
|
+
}
|
|
4400
|
+
}
|
|
4401
|
+
}
|
|
4402
|
+
async createCustomIndexes() {
|
|
4403
|
+
if (!this.#indexes || this.#indexes.length === 0) return;
|
|
4404
|
+
for (const indexDef of this.#indexes) {
|
|
4405
|
+
try {
|
|
4406
|
+
await this.#db.createIndex(indexDef);
|
|
4407
|
+
} catch (error) {
|
|
4408
|
+
this.logger?.warn?.(`Failed to create custom index ${indexDef.name}:`, error);
|
|
4409
|
+
}
|
|
4410
|
+
}
|
|
4411
|
+
}
|
|
4412
|
+
async dangerouslyClearAll() {
|
|
4413
|
+
await this.#db.clearTable({ tableName: storage.TABLE_BACKGROUND_TASKS });
|
|
4414
|
+
}
|
|
4415
|
+
async createTask(task) {
|
|
4416
|
+
await this.#db.insert({
|
|
4417
|
+
tableName: storage.TABLE_BACKGROUND_TASKS,
|
|
4418
|
+
record: {
|
|
4419
|
+
id: task.id,
|
|
4420
|
+
tool_call_id: task.toolCallId,
|
|
4421
|
+
tool_name: task.toolName,
|
|
4422
|
+
agent_id: task.agentId,
|
|
4423
|
+
thread_id: task.threadId ?? null,
|
|
4424
|
+
resource_id: task.resourceId ?? null,
|
|
4425
|
+
run_id: task.runId,
|
|
4426
|
+
status: task.status,
|
|
4427
|
+
args: serializeJson(task.args),
|
|
4428
|
+
result: serializeJson(task.result),
|
|
4429
|
+
error: serializeJson(task.error),
|
|
4430
|
+
retry_count: task.retryCount,
|
|
4431
|
+
max_retries: task.maxRetries,
|
|
4432
|
+
timeout_ms: task.timeoutMs,
|
|
4433
|
+
createdAt: task.createdAt.toISOString(),
|
|
4434
|
+
startedAt: task.startedAt?.toISOString() ?? null,
|
|
4435
|
+
completedAt: task.completedAt?.toISOString() ?? null
|
|
4436
|
+
}
|
|
4437
|
+
});
|
|
4438
|
+
}
|
|
4439
|
+
async updateTask(taskId, update) {
|
|
4440
|
+
const setClauses = [];
|
|
4441
|
+
const params = [];
|
|
4442
|
+
let paramIdx = 1;
|
|
4443
|
+
if ("status" in update) {
|
|
4444
|
+
setClauses.push(`"status" = $${paramIdx++}`);
|
|
4445
|
+
params.push(update.status);
|
|
4446
|
+
}
|
|
4447
|
+
if ("result" in update) {
|
|
4448
|
+
setClauses.push(`"result" = $${paramIdx++}`);
|
|
4449
|
+
params.push(serializeJson(update.result));
|
|
4450
|
+
}
|
|
4451
|
+
if ("error" in update) {
|
|
4452
|
+
setClauses.push(`"error" = $${paramIdx++}`);
|
|
4453
|
+
params.push(serializeJson(update.error));
|
|
4454
|
+
}
|
|
4455
|
+
if ("retryCount" in update) {
|
|
4456
|
+
setClauses.push(`"retry_count" = $${paramIdx++}`);
|
|
4457
|
+
params.push(update.retryCount);
|
|
4458
|
+
}
|
|
4459
|
+
if ("startedAt" in update) {
|
|
4460
|
+
setClauses.push(`"startedAt" = $${paramIdx++}`);
|
|
4461
|
+
params.push(update.startedAt?.toISOString() ?? null);
|
|
4462
|
+
}
|
|
4463
|
+
if ("completedAt" in update) {
|
|
4464
|
+
setClauses.push(`"completedAt" = $${paramIdx++}`);
|
|
4465
|
+
params.push(update.completedAt?.toISOString() ?? null);
|
|
4466
|
+
}
|
|
4467
|
+
if (setClauses.length === 0) return;
|
|
4468
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4469
|
+
params.push(taskId);
|
|
4470
|
+
await this.#db.client.none(`UPDATE ${table} SET ${setClauses.join(", ")} WHERE "id" = $${paramIdx}`, params);
|
|
4471
|
+
}
|
|
4472
|
+
async getTask(taskId) {
|
|
4473
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4474
|
+
const row = await this.#db.client.oneOrNone(`SELECT * FROM ${table} WHERE "id" = $1`, [taskId]);
|
|
4475
|
+
return row ? rowToTask(row) : null;
|
|
4476
|
+
}
|
|
4477
|
+
async listTasks(filter) {
|
|
4478
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4479
|
+
const conditions = [];
|
|
4480
|
+
const params = [];
|
|
4481
|
+
let paramIdx = 1;
|
|
4482
|
+
if (filter.status) {
|
|
4483
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
4484
|
+
const placeholders = statuses.map(() => `$${paramIdx++}`);
|
|
4485
|
+
conditions.push(`"status" IN (${placeholders.join(", ")})`);
|
|
4486
|
+
params.push(...statuses);
|
|
4487
|
+
}
|
|
4488
|
+
if (filter.agentId) {
|
|
4489
|
+
conditions.push(`"agent_id" = $${paramIdx++}`);
|
|
4490
|
+
params.push(filter.agentId);
|
|
4491
|
+
}
|
|
4492
|
+
if (filter.threadId) {
|
|
4493
|
+
conditions.push(`"thread_id" = $${paramIdx++}`);
|
|
4494
|
+
params.push(filter.threadId);
|
|
4495
|
+
}
|
|
4496
|
+
if (filter.resourceId) {
|
|
4497
|
+
conditions.push(`"resource_id" = $${paramIdx++}`);
|
|
4498
|
+
params.push(filter.resourceId);
|
|
4499
|
+
}
|
|
4500
|
+
if (filter.runId) {
|
|
4501
|
+
conditions.push(`"run_id" = $${paramIdx++}`);
|
|
4502
|
+
params.push(filter.runId);
|
|
4503
|
+
}
|
|
4504
|
+
if (filter.toolName) {
|
|
4505
|
+
conditions.push(`"tool_name" = $${paramIdx++}`);
|
|
4506
|
+
params.push(filter.toolName);
|
|
4507
|
+
}
|
|
4508
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? '"startedAt"' : filter.dateFilterBy === "completedAt" ? '"completedAt"' : '"createdAt"';
|
|
4509
|
+
if (filter.fromDate) {
|
|
4510
|
+
conditions.push(`${dateCol} >= $${paramIdx++}`);
|
|
4511
|
+
params.push(filter.fromDate.toISOString());
|
|
4512
|
+
}
|
|
4513
|
+
if (filter.toDate) {
|
|
4514
|
+
conditions.push(`${dateCol} < $${paramIdx++}`);
|
|
4515
|
+
params.push(filter.toDate.toISOString());
|
|
4516
|
+
}
|
|
4517
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
4518
|
+
const countResult = await this.#db.client.oneOrNone(
|
|
4519
|
+
`SELECT COUNT(*) as count FROM ${table} ${where}`,
|
|
4520
|
+
params.slice(0, paramIdx - 1)
|
|
4521
|
+
);
|
|
4522
|
+
const total = Number(countResult?.count ?? 0);
|
|
4523
|
+
const orderCol = filter.orderBy === "startedAt" ? '"startedAt"' : filter.orderBy === "completedAt" ? '"completedAt"' : '"createdAt"';
|
|
4524
|
+
const direction = filter.orderDirection === "desc" ? "DESC" : "ASC";
|
|
4525
|
+
let sql = `SELECT * FROM ${table} ${where} ORDER BY ${orderCol} ${direction}`;
|
|
4526
|
+
if (filter.perPage != null) {
|
|
4527
|
+
sql += ` LIMIT $${paramIdx++}`;
|
|
4528
|
+
params.push(filter.perPage);
|
|
4529
|
+
if (filter.page != null) {
|
|
4530
|
+
sql += ` OFFSET $${paramIdx++}`;
|
|
4531
|
+
params.push(filter.page * filter.perPage);
|
|
4532
|
+
}
|
|
4533
|
+
}
|
|
4534
|
+
const rows = await this.#db.client.manyOrNone(sql, params);
|
|
4535
|
+
return { tasks: rows.map(rowToTask), total };
|
|
4536
|
+
}
|
|
4537
|
+
async deleteTask(taskId) {
|
|
4538
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4539
|
+
await this.#db.client.none(`DELETE FROM ${table} WHERE "id" = $1`, [taskId]);
|
|
4540
|
+
}
|
|
4541
|
+
async deleteTasks(filter) {
|
|
4542
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4543
|
+
const conditions = [];
|
|
4544
|
+
const params = [];
|
|
4545
|
+
let paramIdx = 1;
|
|
4546
|
+
if (filter.status) {
|
|
4547
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
4548
|
+
const placeholders = statuses.map(() => `$${paramIdx++}`);
|
|
4549
|
+
conditions.push(`"status" IN (${placeholders.join(", ")})`);
|
|
4550
|
+
params.push(...statuses);
|
|
4551
|
+
}
|
|
4552
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? '"startedAt"' : filter.dateFilterBy === "completedAt" ? '"completedAt"' : '"createdAt"';
|
|
4553
|
+
if (filter.fromDate) {
|
|
4554
|
+
conditions.push(`${dateCol} >= $${paramIdx++}`);
|
|
4555
|
+
params.push(filter.fromDate.toISOString());
|
|
4556
|
+
}
|
|
4557
|
+
if (filter.toDate) {
|
|
4558
|
+
conditions.push(`${dateCol} < $${paramIdx++}`);
|
|
4559
|
+
params.push(filter.toDate.toISOString());
|
|
4560
|
+
}
|
|
4561
|
+
if (filter.agentId) {
|
|
4562
|
+
conditions.push(`"agent_id" = $${paramIdx++}`);
|
|
4563
|
+
params.push(filter.agentId);
|
|
4564
|
+
}
|
|
4565
|
+
if (conditions.length === 0) return;
|
|
4566
|
+
await this.#db.client.none(`DELETE FROM ${table} WHERE ${conditions.join(" AND ")}`, params);
|
|
4567
|
+
}
|
|
4568
|
+
async getRunningCount() {
|
|
4569
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4570
|
+
const result = await this.#db.client.oneOrNone(
|
|
4571
|
+
`SELECT COUNT(*) FROM ${table} WHERE "status" = 'running'`
|
|
4572
|
+
);
|
|
4573
|
+
return Number(result?.count ?? 0);
|
|
4574
|
+
}
|
|
4575
|
+
async getRunningCountByAgent(agentId) {
|
|
4576
|
+
const table = getTableName3(getSchemaName3(this.#schema));
|
|
4577
|
+
const result = await this.#db.client.oneOrNone(
|
|
4578
|
+
`SELECT COUNT(*) FROM ${table} WHERE "status" = 'running' AND "agent_id" = $1`,
|
|
4579
|
+
[agentId]
|
|
4580
|
+
);
|
|
4581
|
+
return Number(result?.count ?? 0);
|
|
4582
|
+
}
|
|
4583
|
+
};
|
|
4283
4584
|
var BlobsPG = class extends storage.BlobStore {
|
|
4284
4585
|
#db;
|
|
4285
4586
|
#schema;
|
|
@@ -7169,10 +7470,10 @@ try {
|
|
|
7169
7470
|
_omTableSchema = storage.OBSERVATIONAL_MEMORY_TABLE_SCHEMA;
|
|
7170
7471
|
} catch {
|
|
7171
7472
|
}
|
|
7172
|
-
function
|
|
7473
|
+
function getSchemaName4(schema) {
|
|
7173
7474
|
return schema ? `"${schema}"` : '"public"';
|
|
7174
7475
|
}
|
|
7175
|
-
function
|
|
7476
|
+
function getTableName4({ indexName, schemaName }) {
|
|
7176
7477
|
const quotedIndexName = `"${indexName}"`;
|
|
7177
7478
|
return schemaName ? `${schemaName}.${quotedIndexName}` : quotedIndexName;
|
|
7178
7479
|
}
|
|
@@ -7237,9 +7538,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7237
7538
|
ifNotExists: ["resourceId"]
|
|
7238
7539
|
});
|
|
7239
7540
|
if (omSchema) {
|
|
7240
|
-
const omTableName =
|
|
7541
|
+
const omTableName = getTableName4({
|
|
7241
7542
|
indexName: OM_TABLE,
|
|
7242
|
-
schemaName:
|
|
7543
|
+
schemaName: getSchemaName4(this.#schema)
|
|
7243
7544
|
});
|
|
7244
7545
|
await this.#db.client.none(`CREATE INDEX IF NOT EXISTS idx_om_lookup_key ON ${omTableName} ("lookupKey")`);
|
|
7245
7546
|
}
|
|
@@ -7362,7 +7663,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7362
7663
|
}
|
|
7363
7664
|
async getThreadById({ threadId }) {
|
|
7364
7665
|
try {
|
|
7365
|
-
const tableName =
|
|
7666
|
+
const tableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
7366
7667
|
const thread = await this.#db.client.oneOrNone(
|
|
7367
7668
|
`SELECT * FROM ${tableName} WHERE id = $1`,
|
|
7368
7669
|
[threadId]
|
|
@@ -7420,7 +7721,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7420
7721
|
const { field, direction } = this.parseOrderBy(orderBy);
|
|
7421
7722
|
const { offset, perPage: perPageForResponse } = storage.calculatePagination(page, perPageInput, perPage);
|
|
7422
7723
|
try {
|
|
7423
|
-
const tableName =
|
|
7724
|
+
const tableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
7424
7725
|
const whereClauses = [];
|
|
7425
7726
|
const queryParams = [];
|
|
7426
7727
|
let paramIndex = 1;
|
|
@@ -7499,7 +7800,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7499
7800
|
}
|
|
7500
7801
|
async saveThread({ thread }) {
|
|
7501
7802
|
try {
|
|
7502
|
-
const tableName =
|
|
7803
|
+
const tableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
7503
7804
|
await this.#db.client.none(
|
|
7504
7805
|
`INSERT INTO ${tableName} (
|
|
7505
7806
|
id,
|
|
@@ -7550,7 +7851,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7550
7851
|
title,
|
|
7551
7852
|
metadata
|
|
7552
7853
|
}) {
|
|
7553
|
-
const threadTableName =
|
|
7854
|
+
const threadTableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
7554
7855
|
const existingThread = await this.getThreadById({ threadId: id });
|
|
7555
7856
|
if (!existingThread) {
|
|
7556
7857
|
throw new error.MastraError({
|
|
@@ -7607,8 +7908,8 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7607
7908
|
}
|
|
7608
7909
|
async deleteThread({ threadId }) {
|
|
7609
7910
|
try {
|
|
7610
|
-
const tableName =
|
|
7611
|
-
const threadTableName =
|
|
7911
|
+
const tableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
7912
|
+
const threadTableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
7612
7913
|
await this.#db.client.tx(async (t) => {
|
|
7613
7914
|
await t.none(`DELETE FROM ${tableName} WHERE thread_id = $1`, [threadId]);
|
|
7614
7915
|
const schemaName = this.#schema || "public";
|
|
@@ -7622,7 +7923,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7622
7923
|
[schemaName]
|
|
7623
7924
|
);
|
|
7624
7925
|
for (const { tablename } of vectorTables) {
|
|
7625
|
-
const vectorTableName =
|
|
7926
|
+
const vectorTableName = getTableName4({ indexName: tablename, schemaName: getSchemaName4(this.#schema) });
|
|
7626
7927
|
await t.none(`DELETE FROM ${vectorTableName} WHERE metadata->>'thread_id' = $1`, [threadId]);
|
|
7627
7928
|
}
|
|
7628
7929
|
await t.none(`DELETE FROM ${threadTableName} WHERE id = $1`, [threadId]);
|
|
@@ -7673,7 +7974,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7673
7974
|
}
|
|
7674
7975
|
async _getIncludedMessages({ include }) {
|
|
7675
7976
|
if (!include || include.length === 0) return null;
|
|
7676
|
-
const tableName =
|
|
7977
|
+
const tableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
7677
7978
|
const selectColumns = `id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
7678
7979
|
const targetIds = include.map((inc) => inc.id).filter(Boolean);
|
|
7679
7980
|
if (targetIds.length === 0) return null;
|
|
@@ -7754,7 +8055,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7754
8055
|
if (messageIds.length === 0) return { messages: [] };
|
|
7755
8056
|
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
7756
8057
|
try {
|
|
7757
|
-
const tableName =
|
|
8058
|
+
const tableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
7758
8059
|
const query = `
|
|
7759
8060
|
${selectStatement} FROM ${tableName}
|
|
7760
8061
|
WHERE id IN (${inPlaceholders(messageIds.length)})
|
|
@@ -7817,7 +8118,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7817
8118
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
7818
8119
|
const orderByStatement = `ORDER BY COALESCE("${field}Z", "${field}") ${direction}`;
|
|
7819
8120
|
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
7820
|
-
const tableName =
|
|
8121
|
+
const tableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
7821
8122
|
const conditions = [`thread_id IN (${inPlaceholders(threadIds.length)})`];
|
|
7822
8123
|
const queryParams = [...threadIds];
|
|
7823
8124
|
let paramIndex = threadIds.length + 1;
|
|
@@ -7956,7 +8257,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
7956
8257
|
const { field, direction } = this.parseOrderBy(orderBy, "ASC");
|
|
7957
8258
|
const orderByStatement = `ORDER BY COALESCE("${field}Z", "${field}") ${direction}`;
|
|
7958
8259
|
const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
|
|
7959
|
-
const tableName =
|
|
8260
|
+
const tableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
7960
8261
|
const conditions = [];
|
|
7961
8262
|
const queryParams = [];
|
|
7962
8263
|
let paramIndex = 1;
|
|
@@ -8083,7 +8384,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8083
8384
|
});
|
|
8084
8385
|
}
|
|
8085
8386
|
try {
|
|
8086
|
-
const tableName =
|
|
8387
|
+
const tableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
8087
8388
|
await this.#db.client.tx(async (t) => {
|
|
8088
8389
|
for (const message of messages) {
|
|
8089
8390
|
if (!message.threadId) {
|
|
@@ -8117,7 +8418,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8117
8418
|
]
|
|
8118
8419
|
);
|
|
8119
8420
|
}
|
|
8120
|
-
const threadTableName =
|
|
8421
|
+
const threadTableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
8121
8422
|
const now = /* @__PURE__ */ new Date();
|
|
8122
8423
|
await t.none(
|
|
8123
8424
|
`UPDATE ${threadTableName}
|
|
@@ -8161,7 +8462,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8161
8462
|
return [];
|
|
8162
8463
|
}
|
|
8163
8464
|
const messageIds = messages.map((m) => m.id);
|
|
8164
|
-
const selectQuery = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId" FROM ${
|
|
8465
|
+
const selectQuery = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId" FROM ${getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) })} WHERE id IN (${inPlaceholders(messageIds.length)})`;
|
|
8165
8466
|
const existingMessagesDb = await this.#db.client.manyOrNone(selectQuery, messageIds);
|
|
8166
8467
|
if (existingMessagesDb.length === 0) {
|
|
8167
8468
|
return [];
|
|
@@ -8218,7 +8519,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8218
8519
|
}
|
|
8219
8520
|
if (setClauses.length > 0) {
|
|
8220
8521
|
values.push(id);
|
|
8221
|
-
const sql = `UPDATE ${
|
|
8522
|
+
const sql = `UPDATE ${getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) })} SET ${setClauses.join(", ")} WHERE id = $${paramIndex}`;
|
|
8222
8523
|
queries.push(t.none(sql, values));
|
|
8223
8524
|
}
|
|
8224
8525
|
}
|
|
@@ -8226,7 +8527,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8226
8527
|
const threadIds = Array.from(threadIdsToUpdate);
|
|
8227
8528
|
queries.push(
|
|
8228
8529
|
t.none(
|
|
8229
|
-
`UPDATE ${
|
|
8530
|
+
`UPDATE ${getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) })} SET "updatedAt" = NOW(), "updatedAtZ" = NOW() WHERE id IN (${inPlaceholders(threadIds.length)})`,
|
|
8230
8531
|
threadIds
|
|
8231
8532
|
)
|
|
8232
8533
|
);
|
|
@@ -8252,8 +8553,8 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8252
8553
|
return;
|
|
8253
8554
|
}
|
|
8254
8555
|
try {
|
|
8255
|
-
const messageTableName =
|
|
8256
|
-
const threadTableName =
|
|
8556
|
+
const messageTableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
8557
|
+
const threadTableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
8257
8558
|
await this.#db.client.tx(async (t) => {
|
|
8258
8559
|
const placeholders = messageIds.map((_, idx) => `$${idx + 1}`).join(",");
|
|
8259
8560
|
const messages = await t.manyOrNone(
|
|
@@ -8282,7 +8583,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8282
8583
|
}
|
|
8283
8584
|
}
|
|
8284
8585
|
async getResourceById({ resourceId }) {
|
|
8285
|
-
const tableName =
|
|
8586
|
+
const tableName = getTableName4({ indexName: storage.TABLE_RESOURCES, schemaName: getSchemaName4(this.#schema) });
|
|
8286
8587
|
const result = await this.#db.client.oneOrNone(
|
|
8287
8588
|
`SELECT * FROM ${tableName} WHERE id = $1`,
|
|
8288
8589
|
[resourceId]
|
|
@@ -8333,7 +8634,7 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8333
8634
|
},
|
|
8334
8635
|
updatedAt: /* @__PURE__ */ new Date()
|
|
8335
8636
|
};
|
|
8336
|
-
const tableName =
|
|
8637
|
+
const tableName = getTableName4({ indexName: storage.TABLE_RESOURCES, schemaName: getSchemaName4(this.#schema) });
|
|
8337
8638
|
const updates = [];
|
|
8338
8639
|
const values = [];
|
|
8339
8640
|
let paramIndex = 1;
|
|
@@ -8379,8 +8680,8 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8379
8680
|
details: { newThreadId }
|
|
8380
8681
|
});
|
|
8381
8682
|
}
|
|
8382
|
-
const threadTableName =
|
|
8383
|
-
const messageTableName =
|
|
8683
|
+
const threadTableName = getTableName4({ indexName: storage.TABLE_THREADS, schemaName: getSchemaName4(this.#schema) });
|
|
8684
|
+
const messageTableName = getTableName4({ indexName: storage.TABLE_MESSAGES, schemaName: getSchemaName4(this.#schema) });
|
|
8384
8685
|
try {
|
|
8385
8686
|
return await this.#db.client.tx(async (t) => {
|
|
8386
8687
|
let messageQuery = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"
|
|
@@ -8550,9 +8851,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8550
8851
|
async getObservationalMemory(threadId, resourceId) {
|
|
8551
8852
|
try {
|
|
8552
8853
|
const lookupKey = this.getOMKey(threadId, resourceId);
|
|
8553
|
-
const tableName =
|
|
8854
|
+
const tableName = getTableName4({
|
|
8554
8855
|
indexName: OM_TABLE,
|
|
8555
|
-
schemaName:
|
|
8856
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8556
8857
|
});
|
|
8557
8858
|
const result = await this.#db.client.oneOrNone(
|
|
8558
8859
|
`SELECT * FROM ${tableName} WHERE "lookupKey" = $1 ORDER BY "generationCount" DESC LIMIT 1`,
|
|
@@ -8575,9 +8876,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8575
8876
|
async getObservationalMemoryHistory(threadId, resourceId, limit = 10, options) {
|
|
8576
8877
|
try {
|
|
8577
8878
|
const lookupKey = this.getOMKey(threadId, resourceId);
|
|
8578
|
-
const tableName =
|
|
8879
|
+
const tableName = getTableName4({
|
|
8579
8880
|
indexName: OM_TABLE,
|
|
8580
|
-
schemaName:
|
|
8881
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8581
8882
|
});
|
|
8582
8883
|
const conditions = [`"lookupKey" = $1`];
|
|
8583
8884
|
const params = [lookupKey];
|
|
@@ -8642,9 +8943,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8642
8943
|
config: input.config,
|
|
8643
8944
|
observedTimezone: input.observedTimezone
|
|
8644
8945
|
};
|
|
8645
|
-
const tableName =
|
|
8946
|
+
const tableName = getTableName4({
|
|
8646
8947
|
indexName: OM_TABLE,
|
|
8647
|
-
schemaName:
|
|
8948
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8648
8949
|
});
|
|
8649
8950
|
const nowStr = now.toISOString();
|
|
8650
8951
|
await this.#db.client.none(
|
|
@@ -8715,9 +9016,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8715
9016
|
async insertObservationalMemoryRecord(record) {
|
|
8716
9017
|
try {
|
|
8717
9018
|
const lookupKey = this.getOMKey(record.threadId, record.resourceId);
|
|
8718
|
-
const tableName =
|
|
9019
|
+
const tableName = getTableName4({
|
|
8719
9020
|
indexName: OM_TABLE,
|
|
8720
|
-
schemaName:
|
|
9021
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8721
9022
|
});
|
|
8722
9023
|
const lastObservedAtStr = record.lastObservedAt ? record.lastObservedAt.toISOString() : null;
|
|
8723
9024
|
const lastBufferedAtTimeStr = record.lastBufferedAtTime ? record.lastBufferedAtTime.toISOString() : null;
|
|
@@ -8789,9 +9090,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8789
9090
|
async updateActiveObservations(input) {
|
|
8790
9091
|
try {
|
|
8791
9092
|
const now = /* @__PURE__ */ new Date();
|
|
8792
|
-
const tableName =
|
|
9093
|
+
const tableName = getTableName4({
|
|
8793
9094
|
indexName: OM_TABLE,
|
|
8794
|
-
schemaName:
|
|
9095
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8795
9096
|
});
|
|
8796
9097
|
const lastObservedAtStr = input.lastObservedAt.toISOString();
|
|
8797
9098
|
const nowStr = now.toISOString();
|
|
@@ -8873,9 +9174,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8873
9174
|
metadata: input.currentRecord.metadata,
|
|
8874
9175
|
observedTimezone: input.currentRecord.observedTimezone
|
|
8875
9176
|
};
|
|
8876
|
-
const tableName =
|
|
9177
|
+
const tableName = getTableName4({
|
|
8877
9178
|
indexName: OM_TABLE,
|
|
8878
|
-
schemaName:
|
|
9179
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8879
9180
|
});
|
|
8880
9181
|
const nowStr = now.toISOString();
|
|
8881
9182
|
const lastObservedAtStr = record.lastObservedAt?.toISOString() || null;
|
|
@@ -8949,9 +9250,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8949
9250
|
}
|
|
8950
9251
|
async setReflectingFlag(id, isReflecting) {
|
|
8951
9252
|
try {
|
|
8952
|
-
const tableName =
|
|
9253
|
+
const tableName = getTableName4({
|
|
8953
9254
|
indexName: OM_TABLE,
|
|
8954
|
-
schemaName:
|
|
9255
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8955
9256
|
});
|
|
8956
9257
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
8957
9258
|
const result = await this.#db.client.query(
|
|
@@ -8984,9 +9285,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8984
9285
|
}
|
|
8985
9286
|
async setObservingFlag(id, isObserving) {
|
|
8986
9287
|
try {
|
|
8987
|
-
const tableName =
|
|
9288
|
+
const tableName = getTableName4({
|
|
8988
9289
|
indexName: OM_TABLE,
|
|
8989
|
-
schemaName:
|
|
9290
|
+
schemaName: getSchemaName4(this.#schema)
|
|
8990
9291
|
});
|
|
8991
9292
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
8992
9293
|
const result = await this.#db.client.query(
|
|
@@ -9019,9 +9320,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9019
9320
|
}
|
|
9020
9321
|
async setBufferingObservationFlag(id, isBuffering, lastBufferedAtTokens) {
|
|
9021
9322
|
try {
|
|
9022
|
-
const tableName =
|
|
9323
|
+
const tableName = getTableName4({
|
|
9023
9324
|
indexName: OM_TABLE,
|
|
9024
|
-
schemaName:
|
|
9325
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9025
9326
|
});
|
|
9026
9327
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9027
9328
|
let query;
|
|
@@ -9060,9 +9361,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9060
9361
|
}
|
|
9061
9362
|
async setBufferingReflectionFlag(id, isBuffering) {
|
|
9062
9363
|
try {
|
|
9063
|
-
const tableName =
|
|
9364
|
+
const tableName = getTableName4({
|
|
9064
9365
|
indexName: OM_TABLE,
|
|
9065
|
-
schemaName:
|
|
9366
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9066
9367
|
});
|
|
9067
9368
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9068
9369
|
const result = await this.#db.client.query(
|
|
@@ -9096,9 +9397,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9096
9397
|
async clearObservationalMemory(threadId, resourceId) {
|
|
9097
9398
|
try {
|
|
9098
9399
|
const lookupKey = this.getOMKey(threadId, resourceId);
|
|
9099
|
-
const tableName =
|
|
9400
|
+
const tableName = getTableName4({
|
|
9100
9401
|
indexName: OM_TABLE,
|
|
9101
|
-
schemaName:
|
|
9402
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9102
9403
|
});
|
|
9103
9404
|
await this.#db.client.none(`DELETE FROM ${tableName} WHERE "lookupKey" = $1`, [lookupKey]);
|
|
9104
9405
|
} catch (error$1) {
|
|
@@ -9115,9 +9416,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9115
9416
|
}
|
|
9116
9417
|
async setPendingMessageTokens(id, tokenCount) {
|
|
9117
9418
|
try {
|
|
9118
|
-
const tableName =
|
|
9419
|
+
const tableName = getTableName4({
|
|
9119
9420
|
indexName: OM_TABLE,
|
|
9120
|
-
schemaName:
|
|
9421
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9121
9422
|
});
|
|
9122
9423
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9123
9424
|
const result = await this.#db.client.query(
|
|
@@ -9154,9 +9455,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9154
9455
|
}
|
|
9155
9456
|
async updateObservationalMemoryConfig(input) {
|
|
9156
9457
|
try {
|
|
9157
|
-
const tableName =
|
|
9458
|
+
const tableName = getTableName4({
|
|
9158
9459
|
indexName: OM_TABLE,
|
|
9159
|
-
schemaName:
|
|
9460
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9160
9461
|
});
|
|
9161
9462
|
const selectResult = await this.#db.client.query(`SELECT config FROM ${tableName} WHERE id = $1`, [input.id]);
|
|
9162
9463
|
if (selectResult.rowCount === 0) {
|
|
@@ -9196,9 +9497,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9196
9497
|
// ============================================
|
|
9197
9498
|
async updateBufferedObservations(input) {
|
|
9198
9499
|
try {
|
|
9199
|
-
const tableName =
|
|
9500
|
+
const tableName = getTableName4({
|
|
9200
9501
|
indexName: OM_TABLE,
|
|
9201
|
-
schemaName:
|
|
9502
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9202
9503
|
});
|
|
9203
9504
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9204
9505
|
const newChunk = {
|
|
@@ -9250,9 +9551,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9250
9551
|
}
|
|
9251
9552
|
async swapBufferedToActive(input) {
|
|
9252
9553
|
try {
|
|
9253
|
-
const tableName =
|
|
9554
|
+
const tableName = getTableName4({
|
|
9254
9555
|
indexName: OM_TABLE,
|
|
9255
|
-
schemaName:
|
|
9556
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9256
9557
|
});
|
|
9257
9558
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9258
9559
|
const record = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE id = $1`, [input.id]);
|
|
@@ -9415,9 +9716,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9415
9716
|
}
|
|
9416
9717
|
async updateBufferedReflection(input) {
|
|
9417
9718
|
try {
|
|
9418
|
-
const tableName =
|
|
9719
|
+
const tableName = getTableName4({
|
|
9419
9720
|
indexName: OM_TABLE,
|
|
9420
|
-
schemaName:
|
|
9721
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9421
9722
|
});
|
|
9422
9723
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9423
9724
|
const result = await this.#db.client.query(
|
|
@@ -9469,9 +9770,9 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
9469
9770
|
}
|
|
9470
9771
|
async swapBufferedReflectionToActive(input) {
|
|
9471
9772
|
try {
|
|
9472
|
-
const tableName =
|
|
9773
|
+
const tableName = getTableName4({
|
|
9473
9774
|
indexName: OM_TABLE,
|
|
9474
|
-
schemaName:
|
|
9775
|
+
schemaName: getSchemaName4(this.#schema)
|
|
9475
9776
|
});
|
|
9476
9777
|
const record = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE id = $1`, [
|
|
9477
9778
|
input.currentRecord.id
|
|
@@ -9877,6 +10178,51 @@ var ObservabilityPG = class _ObservabilityPG extends storage.ObservabilityStorag
|
|
|
9877
10178
|
);
|
|
9878
10179
|
}
|
|
9879
10180
|
}
|
|
10181
|
+
async getTraceLight(args) {
|
|
10182
|
+
const { traceId } = args;
|
|
10183
|
+
try {
|
|
10184
|
+
const tableName = getTableName2({
|
|
10185
|
+
indexName: storage.TABLE_SPANS,
|
|
10186
|
+
schemaName: getSchemaName2(this.#schema)
|
|
10187
|
+
});
|
|
10188
|
+
const spans = await this.#db.client.manyOrNone(
|
|
10189
|
+
`SELECT
|
|
10190
|
+
"traceId", "spanId", "parentSpanId", "name",
|
|
10191
|
+
"entityType", "entityId", "entityName",
|
|
10192
|
+
"spanType", "error", "isEvent",
|
|
10193
|
+
"startedAtZ" as "startedAt", "endedAtZ" as "endedAt",
|
|
10194
|
+
"createdAtZ" as "createdAt", "updatedAtZ" as "updatedAt"
|
|
10195
|
+
FROM ${tableName}
|
|
10196
|
+
WHERE "traceId" = $1
|
|
10197
|
+
ORDER BY "startedAtZ" ASC`,
|
|
10198
|
+
[traceId]
|
|
10199
|
+
);
|
|
10200
|
+
if (!spans || spans.length === 0) {
|
|
10201
|
+
return null;
|
|
10202
|
+
}
|
|
10203
|
+
return {
|
|
10204
|
+
traceId,
|
|
10205
|
+
spans: spans.map(
|
|
10206
|
+
(span) => transformFromSqlRow({
|
|
10207
|
+
tableName: storage.TABLE_SPANS,
|
|
10208
|
+
sqlRow: span
|
|
10209
|
+
})
|
|
10210
|
+
)
|
|
10211
|
+
};
|
|
10212
|
+
} catch (error$1) {
|
|
10213
|
+
throw new error.MastraError(
|
|
10214
|
+
{
|
|
10215
|
+
id: storage.createStorageErrorId("PG", "GET_TRACE_LIGHT", "FAILED"),
|
|
10216
|
+
domain: error.ErrorDomain.STORAGE,
|
|
10217
|
+
category: error.ErrorCategory.USER,
|
|
10218
|
+
details: {
|
|
10219
|
+
traceId
|
|
10220
|
+
}
|
|
10221
|
+
},
|
|
10222
|
+
error$1
|
|
10223
|
+
);
|
|
10224
|
+
}
|
|
10225
|
+
}
|
|
9880
10226
|
async updateSpan(args) {
|
|
9881
10227
|
const { traceId, spanId, updates } = args;
|
|
9882
10228
|
try {
|
|
@@ -11500,10 +11846,10 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends storage.ScorerDefin
|
|
|
11500
11846
|
};
|
|
11501
11847
|
}
|
|
11502
11848
|
};
|
|
11503
|
-
function
|
|
11849
|
+
function getSchemaName5(schema) {
|
|
11504
11850
|
return schema ? `"${schema}"` : '"public"';
|
|
11505
11851
|
}
|
|
11506
|
-
function
|
|
11852
|
+
function getTableName5({ indexName, schemaName }) {
|
|
11507
11853
|
const quotedIndexName = `"${indexName}"`;
|
|
11508
11854
|
return schemaName ? `${schemaName}.${quotedIndexName}` : quotedIndexName;
|
|
11509
11855
|
}
|
|
@@ -11617,7 +11963,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11617
11963
|
async getScoreById({ id }) {
|
|
11618
11964
|
try {
|
|
11619
11965
|
const result = await this.#db.client.oneOrNone(
|
|
11620
|
-
`SELECT * FROM ${
|
|
11966
|
+
`SELECT * FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE id = $1`,
|
|
11621
11967
|
[id]
|
|
11622
11968
|
);
|
|
11623
11969
|
return result ? transformScoreRow(result) : null;
|
|
@@ -11657,7 +12003,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11657
12003
|
}
|
|
11658
12004
|
const whereClause = conditions.join(" AND ");
|
|
11659
12005
|
const total = await this.#db.client.oneOrNone(
|
|
11660
|
-
`SELECT COUNT(*) FROM ${
|
|
12006
|
+
`SELECT COUNT(*) FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE ${whereClause}`,
|
|
11661
12007
|
queryParams
|
|
11662
12008
|
);
|
|
11663
12009
|
const { page, perPage: perPageInput } = pagination;
|
|
@@ -11677,7 +12023,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11677
12023
|
const limitValue = perPageInput === false ? Number(total?.count) : perPage;
|
|
11678
12024
|
const end = perPageInput === false ? Number(total?.count) : start + perPage;
|
|
11679
12025
|
const result = await this.#db.client.manyOrNone(
|
|
11680
|
-
`SELECT * FROM ${
|
|
12026
|
+
`SELECT * FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
|
|
11681
12027
|
[...queryParams, limitValue, start]
|
|
11682
12028
|
);
|
|
11683
12029
|
return {
|
|
@@ -11772,7 +12118,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11772
12118
|
}) {
|
|
11773
12119
|
try {
|
|
11774
12120
|
const total = await this.#db.client.oneOrNone(
|
|
11775
|
-
`SELECT COUNT(*) FROM ${
|
|
12121
|
+
`SELECT COUNT(*) FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE "runId" = $1`,
|
|
11776
12122
|
[runId]
|
|
11777
12123
|
);
|
|
11778
12124
|
const { page, perPage: perPageInput } = pagination;
|
|
@@ -11792,7 +12138,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11792
12138
|
const limitValue = perPageInput === false ? Number(total?.count) : perPage;
|
|
11793
12139
|
const end = perPageInput === false ? Number(total?.count) : start + perPage;
|
|
11794
12140
|
const result = await this.#db.client.manyOrNone(
|
|
11795
|
-
`SELECT * FROM ${
|
|
12141
|
+
`SELECT * FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE "runId" = $1 LIMIT $2 OFFSET $3`,
|
|
11796
12142
|
[runId, limitValue, start]
|
|
11797
12143
|
);
|
|
11798
12144
|
return {
|
|
@@ -11822,7 +12168,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11822
12168
|
}) {
|
|
11823
12169
|
try {
|
|
11824
12170
|
const total = await this.#db.client.oneOrNone(
|
|
11825
|
-
`SELECT COUNT(*) FROM ${
|
|
12171
|
+
`SELECT COUNT(*) FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2`,
|
|
11826
12172
|
[entityId, entityType]
|
|
11827
12173
|
);
|
|
11828
12174
|
const { page, perPage: perPageInput } = pagination;
|
|
@@ -11842,7 +12188,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11842
12188
|
const limitValue = perPageInput === false ? Number(total?.count) : perPage;
|
|
11843
12189
|
const end = perPageInput === false ? Number(total?.count) : start + perPage;
|
|
11844
12190
|
const result = await this.#db.client.manyOrNone(
|
|
11845
|
-
`SELECT * FROM ${
|
|
12191
|
+
`SELECT * FROM ${getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2 LIMIT $3 OFFSET $4`,
|
|
11846
12192
|
[entityId, entityType, limitValue, start]
|
|
11847
12193
|
);
|
|
11848
12194
|
return {
|
|
@@ -11871,7 +12217,7 @@ var ScoresPG = class _ScoresPG extends storage.ScoresStorage {
|
|
|
11871
12217
|
pagination
|
|
11872
12218
|
}) {
|
|
11873
12219
|
try {
|
|
11874
|
-
const tableName =
|
|
12220
|
+
const tableName = getTableName5({ indexName: storage.TABLE_SCORERS, schemaName: getSchemaName5(this.#schema) });
|
|
11875
12221
|
const countSQLResult = await this.#db.client.oneOrNone(
|
|
11876
12222
|
`SELECT COUNT(*) as count FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2`,
|
|
11877
12223
|
[traceId, spanId]
|
|
@@ -12574,10 +12920,10 @@ var SkillsPG = class _SkillsPG extends storage.SkillsStorage {
|
|
|
12574
12920
|
};
|
|
12575
12921
|
}
|
|
12576
12922
|
};
|
|
12577
|
-
function
|
|
12923
|
+
function getSchemaName6(schema) {
|
|
12578
12924
|
return schema ? `"${schema}"` : '"public"';
|
|
12579
12925
|
}
|
|
12580
|
-
function
|
|
12926
|
+
function getTableName6({ indexName, schemaName }) {
|
|
12581
12927
|
const quotedIndexName = `"${indexName}"`;
|
|
12582
12928
|
return schemaName ? `${schemaName}.${quotedIndexName}` : quotedIndexName;
|
|
12583
12929
|
}
|
|
@@ -12689,7 +13035,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12689
13035
|
}) {
|
|
12690
13036
|
try {
|
|
12691
13037
|
return await this.#db.client.tx(async (t) => {
|
|
12692
|
-
const tableName =
|
|
13038
|
+
const tableName = getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) });
|
|
12693
13039
|
const existingSnapshotResult = await t.oneOrNone(
|
|
12694
13040
|
`SELECT snapshot FROM ${tableName} WHERE workflow_name = $1 AND run_id = $2 FOR UPDATE`,
|
|
12695
13041
|
[workflowName, runId]
|
|
@@ -12750,7 +13096,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12750
13096
|
}) {
|
|
12751
13097
|
try {
|
|
12752
13098
|
return await this.#db.client.tx(async (t) => {
|
|
12753
|
-
const tableName =
|
|
13099
|
+
const tableName = getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) });
|
|
12754
13100
|
const existingSnapshotResult = await t.oneOrNone(
|
|
12755
13101
|
`SELECT snapshot FROM ${tableName} WHERE workflow_name = $1 AND run_id = $2 FOR UPDATE`,
|
|
12756
13102
|
[workflowName, runId]
|
|
@@ -12800,7 +13146,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12800
13146
|
const updatedAtValue = updatedAt ? updatedAt : now;
|
|
12801
13147
|
const sanitizedSnapshot = sanitizeJsonForPg(JSON.stringify(snapshot));
|
|
12802
13148
|
await this.#db.client.none(
|
|
12803
|
-
`INSERT INTO ${
|
|
13149
|
+
`INSERT INTO ${getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) })} (workflow_name, run_id, "resourceId", snapshot, "createdAt", "updatedAt")
|
|
12804
13150
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
12805
13151
|
ON CONFLICT (workflow_name, run_id) DO UPDATE
|
|
12806
13152
|
SET "resourceId" = $3, snapshot = $4, "updatedAt" = $6`,
|
|
@@ -12858,7 +13204,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12858
13204
|
}
|
|
12859
13205
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
12860
13206
|
const query = `
|
|
12861
|
-
SELECT * FROM ${
|
|
13207
|
+
SELECT * FROM ${getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) })}
|
|
12862
13208
|
${whereClause}
|
|
12863
13209
|
ORDER BY "createdAt" DESC LIMIT 1
|
|
12864
13210
|
`;
|
|
@@ -12886,7 +13232,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12886
13232
|
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
12887
13233
|
try {
|
|
12888
13234
|
await this.#db.client.none(
|
|
12889
|
-
`DELETE FROM ${
|
|
13235
|
+
`DELETE FROM ${getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) })} WHERE run_id = $1 AND workflow_name = $2`,
|
|
12890
13236
|
[runId, workflowName]
|
|
12891
13237
|
);
|
|
12892
13238
|
} catch (error$1) {
|
|
@@ -12954,7 +13300,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12954
13300
|
const usePagination = typeof perPage === "number" && typeof page === "number";
|
|
12955
13301
|
if (usePagination) {
|
|
12956
13302
|
const countResult = await this.#db.client.one(
|
|
12957
|
-
`SELECT COUNT(*) as count FROM ${
|
|
13303
|
+
`SELECT COUNT(*) as count FROM ${getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) })} ${whereClause}`,
|
|
12958
13304
|
values
|
|
12959
13305
|
);
|
|
12960
13306
|
total = Number(countResult.count);
|
|
@@ -12962,7 +13308,7 @@ var WorkflowsPG = class _WorkflowsPG extends storage.WorkflowsStorage {
|
|
|
12962
13308
|
const normalizedPerPage = usePagination ? storage.normalizePerPage(perPage, Number.MAX_SAFE_INTEGER) : 0;
|
|
12963
13309
|
const offset = usePagination ? page * normalizedPerPage : void 0;
|
|
12964
13310
|
const query = `
|
|
12965
|
-
SELECT * FROM ${
|
|
13311
|
+
SELECT * FROM ${getTableName6({ indexName: storage.TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName6(this.#schema) })}
|
|
12966
13312
|
${whereClause}
|
|
12967
13313
|
ORDER BY "createdAt" DESC
|
|
12968
13314
|
${usePagination ? ` LIMIT $${paramIndex} OFFSET $${paramIndex + 1}` : ""}
|
|
@@ -13685,7 +14031,8 @@ var ALL_DOMAINS = [
|
|
|
13685
14031
|
AgentsPG,
|
|
13686
14032
|
WorkflowsPG,
|
|
13687
14033
|
DatasetsPG,
|
|
13688
|
-
ExperimentsPG
|
|
14034
|
+
ExperimentsPG,
|
|
14035
|
+
BackgroundTasksPG
|
|
13689
14036
|
];
|
|
13690
14037
|
function exportSchemas(schemaName) {
|
|
13691
14038
|
const statements = [];
|
|
@@ -13739,7 +14086,8 @@ var PostgresStore = class extends storage.MastraCompositeStore {
|
|
|
13739
14086
|
skills: new SkillsPG(domainConfig),
|
|
13740
14087
|
blobs: new BlobsPG(domainConfig),
|
|
13741
14088
|
datasets: new DatasetsPG(domainConfig),
|
|
13742
|
-
experiments: new ExperimentsPG(domainConfig)
|
|
14089
|
+
experiments: new ExperimentsPG(domainConfig),
|
|
14090
|
+
backgroundTasks: new BackgroundTasksPG(domainConfig)
|
|
13743
14091
|
};
|
|
13744
14092
|
} catch (e) {
|
|
13745
14093
|
throw new error.MastraError(
|
|
@@ -13929,6 +14277,7 @@ Example Complex Query:
|
|
|
13929
14277
|
}`;
|
|
13930
14278
|
|
|
13931
14279
|
exports.AgentsPG = AgentsPG;
|
|
14280
|
+
exports.BackgroundTasksPG = BackgroundTasksPG;
|
|
13932
14281
|
exports.BlobsPG = BlobsPG;
|
|
13933
14282
|
exports.DatasetsPG = DatasetsPG;
|
|
13934
14283
|
exports.ExperimentsPG = ExperimentsPG;
|