devcontour-harness-ui 0.0.0-stage → 0.1.0-alpha.2
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 +21 -0
- package/README.md +143 -2
- package/dist/app/assets/index-Det9VYJC.css +1 -0
- package/dist/app/assets/index-veveke8K.js +9 -0
- package/dist/app/index.html +13 -0
- package/dist/demo/ru/view.sample.json +483 -0
- package/dist/demo/ru/work.accepted.sample.json +187 -0
- package/dist/demo/ru/work.sample.json +171 -0
- package/dist/demo/view.sample.json +483 -0
- package/dist/demo/work.accepted.sample.json +187 -0
- package/dist/demo/work.sample.json +171 -0
- package/dist/lib/cli.d.ts +2 -0
- package/dist/lib/cli.js +160 -0
- package/dist/lib/components/Activity.d.ts +13 -0
- package/dist/lib/components/Activity.js +19 -0
- package/dist/lib/components/Decisions.d.ts +6 -0
- package/dist/lib/components/Decisions.js +19 -0
- package/dist/lib/components/Health.d.ts +8 -0
- package/dist/lib/components/Health.js +24 -0
- package/dist/lib/components/History.d.ts +32 -0
- package/dist/lib/components/History.js +129 -0
- package/dist/lib/components/Overview.d.ts +19 -0
- package/dist/lib/components/Overview.js +54 -0
- package/dist/lib/components/Reviews.d.ts +23 -0
- package/dist/lib/components/Reviews.js +22 -0
- package/dist/lib/components/Settings.d.ts +21 -0
- package/dist/lib/components/Settings.js +77 -0
- package/dist/lib/components/WorkDetail.d.ts +18 -0
- package/dist/lib/components/WorkDetail.js +96 -0
- package/dist/lib/components/common.d.ts +71 -0
- package/dist/lib/components/common.js +85 -0
- package/dist/lib/index.d.ts +10 -0
- package/dist/lib/index.js +12 -0
- package/dist/lib/model/compat.d.ts +36 -0
- package/dist/lib/model/compat.js +365 -0
- package/dist/lib/model/dict.d.ts +387 -0
- package/dist/lib/model/dict.js +1119 -0
- package/dist/lib/model/format.d.ts +34 -0
- package/dist/lib/model/format.js +88 -0
- package/dist/lib/model/i18n.d.ts +44 -0
- package/dist/lib/model/i18n.js +109 -0
- package/dist/lib/model/index.d.ts +6 -0
- package/dist/lib/model/index.js +8 -0
- package/dist/lib/model/source.d.ts +16 -0
- package/dist/lib/model/source.js +57 -0
- package/dist/lib/model/view.d.ts +474 -0
- package/dist/lib/model/view.js +10 -0
- package/dist/lib/server/harness.d.ts +44 -0
- package/dist/lib/server/harness.js +172 -0
- package/dist/lib/server/serve.d.ts +62 -0
- package/dist/lib/server/serve.js +302 -0
- package/dist/lib/styles.css +485 -0
- package/docs/design.md +1689 -0
- package/docs/harness-proposals.md +560 -0
- package/docs/release-notes/0.1.0-alpha.1.md +96 -0
- package/docs/release-notes/0.1.0-alpha.2.md +218 -0
- package/package.json +75 -4
- package/skills/harness-ui/SKILL.md +246 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Lang } from './dict.ts';
|
|
2
|
+
import type { Bounded, OperatorQuestion } from './view.ts';
|
|
3
|
+
/** `$1.50`, `≥ $3.40` for a lower bound, "unknown" for anything else — never `$0.00`. */
|
|
4
|
+
export declare function formatUsd(amount: Bounded | undefined, lang?: Lang): string;
|
|
5
|
+
/** A plain dollar amount the producer gave as a number (limits, budgets). */
|
|
6
|
+
export declare const usd: (n: number) => string;
|
|
7
|
+
/** A duration as `1h 05m`, `4m 12s`, `9s` (en) or `1 ч 05 мин`, `4 мин 12 с` (ru). */
|
|
8
|
+
export declare function formatDuration(ms: number, lang?: Lang): string;
|
|
9
|
+
/** A window length: `24 ч`, `7 дней` (ru) or `24 h`, `7 days` (en); other lengths as a duration. */
|
|
10
|
+
export declare function formatWindow(ms: number, lang?: Lang): string;
|
|
11
|
+
/** Elapsed time between an ISO instant and `nowMs`. */
|
|
12
|
+
export declare function formatElapsed(fromIso: string, nowMs: number, lang?: Lang): string;
|
|
13
|
+
/** Label for a stage; an unknown stage from a newer harness is shown as is. */
|
|
14
|
+
export declare const stageLabel: (stage: string, lang?: Lang) => string;
|
|
15
|
+
/** Stages that hold a worker slot. */
|
|
16
|
+
export declare const SLOT_STAGES: ReadonlySet<string>;
|
|
17
|
+
export interface QuestionStatus {
|
|
18
|
+
/** Whether the question waits for a new answer from a person. */
|
|
19
|
+
needsAnswer: boolean;
|
|
20
|
+
text: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* What a question's lifecycle state means for the operator. Only
|
|
24
|
+
* `awaiting-answer` asks for an answer; the other states name who acts.
|
|
25
|
+
*/
|
|
26
|
+
/** Question states this build knows; any other state makes the header's count unknown. */
|
|
27
|
+
export declare const KNOWN_QUESTION_STATES: ReadonlySet<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Whether the lead may answer (design §3.4): `answerableBy: operator-or-lead`
|
|
30
|
+
* and `impact: technical`. An absent, unknown or `user-visible` impact reads
|
|
31
|
+
* as user-visible, which only the operator answers, whatever the severity.
|
|
32
|
+
*/
|
|
33
|
+
export declare function leadMayAnswer(q: Pick<OperatorQuestion, 'answerableBy' | 'impact'>): boolean;
|
|
34
|
+
export declare function questionStatus(q: Pick<OperatorQuestion, 'state' | 'answerableBy' | 'impact'>, lang?: Lang): QuestionStatus;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Small display helpers shared by components. Pure; words come from the
|
|
2
|
+
// dictionary (dict.ts). These helpers default to English for API stability;
|
|
3
|
+
// the components default to Russian. Unknown values from a newer producer get
|
|
4
|
+
// the conservative reading: an unknown bound is "unknown", an unknown answer
|
|
5
|
+
// rule is "only you".
|
|
6
|
+
import { instantMs } from "./compat.js";
|
|
7
|
+
import { dict, label } from "./dict.js";
|
|
8
|
+
/** `$1.50`, `≥ $3.40` for a lower bound, "unknown" for anything else — never `$0.00`. */
|
|
9
|
+
export function formatUsd(amount, lang = 'en') {
|
|
10
|
+
if (!amount ||
|
|
11
|
+
(amount.bound !== 'exact' && amount.bound !== 'lower') ||
|
|
12
|
+
!Number.isFinite(amount.value))
|
|
13
|
+
return dict(lang).usdUnknown;
|
|
14
|
+
const text = '$' + amount.value.toFixed(2);
|
|
15
|
+
return amount.bound === 'lower' ? '≥ ' + text : text;
|
|
16
|
+
}
|
|
17
|
+
/** A plain dollar amount the producer gave as a number (limits, budgets). */
|
|
18
|
+
export const usd = (n) => '$' + n.toFixed(2);
|
|
19
|
+
/** A duration as `1h 05m`, `4m 12s`, `9s` (en) or `1 ч 05 мин`, `4 мин 12 с` (ru). */
|
|
20
|
+
export function formatDuration(ms, lang = 'en') {
|
|
21
|
+
const s = Math.max(0, Math.floor(ms / 1000));
|
|
22
|
+
if (!Number.isFinite(s))
|
|
23
|
+
return dict(lang).unknown;
|
|
24
|
+
return dict(lang).duration(Math.floor(s / 3600), Math.floor((s % 3600) / 60), s % 60);
|
|
25
|
+
}
|
|
26
|
+
/** A window length: `24 ч`, `7 дней` (ru) or `24 h`, `7 days` (en); other lengths as a duration. */
|
|
27
|
+
export function formatWindow(ms, lang = 'en') {
|
|
28
|
+
const t = dict(lang);
|
|
29
|
+
if (Number.isFinite(ms) && ms > 0 && ms % 3_600_000 === 0 && ms <= 48 * 3_600_000)
|
|
30
|
+
return t.hours(ms / 3_600_000);
|
|
31
|
+
if (Number.isFinite(ms) && ms > 0 && ms % 86_400_000 === 0)
|
|
32
|
+
return t.days(ms / 86_400_000);
|
|
33
|
+
return formatDuration(ms, lang);
|
|
34
|
+
}
|
|
35
|
+
/** Elapsed time between an ISO instant and `nowMs`. */
|
|
36
|
+
export function formatElapsed(fromIso, nowMs, lang = 'en') {
|
|
37
|
+
const ms = nowMs - instantMs(fromIso);
|
|
38
|
+
if (!Number.isFinite(ms))
|
|
39
|
+
return dict(lang).unknown;
|
|
40
|
+
return formatDuration(ms, lang);
|
|
41
|
+
}
|
|
42
|
+
/** Label for a stage; an unknown stage from a newer harness is shown as is. */
|
|
43
|
+
export const stageLabel = (stage, lang = 'en') => label(dict(lang).stage, stage);
|
|
44
|
+
/** Stages that hold a worker slot. */
|
|
45
|
+
export const SLOT_STAGES = new Set([
|
|
46
|
+
'prepare',
|
|
47
|
+
'writer',
|
|
48
|
+
'gates',
|
|
49
|
+
'review',
|
|
50
|
+
'integration',
|
|
51
|
+
]);
|
|
52
|
+
/**
|
|
53
|
+
* What a question's lifecycle state means for the operator. Only
|
|
54
|
+
* `awaiting-answer` asks for an answer; the other states name who acts.
|
|
55
|
+
*/
|
|
56
|
+
/** Question states this build knows; any other state makes the header's count unknown. */
|
|
57
|
+
export const KNOWN_QUESTION_STATES = new Set([
|
|
58
|
+
'awaiting-answer',
|
|
59
|
+
'answered-not-applied',
|
|
60
|
+
'applied-awaiting-claim',
|
|
61
|
+
'delivered',
|
|
62
|
+
'carry-stale',
|
|
63
|
+
]);
|
|
64
|
+
/**
|
|
65
|
+
* Whether the lead may answer (design §3.4): `answerableBy: operator-or-lead`
|
|
66
|
+
* and `impact: technical`. An absent, unknown or `user-visible` impact reads
|
|
67
|
+
* as user-visible, which only the operator answers, whatever the severity.
|
|
68
|
+
*/
|
|
69
|
+
export function leadMayAnswer(q) {
|
|
70
|
+
return q.answerableBy === 'operator-or-lead' && q.impact === 'technical';
|
|
71
|
+
}
|
|
72
|
+
export function questionStatus(q, lang = 'en') {
|
|
73
|
+
const t = dict(lang).question;
|
|
74
|
+
switch (q.state) {
|
|
75
|
+
case 'awaiting-answer':
|
|
76
|
+
return { needsAnswer: true, text: leadMayAnswer(q) ? t.youOrLead : t.onlyYou };
|
|
77
|
+
case 'answered-not-applied':
|
|
78
|
+
return { needsAnswer: false, text: t.answeredNotApplied };
|
|
79
|
+
case 'carry-stale':
|
|
80
|
+
return { needsAnswer: false, text: t.carryStale };
|
|
81
|
+
case 'applied-awaiting-claim':
|
|
82
|
+
return { needsAnswer: false, text: t.appliedAwaitingClaim };
|
|
83
|
+
case 'delivered':
|
|
84
|
+
return { needsAnswer: false, text: t.delivered };
|
|
85
|
+
default:
|
|
86
|
+
return { needsAnswer: false, text: t.unknownState(String(q.state)) };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Lang } from './dict.ts';
|
|
2
|
+
import type { TargetView } from './view.ts';
|
|
3
|
+
export { ruPlural } from './dict.ts';
|
|
4
|
+
export type { Lang } from './dict.ts';
|
|
5
|
+
/** `ru` for a Russian locale tag (`ru`, `ru-RU`, `ru_RU.UTF-8`), else `en`. */
|
|
6
|
+
export declare function langOf(locale: string | undefined | null): Lang;
|
|
7
|
+
/**
|
|
8
|
+
* Panel language from the address parameter and the browser locales: an
|
|
9
|
+
* explicit `ru` or `en` parameter wins; an empty or unknown parameter falls
|
|
10
|
+
* back to the first browser locale, never silently to English.
|
|
11
|
+
*/
|
|
12
|
+
export declare function pickLang(param: string | null | undefined, locales: readonly string[]): Lang;
|
|
13
|
+
export interface HeaderCounts {
|
|
14
|
+
running: number;
|
|
15
|
+
queued: number;
|
|
16
|
+
stopped: number;
|
|
17
|
+
paused: boolean;
|
|
18
|
+
/** Questions in `awaiting-answer`. */
|
|
19
|
+
questions: number;
|
|
20
|
+
/** Escalations whose actor is the operator. */
|
|
21
|
+
decisions: number;
|
|
22
|
+
/** `progress.waitingOperator`: works waiting for the operator, over all works. */
|
|
23
|
+
waitingOperator: number;
|
|
24
|
+
/** The questions or escalations list was clipped (`truncated`), so the counts are a floor. */
|
|
25
|
+
incomplete: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** The Overview header sentence: «Сейчас… От вас: …». */
|
|
28
|
+
export declare function headerSentence(c: HeaderCounts, lang: Lang): string;
|
|
29
|
+
/**
|
|
30
|
+
* `HH:MM` of an ISO instant in the given time zone (the viewer's by default);
|
|
31
|
+
* `DD.MM.YYYY HH:MM` when `nowMs` falls on another day, so that stale data
|
|
32
|
+
* never reads as fresh. `undefined` for an invalid instant or time zone.
|
|
33
|
+
*/
|
|
34
|
+
export declare function formatClock(iso: string, timeZone?: string, nowMs?: number): string | undefined;
|
|
35
|
+
/** Footer: «Данные harness на HH:MM; панель ничего не меняет». */
|
|
36
|
+
export declare function footerSentence(generatedAt: string, lang: Lang, timeZone?: string, nowMs?: number): string;
|
|
37
|
+
/**
|
|
38
|
+
* Where an accepted result is (`works[].target`), in one sentence. The remote
|
|
39
|
+
* state is read by the harness from the local remote-tracking ref without a
|
|
40
|
+
* fetch, so the sentence says "as of the last git update on this machine".
|
|
41
|
+
* `unknown` or any state this build does not know reads as unknown, never as
|
|
42
|
+
* "not pushed" or "pushed"; the harness `reason` follows as is.
|
|
43
|
+
*/
|
|
44
|
+
export declare function targetSentence(target: Pick<TargetView, 'remote'>, lang: Lang): string;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Panel language. Russian is first-class; English is the fallback. Every
|
|
2
|
+
// visible word comes from the dictionary (dict.ts). Pure: no node:*, no
|
|
3
|
+
// React. Sentences are phrasing over the view's own counts; no number is
|
|
4
|
+
// derived here.
|
|
5
|
+
import { instantMs } from "./compat.js";
|
|
6
|
+
import { dict } from "./dict.js";
|
|
7
|
+
export { ruPlural } from "./dict.js";
|
|
8
|
+
/** `ru` for a Russian locale tag (`ru`, `ru-RU`, `ru_RU.UTF-8`), else `en`. */
|
|
9
|
+
export function langOf(locale) {
|
|
10
|
+
return typeof locale === 'string' && /^ru(?:[-_.]|$)/i.test(locale.trim()) ? 'ru' : 'en';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Panel language from the address parameter and the browser locales: an
|
|
14
|
+
* explicit `ru` or `en` parameter wins; an empty or unknown parameter falls
|
|
15
|
+
* back to the first browser locale, never silently to English.
|
|
16
|
+
*/
|
|
17
|
+
export function pickLang(param, locales) {
|
|
18
|
+
const p = typeof param === 'string' ? param.trim().toLowerCase() : '';
|
|
19
|
+
if (p === 'ru' || p === 'en')
|
|
20
|
+
return p;
|
|
21
|
+
return langOf(locales.find((l) => typeof l === 'string' && l.trim() !== ''));
|
|
22
|
+
}
|
|
23
|
+
/** The Overview header sentence: «Сейчас… От вас: …». */
|
|
24
|
+
export function headerSentence(c, lang) {
|
|
25
|
+
const h = dict(lang).header;
|
|
26
|
+
const now = [];
|
|
27
|
+
if (c.paused)
|
|
28
|
+
now.push(h.paused);
|
|
29
|
+
if (c.running)
|
|
30
|
+
now.push(h.running(c.running));
|
|
31
|
+
if (c.queued)
|
|
32
|
+
now.push(h.queued(c.queued));
|
|
33
|
+
if (c.stopped)
|
|
34
|
+
now.push(h.stopped(c.stopped));
|
|
35
|
+
const asks = [];
|
|
36
|
+
if (c.questions)
|
|
37
|
+
asks.push(h.answer(c.questions));
|
|
38
|
+
if (c.decisions)
|
|
39
|
+
asks.push(h.decide(c.decisions));
|
|
40
|
+
let from;
|
|
41
|
+
if (asks.length)
|
|
42
|
+
from = asks.join(h.and) + (c.incomplete ? h.maybeMore : '');
|
|
43
|
+
else if (c.waitingOperator)
|
|
44
|
+
from = h.waiting(c.waitingOperator);
|
|
45
|
+
else if (c.incomplete)
|
|
46
|
+
from = h.unknownClipped;
|
|
47
|
+
else
|
|
48
|
+
from = h.nothing;
|
|
49
|
+
return (now.length ? h.now(now.join(', ')) : h.idle) + h.fromYou(from);
|
|
50
|
+
}
|
|
51
|
+
function parts(ms, timeZone) {
|
|
52
|
+
try {
|
|
53
|
+
const out = {};
|
|
54
|
+
for (const p of new Intl.DateTimeFormat('en-GB', {
|
|
55
|
+
year: 'numeric',
|
|
56
|
+
month: '2-digit',
|
|
57
|
+
day: '2-digit',
|
|
58
|
+
hour: '2-digit',
|
|
59
|
+
minute: '2-digit',
|
|
60
|
+
hourCycle: 'h23',
|
|
61
|
+
...(timeZone ? { timeZone } : {}),
|
|
62
|
+
}).formatToParts(new Date(ms)))
|
|
63
|
+
out[p.type] = p.value;
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// Invalid instant or time zone: the caller shows "unknown", never a guess.
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* `HH:MM` of an ISO instant in the given time zone (the viewer's by default);
|
|
73
|
+
* `DD.MM.YYYY HH:MM` when `nowMs` falls on another day, so that stale data
|
|
74
|
+
* never reads as fresh. `undefined` for an invalid instant or time zone.
|
|
75
|
+
*/
|
|
76
|
+
export function formatClock(iso, timeZone, nowMs) {
|
|
77
|
+
const ms = instantMs(iso);
|
|
78
|
+
if (Number.isNaN(ms))
|
|
79
|
+
return undefined;
|
|
80
|
+
const at = parts(ms, timeZone);
|
|
81
|
+
if (!at)
|
|
82
|
+
return undefined;
|
|
83
|
+
const time = `${at.hour}:${at.minute}`;
|
|
84
|
+
if (nowMs === undefined || !Number.isFinite(nowMs))
|
|
85
|
+
return time;
|
|
86
|
+
const today = parts(nowMs, timeZone);
|
|
87
|
+
const sameDay = today && today.year === at.year && today.month === at.month && today.day === at.day;
|
|
88
|
+
return sameDay ? time : `${at.day}.${at.month}.${at.year} ${time}`;
|
|
89
|
+
}
|
|
90
|
+
/** Footer: «Данные harness на HH:MM; панель ничего не меняет». */
|
|
91
|
+
export function footerSentence(generatedAt, lang, timeZone, nowMs) {
|
|
92
|
+
return dict(lang).footer(formatClock(generatedAt, timeZone, nowMs));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Where an accepted result is (`works[].target`), in one sentence. The remote
|
|
96
|
+
* state is read by the harness from the local remote-tracking ref without a
|
|
97
|
+
* fetch, so the sentence says "as of the last git update on this machine".
|
|
98
|
+
* `unknown` or any state this build does not know reads as unknown, never as
|
|
99
|
+
* "not pushed" or "pushed"; the harness `reason` follows as is.
|
|
100
|
+
*/
|
|
101
|
+
export function targetSentence(target, lang) {
|
|
102
|
+
const { state, reason } = target.remote;
|
|
103
|
+
const t = dict(lang).target;
|
|
104
|
+
if (state === 'pushed')
|
|
105
|
+
return t.pushed;
|
|
106
|
+
if (state === 'not-pushed')
|
|
107
|
+
return t.notPushed;
|
|
108
|
+
return t.unknown(typeof reason === 'string' && reason.trim() ? `: ${reason.trim()}` : '');
|
|
109
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './view.ts';
|
|
2
|
+
export { checkView, checkWorkDetail, KNOWN_VIEW_REVISION, type ViewCheck, type WorkDetailCheck, } from './compat.ts';
|
|
3
|
+
export { httpViewSource, staticViewSource, type HttpViewSourceOptions, type ViewSource, } from './source.ts';
|
|
4
|
+
export { KNOWN_QUESTION_STATES, SLOT_STAGES, formatDuration, formatElapsed, leadMayAnswer, formatUsd, formatWindow, usd, questionStatus, stageLabel, type QuestionStatus, } from './format.ts';
|
|
5
|
+
export { footerSentence, formatClock, headerSentence, langOf, pickLang, ruPlural, targetSentence, type HeaderCounts, type Lang, } from './i18n.ts';
|
|
6
|
+
export { dict, dictionaries, label, type Dict, type GlossaryId } from './dict.ts';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// devcontour-harness-ui/model — view types, compatibility gate and data
|
|
2
|
+
// adapters without React. Hosts import this to build or check a view.
|
|
3
|
+
export * from "./view.js";
|
|
4
|
+
export { checkView, checkWorkDetail, KNOWN_VIEW_REVISION, } from "./compat.js";
|
|
5
|
+
export { httpViewSource, staticViewSource, } from "./source.js";
|
|
6
|
+
export { KNOWN_QUESTION_STATES, SLOT_STAGES, formatDuration, formatElapsed, leadMayAnswer, formatUsd, formatWindow, usd, questionStatus, stageLabel, } from "./format.js";
|
|
7
|
+
export { footerSentence, formatClock, headerSentence, langOf, pickLang, ruPlural, targetSentence, } from "./i18n.js";
|
|
8
|
+
export { dict, dictionaries, label } from "./dict.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type ViewCheck, type WorkDetailCheck } from './compat.ts';
|
|
2
|
+
export interface ViewSource {
|
|
3
|
+
/** Current view or a typed refusal; rejects only on transport failure. */
|
|
4
|
+
load(signal?: AbortSignal): Promise<ViewCheck>;
|
|
5
|
+
/** Work detail document or a typed refusal; absent = the host has no detail route. */
|
|
6
|
+
loadWork?(workId: string, signal?: AbortSignal): Promise<WorkDetailCheck>;
|
|
7
|
+
/** Optional push of changes; returns an unsubscribe function. */
|
|
8
|
+
subscribe?(onChange: () => void): () => void;
|
|
9
|
+
}
|
|
10
|
+
/** A fixed document (tests, previews, a host snapshot), with optional work details by id. */
|
|
11
|
+
export declare function staticViewSource(view: unknown, works?: Record<string, unknown>): ViewSource;
|
|
12
|
+
export interface HttpViewSourceOptions {
|
|
13
|
+
/** UI language, passed as `?lang=` so the harness writes plain strings in it. */
|
|
14
|
+
lang?: 'ru' | 'en';
|
|
15
|
+
}
|
|
16
|
+
export declare function httpViewSource(base: string, fetchImpl?: typeof fetch, options?: HttpViewSourceOptions): ViewSource;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Data adapter: how a host feeds the components. The standalone app uses
|
|
2
|
+
// `httpViewSource` against the local CLI; DevContour implements `ViewSource`
|
|
3
|
+
// over its own store. Components never fetch on their own.
|
|
4
|
+
//
|
|
5
|
+
// Wire contract (docs/design.md §6): `GET <base>/api/view[?lang=ru|en]`
|
|
6
|
+
// returns the bare view with an ETag; `GET <base>/api/work/<id>[?lang=]`
|
|
7
|
+
// returns the work detail document (`harness view --work <id>`). `lang` asks
|
|
8
|
+
// the harness for plain strings in that language (a file view ignores it); a
|
|
9
|
+
// compatibility refusal is HTTP 409 and an unavailable document 503, both with
|
|
10
|
+
// `{ error: { code, message } }`. The UI CLI and the proposed
|
|
11
|
+
// `harness serve-view` serve the same routes.
|
|
12
|
+
import { checkView, checkWorkDetail } from "./compat.js";
|
|
13
|
+
/** A fixed document (tests, previews, a host snapshot), with optional work details by id. */
|
|
14
|
+
export function staticViewSource(view, works = {}) {
|
|
15
|
+
return {
|
|
16
|
+
load: async () => checkView(view),
|
|
17
|
+
loadWork: async (id) => Object.hasOwn(works, id)
|
|
18
|
+
? checkWorkDetail(works[id])
|
|
19
|
+
: { ok: false, code: 'VIEW_UNAVAILABLE', message: `No work detail for ${id}.` },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const refusalCodes = new Set(['NOT_A_VIEW', 'VIEW_VERSION_UNSUPPORTED', 'VIEW_UNAVAILABLE']);
|
|
23
|
+
export function httpViewSource(base, fetchImpl = fetch, options = {}) {
|
|
24
|
+
const root = base.endsWith('/') ? base : base + '/';
|
|
25
|
+
const urlOf = (path) => {
|
|
26
|
+
const u = new URL(path, root);
|
|
27
|
+
if (options.lang)
|
|
28
|
+
u.searchParams.set('lang', options.lang);
|
|
29
|
+
return u.toString();
|
|
30
|
+
};
|
|
31
|
+
async function get(url, gate, signal) {
|
|
32
|
+
const res = await fetchImpl(url, {
|
|
33
|
+
method: 'GET',
|
|
34
|
+
headers: { accept: 'application/json' },
|
|
35
|
+
...(signal ? { signal } : {}),
|
|
36
|
+
});
|
|
37
|
+
if (res.ok)
|
|
38
|
+
return gate(await res.json());
|
|
39
|
+
// A typed refusal keeps its code and fix hint instead of becoming a
|
|
40
|
+
// generic transport error.
|
|
41
|
+
const body = (await res.json().catch(() => undefined));
|
|
42
|
+
const code = body?.error?.code;
|
|
43
|
+
if (typeof code === 'string' && refusalCodes.has(code)) {
|
|
44
|
+
return {
|
|
45
|
+
ok: false,
|
|
46
|
+
code: code,
|
|
47
|
+
message: typeof body?.error?.message === 'string' ? body.error.message : code,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
throw new Error(`GET ${url}: ${res.status}`);
|
|
51
|
+
}
|
|
52
|
+
const viewUrl = urlOf('api/view');
|
|
53
|
+
return {
|
|
54
|
+
load: (signal) => get(viewUrl, checkView, signal),
|
|
55
|
+
loadWork: (id, signal) => get(urlOf('api/work/' + encodeURIComponent(id)), checkWorkDetail, signal),
|
|
56
|
+
};
|
|
57
|
+
}
|