@mastra/pg 1.24.0 → 1.25.0-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/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/reference-memory-memory-class.md +2 -1
- package/dist/index.cjs +142 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +142 -107
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/datasets/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +15 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/v-next/helpers.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/docs/SKILL.md
CHANGED
|
@@ -39,7 +39,7 @@ export const agent = new Agent({
|
|
|
39
39
|
|
|
40
40
|
**options** (`MemoryConfig`): Memory configuration options.
|
|
41
41
|
|
|
42
|
-
**options.lastMessages** (`number | false`): Number of most recent messages to include in context. Set to false to disable the message history feature entirely (messages are not loaded into context or saved). Use Number.MAX\_SAFE\_INTEGER to retrieve all messages with no limit. To load messages without saving new ones, use the readOnly option.
|
|
42
|
+
**options.lastMessages** (`number | false`): Number of most recent messages to include in context. Set to false to disable the message history feature entirely (messages are not loaded into context or saved). Use Number.MAX\_SAFE\_INTEGER to retrieve all messages with no limit. To load messages without saving new ones, use the readOnly option. The window slides forward on every request, so once a thread exceeds the limit, each turn invalidates the provider prompt cache. For long-running conversations, use Observational Memory instead.
|
|
43
43
|
|
|
44
44
|
**options.readOnly** (`boolean`): When true, prevents memory from saving new messages and provides working memory as read-only context (without the updateWorkingMemory tool). Useful for read-only operations like previews, internal routing agents, or sub agents that should reference but not modify memory.
|
|
45
45
|
|
|
@@ -147,5 +147,6 @@ export const agent = new Agent({
|
|
|
147
147
|
- [listThreads](https://mastra.ai/reference/memory/listThreads)
|
|
148
148
|
- [deleteMessages](https://mastra.ai/reference/memory/deleteMessages)
|
|
149
149
|
- [cloneThread](https://mastra.ai/reference/memory/cloneThread)
|
|
150
|
+
- [copyThread](https://mastra.ai/reference/memory/copyThread)
|
|
150
151
|
- [settled](https://mastra.ai/reference/memory/settled)
|
|
151
152
|
- [Clone Utility Methods](https://mastra.ai/reference/memory/clone-utilities)
|
package/dist/index.cjs
CHANGED
|
@@ -6298,23 +6298,6 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6298
6298
|
}
|
|
6299
6299
|
async _doUpdateItem(args) {
|
|
6300
6300
|
try {
|
|
6301
|
-
const existing = await this.#getItemById(this.#db.client, { id: args.id });
|
|
6302
|
-
if (!existing) throw new _mastra_core_error.MastraError({
|
|
6303
|
-
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_ITEM", "NOT_FOUND"),
|
|
6304
|
-
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6305
|
-
category: _mastra_core_error.ErrorCategory.USER,
|
|
6306
|
-
details: { itemId: args.id }
|
|
6307
|
-
});
|
|
6308
|
-
if (existing.datasetId !== args.datasetId) throw new _mastra_core_error.MastraError({
|
|
6309
|
-
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_ITEM", "DATASET_MISMATCH"),
|
|
6310
|
-
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6311
|
-
category: _mastra_core_error.ErrorCategory.USER,
|
|
6312
|
-
details: {
|
|
6313
|
-
itemId: args.id,
|
|
6314
|
-
expectedDatasetId: args.datasetId,
|
|
6315
|
-
actualDatasetId: existing.datasetId
|
|
6316
|
-
}
|
|
6317
|
-
});
|
|
6318
6301
|
const datasetsTable = getTableName$5({
|
|
6319
6302
|
indexName: _mastra_core_storage.TABLE_DATASETS,
|
|
6320
6303
|
schemaName: getSchemaName$5(this.#schema)
|
|
@@ -6330,23 +6313,49 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6330
6313
|
const versionId = crypto.randomUUID();
|
|
6331
6314
|
const now = /* @__PURE__ */ new Date();
|
|
6332
6315
|
const nowIso = now.toISOString();
|
|
6333
|
-
|
|
6334
|
-
const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
|
|
6335
|
-
const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
|
|
6336
|
-
const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
|
|
6337
|
-
const mergedUnmockedToolPolicy = args.unmockedToolPolicy !== void 0 ? args.unmockedToolPolicy : existing.unmockedToolPolicy;
|
|
6338
|
-
const mergedScorerIds = args.scorerIds !== void 0 ? args.scorerIds ?? void 0 : existing.scorerIds;
|
|
6339
|
-
const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
|
|
6340
|
-
const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
|
|
6341
|
-
const mergedSource = args.source !== void 0 ? args.source : existing.source;
|
|
6342
|
-
let newVersion;
|
|
6343
|
-
let parentOrganizationId = null;
|
|
6344
|
-
let parentProjectId = null;
|
|
6316
|
+
let updated;
|
|
6345
6317
|
await this.#db.client.tx(async (t) => {
|
|
6318
|
+
await t.oneOrNone(`SELECT "id" FROM ${datasetsTable} WHERE "id" = $1 FOR UPDATE`, [args.datasetId]);
|
|
6319
|
+
const existing = await this.#getItemById(t, { id: args.id });
|
|
6320
|
+
if (!existing) throw new _mastra_core_error.MastraError({
|
|
6321
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_ITEM", "NOT_FOUND"),
|
|
6322
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6323
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
6324
|
+
details: { itemId: args.id }
|
|
6325
|
+
});
|
|
6326
|
+
if (existing.datasetId !== args.datasetId) throw new _mastra_core_error.MastraError({
|
|
6327
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_ITEM", "DATASET_MISMATCH"),
|
|
6328
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6329
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
6330
|
+
details: {
|
|
6331
|
+
itemId: args.id,
|
|
6332
|
+
expectedDatasetId: args.datasetId,
|
|
6333
|
+
actualDatasetId: existing.datasetId
|
|
6334
|
+
}
|
|
6335
|
+
});
|
|
6336
|
+
if (existing.metadata?.__purged === true) throw new _mastra_core_error.MastraError({
|
|
6337
|
+
id: "DATASET_ITEM_PURGED",
|
|
6338
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6339
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
6340
|
+
details: {
|
|
6341
|
+
datasetId: args.datasetId,
|
|
6342
|
+
itemId: args.id
|
|
6343
|
+
},
|
|
6344
|
+
text: `Purged dataset item cannot be updated: ${args.id}`
|
|
6345
|
+
});
|
|
6346
|
+
const mergedInput = args.input !== void 0 ? args.input : existing.input;
|
|
6347
|
+
const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
|
|
6348
|
+
const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
|
|
6349
|
+
const mergedToolMocks = args.toolMocks !== void 0 ? args.toolMocks : existing.toolMocks;
|
|
6350
|
+
const mergedUnmockedToolPolicy = args.unmockedToolPolicy !== void 0 ? args.unmockedToolPolicy : existing.unmockedToolPolicy;
|
|
6351
|
+
const mergedScorerIds = args.scorerIds !== void 0 ? args.scorerIds ?? void 0 : existing.scorerIds;
|
|
6352
|
+
const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
|
|
6353
|
+
const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
|
|
6354
|
+
const mergedSource = args.source !== void 0 ? args.source : existing.source;
|
|
6346
6355
|
const row = await t.one(`UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`, [args.datasetId]);
|
|
6347
|
-
newVersion = row.version;
|
|
6348
|
-
parentOrganizationId = row.organizationId ?? null;
|
|
6349
|
-
parentProjectId = row.projectId ?? null;
|
|
6356
|
+
const newVersion = row.version;
|
|
6357
|
+
const parentOrganizationId = row.organizationId ?? null;
|
|
6358
|
+
const parentProjectId = row.projectId ?? null;
|
|
6350
6359
|
await t.none(`UPDATE ${itemsTable} SET "validTo" = $1 WHERE "id" = $2 AND "validTo" IS NULL AND "isDeleted" = false`, [newVersion, args.id]);
|
|
6351
6360
|
await t.none(`INSERT INTO ${itemsTable} ("id","datasetId","datasetVersion","externalId","organizationId","projectId","validTo","isDeleted","input","groundTruth","expectedTrajectory","toolMocks","unmockedToolPolicy","scorerIds","requestContext","metadata","source","createdAt","createdAtZ","updatedAt","updatedAtZ") VALUES ($1,$2,$3,$4,$5,$6,NULL,false,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19)`, [
|
|
6352
6361
|
args.id,
|
|
@@ -6376,23 +6385,24 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6376
6385
|
nowIso,
|
|
6377
6386
|
nowIso
|
|
6378
6387
|
]);
|
|
6388
|
+
updated = {
|
|
6389
|
+
...existing,
|
|
6390
|
+
datasetVersion: newVersion,
|
|
6391
|
+
organizationId: parentOrganizationId,
|
|
6392
|
+
projectId: parentProjectId,
|
|
6393
|
+
input: mergedInput,
|
|
6394
|
+
groundTruth: mergedGroundTruth,
|
|
6395
|
+
expectedTrajectory: mergedExpectedTrajectory,
|
|
6396
|
+
toolMocks: mergedToolMocks,
|
|
6397
|
+
unmockedToolPolicy: mergedUnmockedToolPolicy,
|
|
6398
|
+
scorerIds: mergedScorerIds,
|
|
6399
|
+
requestContext: mergedRequestContext,
|
|
6400
|
+
metadata: mergedMetadata,
|
|
6401
|
+
source: mergedSource,
|
|
6402
|
+
updatedAt: now
|
|
6403
|
+
};
|
|
6379
6404
|
});
|
|
6380
|
-
return
|
|
6381
|
-
...existing,
|
|
6382
|
-
datasetVersion: newVersion,
|
|
6383
|
-
organizationId: parentOrganizationId,
|
|
6384
|
-
projectId: parentProjectId,
|
|
6385
|
-
input: mergedInput,
|
|
6386
|
-
groundTruth: mergedGroundTruth,
|
|
6387
|
-
expectedTrajectory: mergedExpectedTrajectory,
|
|
6388
|
-
toolMocks: mergedToolMocks,
|
|
6389
|
-
unmockedToolPolicy: mergedUnmockedToolPolicy,
|
|
6390
|
-
scorerIds: mergedScorerIds,
|
|
6391
|
-
requestContext: mergedRequestContext,
|
|
6392
|
-
metadata: mergedMetadata,
|
|
6393
|
-
source: mergedSource,
|
|
6394
|
-
updatedAt: now
|
|
6395
|
-
};
|
|
6405
|
+
return updated;
|
|
6396
6406
|
} catch (error) {
|
|
6397
6407
|
if (error instanceof _mastra_core_error.MastraError) throw error;
|
|
6398
6408
|
throw new _mastra_core_error.MastraError({
|
|
@@ -6404,18 +6414,6 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6404
6414
|
}
|
|
6405
6415
|
async _doDeleteItem({ id, datasetId }) {
|
|
6406
6416
|
try {
|
|
6407
|
-
const existing = await this.#getItemById(this.#db.client, { id });
|
|
6408
|
-
if (!existing) return;
|
|
6409
|
-
if (existing.datasetId !== datasetId) throw new _mastra_core_error.MastraError({
|
|
6410
|
-
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "DELETE_ITEM", "DATASET_MISMATCH"),
|
|
6411
|
-
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6412
|
-
category: _mastra_core_error.ErrorCategory.USER,
|
|
6413
|
-
details: {
|
|
6414
|
-
itemId: id,
|
|
6415
|
-
expectedDatasetId: datasetId,
|
|
6416
|
-
actualDatasetId: existing.datasetId
|
|
6417
|
-
}
|
|
6418
|
-
});
|
|
6419
6417
|
const datasetsTable = getTableName$5({
|
|
6420
6418
|
indexName: _mastra_core_storage.TABLE_DATASETS,
|
|
6421
6419
|
schemaName: getSchemaName$5(this.#schema)
|
|
@@ -6431,6 +6429,19 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6431
6429
|
const versionId = crypto.randomUUID();
|
|
6432
6430
|
const nowIso = (/* @__PURE__ */ new Date()).toISOString();
|
|
6433
6431
|
await this.#db.client.tx(async (t) => {
|
|
6432
|
+
await t.oneOrNone(`SELECT "id" FROM ${datasetsTable} WHERE "id" = $1 FOR UPDATE`, [datasetId]);
|
|
6433
|
+
const existing = await this.#getItemById(t, { id });
|
|
6434
|
+
if (!existing) return;
|
|
6435
|
+
if (existing.datasetId !== datasetId) throw new _mastra_core_error.MastraError({
|
|
6436
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "DELETE_ITEM", "DATASET_MISMATCH"),
|
|
6437
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6438
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
6439
|
+
details: {
|
|
6440
|
+
itemId: id,
|
|
6441
|
+
expectedDatasetId: datasetId,
|
|
6442
|
+
actualDatasetId: existing.datasetId
|
|
6443
|
+
}
|
|
6444
|
+
});
|
|
6434
6445
|
const row = await t.one(`UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version", "organizationId", "projectId"`, [datasetId]);
|
|
6435
6446
|
const newVersion = row.version;
|
|
6436
6447
|
const parentOrganizationId = row.organizationId ?? null;
|
|
@@ -6617,19 +6628,6 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6617
6628
|
}
|
|
6618
6629
|
async _doBatchDeleteItems(input) {
|
|
6619
6630
|
try {
|
|
6620
|
-
const dataset = await this.#getDatasetById(this.#db.client, { id: input.datasetId });
|
|
6621
|
-
if (!dataset) throw new _mastra_core_error.MastraError({
|
|
6622
|
-
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "BULK_DELETE_ITEMS", "DATASET_NOT_FOUND"),
|
|
6623
|
-
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6624
|
-
category: _mastra_core_error.ErrorCategory.USER,
|
|
6625
|
-
details: { datasetId: input.datasetId }
|
|
6626
|
-
});
|
|
6627
|
-
const currentItems = [];
|
|
6628
|
-
for (const itemId of input.itemIds) {
|
|
6629
|
-
const item = await this.#getItemById(this.#db.client, { id: itemId });
|
|
6630
|
-
if (item && item.datasetId === input.datasetId) currentItems.push(item);
|
|
6631
|
-
}
|
|
6632
|
-
if (currentItems.length === 0) return;
|
|
6633
6631
|
const datasetsTable = getTableName$5({
|
|
6634
6632
|
indexName: _mastra_core_storage.TABLE_DATASETS,
|
|
6635
6633
|
schemaName: getSchemaName$5(this.#schema)
|
|
@@ -6644,9 +6642,19 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
|
|
|
6644
6642
|
});
|
|
6645
6643
|
const nowIso = (/* @__PURE__ */ new Date()).toISOString();
|
|
6646
6644
|
const versionId = crypto.randomUUID();
|
|
6647
|
-
const parentOrganizationId = dataset.organizationId ?? null;
|
|
6648
|
-
const parentProjectId = dataset.projectId ?? null;
|
|
6649
6645
|
await this.#db.client.tx(async (t) => {
|
|
6646
|
+
await t.oneOrNone(`SELECT "id" FROM ${datasetsTable} WHERE "id" = $1 FOR UPDATE`, [input.datasetId]);
|
|
6647
|
+
const dataset = await this.#getDatasetById(t, { id: input.datasetId });
|
|
6648
|
+
if (!dataset) throw new _mastra_core_error.MastraError({
|
|
6649
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "BULK_DELETE_ITEMS", "DATASET_NOT_FOUND"),
|
|
6650
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
6651
|
+
category: _mastra_core_error.ErrorCategory.USER,
|
|
6652
|
+
details: { datasetId: input.datasetId }
|
|
6653
|
+
});
|
|
6654
|
+
const currentItems = (await t.manyOrNone(`SELECT * FROM ${itemsTable} WHERE "id" = ANY($1::text[]) AND "datasetId" = $2 AND "validTo" IS NULL AND "isDeleted" = false`, [input.itemIds, input.datasetId])).map((row) => this.transformItemRow(row));
|
|
6655
|
+
if (currentItems.length === 0) return;
|
|
6656
|
+
const parentOrganizationId = dataset.organizationId ?? null;
|
|
6657
|
+
const parentProjectId = dataset.projectId ?? null;
|
|
6650
6658
|
const newVersion = (await t.one(`UPDATE ${datasetsTable} SET "version" = "version" + 1 WHERE "id" = $1 RETURNING "version"`, [input.datasetId])).version;
|
|
6651
6659
|
for (const item of currentItems) {
|
|
6652
6660
|
await t.none(`UPDATE ${itemsTable} SET "validTo" = $1 WHERE "id" = $2 AND "validTo" IS NULL AND "isDeleted" = false`, [newVersion, item.id]);
|
|
@@ -10684,6 +10692,57 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
|
|
|
10684
10692
|
}, error);
|
|
10685
10693
|
}
|
|
10686
10694
|
}
|
|
10695
|
+
/**
|
|
10696
|
+
* Atomically reassign a thread and all of its messages to a different resource.
|
|
10697
|
+
*
|
|
10698
|
+
* Runs inside a single transaction and takes a `SELECT ... FOR UPDATE` row lock on the
|
|
10699
|
+
* thread, so overlapping transfers of the same thread serialize and can never interleave
|
|
10700
|
+
* the thread update with the message update. Either both the thread and every message move
|
|
10701
|
+
* to the new resource, or neither does — there is no split-ownership window. The thread's
|
|
10702
|
+
* `createdAt` is preserved. Callers are responsible for authorizing the reassignment.
|
|
10703
|
+
*/
|
|
10704
|
+
async updateThreadResourceId({ threadId, resourceId }) {
|
|
10705
|
+
const threadsTable = getTableName$3({
|
|
10706
|
+
indexName: _mastra_core_storage.TABLE_THREADS,
|
|
10707
|
+
schemaName: getSchemaName$3(this.#schema)
|
|
10708
|
+
});
|
|
10709
|
+
const messagesTable = getTableName$3({
|
|
10710
|
+
indexName: _mastra_core_storage.TABLE_MESSAGES,
|
|
10711
|
+
schemaName: getSchemaName$3(this.#schema)
|
|
10712
|
+
});
|
|
10713
|
+
try {
|
|
10714
|
+
return await this.#db.client.tx(async (t) => {
|
|
10715
|
+
const thread = await t.oneOrNone(`SELECT * FROM ${threadsTable} WHERE id = $1 FOR UPDATE`, [threadId]);
|
|
10716
|
+
if (!thread) throw new Error(`Thread "${threadId}" not found`);
|
|
10717
|
+
const normalized = {
|
|
10718
|
+
id: thread.id,
|
|
10719
|
+
resourceId: thread.resourceId,
|
|
10720
|
+
title: thread.title,
|
|
10721
|
+
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
10722
|
+
createdAt: thread.createdAtZ || thread.createdAt,
|
|
10723
|
+
updatedAt: thread.updatedAtZ || thread.updatedAt
|
|
10724
|
+
};
|
|
10725
|
+
if (thread.resourceId === resourceId) return normalized;
|
|
10726
|
+
await t.none(`UPDATE ${threadsTable} SET "resourceId" = $1, "updatedAt" = NOW(), "updatedAtZ" = NOW() WHERE id = $2`, [resourceId, threadId]);
|
|
10727
|
+
await t.none(`UPDATE ${messagesTable} SET "resourceId" = $1 WHERE thread_id = $2`, [resourceId, threadId]);
|
|
10728
|
+
return {
|
|
10729
|
+
...normalized,
|
|
10730
|
+
resourceId,
|
|
10731
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
10732
|
+
};
|
|
10733
|
+
});
|
|
10734
|
+
} catch (error) {
|
|
10735
|
+
throw new _mastra_core_error.MastraError({
|
|
10736
|
+
id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_THREAD_RESOURCE_ID", "FAILED"),
|
|
10737
|
+
domain: _mastra_core_error.ErrorDomain.STORAGE,
|
|
10738
|
+
category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
|
|
10739
|
+
details: {
|
|
10740
|
+
threadId,
|
|
10741
|
+
resourceId
|
|
10742
|
+
}
|
|
10743
|
+
}, error);
|
|
10744
|
+
}
|
|
10745
|
+
}
|
|
10687
10746
|
async listThreads(args) {
|
|
10688
10747
|
const { page = 0, perPage: perPageInput, orderBy, filter } = args;
|
|
10689
10748
|
try {
|
|
@@ -11688,7 +11747,7 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
|
|
|
11688
11747
|
await this.#db.client.none(`UPDATE ${tableName} SET ${updates.join(", ")} WHERE id = $${paramIndex}`, values);
|
|
11689
11748
|
return updatedResource;
|
|
11690
11749
|
}
|
|
11691
|
-
async
|
|
11750
|
+
async copyThread(args) {
|
|
11692
11751
|
const { sourceThreadId, newThreadId: providedThreadId, resourceId, title, metadata, options } = args;
|
|
11693
11752
|
const sourceThread = await this.#getThreadById(this.#db.client, { threadId: sourceThreadId });
|
|
11694
11753
|
if (!sourceThread) throw new _mastra_core_error.MastraError({
|
|
@@ -11716,7 +11775,7 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
|
|
|
11716
11775
|
});
|
|
11717
11776
|
try {
|
|
11718
11777
|
return await this.#db.client.tx(async (t) => {
|
|
11719
|
-
let messageQuery = `SELECT id,
|
|
11778
|
+
let messageQuery = `SELECT id, "createdAt"
|
|
11720
11779
|
FROM ${messageTableName} WHERE thread_id = $1`;
|
|
11721
11780
|
const messageParams = [sourceThreadId];
|
|
11722
11781
|
let paramIndex = 2;
|
|
@@ -11777,42 +11836,23 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
|
|
|
11777
11836
|
nowStr,
|
|
11778
11837
|
nowStr
|
|
11779
11838
|
]);
|
|
11780
|
-
const clonedMessages = [];
|
|
11781
11839
|
const messageIdMap = {};
|
|
11782
11840
|
const targetResourceId = resourceId || sourceThread.resourceId;
|
|
11783
11841
|
for (const sourceMsg of sourceMessages) {
|
|
11784
11842
|
const newMessageId = crypto.randomUUID();
|
|
11785
11843
|
messageIdMap[sourceMsg.id] = newMessageId;
|
|
11786
|
-
const
|
|
11787
|
-
|
|
11788
|
-
|
|
11789
|
-
parsedContent = JSON.parse(normalizedMsg.content);
|
|
11790
|
-
} catch {}
|
|
11791
|
-
const createdAt = toUtcISOString(new Date(normalizedMsg.createdAt));
|
|
11792
|
-
await t.none(`INSERT INTO ${messageTableName} (id, thread_id, content, "createdAt", "createdAtZ", role, type, "resourceId")
|
|
11793
|
-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`, [
|
|
11844
|
+
const insertResult = await t.query(`INSERT INTO ${messageTableName} (id, thread_id, content, "createdAt", "createdAtZ", role, type, "resourceId")
|
|
11845
|
+
SELECT $1, $2, content, "createdAt", "createdAtZ", role, type, $3
|
|
11846
|
+
FROM ${messageTableName} WHERE id = $4`, [
|
|
11794
11847
|
newMessageId,
|
|
11795
11848
|
newThreadId,
|
|
11796
|
-
|
|
11797
|
-
|
|
11798
|
-
createdAt,
|
|
11799
|
-
normalizedMsg.role,
|
|
11800
|
-
normalizedMsg.type || "v2",
|
|
11801
|
-
targetResourceId
|
|
11849
|
+
targetResourceId,
|
|
11850
|
+
sourceMsg.id
|
|
11802
11851
|
]);
|
|
11803
|
-
|
|
11804
|
-
id: newMessageId,
|
|
11805
|
-
threadId: newThreadId,
|
|
11806
|
-
content: parsedContent,
|
|
11807
|
-
role: normalizedMsg.role,
|
|
11808
|
-
type: normalizedMsg.type,
|
|
11809
|
-
createdAt: new Date(normalizedMsg.createdAt),
|
|
11810
|
-
resourceId: targetResourceId
|
|
11811
|
-
});
|
|
11852
|
+
if (insertResult.rowCount !== 1) throw new Error(`Failed to copy message ${sourceMsg.id}: expected 1 row copied but got ${insertResult.rowCount}`);
|
|
11812
11853
|
}
|
|
11813
11854
|
return {
|
|
11814
11855
|
thread: newThread,
|
|
11815
|
-
clonedMessages,
|
|
11816
11856
|
messageIdMap
|
|
11817
11857
|
};
|
|
11818
11858
|
});
|
|
@@ -15300,11 +15340,6 @@ function jsonField(value) {
|
|
|
15300
15340
|
}
|
|
15301
15341
|
function parsedJson(value) {
|
|
15302
15342
|
if (value == null) return void 0;
|
|
15303
|
-
if (typeof value === "string") try {
|
|
15304
|
-
return JSON.parse(value);
|
|
15305
|
-
} catch {
|
|
15306
|
-
return;
|
|
15307
|
-
}
|
|
15308
15343
|
return value;
|
|
15309
15344
|
}
|
|
15310
15345
|
function rowToCommonContext(row) {
|