@sigrun/shared 0.4.1 → 0.5.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/CHANGELOG.md +9 -1
- package/dist/access.d.ts +60 -0
- package/dist/access.js +87 -0
- package/dist/ambient.d.ts +54 -0
- package/dist/ambient.js +25 -0
- package/dist/blocks/format.d.ts +28 -0
- package/dist/blocks/format.js +199 -0
- package/dist/blocks/icons.d.ts +12 -0
- package/dist/blocks/icons.js +64 -0
- package/dist/blocks/index.d.ts +25 -0
- package/dist/blocks/index.js +21 -0
- package/dist/blocks/instance.d.ts +29 -0
- package/dist/blocks/instance.js +51 -0
- package/dist/blocks/lttb.d.ts +16 -0
- package/dist/blocks/lttb.js +67 -0
- package/dist/blocks/ops.d.ts +44 -0
- package/dist/blocks/ops.js +294 -0
- package/dist/blocks/text.d.ts +43 -0
- package/dist/blocks/text.js +804 -0
- package/dist/blocks/transport.d.ts +81 -0
- package/dist/blocks/transport.js +11 -0
- package/dist/blocks/types.d.ts +558 -0
- package/dist/blocks/types.js +17 -0
- package/dist/commands.d.ts +75 -0
- package/dist/commands.js +25 -0
- package/dist/conversations.d.ts +31 -0
- package/dist/conversations.js +94 -0
- package/dist/interactions.d.ts +65 -0
- package/dist/interactions.js +23 -0
- package/dist/jobs.d.ts +109 -0
- package/dist/jobs.js +19 -0
- package/dist/json.d.ts +20 -0
- package/dist/json.js +13 -0
- package/dist/live.d.ts +71 -0
- package/dist/live.js +82 -0
- package/dist/session-meta.d.ts +130 -0
- package/dist/session-meta.js +13 -0
- package/dist/settings.d.ts +449 -0
- package/dist/settings.js +19 -0
- package/dist/themes.d.ts +49 -0
- package/dist/themes.js +155 -0
- package/dist/turns.d.ts +133 -0
- package/dist/turns.js +247 -0
- package/dist/usernames.d.ts +8 -0
- package/dist/usernames.js +39 -0
- package/dist/view-delta.d.ts +54 -0
- package/dist/view-delta.js +135 -0
- package/dist/views.d.ts +1021 -0
- package/dist/views.js +54 -0
- package/package.json +3 -2
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,14 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
-
## [
|
|
8
|
+
## [0.5.0] - 2026-11-02
|
|
9
|
+
|
|
10
|
+
Version-companion release for sigrun 0.5.0 (no shared changes).
|
|
11
|
+
## [0.4.2] - 2026-10-31
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
- The published tarball carries `dist/` again (`prepublishOnly` builds
|
|
15
|
+
before `npm publish` — releases 0.2.0–0.4.1 shipped without it).
|
|
9
16
|
|
|
10
17
|
## [0.4.1] - 2026-10-31
|
|
11
18
|
|
|
@@ -54,3 +61,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
54
61
|
conversation/turn shaping, the channel wire types, session-meta,
|
|
55
62
|
commands, interactions, ambient records, access, settings, views,
|
|
56
63
|
view-delta, and jobs — the surfaces the webui, TUI, and server share.
|
|
64
|
+
|
package/dist/access.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-resource sharing and content access (core hardening W1 Task 5, spec
|
|
3
|
+
* §5.9 ruling 6/10: per-resource sharing; multi-user M3b adds groups).
|
|
4
|
+
*
|
|
5
|
+
* Part of the shared leaf package (`@sigrun/shared`): the server computes
|
|
6
|
+
* access through `accessOf`, the clients render the `sharing` state — one
|
|
7
|
+
* source of truth, no drift. PURE module: no imports at all (the
|
|
8
|
+
* views-purity guard enforces this across `shared/src`).
|
|
9
|
+
*
|
|
10
|
+
* The rules, in order (first match wins):
|
|
11
|
+
*
|
|
12
|
+
* 0. internal resource → none (curator P5: a conversation created for
|
|
13
|
+
* curator work is invisible to EVERY caller — the superuser included);
|
|
14
|
+
* 1. superuser → write;
|
|
15
|
+
* 2. ownerless resource → household write (everyone);
|
|
16
|
+
* 3. user === owner → write;
|
|
17
|
+
* 4. household sharing with a defined user → the shared level;
|
|
18
|
+
* 5. people sharing including the user or one of their groups → the shared level;
|
|
19
|
+
* 6. otherwise none (hidden: 404 on every :id route, absent from lists).
|
|
20
|
+
*
|
|
21
|
+
* Anonymous callers (a principal with no user) get nothing from a shared
|
|
22
|
+
* resource: sharing widens access for identified users only (rule 4/5
|
|
23
|
+
* require a defined user), while ownerless stays open to everyone by
|
|
24
|
+
* rule 2.
|
|
25
|
+
*/
|
|
26
|
+
/** What a conversation's owner grants beyond themselves. */
|
|
27
|
+
export type Sharing = {
|
|
28
|
+
readonly scope: "private";
|
|
29
|
+
} | {
|
|
30
|
+
readonly scope: "household";
|
|
31
|
+
readonly level: "read" | "write";
|
|
32
|
+
} | {
|
|
33
|
+
readonly scope: "people";
|
|
34
|
+
readonly users: string[];
|
|
35
|
+
readonly groups?: string[];
|
|
36
|
+
readonly level: "read" | "write";
|
|
37
|
+
};
|
|
38
|
+
/** The resolved access level of a principal to one resource. */
|
|
39
|
+
export type Access = "none" | "read" | "write";
|
|
40
|
+
/** What accessOf needs of the caller (the server's Principal projects here). */
|
|
41
|
+
export interface AccessPrincipal {
|
|
42
|
+
readonly user?: string;
|
|
43
|
+
readonly superuser?: boolean;
|
|
44
|
+
readonly groups?: readonly string[];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolve one principal's access to one resource. Rule order per the
|
|
48
|
+
* module doc; the first matching rule wins.
|
|
49
|
+
*/
|
|
50
|
+
export declare function accessOf(p: AccessPrincipal, r: {
|
|
51
|
+
readonly owner?: string;
|
|
52
|
+
readonly sharing?: Sharing;
|
|
53
|
+
readonly internal?: true;
|
|
54
|
+
}): Access;
|
|
55
|
+
/**
|
|
56
|
+
* Type guard for the wire value: every scope shape checked strictly
|
|
57
|
+
* (wrong field types are not a Sharing). An empty `people.users` list is
|
|
58
|
+
* valid (shares with nobody yet) — validation is about shape, not policy.
|
|
59
|
+
*/
|
|
60
|
+
export declare function isSharing(value: unknown): value is Sharing;
|
package/dist/access.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-resource sharing and content access (core hardening W1 Task 5, spec
|
|
3
|
+
* §5.9 ruling 6/10: per-resource sharing; multi-user M3b adds groups).
|
|
4
|
+
*
|
|
5
|
+
* Part of the shared leaf package (`@sigrun/shared`): the server computes
|
|
6
|
+
* access through `accessOf`, the clients render the `sharing` state — one
|
|
7
|
+
* source of truth, no drift. PURE module: no imports at all (the
|
|
8
|
+
* views-purity guard enforces this across `shared/src`).
|
|
9
|
+
*
|
|
10
|
+
* The rules, in order (first match wins):
|
|
11
|
+
*
|
|
12
|
+
* 0. internal resource → none (curator P5: a conversation created for
|
|
13
|
+
* curator work is invisible to EVERY caller — the superuser included);
|
|
14
|
+
* 1. superuser → write;
|
|
15
|
+
* 2. ownerless resource → household write (everyone);
|
|
16
|
+
* 3. user === owner → write;
|
|
17
|
+
* 4. household sharing with a defined user → the shared level;
|
|
18
|
+
* 5. people sharing including the user or one of their groups → the shared level;
|
|
19
|
+
* 6. otherwise none (hidden: 404 on every :id route, absent from lists).
|
|
20
|
+
*
|
|
21
|
+
* Anonymous callers (a principal with no user) get nothing from a shared
|
|
22
|
+
* resource: sharing widens access for identified users only (rule 4/5
|
|
23
|
+
* require a defined user), while ownerless stays open to everyone by
|
|
24
|
+
* rule 2.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Resolve one principal's access to one resource. Rule order per the
|
|
28
|
+
* module doc; the first matching rule wins.
|
|
29
|
+
*/
|
|
30
|
+
export function accessOf(p, r) {
|
|
31
|
+
// 0. Internal (curator P5): invisible to everyone, the superuser
|
|
32
|
+
// included — BEFORE any other rule can widen access. The marker is
|
|
33
|
+
// server-owned (written once in the creating commit), so no caller,
|
|
34
|
+
// share, or role can ever open an internal resource.
|
|
35
|
+
if (r.internal === true)
|
|
36
|
+
return "none";
|
|
37
|
+
// 1. The superuser always writes.
|
|
38
|
+
if (p.superuser === true)
|
|
39
|
+
return "write";
|
|
40
|
+
// 2. Ownerless is household-write for everyone.
|
|
41
|
+
if (r.owner === undefined)
|
|
42
|
+
return "write";
|
|
43
|
+
// 3. The owner always writes.
|
|
44
|
+
if (p.user !== undefined && p.user === r.owner)
|
|
45
|
+
return "write";
|
|
46
|
+
// 4./5. Sharing widens identified users only — an anonymous caller
|
|
47
|
+
// (no user) falls straight through to none.
|
|
48
|
+
if (p.user !== undefined && r.sharing !== undefined) {
|
|
49
|
+
if (r.sharing.scope === "household")
|
|
50
|
+
return r.sharing.level;
|
|
51
|
+
if (r.sharing.scope === "people") {
|
|
52
|
+
const sharing = r.sharing;
|
|
53
|
+
if (sharing.users.includes(p.user) ||
|
|
54
|
+
p.groups?.some((group) => sharing.groups?.includes(group)) === true) {
|
|
55
|
+
return sharing.level;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// 6. Hidden.
|
|
60
|
+
return "none";
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Type guard for the wire value: every scope shape checked strictly
|
|
64
|
+
* (wrong field types are not a Sharing). An empty `people.users` list is
|
|
65
|
+
* valid (shares with nobody yet) — validation is about shape, not policy.
|
|
66
|
+
*/
|
|
67
|
+
export function isSharing(value) {
|
|
68
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
const record = value;
|
|
72
|
+
switch (record.scope) {
|
|
73
|
+
case "private":
|
|
74
|
+
return true;
|
|
75
|
+
case "household":
|
|
76
|
+
return record.level === "read" || record.level === "write";
|
|
77
|
+
case "people":
|
|
78
|
+
return (Array.isArray(record.users) &&
|
|
79
|
+
record.users.every((user) => typeof user === "string") &&
|
|
80
|
+
(record.groups === undefined ||
|
|
81
|
+
(Array.isArray(record.groups) &&
|
|
82
|
+
record.groups.every((group) => typeof group === "string"))) &&
|
|
83
|
+
(record.level === "read" || record.level === "write"));
|
|
84
|
+
default:
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ambient seam TYPES (spec: docs/approval-ux-seams.md, "Ambient
|
|
3
|
+
* surfaces"; the block mapping: UI-blocks design §9.2): the channel-neutral record
|
|
4
|
+
* shapes for persistent read-mostly state — statusline slots and panel
|
|
5
|
+
* widgets — written by extensions through the host context and rendered
|
|
6
|
+
* by channels generically.
|
|
7
|
+
*
|
|
8
|
+
* The CANONICAL type declarations live here (the shared leaf package);
|
|
9
|
+
* `src/harness/ambient.ts` imports them (type-only) for the doc
|
|
10
|
+
* definitions and validation runtime. The records ride the §5 wire
|
|
11
|
+
* AS-IS (envelope + body + details — never projected), so these shapes
|
|
12
|
+
* are the channel contract too (see `@sigrun/shared/views`).
|
|
13
|
+
*
|
|
14
|
+
* Lifecycle difference from interactions: an ambient surface is replaced
|
|
15
|
+
* repeatedly (write = FULL state replacement, never a delta) and never
|
|
16
|
+
* blocks. One slot key is owned by exactly one extension (allocation,
|
|
17
|
+
* not silent composition — the pi-zentui chrome-skin lesson).
|
|
18
|
+
*
|
|
19
|
+
* Layering mirrors the interaction seam: strict envelope (key, place,
|
|
20
|
+
* budget, refresh, expires) around a block-tree body (placement
|
|
21
|
+
* `ambient`; `status`/`footer` allow one line), with opaque `details`.
|
|
22
|
+
* The legacy `line`/`rows`/`gauge`/`ledger` primitives are REJECTED
|
|
23
|
+
* (ruling 16: no compatibility layer).
|
|
24
|
+
*/
|
|
25
|
+
import type { Tree } from "./blocks/index.js";
|
|
26
|
+
import type { JsonObject } from "./json.js";
|
|
27
|
+
/** Where a channel places the surface. */
|
|
28
|
+
export type AmbientPlace = "status" | "footer" | "panel";
|
|
29
|
+
/** The strict envelope + block-tree body of one ambient surface. */
|
|
30
|
+
export type AmbientRecord = {
|
|
31
|
+
/** The keyed slot id ("goal", "rpiv-todos", "fabric"). */
|
|
32
|
+
readonly key: string;
|
|
33
|
+
/** Which extension owns this key (set by the host context, not the author). */
|
|
34
|
+
readonly owner: string;
|
|
35
|
+
readonly place: AmbientPlace;
|
|
36
|
+
/** Panel row budget / line-slot width guidance (channel advisory). */
|
|
37
|
+
readonly maxRows?: number;
|
|
38
|
+
/** Overflow policy for panels (rpiv-todo's drop-completed-first rule). */
|
|
39
|
+
readonly overflow?: "drop" | "wrap" | "summarize";
|
|
40
|
+
/** Channel re-render hint between data writes (countdowns, clocks), ms. */
|
|
41
|
+
readonly refreshMs?: number;
|
|
42
|
+
/** Auto-clear rule: "never" stays until replaced with an empty body. */
|
|
43
|
+
readonly expires: "never" | "idle";
|
|
44
|
+
readonly updatedAt: number;
|
|
45
|
+
/**
|
|
46
|
+
* The ambient body: a block tree (placement `ambient`). `status` and
|
|
47
|
+
* `footer` allow one line — a single line-level block or one
|
|
48
|
+
* `columns` of one-line blocks. Undefined = the surface is cleared.
|
|
49
|
+
* Legacy-shaped bodies fail validation (ruling 16).
|
|
50
|
+
*/
|
|
51
|
+
readonly body?: Tree;
|
|
52
|
+
/** Opaque extension-owned payload; rendered collapsed, never interpreted. */
|
|
53
|
+
readonly details?: JsonObject;
|
|
54
|
+
};
|
package/dist/ambient.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ambient seam TYPES (spec: docs/approval-ux-seams.md, "Ambient
|
|
3
|
+
* surfaces"; the block mapping: UI-blocks design §9.2): the channel-neutral record
|
|
4
|
+
* shapes for persistent read-mostly state — statusline slots and panel
|
|
5
|
+
* widgets — written by extensions through the host context and rendered
|
|
6
|
+
* by channels generically.
|
|
7
|
+
*
|
|
8
|
+
* The CANONICAL type declarations live here (the shared leaf package);
|
|
9
|
+
* `src/harness/ambient.ts` imports them (type-only) for the doc
|
|
10
|
+
* definitions and validation runtime. The records ride the §5 wire
|
|
11
|
+
* AS-IS (envelope + body + details — never projected), so these shapes
|
|
12
|
+
* are the channel contract too (see `@sigrun/shared/views`).
|
|
13
|
+
*
|
|
14
|
+
* Lifecycle difference from interactions: an ambient surface is replaced
|
|
15
|
+
* repeatedly (write = FULL state replacement, never a delta) and never
|
|
16
|
+
* blocks. One slot key is owned by exactly one extension (allocation,
|
|
17
|
+
* not silent composition — the pi-zentui chrome-skin lesson).
|
|
18
|
+
*
|
|
19
|
+
* Layering mirrors the interaction seam: strict envelope (key, place,
|
|
20
|
+
* budget, refresh, expires) around a block-tree body (placement
|
|
21
|
+
* `ambient`; `status`/`footer` allow one line), with opaque `details`.
|
|
22
|
+
* The legacy `line`/`rows`/`gauge`/`ledger` primitives are REJECTED
|
|
23
|
+
* (ruling 16: no compatibility layer).
|
|
24
|
+
*/
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `formatValue` (UI-blocks spec §5.3): the one value formatter behind
|
|
3
|
+
* `format` props — `stat`, `meter`, table columns, chart axes and card
|
|
4
|
+
* time slots. Pure `Intl` only; no imports outside `shared/src/`.
|
|
5
|
+
*
|
|
6
|
+
* Determinism (spec §14.3): the same input and locale give the same
|
|
7
|
+
* bytes. Everything here is derived from the arguments — no clock
|
|
8
|
+
* reads (the `relative` format takes the timestamp to describe, and
|
|
9
|
+
* `now` comes from `Date.now()` at the CALL SITE inside `toText`
|
|
10
|
+
* rendering... which is equally forbidden to wander). `relative` is
|
|
11
|
+
* therefore stable within a call and pinned by tests that pass a
|
|
12
|
+
* timestamp chosen so the unit boundary cannot flip mid-run.
|
|
13
|
+
*/
|
|
14
|
+
import type { JsonValue } from "../json.js";
|
|
15
|
+
/**
|
|
16
|
+
* Render `value` in `format` for `locale` (spec §5.3's format list).
|
|
17
|
+
*
|
|
18
|
+
* - `undefined` format: numbers group digits; strings pass through;
|
|
19
|
+
* `null` renders empty; booleans spell out; objects and arrays render
|
|
20
|
+
* as JSON with SORTED keys (deterministic bytes regardless of the
|
|
21
|
+
* key order the data arrived in).
|
|
22
|
+
* - `datetime`/`date`/`time` take epoch milliseconds or an ISO string,
|
|
23
|
+
* rendered in `timeZone` when given (defaults to the locale's).
|
|
24
|
+
* - `relative` describes a timestamp relative to `Date.now()`; the unit
|
|
25
|
+
* is chosen by the span, per `Intl.RelativeTimeFormat`.
|
|
26
|
+
* - An unknown format falls back to plain number formatting.
|
|
27
|
+
*/
|
|
28
|
+
export declare function formatValue(value: JsonValue, format: string | undefined, locale: string, timeZone?: string): string;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `formatValue` (UI-blocks spec §5.3): the one value formatter behind
|
|
3
|
+
* `format` props — `stat`, `meter`, table columns, chart axes and card
|
|
4
|
+
* time slots. Pure `Intl` only; no imports outside `shared/src/`.
|
|
5
|
+
*
|
|
6
|
+
* Determinism (spec §14.3): the same input and locale give the same
|
|
7
|
+
* bytes. Everything here is derived from the arguments — no clock
|
|
8
|
+
* reads (the `relative` format takes the timestamp to describe, and
|
|
9
|
+
* `now` comes from `Date.now()` at the CALL SITE inside `toText`
|
|
10
|
+
* rendering... which is equally forbidden to wander). `relative` is
|
|
11
|
+
* therefore stable within a call and pinned by tests that pass a
|
|
12
|
+
* timestamp chosen so the unit boundary cannot flip mid-run.
|
|
13
|
+
*/
|
|
14
|
+
/** The byte-scale units `bytes` walks up through (binary steps of 1024). */
|
|
15
|
+
const BYTE_UNITS = ["B", "KB", "MB", "GB", "TB", "PB"];
|
|
16
|
+
/** The duration steps `duration` walks down through, with their ms sizes. */
|
|
17
|
+
const DURATION_STEPS = [
|
|
18
|
+
{ unit: "d", ms: 86_400_000 },
|
|
19
|
+
{ unit: "h", ms: 3_600_000 },
|
|
20
|
+
{ unit: "m", ms: 60_000 },
|
|
21
|
+
{ unit: "s", ms: 1_000 },
|
|
22
|
+
{ unit: "ms", ms: 1 },
|
|
23
|
+
];
|
|
24
|
+
/** A locale-aware number formatter with at most `max` fraction digits. */
|
|
25
|
+
function numberFormatter(locale, maximumFractionDigits) {
|
|
26
|
+
return new Intl.NumberFormat(locale, { maximumFractionDigits });
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `bytes`: 1024-based scale with a `maximumFractionDigits: 1` number,
|
|
30
|
+
* e.g. `1536 → "1.5 KB"`, `512 → "512 B"`.
|
|
31
|
+
*/
|
|
32
|
+
function formatBytes(value, locale) {
|
|
33
|
+
let unit = 0;
|
|
34
|
+
let scaled = value;
|
|
35
|
+
while (Math.abs(scaled) >= 1024 && unit < BYTE_UNITS.length - 1) {
|
|
36
|
+
scaled /= 1024;
|
|
37
|
+
unit++;
|
|
38
|
+
}
|
|
39
|
+
return `${numberFormatter(locale, 1).format(scaled)} ${BYTE_UNITS[unit]}`;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* `duration`: milliseconds in, the two most significant non-zero units
|
|
43
|
+
* out, e.g. `11520000 → "3h 12m"`, `1500 → "1s 500ms"`, `0 → "0s"`.
|
|
44
|
+
* Sub-second remainders are truncated (not rounded) so the last unit
|
|
45
|
+
* named is the one actually covered.
|
|
46
|
+
*/
|
|
47
|
+
function formatDuration(ms, locale) {
|
|
48
|
+
let rest = Math.trunc(ms);
|
|
49
|
+
const parts = [];
|
|
50
|
+
for (const step of DURATION_STEPS) {
|
|
51
|
+
if (parts.length === 2)
|
|
52
|
+
break;
|
|
53
|
+
if (rest >= step.ms || (parts.length > 0 && rest > 0)) {
|
|
54
|
+
const size = Math.floor(rest / step.ms);
|
|
55
|
+
parts.push(`${numberFormatter(locale, 0).format(size)}${step.unit}`);
|
|
56
|
+
rest -= size * step.ms;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (parts.length === 0)
|
|
60
|
+
return `0s`;
|
|
61
|
+
return parts.join(" ");
|
|
62
|
+
}
|
|
63
|
+
/** Coerce a datetime-ish JSON value to epoch milliseconds, or `undefined`. */
|
|
64
|
+
function epochMillis(value) {
|
|
65
|
+
if (typeof value === "number")
|
|
66
|
+
return value;
|
|
67
|
+
if (typeof value === "string") {
|
|
68
|
+
const parsed = Date.parse(value);
|
|
69
|
+
return Number.isNaN(parsed) ? undefined : parsed;
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Render `value` in `format` for `locale` (spec §5.3's format list).
|
|
75
|
+
*
|
|
76
|
+
* - `undefined` format: numbers group digits; strings pass through;
|
|
77
|
+
* `null` renders empty; booleans spell out; objects and arrays render
|
|
78
|
+
* as JSON with SORTED keys (deterministic bytes regardless of the
|
|
79
|
+
* key order the data arrived in).
|
|
80
|
+
* - `datetime`/`date`/`time` take epoch milliseconds or an ISO string,
|
|
81
|
+
* rendered in `timeZone` when given (defaults to the locale's).
|
|
82
|
+
* - `relative` describes a timestamp relative to `Date.now()`; the unit
|
|
83
|
+
* is chosen by the span, per `Intl.RelativeTimeFormat`.
|
|
84
|
+
* - An unknown format falls back to plain number formatting.
|
|
85
|
+
*/
|
|
86
|
+
export function formatValue(value, format, locale, timeZone) {
|
|
87
|
+
switch (format) {
|
|
88
|
+
case "number":
|
|
89
|
+
case undefined:
|
|
90
|
+
return plain(value, locale);
|
|
91
|
+
case "compact":
|
|
92
|
+
return typeof value === "number"
|
|
93
|
+
? new Intl.NumberFormat(locale, {
|
|
94
|
+
notation: "compact",
|
|
95
|
+
maximumFractionDigits: 1,
|
|
96
|
+
}).format(value)
|
|
97
|
+
: plain(value, locale);
|
|
98
|
+
case "percent":
|
|
99
|
+
return typeof value === "number"
|
|
100
|
+
? new Intl.NumberFormat(locale, {
|
|
101
|
+
style: "percent",
|
|
102
|
+
maximumFractionDigits: 1,
|
|
103
|
+
}).format(value)
|
|
104
|
+
: plain(value, locale);
|
|
105
|
+
case "bytes":
|
|
106
|
+
return typeof value === "number" ? formatBytes(value, locale) : plain(value, locale);
|
|
107
|
+
case "duration":
|
|
108
|
+
return typeof value === "number" ? formatDuration(value, locale) : plain(value, locale);
|
|
109
|
+
case "datetime":
|
|
110
|
+
case "date":
|
|
111
|
+
case "time": {
|
|
112
|
+
const at = epochMillis(value);
|
|
113
|
+
if (at === undefined)
|
|
114
|
+
return plain(value, locale);
|
|
115
|
+
const options = format === "datetime"
|
|
116
|
+
? { dateStyle: "medium", timeStyle: "short" }
|
|
117
|
+
: format === "date"
|
|
118
|
+
? { dateStyle: "medium" }
|
|
119
|
+
: { timeStyle: "medium" };
|
|
120
|
+
return new Intl.DateTimeFormat(locale, timeZone ? { ...options, timeZone } : options).format(at);
|
|
121
|
+
}
|
|
122
|
+
case "relative": {
|
|
123
|
+
const at = epochMillis(value);
|
|
124
|
+
if (at === undefined)
|
|
125
|
+
return plain(value, locale);
|
|
126
|
+
return relativeFrom(at, Date.now(), locale);
|
|
127
|
+
}
|
|
128
|
+
default:
|
|
129
|
+
if (format.startsWith("currency:")) {
|
|
130
|
+
const currency = format.slice("currency:".length);
|
|
131
|
+
if (typeof value !== "number")
|
|
132
|
+
return plain(value, locale);
|
|
133
|
+
try {
|
|
134
|
+
return new Intl.NumberFormat(locale, {
|
|
135
|
+
style: "currency",
|
|
136
|
+
currency,
|
|
137
|
+
}).format(value);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// An invalid ISO 4217 code (validation's job to catch
|
|
141
|
+
// earlier) renders as a plain number, never a throw.
|
|
142
|
+
return numberFormatter(locale, 2).format(value);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return plain(value, locale);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** The no-format rendering: locale-numbered primitives, sorted-key JSON. */
|
|
149
|
+
function plain(value, locale) {
|
|
150
|
+
if (value === null)
|
|
151
|
+
return "";
|
|
152
|
+
switch (typeof value) {
|
|
153
|
+
case "string":
|
|
154
|
+
return value;
|
|
155
|
+
case "number":
|
|
156
|
+
return new Intl.NumberFormat(locale, { maximumFractionDigits: 3 }).format(value);
|
|
157
|
+
case "boolean":
|
|
158
|
+
return value ? "true" : "false";
|
|
159
|
+
default:
|
|
160
|
+
return JSON.stringify(sortKeys(value));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** Recursively copy `value` with every object's keys in sorted order. */
|
|
164
|
+
function sortKeys(value) {
|
|
165
|
+
if (Array.isArray(value))
|
|
166
|
+
return value.map(sortKeys);
|
|
167
|
+
if (value !== null && typeof value === "object") {
|
|
168
|
+
const out = {};
|
|
169
|
+
for (const key of Object.keys(value).sort()) {
|
|
170
|
+
out[key] = sortKeys(value[key]);
|
|
171
|
+
}
|
|
172
|
+
return out;
|
|
173
|
+
}
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* `"<span> ago"` for a timestamp: picks the unit whose threshold the
|
|
178
|
+
* span has crossed (`Intl.RelativeTimeFormat` conventions: seconds
|
|
179
|
+
* under a minute, minutes under an hour, hours under a day, days under
|
|
180
|
+
* a week, then weeks, months, years).
|
|
181
|
+
*/
|
|
182
|
+
function relativeFrom(at, now, locale) {
|
|
183
|
+
const spanSeconds = Math.round((at - now) / 1000);
|
|
184
|
+
const abs = Math.abs(spanSeconds);
|
|
185
|
+
const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
|
186
|
+
if (abs < 60)
|
|
187
|
+
return rtf.format(spanSeconds, "second");
|
|
188
|
+
if (abs < 3600)
|
|
189
|
+
return rtf.format(Math.round(spanSeconds / 60), "minute");
|
|
190
|
+
if (abs < 86_400)
|
|
191
|
+
return rtf.format(Math.round(spanSeconds / 3600), "hour");
|
|
192
|
+
if (abs < 7 * 86_400)
|
|
193
|
+
return rtf.format(Math.round(spanSeconds / 86_400), "day");
|
|
194
|
+
if (abs < 30 * 86_400)
|
|
195
|
+
return rtf.format(Math.round(spanSeconds / (7 * 86_400)), "week");
|
|
196
|
+
if (abs < 365 * 86_400)
|
|
197
|
+
return rtf.format(Math.round(spanSeconds / (30 * 86_400)), "month");
|
|
198
|
+
return rtf.format(Math.round(spanSeconds / (365 * 86_400)), "year");
|
|
199
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The named icons (UI-blocks spec §10.2): a CLOSED set of 49 names.
|
|
3
|
+
*
|
|
4
|
+
* The webui draws them from a bundled subset of Lucide; the TUI maps
|
|
5
|
+
* each name to a Unicode glyph with an ASCII fallback. An unknown icon
|
|
6
|
+
* name fails validation (U0 Task 2) — `IconName` is the literal union,
|
|
7
|
+
* so a typo is a type error wherever the contract is used literally.
|
|
8
|
+
*/
|
|
9
|
+
/** The v1 icon set, exactly as spec §10.2 pins it. */
|
|
10
|
+
export declare const ICON_NAMES: readonly ["add", "edit", "delete", "refresh", "search", "filter", "share", "copy", "link", "external", "more", "check", "close", "drag", "file", "folder", "calendar", "clock", "user", "users", "tag", "list", "board", "chart", "image", "book", "cart", "home", "bell", "lock", "key", "info", "warn", "error", "success", "pending", "running", "sun", "moon", "cloud", "cloud-sun", "cloud-rain", "cloud-snow", "cloud-lightning", "fog", "wind", "droplets", "thermometer", "umbrella"];
|
|
11
|
+
/** One name from the closed set. */
|
|
12
|
+
export type IconName = (typeof ICON_NAMES)[number];
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The named icons (UI-blocks spec §10.2): a CLOSED set of 49 names.
|
|
3
|
+
*
|
|
4
|
+
* The webui draws them from a bundled subset of Lucide; the TUI maps
|
|
5
|
+
* each name to a Unicode glyph with an ASCII fallback. An unknown icon
|
|
6
|
+
* name fails validation (U0 Task 2) — `IconName` is the literal union,
|
|
7
|
+
* so a typo is a type error wherever the contract is used literally.
|
|
8
|
+
*/
|
|
9
|
+
/** The v1 icon set, exactly as spec §10.2 pins it. */
|
|
10
|
+
export const ICON_NAMES = [
|
|
11
|
+
// navigation and actions
|
|
12
|
+
"add",
|
|
13
|
+
"edit",
|
|
14
|
+
"delete",
|
|
15
|
+
"refresh",
|
|
16
|
+
"search",
|
|
17
|
+
"filter",
|
|
18
|
+
"share",
|
|
19
|
+
"copy",
|
|
20
|
+
"link",
|
|
21
|
+
"external",
|
|
22
|
+
"more",
|
|
23
|
+
"check",
|
|
24
|
+
"close",
|
|
25
|
+
"drag",
|
|
26
|
+
// objects
|
|
27
|
+
"file",
|
|
28
|
+
"folder",
|
|
29
|
+
"calendar",
|
|
30
|
+
"clock",
|
|
31
|
+
"user",
|
|
32
|
+
"users",
|
|
33
|
+
"tag",
|
|
34
|
+
"list",
|
|
35
|
+
"board",
|
|
36
|
+
"chart",
|
|
37
|
+
"image",
|
|
38
|
+
"book",
|
|
39
|
+
"cart",
|
|
40
|
+
"home",
|
|
41
|
+
"bell",
|
|
42
|
+
"lock",
|
|
43
|
+
"key",
|
|
44
|
+
// status
|
|
45
|
+
"info",
|
|
46
|
+
"warn",
|
|
47
|
+
"error",
|
|
48
|
+
"success",
|
|
49
|
+
"pending",
|
|
50
|
+
"running",
|
|
51
|
+
// weather
|
|
52
|
+
"sun",
|
|
53
|
+
"moon",
|
|
54
|
+
"cloud",
|
|
55
|
+
"cloud-sun",
|
|
56
|
+
"cloud-rain",
|
|
57
|
+
"cloud-snow",
|
|
58
|
+
"cloud-lightning",
|
|
59
|
+
"fog",
|
|
60
|
+
"wind",
|
|
61
|
+
"droplets",
|
|
62
|
+
"thermometer",
|
|
63
|
+
"umbrella",
|
|
64
|
+
];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sigrun/shared/blocks` — the block-set foundation (UI-blocks U0).
|
|
3
|
+
*
|
|
4
|
+
* One entry point for the wire types (§4/§5/§6.1), the data-op applier
|
|
5
|
+
* (§4.3), the icon names (§10.2), display downsampling (§5.4), and the
|
|
6
|
+
* channel-facing surface transport. Consumed by the server, the webui
|
|
7
|
+
* and the TUI alike; re-exported for extensions through
|
|
8
|
+
* `@sigrun/sigrun/blocks` (U0 Task 4).
|
|
9
|
+
*
|
|
10
|
+
* The runtime half is deliberately tiny and dependency-free: the purity
|
|
11
|
+
* guard (`test/views-purity.test.ts`) holds every specifier inside
|
|
12
|
+
* `shared/src/`.
|
|
13
|
+
*/
|
|
14
|
+
export { BLOCK_SET_VERSION } from "./types.js";
|
|
15
|
+
export { DataOpError, applyOps, resolvePointer } from "./ops.js";
|
|
16
|
+
export { ICON_NAMES } from "./icons.js";
|
|
17
|
+
export { lttb } from "./lttb.js";
|
|
18
|
+
export { formatValue } from "./format.js";
|
|
19
|
+
export { toText } from "./text.js";
|
|
20
|
+
export { canonicalJson, eventKeyWeb, instanceKey } from "./instance.js";
|
|
21
|
+
export type { Action, ActionButton, Bindable, Binding, Block, BlockBase, BulkAction, CalloutBlock, Card, CardBadge, CardMetric, CardMeta, CardStatus, ChartAxisX, ChartAxisY, ChartBlock, ChartSeries, ChartThreshold, ChoiceField, ChoiceOption, CollectionBlock, CollectionColumn, CollectionGroup, CollectionSort, CollectionTime, ColumnChild, ColumnsBlock, Confirm, CoreTarget, DataOp, DataOps, DataUpdate, DateField, DatePreset, DaterangeField, DatetimeField, DetailsBlock, DiffBlock, Field, FieldBase, FieldsBlock, FieldsRow, FileField, Format, FormBlock, GridBlock, GridChild, ImageBlock, ListField, LogBlock, LogLine, LongtextField, MarkdownBlock, MeterBlock, MeterThreshold, NumberField, PageBlock, PageEmpty, Placement, SectionBlock, SecretField, Size, StackBlock, StatBlock, TextField, ToggleField, Tone, Tree, TreeBlock, TreeNode, WidthCondition, } from "./types.js";
|
|
22
|
+
export type { IconName } from "./icons.js";
|
|
23
|
+
export type { TextOptions } from "./text.js";
|
|
24
|
+
export type { SurfaceAccess, SurfaceActErr, SurfaceActOk, SurfaceActResult, SurfaceChangedEvent, SurfaceFetch, SurfaceLoad, SurfaceTransport, SurfaceUploadBody, } from "./transport.js";
|
|
25
|
+
export type { JsonObject, JsonValue } from "../json.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@sigrun/shared/blocks` — the block-set foundation (UI-blocks U0).
|
|
3
|
+
*
|
|
4
|
+
* One entry point for the wire types (§4/§5/§6.1), the data-op applier
|
|
5
|
+
* (§4.3), the icon names (§10.2), display downsampling (§5.4), and the
|
|
6
|
+
* channel-facing surface transport. Consumed by the server, the webui
|
|
7
|
+
* and the TUI alike; re-exported for extensions through
|
|
8
|
+
* `@sigrun/sigrun/blocks` (U0 Task 4).
|
|
9
|
+
*
|
|
10
|
+
* The runtime half is deliberately tiny and dependency-free: the purity
|
|
11
|
+
* guard (`test/views-purity.test.ts`) holds every specifier inside
|
|
12
|
+
* `shared/src/`.
|
|
13
|
+
*/
|
|
14
|
+
// ─── Runtime (pure, dependency-free) ──────────────────────────────────────
|
|
15
|
+
export { BLOCK_SET_VERSION } from "./types.js";
|
|
16
|
+
export { DataOpError, applyOps, resolvePointer } from "./ops.js";
|
|
17
|
+
export { ICON_NAMES } from "./icons.js";
|
|
18
|
+
export { lttb } from "./lttb.js";
|
|
19
|
+
export { formatValue } from "./format.js";
|
|
20
|
+
export { toText } from "./text.js";
|
|
21
|
+
export { canonicalJson, eventKeyWeb, instanceKey } from "./instance.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The instance-key helpers the CHANNELS need (UI-blocks U3 Task 5, spec
|
|
3
|
+
* §7.3): `canonicalJson` (the default `instanceKey`) and
|
|
4
|
+
* `eventKeyWeb`/`eventKeyNode` (the wire `k` — sha256 hex of
|
|
5
|
+
* `slug\0id\0key`). The server's copy lives in
|
|
6
|
+
* `src/server/surfaces/instance.ts`; these are the DOM- and Node-safe
|
|
7
|
+
* twins (WebCrypto's `crypto.subtle.digest` vs `node:crypto`), kept
|
|
8
|
+
* byte-compatible by the shared canonical-JSON rule.
|
|
9
|
+
*
|
|
10
|
+
* The wire's purity rule holds: NO node: imports here — the Node flavor
|
|
11
|
+
* is a separate module (`instance-node.ts`) so the webui never sees it.
|
|
12
|
+
*/
|
|
13
|
+
import type { JsonObject, JsonValue } from "../json.js";
|
|
14
|
+
/**
|
|
15
|
+
* Canonical JSON: every object serializes with keys SORTED (recursive);
|
|
16
|
+
* primitives and arrays serialize as usual. Two params objects equal up
|
|
17
|
+
* to key order produce byte-identical strings — the same rule as the
|
|
18
|
+
* server's `canonicalJson` (kept in sync by hand; both sides pin it by
|
|
19
|
+
* test).
|
|
20
|
+
*/
|
|
21
|
+
export declare function canonicalJson(value: JsonValue): string;
|
|
22
|
+
/** The default instance key (spec §7.3): canonical JSON of the params. */
|
|
23
|
+
export declare function instanceKey(params: JsonObject): string;
|
|
24
|
+
/**
|
|
25
|
+
* The wire `k` for one surface instance (spec §7.3), WebCrypto flavor:
|
|
26
|
+
* `sha256(slug + "\0" + id + "\0" + key)` hex. Async (the DOM digest is
|
|
27
|
+
* promise-shaped); the Node twin (`instance-node.ts`) is sync.
|
|
28
|
+
*/
|
|
29
|
+
export declare function eventKeyWeb(slug: string, id: string, key: string): Promise<string>;
|