@mmon-co/ncept-bb 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +65 -2
- package/dist/app.css +2 -0
- package/dist/app.js +68 -0
- package/dist/app.meta.json +17 -0
- package/dist/host.js +1806 -0
- package/dist/host.js.map +6 -0
- package/dist/host.meta.json +12 -0
- package/dist/package.json +1 -0
- package/dist/server.js +69 -0
- package/dist/server.js.map +6 -0
- package/dist/server.meta.json +11 -0
- package/package.json +69 -4
- package/skills/ncept-bb/SKILL.md +12 -0
- package/src/app.tsx +366 -0
- package/src/contract.ts +81 -0
- package/src/entries.ts +32 -0
- package/src/extension.ts +149 -0
- package/src/graph-positions.ts +302 -0
- package/src/graph.ts +57 -0
- package/src/host.ts +209 -0
- package/src/map.css +131 -0
- package/src/map.tsx +345 -0
- package/src/mission-page.tsx +450 -0
- package/src/mission-reading.ts +37 -0
- package/src/reading.ts +55 -0
- package/src/server.ts +447 -0
- package/src/ui/button.tsx +26 -0
package/src/host.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { readFile, realpath, writeFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { experimental_defineHostEntry } from "@get-bb/plugin-sdk/host";
|
|
5
|
+
import { getAgentDir, ProjectTrustStore } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { initProject } from "@mmon-co/ncept-pi";
|
|
7
|
+
import { openHost, openLearningReader, openProfile } from "@mmon-co/ncept-local";
|
|
8
|
+
import { isMissionRevision, isRunActiveChange } from "@mmon-co/ncept-core";
|
|
9
|
+
import { defaultTheory } from "@mmon-co/ncept-default-theory";
|
|
10
|
+
import { handoffSchema, hostContract } from "./contract";
|
|
11
|
+
import { settleGraph } from "./graph-positions";
|
|
12
|
+
|
|
13
|
+
export default experimental_defineHostEntry({
|
|
14
|
+
contract: hostContract,
|
|
15
|
+
handlers: {
|
|
16
|
+
retire: async ({ directory, missionId, outcome }) => {
|
|
17
|
+
const host = await openHost({
|
|
18
|
+
profileDir: directory,
|
|
19
|
+
theories: [defaultTheory],
|
|
20
|
+
model: async () => {
|
|
21
|
+
throw new Error("Finishing a Mission makes no model requests");
|
|
22
|
+
},
|
|
23
|
+
confirm: async () => true,
|
|
24
|
+
});
|
|
25
|
+
await host.missions.retire(missionId, outcome);
|
|
26
|
+
return {};
|
|
27
|
+
},
|
|
28
|
+
share: async ({ directory, missionId, title }) => {
|
|
29
|
+
const host = await openHost({
|
|
30
|
+
profileDir: directory,
|
|
31
|
+
theories: [defaultTheory],
|
|
32
|
+
model: async () => {
|
|
33
|
+
throw new Error("Sharing a Mission makes no model requests");
|
|
34
|
+
},
|
|
35
|
+
// The Share form in the Learn panel is the confirmation.
|
|
36
|
+
confirm: async () => true,
|
|
37
|
+
hub: {
|
|
38
|
+
url: process.env.NCEPT_HUB_URL,
|
|
39
|
+
credentialsFile: process.env.NCEPT_CREDENTIALS_FILE,
|
|
40
|
+
openUrl: async () => {
|
|
41
|
+
throw new Error("Sign in to the Hub first: send /ncept login in a Learn thread, then share again");
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const { url } = await host.hub.share({ mission: missionId, title });
|
|
46
|
+
return { url };
|
|
47
|
+
},
|
|
48
|
+
recognize: async ({ directory }) => {
|
|
49
|
+
try {
|
|
50
|
+
await readFile(join(directory, "profile.json"));
|
|
51
|
+
return true;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return false;
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
reading: async ({ directory }) => {
|
|
58
|
+
const reading = await (await openLearningReader(directory, [defaultTheory])).reading();
|
|
59
|
+
return {
|
|
60
|
+
missions: reading.missions.map(({ mission, run, next }) => ({
|
|
61
|
+
id: mission.id,
|
|
62
|
+
statement: mission.statement,
|
|
63
|
+
active: Boolean(run.active),
|
|
64
|
+
retired: Boolean(run.retired),
|
|
65
|
+
nodes: mission.targets.length,
|
|
66
|
+
next: next[0]?.name ?? null,
|
|
67
|
+
})),
|
|
68
|
+
dueReview: reading.dueReview,
|
|
69
|
+
sessions: reading.sessions,
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
map: async ({ directory, missionId }) => {
|
|
73
|
+
const reading = await (await openLearningReader(directory, [defaultTheory])).reading();
|
|
74
|
+
const missions = reading.missions.map(({ mission }) => {
|
|
75
|
+
const cone = new Set<string>(mission.targets.map(({ node }) => node));
|
|
76
|
+
for (const id of cone) for (const edge of reading.edges) if (edge.to === id) cone.add(edge.from);
|
|
77
|
+
return { id: mission.id, statement: mission.statement, cone: [...cone] };
|
|
78
|
+
});
|
|
79
|
+
// A Mission's own drawing is its cone as a layered tree, prerequisites first; the whole graph settles freely.
|
|
80
|
+
const cone = missionId === undefined ? undefined : missions.find((item) => item.id === missionId)?.cone;
|
|
81
|
+
if (missionId !== undefined && !cone) throw new Error("Unknown Mission");
|
|
82
|
+
const nodes = cone ? reading.nodes.filter((node) => cone.includes(node.id)) : reading.nodes;
|
|
83
|
+
const edges = cone
|
|
84
|
+
? reading.edges.filter((edge) => cone.includes(edge.from) && cone.includes(edge.to))
|
|
85
|
+
: reading.edges;
|
|
86
|
+
const positions = new Map(
|
|
87
|
+
settleGraph({ nodes, edges, ...(cone ? { arrangement: "layered" as const } : {}) }).map((item) => [
|
|
88
|
+
item.id,
|
|
89
|
+
item,
|
|
90
|
+
]),
|
|
91
|
+
);
|
|
92
|
+
return {
|
|
93
|
+
nodes: nodes.map((node) => ({
|
|
94
|
+
...node,
|
|
95
|
+
x: positions.get(node.id)?.x ?? 0,
|
|
96
|
+
y: positions.get(node.id)?.y ?? 0,
|
|
97
|
+
})),
|
|
98
|
+
edges,
|
|
99
|
+
missions,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
mission: async ({ directory, missionId }) => {
|
|
103
|
+
const reading = await (await openLearningReader(directory, [defaultTheory])).reading();
|
|
104
|
+
const item = reading.missions.find(({ mission }) => mission.id === missionId);
|
|
105
|
+
if (!item) throw new Error("Unknown Mission");
|
|
106
|
+
const label = (id: string) => reading.nodes.find((node) => node.id === id)?.label ?? id;
|
|
107
|
+
const active = item.run.past.find((entry) => entry.kind === "session" && entry.session === item.run.active);
|
|
108
|
+
const frozen = active?.kind === "session" ? active.stop : undefined;
|
|
109
|
+
return {
|
|
110
|
+
id: item.mission.id,
|
|
111
|
+
statement: item.mission.statement,
|
|
112
|
+
active: item.run.active,
|
|
113
|
+
retired: Boolean(item.run.retired),
|
|
114
|
+
canReviseLesson: reading.sessions.find((session) => session.id === item.run.active)?.entry?.kind === "lesson",
|
|
115
|
+
next: item.next[0]?.name ?? null,
|
|
116
|
+
asides: [...item.run.asides],
|
|
117
|
+
sections: (item.path?.sections ?? []).map((section) => ({
|
|
118
|
+
number: section.number,
|
|
119
|
+
label: label(section.milestone),
|
|
120
|
+
minutesLeft: section.minutesLeft,
|
|
121
|
+
lessons: section.lessons.map((stop) => ({
|
|
122
|
+
skip: { kind: stop.kind, number: stop.number, ...(stop.checkpoint ? { checkpoint: stop.checkpoint } : {}) },
|
|
123
|
+
kind: stop.kind,
|
|
124
|
+
name: stop.name ?? stop.nodes.map(label).join(" · "),
|
|
125
|
+
nodes: [...stop.nodes],
|
|
126
|
+
number: stop.number,
|
|
127
|
+
numbered: stop.numbered,
|
|
128
|
+
cleared: stop.cleared,
|
|
129
|
+
probablyKnown:
|
|
130
|
+
stop.cleared &&
|
|
131
|
+
stop.nodes.some((id) => reading.nodes.find((node) => node.id === id)?.status === "tentative"),
|
|
132
|
+
minutes: stop.minutes,
|
|
133
|
+
checkpoint: Boolean(stop.checkpoint),
|
|
134
|
+
active: Boolean(
|
|
135
|
+
frozen &&
|
|
136
|
+
(frozen.kind === "checkpoint"
|
|
137
|
+
? stop.checkpoint && stop.checkpoint.milestone === frozen.milestone
|
|
138
|
+
: stop.kind === frozen.kind &&
|
|
139
|
+
frozen.nodes &&
|
|
140
|
+
stop.nodes.length === frozen.nodes.length &&
|
|
141
|
+
stop.nodes.every((id) => frozen.nodes?.includes(id))),
|
|
142
|
+
),
|
|
143
|
+
})),
|
|
144
|
+
})),
|
|
145
|
+
history: item.run.past.map((entry) => ({
|
|
146
|
+
kind: entry.kind,
|
|
147
|
+
at: entry.at,
|
|
148
|
+
...("session" in entry ? { session: entry.session } : {}),
|
|
149
|
+
...("parent" in entry ? { parent: entry.parent } : {}),
|
|
150
|
+
label:
|
|
151
|
+
entry.kind === "session"
|
|
152
|
+
? (reading.sessions.find((session) => session.id === entry.session)?.title ??
|
|
153
|
+
entry.stop?.name ??
|
|
154
|
+
entry.session)
|
|
155
|
+
: entry.kind === "ghost"
|
|
156
|
+
? "Probably known · " + entry.nodes.map(label).join(", ")
|
|
157
|
+
: entry.kind === "theory-updated"
|
|
158
|
+
? "Theory updated"
|
|
159
|
+
: "Plan changed",
|
|
160
|
+
})),
|
|
161
|
+
};
|
|
162
|
+
},
|
|
163
|
+
init: async ({ directory, source }) => {
|
|
164
|
+
if (!isAbsolute(directory) && directory !== "~" && !directory.startsWith("~/"))
|
|
165
|
+
throw new Error("Use an absolute Profile directory or ~/ path on the selected machine.");
|
|
166
|
+
if (source && !isAbsolute(source)) throw new Error("--source must be absolute on the selected machine.");
|
|
167
|
+
const dir = await initProject(
|
|
168
|
+
directory === "~" ? homedir() : directory.startsWith("~/") ? join(homedir(), directory.slice(2)) : directory,
|
|
169
|
+
);
|
|
170
|
+
const settings = {
|
|
171
|
+
packages: source ? [resolve(source, "../pi"), source] : ["npm:@mmon-co/ncept-pi", "npm:@mmon-co/ncept-bb"],
|
|
172
|
+
sessionDir: "sessions",
|
|
173
|
+
codemode: { mode: "on" },
|
|
174
|
+
};
|
|
175
|
+
await writeFile(join(dir, ".pi/settings.json"), JSON.stringify(settings, null, 2) + "\n");
|
|
176
|
+
new ProjectTrustStore(getAgentDir()).set(await realpath(dir), true);
|
|
177
|
+
return { directory: dir };
|
|
178
|
+
},
|
|
179
|
+
entry: async ({ directory, intent }) => {
|
|
180
|
+
const profile = await (await openProfile(directory, [defaultTheory], { readOnly: true })).read();
|
|
181
|
+
if (!("missionId" in intent)) return { title: intent.kind === "review" ? "Review" : "Charting · New Mission" };
|
|
182
|
+
const revision = profile.log.findLast((item) => isMissionRevision(item) && item.mission.id === intent.missionId);
|
|
183
|
+
if (!revision || !isMissionRevision(revision)) throw new Error("Unknown Mission");
|
|
184
|
+
const active = profile.log.findLast((item) => isRunActiveChange(item) && item.mission === intent.missionId);
|
|
185
|
+
const current = active && isRunActiveChange(active) ? active.active : undefined;
|
|
186
|
+
// Refuse what the log already shows before spawning; the writing tutor
|
|
187
|
+
// resolves the entry again at session_start to cover races and drift.
|
|
188
|
+
if (intent.kind === "revise-lesson" && !current) throw new Error("There is no active Lesson to revise");
|
|
189
|
+
return {
|
|
190
|
+
title: "Learn · " + revision.mission.statement,
|
|
191
|
+
...(intent.kind === "continue" && current ? { resume: current } : {}),
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
session: async ({ directory, session }) => describe(directory, session),
|
|
195
|
+
handoff: async ({ directory, requestFile }) => {
|
|
196
|
+
const request = handoffSchema.parse(JSON.parse(await readFile(requestFile, "utf8")));
|
|
197
|
+
const description = await describe(directory, request.session);
|
|
198
|
+
if (description.mode !== request.mode) throw new Error("Handoff mode does not match this Session");
|
|
199
|
+
return { ...request, title: description.title };
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
async function describe(directory: string, id: string) {
|
|
205
|
+
const reading = await (await openLearningReader(directory, [defaultTheory])).reading();
|
|
206
|
+
const session = reading.sessions.find((item) => item.id === id);
|
|
207
|
+
if (!session) throw new Error("Unknown Session in this Profile");
|
|
208
|
+
return { session: id, mode: session.mode, title: session.title };
|
|
209
|
+
}
|
package/src/map.css
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/* The Learn map: the Application's mark shapes and Edge taper (docs/design/Graphs.md) in bb's theme tokens. */
|
|
2
|
+
.ncept-map .react-flow {
|
|
3
|
+
--xy-background-color: transparent;
|
|
4
|
+
--xy-edge-stroke: var(--foreground);
|
|
5
|
+
}
|
|
6
|
+
.ncept-map .react-flow__node {
|
|
7
|
+
cursor: pointer;
|
|
8
|
+
}
|
|
9
|
+
.ncept-map.still .react-flow__node,
|
|
10
|
+
.ncept-map.still .react-flow__pane {
|
|
11
|
+
cursor: default;
|
|
12
|
+
}
|
|
13
|
+
.ncept-map .react-flow__handle {
|
|
14
|
+
opacity: 0;
|
|
15
|
+
left: 50%;
|
|
16
|
+
top: 50%;
|
|
17
|
+
pointer-events: none;
|
|
18
|
+
}
|
|
19
|
+
.ncept-mark {
|
|
20
|
+
--m: 28px;
|
|
21
|
+
position: relative;
|
|
22
|
+
width: var(--m);
|
|
23
|
+
height: var(--m);
|
|
24
|
+
color: var(--foreground);
|
|
25
|
+
}
|
|
26
|
+
.ncept-mark.out {
|
|
27
|
+
opacity: 0.25;
|
|
28
|
+
}
|
|
29
|
+
.ncept-tok {
|
|
30
|
+
position: absolute;
|
|
31
|
+
inset: 0;
|
|
32
|
+
border-radius: 50%;
|
|
33
|
+
display: grid;
|
|
34
|
+
place-items: center;
|
|
35
|
+
border: 2px dashed var(--muted-foreground);
|
|
36
|
+
background: var(--background);
|
|
37
|
+
font:
|
|
38
|
+
700 12px Arial,
|
|
39
|
+
sans-serif;
|
|
40
|
+
}
|
|
41
|
+
.ncept-tok::after {
|
|
42
|
+
content: "";
|
|
43
|
+
width: 5px;
|
|
44
|
+
height: 5px;
|
|
45
|
+
border-radius: 50%;
|
|
46
|
+
background: var(--muted-foreground);
|
|
47
|
+
}
|
|
48
|
+
[data-state="demonstrated"] > .ncept-tok {
|
|
49
|
+
border: 0;
|
|
50
|
+
background: var(--foreground);
|
|
51
|
+
color: var(--background);
|
|
52
|
+
}
|
|
53
|
+
[data-state="demonstrated"] > .ncept-tok::after {
|
|
54
|
+
content: "✓";
|
|
55
|
+
width: auto;
|
|
56
|
+
height: auto;
|
|
57
|
+
background: none;
|
|
58
|
+
}
|
|
59
|
+
[data-state="tentative"] > .ncept-tok {
|
|
60
|
+
border: 2px solid var(--foreground);
|
|
61
|
+
}
|
|
62
|
+
[data-state="tentative"] > .ncept-tok::after {
|
|
63
|
+
background: var(--foreground);
|
|
64
|
+
}
|
|
65
|
+
[data-state="frontier"] > .ncept-tok {
|
|
66
|
+
border: 0;
|
|
67
|
+
box-shadow:
|
|
68
|
+
inset 0 0 0 2px var(--foreground),
|
|
69
|
+
0 0 0 2px var(--background),
|
|
70
|
+
0 0 0 4px var(--foreground);
|
|
71
|
+
}
|
|
72
|
+
[data-state="frontier"] > .ncept-tok::after {
|
|
73
|
+
width: 7px;
|
|
74
|
+
height: 7px;
|
|
75
|
+
background: var(--foreground);
|
|
76
|
+
}
|
|
77
|
+
.ncept-mark.sel > .ncept-tok {
|
|
78
|
+
outline: 2px solid var(--ring);
|
|
79
|
+
outline-offset: 6px;
|
|
80
|
+
}
|
|
81
|
+
.ncept-lab {
|
|
82
|
+
position: absolute;
|
|
83
|
+
top: calc(var(--m) + 6px);
|
|
84
|
+
left: calc(var(--m) / 2 - 70px);
|
|
85
|
+
width: 140px;
|
|
86
|
+
display: flex;
|
|
87
|
+
flex-direction: column;
|
|
88
|
+
align-items: center;
|
|
89
|
+
text-align: center;
|
|
90
|
+
font-size: 12px;
|
|
91
|
+
line-height: 1.25;
|
|
92
|
+
pointer-events: none;
|
|
93
|
+
}
|
|
94
|
+
.ncept-lab span,
|
|
95
|
+
.ncept-lab small {
|
|
96
|
+
padding: 0 3px;
|
|
97
|
+
border-radius: 3px;
|
|
98
|
+
background: color-mix(in oklch, var(--background) 80%, transparent);
|
|
99
|
+
}
|
|
100
|
+
.ncept-lab small {
|
|
101
|
+
color: var(--muted-foreground);
|
|
102
|
+
font-size: 10.5px;
|
|
103
|
+
}
|
|
104
|
+
.ncept-map.still .ncept-lab small {
|
|
105
|
+
display: none;
|
|
106
|
+
}
|
|
107
|
+
.ncept-edge .fill {
|
|
108
|
+
fill: var(--foreground);
|
|
109
|
+
opacity: 0.4;
|
|
110
|
+
}
|
|
111
|
+
.ncept-edge.gate .fill {
|
|
112
|
+
opacity: 1;
|
|
113
|
+
}
|
|
114
|
+
.ncept-edge .dash {
|
|
115
|
+
fill: none;
|
|
116
|
+
stroke: var(--foreground);
|
|
117
|
+
stroke-width: 1.5;
|
|
118
|
+
stroke-dasharray: 4 5;
|
|
119
|
+
opacity: 0.45;
|
|
120
|
+
}
|
|
121
|
+
.ncept-edge.out {
|
|
122
|
+
opacity: 0.25;
|
|
123
|
+
}
|
|
124
|
+
.ncept-edge.sel .fill {
|
|
125
|
+
fill: var(--ring);
|
|
126
|
+
opacity: 1;
|
|
127
|
+
}
|
|
128
|
+
.ncept-edge.sel .dash {
|
|
129
|
+
stroke: var(--ring);
|
|
130
|
+
opacity: 1;
|
|
131
|
+
}
|
package/src/map.tsx
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
import { memo, useCallback, useEffect, useMemo, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import {
|
|
3
|
+
Handle,
|
|
4
|
+
Panel,
|
|
5
|
+
Position,
|
|
6
|
+
ReactFlow,
|
|
7
|
+
useReactFlow,
|
|
8
|
+
useStore,
|
|
9
|
+
type Edge,
|
|
10
|
+
type EdgeProps,
|
|
11
|
+
type Node,
|
|
12
|
+
type NodeProps,
|
|
13
|
+
} from "@xyflow/react";
|
|
14
|
+
import "@xyflow/react/dist/style.css";
|
|
15
|
+
import "./map.css";
|
|
16
|
+
import { edgeCurve, taperPath } from "./graph";
|
|
17
|
+
import {
|
|
18
|
+
experimental_NewThreadComposer as NewThreadComposer,
|
|
19
|
+
experimental_useRpcQuery,
|
|
20
|
+
useBbNavigate,
|
|
21
|
+
useComposer,
|
|
22
|
+
useComposers,
|
|
23
|
+
useSdk,
|
|
24
|
+
type NewThreadRequest,
|
|
25
|
+
} from "@get-bb/plugin-sdk/app";
|
|
26
|
+
import { rpcContract } from "./contract";
|
|
27
|
+
import { stateName } from "./reading";
|
|
28
|
+
import { Button } from "./ui/button";
|
|
29
|
+
|
|
30
|
+
const RADIUS = 14;
|
|
31
|
+
type Point = { x: number; y: number };
|
|
32
|
+
type Mark = Node<{ label: string; state: string; out: boolean; sel: boolean }, "mark">;
|
|
33
|
+
type Trail = Edge<{ from: Point; to: Point; kind: "gate" | "known" | "dash"; out: boolean }, "trail">;
|
|
34
|
+
type Selection = { kind: "node"; id: string } | { kind: "edge"; from: string; to: string };
|
|
35
|
+
|
|
36
|
+
function MarkNode({ data }: NodeProps<Mark>) {
|
|
37
|
+
return (
|
|
38
|
+
<div className={"ncept-mark" + (data.out ? " out" : "") + (data.sel ? " sel" : "")} data-state={data.state}>
|
|
39
|
+
<Handle type="target" position={Position.Top} isConnectable={false} />
|
|
40
|
+
<Handle type="source" position={Position.Top} isConnectable={false} />
|
|
41
|
+
<i className="ncept-tok" />
|
|
42
|
+
<div className="ncept-lab">
|
|
43
|
+
<span>{data.label}</span>
|
|
44
|
+
<small>{data.state}</small>
|
|
45
|
+
</div>
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function TrailEdge({ data, selected }: EdgeProps<Trail>) {
|
|
51
|
+
const curve = data && edgeCurve(data.from, data.to, RADIUS);
|
|
52
|
+
if (!data || !curve) return null;
|
|
53
|
+
const start = curve.at(curve.start);
|
|
54
|
+
const end = curve.at(curve.end);
|
|
55
|
+
const line = `M${start.x},${start.y} Q${curve.control.x},${curve.control.y} ${end.x},${end.y}`;
|
|
56
|
+
return (
|
|
57
|
+
<g className={"ncept-edge " + data.kind + (data.out ? " out" : "") + (selected ? " sel" : "")}>
|
|
58
|
+
{data.kind === "dash" ? (
|
|
59
|
+
<path className="dash" d={line} />
|
|
60
|
+
) : (
|
|
61
|
+
<path className="fill" d={taperPath(data.from, data.to, RADIUS)} />
|
|
62
|
+
)}
|
|
63
|
+
<path className="react-flow__edge-interaction" d={line} fill="none" strokeOpacity={0} strokeWidth={18} />
|
|
64
|
+
</g>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const nodeTypes = { mark: MarkNode };
|
|
68
|
+
const edgeTypes = { trail: TrailEdge };
|
|
69
|
+
|
|
70
|
+
/** Refits when the map's box changes: the docked composer and bb's panels settle after the first fit. */
|
|
71
|
+
function Refit({ padding }: { padding: number }) {
|
|
72
|
+
const flow = useReactFlow();
|
|
73
|
+
const width = useStore((state) => state.width);
|
|
74
|
+
const height = useStore((state) => state.height);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
void flow.fitView({ padding, maxZoom: 1.25 });
|
|
77
|
+
}, [flow, padding, width, height]);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function Zoom() {
|
|
82
|
+
const flow = useReactFlow();
|
|
83
|
+
const button = "px-2.5 py-1 hover:bg-state-hover";
|
|
84
|
+
return (
|
|
85
|
+
<div className="inline-flex rounded-md border border-border bg-background text-xs divide-x divide-border">
|
|
86
|
+
<button aria-label="Zoom out" className={button} onClick={() => void flow.zoomOut()}>
|
|
87
|
+
−
|
|
88
|
+
</button>
|
|
89
|
+
<button aria-label="Zoom in" className={button} onClick={() => void flow.zoomIn()}>
|
|
90
|
+
+
|
|
91
|
+
</button>
|
|
92
|
+
<button className={button} onClick={() => void flow.fitView({ padding: 0.2, maxZoom: 1.25 })}>
|
|
93
|
+
Fit
|
|
94
|
+
</button>
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** bb's new-thread composer under the map, on the Profile's project. Its own component with a stable `onSubmit`:
|
|
100
|
+
* the map re-renders whenever a composer registers. */
|
|
101
|
+
const Dock = memo(function Dock({ projectId }: { projectId: string }) {
|
|
102
|
+
const sdk = useSdk();
|
|
103
|
+
const navigate = useBbNavigate();
|
|
104
|
+
const submit = useCallback(
|
|
105
|
+
async (request: NewThreadRequest) => navigate.toThread((await sdk.threads.spawn(request)).id),
|
|
106
|
+
[sdk, navigate],
|
|
107
|
+
);
|
|
108
|
+
return (
|
|
109
|
+
<div className="mx-auto w-full max-w-3xl px-5 pb-4">
|
|
110
|
+
<NewThreadComposer
|
|
111
|
+
defaultProjectId={projectId}
|
|
112
|
+
defaultProviderId="pi"
|
|
113
|
+
layout="document"
|
|
114
|
+
placeholder="Ask about your map, or start something. @ mentions a Node"
|
|
115
|
+
onSubmit={submit}
|
|
116
|
+
/>
|
|
117
|
+
</div>
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
/** Whether bb is drawn dark; React Flow otherwise marks its root `light`, which resets bb's tokens inside it. */
|
|
122
|
+
function useDark() {
|
|
123
|
+
return useSyncExternalStore(
|
|
124
|
+
(changed) => {
|
|
125
|
+
const observer = new MutationObserver(changed);
|
|
126
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
|
|
127
|
+
return () => observer.disconnect();
|
|
128
|
+
},
|
|
129
|
+
() => document.documentElement.classList.contains("dark"),
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The Profile's graph. A `session` (the thread tab) opens on that Session's Nodes and writes pills to the thread's
|
|
134
|
+
* composer; `dock` (the Learn page) writes them to a new-thread composer under the map; `still` (a Mission page's
|
|
135
|
+
* head) draws one Mission's cone, fitted, without pan, zoom or selection. */
|
|
136
|
+
export function LearnMap({
|
|
137
|
+
projectId,
|
|
138
|
+
mission,
|
|
139
|
+
session,
|
|
140
|
+
dock = false,
|
|
141
|
+
still = false,
|
|
142
|
+
}: {
|
|
143
|
+
projectId: string;
|
|
144
|
+
mission?: string;
|
|
145
|
+
session?: { nodes: readonly string[]; mission?: string };
|
|
146
|
+
dock?: boolean;
|
|
147
|
+
still?: boolean;
|
|
148
|
+
}) {
|
|
149
|
+
const map = experimental_useRpcQuery({
|
|
150
|
+
contract: rpcContract,
|
|
151
|
+
method: "map",
|
|
152
|
+
input: { projectId, ...(still && mission ? { missionId: mission } : {}) },
|
|
153
|
+
});
|
|
154
|
+
const navigate = useBbNavigate();
|
|
155
|
+
const dark = useDark();
|
|
156
|
+
const own = useComposer();
|
|
157
|
+
const docked = useComposers();
|
|
158
|
+
const [scope, setScope] = useState(session ? "session" : (mission ?? "all"));
|
|
159
|
+
const [selected, setSelected] = useState<Selection>();
|
|
160
|
+
const [added, setAdded] = useState<string>();
|
|
161
|
+
const data = map.data;
|
|
162
|
+
const drawn = useMemo(() => {
|
|
163
|
+
const none: { nodes: Mark[]; edges: Trail[] } = { nodes: [], edges: [] };
|
|
164
|
+
if (!data) return none;
|
|
165
|
+
const coneOf = (id: string | undefined) => data.missions.find((item) => item.id === id)?.cone;
|
|
166
|
+
const cone = scope === "all" ? undefined : scope === "session" ? coneOf(session?.mission) : coneOf(scope);
|
|
167
|
+
const shown = new Set(cone ? [...cone, ...(session?.nodes ?? [])] : data.nodes.map((node) => node.id));
|
|
168
|
+
const lit = scope === "session" && session ? new Set(session.nodes) : shown;
|
|
169
|
+
const byId = new Map(data.nodes.map((node) => [node.id, node]));
|
|
170
|
+
return {
|
|
171
|
+
nodes: data.nodes
|
|
172
|
+
.filter((node) => shown.has(node.id))
|
|
173
|
+
.map<Mark>((node) => ({
|
|
174
|
+
id: node.id,
|
|
175
|
+
type: "mark",
|
|
176
|
+
position: { x: node.x, y: node.y },
|
|
177
|
+
data: {
|
|
178
|
+
label: node.label,
|
|
179
|
+
state: stateName(node),
|
|
180
|
+
out: !lit.has(node.id),
|
|
181
|
+
sel: selected?.kind === "node" && selected.id === node.id,
|
|
182
|
+
},
|
|
183
|
+
})),
|
|
184
|
+
edges: data.edges.flatMap<Trail>((edge) => {
|
|
185
|
+
const from = byId.get(edge.from);
|
|
186
|
+
const to = byId.get(edge.to);
|
|
187
|
+
if (!from || !to || !shown.has(from.id) || !shown.has(to.id)) return [];
|
|
188
|
+
return [
|
|
189
|
+
{
|
|
190
|
+
id: edge.from + ">" + edge.to,
|
|
191
|
+
type: "trail",
|
|
192
|
+
source: edge.from,
|
|
193
|
+
target: edge.to,
|
|
194
|
+
selected: selected?.kind === "edge" && selected.from === edge.from && selected.to === edge.to,
|
|
195
|
+
data: {
|
|
196
|
+
from: { x: from.x, y: from.y },
|
|
197
|
+
to: { x: to.x, y: to.y },
|
|
198
|
+
kind: to.frontier ? "gate" : to.status === "open" ? "dash" : "known",
|
|
199
|
+
out: !lit.has(from.id) || !lit.has(to.id),
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
];
|
|
203
|
+
}),
|
|
204
|
+
};
|
|
205
|
+
}, [data, scope, selected, session]);
|
|
206
|
+
if (!data)
|
|
207
|
+
return (
|
|
208
|
+
<div role={map.error ? "alert" : undefined} className="p-6 text-sm text-muted-foreground">
|
|
209
|
+
{map.error?.message ?? "Loading map…"}
|
|
210
|
+
</div>
|
|
211
|
+
);
|
|
212
|
+
if (!data.nodes.length) return <div className="p-6 text-sm text-muted-foreground">No Nodes yet.</div>;
|
|
213
|
+
|
|
214
|
+
const label = (id: string) => data.nodes.find((node) => node.id === id)?.label ?? id;
|
|
215
|
+
function add(kind: string, ids: readonly string[], name: string) {
|
|
216
|
+
// The Learn page's composer is the one docked under the map; beside a thread it is that thread's.
|
|
217
|
+
const composer = dock ? (docked.at(-1) ?? own) : own;
|
|
218
|
+
composer.insert([{ provider: "map", id: [projectId, kind, ...ids].join("~"), label: name }, " "], { at: "end" });
|
|
219
|
+
setAdded([kind, ...ids].join("~"));
|
|
220
|
+
}
|
|
221
|
+
const node = selected?.kind === "node" ? data.nodes.find((item) => item.id === selected.id) : undefined;
|
|
222
|
+
const scoped = data.missions.find((item) => item.id === scope);
|
|
223
|
+
const names = (ids: readonly string[]) =>
|
|
224
|
+
ids.length ? (
|
|
225
|
+
ids.map((id) => <span key={id}>{label(id)}</span>)
|
|
226
|
+
) : (
|
|
227
|
+
<span className="text-muted-foreground">nothing</span>
|
|
228
|
+
);
|
|
229
|
+
const addButton = (kind: string, ids: readonly string[], name: string) => (
|
|
230
|
+
<Button className="self-start" onClick={() => add(kind, ids, name)}>
|
|
231
|
+
{added === [kind, ...ids].join("~") ? "Added ✓" : "Add to chat"}
|
|
232
|
+
</Button>
|
|
233
|
+
);
|
|
234
|
+
return (
|
|
235
|
+
<div
|
|
236
|
+
className={
|
|
237
|
+
"ncept-map relative flex h-full w-full flex-col bg-background text-foreground" + (still ? " still" : "")
|
|
238
|
+
}
|
|
239
|
+
>
|
|
240
|
+
<div className="relative min-h-0 flex-1">
|
|
241
|
+
<ReactFlow
|
|
242
|
+
key={scope}
|
|
243
|
+
colorMode={dark ? "dark" : "light"}
|
|
244
|
+
nodes={drawn.nodes}
|
|
245
|
+
edges={drawn.edges}
|
|
246
|
+
nodeTypes={nodeTypes}
|
|
247
|
+
edgeTypes={edgeTypes}
|
|
248
|
+
nodeOrigin={[0.5, 0.5]}
|
|
249
|
+
fitView
|
|
250
|
+
fitViewOptions={{ padding: still ? 0.3 : 0.25, maxZoom: 1.25 }}
|
|
251
|
+
minZoom={0.1}
|
|
252
|
+
nodesDraggable={false}
|
|
253
|
+
nodesConnectable={false}
|
|
254
|
+
nodesFocusable={!still}
|
|
255
|
+
edgesFocusable={!still}
|
|
256
|
+
elementsSelectable={!still}
|
|
257
|
+
panOnDrag={!still}
|
|
258
|
+
zoomOnScroll={!still}
|
|
259
|
+
zoomOnPinch={!still}
|
|
260
|
+
zoomOnDoubleClick={!still}
|
|
261
|
+
preventScrolling={!still}
|
|
262
|
+
proOptions={{ hideAttribution: true }}
|
|
263
|
+
onNodeClick={(_, item) => !still && setSelected({ kind: "node", id: item.id })}
|
|
264
|
+
onEdgeClick={(_, item) => !still && setSelected({ kind: "edge", from: item.source, to: item.target })}
|
|
265
|
+
onPaneClick={() => setSelected(undefined)}
|
|
266
|
+
>
|
|
267
|
+
<Refit padding={still ? 0.3 : 0.25} />
|
|
268
|
+
{!still && (
|
|
269
|
+
<Panel position="top-left" className="flex flex-col items-start gap-2">
|
|
270
|
+
<div className="flex items-center gap-2">
|
|
271
|
+
<select
|
|
272
|
+
aria-label="Scope"
|
|
273
|
+
className="max-w-56 rounded-md border border-border bg-background px-2 py-1 text-xs"
|
|
274
|
+
value={scope}
|
|
275
|
+
onChange={(event) => {
|
|
276
|
+
setScope(event.target.value);
|
|
277
|
+
setSelected(undefined);
|
|
278
|
+
}}
|
|
279
|
+
>
|
|
280
|
+
{session && <option value="session">This Session</option>}
|
|
281
|
+
<option value="all">All Missions</option>
|
|
282
|
+
{data.missions.map((item) => (
|
|
283
|
+
<option key={item.id} value={item.id}>
|
|
284
|
+
{item.statement}
|
|
285
|
+
</option>
|
|
286
|
+
))}
|
|
287
|
+
</select>
|
|
288
|
+
{scoped && (
|
|
289
|
+
<Button
|
|
290
|
+
variant="outline"
|
|
291
|
+
className="h-7"
|
|
292
|
+
onClick={() => add("mission", [scoped.id], scoped.statement)}
|
|
293
|
+
>
|
|
294
|
+
{added === "mission~" + scoped.id ? "Added ✓" : "Add Mission to chat"}
|
|
295
|
+
</Button>
|
|
296
|
+
)}
|
|
297
|
+
</div>
|
|
298
|
+
<Zoom />
|
|
299
|
+
</Panel>
|
|
300
|
+
)}
|
|
301
|
+
</ReactFlow>
|
|
302
|
+
{still && (
|
|
303
|
+
<button
|
|
304
|
+
className="absolute bottom-2 right-3 text-xs text-muted-foreground underline underline-offset-2"
|
|
305
|
+
onClick={() => navigate.toPluginPanel("learn", { subPath: "map/" + encodeURIComponent(mission ?? "") })}
|
|
306
|
+
>
|
|
307
|
+
Open in Map →
|
|
308
|
+
</button>
|
|
309
|
+
)}
|
|
310
|
+
{node && (
|
|
311
|
+
<aside className="absolute right-3 top-3 flex w-60 flex-col gap-2 rounded-lg border border-border bg-background p-3 text-xs">
|
|
312
|
+
<div className="text-sm font-semibold">{node.label}</div>
|
|
313
|
+
<div className="line-clamp-2 text-muted-foreground">
|
|
314
|
+
{[
|
|
315
|
+
stateName(node),
|
|
316
|
+
...data.missions.filter((item) => item.cone.includes(node.id)).map((item) => item.statement),
|
|
317
|
+
].join(" · ")}
|
|
318
|
+
</div>
|
|
319
|
+
<div className="flex flex-col">
|
|
320
|
+
<b className="font-medium text-muted-foreground">Needs</b>
|
|
321
|
+
{names(data.edges.filter((edge) => edge.to === node.id).map((edge) => edge.from))}
|
|
322
|
+
</div>
|
|
323
|
+
<div className="flex flex-col">
|
|
324
|
+
<b className="font-medium text-muted-foreground">Unlocks</b>
|
|
325
|
+
{names(data.edges.filter((edge) => edge.from === node.id).map((edge) => edge.to))}
|
|
326
|
+
</div>
|
|
327
|
+
{addButton("node", [node.id], node.label)}
|
|
328
|
+
</aside>
|
|
329
|
+
)}
|
|
330
|
+
{selected?.kind === "edge" && (
|
|
331
|
+
<aside className="absolute right-3 top-3 flex w-60 flex-col gap-2 rounded-lg border border-border bg-background p-3 text-xs">
|
|
332
|
+
<div className="text-sm font-semibold">
|
|
333
|
+
{label(selected.from)} → {label(selected.to)}
|
|
334
|
+
</div>
|
|
335
|
+
<div className="text-muted-foreground">
|
|
336
|
+
{label(selected.from)} is needed for {label(selected.to)}
|
|
337
|
+
</div>
|
|
338
|
+
{addButton("edge", [selected.from, selected.to], label(selected.from) + " → " + label(selected.to))}
|
|
339
|
+
</aside>
|
|
340
|
+
)}
|
|
341
|
+
</div>
|
|
342
|
+
{dock && <Dock projectId={projectId} />}
|
|
343
|
+
</div>
|
|
344
|
+
);
|
|
345
|
+
}
|