@medicine-wheel/app 0.27.16 → 0.27.17
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.
- package/app/api/agents/route.ts +99 -0
- package/app/api/edges/route.ts +140 -35
- package/app/api/nodes/[id]/kinship/route.ts +94 -0
- package/app/api/nodes/[id]/readers/route.ts +40 -0
- package/app/nodes/page.tsx +1 -1
- package/app/relations/page.tsx +12 -4
- package/lib/agent-kinship.ts +50 -0
- package/package.json +30 -30
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { agentKinship, DefineAgentSchema, nodeIdsNamedIn, planDefineAgent, type KinshipEdge } from "@medicine-wheel/community-identity";
|
|
3
|
+
import { nodeRemoved, nodeWritten, relationRemoved } from "@medicine-wheel/events";
|
|
4
|
+
import { createProvider, detectProvider } from "@medicine-wheel/storage-provider";
|
|
5
|
+
import { nodesById, refusalIssues, writeKinshipEdges } from "@/lib/agent-kinship";
|
|
6
|
+
import { recordEvent } from "@/lib/event-log";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A person defines an agent they made (rispecs/agent-kinship.spec.md).
|
|
10
|
+
*
|
|
11
|
+
* The agent's seat and its relations are written together: whom it comes
|
|
12
|
+
* from, and with it who holds responsibility for it, whom it serves and which
|
|
13
|
+
* agents it speaks with, when the person says them. Everything is checked
|
|
14
|
+
* before anything is written, so a statement that names a person the wheel
|
|
15
|
+
* does not hold writes nothing. An agent that already has a node is related
|
|
16
|
+
* afterwards with POST /api/nodes/{id}/kinship.
|
|
17
|
+
*
|
|
18
|
+
* The wheel does not check who is calling. That the caller is the person the
|
|
19
|
+
* words belong to is the check of the application in front of it.
|
|
20
|
+
*/
|
|
21
|
+
export async function POST(request: Request) {
|
|
22
|
+
try {
|
|
23
|
+
const body = await request.json().catch(() => null);
|
|
24
|
+
const parsed = DefineAgentSchema.safeParse(body);
|
|
25
|
+
if (!parsed.success) {
|
|
26
|
+
return NextResponse.json(
|
|
27
|
+
{
|
|
28
|
+
error: "Invalid agent — nothing was written.",
|
|
29
|
+
issues: parsed.error.issues.map((issue) => `${issue.path.join(".") || "body"}: ${issue.message}`),
|
|
30
|
+
},
|
|
31
|
+
{ status: 400 },
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const store = await createProvider();
|
|
36
|
+
const { agent, ...relations } = parsed.data;
|
|
37
|
+
if (agent.id && (await store.getNode(agent.id))) {
|
|
38
|
+
return NextResponse.json(
|
|
39
|
+
{
|
|
40
|
+
error: `A node ${agent.id} already exists — nothing was written.`,
|
|
41
|
+
hint: `Relate an agent that already has a node with POST /api/nodes/${encodeURIComponent(agent.id)}/kinship.`,
|
|
42
|
+
},
|
|
43
|
+
{ status: 409 },
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const named = await nodesById(store, nodeIdsNamedIn(relations));
|
|
48
|
+
const plan = planDefineAgent({ input: parsed.data, nodes: named });
|
|
49
|
+
if (!plan.ok) {
|
|
50
|
+
return NextResponse.json(
|
|
51
|
+
{ error: "Invalid agent — nothing was written.", issues: refusalIssues(plan.refusals) },
|
|
52
|
+
{ status: 400 },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// No provider offers a transaction across a node and its edges, so the
|
|
57
|
+
// two are written in order and undone in reverse when a write fails: the
|
|
58
|
+
// wheel is left without half an agent. The event log is kept in step.
|
|
59
|
+
// Each write is recorded as it lands and each removal as it is made, so
|
|
60
|
+
// the log never holds a relation that was made and is nowhere.
|
|
61
|
+
const written: KinshipEdge[] = [];
|
|
62
|
+
const ends = new Map([...named, [plan.node.id, plan.node]]);
|
|
63
|
+
await store.createNode(plan.node);
|
|
64
|
+
recordEvent(nodeWritten(null, plan.node));
|
|
65
|
+
try {
|
|
66
|
+
await writeKinshipEdges(store, plan.edges, ends, written);
|
|
67
|
+
} catch (error: unknown) {
|
|
68
|
+
for (const edge of [...written].reverse()) {
|
|
69
|
+
const removed = await store.deleteEdge(edge.from_id, edge.to_id, edge.kinship_type).then(() => true, () => false);
|
|
70
|
+
if (removed) recordEvent(relationRemoved(edge, { from: ends.get(edge.from_id), to: ends.get(edge.to_id) }));
|
|
71
|
+
}
|
|
72
|
+
const nodeGone = await store.deleteNode(plan.node.id).then(() => true, () => false);
|
|
73
|
+
if (nodeGone) recordEvent(nodeRemoved(plan.node));
|
|
74
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
75
|
+
return NextResponse.json(
|
|
76
|
+
{
|
|
77
|
+
error: `The agent's relations could not be written: ${message}`,
|
|
78
|
+
rolled_back: nodeGone,
|
|
79
|
+
...(nodeGone ? {} : { left_on_the_wheel: plan.node.id }),
|
|
80
|
+
},
|
|
81
|
+
{ status: 500 },
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return NextResponse.json(
|
|
86
|
+
{
|
|
87
|
+
success: true,
|
|
88
|
+
node: plan.node,
|
|
89
|
+
edges: plan.edges,
|
|
90
|
+
kinship: agentKinship(plan.node.id, plan.edges),
|
|
91
|
+
provider: detectProvider(),
|
|
92
|
+
},
|
|
93
|
+
{ status: 201 },
|
|
94
|
+
);
|
|
95
|
+
} catch (error: unknown) {
|
|
96
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
97
|
+
return NextResponse.json({ error: message }, { status: 500 });
|
|
98
|
+
}
|
|
99
|
+
}
|
package/app/api/edges/route.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { NextResponse } from "next/server";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
+
import { agentKinshipWriteRefusal, isAgentKinshipEdge } from "@medicine-wheel/community-identity";
|
|
4
|
+
import { KinshipEdgeNameSchema, RelationContextSchema } from "@medicine-wheel/ontology-core";
|
|
5
|
+
import type { KinshipEdgeName, RelationContext } from "@medicine-wheel/ontology-core";
|
|
3
6
|
import {
|
|
4
7
|
createProvider,
|
|
5
8
|
detectProvider,
|
|
@@ -9,18 +12,44 @@ import { relationRemoved, relationWritten } from "@medicine-wheel/events";
|
|
|
9
12
|
import { parseLimit } from "@/lib/api-paging";
|
|
10
13
|
import { recordEvent } from "@/lib/event-log";
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The context that authorizes and bounds a relation. Strict here: a context
|
|
17
|
+
* key this wheel does not know is a 400, not a key dropped in silence.
|
|
18
|
+
*/
|
|
19
|
+
const ContextSchema = RelationContextSchema.strict();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A relation may name its governed kinship type (`kinship_type`, from the
|
|
23
|
+
* registry in ontology-core). It is then identified by its two ends and that
|
|
24
|
+
* name, so the same two nodes can hold one relation for each kinship type
|
|
25
|
+
* beside the one relation they hold without any. `relationship_type` may be
|
|
26
|
+
* left out when `kinship_type` is given, and must say the same name when both
|
|
27
|
+
* are sent: one relation does not carry two names.
|
|
28
|
+
*/
|
|
29
|
+
const EdgeCreateSchema = z
|
|
30
|
+
.object({
|
|
31
|
+
from_id: z.string({ required_error: "from_id is required" }).trim().min(1),
|
|
32
|
+
to_id: z.string({ required_error: "to_id is required" }).trim().min(1),
|
|
33
|
+
relationship_type: z.string().trim().min(1, "cannot be empty").optional(),
|
|
34
|
+
kinship_type: KinshipEdgeNameSchema.optional(),
|
|
35
|
+
strength: z.number().min(0).max(1).optional(),
|
|
36
|
+
ceremony_honored: z.boolean().optional(),
|
|
37
|
+
obligations: z.array(z.string()).optional(),
|
|
38
|
+
description: z.string().trim().max(2000).optional(),
|
|
39
|
+
context: ContextSchema.optional(),
|
|
40
|
+
})
|
|
41
|
+
.superRefine((edge, ctx) => {
|
|
42
|
+
if (!edge.relationship_type && !edge.kinship_type) {
|
|
43
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["relationship_type"], message: "relationship_type is required" });
|
|
44
|
+
}
|
|
45
|
+
if (edge.relationship_type && edge.kinship_type && edge.relationship_type !== edge.kinship_type) {
|
|
46
|
+
ctx.addIssue({
|
|
47
|
+
code: z.ZodIssueCode.custom,
|
|
48
|
+
path: ["relationship_type"],
|
|
49
|
+
message: `must be ${edge.kinship_type}, the kinship_type, or left out`,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
24
53
|
|
|
25
54
|
const EdgePatchSchema = z
|
|
26
55
|
.object({
|
|
@@ -29,21 +58,24 @@ const EdgePatchSchema = z
|
|
|
29
58
|
ceremony_honored: z.boolean().optional(),
|
|
30
59
|
obligations: z.array(z.string()).optional(),
|
|
31
60
|
description: z.string().trim().max(2000).optional(),
|
|
61
|
+
context: ContextSchema.optional(),
|
|
32
62
|
})
|
|
33
63
|
.strict()
|
|
34
64
|
.refine((patch) => Object.keys(patch).length > 0, {
|
|
35
65
|
message:
|
|
36
|
-
"Provide at least one field to change: relationship_type, strength, ceremony_honored, obligations, or
|
|
66
|
+
"Provide at least one field to change: relationship_type, strength, ceremony_honored, obligations, description, or context.",
|
|
37
67
|
});
|
|
38
68
|
|
|
39
69
|
/**
|
|
40
|
-
* Relations are identified by their pair of nodes (from → to)
|
|
41
|
-
*
|
|
70
|
+
* Relations are identified by their pair of nodes (from → to), and by their
|
|
71
|
+
* kinship type when they name one.
|
|
72
|
+
* Accepts ?from=&to=, ?id=<from>:<to>, or from_id/to_id in the body; and
|
|
73
|
+
* ?kinship_type=, or kinship_type in the body, for a relation that names one.
|
|
42
74
|
*/
|
|
43
75
|
function resolvePair(
|
|
44
76
|
request: Request,
|
|
45
77
|
body: Record<string, unknown> | null,
|
|
46
|
-
): { from: string; to: string } | null {
|
|
78
|
+
): { from: string; to: string; kinship?: string } | null {
|
|
47
79
|
const { searchParams } = new URL(request.url);
|
|
48
80
|
let from = searchParams.get("from");
|
|
49
81
|
let to = searchParams.get("to");
|
|
@@ -60,7 +92,12 @@ function resolvePair(
|
|
|
60
92
|
if (!to && typeof body.to_id === "string") to = body.to_id;
|
|
61
93
|
}
|
|
62
94
|
|
|
63
|
-
|
|
95
|
+
const kinship =
|
|
96
|
+
searchParams.get("kinship_type") ||
|
|
97
|
+
(body && typeof body.kinship_type === "string" ? body.kinship_type : "") ||
|
|
98
|
+
undefined;
|
|
99
|
+
|
|
100
|
+
return from && to ? { from, to, ...(kinship ? { kinship } : {}) } : null;
|
|
64
101
|
}
|
|
65
102
|
|
|
66
103
|
function pairRequired() {
|
|
@@ -73,16 +110,36 @@ function pairRequired() {
|
|
|
73
110
|
);
|
|
74
111
|
}
|
|
75
112
|
|
|
76
|
-
function relationNotFound(from: string, to: string) {
|
|
113
|
+
function relationNotFound(from: string, to: string, kinship?: string) {
|
|
77
114
|
return NextResponse.json(
|
|
78
115
|
{
|
|
79
|
-
error:
|
|
80
|
-
|
|
116
|
+
error: kinship
|
|
117
|
+
? `No ${kinship} relation exists from ${from} to ${to}.`
|
|
118
|
+
: `No relation exists from ${from} to ${to}.`,
|
|
119
|
+
hint: kinship
|
|
120
|
+
? "Relations are directional — check the from/to order and the kinship_type, or refresh the relations list."
|
|
121
|
+
: "Relations are directional — check the from/to order, or refresh the relations list. A relation that names a kinship type is reached with ?kinship_type=.",
|
|
81
122
|
},
|
|
82
123
|
{ status: 404 },
|
|
83
124
|
);
|
|
84
125
|
}
|
|
85
126
|
|
|
127
|
+
/**
|
|
128
|
+
* This door writes any relation and checks only that its two nodes exist. The
|
|
129
|
+
* four relations between a person and their agents are checked by the kinship
|
|
130
|
+
* door, so this one does not create them and does not change their context or
|
|
131
|
+
* their words (rispecs/agent-kinship.spec.md).
|
|
132
|
+
*/
|
|
133
|
+
function leftToTheKinshipDoor(reason: string) {
|
|
134
|
+
return NextResponse.json(
|
|
135
|
+
{
|
|
136
|
+
error: `${reason} Nothing was written.`,
|
|
137
|
+
hint: "Use POST /api/nodes/{agent id}/kinship, or POST /api/agents for a new agent. This door still releases such a relation (DELETE) and still records that a ceremony honoured it.",
|
|
138
|
+
},
|
|
139
|
+
{ status: 400 },
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
86
143
|
function unexpected(error: unknown) {
|
|
87
144
|
const message = error instanceof Error ? error.message : String(error);
|
|
88
145
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
@@ -162,18 +219,41 @@ export async function POST(request: Request) {
|
|
|
162
219
|
);
|
|
163
220
|
}
|
|
164
221
|
|
|
165
|
-
|
|
222
|
+
// The relation this write replaces is the one with the same identity: the
|
|
223
|
+
// pair, and the kinship type when one is named.
|
|
224
|
+
const kinship = parsed.data.kinship_type as KinshipEdgeName | undefined;
|
|
225
|
+
const previous = await store.getEdge(parsed.data.from_id, parsed.data.to_id, kinship);
|
|
166
226
|
const replaced = Boolean(previous);
|
|
167
227
|
|
|
228
|
+
const refusal = agentKinshipWriteRefusal({
|
|
229
|
+
kinship_type: kinship,
|
|
230
|
+
existing: previous,
|
|
231
|
+
context: parsed.data.context,
|
|
232
|
+
description: parsed.data.description,
|
|
233
|
+
});
|
|
234
|
+
if (refusal) return leftToTheKinshipDoor(refusal);
|
|
235
|
+
// Past that check, one of the four exists already and this write brings
|
|
236
|
+
// its context and its words as they are, or does not bring them: both stay.
|
|
237
|
+
const kept = previous && isAgentKinshipEdge(kinship) ? previous : null;
|
|
238
|
+
const description = kept ? kept.description : parsed.data.description;
|
|
239
|
+
const context = kept ? kept.context : (parsed.data.context as RelationContext | undefined);
|
|
240
|
+
|
|
168
241
|
const edge = {
|
|
169
242
|
from_id: parsed.data.from_id,
|
|
170
243
|
to_id: parsed.data.to_id,
|
|
171
|
-
relationship_type: parsed.data.relationship_type,
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
244
|
+
relationship_type: parsed.data.relationship_type ?? (kinship as string),
|
|
245
|
+
// For one of the four, what the write leaves out stays as it is: this
|
|
246
|
+
// door cannot create such a relation, so a write here is never its first.
|
|
247
|
+
strength: parsed.data.strength ?? kept?.strength ?? 0.5,
|
|
248
|
+
ceremony_honored: parsed.data.ceremony_honored ?? kept?.ceremony_honored ?? false,
|
|
249
|
+
obligations: parsed.data.obligations ?? kept?.obligations ?? [],
|
|
250
|
+
...(kept?.last_ceremony ? { last_ceremony: kept.last_ceremony } : {}),
|
|
251
|
+
...(description ? { description } : {}),
|
|
252
|
+
...(kinship ? { kinship_type: kinship } : {}),
|
|
253
|
+
...(context ? { context } : {}),
|
|
254
|
+
// A governed relation keeps the date it was first written: a ceremony
|
|
255
|
+
// that honours it, or a write that says it again, does not restart it.
|
|
256
|
+
created_at: kinship && previous ? previous.created_at : new Date().toISOString(),
|
|
177
257
|
};
|
|
178
258
|
|
|
179
259
|
await store.createEdge(edge);
|
|
@@ -196,10 +276,11 @@ export async function PATCH(request: Request) {
|
|
|
196
276
|
const pair = resolvePair(request, body);
|
|
197
277
|
if (!pair) return pairRequired();
|
|
198
278
|
|
|
199
|
-
// from_id/to_id in the body only identify the relation — strip before validating the patch.
|
|
279
|
+
// from_id/to_id/kinship_type in the body only identify the relation — strip before validating the patch.
|
|
200
280
|
const patchBody = body ? { ...body } : {};
|
|
201
281
|
delete patchBody.from_id;
|
|
202
282
|
delete patchBody.to_id;
|
|
283
|
+
delete patchBody.kinship_type;
|
|
203
284
|
|
|
204
285
|
const parsed = EdgePatchSchema.safeParse(patchBody);
|
|
205
286
|
if (!parsed.success) {
|
|
@@ -214,15 +295,39 @@ export async function PATCH(request: Request) {
|
|
|
214
295
|
);
|
|
215
296
|
}
|
|
216
297
|
|
|
298
|
+
if (pair.kinship && parsed.data.relationship_type && parsed.data.relationship_type !== pair.kinship) {
|
|
299
|
+
return NextResponse.json(
|
|
300
|
+
{
|
|
301
|
+
error: "Invalid relation patch — nothing was changed.",
|
|
302
|
+
issues: [`relationship_type: a ${pair.kinship} relation keeps its name; remove it and weave another to change its kind`],
|
|
303
|
+
},
|
|
304
|
+
{ status: 400 },
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
217
308
|
const store = await createProvider();
|
|
218
|
-
const previous = await store.getEdge(pair.from, pair.to);
|
|
219
|
-
|
|
309
|
+
const previous = await store.getEdge(pair.from, pair.to, pair.kinship);
|
|
310
|
+
if (previous) {
|
|
311
|
+
const refusal = agentKinshipWriteRefusal({
|
|
312
|
+
kinship_type: pair.kinship,
|
|
313
|
+
existing: previous,
|
|
314
|
+
context: parsed.data.context,
|
|
315
|
+
description: parsed.data.description,
|
|
316
|
+
});
|
|
317
|
+
if (refusal) return leftToTheKinshipDoor(refusal);
|
|
318
|
+
}
|
|
319
|
+
const edge = await store.updateEdge(
|
|
320
|
+
pair.from,
|
|
321
|
+
pair.to,
|
|
322
|
+
{ ...parsed.data, ...(parsed.data.context ? { context: parsed.data.context as RelationContext } : {}) },
|
|
323
|
+
pair.kinship,
|
|
324
|
+
);
|
|
220
325
|
const [fromNode, toNode] = await Promise.all([store.getNode(pair.from), store.getNode(pair.to)]);
|
|
221
326
|
recordEvent(relationWritten(previous ?? edge, edge, { from: fromNode, to: toNode }));
|
|
222
327
|
return NextResponse.json({ success: true, edge, provider: detectProvider() });
|
|
223
328
|
} catch (error: unknown) {
|
|
224
329
|
if (error instanceof EdgeNotFoundError) {
|
|
225
|
-
return relationNotFound(error.fromId, error.toId);
|
|
330
|
+
return relationNotFound(error.fromId, error.toId, error.kinshipType);
|
|
226
331
|
}
|
|
227
332
|
return unexpected(error);
|
|
228
333
|
}
|
|
@@ -235,18 +340,18 @@ export async function DELETE(request: Request) {
|
|
|
235
340
|
if (!pair) return pairRequired();
|
|
236
341
|
|
|
237
342
|
const store = await createProvider();
|
|
238
|
-
const previous = await store.getEdge(pair.from, pair.to);
|
|
343
|
+
const previous = await store.getEdge(pair.from, pair.to, pair.kinship);
|
|
239
344
|
const [fromNode, toNode] = await Promise.all([store.getNode(pair.from), store.getNode(pair.to)]);
|
|
240
|
-
await store.deleteEdge(pair.from, pair.to);
|
|
345
|
+
await store.deleteEdge(pair.from, pair.to, pair.kinship);
|
|
241
346
|
recordEvent(relationRemoved(previous ?? { from_id: pair.from, to_id: pair.to }, { from: fromNode, to: toNode }));
|
|
242
347
|
return NextResponse.json({
|
|
243
348
|
success: true,
|
|
244
|
-
deleted: { from_id: pair.from, to_id: pair.to },
|
|
349
|
+
deleted: { from_id: pair.from, to_id: pair.to, ...(pair.kinship ? { kinship_type: pair.kinship } : {}) },
|
|
245
350
|
provider: detectProvider(),
|
|
246
351
|
});
|
|
247
352
|
} catch (error: unknown) {
|
|
248
353
|
if (error instanceof EdgeNotFoundError) {
|
|
249
|
-
return relationNotFound(error.fromId, error.toId);
|
|
354
|
+
return relationNotFound(error.fromId, error.toId, error.kinshipType);
|
|
250
355
|
}
|
|
251
356
|
return unexpected(error);
|
|
252
357
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { agentKinship, AgentKinshipInputSchema, nodeIdsNamedIn, planAgentKinship, readKinship } from "@medicine-wheel/community-identity";
|
|
3
|
+
import { createProvider, detectProvider } from "@medicine-wheel/storage-provider";
|
|
4
|
+
import { kinshipRecords, nodesById, refusalIssues, writeKinshipEdges } from "@/lib/agent-kinship";
|
|
5
|
+
|
|
6
|
+
function notFound(id: string) {
|
|
7
|
+
return NextResponse.json(
|
|
8
|
+
{ error: `No node with id ${id}.`, hint: "Check the id on /nodes, or list the agents with /api/nodes?type=agent&limit=all." },
|
|
9
|
+
{ status: 404 },
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* What the wheel reads back for one person (rispecs/agent-kinship.spec.md).
|
|
15
|
+
*
|
|
16
|
+
* For an agent: whom it comes from, who holds responsibility for it, whom it
|
|
17
|
+
* serves and what each of those persons gives it, which agents it speaks with
|
|
18
|
+
* and in which state, and the circles it sees through the persons it serves.
|
|
19
|
+
* For any person: the agents they gave rise to, hold responsibility for and
|
|
20
|
+
* are served by. Each relation carries the words of the one who said it.
|
|
21
|
+
*/
|
|
22
|
+
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
23
|
+
try {
|
|
24
|
+
const { id } = await params;
|
|
25
|
+
const store = await createProvider();
|
|
26
|
+
const node = await store.getNode(id);
|
|
27
|
+
if (!node) return notFound(id);
|
|
28
|
+
const reading = readKinship(node, await kinshipRecords(store));
|
|
29
|
+
if (!reading) {
|
|
30
|
+
return NextResponse.json(
|
|
31
|
+
{ error: `${id} is not a person on the wheel.`, hint: "Kinship is read for a person's seat: a human or agent node whose metadata.kind is person." },
|
|
32
|
+
{ status: 400 },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return NextResponse.json({ ...reading, provider: detectProvider() });
|
|
36
|
+
} catch (error: unknown) {
|
|
37
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
38
|
+
return NextResponse.json({ error: message }, { status: 500 });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* An agent that already has a node is related to its persons, and to the
|
|
44
|
+
* agents it speaks with. Saying a relation again amends it: the same two
|
|
45
|
+
* seats and the same kind of relation are one relation, and it keeps the date
|
|
46
|
+
* it was first written. For origin, responsibility and service the new
|
|
47
|
+
* statement replaces the earlier one whole, grants included. For a dialogue,
|
|
48
|
+
* what is left out stays as it is.
|
|
49
|
+
*/
|
|
50
|
+
export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
51
|
+
try {
|
|
52
|
+
const { id } = await params;
|
|
53
|
+
const body = await request.json().catch(() => null);
|
|
54
|
+
const parsed = AgentKinshipInputSchema.safeParse(body);
|
|
55
|
+
if (!parsed.success) {
|
|
56
|
+
return NextResponse.json(
|
|
57
|
+
{
|
|
58
|
+
error: "Invalid kinship — nothing was written.",
|
|
59
|
+
issues: parsed.error.issues.map((issue) => `${issue.path.join(".") || "body"}: ${issue.message}`),
|
|
60
|
+
},
|
|
61
|
+
{ status: 400 },
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const store = await createProvider();
|
|
66
|
+
const agent = await store.getNode(id);
|
|
67
|
+
if (!agent) return notFound(id);
|
|
68
|
+
|
|
69
|
+
const [named, allEdges] = await Promise.all([
|
|
70
|
+
nodesById(store, nodeIdsNamedIn(parsed.data)),
|
|
71
|
+
store.getAllEdges(Number.MAX_SAFE_INTEGER),
|
|
72
|
+
]);
|
|
73
|
+
const touching = allEdges.filter((edge) => edge.from_id === id || edge.to_id === id);
|
|
74
|
+
const plan = planAgentKinship({ agent, input: parsed.data, nodes: named, edges: touching });
|
|
75
|
+
if (!plan.ok) {
|
|
76
|
+
return NextResponse.json(
|
|
77
|
+
{ error: "Invalid kinship — nothing was written.", issues: refusalIssues(plan.refusals) },
|
|
78
|
+
{ status: 400 },
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
await writeKinshipEdges(store, plan.edges, new Map([...named, [agent.id, agent]]));
|
|
83
|
+
const after = await store.getAllEdges(Number.MAX_SAFE_INTEGER);
|
|
84
|
+
return NextResponse.json({
|
|
85
|
+
success: true,
|
|
86
|
+
edges: plan.edges,
|
|
87
|
+
kinship: agentKinship(id, after),
|
|
88
|
+
provider: detectProvider(),
|
|
89
|
+
});
|
|
90
|
+
} catch (error: unknown) {
|
|
91
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
92
|
+
return NextResponse.json({ error: message }, { status: 500 });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { readCircleReaders } from "@medicine-wheel/community-identity";
|
|
3
|
+
import { createProvider, detectProvider } from "@medicine-wheel/storage-provider";
|
|
4
|
+
import { kinshipRecords } from "@/lib/agent-kinship";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The agents that read a circle through one of its members
|
|
8
|
+
* (rispecs/agent-kinship.spec.md): what a circle's page shows.
|
|
9
|
+
*
|
|
10
|
+
* A person can give an agent the view of the circles they sit in. The other
|
|
11
|
+
* members admitted that person, not the agent, so the circle answers for
|
|
12
|
+
* itself: `agent_readers` is its answer (allowed or refused, and who set it),
|
|
13
|
+
* `when_unset` what it answers before anyone has set one. Each reader comes
|
|
14
|
+
* with the member it reads through; `sees` is false for an agent the circle
|
|
15
|
+
* holds back, so a facilitator can see who would read before allowing.
|
|
16
|
+
*
|
|
17
|
+
* The answer is set by patching the circle's node with
|
|
18
|
+
* `agentReadersPatch` from `@medicine-wheel/community-identity`.
|
|
19
|
+
*/
|
|
20
|
+
export async function GET(_request: Request, { params }: { params: Promise<{ id: string }> }) {
|
|
21
|
+
try {
|
|
22
|
+
const { id } = await params;
|
|
23
|
+
const store = await createProvider();
|
|
24
|
+
const node = await store.getNode(id);
|
|
25
|
+
if (!node) {
|
|
26
|
+
return NextResponse.json(
|
|
27
|
+
{ error: `No node with id ${id}.`, hint: "List the circles with /api/nodes?type=circle&limit=all." },
|
|
28
|
+
{ status: 404 },
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
const reading = readCircleReaders(node, await kinshipRecords(store));
|
|
32
|
+
if (!reading) {
|
|
33
|
+
return NextResponse.json({ error: `${id} is not a circle.` }, { status: 400 });
|
|
34
|
+
}
|
|
35
|
+
return NextResponse.json({ ...reading, provider: detectProvider() });
|
|
36
|
+
} catch (error: unknown) {
|
|
37
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
38
|
+
return NextResponse.json({ error: message }, { status: 500 });
|
|
39
|
+
}
|
|
40
|
+
}
|
package/app/nodes/page.tsx
CHANGED
|
@@ -342,7 +342,7 @@ export default function NodesPage() {
|
|
|
342
342
|
const otherId = e.from_id === node.id ? e.to_id : e.from_id;
|
|
343
343
|
const other = nodes.find((n) => n.id === otherId);
|
|
344
344
|
return (
|
|
345
|
-
<div key={
|
|
345
|
+
<div key={[e.from_id, e.to_id, ...(e.kinship_type ? [e.kinship_type] : [])].join(":")} className="py-1 text-sm border-b border-border/50 flex items-center gap-2">
|
|
346
346
|
<span className="text-muted-foreground">{e.relationship_type}</span>
|
|
347
347
|
<span aria-hidden>{e.from_id === node.id ? "→" : "←"}</span>
|
|
348
348
|
{other?.name || "?"}
|
package/app/relations/page.tsx
CHANGED
|
@@ -15,8 +15,16 @@ type NodeRecord = RelationalNode & { description?: string };
|
|
|
15
15
|
|
|
16
16
|
const DIRECTION_ICONS: Record<DirectionName, string> = { east: "🌅", south: "🌞", west: "🌄", north: "❄️" };
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
// A relation is identified by its two ends, and by its kinship type when it
|
|
19
|
+
// names one: an agent can come from a person and also serve that person.
|
|
20
|
+
function edgeKey(edge: Pick<RelationalEdge, "from_id" | "to_id" | "kinship_type">): string {
|
|
21
|
+
return [edge.from_id, edge.to_id, ...(edge.kinship_type ? [edge.kinship_type] : [])].join(":");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The query that reaches this relation on /api/edges, and no other relation of the same pair. */
|
|
25
|
+
function edgeQuery(edge: Pick<RelationalEdge, "from_id" | "to_id" | "kinship_type">): string {
|
|
26
|
+
const base = `from=${encodeURIComponent(edge.from_id)}&to=${encodeURIComponent(edge.to_id)}`;
|
|
27
|
+
return edge.kinship_type ? `${base}&kinship_type=${encodeURIComponent(edge.kinship_type)}` : base;
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
function NodeChip({ node, id }: { node?: NodeRecord; id: string }) {
|
|
@@ -140,7 +148,7 @@ export default function RelationsPage() {
|
|
|
140
148
|
};
|
|
141
149
|
setBusy(true);
|
|
142
150
|
try {
|
|
143
|
-
const res = await fetch(`/api/edges
|
|
151
|
+
const res = await fetch(`/api/edges?${edgeQuery(edge)}`, {
|
|
144
152
|
method: "PATCH",
|
|
145
153
|
headers: { "Content-Type": "application/json" },
|
|
146
154
|
body: JSON.stringify(body),
|
|
@@ -160,7 +168,7 @@ export default function RelationsPage() {
|
|
|
160
168
|
async function releaseRelation(edge: RelationalEdge) {
|
|
161
169
|
setBusy(true);
|
|
162
170
|
try {
|
|
163
|
-
const res = await fetch(`/api/edges
|
|
171
|
+
const res = await fetch(`/api/edges?${edgeQuery(edge)}`, { method: "DELETE" });
|
|
164
172
|
const data = await res.json();
|
|
165
173
|
if (!res.ok) throw new Error(data.error || "Relation was not released");
|
|
166
174
|
toast.success("Relation released — both nodes remain in the wheel");
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wheel's side of agent kinship (rispecs/agent-kinship.spec.md): fetching
|
|
3
|
+
* the nodes a statement names and writing the planned edges with their
|
|
4
|
+
* events. The rules and the readings live in
|
|
5
|
+
* `@medicine-wheel/community-identity`; this file only joins them to the store.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { KinshipEdge, KinshipRefusal } from "@medicine-wheel/community-identity";
|
|
9
|
+
import { relationWritten } from "@medicine-wheel/events";
|
|
10
|
+
import type { RelationalNode, StorageProvider } from "@medicine-wheel/storage-provider";
|
|
11
|
+
import { recordEvent } from "@/lib/event-log";
|
|
12
|
+
|
|
13
|
+
/** The nodes with these ids that the wheel holds, by id. An id it does not hold is absent from the map. */
|
|
14
|
+
export async function nodesById(store: StorageProvider, ids: readonly string[]): Promise<Map<string, RelationalNode>> {
|
|
15
|
+
const found = await Promise.all([...new Set(ids)].map(async (id) => [id, await store.getNode(id)] as const));
|
|
16
|
+
return new Map(found.filter((pair): pair is readonly [string, RelationalNode] => pair[1] !== null));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function refusalIssues(refusals: readonly KinshipRefusal[]): string[] {
|
|
20
|
+
return refusals.map((r) => `${r.path}: ${r.message}`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The whole store, for a reading: a reading over a page would miss relations without saying so. */
|
|
24
|
+
export async function kinshipRecords(store: StorageProvider) {
|
|
25
|
+
const [nodes, edges] = await Promise.all([
|
|
26
|
+
store.getAllNodes(Number.MAX_SAFE_INTEGER),
|
|
27
|
+
store.getAllEdges(Number.MAX_SAFE_INTEGER),
|
|
28
|
+
]);
|
|
29
|
+
return { nodes, edges };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Writes each planned edge and records it in the event log. A relation that
|
|
34
|
+
* already exists is amended in place: same two ends, same kinship type.
|
|
35
|
+
* `written` receives each edge as it lands, so a caller can undo them.
|
|
36
|
+
*/
|
|
37
|
+
export async function writeKinshipEdges(
|
|
38
|
+
store: StorageProvider,
|
|
39
|
+
edges: readonly KinshipEdge[],
|
|
40
|
+
ends: ReadonlyMap<string, RelationalNode>,
|
|
41
|
+
written: KinshipEdge[] = [],
|
|
42
|
+
): Promise<KinshipEdge[]> {
|
|
43
|
+
for (const edge of edges) {
|
|
44
|
+
const previous = await store.getEdge(edge.from_id, edge.to_id, edge.kinship_type);
|
|
45
|
+
await store.createEdge(edge);
|
|
46
|
+
written.push(edge);
|
|
47
|
+
recordEvent(relationWritten(previous, edge, { from: ends.get(edge.from_id), to: ends.get(edge.to_id) }));
|
|
48
|
+
}
|
|
49
|
+
return written;
|
|
50
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@medicine-wheel/app",
|
|
3
|
-
"version": "0.27.
|
|
3
|
+
"version": "0.27.17",
|
|
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",
|
|
@@ -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.
|
|
100
|
-
"@medicine-wheel/ceremony-protocol": "^0.27.
|
|
101
|
-
"@medicine-wheel/community-identity": "^0.27.
|
|
102
|
-
"@medicine-wheel/community-review": "^0.27.
|
|
103
|
-
"@medicine-wheel/consent-lifecycle": "^0.27.
|
|
104
|
-
"@medicine-wheel/creative-orientation": "^0.27.
|
|
105
|
-
"@medicine-wheel/data-store": "^0.27.
|
|
106
|
-
"@medicine-wheel/data-store-postgres": "^0.27.
|
|
107
|
-
"@medicine-wheel/episode-ui": "^0.27.
|
|
108
|
-
"@medicine-wheel/episode-vessel": "^0.27.
|
|
109
|
-
"@medicine-wheel/events": "^0.27.
|
|
110
|
-
"@medicine-wheel/fire-keeper": "^0.27.
|
|
111
|
-
"@medicine-wheel/github-ceremony": "^0.27.
|
|
112
|
-
"@medicine-wheel/graph-viz": "^0.27.
|
|
113
|
-
"@medicine-wheel/honcho": "^0.27.
|
|
114
|
-
"@medicine-wheel/importance-unit": "^0.27.
|
|
115
|
-
"@medicine-wheel/mcp": "^4.27.
|
|
116
|
-
"@medicine-wheel/memory": "^0.27.
|
|
117
|
-
"@medicine-wheel/narrative-cluster": "^0.27.
|
|
118
|
-
"@medicine-wheel/narrative-engine": "^0.27.
|
|
119
|
-
"@medicine-wheel/ontology-core": "^0.27.
|
|
120
|
-
"@medicine-wheel/perception-layer": "^0.27.
|
|
121
|
-
"@medicine-wheel/prompt-decomposition": "^0.27.
|
|
122
|
-
"@medicine-wheel/relational-index": "^0.27.
|
|
123
|
-
"@medicine-wheel/relational-query": "^0.27.
|
|
124
|
-
"@medicine-wheel/session-reader": "^0.27.
|
|
125
|
-
"@medicine-wheel/storage-provider": "^0.27.
|
|
126
|
-
"@medicine-wheel/transformation-tracker": "^0.27.
|
|
127
|
-
"@medicine-wheel/ui-components": "^0.27.
|
|
99
|
+
"@medicine-wheel/ceremonial-diary": "^0.27.17",
|
|
100
|
+
"@medicine-wheel/ceremony-protocol": "^0.27.17",
|
|
101
|
+
"@medicine-wheel/community-identity": "^0.27.17",
|
|
102
|
+
"@medicine-wheel/community-review": "^0.27.17",
|
|
103
|
+
"@medicine-wheel/consent-lifecycle": "^0.27.17",
|
|
104
|
+
"@medicine-wheel/creative-orientation": "^0.27.17",
|
|
105
|
+
"@medicine-wheel/data-store": "^0.27.17",
|
|
106
|
+
"@medicine-wheel/data-store-postgres": "^0.27.17",
|
|
107
|
+
"@medicine-wheel/episode-ui": "^0.27.17",
|
|
108
|
+
"@medicine-wheel/episode-vessel": "^0.27.17",
|
|
109
|
+
"@medicine-wheel/events": "^0.27.17",
|
|
110
|
+
"@medicine-wheel/fire-keeper": "^0.27.17",
|
|
111
|
+
"@medicine-wheel/github-ceremony": "^0.27.17",
|
|
112
|
+
"@medicine-wheel/graph-viz": "^0.27.17",
|
|
113
|
+
"@medicine-wheel/honcho": "^0.27.17",
|
|
114
|
+
"@medicine-wheel/importance-unit": "^0.27.17",
|
|
115
|
+
"@medicine-wheel/mcp": "^4.27.17",
|
|
116
|
+
"@medicine-wheel/memory": "^0.27.17",
|
|
117
|
+
"@medicine-wheel/narrative-cluster": "^0.27.17",
|
|
118
|
+
"@medicine-wheel/narrative-engine": "^0.27.17",
|
|
119
|
+
"@medicine-wheel/ontology-core": "^0.27.17",
|
|
120
|
+
"@medicine-wheel/perception-layer": "^0.27.17",
|
|
121
|
+
"@medicine-wheel/prompt-decomposition": "^0.27.17",
|
|
122
|
+
"@medicine-wheel/relational-index": "^0.27.17",
|
|
123
|
+
"@medicine-wheel/relational-query": "^0.27.17",
|
|
124
|
+
"@medicine-wheel/session-reader": "^0.27.17",
|
|
125
|
+
"@medicine-wheel/storage-provider": "^0.27.17",
|
|
126
|
+
"@medicine-wheel/transformation-tracker": "^0.27.17",
|
|
127
|
+
"@medicine-wheel/ui-components": "^0.27.17",
|
|
128
128
|
"@neondatabase/serverless": "^0.10.0",
|
|
129
129
|
"@xyflow/react": "^12.3.0",
|
|
130
130
|
"clsx": "^2.1.1",
|