@mastra/libsql 0.0.0-structured-output-issue-20260227214155 → 0.0.0-structured-output-errors-20260409185629

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +344 -3
  2. package/LICENSE.md +15 -0
  3. package/dist/docs/SKILL.md +15 -19
  4. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  5. package/dist/docs/references/docs-agents-agent-approval.md +136 -185
  6. package/dist/docs/references/docs-agents-networks.md +90 -207
  7. package/dist/docs/references/docs-memory-memory-processors.md +15 -15
  8. package/dist/docs/references/docs-memory-message-history.md +10 -8
  9. package/dist/docs/references/docs-memory-overview.md +219 -24
  10. package/dist/docs/references/docs-memory-semantic-recall.md +54 -29
  11. package/dist/docs/references/docs-memory-storage.md +14 -16
  12. package/dist/docs/references/docs-memory-working-memory.md +22 -22
  13. package/dist/docs/references/docs-rag-retrieval.md +16 -16
  14. package/dist/docs/references/docs-workflows-snapshots.md +1 -1
  15. package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +3 -3
  16. package/dist/docs/references/reference-core-getMemory.md +4 -5
  17. package/dist/docs/references/reference-core-listMemory.md +3 -4
  18. package/dist/docs/references/reference-core-mastra-class.md +18 -18
  19. package/dist/docs/references/reference-memory-memory-class.md +16 -18
  20. package/dist/docs/references/reference-storage-composite.md +19 -11
  21. package/dist/docs/references/reference-storage-dynamodb.md +16 -16
  22. package/dist/docs/references/reference-storage-libsql.md +3 -3
  23. package/dist/docs/references/reference-vectors-libsql.md +47 -47
  24. package/dist/index.cjs +512 -82
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +513 -83
  27. package/dist/index.js.map +1 -1
  28. package/dist/storage/db/index.d.ts +12 -0
  29. package/dist/storage/db/index.d.ts.map +1 -1
  30. package/dist/storage/domains/datasets/index.d.ts.map +1 -1
  31. package/dist/storage/domains/experiments/index.d.ts +3 -1
  32. package/dist/storage/domains/experiments/index.d.ts.map +1 -1
  33. package/dist/storage/domains/memory/index.d.ts +5 -2
  34. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  35. package/dist/storage/domains/observability/index.d.ts.map +1 -1
  36. package/package.json +8 -8
  37. package/dist/docs/references/docs-agents-agent-memory.md +0 -209
  38. package/dist/docs/references/docs-agents-network-approval.md +0 -275
  39. package/dist/docs/references/docs-observability-overview.md +0 -70
  40. package/dist/docs/references/docs-observability-tracing-exporters-default.md +0 -209
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createClient } from '@libsql/client';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
- import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BlobStore, TABLE_SKILL_BLOBS, SKILL_BLOBS_SCHEMA, DatasetsStorage, DATASETS_SCHEMA, TABLE_DATASETS, DATASET_ITEMS_SCHEMA, TABLE_DATASET_ITEMS, DATASET_VERSIONS_SCHEMA, TABLE_DATASET_VERSIONS, ensureDate, safelyParseJSON, TABLE_EXPERIMENT_RESULTS, TABLE_EXPERIMENTS, ExperimentsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, MCPClientsStorage, MCP_CLIENTS_SCHEMA, TABLE_MCP_CLIENTS, MCP_CLIENT_VERSIONS_SCHEMA, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, MCP_SERVERS_SCHEMA, TABLE_MCP_SERVERS, MCP_SERVER_VERSIONS_SCHEMA, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, SPAN_SCHEMA, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, PROMPT_BLOCKS_SCHEMA, TABLE_PROMPT_BLOCKS, PROMPT_BLOCK_VERSIONS_SCHEMA, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, SCORER_DEFINITIONS_SCHEMA, TABLE_SCORER_DEFINITIONS, SCORER_DEFINITION_VERSIONS_SCHEMA, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, transformScoreRow, SkillsStorage, SKILLS_SCHEMA, TABLE_SKILLS, SKILL_VERSIONS_SCHEMA, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, WORKSPACES_SCHEMA, TABLE_WORKSPACES, WORKSPACE_VERSIONS_SCHEMA, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, TraceStatus, getSqlType } from '@mastra/core/storage';
3
+ import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BlobStore, TABLE_SKILL_BLOBS, SKILL_BLOBS_SCHEMA, DatasetsStorage, DATASETS_SCHEMA, TABLE_DATASETS, DATASET_ITEMS_SCHEMA, TABLE_DATASET_ITEMS, DATASET_VERSIONS_SCHEMA, TABLE_DATASET_VERSIONS, ensureDate, safelyParseJSON, TABLE_EXPERIMENT_RESULTS, TABLE_EXPERIMENTS, ExperimentsStorage, EXPERIMENTS_SCHEMA, EXPERIMENT_RESULTS_SCHEMA, MCPClientsStorage, MCP_CLIENTS_SCHEMA, TABLE_MCP_CLIENTS, MCP_CLIENT_VERSIONS_SCHEMA, TABLE_MCP_CLIENT_VERSIONS, MCPServersStorage, MCP_SERVERS_SCHEMA, TABLE_MCP_SERVERS, MCP_SERVER_VERSIONS_SCHEMA, TABLE_MCP_SERVER_VERSIONS, MemoryStorage, TABLE_SCHEMAS, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ObservabilityStorage, SPAN_SCHEMA, TABLE_SPANS, listTracesArgsSchema, toTraceSpans, PromptBlocksStorage, PROMPT_BLOCKS_SCHEMA, TABLE_PROMPT_BLOCKS, PROMPT_BLOCK_VERSIONS_SCHEMA, TABLE_PROMPT_BLOCK_VERSIONS, ScorerDefinitionsStorage, SCORER_DEFINITIONS_SCHEMA, TABLE_SCORER_DEFINITIONS, SCORER_DEFINITION_VERSIONS_SCHEMA, TABLE_SCORER_DEFINITION_VERSIONS, ScoresStorage, SCORERS_SCHEMA, TABLE_SCORERS, transformScoreRow, SkillsStorage, SKILLS_SCHEMA, TABLE_SKILLS, SKILL_VERSIONS_SCHEMA, TABLE_SKILL_VERSIONS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, WorkspacesStorage, WORKSPACES_SCHEMA, TABLE_WORKSPACES, WORKSPACE_VERSIONS_SCHEMA, TABLE_WORKSPACE_VERSIONS, MastraCompositeStore, getSqlType, TraceStatus } from '@mastra/core/storage';
4
4
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
5
5
  import { MastraVector, validateTopK, validateUpsertInput } from '@mastra/core/vector';
