@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,31 @@
1
+ import { OteaError } from './errors.js';
2
+ import { timeText } from './time.js';
3
+ export const DEFAULT_LEASE_TTL_MS = 15 * 60_000;
4
+ const DURATION_PATTERN = /^([1-9][0-9]*)(s|m|h)$/;
5
+ const UNIT_MS = { s: 1000, m: 60_000, h: 3_600_000 };
6
+ export function parseDuration(text) {
7
+ const match = DURATION_PATTERN.exec(text);
8
+ if (!match?.[1] || !match[2])
9
+ return undefined;
10
+ return Number(match[1]) * (UNIT_MS[match[2]] ?? 0);
11
+ }
12
+ export function leaseExpiry(at, ttlMs) {
13
+ return new Date(Date.parse(at) + ttlMs).toISOString();
14
+ }
15
+ // Expiry is lazy: an expired lease stays stored and simply reads as free.
16
+ export function activeLease(lease, at) {
17
+ return lease && Date.parse(lease.expiresAt) > Date.parse(at) ? lease : null;
18
+ }
19
+ export function heldText(id, lease) {
20
+ return `${id} is held by ${lease.holder} until ${timeText(lease.expiresAt)}`;
21
+ }
22
+ // Another agent holds it: wait for it, or, when that agent ended (a subagent gone with the lease
23
+ // still on), the person frees it on the desk.
24
+ export function leaseHeld(id, lease) {
25
+ return new OteaError('lease_held', `${heldText(id, lease)}; if ${lease.holder} has ended, ask the person to free it in Otea (Liberar)`);
26
+ }
27
+ // Humans lead: only agents are bound by another actor's lease.
28
+ export function assertMayWrite(id, lease, actor) {
29
+ if (actor.kind === 'agent' && lease && lease.holder !== actor.name)
30
+ throw leaseHeld(id, lease);
31
+ }
@@ -0,0 +1,318 @@
1
+ import { z } from 'zod';
2
+ import { evidenceTypeOf } from './evidence.js';
3
+ export const FIELD_MODULES = ['text', 'markdown', 'enum', 'severity'];
4
+ // The severity module's fixed scale, from least to most severe: levels 1 to 4, in that order.
5
+ export const SEVERITY_SCALE = ['low', 'medium', 'high', 'critical'];
6
+ // `files` takes what no other module draws (audio, and any other file), offered for download.
7
+ export const EVIDENCE_MODULES = ['images', 'recording', 'diff', 'log', 'diagram', 'files'];
8
+ // Relation modules draw the item's typed links to other items (`relations`) and, for a parent, its
9
+ // children and how far they got (`children`).
10
+ export const RELATION_MODULES = ['relations', 'children'];
11
+ // Reserved modules validate in a layout today; their components come later and draw nothing yet.
12
+ export const RESERVED_MODULES = ['questions', 'rounds', 'score', 'options'];
13
+ // Where a type's layout places things: the item detail (header, evidence, body, side), the list row,
14
+ // the phone detail and the create form (intake).
15
+ export const LAYOUT_SLOTS = [
16
+ 'header',
17
+ 'evidence',
18
+ 'body',
19
+ 'side',
20
+ 'row',
21
+ 'phone',
22
+ 'intake',
23
+ ];
24
+ const label = z.string().min(1).optional();
25
+ const ID = /^[a-z][a-z0-9-]*$/;
26
+ const id = z
27
+ .string()
28
+ .regex(ID, 'ids are lowercase letters, digits and dashes, starting with a letter')
29
+ .optional();
30
+ const fieldConfig = z.strictObject({ module: z.string(), field: z.string(), label });
31
+ function evidenceConfig(variants) {
32
+ return z.strictObject({
33
+ module: z.string(),
34
+ id,
35
+ ...(variants ? { variant: z.enum(variants).optional() } : {}),
36
+ blind: z.boolean().optional(),
37
+ accepts: z.array(z.string()).nonempty().optional(),
38
+ required: z.boolean().optional(),
39
+ label,
40
+ });
41
+ }
42
+ const labelConfig = z.strictObject({ module: z.string(), label });
43
+ const FIELD_SLOTS = ['header', 'body', 'side', 'row', 'phone', 'intake'];
44
+ const EVIDENCE_SLOTS = ['evidence', 'body', 'side', 'phone'];
45
+ function field() {
46
+ return { family: 'field', slots: FIELD_SLOTS, config: fieldConfig };
47
+ }
48
+ function evidence(accepts, variants) {
49
+ const config = evidenceConfig(variants);
50
+ return { family: 'evidence', slots: EVIDENCE_SLOTS, config, accepts };
51
+ }
52
+ function relation(...slots) {
53
+ return { family: 'relation', slots, config: labelConfig };
54
+ }
55
+ function reserved(...slots) {
56
+ return { family: 'reserved', slots, config: labelConfig };
57
+ }
58
+ export const MODULE_REGISTRY = {
59
+ text: field(),
60
+ markdown: field(),
61
+ enum: field(),
62
+ severity: field(),
63
+ images: evidence(['image'], ['single', 'gallery', 'compare']),
64
+ recording: evidence(['video'], ['player', 'frames']),
65
+ diff: evidence(['diff']),
66
+ log: evidence(['log']),
67
+ relations: relation('body', 'side'),
68
+ children: relation('body', 'side'),
69
+ questions: reserved('body', 'side', 'phone'),
70
+ rounds: reserved('body', 'phone'),
71
+ score: reserved('header', 'side', 'row'),
72
+ options: reserved('body', 'phone'),
73
+ diagram: evidence(['diagram']),
74
+ files: evidence(['audio', 'file']),
75
+ };
76
+ function isModule(name) {
77
+ return Object.hasOwn(MODULE_REGISTRY, name);
78
+ }
79
+ export function moduleFamily(kind) {
80
+ return MODULE_REGISTRY[kind].family;
81
+ }
82
+ function fieldEntries(fields, skipTitle) {
83
+ return Object.entries(fields)
84
+ .filter(([name]) => !skipTitle || name !== 'title')
85
+ .map(([name, { module }]) => ({ module, field: name }));
86
+ }
87
+ // The layout of a type that declares none: what the UI drew before layouts existed. The detail shows
88
+ // every field in the body (the header already shows the title), then the children of a parent, so
89
+ // does the row, and the create form asks for every field. The side lists the item's relations. Both
90
+ // relation modules draw nothing while there is nothing to draw, so an item without links looks as
91
+ // it did before them.
92
+ export function defaultLayout(fields) {
93
+ return {
94
+ header: [],
95
+ evidence: [],
96
+ body: [...fieldEntries(fields, true), { module: 'children' }],
97
+ side: [{ module: 'relations' }],
98
+ row: fieldEntries(fields, true),
99
+ phone: [],
100
+ intake: fieldEntries(fields, false),
101
+ };
102
+ }
103
+ // What an `accepts` entry names: an evidence kind (`image`) or an extension (`png`, `.png`).
104
+ function acceptedKind(accept) {
105
+ const known = ['image', 'video', 'audio', 'diff', 'log', 'diagram', 'file'];
106
+ if (known.includes(accept))
107
+ return accept;
108
+ const { kind } = evidenceTypeOf(`file.${accept}`);
109
+ return kind === 'file' ? undefined : kind;
110
+ }
111
+ function acceptsProblems(entry, spec, at) {
112
+ const family = spec.accepts ?? [];
113
+ return (entry.accepts ?? []).flatMap((accept) => {
114
+ const kind = acceptedKind(accept);
115
+ if (kind === undefined)
116
+ return [`${at}.accepts: "${accept}" is not a known file kind or extension`];
117
+ if (family.includes(kind))
118
+ return [];
119
+ return [
120
+ `${at}.accepts: "${accept}" is a ${kind} file; the ${entry.module} module accepts ${family.join(', ')}`,
121
+ ];
122
+ });
123
+ }
124
+ // Evidence modules always carry their id, and their accepts in lowercase without the dot.
125
+ function normalized(entry, family) {
126
+ if (family !== 'evidence')
127
+ return entry;
128
+ const { module, id = module, ...rest } = entry;
129
+ const accepts = entry.accepts?.map((accept) => accept.toLowerCase().replace(/^\./, ''));
130
+ return { module, id, ...rest, ...(accepts ? { accepts } : {}) };
131
+ }
132
+ function readModule(value, fields, at) {
133
+ const { module } = value;
134
+ if (typeof module !== 'string' || !isModule(module)) {
135
+ return { problems: [`${at}.module: unknown module "${String(module)}"`] };
136
+ }
137
+ const spec = MODULE_REGISTRY[module];
138
+ const result = spec.config.safeParse(value);
139
+ if (!result.success) {
140
+ const problems = result.error.issues.map((issue) => `${[at, ...issue.path.map(String)].join('.')}: ${issue.message}`);
141
+ return { problems };
142
+ }
143
+ const entry = normalized(result.data, spec.family);
144
+ if (spec.family === 'field') {
145
+ const name = entry.field ?? '';
146
+ const declared = fields[name];
147
+ if (!declared)
148
+ return { problems: [`${at}.field: unknown field "${name}"`] };
149
+ if (declared.module !== module) {
150
+ return {
151
+ problems: [
152
+ `${at}.module: field "${name}" uses the ${declared.module} module, not ${module}`,
153
+ ],
154
+ };
155
+ }
156
+ }
157
+ return { entry, problems: acceptsProblems(entry, spec, at) };
158
+ }
159
+ function readEntry(value, fields, at) {
160
+ if (typeof value === 'string') {
161
+ const declared = fields[value];
162
+ if (!declared)
163
+ return { problems: [`${at}: unknown field "${value}"`] };
164
+ return { entry: { module: declared.module, field: value }, problems: [] };
165
+ }
166
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
167
+ return readModule(value, fields, at);
168
+ }
169
+ return { problems: [`${at}: must be a field name or { module: <kind>, ... }`] };
170
+ }
171
+ function idProblems(entry, ids, where, place) {
172
+ if (entry.id === undefined)
173
+ return [];
174
+ const first = ids.get(entry.id);
175
+ if (first === undefined) {
176
+ ids.set(entry.id, place);
177
+ return [];
178
+ }
179
+ return [
180
+ `${where}.id: "${entry.id}" already names ${first}; give each ${entry.module} module its own id`,
181
+ ];
182
+ }
183
+ function readSlot(slot, value, fields, at, ids) {
184
+ if (!Array.isArray(value))
185
+ return { entries: [], problems: [`${at}.${slot}: must be a list`] };
186
+ const entries = [];
187
+ const problems = [];
188
+ const placed = new Set();
189
+ value.forEach((item, index) => {
190
+ const where = `${at}.${slot}[${index}]`;
191
+ const { entry, problems: found } = readEntry(item, fields, where);
192
+ problems.push(...found);
193
+ if (!entry)
194
+ return;
195
+ const { slots } = MODULE_REGISTRY[entry.module];
196
+ if (!slots.includes(slot)) {
197
+ problems.push(`${where}: the ${entry.module} module cannot sit in ${slot} (slots: ${slots.join(', ')})`);
198
+ }
199
+ problems.push(...idProblems(entry, ids, where, `${slot}[${index}]`));
200
+ if (entry.field !== undefined) {
201
+ if (placed.has(entry.field))
202
+ problems.push(`${where}: field "${entry.field}" is already in ${slot}`);
203
+ placed.add(entry.field);
204
+ }
205
+ entries.push(entry);
206
+ });
207
+ return { entries, problems };
208
+ }
209
+ function intakeProblems(intake, fields, at) {
210
+ const asked = new Set(intake.map(({ field }) => field));
211
+ return Object.entries(fields)
212
+ .filter(([name, { required }]) => required === 'create' && !asked.has(name))
213
+ .map(([name]) => `${at}.intake: missing field "${name}", required to create`);
214
+ }
215
+ const SECTION_KEYS = ['title', 'width', 'entries'];
216
+ function readSection(value, fields, at, ids, seen) {
217
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
218
+ return { problems: [`${at}: must be a map with a title and entries`] };
219
+ }
220
+ const raw = value;
221
+ const problems = Object.keys(raw)
222
+ .filter((key) => !SECTION_KEYS.includes(key))
223
+ .map((key) => `${at}: unknown key "${key}" (keys: ${SECTION_KEYS.join(', ')})`);
224
+ const { title, width, entries: list } = raw;
225
+ if (typeof title !== 'string' || title.trim() === '') {
226
+ problems.push(`${at}.title: must be a non-empty text`);
227
+ }
228
+ if (width !== undefined && width !== 'full' && width !== 'half') {
229
+ problems.push(`${at}.width: must be full or half`);
230
+ }
231
+ if (!Array.isArray(list)) {
232
+ problems.push(`${at}.entries: must be a list`);
233
+ return { problems };
234
+ }
235
+ const entries = [];
236
+ list.forEach((item, index) => {
237
+ const where = `${at}.entries[${index}]`;
238
+ const { entry, problems: found } = readEntry(item, fields, where);
239
+ problems.push(...found);
240
+ if (!entry)
241
+ return;
242
+ const { slots } = MODULE_REGISTRY[entry.module];
243
+ if (!slots.includes('body')) {
244
+ problems.push(`${where}: the ${entry.module} module cannot sit in a section (slots: ${slots.join(', ')})`);
245
+ }
246
+ const place = `${at}.entries[${index}]`;
247
+ problems.push(...idProblems(entry, ids, where, place));
248
+ const key = entry.field !== undefined ? `field:${entry.field}` : `module:${entry.module}`;
249
+ const unique = entry.field !== undefined || MODULE_REGISTRY[entry.module].family !== 'evidence';
250
+ if (unique && seen.has(key)) {
251
+ problems.push(entry.field !== undefined
252
+ ? `${where}: field "${entry.field}" is already in the sections`
253
+ : `${where}: the ${entry.module} module is already in the sections`);
254
+ }
255
+ seen.add(key);
256
+ entries.push(entry);
257
+ });
258
+ const section = {
259
+ title: typeof title === 'string' ? title : '',
260
+ ...(width === 'full' || width === 'half' ? { width } : {}),
261
+ entries,
262
+ };
263
+ return { section, problems };
264
+ }
265
+ function readSections(raw, fields, at, ids) {
266
+ if (!Array.isArray(raw))
267
+ return { problems: [`${at}.sections: must be a list`] };
268
+ const seen = new Set();
269
+ const sections = [];
270
+ const problems = [];
271
+ raw.forEach((value, index) => {
272
+ const read = readSection(value, fields, `${at}.sections[${index}]`, ids, seen);
273
+ problems.push(...read.problems);
274
+ if (read.section)
275
+ sections.push(read.section);
276
+ });
277
+ return { sections, problems };
278
+ }
279
+ // Reads a type's `layout`: every slot it declares, in order, and the default for the slots it leaves
280
+ // out. `at` is the layout's path in the preset, for the problems found.
281
+ export function readLayout(raw, fields, at) {
282
+ const layout = defaultLayout(fields);
283
+ if (raw === undefined)
284
+ return { layout, problems: [] };
285
+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) {
286
+ return { layout, problems: [`${at}: must be a map of slots`] };
287
+ }
288
+ const problems = [];
289
+ const ids = new Map();
290
+ if ('sections' in raw) {
291
+ layout.body = [];
292
+ layout.evidence = [];
293
+ const read = readSections(raw.sections, fields, at, ids);
294
+ if (read.sections)
295
+ layout.sections = read.sections;
296
+ problems.push(...read.problems);
297
+ }
298
+ for (const [slot, value] of Object.entries(raw)) {
299
+ if (slot === 'sections')
300
+ continue;
301
+ if ('sections' in raw && (slot === 'body' || slot === 'evidence')) {
302
+ if (!Array.isArray(value) || value.length > 0) {
303
+ problems.push(`${at}.${slot}: must be empty when sections are declared; place its entries in a section`);
304
+ }
305
+ continue;
306
+ }
307
+ if (!LAYOUT_SLOTS.includes(slot)) {
308
+ problems.push(`${at}.${slot}: unknown slot (slots: ${LAYOUT_SLOTS.join(', ')})`);
309
+ continue;
310
+ }
311
+ const read = readSlot(slot, value, fields, at, ids);
312
+ layout[slot] = read.entries;
313
+ problems.push(...read.problems);
314
+ }
315
+ if ('intake' in raw)
316
+ problems.push(...intakeProblems(layout.intake, fields, at));
317
+ return { layout, problems };
318
+ }
@@ -0,0 +1,117 @@
1
+ // What matters to the person on: what is theirs (a review, a question), an answer, a stuck item.
2
+ // An agent setting something aside is theirs to look up, not to be told about, until they ask.
3
+ export const DEFAULT_PREFS = {
4
+ turn: true,
5
+ ask: true,
6
+ replied: true,
7
+ stuck: true,
8
+ aside: false,
9
+ quiet: null,
10
+ muted: [],
11
+ };
12
+ // The quiet hours offered when the person turns them on.
13
+ export const DEFAULT_QUIET = { from: '22:00', to: '08:00' };
14
+ export const GATHER_MS = 10_000;
15
+ export const ITEM_COOLDOWN_MS = 5 * 60_000;
16
+ // The least time between two notices to one listener from one project.
17
+ export const SPACING_MS = 2 * 60_000;
18
+ export const TIME_PATTERN = /^([01][0-9]|2[0-3]):[0-5][0-9]$/;
19
+ const minutesOf = (time) => Number(time.slice(0, 2)) * 60 + Number(time.slice(3, 5));
20
+ export function inQuietHours(quiet, minuteOfDay) {
21
+ if (!quiet)
22
+ return false;
23
+ const from = minutesOf(quiet.from);
24
+ const to = minutesOf(quiet.to);
25
+ if (from === to)
26
+ return false;
27
+ return from < to
28
+ ? minuteOfDay >= from && minuteOfDay < to
29
+ : minuteOfDay >= from || minuteOfDay < to;
30
+ }
31
+ export const localMinuteOfDay = (ms) => {
32
+ const date = new Date(ms);
33
+ return date.getHours() * 60 + date.getMinutes();
34
+ };
35
+ // Of an item's news, the one told: a question first, its turn, that it got stuck, then the rest.
36
+ const RANK = { ask: 0, turn: 1, stuck: 2, replied: 3, aside: 4 };
37
+ function noticeOf(signal) {
38
+ const { kind, item, title, by } = signal;
39
+ return { kind, item, title, ...(by === undefined ? {} : { by }) };
40
+ }
41
+ export class Planner {
42
+ pending = [];
43
+ due = null;
44
+ told = new Map();
45
+ sentAt = null;
46
+ minuteOfDay;
47
+ // The project whose signals this planner hears, for the person's muted projects.
48
+ project;
49
+ constructor(minuteOfDay = localMinuteOfDay, project) {
50
+ this.minuteOfDay = minuteOfDay;
51
+ this.project = project;
52
+ }
53
+ wants(kind, prefs) {
54
+ return prefs[kind] && !(this.project !== undefined && prefs.muted.includes(this.project));
55
+ }
56
+ hasPending() {
57
+ return this.pending.length > 0;
58
+ }
59
+ offer(signals, prefs, now) {
60
+ const wanted = signals.filter(({ kind, key }) => this.wants(kind, prefs) && !this.pending.some((held) => held.key === key));
61
+ if (wanted.length === 0)
62
+ return;
63
+ this.pending.push(...wanted);
64
+ this.due ??= now + GATHER_MS;
65
+ }
66
+ // What goes out now, if anything: one notice, for one item or for the burst. `holding` are the
67
+ // keys of what is true now.
68
+ take(now, holding, prefs) {
69
+ if (this.due === null || now < this.due)
70
+ return [];
71
+ const spaced = this.sentAt === null ? now : this.sentAt + SPACING_MS;
72
+ if (now < spaced) {
73
+ this.due = spaced;
74
+ return [];
75
+ }
76
+ for (const [item, at] of this.told)
77
+ if (at + ITEM_COOLDOWN_MS <= now)
78
+ this.told.delete(item);
79
+ this.pending = this.pending.filter(({ key, kind }) => holding.has(key) && this.wants(kind, prefs));
80
+ const byItem = new Map();
81
+ for (const signal of this.pending) {
82
+ const held = byItem.get(signal.item);
83
+ if (!held || RANK[signal.kind] < RANK[held.kind])
84
+ byItem.set(signal.item, signal);
85
+ }
86
+ const ready = [];
87
+ let wait = null;
88
+ for (const [item, signal] of byItem) {
89
+ const free = (this.told.get(item) ?? Number.NEGATIVE_INFINITY) + ITEM_COOLDOWN_MS;
90
+ if (free <= now)
91
+ ready.push(signal);
92
+ else
93
+ wait = Math.min(wait ?? free, free);
94
+ }
95
+ const out = new Set(ready.map(({ item }) => item));
96
+ this.pending = this.pending.filter(({ item }) => !out.has(item));
97
+ this.due = wait;
98
+ if (ready.length === 0 || inQuietHours(prefs.quiet, this.minuteOfDay(now)))
99
+ return [];
100
+ for (const { item } of ready)
101
+ this.told.set(item, now);
102
+ this.sentAt = now;
103
+ const [only] = ready;
104
+ if (ready.length === 1 && only)
105
+ return [noticeOf(only)];
106
+ const kinds = new Set(ready.map(({ kind }) => kind));
107
+ const [same] = kinds;
108
+ return [
109
+ {
110
+ kind: 'group',
111
+ count: ready.length,
112
+ of: kinds.size === 1 && same ? same : 'mixed',
113
+ items: ready.map(({ item, title, kind }) => ({ item, title, kind })),
114
+ },
115
+ ];
116
+ }
117
+ }
@@ -0,0 +1,66 @@
1
+ // What is worth a notification (a push to a phone, a desktop notification), read from the items as
2
+ // the lists show them. Each signal has a key that stays the same while the thing it says holds, so
3
+ // comparing two looks finds what is new:
4
+ // - turn: an item is yours to review, approve or do (your own step);
5
+ // - ask: an agent asks you something (otea ask), the turn yours until you answer;
6
+ // - replied: an agent answered one of your notes (otea reply);
7
+ // - stuck: an agent's lease ran out with nobody releasing it or taking the item again;
8
+ // - aside: an agent set the item aside for later (otea release --later).
9
+ export const NOTIFY_KINDS = ['turn', 'ask', 'replied', 'stuck', 'aside'];
10
+ const active = (lease, now) => lease !== null && Date.parse(lease.expiresAt) > now;
11
+ export function signalsOf(items, now) {
12
+ const out = [];
13
+ for (const facts of items) {
14
+ const { id: item, title } = facts;
15
+ if (facts.turn === 'human') {
16
+ const base = `turn:${item}:${facts.state}:${facts.round ?? ''}`;
17
+ out.push(facts.asks
18
+ ? {
19
+ key: `${base}:q${facts.asks.question}`,
20
+ kind: 'ask',
21
+ item,
22
+ title,
23
+ by: facts.asks.by,
24
+ }
25
+ : { key: base, kind: 'turn', item, title });
26
+ }
27
+ const quiet = facts.expiredLease;
28
+ if (facts.turn === 'agent' && quiet && !active(facts.lease, now)) {
29
+ out.push({
30
+ key: `stuck:${item}:${quiet.holder}:${quiet.expiresAt}`,
31
+ kind: 'stuck',
32
+ item,
33
+ title,
34
+ by: quiet.holder,
35
+ });
36
+ }
37
+ const aside = facts.setAside;
38
+ if (aside) {
39
+ out.push({
40
+ key: `aside:${item}:${aside.by}:${aside.at}`,
41
+ kind: 'aside',
42
+ item,
43
+ title,
44
+ by: aside.by,
45
+ });
46
+ }
47
+ const replied = facts.notes?.replied;
48
+ if (replied) {
49
+ out.push({
50
+ key: `replied:${item}:${replied.note}:${replied.at}`,
51
+ kind: 'replied',
52
+ item,
53
+ title,
54
+ by: replied.by,
55
+ });
56
+ }
57
+ }
58
+ return out;
59
+ }
60
+ // The signals that were not there the last time; the first look (`before` undefined) is where
61
+ // watching starts, so a server that starts or a phone that subscribes is not told the past.
62
+ export function freshSignals(before, now) {
63
+ if (before === undefined)
64
+ return [];
65
+ return now.filter(({ key }) => !before.has(key));
66
+ }
@@ -0,0 +1,60 @@
1
+ export const isNoticeLang = (value) => value === 'es' || value === 'en';
2
+ const MAX_BODY = 100;
3
+ const short = (text, max = MAX_BODY) => text.length <= max ? text : `${text.slice(0, max - 1).trimEnd()}…`;
4
+ export const itemUrl = (id) => `/#/i/${encodeURIComponent(id)}`;
5
+ const WORDS = {
6
+ es: {
7
+ turn: 'Te toca',
8
+ ask: (by) => `${by} te pregunta`,
9
+ replied: (by) => `${by} contestó tu nota`,
10
+ stuck: (by) => `Se atascó · ${by} no responde`,
11
+ aside: (by) => `${by} lo dejó para después`,
12
+ group: {
13
+ turn: (n) => `${n} cosas te tocan`,
14
+ ask: (n) => `${n} preguntas te esperan`,
15
+ replied: (n) => `${n} notas contestadas`,
16
+ stuck: (n) => `${n} cosas se atascaron`,
17
+ aside: (n) => `${n} cosas apartadas`,
18
+ mixed: (n) => `${n} cosas te esperan`,
19
+ },
20
+ in: (title, project) => `${title} en ${project}`,
21
+ },
22
+ en: {
23
+ turn: 'Your turn',
24
+ ask: (by) => `${by} asks you`,
25
+ replied: (by) => `${by} answered your note`,
26
+ stuck: (by) => `Stuck · ${by} went quiet`,
27
+ aside: (by) => `${by} set it aside for later`,
28
+ group: {
29
+ turn: (n) => `${n} things are yours`,
30
+ ask: (n) => `${n} questions wait for you`,
31
+ replied: (n) => `${n} notes answered`,
32
+ stuck: (n) => `${n} things got stuck`,
33
+ aside: (n) => `${n} things set aside`,
34
+ mixed: (n) => `${n} things wait for you`,
35
+ },
36
+ in: (title, project) => `${title} in ${project}`,
37
+ },
38
+ };
39
+ export function noticeText(notice, lang, project) {
40
+ const words = WORDS[lang];
41
+ if (notice.kind === 'group') {
42
+ const title = words.group[notice.of](notice.count);
43
+ return {
44
+ title: project ? words.in(title, project.name) : title,
45
+ body: short(notice.items.map(({ title }) => title).join(' · ')),
46
+ tag: project ? `otea-group-${project.id}` : 'otea-group',
47
+ url: '/#/',
48
+ item: null,
49
+ };
50
+ }
51
+ const by = notice.by ?? '';
52
+ const title = notice.kind === 'turn' ? words.turn : words[notice.kind](by);
53
+ return {
54
+ title,
55
+ body: short(`${notice.item} · ${notice.title}`),
56
+ tag: `otea-item-${notice.item}`,
57
+ url: itemUrl(notice.item),
58
+ item: notice.item,
59
+ };
60
+ }
@@ -0,0 +1,22 @@
1
+ // The form a real path is compared in: forward slashes, and lowercase where the file system usually
2
+ // ignores case (Windows, macOS).
3
+ export function pathKey(realPath, platform) {
4
+ const slashed = realPath.replaceAll('\\', '/');
5
+ return platform === 'win32' || platform === 'darwin' ? slashed.toLowerCase() : slashed;
6
+ }
7
+ function contains(outer, inner) {
8
+ if (inner === outer)
9
+ return true;
10
+ return inner.startsWith(outer.endsWith('/') ? outer : `${outer}/`);
11
+ }
12
+ // The entry whose path is `key` or the nearest folder above it, so a nested path wins over its parent.
13
+ export function longestContaining(entries, key) {
14
+ let best;
15
+ for (const entry of entries) {
16
+ if (!contains(entry.pathKey, key))
17
+ continue;
18
+ if (!best || entry.pathKey.length > best.pathKey.length)
19
+ best = entry;
20
+ }
21
+ return best;
22
+ }