@mastra/pg 1.0.0-beta.11 → 1.0.0-beta.12
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 +80 -0
- package/dist/docs/README.md +36 -0
- package/dist/docs/SKILL.md +37 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/memory/01-storage.md +181 -0
- package/dist/docs/memory/02-working-memory.md +386 -0
- package/dist/docs/memory/03-semantic-recall.md +235 -0
- package/dist/docs/memory/04-reference.md +135 -0
- package/dist/docs/processors/01-reference.md +295 -0
- package/dist/docs/rag/01-overview.md +74 -0
- package/dist/docs/rag/02-vector-databases.md +638 -0
- package/dist/docs/rag/03-retrieval.md +549 -0
- package/dist/docs/rag/04-reference.md +351 -0
- package/dist/docs/storage/01-reference.md +667 -0
- package/dist/docs/tools/01-reference.md +440 -0
- package/dist/docs/vectors/01-reference.md +307 -0
- package/dist/index.cjs +159 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +159 -7
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +2 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +9 -8
package/dist/index.js
CHANGED
|
@@ -3229,13 +3229,19 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
|
|
|
3229
3229
|
};
|
|
3230
3230
|
}
|
|
3231
3231
|
const limitValue = perPageInput === false ? total : perPage;
|
|
3232
|
-
const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "updatedAt" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`;
|
|
3233
|
-
const rows = await this.#db.client.manyOrNone(
|
|
3232
|
+
const dataQuery = `SELECT id, "resourceId", title, metadata, "createdAt", "createdAtZ", "updatedAt", "updatedAtZ" ${baseQuery} ORDER BY "${field}" ${direction} LIMIT $2 OFFSET $3`;
|
|
3233
|
+
const rows = await this.#db.client.manyOrNone(
|
|
3234
|
+
dataQuery,
|
|
3235
|
+
[...queryParams, limitValue, offset]
|
|
3236
|
+
);
|
|
3234
3237
|
const threads = (rows || []).map((thread) => ({
|
|
3235
|
-
|
|
3238
|
+
id: thread.id,
|
|
3239
|
+
resourceId: thread.resourceId,
|
|
3240
|
+
title: thread.title,
|
|
3236
3241
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata,
|
|
3237
|
-
|
|
3238
|
-
|
|
3242
|
+
// Use timezone-aware columns (*Z) for correct UTC timestamps, with fallback for legacy data
|
|
3243
|
+
createdAt: thread.createdAtZ || thread.createdAt,
|
|
3244
|
+
updatedAt: thread.updatedAtZ || thread.updatedAt
|
|
3239
3245
|
}));
|
|
3240
3246
|
return {
|
|
3241
3247
|
threads,
|
|
@@ -3563,11 +3569,13 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
|
|
|
3563
3569
|
queryParams.push(resourceId);
|
|
3564
3570
|
}
|
|
3565
3571
|
if (filter?.dateRange?.start) {
|
|
3566
|
-
|
|
3572
|
+
const startOp = filter.dateRange.startExclusive ? ">" : ">=";
|
|
3573
|
+
conditions.push(`"createdAt" ${startOp} $${paramIndex++}`);
|
|
3567
3574
|
queryParams.push(filter.dateRange.start);
|
|
3568
3575
|
}
|
|
3569
3576
|
if (filter?.dateRange?.end) {
|
|
3570
|
-
|
|
3577
|
+
const endOp = filter.dateRange.endExclusive ? "<" : "<=";
|
|
3578
|
+
conditions.push(`"createdAt" ${endOp} $${paramIndex++}`);
|
|
3571
3579
|
queryParams.push(filter.dateRange.end);
|
|
3572
3580
|
}
|
|
3573
3581
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
@@ -3952,6 +3960,150 @@ var MemoryPG = class _MemoryPG extends MemoryStorage {
|
|
|
3952
3960
|
await this.#db.client.none(`UPDATE ${tableName} SET ${updates.join(", ")} WHERE id = $${paramIndex}`, values);
|
|
3953
3961
|
return updatedResource;
|
|
3954
3962
|
}
|
|
3963
|
+
async cloneThread(args) {
|
|
3964
|
+
const { sourceThreadId, newThreadId: providedThreadId, resourceId, title, metadata, options } = args;
|
|
3965
|
+
const sourceThread = await this.getThreadById({ threadId: sourceThreadId });
|
|
3966
|
+
if (!sourceThread) {
|
|
3967
|
+
throw new MastraError({
|
|
3968
|
+
id: createStorageErrorId("PG", "CLONE_THREAD", "SOURCE_NOT_FOUND"),
|
|
3969
|
+
domain: ErrorDomain.STORAGE,
|
|
3970
|
+
category: ErrorCategory.USER,
|
|
3971
|
+
text: `Source thread with id ${sourceThreadId} not found`,
|
|
3972
|
+
details: { sourceThreadId }
|
|
3973
|
+
});
|
|
3974
|
+
}
|
|
3975
|
+
const newThreadId = providedThreadId || crypto.randomUUID();
|
|
3976
|
+
const existingThread = await this.getThreadById({ threadId: newThreadId });
|
|
3977
|
+
if (existingThread) {
|
|
3978
|
+
throw new MastraError({
|
|
3979
|
+
id: createStorageErrorId("PG", "CLONE_THREAD", "THREAD_EXISTS"),
|
|
3980
|
+
domain: ErrorDomain.STORAGE,
|
|
3981
|
+
category: ErrorCategory.USER,
|
|
3982
|
+
text: `Thread with id ${newThreadId} already exists`,
|
|
3983
|
+
details: { newThreadId }
|
|
3984
|
+
});
|
|
3985
|
+
}
|
|
3986
|
+
const threadTableName = getTableName3({ indexName: TABLE_THREADS, schemaName: getSchemaName3(this.#schema) });
|
|
3987
|
+
const messageTableName = getTableName3({ indexName: TABLE_MESSAGES, schemaName: getSchemaName3(this.#schema) });
|
|
3988
|
+
try {
|
|
3989
|
+
return await this.#db.client.tx(async (t) => {
|
|
3990
|
+
let messageQuery = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"
|
|
3991
|
+
FROM ${messageTableName} WHERE thread_id = $1`;
|
|
3992
|
+
const messageParams = [sourceThreadId];
|
|
3993
|
+
let paramIndex = 2;
|
|
3994
|
+
if (options?.messageFilter?.startDate) {
|
|
3995
|
+
messageQuery += ` AND "createdAt" >= $${paramIndex++}`;
|
|
3996
|
+
messageParams.push(options.messageFilter.startDate);
|
|
3997
|
+
}
|
|
3998
|
+
if (options?.messageFilter?.endDate) {
|
|
3999
|
+
messageQuery += ` AND "createdAt" <= $${paramIndex++}`;
|
|
4000
|
+
messageParams.push(options.messageFilter.endDate);
|
|
4001
|
+
}
|
|
4002
|
+
if (options?.messageFilter?.messageIds && options.messageFilter.messageIds.length > 0) {
|
|
4003
|
+
messageQuery += ` AND id IN (${options.messageFilter.messageIds.map(() => `$${paramIndex++}`).join(", ")})`;
|
|
4004
|
+
messageParams.push(...options.messageFilter.messageIds);
|
|
4005
|
+
}
|
|
4006
|
+
messageQuery += ` ORDER BY "createdAt" ASC`;
|
|
4007
|
+
if (options?.messageLimit && options.messageLimit > 0) {
|
|
4008
|
+
const limitQuery = `SELECT * FROM (${messageQuery.replace('ORDER BY "createdAt" ASC', 'ORDER BY "createdAt" DESC')} LIMIT $${paramIndex}) AS limited ORDER BY "createdAt" ASC`;
|
|
4009
|
+
messageParams.push(options.messageLimit);
|
|
4010
|
+
messageQuery = limitQuery;
|
|
4011
|
+
}
|
|
4012
|
+
const sourceMessages = await t.manyOrNone(messageQuery, messageParams);
|
|
4013
|
+
const now = /* @__PURE__ */ new Date();
|
|
4014
|
+
const lastMessageId = sourceMessages.length > 0 ? sourceMessages[sourceMessages.length - 1].id : void 0;
|
|
4015
|
+
const cloneMetadata = {
|
|
4016
|
+
sourceThreadId,
|
|
4017
|
+
clonedAt: now,
|
|
4018
|
+
...lastMessageId && { lastMessageId }
|
|
4019
|
+
};
|
|
4020
|
+
const newThread = {
|
|
4021
|
+
id: newThreadId,
|
|
4022
|
+
resourceId: resourceId || sourceThread.resourceId,
|
|
4023
|
+
title: title || (sourceThread.title ? `Clone of ${sourceThread.title}` : void 0),
|
|
4024
|
+
metadata: {
|
|
4025
|
+
...metadata,
|
|
4026
|
+
clone: cloneMetadata
|
|
4027
|
+
},
|
|
4028
|
+
createdAt: now,
|
|
4029
|
+
updatedAt: now
|
|
4030
|
+
};
|
|
4031
|
+
await t.none(
|
|
4032
|
+
`INSERT INTO ${threadTableName} (
|
|
4033
|
+
id,
|
|
4034
|
+
"resourceId",
|
|
4035
|
+
title,
|
|
4036
|
+
metadata,
|
|
4037
|
+
"createdAt",
|
|
4038
|
+
"createdAtZ",
|
|
4039
|
+
"updatedAt",
|
|
4040
|
+
"updatedAtZ"
|
|
4041
|
+
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
|
4042
|
+
[
|
|
4043
|
+
newThread.id,
|
|
4044
|
+
newThread.resourceId,
|
|
4045
|
+
newThread.title,
|
|
4046
|
+
newThread.metadata ? JSON.stringify(newThread.metadata) : null,
|
|
4047
|
+
now,
|
|
4048
|
+
now,
|
|
4049
|
+
now,
|
|
4050
|
+
now
|
|
4051
|
+
]
|
|
4052
|
+
);
|
|
4053
|
+
const clonedMessages = [];
|
|
4054
|
+
const targetResourceId = resourceId || sourceThread.resourceId;
|
|
4055
|
+
for (const sourceMsg of sourceMessages) {
|
|
4056
|
+
const newMessageId = crypto.randomUUID();
|
|
4057
|
+
const normalizedMsg = this.normalizeMessageRow(sourceMsg);
|
|
4058
|
+
let parsedContent = normalizedMsg.content;
|
|
4059
|
+
try {
|
|
4060
|
+
parsedContent = JSON.parse(normalizedMsg.content);
|
|
4061
|
+
} catch {
|
|
4062
|
+
}
|
|
4063
|
+
await t.none(
|
|
4064
|
+
`INSERT INTO ${messageTableName} (id, thread_id, content, "createdAt", "createdAtZ", role, type, "resourceId")
|
|
4065
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
|
4066
|
+
[
|
|
4067
|
+
newMessageId,
|
|
4068
|
+
newThreadId,
|
|
4069
|
+
typeof normalizedMsg.content === "string" ? normalizedMsg.content : JSON.stringify(normalizedMsg.content),
|
|
4070
|
+
normalizedMsg.createdAt,
|
|
4071
|
+
normalizedMsg.createdAt,
|
|
4072
|
+
normalizedMsg.role,
|
|
4073
|
+
normalizedMsg.type || "v2",
|
|
4074
|
+
targetResourceId
|
|
4075
|
+
]
|
|
4076
|
+
);
|
|
4077
|
+
clonedMessages.push({
|
|
4078
|
+
id: newMessageId,
|
|
4079
|
+
threadId: newThreadId,
|
|
4080
|
+
content: parsedContent,
|
|
4081
|
+
role: normalizedMsg.role,
|
|
4082
|
+
type: normalizedMsg.type,
|
|
4083
|
+
createdAt: new Date(normalizedMsg.createdAt),
|
|
4084
|
+
resourceId: targetResourceId
|
|
4085
|
+
});
|
|
4086
|
+
}
|
|
4087
|
+
return {
|
|
4088
|
+
thread: newThread,
|
|
4089
|
+
clonedMessages
|
|
4090
|
+
};
|
|
4091
|
+
});
|
|
4092
|
+
} catch (error) {
|
|
4093
|
+
if (error instanceof MastraError) {
|
|
4094
|
+
throw error;
|
|
4095
|
+
}
|
|
4096
|
+
throw new MastraError(
|
|
4097
|
+
{
|
|
4098
|
+
id: createStorageErrorId("PG", "CLONE_THREAD", "FAILED"),
|
|
4099
|
+
domain: ErrorDomain.STORAGE,
|
|
4100
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
4101
|
+
details: { sourceThreadId, newThreadId }
|
|
4102
|
+
},
|
|
4103
|
+
error
|
|
4104
|
+
);
|
|
4105
|
+
}
|
|
4106
|
+
}
|
|
3955
4107
|
};
|
|
3956
4108
|
var ObservabilityPG = class _ObservabilityPG extends ObservabilityStorage {
|
|
3957
4109
|
#db;
|