@medicine-wheel/app 0.27.5 → 0.27.6

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.
@@ -1,11 +1,13 @@
1
1
  import { NextResponse } from 'next/server';
2
- import { readLimit, readScope, wheelMemory } from '@/lib/memory';
2
+ import { readEarlier, readLimit, readScope, wheelMemory } from '@/lib/memory';
3
3
 
4
4
  export const dynamic = 'force-dynamic';
5
5
 
6
6
  /**
7
7
  * Ask the wheel's memory a question within a scope.
8
- * Body: { question, prompt?, scope, peer?, reasoning_level?, limit? }.
8
+ * Body: { question, prompt?, scope, peer?, reasoning_level?, limit?, earlier? }.
9
+ * `earlier` is the turns of a conversation the host keeps itself; a kept
10
+ * conversation is asked through /api/memory/conversations (jgwill/medicine-wheel#174).
9
11
  * `prompt` is what the memory provider is asked when the host wraps the
10
12
  * question in its own template; the records match keeps `question`
11
13
  * (jgwill/medicine-wheel#171).
@@ -22,6 +24,8 @@ export async function POST(request: Request) {
22
24
  if (prompt.length > 20000) return NextResponse.json({ error: 'prompt holds at most 20000 characters' }, { status: 400 });
23
25
  const read = readScope(body?.scope);
24
26
  if ('error' in read) return NextResponse.json({ error: read.error }, { status: 400 });
27
+ const prior = readEarlier(body?.earlier);
28
+ if ('error' in prior) return NextResponse.json({ error: prior.error }, { status: 400 });
25
29
  try {
26
30
  const answer = await wheelMemory().ask({
27
31
  question,
@@ -30,6 +34,7 @@ export async function POST(request: Request) {
30
34
  ...(typeof body.peer === 'string' && body.peer ? { peer: body.peer } : {}),
31
35
  ...(typeof body.reasoning_level === 'string' ? { reasoning_level: body.reasoning_level } : {}),
32
36
  limit: readLimit(body.limit),
37
+ ...(prior.earlier ? { earlier: prior.earlier } : {}),
33
38
  });
34
39
  return NextResponse.json(answer, { headers: { 'cache-control': 'no-store' } });
35
40
  } catch (error) {
@@ -0,0 +1,57 @@
1
+ import { NextResponse } from 'next/server';
2
+ import { conversationRefusal, wheelConversations } from '@/lib/memory';
3
+
4
+ export const dynamic = 'force-dynamic';
5
+
6
+ type Context = { params: Promise<{ id: string }> };
7
+
8
+ /**
9
+ * One conversation with the wheel's memory (jgwill/medicine-wheel#174).
10
+ *
11
+ * GET the conversation with every turn.
12
+ * PATCH { title?, shared_in? (a ceremony id, or null to share it nowhere), metadata? }
13
+ * DELETE removes it from the store. Nothing of it was ever in the memory provider.
14
+ */
15
+ export async function GET(_request: Request, context: Context) {
16
+ const { id } = await context.params;
17
+ try {
18
+ const c = await (await wheelConversations()).get(decodeURIComponent(id));
19
+ if (!c) return NextResponse.json({ error: `No conversation ${id}` }, { status: 404 });
20
+ return NextResponse.json(c, { headers: { 'cache-control': 'no-store' } });
21
+ } catch (error) {
22
+ return NextResponse.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 });
23
+ }
24
+ }
25
+
26
+ export async function PATCH(request: Request, context: Context) {
27
+ const { id } = await context.params;
28
+ const body = await request.json().catch(() => null);
29
+ if (!body || typeof body !== 'object') return NextResponse.json({ error: 'A JSON body is required' }, { status: 400 });
30
+ if (body.title !== undefined && typeof body.title !== 'string') return NextResponse.json({ error: 'title is a string' }, { status: 400 });
31
+ if (body.shared_in !== undefined && body.shared_in !== null && typeof body.shared_in !== 'string') return NextResponse.json({ error: 'shared_in is a ceremony id or null' }, { status: 400 });
32
+ if (body.metadata !== undefined && (typeof body.metadata !== 'object' || body.metadata === null || Array.isArray(body.metadata))) return NextResponse.json({ error: 'metadata is an object' }, { status: 400 });
33
+ try {
34
+ const c = await (await wheelConversations()).update(decodeURIComponent(id), {
35
+ ...(body.title !== undefined ? { title: body.title } : {}),
36
+ ...(body.shared_in !== undefined ? { shared_in: body.shared_in } : {}),
37
+ ...(body.metadata !== undefined ? { metadata: body.metadata } : {}),
38
+ });
39
+ return NextResponse.json(c, { headers: { 'cache-control': 'no-store' } });
40
+ } catch (error) {
41
+ const refused = conversationRefusal(error);
42
+ if (refused) return NextResponse.json({ error: refused.error }, { status: refused.status });
43
+ return NextResponse.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 });
44
+ }
45
+ }
46
+
47
+ export async function DELETE(_request: Request, context: Context) {
48
+ const { id } = await context.params;
49
+ try {
50
+ await (await wheelConversations()).delete(decodeURIComponent(id));
51
+ return NextResponse.json({ deleted: decodeURIComponent(id) }, { headers: { 'cache-control': 'no-store' } });
52
+ } catch (error) {
53
+ const refused = conversationRefusal(error);
54
+ if (refused) return NextResponse.json({ error: refused.error }, { status: refused.status });
55
+ return NextResponse.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 });
56
+ }
57
+ }
@@ -0,0 +1,86 @@
1
+ import { NextResponse } from 'next/server';
2
+ import { conversationRefusal, readLimit, readScope, wheelConversations } from '@/lib/memory';
3
+
4
+ export const dynamic = 'force-dynamic';
5
+
6
+ const str = (v: unknown) => (typeof v === 'string' && v.trim() ? v.trim() : undefined);
7
+ const meta = (v: unknown) => (v && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : undefined);
8
+
9
+ /**
10
+ * Conversations with the wheel's memory (jgwill/medicine-wheel#174).
11
+ *
12
+ * GET ?held_by=&shared_in=&opened_from=&q=&limit= the conversations that match, most
13
+ * recently continued first, without their turns. `held_by` or `shared_in` is
14
+ * required: the door never lists the whole collection, since it trusts its caller.
15
+ * POST { question, scope, asked_by, asked_by_name?, prompt?, conversation?, retry_of?,
16
+ * held_by?, held_by_name?, opened_from?, shared_in?, title?, metadata?, turn_metadata?,
17
+ * peer?, reasoning_level?, limit? }
18
+ * asks the memory within `scope`. Without `conversation` a new one starts, held by
19
+ * `held_by` (default `asked_by`); with it, the question is a reply asked with the
20
+ * earlier turns. `retry_of` (the latest turn's id) asks that question again and keeps
21
+ * both answers. Answers { conversation, turn, answer }.
22
+ *
23
+ * The host decides who may reach what and who may read a conversation; the wheel refuses
24
+ * a reply from anyone but the holder to a conversation shared nowhere.
25
+ */
26
+ export async function GET(request: Request) {
27
+ const p = new URL(request.url).searchParams;
28
+ if (!p.get('held_by') && !p.get('shared_in')) {
29
+ return NextResponse.json({ error: 'Name held_by (who holds them) or shared_in (the ceremony they are shared in)' }, { status: 400 });
30
+ }
31
+ try {
32
+ const list = await (await wheelConversations()).list({
33
+ ...(p.get('held_by') ? { held_by: p.get('held_by')! } : {}),
34
+ ...(p.get('opened_from') ? { opened_from: p.get('opened_from')! } : {}),
35
+ ...(p.get('shared_in') ? { shared_in: p.get('shared_in')! } : {}),
36
+ ...(p.get('q') ? { q: p.get('q')! } : {}),
37
+ ...(readLimit(Number(p.get('limit')), 500) ? { limit: readLimit(Number(p.get('limit')), 500) } : {}),
38
+ });
39
+ return NextResponse.json({ conversations: list }, { headers: { 'cache-control': 'no-store' } });
40
+ } catch (error) {
41
+ return NextResponse.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 });
42
+ }
43
+ }
44
+
45
+ export async function POST(request: Request) {
46
+ const body = await request.json().catch(() => null);
47
+ if (!body || typeof body !== 'object') return NextResponse.json({ error: 'A JSON body is required' }, { status: 400 });
48
+ const question = str(body.question);
49
+ if (!question && !str(body.retry_of)) return NextResponse.json({ error: 'question is required' }, { status: 400 });
50
+ if (question && question.length > 10000) return NextResponse.json({ error: 'question holds at most 10000 characters' }, { status: 400 });
51
+ if (body.prompt !== undefined && typeof body.prompt !== 'string') return NextResponse.json({ error: 'prompt is a string' }, { status: 400 });
52
+ if (typeof body.prompt === 'string' && body.prompt.length > 20000) return NextResponse.json({ error: 'prompt holds at most 20000 characters' }, { status: 400 });
53
+ const asked_by = str(body.asked_by);
54
+ if (!asked_by) return NextResponse.json({ error: 'asked_by is required: who asks, by wheel id' }, { status: 400 });
55
+ const read = readScope(body.scope);
56
+ if ('error' in read) return NextResponse.json({ error: read.error }, { status: 400 });
57
+ for (const key of ['opened_from', 'shared_in'] as const) {
58
+ if (body[key] !== undefined && body[key] !== null && typeof body[key] !== 'string') return NextResponse.json({ error: `${key} is a wheel id or null` }, { status: 400 });
59
+ }
60
+ try {
61
+ const out = await (await wheelConversations()).converse({
62
+ ...(question ? { question } : {}),
63
+ ...(str(body.prompt) ? { prompt: str(body.prompt) } : {}),
64
+ scope: read.scope,
65
+ asked_by,
66
+ ...(str(body.asked_by_name) ? { asked_by_name: str(body.asked_by_name) } : {}),
67
+ ...(str(body.conversation) ? { conversation: str(body.conversation) } : {}),
68
+ ...(str(body.retry_of) ? { retry_of: str(body.retry_of) } : {}),
69
+ ...(str(body.held_by) ? { held_by: str(body.held_by) } : {}),
70
+ ...(str(body.held_by_name) ? { held_by_name: str(body.held_by_name) } : {}),
71
+ ...(body.opened_from !== undefined ? { opened_from: str(body.opened_from) ?? null } : {}),
72
+ ...(body.shared_in !== undefined ? { shared_in: str(body.shared_in) ?? null } : {}),
73
+ ...(str(body.title) ? { title: str(body.title) } : {}),
74
+ ...(meta(body.metadata) ? { metadata: meta(body.metadata) } : {}),
75
+ ...(meta(body.turn_metadata) ? { turn_metadata: meta(body.turn_metadata) } : {}),
76
+ ...(str(body.peer) ? { peer: str(body.peer) } : {}),
77
+ ...(str(body.reasoning_level) ? { reasoning_level: str(body.reasoning_level) } : {}),
78
+ ...(readLimit(body.limit) ? { limit: readLimit(body.limit) } : {}),
79
+ });
80
+ return NextResponse.json(out, { status: str(body.conversation) ? 200 : 201, headers: { 'cache-control': 'no-store' } });
81
+ } catch (error) {
82
+ const refused = conversationRefusal(error);
83
+ if (refused) return NextResponse.json({ error: refused.error }, { status: refused.status });
84
+ return NextResponse.json({ error: error instanceof Error ? error.message : String(error) }, { status: 500 });
85
+ }
86
+ }
package/lib/memory.ts CHANGED
@@ -10,7 +10,11 @@
10
10
  */
