@composable-os/pack-programs 0.1.0-beta.10 → 0.1.0-beta.13
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/dist/authoring.d.ts +4 -1
- package/dist/authoring.js +65 -5
- package/dist/blocks/programs-builder/program-builder.d.ts +12 -3
- package/dist/blocks/programs-builder/program-builder.js +16 -3
- package/dist/blocks/programs-class-today/class-today.d.ts +1 -1
- package/dist/blocks/programs-coach-client/coach-client.d.ts +1 -1
- package/dist/blocks/programs-history/history.d.ts +11 -0
- package/dist/blocks/programs-history/history.js +84 -0
- package/dist/blocks/programs-native-class-today/class-today-screen.js +3 -2
- package/dist/blocks/programs-native-coach-client/coach-client-screen.js +2 -2
- package/dist/blocks/programs-native-history/history-screen.d.ts +11 -0
- package/dist/blocks/programs-native-history/history-screen.js +98 -0
- package/dist/blocks/programs-native-progress/progress-screen.js +4 -4
- package/dist/blocks/programs-native-session/session-screen.d.ts +6 -2
- package/dist/blocks/programs-native-session/session-screen.js +214 -88
- package/dist/blocks/programs-native-today/today-screen.d.ts +1 -1
- package/dist/blocks/programs-native-today/today-screen.js +22 -3
- package/dist/blocks/programs-session/session-view.d.ts +7 -3
- package/dist/blocks/programs-session/session-view.js +139 -81
- package/dist/blocks/programs-today/today.d.ts +1 -1
- package/dist/client/index.d.ts +367 -12
- package/dist/client/index.js +393 -12
- package/dist/client/logging.d.ts +289 -0
- package/dist/client/logging.js +558 -0
- package/dist/index.d.ts +152 -6
- package/dist/index.js +141 -6
- package/dist/layout.d.ts +3 -1
- package/dist/layout.js +10 -2
- package/dist/tools.d.ts +25 -2
- package/dist/tools.js +269 -37
- package/package.json +3 -3
package/dist/tools.js
CHANGED
|
@@ -7,14 +7,19 @@
|
|
|
7
7
|
//
|
|
8
8
|
// Called by a customer, a tool runs as that customer (ADR 0414): its calls are my.* and see only
|
|
9
9
|
// what the seam shows them. Called by staff, it uses record.* and names the person.
|
|
10
|
-
import { defineTool, OpsCallError, z } from "@composable-os/sdk";
|
|
10
|
+
import { defineTool, OpsCallError, validateDocument, z, } from "@composable-os/sdk";
|
|
11
11
|
import { draftLayout, pickExercises, readBrief, readProgramText, } from "./authoring.js";
|
|
12
|
-
import { prescriptions, } from "./layout.js";
|
|
12
|
+
import { LAYOUT_SCHEMA, prescriptions, } from "./layout.js";
|
|
13
13
|
const EXERCISE = "programs.exercise";
|
|
14
14
|
const PROGRAM = "programs.program";
|
|
15
15
|
const ASSIGNMENT = "programs.assignment";
|
|
16
16
|
const SESSION = "programs.session";
|
|
17
17
|
const LOG = "programs.log";
|
|
18
|
+
/**
|
|
19
|
+
* The coach role's tools (ADR 0889): a client's training, read and logged for them. Any member of
|
|
20
|
+
* staff calls them as before; the coach role holds them for the People it looks after only.
|
|
21
|
+
*/
|
|
22
|
+
const COACH = "programs:coach";
|
|
18
23
|
/** Sessions written in one call: record.createMany's cap (ADR 0448). */
|
|
19
24
|
const SESSIONS_PER_BATCH = 50;
|
|
20
25
|
/** The most sets a history or progress reads (pages of 200). */
|
|
@@ -372,6 +377,7 @@ export const today = defineTool({
|
|
|
372
377
|
}),
|
|
373
378
|
risk: "read",
|
|
374
379
|
audiences: ["staff", "customer"],
|
|
380
|
+
permission: COACH,
|
|
375
381
|
async handler(input, ctx) {
|
|
376
382
|
const day = input.date ?? todayUtc();
|
|
377
383
|
const person = personFilter(ctx, input.personId);
|
|
@@ -401,6 +407,7 @@ export const sessionGet = defineTool({
|
|
|
401
407
|
}),
|
|
402
408
|
risk: "read",
|
|
403
409
|
audiences: ["staff", "customer"],
|
|
410
|
+
permission: COACH,
|
|
404
411
|
async handler(input, ctx) {
|
|
405
412
|
const record = await get(ctx, SESSION, input.sessionId);
|
|
406
413
|
const session = sessionOf(record);
|
|
@@ -483,16 +490,22 @@ function targetIn(plan, item, exercise, level, setNumber) {
|
|
|
483
490
|
throw new OpsCallError("invalid_input", `This session has no item ${item}`, { item });
|
|
484
491
|
}
|
|
485
492
|
const levels = [...new Set(found.map((p) => p.level))];
|
|
486
|
-
|
|
493
|
+
// An item's exercises for everyone (level null) are logged with or without the level taken.
|
|
494
|
+
const everyone = found.filter((p) => p.level === null);
|
|
495
|
+
if (levels.some((l) => l !== null) && level === undefined && !everyone.length) {
|
|
487
496
|
throw new OpsCallError("invalid_input", `Item ${item} has levels: say which was taken`, {
|
|
488
497
|
item,
|
|
489
498
|
levels,
|
|
490
499
|
});
|
|
491
500
|
}
|
|
492
|
-
|
|
493
|
-
if (!atLevel.length) {
|
|
501
|
+
if (level !== undefined && !levels.includes(level)) {
|
|
494
502
|
throw new OpsCallError("invalid_input", `Item ${item} has no level ${level}`, { item, levels });
|
|
495
503
|
}
|
|
504
|
+
const atLevel = level === undefined
|
|
505
|
+
? levels.some((l) => l !== null)
|
|
506
|
+
? everyone
|
|
507
|
+
: found
|
|
508
|
+
: [...found.filter((p) => p.level === level), ...everyone];
|
|
496
509
|
const match = exercise ? atLevel.filter((p) => p.prescription.exercise === exercise) : atLevel;
|
|
497
510
|
if (!match.length || (!exercise && match.length > 1)) {
|
|
498
511
|
throw new OpsCallError("invalid_input", exercise
|
|
@@ -503,7 +516,8 @@ function targetIn(plan, item, exercise, level, setNumber) {
|
|
|
503
516
|
return {
|
|
504
517
|
prescription: chosen.prescription,
|
|
505
518
|
target: chosen.prescription.sets[setNumber - 1] ?? null,
|
|
506
|
-
level
|
|
519
|
+
// An exercise for everyone keeps the level the person took, when they say it.
|
|
520
|
+
level: chosen.level ?? level ?? null,
|
|
507
521
|
};
|
|
508
522
|
}
|
|
509
523
|
const actual = z.object({
|
|
@@ -518,13 +532,30 @@ const rpe = z
|
|
|
518
532
|
.refine((n) => Number.isInteger(n * 2), "In halves: 7 or 7.5")
|
|
519
533
|
.optional()
|
|
520
534
|
.describe("How hard it felt, 1 to 10 in halves");
|
|
535
|
+
/**
|
|
536
|
+
* What names one set of one person (ADR 0448, amended 2026-11-24): the session, the item, the
|
|
537
|
+
* exercise and the set's number. The log declares it unique per person, so the database keeps one
|
|
538
|
+
* row per set however many saves of it arrive at once.
|
|
539
|
+
*/
|
|
540
|
+
const setKeyOf = (sessionId, item, exercise, setNumber) => `${sessionId}/${item}/${exercise}/${setNumber}`;
|
|
541
|
+
/** The live rows of one set: the caller's own for a member, the named person's for staff. */
|
|
542
|
+
async function setRows(ctx, set, person) {
|
|
543
|
+
const page = await list(ctx, LOG, {
|
|
544
|
+
filter: and({ field: "session", op: "eq", value: set.sessionId }, { field: "item", op: "eq", value: set.item }, { field: "exercise", op: "eq", value: set.exercise }, { field: "setNumber", op: "eq", value: set.setNumber }, person ? { field: "person", op: "eq", value: person } : null),
|
|
545
|
+
sort: { field: "setNumber", direction: "asc" },
|
|
546
|
+
limit: 20,
|
|
547
|
+
});
|
|
548
|
+
return page.items;
|
|
549
|
+
}
|
|
521
550
|
export const logAdd = defineTool({
|
|
522
551
|
description: "Log one set done on a session: the item (A1), the exercise when the item has more than " +
|
|
523
552
|
"one, the level taken when it has levels, which set (1, 2, …), what was done (reps, " +
|
|
524
553
|
"seconds, load) and how hard it felt (RPE). The target is taken from the session's plan, " +
|
|
525
554
|
"and who recorded it is the caller. A member logs on their own sessions and their class " +
|
|
526
555
|
"types'; a coach logs for a client (on a class session, naming them). Nobody logs on a " +
|
|
527
|
-
"session that is not theirs.
|
|
556
|
+
"session that is not theirs. A set already logged (a retried save, a second device) is " +
|
|
557
|
+
"not logged twice: its numbers and effort become these, and its note is kept unless one " +
|
|
558
|
+
"is given. Returns the set.",
|
|
528
559
|
input: z.object({
|
|
529
560
|
sessionId: id,
|
|
530
561
|
item: z.string().min(1).max(6),
|
|
@@ -539,6 +570,7 @@ export const logAdd = defineTool({
|
|
|
539
570
|
output: loggedSet,
|
|
540
571
|
risk: "write",
|
|
541
572
|
audiences: ["staff", "customer"],
|
|
573
|
+
permission: COACH,
|
|
542
574
|
customerFields: {
|
|
543
575
|
[LOG]: [
|
|
544
576
|
"label",
|
|
@@ -554,6 +586,7 @@ export const logAdd = defineTool({
|
|
|
554
586
|
"targetSeconds",
|
|
555
587
|
"targetLoad",
|
|
556
588
|
"recordedAs",
|
|
589
|
+
"setKey",
|
|
557
590
|
],
|
|
558
591
|
},
|
|
559
592
|
async handler(input, ctx) {
|
|
@@ -586,20 +619,60 @@ export const logAdd = defineTool({
|
|
|
586
619
|
rpe: input.rpe ?? null,
|
|
587
620
|
note: input.note ?? null,
|
|
588
621
|
recordedAs: isCustomer(ctx) ? "self" : "staff",
|
|
622
|
+
setKey: setKeyOf(input.sessionId, input.item, prescription.exercise, input.setNumber),
|
|
589
623
|
};
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
624
|
+
const set = {
|
|
625
|
+
sessionId: input.sessionId,
|
|
626
|
+
item: input.item,
|
|
627
|
+
exercise: prescription.exercise,
|
|
628
|
+
setNumber: input.setNumber,
|
|
629
|
+
};
|
|
630
|
+
// Logged already: the same set again changes its numbers, so a save sent twice logs it once.
|
|
631
|
+
const again = async () => {
|
|
632
|
+
const [row] = await setRows(ctx, set, person);
|
|
633
|
+
if (!row)
|
|
634
|
+
return null;
|
|
635
|
+
return loggedOf(await ctx.ops.call(isCustomer(ctx) ? "my.update" : "record.update", {
|
|
636
|
+
object: LOG,
|
|
637
|
+
id: row.id,
|
|
638
|
+
values: {
|
|
639
|
+
level,
|
|
640
|
+
actualReps: values.actualReps,
|
|
641
|
+
actualSeconds: values.actualSeconds,
|
|
642
|
+
actualLoad: values.actualLoad,
|
|
643
|
+
rpe: values.rpe,
|
|
644
|
+
...(input.note !== undefined ? { note: input.note } : {}),
|
|
645
|
+
},
|
|
646
|
+
}));
|
|
647
|
+
};
|
|
648
|
+
const logged = await again();
|
|
649
|
+
if (logged)
|
|
650
|
+
return logged;
|
|
594
651
|
const caller = ctx.caller?.kind === "staff" ? ctx.caller : null;
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
652
|
+
try {
|
|
653
|
+
if (isCustomer(ctx)) {
|
|
654
|
+
// The host makes it the caller's, and refuses a session they cannot see (ADR 0447).
|
|
655
|
+
return loggedOf(await ctx.ops.call("my.create", { object: LOG, values }));
|
|
656
|
+
}
|
|
657
|
+
return loggedOf(await ctx.ops.call("record.create", {
|
|
658
|
+
object: LOG,
|
|
659
|
+
values: {
|
|
660
|
+
...values,
|
|
661
|
+
person,
|
|
662
|
+
...(caller ? { recordedBy: caller.userId, recordedByName: caller.name } : {}),
|
|
663
|
+
},
|
|
664
|
+
}));
|
|
665
|
+
}
|
|
666
|
+
catch (error) {
|
|
667
|
+
// Two saves of one set at once: the database kept the first (`unique` on the log); this
|
|
668
|
+
// one changes it.
|
|
669
|
+
if (!(error instanceof OpsCallError) || error.code !== "conflict")
|
|
670
|
+
throw error;
|
|
671
|
+
const changed = await again();
|
|
672
|
+
if (!changed)
|
|
673
|
+
throw error;
|
|
674
|
+
return changed;
|
|
675
|
+
}
|
|
603
676
|
},
|
|
604
677
|
});
|
|
605
678
|
export const logUpdate = defineTool({
|
|
@@ -614,6 +687,7 @@ export const logUpdate = defineTool({
|
|
|
614
687
|
output: loggedSet,
|
|
615
688
|
risk: "write",
|
|
616
689
|
audiences: ["staff", "customer"],
|
|
690
|
+
permission: COACH,
|
|
617
691
|
async handler(input, ctx) {
|
|
618
692
|
const values = {
|
|
619
693
|
...(input.actual?.reps !== undefined ? { actualReps: input.actual.reps } : {}),
|
|
@@ -632,6 +706,60 @@ export const logUpdate = defineTool({
|
|
|
632
706
|
}));
|
|
633
707
|
},
|
|
634
708
|
});
|
|
709
|
+
export const logRemove = defineTool({
|
|
710
|
+
description: "Take back a set logged by mistake (un-tick it): the set with this number of a session's " +
|
|
711
|
+
"item and exercise. A member takes back their own; a coach a client's (on a class session, " +
|
|
712
|
+
"naming them). A set that is not logged changes nothing, so a retried call is safe. The " +
|
|
713
|
+
"row goes to the Trash, where staff can restore it. For staff the Trash is an " +
|
|
714
|
+
"extension's delete, so the org's approval policy may hold it for a person: `waiting` " +
|
|
715
|
+
"counts those. Returns how many rows were taken back.",
|
|
716
|
+
input: z.object({
|
|
717
|
+
sessionId: id,
|
|
718
|
+
item: z.string().min(1).max(6),
|
|
719
|
+
exerciseId: id,
|
|
720
|
+
setNumber: z.number().int().min(1).max(30),
|
|
721
|
+
personId: personInput,
|
|
722
|
+
}),
|
|
723
|
+
output: z.object({
|
|
724
|
+
removed: z.number().int(),
|
|
725
|
+
waiting: z.number().int().describe("Staff: rows whose removal waits for a person's approval"),
|
|
726
|
+
}),
|
|
727
|
+
risk: "write",
|
|
728
|
+
audiences: ["staff", "customer"],
|
|
729
|
+
permission: COACH,
|
|
730
|
+
async handler(input, ctx) {
|
|
731
|
+
// A session the caller may not read is not found, as for logging on it.
|
|
732
|
+
const record = await get(ctx, SESSION, input.sessionId);
|
|
733
|
+
const person = sessionPerson(ctx, record, input.personId);
|
|
734
|
+
if (!isCustomer(ctx) && !person) {
|
|
735
|
+
throw new OpsCallError("invalid_input", "A class session: name the person (personId)");
|
|
736
|
+
}
|
|
737
|
+
const rows = await setRows(ctx, {
|
|
738
|
+
sessionId: input.sessionId,
|
|
739
|
+
item: input.item,
|
|
740
|
+
exercise: input.exerciseId,
|
|
741
|
+
setNumber: input.setNumber,
|
|
742
|
+
}, person);
|
|
743
|
+
let removed = 0;
|
|
744
|
+
let waiting = 0;
|
|
745
|
+
for (const row of rows) {
|
|
746
|
+
try {
|
|
747
|
+
await ctx.ops.call(isCustomer(ctx) ? "my.delete" : "record.delete", {
|
|
748
|
+
object: LOG,
|
|
749
|
+
id: row.id,
|
|
750
|
+
});
|
|
751
|
+
removed += 1;
|
|
752
|
+
}
|
|
753
|
+
catch (error) {
|
|
754
|
+
// The approval policy holds an extension's delete for a person: it is queued, not lost.
|
|
755
|
+
if (!(error instanceof OpsCallError) || error.code !== "pending_approval")
|
|
756
|
+
throw error;
|
|
757
|
+
waiting += 1;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return { removed, waiting };
|
|
761
|
+
},
|
|
762
|
+
});
|
|
635
763
|
export const sessionComplete = defineTool({
|
|
636
764
|
description: "Mark one of a person's sessions done or skipped (or back to planned). A member marks their " +
|
|
637
765
|
"own; staff any personal session. A class type's sessions are not marked by members. Made " +
|
|
@@ -643,6 +771,7 @@ export const sessionComplete = defineTool({
|
|
|
643
771
|
output: z.object({ changed: z.boolean(), session: sessionOut }),
|
|
644
772
|
risk: "write",
|
|
645
773
|
audiences: ["staff", "customer"],
|
|
774
|
+
permission: COACH,
|
|
646
775
|
customerFields: { [SESSION]: ["state", "completedAt"] },
|
|
647
776
|
async handler(input, ctx) {
|
|
648
777
|
const current = sessionOf(await get(ctx, SESSION, input.sessionId));
|
|
@@ -709,6 +838,7 @@ export const history = defineTool({
|
|
|
709
838
|
}),
|
|
710
839
|
risk: "read",
|
|
711
840
|
audiences: ["staff", "customer"],
|
|
841
|
+
permission: COACH,
|
|
712
842
|
async handler(input, ctx) {
|
|
713
843
|
const person = personFilter(ctx, input.personId);
|
|
714
844
|
const exercise = await get(ctx, EXERCISE, input.exerciseId);
|
|
@@ -748,6 +878,7 @@ export const progress = defineTool({
|
|
|
748
878
|
}),
|
|
749
879
|
risk: "read",
|
|
750
880
|
audiences: ["staff", "customer"],
|
|
881
|
+
permission: COACH,
|
|
751
882
|
async handler(input, ctx) {
|
|
752
883
|
const person = personFilter(ctx, input.personId);
|
|
753
884
|
const only = input.assignmentId
|
|
@@ -885,40 +1016,94 @@ export const draft = defineTool({
|
|
|
885
1016
|
};
|
|
886
1017
|
},
|
|
887
1018
|
});
|
|
1019
|
+
/** JSON with every object's keys in order: the same content gives the same text. */
|
|
1020
|
+
function canonical(value) {
|
|
1021
|
+
if (Array.isArray(value))
|
|
1022
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
1023
|
+
if (value && typeof value === "object") {
|
|
1024
|
+
const entries = Object.entries(value)
|
|
1025
|
+
.filter(([, v]) => v !== undefined && v !== null)
|
|
1026
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
1027
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
1028
|
+
}
|
|
1029
|
+
return JSON.stringify(value ?? null);
|
|
1030
|
+
}
|
|
1031
|
+
/** Two 53-bit string hashes (cyrb53) as hex: whether a program still holds what was written. */
|
|
1032
|
+
function contentHash(text) {
|
|
1033
|
+
const one = (seed) => {
|
|
1034
|
+
let h1 = 0xdeadbeef ^ seed;
|
|
1035
|
+
let h2 = 0x41c6ce57 ^ seed;
|
|
1036
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
1037
|
+
const c = text.charCodeAt(i);
|
|
1038
|
+
h1 = Math.imul(h1 ^ c, 2654435761);
|
|
1039
|
+
h2 = Math.imul(h2 ^ c, 1597334677);
|
|
1040
|
+
}
|
|
1041
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
1042
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
1043
|
+
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16).padStart(14, "0");
|
|
1044
|
+
};
|
|
1045
|
+
return `${one(1)}${one(2)}`;
|
|
1046
|
+
}
|
|
1047
|
+
/** What a text import writes of a program, as one hash: what it last wrote, kept beside it. */
|
|
1048
|
+
function importedHashOf(values) {
|
|
1049
|
+
return contentHash(canonical({
|
|
1050
|
+
title: values.title,
|
|
1051
|
+
kind: values.kind,
|
|
1052
|
+
guidance: values.guidance,
|
|
1053
|
+
layout: values.layout,
|
|
1054
|
+
}));
|
|
1055
|
+
}
|
|
888
1056
|
export const importText = defineTool({
|
|
889
1057
|
description: "Turn pasted program shorthand, or a document's text, into a working copy with structured " +
|
|
890
|
-
"targets: Title:, Guidance:, Week:, Session: (a weekday names its day), Focus:,
|
|
891
|
-
"with - lines, Block A: with its rounds, items A1: with an exercise (for everyone)
|
|
892
|
-
'level lines 1: to 5: ("+" done together, "|" alternatives), * notes for
|
|
893
|
-
"notes shown to members. Exercises are matched to the bank by name. Every
|
|
894
|
-
"read is listed with why, for the coach to fix; nothing is guessed.
|
|
895
|
-
"the
|
|
1058
|
+
"targets: Title:, Kind:, Guidance:, Week:, Session: (a weekday names its day), Focus:, " +
|
|
1059
|
+
"Warm-up: with - lines, Block A: with its rounds, items A1: with an exercise (for everyone), " +
|
|
1060
|
+
'with level lines 1: to 5: ("+" done together, "|" alternatives), or with both, * notes for ' +
|
|
1061
|
+
"coaches and TV: notes shown to members. Exercises are matched to the bank by name. Every " +
|
|
1062
|
+
"line it could not read is listed with why, for the coach to fix; nothing is guessed. With " +
|
|
1063
|
+
"a key (the text's name in its source), running it again changes the same program: updated " +
|
|
1064
|
+
"while it still holds what the import last wrote, unchanged when the text is the same, and a " +
|
|
1065
|
+
"conflict, left alone, when someone has edited it since. dryRun reads and checks the text " +
|
|
1066
|
+
"and says what would happen, writing nothing. Never publishes. Returns the program and the " +
|
|
1067
|
+
"unread lines.",
|
|
896
1068
|
input: z.object({
|
|
897
1069
|
text: z.string().min(1).max(200_000),
|
|
898
1070
|
title: z.string().trim().min(1).max(200).optional(),
|
|
899
|
-
kind: z
|
|
1071
|
+
kind: z
|
|
1072
|
+
.enum(["class", "client", "course"])
|
|
1073
|
+
.optional()
|
|
1074
|
+
.describe("Left out: what a Kind: line says, else class"),
|
|
1075
|
+
key: z
|
|
1076
|
+
.string()
|
|
1077
|
+
.trim()
|
|
1078
|
+
.min(1)
|
|
1079
|
+
.max(200)
|
|
1080
|
+
.optional()
|
|
1081
|
+
.describe("The text's import key (its file name in the source): one program per key"),
|
|
1082
|
+
dryRun: z.boolean().default(false).describe("Read and check only; write nothing"),
|
|
900
1083
|
}),
|
|
901
1084
|
output: workingCopy.extend({
|
|
1085
|
+
programId: z.string().nullable().describe("Null only for a dry run that would create one"),
|
|
1086
|
+
outcome: z.enum(["created", "updated", "unchanged", "conflict", "invalid"]),
|
|
902
1087
|
items: z.number(),
|
|
903
1088
|
unread: z.array(z.object({ line: z.number(), text: z.string(), reason: z.string() })),
|
|
1089
|
+
faults: z
|
|
1090
|
+
.array(z.object({ path: z.string(), message: z.string() }))
|
|
1091
|
+
.describe("Where the layout breaks the schema (a dry run's check; a write is refused)"),
|
|
904
1092
|
}),
|
|
905
1093
|
risk: "write",
|
|
906
1094
|
async handler(input, ctx) {
|
|
907
1095
|
const bank = await bankOf(ctx);
|
|
908
1096
|
const read = readProgramText(input.text, bank);
|
|
909
1097
|
const title = input.title ?? read.title ?? "Imported program";
|
|
910
|
-
const
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
},
|
|
918
|
-
});
|
|
1098
|
+
const values = {
|
|
1099
|
+
title,
|
|
1100
|
+
kind: input.kind ?? read.kind ?? "class",
|
|
1101
|
+
// Null when the text has none, so an update clears what an earlier text said.
|
|
1102
|
+
guidance: read.guidance,
|
|
1103
|
+
layout: read.layout,
|
|
1104
|
+
};
|
|
919
1105
|
const sessions = read.layout.weeks.flatMap((w) => w.sessions);
|
|
920
|
-
|
|
921
|
-
programId: created.id,
|
|
1106
|
+
const summary = {
|
|
922
1107
|
title,
|
|
923
1108
|
weeks: read.layout.weeks.length,
|
|
924
1109
|
sessions: sessions.length,
|
|
@@ -926,5 +1111,52 @@ export const importText = defineTool({
|
|
|
926
1111
|
published: false,
|
|
927
1112
|
unread: read.unread,
|
|
928
1113
|
};
|
|
1114
|
+
const checked = validateDocument(LAYOUT_SCHEMA, read.layout);
|
|
1115
|
+
const faults = checked.ok
|
|
1116
|
+
? []
|
|
1117
|
+
: checked.issues.map((i) => ({ path: ["layout", ...i.path].join("."), message: i.message }));
|
|
1118
|
+
const hash = importedHashOf(values);
|
|
1119
|
+
const existing = input.key
|
|
1120
|
+
? (await ctx.ops.call("record.list", {
|
|
1121
|
+
object: PROGRAM,
|
|
1122
|
+
filter: { field: "importKey", op: "eq", value: input.key },
|
|
1123
|
+
limit: 1,
|
|
1124
|
+
})).items[0]
|
|
1125
|
+
: undefined;
|
|
1126
|
+
// Someone edited it since the import wrote it: left alone (a save between this read and the
|
|
1127
|
+
// update below is not seen: record.update has no version guard, ADR 0448).
|
|
1128
|
+
const edited = existing !== undefined &&
|
|
1129
|
+
importedHashOf(existing.values) !== text(existing.values.importedHash);
|
|
1130
|
+
const outcome = existing
|
|
1131
|
+
? edited
|
|
1132
|
+
? "conflict"
|
|
1133
|
+
: hash === text(existing.values.importedHash)
|
|
1134
|
+
? "unchanged"
|
|
1135
|
+
: "updated"
|
|
1136
|
+
: "created";
|
|
1137
|
+
const answer = (programId, said) => ({
|
|
1138
|
+
...summary,
|
|
1139
|
+
programId,
|
|
1140
|
+
outcome: said,
|
|
1141
|
+
faults,
|
|
1142
|
+
});
|
|
1143
|
+
if (input.dryRun)
|
|
1144
|
+
return answer(existing?.id ?? null, faults.length ? "invalid" : outcome);
|
|
1145
|
+
if (existing && outcome !== "updated")
|
|
1146
|
+
return answer(existing.id, outcome);
|
|
1147
|
+
const imported = input.key ? { importKey: input.key, importedHash: hash } : {};
|
|
1148
|
+
if (existing) {
|
|
1149
|
+
await ctx.ops.call("record.update", {
|
|
1150
|
+
object: PROGRAM,
|
|
1151
|
+
id: existing.id,
|
|
1152
|
+
values: { ...values, importedHash: hash },
|
|
1153
|
+
});
|
|
1154
|
+
return answer(existing.id, "updated");
|
|
1155
|
+
}
|
|
1156
|
+
const created = await ctx.ops.call("record.create", {
|
|
1157
|
+
object: PROGRAM,
|
|
1158
|
+
values: { ...values, ...imported },
|
|
1159
|
+
});
|
|
1160
|
+
return answer(created.id, "created");
|
|
929
1161
|
},
|
|
930
1162
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composable-os/pack-programs",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.13",
|
|
4
4
|
"license": "FSL-1.1-ALv2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"description": "Training programs built from one exercise bank, published as versions, assigned to clients and class types as dated sessions, and logged set by set against their targets."
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@composable-os/client": "0.1.0-beta.
|
|
36
|
-
"@composable-os/sdk": "0.1.0-beta.
|
|
35
|
+
"@composable-os/client": "0.1.0-beta.13",
|
|
36
|
+
"@composable-os/sdk": "0.1.0-beta.13"
|
|
37
37
|
}
|
|
38
38
|
}
|