@mastra/dsql 1.3.0-alpha.0 → 1.3.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/index.js
CHANGED
|
@@ -2172,7 +2172,14 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
|
|
|
2172
2172
|
}, error);
|
|
2173
2173
|
});
|
|
2174
2174
|
}
|
|
2175
|
-
|
|
2175
|
+
/**
|
|
2176
|
+
* Fetches the messages named by `include` together with their surrounding context.
|
|
2177
|
+
*
|
|
2178
|
+
* @param include - Message ids to pin, each with an optional before/after window.
|
|
2179
|
+
* @param resourceId - When set, restricts both the pinned messages and their context
|
|
2180
|
+
* to that resource so an id from another resource returns nothing.
|
|
2181
|
+
*/
|
|
2182
|
+
async _getIncludedMessages({ include, resourceId }) {
|
|
2176
2183
|
if (!include || include.length === 0) return null;
|
|
2177
2184
|
const unionQueries = [];
|
|
2178
2185
|
const params = [];
|
|
@@ -2183,17 +2190,18 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
|
|
|
2183
2190
|
});
|
|
2184
2191
|
for (const inc of include) {
|
|
2185
2192
|
const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
|
|
2193
|
+
const resourceCondition = resourceId ? ` AND "resourceId" = $${paramIdx + 3}` : "";
|
|
2186
2194
|
unionQueries.push(`
|
|
2187
2195
|
SELECT * FROM (
|
|
2188
2196
|
WITH target_thread AS (
|
|
2189
|
-
SELECT thread_id FROM ${tableName} WHERE id = $${paramIdx}
|
|
2197
|
+
SELECT thread_id FROM ${tableName} WHERE id = $${paramIdx}${resourceCondition}
|
|
2190
2198
|
),
|
|
2191
2199
|
ordered_messages AS (
|
|
2192
2200
|
SELECT
|
|
2193
2201
|
*,
|
|
2194
2202
|
ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
|
|
2195
2203
|
FROM ${tableName}
|
|
2196
|
-
WHERE thread_id = (SELECT thread_id FROM target_thread)
|
|
2204
|
+
WHERE thread_id = (SELECT thread_id FROM target_thread)${resourceCondition}
|
|
2197
2205
|
)
|
|
2198
2206
|
SELECT
|
|
2199
2207
|
m.id,
|
|
@@ -2219,6 +2227,10 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
|
|
|
2219
2227
|
`);
|
|
2220
2228
|
params.push(id, withPreviousMessages, withNextMessages);
|
|
2221
2229
|
paramIdx += 3;
|
|
2230
|
+
if (resourceId) {
|
|
2231
|
+
params.push(resourceId);
|
|
2232
|
+
paramIdx += 1;
|
|
2233
|
+
}
|
|
2222
2234
|
}
|
|
2223
2235
|
const finalQuery = unionQueries.join(" UNION ALL ") + " ORDER BY \"createdAt\" ASC";
|
|
2224
2236
|
const includedRows = await this.#db.client.manyOrNone(finalQuery, params);
|
|
@@ -2344,7 +2356,10 @@ var MemoryDSQL = class MemoryDSQL extends MemoryStorage {
|
|
|
2344
2356
|
};
|
|
2345
2357
|
const messageIds = new Set(messages.map((m) => m.id));
|
|
2346
2358
|
if (include && include.length > 0) {
|
|
2347
|
-
const includeMessages = await this._getIncludedMessages({
|
|
2359
|
+
const includeMessages = await this._getIncludedMessages({
|
|
2360
|
+
include,
|
|
2361
|
+
resourceId
|
|
2362
|
+
});
|
|
2348
2363
|
if (includeMessages) {
|
|
2349
2364
|
for (const includeMsg of includeMessages) if (!messageIds.has(includeMsg.id)) {
|
|
2350
2365
|
messages.push(includeMsg);
|