@chude/memory 4.0.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.
Files changed (184) hide show
  1. package/README.md +294 -0
  2. package/dist/application/index.d.ts +6 -0
  3. package/dist/application/services/ambient-context-service.d.ts +90 -0
  4. package/dist/application/services/backfill-service.d.ts +71 -0
  5. package/dist/application/services/budget-allocator.d.ts +57 -0
  6. package/dist/application/services/embedding-service.d.ts +131 -0
  7. package/dist/application/services/export-service.d.ts +225 -0
  8. package/dist/application/services/extraction-pipeline.d.ts +50 -0
  9. package/dist/application/services/friction-service.d.ts +148 -0
  10. package/dist/application/services/fts-sanitizer.d.ts +25 -0
  11. package/dist/application/services/index.d.ts +34 -0
  12. package/dist/application/services/llm-extractor.d.ts +96 -0
  13. package/dist/application/services/memory-file-sync-service.d.ts +58 -0
  14. package/dist/application/services/pattern-extractor.d.ts +95 -0
  15. package/dist/application/services/recovery-service.d.ts +81 -0
  16. package/dist/application/services/rrf-fusion.d.ts +53 -0
  17. package/dist/application/services/smart-context-service.d.ts +126 -0
  18. package/dist/application/services/sync-service.d.ts +157 -0
  19. package/dist/application/services/temporal-decay.d.ts +62 -0
  20. package/dist/domain/entities/backfill-state.d.ts +56 -0
  21. package/dist/domain/entities/entity.d.ts +131 -0
  22. package/dist/domain/entities/extraction-state.d.ts +128 -0
  23. package/dist/domain/entities/fact.d.ts +59 -0
  24. package/dist/domain/entities/friction-entry.d.ts +84 -0
  25. package/dist/domain/entities/index.d.ts +15 -0
  26. package/dist/domain/entities/link.d.ts +74 -0
  27. package/dist/domain/entities/memory-file.d.ts +78 -0
  28. package/dist/domain/entities/message.d.ts +70 -0
  29. package/dist/domain/entities/session.d.ts +93 -0
  30. package/dist/domain/entities/tool-use.d.ts +85 -0
  31. package/dist/domain/errors/error-codes.d.ts +37 -0
  32. package/dist/domain/errors/index.d.ts +8 -0
  33. package/dist/domain/errors/memory-error.d.ts +52 -0
  34. package/dist/domain/errors/unknown-error.d.ts +9 -0
  35. package/dist/domain/index.d.ts +11 -0
  36. package/dist/domain/ports/embedding.d.ts +96 -0
  37. package/dist/domain/ports/extraction.d.ts +13 -0
  38. package/dist/domain/ports/index.d.ts +14 -0
  39. package/dist/domain/ports/redactor.d.ts +17 -0
  40. package/dist/domain/ports/repositories.d.ts +658 -0
  41. package/dist/domain/ports/services.d.ts +180 -0
  42. package/dist/domain/ports/signals.d.ts +82 -0
  43. package/dist/domain/ports/sources.d.ts +122 -0
  44. package/dist/domain/ports/types.d.ts +150 -0
  45. package/dist/domain/services/content-extractor.d.ts +61 -0
  46. package/dist/domain/services/index.d.ts +8 -0
  47. package/dist/domain/services/path-decoder.d.ts +56 -0
  48. package/dist/domain/services/query-parser.d.ts +47 -0
  49. package/dist/domain/value-objects/embedding-config.d.ts +56 -0
  50. package/dist/domain/value-objects/embedding-result.d.ts +46 -0
  51. package/dist/domain/value-objects/index.d.ts +10 -0
  52. package/dist/domain/value-objects/project-path.d.ts +92 -0
  53. package/dist/domain/value-objects/search-query.d.ts +30 -0
  54. package/dist/domain/value-objects/search-result.d.ts +92 -0
  55. package/dist/index.d.ts +15 -0
  56. package/dist/index.js +1548 -0
  57. package/dist/infrastructure/database/connection.d.ts +161 -0
  58. package/dist/infrastructure/database/event-log.d.ts +22 -0
  59. package/dist/infrastructure/database/health-checker.d.ts +248 -0
  60. package/dist/infrastructure/database/index.d.ts +11 -0
  61. package/dist/infrastructure/database/repositories/backfill-state-repository.d.ts +19 -0
  62. package/dist/infrastructure/database/repositories/embedding-repository.d.ts +121 -0
  63. package/dist/infrastructure/database/repositories/entity-repository.d.ts +73 -0
  64. package/dist/infrastructure/database/repositories/extraction-log-repository.d.ts +17 -0
  65. package/dist/infrastructure/database/repositories/extraction-state-repository.d.ts +52 -0
  66. package/dist/infrastructure/database/repositories/fact-repository.d.ts +25 -0
  67. package/dist/infrastructure/database/repositories/friction-repository.d.ts +41 -0
  68. package/dist/infrastructure/database/repositories/index.d.ts +17 -0
  69. package/dist/infrastructure/database/repositories/link-repository.d.ts +64 -0
  70. package/dist/infrastructure/database/repositories/memory-file-repository.d.ts +28 -0
  71. package/dist/infrastructure/database/repositories/message-repository.d.ts +87 -0
  72. package/dist/infrastructure/database/repositories/session-repository.d.ts +125 -0
  73. package/dist/infrastructure/database/repositories/tool-use-repository.d.ts +72 -0
  74. package/dist/infrastructure/database/schema.d.ts +203 -0
  75. package/dist/infrastructure/database/services/context-service.d.ts +93 -0
  76. package/dist/infrastructure/database/services/hybrid-search-service.d.ts +156 -0
  77. package/dist/infrastructure/database/services/index.d.ts +10 -0
  78. package/dist/infrastructure/database/services/search-service.d.ts +57 -0
  79. package/dist/infrastructure/database/services/stats-service.d.ts +36 -0
  80. package/dist/infrastructure/embedding/background-embedder.d.ts +125 -0
  81. package/dist/infrastructure/embedding/embedding-provider-factory.d.ts +44 -0
  82. package/dist/infrastructure/embedding/index.d.ts +5 -0
  83. package/dist/infrastructure/embedding/ollama-provider.d.ts +41 -0
  84. package/dist/infrastructure/embedding/openai-provider.d.ts +38 -0
  85. package/dist/infrastructure/embedding/transformers-js-provider.d.ts +34 -0
  86. package/dist/infrastructure/external/index.d.ts +7 -0
  87. package/dist/infrastructure/external/qmd-runner.d.ts +36 -0
  88. package/dist/infrastructure/hooks/auto-memory-writer.d.ts +52 -0
  89. package/dist/infrastructure/hooks/config-manager.d.ts +237 -0
  90. package/dist/infrastructure/hooks/git-syncer.d.ts +44 -0
  91. package/dist/infrastructure/hooks/hook-runner.d.ts +126 -0
  92. package/dist/infrastructure/hooks/index.d.ts +12 -0
  93. package/dist/infrastructure/hooks/log-writer.d.ts +106 -0
  94. package/dist/infrastructure/hooks/settings-manager.d.ts +163 -0
  95. package/dist/infrastructure/hooks/sync-hook-script.d.ts +83 -0
  96. package/dist/infrastructure/hooks/sync-logger-adapter.d.ts +17 -0
  97. package/dist/infrastructure/index.d.ts +11 -0
  98. package/dist/infrastructure/llm/anthropic-extractor.d.ts +20 -0
  99. package/dist/infrastructure/llm/claude-cli-extractor.d.ts +14 -0
  100. package/dist/infrastructure/llm/claude-summary-generator.d.ts +14 -0
  101. package/dist/infrastructure/llm/extraction-helper.d.ts +16 -0
  102. package/dist/infrastructure/llm/ollama-extractor.d.ts +20 -0
  103. package/dist/infrastructure/llm/openai-extractor.d.ts +23 -0
  104. package/dist/infrastructure/migration.d.ts +103 -0
  105. package/dist/infrastructure/parsers/event-classifier.d.ts +111 -0
  106. package/dist/infrastructure/parsers/index.d.ts +8 -0
  107. package/dist/infrastructure/parsers/jsonl-parser.d.ts +25 -0
  108. package/dist/infrastructure/parsers/timestamp.d.ts +18 -0
  109. package/dist/infrastructure/paths.d.ts +129 -0
  110. package/dist/infrastructure/providers/provider-defaults.d.ts +11 -0
  111. package/dist/infrastructure/providers/provider-registry.d.ts +28 -0
  112. package/dist/infrastructure/security/pattern-redactor.d.ts +6 -0
  113. package/dist/infrastructure/signals/adapters.d.ts +27 -0
  114. package/dist/infrastructure/signals/checkpoint-manager.d.ts +83 -0
  115. package/dist/infrastructure/signals/index.d.ts +8 -0
  116. package/dist/infrastructure/signals/signal-handler.d.ts +113 -0
  117. package/dist/infrastructure/sources/index.d.ts +8 -0
  118. package/dist/infrastructure/sources/memory-file-scanner.d.ts +23 -0
  119. package/dist/infrastructure/sources/project-name-resolver.d.ts +67 -0
  120. package/dist/infrastructure/sources/session-source.d.ts +70 -0
  121. package/dist/presentation/cli/command-result.d.ts +10 -0
  122. package/dist/presentation/cli/commands/_helpers/capture-json.d.ts +36 -0
  123. package/dist/presentation/cli/commands/_helpers/deprecation-warning.d.ts +41 -0
  124. package/dist/presentation/cli/commands/backfill.d.ts +56 -0
  125. package/dist/presentation/cli/commands/browse.d.ts +55 -0
  126. package/dist/presentation/cli/commands/completion.d.ts +61 -0
  127. package/dist/presentation/cli/commands/context.d.ts +53 -0
  128. package/dist/presentation/cli/commands/doctor.d.ts +55 -0
  129. package/dist/presentation/cli/commands/export.d.ts +36 -0
  130. package/dist/presentation/cli/commands/extract.d.ts +40 -0
  131. package/dist/presentation/cli/commands/facts.d.ts +17 -0
  132. package/dist/presentation/cli/commands/friction/dashboard.d.ts +18 -0
  133. package/dist/presentation/cli/commands/friction/index.d.ts +12 -0
  134. package/dist/presentation/cli/commands/friction/list.d.ts +12 -0
  135. package/dist/presentation/cli/commands/friction/log.d.ts +12 -0
  136. package/dist/presentation/cli/commands/friction/purge.d.ts +12 -0
  137. package/dist/presentation/cli/commands/friction/resolve.d.ts +12 -0
  138. package/dist/presentation/cli/commands/friction/types.d.ts +86 -0
  139. package/dist/presentation/cli/commands/friction/wontfix.d.ts +12 -0
  140. package/dist/presentation/cli/commands/import.d.ts +38 -0
  141. package/dist/presentation/cli/commands/index.d.ts +51 -0
  142. package/dist/presentation/cli/commands/install.d.ts +67 -0
  143. package/dist/presentation/cli/commands/list.d.ts +61 -0
  144. package/dist/presentation/cli/commands/migrate.d.ts +36 -0
  145. package/dist/presentation/cli/commands/purge.d.ts +100 -0
  146. package/dist/presentation/cli/commands/query.d.ts +51 -0
  147. package/dist/presentation/cli/commands/related.d.ts +47 -0
  148. package/dist/presentation/cli/commands/remote.d.ts +36 -0
  149. package/dist/presentation/cli/commands/search.d.ts +100 -0
  150. package/dist/presentation/cli/commands/show.d.ts +51 -0
  151. package/dist/presentation/cli/commands/stats.d.ts +38 -0
  152. package/dist/presentation/cli/commands/status.d.ts +152 -0
  153. package/dist/presentation/cli/commands/sync/ambient.d.ts +22 -0
  154. package/dist/presentation/cli/commands/sync/background.d.ts +23 -0
  155. package/dist/presentation/cli/commands/sync/embedding-pass.d.ts +32 -0
  156. package/dist/presentation/cli/commands/sync/helpers.d.ts +25 -0
  157. package/dist/presentation/cli/commands/sync/index.d.ts +17 -0
  158. package/dist/presentation/cli/commands/sync/memory-files.d.ts +26 -0
  159. package/dist/presentation/cli/commands/sync/types.d.ts +163 -0
  160. package/dist/presentation/cli/commands/uninstall.d.ts +44 -0
  161. package/dist/presentation/cli/db-startup.d.ts +61 -0
  162. package/dist/presentation/cli/formatters/ai-formatter.d.ts +38 -0
  163. package/dist/presentation/cli/formatters/color.d.ts +82 -0
  164. package/dist/presentation/cli/formatters/context-formatter.d.ts +55 -0
  165. package/dist/presentation/cli/formatters/dto-helpers.d.ts +176 -0
  166. package/dist/presentation/cli/formatters/envelope.d.ts +136 -0
  167. package/dist/presentation/cli/formatters/error-formatter.d.ts +41 -0
  168. package/dist/presentation/cli/formatters/friction-dashboard.d.ts +46 -0
  169. package/dist/presentation/cli/formatters/index.d.ts +17 -0
  170. package/dist/presentation/cli/formatters/list-formatter.d.ts +48 -0
  171. package/dist/presentation/cli/formatters/output-formatter.d.ts +98 -0
  172. package/dist/presentation/cli/formatters/related-formatter.d.ts +57 -0
  173. package/dist/presentation/cli/formatters/show-formatter.d.ts +63 -0
  174. package/dist/presentation/cli/formatters/stats-formatter.d.ts +54 -0
  175. package/dist/presentation/cli/formatters/text-width.d.ts +37 -0
  176. package/dist/presentation/cli/formatters/timestamp-formatter.d.ts +38 -0
  177. package/dist/presentation/cli/index.d.ts +10 -0
  178. package/dist/presentation/cli/index.js +1664 -0
  179. package/dist/presentation/cli/parsers/date-parser.d.ts +28 -0
  180. package/dist/presentation/cli/parsers/index.d.ts +6 -0
  181. package/dist/presentation/cli/pickers/index.d.ts +6 -0
  182. package/dist/presentation/cli/pickers/session-picker.d.ts +59 -0
  183. package/dist/presentation/cli/progress-reporter.d.ts +199 -0
  184. package/package.json +94 -0