6
6
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
@@ -1318,6 +1318,8 @@ var LibSQLDB = class extends MastraBase {
1318
1318
  maxRetries;
1319
1319
  initialBackoffMs;
1320
1320
  executeWriteOperationWithRetry;
1321
+ /** Cache of actual table columns: tableName -> Promise<Set<columnName>> (stores in-flight promise to coalesce concurrent calls) */
1322
+ tableColumnsCache = /* @__PURE__ */ new Map();
1321
1323
  constructor({
1322
1324
  client,
1323
1325
  maxRetries,
@@ -1336,6 +1338,47 @@ var LibSQLDB = class extends MastraBase {
1336
1338
  initialBackoffMs: this.initialBackoffMs
1337
1339
  });
1338
1340
  }
1341
+ /**
1342
+ * Gets the set of column names that actually exist in the database table.
1343
+ * Results are cached; the cache is invalidated when alterTable() adds new columns.
1344
+ */
1345
+ async getTableColumns(tableName) {
1346
+ const cached = this.tableColumnsCache.get(tableName);
1347
+ if (cached) return cached;
1348
+ const promise = (async () => {
1349
+ try {
1350
+ const sanitizedTable = parseSqlIdentifier(tableName, "table name");
1351
+ const result = await this.client.execute({
1352
+ sql: `PRAGMA table_info("${sanitizedTable}")`
1353
+ });
1354
+ const columns = new Set((result.rows || []).map((row) => row.name));
1355
+ if (columns.size === 0) {
1356
+ this.tableColumnsCache.delete(tableName);
1357
+ }
1358
+ return columns;
1359
+ } catch (error) {
1360
+ this.tableColumnsCache.delete(tableName);
1361
+ throw error;
1362
+ }
1363
+ })();
1364
+ this.tableColumnsCache.set(tableName, promise);
1365
+ return promise;
1366
+ }
1367
+ /**
1368
+ * Filters a record to only include columns that exist in the actual database table.
1369
+ * Unknown columns are silently dropped to ensure forward compatibility.
1370
+ */
1371
+ async filterRecordToKnownColumns(tableName, record) {
1372
+ const knownColumns = await this.getTableColumns(tableName);
1373
+ if (knownColumns.size === 0) return record;
1374
+ const filtered = {};
1375
+ for (const [key, value] of Object.entries(record)) {
1376
+ if (knownColumns.has(key)) {
1377
+ filtered[key] = value;
1378
+ }
1379
+ }
1380
+ return filtered;
1381
+ }
1339
1382
  /**
1340
1383
  * Checks if a column exists in the specified table.
1341
1384
  *
@@ -1357,10 +1400,12 @@ var LibSQLDB = class extends MastraBase {
1357
1400
  tableName,
1358
1401
  record
1359
1402
  }) {
1403
+ const filteredRecord = await this.filterRecordToKnownColumns(tableName, record);
1404
+ if (Object.keys(filteredRecord).length === 0) return;
1360
1405
  await this.client.execute(
1361
1406
  prepareStatement({
1362
1407
  tableName,
1363
- record
1408
+ record: filteredRecord
1364
1409
  })
1365
1410
  );
1366
1411
  }
@@ -1382,7 +1427,9 @@ var LibSQLDB = class extends MastraBase {
1382
1427
  keys,
1383
1428
  data
1384
1429
  }) {
1385
- await this.client.execute(prepareUpdateStatement({ tableName, updates: data, keys }));
1430
+ const filteredData = await this.filterRecordToKnownColumns(tableName, data);
1431
+ if (Object.keys(filteredData).length === 0) return;
1432
+ await this.client.execute(prepareUpdateStatement({ tableName, updates: filteredData, keys }));
1386
1433
  }
1387
1434
  /**
1388
1435
  * Updates a record in the specified table with automatic retry on lock errors.
@@ -1403,7 +1450,10 @@ var LibSQLDB = class extends MastraBase {
1403
1450
  records
1404
1451
  }) {
1405
1452
  if (records.length === 0) return;
1406
- const batchStatements = records.map((r) => prepareStatement({ tableName, record: r }));
1453
+ const filteredRecords = await Promise.all(records.map((r) => this.filterRecordToKnownColumns(tableName, r)));
1454
+ const nonEmptyRecords = filteredRecords.filter((r) => Object.keys(r).length > 0);
1455
+ if (nonEmptyRecords.length === 0) return;
1456
+ const batchStatements = nonEmptyRecords.map((r) => prepareStatement({ tableName, record: r }));
1407
1457
  await this.client.batch(batchStatements, "write");
1408
1458
  }
1409
1459
  /**
@@ -1441,7 +1491,15 @@ var LibSQLDB = class extends MastraBase {
1441
1491
  updates
1442
1492
  }) {
1443
1493
  if (updates.length === 0) return;
1444
- const batchStatements = updates.map(
1494
+ const filteredUpdates = [];
1495
+ for (const { keys, data } of updates) {
1496
+ const filteredData = await this.filterRecordToKnownColumns(tableName, data);
1497
+ if (Object.keys(filteredData).length > 0) {
1498
+ filteredUpdates.push({ keys, data: filteredData });
1499
+ }
1500
+ }
1501
+ if (filteredUpdates.length === 0) return;
1502
+ const batchStatements = filteredUpdates.map(
1445
1503
  ({ keys, data }) => prepareUpdateStatement({
1446
1504
  tableName,
1447
1505
  updates: data,
@@ -1751,6 +1809,8 @@ var LibSQLDB = class extends MastraBase {
1751
1809
  },
1752
1810
  error
1753
1811
  );
1812
+ } finally {
1813
+ this.tableColumnsCache.delete(tableName);
1754
1814
  }
1755
1815
  }
1756
1816
  /**
@@ -2030,6 +2090,8 @@ Note: This migration may take some time for large tables.
2030
2090
  },
2031
2091
  error
2032
2092
  );
2093
+ } finally {
2094
+ this.tableColumnsCache.delete(tableName);
2033
2095
  }
2034
2096
  }
2035
2097
  /**
@@ -2922,6 +2984,14 @@ var DatasetsLibSQL = class extends DatasetsStorage {
2922
2984
  await this.#db.createTable({ tableName: TABLE_DATASETS, schema: DATASETS_SCHEMA });
2923
2985
  await this.#db.createTable({ tableName: TABLE_DATASET_ITEMS, schema: DATASET_ITEMS_SCHEMA });
2924
2986
  await this.#db.createTable({ tableName: TABLE_DATASET_VERSIONS, schema: DATASET_VERSIONS_SCHEMA });
2987
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "requestContextSchema", "TEXT");
2988
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "tags", "TEXT");
2989
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "targetType", "TEXT");
2990
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "targetIds", "TEXT");
2991
+ await this.#addColumnIfNotExists(TABLE_DATASETS, "scorerIds", "TEXT");
2992
+ await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "requestContext", "TEXT");
2993
+ await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "source", "TEXT");
2994
+ await this.#addColumnIfNotExists(TABLE_DATASET_ITEMS, "expectedTrajectory", "TEXT");
2925
2995
  await this.#client.execute({
2926
2996
  sql: `CREATE INDEX IF NOT EXISTS idx_dataset_items_dataset_validto ON "${TABLE_DATASET_ITEMS}" ("datasetId", "validTo")`,
2927
2997
  args: []
@@ -2943,6 +3013,12 @@ var DatasetsLibSQL = class extends DatasetsStorage {
2943
3013
  args: []
2944
3014
  });
2945
3015
  }
3016
+ async #addColumnIfNotExists(table, column, sqlType) {
3017
+ const exists = await this.#db.hasColumn(table, column);
3018
+ if (!exists) {
3019
+ await this.#client.execute({ sql: `ALTER TABLE "${table}" ADD COLUMN "${column}" ${sqlType}`, args: [] });
3020
+ }
3021
+ }
2946
3022
  async dangerouslyClearAll() {
2947
3023
  await this.#db.deleteData({ tableName: TABLE_DATASET_VERSIONS });
2948
3024
  await this.#db.deleteData({ tableName: TABLE_DATASET_ITEMS });
@@ -2957,6 +3033,11 @@ var DatasetsLibSQL = class extends DatasetsStorage {
2957
3033
  metadata: row.metadata ? safelyParseJSON(row.metadata) : void 0,
2958
3034
  inputSchema: row.inputSchema ? safelyParseJSON(row.inputSchema) : void 0,
2959
3035
  groundTruthSchema: row.groundTruthSchema ? safelyParseJSON(row.groundTruthSchema) : void 0,
3036
+ requestContextSchema: row.requestContextSchema ? safelyParseJSON(row.requestContextSchema) : void 0,
3037
+ tags: row.tags ? safelyParseJSON(row.tags) : void 0,
3038
+ targetType: row.targetType || void 0,
3039
+ targetIds: row.targetIds ? safelyParseJSON(row.targetIds) : void 0,
3040
+ scorerIds: row.scorerIds ? safelyParseJSON(row.scorerIds) : void 0,
2960
3041
  version: row.version,
2961
3042
  createdAt: ensureDate(row.createdAt),
2962
3043
  updatedAt: ensureDate(row.updatedAt)
@@ -2969,7 +3050,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
2969
3050
  datasetVersion: row.datasetVersion,
2970
3051
  input: safelyParseJSON(row.input),
2971
3052
  groundTruth: row.groundTruth ? safelyParseJSON(row.groundTruth) : void 0,
3053
+ expectedTrajectory: row.expectedTrajectory ? safelyParseJSON(row.expectedTrajectory) : void 0,
3054
+ requestContext: row.requestContext ? safelyParseJSON(row.requestContext) : void 0,
2972
3055
  metadata: row.metadata ? safelyParseJSON(row.metadata) : void 0,
3056
+ source: row.source ? safelyParseJSON(row.source) : void 0,
2973
3057
  createdAt: ensureDate(row.createdAt),
2974
3058
  updatedAt: ensureDate(row.updatedAt)
2975
3059
  };
@@ -2983,7 +3067,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
2983
3067
  isDeleted: Boolean(row.isDeleted),
2984
3068
  input: safelyParseJSON(row.input),
2985
3069
  groundTruth: row.groundTruth ? safelyParseJSON(row.groundTruth) : void 0,
3070
+ expectedTrajectory: row.expectedTrajectory ? safelyParseJSON(row.expectedTrajectory) : void 0,
3071
+ requestContext: row.requestContext ? safelyParseJSON(row.requestContext) : void 0,
2986
3072
  metadata: row.metadata ? safelyParseJSON(row.metadata) : void 0,
3073
+ source: row.source ? safelyParseJSON(row.source) : void 0,
2987
3074
  createdAt: ensureDate(row.createdAt),
2988
3075
  updatedAt: ensureDate(row.updatedAt)
2989
3076
  };
@@ -3011,6 +3098,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3011
3098
  metadata: input.metadata,
3012
3099
  inputSchema: input.inputSchema ?? null,
3013
3100
  groundTruthSchema: input.groundTruthSchema ?? null,
3101
+ requestContextSchema: input.requestContextSchema ?? null,
3102
+ targetType: input.targetType ?? null,
3103
+ targetIds: input.targetIds ? JSON.stringify(input.targetIds) : null,
3104
+ scorerIds: input.scorerIds ? JSON.stringify(input.scorerIds) : null,
3014
3105
  version: 0,
3015
3106
  createdAt: nowIso,
3016
3107
  updatedAt: nowIso
@@ -3023,6 +3114,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3023
3114
  metadata: input.metadata,
3024
3115
  inputSchema: input.inputSchema ?? void 0,
3025
3116
  groundTruthSchema: input.groundTruthSchema ?? void 0,
3117
+ requestContextSchema: input.requestContextSchema ?? void 0,
3118
+ targetType: input.targetType ?? void 0,
3119
+ targetIds: input.targetIds ?? void 0,
3120
+ scorerIds: input.scorerIds ?? void 0,
3026
3121
  version: 0,
3027
3122
  createdAt: now,
3028
3123
  updatedAt: now
@@ -3090,6 +3185,26 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3090
3185
  updates.push("groundTruthSchema = ?");
3091
3186
  values.push(args.groundTruthSchema === null ? null : JSON.stringify(args.groundTruthSchema));
3092
3187
  }
3188
+ if (args.requestContextSchema !== void 0) {
3189
+ updates.push("requestContextSchema = ?");
3190
+ values.push(args.requestContextSchema === null ? null : JSON.stringify(args.requestContextSchema));
3191
+ }
3192
+ if (args.tags !== void 0) {
3193
+ updates.push("tags = ?");
3194
+ values.push(args.tags === null ? null : JSON.stringify(args.tags));
3195
+ }
3196
+ if (args.targetType !== void 0) {
3197
+ updates.push("targetType = ?");
3198
+ values.push(args.targetType === null ? null : args.targetType);
3199
+ }
3200
+ if (args.targetIds !== void 0) {
3201
+ updates.push("targetIds = ?");
3202
+ values.push(args.targetIds === null ? null : JSON.stringify(args.targetIds));
3203
+ }
3204
+ if (args.scorerIds !== void 0) {
3205
+ updates.push("scorerIds = ?");
3206
+ values.push(args.scorerIds === null ? null : JSON.stringify(args.scorerIds));
3207
+ }
3093
3208
  values.push(args.id);
3094
3209
  await this.#client.execute({
3095
3210
  sql: `UPDATE ${TABLE_DATASETS} SET ${updates.join(", ")} WHERE id = ?`,
@@ -3102,6 +3217,11 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3102
3217
  metadata: args.metadata ?? existing.metadata,
3103
3218
  inputSchema: (args.inputSchema !== void 0 ? args.inputSchema : existing.inputSchema) ?? void 0,
3104
3219
  groundTruthSchema: (args.groundTruthSchema !== void 0 ? args.groundTruthSchema : existing.groundTruthSchema) ?? void 0,
3220
+ requestContextSchema: (args.requestContextSchema !== void 0 ? args.requestContextSchema : existing.requestContextSchema) ?? void 0,
3221
+ tags: (args.tags !== void 0 ? args.tags : existing.tags) ?? void 0,
3222
+ targetType: (args.targetType !== void 0 ? args.targetType : existing.targetType) ?? void 0,
3223
+ targetIds: (args.targetIds !== void 0 ? args.targetIds : existing.targetIds) ?? void 0,
3224
+ scorerIds: (args.scorerIds !== void 0 ? args.scorerIds : existing.scorerIds) ?? void 0,
3105
3225
  updatedAt: new Date(now)
3106
3226
  };
3107
3227
  } catch (error) {
@@ -3207,14 +3327,17 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3207
3327
  args: [args.datasetId]
3208
3328
  },
3209
3329
  {
3210
- sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, metadata, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 0, jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3330
+ sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, expectedTrajectory, requestContext, metadata, source, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 0, jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3211
3331
  args: [
3212
3332
  id,
3213
3333
  args.datasetId,
3214
3334
  args.datasetId,
3215
3335
  jsonbArg(args.input),
3216
3336
  jsonbArg(args.groundTruth),
3337
+ jsonbArg(args.expectedTrajectory),
3338
+ jsonbArg(args.requestContext),
3217
3339
  jsonbArg(args.metadata),
3340
+ jsonbArg(args.source),
3218
3341
  nowIso,
3219
3342
  nowIso
3220
3343
  ]
@@ -3233,7 +3356,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3233
3356
  datasetVersion: newVersion,
3234
3357
  input: args.input,
3235
3358
  groundTruth: args.groundTruth,
3359
+ expectedTrajectory: args.expectedTrajectory,
3360
+ requestContext: args.requestContext,
3236
3361
  metadata: args.metadata,
3362
+ source: args.source,
3237
3363
  createdAt: now,
3238
3364
  updatedAt: now
3239
3365
  };
@@ -3271,9 +3397,12 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3271
3397
  const versionId = crypto.randomUUID();
3272
3398
  const now = /* @__PURE__ */ new Date();
3273
3399
  const nowIso = now.toISOString();
3274
- const mergedInput = args.input ?? existing.input;
3275
- const mergedGroundTruth = args.groundTruth ?? existing.groundTruth;
3276
- const mergedMetadata = args.metadata ?? existing.metadata;
3400
+ const mergedInput = args.input !== void 0 ? args.input : existing.input;
3401
+ const mergedGroundTruth = args.groundTruth !== void 0 ? args.groundTruth : existing.groundTruth;
3402
+ const mergedExpectedTrajectory = args.expectedTrajectory !== void 0 ? args.expectedTrajectory : existing.expectedTrajectory;
3403
+ const mergedRequestContext = args.requestContext !== void 0 ? args.requestContext : existing.requestContext;
3404
+ const mergedMetadata = args.metadata !== void 0 ? args.metadata : existing.metadata;
3405
+ const mergedSource = args.source !== void 0 ? args.source : existing.source;
3277
3406
  const results = await this.#client.batch(
3278
3407
  [
3279
3408
  {
@@ -3285,14 +3414,17 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3285
3414
  args: [args.datasetId, args.id]
3286
3415
  },
3287
3416
  {
3288
- sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, metadata, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 0, jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3417
+ sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, expectedTrajectory, requestContext, metadata, source, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 0, jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3289
3418
  args: [
3290
3419
  args.id,
3291
3420
  args.datasetId,
3292
3421
  args.datasetId,
3293
3422
  jsonbArg(mergedInput),
3294
3423
  jsonbArg(mergedGroundTruth),
3424
+ jsonbArg(mergedExpectedTrajectory),
3425
+ jsonbArg(mergedRequestContext),
3295
3426
  jsonbArg(mergedMetadata),
3427
+ jsonbArg(mergedSource),
3296
3428
  existing.createdAt.toISOString(),
3297
3429
  nowIso
3298
3430
  ]
@@ -3310,7 +3442,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3310
3442
  datasetVersion: newVersion,
3311
3443
  input: mergedInput,
3312
3444
  groundTruth: mergedGroundTruth,
3445
+ expectedTrajectory: mergedExpectedTrajectory,
3446
+ requestContext: mergedRequestContext,
3313
3447
  metadata: mergedMetadata,
3448
+ source: mergedSource,
3314
3449
  updatedAt: now
3315
3450
  };
3316
3451
  } catch (error) {
@@ -3350,14 +3485,16 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3350
3485
  args: [datasetId, id]
3351
3486
  },
3352
3487
  {
3353
- sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, metadata, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 1, jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3488
+ sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, requestContext, metadata, source, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 1, jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3354
3489
  args: [
3355
3490
  id,
3356
3491
  datasetId,
3357
3492
  datasetId,
3358
3493
  jsonbArg(existing.input),
3359
3494
  jsonbArg(existing.groundTruth),
3495
+ jsonbArg(existing.requestContext),
3360
3496
  jsonbArg(existing.metadata),
3497
+ jsonbArg(existing.source),
3361
3498
  existing.createdAt.toISOString(),
3362
3499
  nowIso
3363
3500
  ]
@@ -3637,14 +3774,17 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3637
3774
  const id = crypto.randomUUID();
3638
3775
  items.push({ id, input: itemInput });
3639
3776
  statements.push({
3640
- sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, metadata, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 0, jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3777
+ sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, expectedTrajectory, requestContext, metadata, source, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 0, jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3641
3778
  args: [
3642
3779
  id,
3643
3780
  input.datasetId,
3644
3781
  input.datasetId,
3645
3782
  jsonbArg(itemInput.input),
3646
3783
  jsonbArg(itemInput.groundTruth),
3784
+ jsonbArg(itemInput.expectedTrajectory),
3785
+ jsonbArg(itemInput.requestContext),
3647
3786
  jsonbArg(itemInput.metadata),
3787
+ jsonbArg(itemInput.source),
3648
3788
  nowIso,
3649
3789
  nowIso
3650
3790
  ]
@@ -3662,7 +3802,10 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3662
3802
  datasetVersion: newVersion,
3663
3803
  input: itemInput.input,
3664
3804
  groundTruth: itemInput.groundTruth,
3805
+ expectedTrajectory: itemInput.expectedTrajectory,
3806
+ requestContext: itemInput.requestContext,
3665
3807
  metadata: itemInput.metadata,
3808
+ source: itemInput.source,
3666
3809
  createdAt: now,
3667
3810
  updatedAt: now
3668
3811
  }));
@@ -3711,14 +3854,16 @@ var DatasetsLibSQL = class extends DatasetsStorage {
3711
3854
  args: [input.datasetId, item.id]
3712
3855
  });
