@mastra/libsql 1.0.0-beta.2 → 1.0.0-beta.3

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
@@ -3,7 +3,7 @@ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
3
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
4
4
  import { MastraVector } from '@mastra/core/vector';
5
5
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
6
- import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, safelyParseJSON, WorkflowsStorage, MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ObservabilityStorage, TABLE_SCHEMAS, SPAN_SCHEMA } from '@mastra/core/storage';
6
+ import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, transformScoreRow, WorkflowsStorage, MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ObservabilityStorage, TABLE_SCHEMAS, safelyParseJSON, SPAN_SCHEMA } from '@mastra/core/storage';
7
7
  import { MessageList } from '@mastra/core/agent';
8
8
  import { saveScorePayloadSchema } from '@mastra/core/evals';
9
9
 
@@ -1117,26 +1117,24 @@ var MemoryLibSQL = class extends MemoryStorage {
1117
1117
  if (row.type && row.type !== `v2`) result.type = row.type;
1118
1118
  return result;
1119
1119
  }
1120
- async _getIncludedMessages({
1121
- threadId,
1122
- include
1123
- }) {
1124
- if (!threadId.trim()) throw new Error("threadId must be a non-empty string");
1125
- if (!include) return null;
1120
+ async _getIncludedMessages({ include }) {
1121
+ if (!include || include.length === 0) return null;
1126
1122
  const unionQueries = [];
1127
1123
  const params = [];
1128
1124
  for (const inc of include) {
1129
1125
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
1130
- const searchId = inc.threadId || threadId;
1131
1126
  unionQueries.push(
1132
1127
  `
1133
1128
  SELECT * FROM (
1134
- WITH numbered_messages AS (
1129
+ WITH target_thread AS (
1130
+ SELECT thread_id FROM "${TABLE_MESSAGES}" WHERE id = ?
1131
+ ),
1132
+ numbered_messages AS (
1135
1133
  SELECT
1136
1134
  id, content, role, type, "createdAt", thread_id, "resourceId",
1137
1135
  ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
1138
1136
  FROM "${TABLE_MESSAGES}"
1139
- WHERE thread_id = ?
1137
+ WHERE thread_id = (SELECT thread_id FROM target_thread)
1140
1138
  ),
1141
1139
  target_positions AS (
1142
1140
  SELECT row_num as target_pos
@@ -1151,7 +1149,7 @@ var MemoryLibSQL = class extends MemoryStorage {
1151
1149
  `
1152
1150
  // Keep ASC for final sorting after fetching context
1153
1151
  );
1154
- params.push(searchId, id, withPreviousMessages, withNextMessages);
1152
+ params.push(id, id, withPreviousMessages, withNextMessages);
1155
1153
  }
1156
1154
  const finalQuery = unionQueries.join(" UNION ALL ") + ' ORDER BY "createdAt" ASC';
1157
1155
  const includedResult = await this.client.execute({ sql: finalQuery, args: params });
@@ -1198,15 +1196,16 @@ var MemoryLibSQL = class extends MemoryStorage {
1198
1196
  }
1199
1197
  async listMessages(args) {
1200
1198
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
1201
- if (!threadId.trim()) {
1199
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
1200
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
1202
1201
  throw new MastraError(
1203
1202
  {
1204
1203
  id: "STORAGE_LIBSQL_LIST_MESSAGES_INVALID_THREAD_ID",
1205
1204
  domain: ErrorDomain.STORAGE,
1206
1205
  category: ErrorCategory.THIRD_PARTY,
1207
- details: { threadId }
1206
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
1208
1207
  },
1209
- new Error("threadId must be a non-empty string")
1208
+ new Error("threadId must be a non-empty string or array of non-empty strings")
1210
1209
  );
1211
1210
  }
1212
1211
  if (page < 0) {
@@ -1225,8 +1224,9 @@ var MemoryLibSQL = class extends MemoryStorage {
1225
1224
  try {
1226
1225
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
1227
1226
  const orderByStatement = `ORDER BY "${field}" ${direction}`;
1228
- const conditions = [`thread_id = ?`];
1229
- const queryParams = [threadId];
1227
+ const threadPlaceholders = threadIds.map(() => "?").join(", ");
1228
+ const conditions = [`thread_id IN (${threadPlaceholders})`];
1229
+ const queryParams = [...threadIds];
1230
1230
  if (resourceId) {
1231
1231
  conditions.push(`"resourceId" = ?`);
1232
1232
  queryParams.push(resourceId);
@@ -1266,7 +1266,7 @@ var MemoryLibSQL = class extends MemoryStorage {
1266
1266
  }
1267
1267
  const messageIds = new Set(messages.map((m) => m.id));
1268
1268
  if (include && include.length > 0) {
1269
- const includeMessages = await this._getIncludedMessages({ threadId, include });
1269
+ const includeMessages = await this._getIncludedMessages({ include });
1270
1270
  if (includeMessages) {
1271
1271
  for (const includeMsg of includeMessages) {
1272
1272
  if (!messageIds.has(includeMsg.id)) {
@@ -1287,7 +1287,10 @@ var MemoryLibSQL = class extends MemoryStorage {
1287
1287
  }
1288
1288
  return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
1289
1289
  });
1290
- const returnedThreadMessageIds = new Set(finalMessages.filter((m) => m.threadId === threadId).map((m) => m.id));
1290
+ const threadIdSet = new Set(threadIds);
1291
+ const returnedThreadMessageIds = new Set(
1292
+ finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
1293
+ );
1291
1294
  const allThreadMessagesReturned = returnedThreadMessageIds.size >= total;
1292
1295
  const hasMore = perPageInput !== false && !allThreadMessagesReturned && offset + perPage < total;
1293
1296
  return {
@@ -1304,7 +1307,7 @@ var MemoryLibSQL = class extends MemoryStorage {
1304
1307
  domain: ErrorDomain.STORAGE,
1305
1308
  category: ErrorCategory.THIRD_PARTY,
1306
1309
  details: {
1307
- threadId,
1310
+ threadId: Array.isArray(threadId) ? threadId.join(",") : threadId,
1308
1311
  resourceId: resourceId ?? ""
1309
1312
  }
1310
1313
  },
@@ -2723,45 +2726,14 @@ var ScoresLibSQL = class extends ScoresStorage {
2723
2726
  );
2724
2727
  }
2725
2728
  }
2729
+ /**
2730
+ * LibSQL-specific score row transformation.
2731
+ * Maps additionalLLMContext column to additionalContext field.
2732
+ */
2726
2733
  transformScoreRow(row) {
2727
- const scorerValue = safelyParseJSON(row.scorer);
2728
- const inputValue = safelyParseJSON(row.input ?? "{}");
2729
- const outputValue = safelyParseJSON(row.output ?? "{}");
2730
- const additionalLLMContextValue = row.additionalLLMContext ? safelyParseJSON(row.additionalLLMContext) : null;
2731
- const requestContextValue = row.requestContext ? safelyParseJSON(row.requestContext) : null;
2732
- const metadataValue = row.metadata ? safelyParseJSON(row.metadata) : null;
2733
- const entityValue = row.entity ? safelyParseJSON(row.entity) : null;
2734
- const preprocessStepResultValue = row.preprocessStepResult ? safelyParseJSON(row.preprocessStepResult) : null;
2735
- const analyzeStepResultValue = row.analyzeStepResult ? safelyParseJSON(row.analyzeStepResult) : null;
2736
- return {
2737
- id: row.id,
2738
- traceId: row.traceId,
2739
- spanId: row.spanId,
2740
- runId: row.runId,
2741
- scorer: scorerValue,
2742
- score: row.score,
2743
- reason: row.reason,
2744
- preprocessStepResult: preprocessStepResultValue,
2745
- analyzeStepResult: analyzeStepResultValue,
2746
- analyzePrompt: row.analyzePrompt,
2747
- preprocessPrompt: row.preprocessPrompt,
2748
- generateScorePrompt: row.generateScorePrompt,
2749
- generateReasonPrompt: row.generateReasonPrompt,
2750
- metadata: metadataValue,
2751
- input: inputValue,
2752
- output: outputValue,
2753
- additionalContext: additionalLLMContextValue,
2754
- requestContext: requestContextValue,
2755
- entityType: row.entityType,
2756
- entity: entityValue,
2757
- entityId: row.entityId,
2758
- scorerId: row.scorerId,
2759
- source: row.source,
2760
- resourceId: row.resourceId,
2761
- threadId: row.threadId,
2762
- createdAt: row.createdAt,
2763
- updatedAt: row.updatedAt
2764
- };
2734
+ return transformScoreRow(row, {
2735
+ fieldMappings: { additionalContext: "additionalLLMContext" }
2736
+ });
2765
2737
  }
2766
2738
  async getScoreById({ id }) {
2767
2739
  const result = await this.client.execute({