@medicine-wheel/storage-provider 0.27.5 → 0.27.7

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.
@@ -4,6 +4,7 @@
4
4
  * Abstract interface for Medicine Wheel data persistence.
5
5
  * Implementations: JsonlProvider (local/default), NeonProvider (Postgres), RedisProvider (future)
6
6
  */
7
+ import type { MemoryConversation, MemoryConversationFilters, MemoryConversationPatch, MemoryConversationTurn } from '@medicine-wheel/memory';
7
8
  import type { DirectionName, NodeType, CeremonyType, RelationalNode as OntologyRelationalNode, RelationalEdge as OntologyRelationalEdge, CeremonyLog as OntologyCeremonyLog } from '@medicine-wheel/ontology-core';
8
9
  export interface RelationalNode extends OntologyRelationalNode {
9
10
  description?: string;
@@ -339,5 +340,12 @@ export interface StorageProvider {
339
340
  registerCapture(record: CaptureRecord): Promise<CaptureRecord>;
340
341
  getCapture(id: string): Promise<CaptureRecord | null>;
341
342
  listCaptures(filters?: CaptureFilters): Promise<CaptureRecord[]>;
343
+ getMemoryConversation(id: string): Promise<MemoryConversation | null>;
344
+ createMemoryConversation(record: MemoryConversation): Promise<MemoryConversation>;
345
+ /** Reads the stored conversation again before appending, so two replies at once both land. */
346
+ appendMemoryConversationTurn(id: string, turn: MemoryConversationTurn): Promise<MemoryConversation>;
347
+ updateMemoryConversation(id: string, patch: MemoryConversationPatch): Promise<MemoryConversation>;
348
+ listMemoryConversations(filters?: MemoryConversationFilters): Promise<MemoryConversation[]>;
349
+ deleteMemoryConversation(id: string): Promise<void>;
342
350
  }
343
351
  export type ProviderType = 'jsonl' | 'neon' | 'redis' | 'auto';
package/dist/jsonl.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { DirectionName, NodeType, CeremonyType } from '@medicine-wheel/ontology-core';
2
2
  import type { StorageProvider, RelationalNode, RelationalEdge, CeremonyLog, WeaveRecord, InquiryWeaveFilters, PlanPerspectiveRecord, PlanPerspectiveFilters, DiaryEntryRecord, DiaryEntryFilters, CeremonyEventRecord, CeremonyEventFilters, CaptureRecord, CaptureFilters, NodePatch, EdgePatch } from './interface.js';
3
+ import { type MemoryConversation, type MemoryConversationFilters, type MemoryConversationPatch, type MemoryConversationTurn } from '@medicine-wheel/memory';
3
4
  export declare class JsonlProvider implements StorageProvider {
4
5
  readonly name = "jsonl";
5
6
  readonly dataDir: string;
@@ -11,6 +12,7 @@ export declare class JsonlProvider implements StorageProvider {
11
12
  private readonly diaryEntriesFile;
12
13
  private readonly ceremonyEventsFile;
13
14
  private readonly capturesFile;
15
+ private readonly memoryConversationsFile;
14
16
  constructor(dataDir?: string);
15
17
  connect(): Promise<void>;
16
18
  disconnect(): Promise<void>;
@@ -60,6 +62,12 @@ export declare class JsonlProvider implements StorageProvider {
60
62
  private readPlanPerspectives;
61
63
  private readDiaryEntries;
62
64
  private readCeremonyEvents;
65
+ getMemoryConversation(id: string): Promise<MemoryConversation | null>;
66
+ createMemoryConversation(record: MemoryConversation): Promise<MemoryConversation>;
67
+ appendMemoryConversationTurn(id: string, turn: MemoryConversationTurn): Promise<MemoryConversation>;
68
+ updateMemoryConversation(id: string, patch: MemoryConversationPatch): Promise<MemoryConversation>;
69
+ listMemoryConversations(filters?: MemoryConversationFilters): Promise<MemoryConversation[]>;
70
+ deleteMemoryConversation(id: string): Promise<void>;
63
71
  private readCaptures;
64
72
  private upsertById;
65
73
  private upsertEdge;
package/dist/jsonl.js CHANGED
@@ -3,6 +3,7 @@ import * as path from 'path';
3
3
  import { randomUUID } from 'crypto';
4
4
  import { NodeNotFoundError, EdgeNotFoundError, NodeHasRelationsError, } from './interface.js';
5
5
  import { mergePlanPerspectiveRecords, matchesPlanPerspectiveFilters, } from './plan-perspectives.js';
6
+ import { ConversationError, conversationRecords, filterConversations, } from '@medicine-wheel/memory';
6
7
  import { filterInquiryWeaves } from './inquiry-weaves.js';
7
8
  import { filterAndOrderDiaryEntries } from './diary-records.js';
8
9
  import { matchesCeremonyEventFilters } from './ceremony-events.js';
@@ -18,6 +19,7 @@ export class JsonlProvider {
18
19
  diaryEntriesFile;
19
20
  ceremonyEventsFile;
20
21
  capturesFile;
22
+ memoryConversationsFile;
21
23
  constructor(dataDir) {
22
24
  this.dataDir = path.resolve(dataDir ?? resolveProjectDataDir());
23
25
  this.nodesFile = path.join(this.dataDir, 'nodes.jsonl');
@@ -28,6 +30,7 @@ export class JsonlProvider {
28
30
  this.diaryEntriesFile = path.join(this.dataDir, 'diary-entries.jsonl');
29
31
  this.ceremonyEventsFile = path.join(this.dataDir, 'ceremony-events.jsonl');
30
32
  this.capturesFile = path.join(this.dataDir, 'captures.jsonl');
33
+ this.memoryConversationsFile = path.join(this.dataDir, 'memory-conversations.jsonl');
31
34
  }
32
35
  async connect() {
33
36
  ensureDirectory(this.dataDir);
@@ -308,6 +311,42 @@ export class JsonlProvider {
308
311
  readCeremonyEvents() {
309
312
  return readJsonl(this.ceremonyEventsFile);
310
313
  }
314
+ // ── Memory conversations (jgwill/medicine-wheel#174) ──
315
+ async getMemoryConversation(id) {
316
+ return readJsonl(this.memoryConversationsFile).find((c) => c.id === id) ?? null;
317
+ }
318
+ async createMemoryConversation(record) {
319
+ await withWriteLock(this.memoryConversationsFile, () => {
320
+ const all = readJsonl(this.memoryConversationsFile);
321
+ if (all.some((c) => c.id === record.id))
322
+ throw new ConversationError(409, `Conversation ${record.id} exists`);
323
+ writeJsonl(this.memoryConversationsFile, [...all, record]);
324
+ });
325
+ return record;
326
+ }
327
+ async appendMemoryConversationTurn(id, turn) {
328
+ return withWriteLock(this.memoryConversationsFile, () => {
329
+ const { list, kept } = conversationRecords.append(readJsonl(this.memoryConversationsFile), id, turn);
330
+ writeJsonl(this.memoryConversationsFile, list);
331
+ return kept;
332
+ });
333
+ }
334
+ async updateMemoryConversation(id, patch) {
335
+ return withWriteLock(this.memoryConversationsFile, () => {
336
+ const { list, kept } = conversationRecords.patch(readJsonl(this.memoryConversationsFile), id, patch, new Date().toISOString());
337
+ writeJsonl(this.memoryConversationsFile, list);
338
+ return kept;
339
+ });
340
+ }
341
+ async listMemoryConversations(filters = {}) {
342
+ return filterConversations(readJsonl(this.memoryConversationsFile), filters);
343
+ }
344
+ async deleteMemoryConversation(id) {
345
+ await withWriteLock(this.memoryConversationsFile, () => {
346
+ const all = readJsonl(this.memoryConversationsFile);
347
+ writeJsonl(this.memoryConversationsFile, all.filter((c) => c.id !== id));
348
+ });
349
+ }
311
350
  readCaptures() {
312
351
  return readJsonl(this.capturesFile);
313
352
  }
package/dist/neon.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import type { DirectionName, NodeType, CeremonyType } from '@medicine-wheel/ontology-core';
5
5
  import type { StorageProvider, RelationalNode, RelationalEdge, CeremonyLog, WeaveRecord, InquiryWeaveFilters, PlanPerspectiveRecord, PlanPerspectiveFilters, DiaryEntryRecord, DiaryEntryFilters, CeremonyEventRecord, CeremonyEventFilters, CaptureRecord, CaptureFilters, NodePatch, EdgePatch } from './interface.js';
6
+ import { type MemoryConversation, type MemoryConversationFilters, type MemoryConversationPatch, type MemoryConversationTurn } from '@medicine-wheel/memory';
6
7
  export declare class NeonProvider implements StorageProvider {
7
8
  readonly name = "neon";
8
9
  private sql;
@@ -56,5 +57,13 @@ export declare class NeonProvider implements StorageProvider {
56
57
  registerCapture(record: CaptureRecord): Promise<CaptureRecord>;
57
58
  getCapture(id: string): Promise<CaptureRecord | null>;
58
59
  listCaptures(filters?: CaptureFilters): Promise<CaptureRecord[]>;
60
+ getMemoryConversation(id: string): Promise<MemoryConversation | null>;
61
+ createMemoryConversation(record: MemoryConversation): Promise<MemoryConversation>;
62
+ appendMemoryConversationTurn(id: string, turn: MemoryConversationTurn): Promise<MemoryConversation>;
63
+ updateMemoryConversation(id: string, patch: MemoryConversationPatch): Promise<MemoryConversation>;
64
+ listMemoryConversations(filters?: MemoryConversationFilters): Promise<MemoryConversation[]>;
65
+ deleteMemoryConversation(id: string): Promise<void>;
66
+ private changeMemoryConversation;
67
+ private ensureMemoryConversationsTable;
59
68
  private ensureCapturesTable;
60
69
  }
package/dist/neon.js CHANGED
@@ -6,6 +6,7 @@ import { mergePlanPerspectiveRecords, matchesPlanPerspectiveFilters, } from './p
6
6
  import { filterInquiryWeaves } from './inquiry-weaves.js';
7
7
  import { filterAndOrderDiaryEntries } from './diary-records.js';
8
8
  import { matchesCeremonyEventFilters } from './ceremony-events.js';
9
+ import { ConversationError, appendTurn, filterConversations, patchConversation, } from '@medicine-wheel/memory';
9
10
  import { mergeCaptureRecords, filterAndOrderCaptures, } from './capture-records.js';
10
11
  export class NeonProvider {
11
12
  name = 'neon';
@@ -21,6 +22,7 @@ export class NeonProvider {
21
22
  await this.ensureDiaryEntriesTable();
22
23
  await this.ensureCeremonyEventsTable();
23
24
  await this.ensureCapturesTable();
25
+ await this.ensureMemoryConversationsTable();
24
26
  }
25
27
  async disconnect() {
26
28
  this.sql = null;
@@ -568,6 +570,77 @@ export class NeonProvider {
568
570
  const rows = await this.db `SELECT payload FROM captures ORDER BY updated_at DESC`;
569
571
  return filterAndOrderCaptures(rows.map((row) => parseJsonValue(row.payload, null)), filters);
570
572
  }
573
+ // ── Memory conversations (jgwill/medicine-wheel#174) ──
574
+ // A change is written only if the row still holds the version it was read at (its
575
+ // updated_at), so two replies at once both land: the second reads again and retries.
576
+ async getMemoryConversation(id) {
577
+ const rows = await this.db `SELECT payload FROM memory_conversations WHERE id = ${id}`;
578
+ if (rows.length === 0)
579
+ return null;
580
+ return parseJsonValue(rows[0].payload, null);
581
+ }
582
+ async createMemoryConversation(record) {
583
+ const rows = await this.db `
584
+ INSERT INTO memory_conversations (id, payload, held_by, opened_from, shared_in, created_at, updated_at)
585
+ VALUES (${record.id}, ${JSON.stringify(record)}, ${record.held_by}, ${record.opened_from}, ${record.shared_in}, ${record.created_at}, ${record.updated_at})
586
+ ON CONFLICT (id) DO NOTHING
587
+ RETURNING id
588
+ `;
589
+ if (rows.length === 0)
590
+ throw new ConversationError(409, `Conversation ${record.id} exists`);
591
+ return record;
592
+ }
593
+ async appendMemoryConversationTurn(id, turn) {
594
+ return this.changeMemoryConversation(id, (c) => appendTurn(c, turn));
595
+ }
596
+ async updateMemoryConversation(id, patch) {
597
+ return this.changeMemoryConversation(id, (c) => patchConversation(c, patch, new Date().toISOString()));
598
+ }
599
+ async listMemoryConversations(filters = {}) {
600
+ const rows = filters.held_by !== undefined
601
+ ? await this.db `SELECT payload FROM memory_conversations WHERE held_by = ${filters.held_by} ORDER BY updated_at DESC`
602
+ : filters.shared_in !== undefined
603
+ ? await this.db `SELECT payload FROM memory_conversations WHERE shared_in = ${filters.shared_in} ORDER BY updated_at DESC`
604
+ : await this.db `SELECT payload FROM memory_conversations ORDER BY updated_at DESC`;
605
+ return filterConversations(rows.map((row) => parseJsonValue(row.payload, null)), filters);
606
+ }
607
+ async deleteMemoryConversation(id) {
608
+ await this.db `DELETE FROM memory_conversations WHERE id = ${id}`;
609
+ }
610
+ async changeMemoryConversation(id, change) {
611
+ for (let attempt = 0; attempt < 5; attempt++) {
612
+ const current = await this.getMemoryConversation(id);
613
+ if (!current)
614
+ throw new ConversationError(404, `No conversation ${id}`);
615
+ const next = change(current);
616
+ if (next === current)
617
+ return current;
618
+ const rows = await this.db `
619
+ UPDATE memory_conversations
620
+ SET payload = ${JSON.stringify(next)}, shared_in = ${next.shared_in}, updated_at = ${next.updated_at}
621
+ WHERE id = ${id} AND payload->>'updated_at' = ${current.updated_at}
622
+ RETURNING id
623
+ `;
624
+ if (rows.length > 0)
625
+ return next;
626
+ }
627
+ throw new ConversationError(409, 'The conversation kept changing while this was written. Try again.');
628
+ }
629
+ async ensureMemoryConversationsTable() {
630
+ await this.db `
631
+ CREATE TABLE IF NOT EXISTS memory_conversations (
632
+ id TEXT PRIMARY KEY,
633
+ payload JSONB NOT NULL,
634
+ held_by TEXT NOT NULL,
635
+ opened_from TEXT,
636
+ shared_in TEXT,
637
+ created_at TIMESTAMPTZ,
638
+ updated_at TIMESTAMPTZ
639
+ )
640
+ `;
641
+ await this.db `CREATE INDEX IF NOT EXISTS idx_memory_conversations_held_by ON memory_conversations(held_by)`;
642
+ await this.db `CREATE INDEX IF NOT EXISTS idx_memory_conversations_shared_in ON memory_conversations(shared_in)`;
643
+ }
571
644
  async ensureCapturesTable() {
572
645
  await this.db `
573
646
  CREATE TABLE IF NOT EXISTS captures (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medicine-wheel/storage-provider",
3
- "version": "0.27.5",
3
+ "version": "0.27.7",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,7 +16,8 @@
16
16
  "dependencies": {
17
17
  "@neondatabase/serverless": "^0.10.0",
18
18
  "@upstash/redis": "^1.34.3",
19
- "@medicine-wheel/ontology-core": "^0.27.5"
19
+ "@medicine-wheel/ontology-core": "^0.27.7",
20
+ "@medicine-wheel/memory": "^0.27.7"
20
21
  },
21
22
  "devDependencies": {
22
23
  "typescript": "^5.7.0"