3713
3856
  statements.push({
3714
- sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, metadata, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 1, jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3857
+ sql: `INSERT INTO ${TABLE_DATASET_ITEMS} (id, datasetId, datasetVersion, validTo, isDeleted, input, groundTruth, requestContext, metadata, source, createdAt, updatedAt) VALUES (?, ?, (SELECT version FROM ${TABLE_DATASETS} WHERE id = ?), NULL, 1, jsonb(?), jsonb(?), jsonb(?), jsonb(?), jsonb(?), ?, ?)`,
3715
3858
  args: [
3716
3859
  item.id,
3717
3860
  input.datasetId,
3718
3861
  input.datasetId,
3719
3862
  jsonbArg(item.input),
3720
3863
  jsonbArg(item.groundTruth),
3864
+ jsonbArg(item.requestContext),
3721
3865
  jsonbArg(item.metadata),
3866
+ jsonbArg(item.source),
3722
3867
  item.createdAt.toISOString(),
3723
3868
  nowIso
3724
3869
  ]
@@ -3780,6 +3925,7 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
3780
3925
  id: row.id,
3781
3926
  datasetId: row.datasetId ?? null,
3782
3927
  datasetVersion: row.datasetVersion != null ? row.datasetVersion : null,
3928
+ agentVersion: row.agentVersion ?? null,
3783
3929
  targetType: row.targetType,
3784
3930
  targetId: row.targetId,
3785
3931
  name: row.name ?? void 0,
@@ -3811,6 +3957,8 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
3811
3957
  completedAt: ensureDate(row.completedAt),
3812
3958
  retryCount: row.retryCount,
3813
3959
  traceId: row.traceId ?? null,
3960
+ status: row.status ?? null,
3961
+ tags: row.tags ? safelyParseJSON(row.tags) : null,
3814
3962
  createdAt: ensureDate(row.createdAt)
3815
3963
  };
3816
3964
  }
