@littlepress/renderer-web 0.0.0-stage → 1.0.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.
Files changed (187) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +20 -0
  3. package/README.md +229 -2
  4. package/dist/answer/answerComponents.d.ts +107 -0
  5. package/dist/answer/answerComponents.js +637 -0
  6. package/dist/answer/download.d.ts +63 -0
  7. package/dist/answer/download.js +109 -0
  8. package/dist/answer/downloadExport.d.ts +44 -0
  9. package/dist/answer/downloadExport.js +266 -0
  10. package/dist/answer/hostValues.d.ts +28 -0
  11. package/dist/answer/hostValues.js +379 -0
  12. package/dist/answer/mapComponent.d.ts +6 -0
  13. package/dist/answer/mapComponent.js +213 -0
  14. package/dist/answer/math.d.ts +57 -0
  15. package/dist/answer/math.js +168 -0
  16. package/dist/answer/surfaceStream.d.ts +51 -0
  17. package/dist/answer/surfaceStream.js +260 -0
  18. package/dist/answer/tableLayout.d.ts +42 -0
  19. package/dist/answer/tableLayout.js +89 -0
  20. package/dist/controls/ActionFeedback.d.ts +5 -0
  21. package/dist/controls/ActionFeedback.js +43 -0
  22. package/dist/controls/actionDispatch.d.ts +51 -0
  23. package/dist/controls/actionDispatch.js +211 -0
  24. package/dist/controls/actionOutcome.generated.d.ts +64 -0
  25. package/dist/controls/actionOutcome.generated.js +64 -0
  26. package/dist/controls/calculationPublisher.d.ts +14 -0
  27. package/dist/controls/calculationPublisher.js +6 -0
  28. package/dist/controls/calculations.d.ts +20 -0
  29. package/dist/controls/calculations.generated.d.ts +73 -0
  30. package/dist/controls/calculations.generated.js +85 -0
  31. package/dist/controls/calculations.js +208 -0
  32. package/dist/controls/controlState.d.ts +84 -0
  33. package/dist/controls/controlState.js +256 -0
  34. package/dist/controls/fieldMessages.d.ts +41 -0
  35. package/dist/controls/fieldMessages.js +81 -0
  36. package/dist/controls/fieldValidation.d.ts +19 -0
  37. package/dist/controls/fieldValidation.js +98 -0
  38. package/dist/controls/formComponents.d.ts +23 -0
  39. package/dist/controls/formComponents.js +738 -0
  40. package/dist/controls/formProgress.d.ts +63 -0
  41. package/dist/controls/formProgress.js +122 -0
  42. package/dist/controls/inputDrafts.d.ts +55 -0
  43. package/dist/controls/inputDrafts.js +183 -0
  44. package/dist/controls/inputValidation.d.ts +33 -0
  45. package/dist/controls/inputValidation.js +119 -0
  46. package/dist/controls/localWrites.d.ts +9 -0
  47. package/dist/controls/localWrites.js +29 -0
  48. package/dist/core/A2uiSurface.d.ts +13 -0
  49. package/dist/core/A2uiSurface.js +15 -0
  50. package/dist/core/AtlasIcon.d.ts +18 -0
  51. package/dist/core/AtlasIcon.js +22 -0
  52. package/dist/core/AtlasMedia.d.ts +22 -0
  53. package/dist/core/AtlasMedia.js +45 -0
  54. package/dist/core/Carousel.d.ts +61 -0
  55. package/dist/core/Carousel.js +183 -0
  56. package/dist/core/CoreMedia.d.ts +19 -0
  57. package/dist/core/CoreMedia.js +25 -0
  58. package/dist/core/FoundationA2uiSurface.d.ts +86 -0
  59. package/dist/core/FoundationA2uiSurface.js +440 -0
  60. package/dist/core/ImagePreview.d.ts +42 -0
  61. package/dist/core/ImagePreview.js +73 -0
  62. package/dist/core/LucideIcon.d.ts +19 -0
  63. package/dist/core/LucideIcon.js +42 -0
  64. package/dist/core/SurfaceLink.d.ts +35 -0
  65. package/dist/core/SurfaceLink.js +60 -0
  66. package/dist/core/appearance/aegis.d.ts +49 -0
  67. package/dist/core/appearance/aegis.js +34 -0
  68. package/dist/core/appearance/colour.d.ts +24 -0
  69. package/dist/core/appearance/colour.js +103 -0
  70. package/dist/core/appearance/css.d.ts +17 -0
  71. package/dist/core/appearance/css.js +153 -0
  72. package/dist/core/appearance/presets.generated.d.ts +4 -0
  73. package/dist/core/appearance/presets.generated.js +1184 -0
  74. package/dist/core/appearance/react.d.ts +29 -0
  75. package/dist/core/appearance/react.js +110 -0
  76. package/dist/core/appearance/resolve.d.ts +76 -0
  77. package/dist/core/appearance/resolve.js +127 -0
  78. package/dist/core/appearance/textScale.d.ts +11 -0
  79. package/dist/core/appearance/textScale.js +51 -0
  80. package/dist/core/appearance/theme.d.ts +138 -0
  81. package/dist/core/appearance/theme.js +274 -0
  82. package/dist/core/baseComponents.d.ts +53 -0
  83. package/dist/core/baseComponents.js +480 -0
  84. package/dist/core/binding.d.ts +46 -0
  85. package/dist/core/binding.js +118 -0
  86. package/dist/core/bundle.d.ts +23 -0
  87. package/dist/core/bundle.js +75 -0
  88. package/dist/core/catalog/catalog-index.generated.d.ts +44 -0
  89. package/dist/core/catalog/catalog-index.generated.js +79 -0
  90. package/dist/core/catalog/icons.generated.d.ts +6 -0
  91. package/dist/core/catalog/icons.generated.js +1788 -0
  92. package/dist/core/catalog.d.ts +6 -0
  93. package/dist/core/catalog.generated.d.ts +50 -0
  94. package/dist/core/catalog.generated.js +2174 -0
  95. package/dist/core/catalog.js +8 -0
  96. package/dist/core/chartRows.d.ts +7 -0
  97. package/dist/core/chartRows.js +38 -0
  98. package/dist/core/component-support.d.ts +39 -0
  99. package/dist/core/component-support.js +127 -0
  100. package/dist/core/components/actions.d.ts +4 -0
  101. package/dist/core/components/actions.js +21 -0
  102. package/dist/core/components/cards.d.ts +9 -0
  103. package/dist/core/components/cards.js +227 -0
  104. package/dist/core/components/charts.d.ts +18 -0
  105. package/dist/core/components/charts.js +412 -0
  106. package/dist/core/components/comparisonTable.d.ts +50 -0
  107. package/dist/core/components/comparisonTable.js +89 -0
  108. package/dist/core/components/content.d.ts +13 -0
  109. package/dist/core/components/content.js +108 -0
  110. package/dist/core/components/seriesCharts.d.ts +531 -0
  111. package/dist/core/components/seriesCharts.js +1053 -0
  112. package/dist/core/components/stepFlow.d.ts +7 -0
  113. package/dist/core/components/stepFlow.js +13 -0
  114. package/dist/core/components.full.d.ts +10 -0
  115. package/dist/core/components.full.js +41 -0
  116. package/dist/core/context.d.ts +207 -0
  117. package/dist/core/context.js +172 -0
  118. package/dist/core/coverage.d.ts +30 -0
  119. package/dist/core/coverage.js +93 -0
  120. package/dist/core/foundation.d.ts +21 -0
  121. package/dist/core/foundation.js +22 -0
  122. package/dist/core/fullTheme.d.ts +5 -0
  123. package/dist/core/fullTheme.js +9 -0
  124. package/dist/core/iconNodes.generated.d.ts +7 -0
  125. package/dist/core/iconNodes.generated.js +5 -0
  126. package/dist/core/lucideIcons.d.ts +5 -0
  127. package/dist/core/lucideIcons.js +7 -0
  128. package/dist/core/messages.d.ts +111 -0
  129. package/dist/core/messages.js +134 -0
  130. package/dist/core/pointer.d.ts +17 -0
  131. package/dist/core/pointer.js +104 -0
  132. package/dist/core/registry.d.ts +12 -0
  133. package/dist/core/registry.js +114 -0
  134. package/dist/core/runtime.d.ts +8 -0
  135. package/dist/core/runtime.js +6 -0
  136. package/dist/core/state.d.ts +73 -0
  137. package/dist/core/state.js +274 -0
  138. package/dist/core/surfaceMotion.d.ts +5 -0
  139. package/dist/core/surfaceMotion.js +46 -0
  140. package/dist/core/theme.d.ts +5 -0
  141. package/dist/core/theme.js +7 -0
  142. package/dist/core/useSurfaceImage.d.ts +20 -0
  143. package/dist/core/useSurfaceImage.js +65 -0
  144. package/dist/graph/RelationshipGraph.d.ts +14 -0
  145. package/dist/graph/RelationshipGraph.js +206 -0
  146. package/dist/graph/model.d.ts +20 -0
  147. package/dist/graph/model.js +36 -0
  148. package/dist/graph/program/assets.d.ts +3 -0
  149. package/dist/graph/program/assets.js +3 -0
  150. package/dist/graph/program/bridge.d.ts +6 -0
  151. package/dist/graph/program/bridge.generated.d.ts +136 -0
  152. package/dist/graph/program/bridge.generated.js +1 -0
  153. package/dist/graph/program/bridge.js +24 -0
  154. package/dist/graph/program/bridge.schema.json +717 -0
  155. package/dist/graph/program/bridgeValidator.cjs +2 -0
  156. package/dist/graph/program/bridgeValidator.d.cts +4 -0
  157. package/dist/graph/program/runner.d.ts +1 -0
  158. package/dist/graph/program/runner.html +2193 -0
  159. package/dist/graph/program/runner.js +157 -0
  160. package/dist/graph/program/runtime-manifest.json +1 -0
  161. package/dist/graph/program/view.d.ts +11 -0
  162. package/dist/graph/program/view.js +210 -0
  163. package/dist/graph/program/worker.d.ts +1 -0
  164. package/dist/graph/program/worker.js +2186 -0
  165. package/dist/index.d.ts +20 -0
  166. package/dist/index.js +45 -0
  167. package/dist/internal/dsh.d.ts +4 -0
  168. package/dist/internal/dsh.js +3 -0
  169. package/dist/internal/relationship-graph.d.ts +4 -0
  170. package/dist/internal/relationship-graph.js +7202 -0
  171. package/dist/page/layoutComponents.d.ts +6 -0
  172. package/dist/page/layoutComponents.js +123 -0
  173. package/dist/page/layoutGeometry.d.ts +68 -0
  174. package/dist/page/layoutGeometry.js +174 -0
  175. package/dist/page/pageFormat.d.ts +34 -0
  176. package/dist/page/pageFormat.js +119 -0
  177. package/dist/page/printFit.d.ts +81 -0
  178. package/dist/page/printFit.js +338 -0
  179. package/dist/react/index.d.ts +5 -0
  180. package/dist/react/index.js +2 -0
  181. package/dist/report/locate.d.ts +42 -0
  182. package/dist/report/locate.js +34 -0
  183. package/dist/styles/renderer.css +4580 -0
  184. package/licenses/LUCIDE-LICENSE.txt +43 -0
  185. package/licenses/QUICKJS-EMSCRIPTEN-LICENSE.txt +21 -0
  186. package/licenses/QUICKJS-VARIANT-LICENSE.txt +47 -0
  187. package/package.json +68 -3
