@otea/cli 0.0.1 → 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 +251 -3
- 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 +72 -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,95 @@
|
|
|
1
|
+
import { AGENT_COMMANDS, toolName } from './commands.js';
|
|
2
|
+
// A token of a command line: a quoted value, or a run up to a space or the punctuation that ends a
|
|
3
|
+
// sentence or a parenthesis.
|
|
4
|
+
const TOKEN = /^(?:"[^"]*"|[^\s,;)`]+?)(?=[\s,;)`]|[.:](?:\s|$)|$)/;
|
|
5
|
+
// The arguments that name an item, and what can stand for one in a text.
|
|
6
|
+
const ITEM_ARGS = new Set(['id', 'ids...']);
|
|
7
|
+
const ITEM_VALUE = /^(?:"[^"]*"|<[^>]+>|[A-Z][A-Z0-9]*-\d+)$/;
|
|
8
|
+
const quoted = (value) => value.startsWith('"') ? value : JSON.stringify(value.replace(/\.$/, ''));
|
|
9
|
+
// One command line at `text[start]` (just after "otea "), as the tool call it is: its arguments in
|
|
10
|
+
// order, then its options, a flag as true. Undefined when it names no command the server serves.
|
|
11
|
+
function call(text, start, commands) {
|
|
12
|
+
const name = [...AGENT_COMMANDS]
|
|
13
|
+
.sort((a, b) => b.length - a.length)
|
|
14
|
+
.find((candidate) => text.startsWith(candidate, start) &&
|
|
15
|
+
/^(\s|$|[,.;)`])/.test(text.slice(start + candidate.length)));
|
|
16
|
+
const command = name && commands.find((candidate) => candidate.name === name);
|
|
17
|
+
if (!name || !command)
|
|
18
|
+
return undefined;
|
|
19
|
+
let at = start + name.length;
|
|
20
|
+
const args = command.args.map((arg) => arg.replace(/\?$/, ''));
|
|
21
|
+
const pairs = [];
|
|
22
|
+
for (;;) {
|
|
23
|
+
const rest = text.slice(at);
|
|
24
|
+
const space = rest.match(/^ +/)?.[0] ?? '';
|
|
25
|
+
const token = rest.slice(space.length).match(TOKEN)?.[0];
|
|
26
|
+
if (!space || !token || token === '/')
|
|
27
|
+
break;
|
|
28
|
+
if (token.startsWith('--')) {
|
|
29
|
+
const option = token.slice(2);
|
|
30
|
+
const spec = command.options[option];
|
|
31
|
+
if (!spec)
|
|
32
|
+
break;
|
|
33
|
+
at += space.length + token.length;
|
|
34
|
+
if (spec.flag) {
|
|
35
|
+
pairs.push(`${option}: true`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const after = text.slice(at);
|
|
39
|
+
const gap = after.match(/^ +/)?.[0] ?? '';
|
|
40
|
+
const value = after.slice(gap.length).match(TOKEN)?.[0];
|
|
41
|
+
if (!gap || !value || value.startsWith('--')) {
|
|
42
|
+
pairs.push(option);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
at += gap.length + value.length;
|
|
46
|
+
pairs.push(`${option}: ${quoted(value)}`);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const arg = args[0];
|
|
50
|
+
if (arg === undefined)
|
|
51
|
+
break;
|
|
52
|
+
// An item is named by its id, quoted or a placeholder: a word after the command is prose
|
|
53
|
+
// ("pregunta con otea ask antes de seguir").
|
|
54
|
+
if (ITEM_ARGS.has(arg) && !ITEM_VALUE.test(token))
|
|
55
|
+
break;
|
|
56
|
+
args.shift();
|
|
57
|
+
at += space.length + token.length;
|
|
58
|
+
pairs.push(`${arg}: ${quoted(token)}`);
|
|
59
|
+
}
|
|
60
|
+
const tool = toolName(name);
|
|
61
|
+
return { said: pairs.length > 0 ? `${tool} { ${pairs.join(', ')} }` : tool, end: at };
|
|
62
|
+
}
|
|
63
|
+
// What Otea tells an agent, in the words of the MCP tools it calls: `otea wait --turn agent`
|
|
64
|
+
// reads `wait { turn: "agent" }`, an option on its own (`--reason`) reads `{ reason }`. A command
|
|
65
|
+
// the server does not serve is left as the person would run it.
|
|
66
|
+
export function mcpWords(text, commands) {
|
|
67
|
+
let out = '';
|
|
68
|
+
let at = 0;
|
|
69
|
+
while (at < text.length) {
|
|
70
|
+
if (text.startsWith('otea ', at) && (at === 0 || /[\s(`"']/.test(text[at - 1] ?? ''))) {
|
|
71
|
+
// A shell line ("$ otea wait …"), which an MCP agent runs in its shell, is kept as it is.
|
|
72
|
+
const shell = text.slice(Math.max(0, at - 2), at) === '$ ';
|
|
73
|
+
const told = shell ? undefined : call(text, at + 'otea '.length, commands);
|
|
74
|
+
if (told) {
|
|
75
|
+
out += told.said;
|
|
76
|
+
at = told.end;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
// A command line the server does not serve, kept whole up to where it ends.
|
|
80
|
+
const line = text.slice(at).match(/^otea [^,;)`]*?(?=[,;)`]|[.:](?:\s|$)|$)/)?.[0] ?? 'otea ';
|
|
81
|
+
out += line;
|
|
82
|
+
at += line.length;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
const option = text.slice(at).match(/^--([a-z][a-z0-9-]*)(?: ("[^"]*"))?/);
|
|
86
|
+
if (option && (at === 0 || /[\s(`"']/.test(text[at - 1] ?? ''))) {
|
|
87
|
+
out += option[2] === undefined ? `{ ${option[1]} }` : `{ ${option[1]}: ${option[2]} }`;
|
|
88
|
+
at += option[0].length;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
out += text[at];
|
|
92
|
+
at += 1;
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { mergeAgentDraft, requestAgentDraft } from '../app/agent-draft.js';
|
|
4
|
+
import { archiveBoard, archivePlan, deleteBoardForever, deletePlan, listArchivedBoards, unarchiveBoard, } from '../app/board-archive.js';
|
|
5
|
+
import { boardProblems, createBoard, createBoardSet, discardDraft, getBoardRecord, listBoardSummaries, listTemplates, publishBoard, publishPair, publishPlan, restoreBoard, restorePair, saveDraft, } from '../app/boards.js';
|
|
6
|
+
import { LANGS } from '../core/board/templates.js';
|
|
7
|
+
import { RejectedRequest } from './errors.js';
|
|
8
|
+
import { assertConfigurable } from './writes.js';
|
|
9
|
+
// Studio's boards: drafts, publish (with its plan), restore and discard, and the templates. Reads
|
|
10
|
+
// are open like the rest of the API; every write changes how the project works, so, like Studio's
|
|
11
|
+
// other writes, it comes from this computer's signed-in browser.
|
|
12
|
+
//
|
|
13
|
+
// The bodies and answers here are the shapes the UI's board-studio/api.ts reads.
|
|
14
|
+
const lang = z.enum(LANGS);
|
|
15
|
+
const CreateBody = z.strictObject({
|
|
16
|
+
name: z.string(),
|
|
17
|
+
item: z.string().optional(),
|
|
18
|
+
template: z.string().optional(),
|
|
19
|
+
lang,
|
|
20
|
+
});
|
|
21
|
+
const SetBody = z.strictObject({
|
|
22
|
+
set: z.string(),
|
|
23
|
+
lang,
|
|
24
|
+
boards: z.array(z.strictObject({ name: z.string().optional(), item: z.string().optional() })),
|
|
25
|
+
});
|
|
26
|
+
const DraftBody = z.strictObject({
|
|
27
|
+
definition: z.unknown(),
|
|
28
|
+
baseVersion: z.number().int().positive().nullable(),
|
|
29
|
+
draftStamp: z.string().nullable().optional(),
|
|
30
|
+
});
|
|
31
|
+
const PublishBody = z.strictObject({
|
|
32
|
+
moves: z.record(z.string(), z.string()),
|
|
33
|
+
draftStamp: z.string().nullable().optional(),
|
|
34
|
+
closeRequest: z.boolean().optional(),
|
|
35
|
+
// The never-published boards its template set made with it: they go live with it.
|
|
36
|
+
alongside: z.array(z.string()).optional(),
|
|
37
|
+
});
|
|
38
|
+
const RestoreBody = z.strictObject({
|
|
39
|
+
version: z.number().int().positive().nullable(),
|
|
40
|
+
alongside: z.array(z.string()).optional(),
|
|
41
|
+
});
|
|
42
|
+
const Moves = z.record(z.string(), z.string());
|
|
43
|
+
const Stamp = z.string().nullable();
|
|
44
|
+
const Version = z.number().int().positive().nullable();
|
|
45
|
+
// A board that "se divide en" another and that board; a role is acted on when it is named.
|
|
46
|
+
const PublishPairBody = z.strictObject({
|
|
47
|
+
parent: z.string(),
|
|
48
|
+
child: z.string(),
|
|
49
|
+
moves: z.strictObject({ parent: Moves.optional(), child: Moves.optional() }),
|
|
50
|
+
draftStamps: z.strictObject({ parent: Stamp.optional(), child: Stamp.optional() }).optional(),
|
|
51
|
+
closeRequests: z
|
|
52
|
+
.strictObject({ parent: z.boolean().optional(), child: z.boolean().optional() })
|
|
53
|
+
.optional(),
|
|
54
|
+
});
|
|
55
|
+
const RestorePairBody = z.strictObject({
|
|
56
|
+
parent: z.string(),
|
|
57
|
+
child: z.string(),
|
|
58
|
+
versions: z.strictObject({ parent: Version.optional(), child: Version.optional() }),
|
|
59
|
+
});
|
|
60
|
+
// Drops the roles a body left out, so an absent role stays absent (exactOptionalPropertyTypes).
|
|
61
|
+
function roles(value) {
|
|
62
|
+
return {
|
|
63
|
+
...(value?.parent === undefined ? {} : { parent: value.parent }),
|
|
64
|
+
...(value?.child === undefined ? {} : { child: value.child }),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const AgentDraftBody = z.strictObject({
|
|
68
|
+
name: z.string(),
|
|
69
|
+
need: z.string(),
|
|
70
|
+
agent: z.string().min(1).nullable(),
|
|
71
|
+
lang,
|
|
72
|
+
});
|
|
73
|
+
const MergeBody = z.strictObject({ evidence: z.number().int().positive() });
|
|
74
|
+
const EmptyBody = z.strictObject({});
|
|
75
|
+
// The board's name, typed by the person, when the delete takes items with it.
|
|
76
|
+
const DeleteBody = z.strictObject({ confirm: z.string().optional() });
|
|
77
|
+
const HUMAN = { kind: 'human', name: 'human' };
|
|
78
|
+
async function body(c, schema, expected) {
|
|
79
|
+
const raw = await c.req.json().catch(() => undefined);
|
|
80
|
+
const parsed = schema.safeParse(raw);
|
|
81
|
+
if (!parsed.success)
|
|
82
|
+
throw new RejectedRequest(400, 'invalid_body', `Expected ${expected}`);
|
|
83
|
+
return parsed.data;
|
|
84
|
+
}
|
|
85
|
+
function langOf(c) {
|
|
86
|
+
return c.req.query('lang') === 'es' ? 'es' : 'en';
|
|
87
|
+
}
|
|
88
|
+
export function boardsApi(project, feed, auth) {
|
|
89
|
+
const id = (c) => c.req.param('id') ?? '';
|
|
90
|
+
return (new Hono()
|
|
91
|
+
.get('/templates', (c) => c.json({ templates: listTemplates(langOf(c)) }))
|
|
92
|
+
.get('/boards', (c) => c.json({ boards: listBoardSummaries(project) }))
|
|
93
|
+
.post('/boards', async (c) => {
|
|
94
|
+
assertConfigurable(c, auth);
|
|
95
|
+
const input = await body(c, CreateBody, '{"name": string, "item"?: string, "template"?: string, "lang": "en" | "es"}');
|
|
96
|
+
const record = createBoard(project, {
|
|
97
|
+
name: input.name,
|
|
98
|
+
lang: input.lang,
|
|
99
|
+
...(input.item === undefined ? {} : { item: input.item }),
|
|
100
|
+
...(input.template === undefined ? {} : { template: input.template }),
|
|
101
|
+
});
|
|
102
|
+
feed.notify();
|
|
103
|
+
return c.json(record, 201);
|
|
104
|
+
})
|
|
105
|
+
// A template that makes several boards: every one of them, as drafts, or none.
|
|
106
|
+
.post('/boards/set', async (c) => {
|
|
107
|
+
assertConfigurable(c, auth);
|
|
108
|
+
const input = await body(c, SetBody, '{"set": string, "lang": "en" | "es", "boards": [{"name"?: string, "item"?: string}]}');
|
|
109
|
+
const records = createBoardSet(project, input);
|
|
110
|
+
feed.notify();
|
|
111
|
+
return c.json({ records }, 201);
|
|
112
|
+
})
|
|
113
|
+
// "Que lo esboce un agente": creates the Pedido and a waiting board (server slice).
|
|
114
|
+
.post('/boards/agent-draft', async (c) => {
|
|
115
|
+
assertConfigurable(c, auth);
|
|
116
|
+
const input = await body(c, AgentDraftBody, '{"name": string, "need": string, "agent": string | null, "lang": "en" | "es"}');
|
|
117
|
+
const asked = requestAgentDraft(project, input);
|
|
118
|
+
feed.notify();
|
|
119
|
+
return c.json(asked, 201);
|
|
120
|
+
})
|
|
121
|
+
// The joined workspace's Publicar: both boards of a pair (or the one with changes), as one.
|
|
122
|
+
.post('/boards/publish-pair', async (c) => {
|
|
123
|
+
assertConfigurable(c, auth);
|
|
124
|
+
const input = await body(c, PublishPairBody, '{"parent": id, "child": id, "moves": {"parent"?: moves, "child"?: moves}, "draftStamps"?: {"parent"?: string | null, "child"?: string | null}, "closeRequests"?: {"parent"?: boolean, "child"?: boolean}}');
|
|
125
|
+
const result = publishPair(project, {
|
|
126
|
+
parent: input.parent,
|
|
127
|
+
child: input.child,
|
|
128
|
+
moves: roles(input.moves),
|
|
129
|
+
draftStamps: roles(input.draftStamps),
|
|
130
|
+
closeRequests: roles(input.closeRequests),
|
|
131
|
+
by: HUMAN,
|
|
132
|
+
});
|
|
133
|
+
feed.notify();
|
|
134
|
+
return c.json(result);
|
|
135
|
+
})
|
|
136
|
+
// Its Deshacer: both boards back, or neither.
|
|
137
|
+
.post('/boards/restore-pair', async (c) => {
|
|
138
|
+
assertConfigurable(c, auth);
|
|
139
|
+
const input = await body(c, RestorePairBody, '{"parent": id, "child": id, "versions": {"parent"?: number | null, "child"?: number | null}}');
|
|
140
|
+
const result = restorePair(project, {
|
|
141
|
+
parent: input.parent,
|
|
142
|
+
child: input.child,
|
|
143
|
+
versions: roles(input.versions),
|
|
144
|
+
by: HUMAN,
|
|
145
|
+
});
|
|
146
|
+
feed.notify();
|
|
147
|
+
return c.json(result);
|
|
148
|
+
})
|
|
149
|
+
// Archivados: the archived boards and their items, to read and search (board-archive.ts).
|
|
150
|
+
.get('/boards/archived', (c) => c.json({ boards: listArchivedBoards(project) }))
|
|
151
|
+
.get('/boards/:id/archive/plan', (c) => c.json(archivePlan(project, id(c))))
|
|
152
|
+
.get('/boards/:id/delete/plan', (c) => c.json(deletePlan(project, id(c))))
|
|
153
|
+
// Archiving, restoring and deleting a board are the person's, like publishing: only from
|
|
154
|
+
// this computer's signed-in browser, never an agent, a forwarded request or a phone.
|
|
155
|
+
.post('/boards/:id/archive', async (c) => {
|
|
156
|
+
assertConfigurable(c, auth);
|
|
157
|
+
await body(c, EmptyBody, '{}');
|
|
158
|
+
const result = archiveBoard(project, id(c), { by: HUMAN });
|
|
159
|
+
feed.notify();
|
|
160
|
+
return c.json(result);
|
|
161
|
+
})
|
|
162
|
+
.post('/boards/:id/unarchive', async (c) => {
|
|
163
|
+
assertConfigurable(c, auth);
|
|
164
|
+
await body(c, EmptyBody, '{}');
|
|
165
|
+
const result = unarchiveBoard(project, id(c), { by: HUMAN });
|
|
166
|
+
feed.notify();
|
|
167
|
+
return c.json(result);
|
|
168
|
+
})
|
|
169
|
+
.delete('/boards/:id', async (c) => {
|
|
170
|
+
assertConfigurable(c, auth);
|
|
171
|
+
const { confirm } = await body(c, DeleteBody, '{"confirm"?: string}');
|
|
172
|
+
const result = deleteBoardForever(project, id(c), confirm === undefined ? {} : { confirm });
|
|
173
|
+
feed.notify();
|
|
174
|
+
return c.json(result);
|
|
175
|
+
})
|
|
176
|
+
.get('/boards/:id', (c) => {
|
|
177
|
+
const record = getBoardRecord(project, id(c));
|
|
178
|
+
const definition = (record.draft ?? record.live)?.definition;
|
|
179
|
+
const problems = definition ? boardProblems(project, record.id, definition) : [];
|
|
180
|
+
return c.json({ record, problems });
|
|
181
|
+
})
|
|
182
|
+
.put('/boards/:id/draft', async (c) => {
|
|
183
|
+
assertConfigurable(c, auth);
|
|
184
|
+
const input = await body(c, DraftBody, '{"definition": board, "baseVersion": number | null}');
|
|
185
|
+
const saved = saveDraft(project, id(c), {
|
|
186
|
+
definition: input.definition,
|
|
187
|
+
baseVersion: input.baseVersion,
|
|
188
|
+
...(input.draftStamp === undefined ? {} : { draftStamp: input.draftStamp }),
|
|
189
|
+
});
|
|
190
|
+
feed.notify();
|
|
191
|
+
return c.json(saved);
|
|
192
|
+
})
|
|
193
|
+
.delete('/boards/:id/draft', (c) => {
|
|
194
|
+
assertConfigurable(c, auth);
|
|
195
|
+
const result = discardDraft(project, id(c));
|
|
196
|
+
feed.notify();
|
|
197
|
+
return c.json(result);
|
|
198
|
+
})
|
|
199
|
+
// Merges an agent's board draft (its evidence on the Pedido) into the draft (server slice).
|
|
200
|
+
.post('/boards/:id/draft/merge', async (c) => {
|
|
201
|
+
assertConfigurable(c, auth);
|
|
202
|
+
const { evidence } = await body(c, MergeBody, '{"evidence": number}');
|
|
203
|
+
const record = mergeAgentDraft(project, id(c), evidence);
|
|
204
|
+
feed.notify();
|
|
205
|
+
return c.json(record);
|
|
206
|
+
})
|
|
207
|
+
.post('/boards/:id/publish/plan', (c) => c.json(publishPlan(project, id(c))))
|
|
208
|
+
.post('/boards/:id/publish', async (c) => {
|
|
209
|
+
assertConfigurable(c, auth);
|
|
210
|
+
const { moves, draftStamp, closeRequest, alongside } = await body(c, PublishBody, '{"moves": {[stepId]: stepId | "done"}, "draftStamp"?: string | null, "closeRequest"?: boolean, "alongside"?: [id]}');
|
|
211
|
+
const result = publishBoard(project, id(c), {
|
|
212
|
+
moves,
|
|
213
|
+
by: HUMAN,
|
|
214
|
+
...(draftStamp === undefined ? {} : { draftStamp }),
|
|
215
|
+
...(closeRequest === undefined ? {} : { closeRequest }),
|
|
216
|
+
...(alongside === undefined ? {} : { alongside }),
|
|
217
|
+
});
|
|
218
|
+
feed.notify();
|
|
219
|
+
return c.json(result);
|
|
220
|
+
})
|
|
221
|
+
.post('/boards/:id/restore', async (c) => {
|
|
222
|
+
assertConfigurable(c, auth);
|
|
223
|
+
const { version, alongside } = await body(c, RestoreBody, '{"version": number | null, "alongside"?: [id]}');
|
|
224
|
+
const record = restoreBoard(project, id(c), {
|
|
225
|
+
version,
|
|
226
|
+
by: HUMAN,
|
|
227
|
+
...(alongside === undefined ? {} : { alongside }),
|
|
228
|
+
});
|
|
229
|
+
feed.notify();
|
|
230
|
+
return c.json(record);
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { dataVersion } from '../store/database.js';
|
|
2
|
+
export function createChangeFeed(db, intervalMs = 250) {
|
|
3
|
+
const listeners = new Set();
|
|
4
|
+
let timer;
|
|
5
|
+
let version = 0;
|
|
6
|
+
const notify = () => {
|
|
7
|
+
for (const listener of listeners)
|
|
8
|
+
listener();
|
|
9
|
+
};
|
|
10
|
+
const poll = () => {
|
|
11
|
+
const current = dataVersion(db);
|
|
12
|
+
if (current === version)
|
|
13
|
+
return;
|
|
14
|
+
version = current;
|
|
15
|
+
notify();
|
|
16
|
+
};
|
|
17
|
+
const start = () => {
|
|
18
|
+
version = dataVersion(db);
|
|
19
|
+
timer = setInterval(poll, intervalMs);
|
|
20
|
+
};
|
|
21
|
+
const stop = () => {
|
|
22
|
+
clearInterval(timer);
|
|
23
|
+
timer = undefined;
|
|
24
|
+
};
|
|
25
|
+
return {
|
|
26
|
+
subscribe(listener) {
|
|
27
|
+
listeners.add(listener);
|
|
28
|
+
if (!timer)
|
|
29
|
+
start();
|
|
30
|
+
return () => {
|
|
31
|
+
listeners.delete(listener);
|
|
32
|
+
if (listeners.size === 0)
|
|
33
|
+
stop();
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
notify,
|
|
37
|
+
close() {
|
|
38
|
+
listeners.clear();
|
|
39
|
+
stop();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { evidenceFile } from '../app/evidence.js';
|
|
4
|
+
import { getItem } from '../app/items.js';
|
|
5
|
+
import { liveBoardOf } from '../app/next.js';
|
|
6
|
+
import { approvedTipOf } from '../app/tips.js';
|
|
7
|
+
import { isClosed, positionOf } from '../core/board/engine.js';
|
|
8
|
+
import { guardedBranch } from '../core/board/model.js';
|
|
9
|
+
import { selectEvidence } from '../store/queries.js';
|
|
10
|
+
// Git runs off the request's thread, as for the drift.
|
|
11
|
+
function git(cwd, ...args) {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
execFile('git', args, { cwd, encoding: 'utf8', timeout: 20_000, windowsHide: true }, (error, stdout) => (error ? reject(error) : resolve(String(stdout).trim())));
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
async function blobAt(cwd, commit, path) {
|
|
17
|
+
try {
|
|
18
|
+
return (await git(cwd, 'rev-parse', '--verify', '--quiet', `${commit}:${path}`)) || undefined;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// What each file of a git diff becomes, by the blob its index line names (all zeros: deleted).
|
|
25
|
+
// A file whose path git had to quote, or with no index line (a rename alone, a diff made by hand),
|
|
26
|
+
// says nothing and is left out.
|
|
27
|
+
export function diffBlobs(diff) {
|
|
28
|
+
const out = [];
|
|
29
|
+
for (const section of diff.split(/^(?=diff --git )/m)) {
|
|
30
|
+
const head = /^diff --git a\/(\S+) b\/(\S+)\s*$/m.exec(section);
|
|
31
|
+
const index = /^index [0-9a-f]+\.\.([0-9a-f]+)(?: \d+)?\s*$/m.exec(section);
|
|
32
|
+
if (!head?.[2] || !index?.[1])
|
|
33
|
+
continue;
|
|
34
|
+
out.push({ path: head[2], blob: index[1] });
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
// The latest diff attached to an item in review, against the tip its handoff recorded; null while
|
|
39
|
+
// it is not in review, no tip was recorded, it has no diff git can read, or the diff matches.
|
|
40
|
+
export async function diffOffTip(project, id) {
|
|
41
|
+
const item = getItem(project, id);
|
|
42
|
+
const board = liveBoardOf(project, item.collection);
|
|
43
|
+
const guard = board ? guardedBranch(board.def) : undefined;
|
|
44
|
+
const position = positionOf(item.state);
|
|
45
|
+
if (!guard || isClosed(position) || position.phase === 'doing')
|
|
46
|
+
return null;
|
|
47
|
+
const branch = String(item.fields[guard.id] ?? '').trim();
|
|
48
|
+
if (branch === '' || branch === guard.guard.base)
|
|
49
|
+
return null;
|
|
50
|
+
const tip = approvedTipOf(project, item.id, branch);
|
|
51
|
+
if (!tip || tip.unchanged)
|
|
52
|
+
return null;
|
|
53
|
+
const diff = selectEvidence(project.db, item.id)
|
|
54
|
+
.filter(({ kind }) => kind === 'diff')
|
|
55
|
+
.at(-1);
|
|
56
|
+
if (!diff)
|
|
57
|
+
return null;
|
|
58
|
+
let text;
|
|
59
|
+
try {
|
|
60
|
+
text = await readFile(evidenceFile(project.dataDir, diff.sha256), 'utf8');
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const blobs = diffBlobs(text);
|
|
66
|
+
if (blobs.length === 0)
|
|
67
|
+
return null;
|
|
68
|
+
const cwd = project.root;
|
|
69
|
+
try {
|
|
70
|
+
await git(cwd, 'rev-parse', '--verify', '--quiet', `${tip.commit}^{commit}`);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const files = [];
|
|
76
|
+
for (const { path, blob } of blobs) {
|
|
77
|
+
const there = await blobAt(cwd, tip.commit, path);
|
|
78
|
+
const deleted = /^0+$/.test(blob);
|
|
79
|
+
if (deleted ? there !== undefined : !there?.startsWith(blob))
|
|
80
|
+
files.push(path);
|
|
81
|
+
}
|
|
82
|
+
return files.length === 0 ? null : { branch, tip: tip.commit.slice(0, 7), files };
|
|
83
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { getItem } from '../app/items.js';
|
|
3
|
+
import { liveBoardOf } from '../app/next.js';
|
|
4
|
+
import { approvedTipOf } from '../app/tips.js';
|
|
5
|
+
import { commitsAddedSince } from '../cli/gitrepo.js';
|
|
6
|
+
import { isClosed, positionOf } from '../core/board/engine.js';
|
|
7
|
+
import { guardedBranch } from '../core/board/model.js';
|
|
8
|
+
// Git runs off the request's thread: asking the remote can take seconds.
|
|
9
|
+
function git(cwd, ...args) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
execFile('git', args, { cwd, encoding: 'utf8', timeout: 20_000, windowsHide: true }, (error, stdout) => (error ? reject(error) : resolve(String(stdout).trim())));
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
async function has(cwd, ref) {
|
|
15
|
+
try {
|
|
16
|
+
await git(cwd, 'rev-parse', '--verify', '--quiet', `${ref}^{commit}`);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// The remote the branch is pushed to: the one its upstream names, else origin.
|
|
24
|
+
async function remoteOf(cwd, branch) {
|
|
25
|
+
try {
|
|
26
|
+
const named = await git(cwd, 'config', '--get', `branch.${branch}.remote`);
|
|
27
|
+
return named === '' || named === '.' ? 'origin' : named;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return 'origin';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
// One answer per branch and handed-off commit for a few seconds: the page asks again on each look.
|
|
34
|
+
const FRESH_MS = 20_000;
|
|
35
|
+
const asked = new Map();
|
|
36
|
+
// The drift of an item's protected branch while it waits for the person's review; null when it
|
|
37
|
+
// is not in review, its handoff recorded no tip, the remote still has that tip, or git cannot
|
|
38
|
+
// tell (no remote, offline): the desk then says nothing rather than guess.
|
|
39
|
+
export async function branchDrift(project, id) {
|
|
40
|
+
const item = getItem(project, id);
|
|
41
|
+
const board = liveBoardOf(project, item.collection);
|
|
42
|
+
const guard = board ? guardedBranch(board.def) : undefined;
|
|
43
|
+
const position = positionOf(item.state);
|
|
44
|
+
if (!guard || isClosed(position) || position.phase === 'doing')
|
|
45
|
+
return null;
|
|
46
|
+
const branch = String(item.fields[guard.id] ?? '').trim();
|
|
47
|
+
if (branch === '' || branch === guard.guard.base)
|
|
48
|
+
return null;
|
|
49
|
+
const tip = approvedTipOf(project, item.id, branch);
|
|
50
|
+
if (!tip || tip.unchanged)
|
|
51
|
+
return null;
|
|
52
|
+
const key = [project.root, branch, tip.commit].join('\0');
|
|
53
|
+
const now = Date.now();
|
|
54
|
+
const known = asked.get(key);
|
|
55
|
+
if (known && now - known.at < FRESH_MS)
|
|
56
|
+
return known.drift;
|
|
57
|
+
const base = guard.guard.base;
|
|
58
|
+
const drift = (async () => {
|
|
59
|
+
const cwd = project.root;
|
|
60
|
+
try {
|
|
61
|
+
const remote = await remoteOf(cwd, branch);
|
|
62
|
+
const remotes = (await git(cwd, 'remote')).split(/\r?\n/).filter(Boolean);
|
|
63
|
+
if (!remotes.includes(remote))
|
|
64
|
+
return null;
|
|
65
|
+
const listed = await git(cwd, 'ls-remote', '--heads', remote, `refs/heads/${branch}`);
|
|
66
|
+
const at = listed.split(/\s+/)[0] ?? '';
|
|
67
|
+
if (at === '' || at === tip.commit)
|
|
68
|
+
return null;
|
|
69
|
+
if (!(await has(cwd, at))) {
|
|
70
|
+
await git(cwd, 'fetch', '--quiet', '--no-tags', remote, `+refs/heads/${branch}:refs/remotes/${remote}/${branch}`);
|
|
71
|
+
}
|
|
72
|
+
const onto = base && (await has(cwd, `refs/remotes/${remote}/${base}`));
|
|
73
|
+
const start = onto ? `refs/remotes/${remote}/${base}` : undefined;
|
|
74
|
+
return {
|
|
75
|
+
branch,
|
|
76
|
+
handed: tip.commit.slice(0, 7),
|
|
77
|
+
now: at.slice(0, 7),
|
|
78
|
+
added: commitsAddedSince(cwd, tip, at, start),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
})();
|
|
85
|
+
asked.set(key, { at: now, drift });
|
|
86
|
+
return drift;
|
|
87
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { OteaError } from '../core/errors.js';
|
|
2
|
+
export const HTTP_STATUS = {
|
|
3
|
+
not_found: 404,
|
|
4
|
+
invalid_transition: 409,
|
|
5
|
+
not_undoable: 409,
|
|
6
|
+
not_stuck: 409,
|
|
7
|
+
already_exists: 409,
|
|
8
|
+
invalid_name: 400,
|
|
9
|
+
invalid_agent_name: 400,
|
|
10
|
+
unnamed_agent: 400,
|
|
11
|
+
invalid_icon: 400,
|
|
12
|
+
invalid_id: 400,
|
|
13
|
+
forbidden: 403,
|
|
14
|
+
reason_required: 400,
|
|
15
|
+
invalid_preset: 500,
|
|
16
|
+
invalid_fields: 422,
|
|
17
|
+
invalid_type: 422,
|
|
18
|
+
item_closed: 409,
|
|
19
|
+
missing_fields: 422,
|
|
20
|
+
not_claimable: 409,
|
|
21
|
+
lease_held: 409,
|
|
22
|
+
not_lease_holder: 409,
|
|
23
|
+
name_in_use: 409,
|
|
24
|
+
not_relayable: 409,
|
|
25
|
+
quote_required: 400,
|
|
26
|
+
file_not_found: 404,
|
|
27
|
+
evidence_too_large: 413,
|
|
28
|
+
evidence_not_accepted: 422,
|
|
29
|
+
module_required: 400,
|
|
30
|
+
invalid_role: 400,
|
|
31
|
+
invalid_relation: 400,
|
|
32
|
+
invalid_gate: 400,
|
|
33
|
+
invalid_profile: 400,
|
|
34
|
+
profile_required: 400,
|
|
35
|
+
self_relation: 400,
|
|
36
|
+
parent_exists: 409,
|
|
37
|
+
parent_type: 409,
|
|
38
|
+
parent_required: 422,
|
|
39
|
+
relation_cycle: 409,
|
|
40
|
+
parent_across_boards: 409,
|
|
41
|
+
children_open: 409,
|
|
42
|
+
blockers_open: 409,
|
|
43
|
+
different_repo: 409,
|
|
44
|
+
in_use: 409,
|
|
45
|
+
invalid_draft: 422,
|
|
46
|
+
invalid_board: 422,
|
|
47
|
+
board_conflict: 409,
|
|
48
|
+
transport_unavailable: 409,
|
|
49
|
+
board_archived: 409,
|
|
50
|
+
not_publishable: 422,
|
|
51
|
+
};
|
|
52
|
+
export class InvalidQuery extends Error {
|
|
53
|
+
}
|
|
54
|
+
export class ForbiddenHost extends Error {
|
|
55
|
+
}
|
|
56
|
+
export class RejectedRequest extends Error {
|
|
57
|
+
status;
|
|
58
|
+
code;
|
|
59
|
+
details;
|
|
60
|
+
constructor(status, code, message, details) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.status = status;
|
|
63
|
+
this.code = code;
|
|
64
|
+
this.details = details;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function errorBody(code, message, details) {
|
|
68
|
+
return { error: { code, message, ...(details === undefined ? {} : { details }) } };
|
|
69
|
+
}
|
|
70
|
+
export function toErrorResponse(error) {
|
|
71
|
+
if (error instanceof OteaError) {
|
|
72
|
+
return {
|
|
73
|
+
status: HTTP_STATUS[error.code],
|
|
74
|
+
body: errorBody(error.code, error.message, error.details),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
if (error instanceof InvalidQuery) {
|
|
78
|
+
return { status: 400, body: errorBody('invalid_query', error.message) };
|
|
79
|
+
}
|
|
80
|
+
if (error instanceof RejectedRequest) {
|
|
81
|
+
return { status: error.status, body: errorBody(error.code, error.message, error.details) };
|
|
82
|
+
}
|
|
83
|
+
if (error instanceof ForbiddenHost) {
|
|
84
|
+
return { status: 403, body: errorBody('forbidden_host', error.message) };
|
|
85
|
+
}
|
|
86
|
+
return { status: 500, body: errorBody('unexpected', 'Unexpected error') };
|
|
87
|
+
}
|