@voltagent/libsql 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +147 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +147 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -79,6 +79,7 @@ var LibSQLMemoryAdapter = class {
|
|
|
79
79
|
const messagesTable = `${this.tablePrefix}_messages`;
|
|
80
80
|
const usersTable = `${this.tablePrefix}_users`;
|
|
81
81
|
const workflowStatesTable = `${this.tablePrefix}_workflow_states`;
|
|
82
|
+
const stepsTable = `${this.tablePrefix}_steps`;
|
|
82
83
|
const isMemoryDb = this.url === ":memory:" || this.url.includes("mode=memory");
|
|
83
84
|
if (!isMemoryDb && (this.url.startsWith("file:") || this.url.startsWith("libsql:"))) {
|
|
84
85
|
try {
|
|
@@ -147,6 +148,26 @@ var LibSQLMemoryAdapter = class {
|
|
|
147
148
|
metadata TEXT,
|
|
148
149
|
created_at TEXT NOT NULL,
|
|
149
150
|
updated_at TEXT NOT NULL
|
|
151
|
+
)`,
|
|
152
|
+
// Create conversation steps table
|
|
153
|
+
`CREATE TABLE IF NOT EXISTS ${stepsTable} (
|
|
154
|
+
id TEXT PRIMARY KEY,
|
|
155
|
+
conversation_id TEXT NOT NULL,
|
|
156
|
+
user_id TEXT NOT NULL,
|
|
157
|
+
agent_id TEXT NOT NULL,
|
|
158
|
+
agent_name TEXT,
|
|
159
|
+
operation_id TEXT,
|
|
160
|
+
step_index INTEGER NOT NULL,
|
|
161
|
+
type TEXT NOT NULL,
|
|
162
|
+
role TEXT NOT NULL,
|
|
163
|
+
content TEXT,
|
|
164
|
+
arguments TEXT,
|
|
165
|
+
result TEXT,
|
|
166
|
+
usage TEXT,
|
|
167
|
+
sub_agent_id TEXT,
|
|
168
|
+
sub_agent_name TEXT,
|
|
169
|
+
created_at TEXT NOT NULL,
|
|
170
|
+
FOREIGN KEY (conversation_id) REFERENCES ${conversationsTable}(id) ON DELETE CASCADE
|
|
150
171
|
)`,
|
|
151
172
|
// Create indexes for better performance
|
|
152
173
|
`CREATE INDEX IF NOT EXISTS idx_${conversationsTable}_user_id ON ${conversationsTable}(user_id)`,
|
|
@@ -154,7 +175,9 @@ var LibSQLMemoryAdapter = class {
|
|
|
154
175
|
`CREATE INDEX IF NOT EXISTS idx_${messagesTable}_conversation_id ON ${messagesTable}(conversation_id)`,
|
|
155
176
|
`CREATE INDEX IF NOT EXISTS idx_${messagesTable}_created_at ON ${messagesTable}(created_at)`,
|
|
156
177
|
`CREATE INDEX IF NOT EXISTS idx_${workflowStatesTable}_workflow_id ON ${workflowStatesTable}(workflow_id)`,
|
|
157
|
-
`CREATE INDEX IF NOT EXISTS idx_${workflowStatesTable}_status ON ${workflowStatesTable}(status)
|
|
178
|
+
`CREATE INDEX IF NOT EXISTS idx_${workflowStatesTable}_status ON ${workflowStatesTable}(status)`,
|
|
179
|
+
`CREATE INDEX IF NOT EXISTS idx_${stepsTable}_conversation ON ${stepsTable}(conversation_id, step_index)`,
|
|
180
|
+
`CREATE INDEX IF NOT EXISTS idx_${stepsTable}_operation ON ${stepsTable}(conversation_id, operation_id)`
|
|
158
181
|
]);
|
|
159
182
|
}, "initialize database schema");
|
|
160
183
|
await this.addV2ColumnsToMessagesTable();
|
|
@@ -389,6 +412,72 @@ var LibSQLMemoryAdapter = class {
|
|
|
389
412
|
);
|
|
390
413
|
}, "add batch messages");
|
|
391
414
|
}
|
|
415
|
+
async saveConversationSteps(steps) {
|
|
416
|
+
if (steps.length === 0) return;
|
|
417
|
+
await this.initialize();
|
|
418
|
+
const stepsTable = `${this.tablePrefix}_steps`;
|
|
419
|
+
await this.executeWithRetry(async () => {
|
|
420
|
+
await this.client.batch(
|
|
421
|
+
steps.map((step) => {
|
|
422
|
+
const createdAt = step.createdAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
423
|
+
return {
|
|
424
|
+
sql: `INSERT INTO ${stepsTable} (
|
|
425
|
+
id,
|
|
426
|
+
conversation_id,
|
|
427
|
+
user_id,
|
|
428
|
+
agent_id,
|
|
429
|
+
agent_name,
|
|
430
|
+
operation_id,
|
|
431
|
+
step_index,
|
|
432
|
+
type,
|
|
433
|
+
role,
|
|
434
|
+
content,
|
|
435
|
+
arguments,
|
|
436
|
+
result,
|
|
437
|
+
usage,
|
|
438
|
+
sub_agent_id,
|
|
439
|
+
sub_agent_name,
|
|
440
|
+
created_at
|
|
441
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
442
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
443
|
+
conversation_id = excluded.conversation_id,
|
|
444
|
+
user_id = excluded.user_id,
|
|
445
|
+
agent_id = excluded.agent_id,
|
|
446
|
+
agent_name = excluded.agent_name,
|
|
447
|
+
operation_id = excluded.operation_id,
|
|
448
|
+
step_index = excluded.step_index,
|
|
449
|
+
type = excluded.type,
|
|
450
|
+
role = excluded.role,
|
|
451
|
+
content = excluded.content,
|
|
452
|
+
arguments = excluded.arguments,
|
|
453
|
+
result = excluded.result,
|
|
454
|
+
usage = excluded.usage,
|
|
455
|
+
sub_agent_id = excluded.sub_agent_id,
|
|
456
|
+
sub_agent_name = excluded.sub_agent_name,
|
|
457
|
+
created_at = excluded.created_at`,
|
|
458
|
+
args: [
|
|
459
|
+
step.id,
|
|
460
|
+
step.conversationId,
|
|
461
|
+
step.userId,
|
|
462
|
+
step.agentId,
|
|
463
|
+
step.agentName ?? null,
|
|
464
|
+
step.operationId ?? null,
|
|
465
|
+
step.stepIndex,
|
|
466
|
+
step.type,
|
|
467
|
+
step.role,
|
|
468
|
+
step.content ?? null,
|
|
469
|
+
step.arguments ? safeStringify(step.arguments) : null,
|
|
470
|
+
step.result ? safeStringify(step.result) : null,
|
|
471
|
+
step.usage ? safeStringify(step.usage) : null,
|
|
472
|
+
step.subAgentId ?? null,
|
|
473
|
+
step.subAgentName ?? null,
|
|
474
|
+
createdAt
|
|
475
|
+
]
|
|
476
|
+
};
|
|
477
|
+
})
|
|
478
|
+
);
|
|
479
|
+
}, "save conversation steps");
|
|
480
|
+
}
|
|
392
481
|
/**
|
|
393
482
|
* Get messages with optional filtering
|
|
394
483
|
*/
|
|
@@ -450,6 +539,51 @@ var LibSQLMemoryAdapter = class {
|
|
|
450
539
|
};
|
|
451
540
|
});
|
|
452
541
|
}
|
|
542
|
+
async getConversationSteps(userId, conversationId, options) {
|
|
543
|
+
await this.initialize();
|
|
544
|
+
const stepsTable = `${this.tablePrefix}_steps`;
|
|
545
|
+
const limit = options?.limit && options.limit > 0 ? options.limit : void 0;
|
|
546
|
+
let sql = `SELECT * FROM ${stepsTable} WHERE conversation_id = ? AND user_id = ?`;
|
|
547
|
+
const args = [conversationId, userId];
|
|
548
|
+
if (options?.operationId) {
|
|
549
|
+
sql += " AND operation_id = ?";
|
|
550
|
+
args.push(options.operationId);
|
|
551
|
+
}
|
|
552
|
+
sql += " ORDER BY step_index ASC";
|
|
553
|
+
if (limit !== void 0) {
|
|
554
|
+
sql += " LIMIT ?";
|
|
555
|
+
args.push(limit);
|
|
556
|
+
}
|
|
557
|
+
const result = await this.client.execute({ sql, args });
|
|
558
|
+
const parseJsonField = /* @__PURE__ */ __name((value) => {
|
|
559
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
560
|
+
return void 0;
|
|
561
|
+
}
|
|
562
|
+
try {
|
|
563
|
+
return JSON.parse(value);
|
|
564
|
+
} catch {
|
|
565
|
+
return void 0;
|
|
566
|
+
}
|
|
567
|
+
}, "parseJsonField");
|
|
568
|
+
return result.rows.map((row) => ({
|
|
569
|
+
id: row.id,
|
|
570
|
+
conversationId: row.conversation_id,
|
|
571
|
+
userId: row.user_id,
|
|
572
|
+
agentId: row.agent_id,
|
|
573
|
+
agentName: row.agent_name ?? void 0,
|
|
574
|
+
operationId: row.operation_id ?? void 0,
|
|
575
|
+
stepIndex: typeof row.step_index === "number" ? row.step_index : Number(row.step_index ?? 0),
|
|
576
|
+
type: row.type,
|
|
577
|
+
role: row.role,
|
|
578
|
+
content: row.content ?? void 0,
|
|
579
|
+
arguments: parseJsonField(row.arguments),
|
|
580
|
+
result: parseJsonField(row.result),
|
|
581
|
+
usage: parseJsonField(row.usage),
|
|
582
|
+
subAgentId: row.sub_agent_id ?? void 0,
|
|
583
|
+
subAgentName: row.sub_agent_name ?? void 0,
|
|
584
|
+
createdAt: row.created_at ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
585
|
+
}));
|
|
586
|
+
}
|
|
453
587
|
/**
|
|
454
588
|
* Clear messages for a user
|
|
455
589
|
*/
|
|
@@ -457,11 +591,16 @@ var LibSQLMemoryAdapter = class {
|
|
|
457
591
|
await this.initialize();
|
|
458
592
|
const messagesTable = `${this.tablePrefix}_messages`;
|
|
459
593
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
594
|
+
const stepsTable = `${this.tablePrefix}_steps`;
|
|
460
595
|
if (conversationId) {
|
|
461
596
|
await this.client.execute({
|
|
462
597
|
sql: `DELETE FROM ${messagesTable} WHERE conversation_id = ? AND user_id = ?`,
|
|
463
598
|
args: [conversationId, userId]
|
|
464
599
|
});
|
|
600
|
+
await this.client.execute({
|
|
601
|
+
sql: `DELETE FROM ${stepsTable} WHERE conversation_id = ? AND user_id = ?`,
|
|
602
|
+
args: [conversationId, userId]
|
|
603
|
+
});
|
|
465
604
|
} else {
|
|
466
605
|
await this.client.execute({
|
|
467
606
|
sql: `DELETE FROM ${messagesTable}
|
|
@@ -470,6 +609,13 @@ var LibSQLMemoryAdapter = class {
|
|
|
470
609
|
)`,
|
|
471
610
|
args: [userId]
|
|
472
611
|
});
|
|
612
|
+
await this.client.execute({
|
|
613
|
+
sql: `DELETE FROM ${stepsTable}
|
|
614
|
+
WHERE conversation_id IN (
|
|
615
|
+
SELECT id FROM ${conversationsTable} WHERE user_id = ?
|
|
616
|
+
)`,
|
|
617
|
+
args: [userId]
|
|
618
|
+
});
|
|
473
619
|
}
|
|
474
620
|
}
|
|
475
621
|
// ============================================================================
|