@mmon-co/ncept-bb 0.2.0 → 0.2.1

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,11 @@
1
1
  {
2
2
  "sdkMajor": 0,
3
- "sdkVersion": "0.6.157",
3
+ "sdkVersion": "0.6.132",
4
4
  "artifactFormatVersion": 1,
5
5
  "pluginId": "ncept-bb",
6
- "pluginVersion": "0.2.0",
6
+ "pluginVersion": "0.2.1",
7
7
  "builtWith": {
8
- "bbVersion": "0.50.1",
9
- "pluginSdkVersion": "0.6.157"
8
+ "bbVersion": "0.49.0",
9
+ "pluginSdkVersion": "0.6.132"
10
10
  }
11
11
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmon-co/ncept-bb",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "One bb thread per Common Concept tutoring Session",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -24,10 +24,10 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@dagrejs/dagre": "3.1.3",
27
- "@mmon-co/ncept-core": "^0.3.0",
28
- "@mmon-co/ncept-default-theory": "^2.0.0",
29
- "@mmon-co/ncept-local": "^0.3.0",
30
- "@mmon-co/ncept-pi": "^0.3.0",
27
+ "@mmon-co/ncept-core": "^0.4.0",
28
+ "@mmon-co/ncept-default-theory": "^3.0.0",
29
+ "@mmon-co/ncept-local": "^0.3.1",
30
+ "@mmon-co/ncept-pi": "^0.3.1",
31
31
  "@xyflow/react": "12.10.1",
32
32
  "d3-force": "3.0.0",
33
33
  "zod": "^4.6.5"
package/src/contract.ts CHANGED
@@ -19,12 +19,16 @@ export const handoffSchema = z.object({
19
19
  });
20
20
  const retirement = z.object({ missionId: z.string(), outcome: z.enum(["achieved", "abandoned"]) });
21
21
  const sessionDescription = z.object({ session: z.string(), mode: z.string(), title: z.string() });
22
+ const share = z.object({ missionId: z.string(), title: z.string().min(1) });
23
+ const shareResult = z.union([z.object({ url: z.string() }), z.object({ signInRequired: z.literal(true) })]);
24
+ const signIn = z.object({ attempt: z.string() });
25
+ const signInStarted = signIn.extend({ url: z.url() });
22
26
  export const rpcContract = defineRpcContract({
23
27
  retire: { input: retirement.extend({ projectId: z.string() }), output: z.object({}), tags: () => ["learning"] },
24
- share: {
25
- input: z.object({ projectId: z.string(), missionId: z.string(), title: z.string().min(1) }),
26
- output: z.object({ url: z.string() }),
27
- },
28
+ share: { input: share.extend({ projectId: z.string() }), output: shareResult },
29
+ shareSignedIn: { input: z.object({ projectId: z.string() }), output: z.boolean() },
30
+ shareSignIn: { input: z.object({ projectId: z.string() }), output: signInStarted },
31
+ pollShareSignIn: { input: signIn.extend({ projectId: z.string() }), output: z.boolean() },
28
32
  profiles: { input: z.object({}), output: z.array(profileChoice) },
29
33
  mission: {
30
34
  input: z.object({ projectId: z.string(), missionId: z.string() }),
@@ -54,10 +58,10 @@ export const rpcContract = defineRpcContract({
54
58
  });
55
59
  export const hostContract = defineRpcContract({
56
60
  retire: { input: retirement.extend({ directory: z.string() }), output: z.object({}) },
57
- share: {
58
- input: z.object({ directory: z.string(), missionId: z.string(), title: z.string().min(1) }),
59
- output: z.object({ url: z.string() }),
60
- },
61
+ share: { input: share.extend({ directory: z.string() }), output: shareResult },
62
+ shareSignedIn: { input: z.object({ directory: z.string() }), output: z.boolean() },
63
+ shareSignIn: { input: z.object({ directory: z.string() }), output: signInStarted },
64
+ pollShareSignIn: { input: signIn.extend({ directory: z.string() }), output: z.boolean() },
61
65
  recognize: { input: z.object({ directory: z.string() }), output: z.boolean() },
62
66
  mission: { input: z.object({ directory: z.string(), missionId: z.string() }), output: missionReading },
63
67
  reading: { input: z.object({ directory: z.string() }), output: learningReading },
package/src/host.ts CHANGED
@@ -1,15 +1,27 @@
1
+ import { randomUUID } from "node:crypto";
1
2
  import { readFile, realpath, writeFile } from "node:fs/promises";
2
3
  import { homedir } from "node:os";
3
4
  import { isAbsolute, join, resolve } from "node:path";
4
5
  import { experimental_defineHostEntry } from "@get-bb/plugin-sdk/host";
5
6
  import { getAgentDir, ProjectTrustStore } from "@earendil-works/pi-coding-agent";
6
7
  import { initProject } from "@mmon-co/ncept-pi";
7
- import { openHost, openLearningReader, openProfile } from "@mmon-co/ncept-local";
8
+ import {
9
+ beginHubBrowserSignIn,
10
+ hasHubCredentials,
11
+ HubSignInRequired,
12
+ openHost,
13
+ openLearningReader,
14
+ openProfile,
15
+ type HubBrowserSignIn,
16
+ } from "@mmon-co/ncept-local";
8
17
  import { isMissionRevision, isRunActiveChange } from "@mmon-co/ncept-core";
9
18
  import { defaultTheory } from "@mmon-co/ncept-default-theory";
10
19
  import { handoffSchema, hostContract } from "./contract";
11
20
  import { settleGraph } from "./graph-positions";
12
21
 
22
+ const hub = { url: process.env.NCEPT_HUB_URL, credentialsFile: process.env.NCEPT_CREDENTIALS_FILE };
23
+ const signIns = new Map<string, { directory: string; signIn: HubBrowserSignIn; polling: boolean }>();
24
+
13
25
  export default experimental_defineHostEntry({
14
26
  contract: hostContract,
15
27
  handlers: {
@@ -34,16 +46,39 @@ export default experimental_defineHostEntry({
34
46
  },
35
47
  // The Share form in the Learn panel is the confirmation.
36
48
  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
- },
49
+ hub,
44
50
  });
45
- const { url } = await host.hub.share({ mission: missionId, title });
46
- return { url };
51
+ try {
52
+ const { url } = await host.hub.share({ mission: missionId, title, signIn: false });
53
+ return { url };
54
+ } catch (error) {
55
+ if (error instanceof HubSignInRequired) return { signInRequired: true as const };
56
+ throw error;
57
+ }
58
+ },
59
+ shareSignedIn: () => hasHubCredentials(hub),
60
+ shareSignIn: async ({ directory }) => {
61
+ const signIn = await beginHubBrowserSignIn(hub);
62
+ const attempt = randomUUID();
63
+ signIns.set(attempt, { directory, signIn, polling: false });
64
+ setTimeout(() => signIns.delete(attempt), signIn.expiresAt - Date.now()).unref();
65
+ return { attempt, url: signIn.url };
66
+ },
67
+ pollShareSignIn: async ({ directory, attempt }) => {
68
+ const current = signIns.get(attempt);
69
+ if (!current || current.directory !== directory) throw new Error("Sign-in expired. Try again.");
70
+ if (current.polling) return false;
71
+ current.polling = true;
72
+ try {
73
+ const ready = await current.signIn.poll();
74
+ if (ready) signIns.delete(attempt);
75
+ return ready;
76
+ } catch (error) {
77
+ signIns.delete(attempt);
78
+ throw error;
79
+ } finally {
80
+ current.polling = false;
81
+ }
47
82
  },
48
83
  recognize: async ({ directory }) => {
49
84
  try {
package/src/map.tsx CHANGED
@@ -255,7 +255,9 @@ export function LearnMap({
255
255
  edgesFocusable={!still}
256
256
  elementsSelectable={!still}
257
257
  panOnDrag={!still}
258
- zoomOnScroll={!still}
258
+ // The wheel and two-finger scroll pan, as on the Session Map; pinch and Ctrl-wheel zoom.
259
+ panOnScroll={!still}
260
+ zoomOnScroll={false}
259
261
  zoomOnPinch={!still}
260
262
  zoomOnDoubleClick={!still}
261
263
  preventScrolling={!still}
@@ -6,6 +6,7 @@ import type { learningReading } from "./reading";
6
6
  import type { z } from "zod";
7
7
  import { Button } from "./ui/button";
8
8
  import { LearnMap } from "./map";
9
+ import { MissionShare } from "./mission-share";
9
10
 
10
11
  function MenuItem({
11
12
  hint,
@@ -47,8 +48,6 @@ export function MissionPage({
47
48
  });
48
49
  const [menu, setMenu] = useState<"skip" | "revise" | "share">();
49
50
  const [shareTitle, setShareTitle] = useState("");
50
- const [sharing, setSharing] = useState(false);
51
- const [shared, setShared] = useState<string>();
52
51
  const [picking, setPicking] = useState(false);
53
52
  const [finishing, setFinishing] = useState(false);
54
53
  const [retiring, setRetiring] = useState(false);
@@ -74,17 +73,6 @@ export function MissionPage({
74
73
  setError(String(error));
75
74
  }
76
75
  }
77
- async function share() {
78
- setSharing(true);
79
- setError(undefined);
80
- try {
81
- setShared((await rpc.call("share", { projectId, missionId, title: shareTitle.trim() })).url);
82
- } catch (error) {
83
- setError(String(error));
84
- } finally {
85
- setSharing(false);
86
- }
87
- }
88
76
  async function retire() {
89
77
  setRetiring(true);
90
78
  setError(undefined);
@@ -156,7 +144,6 @@ export function MissionPage({
156
144
  onClick={() => {
157
145
  setMenu(menu === "share" ? undefined : "share");
158
146
  setTelling(false);
159
- setShared(undefined);
160
147
  setShareTitle(shareTitle || data.statement.replace(/\s+/g, " ").slice(0, 80));
161
148
  }}
162
149
  >
@@ -262,49 +249,9 @@ export function MissionPage({
262
249
  </div>
263
250
  </form>
264
251
  )}
265
- {menu === "share" &&
266
- (shared ? (
267
- <div className="p-2 space-y-3">
268
- <p className="text-sm">Anyone with this link can read the map and learn from it.</p>
269
- <input
270
- readOnly
271
- aria-label="Link"
272
- value={shared}
273
- onFocus={(event) => event.currentTarget.select()}
274
- className="w-full rounded-md border border-input bg-background p-3 text-sm"
275
- />
276
- <Button type="button" onClick={() => void navigator.clipboard.writeText(shared)}>
277
- Copy link
278
- </Button>
279
- </div>
280
- ) : (
281
- <form
282
- aria-label="Share Mission"
283
- className="p-2 space-y-3"
284
- onSubmit={(event) => {
285
- event.preventDefault();
286
- if (shareTitle.trim()) void share();
287
- }}
288
- >
289
- <label className="block text-sm" htmlFor="share-title">
290
- Title
291
- </label>
292
- <input
293
- id="share-title"
294
- autoFocus
295
- value={shareTitle}
296
- disabled={sharing}
297
- onChange={(event) => setShareTitle(event.target.value)}
298
- className="w-full rounded-md border border-input bg-background p-3 text-sm"
299
- />
300
- <small className="block text-muted-foreground">
301
- Publishes this Mission's map and its Materials on the Hub at a link. It isn't listed.
302
- </small>
303
- <Button disabled={sharing || !shareTitle.trim()} type="submit">
304
- {sharing ? "Sharing…" : "Share"}
305
- </Button>
306
- </form>
307
- ))}
252
+ {menu === "share" && (
253
+ <MissionShare projectId={projectId} missionId={missionId} title={shareTitle} setTitle={setShareTitle} />
254
+ )}
308
255
  {telling && (
309
256
  <form
310
257
  className="p-2 space-y-3"
@@ -0,0 +1,166 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ import { useBbNavigate, useRpc } from "@get-bb/plugin-sdk/app";
3
+ import { rpcContract } from "./contract";
4
+ import { Button } from "./ui/button";
5
+
6
+ export function MissionShare({
7
+ projectId,
8
+ missionId,
9
+ title,
10
+ setTitle,
11
+ }: {
12
+ projectId: string;
13
+ missionId: string;
14
+ title: string;
15
+ setTitle: (title: string) => void;
16
+ }) {
17
+ const rpc = useRpc<typeof rpcContract>();
18
+ const navigate = useBbNavigate();
19
+ const [signedIn, setSignedIn] = useState<boolean>();
20
+ const [signInUrl, setSignInUrl] = useState<string>();
21
+ const [phase, setPhase] = useState<"sign-in" | "publish">();
22
+ const [shared, setShared] = useState<string>();
23
+ const [error, setError] = useState<string>();
24
+ const active = useRef<symbol | null>(null);
25
+
26
+ useEffect(() => {
27
+ let disposed = false;
28
+ void rpc.call("shareSignedIn", { projectId }).then(
29
+ (ready) => {
30
+ if (!disposed) setSignedIn(ready);
31
+ },
32
+ (error: unknown) => {
33
+ if (!disposed) setError(error instanceof Error ? error.message : String(error));
34
+ },
35
+ );
36
+ return () => {
37
+ disposed = true;
38
+ active.current = null;
39
+ };
40
+ }, [projectId]);
41
+
42
+ function cancelSignIn() {
43
+ active.current = null;
44
+ setPhase(undefined);
45
+ setSignInUrl(undefined);
46
+ }
47
+
48
+ async function share() {
49
+ const name = title.trim();
50
+ if (!name || signedIn === undefined || active.current) return;
51
+ const run = Symbol();
52
+ active.current = run;
53
+ const cancelled = () => active.current !== run;
54
+ setError(undefined);
55
+ let tab: Window | null = null;
56
+ let opened = false;
57
+ try {
58
+ if (!signedIn) {
59
+ // Reserve the tab on the click, before the Profile's RPC can outlast browser activation.
60
+ tab = window.open("", "_blank");
61
+ if (tab) tab.opener = null;
62
+ setPhase("sign-in");
63
+ const { attempt, url } = await rpc.call("shareSignIn", { projectId });
64
+ if (cancelled()) return;
65
+ setSignInUrl(url);
66
+ if (tab && !tab.closed) {
67
+ tab.location.replace(url);
68
+ opened = true;
69
+ } else navigate.openUrl(url);
70
+ while (!cancelled()) {
71
+ await new Promise<void>((resolve) => setTimeout(resolve, 2000));
72
+ if (!cancelled() && (await rpc.call("pollShareSignIn", { projectId, attempt }))) break;
73
+ }
74
+ if (cancelled()) return;
75
+ setSignedIn(true);
76
+ }
77
+ setSignInUrl(undefined);
78
+ setPhase("publish");
79
+ const result = await rpc.call("share", { projectId, missionId, title: name });
80
+ if (cancelled()) return;
81
+ if ("url" in result) setShared(result.url);
82
+ else setSignedIn(false);
83
+ } catch (error) {
84
+ if (!cancelled()) setError(error instanceof Error ? error.message : String(error));
85
+ } finally {
86
+ if (!opened) tab?.close();
87
+ if (!cancelled()) cancelSignIn();
88
+ }
89
+ }
90
+
91
+ if (shared)
92
+ return (
93
+ <div className="p-2 space-y-3">
94
+ <p className="text-sm">Anyone with this link can read the map and learn from it.</p>
95
+ <input
96
+ readOnly
97
+ aria-label="Link"
98
+ value={shared}
99
+ onFocus={(event) => event.currentTarget.select()}
100
+ className="w-full rounded-md border border-input bg-background p-3 text-sm"
101
+ />
102
+ <Button type="button" onClick={() => void navigator.clipboard.writeText(shared)}>
103
+ Copy link
104
+ </Button>
105
+ </div>
106
+ );
107
+
108
+ return (
109
+ <form
110
+ aria-label="Share Mission"
111
+ className="p-2 space-y-3"
112
+ onSubmit={(event) => {
113
+ event.preventDefault();
114
+ void share();
115
+ }}
116
+ >
117
+ <label className="block text-sm" htmlFor="share-title">
118
+ Title
119
+ </label>
120
+ <input
121
+ id="share-title"
122
+ autoFocus
123
+ value={title}
124
+ readOnly={phase !== undefined}
125
+ onChange={(event) => setTitle(event.target.value)}
126
+ className="w-full rounded-md border border-input bg-background p-3 text-sm"
127
+ />
128
+ {phase === "sign-in" ? (
129
+ <>
130
+ <p role="status" className="text-sm">
131
+ Waiting for you to sign in to Common Concept.
132
+ </p>
133
+ <div className="flex gap-2 items-center">
134
+ {signInUrl && (
135
+ <a
136
+ href={signInUrl}
137
+ target="_blank"
138
+ rel="noopener noreferrer"
139
+ className="inline-flex h-8 items-center rounded-md px-3 text-xs font-medium bg-foreground text-background hover:bg-foreground/90"
140
+ >
141
+ Open sign-in ↗
142
+ </a>
143
+ )}
144
+ <Button type="button" variant="ghost" onClick={cancelSignIn}>
145
+ Cancel
146
+ </Button>
147
+ </div>
148
+ </>
149
+ ) : (
150
+ <>
151
+ <small className="block text-muted-foreground">
152
+ Publishes this Mission’s map and Materials at an unlisted Hub link.
153
+ </small>
154
+ <Button disabled={!!phase || signedIn === undefined || !title.trim()} type="submit">
155
+ {phase === "publish" ? "Sharing…" : signedIn === false ? "Sign in and share" : "Share"}
156
+ </Button>
157
+ </>
158
+ )}
159
+ {error && (
160
+ <p role="alert" className="text-sm text-destructive">
161
+ {error}
162
+ </p>
163
+ )}
164
+ </form>
165
+ );
166
+ }
package/src/server.ts CHANGED
@@ -189,6 +189,18 @@ export default function ncept(bb: BbPluginApi) {
189
189
  const target = await profile(projectId);
190
190
  return host.call("share", { directory: target.directory, missionId, title }, { hostId: target.hostId });
191
191
  },
192
+ shareSignedIn: async ({ projectId }) => {
193
+ const target = await profile(projectId);
194
+ return host.call("shareSignedIn", { directory: target.directory }, { hostId: target.hostId });
195
+ },
196
+ shareSignIn: async ({ projectId }) => {
197
+ const target = await profile(projectId);
198
+ return host.call("shareSignIn", { directory: target.directory }, { hostId: target.hostId });
199
+ },
200
+ pollShareSignIn: async ({ projectId, attempt }) => {
201
+ const target = await profile(projectId);
202
+ return host.call("pollShareSignIn", { directory: target.directory, attempt }, { hostId: target.hostId });
203
+ },
192
204
  mission: async ({ projectId, missionId }) => {
193
205
  const target = await profile(projectId);
194
206
  return host.call("mission", { directory: target.directory, missionId }, { hostId: target.hostId });