@@ -3826,6 +3974,7 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
3826
3974
  id,
3827
3975
  datasetId: input.datasetId ?? null,
3828
3976
  datasetVersion: input.datasetVersion ?? null,
3977
+ agentVersion: input.agentVersion ?? null,
3829
3978
  targetType: input.targetType,
3830
3979
  targetId: input.targetId,
3831
3980
  name: input.name ?? null,
@@ -3846,6 +3995,7 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
3846
3995
  id,
3847
3996
  datasetId: input.datasetId,
3848
3997
  datasetVersion: input.datasetVersion,
3998
+ agentVersion: input.agentVersion ?? null,
3849
3999
  targetType: input.targetType,
3850
4000
  targetId: input.targetId,
3851
4001
  name: input.name,
@@ -3898,6 +4048,10 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
3898
4048
  updates.push("failedCount = ?");
3899
4049
  values.push(input.failedCount);
3900
4050
  }
4051
+ if (input.totalItems !== void 0) {
4052
+ updates.push("totalItems = ?");
4053
+ values.push(input.totalItems);
4054
+ }
3901
4055
  if (input.startedAt !== void 0) {
3902
4056
  updates.push("startedAt = ?");
3903
4057
  values.push(input.startedAt?.toISOString() ?? null);
@@ -4050,6 +4204,8 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
4050
4204
  completedAt: input.completedAt.toISOString(),
4051
4205
  retryCount: input.retryCount,
4052
4206
  traceId: input.traceId ?? null,
4207
+ status: input.status ?? null,
4208
+ tags: input.tags !== void 0 && input.tags !== null ? JSON.stringify(input.tags) : null,
4053
4209
  createdAt: nowIso
4054
4210
  }
