@mastra/libsql 1.25.1 → 1.25.2-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/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-agents-human-in-the-loop.md +5 -3
- package/dist/docs/references/docs-memory-memory-processors.md +2 -5
- package/dist/docs/references/docs-memory-semantic-recall.md +2 -2
- package/dist/docs/references/docs-memory-working-memory.md +16 -0
- package/dist/docs/references/reference-memory-memory-class.md +2 -2
- package/dist/index.cjs +147 -133
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +147 -133
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/docs/SKILL.md
CHANGED
|
@@ -125,7 +125,7 @@ import { agent } from './agent'
|
|
|
125
125
|
|
|
126
126
|
const approvalClassifier = new Classifier({
|
|
127
127
|
id: 'tool-approval-classifier',
|
|
128
|
-
model: typeSafeAi.
|
|
128
|
+
model: typeSafeAi.decisionModel('jev-latest'),
|
|
129
129
|
questions: {
|
|
130
130
|
requiresApproval: {
|
|
131
131
|
type: 'boolean',
|
|
@@ -137,7 +137,7 @@ const approvalClassifier = new Classifier({
|
|
|
137
137
|
|
|
138
138
|
const stream = await agent.stream('Clean up old records', {
|
|
139
139
|
requireToolApproval: async ({ toolName, args }) => {
|
|
140
|
-
const result = await approvalClassifier.
|
|
140
|
+
const result = await approvalClassifier.decide({
|
|
141
141
|
state: {
|
|
142
142
|
toolName,
|
|
143
143
|
argumentNames: Object.keys(args),
|
|
@@ -149,7 +149,7 @@ const stream = await agent.stream('Clean up old records', {
|
|
|
149
149
|
})
|
|
150
150
|
```
|
|
151
151
|
|
|
152
|
-
The example sends argument names rather than values because the classifier forwards its state to the configured
|
|
152
|
+
The example sends argument names rather than values because the classifier forwards its state to the configured decision model. If the decision requires argument values, redact sensitive data first or use a decision model and provider that meet the protected tool's data-handling requirements.
|
|
153
153
|
|
|
154
154
|
Returning `true` pauses the tool call for human approval. It doesn't approve or decline the call automatically. Choose a threshold that matches the risk of the tools available to the agent. If this `requireToolApproval` function throws, it defaults to requiring approval.
|
|
155
155
|
|
|
@@ -260,6 +260,8 @@ const weatherTool = createTool({
|
|
|
260
260
|
|
|
261
261
|
> **Note:** `suspend()` doesn't throw, return immediately after calling it (e.g. `return await suspend({ ... })`). Code after `await suspend(...)` still runs before the tool pauses.
|
|
262
262
|
|
|
263
|
+
Use `suspend()` instead of a tool without `execute` when the tool needs a structured answer from the user and also runs inside a subagent: subagents can't run client tools, but suspension propagates through the supervisor. To resume from AI SDK `useChat()`, see [resuming a suspended tool from `useChat()`](https://mastra.ai/integrations/agentic-ui/ai-sdk-ui).
|
|
264
|
+
|
|
263
265
|
## Tool approval with `generate()`
|
|
264
266
|
|
|
265
267
|
Tool approval also works with `generate()` for non-streaming use cases. When a tool requires approval, `generate()` returns immediately with `finishReason: 'suspended'`, a `suspendPayload` containing the tool call details (`toolCallId`, `toolName`, `args`), and a `runId`:
|
|
@@ -99,7 +99,7 @@ import { Agent } from '@mastra/core/agent'
|
|
|
99
99
|
import { Memory } from '@mastra/memory'
|
|
100
100
|
import { LibSQLStore } from '@mastra/libsql'
|
|
101
101
|
import { PineconeVector } from '@mastra/pinecone'
|
|
102
|
-
import {
|
|
102
|
+
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
103
103
|
import { openai } from '@ai-sdk/openai'
|
|
104
104
|
|
|
105
105
|
const agent = new Agent({
|
|
@@ -115,10 +115,7 @@ const agent = new Agent({
|
|
|
115
115
|
id: 'memory-vector',
|
|
116
116
|
apiKey: process.env.PINECONE_API_KEY!,
|
|
117
117
|
}),
|
|
118
|
-
embedder: new
|
|
119
|
-
model: 'text-embedding-3-small',
|
|
120
|
-
apiKey: process.env.OPENAI_API_KEY!,
|
|
121
|
-
}),
|
|
118
|
+
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
|
|
122
119
|
semanticRecall: { enabled: true }, // SemanticRecall processor automatically added
|
|
123
120
|
}),
|
|
124
121
|
})
|
|
@@ -411,11 +411,11 @@ PostgreSQL supports both IVFFlat and HNSW indexes. By default, Mastra creates an
|
|
|
411
411
|
|
|
412
412
|
```typescript
|
|
413
413
|
import { Memory } from '@mastra/memory'
|
|
414
|
-
import {
|
|
414
|
+
import { PostgresStore, PgVector } from '@mastra/pg'
|
|
415
415
|
|
|
416
416
|
const agent = new Agent({
|
|
417
417
|
memory: new Memory({
|
|
418
|
-
storage: new
|
|
418
|
+
storage: new PostgresStore({
|
|
419
419
|
id: 'agent-storage',
|
|
420
420
|
connectionString: process.env.DATABASE_URL,
|
|
421
421
|
}),
|
|
@@ -272,6 +272,22 @@ Schema-based working memory uses **merge semantics**, meaning the agent only nee
|
|
|
272
272
|
- **Set a field to `null` to delete it:** This explicitly removes the field from memory
|
|
273
273
|
- **Arrays are replaced entirely:** When an array field is provided, it replaces the existing array (arrays aren't merged element-by-element)
|
|
274
274
|
|
|
275
|
+
### Concurrent updates
|
|
276
|
+
|
|
277
|
+
With `scope: 'resource'` and a storage adapter that supports atomic merges (currently `@mastra/pg`), each merge runs inside a row-locked database transaction. Concurrent updates to different fields from separate processes are all kept. Other adapters merge in the application, which only serializes writers inside a single process.
|
|
278
|
+
|
|
279
|
+
To merge a partial update from your own code, call `memory.mergeWorkingMemory()`:
|
|
280
|
+
|
|
281
|
+
```typescript
|
|
282
|
+
await memory.mergeWorkingMemory({
|
|
283
|
+
threadId: 'thread-1',
|
|
284
|
+
resourceId: 'user-123',
|
|
285
|
+
workingMemory: { preferences: { tone: 'formal' } },
|
|
286
|
+
})
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
It throws if working memory isn't schema-based and resource-scoped, or if the storage adapter doesn't support atomic merges. Check `await memory.supportsAtomicWorkingMemoryMerge()` first if you need to fall back.
|
|
290
|
+
|
|
275
291
|
## Choosing between template and schema
|
|
276
292
|
|
|
277
293
|
- Use a **template** (Markdown) if you want the agent to maintain memory as a free-form text block, such as a user profile or scratchpad. Templates use **replace semantics**: the agent must provide the complete memory content on each update.
|
|
@@ -101,14 +101,14 @@ export const agent = new Agent({
|
|
|
101
101
|
import { Memory } from '@mastra/memory'
|
|
102
102
|
import { Agent } from '@mastra/core/agent'
|
|
103
103
|
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
|
|
104
|
-
import {
|
|
104
|
+
import { PostgresStore, PgVector } from '@mastra/pg'
|
|
105
105
|
|
|
106
106
|
export const agent = new Agent({
|
|
107
107
|
name: 'pg-agent',
|
|
108
108
|
instructions: 'You are an agent with optimized PostgreSQL memory.',
|
|
109
109
|
model: 'openai/gpt-5.6-sol',
|
|
110
110
|
memory: new Memory({
|
|
111
|
-
storage: new
|
|
111
|
+
storage: new PostgresStore({
|
|
112
112
|
id: 'pg-agent-storage',
|
|
113
113
|
connectionString: process.env.DATABASE_URL,
|
|
114
114
|
}),
|
package/dist/index.cjs
CHANGED
|
@@ -7862,6 +7862,14 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
7862
7862
|
initialBackoffMs: config.initialBackoffMs
|
|
7863
7863
|
});
|
|
7864
7864
|
}
|
|
7865
|
+
/**
|
|
7866
|
+
* Runs a write behind the per-client write lock and retries it on transient SQLite lock
|
|
7867
|
+
* errors (`SQLITE_BUSY`), like the shared `LibSQLDB` writes. Each retry re-queues on the lock.
|
|
7868
|
+
* `fn` must be safe to rerun: a single statement, a batch, or a transaction that rolls back.
|
|
7869
|
+
*/
|
|
7870
|
+
#write(operation, fn) {
|
|
7871
|
+
return this.#db.executeWriteOperationWithRetry(() => withClientWriteLock(this.#client, fn), operation);
|
|
7872
|
+
}
|
|
7865
7873
|
async init() {
|
|
7866
7874
|
await this.#db.createTable({
|
|
7867
7875
|
tableName: _mastra_core_storage.TABLE_THREADS,
|
|
@@ -8363,9 +8371,9 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8363
8371
|
const threadUpdateStatement = batchStatements[batchStatements.length - 1];
|
|
8364
8372
|
for (let i = 0; i < messageStatements.length; i += BATCH_SIZE) {
|
|
8365
8373
|
const batch = messageStatements.slice(i, i + BATCH_SIZE);
|
|
8366
|
-
if (batch.length > 0) await this.#client.batch(batch, "write");
|
|
8374
|
+
if (batch.length > 0) await this.#write("saveMessages", () => this.#client.batch(batch, "write"));
|
|
8367
8375
|
}
|
|
8368
|
-
if (threadUpdateStatement) await this.#client.execute(threadUpdateStatement);
|
|
8376
|
+
if (threadUpdateStatement) await this.#write("saveMessages", () => this.#client.execute(threadUpdateStatement));
|
|
8369
8377
|
return { messages: new _mastra_core_agent.MessageList().add(messages, "memory").get.all.db() };
|
|
8370
8378
|
} catch (error) {
|
|
8371
8379
|
throw new _mastra_core_error.MastraError({
|
|
@@ -8431,7 +8439,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8431
8439
|
sql: `UPDATE ${_mastra_core_storage.TABLE_THREADS} SET updatedAt = ? WHERE id = ?`,
|
|
8432
8440
|
args: [now, threadId]
|
|
8433
8441
|
});
|
|
8434
|
-
await this.#client.batch(batchStatements, "write");
|
|
8442
|
+
await this.#write("updateMessages", () => this.#client.batch(batchStatements, "write"));
|
|
8435
8443
|
return (await this.#client.execute({
|
|
8436
8444
|
sql: selectSql,
|
|
8437
8445
|
args: messageIds
|
|
@@ -8441,35 +8449,37 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8441
8449
|
if (!messageIds || messageIds.length === 0) return;
|
|
8442
8450
|
try {
|
|
8443
8451
|
const BATCH_SIZE = 100;
|
|
8444
|
-
|
|
8445
|
-
|
|
8446
|
-
|
|
8447
|
-
|
|
8448
|
-
|
|
8449
|
-
|
|
8450
|
-
|
|
8451
|
-
|
|
8452
|
-
|
|
8453
|
-
|
|
8454
|
-
|
|
8455
|
-
|
|
8456
|
-
|
|
8457
|
-
|
|
8458
|
-
|
|
8459
|
-
|
|
8460
|
-
|
|
8461
|
-
|
|
8462
|
-
|
|
8463
|
-
|
|
8464
|
-
|
|
8465
|
-
|
|
8466
|
-
|
|
8452
|
+
await this.#write("deleteMessages", async () => {
|
|
8453
|
+
const threadIds = /* @__PURE__ */ new Set();
|
|
8454
|
+
const tx = await this.#client.transaction("write");
|
|
8455
|
+
try {
|
|
8456
|
+
for (let i = 0; i < messageIds.length; i += BATCH_SIZE) {
|
|
8457
|
+
const batch = messageIds.slice(i, i + BATCH_SIZE);
|
|
8458
|
+
const placeholders = batch.map(() => "?").join(",");
|
|
8459
|
+
(await tx.execute({
|
|
8460
|
+
sql: `SELECT DISTINCT thread_id FROM "${_mastra_core_storage.TABLE_MESSAGES}" WHERE id IN (${placeholders})`,
|
|
8461
|
+
args: batch
|
|
8462
|
+
})).rows?.forEach((row) => {
|
|
8463
|
+
if (row.thread_id) threadIds.add(row.thread_id);
|
|
8464
|
+
});
|
|
8465
|
+
await tx.execute({
|
|
8466
|
+
sql: `DELETE FROM "${_mastra_core_storage.TABLE_MESSAGES}" WHERE id IN (${placeholders})`,
|
|
8467
|
+
args: batch
|
|
8468
|
+
});
|
|
8469
|
+
}
|
|
8470
|
+
if (threadIds.size > 0) {
|
|
8471
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
8472
|
+
for (const threadId of threadIds) await tx.execute({
|
|
8473
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_THREADS}" SET "updatedAt" = ? WHERE id = ?`,
|
|
8474
|
+
args: [now, threadId]
|
|
8475
|
+
});
|
|
8476
|
+
}
|
|
8477
|
+
await tx.commit();
|
|
8478
|
+
} catch (error) {
|
|
8479
|
+
await tx.rollback();
|
|
8480
|
+
throw error;
|
|
8467
8481
|
}
|
|
8468
|
-
|
|
8469
|
-
} catch (error) {
|
|
8470
|
-
await tx.rollback();
|
|
8471
|
-
throw error;
|
|
8472
|
-
}
|
|
8482
|
+
});
|
|
8473
8483
|
} catch (error) {
|
|
8474
8484
|
throw new _mastra_core_error.MastraError({
|
|
8475
8485
|
id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "DELETE_MESSAGES", "FAILED"),
|
|
@@ -8534,10 +8544,10 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8534
8544
|
updates.push("updatedAt = ?");
|
|
8535
8545
|
values.push(updatedResource.updatedAt.toISOString());
|
|
8536
8546
|
values.push(resourceId);
|
|
8537
|
-
await this.#client.execute({
|
|
8547
|
+
await this.#write("updateResource", () => this.#client.execute({
|
|
8538
8548
|
sql: `UPDATE ${_mastra_core_storage.TABLE_RESOURCES} SET ${updates.join(", ")} WHERE id = ?`,
|
|
8539
8549
|
args: values
|
|
8540
|
-
});
|
|
8550
|
+
}));
|
|
8541
8551
|
return updatedResource;
|
|
8542
8552
|
}
|
|
8543
8553
|
async getThreadById({ threadId, resourceId }) {
|
|
@@ -8574,49 +8584,51 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8574
8584
|
*/
|
|
8575
8585
|
async updateThreadResourceId({ threadId, resourceId }) {
|
|
8576
8586
|
try {
|
|
8577
|
-
|
|
8578
|
-
|
|
8579
|
-
|
|
8580
|
-
|
|
8581
|
-
|
|
8582
|
-
|
|
8583
|
-
|
|
8584
|
-
|
|
8585
|
-
|
|
8586
|
-
|
|
8587
|
-
|
|
8588
|
-
|
|
8589
|
-
|
|
8590
|
-
|
|
8591
|
-
|
|
8592
|
-
|
|
8593
|
-
|
|
8587
|
+
return await this.#write("updateThreadResourceId", async () => {
|
|
8588
|
+
const tx = await this.#client.transaction("write");
|
|
8589
|
+
try {
|
|
8590
|
+
const row = (await tx.execute({
|
|
8591
|
+
sql: `SELECT * FROM "${_mastra_core_storage.TABLE_THREADS}" WHERE id = ?`,
|
|
8592
|
+
args: [threadId]
|
|
8593
|
+
})).rows?.[0];
|
|
8594
|
+
if (!row) throw new Error(`Thread "${threadId}" not found`);
|
|
8595
|
+
const currentResourceId = row.resourceId;
|
|
8596
|
+
const normalized = {
|
|
8597
|
+
id: row.id,
|
|
8598
|
+
resourceId: currentResourceId,
|
|
8599
|
+
title: row.title,
|
|
8600
|
+
metadata: typeof row.metadata === "string" ? JSON.parse(row.metadata) : row.metadata,
|
|
8601
|
+
createdAt: new Date(row.createdAt),
|
|
8602
|
+
updatedAt: new Date(row.updatedAt)
|
|
8603
|
+
};
|
|
8604
|
+
if (currentResourceId === resourceId) {
|
|
8605
|
+
await tx.commit();
|
|
8606
|
+
return normalized;
|
|
8607
|
+
}
|
|
8608
|
+
const now = /* @__PURE__ */ new Date();
|
|
8609
|
+
await tx.execute({
|
|
8610
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_THREADS}" SET "resourceId" = ?, "updatedAt" = ? WHERE id = ?`,
|
|
8611
|
+
args: [
|
|
8612
|
+
resourceId,
|
|
8613
|
+
now.toISOString(),
|
|
8614
|
+
threadId
|
|
8615
|
+
]
|
|
8616
|
+
});
|
|
8617
|
+
await tx.execute({
|
|
8618
|
+
sql: `UPDATE "${_mastra_core_storage.TABLE_MESSAGES}" SET "resourceId" = ? WHERE thread_id = ?`,
|
|
8619
|
+
args: [resourceId, threadId]
|
|
8620
|
+
});
|
|
8594
8621
|
await tx.commit();
|
|
8595
|
-
return
|
|
8596
|
-
|
|
8597
|
-
const now = /* @__PURE__ */ new Date();
|
|
8598
|
-
await tx.execute({
|
|
8599
|
-
sql: `UPDATE "${_mastra_core_storage.TABLE_THREADS}" SET "resourceId" = ?, "updatedAt" = ? WHERE id = ?`,
|
|
8600
|
-
args: [
|
|
8622
|
+
return {
|
|
8623
|
+
...normalized,
|
|
8601
8624
|
resourceId,
|
|
8602
|
-
now
|
|
8603
|
-
|
|
8604
|
-
|
|
8605
|
-
|
|
8606
|
-
|
|
8607
|
-
|
|
8608
|
-
|
|
8609
|
-
});
|
|
8610
|
-
await tx.commit();
|
|
8611
|
-
return {
|
|
8612
|
-
...normalized,
|
|
8613
|
-
resourceId,
|
|
8614
|
-
updatedAt: now
|
|
8615
|
-
};
|
|
8616
|
-
} catch (error) {
|
|
8617
|
-
await tx.rollback();
|
|
8618
|
-
throw error;
|
|
8619
|
-
}
|
|
8625
|
+
updatedAt: now
|
|
8626
|
+
};
|
|
8627
|
+
} catch (error) {
|
|
8628
|
+
await tx.rollback();
|
|
8629
|
+
throw error;
|
|
8630
|
+
}
|
|
8631
|
+
});
|
|
8620
8632
|
} catch (error) {
|
|
8621
8633
|
throw new _mastra_core_error.MastraError({
|
|
8622
8634
|
id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "UPDATE_THREAD_RESOURCE_ID", "FAILED"),
|
|
@@ -8778,7 +8790,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8778
8790
|
updatedAt: now
|
|
8779
8791
|
};
|
|
8780
8792
|
try {
|
|
8781
|
-
await this.#client.execute({
|
|
8793
|
+
await this.#write("updateThread", () => this.#client.execute({
|
|
8782
8794
|
sql: `UPDATE ${_mastra_core_storage.TABLE_THREADS} SET title = COALESCE(?, title), metadata = jsonb(?), updatedAt = ? WHERE id = ?`,
|
|
8783
8795
|
args: [
|
|
8784
8796
|
title ?? null,
|
|
@@ -8786,7 +8798,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8786
8798
|
now.toISOString(),
|
|
8787
8799
|
id
|
|
8788
8800
|
]
|
|
8789
|
-
});
|
|
8801
|
+
}));
|
|
8790
8802
|
return updatedThread;
|
|
8791
8803
|
} catch (error) {
|
|
8792
8804
|
throw new _mastra_core_error.MastraError({
|
|
@@ -8800,14 +8812,14 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8800
8812
|
}
|
|
8801
8813
|
async deleteThread({ threadId }) {
|
|
8802
8814
|
try {
|
|
8803
|
-
await this.#client.execute({
|
|
8815
|
+
await this.#write("deleteThread", () => this.#client.execute({
|
|
8804
8816
|
sql: `DELETE FROM ${_mastra_core_storage.TABLE_MESSAGES} WHERE thread_id = ?`,
|
|
8805
8817
|
args: [threadId]
|
|
8806
|
-
});
|
|
8807
|
-
await this.#client.execute({
|
|
8818
|
+
}));
|
|
8819
|
+
await this.#write("deleteThread", () => this.#client.execute({
|
|
8808
8820
|
sql: `DELETE FROM ${_mastra_core_storage.TABLE_THREADS} WHERE id = ?`,
|
|
8809
8821
|
args: [threadId]
|
|
8810
|
-
});
|
|
8822
|
+
}));
|
|
8811
8823
|
} catch (error) {
|
|
8812
8824
|
throw new _mastra_core_error.MastraError({
|
|
8813
8825
|
id: (0, _mastra_core_storage.createStorageErrorId)("LIBSQL", "DELETE_THREAD", "FAILED"),
|
|
@@ -8880,48 +8892,50 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
8880
8892
|
createdAt: now,
|
|
8881
8893
|
updatedAt: now
|
|
8882
8894
|
};
|
|
8883
|
-
|
|
8884
|
-
|
|
8885
|
-
|
|
8886
|
-
|
|
8887
|
-
|
|
8888
|
-
|
|
8889
|
-
newThread.id,
|
|
8890
|
-
newThread.resourceId,
|
|
8891
|
-
newThread.title ?? "",
|
|
8892
|
-
JSON.stringify(newThread.metadata),
|
|
8893
|
-
nowStr,
|
|
8894
|
-
nowStr
|
|
8895
|
-
]
|
|
8896
|
-
});
|
|
8897
|
-
const messageIdMap = {};
|
|
8898
|
-
const targetResourceId = resourceId || sourceThread.resourceId;
|
|
8899
|
-
for (const sourceMsg of sourceMessages) {
|
|
8900
|
-
const newMessageId = crypto.randomUUID();
|
|
8901
|
-
const sourceMsgId = sourceMsg.id;
|
|
8902
|
-
messageIdMap[sourceMsgId] = newMessageId;
|
|
8903
|
-
const insertResult = await tx.execute({
|
|
8904
|
-
sql: `INSERT INTO "${_mastra_core_storage.TABLE_MESSAGES}" (id, thread_id, content, role, type, "createdAt", "resourceId")
|
|
8905
|
-
SELECT ?, ?, content, role, type, "createdAt", ?
|
|
8906
|
-
FROM "${_mastra_core_storage.TABLE_MESSAGES}" WHERE id = ?`,
|
|
8895
|
+
return await this.#write("copyThread", async () => {
|
|
8896
|
+
const tx = await this.#client.transaction("write");
|
|
8897
|
+
try {
|
|
8898
|
+
await tx.execute({
|
|
8899
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_THREADS}" (id, "resourceId", title, metadata, "createdAt", "updatedAt")
|
|
8900
|
+
VALUES (?, ?, ?, jsonb(?), ?, ?)`,
|
|
8907
8901
|
args: [
|
|
8908
|
-
|
|
8909
|
-
|
|
8910
|
-
|
|
8911
|
-
|
|
8902
|
+
newThread.id,
|
|
8903
|
+
newThread.resourceId,
|
|
8904
|
+
newThread.title ?? "",
|
|
8905
|
+
JSON.stringify(newThread.metadata),
|
|
8906
|
+
nowStr,
|
|
8907
|
+
nowStr
|
|
8912
8908
|
]
|
|
8913
8909
|
});
|
|
8914
|
-
|
|
8910
|
+
const messageIdMap = {};
|
|
8911
|
+
const targetResourceId = resourceId || sourceThread.resourceId;
|
|
8912
|
+
for (const sourceMsg of sourceMessages) {
|
|
8913
|
+
const newMessageId = crypto.randomUUID();
|
|
8914
|
+
const sourceMsgId = sourceMsg.id;
|
|
8915
|
+
messageIdMap[sourceMsgId] = newMessageId;
|
|
8916
|
+
const insertResult = await tx.execute({
|
|
8917
|
+
sql: `INSERT INTO "${_mastra_core_storage.TABLE_MESSAGES}" (id, thread_id, content, role, type, "createdAt", "resourceId")
|
|
8918
|
+
SELECT ?, ?, content, role, type, "createdAt", ?
|
|
8919
|
+
FROM "${_mastra_core_storage.TABLE_MESSAGES}" WHERE id = ?`,
|
|
8920
|
+
args: [
|
|
8921
|
+
newMessageId,
|
|
8922
|
+
newThreadId,
|
|
8923
|
+
targetResourceId,
|
|
8924
|
+
sourceMsgId
|
|
8925
|
+
]
|
|
8926
|
+
});
|
|
8927
|
+
if (insertResult.rowsAffected !== 1) throw new Error(`Failed to copy message ${sourceMsgId}: expected 1 row copied but got ${insertResult.rowsAffected}`);
|
|
8928
|
+
}
|
|
8929
|
+
await tx.commit();
|
|
8930
|
+
return {
|
|
8931
|
+
thread: newThread,
|
|
8932
|
+
messageIdMap
|
|
8933
|
+
};
|
|
8934
|
+
} catch (error) {
|
|
8935
|
+
await tx.rollback();
|
|
8936
|
+
throw error;
|
|
8915
8937
|
}
|
|
8916
|
-
|
|
8917
|
-
return {
|
|
8918
|
-
thread: newThread,
|
|
8919
|
-
messageIdMap
|
|
8920
|
-
};
|
|
8921
|
-
} catch (error) {
|
|
8922
|
-
await tx.rollback();
|
|
8923
|
-
throw error;
|
|
8924
|
-
}
|
|
8938
|
+
});
|
|
8925
8939
|
} catch (error) {
|
|
8926
8940
|
if (error instanceof _mastra_core_error.MastraError) throw error;
|
|
8927
8941
|
throw new _mastra_core_error.MastraError({
|
|
@@ -9081,7 +9095,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9081
9095
|
config: input.config,
|
|
9082
9096
|
observedTimezone: input.observedTimezone
|
|
9083
9097
|
};
|
|
9084
|
-
await
|
|
9098
|
+
await this.#write("initializeObservationalMemory", () => this.#client.execute({
|
|
9085
9099
|
sql: `INSERT INTO "${OM_TABLE}" (
|
|
9086
9100
|
id, "lookupKey", scope, "resourceId", "threadId",
|
|
9087
9101
|
"activeObservations", "activeObservationsPendingUpdate",
|
|
@@ -9133,7 +9147,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9133
9147
|
async insertObservationalMemoryRecord(record) {
|
|
9134
9148
|
try {
|
|
9135
9149
|
const lookupKey = this.getOMKey(record.threadId, record.resourceId);
|
|
9136
|
-
await
|
|
9150
|
+
await this.#write("insertObservationalMemoryRecord", () => this.#client.execute({
|
|
9137
9151
|
sql: `INSERT INTO "${OM_TABLE}" (
|
|
9138
9152
|
id, "lookupKey", scope, "resourceId", "threadId",
|
|
9139
9153
|
"activeObservations", "activeObservationsPendingUpdate",
|
|
@@ -9197,7 +9211,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9197
9211
|
try {
|
|
9198
9212
|
const now = /* @__PURE__ */ new Date();
|
|
9199
9213
|
const observedMessageIdsJson = input.observedMessageIds ? JSON.stringify(input.observedMessageIds) : null;
|
|
9200
|
-
if ((await
|
|
9214
|
+
if ((await this.#write("updateActiveObservations", () => this.#client.execute({
|
|
9201
9215
|
sql: `UPDATE "${OM_TABLE}" SET
|
|
9202
9216
|
"activeObservations" = ?,
|
|
9203
9217
|
"lastObservedAt" = ?,
|
|
@@ -9235,7 +9249,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9235
9249
|
}
|
|
9236
9250
|
async createReflectionGeneration(input) {
|
|
9237
9251
|
try {
|
|
9238
|
-
return await
|
|
9252
|
+
return await this.#write("createReflectionGeneration", async () => {
|
|
9239
9253
|
const tx = await this.#client.transaction("write");
|
|
9240
9254
|
try {
|
|
9241
9255
|
const record = await this.#insertReflectionGeneration(tx, input);
|
|
@@ -9331,7 +9345,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9331
9345
|
}
|
|
9332
9346
|
async setReflectingFlag(id, isReflecting) {
|
|
9333
9347
|
try {
|
|
9334
|
-
if ((await
|
|
9348
|
+
if ((await this.#write("setReflectingFlag", () => this.#client.execute({
|
|
9335
9349
|
sql: `UPDATE "${OM_TABLE}" SET "isReflecting" = ?, "updatedAt" = ? WHERE id = ?`,
|
|
9336
9350
|
args: [
|
|
9337
9351
|
isReflecting,
|
|
@@ -9363,7 +9377,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9363
9377
|
}
|
|
9364
9378
|
async setObservingFlag(id, isObserving) {
|
|
9365
9379
|
try {
|
|
9366
|
-
if ((await
|
|
9380
|
+
if ((await this.#write("setObservingFlag", () => this.#client.execute({
|
|
9367
9381
|
sql: `UPDATE "${OM_TABLE}" SET "isObserving" = ?, "updatedAt" = ? WHERE id = ?`,
|
|
9368
9382
|
args: [
|
|
9369
9383
|
isObserving,
|
|
@@ -9414,7 +9428,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9414
9428
|
id
|
|
9415
9429
|
];
|
|
9416
9430
|
}
|
|
9417
|
-
if ((await
|
|
9431
|
+
if ((await this.#write("setBufferingObservationFlag", () => this.#client.execute({
|
|
9418
9432
|
sql,
|
|
9419
9433
|
args
|
|
9420
9434
|
}))).rowsAffected === 0) throw new _mastra_core_error.MastraError({
|
|
@@ -9444,7 +9458,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9444
9458
|
}
|
|
9445
9459
|
async setBufferingReflectionFlag(id, isBuffering) {
|
|
9446
9460
|
try {
|
|
9447
|
-
if ((await
|
|
9461
|
+
if ((await this.#write("setBufferingReflectionFlag", () => this.#client.execute({
|
|
9448
9462
|
sql: `UPDATE "${OM_TABLE}" SET "isBufferingReflection" = ?, "updatedAt" = ? WHERE id = ?`,
|
|
9449
9463
|
args: [
|
|
9450
9464
|
isBuffering,
|
|
@@ -9477,7 +9491,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9477
9491
|
async clearObservationalMemory(threadId, resourceId) {
|
|
9478
9492
|
try {
|
|
9479
9493
|
const lookupKey = this.getOMKey(threadId, resourceId);
|
|
9480
|
-
await
|
|
9494
|
+
await this.#write("clearObservationalMemory", () => this.#client.execute({
|
|
9481
9495
|
sql: `DELETE FROM "${OM_TABLE}" WHERE "lookupKey" = ?`,
|
|
9482
9496
|
args: [lookupKey]
|
|
9483
9497
|
}));
|
|
@@ -9495,7 +9509,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9495
9509
|
}
|
|
9496
9510
|
async setPendingMessageTokens(id, tokenCount) {
|
|
9497
9511
|
try {
|
|
9498
|
-
if ((await
|
|
9512
|
+
if ((await this.#write("setPendingMessageTokens", () => this.#client.execute({
|
|
9499
9513
|
sql: `UPDATE "${OM_TABLE}" SET
|
|
9500
9514
|
"pendingMessageTokens" = ?,
|
|
9501
9515
|
"updatedAt" = ?
|
|
@@ -9530,7 +9544,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9530
9544
|
}
|
|
9531
9545
|
async updateObservationalMemoryConfig(input) {
|
|
9532
9546
|
try {
|
|
9533
|
-
await
|
|
9547
|
+
await this.#write("updateObservationalMemoryConfig", async () => {
|
|
9534
9548
|
const tx = await this.#client.transaction("write");
|
|
9535
9549
|
try {
|
|
9536
9550
|
const selectResult = await tx.execute({
|
|
@@ -9574,7 +9588,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9574
9588
|
async updateBufferedObservations(input) {
|
|
9575
9589
|
try {
|
|
9576
9590
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9577
|
-
await
|
|
9591
|
+
await this.#write("updateBufferedObservations", async () => {
|
|
9578
9592
|
const tx = await this.#client.transaction("write");
|
|
9579
9593
|
try {
|
|
9580
9594
|
const current = await tx.execute({
|
|
@@ -9655,7 +9669,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9655
9669
|
async swapBufferedToActive(input) {
|
|
9656
9670
|
try {
|
|
9657
9671
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9658
|
-
return await
|
|
9672
|
+
return await this.#write("swapBufferedToActive", async () => {
|
|
9659
9673
|
const tx = await this.#client.transaction("write");
|
|
9660
9674
|
try {
|
|
9661
9675
|
const current = await tx.execute({
|
|
@@ -9804,7 +9818,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9804
9818
|
async updateBufferedReflection(input) {
|
|
9805
9819
|
try {
|
|
9806
9820
|
const nowStr = (/* @__PURE__ */ new Date()).toISOString();
|
|
9807
|
-
if ((await
|
|
9821
|
+
if ((await this.#write("updateBufferedReflection", () => this.#client.execute({
|
|
9808
9822
|
sql: `UPDATE "${OM_TABLE}" SET
|
|
9809
9823
|
"bufferedReflection" = CASE
|
|
9810
9824
|
WHEN "bufferedReflection" IS NOT NULL AND "bufferedReflection" != ''
|
|
@@ -9844,7 +9858,7 @@ var MemoryLibSQL = class MemoryLibSQL extends _mastra_core_storage.MemoryStorage
|
|
|
9844
9858
|
}
|
|
9845
9859
|
async swapBufferedReflectionToActive(input) {
|
|
9846
9860
|
try {
|
|
9847
|
-
return await
|
|
9861
|
+
return await this.#write("swapBufferedReflectionToActive", async () => {
|
|
9848
9862
|
const tx = await this.#client.transaction("write");
|
|
9849
9863
|
try {
|
|
9850
9864
|
const current = await tx.execute({
|