@contractspec/lib.surface-runtime 0.3.0 → 0.3.3
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/dist/adapters/ai-sdk-stub.js +1 -6
- package/dist/adapters/blocknote-stub.js +3 -8
- package/dist/adapters/dnd-kit-adapter.js +6 -11
- package/dist/adapters/dnd-kit-stub.js +1 -6
- package/dist/adapters/floating-ui-stub.js +4 -9
- package/dist/adapters/index.js +7 -176
- package/dist/adapters/interfaces.js +0 -1
- package/dist/adapters/motion-stub.js +2 -7
- package/dist/adapters/resizable-panels-stub.js +7 -12
- package/dist/browser/adapters/ai-sdk-stub.js +1 -5
- package/dist/browser/adapters/blocknote-stub.js +3 -7
- package/dist/browser/adapters/dnd-kit-adapter.js +6 -10
- package/dist/browser/adapters/dnd-kit-stub.js +1 -5
- package/dist/browser/adapters/floating-ui-stub.js +4 -8
- package/dist/browser/adapters/index.js +7 -175
- package/dist/browser/adapters/motion-stub.js +2 -6
- package/dist/browser/adapters/resizable-panels-stub.js +5 -9
- package/dist/browser/evals/golden-harness.js +4 -825
- package/dist/browser/examples/pm-workbench.bundle.js +2 -316
- package/dist/browser/i18n/catalogs/en.js +1 -5
- package/dist/browser/i18n/catalogs/es.js +1 -5
- package/dist/browser/i18n/catalogs/fr.js +1 -5
- package/dist/browser/i18n/catalogs/index.js +3 -133
- package/dist/browser/i18n/index.js +3 -168
- package/dist/browser/i18n/keys.js +1 -5
- package/dist/browser/i18n/messages.js +6 -137
- package/dist/browser/index.js +4 -2470
- package/dist/browser/react/BundleProvider.js +12 -19
- package/dist/browser/react/BundleRenderer.js +15 -662
- package/dist/browser/react/OverlayConflictResolver.js +12 -180
- package/dist/browser/react/PatchProposalCard.js +9 -177
- package/dist/browser/react/RegionRenderer.js +14 -60
- package/dist/browser/react/SlotRenderer.js +15 -58
- package/dist/browser/react/WidgetPalette.js +6 -10
- package/dist/browser/react/index.js +14 -794
- package/dist/browser/runtime/apply-surface-patch.js +3 -208
- package/dist/browser/runtime/audit-events.js +8 -19
- package/dist/browser/runtime/build-context.js +2 -6
- package/dist/browser/runtime/extension-registry.js +3 -9
- package/dist/browser/runtime/field-renderer-registry.js +5 -11
- package/dist/browser/runtime/index.js +54 -1487
- package/dist/browser/runtime/overlay-alignment.js +8 -19
- package/dist/browser/runtime/overlay-signer.js +2 -7
- package/dist/browser/runtime/override-store.js +3 -9
- package/dist/browser/runtime/planner-prompt.js +2 -6
- package/dist/browser/runtime/planner-tools.js +3 -9
- package/dist/browser/runtime/policy-eval.js +3 -135
- package/dist/browser/runtime/preference-adapter.js +2 -62
- package/dist/browser/runtime/resolve-bundle.js +10 -447
- package/dist/browser/runtime/resolve-preferences.js +3 -7
- package/dist/browser/runtime/rollback.js +2 -325
- package/dist/browser/runtime/widget-registry.js +1 -5
- package/dist/browser/spec/define-module-bundle.js +2 -67
- package/dist/browser/spec/index.js +9 -317
- package/dist/browser/spec/validate-bundle.js +3 -8
- package/dist/browser/spec/validate-surface-patch.js +5 -11
- package/dist/browser/telemetry/index.js +4 -16
- package/dist/browser/telemetry/surface-metrics.js +7 -16
- package/dist/evals/golden-context.js +0 -1
- package/dist/evals/golden-harness.js +4 -826
- package/dist/examples/pm-workbench.bundle.js +2 -317
- package/dist/i18n/catalogs/en.js +1 -6
- package/dist/i18n/catalogs/es.js +1 -6
- package/dist/i18n/catalogs/fr.js +1 -6
- package/dist/i18n/catalogs/index.js +3 -134
- package/dist/i18n/index.js +3 -169
- package/dist/i18n/keys.js +1 -6
- package/dist/i18n/messages.js +6 -138
- package/dist/index.js +4 -2471
- package/dist/node/adapters/ai-sdk-stub.js +1 -5
- package/dist/node/adapters/blocknote-stub.js +3 -7
- package/dist/node/adapters/dnd-kit-adapter.js +6 -10
- package/dist/node/adapters/dnd-kit-stub.js +1 -5
- package/dist/node/adapters/floating-ui-stub.js +4 -8
- package/dist/node/adapters/index.js +7 -175
- package/dist/node/adapters/motion-stub.js +2 -6
- package/dist/node/adapters/resizable-panels-stub.js +5 -9
- package/dist/node/evals/golden-harness.js +4 -825
- package/dist/node/examples/pm-workbench.bundle.js +2 -316
- package/dist/node/i18n/catalogs/en.js +1 -5
- package/dist/node/i18n/catalogs/es.js +1 -5
- package/dist/node/i18n/catalogs/fr.js +1 -5
- package/dist/node/i18n/catalogs/index.js +3 -133
- package/dist/node/i18n/index.js +3 -168
- package/dist/node/i18n/keys.js +1 -5
- package/dist/node/i18n/messages.js +6 -137
- package/dist/node/index.js +4 -2470
- package/dist/node/react/BundleProvider.js +12 -19
- package/dist/node/react/BundleRenderer.js +15 -662
- package/dist/node/react/OverlayConflictResolver.js +12 -180
- package/dist/node/react/PatchProposalCard.js +9 -177
- package/dist/node/react/RegionRenderer.js +14 -60
- package/dist/node/react/SlotRenderer.js +15 -58
- package/dist/node/react/WidgetPalette.js +6 -10
- package/dist/node/react/index.js +14 -794
- package/dist/node/runtime/apply-surface-patch.js +3 -208
- package/dist/node/runtime/audit-events.js +8 -19
- package/dist/node/runtime/build-context.js +2 -6
- package/dist/node/runtime/extension-registry.js +3 -9
- package/dist/node/runtime/field-renderer-registry.js +5 -11
- package/dist/node/runtime/index.js +54 -1487
- package/dist/node/runtime/overlay-alignment.js +8 -19
- package/dist/node/runtime/overlay-signer.js +2 -7
- package/dist/node/runtime/override-store.js +3 -9
- package/dist/node/runtime/planner-prompt.js +2 -6
- package/dist/node/runtime/planner-tools.js +3 -9
- package/dist/node/runtime/policy-eval.js +3 -135
- package/dist/node/runtime/preference-adapter.js +2 -62
- package/dist/node/runtime/resolve-bundle.js +10 -447
- package/dist/node/runtime/resolve-preferences.js +3 -7
- package/dist/node/runtime/rollback.js +2 -325
- package/dist/node/runtime/widget-registry.js +1 -5
- package/dist/node/spec/define-module-bundle.js +2 -67
- package/dist/node/spec/index.js +9 -317
- package/dist/node/spec/validate-bundle.js +3 -8
- package/dist/node/spec/validate-surface-patch.js +5 -11
- package/dist/node/telemetry/index.js +4 -16
- package/dist/node/telemetry/surface-metrics.js +7 -16
- package/dist/react/BundleProvider.js +12 -20
- package/dist/react/BundleRenderer.js +15 -663
- package/dist/react/OverlayConflictResolver.js +12 -181
- package/dist/react/PatchProposalCard.js +9 -178
- package/dist/react/RegionRenderer.js +14 -61
- package/dist/react/SlotRenderer.js +15 -59
- package/dist/react/WidgetPalette.js +6 -11
- package/dist/react/index.js +14 -795
- package/dist/runtime/apply-surface-patch.js +3 -209
- package/dist/runtime/audit-events.js +8 -20
- package/dist/runtime/build-context.js +2 -7
- package/dist/runtime/extension-registry.js +3 -10
- package/dist/runtime/field-renderer-registry.js +9 -16
- package/dist/runtime/index.js +54 -1488
- package/dist/runtime/overlay-alignment.js +8 -20
- package/dist/runtime/overlay-signer.js +2 -8
- package/dist/runtime/override-store.js +3 -10
- package/dist/runtime/planner-prompt.js +11 -9
- package/dist/runtime/planner-tools.js +4 -11
- package/dist/runtime/policy-eval.js +3 -136
- package/dist/runtime/preference-adapter.js +2 -63
- package/dist/runtime/resolve-bundle.js +10 -448
- package/dist/runtime/resolve-preferences.js +3 -8
- package/dist/runtime/rollback.js +2 -326
- package/dist/runtime/widget-registry.js +1 -6
- package/dist/spec/define-module-bundle.js +2 -68
- package/dist/spec/index.js +9 -318
- package/dist/spec/types.js +0 -1
- package/dist/spec/validate-bundle.js +3 -9
- package/dist/spec/validate-surface-patch.js +5 -12
- package/dist/spec/verification-snapshot-types.js +0 -1
- package/dist/telemetry/index.js +4 -17
- package/dist/telemetry/surface-metrics.js +7 -17
- package/package.json +7 -7
|
@@ -1,444 +1,10 @@
|
|
|
1
|
-
// src/telemetry/surface-metrics.ts
|
|
2
|
-
import {
|
|
3
|
-
createCounter,
|
|
4
|
-
createHistogram
|
|
5
|
-
} from "@contractspec/lib.observability/metrics";
|
|
6
|
-
var METER_NAME = "@contractspec/lib.surface-runtime";
|
|
7
|
-
var resolutionDurationMs = createHistogram("bundle_surface_resolution_duration_ms", "Time to resolve bundle spec to surface plan in milliseconds", METER_NAME);
|
|
8
|
-
var patchAcceptanceCounter = createCounter("bundle_surface_patch_acceptance_total", "Total AI patch proposals accepted by user", METER_NAME);
|
|
9
|
-
var patchRejectionCounter = createCounter("bundle_surface_patch_rejection_total", "Total AI patch proposals rejected by user", METER_NAME);
|
|
10
|
-
var policyDenialCounter = createCounter("bundle_surface_policy_denial_total", "Total policy denials (actions denied by policy)", METER_NAME);
|
|
11
|
-
var surfaceFallbackCounter = createCounter("bundle_surface_fallback_total", "Total surface or layout fallbacks during resolution", METER_NAME);
|
|
12
|
-
var missingRendererCounter = createCounter("bundle_surface_renderer_missing_total", "Total slots with no renderer for requested node kind", METER_NAME);
|
|
13
|
-
// src/runtime/resolve-preferences.ts
|
|
14
|
-
var DIMENSION_KEYS = [
|
|
15
|
-
"guidance",
|
|
16
|
-
"density",
|
|
17
|
-
"dataDepth",
|
|
18
|
-
"control",
|
|
19
|
-
"media",
|
|
20
|
-
"pace",
|
|
21
|
-
"narrative"
|
|
22
|
-
];
|
|
23
|
-
var SCOPE_ORDER = [
|
|
24
|
-
"user",
|
|
25
|
-
"workspace-user",
|
|
26
|
-
"bundle",
|
|
27
|
-
"surface",
|
|
28
|
-
"entity",
|
|
29
|
-
"session"
|
|
30
|
-
];
|
|
31
|
-
function mergeByScope(layers, ctx) {
|
|
32
|
-
const merged = { ...ctx.preferences };
|
|
33
|
-
const sourceByDimension = {};
|
|
34
|
-
for (const scope of SCOPE_ORDER) {
|
|
35
|
-
const layer = layers[scope];
|
|
36
|
-
if (!layer)
|
|
37
|
-
continue;
|
|
38
|
-
for (const dim of DIMENSION_KEYS) {
|
|
39
|
-
const val = layer[dim];
|
|
40
|
-
if (val !== undefined) {
|
|
41
|
-
merged[dim] = val;
|
|
42
|
-
sourceByDimension[dim] = scope;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
for (const dim of DIMENSION_KEYS) {
|
|
47
|
-
if (sourceByDimension[dim] === undefined) {
|
|
48
|
-
sourceByDimension[dim] = "session";
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return { canonical: merged, sourceByDimension };
|
|
52
|
-
}
|
|
53
|
-
function resolveConstraints(_canonical, _ctx) {
|
|
54
|
-
return { constrained: {}, notes: [] };
|
|
55
|
-
}
|
|
56
|
-
function resolvePreferenceProfile(ctx) {
|
|
57
|
-
const layers = {
|
|
58
|
-
session: ctx.preferences
|
|
59
|
-
};
|
|
60
|
-
const { canonical, sourceByDimension } = mergeByScope(layers, ctx);
|
|
61
|
-
const { constrained, notes } = resolveConstraints(canonical, ctx);
|
|
62
|
-
return {
|
|
63
|
-
canonical,
|
|
64
|
-
sourceByDimension,
|
|
65
|
-
constrained,
|
|
66
|
-
notes
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// src/spec/validate-surface-patch.ts
|
|
71
|
-
var VALID_OPS = [
|
|
72
|
-
"insert-node",
|
|
73
|
-
"replace-node",
|
|
74
|
-
"remove-node",
|
|
75
|
-
"move-node",
|
|
76
|
-
"resize-panel",
|
|
77
|
-
"set-layout",
|
|
78
|
-
"reveal-field",
|
|
79
|
-
"hide-field",
|
|
80
|
-
"promote-action",
|
|
81
|
-
"set-focus"
|
|
82
|
-
];
|
|
83
|
-
var VALID_NODE_KINDS = [
|
|
84
|
-
"metric-strip",
|
|
85
|
-
"data-view",
|
|
86
|
-
"entity-card",
|
|
87
|
-
"entity-header",
|
|
88
|
-
"entity-summary",
|
|
89
|
-
"entity-section",
|
|
90
|
-
"entity-field",
|
|
91
|
-
"entity-activity",
|
|
92
|
-
"entity-relations",
|
|
93
|
-
"entity-timeline",
|
|
94
|
-
"entity-comments",
|
|
95
|
-
"entity-attachments",
|
|
96
|
-
"entity-view-switcher",
|
|
97
|
-
"entity-automation-panel",
|
|
98
|
-
"rich-doc",
|
|
99
|
-
"chat-thread",
|
|
100
|
-
"assistant-panel",
|
|
101
|
-
"action-bar",
|
|
102
|
-
"timeline",
|
|
103
|
-
"board",
|
|
104
|
-
"table",
|
|
105
|
-
"calendar",
|
|
106
|
-
"form",
|
|
107
|
-
"chart",
|
|
108
|
-
"relation-graph",
|
|
109
|
-
"custom-widget"
|
|
110
|
-
];
|
|
111
|
-
function validateSurfaceNode(node, path) {
|
|
112
|
-
if (!node.nodeId || typeof node.nodeId !== "string") {
|
|
113
|
-
throw new Error(`${path}: nodeId must be a non-empty string`);
|
|
114
|
-
}
|
|
115
|
-
if (!node.kind || !VALID_NODE_KINDS.includes(node.kind)) {
|
|
116
|
-
throw new Error(`${path}: kind must be one of ${VALID_NODE_KINDS.join(", ")}`);
|
|
117
|
-
}
|
|
118
|
-
if (node.children) {
|
|
119
|
-
for (let i = 0;i < node.children.length; i++) {
|
|
120
|
-
const child = node.children[i];
|
|
121
|
-
if (child)
|
|
122
|
-
validateSurfaceNode(child, `${path}.children[${i}]`);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
function validateSurfacePatchOp(op, index) {
|
|
127
|
-
const path = `ops[${index}]`;
|
|
128
|
-
if (!op || typeof op !== "object" || !("op" in op)) {
|
|
129
|
-
throw new Error(`${path}: must be an object with op field`);
|
|
130
|
-
}
|
|
131
|
-
const opType = op.op;
|
|
132
|
-
if (!VALID_OPS.includes(opType)) {
|
|
133
|
-
throw new Error(`${path}: op must be one of ${VALID_OPS.join(", ")}`);
|
|
134
|
-
}
|
|
135
|
-
switch (op.op) {
|
|
136
|
-
case "insert-node":
|
|
137
|
-
if (!op.slotId || typeof op.slotId !== "string") {
|
|
138
|
-
throw new Error(`${path}: insert-node requires slotId string`);
|
|
139
|
-
}
|
|
140
|
-
if (!op.node) {
|
|
141
|
-
throw new Error(`${path}: insert-node requires node`);
|
|
142
|
-
}
|
|
143
|
-
validateSurfaceNode(op.node, `${path}.node`);
|
|
144
|
-
if (op.index !== undefined && typeof op.index !== "number") {
|
|
145
|
-
throw new Error(`${path}: insert-node index must be number if present`);
|
|
146
|
-
}
|
|
147
|
-
break;
|
|
148
|
-
case "replace-node":
|
|
149
|
-
if (!op.nodeId || typeof op.nodeId !== "string") {
|
|
150
|
-
throw new Error(`${path}: replace-node requires nodeId string`);
|
|
151
|
-
}
|
|
152
|
-
if (!op.node) {
|
|
153
|
-
throw new Error(`${path}: replace-node requires node`);
|
|
154
|
-
}
|
|
155
|
-
validateSurfaceNode(op.node, `${path}.node`);
|
|
156
|
-
break;
|
|
157
|
-
case "remove-node":
|
|
158
|
-
if (!op.nodeId || typeof op.nodeId !== "string") {
|
|
159
|
-
throw new Error(`${path}: remove-node requires nodeId string`);
|
|
160
|
-
}
|
|
161
|
-
break;
|
|
162
|
-
case "move-node":
|
|
163
|
-
if (!op.nodeId || typeof op.nodeId !== "string") {
|
|
164
|
-
throw new Error(`${path}: move-node requires nodeId string`);
|
|
165
|
-
}
|
|
166
|
-
if (!op.toSlotId || typeof op.toSlotId !== "string") {
|
|
167
|
-
throw new Error(`${path}: move-node requires toSlotId string`);
|
|
168
|
-
}
|
|
169
|
-
if (op.index !== undefined && typeof op.index !== "number") {
|
|
170
|
-
throw new Error(`${path}: move-node index must be number if present`);
|
|
171
|
-
}
|
|
172
|
-
break;
|
|
173
|
-
case "resize-panel":
|
|
174
|
-
if (!op.persistKey || typeof op.persistKey !== "string") {
|
|
175
|
-
throw new Error(`${path}: resize-panel requires persistKey string`);
|
|
176
|
-
}
|
|
177
|
-
if (!Array.isArray(op.sizes) || op.sizes.some((s) => typeof s !== "number")) {
|
|
178
|
-
throw new Error(`${path}: resize-panel requires sizes number[]`);
|
|
179
|
-
}
|
|
180
|
-
break;
|
|
181
|
-
case "set-layout":
|
|
182
|
-
if (!op.layoutId || typeof op.layoutId !== "string") {
|
|
183
|
-
throw new Error(`${path}: set-layout requires layoutId string`);
|
|
184
|
-
}
|
|
185
|
-
break;
|
|
186
|
-
case "reveal-field":
|
|
187
|
-
case "hide-field":
|
|
188
|
-
if (!op.fieldId || typeof op.fieldId !== "string") {
|
|
189
|
-
throw new Error(`${path}: ${op.op} requires fieldId string`);
|
|
190
|
-
}
|
|
191
|
-
break;
|
|
192
|
-
case "promote-action": {
|
|
193
|
-
if (!op.actionId || typeof op.actionId !== "string") {
|
|
194
|
-
throw new Error(`${path}: promote-action requires actionId string`);
|
|
195
|
-
}
|
|
196
|
-
const validPlacements = ["header", "inline", "context", "assistant"];
|
|
197
|
-
if (!op.placement || !validPlacements.includes(op.placement)) {
|
|
198
|
-
throw new Error(`${path}: promote-action placement must be one of ${validPlacements.join(", ")}`);
|
|
199
|
-
}
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
|
-
case "set-focus":
|
|
203
|
-
if (!op.targetId || typeof op.targetId !== "string") {
|
|
204
|
-
throw new Error(`${path}: set-focus requires targetId string`);
|
|
205
|
-
}
|
|
206
|
-
break;
|
|
207
|
-
default:
|
|
208
|
-
throw new Error(`${path}: unknown op "${opType}"`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
function validateSurfacePatch(ops) {
|
|
212
|
-
if (!Array.isArray(ops)) {
|
|
213
|
-
throw new Error("Patch ops must be an array");
|
|
214
|
-
}
|
|
215
|
-
for (let i = 0;i < ops.length; i++) {
|
|
216
|
-
const op = ops[i];
|
|
217
|
-
if (op)
|
|
218
|
-
validateSurfacePatchOp(op, i);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
function validateSurfaceNodeAgainstKinds(node, allowedKinds, path) {
|
|
222
|
-
if (!allowedKinds.includes(node.kind)) {
|
|
223
|
-
throw new Error(`${path}: kind "${node.kind}" not in allowed list [${allowedKinds.join(", ")}]`);
|
|
224
|
-
}
|
|
225
|
-
if (node.children) {
|
|
226
|
-
for (let i = 0;i < node.children.length; i++) {
|
|
227
|
-
const child = node.children[i];
|
|
228
|
-
if (child)
|
|
229
|
-
validateSurfaceNodeAgainstKinds(child, allowedKinds, `${path}.children[${i}]`);
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
function validatePatchProposal(ops, constraints) {
|
|
234
|
-
if (!Array.isArray(ops)) {
|
|
235
|
-
throw new Error("Patch ops must be an array");
|
|
236
|
-
}
|
|
237
|
-
const { allowedOps, allowedSlots, allowedNodeKinds } = constraints;
|
|
238
|
-
for (let i = 0;i < ops.length; i++) {
|
|
239
|
-
const op = ops[i];
|
|
240
|
-
if (!op)
|
|
241
|
-
continue;
|
|
242
|
-
const path = `ops[${i}]`;
|
|
243
|
-
if (!allowedOps.includes(op.op)) {
|
|
244
|
-
throw new Error(`${path}: op "${op.op}" not in allowed list [${allowedOps.join(", ")}]`);
|
|
245
|
-
}
|
|
246
|
-
switch (op.op) {
|
|
247
|
-
case "insert-node":
|
|
248
|
-
if (!allowedSlots.includes(op.slotId)) {
|
|
249
|
-
throw new Error(`${path}: slotId "${op.slotId}" not in allowed slots [${allowedSlots.join(", ")}]`);
|
|
250
|
-
}
|
|
251
|
-
if (op.node) {
|
|
252
|
-
validateSurfaceNodeAgainstKinds(op.node, allowedNodeKinds, `${path}.node`);
|
|
253
|
-
}
|
|
254
|
-
break;
|
|
255
|
-
case "move-node":
|
|
256
|
-
if (!allowedSlots.includes(op.toSlotId)) {
|
|
257
|
-
throw new Error(`${path}: toSlotId "${op.toSlotId}" not in allowed slots [${allowedSlots.join(", ")}]`);
|
|
258
|
-
}
|
|
259
|
-
break;
|
|
260
|
-
case "replace-node":
|
|
261
|
-
if (op.node) {
|
|
262
|
-
validateSurfaceNodeAgainstKinds(op.node, allowedNodeKinds, `${path}.node`);
|
|
263
|
-
}
|
|
264
|
-
break;
|
|
265
|
-
default:
|
|
266
|
-
break;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
validateSurfacePatch(ops);
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// src/runtime/apply-surface-patch.ts
|
|
273
|
-
import { Logger } from "@contractspec/lib.observability";
|
|
274
|
-
var logger = new Logger("@contractspec/lib.surface-runtime");
|
|
275
|
-
function findNode(nodes, nodeId) {
|
|
276
|
-
for (const n of nodes) {
|
|
277
|
-
if (n.nodeId === nodeId)
|
|
278
|
-
return n;
|
|
279
|
-
if (n.children) {
|
|
280
|
-
const found = findNode(n.children, nodeId);
|
|
281
|
-
if (found)
|
|
282
|
-
return found;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
function collectNodeIds(nodes) {
|
|
288
|
-
const ids = new Set;
|
|
289
|
-
for (const n of nodes) {
|
|
290
|
-
ids.add(n.nodeId);
|
|
291
|
-
if (n.children)
|
|
292
|
-
collectNodeIds(n.children).forEach((id) => ids.add(id));
|
|
293
|
-
}
|
|
294
|
-
return ids;
|
|
295
|
-
}
|
|
296
|
-
function validateOp(op, nodeIds) {
|
|
297
|
-
if (op.op === "remove-node" || op.op === "replace-node" || op.op === "move-node") {
|
|
298
|
-
if (!nodeIds.has(op.nodeId)) {
|
|
299
|
-
throw new Error(`Patch op references unknown nodeId: ${op.nodeId}`);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (op.op === "insert-node" && !op.node?.nodeId) {
|
|
303
|
-
throw new Error("insert-node requires node with nodeId");
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
function produceInverse(op, plan) {
|
|
307
|
-
switch (op.op) {
|
|
308
|
-
case "insert-node":
|
|
309
|
-
return op.node ? { op: "remove-node", nodeId: op.node.nodeId } : null;
|
|
310
|
-
case "remove-node": {
|
|
311
|
-
const node = findNode(plan.nodes, op.nodeId);
|
|
312
|
-
return node ? { op: "insert-node", slotId: "primary", node } : null;
|
|
313
|
-
}
|
|
314
|
-
case "replace-node": {
|
|
315
|
-
const prev = findNode(plan.nodes, op.nodeId);
|
|
316
|
-
return prev ? { op: "replace-node", nodeId: op.nodeId, node: prev } : null;
|
|
317
|
-
}
|
|
318
|
-
case "set-layout":
|
|
319
|
-
return { op: "set-layout", layoutId: plan.layoutId };
|
|
320
|
-
case "reveal-field":
|
|
321
|
-
return { op: "hide-field", fieldId: op.fieldId };
|
|
322
|
-
case "hide-field":
|
|
323
|
-
return { op: "reveal-field", fieldId: op.fieldId };
|
|
324
|
-
case "move-node":
|
|
325
|
-
case "resize-panel":
|
|
326
|
-
case "set-focus":
|
|
327
|
-
case "promote-action":
|
|
328
|
-
return null;
|
|
329
|
-
default:
|
|
330
|
-
return null;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
function applySurfacePatch(plan, ops) {
|
|
334
|
-
if (ops.length === 0)
|
|
335
|
-
return { plan, inverseOps: [] };
|
|
336
|
-
validateSurfacePatch(ops);
|
|
337
|
-
const nodeIds = collectNodeIds(plan.nodes);
|
|
338
|
-
for (const op of ops) {
|
|
339
|
-
validateOp(op, nodeIds);
|
|
340
|
-
}
|
|
341
|
-
let nextNodes = [...plan.nodes];
|
|
342
|
-
let nextLayoutId = plan.layoutId;
|
|
343
|
-
const inverseOps = [];
|
|
344
|
-
for (const op of ops) {
|
|
345
|
-
const inv = produceInverse(op, {
|
|
346
|
-
...plan,
|
|
347
|
-
nodes: nextNodes,
|
|
348
|
-
layoutId: nextLayoutId
|
|
349
|
-
});
|
|
350
|
-
if (inv)
|
|
351
|
-
inverseOps.unshift(inv);
|
|
352
|
-
switch (op.op) {
|
|
353
|
-
case "insert-node":
|
|
354
|
-
if (op.node)
|
|
355
|
-
nextNodes = [...nextNodes, op.node];
|
|
356
|
-
break;
|
|
357
|
-
case "remove-node":
|
|
358
|
-
nextNodes = nextNodes.filter((n) => n.nodeId !== op.nodeId);
|
|
359
|
-
break;
|
|
360
|
-
case "replace-node": {
|
|
361
|
-
const replace = (nodes) => nodes.map((n) => n.nodeId === op.nodeId ? op.node : { ...n, children: n.children ? replace(n.children) : undefined });
|
|
362
|
-
nextNodes = replace(nextNodes);
|
|
363
|
-
break;
|
|
364
|
-
}
|
|
365
|
-
case "set-layout":
|
|
366
|
-
nextLayoutId = op.layoutId;
|
|
367
|
-
break;
|
|
368
|
-
case "move-node":
|
|
369
|
-
case "resize-panel":
|
|
370
|
-
case "set-focus":
|
|
371
|
-
case "reveal-field":
|
|
372
|
-
case "hide-field":
|
|
373
|
-
case "promote-action":
|
|
374
|
-
break;
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
const result = {
|
|
378
|
-
plan: { ...plan, nodes: nextNodes, layoutId: nextLayoutId },
|
|
379
|
-
inverseOps
|
|
380
|
-
};
|
|
381
|
-
logger.info("bundle.surface.patch.applied", {
|
|
382
|
-
bundleKey: plan.bundleKey,
|
|
383
|
-
surfaceId: plan.surfaceId,
|
|
384
|
-
opCount: ops.length,
|
|
385
|
-
opTypes: [...new Set(ops.map((o) => o.op))]
|
|
386
|
-
});
|
|
387
|
-
return result;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// src/runtime/override-store.ts
|
|
391
|
-
function buildOverrideTargetKey(bundleKey, surfaceId, routeId) {
|
|
392
|
-
return routeId ? `${bundleKey}:${routeId}:${surfaceId}` : `${bundleKey}:${surfaceId}`;
|
|
393
|
-
}
|
|
394
|
-
function createOverrideStoreWithApprovalGate(store, options) {
|
|
395
|
-
const { requireApprovalForWorkspacePatches = false, requestApproval } = options;
|
|
396
|
-
return {
|
|
397
|
-
list: store.list.bind(store),
|
|
398
|
-
async save(scope, targetKey, patch, saveOptions) {
|
|
399
|
-
if (requireApprovalForWorkspacePatches && scope === "workspace" && requestApproval) {
|
|
400
|
-
const approved = await requestApproval({ scope, targetKey, patch });
|
|
401
|
-
if (!approved) {
|
|
402
|
-
throw new Error("Workspace overlay save rejected: approval required and not granted");
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
return store.save(scope, targetKey, patch, saveOptions);
|
|
406
|
-
},
|
|
407
|
-
remove: store.remove.bind(store)
|
|
408
|
-
};
|
|
409
|
-
}
|
|
410
|
-
function createInMemoryOverrideStore() {
|
|
411
|
-
const overrides = new Map;
|
|
412
|
-
function nextId() {
|
|
413
|
-
return `ov_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
414
|
-
}
|
|
415
|
-
return {
|
|
416
|
-
async list(scope, targetKey) {
|
|
417
|
-
return Array.from(overrides.values()).filter((o) => o.scope === scope && o.targetKey === targetKey);
|
|
418
|
-
},
|
|
419
|
-
async save(scope, targetKey, patch, options) {
|
|
420
|
-
const overrideId = options?.overrideId ?? nextId();
|
|
421
|
-
const stored = {
|
|
422
|
-
overrideId,
|
|
423
|
-
scope,
|
|
424
|
-
targetKey,
|
|
425
|
-
patch,
|
|
426
|
-
createdAt: new Date().toISOString(),
|
|
427
|
-
createdBy: options?.createdBy
|
|
428
|
-
};
|
|
429
|
-
overrides.set(overrideId, stored);
|
|
430
|
-
return overrideId;
|
|
431
|
-
},
|
|
432
|
-
async remove(overrideId) {
|
|
433
|
-
overrides.delete(overrideId);
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// src/runtime/resolve-bundle.ts
|
|
439
1
|
import { traceAsync } from "@contractspec/lib.observability/tracing";
|
|
440
|
-
import { Logger
|
|
441
|
-
|
|
2
|
+
import { Logger } from "@contractspec/lib.observability";
|
|
3
|
+
import { resolutionDurationMs, surfaceFallbackCounter } from "../telemetry";
|
|
4
|
+
import { resolvePreferenceProfile } from "./resolve-preferences";
|
|
5
|
+
import { applySurfacePatch } from "./apply-surface-patch";
|
|
6
|
+
import { buildOverrideTargetKey } from "./override-store";
|
|
7
|
+
const logger = new Logger("@contractspec/lib.surface-runtime");
|
|
442
8
|
function getOpTarget(op) {
|
|
443
9
|
switch (op.op) {
|
|
444
10
|
case "hide-field":
|
|
@@ -544,7 +110,7 @@ function allowFromDecision(decision) {
|
|
|
544
110
|
function generateResolutionId() {
|
|
545
111
|
return `res_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
546
112
|
}
|
|
547
|
-
|
|
113
|
+
const OVERLAY_SCOPE_ORDER = [
|
|
548
114
|
"system",
|
|
549
115
|
"workspace",
|
|
550
116
|
"team",
|
|
@@ -563,7 +129,7 @@ function buildAdaptation(ctx) {
|
|
|
563
129
|
notes
|
|
564
130
|
};
|
|
565
131
|
}
|
|
566
|
-
async function resolveBundle(spec, ctx, options) {
|
|
132
|
+
export async function resolveBundle(spec, ctx, options) {
|
|
567
133
|
return traceAsync("surface.resolveBundle", async (span) => {
|
|
568
134
|
const start = performance.now();
|
|
569
135
|
try {
|
|
@@ -577,7 +143,7 @@ async function resolveBundle(spec, ctx, options) {
|
|
|
577
143
|
span.setAttribute("bundle.key", spec.meta.key);
|
|
578
144
|
span.setAttribute("surface.id", plan.surfaceId);
|
|
579
145
|
span.setAttribute("resolution.duration_ms", durationMs);
|
|
580
|
-
|
|
146
|
+
logger.info("bundle.surface.resolved", {
|
|
581
147
|
bundleKey: spec.meta.key,
|
|
582
148
|
surfaceId: plan.surfaceId,
|
|
583
149
|
layoutId: plan.layoutId,
|
|
@@ -716,7 +282,7 @@ async function resolveBundleInternal(spec, ctx, options) {
|
|
|
716
282
|
appliedOps: ov.patch.length
|
|
717
283
|
});
|
|
718
284
|
} catch (err) {
|
|
719
|
-
|
|
285
|
+
logger.warn("bundle.overlay.apply.failed", {
|
|
720
286
|
overlayId: ov.overrideId,
|
|
721
287
|
scope,
|
|
722
288
|
error: err instanceof Error ? err.message : String(err)
|
|
@@ -762,6 +328,3 @@ function createErrorPlan(spec, ctx, err) {
|
|
|
762
328
|
locale: ctx.locale
|
|
763
329
|
};
|
|
764
330
|
}
|
|
765
|
-
export {
|
|
766
|
-
resolveBundle
|
|
767
|
-
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
var DIMENSION_KEYS = [
|
|
1
|
+
const DIMENSION_KEYS = [
|
|
3
2
|
"guidance",
|
|
4
3
|
"density",
|
|
5
4
|
"dataDepth",
|
|
@@ -8,7 +7,7 @@ var DIMENSION_KEYS = [
|
|
|
8
7
|
"pace",
|
|
9
8
|
"narrative"
|
|
10
9
|
];
|
|
11
|
-
|
|
10
|
+
const SCOPE_ORDER = [
|
|
12
11
|
"user",
|
|
13
12
|
"workspace-user",
|
|
14
13
|
"bundle",
|
|
@@ -41,7 +40,7 @@ function mergeByScope(layers, ctx) {
|
|
|
41
40
|
function resolveConstraints(_canonical, _ctx) {
|
|
42
41
|
return { constrained: {}, notes: [] };
|
|
43
42
|
}
|
|
44
|
-
function resolvePreferenceProfile(ctx) {
|
|
43
|
+
export function resolvePreferenceProfile(ctx) {
|
|
45
44
|
const layers = {
|
|
46
45
|
session: ctx.preferences
|
|
47
46
|
};
|
|
@@ -54,6 +53,3 @@ function resolvePreferenceProfile(ctx) {
|
|
|
54
53
|
notes
|
|
55
54
|
};
|
|
56
55
|
}
|
|
57
|
-
export {
|
|
58
|
-
resolvePreferenceProfile
|
|
59
|
-
};
|