@mastra/libsql 0.0.0-studio-deploy-20260404182525 → 0.0.0-studio-cli-20260504022012
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 +132 -3
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-memory-processors.md +3 -3
- package/dist/docs/references/docs-memory-message-history.md +8 -2
- package/dist/docs/references/docs-memory-storage.md +1 -2
- package/dist/docs/references/docs-memory-working-memory.md +2 -2
- package/dist/docs/references/guides-agent-frameworks-ai-sdk.md +4 -4
- package/dist/docs/references/reference-core-mastra-class.md +11 -1
- package/dist/docs/references/reference-storage-composite.md +5 -3
- package/dist/index.cjs +813 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +812 -10
- package/dist/index.js.map +1 -1
- package/dist/storage/db/utils.d.ts.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +18 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/domains/channels/index.d.ts +20 -0
- package/dist/storage/domains/channels/index.d.ts.map +1 -0
- package/dist/storage/domains/experiments/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +3 -2
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/observability/index.d.ts +2 -1
- package/dist/storage/domains/observability/index.d.ts.map +1 -1
- package/dist/storage/domains/schedules/index.d.ts +19 -0
- package/dist/storage/domains/schedules/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +4 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +14 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +11 -11
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,
|
|
3
|
+
import { createVectorErrorId, AgentsStorage, AGENTS_SCHEMA, TABLE_AGENTS, AGENT_VERSIONS_SCHEMA, TABLE_AGENT_VERSIONS, createStorageErrorId, normalizePerPage, calculatePagination, BackgroundTasksStorage, TABLE_SCHEMAS, TABLE_BACKGROUND_TASKS, BlobStore, TABLE_SKILL_BLOBS, SKILL_BLOBS_SCHEMA, ChannelsStorage, TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG, 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_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, SchedulesStorage, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, 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';
|
|
@@ -509,6 +509,9 @@ var LibSQLVector = class extends MastraVector {
|
|
|
509
509
|
turso;
|
|
510
510
|
maxRetries;
|
|
511
511
|
initialBackoffMs;
|
|
512
|
+
overFetchMultiplier;
|
|
513
|
+
isMemoryDb;
|
|
514
|
+
vectorIndexes;
|
|
512
515
|
constructor({
|
|
513
516
|
url,
|
|
514
517
|
authToken,
|
|
@@ -516,6 +519,7 @@ var LibSQLVector = class extends MastraVector {
|
|
|
516
519
|
syncInterval,
|
|
517
520
|
maxRetries = 5,
|
|
518
521
|
initialBackoffMs = 100,
|
|
522
|
+
vectorTopKOverFetchMultiplier = 10,
|
|
519
523
|
id
|
|
520
524
|
}) {
|
|
521
525
|
super({ id });
|
|
@@ -527,10 +531,27 @@ var LibSQLVector = class extends MastraVector {
|
|
|
527
531
|
});
|
|
528
532
|
this.maxRetries = maxRetries;
|
|
529
533
|
this.initialBackoffMs = initialBackoffMs;
|
|
530
|
-
if (
|
|
534
|
+
if (!Number.isInteger(vectorTopKOverFetchMultiplier) || vectorTopKOverFetchMultiplier < 1) {
|
|
535
|
+
throw new Error("vectorTopKOverFetchMultiplier must be a positive integer");
|
|
536
|
+
}
|
|
537
|
+
this.overFetchMultiplier = vectorTopKOverFetchMultiplier;
|
|
538
|
+
this.isMemoryDb = url.includes(":memory:");
|
|
539
|
+
if (url.includes(`file:`) || this.isMemoryDb) {
|
|
531
540
|
this.turso.execute("PRAGMA journal_mode=WAL;").then(() => this.logger.debug("LibSQLStore: PRAGMA journal_mode=WAL set.")).catch((err) => this.logger.warn("LibSQLStore: Failed to set PRAGMA journal_mode=WAL.", err));
|
|
532
541
|
this.turso.execute("PRAGMA busy_timeout = 5000;").then(() => this.logger.debug("LibSQLStore: PRAGMA busy_timeout=5000 set.")).catch((err) => this.logger.warn("LibSQLStore: Failed to set PRAGMA busy_timeout=5000.", err));
|
|
533
542
|
}
|
|
543
|
+
this.vectorIndexes = this.isMemoryDb ? Promise.resolve(/* @__PURE__ */ new Set()) : this.discoverVectorIndexes();
|
|
544
|
+
}
|
|
545
|
+
async discoverVectorIndexes() {
|
|
546
|
+
try {
|
|
547
|
+
const result = await this.turso.execute({
|
|
548
|
+
sql: `SELECT name FROM sqlite_master WHERE type='index' AND name LIKE '%_vector_idx'`,
|
|
549
|
+
args: []
|
|
550
|
+
});
|
|
551
|
+
return new Set(result.rows.map((row) => row.name));
|
|
552
|
+
} catch {
|
|
553
|
+
return /* @__PURE__ */ new Set();
|
|
554
|
+
}
|
|
534
555
|
}
|
|
535
556
|
async executeWriteOperationWithRetry(operation, isTransaction = false) {
|
|
536
557
|
let attempts = 0;
|
|
@@ -564,6 +585,40 @@ var LibSQLVector = class extends MastraVector {
|
|
|
564
585
|
const translator = new LibSQLFilterTranslator();
|
|
565
586
|
return translator.translate(filter);
|
|
566
587
|
}
|
|
588
|
+
async hasVectorIndex(parsedIndexName) {
|
|
589
|
+
const indexes = await this.vectorIndexes;
|
|
590
|
+
return indexes.has(`${parsedIndexName}_vector_idx`);
|
|
591
|
+
}
|
|
592
|
+
async queryWithIndex(parsedIndexName, vectorStr, topK, filter, includeVector, minScore) {
|
|
593
|
+
const translatedFilter = this.transformFilter(filter);
|
|
594
|
+
const { sql: filterQuery, values: filterValues } = buildFilterQuery(translatedFilter);
|
|
595
|
+
const hasFilter = filterQuery.length > 0;
|
|
596
|
+
const fetchCount = hasFilter ? topK * this.overFetchMultiplier : topK * 2;
|
|
597
|
+
const embeddingSelect = includeVector ? ", vector_extract(t.embedding) as embedding" : "";
|
|
598
|
+
const filterCondition = hasFilter ? filterQuery.replace(/^\s*WHERE\s+/i, "") : "";
|
|
599
|
+
const whereClause = hasFilter ? `WHERE ${filterCondition} AND score > ?` : "WHERE score > ?";
|
|
600
|
+
const query = `
|
|
601
|
+
WITH candidates AS (
|
|
602
|
+
SELECT t.vector_id AS id,
|
|
603
|
+
(1 - vector_distance_cos(t.embedding, vector32(?))) AS score,
|
|
604
|
+
t.metadata
|
|
605
|
+
${embeddingSelect}
|
|
606
|
+
FROM vector_top_k('${parsedIndexName}_vector_idx', vector32(?), ?) AS v
|
|
607
|
+
JOIN "${parsedIndexName}" AS t ON t.rowid = v.id
|
|
608
|
+
)
|
|
609
|
+
SELECT * FROM candidates
|
|
610
|
+
${whereClause}
|
|
611
|
+
ORDER BY score DESC
|
|
612
|
+
LIMIT ?`;
|
|
613
|
+
const args = [vectorStr, vectorStr, fetchCount, ...filterValues, minScore, topK];
|
|
614
|
+
const result = await this.turso.execute({ sql: query, args });
|
|
615
|
+
return result.rows.map(({ id, score, metadata, embedding }) => ({
|
|
616
|
+
id,
|
|
617
|
+
score,
|
|
618
|
+
metadata: JSON.parse(metadata ?? "{}"),
|
|
619
|
+
...includeVector && embedding && { vector: JSON.parse(embedding) }
|
|
620
|
+
}));
|
|
621
|
+
}
|
|
567
622
|
async query({
|
|
568
623
|
indexName,
|
|
569
624
|
queryVector,
|
|
@@ -594,6 +649,23 @@ var LibSQLVector = class extends MastraVector {
|
|
|
594
649
|
try {
|
|
595
650
|
const parsedIndexName = parseSqlIdentifier(indexName, "index name");
|
|
596
651
|
const vectorStr = `[${queryVector.join(",")}]`;
|
|
652
|
+
if (!this.isMemoryDb && await this.hasVectorIndex(parsedIndexName)) {
|
|
653
|
+
try {
|
|
654
|
+
const indexedResults = await this.queryWithIndex(
|
|
655
|
+
parsedIndexName,
|
|
656
|
+
vectorStr,
|
|
657
|
+
topK,
|
|
658
|
+
filter,
|
|
659
|
+
includeVector,
|
|
660
|
+
minScore
|
|
661
|
+
);
|
|
662
|
+
if (!filter || indexedResults.length >= topK) {
|
|
663
|
+
return indexedResults;
|
|
664
|
+
}
|
|
665
|
+
} catch (err) {
|
|
666
|
+
this.logger.warn("LibSQLVector: indexed query failed, falling back to brute-force", err);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
597
669
|
const translatedFilter = this.transformFilter(filter);
|
|
598
670
|
const { sql: filterQuery, values: filterValues } = buildFilterQuery(translatedFilter);
|
|
599
671
|
filterValues.push(minScore);
|
|
@@ -727,6 +799,7 @@ var LibSQLVector = class extends MastraVector {
|
|
|
727
799
|
`,
|
|
728
800
|
args: []
|
|
729
801
|
});
|
|
802
|
+
void this.vectorIndexes.then((indexes) => indexes.add(`${parsedIndexName}_vector_idx`));
|
|
730
803
|
}
|
|
731
804
|
deleteIndex(args) {
|
|
732
805
|
try {
|
|
@@ -749,6 +822,7 @@ var LibSQLVector = class extends MastraVector {
|
|
|
749
822
|
sql: `DROP TABLE IF EXISTS ${parsedIndexName}`,
|
|
750
823
|
args: []
|
|
751
824
|
});
|
|
825
|
+
void this.vectorIndexes.then((indexes) => indexes.delete(`${parsedIndexName}_vector_idx`));
|
|
752
826
|
}
|
|
753
827
|
async listIndexes() {
|
|
754
828
|
try {
|
|
@@ -1285,6 +1359,9 @@ function transformFromSqlRow({
|
|
|
1285
1359
|
const dateColumns = new Set(
|
|
1286
1360
|
Object.keys(TABLE_SCHEMAS[tableName]).filter((key) => TABLE_SCHEMAS[tableName][key].type === "timestamp").map((key) => key)
|
|
1287
1361
|
);
|
|
1362
|
+
const booleanColumns = new Set(
|
|
1363
|
+
Object.keys(TABLE_SCHEMAS[tableName]).filter((key) => TABLE_SCHEMAS[tableName][key].type === "boolean").map((key) => key)
|
|
1364
|
+
);
|
|
1288
1365
|
for (const [key, value] of Object.entries(sqlRow)) {
|
|
1289
1366
|
if (value === null || value === void 0) {
|
|
1290
1367
|
result[key] = value;
|
|
@@ -1298,6 +1375,10 @@ function transformFromSqlRow({
|
|
|
1298
1375
|
result[key] = safelyParseJSON(value);
|
|
1299
1376
|
continue;
|
|
1300
1377
|
}
|
|
1378
|
+
if (booleanColumns.has(key)) {
|
|
1379
|
+
result[key] = Boolean(value);
|
|
1380
|
+
continue;
|
|
1381
|
+
}
|
|
1301
1382
|
result[key] = value;
|
|
1302
1383
|
}
|
|
1303
1384
|
return result;
|
|
@@ -2882,6 +2963,235 @@ var AgentsLibSQL = class extends AgentsStorage {
|
|
|
2882
2963
|
};
|
|
2883
2964
|
}
|
|
2884
2965
|
};
|
|
2966
|
+
function serializeJson(v) {
|
|
2967
|
+
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
2968
|
+
return v ?? null;
|
|
2969
|
+
}
|
|
2970
|
+
function parseJson(val) {
|
|
2971
|
+
if (val == null) return void 0;
|
|
2972
|
+
if (typeof val === "string") {
|
|
2973
|
+
try {
|
|
2974
|
+
return JSON.parse(val);
|
|
2975
|
+
} catch {
|
|
2976
|
+
return val;
|
|
2977
|
+
}
|
|
2978
|
+
}
|
|
2979
|
+
return val;
|
|
2980
|
+
}
|
|
2981
|
+
function rowToTask(row) {
|
|
2982
|
+
return {
|
|
2983
|
+
id: String(row.id),
|
|
2984
|
+
status: String(row.status),
|
|
2985
|
+
toolName: String(row.tool_name),
|
|
2986
|
+
toolCallId: String(row.tool_call_id),
|
|
2987
|
+
args: parseJson(row.args) ?? {},
|
|
2988
|
+
agentId: String(row.agent_id),
|
|
2989
|
+
threadId: row.thread_id != null ? String(row.thread_id) : void 0,
|
|
2990
|
+
resourceId: row.resource_id != null ? String(row.resource_id) : void 0,
|
|
2991
|
+
runId: String(row.run_id),
|
|
2992
|
+
result: parseJson(row.result),
|
|
2993
|
+
error: parseJson(row.error),
|
|
2994
|
+
retryCount: Number(row.retry_count),
|
|
2995
|
+
maxRetries: Number(row.max_retries),
|
|
2996
|
+
timeoutMs: Number(row.timeout_ms),
|
|
2997
|
+
createdAt: new Date(String(row.createdAt)),
|
|
2998
|
+
startedAt: row.startedAt ? new Date(String(row.startedAt)) : void 0,
|
|
2999
|
+
completedAt: row.completedAt ? new Date(String(row.completedAt)) : void 0
|
|
3000
|
+
};
|
|
3001
|
+
}
|
|
3002
|
+
var BackgroundTasksLibSQL = class extends BackgroundTasksStorage {
|
|
3003
|
+
#db;
|
|
3004
|
+
#client;
|
|
3005
|
+
constructor(config) {
|
|
3006
|
+
super();
|
|
3007
|
+
const client = resolveClient(config);
|
|
3008
|
+
this.#client = client;
|
|
3009
|
+
this.#db = new LibSQLDB({ client, maxRetries: config.maxRetries, initialBackoffMs: config.initialBackoffMs });
|
|
3010
|
+
}
|
|
3011
|
+
async init() {
|
|
3012
|
+
await this.#db.createTable({
|
|
3013
|
+
tableName: TABLE_BACKGROUND_TASKS,
|
|
3014
|
+
schema: TABLE_SCHEMAS[TABLE_BACKGROUND_TASKS]
|
|
3015
|
+
});
|
|
3016
|
+
}
|
|
3017
|
+
async dangerouslyClearAll() {
|
|
3018
|
+
await this.#db.deleteData({ tableName: TABLE_BACKGROUND_TASKS });
|
|
3019
|
+
}
|
|
3020
|
+
async createTask(task) {
|
|
3021
|
+
await this.#db.insert({
|
|
3022
|
+
tableName: TABLE_BACKGROUND_TASKS,
|
|
3023
|
+
record: {
|
|
3024
|
+
id: task.id,
|
|
3025
|
+
tool_call_id: task.toolCallId,
|
|
3026
|
+
tool_name: task.toolName,
|
|
3027
|
+
agent_id: task.agentId,
|
|
3028
|
+
thread_id: task.threadId ?? null,
|
|
3029
|
+
resource_id: task.resourceId ?? null,
|
|
3030
|
+
run_id: task.runId,
|
|
3031
|
+
status: task.status,
|
|
3032
|
+
args: task.args,
|
|
3033
|
+
result: task.result ?? null,
|
|
3034
|
+
error: task.error ?? null,
|
|
3035
|
+
retry_count: task.retryCount,
|
|
3036
|
+
max_retries: task.maxRetries,
|
|
3037
|
+
timeout_ms: task.timeoutMs,
|
|
3038
|
+
createdAt: task.createdAt.toISOString(),
|
|
3039
|
+
startedAt: task.startedAt?.toISOString() ?? null,
|
|
3040
|
+
completedAt: task.completedAt?.toISOString() ?? null
|
|
3041
|
+
}
|
|
3042
|
+
});
|
|
3043
|
+
}
|
|
3044
|
+
async updateTask(taskId, update) {
|
|
3045
|
+
const setClauses = [];
|
|
3046
|
+
const params = [];
|
|
3047
|
+
if ("status" in update) {
|
|
3048
|
+
setClauses.push("status = ?");
|
|
3049
|
+
params.push(update.status);
|
|
3050
|
+
}
|
|
3051
|
+
if ("result" in update) {
|
|
3052
|
+
setClauses.push("result = jsonb(?)");
|
|
3053
|
+
params.push(serializeJson(update.result));
|
|
3054
|
+
}
|
|
3055
|
+
if ("error" in update) {
|
|
3056
|
+
setClauses.push("error = jsonb(?)");
|
|
3057
|
+
params.push(serializeJson(update.error));
|
|
3058
|
+
}
|
|
3059
|
+
if ("retryCount" in update) {
|
|
3060
|
+
setClauses.push("retry_count = ?");
|
|
3061
|
+
params.push(update.retryCount);
|
|
3062
|
+
}
|
|
3063
|
+
if ("startedAt" in update) {
|
|
3064
|
+
setClauses.push("startedAt = ?");
|
|
3065
|
+
params.push(update.startedAt?.toISOString() ?? null);
|
|
3066
|
+
}
|
|
3067
|
+
if ("completedAt" in update) {
|
|
3068
|
+
setClauses.push("completedAt = ?");
|
|
3069
|
+
params.push(update.completedAt?.toISOString() ?? null);
|
|
3070
|
+
}
|
|
3071
|
+
if (setClauses.length === 0) return;
|
|
3072
|
+
params.push(taskId);
|
|
3073
|
+
await this.#client.execute({
|
|
3074
|
+
sql: `UPDATE ${TABLE_BACKGROUND_TASKS} SET ${setClauses.join(", ")} WHERE id = ?`,
|
|
3075
|
+
args: params
|
|
3076
|
+
});
|
|
3077
|
+
}
|
|
3078
|
+
async getTask(taskId) {
|
|
3079
|
+
const result = await this.#client.execute({
|
|
3080
|
+
sql: `SELECT ${buildSelectColumns(TABLE_BACKGROUND_TASKS)} FROM ${TABLE_BACKGROUND_TASKS} WHERE id = ?`,
|
|
3081
|
+
args: [taskId]
|
|
3082
|
+
});
|
|
3083
|
+
const row = result.rows[0];
|
|
3084
|
+
return row ? rowToTask(row) : null;
|
|
3085
|
+
}
|
|
3086
|
+
async listTasks(filter) {
|
|
3087
|
+
const conditions = [];
|
|
3088
|
+
const params = [];
|
|
3089
|
+
if (filter.status) {
|
|
3090
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
3091
|
+
conditions.push(`status IN (${statuses.map(() => "?").join(", ")})`);
|
|
3092
|
+
params.push(...statuses);
|
|
3093
|
+
}
|
|
3094
|
+
if (filter.agentId) {
|
|
3095
|
+
conditions.push("agent_id = ?");
|
|
3096
|
+
params.push(filter.agentId);
|
|
3097
|
+
}
|
|
3098
|
+
if (filter.threadId) {
|
|
3099
|
+
conditions.push("thread_id = ?");
|
|
3100
|
+
params.push(filter.threadId);
|
|
3101
|
+
}
|
|
3102
|
+
if (filter.runId) {
|
|
3103
|
+
conditions.push("run_id = ?");
|
|
3104
|
+
params.push(filter.runId);
|
|
3105
|
+
}
|
|
3106
|
+
if (filter.resourceId) {
|
|
3107
|
+
conditions.push("resource_id = ?");
|
|
3108
|
+
params.push(filter.resourceId);
|
|
3109
|
+
}
|
|
3110
|
+
if (filter.toolName) {
|
|
3111
|
+
conditions.push("tool_name = ?");
|
|
3112
|
+
params.push(filter.toolName);
|
|
3113
|
+
}
|
|
3114
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
|
|
3115
|
+
if (filter.fromDate) {
|
|
3116
|
+
conditions.push(`${dateCol} >= ?`);
|
|
3117
|
+
params.push(filter.fromDate.toISOString());
|
|
3118
|
+
}
|
|
3119
|
+
if (filter.toDate) {
|
|
3120
|
+
conditions.push(`${dateCol} < ?`);
|
|
3121
|
+
params.push(filter.toDate.toISOString());
|
|
3122
|
+
}
|
|
3123
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
3124
|
+
const countResult = await this.#client.execute({
|
|
3125
|
+
sql: `SELECT COUNT(*) as count FROM ${TABLE_BACKGROUND_TASKS} ${where}`,
|
|
3126
|
+
args: [...params]
|
|
3127
|
+
});
|
|
3128
|
+
const total = Number(countResult.rows[0]?.count ?? 0);
|
|
3129
|
+
const orderCol = filter.orderBy === "startedAt" ? "startedAt" : filter.orderBy === "completedAt" ? "completedAt" : "createdAt";
|
|
3130
|
+
const direction = filter.orderDirection === "desc" ? "DESC" : "ASC";
|
|
3131
|
+
let sql = `SELECT ${buildSelectColumns(TABLE_BACKGROUND_TASKS)} FROM ${TABLE_BACKGROUND_TASKS} ${where} ORDER BY ${orderCol} ${direction}`;
|
|
3132
|
+
if (filter.perPage != null) {
|
|
3133
|
+
sql += " LIMIT ?";
|
|
3134
|
+
params.push(filter.perPage);
|
|
3135
|
+
if (filter.page != null) {
|
|
3136
|
+
sql += " OFFSET ?";
|
|
3137
|
+
params.push(filter.page * filter.perPage);
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
const result = await this.#client.execute({ sql, args: params });
|
|
3141
|
+
return { tasks: result.rows.map((row) => rowToTask(row)), total };
|
|
3142
|
+
}
|
|
3143
|
+
async deleteTask(taskId) {
|
|
3144
|
+
await this.#client.execute({
|
|
3145
|
+
sql: `DELETE FROM ${TABLE_BACKGROUND_TASKS} WHERE id = ?`,
|
|
3146
|
+
args: [taskId]
|
|
3147
|
+
});
|
|
3148
|
+
}
|
|
3149
|
+
async deleteTasks(filter) {
|
|
3150
|
+
const conditions = [];
|
|
3151
|
+
const params = [];
|
|
3152
|
+
if (filter.status) {
|
|
3153
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
3154
|
+
conditions.push(`status IN (${statuses.map(() => "?").join(", ")})`);
|
|
3155
|
+
params.push(...statuses);
|
|
3156
|
+
}
|
|
3157
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "startedAt" : filter.dateFilterBy === "completedAt" ? "completedAt" : "createdAt";
|
|
3158
|
+
if (filter.fromDate) {
|
|
3159
|
+
conditions.push(`${dateCol} >= ?`);
|
|
3160
|
+
params.push(filter.fromDate.toISOString());
|
|
3161
|
+
}
|
|
3162
|
+
if (filter.toDate) {
|
|
3163
|
+
conditions.push(`${dateCol} < ?`);
|
|
3164
|
+
params.push(filter.toDate.toISOString());
|
|
3165
|
+
}
|
|
3166
|
+
if (filter.agentId) {
|
|
3167
|
+
conditions.push("agent_id = ?");
|
|
3168
|
+
params.push(filter.agentId);
|
|
3169
|
+
}
|
|
3170
|
+
if (filter.runId) {
|
|
3171
|
+
conditions.push("run_id = ?");
|
|
3172
|
+
params.push(filter.runId);
|
|
3173
|
+
}
|
|
3174
|
+
if (conditions.length === 0) return;
|
|
3175
|
+
await this.#client.execute({
|
|
3176
|
+
sql: `DELETE FROM ${TABLE_BACKGROUND_TASKS} WHERE ${conditions.join(" AND ")}`,
|
|
3177
|
+
args: params
|
|
3178
|
+
});
|
|
3179
|
+
}
|
|
3180
|
+
async getRunningCount() {
|
|
3181
|
+
const result = await this.#client.execute({
|
|
3182
|
+
sql: `SELECT COUNT(*) as count FROM ${TABLE_BACKGROUND_TASKS} WHERE status = 'running'`,
|
|
3183
|
+
args: []
|
|
3184
|
+
});
|
|
3185
|
+
return Number(result.rows[0]?.count ?? 0);
|
|
3186
|
+
}
|
|
3187
|
+
async getRunningCountByAgent(agentId) {
|
|
3188
|
+
const result = await this.#client.execute({
|
|
3189
|
+
sql: `SELECT COUNT(*) as count FROM ${TABLE_BACKGROUND_TASKS} WHERE status = 'running' AND agent_id = ?`,
|
|
3190
|
+
args: [agentId]
|
|
3191
|
+
});
|
|
3192
|
+
return Number(result.rows[0]?.count ?? 0);
|
|
3193
|
+
}
|
|
3194
|
+
};
|
|
2885
3195
|
var BlobsLibSQL = class extends BlobStore {
|
|
2886
3196
|
#db;
|
|
2887
3197
|
#client;
|
|
@@ -2968,6 +3278,149 @@ var BlobsLibSQL = class extends BlobStore {
|
|
|
2968
3278
|
};
|
|
2969
3279
|
}
|
|
2970
3280
|
};
|
|
3281
|
+
var ChannelsLibSQL = class extends ChannelsStorage {
|
|
3282
|
+
#db;
|
|
3283
|
+
#client;
|
|
3284
|
+
static MANAGED_TABLES = [TABLE_CHANNEL_INSTALLATIONS, TABLE_CHANNEL_CONFIG];
|
|
3285
|
+
constructor(config) {
|
|
3286
|
+
super();
|
|
3287
|
+
const client = resolveClient(config);
|
|
3288
|
+
this.#client = client;
|
|
3289
|
+
this.#db = new LibSQLDB({ client, maxRetries: config.maxRetries, initialBackoffMs: config.initialBackoffMs });
|
|
3290
|
+
}
|
|
3291
|
+
async init() {
|
|
3292
|
+
await this.#db.createTable({
|
|
3293
|
+
tableName: TABLE_CHANNEL_INSTALLATIONS,
|
|
3294
|
+
schema: TABLE_SCHEMAS[TABLE_CHANNEL_INSTALLATIONS]
|
|
3295
|
+
});
|
|
3296
|
+
await this.#db.createTable({
|
|
3297
|
+
tableName: TABLE_CHANNEL_CONFIG,
|
|
3298
|
+
schema: TABLE_SCHEMAS[TABLE_CHANNEL_CONFIG]
|
|
3299
|
+
});
|
|
3300
|
+
await this.#client.execute(
|
|
3301
|
+
`CREATE UNIQUE INDEX IF NOT EXISTS idx_channel_installations_webhook ON "${TABLE_CHANNEL_INSTALLATIONS}" ("webhookId")`
|
|
3302
|
+
);
|
|
3303
|
+
await this.#client.execute(
|
|
3304
|
+
`CREATE INDEX IF NOT EXISTS idx_channel_installations_platform_agent ON "${TABLE_CHANNEL_INSTALLATIONS}" ("platform", "agentId")`
|
|
3305
|
+
);
|
|
3306
|
+
}
|
|
3307
|
+
async dangerouslyClearAll() {
|
|
3308
|
+
await this.#db.deleteData({ tableName: TABLE_CHANNEL_INSTALLATIONS });
|
|
3309
|
+
await this.#db.deleteData({ tableName: TABLE_CHANNEL_CONFIG });
|
|
3310
|
+
}
|
|
3311
|
+
async saveInstallation(installation) {
|
|
3312
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3313
|
+
await this.#client.execute({
|
|
3314
|
+
sql: `
|
|
3315
|
+
INSERT INTO "${TABLE_CHANNEL_INSTALLATIONS}" (id, platform, agentId, status, webhookId, data, configHash, error, createdAt, updatedAt)
|
|
3316
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
3317
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
3318
|
+
platform = excluded.platform,
|
|
3319
|
+
agentId = excluded.agentId,
|
|
3320
|
+
status = excluded.status,
|
|
3321
|
+
webhookId = excluded.webhookId,
|
|
3322
|
+
data = excluded.data,
|
|
3323
|
+
configHash = excluded.configHash,
|
|
3324
|
+
error = excluded.error,
|
|
3325
|
+
updatedAt = excluded.updatedAt
|
|
3326
|
+
`,
|
|
3327
|
+
args: [
|
|
3328
|
+
installation.id,
|
|
3329
|
+
installation.platform,
|
|
3330
|
+
installation.agentId,
|
|
3331
|
+
installation.status,
|
|
3332
|
+
installation.webhookId ?? null,
|
|
3333
|
+
JSON.stringify(installation.data),
|
|
3334
|
+
installation.configHash ?? null,
|
|
3335
|
+
installation.error ?? null,
|
|
3336
|
+
installation.createdAt?.toISOString() ?? now,
|
|
3337
|
+
now
|
|
3338
|
+
]
|
|
3339
|
+
});
|
|
3340
|
+
}
|
|
3341
|
+
async getInstallation(id) {
|
|
3342
|
+
const result = await this.#client.execute({
|
|
3343
|
+
sql: `SELECT * FROM "${TABLE_CHANNEL_INSTALLATIONS}" WHERE id = ?`,
|
|
3344
|
+
args: [id]
|
|
3345
|
+
});
|
|
3346
|
+
const row = result.rows?.[0];
|
|
3347
|
+
return row ? this.#parseInstallationRow(row) : null;
|
|
3348
|
+
}
|
|
3349
|
+
async getInstallationByAgent(platform, agentId) {
|
|
3350
|
+
const result = await this.#client.execute({
|
|
3351
|
+
sql: `SELECT * FROM "${TABLE_CHANNEL_INSTALLATIONS}" WHERE platform = ? AND agentId = ? ORDER BY CASE status WHEN 'active' THEN 0 WHEN 'pending' THEN 1 ELSE 2 END, updatedAt DESC LIMIT 1`,
|
|
3352
|
+
args: [platform, agentId]
|
|
3353
|
+
});
|
|
3354
|
+
const row = result.rows?.[0];
|
|
3355
|
+
return row ? this.#parseInstallationRow(row) : null;
|
|
3356
|
+
}
|
|
3357
|
+
async getInstallationByWebhookId(webhookId) {
|
|
3358
|
+
const result = await this.#client.execute({
|
|
3359
|
+
sql: `SELECT * FROM "${TABLE_CHANNEL_INSTALLATIONS}" WHERE webhookId = ?`,
|
|
3360
|
+
args: [webhookId]
|
|
3361
|
+
});
|
|
3362
|
+
const row = result.rows?.[0];
|
|
3363
|
+
return row ? this.#parseInstallationRow(row) : null;
|
|
3364
|
+
}
|
|
3365
|
+
async listInstallations(platform) {
|
|
3366
|
+
const result = await this.#client.execute({
|
|
3367
|
+
sql: `SELECT * FROM "${TABLE_CHANNEL_INSTALLATIONS}" WHERE platform = ? ORDER BY createdAt DESC`,
|
|
3368
|
+
args: [platform]
|
|
3369
|
+
});
|
|
3370
|
+
return result.rows.map((row) => this.#parseInstallationRow(row));
|
|
3371
|
+
}
|
|
3372
|
+
async deleteInstallation(id) {
|
|
3373
|
+
await this.#client.execute({
|
|
3374
|
+
sql: `DELETE FROM "${TABLE_CHANNEL_INSTALLATIONS}" WHERE id = ?`,
|
|
3375
|
+
args: [id]
|
|
3376
|
+
});
|
|
3377
|
+
}
|
|
3378
|
+
async saveConfig(config) {
|
|
3379
|
+
await this.#client.execute({
|
|
3380
|
+
sql: `
|
|
3381
|
+
INSERT INTO "${TABLE_CHANNEL_CONFIG}" (platform, data, updatedAt)
|
|
3382
|
+
VALUES (?, ?, ?)
|
|
3383
|
+
ON CONFLICT(platform) DO UPDATE SET
|
|
3384
|
+
data = excluded.data,
|
|
3385
|
+
updatedAt = excluded.updatedAt
|
|
3386
|
+
`,
|
|
3387
|
+
args: [config.platform, JSON.stringify(config.data), config.updatedAt.toISOString()]
|
|
3388
|
+
});
|
|
3389
|
+
}
|
|
3390
|
+
async getConfig(platform) {
|
|
3391
|
+
const result = await this.#client.execute({
|
|
3392
|
+
sql: `SELECT * FROM "${TABLE_CHANNEL_CONFIG}" WHERE platform = ?`,
|
|
3393
|
+
args: [platform]
|
|
3394
|
+
});
|
|
3395
|
+
const row = result.rows?.[0];
|
|
3396
|
+
if (!row) return null;
|
|
3397
|
+
return {
|
|
3398
|
+
platform: row.platform,
|
|
3399
|
+
data: JSON.parse(row.data || "{}"),
|
|
3400
|
+
updatedAt: new Date(row.updatedAt)
|
|
3401
|
+
};
|
|
3402
|
+
}
|
|
3403
|
+
async deleteConfig(platform) {
|
|
3404
|
+
await this.#client.execute({
|
|
3405
|
+
sql: `DELETE FROM "${TABLE_CHANNEL_CONFIG}" WHERE platform = ?`,
|
|
3406
|
+
args: [platform]
|
|
3407
|
+
});
|
|
3408
|
+
}
|
|
3409
|
+
#parseInstallationRow(row) {
|
|
3410
|
+
return {
|
|
3411
|
+
id: row.id,
|
|
3412
|
+
platform: row.platform,
|
|
3413
|
+
agentId: row.agentId,
|
|
3414
|
+
status: row.status,
|
|
3415
|
+
webhookId: row.webhookId || void 0,
|
|
3416
|
+
data: JSON.parse(row.data || "{}"),
|
|
3417
|
+
configHash: row.configHash || void 0,
|
|
3418
|
+
error: row.error || void 0,
|
|
3419
|
+
createdAt: new Date(row.createdAt),
|
|
3420
|
+
updatedAt: new Date(row.updatedAt)
|
|
3421
|
+
};
|
|
3422
|
+
}
|
|
3423
|
+
};
|
|
2971
3424
|
function jsonbArg(value) {
|
|
2972
3425
|
return value === void 0 || value === null ? null : JSON.stringify(value);
|
|
2973
3426
|
}
|
|
@@ -3902,6 +4355,16 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
|
|
|
3902
4355
|
tableName: TABLE_EXPERIMENT_RESULTS,
|
|
3903
4356
|
schema: EXPERIMENT_RESULTS_SCHEMA
|
|
3904
4357
|
});
|
|
4358
|
+
await this.#db.alterTable({
|
|
4359
|
+
tableName: TABLE_EXPERIMENTS,
|
|
4360
|
+
schema: EXPERIMENTS_SCHEMA,
|
|
4361
|
+
ifNotExists: ["agentVersion"]
|
|
4362
|
+
});
|
|
4363
|
+
await this.#db.alterTable({
|
|
4364
|
+
tableName: TABLE_EXPERIMENT_RESULTS,
|
|
4365
|
+
schema: EXPERIMENT_RESULTS_SCHEMA,
|
|
4366
|
+
ifNotExists: ["status", "tags"]
|
|
4367
|
+
});
|
|
3905
4368
|
await this.#client.execute({
|
|
3906
4369
|
sql: `CREATE INDEX IF NOT EXISTS idx_experiments_datasetid ON "${TABLE_EXPERIMENTS}" ("datasetId")`,
|
|
3907
4370
|
args: []
|
|
@@ -4122,6 +4585,22 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
|
|
|
4122
4585
|
conditions.push("datasetId = ?");
|
|
4123
4586
|
queryParams.push(args.datasetId);
|
|
4124
4587
|
}
|
|
4588
|
+
if (args.targetType) {
|
|
4589
|
+
conditions.push("targetType = ?");
|
|
4590
|
+
queryParams.push(args.targetType);
|
|
4591
|
+
}
|
|
4592
|
+
if (args.targetId) {
|
|
4593
|
+
conditions.push("targetId = ?");
|
|
4594
|
+
queryParams.push(args.targetId);
|
|
4595
|
+
}
|
|
4596
|
+
if (args.agentVersion) {
|
|
4597
|
+
conditions.push("agentVersion = ?");
|
|
4598
|
+
queryParams.push(args.agentVersion);
|
|
4599
|
+
}
|
|
4600
|
+
if (args.status) {
|
|
4601
|
+
conditions.push("status = ?");
|
|
4602
|
+
queryParams.push(args.status);
|
|
4603
|
+
}
|
|
4125
4604
|
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
4126
4605
|
const countResult = await this.#client.execute({
|
|
4127
4606
|
sql: `SELECT COUNT(*) as count FROM ${TABLE_EXPERIMENTS} ${whereClause}`,
|
|
@@ -4324,6 +4803,14 @@ var ExperimentsLibSQL = class extends ExperimentsStorage {
|
|
|
4324
4803
|
const { page, perPage: perPageInput } = args.pagination;
|
|
4325
4804
|
const conditions = ["experimentId = ?"];
|
|
4326
4805
|
const queryParams = [args.experimentId];
|
|
4806
|
+
if (args.traceId) {
|
|
4807
|
+
conditions.push("traceId = ?");
|
|
4808
|
+
queryParams.push(args.traceId);
|
|
4809
|
+
}
|
|
4810
|
+
if (args.status) {
|
|
4811
|
+
conditions.push("status = ?");
|
|
4812
|
+
queryParams.push(args.status);
|
|
4813
|
+
}
|
|
4327
4814
|
const whereClause = `WHERE ${conditions.join(" AND ")}`;
|
|
4328
4815
|
const countResult = await this.#client.execute({
|
|
4329
4816
|
sql: `SELECT COUNT(*) as count FROM ${TABLE_EXPERIMENT_RESULTS} ${whereClause}`,
|
|
@@ -6555,14 +7042,26 @@ var MemoryLibSQL = class extends MemoryStorage {
|
|
|
6555
7042
|
);
|
|
6556
7043
|
}
|
|
6557
7044
|
}
|
|
6558
|
-
async getObservationalMemoryHistory(threadId, resourceId, limit = 10) {
|
|
7045
|
+
async getObservationalMemoryHistory(threadId, resourceId, limit = 10, options) {
|
|
6559
7046
|
try {
|
|
6560
7047
|
const lookupKey = this.getOMKey(threadId, resourceId);
|
|
6561
|
-
const
|
|
6562
|
-
|
|
6563
|
-
|
|
6564
|
-
|
|
6565
|
-
|
|
7048
|
+
const conditions = [`"lookupKey" = ?`];
|
|
7049
|
+
const args = [lookupKey];
|
|
7050
|
+
if (options?.from) {
|
|
7051
|
+
conditions.push(`"createdAt" >= ?`);
|
|
7052
|
+
args.push(options.from.toISOString());
|
|
7053
|
+
}
|
|
7054
|
+
if (options?.to) {
|
|
7055
|
+
conditions.push(`"createdAt" <= ?`);
|
|
7056
|
+
args.push(options.to.toISOString());
|
|
7057
|
+
}
|
|
7058
|
+
args.push(limit);
|
|
7059
|
+
let sql = `SELECT * FROM "${OM_TABLE}" WHERE ${conditions.join(" AND ")} ORDER BY "generationCount" DESC LIMIT ?`;
|
|
7060
|
+
if (options?.offset != null) {
|
|
7061
|
+
args.push(options.offset);
|
|
7062
|
+
sql += ` OFFSET ?`;
|
|
7063
|
+
}
|
|
7064
|
+
const result = await this.#client.execute({ sql, args });
|
|
6566
7065
|
if (!result.rows) return [];
|
|
6567
7066
|
return result.rows.map((row) => this.parseOMRow(row));
|
|
6568
7067
|
} catch (error) {
|
|
@@ -7032,6 +7531,43 @@ var MemoryLibSQL = class extends MemoryStorage {
|
|
|
7032
7531
|
);
|
|
7033
7532
|
}
|
|
7034
7533
|
}
|
|
7534
|
+
async updateObservationalMemoryConfig(input) {
|
|
7535
|
+
try {
|
|
7536
|
+
const selectResult = await this.#client.execute({
|
|
7537
|
+
sql: `SELECT config FROM "${OM_TABLE}" WHERE id = ?`,
|
|
7538
|
+
args: [input.id]
|
|
7539
|
+
});
|
|
7540
|
+
if (selectResult.rows.length === 0) {
|
|
7541
|
+
throw new MastraError({
|
|
7542
|
+
id: createStorageErrorId("LIBSQL", "UPDATE_OM_CONFIG", "NOT_FOUND"),
|
|
7543
|
+
text: `Observational memory record not found: ${input.id}`,
|
|
7544
|
+
domain: ErrorDomain.STORAGE,
|
|
7545
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
7546
|
+
details: { id: input.id }
|
|
7547
|
+
});
|
|
7548
|
+
}
|
|
7549
|
+
const row = selectResult.rows[0];
|
|
7550
|
+
const existing = row.config ? JSON.parse(row.config) : {};
|
|
7551
|
+
const merged = this.deepMergeConfig(existing, input.config);
|
|
7552
|
+
await this.#client.execute({
|
|
7553
|
+
sql: `UPDATE "${OM_TABLE}" SET config = ?, "updatedAt" = ? WHERE id = ?`,
|
|
7554
|
+
args: [JSON.stringify(merged), (/* @__PURE__ */ new Date()).toISOString(), input.id]
|
|
7555
|
+
});
|
|
7556
|
+
} catch (error) {
|
|
7557
|
+
if (error instanceof MastraError) {
|
|
7558
|
+
throw error;
|
|
7559
|
+
}
|
|
7560
|
+
throw new MastraError(
|
|
7561
|
+
{
|
|
7562
|
+
id: createStorageErrorId("LIBSQL", "UPDATE_OM_CONFIG", "FAILED"),
|
|
7563
|
+
domain: ErrorDomain.STORAGE,
|
|
7564
|
+
category: ErrorCategory.THIRD_PARTY,
|
|
7565
|
+
details: { id: input.id }
|
|
7566
|
+
},
|
|
7567
|
+
error
|
|
7568
|
+
);
|
|
7569
|
+
}
|
|
7570
|
+
}
|
|
7035
7571
|
// ============================================
|
|
7036
7572
|
// Async Buffering Methods
|
|
7037
7573
|
// ============================================
|
|
@@ -7538,6 +8074,39 @@ var ObservabilityLibSQL = class extends ObservabilityStorage {
|
|
|
7538
8074
|
);
|
|
7539
8075
|
}
|
|
7540
8076
|
}
|
|
8077
|
+
async getTraceLight(args) {
|
|
8078
|
+
const { traceId } = args;
|
|
8079
|
+
try {
|
|
8080
|
+
const spans = await this.#db.selectMany({
|
|
8081
|
+
tableName: TABLE_SPANS,
|
|
8082
|
+
whereClause: { sql: " WHERE traceId = ?", args: [traceId] },
|
|
8083
|
+
orderBy: "startedAt ASC"
|
|
8084
|
+
});
|
|
8085
|
+
if (!spans || spans.length === 0) {
|
|
8086
|
+
return null;
|
|
8087
|
+
}
|
|
8088
|
+
return {
|
|
8089
|
+
traceId,
|
|
8090
|
+
spans: spans.map((span) => {
|
|
8091
|
+
const transformed = transformFromSqlRow({ tableName: TABLE_SPANS, sqlRow: span });
|
|
8092
|
+
const { input, output, attributes, metadata, tags, links, ...light } = transformed;
|
|
8093
|
+
return light;
|
|
8094
|
+
})
|
|
8095
|
+
};
|
|
8096
|
+
} catch (error) {
|
|
8097
|
+
throw new MastraError(
|
|
8098
|
+
{
|
|
8099
|
+
id: createStorageErrorId("LIBSQL", "GET_TRACE_LIGHT", "FAILED"),
|
|
8100
|
+
domain: ErrorDomain.STORAGE,
|
|
8101
|
+
category: ErrorCategory.USER,
|
|
8102
|
+
details: {
|
|
8103
|
+
traceId
|
|
8104
|
+
}
|
|
8105
|
+
},
|
|
8106
|
+
error
|
|
8107
|
+
);
|
|
8108
|
+
}
|
|
8109
|
+
}
|
|
7541
8110
|
async updateSpan(args) {
|
|
7542
8111
|
const { traceId, spanId, updates } = args;
|
|
7543
8112
|
try {
|
|
@@ -8325,6 +8894,233 @@ var PromptBlocksLibSQL = class extends PromptBlocksStorage {
|
|
|
8325
8894
|
};
|
|
8326
8895
|
}
|
|
8327
8896
|
};
|
|
8897
|
+
function parseJson2(val) {
|
|
8898
|
+
if (val == null) return void 0;
|
|
8899
|
+
if (typeof val === "string") {
|
|
8900
|
+
try {
|
|
8901
|
+
return JSON.parse(val);
|
|
8902
|
+
} catch {
|
|
8903
|
+
return val;
|
|
8904
|
+
}
|
|
8905
|
+
}
|
|
8906
|
+
return val;
|
|
8907
|
+
}
|
|
8908
|
+
function toNumber(val) {
|
|
8909
|
+
if (typeof val === "bigint") return Number(val);
|
|
8910
|
+
return Number(val);
|
|
8911
|
+
}
|
|
8912
|
+
function rowToSchedule(row) {
|
|
8913
|
+
const target = parseJson2(row.target);
|
|
8914
|
+
if (!target) {
|
|
8915
|
+
throw new Error(`Schedule row ${row.id} has invalid target`);
|
|
8916
|
+
}
|
|
8917
|
+
const schedule = {
|
|
8918
|
+
id: String(row.id),
|
|
8919
|
+
target,
|
|
8920
|
+
cron: String(row.cron),
|
|
8921
|
+
status: String(row.status),
|
|
8922
|
+
nextFireAt: toNumber(row.next_fire_at),
|
|
8923
|
+
createdAt: toNumber(row.created_at),
|
|
8924
|
+
updatedAt: toNumber(row.updated_at)
|
|
8925
|
+
};
|
|
8926
|
+
if (row.timezone != null) schedule.timezone = String(row.timezone);
|
|
8927
|
+
if (row.last_fire_at != null) schedule.lastFireAt = toNumber(row.last_fire_at);
|
|
8928
|
+
if (row.last_run_id != null) schedule.lastRunId = String(row.last_run_id);
|
|
8929
|
+
const metadata = parseJson2(row.metadata);
|
|
8930
|
+
if (metadata !== void 0) schedule.metadata = metadata;
|
|
8931
|
+
return schedule;
|
|
8932
|
+
}
|
|
8933
|
+
function rowToTrigger(row) {
|
|
8934
|
+
const trigger = {
|
|
8935
|
+
scheduleId: String(row.schedule_id),
|
|
8936
|
+
runId: String(row.run_id),
|
|
8937
|
+
scheduledFireAt: toNumber(row.scheduled_fire_at),
|
|
8938
|
+
actualFireAt: toNumber(row.actual_fire_at),
|
|
8939
|
+
status: String(row.status)
|
|
8940
|
+
};
|
|
8941
|
+
if (row.error != null) trigger.error = String(row.error);
|
|
8942
|
+
return trigger;
|
|
8943
|
+
}
|
|
8944
|
+
var SchedulesLibSQL = class extends SchedulesStorage {
|
|
8945
|
+
#db;
|
|
8946
|
+
#client;
|
|
8947
|
+
constructor(config) {
|
|
8948
|
+
super();
|
|
8949
|
+
const client = resolveClient(config);
|
|
8950
|
+
this.#client = client;
|
|
8951
|
+
this.#db = new LibSQLDB({ client, maxRetries: config.maxRetries, initialBackoffMs: config.initialBackoffMs });
|
|
8952
|
+
}
|
|
8953
|
+
async init() {
|
|
8954
|
+
await this.#db.createTable({
|
|
8955
|
+
tableName: TABLE_SCHEDULES,
|
|
8956
|
+
schema: TABLE_SCHEMAS[TABLE_SCHEDULES]
|
|
8957
|
+
});
|
|
8958
|
+
await this.#db.createTable({
|
|
8959
|
+
tableName: TABLE_SCHEDULE_TRIGGERS,
|
|
8960
|
+
schema: TABLE_SCHEMAS[TABLE_SCHEDULE_TRIGGERS]
|
|
8961
|
+
});
|
|
8962
|
+
}
|
|
8963
|
+
async dangerouslyClearAll() {
|
|
8964
|
+
await this.#db.deleteData({ tableName: TABLE_SCHEDULE_TRIGGERS });
|
|
8965
|
+
await this.#db.deleteData({ tableName: TABLE_SCHEDULES });
|
|
8966
|
+
}
|
|
8967
|
+
async createSchedule(schedule) {
|
|
8968
|
+
const existing = await this.getSchedule(schedule.id);
|
|
8969
|
+
if (existing) {
|
|
8970
|
+
throw new Error(`Schedule with id "${schedule.id}" already exists`);
|
|
8971
|
+
}
|
|
8972
|
+
await this.#db.insert({
|
|
8973
|
+
tableName: TABLE_SCHEDULES,
|
|
8974
|
+
record: {
|
|
8975
|
+
id: schedule.id,
|
|
8976
|
+
target: schedule.target,
|
|
8977
|
+
cron: schedule.cron,
|
|
8978
|
+
timezone: schedule.timezone ?? null,
|
|
8979
|
+
status: schedule.status,
|
|
8980
|
+
next_fire_at: schedule.nextFireAt,
|
|
8981
|
+
last_fire_at: schedule.lastFireAt ?? null,
|
|
8982
|
+
last_run_id: schedule.lastRunId ?? null,
|
|
8983
|
+
created_at: schedule.createdAt,
|
|
8984
|
+
updated_at: schedule.updatedAt,
|
|
8985
|
+
metadata: schedule.metadata ?? null
|
|
8986
|
+
}
|
|
8987
|
+
});
|
|
8988
|
+
return schedule;
|
|
8989
|
+
}
|
|
8990
|
+
async getSchedule(id) {
|
|
8991
|
+
const result = await this.#client.execute({
|
|
8992
|
+
sql: `SELECT ${buildSelectColumns(TABLE_SCHEDULES)} FROM ${TABLE_SCHEDULES} WHERE id = ?`,
|
|
8993
|
+
args: [id]
|
|
8994
|
+
});
|
|
8995
|
+
const row = result.rows[0];
|
|
8996
|
+
return row ? rowToSchedule(row) : null;
|
|
8997
|
+
}
|
|
8998
|
+
async listSchedules(filter) {
|
|
8999
|
+
const conditions = [];
|
|
9000
|
+
const params = [];
|
|
9001
|
+
if (filter?.status) {
|
|
9002
|
+
conditions.push("status = ?");
|
|
9003
|
+
params.push(filter.status);
|
|
9004
|
+
}
|
|
9005
|
+
if (filter?.workflowId) {
|
|
9006
|
+
conditions.push("json_extract(target, '$.workflowId') = ?");
|
|
9007
|
+
params.push(filter.workflowId);
|
|
9008
|
+
}
|
|
9009
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
9010
|
+
const result = await this.#client.execute({
|
|
9011
|
+
sql: `SELECT ${buildSelectColumns(TABLE_SCHEDULES)} FROM ${TABLE_SCHEDULES} ${where} ORDER BY created_at ASC`,
|
|
9012
|
+
args: params
|
|
9013
|
+
});
|
|
9014
|
+
return result.rows.map((r) => rowToSchedule(r));
|
|
9015
|
+
}
|
|
9016
|
+
async listDueSchedules(now, limit) {
|
|
9017
|
+
const cap = limit ?? 100;
|
|
9018
|
+
const result = await this.#client.execute({
|
|
9019
|
+
sql: `SELECT ${buildSelectColumns(TABLE_SCHEDULES)} FROM ${TABLE_SCHEDULES}
|
|
9020
|
+
WHERE status = ? AND next_fire_at <= ?
|
|
9021
|
+
ORDER BY next_fire_at ASC
|
|
9022
|
+
LIMIT ?`,
|
|
9023
|
+
args: ["active", now, cap]
|
|
9024
|
+
});
|
|
9025
|
+
return result.rows.map((r) => rowToSchedule(r));
|
|
9026
|
+
}
|
|
9027
|
+
async updateSchedule(id, patch) {
|
|
9028
|
+
const setClauses = [];
|
|
9029
|
+
const params = [];
|
|
9030
|
+
if ("cron" in patch && patch.cron !== void 0) {
|
|
9031
|
+
setClauses.push("cron = ?");
|
|
9032
|
+
params.push(patch.cron);
|
|
9033
|
+
}
|
|
9034
|
+
if ("timezone" in patch) {
|
|
9035
|
+
setClauses.push("timezone = ?");
|
|
9036
|
+
params.push(patch.timezone ?? null);
|
|
9037
|
+
}
|
|
9038
|
+
if ("status" in patch && patch.status !== void 0) {
|
|
9039
|
+
setClauses.push("status = ?");
|
|
9040
|
+
params.push(patch.status);
|
|
9041
|
+
}
|
|
9042
|
+
if ("nextFireAt" in patch && patch.nextFireAt !== void 0) {
|
|
9043
|
+
setClauses.push("next_fire_at = ?");
|
|
9044
|
+
params.push(patch.nextFireAt);
|
|
9045
|
+
}
|
|
9046
|
+
if ("target" in patch && patch.target !== void 0) {
|
|
9047
|
+
setClauses.push("target = jsonb(?)");
|
|
9048
|
+
params.push(JSON.stringify(patch.target));
|
|
9049
|
+
}
|
|
9050
|
+
if ("metadata" in patch) {
|
|
9051
|
+
setClauses.push("metadata = jsonb(?)");
|
|
9052
|
+
params.push(patch.metadata != null ? JSON.stringify(patch.metadata) : null);
|
|
9053
|
+
}
|
|
9054
|
+
setClauses.push("updated_at = ?");
|
|
9055
|
+
params.push(Date.now());
|
|
9056
|
+
params.push(id);
|
|
9057
|
+
if (setClauses.length === 1) {
|
|
9058
|
+
const existing = await this.getSchedule(id);
|
|
9059
|
+
if (!existing) throw new Error(`Schedule ${id} not found`);
|
|
9060
|
+
return existing;
|
|
9061
|
+
}
|
|
9062
|
+
await this.#client.execute({
|
|
9063
|
+
sql: `UPDATE ${TABLE_SCHEDULES} SET ${setClauses.join(", ")} WHERE id = ?`,
|
|
9064
|
+
args: params
|
|
9065
|
+
});
|
|
9066
|
+
const updated = await this.getSchedule(id);
|
|
9067
|
+
if (!updated) throw new Error(`Schedule ${id} not found`);
|
|
9068
|
+
return updated;
|
|
9069
|
+
}
|
|
9070
|
+
async updateScheduleNextFire(id, expectedNextFireAt, newNextFireAt, lastFireAt, lastRunId) {
|
|
9071
|
+
const result = await this.#client.execute({
|
|
9072
|
+
sql: `UPDATE ${TABLE_SCHEDULES}
|
|
9073
|
+
SET next_fire_at = ?, last_fire_at = ?, last_run_id = ?, updated_at = ?
|
|
9074
|
+
WHERE id = ? AND next_fire_at = ? AND status = ?`,
|
|
9075
|
+
args: [newNextFireAt, lastFireAt, lastRunId, Date.now(), id, expectedNextFireAt, "active"]
|
|
9076
|
+
});
|
|
9077
|
+
return (result.rowsAffected ?? 0) > 0;
|
|
9078
|
+
}
|
|
9079
|
+
async deleteSchedule(id) {
|
|
9080
|
+
await this.#client.execute({
|
|
9081
|
+
sql: `DELETE FROM ${TABLE_SCHEDULE_TRIGGERS} WHERE schedule_id = ?`,
|
|
9082
|
+
args: [id]
|
|
9083
|
+
});
|
|
9084
|
+
await this.#client.execute({
|
|
9085
|
+
sql: `DELETE FROM ${TABLE_SCHEDULES} WHERE id = ?`,
|
|
9086
|
+
args: [id]
|
|
9087
|
+
});
|
|
9088
|
+
}
|
|
9089
|
+
async recordTrigger(trigger) {
|
|
9090
|
+
await this.#db.insert({
|
|
9091
|
+
tableName: TABLE_SCHEDULE_TRIGGERS,
|
|
9092
|
+
record: {
|
|
9093
|
+
schedule_id: trigger.scheduleId,
|
|
9094
|
+
run_id: trigger.runId,
|
|
9095
|
+
scheduled_fire_at: trigger.scheduledFireAt,
|
|
9096
|
+
actual_fire_at: trigger.actualFireAt,
|
|
9097
|
+
status: trigger.status,
|
|
9098
|
+
error: trigger.error ?? null
|
|
9099
|
+
}
|
|
9100
|
+
});
|
|
9101
|
+
}
|
|
9102
|
+
async listTriggers(scheduleId, opts) {
|
|
9103
|
+
const conditions = ["schedule_id = ?"];
|
|
9104
|
+
const params = [scheduleId];
|
|
9105
|
+
if (opts?.fromActualFireAt != null) {
|
|
9106
|
+
conditions.push("actual_fire_at >= ?");
|
|
9107
|
+
params.push(opts.fromActualFireAt);
|
|
9108
|
+
}
|
|
9109
|
+
if (opts?.toActualFireAt != null) {
|
|
9110
|
+
conditions.push("actual_fire_at < ?");
|
|
9111
|
+
params.push(opts.toActualFireAt);
|
|
9112
|
+
}
|
|
9113
|
+
const limitClause = opts?.limit != null ? `LIMIT ${Math.floor(opts.limit)}` : "";
|
|
9114
|
+
const result = await this.#client.execute({
|
|
9115
|
+
sql: `SELECT ${buildSelectColumns(TABLE_SCHEDULE_TRIGGERS)} FROM ${TABLE_SCHEDULE_TRIGGERS}
|
|
9116
|
+
WHERE ${conditions.join(" AND ")}
|
|
9117
|
+
ORDER BY actual_fire_at DESC
|
|
9118
|
+
${limitClause}`,
|
|
9119
|
+
args: params
|
|
9120
|
+
});
|
|
9121
|
+
return result.rows.map((r) => rowToTrigger(r));
|
|
9122
|
+
}
|
|
9123
|
+
};
|
|
8328
9124
|
var ScorerDefinitionsLibSQL = class extends ScorerDefinitionsStorage {
|
|
8329
9125
|
#db;
|
|
8330
9126
|
#client;
|
|
@@ -10545,6 +11341,7 @@ var LibSQLStore = class extends MastraCompositeStore {
|
|
|
10545
11341
|
const memory = new MemoryLibSQL(domainConfig);
|
|
10546
11342
|
const observability = new ObservabilityLibSQL(domainConfig);
|
|
10547
11343
|
const agents = new AgentsLibSQL(domainConfig);
|
|
11344
|
+
const channels = new ChannelsLibSQL(domainConfig);
|
|
10548
11345
|
const datasets = new DatasetsLibSQL(domainConfig);
|
|
10549
11346
|
const experiments = new ExperimentsLibSQL(domainConfig);
|
|
10550
11347
|
const promptBlocks = new PromptBlocksLibSQL(domainConfig);
|
|
@@ -10554,12 +11351,15 @@ var LibSQLStore = class extends MastraCompositeStore {
|
|
|
10554
11351
|
const workspaces = new WorkspacesLibSQL(domainConfig);
|
|
10555
11352
|
const skills = new SkillsLibSQL(domainConfig);
|
|
10556
11353
|
const blobs = new BlobsLibSQL(domainConfig);
|
|
11354
|
+
const backgroundTasks = new BackgroundTasksLibSQL(domainConfig);
|
|
11355
|
+
const schedules = new SchedulesLibSQL(domainConfig);
|
|
10557
11356
|
this.stores = {
|
|
10558
11357
|
scores,
|
|
10559
11358
|
workflows,
|
|
10560
11359
|
memory,
|
|
10561
11360
|
observability,
|
|
10562
11361
|
agents,
|
|
11362
|
+
channels,
|
|
10563
11363
|
datasets,
|
|
10564
11364
|
experiments,
|
|
10565
11365
|
promptBlocks,
|
|
@@ -10568,7 +11368,9 @@ var LibSQLStore = class extends MastraCompositeStore {
|
|
|
10568
11368
|
mcpServers,
|
|
10569
11369
|
workspaces,
|
|
10570
11370
|
skills,
|
|
10571
|
-
blobs
|
|
11371
|
+
blobs,
|
|
11372
|
+
backgroundTasks,
|
|
11373
|
+
schedules
|
|
10572
11374
|
};
|
|
10573
11375
|
}
|
|
10574
11376
|
};
|
|
@@ -10672,6 +11474,6 @@ Example Complex Query:
|
|
|
10672
11474
|
]
|
|
10673
11475
|
}`;
|
|
10674
11476
|
|
|
10675
|
-
export { AgentsLibSQL, BlobsLibSQL, DatasetsLibSQL, LibSQLStore as DefaultStorage, ExperimentsLibSQL, LIBSQL_PROMPT, LibSQLStore, LibSQLVector, MCPClientsLibSQL, MCPServersLibSQL, MemoryLibSQL, ObservabilityLibSQL, PromptBlocksLibSQL, ScorerDefinitionsLibSQL, ScoresLibSQL, SkillsLibSQL, WorkflowsLibSQL, WorkspacesLibSQL };
|
|
11477
|
+
export { AgentsLibSQL, BackgroundTasksLibSQL, BlobsLibSQL, ChannelsLibSQL, DatasetsLibSQL, LibSQLStore as DefaultStorage, ExperimentsLibSQL, LIBSQL_PROMPT, LibSQLStore, LibSQLVector, MCPClientsLibSQL, MCPServersLibSQL, MemoryLibSQL, ObservabilityLibSQL, PromptBlocksLibSQL, SchedulesLibSQL, ScorerDefinitionsLibSQL, ScoresLibSQL, SkillsLibSQL, WorkflowsLibSQL, WorkspacesLibSQL };
|
|
10676
11478
|
//# sourceMappingURL=index.js.map
|
|
10677
11479
|
//# sourceMappingURL=index.js.map
|