@mastra/mssql 1.2.0 → 1.2.1-alpha.0
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 +9 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/index.cjs +318 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +319 -5
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/background-tasks/index.d.ts +30 -0
- package/dist/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/storage/index.d.ts +2 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mastra/mssql
|
|
2
2
|
|
|
3
|
+
## 1.2.1-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Add `BackgroundTasksStorage` domain implementation so `@mastra/core` background task execution works with any storage adapter. ([#15307](https://github.com/mastra-ai/mastra/pull/15307))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`d63ffdb`](https://github.com/mastra-ai/mastra/commit/d63ffdbb2c11e76fe5ea45faab44bc15460f010c)]:
|
|
10
|
+
- @mastra/core@1.25.1-alpha.0
|
|
11
|
+
|
|
3
12
|
## 1.2.0
|
|
4
13
|
|
|
5
14
|
### Minor Changes
|
package/dist/docs/SKILL.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
var error = require('@mastra/core/error');
|
|
4
4
|
var storage = require('@mastra/core/storage');
|
|
5
5
|
var sql = require('mssql');
|
|
6
|
-
var agent = require('@mastra/core/agent');
|
|
7
6
|
var base = require('@mastra/core/base');
|
|
8
7
|
var utils = require('@mastra/core/utils');
|
|
8
|
+
var agent = require('@mastra/core/agent');
|
|
9
9
|
var crypto = require('crypto');
|
|
10
10
|
var evals = require('@mastra/core/evals');
|
|
11
11
|
|
|
@@ -1356,7 +1356,319 @@ function transformFromSqlRow({
|
|
|
1356
1356
|
return result;
|
|
1357
1357
|
}
|
|
1358
1358
|
|
|
1359
|
-
// src/storage/domains/
|
|
1359
|
+
// src/storage/domains/background-tasks/index.ts
|
|
1360
|
+
function serializeJson(v) {
|
|
1361
|
+
if (typeof v === "object" && v != null) return JSON.stringify(v);
|
|
1362
|
+
return v ?? null;
|
|
1363
|
+
}
|
|
1364
|
+
function rowToTask(row) {
|
|
1365
|
+
const parseJson = (val) => {
|
|
1366
|
+
if (val == null) return void 0;
|
|
1367
|
+
if (typeof val === "string") {
|
|
1368
|
+
try {
|
|
1369
|
+
return JSON.parse(val);
|
|
1370
|
+
} catch {
|
|
1371
|
+
return val;
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
return val;
|
|
1375
|
+
};
|
|
1376
|
+
return {
|
|
1377
|
+
id: row.id,
|
|
1378
|
+
status: row.status,
|
|
1379
|
+
toolName: row.tool_name,
|
|
1380
|
+
toolCallId: row.tool_call_id,
|
|
1381
|
+
args: parseJson(row.args) ?? {},
|
|
1382
|
+
agentId: row.agent_id,
|
|
1383
|
+
threadId: row.thread_id ?? void 0,
|
|
1384
|
+
resourceId: row.resource_id ?? void 0,
|
|
1385
|
+
runId: row.run_id ?? "",
|
|
1386
|
+
result: parseJson(row.result),
|
|
1387
|
+
error: parseJson(row.error),
|
|
1388
|
+
retryCount: Number(row.retry_count),
|
|
1389
|
+
maxRetries: Number(row.max_retries),
|
|
1390
|
+
timeoutMs: Number(row.timeout_ms),
|
|
1391
|
+
createdAt: row.createdAt instanceof Date ? row.createdAt : new Date(row.createdAt),
|
|
1392
|
+
startedAt: row.startedAt ? row.startedAt instanceof Date ? row.startedAt : new Date(row.startedAt) : void 0,
|
|
1393
|
+
completedAt: row.completedAt ? row.completedAt instanceof Date ? row.completedAt : new Date(row.completedAt) : void 0
|
|
1394
|
+
};
|
|
1395
|
+
}
|
|
1396
|
+
var BackgroundTasksMSSQL = class _BackgroundTasksMSSQL extends storage.BackgroundTasksStorage {
|
|
1397
|
+
pool;
|
|
1398
|
+
db;
|
|
1399
|
+
schema;
|
|
1400
|
+
needsConnect;
|
|
1401
|
+
skipDefaultIndexes;
|
|
1402
|
+
indexes;
|
|
1403
|
+
static MANAGED_TABLES = [storage.TABLE_BACKGROUND_TASKS];
|
|
1404
|
+
constructor(config) {
|
|
1405
|
+
super();
|
|
1406
|
+
const { pool, schemaName, skipDefaultIndexes, indexes, needsConnect } = resolveMssqlConfig(config);
|
|
1407
|
+
this.pool = pool;
|
|
1408
|
+
this.schema = schemaName;
|
|
1409
|
+
this.db = new MssqlDB({ pool, schemaName, skipDefaultIndexes });
|
|
1410
|
+
this.needsConnect = needsConnect;
|
|
1411
|
+
this.skipDefaultIndexes = skipDefaultIndexes;
|
|
1412
|
+
this.indexes = indexes?.filter(
|
|
1413
|
+
(idx) => _BackgroundTasksMSSQL.MANAGED_TABLES.includes(idx.table)
|
|
1414
|
+
);
|
|
1415
|
+
}
|
|
1416
|
+
async init() {
|
|
1417
|
+
if (this.needsConnect) {
|
|
1418
|
+
await this.pool.connect();
|
|
1419
|
+
this.needsConnect = false;
|
|
1420
|
+
}
|
|
1421
|
+
await this.db.createTable({
|
|
1422
|
+
tableName: storage.TABLE_BACKGROUND_TASKS,
|
|
1423
|
+
schema: storage.TABLE_SCHEMAS[storage.TABLE_BACKGROUND_TASKS]
|
|
1424
|
+
});
|
|
1425
|
+
await this.createDefaultIndexes();
|
|
1426
|
+
await this.createCustomIndexes();
|
|
1427
|
+
}
|
|
1428
|
+
getDefaultIndexDefinitions() {
|
|
1429
|
+
const schemaPrefix = this.schema ? `${this.schema}_` : "";
|
|
1430
|
+
return [
|
|
1431
|
+
{
|
|
1432
|
+
name: `${schemaPrefix}mastra_bg_tasks_status_created_at_idx`,
|
|
1433
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
1434
|
+
columns: ["status", "createdAt"]
|
|
1435
|
+
},
|
|
1436
|
+
{
|
|
1437
|
+
name: `${schemaPrefix}mastra_bg_tasks_agent_status_idx`,
|
|
1438
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
1439
|
+
columns: ["agent_id", "status"]
|
|
1440
|
+
},
|
|
1441
|
+
{
|
|
1442
|
+
name: `${schemaPrefix}mastra_bg_tasks_thread_idx`,
|
|
1443
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
1444
|
+
columns: ["thread_id", "createdAt"]
|
|
1445
|
+
},
|
|
1446
|
+
{
|
|
1447
|
+
name: `${schemaPrefix}mastra_bg_tasks_tool_call_idx`,
|
|
1448
|
+
table: storage.TABLE_BACKGROUND_TASKS,
|
|
1449
|
+
columns: ["tool_call_id"]
|
|
1450
|
+
}
|
|
1451
|
+
];
|
|
1452
|
+
}
|
|
1453
|
+
async createDefaultIndexes() {
|
|
1454
|
+
if (this.skipDefaultIndexes) return;
|
|
1455
|
+
for (const indexDef of this.getDefaultIndexDefinitions()) {
|
|
1456
|
+
try {
|
|
1457
|
+
await this.db.createIndex(indexDef);
|
|
1458
|
+
} catch (error) {
|
|
1459
|
+
this.logger?.warn?.(`Failed to create index ${indexDef.name}:`, error);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
async createCustomIndexes() {
|
|
1464
|
+
if (!this.indexes || this.indexes.length === 0) return;
|
|
1465
|
+
for (const indexDef of this.indexes) {
|
|
1466
|
+
try {
|
|
1467
|
+
await this.db.createIndex(indexDef);
|
|
1468
|
+
} catch (error) {
|
|
1469
|
+
this.logger?.warn?.(`Failed to create custom index ${indexDef.name}:`, error);
|
|
1470
|
+
}
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
async dangerouslyClearAll() {
|
|
1474
|
+
await this.db.clearTable({ tableName: storage.TABLE_BACKGROUND_TASKS });
|
|
1475
|
+
}
|
|
1476
|
+
tableName() {
|
|
1477
|
+
return getTableName2({ indexName: storage.TABLE_BACKGROUND_TASKS, schemaName: getSchemaName2(this.schema) });
|
|
1478
|
+
}
|
|
1479
|
+
async createTask(task) {
|
|
1480
|
+
await this.db.insert({
|
|
1481
|
+
tableName: storage.TABLE_BACKGROUND_TASKS,
|
|
1482
|
+
record: {
|
|
1483
|
+
id: task.id,
|
|
1484
|
+
tool_call_id: task.toolCallId,
|
|
1485
|
+
tool_name: task.toolName,
|
|
1486
|
+
agent_id: task.agentId,
|
|
1487
|
+
thread_id: task.threadId ?? null,
|
|
1488
|
+
resource_id: task.resourceId ?? null,
|
|
1489
|
+
run_id: task.runId,
|
|
1490
|
+
status: task.status,
|
|
1491
|
+
args: serializeJson(task.args),
|
|
1492
|
+
result: serializeJson(task.result),
|
|
1493
|
+
error: serializeJson(task.error),
|
|
1494
|
+
retry_count: task.retryCount,
|
|
1495
|
+
max_retries: task.maxRetries,
|
|
1496
|
+
timeout_ms: task.timeoutMs,
|
|
1497
|
+
createdAt: task.createdAt.toISOString(),
|
|
1498
|
+
startedAt: task.startedAt?.toISOString() ?? null,
|
|
1499
|
+
completedAt: task.completedAt?.toISOString() ?? null
|
|
1500
|
+
}
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
async updateTask(taskId, update) {
|
|
1504
|
+
const setClauses = [];
|
|
1505
|
+
const params = {};
|
|
1506
|
+
let idx = 1;
|
|
1507
|
+
if ("status" in update) {
|
|
1508
|
+
setClauses.push(`[status] = @p${idx}`);
|
|
1509
|
+
params[`p${idx++}`] = update.status;
|
|
1510
|
+
}
|
|
1511
|
+
if ("result" in update) {
|
|
1512
|
+
setClauses.push(`[result] = @p${idx}`);
|
|
1513
|
+
params[`p${idx++}`] = serializeJson(update.result);
|
|
1514
|
+
}
|
|
1515
|
+
if ("error" in update) {
|
|
1516
|
+
setClauses.push(`[error] = @p${idx}`);
|
|
1517
|
+
params[`p${idx++}`] = serializeJson(update.error);
|
|
1518
|
+
}
|
|
1519
|
+
if ("retryCount" in update) {
|
|
1520
|
+
setClauses.push(`[retry_count] = @p${idx}`);
|
|
1521
|
+
params[`p${idx++}`] = update.retryCount;
|
|
1522
|
+
}
|
|
1523
|
+
if ("startedAt" in update) {
|
|
1524
|
+
setClauses.push(`[startedAt] = @p${idx}`);
|
|
1525
|
+
params[`p${idx++}`] = update.startedAt?.toISOString() ?? null;
|
|
1526
|
+
}
|
|
1527
|
+
if ("completedAt" in update) {
|
|
1528
|
+
setClauses.push(`[completedAt] = @p${idx}`);
|
|
1529
|
+
params[`p${idx++}`] = update.completedAt?.toISOString() ?? null;
|
|
1530
|
+
}
|
|
1531
|
+
if (setClauses.length === 0) return;
|
|
1532
|
+
setClauses.push(`[id] = [id]`);
|
|
1533
|
+
params[`p${idx}`] = taskId;
|
|
1534
|
+
const request = this.pool.request();
|
|
1535
|
+
for (const [name, value] of Object.entries(params)) {
|
|
1536
|
+
request.input(name, value);
|
|
1537
|
+
}
|
|
1538
|
+
await request.query(`UPDATE ${this.tableName()} SET ${setClauses.join(", ")} WHERE [id] = @p${idx}`);
|
|
1539
|
+
}
|
|
1540
|
+
async getTask(taskId) {
|
|
1541
|
+
const request = this.pool.request();
|
|
1542
|
+
request.input("p1", taskId);
|
|
1543
|
+
const result = await request.query(`SELECT * FROM ${this.tableName()} WHERE [id] = @p1`);
|
|
1544
|
+
if (result.recordset.length === 0) return null;
|
|
1545
|
+
return rowToTask(result.recordset[0]);
|
|
1546
|
+
}
|
|
1547
|
+
async listTasks(filter) {
|
|
1548
|
+
const conditions = [];
|
|
1549
|
+
const params = {};
|
|
1550
|
+
let idx = 1;
|
|
1551
|
+
if (filter.status) {
|
|
1552
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
1553
|
+
const placeholders = statuses.map(() => {
|
|
1554
|
+
const name = `p${idx++}`;
|
|
1555
|
+
return `@${name}`;
|
|
1556
|
+
});
|
|
1557
|
+
statuses.forEach((s, i) => {
|
|
1558
|
+
params[`p${idx - statuses.length + i}`] = s;
|
|
1559
|
+
});
|
|
1560
|
+
conditions.push(`[status] IN (${placeholders.join(", ")})`);
|
|
1561
|
+
}
|
|
1562
|
+
if (filter.agentId) {
|
|
1563
|
+
params[`p${idx}`] = filter.agentId;
|
|
1564
|
+
conditions.push(`[agent_id] = @p${idx++}`);
|
|
1565
|
+
}
|
|
1566
|
+
if (filter.threadId) {
|
|
1567
|
+
params[`p${idx}`] = filter.threadId;
|
|
1568
|
+
conditions.push(`[thread_id] = @p${idx++}`);
|
|
1569
|
+
}
|
|
1570
|
+
if (filter.resourceId) {
|
|
1571
|
+
params[`p${idx}`] = filter.resourceId;
|
|
1572
|
+
conditions.push(`[resource_id] = @p${idx++}`);
|
|
1573
|
+
}
|
|
1574
|
+
if (filter.runId) {
|
|
1575
|
+
params[`p${idx}`] = filter.runId;
|
|
1576
|
+
conditions.push(`[run_id] = @p${idx++}`);
|
|
1577
|
+
}
|
|
1578
|
+
if (filter.toolName) {
|
|
1579
|
+
params[`p${idx}`] = filter.toolName;
|
|
1580
|
+
conditions.push(`[tool_name] = @p${idx++}`);
|
|
1581
|
+
}
|
|
1582
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "[startedAt]" : filter.dateFilterBy === "completedAt" ? "[completedAt]" : "[createdAt]";
|
|
1583
|
+
if (filter.fromDate) {
|
|
1584
|
+
params[`p${idx}`] = filter.fromDate.toISOString();
|
|
1585
|
+
conditions.push(`${dateCol} >= @p${idx++}`);
|
|
1586
|
+
}
|
|
1587
|
+
if (filter.toDate) {
|
|
1588
|
+
params[`p${idx}`] = filter.toDate.toISOString();
|
|
1589
|
+
conditions.push(`${dateCol} < @p${idx++}`);
|
|
1590
|
+
}
|
|
1591
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
1592
|
+
const countRequest = this.pool.request();
|
|
1593
|
+
for (const [name, value] of Object.entries(params)) {
|
|
1594
|
+
countRequest.input(name, value);
|
|
1595
|
+
}
|
|
1596
|
+
const countResult = await countRequest.query(`SELECT COUNT(*) as count FROM ${this.tableName()} ${where}`);
|
|
1597
|
+
const total = Number(countResult.recordset[0]?.count ?? 0);
|
|
1598
|
+
const orderCol = filter.orderBy === "startedAt" ? "[startedAt]" : filter.orderBy === "completedAt" ? "[completedAt]" : "[createdAt]";
|
|
1599
|
+
const direction = filter.orderDirection === "desc" ? "DESC" : "ASC";
|
|
1600
|
+
let sql5 = `SELECT * FROM ${this.tableName()} ${where} ORDER BY ${orderCol} ${direction}`;
|
|
1601
|
+
if (filter.perPage != null) {
|
|
1602
|
+
const offset = filter.page != null ? filter.page * filter.perPage : 0;
|
|
1603
|
+
params[`p${idx}`] = offset;
|
|
1604
|
+
params[`p${idx + 1}`] = filter.perPage;
|
|
1605
|
+
sql5 += ` OFFSET @p${idx} ROWS FETCH NEXT @p${idx + 1} ROWS ONLY`;
|
|
1606
|
+
idx += 2;
|
|
1607
|
+
}
|
|
1608
|
+
const request = this.pool.request();
|
|
1609
|
+
for (const [name, value] of Object.entries(params)) {
|
|
1610
|
+
request.input(name, value);
|
|
1611
|
+
}
|
|
1612
|
+
const result = await request.query(sql5);
|
|
1613
|
+
return { tasks: result.recordset.map(rowToTask), total };
|
|
1614
|
+
}
|
|
1615
|
+
async deleteTask(taskId) {
|
|
1616
|
+
const request = this.pool.request();
|
|
1617
|
+
request.input("p1", taskId);
|
|
1618
|
+
await request.query(`DELETE FROM ${this.tableName()} WHERE [id] = @p1`);
|
|
1619
|
+
}
|
|
1620
|
+
async deleteTasks(filter) {
|
|
1621
|
+
const conditions = [];
|
|
1622
|
+
const params = {};
|
|
1623
|
+
let idx = 1;
|
|
1624
|
+
if (filter.status) {
|
|
1625
|
+
const statuses = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
1626
|
+
const placeholders = statuses.map(() => {
|
|
1627
|
+
const name = `p${idx++}`;
|
|
1628
|
+
return `@${name}`;
|
|
1629
|
+
});
|
|
1630
|
+
statuses.forEach((s, i) => {
|
|
1631
|
+
params[`p${idx - statuses.length + i}`] = s;
|
|
1632
|
+
});
|
|
1633
|
+
conditions.push(`[status] IN (${placeholders.join(", ")})`);
|
|
1634
|
+
}
|
|
1635
|
+
const dateCol = filter.dateFilterBy === "startedAt" ? "[startedAt]" : filter.dateFilterBy === "completedAt" ? "[completedAt]" : "[createdAt]";
|
|
1636
|
+
if (filter.fromDate) {
|
|
1637
|
+
params[`p${idx}`] = filter.fromDate.toISOString();
|
|
1638
|
+
conditions.push(`${dateCol} >= @p${idx++}`);
|
|
1639
|
+
}
|
|
1640
|
+
if (filter.toDate) {
|
|
1641
|
+
params[`p${idx}`] = filter.toDate.toISOString();
|
|
1642
|
+
conditions.push(`${dateCol} < @p${idx++}`);
|
|
1643
|
+
}
|
|
1644
|
+
if (filter.agentId) {
|
|
1645
|
+
params[`p${idx}`] = filter.agentId;
|
|
1646
|
+
conditions.push(`[agent_id] = @p${idx++}`);
|
|
1647
|
+
}
|
|
1648
|
+
if (filter.runId) {
|
|
1649
|
+
params[`p${idx}`] = filter.runId;
|
|
1650
|
+
conditions.push(`[run_id] = @p${idx++}`);
|
|
1651
|
+
}
|
|
1652
|
+
if (conditions.length === 0) return;
|
|
1653
|
+
const request = this.pool.request();
|
|
1654
|
+
for (const [name, value] of Object.entries(params)) {
|
|
1655
|
+
request.input(name, value);
|
|
1656
|
+
}
|
|
1657
|
+
await request.query(`DELETE FROM ${this.tableName()} WHERE ${conditions.join(" AND ")}`);
|
|
1658
|
+
}
|
|
1659
|
+
async getRunningCount() {
|
|
1660
|
+
const result = await this.pool.request().query(`SELECT COUNT(*) as count FROM ${this.tableName()} WHERE [status] = 'running'`);
|
|
1661
|
+
return Number(result.recordset[0]?.count ?? 0);
|
|
1662
|
+
}
|
|
1663
|
+
async getRunningCountByAgent(agentId) {
|
|
1664
|
+
const request = this.pool.request();
|
|
1665
|
+
request.input("p1", agentId);
|
|
1666
|
+
const result = await request.query(
|
|
1667
|
+
`SELECT COUNT(*) as count FROM ${this.tableName()} WHERE [status] = 'running' AND [agent_id] = @p1`
|
|
1668
|
+
);
|
|
1669
|
+
return Number(result.recordset[0]?.count ?? 0);
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1360
1672
|
var MemoryMSSQL = class _MemoryMSSQL extends storage.MemoryStorage {
|
|
1361
1673
|
pool;
|
|
1362
1674
|
schema;
|
|
@@ -3994,11 +4306,13 @@ var MSSQLStore = class extends storage.MastraCompositeStore {
|
|
|
3994
4306
|
const workflows = new WorkflowsMSSQL(domainConfig);
|
|
3995
4307
|
const memory = new MemoryMSSQL(domainConfig);
|
|
3996
4308
|
const observability = new ObservabilityMSSQL(domainConfig);
|
|
4309
|
+
const backgroundTasks = new BackgroundTasksMSSQL(domainConfig);
|
|
3997
4310
|
this.stores = {
|
|
3998
4311
|
scores,
|
|
3999
4312
|
workflows,
|
|
4000
4313
|
memory,
|
|
4001
|
-
observability
|
|
4314
|
+
observability,
|
|
4315
|
+
backgroundTasks
|
|
4002
4316
|
};
|
|
4003
4317
|
} catch (e) {
|
|
4004
4318
|
throw new error.MastraError(
|
|
@@ -4051,6 +4365,7 @@ var MSSQLStore = class extends storage.MastraCompositeStore {
|
|
|
4051
4365
|
}
|
|
4052
4366
|
};
|
|
4053
4367
|
|
|
4368
|
+
exports.BackgroundTasksMSSQL = BackgroundTasksMSSQL;
|
|
4054
4369
|
exports.MSSQLStore = MSSQLStore;
|
|
4055
4370
|
exports.MemoryMSSQL = MemoryMSSQL;
|
|
4056
4371
|
exports.ObservabilityMSSQL = ObservabilityMSSQL;
|