dsh-diary-memory 0.1.0 → 0.1.2
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/README.md +71 -15
- package/cordis.patch.yml +0 -1
- package/lib/bailian-client.js +8 -3
- package/lib/bailian-client.js.map +1 -1
- package/lib/client/choices.d.ts +53 -0
- package/lib/client/choices.js +51 -0
- package/lib/client/choices.js.map +1 -0
- package/lib/client/controller.d.ts +40 -0
- package/lib/client/controller.js +130 -0
- package/lib/client/controller.js.map +1 -0
- package/lib/client/index.d.ts +5 -0
- package/lib/client/index.js +19 -0
- package/lib/client/index.js.map +1 -0
- package/lib/client/page.d.ts +5 -0
- package/lib/client/page.js +24 -0
- package/lib/client/page.js.map +1 -0
- package/lib/client.js +430 -0
- package/lib/client.js.map +7 -0
- package/lib/config.d.ts +63 -1
- package/lib/config.js +33 -4
- package/lib/config.js.map +1 -1
- package/lib/diary-runtime.d.ts +10 -0
- package/lib/diary-runtime.js +65 -0
- package/lib/diary-runtime.js.map +1 -0
- package/lib/index.d.ts +3 -3
- package/lib/index.js +7 -2
- package/lib/index.js.map +1 -1
- package/lib/memory-store.d.ts +6 -3
- package/lib/memory-store.js +6 -3
- package/lib/memory-store.js.map +1 -1
- package/lib/model-parameters.d.ts +7 -0
- package/lib/model-parameters.js +38 -0
- package/lib/model-parameters.js.map +1 -0
- package/lib/orchestrator.js +4 -66
- package/lib/orchestrator.js.map +1 -1
- package/lib/retrieval-service.d.ts +12 -3
- package/lib/retrieval-service.js +25 -13
- package/lib/retrieval-service.js.map +1 -1
- package/lib/session-memory.d.ts +20 -0
- package/lib/session-memory.js +161 -0
- package/lib/session-memory.js.map +1 -0
- package/lib/session-selection.d.ts +1 -0
- package/lib/session-selection.js +6 -0
- package/lib/session-selection.js.map +1 -0
- package/lib/sqlite.d.ts +25 -0
- package/lib/sqlite.js +64 -0
- package/lib/sqlite.js.map +1 -0
- package/lib/tools.d.ts +1 -0
- package/lib/tools.js +3 -0
- package/lib/tools.js.map +1 -1
- package/package.json +140 -26
- package/src/bailian-client.ts +7 -3
- package/src/client/choices.ts +66 -0
- package/src/client/controller.ts +108 -0
- package/src/client/index.ts +26 -0
- package/src/client/page.tsx +100 -0
- package/src/config.ts +41 -4
- package/src/diary-runtime.ts +62 -0
- package/src/index.ts +11 -5
- package/src/memory-store.ts +10 -6
- package/src/model-parameters.ts +32 -0
- package/src/orchestrator.ts +4 -57
- package/src/retrieval-service.ts +27 -14
- package/src/session-memory.ts +151 -0
- package/src/session-selection.ts +4 -0
- package/src/sqlite.ts +78 -0
- package/src/tools.ts +4 -0
package/src/sqlite.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import type { DatabaseSync, SQLInputValue } from 'node:sqlite'
|
|
3
|
+
|
|
4
|
+
export type SqliteDriver = 'auto' | 'builtin' | 'native'
|
|
5
|
+
export interface SqliteStatement {
|
|
6
|
+
run(...parameters: SQLInputValue[]): { changes: number | bigint; lastInsertRowid: number | bigint }
|
|
7
|
+
get(...parameters: SQLInputValue[]): Record<string, unknown> | undefined
|
|
8
|
+
all(...parameters: SQLInputValue[]): Record<string, unknown>[]
|
|
9
|
+
iterate(...parameters: SQLInputValue[]): IterableIterator<Record<string, unknown>>
|
|
10
|
+
}
|
|
11
|
+
export interface SqliteConnection {
|
|
12
|
+
readonly open: boolean
|
|
13
|
+
exec(sql: string): unknown
|
|
14
|
+
prepare(sql: string): SqliteStatement
|
|
15
|
+
pragma(sql: string, options?: { simple: boolean }): unknown
|
|
16
|
+
transaction<T>(operation: () => T): () => T
|
|
17
|
+
close(): unknown
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const require = createRequire(import.meta.url)
|
|
21
|
+
|
|
22
|
+
/** Use the same SQLite file format without requiring an Electron-specific addon. */
|
|
23
|
+
function builtinDatabase(filename: string): SqliteConnection {
|
|
24
|
+
const { DatabaseSync: Database } = require('node:sqlite') as { DatabaseSync: typeof DatabaseSync }
|
|
25
|
+
const database = new Database(filename)
|
|
26
|
+
let open = true
|
|
27
|
+
let transactionId = 0
|
|
28
|
+
const normalize = (row: Record<string, unknown> | undefined) => row === undefined ? undefined
|
|
29
|
+
: Object.fromEntries(Object.entries(row).map(([key, value]) => [key, value instanceof Uint8Array ? Buffer.from(value) : value]))
|
|
30
|
+
return {
|
|
31
|
+
get open() { return open },
|
|
32
|
+
exec: sql => database.exec(sql),
|
|
33
|
+
prepare(sql) {
|
|
34
|
+
const statement = database.prepare(sql)
|
|
35
|
+
return {
|
|
36
|
+
run: (...parameters) => statement.run(...parameters),
|
|
37
|
+
get: (...parameters) => normalize(statement.get(...parameters)),
|
|
38
|
+
all: (...parameters) => statement.all(...parameters).map(row => normalize(row)!),
|
|
39
|
+
*iterate(...parameters) { for (const row of statement.iterate(...parameters)) yield normalize(row)! },
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
pragma(sql, options) {
|
|
43
|
+
const rows = database.prepare(`PRAGMA ${sql}`).all()
|
|
44
|
+
return options?.simple ? Object.values(rows[0] ?? {})[0] : rows
|
|
45
|
+
},
|
|
46
|
+
transaction: operation => () => {
|
|
47
|
+
// Savepoints also give nested synchronous operations rollback isolation.
|
|
48
|
+
const point = `diary_${++transactionId}`
|
|
49
|
+
database.exec(`SAVEPOINT ${point}`)
|
|
50
|
+
try {
|
|
51
|
+
const result = operation()
|
|
52
|
+
if (result instanceof Promise) throw new TypeError('SQLite 事务必须同步完成')
|
|
53
|
+
database.exec(`RELEASE ${point}`)
|
|
54
|
+
return result
|
|
55
|
+
} catch (error) {
|
|
56
|
+
database.exec(`ROLLBACK TO ${point}; RELEASE ${point}`)
|
|
57
|
+
throw error
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
close() { database.close(); open = false },
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function openDatabase(filename: string, driver: SqliteDriver = 'auto'): { db: SqliteConnection; backend: 'better-sqlite3' | 'node:sqlite' } {
|
|
65
|
+
if (driver !== 'builtin') {
|
|
66
|
+
try {
|
|
67
|
+
const Database = require('better-sqlite3') as typeof import('better-sqlite3')
|
|
68
|
+
return { db: new Database(filename) as unknown as SqliteConnection, backend: 'better-sqlite3' }
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const failure = error as NodeJS.ErrnoException
|
|
71
|
+
const unavailable = failure.code === 'MODULE_NOT_FOUND' || failure.code === 'ERR_DLOPEN_FAILED'
|
|
72
|
+
|| failure.message.startsWith('Could not locate the bindings file.')
|
|
73
|
+
// Disk, SQL, permission and corruption failures must never select another backend.
|
|
74
|
+
if (driver === 'native' || !unavailable) throw error
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return { db: builtinDatabase(filename), backend: 'node:sqlite' }
|
|
78
|
+
}
|
package/src/tools.ts
CHANGED
|
@@ -6,6 +6,10 @@ export const inject = ['tools', 'diaryMemory']
|
|
|
6
6
|
|
|
7
7
|
/** Model-visible read-only recall, scoped by the companion preset and namespace. */
|
|
8
8
|
export function apply(ctx: Context): void {
|
|
9
|
+
registerRecall(ctx)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function registerRecall(ctx: Context): void {
|
|
9
13
|
ctx.tools.register({
|
|
10
14
|
name: 'diary_recall',
|
|
11
15
|
description: '想起我们之前的经历、称呼、偏好、情绪变化、约定或我自己的反思。输入自然语言,会检索历史日记并按相关性重排。日记中的主观感受和不确定解释不等于已确认事实。',
|