@@ -0,0 +1,52 @@
1
+ /**
2
+ * SQLite Extraction State Repository
3
+ *
4
+ * Implements IExtractionStateRepository using bun:sqlite prepared statements.
5
+ * Uses INSERT OR REPLACE for upsert semantics on state updates.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import type { IExtractionStateRepository } from "../../../domain/ports/repositories.js";
9
+ import { ExtractionState } from "../../../domain/entities/extraction-state.js";
10
+ /**
11
+ * SQLite implementation of IExtractionStateRepository
12
+ *
13
+ * Uses prepared statements for all operations. State identity is based
14
+ * on the unique ID. INSERT OR REPLACE provides upsert semantics.
15
+ *
16
+ * Transaction-based state updates: save() should be called within the same
17
+ * transaction as message inserts. State is ONLY marked 'complete' after
18
+ * data commit succeeds.
19
+ */
20
+ export declare class SqliteExtractionStateRepository implements IExtractionStateRepository {
21
+ private readonly findByIdStmt;
22
+ private readonly findBySessionPathStmt;
23
+ private readonly findPendingStmt;
24
+ private readonly saveStmt;
25
+ constructor(db: Database);
26
+ /**
27
+ * Map a database row to an ExtractionState entity
28
+ */
29
+ private rowToExtractionState;
30
+ /**
31
+ * Find an extraction state by its unique identifier.
32
+ */
33
+ findById(id: string): Promise<ExtractionState | null>;
34
+ /**
35
+ * Find extraction state by session path.
36
+ * Used to check if a session has already been extracted.
37
+ */
38
+ findBySessionPath(sessionPath: string): Promise<ExtractionState | null>;
39
+ /**
40
+ * Find all extractions that are pending or in progress.
41
+ * Used to resume interrupted extractions.
42
+ */
43
+ findPending(): Promise<ExtractionState[]>;
44
+ /**
45
+ * Save an extraction state to the repository.
46
+ * Uses INSERT OR REPLACE for upsert semantics.
47
+ *
48
+ * File metadata (fileMtime, fileSize) is stored for incremental sync detection.
49
+ * fileMtime is stored as ISO 8601 string, fileSize as integer bytes.
50
+ */
51
+ save(state: ExtractionState): Promise<void>;
52
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * SQLite implementation of IFactRepository.
3
+ *
4
+ * Persists and queries derived Fact projections stored in SQLite.
5
+ * Follows hexagonal rules and maps row results to Fact domain entities.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import { Fact } from "../../../domain/entities/fact.js";
9
+ import type { IFactRepository } from "../../../domain/ports/repositories.js";
10
+ export declare class SqliteFactRepository implements IFactRepository {
11
+ private readonly db;
12
+ constructor(db: Database);
13
+ findById(id: number): Promise<Fact | null>;
14
+ findByUuid(uuid: string): Promise<Fact | null>;
15
+ findByProject(project: string): Promise<Fact[]>;
16
+ findRecent(limit: number): Promise<Fact[]>;
17
+ save(fact: Fact): Promise<Fact>;
18
+ saveMany(facts: Fact[]): Promise<Fact[]>;
19
+ search(query: string, limit?: number): Promise<Fact[]>;
20
+ superseded(uuid: string, supersededAt: Date, supersededByUuid: string): Promise<void>;
21
+ supersede(uuid: string, supersededAt: Date, supersededByUuid: string): Promise<void>;
22
+ findAll(): Promise<Fact[]>;
23
+ clearAll(): Promise<void>;
24
+ private toEntity;
25
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * SQLite Friction Repository
3
+ *
4
+ * Implements IFrictionRepository using bun:sqlite prepared statements.
5
+ * Provides full CRUD, stats aggregation via SQL, and weekly trend analysis.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import type { IFrictionRepository, FrictionStats, FrictionPattern } from "../../../domain/ports/repositories.js";
9
+ import { FrictionEntry, type FrictionCategory, type FrictionStatus } from "../../../domain/entities/friction-entry.js";
10
+ /**
11
+ * SQLite implementation of IFrictionRepository.
12
+ *
13
+ * Persists FrictionEntry entities in the friction_log table with
14
+ * SQL-based aggregation for stats and trend analysis.
15
+ */
16
+ export declare class SqliteFrictionRepository implements IFrictionRepository {
17
+ private readonly db;
18
+ constructor(db: Database);
19
+ save(entry: FrictionEntry): Promise<FrictionEntry>;
20
+ findById(id: number): Promise<FrictionEntry | null>;
21
+ findOpen(): Promise<FrictionEntry[]>;
22
+ findAll(options?: {
23
+ status?: FrictionStatus;
24
+ category?: FrictionCategory;
25
+ tool?: string;
26
+ sourceProject?: string;
27
+ limit?: number;
28
+ }): Promise<FrictionEntry[]>;
29
+ resolve(id: number, resolution: string): Promise<void>;
30
+ updateStatus(id: number, status: FrictionStatus): Promise<void>;
31
+ getStats(): Promise<FrictionStats>;
32
+ getWeeklyTrends(weeks: number): Promise<Array<{
33
+ week: string;
34
+ newCount: number;
35
+ resolvedCount: number;
36
+ }>>;
37
+ markReviewed(tool: string, reviewedAt: Date): Promise<void>;
38
+ findPatterns(threshold: number): Promise<FrictionPattern[]>;
39
+ deleteByPattern(pattern: string): Promise<number>;
40
+ private toEntity;
41
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Repository Infrastructure
3
+ *
4
+ * Exports SQLite implementations of domain repository interfaces.
5
+ */
6
+ export { SqliteSessionRepository } from "./session-repository.js";
7
+ export { SqliteMessageRepository } from "./message-repository.js";
8
+ export { SqliteExtractionStateRepository } from "./extraction-state-repository.js";
9
+ export { SqliteToolUseRepository, type BatchResult, type BatchOptions, } from "./tool-use-repository.js";
10
+ export { SqliteLinkRepository, type RelatedLink, } from "./link-repository.js";
11
+ export { SqliteEntityRepository } from "./entity-repository.js";
12
+ export { EmbeddingRepository, type UnembeddedMessage, type EmbeddingBatchItem, } from "./embedding-repository.js";
13
+ export { SqliteMemoryFileRepository } from "./memory-file-repository.js";
14
+ export { SqliteFrictionRepository } from "./friction-repository.js";
15
+ export { SqliteBackfillStateRepository } from "./backfill-state-repository.js";
16
+ export { SqliteFactRepository } from "./fact-repository.js";
17
+ export { SqliteExtractionLogRepository } from "./extraction-log-repository.js";
@@ -0,0 +1,64 @@
1
+ /**
2
+ * SQLite Link Repository
3
+ *
4
+ * Implements ILinkRepository using bun:sqlite prepared statements.
5
+ * Supports graph-like traversal using WITH RECURSIVE CTE.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import type { ILinkRepository } from "../../../domain/ports/repositories.js";
9
+ import { Link, type EntityType } from "../../../domain/entities/link.js";
10
+ /**
11
+ * Link with hop count for graph traversal results
12
+ */
13
+ export interface RelatedLink {
14
+ link: Link;
15
+ hop: number;
16
+ }
17
+ /**
18
+ * SQLite implementation of ILinkRepository
19
+ *
20
+ * Uses prepared statements for all operations. Supports multi-hop graph
21
+ * traversal using WITH RECURSIVE CTE with cycle prevention.
22
+ */
23
+ export declare class SqliteLinkRepository implements ILinkRepository {
24
+ private readonly db;
25
+ private readonly findBySourceStmt;
26
+ private readonly findByTargetStmt;
27
+ private readonly insertStmt;
28
+ constructor(db: Database);
29
+ /**
30
+ * Map a database row to a Link entity
31
+ */
32
+ private rowToLink;
33
+ /**
34
+ * Find all links originating from a specific entity.
35
+ */
36
+ findBySource(sourceType: EntityType, sourceId: string): Promise<Link[]>;
37
+ /**
38
+ * Find all links pointing to a specific entity.
39
+ */
40
+ findByTarget(targetType: EntityType, targetId: string): Promise<Link[]>;
41
+ /**
42
+ * Find all entities related to a given entity within N hops.
43
+ * Uses WITH RECURSIVE CTE for graph traversal with cycle prevention.
44
+ *
45
+ * Note: The interface returns Link[], but we also track hop count.
46
+ * Use findRelatedWithHops() if hop information is needed.
47
+ */
48
+ findRelated(entityType: EntityType, entityId: string, maxHops?: number): Promise<Link[]>;
49
+ /**
50
+ * Find related entities with hop information.
51
+ * Enables weight decay analysis and distance-based filtering.
52
+ */
53
+ findRelatedWithHops(entityType: EntityType, entityId: string, maxHops?: number): Promise<RelatedLink[]>;
54
+ /**
55
+ * Save a link to the repository.
56
+ * Uses INSERT OR REPLACE for upsert behavior on unique constraint.
57
+ */
58
+ save(link: Link): Promise<void>;
59
+ /**
60
+ * Save multiple links in a single transaction.
61
+ * Uses BEGIN IMMEDIATE for write locking.
62
+ */
63
+ saveMany(links: Link[]): Promise<void>;
64
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * SQLite Memory File Repository
3
+ *
4
+ * Implements IMemoryFileRepository using bun:sqlite prepared statements.
5
+ * Uses INSERT ... ON CONFLICT for idempotent upsert by file_path.
6
+ * FTS5 search delegates to sanitizeFtsQuery for safe query handling.
7
+ */
8
+ import type { Database } from "bun:sqlite";
9
+ import type { IMemoryFileRepository } from "../../../domain/ports/repositories.js";
10
+ import { MemoryFile, type MemoryFileType } from "../../../domain/entities/memory-file.js";
11
+ /**
12
+ * SQLite implementation of IMemoryFileRepository.
13
+ *
14
+ * Persists MemoryFile entities in the memory_files table with
15
+ * FTS5 search support via the memory_files_fts virtual table.
16
+ */
17
+ export declare class SqliteMemoryFileRepository implements IMemoryFileRepository {
18
+ private readonly db;
19
+ constructor(db: Database);
20
+ findByPath(filePath: string): Promise<MemoryFile | null>;
21
+ findByType(fileType: MemoryFileType): Promise<MemoryFile[]>;
22
+ findByProject(projectEncoded: string): Promise<MemoryFile[]>;
23
+ save(file: MemoryFile): Promise<void>;
24
+ saveMany(files: MemoryFile[]): Promise<void>;
25
+ searchContent(query: string, limit?: number): Promise<MemoryFile[]>;
26
+ findCrossProjectLearnings(excludeProject?: string, limit?: number): Promise<MemoryFile[]>;
27
+ private toEntity;
28
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * SQLite Message Repository Implementation
3
+ *
4
+ * Implements IMessageRepository for SQLite with FTS5 integration.
5
+ * Messages are automatically indexed via triggers when inserted.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import { Message } from "../../../domain/entities/message.js";
9
+ import type { IMessageRepository } from "../../../domain/ports/repositories.js";
10
+ /**
11
+ * Result of a batch save operation
12
+ */
13
+ export interface BatchResult {
14
+ /** Number of messages successfully inserted */
15
+ inserted: number;
16
+ /** Number of messages skipped (duplicates or errors) */
17
+ skipped: number;
18
+ /** Details of any errors encountered */
19
+ errors: Array<{
20
+ id: string;
21
+ reason: string;
22
+ }>;
23
+ }
24
+ /**
25
+ * Options for batch save operations
26
+ */
27
+ export interface BatchOptions {
28
+ /** Callback for progress updates */
29
+ onProgress?: (progress: {
30
+ inserted: number;
31
+ total: number;
32
+ }) => void;
33
+ }
34
+ /**
35
+ * SQLite implementation of IMessageRepository
36
+ *
37
+ * Features:
38
+ * - Prepared statements for optimal performance
39
+ * - INSERT OR IGNORE for idempotent inserts
40
+ * - Batch processing with transactions
41
+ * - FTS5 indexing via automatic triggers
42
+ */
43
+ export declare class SqliteMessageRepository implements IMessageRepository {
44
+ private readonly db;
45
+ private readonly findByIdStmt;
46
+ private readonly findBySessionStmt;
47
+ private readonly existsStmt;
48
+ private readonly insertStmt;
49
+ /**
50
+ * Create a new SqliteMessageRepository
51
+ *
52
+ * @param db - Initialized SQLite database with schema applied
53
+ */
54
+ constructor(db: Database);
55
+ /**
56
+ * Find a message by its unique identifier
57
+ */
58
+ findById(id: string): Promise<Message | null>;
59
+ /**
60
+ * Find all messages belonging to a session, ordered by timestamp
61
+ */
62
+ findBySession(sessionId: string): Promise<Message[]>;
63
+ /**
64
+ * Save a message associated with a session
65
+ *
66
+ * Uses INSERT OR IGNORE for idempotent behavior - duplicates are silently skipped.
67
+ */
68
+ save(message: Message, sessionId: string): Promise<void>;
69
+ /**
70
+ * Save multiple messages in a single batch operation
71
+ *
72
+ * Processes messages in batches of 100 within immediate transactions.
73
+ * Reports progress via optional callback.
74
+ *
75
+ * @param messages - Array of message/sessionId pairs to save
76
+ * @param options - Optional configuration including progress callback
77
+ * @returns Result with counts of inserted, skipped, and any errors
78
+ */
79
+ saveMany(messages: Array<{
80
+ message: Message;
81
+ sessionId: string;
82
+ }>, options?: BatchOptions): Promise<BatchResult>;
83
+ /**
84
+ * Convert a database row to a Message entity
85
+ */
86
+ private rowToMessage;
87
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * SQLite Session Repository
3
+ *
4
+ * Implements ISessionRepository using bun:sqlite prepared statements.
5
+ * Uses INSERT OR IGNORE for idempotent session inserts.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import type { ISessionRepository, SessionListOptions } from "../../../domain/ports/repositories.js";
9
+ import { Session } from "../../../domain/entities/session.js";
10
+ import { ProjectPath } from "../../../domain/value-objects/project-path.js";
11
+ /**
12
+ * SQLite implementation of ISessionRepository
13
+ *
14
+ * Uses prepared statements for all operations. Session identity is based
15
+ * on the unique ID. INSERT OR IGNORE ensures idempotent saves.
16
+ */
17
+ export declare class SqliteSessionRepository implements ISessionRepository {
18
+ private readonly db;
19
+ private readonly findByIdStmt;
20
+ private readonly findByProjectStmt;
21
+ private readonly findRecentStmt;
22
+ private readonly insertStmt;
23
+ private readonly deleteStmt;
24
+ private readonly updateSummaryStmt;
25
+ private readonly updateProjectNameStmt;
26
+ private readonly findDistinctEncodedPathsStmt;
27
+ constructor(db: Database);
28
+ /**
29
+ * Map a database row to a Session entity
30
+ *
31
+ * Uses project_path_decoded (lossless) not project_path_encoded (lossy
32
+ * for paths containing hyphens). The encoded form is only used for
33
+ * efficient lookups.
34
+ *
35
+ * Populates messageCount from the stored count in the database row,
36
+ * enabling accurate display without loading all messages.
37
+ */
38
+ private rowToSession;
39
+ /**
40
+ * Find a session by its unique identifier.
41
+ */
42
+ findById(id: string): Promise<Session | null>;
43
+ /**
44
+ * Find all sessions belonging to a project.
45
+ */
46
+ findByProject(projectPath: ProjectPath): Promise<Session[]>;
47
+ /**
48
+ * Find the most recent sessions across all projects.
49
+ */
50
+ findRecent(limit: number): Promise<Session[]>;
51
+ /**
52
+ * Save a session to the repository.
53
+ * Uses INSERT OR IGNORE for idempotency (no error on duplicate).
54
+ */
55
+ save(session: Session): Promise<void>;
56
+ /**
57
+ * Save multiple sessions in a single transaction.
58
+ * Uses BEGIN IMMEDIATE for write locking.
59
+ */
60
+ saveMany(sessions: Session[]): Promise<void>;
61
+ /**
62
+ * Delete a session by its identifier.
63
+ * Associated messages are deleted via foreign key CASCADE.
64
+ */
65
+ delete(id: string): Promise<void>;
66
+ /**
67
+ * Update the summary for a session.
68
+ *
69
+ * This triggers the sessions_fts_update trigger which indexes the summary
70
+ * in the FTS5 virtual table for full-text search.
71
+ *
72
+ * @param sessionId - Session identifier
73
+ * @param summary - LLM-generated summary text
74
+ */
75
+ updateSummary(sessionId: string, summary: string): Promise<void>;
76
+ /**
77
+ * Find sessions older than a specified date.
78
+ * Returns sessions where updated_at < cutoffDate.
79
+ *
80
+ * @param cutoffDate - Sessions updated before this date will be returned
81
+ * @returns Array of sessions older than the cutoff
82
+ */
83
+ findOlderThan(cutoffDate: Date): Promise<Session[]>;
84
+ /**
85
+ * Count sessions older than a specified date.
86
+ * Efficient for showing preview before purge.
87
+ *
88
+ * @param cutoffDate - Sessions updated before this date will be counted
89
+ * @returns Number of sessions older than the cutoff
90
+ */
91
+ countOlderThan(cutoffDate: Date): Promise<number>;
92
+ /**
93
+ * Delete sessions older than a specified date.
94
+ * Cascade deletes to messages, tool_uses, links, entities via foreign keys.
95
+ *
96
+ * @param cutoffDate - Sessions updated before this date will be deleted
97
+ * @returns Number of sessions deleted
98
+ */
99
+ deleteOlderThan(cutoffDate: Date): Promise<number>;
100
+ /**
101
+ * Update the project name for all sessions with a matching encoded path.
102
+ */
103
+ updateProjectName(encodedPath: string, projectName: string): Promise<number>;
104
+ /**
105
+ * Find all distinct encoded project paths stored in sessions.
106
+ */
107
+ findDistinctEncodedPaths(): Promise<string[]>;
108
+ /**
109
+ * Find sessions with filtering options.
110
+ * Builds dynamic WHERE clause based on provided filters.
111
+ */
112
+ findFiltered(options: SessionListOptions): Promise<Session[]>;
113
+ /**
114
+ * Search session summaries via FTS5.
115
+ *
116
+ * Queries the sessions_fts virtual table for summaries matching the
117
+ * given search term. Applies sanitizeFtsQuery() to prevent FTS5
118
+ * syntax errors from special characters in user input.
119
+ *
120
+ * @param query - Search query (sanitized internally for FTS5 safety)
121
+ * @param limit - Maximum results (default: 20)
122
+ * @returns Array of sessions whose summaries match the query
123
+ */
124
+ searchSummaries(query: string, limit?: number): Promise<Session[]>;
125
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * SQLite Tool Use Repository Implementation
3
+ *
4
+ * Persists ToolUse entities to SQLite database.
5
+ * Implements IToolUseRepository with batch support and idempotent inserts.
6
+ */
7
+ import type { Database } from "bun:sqlite";
8
+ import type { IToolUseRepository } from "../../../domain/ports/repositories.js";
9
+ import { ToolUse } from "../../../domain/entities/tool-use.js";
10
+ /**
11
+ * Result of a batch save operation
12
+ */
13
+ export interface BatchResult {
14
+ inserted: number;
15
+ skipped: number;
16
+ errors: Array<{
17
+ id: string;
18
+ reason: string;
19
+ }>;
20
+ }
21
+ /**
22
+ * Options for batch save operations
23
+ */
24
+ export interface BatchOptions {
25
+ onProgress?: (progress: {
26
+ inserted: number;
27
+ total: number;
28
+ }) => void;
29
+ }
30
+ /**
31
+ * SQLite implementation of IToolUseRepository
32
+ *
33
+ * Features:
34
+ * - INSERT OR IGNORE for idempotent inserts
35
+ * - JSON serialization for input objects
36
+ * - Batch insert with transaction support
37
+ * - Progress callback for CLI integration
38
+ */
39
+ export declare class SqliteToolUseRepository implements IToolUseRepository {
40
+ private readonly db;
41
+ private readonly findByIdStmt;
42
+ private readonly findBySessionStmt;
43
+ private readonly insertStmt;
44
+ constructor(db: Database);
45
+ /**
46
+ * Find a tool use by its unique identifier.
47
+ */
48
+ findById(id: string): Promise<ToolUse | null>;
49
+ /**
50
+ * Find all tool uses belonging to a session.
51
+ * Returns array ordered by timestamp ascending.
52
+ */
53
+ findBySession(sessionId: string): Promise<ToolUse[]>;
54
+ /**
55
+ * Save a tool use associated with a session.
56
+ * Uses INSERT OR IGNORE for idempotent inserts.
57
+ */
58
+ save(toolUse: ToolUse, sessionId: string): Promise<void>;
59
+ /**
60
+ * Save multiple tool uses in a single transaction.
61
+ * Processes in batches of 100 for memory efficiency.
62
+ * Returns batch result with counts and any errors encountered.
63
+ */
64
+ saveMany(toolUses: Array<{
65
+ toolUse: ToolUse;
66
+ sessionId: string;
67
+ }>, options?: BatchOptions): Promise<BatchResult>;
68
+ /**
69
+ * Convert a database row to a ToolUse entity.
70
+ */
71
+ private rowToEntity;
72
+ }