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.
Files changed (67) hide show
  1. package/README.md +71 -15
  2. package/cordis.patch.yml +0 -1
  3. package/lib/bailian-client.js +8 -3
  4. package/lib/bailian-client.js.map +1 -1
  5. package/lib/client/choices.d.ts +53 -0
  6. package/lib/client/choices.js +51 -0
  7. package/lib/client/choices.js.map +1 -0
  8. package/lib/client/controller.d.ts +40 -0
  9. package/lib/client/controller.js +130 -0
  10. package/lib/client/controller.js.map +1 -0
  11. package/lib/client/index.d.ts +5 -0
  12. package/lib/client/index.js +19 -0
  13. package/lib/client/index.js.map +1 -0
  14. package/lib/client/page.d.ts +5 -0
  15. package/lib/client/page.js +24 -0
  16. package/lib/client/page.js.map +1 -0
  17. package/lib/client.js +430 -0
  18. package/lib/client.js.map +7 -0
  19. package/lib/config.d.ts +63 -1
  20. package/lib/config.js +33 -4
  21. package/lib/config.js.map +1 -1
  22. package/lib/diary-runtime.d.ts +10 -0
  23. package/lib/diary-runtime.js +65 -0
  24. package/lib/diary-runtime.js.map +1 -0
  25. package/lib/index.d.ts +3 -3
  26. package/lib/index.js +7 -2
  27. package/lib/index.js.map +1 -1
  28. package/lib/memory-store.d.ts +6 -3
  29. package/lib/memory-store.js +6 -3
  30. package/lib/memory-store.js.map +1 -1
  31. package/lib/model-parameters.d.ts +7 -0
  32. package/lib/model-parameters.js +38 -0
  33. package/lib/model-parameters.js.map +1 -0
  34. package/lib/orchestrator.js +4 -66
  35. package/lib/orchestrator.js.map +1 -1
  36. package/lib/retrieval-service.d.ts +12 -3
  37. package/lib/retrieval-service.js +25 -13
  38. package/lib/retrieval-service.js.map +1 -1
  39. package/lib/session-memory.d.ts +20 -0
  40. package/lib/session-memory.js +161 -0
  41. package/lib/session-memory.js.map +1 -0
  42. package/lib/session-selection.d.ts +1 -0
  43. package/lib/session-selection.js +6 -0
  44. package/lib/session-selection.js.map +1 -0
  45. package/lib/sqlite.d.ts +25 -0
  46. package/lib/sqlite.js +64 -0
  47. package/lib/sqlite.js.map +1 -0
  48. package/lib/tools.d.ts +1 -0
  49. package/lib/tools.js +3 -0
  50. package/lib/tools.js.map +1 -1
  51. package/package.json +140 -26
  52. package/src/bailian-client.ts +7 -3
  53. package/src/client/choices.ts +66 -0
  54. package/src/client/controller.ts +108 -0
  55. package/src/client/index.ts +26 -0
  56. package/src/client/page.tsx +100 -0
  57. package/src/config.ts +41 -4
  58. package/src/diary-runtime.ts +62 -0
  59. package/src/index.ts +11 -5
  60. package/src/memory-store.ts +10 -6
  61. package/src/model-parameters.ts +32 -0
  62. package/src/orchestrator.ts +4 -57
  63. package/src/retrieval-service.ts +27 -14
  64. package/src/session-memory.ts +151 -0
  65. package/src/session-selection.ts +4 -0
  66. package/src/sqlite.ts +78 -0
  67. 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: '想起我们之前的经历、称呼、偏好、情绪变化、约定或我自己的反思。输入自然语言,会检索历史日记并按相关性重排。日记中的主观感受和不确定解释不等于已确认事实。',