@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,80 @@
|
|
|
1
|
+
// The diagram module (ui/src/diagram.ts lays it out) draws a Mermaid flowchart an agent attached,
|
|
2
|
+
// without running Mermaid: a strict subset (`flowchart` or `graph`, LR/RL/TD/TB/BT, `id[label]`-style
|
|
3
|
+
// nodes, `-->`-style edges with an optional `|label|`) is parsed here, and attach checks a diagram
|
|
4
|
+
// with it. Nothing in the file can add links, scripts, HTML or images. A file outside the subset
|
|
5
|
+
// does not parse: the module shows its source and the line that stopped the parser.
|
|
6
|
+
const HEADER = /^(?:flowchart|graph)\s+(LR|RL|TD|TB|BT)\s*;?$/;
|
|
7
|
+
const IGNORED = /^(?:classDef|class|style|linkStyle|click|subgraph|end|direction)\b/;
|
|
8
|
+
const NODE = /^([A-Za-z0-9_]+)(?:(\[\[|\[\(|\(\(|\(\[|\[|\(|\{)(.*?)(\]\]|\)\]|\)\)|\]\)|\]|\)|\}))?(?::::[\w-]+)?/;
|
|
9
|
+
const EDGE = /^(-\.->|==>|-->|---|-\.-|--\s+([^-][^>]*?)\s+-->)(?:\|([^|]*)\|)?/;
|
|
10
|
+
function labelOf(raw) {
|
|
11
|
+
const text = raw.replace(/^"(.*)"$/, '$1').trim();
|
|
12
|
+
const split = text.indexOf(' · ');
|
|
13
|
+
if (split < 0)
|
|
14
|
+
return { title: text };
|
|
15
|
+
return { title: text.slice(0, split).trim(), sub: text.slice(split + 3).trim() };
|
|
16
|
+
}
|
|
17
|
+
const shapeOf = (open) => open === '{' ? 'diamond' : open === '(' || open === '((' || open === '([' ? 'round' : 'box';
|
|
18
|
+
export function parseDiagram(source) {
|
|
19
|
+
const lines = source.split(/\r?\n/);
|
|
20
|
+
const nodes = new Map();
|
|
21
|
+
const edges = [];
|
|
22
|
+
let direction;
|
|
23
|
+
const fail = (line, message, why, at) => ({
|
|
24
|
+
ok: false,
|
|
25
|
+
line,
|
|
26
|
+
message,
|
|
27
|
+
why,
|
|
28
|
+
...(at === undefined ? {} : { at }),
|
|
29
|
+
});
|
|
30
|
+
const node = (id, open, label) => {
|
|
31
|
+
const known = nodes.get(id);
|
|
32
|
+
if (known && label === undefined)
|
|
33
|
+
return;
|
|
34
|
+
nodes.set(id, { id, ...labelOf(label ?? known?.title ?? id), shape: shapeOf(open) });
|
|
35
|
+
};
|
|
36
|
+
for (const [index, raw] of lines.entries()) {
|
|
37
|
+
const line = raw.trim().replace(/;$/, '');
|
|
38
|
+
if (line === '' || line.startsWith('%%'))
|
|
39
|
+
continue;
|
|
40
|
+
if (direction === undefined) {
|
|
41
|
+
const header = HEADER.exec(line);
|
|
42
|
+
if (!header)
|
|
43
|
+
return fail(index + 1, 'expected "flowchart LR" (or TD, RL, BT) first', 'header');
|
|
44
|
+
const named = header[1] === 'TB' ? 'TD' : header[1];
|
|
45
|
+
direction = named;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (IGNORED.test(line))
|
|
49
|
+
continue;
|
|
50
|
+
let rest = line;
|
|
51
|
+
let previous;
|
|
52
|
+
let pending;
|
|
53
|
+
while (rest !== '') {
|
|
54
|
+
const found = NODE.exec(rest);
|
|
55
|
+
if (!found)
|
|
56
|
+
return fail(index + 1, `cannot read "${rest}"`, 'unread', rest);
|
|
57
|
+
const [whole, id = '', open, label] = found;
|
|
58
|
+
node(id, open, label);
|
|
59
|
+
if (previous !== undefined && pending)
|
|
60
|
+
edges.push({ from: previous, to: id, ...pending });
|
|
61
|
+
previous = id;
|
|
62
|
+
pending = undefined;
|
|
63
|
+
rest = rest.slice(whole.length).trim();
|
|
64
|
+
if (rest === '')
|
|
65
|
+
break;
|
|
66
|
+
const edge = EDGE.exec(rest);
|
|
67
|
+
if (!edge)
|
|
68
|
+
return fail(index + 1, `expected an edge such as --> before "${rest}"`, 'edge', rest);
|
|
69
|
+
const [arrow, kind = '', inline, piped] = edge;
|
|
70
|
+
const text = (piped ?? inline)?.trim();
|
|
71
|
+
pending = { dashed: kind.startsWith('-.'), ...(text ? { label: text } : {}) };
|
|
72
|
+
rest = rest.slice(arrow.length).trim();
|
|
73
|
+
if (rest === '')
|
|
74
|
+
return fail(index + 1, 'an edge needs a node after it', 'edgeEnd');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (direction === undefined)
|
|
78
|
+
return fail(1, 'the file is empty', 'empty');
|
|
79
|
+
return { ok: true, diagram: { direction, nodes: [...nodes.values()], edges } };
|
|
80
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// `details`: what the refusal says, as data, for the web UI to word it in its own language; the
|
|
2
|
+
// message is the English agents and the CLI read.
|
|
3
|
+
export class OteaError extends Error {
|
|
4
|
+
code;
|
|
5
|
+
details;
|
|
6
|
+
constructor(code, message, details) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'OteaError';
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.details = details;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Before/after pairs carry no pair id: a viewer pairs the latest before with the latest after.
|
|
2
|
+
export const EVIDENCE_ROLES = ['before', 'after', 'single'];
|
|
3
|
+
export const MAX_EVIDENCE_MB = 50;
|
|
4
|
+
export const MAX_EVIDENCE_BYTES = MAX_EVIDENCE_MB * 1024 * 1024;
|
|
5
|
+
// Only formats a browser renders without running anything are images, videos and audio. SVG is a
|
|
6
|
+
// document that can carry scripts, so it stays an opaque file the server only offers as a download.
|
|
7
|
+
const TYPES = {
|
|
8
|
+
gif: { kind: 'image', contentType: 'image/gif' },
|
|
9
|
+
png: { kind: 'image', contentType: 'image/png' },
|
|
10
|
+
jpg: { kind: 'image', contentType: 'image/jpeg' },
|
|
11
|
+
jpeg: { kind: 'image', contentType: 'image/jpeg' },
|
|
12
|
+
webp: { kind: 'image', contentType: 'image/webp' },
|
|
13
|
+
mp4: { kind: 'video', contentType: 'video/mp4' },
|
|
14
|
+
webm: { kind: 'video', contentType: 'video/webm' },
|
|
15
|
+
mov: { kind: 'video', contentType: 'video/quicktime' },
|
|
16
|
+
mp3: { kind: 'audio', contentType: 'audio/mpeg' },
|
|
17
|
+
wav: { kind: 'audio', contentType: 'audio/wav' },
|
|
18
|
+
ogg: { kind: 'audio', contentType: 'audio/ogg' },
|
|
19
|
+
diff: { kind: 'diff', contentType: 'text/x-diff' },
|
|
20
|
+
patch: { kind: 'diff', contentType: 'text/x-diff' },
|
|
21
|
+
log: { kind: 'log', contentType: 'text/plain' },
|
|
22
|
+
txt: { kind: 'log', contentType: 'text/plain' },
|
|
23
|
+
// A Mermaid diagram is its source: text the diagram module draws, never a page the browser runs.
|
|
24
|
+
mmd: { kind: 'diagram', contentType: 'text/plain' },
|
|
25
|
+
mermaid: { kind: 'diagram', contentType: 'text/plain' },
|
|
26
|
+
svg: { kind: 'file', contentType: 'image/svg+xml' },
|
|
27
|
+
};
|
|
28
|
+
const OPAQUE = { kind: 'file', contentType: 'application/octet-stream' };
|
|
29
|
+
export function evidenceTypeOf(filename) {
|
|
30
|
+
const dot = filename.lastIndexOf('.');
|
|
31
|
+
const extension = dot < 0 ? '' : filename.slice(dot + 1).toLowerCase();
|
|
32
|
+
return TYPES[extension] ?? OPAQUE;
|
|
33
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { OteaError } from './errors.js';
|
|
2
|
+
import { stateLabel, turnOf } from './workflow.js';
|
|
3
|
+
function isBlank(value) {
|
|
4
|
+
return value === undefined || value.trim() === '';
|
|
5
|
+
}
|
|
6
|
+
// The name a reader knows a field by: its label (a board part's name), or its key.
|
|
7
|
+
function said(name, spec) {
|
|
8
|
+
return spec.label ?? name;
|
|
9
|
+
}
|
|
10
|
+
export function valueProblem(name, spec, value) {
|
|
11
|
+
if (spec.values && !spec.values.includes(value)) {
|
|
12
|
+
return `field "${said(name, spec)}" must be one of ${spec.values.join(', ')}`;
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
export function intake(preset, input) {
|
|
17
|
+
const unknown = Object.keys(input).filter((name) => !(name in preset.fields));
|
|
18
|
+
const problems = unknown.map((name) => `unknown field "${name}"`);
|
|
19
|
+
// The fields refused, by key, for a program to read beside the names in the message.
|
|
20
|
+
const refused = [...unknown];
|
|
21
|
+
const values = {};
|
|
22
|
+
let complete = true;
|
|
23
|
+
for (const [name, spec] of Object.entries(preset.fields)) {
|
|
24
|
+
const given = input[name];
|
|
25
|
+
const value = isBlank(given) ? spec.default : given;
|
|
26
|
+
if (value === undefined) {
|
|
27
|
+
if (spec.required === 'create') {
|
|
28
|
+
problems.push(`missing required field "${said(name, spec)}"`);
|
|
29
|
+
refused.push(name);
|
|
30
|
+
}
|
|
31
|
+
if (spec.required === 'complete')
|
|
32
|
+
complete = false;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const problem = valueProblem(name, spec, value);
|
|
36
|
+
if (problem) {
|
|
37
|
+
problems.push(problem);
|
|
38
|
+
refused.push(name);
|
|
39
|
+
}
|
|
40
|
+
values[name] = value;
|
|
41
|
+
}
|
|
42
|
+
if (problems.length > 0)
|
|
43
|
+
throw new OteaError('invalid_fields', problems.join('; '), {
|
|
44
|
+
kind: 'invalid_fields',
|
|
45
|
+
fields: refused,
|
|
46
|
+
});
|
|
47
|
+
const { title = '', ...fields } = values;
|
|
48
|
+
const { initial, incomplete } = preset.workflow;
|
|
49
|
+
return { title, fields, state: complete ? initial : (incomplete ?? initial) };
|
|
50
|
+
}
|
|
51
|
+
function stored(value) {
|
|
52
|
+
return typeof value === 'string' ? value : null;
|
|
53
|
+
}
|
|
54
|
+
export function revise(preset, current, input) {
|
|
55
|
+
const values = { ...current.fields, title: current.title };
|
|
56
|
+
const problems = [];
|
|
57
|
+
const changes = {};
|
|
58
|
+
for (const [name, given] of Object.entries(input)) {
|
|
59
|
+
const spec = preset.fields[name];
|
|
60
|
+
if (!spec) {
|
|
61
|
+
problems.push(`unknown field "${name}"`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const next = isBlank(given) ? null : given;
|
|
65
|
+
if (next === null && spec.required === 'create') {
|
|
66
|
+
problems.push(`field "${said(name, spec)}" is required and cannot be cleared`);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const problem = next === null ? undefined : valueProblem(name, spec, next);
|
|
70
|
+
if (problem)
|
|
71
|
+
problems.push(problem);
|
|
72
|
+
const previous = stored(values[name]);
|
|
73
|
+
if (previous === next)
|
|
74
|
+
continue;
|
|
75
|
+
changes[name] = { from: previous, to: next };
|
|
76
|
+
if (next === null)
|
|
77
|
+
delete values[name];
|
|
78
|
+
else
|
|
79
|
+
values[name] = next;
|
|
80
|
+
}
|
|
81
|
+
if (problems.length > 0)
|
|
82
|
+
throw new OteaError('invalid_fields', problems.join('; '));
|
|
83
|
+
const { title, ...fields } = values;
|
|
84
|
+
return { title: String(title), fields, changes };
|
|
85
|
+
}
|
|
86
|
+
export function missingToComplete(preset, fields) {
|
|
87
|
+
return Object.entries(preset.fields)
|
|
88
|
+
.filter(([name, spec]) => {
|
|
89
|
+
const value = fields[name];
|
|
90
|
+
return spec.required === 'complete' && (typeof value !== 'string' || value.trim() === '');
|
|
91
|
+
})
|
|
92
|
+
.map(([name]) => name);
|
|
93
|
+
}
|
|
94
|
+
export function assertCanLeave(preset, from, to, fields) {
|
|
95
|
+
const { workflow } = preset;
|
|
96
|
+
if (from !== workflow.incomplete || turnOf(workflow, to) === 'none')
|
|
97
|
+
return;
|
|
98
|
+
const missing = missingToComplete(preset, fields);
|
|
99
|
+
if (missing.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
throw new OteaError('missing_fields', `Cannot move from "${stateLabel(workflow, from)}" to "${stateLabel(workflow, to)}" while fields required to complete are missing: ${missing.map((key) => preset.fields[key]?.label ?? key).join(', ')}`, {
|
|
102
|
+
kind: 'fields_missing',
|
|
103
|
+
fields: missing.map((key) => {
|
|
104
|
+
const said = Object.entries(preset.labels ?? {}).flatMap(([language, words]) => {
|
|
105
|
+
const label = words.fields[key];
|
|
106
|
+
return label === undefined ? [] : [[language, label]];
|
|
107
|
+
});
|
|
108
|
+
return said.length === 0 ? { key } : { key, labels: Object.fromEntries(said) };
|
|
109
|
+
}),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// A field key from a name someone typed ("Found in build" gives found_in_build), unique among the
|
|
113
|
+
// keys already taken: a clash gets _2, _3, and so on.
|
|
114
|
+
export function fieldKeyFor(name, taken) {
|
|
115
|
+
const used = new Set(taken);
|
|
116
|
+
const base = name
|
|
117
|
+
.normalize('NFD')
|
|
118
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
119
|
+
.toLowerCase()
|
|
120
|
+
.replace(/[^a-z0-9]+/g, '_')
|
|
121
|
+
.replace(/^_+|_+$/g, '')
|
|
122
|
+
.replace(/^[0-9]+/, '') || 'field';
|
|
123
|
+
if (!used.has(base))
|
|
124
|
+
return base;
|
|
125
|
+
let n = 2;
|
|
126
|
+
while (used.has(`${base}_${n}`))
|
|
127
|
+
n++;
|
|
128
|
+
return `${base}_${n}`;
|
|
129
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { OteaError } from './errors.js';
|
|
2
|
+
function article(word) {
|
|
3
|
+
return /^[aeiou]/.test(word) ? `an ${word}` : `a ${word}`;
|
|
4
|
+
}
|
|
5
|
+
function listed(words, last) {
|
|
6
|
+
if (words.length < 2)
|
|
7
|
+
return words.join('');
|
|
8
|
+
return `${words.slice(0, -1).join(', ')} ${last} ${words.at(-1)}`;
|
|
9
|
+
}
|
|
10
|
+
// A parent of a type that lists its children takes only those; a type without the list takes any.
|
|
11
|
+
// `parent` is the parent item's id, to name it.
|
|
12
|
+
export function assertTakesChild(parent, type, child) {
|
|
13
|
+
const { children } = type;
|
|
14
|
+
if (children === undefined || children.includes(child))
|
|
15
|
+
return;
|
|
16
|
+
const takes = children.length === 0
|
|
17
|
+
? 'takes no children'
|
|
18
|
+
: `takes ${listed(children, 'and')} as children, not ${child}`;
|
|
19
|
+
throw new OteaError('parent_type', `${parent} is ${article(type.name)}, which ${takes}`);
|
|
20
|
+
}
|
|
21
|
+
// A type that may not stand alone is created under a parent, of a type that takes it.
|
|
22
|
+
export function assertStandsAlone(preset, type) {
|
|
23
|
+
if (type.root !== false)
|
|
24
|
+
return;
|
|
25
|
+
const parents = preset.types
|
|
26
|
+
.filter(({ children }) => children?.includes(type.name))
|
|
27
|
+
.map(({ name }) => article(name));
|
|
28
|
+
const named = article(type.name);
|
|
29
|
+
throw new OteaError('parent_required', `${named[0]?.toUpperCase()}${named.slice(1)} needs a parent (${listed(parents, 'or')}); create it with --parent <id>`);
|
|
30
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { OteaError } from './errors.js';
|
|
2
|
+
// The names of the type icons a preset may give its types (`icon: bug`). The drawings live in the
|
|
3
|
+
// UI (`ui/src/icons/`), keyed by these names, so the core checks a preset without carrying them.
|
|
4
|
+
export const ICON_NAMES = [
|
|
5
|
+
// work
|
|
6
|
+
'layers',
|
|
7
|
+
'checkbox',
|
|
8
|
+
'list',
|
|
9
|
+
'stack',
|
|
10
|
+
'inbox',
|
|
11
|
+
'flag',
|
|
12
|
+
'milestone',
|
|
13
|
+
'target',
|
|
14
|
+
'rocket',
|
|
15
|
+
'clipboard',
|
|
16
|
+
'calendar',
|
|
17
|
+
'clock',
|
|
18
|
+
'hourglass',
|
|
19
|
+
'checklist',
|
|
20
|
+
'briefcase',
|
|
21
|
+
'chart',
|
|
22
|
+
// code
|
|
23
|
+
'bug',
|
|
24
|
+
'git-branch',
|
|
25
|
+
'git-merge',
|
|
26
|
+
'terminal',
|
|
27
|
+
'code',
|
|
28
|
+
'brackets',
|
|
29
|
+
'database',
|
|
30
|
+
'server',
|
|
31
|
+
'plug',
|
|
32
|
+
'cpu',
|
|
33
|
+
'package',
|
|
34
|
+
'gear',
|
|
35
|
+
'wrench',
|
|
36
|
+
'shield',
|
|
37
|
+
'lock',
|
|
38
|
+
'key',
|
|
39
|
+
'cloud',
|
|
40
|
+
'browser',
|
|
41
|
+
'mobile',
|
|
42
|
+
'test-tube',
|
|
43
|
+
'gauge',
|
|
44
|
+
// design and media
|
|
45
|
+
'image',
|
|
46
|
+
'film',
|
|
47
|
+
'camera',
|
|
48
|
+
'play',
|
|
49
|
+
'music',
|
|
50
|
+
'palette',
|
|
51
|
+
'brush',
|
|
52
|
+
'pen',
|
|
53
|
+
'ruler',
|
|
54
|
+
'shapes',
|
|
55
|
+
'eye',
|
|
56
|
+
'frame',
|
|
57
|
+
'sparkle',
|
|
58
|
+
'pen-nib',
|
|
59
|
+
'layout',
|
|
60
|
+
'type',
|
|
61
|
+
'cube',
|
|
62
|
+
// docs and talk
|
|
63
|
+
'book',
|
|
64
|
+
'doc',
|
|
65
|
+
'note',
|
|
66
|
+
'chat-bubble',
|
|
67
|
+
'question',
|
|
68
|
+
'lightbulb',
|
|
69
|
+
'megaphone',
|
|
70
|
+
'bookmark',
|
|
71
|
+
'quote',
|
|
72
|
+
'link',
|
|
73
|
+
'mail',
|
|
74
|
+
'diagram',
|
|
75
|
+
'compare',
|
|
76
|
+
// game and creatures
|
|
77
|
+
'paw',
|
|
78
|
+
'feather',
|
|
79
|
+
'bone',
|
|
80
|
+
'egg',
|
|
81
|
+
'fish',
|
|
82
|
+
'bird',
|
|
83
|
+
'leaf',
|
|
84
|
+
'sprout',
|
|
85
|
+
'tree',
|
|
86
|
+
'mountain',
|
|
87
|
+
'flame',
|
|
88
|
+
'drop',
|
|
89
|
+
'moon',
|
|
90
|
+
'sun',
|
|
91
|
+
'crystal',
|
|
92
|
+
'sword',
|
|
93
|
+
'shieldcrest',
|
|
94
|
+
'potion',
|
|
95
|
+
'map',
|
|
96
|
+
'compass',
|
|
97
|
+
'dice',
|
|
98
|
+
'controller',
|
|
99
|
+
'heart',
|
|
100
|
+
'star',
|
|
101
|
+
'skull',
|
|
102
|
+
'crown',
|
|
103
|
+
'ghost',
|
|
104
|
+
// status and flow
|
|
105
|
+
'puzzle',
|
|
106
|
+
'beaker',
|
|
107
|
+
'magnet',
|
|
108
|
+
'bolt',
|
|
109
|
+
'anchor',
|
|
110
|
+
'hook',
|
|
111
|
+
'trophy',
|
|
112
|
+
'gift',
|
|
113
|
+
'tag',
|
|
114
|
+
'pin',
|
|
115
|
+
'globe',
|
|
116
|
+
'home',
|
|
117
|
+
'user',
|
|
118
|
+
'users',
|
|
119
|
+
'robot',
|
|
120
|
+
'cart',
|
|
121
|
+
'coins',
|
|
122
|
+
'building',
|
|
123
|
+
];
|
|
124
|
+
export function isIconName(name) {
|
|
125
|
+
return ICON_NAMES.includes(name);
|
|
126
|
+
}
|
|
127
|
+
// Edits between two names: insertions, deletions, substitutions and swaps of two neighbours.
|
|
128
|
+
function distance(a, b) {
|
|
129
|
+
const rows = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
130
|
+
const at = (i, j) => rows[i]?.[j] ?? 0;
|
|
131
|
+
for (let i = 1; i <= a.length; i++) {
|
|
132
|
+
for (let j = 1; j <= b.length; j++) {
|
|
133
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
134
|
+
let best = Math.min(at(i - 1, j) + 1, at(i, j - 1) + 1, at(i - 1, j - 1) + cost);
|
|
135
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
136
|
+
best = Math.min(best, at(i - 2, j - 2) + 1);
|
|
137
|
+
}
|
|
138
|
+
const row = rows[i];
|
|
139
|
+
if (row)
|
|
140
|
+
row[j] = best;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return at(a.length, b.length);
|
|
144
|
+
}
|
|
145
|
+
// The names closest to a mistyped one: those containing it (or contained in it) first, then those
|
|
146
|
+
// a few edits away, nearest first.
|
|
147
|
+
export function closeNames(name, names, limit = 3) {
|
|
148
|
+
const needle = name.trim().toLowerCase();
|
|
149
|
+
const allowed = needle.length <= 4 ? 1 : 2;
|
|
150
|
+
const contains = (candidate) => needle.length >= 3 && (candidate.includes(needle) || needle.includes(candidate));
|
|
151
|
+
return names
|
|
152
|
+
.map((candidate) => ({
|
|
153
|
+
candidate,
|
|
154
|
+
edits: distance(needle, candidate),
|
|
155
|
+
part: contains(candidate),
|
|
156
|
+
}))
|
|
157
|
+
.filter(({ edits, part }) => part || edits <= allowed)
|
|
158
|
+
.sort((a, b) => Number(b.part) - Number(a.part) || a.edits - b.edits)
|
|
159
|
+
.slice(0, limit)
|
|
160
|
+
.map(({ candidate }) => candidate);
|
|
161
|
+
}
|
|
162
|
+
export function closeIconNames(name, limit = 3) {
|
|
163
|
+
return closeNames(name, ICON_NAMES, limit);
|
|
164
|
+
}
|
|
165
|
+
function unknownIcon(name) {
|
|
166
|
+
const close = closeIconNames(name);
|
|
167
|
+
const hint = close.length > 0
|
|
168
|
+
? `did you mean ${close.join(', ')}?`
|
|
169
|
+
: `none of the ${ICON_NAMES.length} icons is close; see the icon list in the preset format`;
|
|
170
|
+
return `unknown icon "${name}" (${hint})`;
|
|
171
|
+
}
|
|
172
|
+
export function iconProblem(at, name) {
|
|
173
|
+
return isIconName(name) ? undefined : `${at}: ${unknownIcon(name)}`;
|
|
174
|
+
}
|
|
175
|
+
// The icon a collection or project is given by name, refused with the close names when unknown.
|
|
176
|
+
export function assertIconName(name) {
|
|
177
|
+
if (isIconName(name))
|
|
178
|
+
return name;
|
|
179
|
+
const message = unknownIcon(name);
|
|
180
|
+
throw new OteaError('invalid_icon', `${message[0]?.toUpperCase()}${message.slice(1)}`);
|
|
181
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { deflateSync, inflateSync } from 'node:zlib';
|
|
2
|
+
const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
3
|
+
// The size of a PNG, GIF, JPEG or WebP, read from its header; null for anything else.
|
|
4
|
+
export function imageSize(bytes) {
|
|
5
|
+
if (bytes.length >= 24 && bytes.subarray(0, 8).equals(PNG_SIGNATURE))
|
|
6
|
+
return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20), format: 'png' };
|
|
7
|
+
if (bytes.length >= 10 && bytes.toString('latin1', 0, 4) === 'GIF8')
|
|
8
|
+
return { width: bytes.readUInt16LE(6), height: bytes.readUInt16LE(8), format: 'gif' };
|
|
9
|
+
if (bytes.length >= 30 && bytes.toString('latin1', 0, 4) === 'RIFF') {
|
|
10
|
+
const kind = bytes.toString('latin1', 12, 16);
|
|
11
|
+
if (kind === 'VP8X')
|
|
12
|
+
return {
|
|
13
|
+
width: 1 + bytes.readUIntLE(24, 3),
|
|
14
|
+
height: 1 + bytes.readUIntLE(27, 3),
|
|
15
|
+
format: 'webp',
|
|
16
|
+
};
|
|
17
|
+
if (kind === 'VP8L') {
|
|
18
|
+
const bits = bytes.readUInt32LE(21);
|
|
19
|
+
return { width: 1 + (bits & 0x3fff), height: 1 + ((bits >> 14) & 0x3fff), format: 'webp' };
|
|
20
|
+
}
|
|
21
|
+
if (kind === 'VP8 ')
|
|
22
|
+
return {
|
|
23
|
+
width: bytes.readUInt16LE(26) & 0x3fff,
|
|
24
|
+
height: bytes.readUInt16LE(28) & 0x3fff,
|
|
25
|
+
format: 'webp',
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
if (bytes.length >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8) {
|
|
29
|
+
let at = 2;
|
|
30
|
+
while (at + 8 < bytes.length) {
|
|
31
|
+
if (bytes[at] !== 0xff)
|
|
32
|
+
return null;
|
|
33
|
+
const marker = bytes[at + 1] ?? 0;
|
|
34
|
+
const frame = marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker);
|
|
35
|
+
if (frame)
|
|
36
|
+
return {
|
|
37
|
+
height: bytes.readUInt16BE(at + 5),
|
|
38
|
+
width: bytes.readUInt16BE(at + 7),
|
|
39
|
+
format: 'jpeg',
|
|
40
|
+
};
|
|
41
|
+
at += 2 + bytes.readUInt16BE(at + 2);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
// The zone a note's spot asks to be cut: a zone as it is; a point, a fifth of the image around it,
|
|
47
|
+
// kept inside the image.
|
|
48
|
+
export function zoneOf(spot) {
|
|
49
|
+
if (spot.w !== undefined && spot.h !== undefined)
|
|
50
|
+
return { x: spot.x, y: spot.y, w: spot.w, h: spot.h };
|
|
51
|
+
const side = 20;
|
|
52
|
+
const start = (at) => Math.min(100 - side, Math.max(0, at - side / 2));
|
|
53
|
+
return { x: start(spot.x), y: start(spot.y), w: side, h: side };
|
|
54
|
+
}
|
|
55
|
+
// A zone in whole pixels of an image of `size`, at least one pixel, inside the image.
|
|
56
|
+
export function pixelRect(zone, size) {
|
|
57
|
+
const left = Math.min(size.width - 1, Math.max(0, Math.floor((zone.x / 100) * size.width)));
|
|
58
|
+
const top = Math.min(size.height - 1, Math.max(0, Math.floor((zone.y / 100) * size.height)));
|
|
59
|
+
const right = Math.min(size.width, Math.ceil(((zone.x + zone.w) / 100) * size.width));
|
|
60
|
+
const bottom = Math.min(size.height, Math.ceil(((zone.y + zone.h) / 100) * size.height));
|
|
61
|
+
return { left, top, width: Math.max(1, right - left), height: Math.max(1, bottom - top) };
|
|
62
|
+
}
|
|
63
|
+
const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
|
|
64
|
+
let c = n;
|
|
65
|
+
for (let k = 0; k < 8; k++)
|
|
66
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
67
|
+
return c >>> 0;
|
|
68
|
+
});
|
|
69
|
+
function crc32(bytes) {
|
|
70
|
+
let c = 0xffffffff;
|
|
71
|
+
for (const byte of bytes)
|
|
72
|
+
c = (CRC_TABLE[(c ^ byte) & 0xff] ?? 0) ^ (c >>> 8);
|
|
73
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
74
|
+
}
|
|
75
|
+
function chunk(type, data) {
|
|
76
|
+
const length = Buffer.alloc(4);
|
|
77
|
+
length.writeUInt32BE(data.length);
|
|
78
|
+
const body = Buffer.concat([Buffer.from(type, 'latin1'), data]);
|
|
79
|
+
const sum = Buffer.alloc(4);
|
|
80
|
+
sum.writeUInt32BE(crc32(body));
|
|
81
|
+
return Buffer.concat([length, body, sum]);
|
|
82
|
+
}
|
|
83
|
+
const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
|
|
84
|
+
function paeth(a, b, c) {
|
|
85
|
+
const p = a + b - c;
|
|
86
|
+
const pa = Math.abs(p - a);
|
|
87
|
+
const pb = Math.abs(p - b);
|
|
88
|
+
const pc = Math.abs(p - c);
|
|
89
|
+
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
|
90
|
+
}
|
|
91
|
+
// The pixels of `rect` of a PNG as a PNG of their own; null for a PNG this cannot read (interlaced,
|
|
92
|
+
// or fewer than 8 bits a sample) or for anything that is not a PNG.
|
|
93
|
+
export function cropPng(bytes, rect) {
|
|
94
|
+
if (bytes.length < 33 || !bytes.subarray(0, 8).equals(PNG_SIGNATURE))
|
|
95
|
+
return null;
|
|
96
|
+
let header;
|
|
97
|
+
const data = [];
|
|
98
|
+
const kept = [];
|
|
99
|
+
for (let at = 8; at + 8 <= bytes.length;) {
|
|
100
|
+
const length = bytes.readUInt32BE(at);
|
|
101
|
+
const type = bytes.toString('latin1', at + 4, at + 8);
|
|
102
|
+
const body = bytes.subarray(at + 8, at + 8 + length);
|
|
103
|
+
if (type === 'IHDR')
|
|
104
|
+
header = body;
|
|
105
|
+
else if (type === 'IDAT')
|
|
106
|
+
data.push(body);
|
|
107
|
+
else if (type === 'PLTE' || type === 'tRNS')
|
|
108
|
+
kept.push(chunk(type, body));
|
|
109
|
+
else if (type === 'IEND')
|
|
110
|
+
break;
|
|
111
|
+
at += 12 + length;
|
|
112
|
+
}
|
|
113
|
+
if (!header)
|
|
114
|
+
return null;
|
|
115
|
+
const width = header.readUInt32BE(0);
|
|
116
|
+
const height = header.readUInt32BE(4);
|
|
117
|
+
const depth = header[8] ?? 0;
|
|
118
|
+
const color = header[9] ?? 0;
|
|
119
|
+
const channels = CHANNELS[color];
|
|
120
|
+
if (header[12] !== 0 || depth < 8 || channels === undefined)
|
|
121
|
+
return null;
|
|
122
|
+
const bpp = (channels * depth) / 8;
|
|
123
|
+
const stride = width * bpp;
|
|
124
|
+
const raw = inflateSync(Buffer.concat(data));
|
|
125
|
+
const rows = [];
|
|
126
|
+
let prev = Buffer.alloc(stride);
|
|
127
|
+
for (let y = 0; y < height; y++) {
|
|
128
|
+
const start = y * (stride + 1);
|
|
129
|
+
const filter = raw[start] ?? 0;
|
|
130
|
+
const row = Buffer.from(raw.subarray(start + 1, start + 1 + stride));
|
|
131
|
+
for (let i = 0; i < stride; i++) {
|
|
132
|
+
const a = i >= bpp ? (row[i - bpp] ?? 0) : 0;
|
|
133
|
+
const b = prev[i] ?? 0;
|
|
134
|
+
const c = i >= bpp ? (prev[i - bpp] ?? 0) : 0;
|
|
135
|
+
const add = filter === 1
|
|
136
|
+
? a
|
|
137
|
+
: filter === 2
|
|
138
|
+
? b
|
|
139
|
+
: filter === 3
|
|
140
|
+
? (a + b) >> 1
|
|
141
|
+
: filter === 4
|
|
142
|
+
? paeth(a, b, c)
|
|
143
|
+
: 0;
|
|
144
|
+
row[i] = ((row[i] ?? 0) + add) & 0xff;
|
|
145
|
+
}
|
|
146
|
+
rows.push(row);
|
|
147
|
+
prev = row;
|
|
148
|
+
}
|
|
149
|
+
const out = [];
|
|
150
|
+
for (let y = rect.top; y < rect.top + rect.height; y++) {
|
|
151
|
+
const row = rows[y];
|
|
152
|
+
if (!row)
|
|
153
|
+
break;
|
|
154
|
+
out.push(Buffer.from([0]), row.subarray(rect.left * bpp, (rect.left + rect.width) * bpp));
|
|
155
|
+
}
|
|
156
|
+
const head = Buffer.from(header);
|
|
157
|
+
head.writeUInt32BE(rect.width, 0);
|
|
158
|
+
head.writeUInt32BE(out.length / 2, 4);
|
|
159
|
+
return Buffer.concat([
|
|
160
|
+
PNG_SIGNATURE,
|
|
161
|
+
chunk('IHDR', head),
|
|
162
|
+
...kept,
|
|
163
|
+
chunk('IDAT', deflateSync(Buffer.concat(out))),
|
|
164
|
+
chunk('IEND', Buffer.alloc(0)),
|
|
165
|
+
]);
|
|
166
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { OteaError } from './errors.js';
|
|
2
|
+
// An agent's name is its identity, one per agent working at the same time: a slug of at most 40
|
|
3
|
+
// characters (a subagent's <name>-<role> runs long), lowercase letters and digits joined by single
|
|
4
|
+
// dashes, starting with a letter. The web UI says "you" for the human, so neither word names an
|
|
5
|
+
// agent, and "agent" names no one in particular.
|
|
6
|
+
const AGENT_NAME = /^[a-z](?:[a-z0-9]|-(?=[a-z0-9])){0,39}$/;
|
|
7
|
+
const RESERVED_AGENT_NAMES = ['you', 'human', 'agent'];
|
|
8
|
+
export function isAgentName(name) {
|
|
9
|
+
return AGENT_NAME.test(name) && !RESERVED_AGENT_NAMES.includes(name);
|
|
10
|
+
}
|
|
11
|
+
export function assertAgentName(name) {
|
|
12
|
+
if (isAgentName(name))
|
|
13
|
+
return;
|
|
14
|
+
throw new OteaError('invalid_agent_name', `"${name}" cannot name an agent: use at most 40 lowercase letters, digits and single dashes, starting with a letter (not "you", "human" or "agent"), such as claude-b`, { kind: 'agent_name', name });
|
|
15
|
+
}
|
|
16
|
+
// The agent acting under the name it gave, checked by the one rule every surface uses. `how` says
|
|
17
|
+
// how to give a name on that surface, for an agent that gave none: there is no default name.
|
|
18
|
+
export function agentActor(name, how) {
|
|
19
|
+
if (name === undefined || name === '') {
|
|
20
|
+
throw new OteaError('unnamed_agent', `Name yourself first: ${how}. otea whoami (MCP: whoami) gives back the name you worked under here before; else pick a short name no other agent working now uses, such as claude-b; subagents each use their own.`);
|
|
21
|
+
}
|
|
22
|
+
assertAgentName(name);
|
|
23
|
+
return { kind: 'agent', name };
|
|
24
|
+
}
|
|
25
|
+
// The person reading at a terminal: reads need no agent name, and are never recorded as an agent.
|
|
26
|
+
export const PERSON_READER = { kind: 'human', name: 'human' };
|
|
27
|
+
// Who reads: the agent the name gives (checked), or the person when no name was given.
|
|
28
|
+
export function readerActor(name) {
|
|
29
|
+
if (name === undefined || name === '')
|
|
30
|
+
return PERSON_READER;
|
|
31
|
+
assertAgentName(name);
|
|
32
|
+
return { kind: 'agent', name };
|
|
33
|
+
}
|
|
34
|
+
const ID_PATTERN = /^([A-Z0-9]+)-([1-9][0-9]*)$/;
|
|
35
|
+
export function prefixFor(collectionName) {
|
|
36
|
+
const prefix = collectionName.toUpperCase().replace(/[^A-Z0-9]/g, '');
|
|
37
|
+
if (!prefix)
|
|
38
|
+
throw new OteaError('invalid_name', `"${collectionName}" has no usable characters`);
|
|
39
|
+
return prefix;
|
|
40
|
+
}
|
|
41
|
+
export function formatItemId(prefix, seq) {
|
|
42
|
+
return `${prefix}-${seq}`;
|
|
43
|
+
}
|
|
44
|
+
export function parseItemId(id) {
|
|
45
|
+
const match = ID_PATTERN.exec(id);
|
|
46
|
+
if (!match?.[1] || !match[2])
|
|
47
|
+
throw new OteaError('invalid_id', `"${id}" is not an item id`);
|
|
48
|
+
return { prefix: match[1], seq: Number(match[2]) };
|
|
49
|
+
}
|