@@ -0,0 +1,51 @@
1
+ import { type ActionOutcome } from "./actionOutcome.generated.ts";
2
+ import type { A2uiSurfaceState } from "../core/state.ts";
3
+ import type { RenderContext, ResolvedAction } from "../core/context.ts";
4
+ export type { ActionOutcome } from "./actionOutcome.generated.ts";
5
+ /** One local execution and its original input snapshot; not a persistent business receipt. */
6
+ export type ActionInvocation = Readonly<{
7
+ id: string;
8
+ action: ResolvedAction;
9
+ }>;
10
+ /** Routing is synchronous and side-effect free. Business work returns its result directly. */
11
+ export interface A2uiActionHandler {
12
+ canHandle(action: ResolvedAction): boolean;
13
+ execute(invocation: ActionInvocation): ActionOutcome | Promise<ActionOutcome>;
14
+ canQuery?(invocation: ActionInvocation): boolean;
15
+ query?(invocation: ActionInvocation): ActionOutcome | Promise<ActionOutcome>;
16
+ }
17
+ export type ActionExecution = {
18
+ key: string;
19
+ scope: string;
20
+ action: ResolvedAction;
21
+ pending: boolean;
22
+ querying: boolean;
23
+ outcome?: ActionOutcome;
24
+ invocation: ActionInvocation;
25
+ handler?: A2uiActionHandler;
26
+ queryable: boolean;
27
+ };
28
+ /** Validate against owner-generated shapes; neither truthy values nor HTTP success confirm completion. */
29
+ export declare function readActionOutcome(value: unknown): ActionOutcome;
30
+ /** Artifact provenance survives input echoes; lower-level states invalidate conservatively. */
31
+ export declare function actionRevisionOf(state: A2uiSurfaceState | null): string;
32
+ /**
33
+ * Whether a whole answer is still being written: its data model's
34
+ * `/runtimeState/answerState` is "streaming" from createSurface until the last message.
35
+ * A block's own `streaming` flag cannot say this: there are moments between blocks, and
36
+ * while the last tokens arrive, when no block is open although the answer is not done.
37
+ */
38
+ export declare function surfaceIsStreaming(state: A2uiSurfaceState | null): boolean;
39
+ export declare function actionScope(componentId: string, itemPointer: string | null): string;
40
+ /** One mounted revision owns these local execution tokens, never business invocation IDs. */
41
+ export declare function createActionSession(handler: () => A2uiActionHandler | undefined, changed: () => void): {
42
+ records: Map<string, ActionExecution>;
43
+ blocked: (action: ResolvedAction, context?: RenderContext) => boolean;
44
+ hostAccepts(action: ResolvedAction): boolean;
45
+ pending(action: ResolvedAction, context?: RenderContext): boolean;
46
+ dispose(): void;
47
+ activate(): void;
48
+ dispatch(action: ResolvedAction, context: RenderContext): void;
49
+ recover(key: string): void;
50
+ };
51
+ export type ActionSession = ReturnType<typeof createActionSession>;
@@ -0,0 +1,211 @@
1
+ import { actionOutcomeShapes } from "./actionOutcome.generated.js";
2
+ import { isRecord } from "../core/messages.js";
3
+ import { isDefaultOpenableLinkUrl, validLinkUrl } from "../answer/hostValues.js";
4
+ /** Validate against owner-generated shapes; neither truthy values nor HTTP success confirm completion. */
5
+ export function readActionOutcome(value) {
6
+ try {
7
+ if (!isRecord(value))
8
+ return { status: "unknown" };
9
+ const valid = actionOutcomeShapes.some(shape => {
10
+ const properties = shape;
11
+ return Object.keys(value).length === Object.keys(properties).length
12
+ && Object.entries(properties).every(([key, rule]) => {
13
+ const entry = value[key];
14
+ return typeof entry === rule.type
15
+ && (rule.const === undefined || entry === rule.const)
16
+ && (typeof entry !== "string" || ((rule.minLength === undefined || [...entry].length >= rule.minLength)
17
+ && (rule.maxLength === undefined || [...entry].length <= rule.maxLength)));
18
+ });
19
+ });
20
+ return valid ? structuredClone(value) : { status: "unknown" };
21
+ }
22
+ catch {
23
+ return { status: "unknown" };
24
+ }
25
+ }
26
+ /** Artifact provenance survives input echoes; lower-level states invalidate conservatively. */
27
+ export function actionRevisionOf(state) {
28
+ if (!state)
29
+ return "null";
30
+ return JSON.stringify([state.sourceRevision, state.surfaceId, state.deleted, state.components,
31
+ state.rendererHints, state.releaseManifest, state.sourceRevision === undefined ? state.dataModel : null]);
32
+ }
33
+ /**
34
+ * Whether a whole answer is still being written: its data model's
35
+ * `/runtimeState/answerState` is "streaming" from createSurface until the last message.
36
+ * A block's own `streaming` flag cannot say this: there are moments between blocks, and
37
+ * while the last tokens arrive, when no block is open although the answer is not done.
38
+ */
39
+ export function surfaceIsStreaming(state) {
40
+ const runtime = isRecord(state?.dataModel) ? state.dataModel.runtimeState : undefined;
41
+ return isRecord(runtime) && runtime.answerState === "streaming";
42
+ }
43
+ export function actionScope(componentId, itemPointer) {
44
+ return JSON.stringify([componentId, itemPointer]);
45
+ }
46
+ /** JSON with object keys sorted, so equal payloads give equal text. */
47
+ function canonicalJson(value) {
48
+ if (Array.isArray(value))
49
+ return `[${value.map(canonicalJson).join(",")}]`;
50
+ if (isRecord(value)) {
51
+ return `{${Object.keys(value).sort().map(key => `${JSON.stringify(key)}:${canonicalJson(value[key])}`).join(",")}}`;
52
+ }
53
+ return JSON.stringify(value ?? null);
54
+ }
55
+ /**
56
+ * An action's state belongs to its component and, in a repeated row, to that row's item. A
57
+ * list row names its item with `itemId`. A composed row has none, so its item is named by the
58
+ * action's own payload, which the Host binds to the item's fields: after a refresh reorders the
59
+ * rows, a pending or failed state stays with the same item rather than the same position.
60
+ */
61
+ function keyOf(action, context) {
62
+ const rowItem = action.itemId === undefined && context?.composedRow && Object.keys(action.payload).length
63
+ ? canonicalJson(action.payload)
64
+ : null;
65
+ return JSON.stringify([action.componentId, action.itemId ?? rowItem, action.kind, action.name, action.actionKey ?? null]);
66
+ }
67
+ function invocationId() {
68
+ // randomUUID is restricted to secure contexts; getRandomValues also supports HTTP Hosts.
69
+ const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
70
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
71
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
72
+ const hex = [...bytes].map(value => value.toString(16).padStart(2, "0")).join("");
73
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
74
+ }
75
+ function feedbackScope(action, context) {
76
+ if (action.metadata.loadingBehavior === "surface")
77
+ return "surface";
78
+ if (action.metadata.loadingBehavior === "container") {
79
+ const id = [action.componentId, ...context.ancestors.slice().reverse()].find(id => /^(lp\.(form|card)|LP(Form|Card)_Default)$/.test(context.state.components[id]?.component ?? ""));
80
+ return id ? context.actionScopes?.[id] ?? actionScope(id, context.binding.itemPointer) : "surface";
81
+ }
82
+ return actionScope(action.componentId, context.binding.itemPointer);
83
+ }
84
+ const defaultOpenSchemes = new Set(["http:", "https:", "mailto:", "tel:"]);
85
+ /** Open an `openUrl` target the Host left to the renderer; false when it cannot be opened. */
86
+ function openUrlByDefault(action) {
87
+ if (action.kind !== "functionCall" || action.name !== "openUrl")
88
+ return false;
89
+ const safeUrl = typeof action.payload.url === "string" ? validLinkUrl(action.payload.url) : null;
90
+ if (!safeUrl || !isDefaultOpenableLinkUrl(safeUrl))
91
+ return false;
92
+ let parsed;
93
+ try {
94
+ parsed = new URL(safeUrl);
95
+ }
96
+ catch {
97
+ return false;
98
+ }
99
+ if (!defaultOpenSchemes.has(parsed.protocol))
100
+ return false;
101
+ const activation = globalThis.navigator?.userActivation;
102
+ if (activation && !activation.isActive)
103
+ return false;
104
+ const isHttp = parsed.protocol === "http:" || parsed.protocol === "https:";
105
+ const opened = isHttp
106
+ ? globalThis.window?.open(parsed.href, "_blank", "noopener,noreferrer")
107
+ : globalThis.window?.open(parsed.href, "_self");
108
+ // With `noopener` most browsers return null even when the tab opened.
109
+ return opened !== undefined;
110
+ }
111
+ /** One mounted revision owns these local execution tokens, never business invocation IDs. */
112
+ export function createActionSession(handler, changed) {
113
+ const records = new Map();
114
+ let active = true;
115
+ const publish = () => { if (active)
116
+ changed(); };
117
+ const settle = (record, result) => {
118
+ if (!active || records.get(record.key) !== record)
119
+ return;
120
+ record.pending = false;
121
+ record.querying = false;
122
+ record.outcome = readActionOutcome(result);
123
+ publish();
124
+ };
125
+ const blocked = (action, context) => {
126
+ const record = records.get(keyOf(action, context));
127
+ return Boolean(record && (record.pending || record.querying || record.outcome?.status === "unknown"
128
+ || (record.outcome?.status === "failed" && !record.outcome.retryable)));
129
+ };
130
+ return {
131
+ records,
132
+ blocked,
133
+ hostAccepts(action) {
134
+ try {
135
+ const accepted = handler()?.canHandle(structuredClone(action));
136
+ if (accepted !== undefined && typeof accepted !== "boolean") {
137
+ void Promise.resolve(accepted).catch(() => undefined);
138
+ }
139
+ return accepted === true;
140
+ }
141
+ catch {
142
+ return false;
143
+ }
144
+ },
145
+ pending(action, context) { const record = records.get(keyOf(action, context)); return Boolean(record?.pending || record?.querying); },
146
+ dispose() { active = false; },
147
+ activate() { active = true; },
148
+ dispatch(action, context) {
149
+ if (!active || blocked(action, context))
150
+ return;
151
+ if (action.kind === "functionCall" && action.name === "openUrl" && validLinkUrl(action.payload.url) === null) {
152
+ return;
153
+ }
154
+ const record = {
155
+ key: keyOf(action, context), scope: feedbackScope(action, context), action: structuredClone(action), pending: true, querying: false,
156
+ invocation: Object.freeze({ id: invocationId(), action: structuredClone(action) }), queryable: false,
157
+ };
158
+ // Reserve before invoking customer code, including synchronous re-entry.
159
+ records.set(record.key, record);
160
+ publish();
161
+ try {
162
+ const host = handler();
163
+ const accepted = host ? host.canHandle(record.action) : false;
164
+ if (accepted === false && openUrlByDefault(record.action)) {
165
+ // `openUrl` is a client function: when the Host does not take it, the renderer opens
166
+ // the link itself, inside the same user gesture (ADR 0004 §7). A link keeps no state
167
+ // afterwards and shows no message: it can be opened again like any link.
168
+ records.delete(record.key);
169
+ publish();
170
+ }
171
+ else if (accepted === false) {
172
+ settle(record, { status: "failed", code: "action_unavailable", message: "This action is unavailable in this app.", retryable: false });
173
+ }
174
+ else if (accepted === true && host) {
175
+ // Keep this Host and invocation even if the mounted view receives another handler.
176
+ record.handler = host;
177
+ const queryable = host.canQuery?.(record.invocation) ?? false;
178
+ if (typeof queryable !== "boolean" || (queryable && typeof host.query !== "function")) {
179
+ void Promise.resolve(queryable).catch(() => undefined);
180
+ settle(record, { status: "unknown" });
181
+ return;
182
+ }
183
+ record.queryable = queryable;
184
+ // Invoke before returning from the trusted gesture; never defer routing until completion.
185
+ void Promise.resolve(host.execute(record.invocation)).then(value => settle(record, value), () => settle(record, { status: "unknown" }));
186
+ }
187
+ else {
188
+ // An async or malformed routing decision cannot trigger a fallback or business execution.
189
+ void Promise.resolve(accepted).catch(() => undefined);
190
+ settle(record, { status: "unknown" });
191
+ }
192
+ }
193
+ catch {
194
+ settle(record, { status: "unknown" });
195
+ }
196
+ },
197
+ recover(key) {
198
+ const record = records.get(key);
199
+ if (!active || !record || record.querying || record.outcome?.status !== "unknown" || !record.queryable || !record.handler?.query)
200
+ return;
201
+ record.querying = true;
202
+ publish();
203
+ try {
204
+ void Promise.resolve(record.handler.query(record.invocation)).then(value => settle(record, value), () => settle(record, { status: "unknown" }));
205
+ }
206
+ catch {
207
+ settle(record, { status: "unknown" });
208
+ }
209
+ },
210
+ };
211
+ }
@@ -0,0 +1,64 @@
1
+ export type ActionOutcome = {
2
+ status: "succeeded";
3
+ message: string;
4
+ } | {
5
+ status: "failed";
6
+ code: string;
7
+ message: string;
8
+ retryable: boolean;
9
+ } | {
10
+ status: "cancelled";
11
+ } | {
12
+ status: "unknown";
13
+ };
14
+ export declare const actionOutcomeShapes: readonly [{
15
+ readonly status: {
16
+ readonly const: "succeeded";
17
+ readonly enum: readonly ["succeeded"];
18
+ readonly title: "Status";
19
+ readonly type: "string";
20
+ };
21
+ readonly message: {
22
+ readonly maxLength: 1000;
23
+ readonly minLength: 1;
24
+ readonly title: "Message";
25
+ readonly type: "string";
26
+ };
27
+ }, {
28
+ readonly status: {
29
+ readonly const: "failed";
30
+ readonly enum: readonly ["failed"];
31
+ readonly title: "Status";
32
+ readonly type: "string";
33
+ };
34
+ readonly code: {
35
+ readonly maxLength: 120;
36
+ readonly minLength: 1;
37
+ readonly title: "Code";
38
+ readonly type: "string";
39
+ };
40
+ readonly message: {
41
+ readonly maxLength: 1000;
42
+ readonly minLength: 1;
43
+ readonly title: "Message";
44
+ readonly type: "string";
45
+ };
46
+ readonly retryable: {
47
+ readonly title: "Retryable";
48
+ readonly type: "boolean";
49
+ };
50
+ }, {
51
+ readonly status: {
52
+ readonly const: "cancelled";
53
+ readonly enum: readonly ["cancelled"];
54
+ readonly title: "Status";
55
+ readonly type: "string";
56
+ };
57
+ }, {
58
+ readonly status: {
59
+ readonly const: "unknown";
60
+ readonly enum: readonly ["unknown"];
61
+ readonly title: "Status";
62
+ readonly type: "string";
63
+ };
64
+ }];
@@ -0,0 +1,64 @@
1
+ export const actionOutcomeShapes = [
2
+ {
3
+ "status": {
4
+ "const": "succeeded",
5
+ "enum": [
6
+ "succeeded"
7
+ ],
8
+ "title": "Status",
9
+ "type": "string"
10
+ },
11
+ "message": {
12
+ "maxLength": 1000,
13
+ "minLength": 1,
14
+ "title": "Message",
15
+ "type": "string"
16
+ }
17
+ },
18
+ {
19
+ "status": {
20
+ "const": "failed",
21
+ "enum": [
22
+ "failed"
23
+ ],
24
+ "title": "Status",
25
+ "type": "string"
26
+ },
27
+ "code": {
28
+ "maxLength": 120,
29
+ "minLength": 1,
30
+ "title": "Code",
31
+ "type": "string"
32
+ },
33
+ "message": {
34
+ "maxLength": 1000,
35
+ "minLength": 1,
36
+ "title": "Message",
37
+ "type": "string"
38
+ },
39
+ "retryable": {
40
+ "title": "Retryable",
41
+ "type": "boolean"
42
+ }
43
+ },
44
+ {
45
+ "status": {
46
+ "const": "cancelled",
47
+ "enum": [
48
+ "cancelled"
49
+ ],
50
+ "title": "Status",
51
+ "type": "string"
52
+ }
53
+ },
54
+ {
55
+ "status": {
56
+ "const": "unknown",
57
+ "enum": [
58
+ "unknown"
59
+ ],
60
+ "title": "Status",
61
+ "type": "string"
62
+ }
63
+ }
64
+ ];
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The snapshot publication policy for a calculation surface, kept outside React so the
3
+ * equality-before-setter rule is testable without a renderer.
4
+ *
5
+ * React 18 keeps an equal updater queued and can replay it against an older base state, which
6
+ * schedules another commit — the loop that made typing into a surface with calculations take the
7
+ * page down. Comparing *before* calling the setter is what breaks that loop.
8
+ */
9
+ export interface CalculationSnapshot {
10
+ surfaceId: string | null;
11
+ snapshot: string;
12
+ }
13
+ /** Returns `previous` itself when nothing changed, so the caller can skip the setter. */
14
+ export declare function calculationSnapshotFor(previous: CalculationSnapshot, surfaceId: string | null, snapshot: string): CalculationSnapshot;
@@ -0,0 +1,6 @@
1
+ /** Returns `previous` itself when nothing changed, so the caller can skip the setter. */
2
+ export function calculationSnapshotFor(previous, surfaceId, snapshot) {
3
+ return previous.surfaceId === surfaceId && previous.snapshot === snapshot
4
+ ? previous
5
+ : { surfaceId, snapshot };
6
+ }
@@ -0,0 +1,20 @@
1
+ /** A failure is deliberately separate from JSON null and from a valid zero amount. */
2
+ export type CalculationFailure = {
3
+ kind: "calculation-error";
4
+ code: "invalid_integer" | "invalid_input" | "division_by_zero" | "calculation_overflow" | "calculation_budget" | "invalid_calculation" | "unsupported_currency";
5
+ path?: string;
6
+ message?: string;
7
+ };
8
+ export type CalculationRead = {
9
+ value: unknown;
10
+ } | {
11
+ error: CalculationFailure;
12
+ };
13
+ export type CalculationSession = ReturnType<typeof createCalculationSession>;
14
+ export declare function isCalculationCall(value: unknown): boolean;
15
+ export declare function isCalculationFailure(value: unknown): value is CalculationFailure;
16
+ export declare function calculationMessage(error: CalculationFailure, locale?: unknown): string;
17
+ /** One input snapshot owns this budget and cache. A new edit or Host update gets a new session. */
18
+ export declare function createCalculationSession(read: (path: string, itemPointer: string | null) => CalculationRead): {
19
+ resolve(value: unknown, itemPointer?: string | null, numeric?: boolean): string | number | CalculationFailure;
20
+ };
@@ -0,0 +1,73 @@
1
+ export declare const calculationSpec: {
2
+ readonly contract_version: "1.0";
3
+ readonly catalog: {
4
+ readonly id: "https://littlepress.ai/catalogs/calculation-functions";
5
+ readonly version: "1.0";
6
+ };
7
+ readonly capability: "local-calculation-v1";
8
+ readonly limits: {
9
+ readonly max_nodes: 64;
10
+ readonly max_depth: 8;
11
+ readonly max_calls_per_evaluation: 1024;
12
+ readonly max_safe_integer: 9007199254740991;
13
+ };
14
+ readonly rounding: "nearest-half-away-from-zero";
15
+ readonly functions: readonly [{
16
+ readonly name: "parseInteger";
17
+ readonly arguments: {
18
+ readonly value: "integer-source";
19
+ };
20
+ readonly return_type: "integer";
21
+ }, {
22
+ readonly name: "add";
23
+ readonly arguments: {
24
+ readonly left: "rational";
25
+ readonly right: "rational";
26
+ };
27
+ readonly return_type: "rational";
28
+ }, {
29
+ readonly name: "subtract";
30
+ readonly arguments: {
31
+ readonly left: "rational";
32
+ readonly right: "rational";
33
+ };
34
+ readonly return_type: "rational";
35
+ }, {
36
+ readonly name: "multiply";
37
+ readonly arguments: {
38
+ readonly left: "rational";
39
+ readonly right: "rational";
40
+ };
41
+ readonly return_type: "rational";
42
+ }, {
43
+ readonly name: "divide";
44
+ readonly arguments: {
45
+ readonly left: "rational";
46
+ readonly right: "rational";
47
+ };
48
+ readonly return_type: "rational";
49
+ }, {
50
+ readonly name: "roundMinor";
51
+ readonly arguments: {
52
+ readonly value: "rational";
53
+ };
54
+ readonly return_type: "integer";
55
+ }, {
56
+ readonly name: "formatMoney";
57
+ readonly arguments: {
58
+ readonly minor: "rational";
59
+ readonly currency: "currency";
60
+ };
61
+ readonly return_type: "string";
62
+ }];
63
+ readonly currencies: {
64
+ readonly CNY: {
65
+ readonly minor_digits: 2;
66
+ };
67
+ };
68
+ readonly literal_arguments: {
69
+ readonly divide: {
70
+ readonly right: readonly [100, 10000];
71
+ };
72
+ };
73
+ };
@@ -0,0 +1,85 @@
1
+ // Generated from contracts/catalog/calculation-functions.json. Do not edit by hand.
2
+ export const calculationSpec = {
3
+ "contract_version": "1.0",
4
+ "catalog": {
5
+ "id": "https://littlepress.ai/catalogs/calculation-functions",
6
+ "version": "1.0"
7
+ },
8
+ "capability": "local-calculation-v1",
9
+ "limits": {
10
+ "max_nodes": 64,
11
+ "max_depth": 8,
12
+ "max_calls_per_evaluation": 1024,
13
+ "max_safe_integer": 9007199254740991
14
+ },
15
+ "rounding": "nearest-half-away-from-zero",
16
+ "functions": [
17
+ {
18
+ "name": "parseInteger",
19
+ "arguments": {
20
+ "value": "integer-source"
21
+ },
22
+ "return_type": "integer"
23
+ },
24
+ {
25
+ "name": "add",
26
+ "arguments": {
27
+ "left": "rational",
28
+ "right": "rational"
29
+ },
30
+ "return_type": "rational"
31
+ },
32
+ {
33
+ "name": "subtract",
34
+ "arguments": {
35
+ "left": "rational",
36
+ "right": "rational"
37
+ },
38
+ "return_type": "rational"
39
+ },
40
+ {
41
+ "name": "multiply",
42
+ "arguments": {
43
+ "left": "rational",
44
+ "right": "rational"
45
+ },
46
+ "return_type": "rational"
47
+ },
48
+ {
49
+ "name": "divide",
50
+ "arguments": {
51
+ "left": "rational",
52
+ "right": "rational"
53
+ },
54
+ "return_type": "rational"
55
+ },
56
+ {
57
+ "name": "roundMinor",
58
+ "arguments": {
59
+ "value": "rational"
60
+ },
61
+ "return_type": "integer"
62
+ },
63
+ {
64
+ "name": "formatMoney",
65
+ "arguments": {
66
+ "minor": "rational",
67
+ "currency": "currency"
68
+ },
69
+ "return_type": "string"
70
+ }
71
+ ],
72
+ "currencies": {
73
+ "CNY": {
74
+ "minor_digits": 2
75
+ }
76
+ },
77
+ "literal_arguments": {
78
+ "divide": {
79
+ "right": [
80
+ 100,
81
+ 10000
82
+ ]
83
+ }
84
+ }
85
+ };