4055
4211
  });
@@ -4066,6 +4222,8 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
4066
4222
  completedAt: input.completedAt,
4067
4223
  retryCount: input.retryCount,
4068
4224
  traceId: input.traceId ?? null,
4225
+ status: input.status ?? null,
4226
+ tags: input.tags ?? null,
4069
4227
  createdAt: now
4070
4228
  };
4071
4229
  } catch (error) {
@@ -4079,6 +4237,70 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
4079
4237
  );
4080
4238
  }
4081
4239
  }
4240
+ async updateExperimentResult(input) {
4241
+ try {
4242
+ const setClauses = [];
4243
+ const values = [];
4244
+ if (input.status !== void 0) {
4245
+ setClauses.push(`"status" = ?`);
4246
+ values.push(input.status);
4247
+ }
4248
+ if (input.tags !== void 0) {
4249
+ setClauses.push(`"tags" = ?`);
4250
+ values.push(JSON.stringify(input.tags));
4251
+ }
4252
+ if (setClauses.length === 0) {
4253
+ const existing = await this.getExperimentResultById({ id: input.id });
4254
+ if (!existing) {
4255
+ throw new MastraError({
4256
+ id: createStorageErrorId("LIBSQL", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
4257
+ domain: ErrorDomain.STORAGE,
4258
+ category: ErrorCategory.USER,
4259
+ details: { resultId: input.id }
4260
+ });
4261
+ }
4262
+ return existing;
4263
+ }
4264
+ values.push(input.id);
4265
+ let whereClause = `"id" = ?`;
4266
+ if (input.experimentId) {
4267
+ values.push(input.experimentId);
4268
+ whereClause += ` AND "experimentId" = ?`;
4269
+ }
4270
+ const updateResult = await this.#client.execute({
4271
+ sql: `UPDATE ${TABLE_EXPERIMENT_RESULTS} SET ${setClauses.join(", ")} WHERE ${whereClause}`,
4272
+ args: values
4273
+ });
4274
+ if (updateResult.rowsAffected === 0) {
4275
+ throw new MastraError({
4276
+ id: createStorageErrorId("LIBSQL", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
4277
+ domain: ErrorDomain.STORAGE,
4278
+ category: ErrorCategory.USER,
4279
+ details: { resultId: input.id, ...input.experimentId ? { experimentId: input.experimentId } : {} }
4280
+ });
4281
+ }
4282
+ const result = await this.getExperimentResultById({ id: input.id });
4283
+ if (!result) {
4284
+ throw new MastraError({
4285
+ id: createStorageErrorId("LIBSQL", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
4286
+ domain: ErrorDomain.STORAGE,
4287
+ category: ErrorCategory.USER,
4288
+ details: { resultId: input.id }
4289
+ });
4290
+ }
4291
+ return result;
4292
+ } catch (error) {
4293
+ if (error instanceof MastraError) throw error;
4294
+ throw new MastraError(
4295
+ {
4296
+ id: createStorageErrorId("LIBSQL", "UPDATE_EXPERIMENT_RESULT", "FAILED"),
4297
+ domain: ErrorDomain.STORAGE,
4298
+ category: ErrorCategory.THIRD_PARTY
4299
+ },
4300
+ error
4301
+ );
4302
+ }
4303
+ }
4082
4304
  async getExperimentResultById(args) {
4083
4305
  try {
4084
4306
  const result = await this.#client.execute({
@@ -4159,6 +4381,37 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
4159
4381
  );
4160
4382
  }
4161
4383
  }
4384
+ async getReviewSummary() {
4385
+ try {
4386
+ const result = await this.#client.execute({
4387
+ sql: `SELECT
4388
+ "experimentId",
4389
+ COUNT(*) as total,
4390
+ SUM(CASE WHEN status = 'needs-review' THEN 1 ELSE 0 END) as "needsReview",
4391
+ SUM(CASE WHEN status = 'reviewed' THEN 1 ELSE 0 END) as reviewed,
4392
+ SUM(CASE WHEN status = 'complete' THEN 1 ELSE 0 END) as complete
4393
+ FROM ${TABLE_EXPERIMENT_RESULTS}
4394
+ GROUP BY "experimentId"`,
4395
+ args: []
4396
+ });
4397
+ return (result.rows ?? []).map((row) => ({
4398
+ experimentId: row.experimentId,
4399
+ total: Number(row.total ?? 0),
4400
+ needsReview: Number(row.needsReview ?? 0),
4401
+ reviewed: Number(row.reviewed ?? 0),
4402
+ complete: Number(row.complete ?? 0)
4403
+ }));
4404
+ } catch (error) {
4405
+ throw new MastraError(
4406
+ {
4407
+ id: createStorageErrorId("LIBSQL", "GET_REVIEW_SUMMARY", "FAILED"),
4408
+ domain: ErrorDomain.STORAGE,
4409
+ category: ErrorCategory.THIRD_PARTY
4410
+ },
4411
+ error
4412
+ );
4413
+ }
4414
+ }
4162
4415
  };
4163
4416
  var MCPClientsLibSQL = class extends MCPClientsStorage {
4164
4417
  #db;
@@ -5166,7 +5419,8 @@ var MemoryLibSQL = class extends MemoryStorage {
5166
5419
  "isBufferingObservation",
5167
5420
  "isBufferingReflection",
5168
5421
  "lastBufferedAtTokens",
5169
- "lastBufferedAtTime"
5422
+ "lastBufferedAtTime",
5423
+ "metadata"
5170
5424
  ]
5171
5425
  });
5172
5426
  }
@@ -5207,41 +5461,64 @@ var MemoryLibSQL = class extends MemoryStorage {
5207
5461
  if (row.type && row.type !== `v2`) result.type = row.type;
5208
5462
  return result;
5209
5463
  }
5464
+ _sortMessages(messages, field, direction) {
5465
+ return messages.sort((a, b) => {
5466
+ const isDateField = field === "createdAt" || field === "updatedAt";
5467
+ const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
5468
+ const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
5469
+ if (typeof aValue === "number" && typeof bValue === "number") {
5470
+ return direction === "ASC" ? aValue - bValue : bValue - aValue;
5471
+ }
5472
+ return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
5473
+ });
5474
+ }
5210
5475
  async _getIncludedMessages({ include }) {
5211
5476
  if (!include || include.length === 0) return null;
5477
+ const targetIds = include.map((inc) => inc.id).filter(Boolean);
5478
+ if (targetIds.length === 0) return null;
5479
+ const idPlaceholders = targetIds.map(() => "?").join(", ");
5480
+ const targetResult = await this.#client.execute({
5481
+ sql: `SELECT id, thread_id, "createdAt" FROM "${TABLE_MESSAGES}" WHERE id IN (${idPlaceholders})`,
5482
+ args: targetIds
5483
+ });
5484
+ if (!targetResult.rows || targetResult.rows.length === 0) return null;
5485
+ const targetMap = new Map(
5486
+ targetResult.rows.map((r) => [r.id, { threadId: r.thread_id, createdAt: r.createdAt }])
5487
+ );
5212
5488
  const unionQueries = [];
5213
5489
  const params = [];
