@mastra/libsql 1.0.0-beta.5 → 1.0.0-beta.7

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
@@ -1,6 +1,6 @@
1
1
  import { createClient } from '@libsql/client';
2
2
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
- import { createVectorErrorId, MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, createStorageErrorId, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, transformScoreRow, WorkflowsStorage, MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ObservabilityStorage, TABLE_SCHEMAS, safelyParseJSON, SPAN_SCHEMA } from '@mastra/core/storage';
3
+ import { createVectorErrorId, MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, TABLE_SPANS, createStorageErrorId, ScoresStorage, TABLE_SCORERS, normalizePerPage, calculatePagination, transformScoreRow, WorkflowsStorage, MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ObservabilityStorage, AgentsStorage, TABLE_AGENTS, TABLE_SCHEMAS, safelyParseJSON, SPAN_SCHEMA } from '@mastra/core/storage';
4
4
  import { parseSqlIdentifier, parseFieldKey } from '@mastra/core/utils';
5
5
  import { MastraVector } from '@mastra/core/vector';
6
6
  import { BaseFilterTranslator } from '@mastra/core/vector/filter';
@@ -1092,6 +1092,298 @@ var LibSQLVector = class extends MastraVector {
1092
1092
  });
1093
1093
  }
1094
1094
  };
1095
+ var AgentsLibSQL = class extends AgentsStorage {
1096
+ client;
1097
+ constructor({ client, operations: _ }) {
1098
+ super();
1099
+ this.client = client;
1100
+ }
1101
+ parseJson(value, fieldName) {
1102
+ if (!value) return void 0;
1103
+ if (typeof value !== "string") return value;
1104
+ try {
1105
+ return JSON.parse(value);
1106
+ } catch (error) {
1107
+ const details = {
1108
+ value: value.length > 100 ? value.substring(0, 100) + "..." : value
1109
+ };
1110
+ if (fieldName) {
1111
+ details.field = fieldName;
1112
+ }
1113
+ throw new MastraError(
1114
+ {
1115
+ id: createStorageErrorId("LIBSQL", "PARSE_JSON", "INVALID_JSON"),
1116
+ domain: ErrorDomain.STORAGE,
1117
+ category: ErrorCategory.SYSTEM,
1118
+ text: `Failed to parse JSON${fieldName ? ` for field "${fieldName}"` : ""}: ${error instanceof Error ? error.message : "Unknown error"}`,
1119
+ details
1120
+ },
1121
+ error
1122
+ );
1123
+ }
1124
+ }
1125
+ parseRow(row) {
1126
+ return {
1127
+ id: row.id,
1128
+ name: row.name,
1129
+ description: row.description,
1130
+ instructions: row.instructions,
1131
+ model: this.parseJson(row.model, "model"),
1132
+ tools: this.parseJson(row.tools, "tools"),
1133
+ defaultOptions: this.parseJson(row.defaultOptions, "defaultOptions"),
1134
+ workflows: this.parseJson(row.workflows, "workflows"),
1135
+ agents: this.parseJson(row.agents, "agents"),
1136
+ inputProcessors: this.parseJson(row.inputProcessors, "inputProcessors"),
1137
+ outputProcessors: this.parseJson(row.outputProcessors, "outputProcessors"),
1138
+ memory: this.parseJson(row.memory, "memory"),
1139
+ scorers: this.parseJson(row.scorers, "scorers"),
1140
+ metadata: this.parseJson(row.metadata, "metadata"),
1141
+ createdAt: new Date(row.createdAt),
1142
+ updatedAt: new Date(row.updatedAt)
1143
+ };
1144
+ }
1145
+ async getAgentById({ id }) {
1146
+ try {
1147
+ const result = await this.client.execute({
1148
+ sql: `SELECT * FROM "${TABLE_AGENTS}" WHERE id = ?`,
1149
+ args: [id]
1150
+ });
1151
+ if (!result.rows || result.rows.length === 0) {
1152
+ return null;
1153
+ }
1154
+ return this.parseRow(result.rows[0]);
1155
+ } catch (error) {
1156
+ throw new MastraError(
1157
+ {
1158
+ id: createStorageErrorId("LIBSQL", "GET_AGENT_BY_ID", "FAILED"),
1159
+ domain: ErrorDomain.STORAGE,
1160
+ category: ErrorCategory.THIRD_PARTY,
1161
+ details: { agentId: id }
1162
+ },
1163
+ error
1164
+ );
1165
+ }
1166
+ }
1167
+ async createAgent({ agent }) {
1168
+ try {
1169
+ const now = /* @__PURE__ */ new Date();
1170
+ const nowIso = now.toISOString();
1171
+ await this.client.execute({
1172
+ sql: `INSERT INTO "${TABLE_AGENTS}" (id, name, description, instructions, model, tools, "defaultOptions", workflows, agents, "inputProcessors", "outputProcessors", memory, scorers, metadata, "createdAt", "updatedAt")
1173
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
1174
+ args: [
1175
+ agent.id,
1176
+ agent.name,
1177
+ agent.description ?? null,
1178
+ agent.instructions,
1179
+ JSON.stringify(agent.model),
1180
+ agent.tools ? JSON.stringify(agent.tools) : null,
1181
+ agent.defaultOptions ? JSON.stringify(agent.defaultOptions) : null,
1182
+ agent.workflows ? JSON.stringify(agent.workflows) : null,
1183
+ agent.agents ? JSON.stringify(agent.agents) : null,
1184
+ agent.inputProcessors ? JSON.stringify(agent.inputProcessors) : null,
1185
+ agent.outputProcessors ? JSON.stringify(agent.outputProcessors) : null,
1186
+ agent.memory ? JSON.stringify(agent.memory) : null,
1187
+ agent.scorers ? JSON.stringify(agent.scorers) : null,
1188
+ agent.metadata ? JSON.stringify(agent.metadata) : null,
1189
+ nowIso,
1190
+ nowIso
1191
+ ]
1192
+ });
1193
+ return {
1194
+ ...agent,
1195
+ createdAt: now,
1196
+ updatedAt: now
1197
+ };
1198
+ } catch (error) {
1199
+ throw new MastraError(
1200
+ {
1201
+ id: createStorageErrorId("LIBSQL", "CREATE_AGENT", "FAILED"),
1202
+ domain: ErrorDomain.STORAGE,
1203
+ category: ErrorCategory.THIRD_PARTY,
1204
+ details: { agentId: agent.id }
1205
+ },
1206
+ error
1207
+ );
1208
+ }
1209
+ }
1210
+ async updateAgent({ id, ...updates }) {
1211
+ try {
1212
+ const existingAgent = await this.getAgentById({ id });
1213
+ if (!existingAgent) {
1214
+ throw new MastraError({
1215
+ id: createStorageErrorId("LIBSQL", "UPDATE_AGENT", "NOT_FOUND"),
1216
+ domain: ErrorDomain.STORAGE,
1217
+ category: ErrorCategory.USER,
1218
+ text: `Agent ${id} not found`,
1219
+ details: { agentId: id }
1220
+ });
1221
+ }
1222
+ const setClauses = [];
1223
+ const args = [];
1224
+ if (updates.name !== void 0) {
1225
+ setClauses.push("name = ?");
1226
+ args.push(updates.name);
1227
+ }
1228
+ if (updates.description !== void 0) {
1229
+ setClauses.push("description = ?");
1230
+ args.push(updates.description);
1231
+ }
1232
+ if (updates.instructions !== void 0) {
1233
+ setClauses.push("instructions = ?");
1234
+ args.push(updates.instructions);
1235
+ }
1236
+ if (updates.model !== void 0) {
1237
+ setClauses.push("model = ?");
1238
+ args.push(JSON.stringify(updates.model));
1239
+ }
1240
+ if (updates.tools !== void 0) {
1241
+ setClauses.push("tools = ?");
1242
+ args.push(JSON.stringify(updates.tools));
1243
+ }
1244
+ if (updates.defaultOptions !== void 0) {
1245
+ setClauses.push('"defaultOptions" = ?');
1246
+ args.push(JSON.stringify(updates.defaultOptions));
1247
+ }
1248
+ if (updates.workflows !== void 0) {
1249
+ setClauses.push("workflows = ?");
1250
+ args.push(JSON.stringify(updates.workflows));
1251
+ }
1252
+ if (updates.agents !== void 0) {
1253
+ setClauses.push("agents = ?");
1254
+ args.push(JSON.stringify(updates.agents));
1255
+ }
1256
+ if (updates.inputProcessors !== void 0) {
1257
+ setClauses.push('"inputProcessors" = ?');
1258
+ args.push(JSON.stringify(updates.inputProcessors));
1259
+ }
1260
+ if (updates.outputProcessors !== void 0) {
1261
+ setClauses.push('"outputProcessors" = ?');
1262
+ args.push(JSON.stringify(updates.outputProcessors));
1263
+ }
1264
+ if (updates.memory !== void 0) {
1265
+ setClauses.push("memory = ?");
1266
+ args.push(JSON.stringify(updates.memory));
1267
+ }
1268
+ if (updates.scorers !== void 0) {
1269
+ setClauses.push("scorers = ?");
1270
+ args.push(JSON.stringify(updates.scorers));
1271
+ }
1272
+ if (updates.metadata !== void 0) {
1273
+ const mergedMetadata = { ...existingAgent.metadata, ...updates.metadata };
1274
+ setClauses.push("metadata = ?");
1275
+ args.push(JSON.stringify(mergedMetadata));
1276
+ }
1277
+ const now = /* @__PURE__ */ new Date();
1278
+ setClauses.push('"updatedAt" = ?');
1279
+ args.push(now.toISOString());
1280
+ args.push(id);
1281
+ if (setClauses.length > 1) {
1282
+ await this.client.execute({
1283
+ sql: `UPDATE "${TABLE_AGENTS}" SET ${setClauses.join(", ")} WHERE id = ?`,
1284
+ args
1285
+ });
1286
+ }
1287
+ const updatedAgent = await this.getAgentById({ id });
1288
+ if (!updatedAgent) {
1289
+ throw new MastraError({
1290
+ id: createStorageErrorId("LIBSQL", "UPDATE_AGENT", "NOT_FOUND_AFTER_UPDATE"),
1291
+ domain: ErrorDomain.STORAGE,
1292
+ category: ErrorCategory.SYSTEM,
1293
+ text: `Agent ${id} not found after update`,
1294
+ details: { agentId: id }
1295
+ });
1296
+ }
1297
+ return updatedAgent;
1298
+ } catch (error) {
1299
+ if (error instanceof MastraError) {
1300
+ throw error;
1301
+ }
1302
+ throw new MastraError(
1303
+ {
1304
+ id: createStorageErrorId("LIBSQL", "UPDATE_AGENT", "FAILED"),
1305
+ domain: ErrorDomain.STORAGE,
1306
+ category: ErrorCategory.THIRD_PARTY,
1307
+ details: { agentId: id }
1308
+ },
1309
+ error
1310
+ );
1311
+ }
1312
+ }
1313
+ async deleteAgent({ id }) {
1314
+ try {
1315
+ await this.client.execute({
1316
+ sql: `DELETE FROM "${TABLE_AGENTS}" WHERE id = ?`,
1317
+ args: [id]
1318
+ });
1319
+ } catch (error) {
1320
+ throw new MastraError(
1321
+ {
1322
+ id: createStorageErrorId("LIBSQL", "DELETE_AGENT", "FAILED"),
1323
+ domain: ErrorDomain.STORAGE,
1324
+ category: ErrorCategory.THIRD_PARTY,
1325
+ details: { agentId: id }
1326
+ },
1327
+ error
1328
+ );
1329
+ }
1330
+ }
1331
+ async listAgents(args) {
1332
+ const { page = 0, perPage: perPageInput, orderBy } = args || {};
1333
+ const { field, direction } = this.parseOrderBy(orderBy);
1334
+ if (page < 0) {
1335
+ throw new MastraError(
1336
+ {
1337
+ id: createStorageErrorId("LIBSQL", "LIST_AGENTS", "INVALID_PAGE"),
1338
+ domain: ErrorDomain.STORAGE,
1339
+ category: ErrorCategory.USER,
1340
+ details: { page }
1341
+ },
1342
+ new Error("page must be >= 0")
1343
+ );
1344
+ }
1345
+ const perPage = normalizePerPage(perPageInput, 100);
1346
+ const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
1347
+ try {
1348
+ const countResult = await this.client.execute({
1349
+ sql: `SELECT COUNT(*) as count FROM "${TABLE_AGENTS}"`,
1350
+ args: []
1351
+ });
1352
+ const total = Number(countResult.rows?.[0]?.count ?? 0);
1353
+ if (total === 0) {
1354
+ return {
1355
+ agents: [],
1356
+ total: 0,
1357
+ page,
1358
+ perPage: perPageForResponse,
1359
+ hasMore: false
1360
+ };
1361
+ }
1362
+ const limitValue = perPageInput === false ? total : perPage;
1363
+ const dataResult = await this.client.execute({
1364
+ sql: `SELECT * FROM "${TABLE_AGENTS}" ORDER BY "${field}" ${direction} LIMIT ? OFFSET ?`,
1365
+ args: [limitValue, offset]
1366
+ });
1367
+ const agents = (dataResult.rows || []).map((row) => this.parseRow(row));
1368
+ return {
1369
+ agents,
1370
+ total,
1371
+ page,
1372
+ perPage: perPageForResponse,
1373
+ hasMore: perPageInput === false ? false : offset + perPage < total
1374
+ };
1375
+ } catch (error) {
1376
+ throw new MastraError(
1377
+ {
1378
+ id: createStorageErrorId("LIBSQL", "LIST_AGENTS", "FAILED"),
1379
+ domain: ErrorDomain.STORAGE,
1380
+ category: ErrorCategory.THIRD_PARTY
1381
+ },
1382
+ error
1383
+ );
1384
+ }
1385
+ }
1386
+ };
1095
1387
  var MemoryLibSQL = class extends MemoryStorage {
1096
1388
  client;
1097
1389
  operations;
@@ -3130,6 +3422,24 @@ var WorkflowsLibSQL = class extends WorkflowsStorage {
3130
3422
  );
3131
3423
  }
3132
3424
  }
3425
+ async deleteWorkflowRunById({ runId, workflowName }) {
3426
+ try {
3427
+ await this.client.execute({
3428
+ sql: `DELETE FROM ${TABLE_WORKFLOW_SNAPSHOT} WHERE workflow_name = ? AND run_id = ?`,
3429
+ args: [workflowName, runId]
3430
+ });
3431
+ } catch (error) {
3432
+ throw new MastraError(
3433
+ {
3434
+ id: createStorageErrorId("LIBSQL", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
3435
+ domain: ErrorDomain.STORAGE,
3436
+ category: ErrorCategory.THIRD_PARTY,
3437
+ details: { runId, workflowName }
3438
+ },
3439
+ error
3440
+ );
3441
+ }
3442
+ }
3133
3443
  async listWorkflowRuns({
3134
3444
  workflowName,
3135
3445
  fromDate,
@@ -3235,12 +3545,14 @@ var LibSQLStore = class extends MastraStorage {
3235
3545
  const workflows = new WorkflowsLibSQL({ client: this.client, operations });
3236
3546
  const memory = new MemoryLibSQL({ client: this.client, operations });
3237
3547
  const observability = new ObservabilityLibSQL({ operations });
3548
+ const agents = new AgentsLibSQL({ client: this.client, operations });
3238
3549
  this.stores = {
3239
3550
  operations,
3240
3551
  scores,
3241
3552
  workflows,
3242
3553
  memory,
3243
- observability
3554
+ observability,
3555
+ agents
3244
3556
  };
3245
3557
  }
3246
3558
  get supports() {
@@ -3251,7 +3563,8 @@ var LibSQLStore = class extends MastraStorage {
3251
3563
  createTable: true,
3252
3564
  deleteMessages: true,
3253
3565
  observabilityInstance: true,
3254
- listScoresBySpan: true
3566
+ listScoresBySpan: true,
3567
+ agents: true
3255
3568
  };
3256
3569
  }
3257
3570
  async createTable({
@@ -3389,6 +3702,9 @@ var LibSQLStore = class extends MastraStorage {
3389
3702
  }) {
3390
3703
  return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
3391
3704
  }
3705
+ async deleteWorkflowRunById({ runId, workflowName }) {
3706
+ return this.stores.workflows.deleteWorkflowRunById({ runId, workflowName });
3707
+ }
3392
3708
  async getResourceById({ resourceId }) {
3393
3709
  return this.stores.memory.getResourceById({ resourceId });
3394
3710
  }