@otea/cli 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.
Files changed (243) hide show
  1. package/LICENSE +661 -0
  2. package/README.es.md +151 -0
  3. package/README.md +252 -2
  4. package/THIRD_PARTY_NOTICES.md +114 -0
  5. package/bin/otea.mjs +28 -0
  6. package/dist/app/agent-detail.js +26 -0
  7. package/dist/app/agent-draft.js +429 -0
  8. package/dist/app/agent-prompts.js +52 -0
  9. package/dist/app/agents.js +77 -0
  10. package/dist/app/asking.js +43 -0
  11. package/dist/app/board-archive.js +243 -0
  12. package/dist/app/board-migration.js +272 -0
  13. package/dist/app/boards.js +870 -0
  14. package/dist/app/branch-owner.js +48 -0
  15. package/dist/app/brief.js +53 -0
  16. package/dist/app/choices.js +61 -0
  17. package/dist/app/collections.js +101 -0
  18. package/dist/app/context.js +1 -0
  19. package/dist/app/crop.js +56 -0
  20. package/dist/app/devices.js +62 -0
  21. package/dist/app/draft-request.js +82 -0
  22. package/dist/app/events.js +12 -0
  23. package/dist/app/evidence.js +287 -0
  24. package/dist/app/gates.js +610 -0
  25. package/dist/app/guide.js +37 -0
  26. package/dist/app/home.js +145 -0
  27. package/dist/app/init-boards.js +36 -0
  28. package/dist/app/intake.js +25 -0
  29. package/dist/app/item-search.js +141 -0
  30. package/dist/app/items.js +318 -0
  31. package/dist/app/keep-waiting.js +44 -0
  32. package/dist/app/land-order.js +107 -0
  33. package/dist/app/language.js +52 -0
  34. package/dist/app/launcher.js +56 -0
  35. package/dist/app/lease-take.js +54 -0
  36. package/dist/app/leases.js +86 -0
  37. package/dist/app/machine.js +39 -0
  38. package/dist/app/merged.js +89 -0
  39. package/dist/app/migrate.js +171 -0
  40. package/dist/app/next.js +1637 -0
  41. package/dist/app/notes.js +319 -0
  42. package/dist/app/notify-targets.js +113 -0
  43. package/dist/app/opened.js +25 -0
  44. package/dist/app/owner-only.js +34 -0
  45. package/dist/app/performance.js +28 -0
  46. package/dist/app/phone.js +97 -0
  47. package/dist/app/presence.js +596 -0
  48. package/dist/app/preset-views.js +25 -0
  49. package/dist/app/presets.js +129 -0
  50. package/dist/app/priority.js +156 -0
  51. package/dist/app/profile.js +86 -0
  52. package/dist/app/project.js +247 -0
  53. package/dist/app/proofs.js +179 -0
  54. package/dist/app/questions.js +215 -0
  55. package/dist/app/reader-filter.js +58 -0
  56. package/dist/app/relations.js +164 -0
  57. package/dist/app/relay.js +98 -0
  58. package/dist/app/repos.js +176 -0
  59. package/dist/app/restart.js +92 -0
  60. package/dist/app/set-aside.js +64 -0
  61. package/dist/app/system-locale.js +6 -0
  62. package/dist/app/tailscale-cli.js +77 -0
  63. package/dist/app/tailscale-testing.js +41 -0
  64. package/dist/app/tailscale.js +177 -0
  65. package/dist/app/tips.js +30 -0
  66. package/dist/app/topics-everywhere.js +53 -0
  67. package/dist/app/topics.js +453 -0
  68. package/dist/app/undo.js +52 -0
  69. package/dist/app/validate.js +131 -0
  70. package/dist/app/watch.js +48 -0
  71. package/dist/cli/command.js +44 -0
  72. package/dist/cli/commands.js +3303 -0
  73. package/dist/cli/exit-codes.js +70 -0
  74. package/dist/cli/forge.js +176 -0
  75. package/dist/cli/format.js +340 -0
  76. package/dist/cli/gh.js +17 -0
  77. package/dist/cli/gitrepo.js +489 -0
  78. package/dist/cli/help.js +101 -0
  79. package/dist/cli/hooks.js +232 -0
  80. package/dist/cli/main.js +37 -0
  81. package/dist/cli/merged-outside.js +58 -0
  82. package/dist/cli/names.js +154 -0
  83. package/dist/cli/opener.js +21 -0
  84. package/dist/cli/owner.js +154 -0
  85. package/dist/cli/phone.js +221 -0
  86. package/dist/cli/ports.js +24 -0
  87. package/dist/cli/probe.js +18 -0
  88. package/dist/cli/run.js +246 -0
  89. package/dist/cli/serve-file.js +21 -0
  90. package/dist/cli/stats.js +55 -0
  91. package/dist/cli/version.js +3 -0
  92. package/dist/core/agent-guide.js +31 -0
  93. package/dist/core/agent-prompts.js +139 -0
  94. package/dist/core/authority.js +94 -0
  95. package/dist/core/board/analysis.js +86 -0
  96. package/dist/core/board/choices.js +100 -0
  97. package/dist/core/board/compile.js +362 -0
  98. package/dist/core/board/engine.js +523 -0
  99. package/dist/core/board/hints.js +46 -0
  100. package/dist/core/board/history.js +146 -0
  101. package/dist/core/board/ids.js +87 -0
  102. package/dist/core/board/joins.js +30 -0
  103. package/dist/core/board/mentions.js +68 -0
  104. package/dist/core/board/merged.js +18 -0
  105. package/dist/core/board/model.js +135 -0
  106. package/dist/core/board/next.js +1116 -0
  107. package/dist/core/board/notes.js +298 -0
  108. package/dist/core/board/performance.js +218 -0
  109. package/dist/core/board/plan.js +299 -0
  110. package/dist/core/board/problems.js +232 -0
  111. package/dist/core/board/proofs.js +84 -0
  112. package/dist/core/board/questions.js +114 -0
  113. package/dist/core/board/reading.js +220 -0
  114. package/dist/core/board/readings.js +534 -0
  115. package/dist/core/board/schema.js +335 -0
  116. package/dist/core/board/shape-words.js +179 -0
  117. package/dist/core/board/template-words.js +23 -0
  118. package/dist/core/board/templates-catalogue.js +965 -0
  119. package/dist/core/board/templates-coverage.js +261 -0
  120. package/dist/core/board/templates-flows.js +779 -0
  121. package/dist/core/board/templates.js +586 -0
  122. package/dist/core/brief.js +93 -0
  123. package/dist/core/diagram.js +80 -0
  124. package/dist/core/errors.js +12 -0
  125. package/dist/core/evidence.js +33 -0
  126. package/dist/core/fields.js +129 -0
  127. package/dist/core/hierarchy.js +30 -0
  128. package/dist/core/icons.js +181 -0
  129. package/dist/core/image-crop.js +166 -0
  130. package/dist/core/item.js +49 -0
  131. package/dist/core/lease.js +31 -0
  132. package/dist/core/modules.js +318 -0
  133. package/dist/core/notify/planner.js +117 -0
  134. package/dist/core/notify/signals.js +66 -0
  135. package/dist/core/notify/text.js +60 -0
  136. package/dist/core/path-key.js +22 -0
  137. package/dist/core/placement.js +161 -0
  138. package/dist/core/preset-labels.js +66 -0
  139. package/dist/core/preset-notes.js +109 -0
  140. package/dist/core/preset.js +541 -0
  141. package/dist/core/profile.js +55 -0
  142. package/dist/core/project-id.js +20 -0
  143. package/dist/core/refusal.js +1 -0
  144. package/dist/core/relations.js +208 -0
  145. package/dist/core/repo.js +54 -0
  146. package/dist/core/samples.js +161 -0
  147. package/dist/core/search-words.js +68 -0
  148. package/dist/core/test-report.js +120 -0
  149. package/dist/core/time.js +8 -0
  150. package/dist/core/validation.js +60 -0
  151. package/dist/core/views.js +171 -0
  152. package/dist/core/workflow.js +51 -0
  153. package/dist/mcp/commands.js +101 -0
  154. package/dist/mcp/server.js +239 -0
  155. package/dist/mcp/words.js +95 -0
  156. package/dist/server/boards.js +232 -0
  157. package/dist/server/changes.js +42 -0
  158. package/dist/server/diff-tip.js +83 -0
  159. package/dist/server/drift.js +87 -0
  160. package/dist/server/errors.js +87 -0
  161. package/dist/server/evidence.js +82 -0
  162. package/dist/server/hosts.js +6 -0
  163. package/dist/server/hub.js +98 -0
  164. package/dist/server/lan-access.js +62 -0
  165. package/dist/server/lan.js +164 -0
  166. package/dist/server/listen.js +70 -0
  167. package/dist/server/notifier.js +156 -0
  168. package/dist/server/notify-api.js +151 -0
  169. package/dist/server/pairing.js +124 -0
  170. package/dist/server/projects.js +191 -0
  171. package/dist/server/push.js +199 -0
  172. package/dist/server/server.js +960 -0
  173. package/dist/server/session.js +43 -0
  174. package/dist/server/stream.js +36 -0
  175. package/dist/server/test-push.js +38 -0
  176. package/dist/server/token.js +37 -0
  177. package/dist/server/transports.js +54 -0
  178. package/dist/server/ui.js +44 -0
  179. package/dist/server/writes.js +361 -0
  180. package/dist/server/wrong-address.js +36 -0
  181. package/dist/store/boards.js +111 -0
  182. package/dist/store/database.js +45 -0
  183. package/dist/store/machine.js +77 -0
  184. package/dist/store/migrations.js +493 -0
  185. package/dist/store/queries.js +585 -0
  186. package/dist/store/registry.js +72 -0
  187. package/package.json +73 -4
  188. package/presets/architecture/playbook.md +69 -0
  189. package/presets/architecture/preset.yaml +80 -0
  190. package/presets/architecture/samples.yaml +23 -0
  191. package/presets/bug-triage/playbook.md +129 -0
  192. package/presets/bug-triage/preset.yaml +127 -0
  193. package/presets/bug-triage/samples/wall-slide-after.png +0 -0
  194. package/presets/bug-triage/samples/wall-slide-before.png +0 -0
  195. package/presets/bug-triage/samples.yaml +72 -0
  196. package/presets/change/playbook.md +115 -0
  197. package/presets/change/preset.yaml +141 -0
  198. package/presets/change/samples.yaml +62 -0
  199. package/presets/review/playbook.md +51 -0
  200. package/presets/review/preset.yaml +62 -0
  201. package/presets/review/samples.yaml +27 -0
  202. package/presets/tasks/playbook.md +78 -0
  203. package/presets/tasks/preset.yaml +61 -0
  204. package/presets/tasks/samples.yaml +46 -0
  205. package/schemas/board.schema.json +764 -0
  206. package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
  207. package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
  208. package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
  209. package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
  210. package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
  211. package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
  212. package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
  213. package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
  214. package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
  215. package/ui/dist/assets/index-C3OzJope.js +103 -0
  216. package/ui/dist/assets/index-COdEjckQ.css +1 -0
  217. package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
  218. package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
  219. package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
  220. package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
  221. package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
  222. package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
  223. package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
  224. package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
  225. package/ui/dist/assets/pdf-rELvHkft.js +56 -0
  226. package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
  227. package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
  228. package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
  229. package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
  230. package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
  231. package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
  232. package/ui/dist/icons/apple-touch-icon.png +0 -0
  233. package/ui/dist/icons/otea-192.png +0 -0
  234. package/ui/dist/icons/otea-512.png +0 -0
  235. package/ui/dist/icons/otea-badge.png +0 -0
  236. package/ui/dist/icons/otea-maskable-512.png +0 -0
  237. package/ui/dist/index.html +25 -0
  238. package/ui/dist/manifest.webmanifest +24 -0
  239. package/ui/dist/otea-idle.svg +5 -0
  240. package/ui/dist/otea-working.svg +5 -0
  241. package/ui/dist/otea-yours-working.svg +5 -0
  242. package/ui/dist/otea-yours.svg +5 -0
  243. package/ui/dist/sw.js +1 -0
