@mmon-co/ncept-local 0.0.0-stage → 0.1.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 +77 -2
- package/dist/days.d.ts +4 -0
- package/dist/days.js +47 -0
- package/dist/directory.d.ts +32 -0
- package/dist/directory.js +226 -0
- package/dist/exchange.d.ts +39 -0
- package/dist/exchange.js +219 -0
- package/dist/host-tools.d.ts +5 -0
- package/dist/host-tools.js +515 -0
- package/dist/host.d.ts +122 -0
- package/dist/host.js +1 -0
- package/dist/hub-auth.d.ts +46 -0
- package/dist/hub-auth.js +171 -0
- package/dist/hub.d.ts +4 -0
- package/dist/hub.js +334 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/inputs.d.ts +12 -0
- package/dist/inputs.js +266 -0
- package/dist/learning.d.ts +78 -0
- package/dist/learning.js +603 -0
- package/dist/mechanics.d.ts +3 -0
- package/dist/mechanics.js +6 -0
- package/dist/runtime.d.ts +66 -0
- package/dist/runtime.js +338 -0
- package/dist/sessions.d.ts +80 -0
- package/dist/sessions.js +125 -0
- package/dist/tar.d.ts +3 -0
- package/dist/tar.js +99 -0
- package/package.json +44 -4
package/dist/learning.js
ADDED
|
@@ -0,0 +1,603 @@
|
|
|
1
|
+
import { isMission, isMissionRevision, isRunActiveChange, isRunPastEntry, isRunRetirement, nodeCascadedProperty, readMissionPath, readMissionRun, retire, selectGraph, setActive, tagIndex, } from "@mmon-co/ncept-core";
|
|
2
|
+
import { openProfile } from "./directory.js";
|
|
3
|
+
import { connectHub } from "./hub.js";
|
|
4
|
+
import { checkedTimezone, learnerDayAt } from "./days.js";
|
|
5
|
+
import { localPayload, localSettings, platform, Runtime, sessionStates, transcript, } from "./runtime.js";
|
|
6
|
+
import { hostTools } from "./host-tools.js";
|
|
7
|
+
import { mechanics } from "./mechanics.js";
|
|
8
|
+
import { isSurface, modeEntry } from "./inputs.js";
|
|
9
|
+
import { parsePiSession } from "./sessions.js";
|
|
10
|
+
function identity() {
|
|
11
|
+
return crypto.randomUUID();
|
|
12
|
+
}
|
|
13
|
+
function stringProperty(graph, id, key, fallback) {
|
|
14
|
+
const value = nodeCascadedProperty(graph, id, key);
|
|
15
|
+
return typeof value === "string" ? value : fallback;
|
|
16
|
+
}
|
|
17
|
+
function missionRun(profile, mission) {
|
|
18
|
+
let active = null;
|
|
19
|
+
let retired;
|
|
20
|
+
const past = new Map();
|
|
21
|
+
for (const entry of profile.log) {
|
|
22
|
+
if (isRunPastEntry(entry) && entry.mission === mission)
|
|
23
|
+
past.set(entry.sequence, entry.entry);
|
|
24
|
+
if (isRunActiveChange(entry) && entry.mission === mission)
|
|
25
|
+
active = entry.active;
|
|
26
|
+
if (isRunRetirement(entry) && entry.mission === mission)
|
|
27
|
+
retired = { outcome: entry.outcome, at: entry.at };
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
mission,
|
|
31
|
+
active,
|
|
32
|
+
past: [...past].sort(([a], [b]) => a - b).map(([, entry]) => entry),
|
|
33
|
+
...(retired ? { retired } : {}),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function missions(profile) {
|
|
37
|
+
const revisions = new Map();
|
|
38
|
+
for (const entry of profile.log)
|
|
39
|
+
if (isMissionRevision(entry))
|
|
40
|
+
revisions.set(entry.mission.id, entry.mission);
|
|
41
|
+
return [...revisions.values()].sort((a, b) => b.priority - a.priority);
|
|
42
|
+
}
|
|
43
|
+
function runs(sessions) {
|
|
44
|
+
return sessions.map((session) => ({
|
|
45
|
+
ref: session.id,
|
|
46
|
+
open: !session.closed,
|
|
47
|
+
...(session.parent === undefined ? {} : { parent: session.parent }),
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
function runEntries(before, after, at) {
|
|
51
|
+
return [
|
|
52
|
+
...after.past.slice(before.past.length).map((entry, index) => ({
|
|
53
|
+
kind: "run.past",
|
|
54
|
+
id: identity(),
|
|
55
|
+
at: entry.at,
|
|
56
|
+
mission: after.mission,
|
|
57
|
+
sequence: before.past.length + index,
|
|
58
|
+
entry,
|
|
59
|
+
})),
|
|
60
|
+
...(before.active !== after.active
|
|
61
|
+
? [
|
|
62
|
+
{
|
|
63
|
+
kind: "run.active",
|
|
64
|
+
id: identity(),
|
|
65
|
+
at,
|
|
66
|
+
mission: after.mission,
|
|
67
|
+
active: after.active,
|
|
68
|
+
},
|
|
69
|
+
]
|
|
70
|
+
: []),
|
|
71
|
+
...(!before.retired && after.retired
|
|
72
|
+
? [
|
|
73
|
+
{
|
|
74
|
+
kind: "run.retired",
|
|
75
|
+
id: identity(),
|
|
76
|
+
at,
|
|
77
|
+
mission: after.mission,
|
|
78
|
+
outcome: after.retired.outcome,
|
|
79
|
+
},
|
|
80
|
+
]
|
|
81
|
+
: []),
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
/** Owns execution serialization; the front end owns pi's conversation and transcript writer. */
|
|
85
|
+
export class LearningHost {
|
|
86
|
+
runtime;
|
|
87
|
+
confirm;
|
|
88
|
+
tail = Promise.resolve();
|
|
89
|
+
pendingSwitch;
|
|
90
|
+
drafts = new Map();
|
|
91
|
+
constructor(runtime, confirm) {
|
|
92
|
+
this.runtime = runtime;
|
|
93
|
+
this.confirm = confirm;
|
|
94
|
+
}
|
|
95
|
+
serial(operation) {
|
|
96
|
+
const result = this.tail.then(operation);
|
|
97
|
+
this.tail = result.catch(() => { });
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
async reload() {
|
|
101
|
+
this.pendingSwitch = undefined;
|
|
102
|
+
this.drafts.clear();
|
|
103
|
+
const { timezone } = localSettings(await this.runtime.profile.read());
|
|
104
|
+
this.runtime.timezone = checkedTimezone(typeof timezone === "string" ? timezone : Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
105
|
+
await this.start();
|
|
106
|
+
}
|
|
107
|
+
async current(session) {
|
|
108
|
+
const state = sessionStates(await this.runtime.profile.read()).find(({ id }) => id === session.id);
|
|
109
|
+
if (!state)
|
|
110
|
+
throw new Error("Unknown Session");
|
|
111
|
+
return state;
|
|
112
|
+
}
|
|
113
|
+
mode(theory, mode, entry) {
|
|
114
|
+
if (entry)
|
|
115
|
+
modeEntry(entry);
|
|
116
|
+
const selected = theory.modes?.[mode];
|
|
117
|
+
if (theory.modes && !selected)
|
|
118
|
+
throw new Error("Unknown Theory mode: " + mode);
|
|
119
|
+
if (selected && ((selected.entry.required && !entry) || (entry && !selected.entry.kinds.includes(entry.kind))))
|
|
120
|
+
throw new Error("Invalid entry for " + mode + "; expected " + selected.entry.kinds.join(", "));
|
|
121
|
+
return selected;
|
|
122
|
+
}
|
|
123
|
+
async missionReadings() {
|
|
124
|
+
const view = await this.runtime.snapshot();
|
|
125
|
+
const projection = view.theory.project(view.graph, view.state);
|
|
126
|
+
const sessions = runs(sessionStates(view.profile));
|
|
127
|
+
return missions(view.profile).map((mission) => {
|
|
128
|
+
const run = missionRun(view.profile, mission.id);
|
|
129
|
+
const context = { graph: view.graph, projection, observations: view.observations, activities: [] };
|
|
130
|
+
const path = view.theory.path?.({
|
|
131
|
+
...context,
|
|
132
|
+
targets: mission.targets.map(({ node }) => node),
|
|
133
|
+
past: run.past,
|
|
134
|
+
now: view.now,
|
|
135
|
+
minutes: 30,
|
|
136
|
+
...(run.retired ? { retired: run.retired } : {}),
|
|
137
|
+
});
|
|
138
|
+
const structured = path
|
|
139
|
+
? readMissionPath(path, view.theory, context).sections.flatMap(({ lessons }) => lessons)
|
|
140
|
+
: [];
|
|
141
|
+
return {
|
|
142
|
+
mission,
|
|
143
|
+
run: readMissionRun(run, sessions),
|
|
144
|
+
next: (path?.future ?? []).map((lesson, index) => structured.find((item) => item.kind === lesson.kind &&
|
|
145
|
+
item.tag === lesson.tag &&
|
|
146
|
+
JSON.stringify(item.nodes) === JSON.stringify(lesson.nodes)) ?? { ...lesson, number: index + 1, cleared: false }),
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
async lessonEntry(missionId, number) {
|
|
151
|
+
const view = await this.runtime.snapshot();
|
|
152
|
+
const reading = (await this.missionReadings()).find(({ mission }) => mission.id === missionId);
|
|
153
|
+
if (!reading)
|
|
154
|
+
throw new Error("Unknown Mission");
|
|
155
|
+
const context = {
|
|
156
|
+
graph: view.graph,
|
|
157
|
+
projection: view.theory.project(view.graph, view.state),
|
|
158
|
+
observations: view.observations,
|
|
159
|
+
activities: [],
|
|
160
|
+
};
|
|
161
|
+
const future = view.theory.path?.({
|
|
162
|
+
...context,
|
|
163
|
+
targets: reading.mission.targets.map(({ node }) => node),
|
|
164
|
+
past: reading.run.past,
|
|
165
|
+
now: view.now,
|
|
166
|
+
minutes: 30,
|
|
167
|
+
});
|
|
168
|
+
if (!future)
|
|
169
|
+
throw new Error("Theory provides no Mission path");
|
|
170
|
+
const path = readMissionPath(future, view.theory, context);
|
|
171
|
+
const section = path.sections.find(({ lessons }) => lessons.some((lesson) => lesson.kind === "lesson" && (number === undefined ? !lesson.cleared : lesson.number === number)));
|
|
172
|
+
const lesson = section?.lessons.find((lesson) => lesson.kind === "lesson" && (number === undefined ? !lesson.cleared : lesson.number === number));
|
|
173
|
+
if (!section || !lesson)
|
|
174
|
+
throw new Error("No matching Lesson in this Mission");
|
|
175
|
+
return {
|
|
176
|
+
kind: "lesson",
|
|
177
|
+
lesson: {
|
|
178
|
+
mission: { id: reading.mission.id, statement: reading.mission.statement },
|
|
179
|
+
number: lesson.number,
|
|
180
|
+
of: path.lessonCount,
|
|
181
|
+
name: lesson.name ?? lesson.nodes.map((id) => stringProperty(view.graph, id, "display:label", id)).join(", "),
|
|
182
|
+
section: {
|
|
183
|
+
number: section.number,
|
|
184
|
+
milestone: section.milestone,
|
|
185
|
+
name: stringProperty(view.graph, section.milestone, "display:label", section.milestone),
|
|
186
|
+
},
|
|
187
|
+
nodes: lesson.nodes,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async reading() {
|
|
192
|
+
const view = await this.runtime.snapshot();
|
|
193
|
+
return {
|
|
194
|
+
missions: await this.missionReadings(),
|
|
195
|
+
dueReview: [...tagIndex(view.theory.project(view.graph, view.state))("review:due")],
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async reviseMission(mission, create) {
|
|
199
|
+
if (!isMission(mission))
|
|
200
|
+
throw new Error("Invalid Mission");
|
|
201
|
+
const view = await this.runtime.snapshot();
|
|
202
|
+
const exists = missions(view.profile).some(({ id }) => id === mission.id);
|
|
203
|
+
if (exists === create)
|
|
204
|
+
throw new Error(create ? "Mission already exists" : "Unknown Mission");
|
|
205
|
+
for (const target of mission.targets) {
|
|
206
|
+
if (!view.graph.nodes.some(({ id }) => id === target.node))
|
|
207
|
+
throw new Error("Mission target is not in the graph");
|
|
208
|
+
const source = await this.runtime.snapshot(target.provenance);
|
|
209
|
+
if (!source.graph.nodes.some(({ id }) => id === target.node))
|
|
210
|
+
throw new Error("Invalid Mission target provenance");
|
|
211
|
+
}
|
|
212
|
+
const event = await this.runtime.event({ kind: "mission", text: mission.statement });
|
|
213
|
+
await this.runtime.profile.appendLog([
|
|
214
|
+
{ kind: "mission.revised", id: identity(), at: view.now, mission },
|
|
215
|
+
...event.entries,
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
async closing(session) {
|
|
219
|
+
if (session.closed)
|
|
220
|
+
return [];
|
|
221
|
+
const view = await this.runtime.snapshot();
|
|
222
|
+
const conversation = transcript(view.profile, session.surface);
|
|
223
|
+
const event = await this.runtime.event({
|
|
224
|
+
kind: "surface.close",
|
|
225
|
+
surface: session.surface,
|
|
226
|
+
about: session.about,
|
|
227
|
+
span: conversation.span,
|
|
228
|
+
}, session);
|
|
229
|
+
return [
|
|
230
|
+
...event.entries,
|
|
231
|
+
platform({ kind: "local.session", session: { ...session, closed: true } }, session.surface),
|
|
232
|
+
];
|
|
233
|
+
}
|
|
234
|
+
async closingRun(current, profile) {
|
|
235
|
+
const known = sessionStates(profile);
|
|
236
|
+
const closing = new Set([current.id]);
|
|
237
|
+
for (const id of closing)
|
|
238
|
+
for (const child of known)
|
|
239
|
+
if (!child.closed && child.parent === id)
|
|
240
|
+
closing.add(child.id);
|
|
241
|
+
const entries = [];
|
|
242
|
+
if (current.mission) {
|
|
243
|
+
const before = missionRun(profile, current.mission);
|
|
244
|
+
if (before.active === current.id) {
|
|
245
|
+
const at = new Date().toISOString();
|
|
246
|
+
const result = setActive(before, { kind: "none" }, at, runs(known));
|
|
247
|
+
for (const id of result.close)
|
|
248
|
+
closing.add(id);
|
|
249
|
+
entries.push(...runEntries(before, result.run, at));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
for (const session of known)
|
|
253
|
+
if (closing.has(session.id))
|
|
254
|
+
entries.push(...(await this.closing(session)));
|
|
255
|
+
return entries;
|
|
256
|
+
}
|
|
257
|
+
async close(session) {
|
|
258
|
+
const current = await this.current(session);
|
|
259
|
+
await this.runtime.profile.appendLog(await this.closingRun(current, await this.runtime.profile.read()));
|
|
260
|
+
}
|
|
261
|
+
async retireMission(id, outcome) {
|
|
262
|
+
const view = await this.runtime.snapshot();
|
|
263
|
+
if (!missions(view.profile).some((mission) => mission.id === id))
|
|
264
|
+
throw new Error("Unknown Mission");
|
|
265
|
+
const before = missionRun(view.profile, id);
|
|
266
|
+
const cleared = setActive(before, { kind: "none" }, view.now, runs(sessionStates(view.profile)));
|
|
267
|
+
const after = retire(cleared.run, outcome, view.now);
|
|
268
|
+
const entries = [...runEntries(before, after, view.now)];
|
|
269
|
+
for (const ref of cleared.close) {
|
|
270
|
+
const session = sessionStates(view.profile).find(({ id }) => id === ref);
|
|
271
|
+
if (session)
|
|
272
|
+
entries.push(...(await this.closing(session)));
|
|
273
|
+
}
|
|
274
|
+
await this.runtime.profile.appendLog(entries);
|
|
275
|
+
}
|
|
276
|
+
async open(input) {
|
|
277
|
+
const view = await this.runtime.snapshot();
|
|
278
|
+
const known = sessionStates(view.profile);
|
|
279
|
+
let resumed = input.resume ? known.find(({ id }) => id === input.resume) : undefined;
|
|
280
|
+
if (input.resume && !resumed) {
|
|
281
|
+
for (const [surface, source] of view.profile.sessions) {
|
|
282
|
+
const imported = parsePiSession(source);
|
|
283
|
+
if (isSurface(surface) && (imported.header.id === input.resume || surface === input.resume)) {
|
|
284
|
+
resumed = {
|
|
285
|
+
id: imported.header.id,
|
|
286
|
+
surface,
|
|
287
|
+
mode: input.mode,
|
|
288
|
+
about: input.entry?.kind === "review"
|
|
289
|
+
? input.entry.nodes
|
|
290
|
+
: input.entry?.kind === "lesson"
|
|
291
|
+
? input.entry.lesson.nodes
|
|
292
|
+
: input.entry?.kind === "target"
|
|
293
|
+
? [input.entry.node]
|
|
294
|
+
: [
|
|
295
|
+
...new Set(view.observations
|
|
296
|
+
.filter((observation) => observation.surface === surface)
|
|
297
|
+
.flatMap((observation) => observation.nodes ?? observation.about ?? [])),
|
|
298
|
+
],
|
|
299
|
+
closed: true,
|
|
300
|
+
...(input.entry === undefined ? {} : { entry: input.entry }),
|
|
301
|
+
};
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (!resumed)
|
|
306
|
+
throw new Error("Unknown trusted Session to resume");
|
|
307
|
+
}
|
|
308
|
+
const mode = resumed?.mode ?? input.mode;
|
|
309
|
+
const entry = resumed?.entry ?? input.entry;
|
|
310
|
+
this.mode(view.theory, mode, entry);
|
|
311
|
+
const pending = !resumed &&
|
|
312
|
+
this.pendingSwitch?.to.mode === mode &&
|
|
313
|
+
JSON.stringify(this.pendingSwitch.to.entry) === JSON.stringify(entry)
|
|
314
|
+
? this.pendingSwitch
|
|
315
|
+
: undefined;
|
|
316
|
+
const missionId = resumed?.mission ??
|
|
317
|
+
(entry?.kind === "mission"
|
|
318
|
+
? entry.id
|
|
319
|
+
: entry?.kind === "lesson"
|
|
320
|
+
? entry.lesson.mission.id
|
|
321
|
+
: entry?.kind === "checkpoint"
|
|
322
|
+
? entry.missionId
|
|
323
|
+
: pending?.from.mission);
|
|
324
|
+
const mission = missionId ? missions(view.profile).find(({ id }) => id === missionId) : undefined;
|
|
325
|
+
if (missionId && !mission)
|
|
326
|
+
throw new Error("Unknown Mission");
|
|
327
|
+
const about = resumed?.about ??
|
|
328
|
+
(entry?.kind === "target"
|
|
329
|
+
? [entry.node]
|
|
330
|
+
: entry?.kind === "review"
|
|
331
|
+
? entry.nodes
|
|
332
|
+
: entry?.kind === "lesson"
|
|
333
|
+
? entry.lesson.nodes
|
|
334
|
+
: mission
|
|
335
|
+
? selectGraph(view.graph, { cone: { targets: mission.targets.map(({ node }) => node) } }).nodes.map(({ id }) => id)
|
|
336
|
+
: []);
|
|
337
|
+
for (const node of about)
|
|
338
|
+
if (!view.graph.nodes.some(({ id }) => id === node))
|
|
339
|
+
throw new Error("Missing Node " + node);
|
|
340
|
+
const id = resumed?.id ?? identity();
|
|
341
|
+
const session = {
|
|
342
|
+
id,
|
|
343
|
+
surface: resumed?.surface ?? `surface-${id}`,
|
|
344
|
+
mode,
|
|
345
|
+
about,
|
|
346
|
+
closed: false,
|
|
347
|
+
...(entry === undefined ? {} : { entry }),
|
|
348
|
+
...(missionId === undefined ? {} : { mission: missionId }),
|
|
349
|
+
...(pending?.to.then === "aside"
|
|
350
|
+
? { parent: pending.from.id }
|
|
351
|
+
: resumed?.parent
|
|
352
|
+
? { parent: resumed.parent }
|
|
353
|
+
: {}),
|
|
354
|
+
};
|
|
355
|
+
const entries = [];
|
|
356
|
+
if (missionId) {
|
|
357
|
+
const before = missionRun(view.profile, missionId);
|
|
358
|
+
const stop = {
|
|
359
|
+
kind: entry?.kind ?? mode,
|
|
360
|
+
name: entry?.kind === "lesson" ? entry.lesson.name : mode,
|
|
361
|
+
nodes: about,
|
|
362
|
+
};
|
|
363
|
+
const target = resumed && before.past.some((entry) => entry.kind === "session" && entry.session === id)
|
|
364
|
+
? { kind: "resume", session: id }
|
|
365
|
+
: pending?.to.then === "aside"
|
|
366
|
+
? { kind: "aside", session: id }
|
|
367
|
+
: { kind: "new", session: id, stop };
|
|
368
|
+
const result = setActive(before, target, view.now, runs(known));
|
|
369
|
+
entries.push(...runEntries(before, result.run, view.now));
|
|
370
|
+
for (const ref of result.close) {
|
|
371
|
+
const outgoing = known.find(({ id }) => id === ref);
|
|
372
|
+
if (outgoing)
|
|
373
|
+
entries.push(...(await this.closing(outgoing)));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (pending?.to.then === "next" && (!missionId || pending.from.mission !== missionId))
|
|
377
|
+
entries.push(...(await this.closingRun(pending.from, view.profile)));
|
|
378
|
+
const event = await this.runtime.event({ kind: "surface.open", surface: session.surface, format: "local", about }, session);
|
|
379
|
+
entries.push(...event.entries, platform({ kind: "local.session", session }, session.surface));
|
|
380
|
+
await this.runtime.profile.appendLog(entries);
|
|
381
|
+
if (pending)
|
|
382
|
+
this.pendingSwitch = undefined;
|
|
383
|
+
return session;
|
|
384
|
+
}
|
|
385
|
+
async switchMode(session, to) {
|
|
386
|
+
const view = await this.runtime.snapshot();
|
|
387
|
+
const selected = this.mode(view.theory, session.mode, session.entry);
|
|
388
|
+
if (!selected?.switches.some((allowed) => allowed.mode === to.mode && allowed.then === to.then))
|
|
389
|
+
throw new Error("This mode does not allow that switch");
|
|
390
|
+
this.mode(view.theory, to.mode, to.entry);
|
|
391
|
+
if (to.then === "here") {
|
|
392
|
+
const missionId = to.entry?.kind === "lesson"
|
|
393
|
+
? to.entry.lesson.mission.id
|
|
394
|
+
: to.entry?.kind === "mission"
|
|
395
|
+
? to.entry.id
|
|
396
|
+
: to.entry?.kind === "checkpoint"
|
|
397
|
+
? to.entry.missionId
|
|
398
|
+
: session.mission;
|
|
399
|
+
const mission = missions(view.profile).find(({ id }) => id === missionId);
|
|
400
|
+
if (missionId && !mission)
|
|
401
|
+
throw new Error("Unknown Mission");
|
|
402
|
+
const about = to.entry?.kind === "review"
|
|
403
|
+
? to.entry.nodes
|
|
404
|
+
: to.entry?.kind === "lesson"
|
|
405
|
+
? to.entry.lesson.nodes
|
|
406
|
+
: to.entry?.kind === "target"
|
|
407
|
+
? [to.entry.node]
|
|
408
|
+
: mission
|
|
409
|
+
? selectGraph(view.graph, { cone: { targets: mission.targets.map(({ node }) => node) } }).nodes.map(({ id }) => id)
|
|
410
|
+
: session.about;
|
|
411
|
+
for (const node of about)
|
|
412
|
+
if (!view.graph.nodes.some(({ id }) => id === node))
|
|
413
|
+
throw new Error("Missing Node " + node);
|
|
414
|
+
const updated = {
|
|
415
|
+
...session,
|
|
416
|
+
mode: to.mode,
|
|
417
|
+
entry: to.entry,
|
|
418
|
+
about,
|
|
419
|
+
...(missionId ? { mission: missionId } : {}),
|
|
420
|
+
};
|
|
421
|
+
const entries = [];
|
|
422
|
+
if (missionId) {
|
|
423
|
+
const before = missionRun(view.profile, missionId);
|
|
424
|
+
const attached = before.past.some((entry) => entry.kind === "session" && entry.session === session.id);
|
|
425
|
+
const result = setActive(before, { kind: attached ? "resume" : "attach", session: session.id }, view.now, runs(sessionStates(view.profile)));
|
|
426
|
+
entries.push(...runEntries(before, result.run, view.now));
|
|
427
|
+
for (const id of result.close) {
|
|
428
|
+
const outgoing = sessionStates(view.profile).find((item) => item.id === id);
|
|
429
|
+
if (outgoing)
|
|
430
|
+
entries.push(...(await this.closing(outgoing)));
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
entries.push(platform({ kind: "local.session", session: updated }, session.surface));
|
|
434
|
+
await this.runtime.profile.appendLog(entries);
|
|
435
|
+
}
|
|
436
|
+
else
|
|
437
|
+
this.pendingSwitch = { from: session, to };
|
|
438
|
+
}
|
|
439
|
+
async context(session) {
|
|
440
|
+
const view = await this.runtime.snapshot();
|
|
441
|
+
const ctx = await this.runtime.context(session);
|
|
442
|
+
const readings = await this.missionReadings();
|
|
443
|
+
let lesson;
|
|
444
|
+
if (session.entry?.kind === "lesson") {
|
|
445
|
+
const snapshot = session.entry.lesson;
|
|
446
|
+
const projection = view.theory.project(ctx.graph, ctx.state);
|
|
447
|
+
const progress = view.theory.progress(ctx.graph, projection, snapshot.nodes);
|
|
448
|
+
const nodes = snapshot.nodes.map((id) => ({
|
|
449
|
+
id,
|
|
450
|
+
label: stringProperty(ctx.graph, id, "display:label", id),
|
|
451
|
+
criterion: stringProperty(ctx.graph, id, "pedagogy:criterion", ""),
|
|
452
|
+
status: progress[id]?.status ?? "open",
|
|
453
|
+
...(progress[id]?.unavailable ? { unavailable: true } : {}),
|
|
454
|
+
...(progress[id]?.display ? { score: progress[id].display } : {}),
|
|
455
|
+
}));
|
|
456
|
+
const estimate = view.theory.estimate({
|
|
457
|
+
graph: ctx.graph,
|
|
458
|
+
projection,
|
|
459
|
+
observations: view.observations,
|
|
460
|
+
activities: [],
|
|
461
|
+
}, [{ kind: "lesson", nodes: snapshot.nodes, numbered: true }]);
|
|
462
|
+
lesson = {
|
|
463
|
+
lesson: snapshot,
|
|
464
|
+
nodes,
|
|
465
|
+
edges: ctx.graph.edges.filter(({ from, to }) => snapshot.nodes.includes(from) && snapshot.nodes.includes(to)),
|
|
466
|
+
alsoShown: [],
|
|
467
|
+
doneCount: nodes.filter(({ status }) => status === "done").length,
|
|
468
|
+
complete: nodes.every(({ status }) => status === "done"),
|
|
469
|
+
minutesLeft: estimate.minutes,
|
|
470
|
+
rough: estimate.rough,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
return {
|
|
474
|
+
...ctx,
|
|
475
|
+
...(lesson ? { lesson } : {}),
|
|
476
|
+
missions: readings.map(({ mission, run, next }) => ({
|
|
477
|
+
id: mission.id,
|
|
478
|
+
statement: mission.statement,
|
|
479
|
+
change: view.profile.log.filter(isMissionRevision).findLast((entry) => entry.mission.id === mission.id)?.id ??
|
|
480
|
+
mission.id,
|
|
481
|
+
nodes: selectGraph(view.graph, { cone: { targets: mission.targets.map(({ node }) => node) } }).nodes.map(({ id }) => id),
|
|
482
|
+
targets: mission.targets.map(({ node }) => node),
|
|
483
|
+
priority: mission.priority,
|
|
484
|
+
run: { ...run, next },
|
|
485
|
+
})),
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
async turn(input, learnerMessage) {
|
|
489
|
+
const session = await this.current(input);
|
|
490
|
+
if (session.closed)
|
|
491
|
+
throw new Error("Session is closed; resume it first");
|
|
492
|
+
let view = await this.runtime.snapshot();
|
|
493
|
+
let ctx = await this.context(session);
|
|
494
|
+
if (learnerMessage !== undefined && view.theory.chat?.reply) {
|
|
495
|
+
await this.runtime.execute(view.theory.chat.reply(ctx, learnerMessage), session);
|
|
496
|
+
view = await this.runtime.snapshot();
|
|
497
|
+
ctx = await this.context(session);
|
|
498
|
+
}
|
|
499
|
+
const selected = this.mode(view.theory, session.mode, session.entry);
|
|
500
|
+
const program = selected ? selected.setup(ctx) : view.theory.chat?.turn(ctx);
|
|
501
|
+
if (!program)
|
|
502
|
+
throw new Error("Theory does not provide a tutor setup");
|
|
503
|
+
const setup = await this.runtime.execute(program, session);
|
|
504
|
+
const native = (tools) => tools.flatMap((tool) => (tool.members ? native(tool.members) : [tool]));
|
|
505
|
+
const capabilities = selected?.capabilities ?? [];
|
|
506
|
+
const tools = native(setup.tools).map((tool) => ({
|
|
507
|
+
name: tool.name,
|
|
508
|
+
description: tool.description,
|
|
509
|
+
schema: tool.schema,
|
|
510
|
+
run: (args) => this.serial(async () => {
|
|
511
|
+
const current = await this.current(session);
|
|
512
|
+
if (current.closed || current.mode !== session.mode)
|
|
513
|
+
throw new Error("Tool belongs to an inactive Session mode");
|
|
514
|
+
const result = await this.runtime.prepare(tool.run(args, await this.context(current)), current);
|
|
515
|
+
const feature = await this.runtime.event({ kind: "feature.used", operation: tool.name, at: new Date().toISOString(), about: current.about }, current);
|
|
516
|
+
await this.runtime.profile.appendLog([...result.entries, ...feature.entries]);
|
|
517
|
+
return { content: result.value };
|
|
518
|
+
}),
|
|
519
|
+
}));
|
|
520
|
+
const settings = localSettings(view.profile);
|
|
521
|
+
return {
|
|
522
|
+
...setup,
|
|
523
|
+
prompt: setup.prompt +
|
|
524
|
+
(typeof settings.preferences === "string" ? "\n\nLearner preferences:\n" + settings.preferences : ""),
|
|
525
|
+
capabilities,
|
|
526
|
+
context: [
|
|
527
|
+
setup.context,
|
|
528
|
+
"Local host (native tools, not catalog envelopes):\n" +
|
|
529
|
+
JSON.stringify({
|
|
530
|
+
mode: session.mode,
|
|
531
|
+
capabilities,
|
|
532
|
+
switches: selected?.switches ?? [],
|
|
533
|
+
entries: Object.fromEntries(Object.entries(view.theory.modes ?? {}).map(([name, mode]) => [name, mode.entry])),
|
|
534
|
+
}),
|
|
535
|
+
]
|
|
536
|
+
.filter(Boolean)
|
|
537
|
+
.join("\n\n"),
|
|
538
|
+
tools: [...tools, ...hostTools(this, session, capabilities, setup.skills, selected?.metadataKeys ?? [])],
|
|
539
|
+
skills: [...setup.skills, mechanics],
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
async config() {
|
|
543
|
+
const { manifest } = await this.runtime.profile.read();
|
|
544
|
+
return { theory: manifest.theory, theories: manifest.theories, timezone: this.runtime.timezone };
|
|
545
|
+
}
|
|
546
|
+
async configure(config) {
|
|
547
|
+
const timezone = checkedTimezone(config.timezone ?? this.runtime.timezone);
|
|
548
|
+
await this.runtime.profile.configure({ theory: config.theory, theories: config.theories });
|
|
549
|
+
await this.runtime.profile.appendLog([platform({ kind: "local.config", timezone })]);
|
|
550
|
+
this.runtime.timezone = timezone;
|
|
551
|
+
}
|
|
552
|
+
async start() {
|
|
553
|
+
const view = await this.runtime.snapshot();
|
|
554
|
+
const lastDay = view.profile.log
|
|
555
|
+
.map((entry) => localPayload(entry, "day.ended"))
|
|
556
|
+
.filter((entry) => entry !== undefined)
|
|
557
|
+
.at(-1);
|
|
558
|
+
const end = lastDay && typeof lastDay.end === "string" ? lastDay.end : undefined;
|
|
559
|
+
let day = learnerDayAt(end ?? view.profile.log[0]?.at ?? view.now, this.runtime.timezone);
|
|
560
|
+
while (Date.parse(day.end) <= Date.parse(view.now)) {
|
|
561
|
+
const event = await this.runtime.event({ kind: "day.ended", day, about: view.graph.nodes.map(({ id }) => id) });
|
|
562
|
+
await this.runtime.profile.appendLog([...event.entries, platform({ kind: "day.ended", ...day })]);
|
|
563
|
+
day = learnerDayAt(day.end, this.runtime.timezone);
|
|
564
|
+
}
|
|
565
|
+
if (typeof localSettings(view.profile).timezone !== "string")
|
|
566
|
+
await this.runtime.profile.appendLog([platform({ kind: "local.config", timezone: this.runtime.timezone })]);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
export async function openHost(options) {
|
|
570
|
+
const profile = await openProfile(options.profileDir, options.theories);
|
|
571
|
+
const config = localSettings(await profile.read());
|
|
572
|
+
const timezone = checkedTimezone(typeof config.timezone === "string" ? config.timezone : Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
573
|
+
const learning = new LearningHost(new Runtime(profile, options.theories, options.model, timezone), options.confirm);
|
|
574
|
+
await learning.start();
|
|
575
|
+
const hub = connectHub(profile, options.theories, options.confirm, options.hub);
|
|
576
|
+
return {
|
|
577
|
+
profile,
|
|
578
|
+
sessions: {
|
|
579
|
+
open: (input) => learning.serial(() => learning.open(input)),
|
|
580
|
+
turn: (session, input) => learning.serial(() => learning.turn(session, input.learnerMessage)),
|
|
581
|
+
close: (session) => learning.serial(() => learning.close(session)),
|
|
582
|
+
},
|
|
583
|
+
missions: {
|
|
584
|
+
list: () => learning.missionReadings(),
|
|
585
|
+
create: (mission) => learning.serial(() => learning.reviseMission(mission, true)),
|
|
586
|
+
revise: (mission) => learning.serial(() => learning.reviseMission(mission, false)),
|
|
587
|
+
retire: (id, outcome) => learning.serial(() => learning.retireMission(id, outcome)),
|
|
588
|
+
},
|
|
589
|
+
reading: () => learning.reading(),
|
|
590
|
+
config: { read: () => learning.config(), update: (config) => learning.serial(() => learning.configure(config)) },
|
|
591
|
+
hub: {
|
|
592
|
+
pull: (address) => learning.serial(() => hub.pull(address)),
|
|
593
|
+
signIn: () => learning.serial(() => hub.signIn()),
|
|
594
|
+
publish: (input) => learning.serial(() => hub.publish(input)),
|
|
595
|
+
exportTo: (destination) => learning.serial(() => hub.exportTo(destination)),
|
|
596
|
+
importFrom: (source) => learning.serial(async () => {
|
|
597
|
+
const result = await hub.importFrom(source);
|
|
598
|
+
await learning.reload();
|
|
599
|
+
return result;
|
|
600
|
+
}),
|
|
601
|
+
},
|
|
602
|
+
};
|
|
603
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The mechanics Skill from docs/attachments/local-host-mechanics.md. */
|
|
2
|
+
export const mechanics = {
|
|
3
|
+
name: "local-host",
|
|
4
|
+
description: "A Material needs making, opening or reading back, or an outcome, change or reminder needs recording.",
|
|
5
|
+
body: "The learner's work lives in the Profile directory; the Theory's tools, not you, decide what a record means.\n\n## Materials\n\nMake a Material with the `material` tool, in whatever form fits the capability: Markdown for reading, a quiz as a file of tasks with their answers, cards as one file of front/back pairs, an applet as an HTML page or a program in any language the learner can run, audio as a clip. The tool records it in the learner's map and checks out a working copy under `materials/<node>/`; a file you write there yourself is not a Material. Name each by what it teaches. Reuse a Material about the Node before making another. Show one by telling the learner its path (or running it for them when they have asked); what they change in the working copy is their work, read back as such.\n\n## Grading and outcomes\n\nGrade mechanically by running the check yourself where the answer is mechanical; otherwise judge the learner's work against the Node's criterion. Record the result only through the Theory's record tools (`record_material_attempt`, `record_demonstration`, `record_retrieval_opener`, `record_teaching`, `record_claim`, `record_retraction`, `close_recount`) with the task and the learner's response as they were; never write a label or edit the log. For quiz items answered in chat, use `record_material_attempt` with the Material identity (playground `local` for local files), item id, Node, task, response, outcome and unaided status. Use `record_demonstration` for ordinary conversational checks. A runner's existing item result is returned without duplication; correct a wrong grade with `evaluation_override` and a reason. The Theory validates the payload and refuses a malformed one; fix what it names.\n\n## Applets and read-back\n\nAn applet keeps its state in a file beside it. Read that file, or run its summary command, before describing the learner's position; do not rely on your last edit. A learner action arrives as the next message or as a file change you are told about; a program that needs your attention sends a notification (a terminal bell or the harness's own notice) and writes what happened to the state file first.\n\n## Notifications and wake-ups\n\nYou act only when the learner speaks or the harness runs you. To remind the learner or return later, write a scheduled prompt (the harness's scheduler or a note in `reminders.md`) naming the Node and the Session to resume; never promise a message you cannot send.\n\n## Changes\n\nA Mission, graph or guidance change is a proposal: show the diff as a file the learner can read and ask for approval in the harness's own prompt. Apply it through the Theory's operations only after they say yes. Nothing proposed is saved before that.\n",
|
|
6
|
+
};
|