5214
5490
  for (const inc of include) {
5215
5491
  const { id, withPreviousMessages = 0, withNextMessages = 0 } = inc;
5216
- unionQueries.push(
5217
- `
5218
- SELECT * FROM (
5219
- WITH target_thread AS (
5220
- SELECT thread_id FROM "${TABLE_MESSAGES}" WHERE id = ?
5221
- ),
5222
- numbered_messages AS (
5223
- SELECT
5224
- id, content, role, type, "createdAt", thread_id, "resourceId",
5225
- ROW_NUMBER() OVER (ORDER BY "createdAt" ASC) as row_num
5226
- FROM "${TABLE_MESSAGES}"
5227
- WHERE thread_id = (SELECT thread_id FROM target_thread)
5228
- ),
5229
- target_positions AS (
5230
- SELECT row_num as target_pos
5231
- FROM numbered_messages
5232
- WHERE id = ?
5233
- )
5234
- SELECT DISTINCT m.*
5235
- FROM numbered_messages m
5236
- CROSS JOIN target_positions t
5237
- WHERE m.row_num BETWEEN (t.target_pos - ?) AND (t.target_pos + ?)
5238
- )
5239
- `
5240
- // Keep ASC for final sorting after fetching context
5241
- );
5242
- params.push(id, id, withPreviousMessages, withNextMessages);
5243
- }
5244
- const finalQuery = unionQueries.join(" UNION ALL ") + ' ORDER BY "createdAt" ASC';
5492
+ const target = targetMap.get(id);
5493
+ if (!target) continue;
5494
+ unionQueries.push(`SELECT * FROM (
5495
+ SELECT id, content, role, type, "createdAt", thread_id, "resourceId"
5496
+ FROM "${TABLE_MESSAGES}"
5497
+ WHERE thread_id = ?
5498
+ AND "createdAt" <= ?
5499
+ ORDER BY "createdAt" DESC, id DESC
5500
+ LIMIT ?
5501
+ )`);
5502
+ params.push(target.threadId, target.createdAt, withPreviousMessages + 1);
5503
+ if (withNextMessages > 0) {
5504
+ unionQueries.push(`SELECT * FROM (
5505
+ SELECT id, content, role, type, "createdAt", thread_id, "resourceId"
5506
+ FROM "${TABLE_MESSAGES}"
5507
+ WHERE thread_id = ?
5508
+ AND "createdAt" > ?
5509
+ ORDER BY "createdAt" ASC, id ASC
5510
+ LIMIT ?
5511
+ )`);
5512
+ params.push(target.threadId, target.createdAt, withNextMessages);
5513
+ }
5514
+ }
5515
+ if (unionQueries.length === 0) return null;
5516
+ let finalQuery;
5517
+ if (unionQueries.length === 1) {
5518
+ finalQuery = unionQueries[0];
5519
+ } else {
5520
+ finalQuery = `${unionQueries.join(" UNION ALL ")} ORDER BY "createdAt" ASC, id ASC`;
5521
+ }
5245
5522
  const includedResult = await this.#client.execute({ sql: finalQuery, args: params });
5246
5523
  const includedRows = includedResult.rows?.map((row) => this.parseRow(row));
5247
5524
  const seen = /* @__PURE__ */ new Set();
@@ -5336,6 +5613,23 @@ var MemoryLibSQL = class extends MemoryStorage {
5336
5613
  );
5337
5614
  }
5338
5615
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
5616
+ if (perPage === 0 && (!include || include.length === 0)) {
5617
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
5618
+ }
5619
+ if (perPage === 0 && include && include.length > 0) {
5620
+ const includeMessages = await this._getIncludedMessages({ include });
5621
+ if (!includeMessages || includeMessages.length === 0) {
5622
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
5623
+ }
5624
+ const list2 = new MessageList().add(includeMessages, "memory");
5625
+ return {
5626
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
5627
+ total: 0,
5628
+ page,
5629
+ perPage: perPageForResponse,
5630
+ hasMore: false
5631
+ };
5632
+ }
5339
5633
  const countResult = await this.#client.execute({
5340
5634
  sql: `SELECT COUNT(*) as count FROM ${TABLE_MESSAGES} ${whereClause}`,
5341
5635
  args: queryParams
@@ -5369,16 +5663,7 @@ var MemoryLibSQL = class extends MemoryStorage {
5369
5663
  }
5370
5664
  }
5371
5665
  const list = new MessageList().add(messages, "memory");
5372
- let finalMessages = list.get.all.db();
5373
- finalMessages = finalMessages.sort((a, b) => {
5374
- const isDateField = field === "createdAt" || field === "updatedAt";
5375
- const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
5376
- const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
5377
- if (typeof aValue === "number" && typeof bValue === "number") {
5378
- return direction === "ASC" ? aValue - bValue : bValue - aValue;
5379
- }
5380
- return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
5381
- });
5666
+ const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
5382
5667
  const threadIdSet = new Set(threadIds);
5383
5668
  const returnedThreadMessageIds = new Set(
5384
5669
  finalMessages.filter((m) => m.threadId && threadIdSet.has(m.threadId)).map((m) => m.id)
@@ -5464,6 +5749,23 @@ var MemoryLibSQL = class extends MemoryStorage {
5464
5749
  );
5465
5750
  }
5466
5751
  const whereClause = `WHERE ${conditions.join(" AND ")}`;
5752
+ if (perPage === 0 && (!include || include.length === 0)) {
5753
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
5754
+ }
5755
+ if (perPage === 0 && include && include.length > 0) {
5756
+ const includeMessages = await this._getIncludedMessages({ include });
5757
+ if (!includeMessages || includeMessages.length === 0) {
5758
+ return { messages: [], total: 0, page, perPage: perPageForResponse, hasMore: false };
5759
+ }
5760
+ const list2 = new MessageList().add(includeMessages, "memory");
5761
+ return {
5762
+ messages: this._sortMessages(list2.get.all.db(), field, direction),
5763
+ total: 0,
5764
+ page,
5765
+ perPage: perPageForResponse,
5766
+ hasMore: false
5767
+ };
5768
+ }
5467
5769
  const countResult = await this.#client.execute({
5468
5770
  sql: `SELECT COUNT(*) as count FROM ${TABLE_MESSAGES} ${whereClause}`,
5469
5771
  args: queryParams
@@ -5497,16 +5799,7 @@ var MemoryLibSQL = class extends MemoryStorage {
5497
5799
  }
5498
5800
  }
5499
5801
  const list = new MessageList().add(messages, "memory");
5500
- let finalMessages = list.get.all.db();
5501
- finalMessages = finalMessages.sort((a, b) => {
5502
- const isDateField = field === "createdAt" || field === "updatedAt";
5503
- const aValue = isDateField ? new Date(a[field]).getTime() : a[field];
5504
- const bValue = isDateField ? new Date(b[field]).getTime() : b[field];
5505
- if (typeof aValue === "number" && typeof bValue === "number") {
5506
- return direction === "ASC" ? aValue - bValue : bValue - aValue;
5507
- }
5508
- return direction === "ASC" ? String(aValue).localeCompare(String(bValue)) : String(bValue).localeCompare(String(aValue));
5509
- });
5802
+ const finalMessages = this._sortMessages(list.get.all.db(), field, direction);
5510
5803
  const hasMore = perPageInput !== false && offset + perPage < total;
5511
5804
  return {
5512
5805
  messages: finalMessages,
@@ -6136,9 +6429,11 @@ var MemoryLibSQL = class extends MemoryStorage {
6136
6429
  ]
6137
6430
  });
6138
6431
  const clonedMessages = [];
6432
+ const messageIdMap = {};
6139
6433
  const targetResourceId = resourceId || sourceThread.resourceId;
