@ai-devkit/memory 0.11.0 → 0.12.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/api.d.ts +4 -4
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +17 -46
- package/dist/api.js.map +1 -1
- package/dist/database/connection.d.ts.map +1 -1
- package/dist/database/connection.js +15 -45
- package/dist/database/connection.js.map +1 -1
- package/dist/database/index.d.ts +3 -3
- package/dist/database/index.d.ts.map +1 -1
- package/dist/database/index.js +2 -35
- package/dist/database/index.js.map +1 -1
- package/dist/database/schema.d.ts +1 -1
- package/dist/database/schema.d.ts.map +1 -1
- package/dist/database/schema.js +12 -34
- package/dist/database/schema.js.map +1 -1
- package/dist/handlers/search.d.ts +1 -1
- package/dist/handlers/search.d.ts.map +1 -1
- package/dist/handlers/search.js +12 -22
- package/dist/handlers/search.js.map +1 -1
- package/dist/handlers/store.d.ts +1 -1
- package/dist/handlers/store.d.ts.map +1 -1
- package/dist/handlers/store.js +17 -27
- package/dist/handlers/store.js.map +1 -1
- package/dist/handlers/update.d.ts +1 -1
- package/dist/handlers/update.d.ts.map +1 -1
- package/dist/handlers/update.js +16 -26
- package/dist/handlers/update.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -21
- package/dist/index.js.map +1 -1
- package/dist/server.js +18 -39
- package/dist/server.js.map +1 -1
- package/dist/services/normalizer.js +17 -34
- package/dist/services/normalizer.js.map +1 -1
- package/dist/services/ranker.d.ts +1 -1
- package/dist/services/ranker.d.ts.map +1 -1
- package/dist/services/ranker.js +5 -11
- package/dist/services/ranker.js.map +1 -1
- package/dist/services/search.js +15 -24
- package/dist/services/search.js.map +1 -1
- package/dist/services/validator.d.ts +1 -1
- package/dist/services/validator.d.ts.map +1 -1
- package/dist/services/validator.js +9 -39
- package/dist/services/validator.js.map +1 -1
- package/dist/types/index.js +1 -4
- package/dist/types/index.js.map +1 -1
- package/dist/utils/errors.js +10 -37
- package/dist/utils/errors.js.map +1 -1
- package/package.json +11 -10
package/dist/api.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { storeKnowledge } from './handlers/store';
|
|
2
|
-
import { searchKnowledge } from './handlers/search';
|
|
3
|
-
import { updateKnowledge } from './handlers/update';
|
|
4
|
-
import type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult } from './types';
|
|
1
|
+
import { storeKnowledge } from './handlers/store.js';
|
|
2
|
+
import { searchKnowledge } from './handlers/search.js';
|
|
3
|
+
import { updateKnowledge } from './handlers/update.js';
|
|
4
|
+
import type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult } from './types/index.js';
|
|
5
5
|
export { storeKnowledge, searchKnowledge, updateKnowledge };
|
|
6
6
|
export type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult };
|
|
7
7
|
export interface MemoryStoreOptions {
|
package/dist/api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,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});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get memorySearchCommand () {
|
|
13
|
-
return memorySearchCommand;
|
|
14
|
-
},
|
|
15
|
-
get memoryStoreCommand () {
|
|
16
|
-
return memoryStoreCommand;
|
|
17
|
-
},
|
|
18
|
-
get memoryUpdateCommand () {
|
|
19
|
-
return memoryUpdateCommand;
|
|
20
|
-
},
|
|
21
|
-
get searchKnowledge () {
|
|
22
|
-
return _search.searchKnowledge;
|
|
23
|
-
},
|
|
24
|
-
get storeKnowledge () {
|
|
25
|
-
return _store.storeKnowledge;
|
|
26
|
-
},
|
|
27
|
-
get updateKnowledge () {
|
|
28
|
-
return _update.updateKnowledge;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
const _store = require("./handlers/store");
|
|
32
|
-
const _search = require("./handlers/search");
|
|
33
|
-
const _update = require("./handlers/update");
|
|
34
|
-
const _database = require("./database");
|
|
35
|
-
function memoryStoreCommand(options) {
|
|
1
|
+
import { storeKnowledge } from './handlers/store.js';
|
|
2
|
+
import { searchKnowledge } from './handlers/search.js';
|
|
3
|
+
import { updateKnowledge } from './handlers/update.js';
|
|
4
|
+
import { closeDatabase, getDatabase } from './database/index.js';
|
|
5
|
+
export { storeKnowledge, searchKnowledge, updateKnowledge };
|
|
6
|
+
export function memoryStoreCommand(options) {
|
|
36
7
|
try {
|
|
37
|
-
|
|
8
|
+
getDatabase({
|
|
38
9
|
dbPath: options.dbPath
|
|
39
10
|
});
|
|
40
11
|
const input = {
|
|
@@ -43,14 +14,14 @@ function memoryStoreCommand(options) {
|
|
|
43
14
|
tags: options.tags ? options.tags.split(',').map((t)=>t.trim()) : undefined,
|
|
44
15
|
scope: options.scope
|
|
45
16
|
};
|
|
46
|
-
return
|
|
17
|
+
return storeKnowledge(input);
|
|
47
18
|
} finally{
|
|
48
|
-
|
|
19
|
+
closeDatabase();
|
|
49
20
|
}
|
|
50
21
|
}
|
|
51
|
-
function memoryUpdateCommand(options) {
|
|
22
|
+
export function memoryUpdateCommand(options) {
|
|
52
23
|
try {
|
|
53
|
-
|
|
24
|
+
getDatabase({
|
|
54
25
|
dbPath: options.dbPath
|
|
55
26
|
});
|
|
56
27
|
const input = {
|
|
@@ -60,14 +31,14 @@ function memoryUpdateCommand(options) {
|
|
|
60
31
|
tags: options.tags ? options.tags.split(',').map((t)=>t.trim()) : undefined,
|
|
61
32
|
scope: options.scope
|
|
62
33
|
};
|
|
63
|
-
return
|
|
34
|
+
return updateKnowledge(input);
|
|
64
35
|
} finally{
|
|
65
|
-
|
|
36
|
+
closeDatabase();
|
|
66
37
|
}
|
|
67
38
|
}
|
|
68
|
-
function memorySearchCommand(options) {
|
|
39
|
+
export function memorySearchCommand(options) {
|
|
69
40
|
try {
|
|
70
|
-
|
|
41
|
+
getDatabase({
|
|
71
42
|
dbPath: options.dbPath
|
|
72
43
|
});
|
|
73
44
|
const input = {
|
|
@@ -76,9 +47,9 @@ function memorySearchCommand(options) {
|
|
|
76
47
|
scope: options.scope,
|
|
77
48
|
limit: options.limit
|
|
78
49
|
};
|
|
79
|
-
return
|
|
50
|
+
return searchKnowledge(input);
|
|
80
51
|
} finally{
|
|
81
|
-
|
|
52
|
+
closeDatabase();
|
|
82
53
|
}
|
|
83
54
|
}
|
|
84
55
|
|
package/dist/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/api.ts"],"sourcesContent":["import { storeKnowledge } from './handlers/store';\nimport { searchKnowledge } from './handlers/search';\nimport { updateKnowledge } from './handlers/update';\nimport { closeDatabase, getDatabase } from './database';\nimport type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult } from './types';\n\nexport { storeKnowledge, searchKnowledge, updateKnowledge };\nexport type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult };\n\n// CLI command handlers for integration with main ai-devkit CLI\nexport interface MemoryStoreOptions {\n title: string;\n content: string;\n tags?: string;\n scope?: string;\n dbPath?: string;\n}\n\nexport interface MemoryUpdateOptions {\n id: string;\n title?: string;\n content?: string;\n tags?: string;\n scope?: string;\n dbPath?: string;\n}\n\nexport interface MemorySearchOptions {\n query: string;\n tags?: string;\n scope?: string;\n limit?: number;\n dbPath?: string;\n}\n\nexport function memoryStoreCommand(options: MemoryStoreOptions): StoreKnowledgeResult {\n try {\n getDatabase({ dbPath: options.dbPath });\n const input: StoreKnowledgeInput = {\n title: options.title,\n content: options.content,\n tags: options.tags ? options.tags.split(',').map(t => t.trim()) : undefined,\n scope: options.scope,\n };\n\n return storeKnowledge(input);\n } finally {\n closeDatabase();\n }\n}\n\nexport function memoryUpdateCommand(options: MemoryUpdateOptions): UpdateKnowledgeResult {\n try {\n getDatabase({ dbPath: options.dbPath });\n const input: UpdateKnowledgeInput = {\n id: options.id,\n title: options.title,\n content: options.content,\n tags: options.tags ? options.tags.split(',').map(t => t.trim()) : undefined,\n scope: options.scope,\n };\n\n return updateKnowledge(input);\n } finally {\n closeDatabase();\n }\n}\n\nexport function memorySearchCommand(options: MemorySearchOptions): SearchKnowledgeResult {\n try {\n getDatabase({ dbPath: options.dbPath });\n const input: SearchKnowledgeInput = {\n query: options.query,\n contextTags: options.tags ? options.tags.split(',').map(t => t.trim()) : undefined,\n scope: options.scope,\n limit: options.limit,\n };\n\n return searchKnowledge(input);\n } finally {\n closeDatabase();\n }\n}\n"],"names":["
|
|
1
|
+
{"version":3,"sources":["../src/api.ts"],"sourcesContent":["import { storeKnowledge } from './handlers/store.js';\nimport { searchKnowledge } from './handlers/search.js';\nimport { updateKnowledge } from './handlers/update.js';\nimport { closeDatabase, getDatabase } from './database/index.js';\nimport type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult } from './types/index.js';\n\nexport { storeKnowledge, searchKnowledge, updateKnowledge };\nexport type { StoreKnowledgeInput, SearchKnowledgeInput, StoreKnowledgeResult, SearchKnowledgeResult, UpdateKnowledgeInput, UpdateKnowledgeResult };\n\n// CLI command handlers for integration with main ai-devkit CLI\nexport interface MemoryStoreOptions {\n title: string;\n content: string;\n tags?: string;\n scope?: string;\n dbPath?: string;\n}\n\nexport interface MemoryUpdateOptions {\n id: string;\n title?: string;\n content?: string;\n tags?: string;\n scope?: string;\n dbPath?: string;\n}\n\nexport interface MemorySearchOptions {\n query: string;\n tags?: string;\n scope?: string;\n limit?: number;\n dbPath?: string;\n}\n\nexport function memoryStoreCommand(options: MemoryStoreOptions): StoreKnowledgeResult {\n try {\n getDatabase({ dbPath: options.dbPath });\n const input: StoreKnowledgeInput = {\n title: options.title,\n content: options.content,\n tags: options.tags ? options.tags.split(',').map(t => t.trim()) : undefined,\n scope: options.scope,\n };\n\n return storeKnowledge(input);\n } finally {\n closeDatabase();\n }\n}\n\nexport function memoryUpdateCommand(options: MemoryUpdateOptions): UpdateKnowledgeResult {\n try {\n getDatabase({ dbPath: options.dbPath });\n const input: UpdateKnowledgeInput = {\n id: options.id,\n title: options.title,\n content: options.content,\n tags: options.tags ? options.tags.split(',').map(t => t.trim()) : undefined,\n scope: options.scope,\n };\n\n return updateKnowledge(input);\n } finally {\n closeDatabase();\n }\n}\n\nexport function memorySearchCommand(options: MemorySearchOptions): SearchKnowledgeResult {\n try {\n getDatabase({ dbPath: options.dbPath });\n const input: SearchKnowledgeInput = {\n query: options.query,\n contextTags: options.tags ? options.tags.split(',').map(t => t.trim()) : undefined,\n scope: options.scope,\n limit: options.limit,\n };\n\n return searchKnowledge(input);\n } finally {\n closeDatabase();\n }\n}\n"],"names":["storeKnowledge","searchKnowledge","updateKnowledge","closeDatabase","getDatabase","memoryStoreCommand","options","dbPath","input","title","content","tags","split","map","t","trim","undefined","scope","memoryUpdateCommand","id","memorySearchCommand","query","contextTags","limit"],"mappings":"AAAA,SAASA,cAAc,QAAQ,sBAAsB;AAC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W,KAAK;YACpBM,OAAOjB,QAAQiB,KAAK;QACxB;QAEA,OAAOtB,gBAAgBO;IAC3B,SAAU;QACNL;IACJ;AACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/database/connection.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/database/connection.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAMtC;;GAEG;AACH,eAAO,MAAM,eAAe,QAA6C,CAAC;AAE1E,MAAM,WAAW,eAAe;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,qBAAa,kBAAkB;IAC3B,OAAO,CAAC,EAAE,CAAoB;IAC9B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;gBAEpB,OAAO,GAAE,eAAoB;IAczC,OAAO,CAAC,SAAS;IAQjB,IAAI,QAAQ,IAAI,QAAQ,CAAC,QAAQ,CAEhC;IAED,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,GAAG,CAAC,EAAE;IAIlD,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,GAAG,CAAC,GAAG,SAAS;IAI/D,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAO,EAAO,GAAG,QAAQ,CAAC,SAAS;IAGhE,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC;IAI9B,KAAK,IAAI,IAAI;CAKhB;AAKD,wBAAgB,WAAW,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,kBAAkB,CAWzE;AAED,wBAAgB,aAAa,IAAI,IAAI,CAMpC"}
|
|
@@ -1,47 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get DEFAULT_DB_PATH () {
|
|
13
|
-
return DEFAULT_DB_PATH;
|
|
14
|
-
},
|
|
15
|
-
get DatabaseConnection () {
|
|
16
|
-
return DatabaseConnection;
|
|
17
|
-
},
|
|
18
|
-
get closeDatabase () {
|
|
19
|
-
return closeDatabase;
|
|
20
|
-
},
|
|
21
|
-
get getDatabase () {
|
|
22
|
-
return getDatabase;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
const _bettersqlite3 = /*#__PURE__*/ _interop_require_default(require("better-sqlite3"));
|
|
26
|
-
const _fs = require("fs");
|
|
27
|
-
const _path = require("path");
|
|
28
|
-
const _os = require("os");
|
|
29
|
-
function _interop_require_default(obj) {
|
|
30
|
-
return obj && obj.__esModule ? obj : {
|
|
31
|
-
default: obj
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
const DEFAULT_DB_PATH = (0, _path.join)((0, _os.homedir)(), '.ai-devkit', 'memory.db');
|
|
35
|
-
let DatabaseConnection = class DatabaseConnection {
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
import { mkdirSync } from 'fs';
|
|
3
|
+
import { dirname, join } from 'path';
|
|
4
|
+
import { homedir } from 'os';
|
|
5
|
+
import { initializeSchema } from './schema.js';
|
|
6
|
+
/**
|
|
7
|
+
* Default database path: ~/.ai-devkit/memory.db
|
|
8
|
+
*/ export const DEFAULT_DB_PATH = join(homedir(), '.ai-devkit', 'memory.db');
|
|
9
|
+
export class DatabaseConnection {
|
|
36
10
|
db;
|
|
37
11
|
dbPath;
|
|
38
12
|
constructor(options = {}){
|
|
39
13
|
this.dbPath = options.dbPath ?? DEFAULT_DB_PATH;
|
|
40
|
-
const dir =
|
|
41
|
-
|
|
14
|
+
const dir = dirname(this.dbPath);
|
|
15
|
+
mkdirSync(dir, {
|
|
42
16
|
recursive: true
|
|
43
17
|
});
|
|
44
|
-
this.db = new
|
|
18
|
+
this.db = new Database(this.dbPath, {
|
|
45
19
|
readonly: options.readonly ?? false,
|
|
46
20
|
verbose: options.verbose ? console.log : undefined
|
|
47
21
|
});
|
|
@@ -80,24 +54,20 @@ let DatabaseConnection = class DatabaseConnection {
|
|
|
80
54
|
this.db.close();
|
|
81
55
|
}
|
|
82
56
|
}
|
|
83
|
-
}
|
|
57
|
+
}
|
|
84
58
|
let instance = null;
|
|
85
59
|
let schemaInitialized = false;
|
|
86
|
-
function getDatabase(options) {
|
|
60
|
+
export function getDatabase(options) {
|
|
87
61
|
if (!instance) {
|
|
88
62
|
instance = new DatabaseConnection(options);
|
|
89
63
|
}
|
|
90
|
-
// Auto-run migrations on first access
|
|
91
64
|
if (!schemaInitialized) {
|
|
92
|
-
// Lazy import to avoid circular dependency
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
94
|
-
const { initializeSchema } = require('./schema');
|
|
95
65
|
initializeSchema(instance);
|
|
96
66
|
schemaInitialized = true;
|
|
97
67
|
}
|
|
98
68
|
return instance;
|
|
99
69
|
}
|
|
100
|
-
function closeDatabase() {
|
|
70
|
+
export function closeDatabase() {
|
|
101
71
|
if (instance) {
|
|
102
72
|
instance.close();
|
|
103
73
|
instance = null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/database/connection.ts"],"sourcesContent":["import Database from 'better-sqlite3';\nimport { mkdirSync } from 'fs';\nimport { dirname, join } from 'path';\nimport { homedir } from 'os';\n\n/**\n * Default database path: ~/.ai-devkit/memory.db\n */\nexport const DEFAULT_DB_PATH = join(homedir(), '.ai-devkit', 'memory.db');\n\nexport interface DatabaseOptions {\n dbPath?: string;\n verbose?: boolean;\n readonly?: boolean;\n}\n\nexport class DatabaseConnection {\n private db: Database.Database;\n private readonly dbPath: string;\n\n constructor(options: DatabaseOptions = {}) {\n this.dbPath = options.dbPath ?? DEFAULT_DB_PATH;\n\n const dir = dirname(this.dbPath);\n mkdirSync(dir, { recursive: true });\n\n this.db = new Database(this.dbPath, {\n readonly: options.readonly ?? false,\n verbose: options.verbose ? console.log : undefined,\n });\n\n this.configure();\n }\n\n private configure(): void {\n this.db.pragma('journal_mode = WAL');\n this.db.pragma('foreign_keys = ON');\n this.db.pragma('synchronous = NORMAL');\n this.db.pragma('busy_timeout = 5000');\n this.db.pragma('mmap_size = 268435456');\n }\n\n get instance(): Database.Database {\n return this.db;\n }\n\n get path(): string {\n return this.dbPath;\n }\n\n get isOpen(): boolean {\n return this.db.open;\n }\n\n query<T>(sql: string, params: unknown[] = []): T[] {\n return this.db.prepare(sql).all(...params) as T[];\n }\n\n queryOne<T>(sql: string, params: unknown[] = []): T | undefined {\n return this.db.prepare(sql).get(...params) as T | undefined;\n }\n\n execute(sql: string, params: unknown[] = []): Database.RunResult {\n return this.db.prepare(sql).run(...params);\n }\n transaction<T>(fn: () => T): T {\n return this.db.transaction(fn)();\n }\n\n close(): void {\n if (this.db.open) {\n this.db.close();\n }\n }\n}\n\nlet instance: DatabaseConnection | null = null;\nlet schemaInitialized = false;\n\nexport function getDatabase(options?: DatabaseOptions): DatabaseConnection {\n if (!instance) {\n instance = new DatabaseConnection(options);\n }\n\n
|
|
1
|
+
{"version":3,"sources":["../../src/database/connection.ts"],"sourcesContent":["import Database from 'better-sqlite3';\nimport { mkdirSync } from 'fs';\nimport { dirname, join } from 'path';\nimport { homedir } from 'os';\nimport { initializeSchema } from './schema.js';\n\n/**\n * Default database path: ~/.ai-devkit/memory.db\n */\nexport const DEFAULT_DB_PATH = join(homedir(), '.ai-devkit', 'memory.db');\n\nexport interface DatabaseOptions {\n dbPath?: string;\n verbose?: boolean;\n readonly?: boolean;\n}\n\nexport class DatabaseConnection {\n private db: Database.Database;\n private readonly dbPath: string;\n\n constructor(options: DatabaseOptions = {}) {\n this.dbPath = options.dbPath ?? DEFAULT_DB_PATH;\n\n const dir = dirname(this.dbPath);\n mkdirSync(dir, { recursive: true });\n\n this.db = new Database(this.dbPath, {\n readonly: options.readonly ?? false,\n verbose: options.verbose ? console.log : undefined,\n });\n\n this.configure();\n }\n\n private configure(): void {\n this.db.pragma('journal_mode = WAL');\n this.db.pragma('foreign_keys = ON');\n this.db.pragma('synchronous = NORMAL');\n this.db.pragma('busy_timeout = 5000');\n this.db.pragma('mmap_size = 268435456');\n }\n\n get instance(): Database.Database {\n return this.db;\n }\n\n get path(): string {\n return this.dbPath;\n }\n\n get isOpen(): boolean {\n return this.db.open;\n }\n\n query<T>(sql: string, params: unknown[] = []): T[] {\n return this.db.prepare(sql).all(...params) as T[];\n }\n\n queryOne<T>(sql: string, params: unknown[] = []): T | undefined {\n return this.db.prepare(sql).get(...params) as T | undefined;\n }\n\n execute(sql: string, params: unknown[] = []): Database.RunResult {\n return this.db.prepare(sql).run(...params);\n }\n transaction<T>(fn: () => T): T {\n return this.db.transaction(fn)();\n }\n\n close(): void {\n if (this.db.open) {\n this.db.close();\n }\n }\n}\n\nlet instance: DatabaseConnection | null = null;\nlet schemaInitialized = false;\n\nexport function getDatabase(options?: DatabaseOptions): DatabaseConnection {\n if (!instance) {\n instance = new DatabaseConnection(options);\n }\n\n if (!schemaInitialized) {\n initializeSchema(instance);\n schemaInitialized = true;\n }\n\n return instance;\n}\n\nexport function closeDatabase(): void {\n if (instance) {\n instance.close();\n instance = null;\n schemaInitialized = false;\n }\n}"],"names":["Database","mkdirSync","dirname","join","homedir","initializeSchema","DEFAULT_DB_PATH","DatabaseConnection","db","dbPath","options","dir","recursive","readonly","verbose","console","log","undefined","configure","pragma","instance","path","isOpen","open","query","sql","params","prepare","all","queryOne","get","execute","run","transaction","fn","close","schemaInitialized","getDatabase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AW,EAAEC,SAAoB,EAAE,EAAiB;QAC5D,OAAO,IAAI,CAAClB,EAAE,CAACmB,OAAO,CAACF,KAAKK,GAAG,IAAIJ;IACvC;IAEAK,QAAQN,GAAW,EAAEC,SAAoB,EAAE,EAAsB;QAC7D,OAAO,IAAI,CAAClB,EAAE,CAACmB,OAAO,CAACF,KAAKO,GAAG,IAAIN;IACvC;IACAO,YAAeC,EAAW,EAAK;QAC3B,OAAO,IAAI,CAAC1B,EAAE,CAACyB,WAAW,CAACC;IAC/B;IAEAC,QAAc;QACV,IAAI,IAAI,CAAC3B,EAAE,CAACe,IAAI,EAAE;YACd,IAAI,CAACf,EAAE,CAAC2B,KAAK;QACjB;IACJ;AACJ;AAEA,IAAIf,WAAsC;AAC1C,IAAIgB,oBAAoB;AAExB,OAAO,SAASC,YAAY3B,OAAyB;IACjD,IAAI,CAACU,UAAU;QACXA,WAAW,IAAIb,mBAAmBG;IACtC;IAEA,IAAI,CAAC0B,mBAAmB;QACpB/B,iBAAiBe;QACjBgB,oBAAoB;IACxB;IAEA,OAAOhB;AACX;AAEA,OAAO,SAASkB;IACZ,IAAIlB,UAAU;QACVA,SAASe,KAAK;QACdf,WAAW;QACXgB,oBAAoB;IACxB;AACJ"}
|
package/dist/database/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DatabaseConnection, getDatabase, closeDatabase, DEFAULT_DB_PATH } from './connection';
|
|
2
|
-
export type { DatabaseOptions } from './connection';
|
|
3
|
-
export { initializeSchema, getSchemaVersion, resetSchema } from './schema';
|
|
1
|
+
export { DatabaseConnection, getDatabase, closeDatabase, DEFAULT_DB_PATH } from './connection.js';
|
|
2
|
+
export type { DatabaseOptions } from './connection.js';
|
|
3
|
+
export { initializeSchema, getSchemaVersion, resetSchema } from './schema.js';
|
|
4
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClG,YAAY,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/database/index.js
CHANGED
|
@@ -1,37 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get DEFAULT_DB_PATH () {
|
|
13
|
-
return _connection.DEFAULT_DB_PATH;
|
|
14
|
-
},
|
|
15
|
-
get DatabaseConnection () {
|
|
16
|
-
return _connection.DatabaseConnection;
|
|
17
|
-
},
|
|
18
|
-
get closeDatabase () {
|
|
19
|
-
return _connection.closeDatabase;
|
|
20
|
-
},
|
|
21
|
-
get getDatabase () {
|
|
22
|
-
return _connection.getDatabase;
|
|
23
|
-
},
|
|
24
|
-
get getSchemaVersion () {
|
|
25
|
-
return _schema.getSchemaVersion;
|
|
26
|
-
},
|
|
27
|
-
get initializeSchema () {
|
|
28
|
-
return _schema.initializeSchema;
|
|
29
|
-
},
|
|
30
|
-
get resetSchema () {
|
|
31
|
-
return _schema.resetSchema;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
const _connection = require("./connection");
|
|
35
|
-
const _schema = require("./schema");
|
|
1
|
+
export { DatabaseConnection, getDatabase, closeDatabase, DEFAULT_DB_PATH } from './connection.js';
|
|
2
|
+
export { initializeSchema, getSchemaVersion, resetSchema } from './schema.js';
|
|
36
3
|
|
|
37
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/database/index.ts"],"sourcesContent":["export { DatabaseConnection, getDatabase, closeDatabase, DEFAULT_DB_PATH } from './connection';\nexport type { DatabaseOptions } from './connection';\nexport { initializeSchema, getSchemaVersion, resetSchema } from './schema';\n"],"names":["
|
|
1
|
+
{"version":3,"sources":["../../src/database/index.ts"],"sourcesContent":["export { DatabaseConnection, getDatabase, closeDatabase, DEFAULT_DB_PATH } from './connection.js';\nexport type { DatabaseOptions } from './connection.js';\nexport { initializeSchema, getSchemaVersion, resetSchema } from './schema.js';\n"],"names":["DatabaseConnection","getDatabase","closeDatabase","DEFAULT_DB_PATH","initializeSchema","getSchemaVersion","resetSchema"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,WAAW,EAAEC,aAAa,EAAEC,eAAe,QAAQ,kBAAkB;AAElG,SAASC,gBAAgB,EAAEC,gBAAgB,EAAEC,WAAW,QAAQ,cAAc"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DatabaseConnection } from './connection';
|
|
1
|
+
import type { DatabaseConnection } from './connection.js';
|
|
2
2
|
export declare function getSchemaVersion(db: DatabaseConnection): number;
|
|
3
3
|
export declare function initializeSchema(db: DatabaseConnection): void;
|
|
4
4
|
export declare function resetSchema(db: DatabaseConnection): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/database/schema.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/database/schema.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAI1D,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,kBAAkB,GAAG,MAAM,CAG/D;AAsCD,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,kBAAkB,GAAG,IAAI,CAkB7D;AAED,wBAAgB,WAAW,CAAC,EAAE,EAAE,kBAAkB,GAAG,IAAI,CAQxD;AAED,wBAAgB,oBAAoB,CAAC,EAAE,EAAE,kBAAkB,GAAG,MAAM,EAAE,CAMrE"}
|
package/dist/database/schema.js
CHANGED
|
@@ -1,30 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get getPendingMigrations () {
|
|
13
|
-
return getPendingMigrations;
|
|
14
|
-
},
|
|
15
|
-
get getSchemaVersion () {
|
|
16
|
-
return getSchemaVersion;
|
|
17
|
-
},
|
|
18
|
-
get initializeSchema () {
|
|
19
|
-
return initializeSchema;
|
|
20
|
-
},
|
|
21
|
-
get resetSchema () {
|
|
22
|
-
return resetSchema;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
const _fs = require("fs");
|
|
26
|
-
const _path = require("path");
|
|
27
|
-
function getSchemaVersion(db) {
|
|
1
|
+
import { readFileSync, readdirSync } from 'fs';
|
|
2
|
+
import { dirname, join } from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
5
|
+
export function getSchemaVersion(db) {
|
|
28
6
|
const result = db.instance.pragma('user_version');
|
|
29
7
|
return result[0]?.user_version ?? 0;
|
|
30
8
|
}
|
|
@@ -34,14 +12,14 @@ function setSchemaVersion(db, version) {
|
|
|
34
12
|
function getMigrationsDir() {
|
|
35
13
|
// In production, migrations are in dist/database/migrations
|
|
36
14
|
// In development/testing, they are in src/database/migrations
|
|
37
|
-
const distPath =
|
|
15
|
+
const distPath = join(__dirname, 'migrations');
|
|
38
16
|
return distPath;
|
|
39
17
|
}
|
|
40
18
|
function getMigrationFiles() {
|
|
41
19
|
const migrationsDir = getMigrationsDir();
|
|
42
20
|
let files;
|
|
43
21
|
try {
|
|
44
|
-
files =
|
|
22
|
+
files = readdirSync(migrationsDir).filter((f)=>f.endsWith('.sql')).sort();
|
|
45
23
|
} catch {
|
|
46
24
|
return [];
|
|
47
25
|
}
|
|
@@ -53,11 +31,11 @@ function getMigrationFiles() {
|
|
|
53
31
|
return {
|
|
54
32
|
version: parseInt(match[1], 10),
|
|
55
33
|
name: match[2],
|
|
56
|
-
path:
|
|
34
|
+
path: join(migrationsDir, file)
|
|
57
35
|
};
|
|
58
36
|
});
|
|
59
37
|
}
|
|
60
|
-
function initializeSchema(db) {
|
|
38
|
+
export function initializeSchema(db) {
|
|
61
39
|
const currentVersion = getSchemaVersion(db);
|
|
62
40
|
const migrations = getMigrationFiles();
|
|
63
41
|
const pendingMigrations = migrations.filter((m)=>m.version > currentVersion);
|
|
@@ -65,14 +43,14 @@ function initializeSchema(db) {
|
|
|
65
43
|
return;
|
|
66
44
|
}
|
|
67
45
|
for (const migration of pendingMigrations){
|
|
68
|
-
const sql =
|
|
46
|
+
const sql = readFileSync(migration.path, 'utf-8');
|
|
69
47
|
db.transaction(()=>{
|
|
70
48
|
db.instance.exec(sql);
|
|
71
49
|
setSchemaVersion(db, migration.version);
|
|
72
50
|
});
|
|
73
51
|
}
|
|
74
52
|
}
|
|
75
|
-
function resetSchema(db) {
|
|
53
|
+
export function resetSchema(db) {
|
|
76
54
|
db.transaction(()=>{
|
|
77
55
|
db.execute('DROP TABLE IF EXISTS knowledge_fts');
|
|
78
56
|
db.execute('DROP TABLE IF EXISTS knowledge');
|
|
@@ -80,7 +58,7 @@ function resetSchema(db) {
|
|
|
80
58
|
});
|
|
81
59
|
initializeSchema(db);
|
|
82
60
|
}
|
|
83
|
-
function getPendingMigrations(db) {
|
|
61
|
+
export function getPendingMigrations(db) {
|
|
84
62
|
const currentVersion = getSchemaVersion(db);
|
|
85
63
|
const migrations = getMigrationFiles();
|
|
86
64
|
return migrations.filter((m)=>m.version > currentVersion).map((m)=>`${m.version}_${m.name}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/database/schema.ts"],"sourcesContent":["import { readFileSync, readdirSync } from 'fs';\nimport { join } from 'path';\nimport type { DatabaseConnection } from './connection';\n\nexport function getSchemaVersion(db: DatabaseConnection): number {\n const result = db.instance.pragma('user_version') as { user_version: number }[];\n return result[0]?.user_version ?? 0;\n}\n\nfunction setSchemaVersion(db: DatabaseConnection, version: number): void {\n db.instance.pragma(`user_version = ${version}`);\n}\n\nfunction getMigrationsDir(): string {\n // In production, migrations are in dist/database/migrations\n // In development/testing, they are in src/database/migrations\n const distPath = join(__dirname, 'migrations');\n return distPath;\n}\n\nfunction getMigrationFiles(): { version: number; path: string; name: string }[] {\n const migrationsDir = getMigrationsDir();\n\n let files: string[];\n try {\n files = readdirSync(migrationsDir)\n .filter(f => f.endsWith('.sql'))\n .sort();\n } catch {\n return [];\n }\n\n return files.map(file => {\n const match = file.match(/^(\\d+)_(.+)\\.sql$/);\n if (!match || !match[1] || !match[2]) {\n throw new Error(`Invalid migration filename: ${file}. Expected format: 001_name.sql`);\n }\n return {\n version: parseInt(match[1], 10),\n name: match[2],\n path: join(migrationsDir, file),\n };\n });\n}\n\nexport function initializeSchema(db: DatabaseConnection): void {\n const currentVersion = getSchemaVersion(db);\n const migrations = getMigrationFiles();\n\n const pendingMigrations = migrations.filter(m => m.version > currentVersion);\n\n if (pendingMigrations.length === 0) {\n return;\n }\n\n for (const migration of pendingMigrations) {\n const sql = readFileSync(migration.path, 'utf-8');\n\n db.transaction(() => {\n db.instance.exec(sql);\n setSchemaVersion(db, migration.version);\n });\n }\n}\n\nexport function resetSchema(db: DatabaseConnection): void {\n db.transaction(() => {\n db.execute('DROP TABLE IF EXISTS knowledge_fts');\n db.execute('DROP TABLE IF EXISTS knowledge');\n setSchemaVersion(db, 0);\n });\n\n initializeSchema(db);\n}\n\nexport function getPendingMigrations(db: DatabaseConnection): string[] {\n const currentVersion = getSchemaVersion(db);\n const migrations = getMigrationFiles();\n return migrations\n .filter(m => m.version > currentVersion)\n .map(m => `${m.version}_${m.name}`);\n}\n"],"names":["
|
|
1
|
+
{"version":3,"sources":["../../src/database/schema.ts"],"sourcesContent":["import { readFileSync, readdirSync } from 'fs';\nimport { dirname, join } from 'path';\nimport { fileURLToPath } from 'url';\nimport type { DatabaseConnection } from './connection.js';\n\nconst __dirname = dirname(fileURLToPath(import.meta.url));\n\nexport function getSchemaVersion(db: DatabaseConnection): number {\n const result = db.instance.pragma('user_version') as { user_version: number }[];\n return result[0]?.user_version ?? 0;\n}\n\nfunction setSchemaVersion(db: DatabaseConnection, version: number): void {\n db.instance.pragma(`user_version = ${version}`);\n}\n\nfunction getMigrationsDir(): string {\n // In production, migrations are in dist/database/migrations\n // In development/testing, they are in src/database/migrations\n const distPath = join(__dirname, 'migrations');\n return distPath;\n}\n\nfunction getMigrationFiles(): { version: number; path: string; name: string }[] {\n const migrationsDir = getMigrationsDir();\n\n let files: string[];\n try {\n files = readdirSync(migrationsDir)\n .filter(f => f.endsWith('.sql'))\n .sort();\n } catch {\n return [];\n }\n\n return files.map(file => {\n const match = file.match(/^(\\d+)_(.+)\\.sql$/);\n if (!match || !match[1] || !match[2]) {\n throw new Error(`Invalid migration filename: ${file}. Expected format: 001_name.sql`);\n }\n return {\n version: parseInt(match[1], 10),\n name: match[2],\n path: join(migrationsDir, file),\n };\n });\n}\n\nexport function initializeSchema(db: DatabaseConnection): void {\n const currentVersion = getSchemaVersion(db);\n const migrations = getMigrationFiles();\n\n const pendingMigrations = migrations.filter(m => m.version > currentVersion);\n\n if (pendingMigrations.length === 0) {\n return;\n }\n\n for (const migration of pendingMigrations) {\n const sql = readFileSync(migration.path, 'utf-8');\n\n db.transaction(() => {\n db.instance.exec(sql);\n setSchemaVersion(db, migration.version);\n });\n }\n}\n\nexport function resetSchema(db: DatabaseConnection): void {\n db.transaction(() => {\n db.execute('DROP TABLE IF EXISTS knowledge_fts');\n db.execute('DROP TABLE IF EXISTS knowledge');\n setSchemaVersion(db, 0);\n });\n\n initializeSchema(db);\n}\n\nexport function getPendingMigrations(db: DatabaseConnection): string[] {\n const currentVersion = getSchemaVersion(db);\n const migrations = getMigrationFiles();\n return migrations\n .filter(m => m.version > currentVersion)\n .map(m => `${m.version}_${m.name}`);\n}\n"],"names":["readFileSync","readdirSync","dirname","join","fileURLToPath","__dirname","url","getSchemaVersion","db","result","instance","pragma","user_version","setSchemaVersion","version","getMigrationsDir","distPath","getMigrationFiles","migrationsDir","files","filter","f","endsWith","sort","map","file","match","Error","parseInt","name","path","initializeSchema","currentVersion","migrations","pendingMigrations","m","length","migration","sql","transaction","exec","resetSchema","execute","getPendingMigrations"],"mappings":"AAAA,SAASA,YAAY,EAAEC,WAAW,QAAQ,KAAK;AAC/C,SAASC,OAAO,EAAEC,IAAI,QAAQ,OAAO;AACrC,SAASC,aAAa,QAAQ,MAAM;AAGpC,MAAMC,YAAYH,QAAQE,cAAc,YAAYE,GAAG;AAEvD,OAAO,SAASC,iBAAiBC,EAAsB;IACnD,MAAMC,SAASD,GAAGE,QAAQ,CAACC,MAAM,CAAC;IAClC,OAAOF,MAAM,CAAC,EAAE,EAAEG,gBAAgB;AACtC;AAEA,SAASC,iBAAiBL,EAAsB,EAAEM,OAAe;IAC7DN,GAAGE,QAAQ,CAACC,MAAM,CAAC,CAAC,eAAe,EAAEG,SAAS;AAClD;AAEA,SAASC;IACL,4DAA4D;IAC5D,8DAA8D;IAC9D,MAAMC,WAAWb,KAAKE,WAAW;IACjC,OAAOW;AACX;AAEA,SAASC;IACL,MAAMC,gBAAgBH;IAEtB,IAAII;IACJ,IAAI;QACAA,QAAQlB,YAAYiB,eACfE,MAAM,CAACC,CA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AGkB,gBACxBR,GAAG,CAACW,CAAAA,IAAK,GAAGA,EAAErB,OAAO,CAAC,CAAC,EAAEqB,EAAEN,IAAI,EAAE;AAC1C"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { SearchKnowledgeInput, SearchKnowledgeResult } from '../types';
|
|
1
|
+
import type { SearchKnowledgeInput, SearchKnowledgeResult } from '../types/index.js';
|
|
2
2
|
export declare function searchKnowledge(input: SearchKnowledgeInput): SearchKnowledgeResult;
|
|
3
3
|
//# sourceMappingURL=search.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/handlers/search.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../src/handlers/search.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAgBrF,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,GAAG,qBAAqB,CAwClF"}
|
package/dist/handlers/search.js
CHANGED
|
@@ -1,44 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
Object.defineProperty(exports, "searchKnowledge", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return searchKnowledge;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _database = require("../database");
|
|
12
|
-
const _search = require("../services/search");
|
|
13
|
-
const _ranker = require("../services/ranker");
|
|
14
|
-
const _errors = require("../utils/errors");
|
|
1
|
+
import { getDatabase } from '../database/index.js';
|
|
2
|
+
import { buildFtsQuery, buildSearchQuery, buildSimpleQuery } from '../services/search.js';
|
|
3
|
+
import { rankResults } from '../services/ranker.js';
|
|
4
|
+
import { ValidationError } from '../utils/errors.js';
|
|
15
5
|
const DEFAULT_LIMIT = 5;
|
|
16
6
|
const MAX_LIMIT = 20;
|
|
17
7
|
const MIN_QUERY_LENGTH = 3;
|
|
18
8
|
const MAX_QUERY_LENGTH = 500;
|
|
19
|
-
function searchKnowledge(input) {
|
|
9
|
+
export function searchKnowledge(input) {
|
|
20
10
|
validateSearchInput(input);
|
|
21
|
-
const db =
|
|
11
|
+
const db = getDatabase();
|
|
22
12
|
const limit = Math.min(Math.max(input.limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);
|
|
23
|
-
const ftsQuery =
|
|
13
|
+
const ftsQuery = buildFtsQuery(input.query);
|
|
24
14
|
let rows;
|
|
25
15
|
if (ftsQuery === '') {
|
|
26
16
|
// Empty or invalid query - return recent items
|
|
27
|
-
const { sql, params } =
|
|
17
|
+
const { sql, params } = buildSimpleQuery(input.scope, limit);
|
|
28
18
|
rows = db.query(sql, params);
|
|
29
19
|
} else {
|
|
30
20
|
// Full-text search with BM25
|
|
31
|
-
const { sql, params } =
|
|
21
|
+
const { sql, params } = buildSearchQuery(ftsQuery, input.scope, limit * 2);
|
|
32
22
|
try {
|
|
33
23
|
rows = db.query(sql, params);
|
|
34
24
|
} catch (error) {
|
|
35
25
|
// FTS query syntax error - fallback to simple query
|
|
36
|
-
const { sql: fallbackSql, params: fallbackParams } =
|
|
26
|
+
const { sql: fallbackSql, params: fallbackParams } = buildSimpleQuery(input.scope, limit);
|
|
37
27
|
rows = db.query(fallbackSql, fallbackParams);
|
|
38
28
|
}
|
|
39
29
|
}
|
|
40
30
|
// Apply ranking with tag and scope boosts
|
|
41
|
-
const ranked =
|
|
31
|
+
const ranked = rankResults(rows, {
|
|
42
32
|
contextTags: input.contextTags,
|
|
43
33
|
queryScope: input.scope
|
|
44
34
|
});
|
|
@@ -69,7 +59,7 @@ function validateSearchInput(input) {
|
|
|
69
59
|
}
|
|
70
60
|
}
|
|
71
61
|
if (errors.length > 0) {
|
|
72
|
-
throw new
|
|
62
|
+
throw new ValidationError(errors.join('; '), {
|
|
73
63
|
errors
|
|
74
64
|
});
|
|
75
65
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/handlers/search.ts"],"sourcesContent":["import { getDatabase } from '../database';\nimport { buildFtsQuery, buildSearchQuery, buildSimpleQuery } from '../services/search';\nimport { rankResults } from '../services/ranker';\nimport { ValidationError } from '../utils/errors';\nimport type { SearchKnowledgeInput, SearchKnowledgeResult } from '../types';\n\nconst DEFAULT_LIMIT = 5;\nconst MAX_LIMIT = 20;\nconst MIN_QUERY_LENGTH = 3;\nconst MAX_QUERY_LENGTH = 500;\n\ninterface RawSearchRow {\n id: string;\n title: string;\n content: string;\n tags: string;\n scope: string;\n bm25_score: number;\n}\n\nexport function searchKnowledge(input: SearchKnowledgeInput): SearchKnowledgeResult {\n validateSearchInput(input);\n\n const db = getDatabase();\n const limit = Math.min(Math.max(input.limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);\n const ftsQuery = buildFtsQuery(input.query);\n\n let rows: RawSearchRow[];\n\n if (ftsQuery === '') {\n // Empty or invalid query - return recent items\n const { sql, params } = buildSimpleQuery(input.scope, limit);\n rows = db.query<RawSearchRow>(sql, params);\n } else {\n // Full-text search with BM25\n const { sql, params } = buildSearchQuery(ftsQuery, input.scope, limit * 2);\n\n try {\n rows = db.query<RawSearchRow>(sql, params);\n } catch (error) {\n // FTS query syntax error - fallback to simple query\n const { sql: fallbackSql, params: fallbackParams } = buildSimpleQuery(input.scope, limit);\n rows = db.query<RawSearchRow>(fallbackSql, fallbackParams);\n }\n }\n\n // Apply ranking with tag and scope boosts\n const ranked = rankResults(rows, {\n contextTags: input.contextTags,\n queryScope: input.scope,\n });\n\n // Limit to requested count\n const results = ranked.slice(0, limit);\n\n return {\n results,\n totalMatches: ranked.length,\n query: input.query,\n };\n}\n\nfunction validateSearchInput(input: SearchKnowledgeInput): void {\n const errors: string[] = [];\n\n if (!input.query || typeof input.query !== 'string') {\n errors.push('Query is required');\n } else {\n const trimmed = input.query.trim();\n if (trimmed.length < MIN_QUERY_LENGTH) {\n errors.push(`Query must be at least ${MIN_QUERY_LENGTH} characters`);\n }\n if (trimmed.length > MAX_QUERY_LENGTH) {\n errors.push(`Query must be at most ${MAX_QUERY_LENGTH} characters`);\n }\n }\n\n if (input.limit !== undefined) {\n if (typeof input.limit !== 'number' || input.limit < 1) {\n errors.push('Limit must be a positive number');\n }\n }\n\n if (errors.length > 0) {\n throw new ValidationError(errors.join('; '), { errors });\n }\n}\n"],"names":["
|
|
1
|
+
{"version":3,"sources":["../../src/handlers/search.ts"],"sourcesContent":["import { getDatabase } from '../database/index.js';\nimport { buildFtsQuery, buildSearchQuery, buildSimpleQuery } from '../services/search.js';\nimport { rankResults } from '../services/ranker.js';\nimport { ValidationError } from '../utils/errors.js';\nimport type { SearchKnowledgeInput, SearchKnowledgeResult } from '../types/index.js';\n\nconst DEFAULT_LIMIT = 5;\nconst MAX_LIMIT = 20;\nconst MIN_QUERY_LENGTH = 3;\nconst MAX_QUERY_LENGTH = 500;\n\ninterface RawSearchRow {\n id: string;\n title: string;\n content: string;\n tags: string;\n scope: string;\n bm25_score: number;\n}\n\nexport function searchKnowledge(input: SearchKnowledgeInput): SearchKnowledgeResult {\n validateSearchInput(input);\n\n const db = getDatabase();\n const limit = Math.min(Math.max(input.limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);\n const ftsQuery = buildFtsQuery(input.query);\n\n let rows: RawSearchRow[];\n\n if (ftsQuery === '') {\n // Empty or invalid query - return recent items\n const { sql, params } = buildSimpleQuery(input.scope, limit);\n rows = db.query<RawSearchRow>(sql, params);\n } else {\n // Full-text search with BM25\n const { sql, params } = buildSearchQuery(ftsQuery, input.scope, limit * 2);\n\n try {\n rows = db.query<RawSearchRow>(sql, params);\n } catch (error) {\n // FTS query syntax error - fallback to simple query\n const { sql: fallbackSql, params: fallbackParams } = buildSimpleQuery(input.scope, limit);\n rows = db.query<RawSearchRow>(fallbackSql, fallbackParams);\n }\n }\n\n // Apply ranking with tag and scope boosts\n const ranked = rankResults(rows, {\n contextTags: input.contextTags,\n queryScope: input.scope,\n });\n\n // Limit to requested count\n const results = ranked.slice(0, limit);\n\n return {\n results,\n totalMatches: ranked.length,\n query: input.query,\n };\n}\n\nfunction validateSearchInput(input: SearchKnowledgeInput): void {\n const errors: string[] = [];\n\n if (!input.query || typeof input.query !== 'string') {\n errors.push('Query is required');\n } else {\n const trimmed = input.query.trim();\n if (trimmed.length < MIN_QUERY_LENGTH) {\n errors.push(`Query must be at least ${MIN_QUERY_LENGTH} characters`);\n }\n if (trimmed.length > MAX_QUERY_LENGTH) {\n errors.push(`Query must be at most ${MAX_QUERY_LENGTH} characters`);\n }\n }\n\n if (input.limit !== undefined) {\n if (typeof input.limit !== 'number' || input.limit < 1) {\n errors.push('Limit must be a positive number');\n }\n }\n\n if (errors.length > 0) {\n throw new ValidationError(errors.join('; '), { errors });\n }\n}\n"],"names":["getDatabase","buildFtsQuery","buildSearchQuery","buildSimpleQuery","rankResults","ValidationError","DEFAULT_LIMIT","MAX_LIMIT","MIN_QUERY_LENGTH","MAX_QUERY_LENGTH","searchKnowledge","input","validateSearchInput","db","limit","Math","min","max","ftsQuery","query","rows","sql","params","scope","error","fallbackSql","fallbackParams","ranked","contextTags","queryScope","results","slice","totalMatches","length","errors","push","trimmed","trim","undefined","join"],"mappings":"AAAA,SAASA,WAAW,QAAQ,uBAAuB;AACnD,SAASC,aAAa,EAAEC,gBAAgB,EAAEC,gBAAgB,QAAQ,wBAAwB;AAC1F,SAASC,WAAW,QAAQ,wBAAwB;AACpD,SAASC,eAAe,QAAQ,qBAAqB;AAGrD,MAAMC,gBAAgB;AACtB,MAAMC,YAAY;AAClB,MAAMC,mBAAmB;AACzB,MAAMC,mBAAmB;AAWzB,OAAO,SAASC,gBAAgBC,KAA2B;IACvDC,oBAAoBD;IAEpB,MAAME,KAAKb;IACX,MAAMc,QAAQC,KAAKC,GAAG,CAACD,KAAKE,GAAG,CAACN,M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vE;QACA,IAAI4B,QAAQH,MAAM,GAAGxB,kBAAkB;YACnCyB,OAAOC,IAAI,CAAC,CAAC,sBAAsB,EAAE1B,iBAAiB,WAAW,CAAC;QACtE;IACJ;IAEA,IAAIE,MAAMG,KAAK,KAAKwB,WAAW;QAC3B,IAAI,OAAO3B,MAAMG,KAAK,KAAK,YAAYH,MAAMG,KAAK,GAAG,GAAG;YACpDoB,OAAOC,IAAI,CAAC;QAChB;IACJ;IAEA,IAAID,OAAOD,MAAM,GAAG,GAAG;QACnB,MAAM,IAAI5B,gBAAgB6B,OAAOK,IAAI,CAAC,OAAO;YAAEL;QAAO;IAC1D;AACJ"}
|
package/dist/handlers/store.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { StoreKnowledgeInput, StoreKnowledgeResult } from '../types';
|
|
1
|
+
import type { StoreKnowledgeInput, StoreKnowledgeResult } from '../types/index.js';
|
|
2
2
|
export declare function storeKnowledge(input: StoreKnowledgeInput): StoreKnowledgeResult;
|
|
3
3
|
//# sourceMappingURL=store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/handlers/store.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAgB,MAAM,
|
|
1
|
+
{"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/handlers/store.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAgB,MAAM,mBAAmB,CAAC;AAEjG,wBAAgB,cAAc,CAAC,KAAK,EAAE,mBAAmB,GAAG,oBAAoB,CAwE/E"}
|