@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,319 @@
|
|
|
1
|
+
import { deliverables, isClosed, positionOf } from '../core/board/engine.js';
|
|
2
|
+
import { SAME, stepById } from '../core/board/model.js';
|
|
3
|
+
import { NOTE_ANSWERED, NOTE_EDITED, NOTE_LEFT, NOTE_WITHDRAWN, newNotesText, notesOf, repliedText, unansweredOf, waitNoteText, } from '../core/board/notes.js';
|
|
4
|
+
import { OteaError } from '../core/errors.js';
|
|
5
|
+
import { activeLease } from '../core/lease.js';
|
|
6
|
+
import { transaction } from '../store/database.js';
|
|
7
|
+
import { appendEvent, findEvidence, selectEvents, selectLeases, } from '../store/queries.js';
|
|
8
|
+
import { requireItem, requireWritableItem, systemClock } from './items.js';
|
|
9
|
+
import { assertMayWriteItem } from './lease-take.js';
|
|
10
|
+
import { itemOnBoard, liveBoardOf, workerOf } from './next.js';
|
|
11
|
+
import { liftSetAside } from './set-aside.js';
|
|
12
|
+
function refuse(code, id, why, message) {
|
|
13
|
+
throw new OteaError(code, message, { kind: 'note', item: id, why });
|
|
14
|
+
}
|
|
15
|
+
// The board and the step a note goes to, or the refusal of a note on the item: the step an agent
|
|
16
|
+
// is doing; or the step the person reviews, where the note is a review comment for the round a
|
|
17
|
+
// send-back would start (`review`).
|
|
18
|
+
function workedOn(ctx, item) {
|
|
19
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
20
|
+
const at = positionOf(item.state);
|
|
21
|
+
const position = isClosed(at) ? undefined : at;
|
|
22
|
+
const step = board && position ? stepById(board.def, position.step) : undefined;
|
|
23
|
+
if (board && step && position?.phase === 'doing' && step.doer !== 'you')
|
|
24
|
+
return { board, step: step.id };
|
|
25
|
+
if (board && step && position?.phase === 'review' && step.reviewer === 'you') {
|
|
26
|
+
const back = step.back === SAME ? step.id : step.back;
|
|
27
|
+
const { rounds } = itemOnBoard(ctx, item, board.def);
|
|
28
|
+
return {
|
|
29
|
+
board,
|
|
30
|
+
step: step.id,
|
|
31
|
+
review: { for: { step: back, round: (rounds.rounds[back] ?? 0) + 1 } },
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return refuse('invalid_transition', item.id, 'not_working', `No note left on ${item.id}: notes go to an agent while it does a step, or with your review of one, and neither is happening on it`);
|
|
35
|
+
}
|
|
36
|
+
// A line quoted, or a node named, as seen: trimmed, and never longer than an agent needs to find it.
|
|
37
|
+
const QUOTE_MAX = 200;
|
|
38
|
+
const NODE_MAX = 120;
|
|
39
|
+
function clip(text, max) {
|
|
40
|
+
const trimmed = text?.trim();
|
|
41
|
+
return trimmed ? trimmed.slice(0, max) : undefined;
|
|
42
|
+
}
|
|
43
|
+
function anchorFor(ctx, item, board, given) {
|
|
44
|
+
// Any file of the item, of this round or an earlier step's: the reader lists them all.
|
|
45
|
+
const evidence = findEvidence(ctx.db, given.evidence);
|
|
46
|
+
const { seconds, line, page } = given;
|
|
47
|
+
const fits = evidence !== undefined &&
|
|
48
|
+
evidence.itemId === item.id &&
|
|
49
|
+
(seconds === undefined || (Number.isFinite(seconds) && seconds >= 0)) &&
|
|
50
|
+
(line === undefined || (Number.isInteger(line) && line > 0)) &&
|
|
51
|
+
(page === undefined || (Number.isInteger(page) && page > 0));
|
|
52
|
+
if (!fits)
|
|
53
|
+
refuse('invalid_fields', item.id, 'anchor', `No note left on ${item.id}: evidence #${given.evidence} is not attached to it`);
|
|
54
|
+
const spot = spotOf(given);
|
|
55
|
+
if (spot === null)
|
|
56
|
+
refuse('invalid_fields', item.id, 'anchor', `No note left on ${item.id}: a point needs x and y between 0 and 100, and a zone a width and a height that keep it inside the image`);
|
|
57
|
+
const part = evidence.module ?? undefined;
|
|
58
|
+
const partName = part === undefined ? undefined : board.def.parts[part]?.name;
|
|
59
|
+
const quote = clip(given.quote, QUOTE_MAX);
|
|
60
|
+
const node = clip(given.node, NODE_MAX);
|
|
61
|
+
return {
|
|
62
|
+
evidence: evidence.id,
|
|
63
|
+
filename: evidence.filename,
|
|
64
|
+
...(part === undefined ? {} : { part }),
|
|
65
|
+
...(partName === undefined ? {} : { partName }),
|
|
66
|
+
...(seconds === undefined ? {} : { seconds }),
|
|
67
|
+
...(line === undefined ? {} : { line }),
|
|
68
|
+
...(quote === undefined ? {} : { quote }),
|
|
69
|
+
...(quote !== undefined && given.whole === true ? { whole: true } : {}),
|
|
70
|
+
...(node === undefined ? {} : { node }),
|
|
71
|
+
...(page === undefined ? {} : { page }),
|
|
72
|
+
...spot,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const tenth = (n) => Math.round(n * 10) / 10;
|
|
76
|
+
// The point or zone of an image a note is on, to a tenth of a percent; {} when it is on none, null
|
|
77
|
+
// when what was given is no point or zone inside the image.
|
|
78
|
+
function spotOf(given) {
|
|
79
|
+
const { x, y, w, h } = given;
|
|
80
|
+
if (x === undefined && y === undefined && w === undefined && h === undefined)
|
|
81
|
+
return {};
|
|
82
|
+
const inside = (n) => typeof n === 'number' && Number.isFinite(n) && n >= 0 && n <= 100;
|
|
83
|
+
if (!inside(x) || !inside(y))
|
|
84
|
+
return null;
|
|
85
|
+
if (w === undefined && h === undefined)
|
|
86
|
+
return { x: tenth(x), y: tenth(y) };
|
|
87
|
+
if (!inside(w) || !inside(h) || w <= 0 || h <= 0 || x + w > 100.05 || y + h > 100.05)
|
|
88
|
+
return null;
|
|
89
|
+
return { x: tenth(x), y: tenth(y), w: tenth(w), h: tenth(h) };
|
|
90
|
+
}
|
|
91
|
+
// The person leaves a note on an item an agent is doing: the step, its round and the lease stay as
|
|
92
|
+
// they are. The note keeps its moment: the step and round, how many of the step's required
|
|
93
|
+
// deliverables were in, the agent at work and the evidence it is on.
|
|
94
|
+
export function leaveNote(ctx, input, now = systemClock) {
|
|
95
|
+
const { id } = input;
|
|
96
|
+
if (input.actor.kind !== 'human')
|
|
97
|
+
refuse('forbidden', id, 'not_person', `No note left on ${id}: only the person leaves notes. To ask the person something: otea ask ${id} "<question>"`);
|
|
98
|
+
const text = input.text.trim();
|
|
99
|
+
if (text === '')
|
|
100
|
+
refuse('reason_required', id, 'empty', `No note left on ${id}: it has no text`);
|
|
101
|
+
return transaction(ctx.db, () => {
|
|
102
|
+
const item = requireWritableItem(ctx.db, id);
|
|
103
|
+
const at = now();
|
|
104
|
+
const { board, step, review } = workedOn(ctx, item);
|
|
105
|
+
const on = itemOnBoard(ctx, item, board.def);
|
|
106
|
+
const anchor = input.anchor === undefined ? undefined : anchorFor(ctx, item, board, input.anchor);
|
|
107
|
+
// Numbered past every note ever left, a withdrawn one too: a number never names two notes.
|
|
108
|
+
const note = Math.max(0, ...leftNumbers(ctx, item.id)) + 1;
|
|
109
|
+
const to = activeLease(item.lease, at)?.holder;
|
|
110
|
+
appendEvent(ctx.db, {
|
|
111
|
+
itemId: item.id,
|
|
112
|
+
type: NOTE_LEFT,
|
|
113
|
+
actor: input.actor,
|
|
114
|
+
at,
|
|
115
|
+
data: {
|
|
116
|
+
note,
|
|
117
|
+
text,
|
|
118
|
+
step,
|
|
119
|
+
stepName: stepById(board.def, step)?.name ?? step,
|
|
120
|
+
round: on.round,
|
|
121
|
+
delivered: deliverables(board.def, step, on.facts),
|
|
122
|
+
...(to === undefined ? {} : { to }),
|
|
123
|
+
...(anchor === undefined ? {} : { anchor }),
|
|
124
|
+
...(review === undefined ? {} : { review }),
|
|
125
|
+
channel: input.channel,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
// A note on an item an agent set aside is the person taking it up again (P7-R3-1).
|
|
129
|
+
liftSetAside(ctx.db, { id: item.id, actor: input.actor, at, cause: 'note' }, { note });
|
|
130
|
+
const left = notesOf(selectEvents(ctx.db, item.id)).find((each) => each.note === note);
|
|
131
|
+
if (!left)
|
|
132
|
+
throw new Error(`note ${note} on ${item.id} was not kept`);
|
|
133
|
+
return left;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function leftNumbers(ctx, id) {
|
|
137
|
+
return selectEvents(ctx.db, id)
|
|
138
|
+
.filter(({ type, data }) => type === NOTE_LEFT && typeof data.note === 'number')
|
|
139
|
+
.map(({ data }) => Number(data.note));
|
|
140
|
+
}
|
|
141
|
+
// A review comment of the review still open, the one the person may change or take back: once a
|
|
142
|
+
// send-back took it to the agent, or an approval kept it, it stays as it went.
|
|
143
|
+
function pendingComment(ctx, item, n, actor) {
|
|
144
|
+
if (actor.kind !== 'human')
|
|
145
|
+
refuse('forbidden', item.id, 'not_person', `Note ${n} on ${item.id} unchanged: only the person changes their notes`);
|
|
146
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
147
|
+
const on = board ? itemOnBoard(ctx, item, board.def) : undefined;
|
|
148
|
+
const note = on?.thread.find((each) => each.note === n);
|
|
149
|
+
if (!on || !note)
|
|
150
|
+
throw new OteaError('not_found', `${item.id} has no note ${n}`);
|
|
151
|
+
const open = note.review !== undefined &&
|
|
152
|
+
!isClosed(on.position) &&
|
|
153
|
+
on.position.phase === 'review' &&
|
|
154
|
+
on.position.step === note.step &&
|
|
155
|
+
on.round === note.round;
|
|
156
|
+
if (!open)
|
|
157
|
+
refuse('invalid_transition', item.id, 'not_working', `Note ${n} on ${item.id} unchanged: it already went with the review it was left in`);
|
|
158
|
+
return note;
|
|
159
|
+
}
|
|
160
|
+
// The person changes the words of a review comment while the review is open; where it is stays.
|
|
161
|
+
export function editNote(ctx, input, now = systemClock) {
|
|
162
|
+
const text = input.text.trim();
|
|
163
|
+
if (text === '')
|
|
164
|
+
refuse('reason_required', input.id, 'empty', `Note ${input.note} on ${input.id} unchanged: it has no text`);
|
|
165
|
+
return transaction(ctx.db, () => {
|
|
166
|
+
const item = requireWritableItem(ctx.db, input.id);
|
|
167
|
+
pendingComment(ctx, item, input.note, input.actor);
|
|
168
|
+
appendEvent(ctx.db, {
|
|
169
|
+
itemId: item.id,
|
|
170
|
+
type: NOTE_EDITED,
|
|
171
|
+
actor: input.actor,
|
|
172
|
+
at: now(),
|
|
173
|
+
data: { note: input.note, text, channel: input.channel },
|
|
174
|
+
});
|
|
175
|
+
const note = notesOf(selectEvents(ctx.db, item.id)).find((each) => each.note === input.note);
|
|
176
|
+
if (!note)
|
|
177
|
+
throw new Error(`note ${input.note} on ${item.id} was not kept`);
|
|
178
|
+
return note;
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
// The person takes back a review comment while the review is open: it never reaches the agent.
|
|
182
|
+
export function withdrawNote(ctx, input, now = systemClock) {
|
|
183
|
+
transaction(ctx.db, () => {
|
|
184
|
+
const item = requireWritableItem(ctx.db, input.id);
|
|
185
|
+
pendingComment(ctx, item, input.note, input.actor);
|
|
186
|
+
appendEvent(ctx.db, {
|
|
187
|
+
itemId: item.id,
|
|
188
|
+
type: NOTE_WITHDRAWN,
|
|
189
|
+
actor: input.actor,
|
|
190
|
+
at: now(),
|
|
191
|
+
data: { note: input.note, channel: input.channel },
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
// The notes of the round a note was left in.
|
|
196
|
+
function roundOf(thread, note) {
|
|
197
|
+
return thread.filter(({ step, round }) => step === note.step && round === note.round);
|
|
198
|
+
}
|
|
199
|
+
// The agent answers a note: once, while no other agent holds the item. The answer says what of the
|
|
200
|
+
// round is still unanswered, or that the item may be handed off.
|
|
201
|
+
export function replyNote(ctx, input, now = systemClock) {
|
|
202
|
+
const { id } = input;
|
|
203
|
+
if (input.actor.kind !== 'agent')
|
|
204
|
+
refuse('forbidden', id, 'not_agent', `Not answered: the agent answers the notes on ${id}`);
|
|
205
|
+
return transaction(ctx.db, () => {
|
|
206
|
+
const item = requireWritableItem(ctx.db, id);
|
|
207
|
+
const at = now();
|
|
208
|
+
const left = notesOf(selectEvents(ctx.db, item.id)).find(({ note }) => note === input.note);
|
|
209
|
+
if (!left)
|
|
210
|
+
throw new OteaError('not_found', `${item.id} has no note ${input.note}: the person's notes are numbered (next and show print them), and a send-back reason is not one: address it, then say what you did in the note of your next handoff`);
|
|
211
|
+
if (left.answer)
|
|
212
|
+
throw new OteaError('invalid_transition', `Note ${input.note} on ${item.id} is already answered`);
|
|
213
|
+
assertMayWriteItem(ctx.db, item, input.actor, at);
|
|
214
|
+
const text = input.text.trim();
|
|
215
|
+
if (text === '')
|
|
216
|
+
refuse('reason_required', id, 'empty', `Not answered: say your answer to note ${input.note}`);
|
|
217
|
+
appendEvent(ctx.db, {
|
|
218
|
+
itemId: item.id,
|
|
219
|
+
type: NOTE_ANSWERED,
|
|
220
|
+
actor: input.actor,
|
|
221
|
+
at,
|
|
222
|
+
data: { note: input.note, text, channel: input.channel },
|
|
223
|
+
});
|
|
224
|
+
const thread = notesOf(selectEvents(ctx.db, item.id));
|
|
225
|
+
const note = thread.find((each) => each.note === input.note) ?? left;
|
|
226
|
+
const unanswered = unansweredOf(roundOf(thread, note));
|
|
227
|
+
return { note, unanswered, text: repliedText(item.id, input.note, unanswered) };
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
// The notes still unanswered in the round of the step being done.
|
|
231
|
+
function waiting(ctx, item) {
|
|
232
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
233
|
+
if (!board)
|
|
234
|
+
return [];
|
|
235
|
+
const on = itemOnBoard(ctx, item, board.def);
|
|
236
|
+
if (isClosed(on.position) || on.position.phase !== 'doing')
|
|
237
|
+
return [];
|
|
238
|
+
return on.roundNotes.filter(({ answer }) => !answer);
|
|
239
|
+
}
|
|
240
|
+
// What a heartbeat or an attach adds while notes of the round wait for an answer: the text block
|
|
241
|
+
// and the notes; undefined when none waits.
|
|
242
|
+
export function agentNotes(ctx, id) {
|
|
243
|
+
const open = waiting(ctx, requireItem(ctx.db, id));
|
|
244
|
+
if (open.length === 0)
|
|
245
|
+
return undefined;
|
|
246
|
+
return { text: newNotesText(id, open), notes: open };
|
|
247
|
+
}
|
|
248
|
+
// A note waiting for an answer from this agent: on an item it holds (its lease, even run out),
|
|
249
|
+
// while the step is being done. otea wait stops on it.
|
|
250
|
+
export function noteWaiting(ctx, agent) {
|
|
251
|
+
for (const { id, lease } of selectLeases(ctx.db)) {
|
|
252
|
+
if (lease.holder !== agent)
|
|
253
|
+
continue;
|
|
254
|
+
const open = waiting(ctx, requireItem(ctx.db, id));
|
|
255
|
+
if (open.length === 0)
|
|
256
|
+
continue;
|
|
257
|
+
const unanswered = open.map(({ note }) => note);
|
|
258
|
+
return { item: id, unanswered, notes: open, text: waitNoteText(id, open) };
|
|
259
|
+
}
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
// The item's notes for the page: every round's, or those of a step (its name or id; the step it
|
|
263
|
+
// stands on when only a round is given) and round; with the agent at work and its last sign of life.
|
|
264
|
+
export function notesView(ctx, id, filter = {}, now = systemClock) {
|
|
265
|
+
const item = requireItem(ctx.db, id);
|
|
266
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
267
|
+
if (!board)
|
|
268
|
+
return { item: item.id, notes: [], worker: null };
|
|
269
|
+
const on = itemOnBoard(ctx, item, board.def);
|
|
270
|
+
const wanted = filter.step?.trim().toLocaleLowerCase();
|
|
271
|
+
const current = isClosed(on.position) ? undefined : on.position.step;
|
|
272
|
+
const stepNamed = (id) => stepById(board.def, id)?.name.toLocaleLowerCase() ?? id;
|
|
273
|
+
// A note is in a step and round where it was left, or, a review comment sent back, in the round
|
|
274
|
+
// it went to.
|
|
275
|
+
const lies = (stepId, name, round) => {
|
|
276
|
+
if (wanted !== undefined) {
|
|
277
|
+
if (stepId !== filter.step && name !== wanted)
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
else if (filter.round !== undefined && current !== undefined && stepId !== current)
|
|
281
|
+
return false;
|
|
282
|
+
return filter.round === undefined || round === filter.round;
|
|
283
|
+
};
|
|
284
|
+
const notes = on.thread.filter((note) => lies(note.step, note.stepName.toLocaleLowerCase(), note.round) ||
|
|
285
|
+
(note.review !== undefined &&
|
|
286
|
+
lies(note.review.for.step, stepNamed(note.review.for.step), note.review.for.round)));
|
|
287
|
+
return { item: item.id, notes, worker: workerOf(ctx, item, board.def, now()) ?? null };
|
|
288
|
+
}
|
|
289
|
+
export function notesSummary(ctx, item, boards) {
|
|
290
|
+
const board = boards === undefined
|
|
291
|
+
? liveBoardOf(ctx, item.collection)
|
|
292
|
+
: boards.find(({ collection }) => collection === item.collection);
|
|
293
|
+
if (!board)
|
|
294
|
+
return undefined;
|
|
295
|
+
const { roundNotes } = itemOnBoard(ctx, item, board.def);
|
|
296
|
+
if (roundNotes.length === 0)
|
|
297
|
+
return undefined;
|
|
298
|
+
const latest = roundNotes
|
|
299
|
+
.filter((note) => note.answer !== undefined)
|
|
300
|
+
.sort((a, b) => (a.answer?.at ?? '').localeCompare(b.answer?.at ?? ''))
|
|
301
|
+
.at(-1);
|
|
302
|
+
// Only while the agent does the step is a note waiting for its answer: comments left while you
|
|
303
|
+
// review go with the send-back, and a list counted them «sin respuesta» before (P5-8).
|
|
304
|
+
const position = positionOf(item.state);
|
|
305
|
+
const doing = !isClosed(position) && position.phase === 'doing';
|
|
306
|
+
return {
|
|
307
|
+
count: roundNotes.length,
|
|
308
|
+
unanswered: doing ? unansweredOf(roundNotes).length : 0,
|
|
309
|
+
...(latest?.answer
|
|
310
|
+
? { replied: { note: latest.note, by: latest.answer.by, at: latest.answer.at } }
|
|
311
|
+
: {}),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
// The notes of the round of the step the item stands on (show gives them as data).
|
|
315
|
+
export function roundNotesOf(ctx, id) {
|
|
316
|
+
const item = requireItem(ctx.db, id);
|
|
317
|
+
const board = liveBoardOf(ctx, item.collection);
|
|
318
|
+
return board ? itemOnBoard(ctx, item, board.def).roundNotes : [];
|
|
319
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { OteaError } from '../core/errors.js';
|
|
2
|
+
import { DEFAULT_PREFS, TIME_PATTERN } from '../core/notify/planner.js';
|
|
3
|
+
import { NOTIFY_KINDS } from '../core/notify/signals.js';
|
|
4
|
+
import { isNoticeLang } from '../core/notify/text.js';
|
|
5
|
+
// Who is told when something becomes yours, and how they want it (see core/notify):
|
|
6
|
+
// - a paired device that subscribed to Web Push from Preferencias › Avisos; the subscription goes
|
|
7
|
+
// with the device when it is unpaired (ON DELETE CASCADE);
|
|
8
|
+
// - the desktop app, which shows the same notices as the system's own notifications; it has no
|
|
9
|
+
// subscription, only its prefs.
|
|
10
|
+
export const DESKTOP = 'desktop';
|
|
11
|
+
const deviceTarget = (device) => `device:${device}`;
|
|
12
|
+
const isQuiet = (value) => {
|
|
13
|
+
if (value === null)
|
|
14
|
+
return true;
|
|
15
|
+
const quiet = value;
|
|
16
|
+
return (typeof quiet?.from === 'string' &&
|
|
17
|
+
TIME_PATTERN.test(quiet.from) &&
|
|
18
|
+
typeof quiet.to === 'string' &&
|
|
19
|
+
TIME_PATTERN.test(quiet.to));
|
|
20
|
+
};
|
|
21
|
+
// What was kept, over the defaults: a value that does not read is the default's.
|
|
22
|
+
function readPrefs(text) {
|
|
23
|
+
let kept = {};
|
|
24
|
+
try {
|
|
25
|
+
kept = JSON.parse(text) ?? {};
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// the defaults
|
|
29
|
+
}
|
|
30
|
+
const flag = (key) => {
|
|
31
|
+
const value = kept[key];
|
|
32
|
+
return typeof value === 'boolean' ? value : DEFAULT_PREFS[key];
|
|
33
|
+
};
|
|
34
|
+
const muted = Array.isArray(kept.muted)
|
|
35
|
+
? kept.muted.filter((id) => typeof id === 'string')
|
|
36
|
+
: DEFAULT_PREFS.muted;
|
|
37
|
+
return {
|
|
38
|
+
...Object.fromEntries(NOTIFY_KINDS.map((kind) => [kind, flag(kind)])),
|
|
39
|
+
quiet: isQuiet(kept.quiet) ? kept.quiet : DEFAULT_PREFS.quiet,
|
|
40
|
+
muted,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function toTarget(row) {
|
|
44
|
+
const { endpoint, p256dh, auth } = row;
|
|
45
|
+
return {
|
|
46
|
+
id: row.id,
|
|
47
|
+
device: row.device,
|
|
48
|
+
push: endpoint && p256dh && auth ? { endpoint, p256dh, auth } : null,
|
|
49
|
+
lang: isNoticeLang(row.lang) ? row.lang : 'en',
|
|
50
|
+
prefs: readPrefs(row.prefs),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
// The subscriptions to push to.
|
|
54
|
+
export function listTargets(db) {
|
|
55
|
+
const rows = db
|
|
56
|
+
.prepare('SELECT * FROM notify_targets WHERE endpoint IS NOT NULL ORDER BY created_at, id')
|
|
57
|
+
.all();
|
|
58
|
+
return rows.map(toTarget);
|
|
59
|
+
}
|
|
60
|
+
export function targetOfDevice(db, device) {
|
|
61
|
+
const row = db.prepare('SELECT * FROM notify_targets WHERE device = ?').get(device);
|
|
62
|
+
return row ? toTarget(row) : undefined;
|
|
63
|
+
}
|
|
64
|
+
// A device subscribes, or subscribes again (a new endpoint): the prefs it chose stay.
|
|
65
|
+
export function saveSubscription(db, device, push, lang, now = new Date()) {
|
|
66
|
+
const prefs = targetOfDevice(db, device)?.prefs ?? DEFAULT_PREFS;
|
|
67
|
+
// A browser hands the same endpoint to whoever paired last on it.
|
|
68
|
+
db.prepare('DELETE FROM notify_targets WHERE endpoint = ? AND device IS NOT ?').run(push.endpoint, device);
|
|
69
|
+
db.prepare(`INSERT INTO notify_targets (id, device, endpoint, p256dh, auth, lang, prefs, created_at)
|
|
70
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
71
|
+
ON CONFLICT (id) DO UPDATE SET endpoint = excluded.endpoint, p256dh = excluded.p256dh,
|
|
72
|
+
auth = excluded.auth, lang = excluded.lang`).run(deviceTarget(device), device, push.endpoint, push.p256dh, push.auth, lang, JSON.stringify(prefs), now.toISOString());
|
|
73
|
+
return targetOfDevice(db, device);
|
|
74
|
+
}
|
|
75
|
+
export function savePrefs(db, device, prefs, lang) {
|
|
76
|
+
const updated = db
|
|
77
|
+
.prepare('UPDATE notify_targets SET prefs = ?, lang = ? WHERE device = ?')
|
|
78
|
+
.run(JSON.stringify(prefs), lang, device);
|
|
79
|
+
if (updated.changes === 0) {
|
|
80
|
+
throw new OteaError('not_found', 'This device is not subscribed to notifications');
|
|
81
|
+
}
|
|
82
|
+
return targetOfDevice(db, device);
|
|
83
|
+
}
|
|
84
|
+
// The device turned them off: nothing is kept.
|
|
85
|
+
export function dropSubscription(db, device) {
|
|
86
|
+
db.prepare('DELETE FROM notify_targets WHERE device = ?').run(device);
|
|
87
|
+
}
|
|
88
|
+
// A subscription lost without the device choosing it: nothing is pushed to it any more, but its
|
|
89
|
+
// prefs stay and the device is told it stopped getting them (TS-3), until it subscribes again.
|
|
90
|
+
const FORGET_PUSH = 'UPDATE notify_targets SET endpoint = NULL, p256dh = NULL, auth = NULL';
|
|
91
|
+
// The phone found its browser no longer holds the subscription.
|
|
92
|
+
export function loseSubscription(db, device) {
|
|
93
|
+
db.prepare(`${FORGET_PUSH} WHERE device = ?`).run(device);
|
|
94
|
+
}
|
|
95
|
+
// The push service said the subscription is gone (404 or 410).
|
|
96
|
+
export function dropEndpoint(db, endpoint) {
|
|
97
|
+
db.prepare(`${FORGET_PUSH} WHERE endpoint = ?`).run(endpoint);
|
|
98
|
+
}
|
|
99
|
+
export function desktopTarget(db) {
|
|
100
|
+
const row = db.prepare('SELECT * FROM notify_targets WHERE id = ?').get(DESKTOP);
|
|
101
|
+
return row
|
|
102
|
+
? toTarget(row)
|
|
103
|
+
: { id: DESKTOP, device: null, push: null, lang: 'en', prefs: DEFAULT_PREFS };
|
|
104
|
+
}
|
|
105
|
+
// Whether the desktop app's prefs were ever chosen in Preferencias (its language with them).
|
|
106
|
+
export function desktopChosen(db) {
|
|
107
|
+
return db.prepare('SELECT 1 FROM notify_targets WHERE id = ?').get(DESKTOP) !== undefined;
|
|
108
|
+
}
|
|
109
|
+
export function saveDesktopPrefs(db, prefs, lang, now = new Date()) {
|
|
110
|
+
db.prepare(`INSERT INTO notify_targets (id, lang, prefs, created_at) VALUES (?, ?, ?, ?)
|
|
111
|
+
ON CONFLICT (id) DO UPDATE SET lang = excluded.lang, prefs = excluded.prefs`).run(DESKTOP, lang, JSON.stringify(prefs), now.toISOString());
|
|
112
|
+
return desktopTarget(db);
|
|
113
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { OteaError } from '../core/errors.js';
|
|
2
|
+
import { findEvidence } from '../store/queries.js';
|
|
3
|
+
import { requireItem, systemClock } from './items.js';
|
|
4
|
+
// The files of an item the person opened in the reader, on the desk or the phone: kept so that
|
|
5
|
+
// what Aprobar says is left unopened holds after a reload and from one device to another. It is
|
|
6
|
+
// the reader's bookkeeping, not an act on the item: no event, no change of turn.
|
|
7
|
+
export function openedFiles(ctx, id) {
|
|
8
|
+
requireItem(ctx.db, id);
|
|
9
|
+
const rows = ctx.db
|
|
10
|
+
.prepare('SELECT evidence_id FROM evidence_opened WHERE item_id = ? ORDER BY opened_at, evidence_id')
|
|
11
|
+
.all(id);
|
|
12
|
+
return rows.map(({ evidence_id }) => evidence_id);
|
|
13
|
+
}
|
|
14
|
+
export function markOpened(ctx, input, now = systemClock) {
|
|
15
|
+
if (input.actor.kind !== 'human')
|
|
16
|
+
throw new OteaError('forbidden', 'Only the person opens files in the reader');
|
|
17
|
+
const item = requireItem(ctx.db, input.id);
|
|
18
|
+
const evidence = findEvidence(ctx.db, input.evidence);
|
|
19
|
+
if (!evidence || evidence.itemId !== item.id)
|
|
20
|
+
throw new OteaError('not_found', `${item.id} has no evidence #${input.evidence}`);
|
|
21
|
+
ctx.db
|
|
22
|
+
.prepare(`INSERT INTO evidence_opened (evidence_id, item_id, opened_at) VALUES (?, ?, ?)
|
|
23
|
+
ON CONFLICT (evidence_id) DO NOTHING`)
|
|
24
|
+
.run(evidence.id, item.id, now());
|
|
25
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
const execRunner = (file, args) => {
|
|
3
|
+
execFileSync(file, args, { stdio: 'pipe', windowsHide: true, timeout: 10_000 });
|
|
4
|
+
};
|
|
5
|
+
const stderrWarn = (message) => {
|
|
6
|
+
process.stderr.write(`Warning: ${message}\n`);
|
|
7
|
+
};
|
|
8
|
+
// The icacls call that leaves the file to `user` (DOMAIN\user when the domain is known).
|
|
9
|
+
export function ownerOnlyCommand(path, user, domain) {
|
|
10
|
+
const who = domain ? `${domain}\\${user}` : user;
|
|
11
|
+
return { file: 'icacls', args: [path, '/inheritance:r', '/grant:r', `${who}:F`] };
|
|
12
|
+
}
|
|
13
|
+
const reasonOf = (error) => {
|
|
14
|
+
const stderr = error.stderr?.toString().trim();
|
|
15
|
+
return stderr || error.message;
|
|
16
|
+
};
|
|
17
|
+
export function restrictToOwner(path, options = {}) {
|
|
18
|
+
const { platform = process.platform, env = process.env, run = execRunner, warn = stderrWarn, } = options;
|
|
19
|
+
if (platform !== 'win32')
|
|
20
|
+
return;
|
|
21
|
+
const lost = 'other accounts on this computer may read it';
|
|
22
|
+
const user = env.USERNAME;
|
|
23
|
+
if (!user) {
|
|
24
|
+
warn(`Could not keep ${path} to the current user (no USERNAME); ${lost}`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const { file, args } = ownerOnlyCommand(path, user, env.USERDOMAIN || undefined);
|
|
28
|
+
try {
|
|
29
|
+
run(file, args);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
warn(`Could not keep ${path} to the current user (${reasonOf(error)}); ${lost}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { modelPerformance, PERIOD_HINT, parsePeriod, } from '../core/board/performance.js';
|
|
2
|
+
import { OteaError } from '../core/errors.js';
|
|
3
|
+
import { selectEvents, selectItems } from '../store/queries.js';
|
|
4
|
+
import { getCollection } from './collections.js';
|
|
5
|
+
import { systemClock } from './items.js';
|
|
6
|
+
import { liveBoards } from './next.js';
|
|
7
|
+
export function performanceOf(ctx, input, now = systemClock) {
|
|
8
|
+
const period = parsePeriod(input.since ?? 'all', now());
|
|
9
|
+
if (!period)
|
|
10
|
+
throw new OteaError('invalid_fields', `--since takes ${PERIOD_HINT}`);
|
|
11
|
+
const only = input.board === undefined ? undefined : getCollection(ctx.db, input.board).name;
|
|
12
|
+
const boards = liveBoards(ctx).filter(({ collection }) => !only || collection === only);
|
|
13
|
+
if (only && boards.length === 0) {
|
|
14
|
+
throw new OteaError('not_found', `Board "${only}" is not published yet`);
|
|
15
|
+
}
|
|
16
|
+
const trails = boards.flatMap(({ collection, def }) => selectItems(ctx.db, { collection }).map((item) => ({
|
|
17
|
+
item: item.id,
|
|
18
|
+
board: collection,
|
|
19
|
+
def,
|
|
20
|
+
events: selectEvents(ctx.db, item.id),
|
|
21
|
+
start: { initial: def.steps[0]?.id ?? item.state, createdAt: item.createdAt },
|
|
22
|
+
})));
|
|
23
|
+
return {
|
|
24
|
+
...modelPerformance(trails, period),
|
|
25
|
+
boards: boards.map(({ collection }) => collection),
|
|
26
|
+
since: period.since ?? null,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { OteaError } from '../core/errors.js';
|
|
2
|
+
import { checkTailscale, setUpTailscale, } from './tailscale.js';
|
|
3
|
+
// How a phone reaches the desk: the phone transport. They are interchangeable; the pairing code,
|
|
4
|
+
// the device tokens and what a paired phone may do stay the same whichever carries it.
|
|
5
|
+
// - lan: the local network, plain HTTP to this computer's address (open it in Settings › Phone or
|
|
6
|
+
// with otea phone lan --open). The default.
|
|
7
|
+
// - tailscale: `tailscale serve` gives this computer an HTTPS name on the person's tailnet,
|
|
8
|
+
// `https://<machine>.<tailnet>.ts.net`, reachable from their own devices wherever they are.
|
|
9
|
+
// - relay: the Otea relay, a future service. Only the seam exists: choosing it is refused.
|
|
10
|
+
export const TRANSPORTS = ['lan', 'tailscale', 'relay'];
|
|
11
|
+
export const RELAY_UNAVAILABLE = 'The Otea relay is not available yet; use the local network (otea phone lan --open) or Tailscale (otea phone tailscale)';
|
|
12
|
+
const DEFAULT = { transport: 'lan', tailscaleHost: null };
|
|
13
|
+
export function isTransport(value) {
|
|
14
|
+
return TRANSPORTS.includes(value);
|
|
15
|
+
}
|
|
16
|
+
export function phoneSettings(db) {
|
|
17
|
+
const row = db.prepare('SELECT transport, tailscale_host FROM phone').get();
|
|
18
|
+
if (!row || !isTransport(row.transport))
|
|
19
|
+
return DEFAULT;
|
|
20
|
+
return { transport: row.transport, tailscaleHost: row.tailscale_host };
|
|
21
|
+
}
|
|
22
|
+
export function choosePhoneTransport(db, settings) {
|
|
23
|
+
if (settings.transport === 'relay')
|
|
24
|
+
throw new OteaError('transport_unavailable', RELAY_UNAVAILABLE);
|
|
25
|
+
if (settings.transport === 'tailscale' && !settings.tailscaleHost) {
|
|
26
|
+
throw new OteaError('transport_unavailable', 'Tailscale needs the name this computer has on the tailnet; run otea phone tailscale');
|
|
27
|
+
}
|
|
28
|
+
const host = settings.transport === 'tailscale' ? settings.tailscaleHost : null;
|
|
29
|
+
db.prepare(`INSERT INTO phone (id, transport, tailscale_host) VALUES (1, ?, ?)
|
|
30
|
+
ON CONFLICT (id) DO UPDATE SET transport = excluded.transport,
|
|
31
|
+
tailscale_host = excluded.tailscale_host`).run(settings.transport, host);
|
|
32
|
+
return { transport: settings.transport, tailscaleHost: host };
|
|
33
|
+
}
|
|
34
|
+
// Whether the local network is open to phones: chosen in Settings › Phone or with otea phone lan
|
|
35
|
+
// --open, and kept, so every otea serve (the desktop app's too) starts with it open. It is apart
|
|
36
|
+
// from the transport: the LAN may stay open while Tailscale carries the pairing code.
|
|
37
|
+
export function lanOpen(db) {
|
|
38
|
+
const row = db.prepare('SELECT lan_open FROM phone').get();
|
|
39
|
+
return row?.lan_open === 1;
|
|
40
|
+
}
|
|
41
|
+
export function setLanOpen(db, open) {
|
|
42
|
+
db.prepare(`INSERT INTO phone (id, transport, tailscale_host, lan_open) VALUES (1, 'lan', NULL, ?)
|
|
43
|
+
ON CONFLICT (id) DO UPDATE SET lan_open = excluded.lan_open`).run(open ? 1 : 0);
|
|
44
|
+
}
|
|
45
|
+
const PROBE_TIMEOUT_MS = 3000;
|
|
46
|
+
export const fetchProbe = async (url) => {
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(`${url}/manifest.webmanifest`, {
|
|
49
|
+
signal: AbortSignal.timeout(PROBE_TIMEOUT_MS),
|
|
50
|
+
redirect: 'manual',
|
|
51
|
+
});
|
|
52
|
+
return response.ok;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
export async function phoneStatus(db, sources) {
|
|
59
|
+
const settings = phoneSettings(db);
|
|
60
|
+
if (settings.transport === 'tailscale' && settings.tailscaleHost) {
|
|
61
|
+
const check = await checkTailscale(sources.tailscale, sources.port, sources.platform);
|
|
62
|
+
const url = `https://${settings.tailscaleHost}`;
|
|
63
|
+
const serving = check.state === 'ready' && check.serving && check.host === settings.tailscaleHost;
|
|
64
|
+
return {
|
|
65
|
+
transport: 'tailscale',
|
|
66
|
+
url,
|
|
67
|
+
reachable: serving ? await sources.probe(url) : false,
|
|
68
|
+
tailscale: check,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const url = sources.lanUrls[0] ?? null;
|
|
72
|
+
return {
|
|
73
|
+
transport: 'lan',
|
|
74
|
+
url,
|
|
75
|
+
reachable: url === null ? null : await sources.probe(url),
|
|
76
|
+
tailscale: null,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Configuración's Tailscale option: makes it the phone transport once tailscale serve already
|
|
80
|
+
// forwards to Otea, and otherwise only says what is missing. It never changes Tailscale.
|
|
81
|
+
export async function adoptTailscale(db, cli, port, platform) {
|
|
82
|
+
const check = await checkTailscale(cli, port, platform);
|
|
83
|
+
if (check.state === 'ready' && check.serving) {
|
|
84
|
+
choosePhoneTransport(db, { transport: 'tailscale', tailscaleHost: check.host });
|
|
85
|
+
}
|
|
86
|
+
return check;
|
|
87
|
+
}
|
|
88
|
+
// Sets Tailscale up for Otea on this port and, once it is ready, makes it the phone transport:
|
|
89
|
+
// `otea phone tailscale`, which the person runs, and the guide's button, once they confirm the
|
|
90
|
+
// command it shows.
|
|
91
|
+
export async function switchToTailscale(db, cli, port, options = {}) {
|
|
92
|
+
const check = await setUpTailscale(cli, port, options);
|
|
93
|
+
if (check.state === 'ready') {
|
|
94
|
+
choosePhoneTransport(db, { transport: 'tailscale', tailscaleHost: check.host });
|
|
95
|
+
}
|
|
96
|
+
return check;
|
|
97
|
+
}
|