6140
6434
  for (const sourceMsg of sourceMessages) {
6141
6435
  const newMessageId = crypto.randomUUID();
6436
+ messageIdMap[sourceMsg.id] = newMessageId;
6142
6437
  const contentStr = sourceMsg.content;
6143
6438
  let parsedContent;
6144
6439
  try {
@@ -6172,7 +6467,8 @@ var MemoryLibSQL = class extends MemoryStorage {
6172
6467
  await tx.commit();
6173
6468
  return {
6174
6469
  thread: newThread,
6175
- clonedMessages
6470
+ clonedMessages,
6471
+ messageIdMap
6176
6472
  };
6177
6473
  } catch (error) {
6178
6474
  await tx.rollback();
@@ -6259,14 +6555,26 @@ var MemoryLibSQL = class extends MemoryStorage {
6259
6555
  );
6260
6556
  }
6261
6557
  }
6262
- async getObservationalMemoryHistory(threadId, resourceId, limit = 10) {
6558
+ async getObservationalMemoryHistory(threadId, resourceId, limit = 10, options) {
6263
6559
  try {
6264
6560
  const lookupKey = this.getOMKey(threadId, resourceId);
6265
- const result = await this.#client.execute({
6266
- // Use generationCount DESC for reliable ordering (incremented for each new record)
6267
- sql: `SELECT * FROM "${OM_TABLE}" WHERE "lookupKey" = ? ORDER BY "generationCount" DESC LIMIT ?`,
6268
- args: [lookupKey, limit]
6269
- });
6561
+ const conditions = [`"lookupKey" = ?`];
6562
+ const args = [lookupKey];
6563
+ if (options?.from) {
6564
+ conditions.push(`"createdAt" >= ?`);
6565
+ args.push(options.from.toISOString());
6566
+ }
6567
+ if (options?.to) {
6568
+ conditions.push(`"createdAt" <= ?`);
6569
+ args.push(options.to.toISOString());
6570
+ }
6571
+ args.push(limit);
6572
+ let sql = `SELECT * FROM "${OM_TABLE}" WHERE ${conditions.join(" AND ")} ORDER BY "generationCount" DESC LIMIT ?`;
6573
+ if (options?.offset != null) {
6574
+ args.push(options.offset);
6575
+ sql += ` OFFSET ?`;
6576
+ }
6577
+ const result = await this.#client.execute({ sql, args });
6270
6578
  if (!result.rows) return [];
6271
6579
  return result.rows.map((row) => this.parseOMRow(row));
6272
6580
  } catch (error) {
@@ -6362,6 +6670,68 @@ var MemoryLibSQL = class extends MemoryStorage {
6362
6670
  );
6363
6671
  }
6364
6672
  }
