Skip to content

Commit 5bc0057

Browse files
committed
refactor: Enhance arrayToVectorString method with validation and update schema comments
- Updated arrayToVectorString method to include validation for input type, length, and element types. - Modified schema comments for clarity and consistency in the database migration files. - Ensured that the embedding field in the entities table is validated to have exactly 4 dimensions.
1 parent a998b51 commit 5bc0057

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

src/db/client.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,19 @@ export class DatabaseManager {
2929
return DatabaseManager.instance;
3030
}
3131

32-
// Convert array to vector string representation
33-
private arrayToVectorString(numbers: number[]): string {
32+
// Convert array to vector string representation with validation
33+
private arrayToVectorString(numbers: number[] | undefined): string | null {
34+
if (!numbers || !Array.isArray(numbers)) {
35+
return null;
36+
}
37+
// Validate vector dimensions match schema (4 dimensions for testing)
38+
if (numbers.length !== 4) {
39+
throw new Error(`Vector must have exactly 4 dimensions, got ${numbers.length}`);
40+
}
41+
// Validate all elements are numbers
42+
if (!numbers.every(n => typeof n === 'number' && !isNaN(n))) {
43+
throw new Error('Vector must contain only valid numbers');
44+
}
3445
return `[${numbers.join(', ')}]`;
3546
}
3647

@@ -60,7 +71,7 @@ export class DatabaseManager {
6071
args: [
6172
entity.name,
6273
entity.entityType,
63-
entity.embedding ? this.arrayToVectorString(entity.embedding) : null
74+
this.arrayToVectorString(entity.embedding)
6475
],
6576
});
6677

src/db/migrations/schema.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
export const schema = [
2-
// Create entities table
3-
`CREATE TABLE IF NOT EXISTS entities (
2+
// Create entities table
3+
`CREATE TABLE IF NOT EXISTS entities (
44
name TEXT PRIMARY KEY,
55
entity_type TEXT NOT NULL,
66
embedding F32_BLOB(4), -- 4 dimensions for testing
77
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
88
)`,
99

10-
// Create observations table
11-
`CREATE TABLE IF NOT EXISTS observations (
10+
// Create observations table
11+
`CREATE TABLE IF NOT EXISTS observations (
1212
id INTEGER PRIMARY KEY AUTOINCREMENT,
1313
entity_name TEXT NOT NULL,
1414
content TEXT NOT NULL,
1515
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
1616
FOREIGN KEY (entity_name) REFERENCES entities(name)
1717
)`,
1818

19-
// Create relations table
20-
`CREATE TABLE IF NOT EXISTS relations (
19+
// Create relations table
20+
`CREATE TABLE IF NOT EXISTS relations (
2121
id INTEGER PRIMARY KEY AUTOINCREMENT,
2222
source TEXT NOT NULL,
2323
target TEXT NOT NULL,
@@ -27,12 +27,12 @@ export const schema = [
2727
FOREIGN KEY (target) REFERENCES entities(name)
2828
)`,
2929

30-
// Create indexes
31-
`CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name)`,
32-
`CREATE INDEX IF NOT EXISTS idx_observations_entity ON observations(entity_name)`,
33-
`CREATE INDEX IF NOT EXISTS idx_relations_source ON relations(source)`,
34-
`CREATE INDEX IF NOT EXISTS idx_relations_target ON relations(target)`,
30+
// Create indexes
31+
`CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name)`,
32+
`CREATE INDEX IF NOT EXISTS idx_observations_entity ON observations(entity_name)`,
33+
`CREATE INDEX IF NOT EXISTS idx_relations_source ON relations(source)`,
34+
`CREATE INDEX IF NOT EXISTS idx_relations_target ON relations(target)`,
3535

36-
// Create vector index for similarity search
37-
`CREATE INDEX IF NOT EXISTS idx_entities_embedding ON entities(libsql_vector_idx(embedding))`
36+
// Create vector index for similarity search
37+
`CREATE INDEX IF NOT EXISTS idx_entities_embedding ON entities(libsql_vector_idx(embedding))`,
3838
];

0 commit comments

Comments
 (0)