1
0

index.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Opt-in SQLite persistence provider. Logical sessions remain unchanged;
  3. * the physical backend packs eligible chunk runs into schema-17 rows.
  4. * @module @deepseek-ai/dsh-session-persistence-sqlite
  5. */
  6. import { Context, Service } from '@deepseek-ai/cordis'
  7. import z from '@deepseek-ai/schemastery'
  8. import type {
  9. SessionEvent,
  10. SessionHeader,
  11. SessionId,
  12. SessionPreparation,
  13. } from '@deepseek-ai/dsh-session'
  14. import {
  15. DEFAULT_PREPARED_SESSION_CACHE_SIZE,
  16. DEFAULT_WRITE_BATCH_MAX_DELAY_MS,
  17. MAX_WRITE_BATCH_DELAY_MS,
  18. PersistenceCoordinator,
  19. SessionPersistence,
  20. type SessionInspection,
  21. type SessionLocation,
  22. type SessionPersistenceSnapshot,
  23. } from '@deepseek-ai/dsh-session-persistence'
  24. import type { JournalMode } from './schema.ts'
  25. import { SqliteStore } from './store.ts'
  26. export { SCHEMA_VERSION } from './schema.ts'
  27. /** Default wait for another SQLite connection's write reservation. */
  28. export const DEFAULT_BUSY_TIMEOUT_MS = 5_000
  29. /** Largest busy timeout accepted by SQLite's signed millisecond interface. */
  30. export const MAX_BUSY_TIMEOUT_MS = 2_147_483_647
  31. /** Plugin configuration. */
  32. export interface Config {
  33. /** SQLite database path, or `:memory:` for an in-process database. */
  34. path: string
  35. /** Durable SQLite journal mode; defaults to `wal`. */
  36. journalMode?: JournalMode
  37. /** Maximum wait for another SQLite connection's lock; defaults to 5,000 ms. */
  38. busyTimeoutMs?: number
  39. /** Maximum cold Session preparations retained for history-to-resume reuse. */
  40. preparedSessionCacheSize?: number
  41. /** Fixed live-event coalescing window; not a backend completion deadline. */
  42. writeBatchMaxDelayMs?: number
  43. }
  44. /**
  45. * SQLite `SessionPersistence` provider with a schema-owned physical codec.
  46. */
  47. export class SqliteSessionPersistence extends SessionPersistence {
  48. override readonly supportsRawArtifacts = false
  49. override readonly name = 'session-persistence-sqlite'
  50. static inject = ['sessions']
  51. static Config: z<Config> = z.object({
  52. path: z.string().required(),
  53. journalMode: z.union(['wal', 'delete', 'truncate', 'persist'] as const).default('wal'),
  54. busyTimeoutMs: z.number().step(1).min(0).max(MAX_BUSY_TIMEOUT_MS).default(DEFAULT_BUSY_TIMEOUT_MS),
  55. preparedSessionCacheSize: z.number().step(1).min(1).default(DEFAULT_PREPARED_SESSION_CACHE_SIZE),
  56. writeBatchMaxDelayMs: z.number().step(1).min(1).max(MAX_WRITE_BATCH_DELAY_MS)
  57. .default(DEFAULT_WRITE_BATCH_MAX_DELAY_MS),
  58. })
  59. private readonly store: SqliteStore
  60. private readonly coordinator: PersistenceCoordinator<number>
  61. constructor(ctx: Context, public config: Config) {
  62. super(ctx)
  63. const preparedSessionCacheSize = config.preparedSessionCacheSize
  64. ?? DEFAULT_PREPARED_SESSION_CACHE_SIZE
  65. const writeBatchMaxDelayMs = config.writeBatchMaxDelayMs
  66. ?? DEFAULT_WRITE_BATCH_MAX_DELAY_MS
  67. this.store = new SqliteStore({
  68. path: config.path,
  69. journalMode: config.journalMode ?? 'wal',
  70. busyTimeoutMs: config.busyTimeoutMs ?? DEFAULT_BUSY_TIMEOUT_MS,
  71. })
  72. this.coordinator = new PersistenceCoordinator(this.ctx, this.store, {
  73. preparedSessionCacheSize,
  74. writeBatchMaxDelayMs,
  75. })
  76. }
  77. /** Reject self-contained path and ownership failures without loading Node SQLite. */
  78. protected async [Service.init](): Promise<void> {
  79. await this.store.validatePath()
  80. }
  81. /** SQLite has one database, not an independent per-session artifact. */
  82. locate(_meta: SessionHeader): SessionLocation | undefined {
  83. return undefined
  84. }
  85. create(meta: SessionHeader): Promise<void> {
  86. return this.coordinator.create(meta)
  87. }
  88. append(id: SessionId, events: readonly SessionEvent[]): Promise<void> {
  89. return this.coordinator.append(id, events)
  90. }
  91. override prepare(id: SessionId, signal?: AbortSignal): Promise<SessionPreparation> {
  92. return this.coordinator.prepare(id, signal)
  93. }
  94. load(id: SessionId): Promise<SessionInspection> {
  95. return this.coordinator.load(id)
  96. }
  97. inspect(id: SessionId, signal?: AbortSignal): Promise<SessionInspection> {
  98. return this.coordinator.inspect(id, signal)
  99. }
  100. readFrom(
  101. id: SessionId,
  102. fromSeq: number,
  103. signal?: AbortSignal,
  104. ): Promise<{ meta: SessionHeader; events: SessionEvent[] }> {
  105. return this.coordinator.readFrom(id, fromSeq, signal)
  106. }
  107. list(signal?: AbortSignal): Promise<SessionHeader[]> {
  108. return this.store.list(signal)
  109. }
  110. listSnapshots(signal?: AbortSignal): Promise<SessionPersistenceSnapshot[]> {
  111. return this.store.listSnapshots(signal)
  112. }
  113. }
  114. export default SqliteSessionPersistence