6673
+ async insertObservationalMemoryRecord(record) {
6674
+ try {
6675
+ const lookupKey = this.getOMKey(record.threadId, record.resourceId);
6676
+ await this.#client.execute({
6677
+ sql: `INSERT INTO "${OM_TABLE}" (
6678
+ id, "lookupKey", scope, "resourceId", "threadId",
6679
+ "activeObservations", "activeObservationsPendingUpdate",
6680
+ "originType", config, "generationCount", "lastObservedAt", "lastReflectionAt",
6681
+ "pendingMessageTokens", "totalTokensObserved", "observationTokenCount",
6682
+ "observedMessageIds", "bufferedObservationChunks",
6683
+ "bufferedReflection", "bufferedReflectionTokens", "bufferedReflectionInputTokens",
6684
+ "reflectedObservationLineCount",
6685
+ "isObserving", "isReflecting", "isBufferingObservation", "isBufferingReflection",
6686
+ "lastBufferedAtTokens", "lastBufferedAtTime",
6687
+ "observedTimezone", metadata, "createdAt", "updatedAt"
6688
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
6689
+ args: [
6690
+ record.id,
6691
+ lookupKey,
6692
+ record.scope,
6693
+ record.resourceId,
6694
+ record.threadId || null,
6695
+ record.activeObservations || "",
6696
+ null,
6697
+ record.originType || "initial",
6698
+ record.config ? JSON.stringify(record.config) : null,
6699
+ record.generationCount || 0,
6700
+ record.lastObservedAt ? record.lastObservedAt.toISOString() : null,
6701
+ null,
6702
+ record.pendingMessageTokens || 0,
6703
+ record.totalTokensObserved || 0,
6704
+ record.observationTokenCount || 0,
6705
+ record.observedMessageIds ? JSON.stringify(record.observedMessageIds) : null,
6706
+ record.bufferedObservationChunks ? JSON.stringify(record.bufferedObservationChunks) : null,
6707
+ record.bufferedReflection || null,
6708
+ record.bufferedReflectionTokens ?? null,
6709
+ record.bufferedReflectionInputTokens ?? null,
6710
+ record.reflectedObservationLineCount ?? null,
6711
+ record.isObserving || false,
6712
+ record.isReflecting || false,
6713
+ record.isBufferingObservation || false,
6714
+ record.isBufferingReflection || false,
6715
+ record.lastBufferedAtTokens || 0,
6716
+ record.lastBufferedAtTime ? record.lastBufferedAtTime.toISOString() : null,
6717
+ record.observedTimezone || null,
6718
+ record.metadata ? JSON.stringify(record.metadata) : null,
6719
+ record.createdAt.toISOString(),
6720
+ record.updatedAt.toISOString()
6721
+ ]
6722
+ });
6723
+ } catch (error) {
6724
+ throw new MastraError(
6725
+ {
6726
+ id: createStorageErrorId("LIBSQL", "INSERT_OBSERVATIONAL_MEMORY_RECORD", "FAILED"),
6727
+ domain: ErrorDomain.STORAGE,
6728
+ category: ErrorCategory.THIRD_PARTY,
6729
+ details: { id: record.id, threadId: record.threadId, resourceId: record.resourceId }
6730
+ },
6731
+ error
6732
+ );
6733
+ }
6734
+ }
6365
6735
  async updateActiveObservations(input) {
6366
6736
  try {
6367
6737
  const now = /* @__PURE__ */ new Date();
@@ -6446,8 +6816,8 @@ var MemoryLibSQL = class extends MemoryStorage {
6446
6816
  "originType", config, "generationCount", "lastObservedAt", "lastReflectionAt",
6447
6817
  "pendingMessageTokens", "totalTokensObserved", "observationTokenCount",
6448
6818
  "isObserving", "isReflecting", "isBufferingObservation", "isBufferingReflection", "lastBufferedAtTokens", "lastBufferedAtTime",
6449
- "observedTimezone", "createdAt", "updatedAt"
6450
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
6819
+ "observedTimezone", metadata, "createdAt", "updatedAt"
6820
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
6451
6821
  args: [
6452
6822
  id,
6453
6823
  lookupKey,
@@ -6477,6 +6847,7 @@ var MemoryLibSQL = class extends MemoryStorage {
6477
6847
  null,
6478
6848
  // lastBufferedAtTime
6479
6849
  record.observedTimezone || null,
6850
+ record.metadata ? JSON.stringify(record.metadata) : null,
6480
6851
  now.toISOString(),
6481
6852
  now.toISOString()
6482
6853
  ]
@@ -6673,6 +7044,43 @@ var MemoryLibSQL = class extends MemoryStorage {
6673
7044
  );
6674
7045
  }
6675
7046
  }
7047
+ async updateObservationalMemoryConfig(input) {
7048
+ try {
7049
+ const selectResult = await this.#client.execute({
7050
+ sql: `SELECT config FROM "${OM_TABLE}" WHERE id = ?`,
7051
+ args: [input.id]
7052
+ });
7053
+ if (selectResult.rows.length === 0) {
7054
+ throw new MastraError({
7055
+ id: createStorageErrorId("LIBSQL", "UPDATE_OM_CONFIG", "NOT_FOUND"),
7056
+ text: `Observational memory record not found: ${input.id}`,
7057
+ domain: ErrorDomain.STORAGE,
7058
+ category: ErrorCategory.THIRD_PARTY,
7059
+ details: { id: input.id }
7060
+ });
7061
+ }
7062
+ const row = selectResult.rows[0];
7063
+ const existing = row.config ? JSON.parse(row.config) : {};
7064
+ const merged = this.deepMergeConfig(existing, input.config);
7065
+ await this.#client.execute({
7066
+ sql: `UPDATE "${OM_TABLE}" SET config = ?, "updatedAt" = ? WHERE id = ?`,
7067
+ args: [JSON.stringify(merged), (/* @__PURE__ */ new Date()).toISOString(), input.id]
7068
+ });
7069
+ } catch (error) {
7070
+ if (error instanceof MastraError) {
7071
+ throw error;
7072
+ }
7073
+ throw new MastraError(
7074
+ {
7075
+ id: createStorageErrorId("LIBSQL", "UPDATE_OM_CONFIG", "FAILED"),
7076
+ domain: ErrorDomain.STORAGE,
7077
+ category: ErrorCategory.THIRD_PARTY,
7078
+ details: { id: input.id }
7079
+ },
7080
+ error
7081
+ );
7082
+ }
7083
+ }
6676
7084
  // ============================================
6677
7085
  // Async Buffering Methods
6678
7086
  // ============================================
@@ -6712,7 +7120,8 @@ var MemoryLibSQL = class extends MemoryStorage {
6712
7120
  lastObservedAt: input.chunk.lastObservedAt,
6713
7121
  createdAt: /* @__PURE__ */ new Date(),
6714
7122
  suggestedContinuation: input.chunk.suggestedContinuation,
6715
- currentTask: input.chunk.currentTask
7123
+ currentTask: input.chunk.currentTask,
7124
+ threadTitle: input.chunk.threadTitle
6716
7125
  };
6717
7126
  const newChunks = [...existingChunks, newChunk];
6718
7127
  const lastBufferedAtTime = input.lastBufferedAtTime ? input.lastBufferedAtTime.toISOString() : null;
@@ -6793,15 +7202,15 @@ var MemoryLibSQL = class extends MemoryStorage {
6793
7202
  let bestUnderTokens = 0;
6794
7203
  for (let i = 0; i < chunks.length; i++) {
6795
7204
  cumulativeMessageTokens += chunks[i].messageTokens ?? 0;
6796
- const boundary = i + 1;
7205
+ const boundary2 = i + 1;
6797
7206
  if (cumulativeMessageTokens >= targetMessageTokens) {
6798
7207
  if (bestOverBoundary === 0 || cumulativeMessageTokens < bestOverTokens) {
6799
- bestOverBoundary = boundary;
7208
+ bestOverBoundary = boundary2;
6800
7209
  bestOverTokens = cumulativeMessageTokens;
6801
7210
  }
6802
7211
  } else {
6803
7212
  if (cumulativeMessageTokens > bestUnderTokens) {
6804
- bestUnderBoundary = boundary;
7213
+ bestUnderBoundary = boundary2;
6805
7214
  bestUnderTokens = cumulativeMessageTokens;
6806
7215
  }
6807
7216
  }
@@ -6836,13 +7245,16 @@ var MemoryLibSQL = class extends MemoryStorage {
6836
7245
  const lastObservedAtStr = lastObservedAt.toISOString();
6837
7246
  const existingActive = row.activeObservations || "";
6838
7247
  const existingTokenCount = Number(row.observationTokenCount || 0);
6839
- const newActive = existingActive ? `${existingActive}
7248
+ const boundary = `
7249
+
7250
+ --- message boundary (${lastObservedAt.toISOString()}) ---
6840
7251
 
6841
- ${activatedContent}` : activatedContent;
7252
+ `;
7253
+ const newActive = existingActive ? `${existingActive}${boundary}${activatedContent}` : activatedContent;
6842
7254
  const newTokenCount = existingTokenCount + activatedTokens;
6843
7255
  const existingPending = Number(row.pendingMessageTokens || 0);
6844
7256
  const newPending = Math.max(0, existingPending - activatedMessageTokens);
6845
- await this.#client.execute({
7257
+ const updateResult = await this.#client.execute({
6846
7258
  sql: `UPDATE "${OM_TABLE}" SET
6847
7259
  "activeObservations" = ?,
6848
7260
  "observationTokenCount" = ?,
@@ -6850,7 +7262,9 @@ ${activatedContent}` : activatedContent;
6850
7262
  "bufferedObservationChunks" = ?,
6851
7263
  "lastObservedAt" = ?,
6852
7264
  "updatedAt" = ?
6853
- WHERE id = ?`,
7265
+ WHERE id = ?
7266
+ AND "bufferedObservationChunks" IS NOT NULL
7267
+ AND "bufferedObservationChunks" != '[]'`,
6854
7268
  args: [
6855
7269
  newActive,
6856
7270
  newTokenCount,
@@ -6861,6 +7275,16 @@ ${activatedContent}` : activatedContent;
6861
7275
  input.id
6862
7276
  ]
6863
7277
  });
7278
+ if (updateResult.rowsAffected === 0) {
7279
+ return {
7280
+ chunksActivated: 0,
7281
+ messageTokensActivated: 0,
7282
+ observationTokensActivated: 0,
7283
+ messagesActivated: 0,
7284
+ activatedCycleIds: [],
7285
+ activatedMessageIds: []
7286
+ };
7287
+ }
6864
7288
  const latestChunkHints = activatedChunks[activatedChunks.length - 1];
6865
7289
  return {
6866
7290
  chunksActivated: activatedChunks.length,
@@ -7020,6 +7444,11 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
7020
7444
  }
7021
7445
  async init() {
7022
7446
  await this.#db.createTable({ tableName: TABLE_SPANS, schema: SPAN_SCHEMA });
7447
+ await this.#db.alterTable({
7448
+ tableName: TABLE_SPANS,
7449
+ schema: SPAN_SCHEMA,
7450
+ ifNotExists: ["requestContext"]
7451
+ });
7023
7452
  }
7024
7453
  async dangerouslyClearAll() {
7025
7454
  await this.#db.deleteData({ tableName: TABLE_SPANS });
@@ -7191,7 +7620,8 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
7191
7620
  }
7192
7621
  async listTraces(args) {
7193
7622
  const { filters, pagination, orderBy } = listTracesArgsSchema.parse(args);
7194
- const { page, perPage } = pagination;
7623
+ const page = pagination?.page ?? 0;
7624
+ const perPage = pagination?.perPage ?? 10;
7195
7625
  const tableName = parseSqlIdentifier(TABLE_SPANS, "table name");
7196
7626
  try {
7197
7627
  const conditions = ["parentSpanId IS NULL"];
@@ -7331,8 +7761,8 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
7331
7761
  }
7332
7762
  }
7333
7763
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
7334
- const sortField = orderBy.field;
7335
- const sortDirection = orderBy.direction;
7764
+ const sortField = orderBy?.field ?? "startedAt";
7765
+ const sortDirection = orderBy?.direction ?? "DESC";
7336
7766
  let orderByClause;
7337
7767
  if (sortField === "endedAt") {
7338
7768
  orderByClause = sortDirection === "DESC" ? `CASE WHEN ${sortField} IS NULL THEN 0 ELSE 1 END, ${sortField} DESC` : `CASE WHEN ${sortField} IS NULL THEN 1 ELSE 0 END, ${sortField} ASC`;