@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.
- package/LICENSE +661 -0
- package/README.es.md +151 -0
- package/README.md +252 -2
- package/THIRD_PARTY_NOTICES.md +114 -0
- package/bin/otea.mjs +28 -0
- package/dist/app/agent-detail.js +26 -0
- package/dist/app/agent-draft.js +429 -0
- package/dist/app/agent-prompts.js +52 -0
- package/dist/app/agents.js +77 -0
- package/dist/app/asking.js +43 -0
- package/dist/app/board-archive.js +243 -0
- package/dist/app/board-migration.js +272 -0
- package/dist/app/boards.js +870 -0
- package/dist/app/branch-owner.js +48 -0
- package/dist/app/brief.js +53 -0
- package/dist/app/choices.js +61 -0
- package/dist/app/collections.js +101 -0
- package/dist/app/context.js +1 -0
- package/dist/app/crop.js +56 -0
- package/dist/app/devices.js +62 -0
- package/dist/app/draft-request.js +82 -0
- package/dist/app/events.js +12 -0
- package/dist/app/evidence.js +287 -0
- package/dist/app/gates.js +610 -0
- package/dist/app/guide.js +37 -0
- package/dist/app/home.js +145 -0
- package/dist/app/init-boards.js +36 -0
- package/dist/app/intake.js +25 -0
- package/dist/app/item-search.js +141 -0
- package/dist/app/items.js +318 -0
- package/dist/app/keep-waiting.js +44 -0
- package/dist/app/land-order.js +107 -0
- package/dist/app/language.js +52 -0
- package/dist/app/launcher.js +56 -0
- package/dist/app/lease-take.js +54 -0
- package/dist/app/leases.js +86 -0
- package/dist/app/machine.js +39 -0
- package/dist/app/merged.js +89 -0
- package/dist/app/migrate.js +171 -0
- package/dist/app/next.js +1637 -0
- package/dist/app/notes.js +319 -0
- package/dist/app/notify-targets.js +113 -0
- package/dist/app/opened.js +25 -0
- package/dist/app/owner-only.js +34 -0
- package/dist/app/performance.js +28 -0
- package/dist/app/phone.js +97 -0
- package/dist/app/presence.js +596 -0
- package/dist/app/preset-views.js +25 -0
- package/dist/app/presets.js +129 -0
- package/dist/app/priority.js +156 -0
- package/dist/app/profile.js +86 -0
- package/dist/app/project.js +247 -0
- package/dist/app/proofs.js +179 -0
- package/dist/app/questions.js +215 -0
- package/dist/app/reader-filter.js +58 -0
- package/dist/app/relations.js +164 -0
- package/dist/app/relay.js +98 -0
- package/dist/app/repos.js +176 -0
- package/dist/app/restart.js +92 -0
- package/dist/app/set-aside.js +64 -0
- package/dist/app/system-locale.js +6 -0
- package/dist/app/tailscale-cli.js +77 -0
- package/dist/app/tailscale-testing.js +41 -0
- package/dist/app/tailscale.js +177 -0
- package/dist/app/tips.js +30 -0
- package/dist/app/topics-everywhere.js +53 -0
- package/dist/app/topics.js +453 -0
- package/dist/app/undo.js +52 -0
- package/dist/app/validate.js +131 -0
- package/dist/app/watch.js +48 -0
- package/dist/cli/command.js +44 -0
- package/dist/cli/commands.js +3303 -0
- package/dist/cli/exit-codes.js +70 -0
- package/dist/cli/forge.js +176 -0
- package/dist/cli/format.js +340 -0
- package/dist/cli/gh.js +17 -0
- package/dist/cli/gitrepo.js +489 -0
- package/dist/cli/help.js +101 -0
- package/dist/cli/hooks.js +232 -0
- package/dist/cli/main.js +37 -0
- package/dist/cli/merged-outside.js +58 -0
- package/dist/cli/names.js +154 -0
- package/dist/cli/opener.js +21 -0
- package/dist/cli/owner.js +154 -0
- package/dist/cli/phone.js +221 -0
- package/dist/cli/ports.js +24 -0
- package/dist/cli/probe.js +18 -0
- package/dist/cli/run.js +246 -0
- package/dist/cli/serve-file.js +21 -0
- package/dist/cli/stats.js +55 -0
- package/dist/cli/version.js +3 -0
- package/dist/core/agent-guide.js +31 -0
- package/dist/core/agent-prompts.js +139 -0
- package/dist/core/authority.js +94 -0
- package/dist/core/board/analysis.js +86 -0
- package/dist/core/board/choices.js +100 -0
- package/dist/core/board/compile.js +362 -0
- package/dist/core/board/engine.js +523 -0
- package/dist/core/board/hints.js +46 -0
- package/dist/core/board/history.js +146 -0
- package/dist/core/board/ids.js +87 -0
- package/dist/core/board/joins.js +30 -0
- package/dist/core/board/mentions.js +68 -0
- package/dist/core/board/merged.js +18 -0
- package/dist/core/board/model.js +135 -0
- package/dist/core/board/next.js +1116 -0
- package/dist/core/board/notes.js +298 -0
- package/dist/core/board/performance.js +218 -0
- package/dist/core/board/plan.js +299 -0
- package/dist/core/board/problems.js +232 -0
- package/dist/core/board/proofs.js +84 -0
- package/dist/core/board/questions.js +114 -0
- package/dist/core/board/reading.js +220 -0
- package/dist/core/board/readings.js +534 -0
- package/dist/core/board/schema.js +335 -0
- package/dist/core/board/shape-words.js +179 -0
- package/dist/core/board/template-words.js +23 -0
- package/dist/core/board/templates-catalogue.js +965 -0
- package/dist/core/board/templates-coverage.js +261 -0
- package/dist/core/board/templates-flows.js +779 -0
- package/dist/core/board/templates.js +586 -0
- package/dist/core/brief.js +93 -0
- package/dist/core/diagram.js +80 -0
- package/dist/core/errors.js +12 -0
- package/dist/core/evidence.js +33 -0
- package/dist/core/fields.js +129 -0
- package/dist/core/hierarchy.js +30 -0
- package/dist/core/icons.js +181 -0
- package/dist/core/image-crop.js +166 -0
- package/dist/core/item.js +49 -0
- package/dist/core/lease.js +31 -0
- package/dist/core/modules.js +318 -0
- package/dist/core/notify/planner.js +117 -0
- package/dist/core/notify/signals.js +66 -0
- package/dist/core/notify/text.js +60 -0
- package/dist/core/path-key.js +22 -0
- package/dist/core/placement.js +161 -0
- package/dist/core/preset-labels.js +66 -0
- package/dist/core/preset-notes.js +109 -0
- package/dist/core/preset.js +541 -0
- package/dist/core/profile.js +55 -0
- package/dist/core/project-id.js +20 -0
- package/dist/core/refusal.js +1 -0
- package/dist/core/relations.js +208 -0
- package/dist/core/repo.js +54 -0
- package/dist/core/samples.js +161 -0
- package/dist/core/search-words.js +68 -0
- package/dist/core/test-report.js +120 -0
- package/dist/core/time.js +8 -0
- package/dist/core/validation.js +60 -0
- package/dist/core/views.js +171 -0
- package/dist/core/workflow.js +51 -0
- package/dist/mcp/commands.js +101 -0
- package/dist/mcp/server.js +239 -0
- package/dist/mcp/words.js +95 -0
- package/dist/server/boards.js +232 -0
- package/dist/server/changes.js +42 -0
- package/dist/server/diff-tip.js +83 -0
- package/dist/server/drift.js +87 -0
- package/dist/server/errors.js +87 -0
- package/dist/server/evidence.js +82 -0
- package/dist/server/hosts.js +6 -0
- package/dist/server/hub.js +98 -0
- package/dist/server/lan-access.js +62 -0
- package/dist/server/lan.js +164 -0
- package/dist/server/listen.js +70 -0
- package/dist/server/notifier.js +156 -0
- package/dist/server/notify-api.js +151 -0
- package/dist/server/pairing.js +124 -0
- package/dist/server/projects.js +191 -0
- package/dist/server/push.js +199 -0
- package/dist/server/server.js +960 -0
- package/dist/server/session.js +43 -0
- package/dist/server/stream.js +36 -0
- package/dist/server/test-push.js +38 -0
- package/dist/server/token.js +37 -0
- package/dist/server/transports.js +54 -0
- package/dist/server/ui.js +44 -0
- package/dist/server/writes.js +361 -0
- package/dist/server/wrong-address.js +36 -0
- package/dist/store/boards.js +111 -0
- package/dist/store/database.js +45 -0
- package/dist/store/machine.js +77 -0
- package/dist/store/migrations.js +493 -0
- package/dist/store/queries.js +585 -0
- package/dist/store/registry.js +72 -0
- package/package.json +73 -4
- package/presets/architecture/playbook.md +69 -0
- package/presets/architecture/preset.yaml +80 -0
- package/presets/architecture/samples.yaml +23 -0
- package/presets/bug-triage/playbook.md +129 -0
- package/presets/bug-triage/preset.yaml +127 -0
- package/presets/bug-triage/samples/wall-slide-after.png +0 -0
- package/presets/bug-triage/samples/wall-slide-before.png +0 -0
- package/presets/bug-triage/samples.yaml +72 -0
- package/presets/change/playbook.md +115 -0
- package/presets/change/preset.yaml +141 -0
- package/presets/change/samples.yaml +62 -0
- package/presets/review/playbook.md +51 -0
- package/presets/review/preset.yaml +62 -0
- package/presets/review/samples.yaml +27 -0
- package/presets/tasks/playbook.md +78 -0
- package/presets/tasks/preset.yaml +61 -0
- package/presets/tasks/samples.yaml +46 -0
- package/schemas/board.schema.json +764 -0
- package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
- package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
- package/ui/dist/assets/index-C3OzJope.js +103 -0
- package/ui/dist/assets/index-COdEjckQ.css +1 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
- package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
- package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
- package/ui/dist/assets/pdf-rELvHkft.js +56 -0
- package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
- package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
- package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
- package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
- package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
- package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
- package/ui/dist/icons/apple-touch-icon.png +0 -0
- package/ui/dist/icons/otea-192.png +0 -0
- package/ui/dist/icons/otea-512.png +0 -0
- package/ui/dist/icons/otea-badge.png +0 -0
- package/ui/dist/icons/otea-maskable-512.png +0 -0
- package/ui/dist/index.html +25 -0
- package/ui/dist/manifest.webmanifest +24 -0
- package/ui/dist/otea-idle.svg +5 -0
- package/ui/dist/otea-working.svg +5 -0
- package/ui/dist/otea-yours-working.svg +5 -0
- package/ui/dist/otea-yours.svg +5 -0
- package/ui/dist/sw.js +1 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { isClosed, positionOf } from '../core/board/engine.js';
|
|
3
|
+
import { findTest, logLine, POINT_PROVED, proofsFrom, testTitle, } from '../core/board/proofs.js';
|
|
4
|
+
import { OteaError } from '../core/errors.js';
|
|
5
|
+
import { transaction } from '../store/database.js';
|
|
6
|
+
import { appendEvent, selectEvents, selectEvidence, } from '../store/queries.js';
|
|
7
|
+
import { evidenceFile } from './evidence.js';
|
|
8
|
+
import { getItem, requireItem, requireWritableItem, systemClock, updateItem } from './items.js';
|
|
9
|
+
import { assertMayWriteItem } from './lease-take.js';
|
|
10
|
+
import { itemOnBoard, liveBoardOf } from './next.js';
|
|
11
|
+
function fold(text) {
|
|
12
|
+
return text.normalize('NFD').replace(/[̀-ͯ]/g, '').trim().toLowerCase();
|
|
13
|
+
}
|
|
14
|
+
function checklists(def) {
|
|
15
|
+
return Object.values(def.parts).filter((part) => part.kind === 'check');
|
|
16
|
+
}
|
|
17
|
+
// The checklist and the point a reader names: the point by name or id, in the checklist named or,
|
|
18
|
+
// when none is, in the one checklist that has it.
|
|
19
|
+
export function pointOf(def, point, part) {
|
|
20
|
+
const lists = checklists(def).filter((list) => part === undefined || fold(list.name) === fold(part) || list.id === part);
|
|
21
|
+
if (part !== undefined && lists.length === 0)
|
|
22
|
+
throw new OteaError('not_found', `${def.name} has no checklist "${part}"; its checklists are ${checklists(def)
|
|
23
|
+
.map(({ name }) => name)
|
|
24
|
+
.join(', ') || 'none'}`);
|
|
25
|
+
const found = lists.flatMap((list) => list.options
|
|
26
|
+
.filter((option) => option.id === point || fold(option.name) === fold(point))
|
|
27
|
+
.map((option) => ({ part: list, point: option })));
|
|
28
|
+
const [one, other] = found;
|
|
29
|
+
if (!one)
|
|
30
|
+
throw new OteaError('not_found', `No checklist of ${def.name} has the point "${point}"; its points are ${lists
|
|
31
|
+
.flatMap(({ options }) => options.map(({ name }) => `"${name}"`))
|
|
32
|
+
.join(', ')}`);
|
|
33
|
+
if (other)
|
|
34
|
+
throw new OteaError('invalid_fields', `"${point}" is a point of ${one.part.name} and ${other.part.name}: say which with --part`);
|
|
35
|
+
return one;
|
|
36
|
+
}
|
|
37
|
+
function readText(project, entry) {
|
|
38
|
+
return readFileSync(evidenceFile(project.dataDir, entry.sha256), 'utf8');
|
|
39
|
+
}
|
|
40
|
+
// The evidence of the item's current round, newest first: a proof rests on what the round showed.
|
|
41
|
+
function roundEvidence(project, id, since) {
|
|
42
|
+
return selectEvidence(project.db, id)
|
|
43
|
+
.filter((entry) => since === undefined || entry.createdAt >= since)
|
|
44
|
+
.reverse();
|
|
45
|
+
}
|
|
46
|
+
// The proof the source names, verified: the evidence exists in the round, the log has that line,
|
|
47
|
+
// the test is in an attached report and passes.
|
|
48
|
+
function proofOf(project, id, source, since) {
|
|
49
|
+
const round = roundEvidence(project, id, since);
|
|
50
|
+
if ('evidence' in source) {
|
|
51
|
+
const entry = round.find(({ id: n }) => n === source.evidence);
|
|
52
|
+
if (!entry)
|
|
53
|
+
throw new OteaError('not_found', `${id} has no evidence #${source.evidence} attached in this round: attach it, then tick the point`);
|
|
54
|
+
return { kind: 'file', evidence: entry.id, file: entry.filename };
|
|
55
|
+
}
|
|
56
|
+
if ('log' in source) {
|
|
57
|
+
const { file, line } = source.log;
|
|
58
|
+
const entry = round.find(({ filename }) => fold(filename) === fold(file));
|
|
59
|
+
if (!entry)
|
|
60
|
+
throw new OteaError('not_found', `${id} has no log "${file}" attached in this round: attach it, then tick the point`);
|
|
61
|
+
const read = logLine(readText(project, entry), line);
|
|
62
|
+
if (!read.ok)
|
|
63
|
+
throw new OteaError('invalid_fields', `${file} cannot prove it at line ${line}: ${read.why}`);
|
|
64
|
+
return {
|
|
65
|
+
kind: 'log',
|
|
66
|
+
evidence: entry.id,
|
|
67
|
+
file: entry.filename,
|
|
68
|
+
line,
|
|
69
|
+
text: read.text,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if ('test' in source) {
|
|
73
|
+
for (const entry of round) {
|
|
74
|
+
if (entry.size > 20 * 1024 * 1024)
|
|
75
|
+
continue;
|
|
76
|
+
const test = findTest(readText(project, entry), source.test);
|
|
77
|
+
if (!test)
|
|
78
|
+
continue;
|
|
79
|
+
if (test.status !== 'passed')
|
|
80
|
+
throw new OteaError('invalid_fields', `"${testTitle(test)}" ${test.status === 'failed' ? 'fails' : 'was skipped'} in ${entry.filename}: a point is proved only by a test that passes`);
|
|
81
|
+
return {
|
|
82
|
+
kind: 'test',
|
|
83
|
+
evidence: entry.id,
|
|
84
|
+
file: entry.filename,
|
|
85
|
+
test: testTitle(test),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
throw new OteaError('not_found', `No test report attached to ${id} in this round has a test "${source.test}": attach the JUnit XML or the vitest/jest JSON, then tick the point`);
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
// Ticks a checklist point and, when a source is given, keeps its proof: the round's evidence is
|
|
93
|
+
// checked first, so a refused proof ticks nothing.
|
|
94
|
+
export function tickPoint(project, input, now = systemClock) {
|
|
95
|
+
const item = requireWritableItem(project.db, input.id);
|
|
96
|
+
const board = liveBoardOf(project, item.collection);
|
|
97
|
+
if (!board)
|
|
98
|
+
throw new OteaError('invalid_type', `${input.id} is not on a board: it has no checklist`);
|
|
99
|
+
const { def } = board;
|
|
100
|
+
const { part, point } = pointOf(def, input.point, input.part);
|
|
101
|
+
const at = now();
|
|
102
|
+
assertMayWriteItem(project.db, item, input.actor, at);
|
|
103
|
+
const position = positionOf(item.state);
|
|
104
|
+
if (isClosed(position))
|
|
105
|
+
throw new OteaError('item_closed', `${item.id} is closed and takes no proof`, {
|
|
106
|
+
kind: 'closed',
|
|
107
|
+
item: item.id,
|
|
108
|
+
});
|
|
109
|
+
const since = itemOnBoard(project, item, def).facts.roundStartedAt;
|
|
110
|
+
const proof = proofOf(project, item.id, input.source, since);
|
|
111
|
+
const ticked = new Set(listOf(item.fields[part.id]));
|
|
112
|
+
if (!ticked.has(point.id)) {
|
|
113
|
+
const names = part.options
|
|
114
|
+
.filter(({ id }) => ticked.has(id) || id === point.id)
|
|
115
|
+
.map(({ name }) => name);
|
|
116
|
+
updateItem(project, {
|
|
117
|
+
id: item.id,
|
|
118
|
+
fields: { [part.name]: names.join(', ') },
|
|
119
|
+
actor: input.actor,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
if (!proof)
|
|
123
|
+
return {
|
|
124
|
+
part: part.name,
|
|
125
|
+
point: point.name,
|
|
126
|
+
text: `Ticked "${point.name}" in ${part.name}${part.proof ? `, without proof yet: ${part.name} asks for one` : ''}.`,
|
|
127
|
+
};
|
|
128
|
+
transaction(project.db, () => {
|
|
129
|
+
appendEvent(project.db, {
|
|
130
|
+
itemId: item.id,
|
|
131
|
+
type: POINT_PROVED,
|
|
132
|
+
actor: input.actor,
|
|
133
|
+
at,
|
|
134
|
+
data: {
|
|
135
|
+
part: part.id,
|
|
136
|
+
point: point.id,
|
|
137
|
+
...proof,
|
|
138
|
+
channel: input.channel,
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
const kept = proofsFrom(selectEvents(project.db, item.id)).find((one) => one.part === part.id && one.point === point.id);
|
|
143
|
+
return {
|
|
144
|
+
part: part.name,
|
|
145
|
+
point: point.name,
|
|
146
|
+
...(kept === undefined ? {} : { proof: kept }),
|
|
147
|
+
text: `Ticked "${point.name}" in ${part.name} with ${provedBy(proof)}.`,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function provedBy(proof) {
|
|
151
|
+
if (proof.kind === 'log')
|
|
152
|
+
return `${proof.file} line ${proof.line}`;
|
|
153
|
+
if (proof.kind === 'test')
|
|
154
|
+
return `the test "${proof.test}" of ${proof.file} (passes)`;
|
|
155
|
+
return `${proof.file} (#${proof.evidence})`;
|
|
156
|
+
}
|
|
157
|
+
function listOf(value) {
|
|
158
|
+
if (Array.isArray(value))
|
|
159
|
+
return value.map(String);
|
|
160
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
161
|
+
return [];
|
|
162
|
+
try {
|
|
163
|
+
const parsed = JSON.parse(value);
|
|
164
|
+
if (Array.isArray(parsed))
|
|
165
|
+
return parsed.map(String);
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
// Not JSON.
|
|
169
|
+
}
|
|
170
|
+
return [value];
|
|
171
|
+
}
|
|
172
|
+
// The proofs of the item's current round, for its page and its text.
|
|
173
|
+
export function roundProofs(project, id) {
|
|
174
|
+
const item = getItem(project, id);
|
|
175
|
+
const board = liveBoardOf(project, item.collection);
|
|
176
|
+
if (!board)
|
|
177
|
+
return [];
|
|
178
|
+
return itemOnBoard(project, requireItem(project.db, id), board.def).proofs;
|
|
179
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { isClosed, positionOf } from '../core/board/engine.js';
|
|
2
|
+
import { stepById } from '../core/board/model.js';
|
|
3
|
+
import { NOTE_LEFT } from '../core/board/notes.js';
|
|
4
|
+
import { askedText, chatAnswerText, QUESTION_ANSWERED, QUESTION_ASKED, questionsOf, waitAnswerText, } from '../core/board/questions.js';
|
|
5
|
+
import { OteaError } from '../core/errors.js';
|
|
6
|
+
import { activeLease } from '../core/lease.js';
|
|
7
|
+
import { transaction } from '../store/database.js';
|
|
8
|
+
import { appendEvent, selectEvents, selectEventsOfTypes, selectRelationsOf, selectStateEntries, } from '../store/queries.js';
|
|
9
|
+
import { askingItems } from './asking.js';
|
|
10
|
+
import { requireItem, requireWritableItem, systemClock } from './items.js';
|
|
11
|
+
import { assertMayWriteItem, takeItem } from './lease-take.js';
|
|
12
|
+
import { itemOnBoard, liveBoardOf } from './next.js';
|
|
13
|
+
import { sessionAlive } from './presence.js';
|
|
14
|
+
import { RELAYED_HUMAN } from './relay.js';
|
|
15
|
+
import { liftSetAside, SET_ASIDE_LIFTED } from './set-aside.js';
|
|
16
|
+
function refuse(code, id, why, message) {
|
|
17
|
+
throw new OteaError(code, message, { kind: 'question', item: id, why });
|
|
18
|
+
}
|
|
19
|
+
// The board and the step an agent is doing on the item, or the refusal of a question on it.
|
|
20
|
+
function doing(ctx, item) {
|
|
21
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
22
|
+
const position = positionOf(item.state);
|
|
23
|
+
const step = board && !isClosed(position) && position.phase === 'doing'
|
|
24
|
+
? stepById(board.def, position.step)
|
|
25
|
+
: undefined;
|
|
26
|
+
if (!board || !step || step.doer === 'you')
|
|
27
|
+
refuse('invalid_transition', item.id, 'not_working', `Not asked on ${item.id}: ask only while you do a step on a board`);
|
|
28
|
+
return { board, step: step.id };
|
|
29
|
+
}
|
|
30
|
+
// The agent doing the item's step asks the person: one question at a time, while no other agent
|
|
31
|
+
// holds the item. The question keeps the step and round it was asked in.
|
|
32
|
+
export function askQuestion(ctx, input, now = systemClock) {
|
|
33
|
+
const { id } = input;
|
|
34
|
+
if (input.actor.kind !== 'agent')
|
|
35
|
+
refuse('forbidden', id, 'not_agent', `Not asked: only an agent asks the person on ${id}`);
|
|
36
|
+
const text = input.text.trim();
|
|
37
|
+
if (text === '')
|
|
38
|
+
refuse('reason_required', id, 'empty', `Not asked on ${id}: say your question`);
|
|
39
|
+
return transaction(ctx.db, () => {
|
|
40
|
+
const item = requireWritableItem(ctx.db, id);
|
|
41
|
+
const at = now();
|
|
42
|
+
assertMayWriteItem(ctx.db, item, input.actor, at);
|
|
43
|
+
const { board, step } = doing(ctx, item);
|
|
44
|
+
const on = itemOnBoard(ctx, item, board.def);
|
|
45
|
+
if (on.asking)
|
|
46
|
+
refuse('invalid_transition', item.id, 'asking', `Not asked on ${item.id}: a question already waits for the person ("${on.asking.text}")`);
|
|
47
|
+
const question = Math.max(0, ...on.questions.map(({ question: n }) => n)) + 1;
|
|
48
|
+
appendEvent(ctx.db, {
|
|
49
|
+
itemId: item.id,
|
|
50
|
+
type: QUESTION_ASKED,
|
|
51
|
+
actor: input.actor,
|
|
52
|
+
at,
|
|
53
|
+
data: {
|
|
54
|
+
question,
|
|
55
|
+
text,
|
|
56
|
+
step,
|
|
57
|
+
stepName: stepById(board.def, step)?.name ?? step,
|
|
58
|
+
round: on.round,
|
|
59
|
+
channel: input.channel,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
const asked = questionsOf(selectEvents(ctx.db, item.id)).find((each) => each.question === question);
|
|
63
|
+
if (!asked)
|
|
64
|
+
throw new Error(`question ${question} on ${item.id} was not kept`);
|
|
65
|
+
return { question: asked, text: askedText(item.id) };
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
// The person answers the question that waits: the turn goes back to the agent, in the same round.
|
|
69
|
+
export function answerQuestion(ctx, input, now = systemClock) {
|
|
70
|
+
const { id } = input;
|
|
71
|
+
if (input.actor.kind !== 'human')
|
|
72
|
+
refuse('forbidden', id, 'not_person', `Not answered: the person answers questions on ${id}`);
|
|
73
|
+
const text = input.text.trim();
|
|
74
|
+
if (text === '')
|
|
75
|
+
refuse('reason_required', id, 'empty', `Not answered on ${id}: say your answer`);
|
|
76
|
+
return recordAnswer(ctx, { ...input, text }, {}, now).question;
|
|
77
|
+
}
|
|
78
|
+
// The answer the person gave in the chat instead of in Otea, put on the question by an agent (otea
|
|
79
|
+
// answer, P4-R2-2): it is the person's, as a relayed decision is, marked as noted by that agent,
|
|
80
|
+
// and hands the turn back as an answer given in Otea does.
|
|
81
|
+
export function noteChatAnswer(ctx, input, now = systemClock) {
|
|
82
|
+
const { id, actor } = input;
|
|
83
|
+
if (actor.kind !== 'agent')
|
|
84
|
+
refuse('forbidden', id, 'not_agent', `Not noted: the person answers on the item page; an agent notes an answer they gave in the chat`);
|
|
85
|
+
const text = input.text.trim();
|
|
86
|
+
if (text === '')
|
|
87
|
+
refuse('reason_required', id, 'empty', `Not noted on ${id}: give the person's words`);
|
|
88
|
+
const data = { via: actor.name, source: 'conversation' };
|
|
89
|
+
const { question, gone } = recordAnswer(ctx, { ...input, text, actor: RELAYED_HUMAN }, data, now);
|
|
90
|
+
const asker = question.by === actor.name ? undefined : { name: question.by, gone };
|
|
91
|
+
return { question, text: chatAnswerText(id, asker) };
|
|
92
|
+
}
|
|
93
|
+
// The question answered, and whether the session of the agent that asked it ended (`gone`).
|
|
94
|
+
function recordAnswer(ctx, input, extra, now) {
|
|
95
|
+
const { id, text } = input;
|
|
96
|
+
return transaction(ctx.db, () => {
|
|
97
|
+
const item = requireWritableItem(ctx.db, id);
|
|
98
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
99
|
+
const open = board ? itemOnBoard(ctx, item, board.def).asking : undefined;
|
|
100
|
+
if (!open)
|
|
101
|
+
refuse('invalid_transition', item.id, 'none_open', `${item.id}: no question waits for ${extra.via ? 'the person' : 'you'}`);
|
|
102
|
+
appendEvent(ctx.db, {
|
|
103
|
+
itemId: item.id,
|
|
104
|
+
type: QUESTION_ANSWERED,
|
|
105
|
+
actor: input.actor,
|
|
106
|
+
at: now(),
|
|
107
|
+
data: { question: open.question, text, channel: input.channel, ...extra },
|
|
108
|
+
});
|
|
109
|
+
const answered = questionsOf(selectEvents(ctx.db, item.id)).find((each) => each.question === open.question);
|
|
110
|
+
if (!answered)
|
|
111
|
+
throw new Error(`question ${open.question} on ${item.id} was not kept`);
|
|
112
|
+
// The person's answer takes up an item set aside for later (P7-R3-1).
|
|
113
|
+
const cause = 'answer';
|
|
114
|
+
liftSetAside(ctx.db, { id: item.id, actor: input.actor, at: now(), cause }, extra);
|
|
115
|
+
// The answer goes back to the agent that asked: its lease, run out while the person thought,
|
|
116
|
+
// is taken again for it, unless another agent holds the item now, or its session is no longer
|
|
117
|
+
// alive (P7-R3-11): then the item waits for whichever agent claims it (P3C-R3-11).
|
|
118
|
+
const at = now();
|
|
119
|
+
const lease = activeLease(item.lease, at);
|
|
120
|
+
// The agent that asked, noting the answer from its own chat, is at work this moment.
|
|
121
|
+
const gone = extra.via !== open.by && !sessionAlive(ctx.db, open.by, () => at);
|
|
122
|
+
if (!lease && !gone)
|
|
123
|
+
takeItem(ctx.db, item, { kind: 'agent', name: open.by }, at);
|
|
124
|
+
return { question: answered, gone };
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// An answer this agent has not acted on yet: to a question it asked in the round of the step still
|
|
128
|
+
// being done, with nothing done by it on the item since. otea wait stops on it.
|
|
129
|
+
export function answerWaiting(ctx, agent) {
|
|
130
|
+
const asked = new Set(selectEventsOfTypes(ctx.db, [QUESTION_ASKED])
|
|
131
|
+
.filter(({ actor }) => actor.kind === 'agent' && actor.name === agent)
|
|
132
|
+
.map(({ itemId }) => itemId));
|
|
133
|
+
for (const id of asked) {
|
|
134
|
+
const item = requireItem(ctx.db, id);
|
|
135
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
136
|
+
const position = positionOf(item.state);
|
|
137
|
+
if (!board || isClosed(position) || position.phase !== 'doing')
|
|
138
|
+
continue;
|
|
139
|
+
const on = itemOnBoard(ctx, item, board.def);
|
|
140
|
+
const answered = on.roundQuestions.findLast((q) => q.by === agent && q.answer);
|
|
141
|
+
const at = answered?.answer?.at;
|
|
142
|
+
// An answer it noted from the chat itself is one it knows.
|
|
143
|
+
if (!answered || at === undefined || answered.answer?.via === agent)
|
|
144
|
+
continue;
|
|
145
|
+
const acted = selectEvents(ctx.db, item.id).some((event) => event.at > at && event.actor.kind === 'agent' && event.actor.name === agent);
|
|
146
|
+
if (!acted)
|
|
147
|
+
return { item: item.id, question: answered.question, text: waitAnswerText(item.id) };
|
|
148
|
+
}
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
// The items where a question this agent asked still waits for the person, oldest first. While one
|
|
152
|
+
// does, otea wait keeps the agent on its answer: other free work would wake it only for it to end
|
|
153
|
+
// its turn there, and the answer would go unread.
|
|
154
|
+
export function askedByAgent(ctx, agent) {
|
|
155
|
+
return [...askingItems(ctx.db)]
|
|
156
|
+
.filter(([, open]) => open.by === agent)
|
|
157
|
+
.sort(([, a], [, b]) => a.at.localeCompare(b.at))
|
|
158
|
+
.map(([id]) => id);
|
|
159
|
+
}
|
|
160
|
+
// While a question this agent asked waits for the person, the agent work that still wakes its wait
|
|
161
|
+
// (P4-R2-1): an item sent back that it worked on, and work that reached its state after it first
|
|
162
|
+
// asked. Work already free when it asked does not: it would wake the agent at once, only for it to
|
|
163
|
+
// end its turn there with the answer unread. Undefined while no question of its waits.
|
|
164
|
+
export function wakesWhileAsking(ctx, agent) {
|
|
165
|
+
const asking = askingItems(ctx.db);
|
|
166
|
+
const asked = [...asking.values()]
|
|
167
|
+
.filter((open) => open.by === agent)
|
|
168
|
+
.map(({ at }) => at)
|
|
169
|
+
.sort()[0];
|
|
170
|
+
if (asked === undefined)
|
|
171
|
+
return undefined;
|
|
172
|
+
const entered = selectStateEntries(ctx.db);
|
|
173
|
+
return (item) => {
|
|
174
|
+
// An item a question waits on is the person's until they answer it.
|
|
175
|
+
if (asking.has(item.id))
|
|
176
|
+
return false;
|
|
177
|
+
if ((entered.get(item.id) ?? item.createdAt) > asked)
|
|
178
|
+
return true;
|
|
179
|
+
const record = requireItem(ctx.db, item.id);
|
|
180
|
+
const events = selectEvents(ctx.db, item.id);
|
|
181
|
+
const mine = ({ actor }) => actor.kind === 'agent' && actor.name === agent;
|
|
182
|
+
// What happened since it asked, while it did nothing on the item after it (P7-R3-9): the
|
|
183
|
+
// person took the item up (a note, Retomar, any act of theirs that lifted a set-aside), or,
|
|
184
|
+
// on an item it holds, the parts or blockers it waited for closed: its work again.
|
|
185
|
+
const since = (at) => at !== undefined && at > asked && !events.some((event) => event.at > at && mine(event));
|
|
186
|
+
if (since(events.findLast(tookUp)?.at))
|
|
187
|
+
return true;
|
|
188
|
+
if (record.lease?.holder === agent && since(waitsClosedAt(ctx, item.id)))
|
|
189
|
+
return true;
|
|
190
|
+
const board = liveBoardOf(ctx, record.collection);
|
|
191
|
+
if (!board || itemOnBoard(ctx, record, board.def).sentBack === undefined)
|
|
192
|
+
return false;
|
|
193
|
+
return events.some(mine);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// The person's own acts that take an item up: a note, or what lifted a set-aside (P7-R3-9).
|
|
197
|
+
function tookUp({ type, actor }) {
|
|
198
|
+
return actor.kind === 'human' && (type === NOTE_LEFT || type === SET_ASIDE_LIFTED);
|
|
199
|
+
}
|
|
200
|
+
// When the last of the item's parts and blockers closed, while every one of them is closed;
|
|
201
|
+
// undefined while one is open or it has none.
|
|
202
|
+
function waitsClosedAt(ctx, id) {
|
|
203
|
+
let last;
|
|
204
|
+
for (const relation of selectRelationsOf(ctx.db, id)) {
|
|
205
|
+
if (relation.to !== id || (relation.kind !== 'parent' && relation.kind !== 'blocks'))
|
|
206
|
+
continue;
|
|
207
|
+
const other = requireItem(ctx.db, relation.from);
|
|
208
|
+
if (!isClosed(positionOf(other.state)))
|
|
209
|
+
return undefined;
|
|
210
|
+
const closed = selectEvents(ctx.db, other.id).findLast(({ type }) => type === 'transitioned');
|
|
211
|
+
if (closed && (last === undefined || closed.at > last))
|
|
212
|
+
last = closed.at;
|
|
213
|
+
}
|
|
214
|
+
return last;
|
|
215
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { TITLE } from '../core/board/model.js';
|
|
2
|
+
import { findState, readFieldInput, stateName, stateNames } from '../core/board/reading.js';
|
|
3
|
+
import { OteaError } from '../core/errors.js';
|
|
4
|
+
import { listCollections } from '../store/queries.js';
|
|
5
|
+
import { collectionNamed } from './collections.js';
|
|
6
|
+
import { liveBoardOf } from './next.js';
|
|
7
|
+
// The test of an item on one board; refused, naming what the board has, when the board has no
|
|
8
|
+
// such step or field, or no such choice.
|
|
9
|
+
function boardTest(def, { state, fields }) {
|
|
10
|
+
let named;
|
|
11
|
+
if (state !== undefined && def) {
|
|
12
|
+
const found = findState(def, state);
|
|
13
|
+
if (found === undefined)
|
|
14
|
+
throw new OteaError('not_found', `${def.name} has no step "${state}"; its steps are ${stateNames(def).join(', ')}`);
|
|
15
|
+
named = stateName(def, found);
|
|
16
|
+
}
|
|
17
|
+
const values = fields && def ? readFieldInput(def, fields) : (fields ?? {});
|
|
18
|
+
// By its step's name, so a discard with a motive is Dropped as well.
|
|
19
|
+
const atState = (item) => state === undefined || (def ? stateName(def, item.state) === named : item.state === state);
|
|
20
|
+
return (item) => atState(item) &&
|
|
21
|
+
Object.entries(values).every(([key, value]) => (key === TITLE ? item.title : item.fields[key]) === value);
|
|
22
|
+
}
|
|
23
|
+
// The items a reader's filter keeps, each read against its own board: without a collection, the
|
|
24
|
+
// boards that have no such step or field keep none of theirs. Refused when none has it.
|
|
25
|
+
export function readerFilter(ctx, filter) {
|
|
26
|
+
if (filter.state === undefined && filter.fields === undefined)
|
|
27
|
+
return () => true;
|
|
28
|
+
const { collection } = filter;
|
|
29
|
+
const names = collection === undefined
|
|
30
|
+
? listCollections(ctx.db).map(({ name }) => name)
|
|
31
|
+
: [collectionNamed(ctx.db, collection)];
|
|
32
|
+
const tests = new Map();
|
|
33
|
+
const refusals = [];
|
|
34
|
+
for (const name of names) {
|
|
35
|
+
try {
|
|
36
|
+
tests.set(name, boardTest(liveBoardOf(ctx, name)?.def, filter));
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (!(error instanceof OteaError))
|
|
40
|
+
throw error;
|
|
41
|
+
refusals.push(error);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const [first] = refusals;
|
|
45
|
+
if (tests.size === 0 && first !== undefined)
|
|
46
|
+
throw noBoardHas(ctx, names, filter) ?? first;
|
|
47
|
+
return (item) => tests.get(item.collection)?.(item) ?? false;
|
|
48
|
+
}
|
|
49
|
+
// Across boards, a step none of them has, with the steps they have.
|
|
50
|
+
function noBoardHas(ctx, names, { state }) {
|
|
51
|
+
const defs = names.flatMap((name) => liveBoardOf(ctx, name)?.def ?? []);
|
|
52
|
+
if (state === undefined || defs.length < 2)
|
|
53
|
+
return undefined;
|
|
54
|
+
if (defs.some((def) => findState(def, state) !== undefined))
|
|
55
|
+
return undefined;
|
|
56
|
+
const steps = [...new Set(defs.flatMap(stateNames))].join(', ');
|
|
57
|
+
return new OteaError('not_found', `No board has a step "${state}"; the steps are ${steps}`);
|
|
58
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { partRule } from '../core/board/joins.js';
|
|
2
|
+
import { parentTypeText, partsAtHomeText } from '../core/board/next.js';
|
|
3
|
+
import { OteaError } from '../core/errors.js';
|
|
4
|
+
import { assertTakesChild } from '../core/hierarchy.js';
|
|
5
|
+
import { assertBlockersClosed, assertCanRelate, assertChildrenClosed, childrenSummary, matchesRelation, normalizeRelation, parseRelationKind, RELATION_VIEWS, viewOf, } from '../core/relations.js';
|
|
6
|
+
import { requirementsOf } from '../core/workflow.js';
|
|
7
|
+
import { transaction } from '../store/database.js';
|
|
8
|
+
import { appendEvent, deleteRelation, insertRelation, selectRelations, selectRelationsOf, } from '../store/queries.js';
|
|
9
|
+
import { typeOfItem } from './collections.js';
|
|
10
|
+
import { present, requireItem, requireWritableItem, systemClock, workflowOf, } from './items.js';
|
|
11
|
+
import { liveBoardOf, partsBoardOf } from './next.js';
|
|
12
|
+
function presented(ctx, record, at) {
|
|
13
|
+
return present(record, workflowOf(ctx, record), at);
|
|
14
|
+
}
|
|
15
|
+
function recordRelation(db, type, relation, who, at) {
|
|
16
|
+
const data = { kind: relation.kind, from: relation.from, to: relation.to, channel: who.channel };
|
|
17
|
+
for (const itemId of new Set([relation.from, relation.to])) {
|
|
18
|
+
appendEvent(db, { itemId, type, actor: who.actor, at, data });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// A parent's parts live on the board it divides into ("se divide en"), or, on a board that divides
|
|
22
|
+
// into nothing, on its own board: a part on another board is refused. What waits on another board
|
|
23
|
+
// says so with blocks; what delivers it, with implements; those links are free between boards.
|
|
24
|
+
function assertMayBePart(ctx, from, to) {
|
|
25
|
+
const parent = liveBoardOf(ctx, to.collection);
|
|
26
|
+
const child = liveBoardOf(ctx, from.collection);
|
|
27
|
+
const rule = parent && child
|
|
28
|
+
? partRule(parent, child)
|
|
29
|
+
: from.collection === to.collection
|
|
30
|
+
? 'ok'
|
|
31
|
+
: 'across_boards';
|
|
32
|
+
if (rule === 'ok')
|
|
33
|
+
return;
|
|
34
|
+
const into = parent ? partsBoardOf(ctx, parent) : undefined;
|
|
35
|
+
if (rule === 'parent_type' && parent && into)
|
|
36
|
+
throw new OteaError('parent_type', parentTypeText(parent.def, into.def, from.id, child?.def.name ?? from.collection), { kind: 'parent_board', item: from.id, parent: to.id, board: into.def.name });
|
|
37
|
+
if (parent && child)
|
|
38
|
+
throw new OteaError('parent_across_boards', partsAtHomeText(parent.def, to.id, from.id, child.def.name), { kind: 'parent_board', item: from.id, parent: to.id, board: parent.def.name });
|
|
39
|
+
throw new OteaError('parent_across_boards', `${from.id} (${from.collection}) cannot be a child of ${to.id} (${to.collection}): a parent lives in the same collection; link ${from.id} blocks ${to.id} to make ${to.id} wait, or ${from.id} implements ${to.id}`, { kind: 'parent_board', item: from.id, parent: to.id });
|
|
40
|
+
}
|
|
41
|
+
// Stores a relation inside an open transaction, once both items exist and the core rules hold. A
|
|
42
|
+
// parent lives in its child's collection, and an open item cannot get a closed parent: a closed
|
|
43
|
+
// parent stays with every child it closed with. In a preset that declares a hierarchy, a parent
|
|
44
|
+
// takes only the child types its type lists.
|
|
45
|
+
export function addRelation(ctx, given, who, at) {
|
|
46
|
+
const { db } = ctx;
|
|
47
|
+
const relation = normalizeRelation(given);
|
|
48
|
+
const from = requireWritableItem(db, relation.from);
|
|
49
|
+
const to = requireWritableItem(db, relation.to);
|
|
50
|
+
if (relation.kind === 'parent')
|
|
51
|
+
assertMayBePart(ctx, from, to);
|
|
52
|
+
assertCanRelate(selectRelations(db), relation);
|
|
53
|
+
if (relation.kind === 'parent') {
|
|
54
|
+
assertTakesChild(to.id, typeOfItem(ctx, to), from.type);
|
|
55
|
+
const parent = presented(ctx, to, at);
|
|
56
|
+
if (parent.closes !== undefined && presented(ctx, from, at).closes === undefined) {
|
|
57
|
+
throw new OteaError('item_closed', `${to.id} is ${to.state} (closed) and takes no open child; reopen it or link with relates`, { kind: 'parent_closed', parent: to.id });
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const record = { ...relation, createdAt: at, actor: who.actor, channel: who.channel };
|
|
61
|
+
insertRelation(db, record);
|
|
62
|
+
recordRelation(db, 'relation_added', relation, who, at);
|
|
63
|
+
return record;
|
|
64
|
+
}
|
|
65
|
+
function relationOf(input) {
|
|
66
|
+
return { kind: parseRelationKind(input.kind), from: input.id, to: input.other };
|
|
67
|
+
}
|
|
68
|
+
// Agents and humans link alike. A relation changes neither item's fields nor state, so it is not
|
|
69
|
+
// bound by leases and links closed items too.
|
|
70
|
+
export function linkItems(ctx, input, now = systemClock) {
|
|
71
|
+
const relation = relationOf(input);
|
|
72
|
+
const { actor, channel } = input;
|
|
73
|
+
return transaction(ctx.db, () => addRelation(ctx, relation, { actor, channel }, now()));
|
|
74
|
+
}
|
|
75
|
+
export function unlinkItems(ctx, input, now = systemClock) {
|
|
76
|
+
const given = relationOf(input);
|
|
77
|
+
const { db } = ctx;
|
|
78
|
+
return transaction(db, () => {
|
|
79
|
+
requireWritableItem(db, given.from);
|
|
80
|
+
requireWritableItem(db, given.to);
|
|
81
|
+
const stored = selectRelations(db, given.kind).find((relation) => matchesRelation(relation, given));
|
|
82
|
+
if (!stored) {
|
|
83
|
+
const text = `${given.from} ${given.kind} ${given.to}`;
|
|
84
|
+
throw new OteaError('not_found', `No relation "${text}" to remove`, { kind: 'no_link' });
|
|
85
|
+
}
|
|
86
|
+
const relation = { kind: stored.kind, from: stored.from, to: stored.to };
|
|
87
|
+
deleteRelation(db, relation);
|
|
88
|
+
recordRelation(db, 'relation_removed', relation, input, now());
|
|
89
|
+
return relation;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
export function listRelations(ctx) {
|
|
93
|
+
return selectRelations(ctx.db);
|
|
94
|
+
}
|
|
95
|
+
const VIEW_ORDER = new Map(RELATION_VIEWS.map((kind, index) => [kind, index]));
|
|
96
|
+
// An item's relations as it reads them, grouped by kind in a fixed order, oldest first within each.
|
|
97
|
+
export function refsOf(relations, id) {
|
|
98
|
+
return relations
|
|
99
|
+
.filter(({ from, to }) => from === id || to === id)
|
|
100
|
+
.map((relation) => viewOf(relation, id))
|
|
101
|
+
.sort((a, b) => (VIEW_ORDER.get(a.kind) ?? 0) - (VIEW_ORDER.get(b.kind) ?? 0));
|
|
102
|
+
}
|
|
103
|
+
export function relatedItems(ctx, id, at) {
|
|
104
|
+
return refsOf(selectRelationsOf(ctx.db, id), id).map((ref) => {
|
|
105
|
+
const other = presented(ctx, requireItem(ctx.db, ref.id), at);
|
|
106
|
+
const related = {
|
|
107
|
+
...ref,
|
|
108
|
+
collection: other.collection,
|
|
109
|
+
type: other.type,
|
|
110
|
+
title: other.title,
|
|
111
|
+
state: other.state,
|
|
112
|
+
turn: other.turn,
|
|
113
|
+
lease: other.lease,
|
|
114
|
+
};
|
|
115
|
+
if (other.closes !== undefined)
|
|
116
|
+
related.closes = other.closes;
|
|
117
|
+
return related;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
// The items that block one and are still open, from its relations as `otea show` reads them.
|
|
121
|
+
export function openBlockers(related) {
|
|
122
|
+
return related
|
|
123
|
+
.filter(({ kind, closes }) => kind === 'blocked_by' && closes === undefined)
|
|
124
|
+
.map(({ id }) => id);
|
|
125
|
+
}
|
|
126
|
+
export function childIds(relations, id) {
|
|
127
|
+
return relations.filter(({ kind, to }) => kind === 'parent' && to === id).map(({ from }) => from);
|
|
128
|
+
}
|
|
129
|
+
export function childrenOf(ctx, id, at) {
|
|
130
|
+
return childIds(selectRelations(ctx.db, 'parent'), id).map((child) => presented(ctx, requireItem(ctx.db, child), at));
|
|
131
|
+
}
|
|
132
|
+
export function blockersOf(ctx, id, at) {
|
|
133
|
+
return selectRelations(ctx.db, 'blocks')
|
|
134
|
+
.filter(({ to }) => to === id)
|
|
135
|
+
.map(({ from }) => presented(ctx, requireItem(ctx.db, from), at));
|
|
136
|
+
}
|
|
137
|
+
// A move into a state that declares requires: children_closed waits for every child, and one that
|
|
138
|
+
// declares blockers_closed for everything that blocks the item, whatever the state's turn: every way
|
|
139
|
+
// in (a UI move, an agent move, a relay) passes through here.
|
|
140
|
+
export function assertMayEnter(ctx, item, workflow, to, at) {
|
|
141
|
+
const requires = requirementsOf(workflow, to);
|
|
142
|
+
if (requires.includes('children_closed')) {
|
|
143
|
+
assertChildrenClosed(item.id, to, childrenOf(ctx, item.id, at));
|
|
144
|
+
}
|
|
145
|
+
if (requires.includes('blockers_closed')) {
|
|
146
|
+
assertBlockersClosed(item.id, to, blockersOf(ctx, item.id, at));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Each item with its relations and, for a parent, its children summary, only when it has them.
|
|
150
|
+
export function withRelations(items, relations, closingOf) {
|
|
151
|
+
if (relations.length === 0)
|
|
152
|
+
return [...items];
|
|
153
|
+
return items.map((item) => {
|
|
154
|
+
const refs = refsOf(relations, item.id);
|
|
155
|
+
if (refs.length === 0)
|
|
156
|
+
return item;
|
|
157
|
+
const children = childIds(relations, item.id).map(closingOf);
|
|
158
|
+
return {
|
|
159
|
+
...item,
|
|
160
|
+
relations: refs,
|
|
161
|
+
...(children.length > 0 ? { children: childrenSummary(children) } : {}),
|
|
162
|
+
};
|
|
163
|
+
});
|
|
164
|
+
}
|