@@ -0,0 +1,161 @@
1
+ import { OteaError } from './errors.js';
2
+ import { evidenceTypeOf } from './evidence.js';
3
+ import { MODULE_REGISTRY } from './modules.js';
4
+ // Where evidence modules may sit, in the order attach and show read them.
5
+ const EVIDENCE_SLOTS = ['evidence', 'body', 'side', 'phone'];
6
+ export function evidenceModules(layout) {
7
+ const isModule = (entry) => MODULE_REGISTRY[entry.module].family === 'evidence' && entry.id !== undefined;
8
+ const sectioned = (layout.sections ?? []).flatMap(({ entries }) => entries.filter(isModule));
9
+ return EVIDENCE_SLOTS.flatMap((slot) => [
10
+ ...layout[slot].filter(isModule),
11
+ ...(slot === 'body' ? sectioned : []),
12
+ ]);
13
+ }
14
+ function extensionOf(filename) {
15
+ const dot = filename.lastIndexOf('.');
16
+ return dot < 0 ? '' : filename.slice(dot + 1).toLowerCase();
17
+ }
18
+ // The extensions a test report comes as.
19
+ const REPORTS = new Set(['xml', 'json']);
20
+ // What a module takes: its `accepts` (kinds or extensions), or every kind its family draws.
21
+ function takes(entry) {
22
+ return entry.accepts ?? MODULE_REGISTRY[entry.module].accepts ?? [];
23
+ }
24
+ export function moduleAccepts(entry, filename) {
25
+ const { kind } = evidenceTypeOf(filename);
26
+ const accepted = takes(entry);
27
+ // A test run's report (JUnit XML, vitest or jest JSON) is test output: a log module takes it, and
28
+ // reads it as its results.
29
+ if (kind === 'file' && accepted.includes('log') && REPORTS.has(extensionOf(filename)))
30
+ return true;
31
+ // An opaque file goes only to a module that takes files as such (files), never one that draws.
32
+ if (kind === 'file')
33
+ return accepted.includes('file');
34
+ return accepted.includes(kind) || accepted.includes(extensionOf(filename));
35
+ }
36
+ // A module as a reader knows it: its label (a board part's name), or its id.
37
+ function quoted(entry) {
38
+ return `"${entry.label ?? entry.id}"`;
39
+ }
40
+ function named(entry) {
41
+ const variant = entry.variant === undefined ? '' : `, ${entry.variant}`;
42
+ return `${quoted(entry)} (${entry.module}${variant})`;
43
+ }
44
+ // A text file refused by a module that takes diffs: a diff is known by its extension alone, so a
45
+ // diff saved as .txt reads as a log (P8-R3-5). The refusal says how to name it.
46
+ function asDiff(entry, filename) {
47
+ const accepted = takes(entry);
48
+ if (!accepted.includes('diff') || evidenceTypeOf(filename).kind !== 'log')
49
+ return '';
50
+ return `: a diff is known by its extension, so save it as ${diffName(filename)} (or .patch) and attach that`;
51
+ }
52
+ // The file's name as a .diff.
53
+ function diffName(filename) {
54
+ const dot = filename.lastIndexOf('.');
55
+ return `${dot < 0 ? filename : filename.slice(0, dot)}.diff`;
56
+ }
57
+ // Whether a text reads as a unified diff: git's header, or a file pair followed by a hunk.
58
+ export function readsAsDiff(text) {
59
+ return /^diff --git \S/m.test(text) || /^--- \S.*\r?\n\+\+\+ \S.*\r?\n@@ -\d/m.test(text);
60
+ }
61
+ // A diff saved as a text file and attached without naming a part goes, by its extension, to a part
62
+ // that takes logs, though the layout has one that takes diffs (P8-R3-5): that part, to say so.
63
+ // Undefined for any other file, or when no part takes diffs.
64
+ export function diffPartFor(layout, filename, text) {
65
+ if (evidenceTypeOf(filename).kind !== 'log' || !readsAsDiff(text))
66
+ return undefined;
67
+ return evidenceModules(layout).find((entry) => moduleAccepts(entry, diffName(filename)));
68
+ }
69
+ // What Otea says of such a diff, with the commands that put it in the diff part. `went`: the part
70
+ // it went to, when it went to one.
71
+ export function diffPartText(id, filename, part, went) {
72
+ const named = (text) => (/^[\w.<>/-]+$/.test(text) ? text : `"${text}"`);
73
+ const target = part.label ?? part.id;
74
+ const where = went === undefined ? '' : `, but a .${extensionOf(filename)} goes to ${went}`;
75
+ return `"${filename}" reads as a unified diff${where}: ${target} takes a diff saved as .diff. Save it as "${diffName(filename)}" and run otea attach ${id} ${named(diffName(filename))} --module ${named(target)}`;
76
+ }
77
+ function isCompare(entry) {
78
+ return entry.module === 'images' && entry.variant === 'compare';
79
+ }
80
+ function chosen(modules, target, { filename, module, role }) {
81
+ const subject = `${target.id} (${target.type})`;
82
+ if (module !== undefined) {
83
+ const found = modules.find(({ id }) => id === module);
84
+ if (!found) {
85
+ const known = modules.length > 0
86
+ ? `modules: ${modules.map(({ id }) => id).join(', ')}`
87
+ : 'it places none';
88
+ throw new OteaError('not_found', `${subject} has no evidence module "${module}" (${known})`);
89
+ }
90
+ if (!moduleAccepts(found, filename)) {
91
+ throw new OteaError('evidence_not_accepted', `${named(found)} takes ${takes(found).join(', ')}, not ${filename}${asDiff(found, filename)}`);
92
+ }
93
+ return found;
94
+ }
95
+ const fitting = modules.filter((entry) => moduleAccepts(entry, filename));
96
+ const [only, ...more] = fitting;
97
+ if (!only) {
98
+ const list = modules.map((entry) => `${named(entry)}: ${takes(entry).join(', ')}`).join('; ');
99
+ throw new OteaError('evidence_not_accepted', `${subject} does not take ${filename}. It takes: ${list}`, { kind: 'modules', modules: modules.map(({ id }) => id) });
100
+ }
101
+ // A before or an after goes only to a compare: when one compare alone fits, the role names it.
102
+ const compares = fitting.filter(isCompare);
103
+ if (more.length > 0 && (role === 'before' || role === 'after') && compares.length === 1) {
104
+ const [compare] = compares;
105
+ if (compare)
106
+ return compare;
107
+ }
108
+ if (more.length > 0) {
109
+ // A board takes its evidence by name (--module "Vídeo del arreglo"), a preset by id.
110
+ const options = fitting
111
+ .map(({ id, label }) => `--module ${label === undefined ? id : `"${label}"`}`)
112
+ .join(' or ');
113
+ throw new OteaError('module_required', `${filename} fits more than one evidence module of ${target.id} (${target.type}): pass ${options}`, { kind: 'modules', modules: fitting.map(({ id }) => id) });
114
+ }
115
+ return only;
116
+ }
117
+ function fittingRole(entry, role) {
118
+ if (isCompare(entry)) {
119
+ if (role === 'before' || role === 'after')
120
+ return role;
121
+ throw new OteaError('invalid_role', `${named(entry)} compares a before and an after: pass --role before or --role after`);
122
+ }
123
+ if (role === 'before' || role === 'after') {
124
+ throw new OteaError('invalid_role', `${named(entry)} holds single files, not a before and an after: drop --role ${role}`);
125
+ }
126
+ return 'single';
127
+ }
128
+ // Which module of the item's layout a file goes to, and in which role. A type that places no evidence
129
+ // module takes any file, in any role, as a plain list did before layouts.
130
+ export function placeEvidence(layout, target, request) {
131
+ const modules = evidenceModules(layout);
132
+ if (modules.length === 0 && request.module === undefined) {
133
+ return { module: null, role: request.role ?? 'single' };
134
+ }
135
+ const entry = chosen(modules, target, request);
136
+ return { module: entry.id, role: fittingRole(entry, request.role) };
137
+ }
138
+ // The module that draws an evidence entry: the one it was attached to while the layout still places
139
+ // it and it still accepts the file, else the first module that accepts it (evidence attached before
140
+ // modules, or after a preset edit). null when no module accepts it. `keepLoose`, on a board: a file
141
+ // attached to no part (the person's, outside the parts the agents hand) stays outside them.
142
+ export function holderOf(layout, evidence, keepLoose = false) {
143
+ if (keepLoose && evidence.module === null)
144
+ return null;
145
+ const modules = evidenceModules(layout).filter((entry) => moduleAccepts(entry, evidence.filename));
146
+ const recorded = modules.find(({ id }) => id === evidence.module);
147
+ return (recorded ?? modules[0])?.id ?? null;
148
+ }
149
+ export function groupEvidence(layout, evidence, keepLoose = false) {
150
+ const modules = evidenceModules(layout).map((module) => ({ module, evidence: [] }));
151
+ const others = [];
152
+ for (const entry of evidence) {
153
+ const holder = holderOf(layout, entry, keepLoose);
154
+ const group = modules.find(({ module }) => module.id === holder);
155
+ if (group)
156
+ group.evidence.push(entry);
157
+ else
158
+ others.push(entry);
159
+ }
160
+ return { modules, others };
161
+ }
@@ -0,0 +1,66 @@
1
+ import { z } from 'zod';
2
+ const words = z.record(z.string(), z.string().trim().min(1, 'a label cannot be empty'));
3
+ export const labelsSchema = z.record(z.string().regex(/^[a-z]{2,3}$/, 'languages are two- or three-letter codes (es, pt)'), z.strictObject({
4
+ types: words.optional(),
5
+ states: words.optional(),
6
+ fields: words.optional(),
7
+ // The preset's own name in the language, for Studio.
8
+ name: z.string().trim().min(1, 'a label cannot be empty').optional(),
9
+ values: z.record(z.string(), words).optional(),
10
+ }));
11
+ const SINGULAR = { types: 'type', states: 'state', fields: 'field' };
12
+ function namesOf(types, kind) {
13
+ if (kind === 'types')
14
+ return types.map(({ name }) => name);
15
+ if (kind === 'fields')
16
+ return [...new Set(types.flatMap((type) => Object.keys(type.fields)))];
17
+ return [...new Set(types.flatMap((type) => type.workflow.states.map(({ name }) => name)))];
18
+ }
19
+ // `implicit`: the preset declares no types, so naming its only type is optional.
20
+ export function labelProblems(raw, types, implicit) {
21
+ return Object.entries(raw ?? {}).flatMap(([language, given]) => [
22
+ ...Object.keys(given.values ?? {})
23
+ .filter((name) => !namesOf(types, 'fields').includes(name))
24
+ .map((name) => `labels.${language}.values.${name}: unknown field "${name}"`),
25
+ ...['types', 'states', 'fields'].flatMap((kind) => {
26
+ const at = `labels.${language}.${kind}`;
27
+ const known = namesOf(types, kind);
28
+ const labelled = Object.keys(given[kind] ?? {});
29
+ const unknown = labelled
30
+ .filter((name) => !known.includes(name))
31
+ .map((name) => `${at}.${name}: unknown ${SINGULAR[kind]} "${name}"`);
32
+ const missing = kind === 'types' && implicit ? [] : known.filter((n) => !labelled.includes(n));
33
+ if (missing.length === 0)
34
+ return unknown;
35
+ return [
36
+ ...unknown,
37
+ `${at}: missing ${missing.join(', ')}; a language labels every type, state and field`,
38
+ ];
39
+ }),
40
+ ]);
41
+ }
42
+ // The preset's name in each language that gives one, or undefined.
43
+ export function presetNames(raw) {
44
+ const names = Object.entries(raw ?? {}).flatMap(([language, given]) => given.name ? [[language, given.name]] : []);
45
+ return names.length === 0 ? undefined : Object.fromEntries(names);
46
+ }
47
+ function pick(words, names) {
48
+ return Object.fromEntries(names.flatMap((name) => (words?.[name] ? [[name, words[name]]] : [])));
49
+ }
50
+ // A type's labels in each language, or undefined when its preset speaks only its own.
51
+ export function typeLabels(raw, type) {
52
+ const languages = Object.entries(raw ?? {});
53
+ if (languages.length === 0)
54
+ return undefined;
55
+ return Object.fromEntries(languages.map(([language, given]) => {
56
+ const labels = {
57
+ states: pick(given.states, type.workflow.states.map(({ name }) => name)),
58
+ fields: pick(given.fields, Object.keys(type.fields)),
59
+ };
60
+ const values = Object.fromEntries(Object.entries(given.values ?? {}).filter(([field]) => field in type.fields));
61
+ if (Object.keys(values).length > 0)
62
+ labels.values = values;
63
+ const label = given.types?.[type.name];
64
+ return [language, label === undefined ? labels : { label, ...labels }];
65
+ }));
66
+ }
@@ -0,0 +1,109 @@
1
+ // The slots a field can be seen in. evidence takes files, never a field.
2
+ const FIELD_SLOTS = ['header', 'body', 'side', 'row', 'intake', 'phone'];
3
+ // A field nobody sees: declared, but placed in no slot of the type's layout. The title is drawn by
4
+ // the header of every item, so it is never one.
5
+ function unplacedNotes(preset) {
6
+ const shared = preset.types.length > 1;
7
+ return preset.types.flatMap((type) => {
8
+ const sectioned = (type.layout.sections ?? []).flatMap(({ entries }) => entries.map((e) => e.field));
9
+ const placed = new Set([
10
+ ...FIELD_SLOTS.flatMap((slot) => type.layout[slot].map((e) => e.field)),
11
+ ...sectioned,
12
+ ]);
13
+ return Object.keys(type.fields)
14
+ .filter((field) => field !== 'title' && !placed.has(field))
15
+ .map((field) => {
16
+ const at = field in preset.fields ? 'fields' : `types.${type.name}.fields`;
17
+ const of = shared ? ` of ${type.name}` : '';
18
+ return {
19
+ code: 'field_unplaced',
20
+ level: 'info',
21
+ message: `${at}.${field}: placed in no layout slot${of} (${FIELD_SLOTS.join(', ')}), so nobody sees it`,
22
+ path: { section: 'fields', type: type.name, field },
23
+ };
24
+ });
25
+ });
26
+ }
27
+ // Each workflow once, with the type that owns it; the preset's own workflow has no type.
28
+ function workflowsOf(preset) {
29
+ const own = preset.types
30
+ .filter((type) => type.workflow !== preset.workflow)
31
+ .map((type) => ({ workflow: type.workflow, type }));
32
+ const shared = preset.types.some((type) => type.workflow === preset.workflow);
33
+ return [...(shared ? [{ workflow: preset.workflow }] : []), ...own];
34
+ }
35
+ function prefixOf(type) {
36
+ return type ? `types.${type.name}.workflow` : 'workflow';
37
+ }
38
+ function scoped(type) {
39
+ return type ? { type: type.name } : {};
40
+ }
41
+ // How many moves each state lies from the start (the initial or the incomplete state).
42
+ function depths(workflow) {
43
+ const entries = [workflow.initial, workflow.incomplete ?? workflow.initial];
44
+ const depth = new Map(entries.map((state) => [state, 0]));
45
+ const queue = [...new Set(entries)];
46
+ for (let state = queue.shift(); state !== undefined; state = queue.shift()) {
47
+ const next = (depth.get(state) ?? 0) + 1;
48
+ for (const { from, to } of workflow.transitions) {
49
+ if (from === state && !depth.has(to)) {
50
+ depth.set(to, next);
51
+ queue.push(to);
52
+ }
53
+ }
54
+ }
55
+ return depth;
56
+ }
57
+ // A human-turn state is stuck in chat when no decision relayed from chat moves it forward. Chat
58
+ // relays only `human` moves (relayableMoves); `human-ui` ones take the web UI. A move is forward
59
+ // when its target closes the item as done, or is still open and lies more moves from the start
60
+ // than the state itself. Going back, sideways or to a discarded close is not forward: from chat
61
+ // the item could then be sent back or thrown away, never advanced.
62
+ function stuckNotes(preset) {
63
+ return workflowsOf(preset).flatMap(({ workflow, type }) => {
64
+ const depth = depths(workflow);
65
+ const closes = new Map(workflow.states.map(({ name, closes }) => [name, closes]));
66
+ const forward = (from, to) => closes.get(to) === 'done' ||
67
+ (closes.get(to) === undefined && (depth.get(to) ?? 0) > (depth.get(from) ?? 0));
68
+ return workflow.states
69
+ .filter(({ turn }) => turn === 'human')
70
+ .flatMap(({ name }) => {
71
+ const exits = workflow.transitions.filter(({ from }) => from === name);
72
+ const relayed = exits.filter(({ by }) => by === 'human');
73
+ if (relayed.some(({ to }) => forward(name, to)))
74
+ return [];
75
+ const at = `${prefixOf(type)}.states.${name}`;
76
+ const targets = (list) => [...new Set(list.map(({ to }) => to))].join(', ');
77
+ const ahead = exits.filter(({ by, to }) => by === 'human-ui' && forward(name, to));
78
+ const message = relayed.length === 0
79
+ ? `${at}: turn human, but every way out is human-ui, so a decision relayed from chat cannot move it`
80
+ : `${at}: turn human, but a decision relayed from chat cannot move it forward, only to ${targets(relayed)}${ahead.length > 0 ? `; ${targets(ahead)} takes the web UI` : ''}`;
81
+ return [
82
+ {
83
+ code: 'state_stuck_in_chat',
84
+ level: 'warning',
85
+ message,
86
+ path: { section: 'states', ...scoped(type), state: name },
87
+ },
88
+ ];
89
+ });
90
+ });
91
+ }
92
+ // A field only one type of several has: fine when meant, but it reads as missing once an item
93
+ // switches type.
94
+ function singleTypeNotes(preset) {
95
+ if (preset.types.length < 2)
96
+ return [];
97
+ return preset.types.flatMap((type) => Object.keys(type.fields)
98
+ .filter((field) => preset.types.every((other) => other === type || !(field in other.fields)))
99
+ .map((field) => ({
100
+ code: 'field_single_type',
101
+ level: 'info',
102
+ message: `types.${type.name}.fields.${field}: only ${type.name} has it; it reads as missing when an item switches type`,
103
+ path: { section: 'fields', type: type.name, field },
104
+ })));
105
+ }
106
+ // The notes the preset's YAML alone shows.
107
+ export function presetNotes(preset) {
108
+ return [...unplacedNotes(preset), ...stuckNotes(preset), ...singleTypeNotes(preset)];
109
+ }