11
11
 
12
12
  import {
13
+ ConversationError,
14
+ createConversations,
13
15
  createMemory,
16
+ type Conversations,
17
+ type EarlierTurn,
14
18
  type CeremonyRef,
15
19
  type Memory,
16
20
  type MemoryRecord,
@@ -150,6 +154,33 @@ export function wheelMemory(): Memory {
150
154
  return current.memory;
151
155
  }
152
156
 
157
+ /**
158
+ * Conversations with the memory, kept in this wheel's store and asked through
159
+ * `wheelMemory()`. They are never projected into the memory provider.
160
+ * jgwill/medicine-wheel#174
161
+ */
162
+ export async function wheelConversations(): Promise<Conversations> {
163
+ return createConversations({ memory: wheelMemory(), store: await createProvider() });
164
+ }
165
+
166
+ /** A door's answer to a refused conversation request, or null when the error is not one. */
167
+ export function conversationRefusal(error: unknown): { status: number; error: string } | null {
168
+ return error instanceof ConversationError ? { status: error.status, error: error.message } : null;
169
+ }
170
+
171
+ /** Earlier turns from a request body, or the reason they are not. */
172
+ export function readEarlier(value: unknown): { earlier?: EarlierTurn[] } | { error: string } {
173
+ if (value === undefined || value === null) return {};
174
+ if (!Array.isArray(value) || value.length > 200) return { error: 'earlier must be a list of at most 200 turns: [{ asked_by?, question, answer? }]' };
175
+ const earlier: EarlierTurn[] = [];
176
+ for (const t of value) {
177
+ if (!t || typeof t !== 'object' || typeof t.question !== 'string' || !t.question.trim()) return { error: 'each earlier turn needs its question' };
178
+ if (t.answer !== undefined && t.answer !== null && typeof t.answer !== 'string') return { error: 'an earlier answer is a string or null' };
179
+ earlier.push({ question: t.question, ...(typeof t.asked_by === 'string' ? { asked_by: t.asked_by } : {}), ...(typeof t.answer === 'string' ? { answer: t.answer } : {}) });
180
+ }
181
+ return earlier.length ? { earlier } : {};
182
+ }
183
+
153
184
  const SCOPE_LISTS = ['ceremonies', 'subject_id', 'circle_id', 'episode_path'] as const;
154
185
 
155
186
  /** A scope from a request body, or the reason it is not one. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medicine-wheel/app",
3
- "version": "0.27.5",
3
+ "version": "0.27.6",
4
4
  "description": "Medicine Wheel — Interactive visual layer for Indigenous relational research with Four Directions, ceremonies, and narrative arcs",
5
5
  "bin": {
6
6
  "mw": "dist/cli/mw.js",
@@ -64,7 +64,7 @@
64
64
  "mcp"
65
65
  ],
66
66
  "scripts": {
67
- "build:packages": "npm run build -w src/ontology-core -w src/creative-orientation && npm run build --workspaces --if-present",
67
+ "build:packages": "npm run build -w src/ontology-core -w src/creative-orientation -w src/memory && npm run build --workspaces --if-present",
68
68
  "build:cli": "tsc -p tsconfig.cli.json && node -e \"for (const f of ['dist/cli/mw.js','dist/cli/mwsrv.js']) { try { require('fs').chmodSync(f, 0o755); } catch (e) { console.warn('[cli] chmod skipped (non-fatal):', e.message); } }\"",
69
69
  "clean:packages": "npm run clean --workspaces --if-present",
70
70
  "prebuild": "npm run build:packages",
@@ -96,35 +96,35 @@
96
96
  "release:major": "npm run version:major && npm run publish:all && npm run release:commit"
97
97
  },
98
98
  "dependencies": {
99
- "@medicine-wheel/ceremonial-diary": "^0.27.5",
100
- "@medicine-wheel/ceremony-protocol": "^0.27.5",
101
- "@medicine-wheel/community-identity": "^0.27.5",
102
- "@medicine-wheel/community-review": "^0.27.5",
103
- "@medicine-wheel/consent-lifecycle": "^0.27.5",
104
- "@medicine-wheel/creative-orientation": "^0.27.5",
105
- "@medicine-wheel/data-store": "^0.27.5",
106
- "@medicine-wheel/data-store-postgres": "^0.27.5",
107
- "@medicine-wheel/episode-ui": "^0.27.5",
108
- "@medicine-wheel/episode-vessel": "^0.27.5",
109
- "@medicine-wheel/events": "^0.27.5",
110
- "@medicine-wheel/fire-keeper": "^0.27.5",
111
- "@medicine-wheel/github-ceremony": "^0.27.5",
112
- "@medicine-wheel/graph-viz": "^0.27.5",
113
- "@medicine-wheel/honcho": "^0.27.5",
114
- "@medicine-wheel/importance-unit": "^0.27.5",
115
- "@medicine-wheel/mcp": "^4.27.5",
116
- "@medicine-wheel/memory": "^0.27.5",
117
- "@medicine-wheel/narrative-cluster": "^0.27.5",
118
- "@medicine-wheel/narrative-engine": "^0.27.5",
119
- "@medicine-wheel/ontology-core": "^0.27.5",
120
- "@medicine-wheel/perception-layer": "^0.27.5",
121
- "@medicine-wheel/prompt-decomposition": "^0.27.5",
122
- "@medicine-wheel/relational-index": "^0.27.5",
123
- "@medicine-wheel/relational-query": "^0.27.5",
124
- "@medicine-wheel/session-reader": "^0.27.5",
125
- "@medicine-wheel/storage-provider": "^0.27.5",
126
- "@medicine-wheel/transformation-tracker": "^0.27.5",
127
- "@medicine-wheel/ui-components": "^0.27.5",
99
+ "@medicine-wheel/ceremonial-diary": "^0.27.6",
100
+ "@medicine-wheel/ceremony-protocol": "^0.27.6",
101
+ "@medicine-wheel/community-identity": "^0.27.6",
102
+ "@medicine-wheel/community-review": "^0.27.6",
103
+ "@medicine-wheel/consent-lifecycle": "^0.27.6",
104
+ "@medicine-wheel/creative-orientation": "^0.27.6",
105
+ "@medicine-wheel/data-store": "^0.27.6",
106
+ "@medicine-wheel/data-store-postgres": "^0.27.6",
107
+ "@medicine-wheel/episode-ui": "^0.27.6",
108
+ "@medicine-wheel/episode-vessel": "^0.27.6",
109
+ "@medicine-wheel/events": "^0.27.6",
110
+ "@medicine-wheel/fire-keeper": "^0.27.6",
111
+ "@medicine-wheel/github-ceremony": "^0.27.6",
112
+ "@medicine-wheel/graph-viz": "^0.27.6",
113
+ "@medicine-wheel/honcho": "^0.27.6",
114
+ "@medicine-wheel/importance-unit": "^0.27.6",
115
+ "@medicine-wheel/mcp": "^4.27.6",
116
+ "@medicine-wheel/memory": "^0.27.6",
117
+ "@medicine-wheel/narrative-cluster": "^0.27.6",
118
+ "@medicine-wheel/narrative-engine": "^0.27.6",
119
+ "@medicine-wheel/ontology-core": "^0.27.6",
120
+ "@medicine-wheel/perception-layer": "^0.27.6",
121
+ "@medicine-wheel/prompt-decomposition": "^0.27.6",
122
+ "@medicine-wheel/relational-index": "^0.27.6",
123
+ "@medicine-wheel/relational-query": "^0.27.6",
124
+ "@medicine-wheel/session-reader": "^0.27.6",
125
+ "@medicine-wheel/storage-provider": "^0.27.6",
126
+ "@medicine-wheel/transformation-tracker": "^0.27.6",
127
+ "@medicine-wheel/ui-components": "^0.27.6",
128
128
  "@neondatabase/serverless": "^0.10.0",
129
129
  "@xyflow/react": "^12.3.0",
130
130
  "clsx": "^2.1.1",