@voltagent/libsql 1.0.13 → 1.1.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/edge.d.mts +111 -0
- package/dist/edge.d.ts +111 -0
- package/dist/edge.js +2183 -0
- package/dist/edge.js.map +1 -0
- package/dist/edge.mjs +2155 -0
- package/dist/edge.mjs.map +1 -0
- package/dist/index.d.mts +24 -380
- package/dist/index.d.ts +24 -380
- package/dist/index.js +263 -337
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +262 -340
- package/dist/index.mjs.map +1 -1
- package/dist/vector-core-CKn8FNVK.d.mts +274 -0
- package/dist/vector-core-CKn8FNVK.d.ts +274 -0
- package/package.json +12 -2
package/dist/index.js
CHANGED
|
@@ -41,12 +41,15 @@ module.exports = __toCommonJS(index_exports);
|
|
|
41
41
|
var import_node_fs = __toESM(require("fs"));
|
|
42
42
|
var import_node_path = __toESM(require("path"));
|
|
43
43
|
var import_client = require("@libsql/client");
|
|
44
|
+
var import_core2 = require("@voltagent/core");
|
|
45
|
+
var import_logger = require("@voltagent/logger");
|
|
46
|
+
|
|
47
|
+
// src/memory-core.ts
|
|
44
48
|
var import_core = require("@voltagent/core");
|
|
45
49
|
var import_internal = require("@voltagent/internal");
|
|
46
|
-
var
|
|
47
|
-
var LibSQLMemoryAdapter = class {
|
|
50
|
+
var LibSQLMemoryCore = class {
|
|
48
51
|
static {
|
|
49
|
-
__name(this, "
|
|
52
|
+
__name(this, "LibSQLMemoryCore");
|
|
50
53
|
}
|
|
51
54
|
client;
|
|
52
55
|
tablePrefix;
|
|
@@ -55,25 +58,14 @@ var LibSQLMemoryAdapter = class {
|
|
|
55
58
|
maxRetries;
|
|
56
59
|
retryDelayMs;
|
|
57
60
|
url;
|
|
58
|
-
constructor(options
|
|
61
|
+
constructor(client, url, options, logger) {
|
|
62
|
+
this.client = client;
|
|
63
|
+
this.url = url;
|
|
59
64
|
this.tablePrefix = options.tablePrefix ?? "voltagent_memory";
|
|
60
65
|
this.maxRetries = options.maxRetries ?? 3;
|
|
61
66
|
this.retryDelayMs = options.retryDelayMs ?? 100;
|
|
62
|
-
this.logger =
|
|
63
|
-
this.
|
|
64
|
-
if (this.url.startsWith("file:")) {
|
|
65
|
-
const dbPath = this.url.replace("file:", "");
|
|
66
|
-
const dbDir = import_node_path.default.dirname(dbPath);
|
|
67
|
-
if (dbDir && dbDir !== "." && !import_node_fs.default.existsSync(dbDir)) {
|
|
68
|
-
import_node_fs.default.mkdirSync(dbDir, { recursive: true });
|
|
69
|
-
this.logger.debug(`Created database directory: ${dbDir}`);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
this.client = (0, import_client.createClient)({
|
|
73
|
-
url: this.url,
|
|
74
|
-
authToken: options.authToken
|
|
75
|
-
});
|
|
76
|
-
this.logger.debug("LibSQL Memory adapter initialized", { url: this.url });
|
|
67
|
+
this.logger = logger;
|
|
68
|
+
this.logger.debug("LibSQL Memory adapter core initialized", { url: this.url });
|
|
77
69
|
}
|
|
78
70
|
/**
|
|
79
71
|
* Execute a database operation with retry logic
|
|
@@ -136,14 +128,12 @@ var LibSQLMemoryAdapter = class {
|
|
|
136
128
|
this.logger.debug("Applied PRAGMA settings for better concurrency");
|
|
137
129
|
await this.executeWithRetry(async () => {
|
|
138
130
|
await this.client.batch([
|
|
139
|
-
// Create users table (for user-level working memory)
|
|
140
131
|
`CREATE TABLE IF NOT EXISTS ${usersTable} (
|
|
141
132
|
id TEXT PRIMARY KEY,
|
|
142
133
|
metadata TEXT,
|
|
143
134
|
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
|
|
144
135
|
updated_at TEXT DEFAULT CURRENT_TIMESTAMP
|
|
145
136
|
)`,
|
|
146
|
-
// Create conversations table (matching existing structure)
|
|
147
137
|
`CREATE TABLE IF NOT EXISTS ${conversationsTable} (
|
|
148
138
|
id TEXT PRIMARY KEY,
|
|
149
139
|
resource_id TEXT NOT NULL,
|
|
@@ -153,7 +143,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
153
143
|
created_at TEXT NOT NULL,
|
|
154
144
|
updated_at TEXT NOT NULL
|
|
155
145
|
)`,
|
|
156
|
-
// Create messages table (matching existing structure)
|
|
157
146
|
`CREATE TABLE IF NOT EXISTS ${messagesTable} (
|
|
158
147
|
conversation_id TEXT NOT NULL,
|
|
159
148
|
message_id TEXT NOT NULL,
|
|
@@ -165,7 +154,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
165
154
|
created_at TEXT NOT NULL,
|
|
166
155
|
PRIMARY KEY (conversation_id, message_id)
|
|
167
156
|
)`,
|
|
168
|
-
// Create workflow states table
|
|
169
157
|
`CREATE TABLE IF NOT EXISTS ${workflowStatesTable} (
|
|
170
158
|
id TEXT PRIMARY KEY,
|
|
171
159
|
workflow_id TEXT NOT NULL,
|
|
@@ -181,7 +169,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
181
169
|
created_at TEXT NOT NULL,
|
|
182
170
|
updated_at TEXT NOT NULL
|
|
183
171
|
)`,
|
|
184
|
-
// Create conversation steps table
|
|
185
172
|
`CREATE TABLE IF NOT EXISTS ${stepsTable} (
|
|
186
173
|
id TEXT PRIMARY KEY,
|
|
187
174
|
conversation_id TEXT NOT NULL,
|
|
@@ -201,7 +188,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
201
188
|
created_at TEXT NOT NULL,
|
|
202
189
|
FOREIGN KEY (conversation_id) REFERENCES ${conversationsTable}(id) ON DELETE CASCADE
|
|
203
190
|
)`,
|
|
204
|
-
// Create indexes for better performance
|
|
205
191
|
`CREATE INDEX IF NOT EXISTS idx_${conversationsTable}_user_id ON ${conversationsTable}(user_id)`,
|
|
206
192
|
`CREATE INDEX IF NOT EXISTS idx_${conversationsTable}_resource_id ON ${conversationsTable}(resource_id)`,
|
|
207
193
|
`CREATE INDEX IF NOT EXISTS idx_${messagesTable}_conversation_id ON ${messagesTable}(conversation_id)`,
|
|
@@ -218,10 +204,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
218
204
|
this.initialized = true;
|
|
219
205
|
this.logger.debug("Database schema initialized");
|
|
220
206
|
}
|
|
221
|
-
/**
|
|
222
|
-
* Add new columns to messages table for V2 format if they don't exist
|
|
223
|
-
* This allows existing tables to support both old and new message formats
|
|
224
|
-
*/
|
|
225
207
|
async addV2ColumnsToMessagesTable() {
|
|
226
208
|
const messagesTableName = `${this.tablePrefix}_messages`;
|
|
227
209
|
try {
|
|
@@ -294,10 +276,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
294
276
|
} catch (_) {
|
|
295
277
|
}
|
|
296
278
|
}
|
|
297
|
-
/**
|
|
298
|
-
* Migrate default user_id values in messages table
|
|
299
|
-
* Updates messages with user_id='default' to use the actual user_id from their conversation
|
|
300
|
-
*/
|
|
301
279
|
async migrateDefaultUserIds() {
|
|
302
280
|
const messagesTableName = `${this.tablePrefix}_messages`;
|
|
303
281
|
const conversationsTableName = `${this.tablePrefix}_conversations`;
|
|
@@ -315,14 +293,14 @@ var LibSQLMemoryAdapter = class {
|
|
|
315
293
|
const result = await this.client.execute({
|
|
316
294
|
sql: `UPDATE ${messagesTableName}
|
|
317
295
|
SET user_id = (
|
|
318
|
-
SELECT c.user_id
|
|
319
|
-
FROM ${conversationsTableName} c
|
|
296
|
+
SELECT c.user_id
|
|
297
|
+
FROM ${conversationsTableName} c
|
|
320
298
|
WHERE c.id = ${messagesTableName}.conversation_id
|
|
321
299
|
)
|
|
322
300
|
WHERE user_id = 'default'
|
|
323
301
|
AND EXISTS (
|
|
324
|
-
SELECT 1
|
|
325
|
-
FROM ${conversationsTableName} c
|
|
302
|
+
SELECT 1
|
|
303
|
+
FROM ${conversationsTableName} c
|
|
326
304
|
WHERE c.id = ${messagesTableName}.conversation_id
|
|
327
305
|
)`,
|
|
328
306
|
args: []
|
|
@@ -346,10 +324,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
346
324
|
this.logger.error("Failed to migrate default user_ids", error);
|
|
347
325
|
}
|
|
348
326
|
}
|
|
349
|
-
/**
|
|
350
|
-
* Add new columns to workflow_states table for event persistence
|
|
351
|
-
* This migration adds support for events, output, and cancellation tracking
|
|
352
|
-
*/
|
|
353
327
|
async addWorkflowStateColumns() {
|
|
354
328
|
const workflowStatesTable = `${this.tablePrefix}_workflow_states`;
|
|
355
329
|
try {
|
|
@@ -385,9 +359,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
385
359
|
// ============================================================================
|
|
386
360
|
// Message Operations
|
|
387
361
|
// ============================================================================
|
|
388
|
-
/**
|
|
389
|
-
* Add a single message
|
|
390
|
-
*/
|
|
391
362
|
async addMessage(message, userId, conversationId) {
|
|
392
363
|
await this.initialize();
|
|
393
364
|
const messagesTable = `${this.tablePrefix}_messages`;
|
|
@@ -397,7 +368,7 @@ var LibSQLMemoryAdapter = class {
|
|
|
397
368
|
}
|
|
398
369
|
await this.executeWithRetry(async () => {
|
|
399
370
|
await this.client.execute({
|
|
400
|
-
sql: `INSERT INTO ${messagesTable} (conversation_id, message_id, user_id, role, parts, metadata, format_version, created_at)
|
|
371
|
+
sql: `INSERT INTO ${messagesTable} (conversation_id, message_id, user_id, role, parts, metadata, format_version, created_at)
|
|
401
372
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
402
373
|
args: [
|
|
403
374
|
conversationId,
|
|
@@ -407,15 +378,11 @@ var LibSQLMemoryAdapter = class {
|
|
|
407
378
|
(0, import_internal.safeStringify)(message.parts),
|
|
408
379
|
message.metadata ? (0, import_internal.safeStringify)(message.metadata) : null,
|
|
409
380
|
2,
|
|
410
|
-
// format_version
|
|
411
381
|
(/* @__PURE__ */ new Date()).toISOString()
|
|
412
382
|
]
|
|
413
383
|
});
|
|
414
384
|
}, "add message");
|
|
415
385
|
}
|
|
416
|
-
/**
|
|
417
|
-
* Add multiple messages
|
|
418
|
-
*/
|
|
419
386
|
async addMessages(messages, userId, conversationId) {
|
|
420
387
|
await this.initialize();
|
|
421
388
|
const messagesTable = `${this.tablePrefix}_messages`;
|
|
@@ -427,7 +394,7 @@ var LibSQLMemoryAdapter = class {
|
|
|
427
394
|
await this.executeWithRetry(async () => {
|
|
428
395
|
await this.client.batch(
|
|
429
396
|
messages.map((message) => ({
|
|
430
|
-
sql: `INSERT INTO ${messagesTable} (conversation_id, message_id, user_id, role, parts, metadata, format_version, created_at)
|
|
397
|
+
sql: `INSERT INTO ${messagesTable} (conversation_id, message_id, user_id, role, parts, metadata, format_version, created_at)
|
|
431
398
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
432
399
|
args: [
|
|
433
400
|
conversationId,
|
|
@@ -437,7 +404,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
437
404
|
(0, import_internal.safeStringify)(message.parts),
|
|
438
405
|
message.metadata ? (0, import_internal.safeStringify)(message.metadata) : null,
|
|
439
406
|
2,
|
|
440
|
-
// format_version
|
|
441
407
|
now
|
|
442
408
|
]
|
|
443
409
|
}))
|
|
@@ -510,9 +476,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
510
476
|
);
|
|
511
477
|
}, "save conversation steps");
|
|
512
478
|
}
|
|
513
|
-
/**
|
|
514
|
-
* Get messages with optional filtering
|
|
515
|
-
*/
|
|
516
479
|
async getMessages(userId, conversationId, options) {
|
|
517
480
|
await this.initialize();
|
|
518
481
|
const messagesTable = `${this.tablePrefix}_messages`;
|
|
@@ -620,9 +583,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
620
583
|
createdAt: row.created_at ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
621
584
|
}));
|
|
622
585
|
}
|
|
623
|
-
/**
|
|
624
|
-
* Clear messages for a user
|
|
625
|
-
*/
|
|
626
586
|
async clearMessages(userId, conversationId) {
|
|
627
587
|
await this.initialize();
|
|
628
588
|
const messagesTable = `${this.tablePrefix}_messages`;
|
|
@@ -657,9 +617,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
657
617
|
// ============================================================================
|
|
658
618
|
// Conversation Operations
|
|
659
619
|
// ============================================================================
|
|
660
|
-
/**
|
|
661
|
-
* Create a new conversation
|
|
662
|
-
*/
|
|
663
620
|
async createConversation(input) {
|
|
664
621
|
await this.initialize();
|
|
665
622
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
@@ -670,7 +627,7 @@ var LibSQLMemoryAdapter = class {
|
|
|
670
627
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
671
628
|
await this.executeWithRetry(async () => {
|
|
672
629
|
await this.client.execute({
|
|
673
|
-
sql: `INSERT INTO ${conversationsTable} (id, resource_id, user_id, title, metadata, created_at, updated_at)
|
|
630
|
+
sql: `INSERT INTO ${conversationsTable} (id, resource_id, user_id, title, metadata, created_at, updated_at)
|
|
674
631
|
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
675
632
|
args: [
|
|
676
633
|
input.id,
|
|
@@ -693,9 +650,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
693
650
|
updatedAt: now
|
|
694
651
|
};
|
|
695
652
|
}
|
|
696
|
-
/**
|
|
697
|
-
* Get a conversation by ID
|
|
698
|
-
*/
|
|
699
653
|
async getConversation(id) {
|
|
700
654
|
await this.initialize();
|
|
701
655
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
@@ -717,9 +671,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
717
671
|
updatedAt: row.updated_at
|
|
718
672
|
};
|
|
719
673
|
}
|
|
720
|
-
/**
|
|
721
|
-
* Get conversations by resource ID
|
|
722
|
-
*/
|
|
723
674
|
async getConversations(resourceId) {
|
|
724
675
|
await this.initialize();
|
|
725
676
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
@@ -737,15 +688,9 @@ var LibSQLMemoryAdapter = class {
|
|
|
737
688
|
updatedAt: row.updated_at
|
|
738
689
|
}));
|
|
739
690
|
}
|
|
740
|
-
/**
|
|
741
|
-
* Get conversations by user ID
|
|
742
|
-
*/
|
|
743
691
|
async getConversationsByUserId(userId, options) {
|
|
744
692
|
return this.queryConversations({ ...options, userId });
|
|
745
693
|
}
|
|
746
|
-
/**
|
|
747
|
-
* Query conversations with filters
|
|
748
|
-
*/
|
|
749
694
|
async queryConversations(options) {
|
|
750
695
|
await this.initialize();
|
|
751
696
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
@@ -781,9 +726,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
781
726
|
updatedAt: row.updated_at
|
|
782
727
|
}));
|
|
783
728
|
}
|
|
784
|
-
/**
|
|
785
|
-
* Update a conversation
|
|
786
|
-
*/
|
|
787
729
|
async updateConversation(id, updates) {
|
|
788
730
|
await this.initialize();
|
|
789
731
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
@@ -817,9 +759,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
817
759
|
}
|
|
818
760
|
return updated;
|
|
819
761
|
}
|
|
820
|
-
/**
|
|
821
|
-
* Delete a conversation
|
|
822
|
-
*/
|
|
823
762
|
async deleteConversation(id) {
|
|
824
763
|
await this.initialize();
|
|
825
764
|
const conversationsTable = `${this.tablePrefix}_conversations`;
|
|
@@ -831,9 +770,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
831
770
|
// ============================================================================
|
|
832
771
|
// Working Memory Operations
|
|
833
772
|
// ============================================================================
|
|
834
|
-
/**
|
|
835
|
-
* Get working memory
|
|
836
|
-
*/
|
|
837
773
|
async getWorkingMemory(params) {
|
|
838
774
|
await this.initialize();
|
|
839
775
|
if (params.scope === "conversation" && params.conversationId) {
|
|
@@ -853,9 +789,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
853
789
|
}
|
|
854
790
|
return null;
|
|
855
791
|
}
|
|
856
|
-
/**
|
|
857
|
-
* Set working memory
|
|
858
|
-
*/
|
|
859
792
|
async setWorkingMemory(params) {
|
|
860
793
|
await this.initialize();
|
|
861
794
|
if (params.scope === "conversation" && params.conversationId) {
|
|
@@ -889,9 +822,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
889
822
|
}
|
|
890
823
|
}
|
|
891
824
|
}
|
|
892
|
-
/**
|
|
893
|
-
* Delete working memory
|
|
894
|
-
*/
|
|
895
825
|
async deleteWorkingMemory(params) {
|
|
896
826
|
await this.initialize();
|
|
897
827
|
if (params.scope === "conversation" && params.conversationId) {
|
|
@@ -923,9 +853,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
923
853
|
// ============================================================================
|
|
924
854
|
// Workflow State Operations
|
|
925
855
|
// ============================================================================
|
|
926
|
-
/**
|
|
927
|
-
* Get workflow state by execution ID
|
|
928
|
-
*/
|
|
929
856
|
async getWorkflowState(executionId) {
|
|
930
857
|
await this.initialize();
|
|
931
858
|
const workflowStatesTable = `${this.tablePrefix}_workflow_states`;
|
|
@@ -953,9 +880,60 @@ var LibSQLMemoryAdapter = class {
|
|
|
953
880
|
updatedAt: new Date(row.updated_at)
|
|
954
881
|
};
|
|
955
882
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
883
|
+
async queryWorkflowRuns(query) {
|
|
884
|
+
await this.initialize();
|
|
885
|
+
const workflowStatesTable = `${this.tablePrefix}_workflow_states`;
|
|
886
|
+
const conditions = [];
|
|
887
|
+
const args = [];
|
|
888
|
+
if (query.workflowId) {
|
|
889
|
+
conditions.push("workflow_id = ?");
|
|
890
|
+
args.push(query.workflowId);
|
|
891
|
+
}
|
|
892
|
+
if (query.status) {
|
|
893
|
+
conditions.push("status = ?");
|
|
894
|
+
args.push(query.status);
|
|
895
|
+
}
|
|
896
|
+
if (query.from) {
|
|
897
|
+
conditions.push("created_at >= ?");
|
|
898
|
+
args.push(query.from.toISOString());
|
|
899
|
+
}
|
|
900
|
+
if (query.to) {
|
|
901
|
+
conditions.push("created_at <= ?");
|
|
902
|
+
args.push(query.to.toISOString());
|
|
903
|
+
}
|
|
904
|
+
let sql = `SELECT * FROM ${workflowStatesTable}`;
|
|
905
|
+
if (conditions.length > 0) {
|
|
906
|
+
sql += ` WHERE ${conditions.join(" AND ")}`;
|
|
907
|
+
}
|
|
908
|
+
sql += " ORDER BY created_at DESC";
|
|
909
|
+
if (query.limit !== void 0) {
|
|
910
|
+
sql += " LIMIT ?";
|
|
911
|
+
args.push(query.limit);
|
|
912
|
+
}
|
|
913
|
+
if (query.offset !== void 0) {
|
|
914
|
+
sql += " OFFSET ?";
|
|
915
|
+
args.push(query.offset);
|
|
916
|
+
}
|
|
917
|
+
const result = await this.client.execute({
|
|
918
|
+
sql,
|
|
919
|
+
args
|
|
920
|
+
});
|
|
921
|
+
return result.rows.map((row) => ({
|
|
922
|
+
id: row.id,
|
|
923
|
+
workflowId: row.workflow_id,
|
|
924
|
+
workflowName: row.workflow_name,
|
|
925
|
+
status: row.status,
|
|
926
|
+
suspension: row.suspension ? JSON.parse(row.suspension) : void 0,
|
|
927
|
+
events: row.events ? JSON.parse(row.events) : void 0,
|
|
928
|
+
output: row.output ? JSON.parse(row.output) : void 0,
|
|
929
|
+
cancellation: row.cancellation ? JSON.parse(row.cancellation) : void 0,
|
|
930
|
+
userId: row.user_id,
|
|
931
|
+
conversationId: row.conversation_id,
|
|
932
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : void 0,
|
|
933
|
+
createdAt: new Date(row.created_at),
|
|
934
|
+
updatedAt: new Date(row.updated_at)
|
|
935
|
+
}));
|
|
936
|
+
}
|
|
959
937
|
async setWorkflowState(executionId, state) {
|
|
960
938
|
await this.initialize();
|
|
961
939
|
const workflowStatesTable = `${this.tablePrefix}_workflow_states`;
|
|
@@ -980,9 +958,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
980
958
|
]
|
|
981
959
|
});
|
|
982
960
|
}
|
|
983
|
-
/**
|
|
984
|
-
* Update workflow state
|
|
985
|
-
*/
|
|
986
961
|
async updateWorkflowState(executionId, updates) {
|
|
987
962
|
await this.initialize();
|
|
988
963
|
const existing = await this.getWorkflowState(executionId);
|
|
@@ -996,9 +971,6 @@ var LibSQLMemoryAdapter = class {
|
|
|
996
971
|
};
|
|
997
972
|
await this.setWorkflowState(executionId, updated);
|
|
998
973
|
}
|
|
999
|
-
/**
|
|
1000
|
-
* Get suspended workflow states for a workflow
|
|
1001
|
-
*/
|
|
1002
974
|
async getSuspendedWorkflowStates(workflowId) {
|
|
1003
975
|
await this.initialize();
|
|
1004
976
|
const workflowStatesTable = `${this.tablePrefix}_workflow_states`;
|
|
@@ -1022,23 +994,46 @@ var LibSQLMemoryAdapter = class {
|
|
|
1022
994
|
updatedAt: new Date(row.updated_at)
|
|
1023
995
|
}));
|
|
1024
996
|
}
|
|
1025
|
-
/**
|
|
1026
|
-
* Close database connection
|
|
1027
|
-
*/
|
|
1028
997
|
async close() {
|
|
1029
998
|
this.logger.debug("Closing LibSQL Memory adapter");
|
|
1030
999
|
}
|
|
1031
1000
|
};
|
|
1032
1001
|
|
|
1002
|
+
// src/memory-v2-adapter.ts
|
|
1003
|
+
var LibSQLMemoryAdapter = class extends LibSQLMemoryCore {
|
|
1004
|
+
static {
|
|
1005
|
+
__name(this, "LibSQLMemoryAdapter");
|
|
1006
|
+
}
|
|
1007
|
+
constructor(options = {}) {
|
|
1008
|
+
const url = options.url ?? "file:./.voltagent/memory.db";
|
|
1009
|
+
const logger = options.logger || import_core2.AgentRegistry.getInstance().getGlobalLogger() || (0, import_logger.createPinoLogger)({ name: "libsql-memory" });
|
|
1010
|
+
if (url.startsWith("file:")) {
|
|
1011
|
+
const dbPath = url.replace("file:", "");
|
|
1012
|
+
const dbDir = import_node_path.default.dirname(dbPath);
|
|
1013
|
+
if (dbDir && dbDir !== "." && !import_node_fs.default.existsSync(dbDir)) {
|
|
1014
|
+
import_node_fs.default.mkdirSync(dbDir, { recursive: true });
|
|
1015
|
+
logger.debug(`Created database directory: ${dbDir}`);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
const client = (0, import_client.createClient)({
|
|
1019
|
+
url,
|
|
1020
|
+
authToken: options.authToken
|
|
1021
|
+
});
|
|
1022
|
+
super(client, url, options, logger);
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
1025
|
+
|
|
1033
1026
|
// src/observability-adapter.ts
|
|
1034
1027
|
var import_node_fs2 = require("fs");
|
|
1035
1028
|
var import_node_path2 = require("path");
|
|
1036
1029
|
var import_client2 = require("@libsql/client");
|
|
1037
|
-
var import_utils = require("@voltagent/internal/utils");
|
|
1038
1030
|
var import_logger2 = require("@voltagent/logger");
|
|
1039
|
-
|
|
1031
|
+
|
|
1032
|
+
// src/observability-core.ts
|
|
1033
|
+
var import_utils = require("@voltagent/internal/utils");
|
|
1034
|
+
var LibSQLObservabilityCore = class {
|
|
1040
1035
|
static {
|
|
1041
|
-
__name(this, "
|
|
1036
|
+
__name(this, "LibSQLObservabilityCore");
|
|
1042
1037
|
}
|
|
1043
1038
|
client;
|
|
1044
1039
|
tablePrefix;
|
|
@@ -1046,47 +1041,24 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1046
1041
|
logger;
|
|
1047
1042
|
initialized;
|
|
1048
1043
|
maxSpansPerQuery;
|
|
1049
|
-
constructor(options
|
|
1050
|
-
this.
|
|
1044
|
+
constructor(client, options, logger) {
|
|
1045
|
+
this.client = client;
|
|
1046
|
+
this.logger = logger;
|
|
1051
1047
|
this.tablePrefix = options.tablePrefix || "observability";
|
|
1052
1048
|
this.debug = options.debug || false;
|
|
1053
1049
|
this.maxSpansPerQuery = options.maxSpansPerQuery || 1e3;
|
|
1054
|
-
|
|
1055
|
-
if (url.startsWith("file:") && !url.includes(":memory:")) {
|
|
1056
|
-
const filePath = url.substring(5);
|
|
1057
|
-
const dir = (0, import_node_path2.dirname)(filePath);
|
|
1058
|
-
if (dir && dir !== "." && !(0, import_node_fs2.existsSync)(dir)) {
|
|
1059
|
-
try {
|
|
1060
|
-
(0, import_node_fs2.mkdirSync)(dir, { recursive: true });
|
|
1061
|
-
this.debugLog("Created directory for database", { dir });
|
|
1062
|
-
} catch (error) {
|
|
1063
|
-
this.logger.warn("Failed to create directory for database", { dir, error });
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
this.client = (0, import_client2.createClient)({
|
|
1068
|
-
url,
|
|
1069
|
-
authToken: options.authToken
|
|
1070
|
-
});
|
|
1071
|
-
this.debugLog("LibSQL observability adapter initialized with options", {
|
|
1072
|
-
url,
|
|
1050
|
+
this.debugLog("LibSQL observability adapter core initialized", {
|
|
1073
1051
|
tablePrefix: this.tablePrefix,
|
|
1074
1052
|
debug: this.debug,
|
|
1075
1053
|
maxSpansPerQuery: this.maxSpansPerQuery
|
|
1076
1054
|
});
|
|
1077
1055
|
this.initialized = this.initializeDatabase();
|
|
1078
1056
|
}
|
|
1079
|
-
/**
|
|
1080
|
-
* Log a debug message if debug is enabled
|
|
1081
|
-
*/
|
|
1082
1057
|
debugLog(message, data) {
|
|
1083
1058
|
if (this.debug) {
|
|
1084
1059
|
this.logger.debug(`${message}`, data || "");
|
|
1085
1060
|
}
|
|
1086
1061
|
}
|
|
1087
|
-
/**
|
|
1088
|
-
* Initialize database tables for observability
|
|
1089
|
-
*/
|
|
1090
1062
|
async initializeDatabase() {
|
|
1091
1063
|
try {
|
|
1092
1064
|
await this.client.execute(`
|
|
@@ -1113,27 +1085,27 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1113
1085
|
)
|
|
1114
1086
|
`);
|
|
1115
1087
|
await this.client.execute(`
|
|
1116
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_trace_id
|
|
1088
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_trace_id
|
|
1117
1089
|
ON ${this.tablePrefix}_spans(trace_id)
|
|
1118
1090
|
`);
|
|
1119
1091
|
await this.client.execute(`
|
|
1120
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_parent_span_id
|
|
1092
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_parent_span_id
|
|
1121
1093
|
ON ${this.tablePrefix}_spans(parent_span_id)
|
|
1122
1094
|
`);
|
|
1123
1095
|
await this.client.execute(`
|
|
1124
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_start_time
|
|
1096
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_start_time
|
|
1125
1097
|
ON ${this.tablePrefix}_spans(start_time)
|
|
1126
1098
|
`);
|
|
1127
1099
|
await this.client.execute(`
|
|
1128
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_name
|
|
1100
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_name
|
|
1129
1101
|
ON ${this.tablePrefix}_spans(name)
|
|
1130
1102
|
`);
|
|
1131
1103
|
await this.client.execute(`
|
|
1132
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_entity_id
|
|
1104
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_entity_id
|
|
1133
1105
|
ON ${this.tablePrefix}_spans(entity_id)
|
|
1134
1106
|
`);
|
|
1135
1107
|
await this.client.execute(`
|
|
1136
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_entity_type
|
|
1108
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_spans_entity_type
|
|
1137
1109
|
ON ${this.tablePrefix}_spans(entity_type)
|
|
1138
1110
|
`);
|
|
1139
1111
|
await this.client.execute(`
|
|
@@ -1150,15 +1122,15 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1150
1122
|
)
|
|
1151
1123
|
`);
|
|
1152
1124
|
await this.client.execute(`
|
|
1153
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_traces_start_time
|
|
1125
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_traces_start_time
|
|
1154
1126
|
ON ${this.tablePrefix}_traces(start_time DESC)
|
|
1155
1127
|
`);
|
|
1156
1128
|
await this.client.execute(`
|
|
1157
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_traces_entity_id
|
|
1129
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_traces_entity_id
|
|
1158
1130
|
ON ${this.tablePrefix}_traces(entity_id)
|
|
1159
1131
|
`);
|
|
1160
1132
|
await this.client.execute(`
|
|
1161
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_traces_entity_type
|
|
1133
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_traces_entity_type
|
|
1162
1134
|
ON ${this.tablePrefix}_traces(entity_type)
|
|
1163
1135
|
`);
|
|
1164
1136
|
await this.client.execute(`
|
|
@@ -1178,19 +1150,19 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1178
1150
|
)
|
|
1179
1151
|
`);
|
|
1180
1152
|
await this.client.execute(`
|
|
1181
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_trace_id
|
|
1153
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_trace_id
|
|
1182
1154
|
ON ${this.tablePrefix}_logs(trace_id)
|
|
1183
1155
|
`);
|
|
1184
1156
|
await this.client.execute(`
|
|
1185
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_span_id
|
|
1157
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_span_id
|
|
1186
1158
|
ON ${this.tablePrefix}_logs(span_id)
|
|
1187
1159
|
`);
|
|
1188
1160
|
await this.client.execute(`
|
|
1189
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_timestamp
|
|
1161
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_timestamp
|
|
1190
1162
|
ON ${this.tablePrefix}_logs(timestamp DESC)
|
|
1191
1163
|
`);
|
|
1192
1164
|
await this.client.execute(`
|
|
1193
|
-
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_severity
|
|
1165
|
+
CREATE INDEX IF NOT EXISTS idx_${this.tablePrefix}_logs_severity
|
|
1194
1166
|
ON ${this.tablePrefix}_logs(severity_number)
|
|
1195
1167
|
`);
|
|
1196
1168
|
this.debugLog("Database tables initialized successfully");
|
|
@@ -1199,22 +1171,15 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1199
1171
|
throw error;
|
|
1200
1172
|
}
|
|
1201
1173
|
}
|
|
1202
|
-
/**
|
|
1203
|
-
* Ensure database is initialized before operations
|
|
1204
|
-
*/
|
|
1205
1174
|
async ensureInitialized() {
|
|
1206
1175
|
await this.initialized;
|
|
1207
1176
|
}
|
|
1208
|
-
/**
|
|
1209
|
-
* Add a span to the database
|
|
1210
|
-
*/
|
|
1211
1177
|
async addSpan(span) {
|
|
1212
1178
|
await this.ensureInitialized();
|
|
1213
1179
|
try {
|
|
1214
1180
|
const entityId = span.attributes?.["entity.id"] || null;
|
|
1215
1181
|
const entityType = span.attributes?.["entity.type"] || null;
|
|
1216
1182
|
await this.client.batch([
|
|
1217
|
-
// Insert the span with entity columns
|
|
1218
1183
|
{
|
|
1219
1184
|
sql: `
|
|
1220
1185
|
INSERT INTO ${this.tablePrefix}_spans (
|
|
@@ -1245,7 +1210,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1245
1210
|
span.instrumentationScope ? (0, import_utils.safeStringify)(span.instrumentationScope) : null
|
|
1246
1211
|
]
|
|
1247
1212
|
},
|
|
1248
|
-
// Update or insert trace metadata with entity columns
|
|
1249
1213
|
{
|
|
1250
1214
|
sql: `
|
|
1251
1215
|
INSERT INTO ${this.tablePrefix}_traces (
|
|
@@ -1262,7 +1226,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1262
1226
|
args: [
|
|
1263
1227
|
span.traceId,
|
|
1264
1228
|
span.parentSpanId ? null : span.spanId,
|
|
1265
|
-
// Root span if no parent
|
|
1266
1229
|
entityId,
|
|
1267
1230
|
entityType,
|
|
1268
1231
|
span.startTime,
|
|
@@ -1279,9 +1242,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1279
1242
|
throw error;
|
|
1280
1243
|
}
|
|
1281
1244
|
}
|
|
1282
|
-
/**
|
|
1283
|
-
* Update an existing span
|
|
1284
|
-
*/
|
|
1285
1245
|
async updateSpan(spanId, updates) {
|
|
1286
1246
|
await this.ensureInitialized();
|
|
1287
1247
|
try {
|
|
@@ -1318,7 +1278,7 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1318
1278
|
args.push(spanId);
|
|
1319
1279
|
await this.client.execute({
|
|
1320
1280
|
sql: `
|
|
1321
|
-
UPDATE ${this.tablePrefix}_spans
|
|
1281
|
+
UPDATE ${this.tablePrefix}_spans
|
|
1322
1282
|
SET ${setClauses.join(", ")}
|
|
1323
1283
|
WHERE span_id = ?
|
|
1324
1284
|
`,
|
|
@@ -1344,32 +1304,22 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1344
1304
|
throw error;
|
|
1345
1305
|
}
|
|
1346
1306
|
}
|
|
1347
|
-
/**
|
|
1348
|
-
* Get a span by ID
|
|
1349
|
-
*/
|
|
1350
1307
|
async getSpan(spanId) {
|
|
1351
1308
|
await this.ensureInitialized();
|
|
1352
1309
|
try {
|
|
1353
1310
|
const result = await this.client.execute({
|
|
1354
|
-
sql: `
|
|
1355
|
-
SELECT * FROM ${this.tablePrefix}_spans
|
|
1356
|
-
WHERE span_id = ?
|
|
1357
|
-
`,
|
|
1311
|
+
sql: `SELECT * FROM ${this.tablePrefix}_spans WHERE span_id = ?`,
|
|
1358
1312
|
args: [spanId]
|
|
1359
1313
|
});
|
|
1360
1314
|
if (result.rows.length === 0) {
|
|
1361
1315
|
return null;
|
|
1362
1316
|
}
|
|
1363
|
-
|
|
1364
|
-
return this.rowToSpan(row);
|
|
1317
|
+
return this.rowToSpan(result.rows[0]);
|
|
1365
1318
|
} catch (error) {
|
|
1366
1319
|
this.logger.error("Failed to get span", { error, spanId });
|
|
1367
1320
|
throw error;
|
|
1368
1321
|
}
|
|
1369
1322
|
}
|
|
1370
|
-
/**
|
|
1371
|
-
* Get all spans in a trace
|
|
1372
|
-
*/
|
|
1373
1323
|
async getTrace(traceId) {
|
|
1374
1324
|
await this.ensureInitialized();
|
|
1375
1325
|
try {
|
|
@@ -1388,9 +1338,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1388
1338
|
throw error;
|
|
1389
1339
|
}
|
|
1390
1340
|
}
|
|
1391
|
-
/**
|
|
1392
|
-
* List all traces with optional entity filter
|
|
1393
|
-
*/
|
|
1394
1341
|
async listTraces(limit = 100, offset = 0, filter) {
|
|
1395
1342
|
await this.ensureInitialized();
|
|
1396
1343
|
try {
|
|
@@ -1428,52 +1375,36 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1428
1375
|
throw error;
|
|
1429
1376
|
}
|
|
1430
1377
|
}
|
|
1431
|
-
/**
|
|
1432
|
-
* Delete old spans
|
|
1433
|
-
*/
|
|
1434
1378
|
async deleteOldSpans(beforeTimestamp) {
|
|
1435
1379
|
await this.ensureInitialized();
|
|
1436
1380
|
try {
|
|
1437
1381
|
const beforeDate = new Date(beforeTimestamp).toISOString();
|
|
1438
1382
|
const tracesResult = await this.client.execute({
|
|
1439
|
-
sql: `
|
|
1440
|
-
SELECT DISTINCT trace_id FROM ${this.tablePrefix}_spans
|
|
1441
|
-
WHERE start_time < ?
|
|
1442
|
-
`,
|
|
1383
|
+
sql: `SELECT DISTINCT trace_id FROM ${this.tablePrefix}_spans WHERE start_time < ?`,
|
|
1443
1384
|
args: [beforeDate]
|
|
1444
1385
|
});
|
|
1445
1386
|
const affectedTraceIds = tracesResult.rows.map((row) => row.trace_id);
|
|
1446
1387
|
const deleteResult = await this.client.execute({
|
|
1447
|
-
sql: `
|
|
1448
|
-
DELETE FROM ${this.tablePrefix}_spans
|
|
1449
|
-
WHERE start_time < ?
|
|
1450
|
-
`,
|
|
1388
|
+
sql: `DELETE FROM ${this.tablePrefix}_spans WHERE start_time < ?`,
|
|
1451
1389
|
args: [beforeDate]
|
|
1452
1390
|
});
|
|
1453
1391
|
if (affectedTraceIds.length > 0) {
|
|
1454
1392
|
for (const traceId of affectedTraceIds) {
|
|
1455
1393
|
const countResult = await this.client.execute({
|
|
1456
|
-
sql: `
|
|
1457
|
-
SELECT COUNT(*) as count FROM ${this.tablePrefix}_spans
|
|
1458
|
-
WHERE trace_id = ?
|
|
1459
|
-
`,
|
|
1394
|
+
sql: `SELECT COUNT(*) as count FROM ${this.tablePrefix}_spans WHERE trace_id = ?`,
|
|
1460
1395
|
args: [traceId]
|
|
1461
1396
|
});
|
|
1462
1397
|
const count = countResult.rows[0].count;
|
|
1463
1398
|
if (count === 0) {
|
|
1464
1399
|
await this.client.execute({
|
|
1465
|
-
sql: `
|
|
1466
|
-
DELETE FROM ${this.tablePrefix}_traces
|
|
1467
|
-
WHERE trace_id = ?
|
|
1468
|
-
`,
|
|
1400
|
+
sql: `DELETE FROM ${this.tablePrefix}_traces WHERE trace_id = ?`,
|
|
1469
1401
|
args: [traceId]
|
|
1470
1402
|
});
|
|
1471
1403
|
} else {
|
|
1472
1404
|
await this.client.execute({
|
|
1473
1405
|
sql: `
|
|
1474
1406
|
UPDATE ${this.tablePrefix}_traces
|
|
1475
|
-
SET span_count = ?,
|
|
1476
|
-
updated_at = CURRENT_TIMESTAMP
|
|
1407
|
+
SET span_count = ?, updated_at = CURRENT_TIMESTAMP
|
|
1477
1408
|
WHERE trace_id = ?
|
|
1478
1409
|
`,
|
|
1479
1410
|
args: [count, traceId]
|
|
@@ -1489,9 +1420,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1489
1420
|
throw error;
|
|
1490
1421
|
}
|
|
1491
1422
|
}
|
|
1492
|
-
/**
|
|
1493
|
-
* Clear all spans, traces, and logs
|
|
1494
|
-
*/
|
|
1495
1423
|
async clear() {
|
|
1496
1424
|
await this.ensureInitialized();
|
|
1497
1425
|
try {
|
|
@@ -1506,9 +1434,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1506
1434
|
throw error;
|
|
1507
1435
|
}
|
|
1508
1436
|
}
|
|
1509
|
-
/**
|
|
1510
|
-
* Convert a database row to an ObservabilitySpan
|
|
1511
|
-
*/
|
|
1512
1437
|
rowToSpan(row) {
|
|
1513
1438
|
const span = {
|
|
1514
1439
|
traceId: row.trace_id,
|
|
@@ -1554,9 +1479,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1554
1479
|
}
|
|
1555
1480
|
return span;
|
|
1556
1481
|
}
|
|
1557
|
-
/**
|
|
1558
|
-
* Get statistics about stored spans
|
|
1559
|
-
*/
|
|
1560
1482
|
async getStats() {
|
|
1561
1483
|
await this.ensureInitialized();
|
|
1562
1484
|
try {
|
|
@@ -1564,9 +1486,7 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1564
1486
|
this.client.execute(`SELECT COUNT(*) as count FROM ${this.tablePrefix}_spans`),
|
|
1565
1487
|
this.client.execute(`SELECT COUNT(*) as count FROM ${this.tablePrefix}_traces`),
|
|
1566
1488
|
this.client.execute(`
|
|
1567
|
-
SELECT
|
|
1568
|
-
MIN(start_time) as oldest,
|
|
1569
|
-
MAX(start_time) as newest
|
|
1489
|
+
SELECT MIN(start_time) as oldest, MAX(start_time) as newest
|
|
1570
1490
|
FROM ${this.tablePrefix}_spans
|
|
1571
1491
|
`)
|
|
1572
1492
|
]);
|
|
@@ -1586,9 +1506,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1586
1506
|
throw error;
|
|
1587
1507
|
}
|
|
1588
1508
|
}
|
|
1589
|
-
/**
|
|
1590
|
-
* Save a log record to the database
|
|
1591
|
-
*/
|
|
1592
1509
|
async saveLogRecord(logRecord) {
|
|
1593
1510
|
await this.ensureInitialized();
|
|
1594
1511
|
try {
|
|
@@ -1643,9 +1560,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1643
1560
|
throw error;
|
|
1644
1561
|
}
|
|
1645
1562
|
}
|
|
1646
|
-
/**
|
|
1647
|
-
* Get logs by trace ID
|
|
1648
|
-
*/
|
|
1649
1563
|
async getLogsByTraceId(traceId) {
|
|
1650
1564
|
await this.ensureInitialized();
|
|
1651
1565
|
try {
|
|
@@ -1664,9 +1578,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1664
1578
|
throw error;
|
|
1665
1579
|
}
|
|
1666
1580
|
}
|
|
1667
|
-
/**
|
|
1668
|
-
* Get logs by span ID
|
|
1669
|
-
*/
|
|
1670
1581
|
async getLogsBySpanId(spanId) {
|
|
1671
1582
|
await this.ensureInitialized();
|
|
1672
1583
|
try {
|
|
@@ -1685,9 +1596,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1685
1596
|
throw error;
|
|
1686
1597
|
}
|
|
1687
1598
|
}
|
|
1688
|
-
/**
|
|
1689
|
-
* Query logs with flexible filtering
|
|
1690
|
-
*/
|
|
1691
1599
|
async queryLogs(filter) {
|
|
1692
1600
|
await this.ensureInitialized();
|
|
1693
1601
|
try {
|
|
@@ -1756,18 +1664,12 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1756
1664
|
throw error;
|
|
1757
1665
|
}
|
|
1758
1666
|
}
|
|
1759
|
-
/**
|
|
1760
|
-
* Delete old logs
|
|
1761
|
-
*/
|
|
1762
1667
|
async deleteOldLogs(beforeTimestamp) {
|
|
1763
1668
|
await this.ensureInitialized();
|
|
1764
1669
|
try {
|
|
1765
1670
|
const beforeDate = new Date(beforeTimestamp).toISOString();
|
|
1766
1671
|
const result = await this.client.execute({
|
|
1767
|
-
sql: `
|
|
1768
|
-
DELETE FROM ${this.tablePrefix}_logs
|
|
1769
|
-
WHERE timestamp < ?
|
|
1770
|
-
`,
|
|
1672
|
+
sql: `DELETE FROM ${this.tablePrefix}_logs WHERE timestamp < ?`,
|
|
1771
1673
|
args: [beforeDate]
|
|
1772
1674
|
});
|
|
1773
1675
|
const deletedCount = result.rowsAffected || 0;
|
|
@@ -1778,9 +1680,6 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1778
1680
|
throw error;
|
|
1779
1681
|
}
|
|
1780
1682
|
}
|
|
1781
|
-
/**
|
|
1782
|
-
* Convert a database row to an ObservabilityLogRecord
|
|
1783
|
-
*/
|
|
1784
1683
|
rowToLogRecord(row) {
|
|
1785
1684
|
const log = {
|
|
1786
1685
|
timestamp: row.timestamp,
|
|
@@ -1847,24 +1746,53 @@ var LibSQLObservabilityAdapter = class {
|
|
|
1847
1746
|
description: "Persists spans and logs to a LibSQL/Turso database for long-term retention."
|
|
1848
1747
|
};
|
|
1849
1748
|
}
|
|
1850
|
-
/**
|
|
1851
|
-
* Close the database connection
|
|
1852
|
-
*/
|
|
1853
1749
|
async close() {
|
|
1854
1750
|
this.debugLog("LibSQL observability adapter closed");
|
|
1855
1751
|
}
|
|
1856
1752
|
};
|
|
1857
1753
|
|
|
1754
|
+
// src/observability-adapter.ts
|
|
1755
|
+
var LibSQLObservabilityAdapter = class extends LibSQLObservabilityCore {
|
|
1756
|
+
static {
|
|
1757
|
+
__name(this, "LibSQLObservabilityAdapter");
|
|
1758
|
+
}
|
|
1759
|
+
constructor(options = {}) {
|
|
1760
|
+
const url = options.url || "file:./.voltagent/observability.db";
|
|
1761
|
+
const logger = options.logger || (0, import_logger2.createPinoLogger)({ name: "libsql-observability" });
|
|
1762
|
+
if (url.startsWith("file:") && !url.includes(":memory:")) {
|
|
1763
|
+
const filePath = url.substring(5);
|
|
1764
|
+
const dir = (0, import_node_path2.dirname)(filePath);
|
|
1765
|
+
if (dir && dir !== "." && !(0, import_node_fs2.existsSync)(dir)) {
|
|
1766
|
+
try {
|
|
1767
|
+
(0, import_node_fs2.mkdirSync)(dir, { recursive: true });
|
|
1768
|
+
if (options.debug) {
|
|
1769
|
+
logger.debug("Created directory for database", { dir });
|
|
1770
|
+
}
|
|
1771
|
+
} catch (error) {
|
|
1772
|
+
logger.warn("Failed to create directory for database", { dir, error });
|
|
1773
|
+
}
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
const client = (0, import_client2.createClient)({
|
|
1777
|
+
url,
|
|
1778
|
+
authToken: options.authToken
|
|
1779
|
+
});
|
|
1780
|
+
super(client, options, logger);
|
|
1781
|
+
}
|
|
1782
|
+
};
|
|
1783
|
+
|
|
1858
1784
|
// src/vector-adapter.ts
|
|
1859
1785
|
var import_node_fs3 = __toESM(require("fs"));
|
|
1860
1786
|
var import_node_path3 = __toESM(require("path"));
|
|
1861
1787
|
var import_client3 = require("@libsql/client");
|
|
1862
|
-
var import_core2 = require("@voltagent/core");
|
|
1863
|
-
var import_internal2 = require("@voltagent/internal");
|
|
1864
1788
|
var import_logger3 = require("@voltagent/logger");
|
|
1865
|
-
|
|
1789
|
+
|
|
1790
|
+
// src/vector-core.ts
|
|
1791
|
+
var import_core3 = require("@voltagent/core");
|
|
1792
|
+
var import_internal2 = require("@voltagent/internal");
|
|
1793
|
+
var LibSQLVectorCore = class {
|
|
1866
1794
|
static {
|
|
1867
|
-
__name(this, "
|
|
1795
|
+
__name(this, "LibSQLVectorCore");
|
|
1868
1796
|
}
|
|
1869
1797
|
client;
|
|
1870
1798
|
tablePrefix;
|
|
@@ -1875,11 +1803,11 @@ var LibSQLVectorAdapter = class {
|
|
|
1875
1803
|
logger;
|
|
1876
1804
|
maxRetries;
|
|
1877
1805
|
retryDelayMs;
|
|
1878
|
-
url;
|
|
1879
1806
|
initialized = false;
|
|
1880
1807
|
vectorCache;
|
|
1881
1808
|
dimensions = null;
|
|
1882
|
-
constructor(options
|
|
1809
|
+
constructor(client, options, logger) {
|
|
1810
|
+
this.client = client;
|
|
1883
1811
|
this.tablePrefix = options.tablePrefix ?? "voltagent";
|
|
1884
1812
|
this.maxVectorDimensions = options.maxVectorDimensions ?? 1536;
|
|
1885
1813
|
this.cacheSize = options.cacheSize ?? 100;
|
|
@@ -1887,28 +1815,32 @@ var LibSQLVectorAdapter = class {
|
|
|
1887
1815
|
this.maxRetries = options.maxRetries ?? 3;
|
|
1888
1816
|
this.retryDelayMs = options.retryDelayMs ?? 100;
|
|
1889
1817
|
this.debug = options.debug ?? false;
|
|
1890
|
-
this.logger =
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1818
|
+
this.logger = logger;
|
|
1819
|
+
this.vectorCache = /* @__PURE__ */ new Map();
|
|
1820
|
+
}
|
|
1821
|
+
/**
|
|
1822
|
+
* Serialize a vector to binary format
|
|
1823
|
+
* Uses ArrayBuffer/DataView for cross-platform compatibility
|
|
1824
|
+
*/
|
|
1825
|
+
serializeVector(vector) {
|
|
1826
|
+
const buffer = new ArrayBuffer(vector.length * 4);
|
|
1827
|
+
const view = new DataView(buffer);
|
|
1828
|
+
for (let i = 0; i < vector.length; i++) {
|
|
1829
|
+
view.setFloat32(i * 4, vector[i], true);
|
|
1899
1830
|
}
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1831
|
+
return new Uint8Array(buffer);
|
|
1832
|
+
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Deserialize a vector from binary format
|
|
1835
|
+
*/
|
|
1836
|
+
deserializeVector(data) {
|
|
1837
|
+
const bytes = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
1838
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1839
|
+
const vector = [];
|
|
1840
|
+
for (let i = 0; i < bytes.length; i += 4) {
|
|
1841
|
+
vector.push(view.getFloat32(i, true));
|
|
1906
1842
|
}
|
|
1907
|
-
|
|
1908
|
-
url: this.url,
|
|
1909
|
-
authToken: options.authToken
|
|
1910
|
-
});
|
|
1911
|
-
this.vectorCache = /* @__PURE__ */ new Map();
|
|
1843
|
+
return vector;
|
|
1912
1844
|
}
|
|
1913
1845
|
/**
|
|
1914
1846
|
* Initialize the database schema
|
|
@@ -1917,8 +1849,7 @@ var LibSQLVectorAdapter = class {
|
|
|
1917
1849
|
if (this.initialized) return;
|
|
1918
1850
|
const tableName = `${this.tablePrefix}_vectors`;
|
|
1919
1851
|
try {
|
|
1920
|
-
await this.client.
|
|
1921
|
-
BEGIN;
|
|
1852
|
+
await this.client.execute(`
|
|
1922
1853
|
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
1923
1854
|
id TEXT PRIMARY KEY,
|
|
1924
1855
|
vector BLOB NOT NULL,
|
|
@@ -1927,11 +1858,14 @@ var LibSQLVectorAdapter = class {
|
|
|
1927
1858
|
content TEXT,
|
|
1928
1859
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
1929
1860
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
1930
|
-
)
|
|
1931
|
-
CREATE INDEX IF NOT EXISTS idx_${tableName}_created ON ${tableName}(created_at);
|
|
1932
|
-
CREATE INDEX IF NOT EXISTS idx_${tableName}_dimensions ON ${tableName}(dimensions);
|
|
1933
|
-
COMMIT;
|
|
1861
|
+
)
|
|
1934
1862
|
`);
|
|
1863
|
+
await this.client.execute(
|
|
1864
|
+
`CREATE INDEX IF NOT EXISTS idx_${tableName}_created ON ${tableName}(created_at)`
|
|
1865
|
+
);
|
|
1866
|
+
await this.client.execute(
|
|
1867
|
+
`CREATE INDEX IF NOT EXISTS idx_${tableName}_dimensions ON ${tableName}(dimensions)`
|
|
1868
|
+
);
|
|
1935
1869
|
this.initialized = true;
|
|
1936
1870
|
this.logger.debug("Vector adapter initialized");
|
|
1937
1871
|
} catch (error) {
|
|
@@ -1939,34 +1873,6 @@ var LibSQLVectorAdapter = class {
|
|
|
1939
1873
|
throw error;
|
|
1940
1874
|
}
|
|
1941
1875
|
}
|
|
1942
|
-
/**
|
|
1943
|
-
* Serialize a vector to binary format
|
|
1944
|
-
*/
|
|
1945
|
-
serializeVector(vector) {
|
|
1946
|
-
const buffer = Buffer.allocUnsafe(vector.length * 4);
|
|
1947
|
-
for (let i = 0; i < vector.length; i++) {
|
|
1948
|
-
buffer.writeFloatLE(vector[i], i * 4);
|
|
1949
|
-
}
|
|
1950
|
-
return buffer;
|
|
1951
|
-
}
|
|
1952
|
-
/**
|
|
1953
|
-
* Deserialize a vector from binary format
|
|
1954
|
-
*/
|
|
1955
|
-
deserializeVector(buffer) {
|
|
1956
|
-
let bytes;
|
|
1957
|
-
if (buffer instanceof Buffer) {
|
|
1958
|
-
bytes = buffer;
|
|
1959
|
-
} else if (buffer instanceof ArrayBuffer) {
|
|
1960
|
-
bytes = Buffer.from(buffer);
|
|
1961
|
-
} else {
|
|
1962
|
-
bytes = Buffer.from(buffer);
|
|
1963
|
-
}
|
|
1964
|
-
const vector = [];
|
|
1965
|
-
for (let i = 0; i < bytes.length; i += 4) {
|
|
1966
|
-
vector.push(bytes.readFloatLE(i));
|
|
1967
|
-
}
|
|
1968
|
-
return vector;
|
|
1969
|
-
}
|
|
1970
1876
|
/**
|
|
1971
1877
|
* Execute a database operation with retries
|
|
1972
1878
|
*/
|
|
@@ -1988,9 +1894,6 @@ var LibSQLVectorAdapter = class {
|
|
|
1988
1894
|
this.logger.error(`Operation failed after ${this.maxRetries} attempts: ${context}`, lastError);
|
|
1989
1895
|
throw lastError;
|
|
1990
1896
|
}
|
|
1991
|
-
/**
|
|
1992
|
-
* Store a vector with associated metadata
|
|
1993
|
-
*/
|
|
1994
1897
|
async store(id, vector, metadata) {
|
|
1995
1898
|
await this.initialize();
|
|
1996
1899
|
if (!Array.isArray(vector) || vector.length === 0) {
|
|
@@ -2014,7 +1917,7 @@ var LibSQLVectorAdapter = class {
|
|
|
2014
1917
|
await this.executeWithRetry(async () => {
|
|
2015
1918
|
await this.client.execute({
|
|
2016
1919
|
sql: `
|
|
2017
|
-
INSERT OR REPLACE INTO ${tableName}
|
|
1920
|
+
INSERT OR REPLACE INTO ${tableName}
|
|
2018
1921
|
(id, vector, dimensions, metadata, updated_at)
|
|
2019
1922
|
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)
|
|
2020
1923
|
`,
|
|
@@ -2028,9 +1931,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2028
1931
|
this.vectorCache.set(id, { id, vector, metadata });
|
|
2029
1932
|
this.logger.debug(`Vector stored: ${id} (${vector.length} dimensions)`);
|
|
2030
1933
|
}
|
|
2031
|
-
/**
|
|
2032
|
-
* Store multiple vectors in batch
|
|
2033
|
-
*/
|
|
2034
1934
|
async storeBatch(items) {
|
|
2035
1935
|
await this.initialize();
|
|
2036
1936
|
if (items.length === 0) return;
|
|
@@ -2063,9 +1963,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2063
1963
|
this.logger.debug(`Batch of ${batch.length} vectors stored`);
|
|
2064
1964
|
}
|
|
2065
1965
|
}
|
|
2066
|
-
/**
|
|
2067
|
-
* Search for similar vectors using cosine similarity
|
|
2068
|
-
*/
|
|
2069
1966
|
async search(queryVector, options) {
|
|
2070
1967
|
await this.initialize();
|
|
2071
1968
|
const { limit = 10, threshold = 0, filter } = options || {};
|
|
@@ -2096,7 +1993,7 @@ var LibSQLVectorAdapter = class {
|
|
|
2096
1993
|
continue;
|
|
2097
1994
|
}
|
|
2098
1995
|
const vector = this.deserializeVector(vectorBlob);
|
|
2099
|
-
const similarity = (0,
|
|
1996
|
+
const similarity = (0, import_core3.cosineSimilarity)(queryVector, vector);
|
|
2100
1997
|
const score = (similarity + 1) / 2;
|
|
2101
1998
|
if (score >= threshold) {
|
|
2102
1999
|
searchResults.push({
|
|
@@ -2106,16 +2003,12 @@ var LibSQLVectorAdapter = class {
|
|
|
2106
2003
|
content,
|
|
2107
2004
|
score,
|
|
2108
2005
|
distance: 1 - similarity
|
|
2109
|
-
// Convert to distance metric
|
|
2110
2006
|
});
|
|
2111
2007
|
}
|
|
2112
2008
|
}
|
|
2113
2009
|
searchResults.sort((a, b) => b.score - a.score);
|
|
2114
2010
|
return searchResults.slice(0, limit);
|
|
2115
2011
|
}
|
|
2116
|
-
/**
|
|
2117
|
-
* Check if metadata matches the filter criteria
|
|
2118
|
-
*/
|
|
2119
2012
|
matchesFilter(metadata, filter) {
|
|
2120
2013
|
if (!metadata) {
|
|
2121
2014
|
return false;
|
|
@@ -2127,9 +2020,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2127
2020
|
}
|
|
2128
2021
|
return true;
|
|
2129
2022
|
}
|
|
2130
|
-
/**
|
|
2131
|
-
* Delete a vector by ID
|
|
2132
|
-
*/
|
|
2133
2023
|
async delete(id) {
|
|
2134
2024
|
await this.initialize();
|
|
2135
2025
|
const tableName = `${this.tablePrefix}_vectors`;
|
|
@@ -2142,9 +2032,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2142
2032
|
this.vectorCache.delete(id);
|
|
2143
2033
|
this.logger.debug(`Vector deleted: ${id}`);
|
|
2144
2034
|
}
|
|
2145
|
-
/**
|
|
2146
|
-
* Delete multiple vectors by IDs
|
|
2147
|
-
*/
|
|
2148
2035
|
async deleteBatch(ids) {
|
|
2149
2036
|
await this.initialize();
|
|
2150
2037
|
if (ids.length === 0) return;
|
|
@@ -2164,9 +2051,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2164
2051
|
this.logger.debug(`Batch of ${batch.length} vectors deleted`);
|
|
2165
2052
|
}
|
|
2166
2053
|
}
|
|
2167
|
-
/**
|
|
2168
|
-
* Clear all vectors
|
|
2169
|
-
*/
|
|
2170
2054
|
async clear() {
|
|
2171
2055
|
await this.initialize();
|
|
2172
2056
|
const tableName = `${this.tablePrefix}_vectors`;
|
|
@@ -2177,9 +2061,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2177
2061
|
this.dimensions = null;
|
|
2178
2062
|
this.logger.debug("All vectors cleared");
|
|
2179
2063
|
}
|
|
2180
|
-
/**
|
|
2181
|
-
* Get total count of stored vectors
|
|
2182
|
-
*/
|
|
2183
2064
|
async count() {
|
|
2184
2065
|
await this.initialize();
|
|
2185
2066
|
const tableName = `${this.tablePrefix}_vectors`;
|
|
@@ -2192,9 +2073,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2192
2073
|
if (typeof raw === "string") return Number.parseInt(raw, 10) || 0;
|
|
2193
2074
|
return raw ?? 0;
|
|
2194
2075
|
}
|
|
2195
|
-
/**
|
|
2196
|
-
* Get a specific vector by ID
|
|
2197
|
-
*/
|
|
2198
2076
|
async get(id) {
|
|
2199
2077
|
await this.initialize();
|
|
2200
2078
|
if (this.vectorCache.has(id)) {
|
|
@@ -2203,7 +2081,6 @@ var LibSQLVectorAdapter = class {
|
|
|
2203
2081
|
return {
|
|
2204
2082
|
...cached,
|
|
2205
2083
|
vector: [...cached.vector],
|
|
2206
|
-
// Return a copy
|
|
2207
2084
|
metadata: cached.metadata ? { ...cached.metadata } : void 0
|
|
2208
2085
|
};
|
|
2209
2086
|
}
|
|
@@ -2238,20 +2115,10 @@ var LibSQLVectorAdapter = class {
|
|
|
2238
2115
|
this.vectorCache.set(id, item);
|
|
2239
2116
|
return item;
|
|
2240
2117
|
}
|
|
2241
|
-
/**
|
|
2242
|
-
* Close the database connection
|
|
2243
|
-
*/
|
|
2244
2118
|
async close() {
|
|
2245
2119
|
this.vectorCache.clear();
|
|
2246
2120
|
this.logger.debug("Vector adapter closed");
|
|
2247
|
-
try {
|
|
2248
|
-
this.client?.close?.();
|
|
2249
|
-
} catch {
|
|
2250
|
-
}
|
|
2251
2121
|
}
|
|
2252
|
-
/**
|
|
2253
|
-
* Get statistics about the vector table and cache
|
|
2254
|
-
*/
|
|
2255
2122
|
async getStats() {
|
|
2256
2123
|
await this.initialize();
|
|
2257
2124
|
const tableName = `${this.tablePrefix}_vectors`;
|
|
@@ -2262,12 +2129,11 @@ var LibSQLVectorAdapter = class {
|
|
|
2262
2129
|
),
|
|
2263
2130
|
"getStats count"
|
|
2264
2131
|
),
|
|
2265
|
-
// Approximate table size by summing blob/text lengths
|
|
2266
2132
|
this.executeWithRetry(
|
|
2267
2133
|
async () => await this.client.execute({
|
|
2268
|
-
sql: `SELECT
|
|
2269
|
-
COALESCE(SUM(LENGTH(id)),0) +
|
|
2270
|
-
COALESCE(SUM(LENGTH(vector)),0) +
|
|
2134
|
+
sql: `SELECT
|
|
2135
|
+
COALESCE(SUM(LENGTH(id)),0) +
|
|
2136
|
+
COALESCE(SUM(LENGTH(vector)),0) +
|
|
2271
2137
|
COALESCE(SUM(LENGTH(metadata)),0) +
|
|
2272
2138
|
COALESCE(SUM(LENGTH(content)),0) AS size
|
|
2273
2139
|
FROM ${tableName}`
|
|
@@ -2289,6 +2155,66 @@ var LibSQLVectorAdapter = class {
|
|
|
2289
2155
|
};
|
|
2290
2156
|
}
|
|
2291
2157
|
};
|
|
2158
|
+
|
|
2159
|
+
// src/vector-adapter.ts
|
|
2160
|
+
var LibSQLVectorAdapter = class extends LibSQLVectorCore {
|
|
2161
|
+
static {
|
|
2162
|
+
__name(this, "LibSQLVectorAdapter");
|
|
2163
|
+
}
|
|
2164
|
+
constructor(options = {}) {
|
|
2165
|
+
const logger = options.logger ?? (0, import_logger3.createPinoLogger)({
|
|
2166
|
+
name: "libsql-vector-adapter",
|
|
2167
|
+
level: options.debug ? "debug" : "info"
|
|
2168
|
+
});
|
|
2169
|
+
const requestedUrl = options.url ?? "file:./.voltagent/memory.db";
|
|
2170
|
+
let url;
|
|
2171
|
+
if (requestedUrl === ":memory:" || requestedUrl === "file::memory:" || requestedUrl.startsWith("file::memory:")) {
|
|
2172
|
+
url = ":memory:";
|
|
2173
|
+
} else {
|
|
2174
|
+
url = requestedUrl;
|
|
2175
|
+
}
|
|
2176
|
+
if (url.startsWith("file:") && !url.startsWith("file::memory:")) {
|
|
2177
|
+
const dbPath = url.replace("file:", "");
|
|
2178
|
+
const dbDir = import_node_path3.default.dirname(dbPath);
|
|
2179
|
+
if (!import_node_fs3.default.existsSync(dbDir)) {
|
|
2180
|
+
import_node_fs3.default.mkdirSync(dbDir, { recursive: true });
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
const client = (0, import_client3.createClient)({
|
|
2184
|
+
url,
|
|
2185
|
+
authToken: options.authToken
|
|
2186
|
+
});
|
|
2187
|
+
super(client, options, logger);
|
|
2188
|
+
}
|
|
2189
|
+
/**
|
|
2190
|
+
* Override to use Buffer for more efficient serialization in Node.js
|
|
2191
|
+
*/
|
|
2192
|
+
serializeVector(vector) {
|
|
2193
|
+
const buffer = Buffer.allocUnsafe(vector.length * 4);
|
|
2194
|
+
for (let i = 0; i < vector.length; i++) {
|
|
2195
|
+
buffer.writeFloatLE(vector[i], i * 4);
|
|
2196
|
+
}
|
|
2197
|
+
return buffer;
|
|
2198
|
+
}
|
|
2199
|
+
/**
|
|
2200
|
+
* Override to use Buffer for more efficient deserialization in Node.js
|
|
2201
|
+
*/
|
|
2202
|
+
deserializeVector(data) {
|
|
2203
|
+
let buffer;
|
|
2204
|
+
if (data instanceof Buffer) {
|
|
2205
|
+
buffer = data;
|
|
2206
|
+
} else if (data instanceof ArrayBuffer) {
|
|
2207
|
+
buffer = Buffer.from(data);
|
|
2208
|
+
} else {
|
|
2209
|
+
buffer = Buffer.from(data);
|
|
2210
|
+
}
|
|
2211
|
+
const vector = [];
|
|
2212
|
+
for (let i = 0; i < buffer.length; i += 4) {
|
|
2213
|
+
vector.push(buffer.readFloatLE(i));
|
|
2214
|
+
}
|
|
2215
|
+
return vector;
|
|
2216
|
+
}
|
|
2217
|
+
};
|
|
2292
2218
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2293
2219
|
0 && (module.exports = {
|
|
2294
2220
|
LibSQLMemoryAdapter,
|