@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,43 @@
|
|
|
1
|
+
import { getCookie, setCookie } from 'hono/cookie';
|
|
2
|
+
import { sameToken } from './token.js';
|
|
3
|
+
export const sessionCookie = (port) => `otea_session_${port}`;
|
|
4
|
+
// A paired device's token, set once by POST /api/pair (local network or Tailscale).
|
|
5
|
+
export const deviceCookie = (port) => `otea_device_${port}`;
|
|
6
|
+
export const LOCAL = { kind: 'local' };
|
|
7
|
+
export function clientOf(c) {
|
|
8
|
+
return c.get('client') ?? LOCAL;
|
|
9
|
+
}
|
|
10
|
+
const BAD_LINK = `<!doctype html>
|
|
11
|
+
<meta charset="utf-8">
|
|
12
|
+
<title>Otea</title>
|
|
13
|
+
<p>This sign-in link is not valid for this Otea server. Run <code>otea open</code> in a project folder to open the UI with a fresh link.</p>
|
|
14
|
+
`;
|
|
15
|
+
export function session(token, port) {
|
|
16
|
+
const signedIn = (c) => clientOf(c).kind === 'local' &&
|
|
17
|
+
token !== undefined &&
|
|
18
|
+
sameToken(getCookie(c, sessionCookie(port())) ?? '', token);
|
|
19
|
+
return {
|
|
20
|
+
canWrite: (c) => clientOf(c).kind === 'device' || signedIn(c),
|
|
21
|
+
canManageDevices: signedIn,
|
|
22
|
+
canConfigure: signedIn,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// Where a sign-in goes on to: a project's page on this server (`otea open` names the project it
|
|
26
|
+
// was run in), never another site; anything else lands on `/`, which opens the last project.
|
|
27
|
+
const PROJECT_PAGE = /^\/p\/[a-z0-9-]{1,64}\/(?:#[^\s]*)?$/;
|
|
28
|
+
export function nextPage(next) {
|
|
29
|
+
return next !== undefined && PROJECT_PAGE.test(next) ? next : '/';
|
|
30
|
+
}
|
|
31
|
+
export function mountAuth(app, token, port) {
|
|
32
|
+
app.get('/auth', (c) => {
|
|
33
|
+
c.header('referrer-policy', 'no-referrer');
|
|
34
|
+
c.header('cache-control', 'no-store');
|
|
35
|
+
const given = c.req.query('token') ?? '';
|
|
36
|
+
// The sign-in link is for this computer's browser; a device on the LAN pairs instead.
|
|
37
|
+
const local = clientOf(c).kind === 'local';
|
|
38
|
+
if (!local || token === undefined || !sameToken(given, token))
|
|
39
|
+
return c.html(BAD_LINK, 401);
|
|
40
|
+
setCookie(c, sessionCookie(port()), token, { path: '/', httpOnly: true, sameSite: 'Strict' });
|
|
41
|
+
return c.redirect(nextPage(c.req.query('next')), 302);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { streamSSE } from 'hono/streaming';
|
|
2
|
+
// Every change sends `change`. When `presence` is given, a change that also moved the agents'
|
|
3
|
+
// presence (a new agent, its harness, when it was last seen, what it holds) sends `presence` too,
|
|
4
|
+
// so a view of the agents alone can read them again without reloading the rest.
|
|
5
|
+
export function liveStreams(feed, presence) {
|
|
6
|
+
const open = new Set();
|
|
7
|
+
return {
|
|
8
|
+
handle: (c) => streamSSE(c, async (stream) => {
|
|
9
|
+
const { promise: ended, resolve } = Promise.withResolvers();
|
|
10
|
+
let seen = presence?.();
|
|
11
|
+
const unsubscribe = feed.subscribe(() => {
|
|
12
|
+
void stream.writeSSE({ event: 'change', data: '' });
|
|
13
|
+
if (!presence)
|
|
14
|
+
return;
|
|
15
|
+
const now = presence();
|
|
16
|
+
if (now === seen)
|
|
17
|
+
return;
|
|
18
|
+
seen = now;
|
|
19
|
+
void stream.writeSSE({ event: 'presence', data: '' });
|
|
20
|
+
});
|
|
21
|
+
const end = () => {
|
|
22
|
+
unsubscribe();
|
|
23
|
+
open.delete(end);
|
|
24
|
+
resolve();
|
|
25
|
+
};
|
|
26
|
+
open.add(end);
|
|
27
|
+
stream.onAbort(end);
|
|
28
|
+
await stream.writeSSE({ event: 'ready', data: '' });
|
|
29
|
+
await ended;
|
|
30
|
+
}),
|
|
31
|
+
closeAll() {
|
|
32
|
+
for (const end of open)
|
|
33
|
+
end();
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { dropEndpoint, targetOfDevice } from '../app/notify-targets.js';
|
|
2
|
+
// What a push came to, in the serve output: the device by its id alone, never the endpoint, the
|
|
3
|
+
// keys or the words it carried.
|
|
4
|
+
export function outcomeLine(who, outcome) {
|
|
5
|
+
const { result, status, detail } = outcome;
|
|
6
|
+
const code = status === null ? '' : ` (${status})`;
|
|
7
|
+
if (result === 'sent')
|
|
8
|
+
return `${who}: sent${code}`;
|
|
9
|
+
if (result === 'gone')
|
|
10
|
+
return `${who}: gone${code}, subscription dropped`;
|
|
11
|
+
if (status === null)
|
|
12
|
+
return `${who}: network error: ${detail || 'no answer'}`;
|
|
13
|
+
return `${who}: refused by the push service${code}${detail ? `: ${detail}` : ''}`;
|
|
14
|
+
}
|
|
15
|
+
const WORDS = {
|
|
16
|
+
es: {
|
|
17
|
+
title: 'Otea · aviso de prueba',
|
|
18
|
+
body: 'Si ves esto, los avisos llegan a este dispositivo.',
|
|
19
|
+
},
|
|
20
|
+
en: {
|
|
21
|
+
title: 'Otea · test notification',
|
|
22
|
+
body: 'If you see this, notifications reach this device.',
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
// Sends one push to a device now, past the planner, the quiet hours and the spacing: Preferencias ›
|
|
26
|
+
// Avisos and the desk's device list use it to see whether notifications get through. A subscription
|
|
27
|
+
// its push service calls gone is dropped, as a real push drops it.
|
|
28
|
+
export async function sendTestPush(machine, sender, device, log = () => undefined) {
|
|
29
|
+
const target = targetOfDevice(machine, device);
|
|
30
|
+
if (!target?.push)
|
|
31
|
+
return { result: 'off', status: null, detail: '' };
|
|
32
|
+
const words = WORDS[target.lang];
|
|
33
|
+
const outcome = await sender.send(target.push, JSON.stringify({ ...words, tag: 'otea-test', url: '/#/' }));
|
|
34
|
+
log(`Otea ${outcomeLine(`test push to device ${device}`, outcome)}`);
|
|
35
|
+
if (outcome.result === 'gone')
|
|
36
|
+
dropEndpoint(machine, target.push.endpoint);
|
|
37
|
+
return outcome;
|
|
38
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createHash, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { chmodSync, existsSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { restrictToOwner } from '../app/owner-only.js';
|
|
5
|
+
export const UI_TOKEN_FILE = 'ui-token';
|
|
6
|
+
const TOKEN_PATTERN = /^[A-Za-z0-9_-]{43}$/;
|
|
7
|
+
export function readUiToken(dir) {
|
|
8
|
+
const path = join(dir, UI_TOKEN_FILE);
|
|
9
|
+
if (!existsSync(path))
|
|
10
|
+
return undefined;
|
|
11
|
+
const token = readFileSync(path, 'utf8').trim();
|
|
12
|
+
return TOKEN_PATTERN.test(token) ? token : undefined;
|
|
13
|
+
}
|
|
14
|
+
// The token is kept to its owner: mode 0600, and on Windows, which ignores the mode, an ACL for the
|
|
15
|
+
// current user alone (`restrict`); `warn` says when that could not be done.
|
|
16
|
+
export function ensureUiToken(dir, { warn, restrict = (path) => restrictToOwner(path, warn ? { warn } : {}), } = {}) {
|
|
17
|
+
const existing = readUiToken(dir);
|
|
18
|
+
if (existing)
|
|
19
|
+
return existing;
|
|
20
|
+
const token = randomBytes(32).toString('base64url');
|
|
21
|
+
const path = join(dir, UI_TOKEN_FILE);
|
|
22
|
+
writeFileSync(path, `${token}\n`, { mode: 0o600 });
|
|
23
|
+
try {
|
|
24
|
+
chmodSync(path, 0o600);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
// best effort: some file systems ignore permissions
|
|
28
|
+
}
|
|
29
|
+
restrict(path);
|
|
30
|
+
return token;
|
|
31
|
+
}
|
|
32
|
+
const digest = (value) => createHash('sha256').update(value).digest();
|
|
33
|
+
export function sameToken(given, expected) {
|
|
34
|
+
if (expected === '')
|
|
35
|
+
return false;
|
|
36
|
+
return timingSafeEqual(digest(given), digest(expected));
|
|
37
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { isLoopback, isPrivateAddress, plainAddress } from './lan.js';
|
|
2
|
+
// A request a proxy on this computer forwarded (tailscale serve, or any other): it comes from this
|
|
3
|
+
// computer but on someone else's behalf, so it is never this computer's own browser.
|
|
4
|
+
export function proxied(arrival) {
|
|
5
|
+
return (arrival.remote !== undefined &&
|
|
6
|
+
isLoopback(arrival.remote) &&
|
|
7
|
+
(arrival.forwardedFor ?? '').trim() !== '');
|
|
8
|
+
}
|
|
9
|
+
// The local network (while it is open): a private address naming one of this computer's names or
|
|
10
|
+
// addresses, over plain HTTP.
|
|
11
|
+
export function lanTransport(hosts) {
|
|
12
|
+
return {
|
|
13
|
+
kind: 'lan',
|
|
14
|
+
urls: (port) => hosts.map((host) => `http://${host}:${port}`),
|
|
15
|
+
admit({ host, remote }, port) {
|
|
16
|
+
if (remote === undefined || isLoopback(remote) || !isPrivateAddress(remote))
|
|
17
|
+
return undefined;
|
|
18
|
+
const named = hosts.some((name) => host?.toLowerCase() === `${name.toLowerCase()}:${port}`);
|
|
19
|
+
return named ? { address: plainAddress(remote), secure: false } : undefined;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
// Tailscale's addresses: 100.64.0.0/10 (isPrivateAddress) and fd7a:115c:a1e0::/48.
|
|
24
|
+
function tailnetAddress(text) {
|
|
25
|
+
const first = plainAddress(text?.split(',')[0]?.trim() ?? '');
|
|
26
|
+
if (/^fd7a:115c:a1e0:/i.test(first))
|
|
27
|
+
return first.toLowerCase();
|
|
28
|
+
return /^100\./.test(first) && isPrivateAddress(first) ? first : undefined;
|
|
29
|
+
}
|
|
30
|
+
// Tailscale: `tailscale serve` terminates HTTPS for `https://<host>` and forwards to Otea on this
|
|
31
|
+
// computer, so what it forwards comes from this computer, naming the tailnet name (Tailscale keeps
|
|
32
|
+
// the Host header) and carrying the phone's tailnet address in X-Forwarded-For.
|
|
33
|
+
export function tailscaleTransport(tailnetHost) {
|
|
34
|
+
const name = tailnetHost.toLowerCase();
|
|
35
|
+
return {
|
|
36
|
+
kind: 'tailscale',
|
|
37
|
+
urls: () => [`https://${name}`],
|
|
38
|
+
admit(arrival) {
|
|
39
|
+
const { host, remote, forwardedFor } = arrival;
|
|
40
|
+
if (remote === undefined || !isLoopback(remote))
|
|
41
|
+
return undefined;
|
|
42
|
+
const named = host?.toLowerCase() === name || host?.toLowerCase() === `${name}:443`;
|
|
43
|
+
if (!named && !proxied(arrival))
|
|
44
|
+
return undefined;
|
|
45
|
+
return { address: tailnetAddress(forwardedFor) ?? 'tailscale', secure: true };
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// The Otea relay, a future paid service: the seam a relay plugs into. It opens nowhere yet.
|
|
50
|
+
export const relayTransport = {
|
|
51
|
+
kind: 'relay',
|
|
52
|
+
urls: () => [],
|
|
53
|
+
admit: () => undefined,
|
|
54
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { serveStatic } from '@hono/node-server/serve-static';
|
|
5
|
+
export const BUILT_UI_DIR = fileURLToPath(new URL('../../ui/dist', import.meta.url));
|
|
6
|
+
const NOT_BUILT = 'The Otea UI is not built yet. Run "npm run build:ui" in the Otea checkout, then restart "otea serve".';
|
|
7
|
+
// One manifest for every page (TS-30): a paired phone opens every project, so the installed app is
|
|
8
|
+
// one app, «Otea» by the id `/`, whichever page it was installed from. A project's own manifest
|
|
9
|
+
// would be a second identity: a second install offer, and an app named after one project.
|
|
10
|
+
function localizedManifest(c, path) {
|
|
11
|
+
const manifest = JSON.parse(readFileSync(path, 'utf8'));
|
|
12
|
+
const localized = manifest.description_localized ?? {};
|
|
13
|
+
const asked = (c.req.header('accept-language') ?? '')
|
|
14
|
+
.split(',')
|
|
15
|
+
.map((part) => part.split(';')[0]?.trim().split('-')[0]?.toLowerCase() ?? '');
|
|
16
|
+
const lang = asked.find((tag) => tag === manifest.lang || tag in localized);
|
|
17
|
+
const described = lang && lang !== manifest.lang && localized[lang]
|
|
18
|
+
? { ...manifest, lang, description: localized[lang] }
|
|
19
|
+
: manifest;
|
|
20
|
+
c.header('vary', 'Accept-Language');
|
|
21
|
+
return c.body(JSON.stringify(described), 200, { 'content-type': 'application/manifest+json' });
|
|
22
|
+
}
|
|
23
|
+
export function mountUi(app, uiDir) {
|
|
24
|
+
const index = join(uiDir, 'index.html');
|
|
25
|
+
if (!existsSync(index)) {
|
|
26
|
+
app.get('*', (c) => c.text(NOT_BUILT, 503));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
// The page is checked with the server on every load (a revoked device is sent back to the
|
|
30
|
+
// pairing screen instead of opening a cached UI); the hashed assets cache as usual.
|
|
31
|
+
app.use('*', async (c, next) => {
|
|
32
|
+
await next();
|
|
33
|
+
if (!c.req.path.startsWith('/assets/'))
|
|
34
|
+
c.header('cache-control', 'no-cache');
|
|
35
|
+
});
|
|
36
|
+
app.get('/manifest.webmanifest', (c) => localizedManifest(c, join(uiDir, 'manifest.webmanifest')));
|
|
37
|
+
app.use('*', serveStatic({ root: uiDir }));
|
|
38
|
+
// The page: a project's (/p/<id>/…, its places in the hash) and the pairing screen. The installed
|
|
39
|
+
// app keeps it as /index.html, so it opens any project's page without the PC.
|
|
40
|
+
const page = (c) => c.html(readFileSync(index, 'utf8'));
|
|
41
|
+
app.get('/p/*', page);
|
|
42
|
+
app.get('/pair', page);
|
|
43
|
+
app.get('*', (c) => c.text('Not found', 404));
|
|
44
|
+
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { EVIDENCE_ROLES, MAX_EVIDENCE_BYTES, MAX_EVIDENCE_MB, } from '../core/evidence.js';
|
|
4
|
+
import { RejectedRequest } from './errors.js';
|
|
5
|
+
import { clientOf } from './session.js';
|
|
6
|
+
export const HUMAN_IN_UI = { actor: { kind: 'human', name: 'human' }, channel: 'ui' };
|
|
7
|
+
// `choice`: on a board item that carries options, the one the person picks with this approval.
|
|
8
|
+
const MoveBody = z.object({
|
|
9
|
+
to: z.string().min(1),
|
|
10
|
+
reason: z.string().optional(),
|
|
11
|
+
choice: z.object({ option: z.string().min(1), comment: z.string().optional() }).optional(),
|
|
12
|
+
});
|
|
13
|
+
const UndoBody = z.union([
|
|
14
|
+
z.object({ from: z.string().min(1) }),
|
|
15
|
+
z.object({ release: z.literal(true) }),
|
|
16
|
+
]);
|
|
17
|
+
const ReassignBody = z.object({ agent: z.string().min(1) });
|
|
18
|
+
const JSON_TYPE = 'application/json';
|
|
19
|
+
const FORM_TYPE = 'multipart/form-data';
|
|
20
|
+
function mediaOf(contentType) {
|
|
21
|
+
return contentType?.split(';')[0]?.trim().toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
export function assertSignedIn(c, auth, purpose = 'act from the UI') {
|
|
24
|
+
if (!auth.canWrite(c)) {
|
|
25
|
+
throw new RejectedRequest(401, 'unauthorized', `Sign in with "otea open" to ${purpose}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// A write comes from a page this server served, as JSON: no other site can forge one. Creating an
|
|
29
|
+
// item may also come as a form, to carry the files it is filed with (withFiles); the origin check
|
|
30
|
+
// covers both. A phone on Tailscale opens the UI over HTTPS, so its pages' origin is https://.
|
|
31
|
+
export function assertSameOrigin(c, withFiles = false) {
|
|
32
|
+
const origin = c.req.header('origin');
|
|
33
|
+
const client = clientOf(c);
|
|
34
|
+
const scheme = client.kind !== 'local' && client.secure ? 'https' : 'http';
|
|
35
|
+
if (origin !== `${scheme}://${c.req.header('host')}`) {
|
|
36
|
+
throw new RejectedRequest(403, 'forbidden_origin', `Origin "${origin ?? ''}" is not allowed`);
|
|
37
|
+
}
|
|
38
|
+
const media = mediaOf(c.req.header('content-type'));
|
|
39
|
+
if (media === JSON_TYPE || (withFiles && media === FORM_TYPE))
|
|
40
|
+
return;
|
|
41
|
+
const expected = withFiles ? `${JSON_TYPE} or ${FORM_TYPE}` : JSON_TYPE;
|
|
42
|
+
throw new RejectedRequest(415, 'unsupported_media_type', `Send the body as ${expected}`);
|
|
43
|
+
}
|
|
44
|
+
// Writes come signed in, from the UI's own origin (assertSameOrigin).
|
|
45
|
+
export function assertWritable(c, auth, withFiles = false) {
|
|
46
|
+
assertSignedIn(c, auth);
|
|
47
|
+
assertSameOrigin(c, withFiles);
|
|
48
|
+
}
|
|
49
|
+
// Changes to the project's setup (paths, presets, boards) come from this computer's signed-in
|
|
50
|
+
// browser, from the UI's own origin: a paired device may act on items but not reconfigure the project.
|
|
51
|
+
export function assertConfigurable(c, auth) {
|
|
52
|
+
assertSignedIn(c, auth);
|
|
53
|
+
if (!auth.canConfigure(c)) {
|
|
54
|
+
throw new RejectedRequest(403, 'forbidden', 'Only the browser on this computer, signed in with "otea open", changes the project setup', { kind: 'desk_only' });
|
|
55
|
+
}
|
|
56
|
+
assertSameOrigin(c);
|
|
57
|
+
}
|
|
58
|
+
export async function moveRequest(c) {
|
|
59
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
60
|
+
const parsed = MoveBody.safeParse(raw);
|
|
61
|
+
if (!parsed.success) {
|
|
62
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"to": string, "reason"?: string, "choice"?: {"option": string, "comment"?: string}}');
|
|
63
|
+
}
|
|
64
|
+
const { to, reason, choice } = parsed.data;
|
|
65
|
+
return {
|
|
66
|
+
to,
|
|
67
|
+
...(reason === undefined ? {} : { reason }),
|
|
68
|
+
...(choice === undefined
|
|
69
|
+
? {}
|
|
70
|
+
: {
|
|
71
|
+
choice: {
|
|
72
|
+
option: choice.option,
|
|
73
|
+
...(choice.comment === undefined ? {} : { comment: choice.comment }),
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const UpdateBody = z.object({ fields: z.record(z.string(), z.string()) });
|
|
79
|
+
// The fields the person fills on the item page (its own step's "Rellenar").
|
|
80
|
+
export async function updateRequest(c) {
|
|
81
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
82
|
+
const parsed = UpdateBody.safeParse(raw);
|
|
83
|
+
if (!parsed.success) {
|
|
84
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"fields": {[key]: string}}');
|
|
85
|
+
}
|
|
86
|
+
return { fields: parsed.data.fields };
|
|
87
|
+
}
|
|
88
|
+
const CreateBody = z.object({
|
|
89
|
+
collection: z.string().min(1),
|
|
90
|
+
type: z.string().min(1).optional(),
|
|
91
|
+
parent: z.string().min(1).optional(),
|
|
92
|
+
fields: z.record(z.string(), z.string()),
|
|
93
|
+
// One entry per file part, in order: where each file goes.
|
|
94
|
+
evidence: z
|
|
95
|
+
.array(z.object({
|
|
96
|
+
role: z.enum(EVIDENCE_ROLES).optional(),
|
|
97
|
+
module: z.string().min(1).optional(),
|
|
98
|
+
}))
|
|
99
|
+
.optional(),
|
|
100
|
+
});
|
|
101
|
+
const CREATE_SHAPE = '{"collection": string, "type"?: string, "parent"?: string, "fields": {[key]: string}}';
|
|
102
|
+
// At most this many files come with a new item, each within the evidence limit.
|
|
103
|
+
export const MAX_INTAKE_FILES = 10;
|
|
104
|
+
// The largest body a create may send: its files and some room for the rest of the form.
|
|
105
|
+
export const MAX_CREATE_BYTES = MAX_INTAKE_FILES * MAX_EVIDENCE_BYTES + 1024 * 1024;
|
|
106
|
+
function invalidCreate(detail = '') {
|
|
107
|
+
return new RejectedRequest(400, 'invalid_body', `Expected ${CREATE_SHAPE}${detail}`);
|
|
108
|
+
}
|
|
109
|
+
async function fileOf(file, place = {}) {
|
|
110
|
+
const filename = basename(file.name.replaceAll('\\', '/'));
|
|
111
|
+
if (filename === '' || filename === '.' || filename === '..') {
|
|
112
|
+
throw new RejectedRequest(400, 'invalid_body', 'Each file needs a name');
|
|
113
|
+
}
|
|
114
|
+
if (file.size > MAX_EVIDENCE_BYTES) {
|
|
115
|
+
throw new RejectedRequest(413, 'evidence_too_large', `${filename} is over the ${MAX_EVIDENCE_MB} MB evidence limit`);
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
filename,
|
|
119
|
+
bytes: Buffer.from(await file.arrayBuffer()),
|
|
120
|
+
...(place.role === undefined ? {} : { role: place.role }),
|
|
121
|
+
...(place.module === undefined ? {} : { module: place.module }),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
// The item as JSON, or as a form: its JSON in the `item` part and each file in a `file` part, in
|
|
125
|
+
// the order of `evidence`.
|
|
126
|
+
async function readCreate(c) {
|
|
127
|
+
if (mediaOf(c.req.header('content-type')) !== FORM_TYPE) {
|
|
128
|
+
return { raw: await c.req.json().catch(() => undefined), files: [] };
|
|
129
|
+
}
|
|
130
|
+
const form = await c.req.parseBody({ all: true }).catch(() => {
|
|
131
|
+
throw invalidCreate(' as the "item" part of the form');
|
|
132
|
+
});
|
|
133
|
+
const { item, file = [] } = form;
|
|
134
|
+
const files = (Array.isArray(file) ? file : [file]).filter((part) => part instanceof File);
|
|
135
|
+
if (typeof item !== 'string')
|
|
136
|
+
throw invalidCreate(' as the "item" part of the form');
|
|
137
|
+
let raw;
|
|
138
|
+
try {
|
|
139
|
+
raw = JSON.parse(item);
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
throw invalidCreate(' as the "item" part of the form');
|
|
143
|
+
}
|
|
144
|
+
return { raw, files };
|
|
145
|
+
}
|
|
146
|
+
export async function createRequest(c) {
|
|
147
|
+
const { raw, files } = await readCreate(c);
|
|
148
|
+
const parsed = CreateBody.safeParse(raw);
|
|
149
|
+
if (!parsed.success)
|
|
150
|
+
throw invalidCreate();
|
|
151
|
+
const { collection, type, parent, fields, evidence = [] } = parsed.data;
|
|
152
|
+
if (files.length > MAX_INTAKE_FILES) {
|
|
153
|
+
throw new RejectedRequest(400, 'invalid_body', `Send at most ${MAX_INTAKE_FILES} files`);
|
|
154
|
+
}
|
|
155
|
+
if (evidence.length > files.length) {
|
|
156
|
+
throw new RejectedRequest(400, 'invalid_body', 'Each evidence entry needs its file part');
|
|
157
|
+
}
|
|
158
|
+
const { title = '', ...rest } = fields;
|
|
159
|
+
return {
|
|
160
|
+
collection,
|
|
161
|
+
...(type === undefined ? {} : { type }),
|
|
162
|
+
...(parent === undefined ? {} : { parent }),
|
|
163
|
+
title,
|
|
164
|
+
fields: rest,
|
|
165
|
+
files: await Promise.all(files.map((file, at) => fileOf(file, evidence[at]))),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
const Places = z.array(z.object({ role: z.enum(EVIDENCE_ROLES).optional(), module: z.string().min(1).optional() }));
|
|
169
|
+
// Files the person adds to an item that exists (ONB-18), as a form: each file in a "file" part,
|
|
170
|
+
// and optionally where each goes, as JSON in an "evidence" part (one entry per file, in order).
|
|
171
|
+
export async function attachRequest(c) {
|
|
172
|
+
const invalid = (detail) => new RejectedRequest(400, 'invalid_body', detail);
|
|
173
|
+
if (mediaOf(c.req.header('content-type')) !== FORM_TYPE) {
|
|
174
|
+
throw invalid('Send the files as a form, each in a "file" part');
|
|
175
|
+
}
|
|
176
|
+
const form = await c.req.parseBody({ all: true }).catch(() => {
|
|
177
|
+
throw invalid('Send the files as a form, each in a "file" part');
|
|
178
|
+
});
|
|
179
|
+
const { evidence, file = [] } = form;
|
|
180
|
+
const files = (Array.isArray(file) ? file : [file]).filter((part) => part instanceof File);
|
|
181
|
+
if (files.length === 0)
|
|
182
|
+
throw invalid('Send at least one file, in a "file" part');
|
|
183
|
+
if (files.length > MAX_INTAKE_FILES)
|
|
184
|
+
throw invalid(`Send at most ${MAX_INTAKE_FILES} files`);
|
|
185
|
+
let places = [];
|
|
186
|
+
if (typeof evidence === 'string') {
|
|
187
|
+
let raw;
|
|
188
|
+
try {
|
|
189
|
+
raw = JSON.parse(evidence);
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
raw = undefined;
|
|
193
|
+
}
|
|
194
|
+
const parsed = Places.safeParse(raw);
|
|
195
|
+
if (!parsed.success)
|
|
196
|
+
throw invalid('Expected "evidence" as [{"role"?: string, "module"?: string}]');
|
|
197
|
+
places = parsed.data;
|
|
198
|
+
}
|
|
199
|
+
return Promise.all(files.map((one, at) => fileOf(one, places[at])));
|
|
200
|
+
}
|
|
201
|
+
const CLIENT_KEY = /^[A-Za-z0-9_-]{1,128}$/;
|
|
202
|
+
// The create's Idempotency-Key, when the client sends one: a phone resending a capture it queued
|
|
203
|
+
// offline gets the item the first send made instead of a second one.
|
|
204
|
+
export function clientKeyOf(c) {
|
|
205
|
+
const key = c.req.header('idempotency-key');
|
|
206
|
+
if (key === undefined)
|
|
207
|
+
return undefined;
|
|
208
|
+
if (!CLIENT_KEY.test(key)) {
|
|
209
|
+
throw new RejectedRequest(400, 'invalid_idempotency_key', 'Idempotency-Key takes 1 to 128 letters, digits, "-" or "_"');
|
|
210
|
+
}
|
|
211
|
+
return key;
|
|
212
|
+
}
|
|
213
|
+
const LinkBody = z.object({ kind: z.string().min(1), other: z.string().min(1) });
|
|
214
|
+
export async function linkRequest(c) {
|
|
215
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
216
|
+
const parsed = LinkBody.safeParse(raw);
|
|
217
|
+
if (!parsed.success) {
|
|
218
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"kind": string, "other": string}');
|
|
219
|
+
}
|
|
220
|
+
return parsed.data;
|
|
221
|
+
}
|
|
222
|
+
const BriefBody = z.object({
|
|
223
|
+
ids: z.array(z.string().min(1)).min(1),
|
|
224
|
+
limits: z.array(z.string()).optional(),
|
|
225
|
+
success: z.array(z.string()).optional(),
|
|
226
|
+
});
|
|
227
|
+
export async function briefRequest(c) {
|
|
228
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
229
|
+
const parsed = BriefBody.safeParse(raw);
|
|
230
|
+
if (!parsed.success) {
|
|
231
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"ids": string[], "limits"?: string[], "success"?: string[]}');
|
|
232
|
+
}
|
|
233
|
+
const { ids, limits, success } = parsed.data;
|
|
234
|
+
return {
|
|
235
|
+
ids,
|
|
236
|
+
...(limits === undefined ? {} : { limits }),
|
|
237
|
+
...(success === undefined ? {} : { success }),
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const PathBody = z.object({ path: z.string().min(1) });
|
|
241
|
+
export async function pathRequest(c) {
|
|
242
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
243
|
+
const parsed = PathBody.safeParse(raw);
|
|
244
|
+
if (!parsed.success)
|
|
245
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"path": string}');
|
|
246
|
+
return parsed.data;
|
|
247
|
+
}
|
|
248
|
+
const RelinkBody = z.object({
|
|
249
|
+
from: z.string().min(1),
|
|
250
|
+
to: z.string().min(1),
|
|
251
|
+
force: z.boolean().optional(),
|
|
252
|
+
});
|
|
253
|
+
export async function relinkRequest(c) {
|
|
254
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
255
|
+
const parsed = RelinkBody.safeParse(raw);
|
|
256
|
+
if (!parsed.success) {
|
|
257
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"from": string, "to": string, "force"?: boolean}');
|
|
258
|
+
}
|
|
259
|
+
const { from, to, force = false } = parsed.data;
|
|
260
|
+
return { from, to, force };
|
|
261
|
+
}
|
|
262
|
+
// Takes back the person's last move (the state it left, as the client saw it), or their last
|
|
263
|
+
// restart or reassign of a stuck item ({"release": true}).
|
|
264
|
+
export async function undoRequest(c) {
|
|
265
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
266
|
+
const parsed = UndoBody.safeParse(raw);
|
|
267
|
+
if (!parsed.success) {
|
|
268
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"from": string} or {"release": true}');
|
|
269
|
+
}
|
|
270
|
+
return 'from' in parsed.data ? { from: parsed.data.from } : { release: true };
|
|
271
|
+
}
|
|
272
|
+
// Leaves a stuck item for the agent named.
|
|
273
|
+
export async function reassignRequest(c) {
|
|
274
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
275
|
+
const parsed = ReassignBody.safeParse(raw);
|
|
276
|
+
if (!parsed.success)
|
|
277
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"agent": string}');
|
|
278
|
+
return parsed.data;
|
|
279
|
+
}
|
|
280
|
+
const NoteBody = z.object({
|
|
281
|
+
text: z.string(),
|
|
282
|
+
anchor: z
|
|
283
|
+
.object({
|
|
284
|
+
evidence: z.number().int().positive(),
|
|
285
|
+
seconds: z.number().min(0).optional(),
|
|
286
|
+
line: z.number().int().positive().optional(),
|
|
287
|
+
quote: z.string().optional(),
|
|
288
|
+
// The quote is a whole paragraph of a document, not a phrase of it (P5-R2-8).
|
|
289
|
+
whole: z.boolean().optional(),
|
|
290
|
+
node: z.string().optional(),
|
|
291
|
+
page: z.number().int().positive().optional(),
|
|
292
|
+
x: z.number().optional(),
|
|
293
|
+
y: z.number().optional(),
|
|
294
|
+
w: z.number().optional(),
|
|
295
|
+
h: z.number().optional(),
|
|
296
|
+
})
|
|
297
|
+
.optional(),
|
|
298
|
+
});
|
|
299
|
+
const AnswerBody = z.object({ text: z.string() });
|
|
300
|
+
// The person's answer to an agent's question: its text.
|
|
301
|
+
export async function answerRequest(c) {
|
|
302
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
303
|
+
const parsed = AnswerBody.safeParse(raw);
|
|
304
|
+
if (!parsed.success)
|
|
305
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"text": string}');
|
|
306
|
+
return { text: parsed.data.text };
|
|
307
|
+
}
|
|
308
|
+
// A note from the person to the agent at work: its text, and the evidence it is on, if any.
|
|
309
|
+
export async function noteRequest(c) {
|
|
310
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
311
|
+
const parsed = NoteBody.safeParse(raw);
|
|
312
|
+
if (!parsed.success)
|
|
313
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"text": string, "anchor"?: {"evidence": number, "seconds"?: number, "line"?: number, "quote"?: string, "whole"?: boolean, "node"?: string, "x"?: number, "y"?: number, "w"?: number, "h"?: number}}');
|
|
314
|
+
const { text, anchor } = parsed.data;
|
|
315
|
+
if (anchor === undefined)
|
|
316
|
+
return { text };
|
|
317
|
+
const { evidence, seconds, line, quote, whole, node, page, x, y, w, h } = anchor;
|
|
318
|
+
return {
|
|
319
|
+
text,
|
|
320
|
+
anchor: {
|
|
321
|
+
evidence,
|
|
322
|
+
...(seconds === undefined ? {} : { seconds }),
|
|
323
|
+
...(line === undefined ? {} : { line }),
|
|
324
|
+
...(quote === undefined ? {} : { quote }),
|
|
325
|
+
...(whole === true ? { whole } : {}),
|
|
326
|
+
...(node === undefined ? {} : { node }),
|
|
327
|
+
...(page === undefined ? {} : { page }),
|
|
328
|
+
...(x === undefined ? {} : { x }),
|
|
329
|
+
...(y === undefined ? {} : { y }),
|
|
330
|
+
...(w === undefined ? {} : { w }),
|
|
331
|
+
...(h === undefined ? {} : { h }),
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const OpenedBody = z.object({ evidence: z.number().int().positive() });
|
|
336
|
+
// A file the person opened in the reader: its evidence id.
|
|
337
|
+
export async function openedRequest(c) {
|
|
338
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
339
|
+
const parsed = OpenedBody.safeParse(raw);
|
|
340
|
+
if (!parsed.success)
|
|
341
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"evidence": number}');
|
|
342
|
+
return parsed.data;
|
|
343
|
+
}
|
|
344
|
+
const OrderBody = z.object({ collection: z.string().min(1), ids: z.array(z.string().min(1)) });
|
|
345
|
+
// "Fijar el orden" from a board's matrix: the items, in order.
|
|
346
|
+
export async function orderRequest(c) {
|
|
347
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
348
|
+
const parsed = OrderBody.safeParse(raw);
|
|
349
|
+
if (!parsed.success)
|
|
350
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"collection": string, "ids": string[]}');
|
|
351
|
+
return parsed.data;
|
|
352
|
+
}
|
|
353
|
+
const PinBody = z.object({ ids: z.array(z.string().min(1)).min(1), pinned: z.boolean() });
|
|
354
|
+
// "Poner arriba" / "Quitar de arriba": the items, and whether they go on top.
|
|
355
|
+
export async function pinRequest(c) {
|
|
356
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
357
|
+
const parsed = PinBody.safeParse(raw);
|
|
358
|
+
if (!parsed.success)
|
|
359
|
+
throw new RejectedRequest(400, 'invalid_body', 'Expected {"ids": string[], "pinned": boolean}');
|
|
360
|
+
return parsed.data;
|
|
361
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isApiPath } from './hosts.js';
|
|
2
|
+
// A page opened at an address this server does not answer at: the Android emulator's 10.0.2.2, a
|
|
3
|
+
// name the router gave the computer, an old IP. The API keeps its JSON refusal (forbidden_host);
|
|
4
|
+
// a person in a browser reads what to open instead (DR2-11). The page says nothing of the project
|
|
5
|
+
// and never repeats the address it was asked for.
|
|
6
|
+
const WORDS = {
|
|
7
|
+
en: {
|
|
8
|
+
title: 'Otea does not open at this address',
|
|
9
|
+
text: 'This address does not reach Otea. On your phone, scan the pairing code your computer shows in Settings › Phone, or open the address written under it.',
|
|
10
|
+
},
|
|
11
|
+
es: {
|
|
12
|
+
title: 'Otea no abre en esta dirección',
|
|
13
|
+
text: 'Esta dirección no llega a Otea. En el teléfono, escanea el código de emparejamiento que muestra tu equipo en Configuración › Teléfono, o abre la dirección que aparece debajo.',
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
// Whether the refusal goes to a person's browser: a page asked for, outside the API.
|
|
17
|
+
export function wantsPage(c) {
|
|
18
|
+
if (c.req.method !== 'GET' || isApiPath(c.req.path))
|
|
19
|
+
return false;
|
|
20
|
+
return (c.req.header('accept') ?? '').includes('text/html');
|
|
21
|
+
}
|
|
22
|
+
export function wrongAddressPage(acceptLanguage) {
|
|
23
|
+
const lang = /^\s*es\b/i.test(acceptLanguage ?? '') ? 'es' : 'en';
|
|
24
|
+
const { title, text } = WORDS[lang];
|
|
25
|
+
return `<!doctype html>
|
|
26
|
+
<html lang="${lang}">
|
|
27
|
+
<meta charset="utf-8">
|
|
28
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
29
|
+
<meta name="color-scheme" content="light dark">
|
|
30
|
+
<title>${title}</title>
|
|
31
|
+
<style>body{font:17px/1.5 system-ui,sans-serif;max-width:32rem;margin:15vh auto;padding:0 20px}h1{font-size:1.4rem;font-weight:600}</style>
|
|
32
|
+
<h1>${title}</h1>
|
|
33
|
+
<p>${text}</p>
|
|
34
|
+
</html>
|
|
35
|
+
`;
|
|
36
|
+
}
|