@elementumai/edk 0.9.17 → 0.9.19

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 (130) hide show
  1. package/README.md +8 -3
  2. package/dist-sdk/src/catalog/factories.d.ts +11 -0
  3. package/dist-sdk/src/catalog/factories.js +6 -0
  4. package/dist-sdk/src/catalog/index.d.ts +2 -2
  5. package/dist-sdk/src/catalog/index.js +2 -2
  6. package/dist-sdk/src/catalog/org.d.ts +39 -1
  7. package/dist-sdk/src/catalog/org.js +460 -50
  8. package/dist-sdk/src/catalog/types.d.ts +12 -2
  9. package/dist-sdk/src/catalog/types.js +2 -0
  10. package/dist-sdk/src/engine/pull/ref-only.js +54 -20
  11. package/dist-sdk/src/engine/pull/result-output.d.ts +2 -0
  12. package/dist-sdk/src/engine/pull/result-output.js +1 -1
  13. package/dist-sdk/src/engine/tf/expr.js +38 -13
  14. package/dist-sdk/src/engine/tf/index.d.ts +2 -2
  15. package/dist-sdk/src/engine/tf/index.js +2 -2
  16. package/dist-sdk/src/engine/tf/labels.d.ts +2 -0
  17. package/dist-sdk/src/engine/tf/labels.js +4 -0
  18. package/dist-sdk/src/engine/tf/parse.d.ts +19 -0
  19. package/dist-sdk/src/engine/tf/parse.js +105 -6
  20. package/dist-sdk/src/engine/workspace/loader.js +49 -12
  21. package/dist-sdk/src/entities/access-policy/from-tf.d.ts +11 -1
  22. package/dist-sdk/src/entities/access-policy/from-tf.js +6 -1
  23. package/dist-sdk/src/entities/access-policy/to-tf.d.ts +11 -0
  24. package/dist-sdk/src/entities/access-policy/to-tf.js +11 -4
  25. package/dist-sdk/src/entities/agent-tool/to-tf.js +2 -1
  26. package/dist-sdk/src/entities/app/build.d.ts +10 -0
  27. package/dist-sdk/src/entities/app/build.js +0 -13
  28. package/dist-sdk/src/entities/app/deserializer/normalize.d.ts +6 -1
  29. package/dist-sdk/src/entities/app/from-tf.js +4 -3
  30. package/dist-sdk/src/entities/app/index.d.ts +2 -0
  31. package/dist-sdk/src/entities/app/index.js +1 -0
  32. package/dist-sdk/src/entities/app/module.js +13 -0
  33. package/dist-sdk/src/entities/app/to-tf.js +2 -0
  34. package/dist-sdk/src/entities/automation/build.d.ts +2 -1
  35. package/dist-sdk/src/entities/automation/build.js +27 -0
  36. package/dist-sdk/src/entities/automation/deserializer/print-automation.js +6 -0
  37. package/dist-sdk/src/entities/automation/from-tf.js +156 -26
  38. package/dist-sdk/src/entities/automation/index.d.ts +1 -1
  39. package/dist-sdk/src/entities/automation/module.d.ts +2 -0
  40. package/dist-sdk/src/entities/automation/module.js +56 -0
  41. package/dist-sdk/src/entities/automation/output-field-types.d.ts +5 -4
  42. package/dist-sdk/src/entities/automation/output-field-types.js +23 -5
  43. package/dist-sdk/src/entities/automation/system-ref-name.d.ts +16 -0
  44. package/dist-sdk/src/entities/automation/system-ref-name.js +44 -0
  45. package/dist-sdk/src/entities/automation/task-output-labels.d.ts +15 -0
  46. package/dist-sdk/src/entities/automation/task-output-labels.js +38 -0
  47. package/dist-sdk/src/entities/automation/tasks.d.ts +37 -5
  48. package/dist-sdk/src/entities/automation/tasks.js +23 -1
  49. package/dist-sdk/src/entities/automation/to-tf.js +158 -84
  50. package/dist-sdk/src/entities/automation/types.js +1 -0
  51. package/dist-sdk/src/entities/chart/from-tf.js +33 -17
  52. package/dist-sdk/src/entities/chart/module.js +3 -1
  53. package/dist-sdk/src/entities/chart/printer.d.ts +1 -0
  54. package/dist-sdk/src/entities/chart/printer.js +26 -16
  55. package/dist-sdk/src/entities/chart/rebind-owner.d.ts +3 -0
  56. package/dist-sdk/src/entities/chart/rebind-owner.js +42 -0
  57. package/dist-sdk/src/entities/chart/to-tf.js +12 -10
  58. package/dist-sdk/src/entities/dashboard/build.d.ts +4 -0
  59. package/dist-sdk/src/entities/dashboard/from-tf.js +15 -4
  60. package/dist-sdk/src/entities/dashboard/to-tf.d.ts +2 -0
  61. package/dist-sdk/src/entities/dashboard/to-tf.js +13 -0
  62. package/dist-sdk/src/entities/dashboard-widget/module.js +4 -0
  63. package/dist-sdk/src/entities/dynamic-layout/build.d.ts +51 -2
  64. package/dist-sdk/src/entities/dynamic-layout/dialect.d.ts +3 -0
  65. package/dist-sdk/src/entities/dynamic-layout/dialect.js +5 -0
  66. package/dist-sdk/src/entities/dynamic-layout/from-tf.d.ts +9 -6
  67. package/dist-sdk/src/entities/dynamic-layout/from-tf.js +188 -26
  68. package/dist-sdk/src/entities/dynamic-layout/module.js +4 -1
  69. package/dist-sdk/src/entities/dynamic-layout/to-tf.d.ts +5 -1
  70. package/dist-sdk/src/entities/dynamic-layout/to-tf.js +254 -23
  71. package/dist-sdk/src/entities/element/build.d.ts +8 -0
  72. package/dist-sdk/src/entities/element/build.js +1 -0
  73. package/dist-sdk/src/entities/element/from-tf.d.ts +1 -0
  74. package/dist-sdk/src/entities/element/from-tf.js +6 -2
  75. package/dist-sdk/src/entities/element/index.d.ts +1 -0
  76. package/dist-sdk/src/entities/element/index.js +1 -0
  77. package/dist-sdk/src/entities/element/module.js +2 -0
  78. package/dist-sdk/src/entities/element/to-tf.js +12 -0
  79. package/dist-sdk/src/entities/field/from-tf.d.ts +2 -0
  80. package/dist-sdk/src/entities/field/from-tf.js +11 -0
  81. package/dist-sdk/src/entities/field/to-tf.js +13 -9
  82. package/dist-sdk/src/entities/relationship/from-tf.d.ts +8 -1
  83. package/dist-sdk/src/entities/relationship/from-tf.js +53 -10
  84. package/dist-sdk/src/entities/relationship/to-tf.d.ts +8 -0
  85. package/dist-sdk/src/entities/relationship/to-tf.js +19 -10
  86. package/dist-sdk/src/entities/shared/filters/ast.d.ts +7 -2
  87. package/dist-sdk/src/entities/shared/filters/index.d.ts +23 -2
  88. package/dist-sdk/src/entities/shared/filters/index.js +24 -1
  89. package/dist-sdk/src/entities/shared/filters/print.js +22 -4
  90. package/dist-sdk/src/entities/shared/filters/tf-codec.d.ts +23 -3
  91. package/dist-sdk/src/entities/shared/filters/tf-codec.js +60 -18
  92. package/dist-sdk/src/entities/shared/filters/types.d.ts +13 -4
  93. package/dist-sdk/src/entities/shared/foreign-ref.d.ts +16 -0
  94. package/dist-sdk/src/entities/shared/foreign-ref.js +17 -1
  95. package/dist-sdk/src/entities/shared/tool-field-ref.js +6 -9
  96. package/dist-sdk/src/entities/skill/access-policy.d.ts +17 -0
  97. package/dist-sdk/src/entities/skill/access-policy.js +101 -0
  98. package/dist-sdk/src/entities/skill/build.d.ts +3 -0
  99. package/dist-sdk/src/entities/skill/build.js +3 -0
  100. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.d.ts +4 -1
  101. package/dist-sdk/src/entities/skill/deserializer/print-skill-v2.js +21 -0
  102. package/dist-sdk/src/entities/skill/from-tf.d.ts +2 -0
  103. package/dist-sdk/src/entities/skill/from-tf.js +4 -9
  104. package/dist-sdk/src/entities/skill/index.d.ts +2 -0
  105. package/dist-sdk/src/entities/skill/index.js +1 -0
  106. package/dist-sdk/src/entities/skill/module.js +1 -0
  107. package/dist-sdk/src/entities/skill/to-tf.d.ts +2 -2
  108. package/dist-sdk/src/entities/skill/to-tf.js +5 -2
  109. package/dist-sdk/src/entities/skill-tool/to-tf.js +2 -1
  110. package/dist-sdk/src/entities/table/to-tf.js +3 -3
  111. package/dist-sdk/src/entities/view/agent-view-layout-from-tf.d.ts +49 -0
  112. package/dist-sdk/src/entities/view/agent-view-layout-from-tf.js +224 -0
  113. package/dist-sdk/src/entities/view/agent-view-layout-to-tf.d.ts +36 -0
  114. package/dist-sdk/src/entities/view/agent-view-layout-to-tf.js +288 -0
  115. package/dist-sdk/src/entities/view/agent-view-layout.d.ts +121 -0
  116. package/dist-sdk/src/entities/view/agent-view-layout.js +21 -0
  117. package/dist-sdk/src/entities/view/from-tf.d.ts +3 -1
  118. package/dist-sdk/src/entities/view/from-tf.js +42 -7
  119. package/dist-sdk/src/entities/view/ragnarok.d.ts +7 -0
  120. package/dist-sdk/src/entities/view/ragnarok.js +1 -0
  121. package/dist-sdk/src/entities/view/to-tf.d.ts +10 -1
  122. package/dist-sdk/src/entities/view/to-tf.js +30 -1
  123. package/dist-sdk/src/entities/view/types.js +2 -0
  124. package/dist-sdk/src/entities/widget/to-tf.js +2 -2
  125. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.d.ts +815 -0
  126. package/dist-sdk/src/generated/ragnarok/ragnarok-spec.js +5 -0
  127. package/dist-sdk/src/graph/index.d.ts +43 -0
  128. package/dist-sdk/src/graph/index.js +9 -0
  129. package/dist-sdk/src/graph/types.d.ts +78 -0
  130. package/package.json +11 -1
@@ -0,0 +1,224 @@
1
+ /**
2
+ * Pull: `elementum_agent_view.layout` → the normalized layout the printer
3
+ * turns back into `agentViews.<key>.layout`. References inside the custom
4
+ * view become typed markers at the per-block id positions; raw platform ids
5
+ * there become `unsafePlatformId(...)`. A layout this cannot represent makes
6
+ * the whole view unmanaged (skipped), never silently dropped: the code owns
7
+ * the layout, so a dropped detail would be cleared on the next apply.
8
+ */
9
+ import { isTfExpr } from "../../engine/tf/index.js";
10
+ import { AGENT_VIEW_SPEC_FILTER_PROPS, AGENT_VIEW_SPEC_ID_PROPS } from "./agent-view-layout.js";
11
+ const PLATFORM_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
12
+ const ATTACHMENT_URL = /^\/api\/v1\/attachments\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i;
13
+ const FILTER_PROPS = new Set(AGENT_VIEW_SPEC_FILTER_PROPS);
14
+ const ID_PROPS = AGENT_VIEW_SPEC_ID_PROPS;
15
+ const LAYOUT_STRING_ATTRS = {
16
+ header: "header",
17
+ sub_header: "subHeader",
18
+ background_color: "backgroundColor",
19
+ primary_color: "primaryColor",
20
+ font_family: "fontFamily",
21
+ };
22
+ const KNOWN_LAYOUT_ATTRS = new Set(["image_url", "custom_view", ...Object.keys(LAYOUT_STRING_ATTRS)]);
23
+ class Unrepresentable extends Error {
24
+ }
25
+ export function agentViewLayoutFromTf(value, opts) {
26
+ try {
27
+ return { ok: true, layout: decodeLayout(value, opts) };
28
+ }
29
+ catch (error) {
30
+ if (error instanceof Unrepresentable)
31
+ return { ok: false, reason: error.message };
32
+ throw error;
33
+ }
34
+ }
35
+ function decodeLayout(value, opts) {
36
+ const attrs = plain(value);
37
+ if (attrs === undefined)
38
+ throw new Unrepresentable("layout is not an object");
39
+ const unknown = Object.keys(attrs).filter((key) => !KNOWN_LAYOUT_ATTRS.has(key));
40
+ if (unknown.length > 0)
41
+ throw new Unrepresentable(`layout has unhandled attributes: ${unknown.sort().join(", ")}`);
42
+ const layout = {};
43
+ const imageUrl = attrs["image_url"];
44
+ if (imageUrl !== undefined) {
45
+ if (typeof imageUrl !== "string")
46
+ throw new Unrepresentable("layout.image_url is not a string");
47
+ const attachment = ATTACHMENT_URL.exec(imageUrl)?.[1];
48
+ layout.image = attachment !== undefined ? { attachment } : { url: imageUrl };
49
+ }
50
+ for (const [attr, key] of Object.entries(LAYOUT_STRING_ATTRS)) {
51
+ const v = attrs[attr];
52
+ if (v === undefined)
53
+ continue;
54
+ if (typeof v !== "string")
55
+ throw new Unrepresentable(`layout.${attr} is not a string`);
56
+ const normalized = key === "backgroundColor" || key === "primaryColor"
57
+ ? normalizeColor(v)
58
+ : key === "fontFamily" ? normalizeFont(v) : v;
59
+ if (normalized !== undefined)
60
+ layout[key] = normalized;
61
+ }
62
+ const customView = attrs["custom_view"];
63
+ if (customView !== undefined) {
64
+ if (!isTfExpr(customView) || customView.$expr !== "jsonencode")
65
+ throw new Unrepresentable("layout.custom_view is not a jsonencode(...) expression");
66
+ layout.customView = decodeSpec(customView.value, opts);
67
+ }
68
+ return layout;
69
+ }
70
+ function decodeSpec(value, opts) {
71
+ const spec = plain(value);
72
+ if (spec === undefined)
73
+ throw new Unrepresentable("custom_view is not an object");
74
+ const out = {};
75
+ for (const [key, child] of Object.entries(spec)) {
76
+ if (key !== "elements") {
77
+ out[key] = literal(child, `custom_view.${key}`);
78
+ continue;
79
+ }
80
+ const elements = plain(child);
81
+ if (elements === undefined)
82
+ throw new Unrepresentable("custom_view.elements is not an object");
83
+ const outElements = {};
84
+ for (const [id, rawElement] of Object.entries(elements)) {
85
+ const element = plain(rawElement);
86
+ if (element === undefined)
87
+ throw new Unrepresentable(`custom_view.elements.${id} is not an object`);
88
+ const type = typeof element["type"] === "string" ? element["type"] : "";
89
+ const outElement = {};
90
+ for (const [elKey, elValue] of Object.entries(element)) {
91
+ const where = `custom_view.elements.${id}.${elKey}`;
92
+ outElement[elKey] = elKey === "props" ? decodeProps(type, elValue, where, opts) : literal(elValue, where);
93
+ }
94
+ outElements[id] = outElement;
95
+ }
96
+ out["elements"] = outElements;
97
+ }
98
+ return out;
99
+ }
100
+ function decodeProps(type, value, where, opts) {
101
+ const props = plain(value);
102
+ if (props === undefined)
103
+ return literal(value, where);
104
+ const idProps = ID_PROPS[type] ?? {};
105
+ const out = {};
106
+ for (const [key, child] of Object.entries(props)) {
107
+ const propWhere = `${where}.${key}`;
108
+ const kind = idProps[key];
109
+ if (kind !== undefined && (isTfExpr(child) || (typeof child === "string" && child !== ""))) {
110
+ out[key] = kind === "field" ? fieldMarker(child, propWhere, opts) : appMarker(child, propWhere, opts);
111
+ }
112
+ else if (FILTER_PROPS.has(key)) {
113
+ out[key] = deepRefs(child, propWhere, opts);
114
+ }
115
+ else if (type === "ButtonBlock" && key === "buttons" && Array.isArray(child)) {
116
+ out[key] = child.map((item, i) => {
117
+ const button = plain(item);
118
+ if (button === undefined)
119
+ return literal(item, `${propWhere}[${i}]`);
120
+ return Object.fromEntries(Object.entries(button).map(([bKey, bValue]) => [
121
+ bKey,
122
+ bKey === "viewId" && typeof bValue === "string" && bValue !== "" ? unsafe(bValue) : literal(bValue, `${propWhere}[${i}].${bKey}`),
123
+ ]));
124
+ });
125
+ }
126
+ else {
127
+ out[key] = literal(child, propWhere);
128
+ }
129
+ }
130
+ return out;
131
+ }
132
+ function deepRefs(value, where, opts) {
133
+ if (isTfExpr(value)) {
134
+ if (opts.isOwnApp(value))
135
+ return { __specRef: "app" };
136
+ return fieldMarker(value, where, opts);
137
+ }
138
+ if (isPlatformId(value))
139
+ return unsafe(value);
140
+ if (Array.isArray(value))
141
+ return value.map((item, i) => deepRefs(item, `${where}[${i}]`, opts));
142
+ const rec = plain(value);
143
+ if (rec !== undefined)
144
+ return Object.fromEntries(Object.entries(rec).map(([key, child]) => [key, deepRefs(child, `${where}.${key}`, opts)]));
145
+ return value;
146
+ }
147
+ // Any non-empty string at an id position — a platform id, or a value the
148
+ // platform accepted that is not one — prints as unsafePlatformId(...), which
149
+ // build passes through unchanged, so pull → build round-trips exactly. An
150
+ // empty id stays a plain "" (unsafePlatformId rejects it).
151
+ function appMarker(value, where, opts) {
152
+ if (typeof value === "string")
153
+ return unsafe(value);
154
+ if (opts.isOwnApp(value))
155
+ return { __specRef: "app" };
156
+ throw new Unrepresentable(`${where} references an app the EDK cannot represent here`);
157
+ }
158
+ function fieldMarker(value, where, opts) {
159
+ if (typeof value === "string")
160
+ return unsafe(value);
161
+ const key = opts.fieldKey(value);
162
+ if (key === undefined)
163
+ throw new Unrepresentable(`${where} references a field the EDK cannot represent here`);
164
+ return PLATFORM_ID.test(key) ? unsafe(key) : { __specRef: "field", key };
165
+ }
166
+ /** Plain JSON outside the id positions: any reference here is unrepresentable. */
167
+ function literal(value, where) {
168
+ if (isTfExpr(value))
169
+ throw new Unrepresentable(`${where} holds a reference outside the custom view's id props`);
170
+ if (Array.isArray(value))
171
+ return value.map((item, i) => literal(item, `${where}[${i}]`));
172
+ const rec = plain(value);
173
+ if (rec !== undefined)
174
+ return Object.fromEntries(Object.entries(rec).map(([key, child]) => [key, literal(child, `${where}.${key}`)]));
175
+ return value;
176
+ }
177
+ const HEX_COLOR = /^#?[0-9a-fA-F]{3,8}$/;
178
+ const FONT_FAMILIES = new Set(["sans", "serif", "mono"]);
179
+ /**
180
+ * The page renders only hex colours (with or without '#'), so pull mirrors the
181
+ * provider's normalization: '#'-prefix a bare hex, and treat anything else as
182
+ * unset (the platform ignores it).
183
+ */
184
+ function normalizeColor(color) {
185
+ const trimmed = color.trim();
186
+ if (!HEX_COLOR.test(trimmed))
187
+ return undefined;
188
+ return trimmed.startsWith("#") ? trimmed : `#${trimmed}`;
189
+ }
190
+ /** An unknown font renders as system, the default. */
191
+ function normalizeFont(font) {
192
+ return FONT_FAMILIES.has(font) ? font : undefined;
193
+ }
194
+ function unsafe(id) {
195
+ return { __specRef: "unsafe", id };
196
+ }
197
+ function isPlatformId(value) {
198
+ return typeof value === "string" && PLATFORM_ID.test(value);
199
+ }
200
+ function plain(value) {
201
+ return value !== null && typeof value === "object" && !Array.isArray(value) && !isTfExpr(value)
202
+ ? value
203
+ : undefined;
204
+ }
205
+ /** Own-app field bag keys a pulled layout references (for hoisting their consts). */
206
+ export function collectAgentViewLayoutFieldKeys(layout) {
207
+ const keys = [];
208
+ const walk = (value) => {
209
+ if (Array.isArray(value)) {
210
+ value.forEach(walk);
211
+ return;
212
+ }
213
+ if (value === null || typeof value !== "object")
214
+ return;
215
+ const rec = value;
216
+ if (rec["__specRef"] === "field" && typeof rec["key"] === "string") {
217
+ keys.push(rec["key"]);
218
+ return;
219
+ }
220
+ Object.values(rec).forEach(walk);
221
+ };
222
+ walk(layout?.customView);
223
+ return keys;
224
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Build: `agentViews.<key>.layout` → the `layout` attribute of
3
+ * `elementum_agent_view`. Platform ids in the custom view resolve from typed
4
+ * references at the per-block id positions; a structurally broken custom view
5
+ * fails the build, because dropping it would make apply clear the live one.
6
+ */
7
+ import type { OrgRefLookup, WorkspaceAspect } from "../../engine/build/context.js";
8
+ import { type ChildResourceScope, type TfValue } from "../../engine/tf/index.js";
9
+ export interface AgentViewLayoutToTfOptions {
10
+ readonly ownerRefName: string;
11
+ readonly ownerTfLabel: string;
12
+ readonly scope: ChildResourceScope;
13
+ readonly objectId: TfValue;
14
+ readonly diagnostics: string[];
15
+ readonly fieldAddresses?: ReadonlyMap<string, string>;
16
+ readonly orgRefs?: OrgRefLookup;
17
+ readonly workspaceAspects?: ReadonlyMap<string, WorkspaceAspect>;
18
+ /** Resolves an own-app field reference (view/to-tf's field resolver). */
19
+ readonly resolveField: (ref: unknown, where: string) => TfValue | undefined;
20
+ }
21
+ /** Wire shape of an authored layout (the public type, unchecked). */
22
+ export interface AppAgentViewLayoutWire {
23
+ readonly image?: {
24
+ readonly attachment?: unknown;
25
+ readonly url?: unknown;
26
+ };
27
+ readonly header?: string;
28
+ readonly subHeader?: string;
29
+ readonly branding?: {
30
+ readonly backgroundColor?: string;
31
+ readonly primaryColor?: string;
32
+ readonly fontFamily?: string;
33
+ };
34
+ readonly customView?: unknown;
35
+ }
36
+ export declare function agentViewLayoutToTf(layout: AppAgentViewLayoutWire, where: string, opts: AgentViewLayoutToTfOptions): TfValue;
@@ -0,0 +1,288 @@
1
+ import { workspaceAspectKey } from "../../engine/build/context.js";
2
+ import { EntityTfError } from "../../engine/entity.js";
3
+ import { expr } from "../../engine/tf/index.js";
4
+ import { resolveForeignRef } from "../shared/foreign-ref.js";
5
+ import { SYSTEM_FIELD_ID_ATTR_BY_KEY } from "./types.js";
6
+ import { AGENT_VIEW_SPEC_FILTER_PROPS, AGENT_VIEW_SPEC_ID_PROPS, agentViewFontFamily, } from "./agent-view-layout.js";
7
+ const PLATFORM_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
8
+ const HEX_COLOR = /^#[0-9a-fA-F]{3,8}$/;
9
+ const FONT_FAMILIES = new Set(Object.values(agentViewFontFamily));
10
+ const FILTER_PROPS = new Set(AGENT_VIEW_SPEC_FILTER_PROPS);
11
+ const ID_PROPS = AGENT_VIEW_SPEC_ID_PROPS;
12
+ /** Blocks the admin UI's JSON editor re-applies (its managed-view authoring policy). */
13
+ const SYSTEM_FIELD_KEYS = new Set(SYSTEM_FIELD_ID_ATTR_BY_KEY.keys());
14
+ const UI_EDITABLE_BLOCKS = new Set([
15
+ "ActionCardBlock", "BodyBlock", "BulletsBlock", "CardBlock", "DataTableBlock", "DetailsFieldsBlock",
16
+ "DetailsListBlock", "DividerBlock", "HeaderBlock", "RowBlock", "SpacerBlock", "StackBlock",
17
+ ]);
18
+ export function agentViewLayoutToTf(layout, where, opts) {
19
+ const attrs = {};
20
+ const image = layout.image;
21
+ if (image !== undefined) {
22
+ const attachment = unsafeId(image.attachment);
23
+ if (attachment !== undefined) {
24
+ if (!PLATFORM_ID.test(attachment))
25
+ throw new EntityTfError(`${where}.image.attachment: "${attachment}" is not a platform id`);
26
+ attrs["image_url"] = `/api/v1/attachments/${attachment}`;
27
+ }
28
+ else if (typeof image.url === "string" && image.url !== "") {
29
+ attrs["image_url"] = image.url;
30
+ }
31
+ else {
32
+ throw new EntityTfError(`${where}.image must be { attachment: unsafePlatformId(...) } or { url: "..." }`);
33
+ }
34
+ }
35
+ if (layout.header !== undefined && layout.header !== "")
36
+ attrs["header"] = layout.header;
37
+ if (layout.subHeader !== undefined && layout.subHeader !== "")
38
+ attrs["sub_header"] = layout.subHeader;
39
+ const branding = layout.branding ?? {};
40
+ for (const [key, attr] of [["backgroundColor", "background_color"], ["primaryColor", "primary_color"]]) {
41
+ const color = branding[key];
42
+ if (color === undefined || color === "")
43
+ continue;
44
+ if (!HEX_COLOR.test(color))
45
+ throw new EntityTfError(`${where}.branding.${key}: "${color}" is not a hex colour (#RGB to #RRGGBBAA)`);
46
+ attrs[attr] = color;
47
+ }
48
+ if (branding.fontFamily !== undefined) {
49
+ if (!FONT_FAMILIES.has(branding.fontFamily)) {
50
+ throw new EntityTfError(`${where}.branding.fontFamily: "${branding.fontFamily}" is not one of ${[...FONT_FAMILIES].join(", ")}`);
51
+ }
52
+ attrs["font_family"] = branding.fontFamily;
53
+ }
54
+ if (layout.customView !== undefined) {
55
+ attrs["custom_view"] = expr.jsonencode(customViewToTf(layout.customView, `${where}.customView`, opts));
56
+ }
57
+ return attrs;
58
+ }
59
+ function customViewToTf(spec, where, opts) {
60
+ const doc = record(spec);
61
+ if (doc === undefined)
62
+ throw new EntityTfError(`${where} must be an object`);
63
+ const elements = record(doc["elements"]);
64
+ if (typeof doc["root"] !== "string" || doc["root"] === "")
65
+ throw new EntityTfError(`${where}.root must name an element`);
66
+ if (elements === undefined || Object.keys(elements).length === 0)
67
+ throw new EntityTfError(`${where}.elements must have at least one element`);
68
+ checkStructure(doc["root"], elements, where, opts.diagnostics);
69
+ const out = {};
70
+ for (const [key, value] of Object.entries(doc)) {
71
+ if (key === "elements")
72
+ continue;
73
+ out[key] = literal(value, `${where}.${key}`, opts);
74
+ }
75
+ const outElements = {};
76
+ for (const [id, raw] of Object.entries(elements)) {
77
+ const element = record(raw);
78
+ const elementWhere = `${where}.elements.${id}`;
79
+ const type = typeof element["type"] === "string" ? element["type"] : "";
80
+ policyWarnings(type, element, elementWhere, opts.diagnostics);
81
+ const outElement = {};
82
+ for (const [key, value] of Object.entries(element)) {
83
+ outElement[key] = key === "props"
84
+ ? propsToTf(type, value, `${elementWhere}.props`, opts)
85
+ : literal(value, `${elementWhere}.${key}`, opts);
86
+ }
87
+ outElements[id] = outElement;
88
+ }
89
+ out["elements"] = outElements;
90
+ return out;
91
+ }
92
+ function propsToTf(type, props, where, opts) {
93
+ const rec = record(props);
94
+ if (rec === undefined)
95
+ return literal(props, where, opts);
96
+ const idProps = ID_PROPS[type] ?? {};
97
+ const out = {};
98
+ for (const [key, value] of Object.entries(rec)) {
99
+ const propWhere = `${where}.${key}`;
100
+ const kind = idProps[key];
101
+ if (kind === "field" && (isReference(value) || SYSTEM_FIELD_KEYS.has(value))) {
102
+ // A string here is a system field key ("status", "createdAt", ...).
103
+ out[key] = fieldRef(value, propWhere, opts);
104
+ }
105
+ else if (kind === "app" && isReference(value)) {
106
+ out[key] = appRef(value, propWhere, opts);
107
+ }
108
+ else if (FILTER_PROPS.has(key)) {
109
+ out[key] = deepRefs(value, propWhere, opts);
110
+ }
111
+ else if (type === "ButtonBlock" && key === "buttons" && Array.isArray(value)) {
112
+ out[key] = value.map((item, i) => buttonToTf(item, `${propWhere}[${i}]`, opts));
113
+ }
114
+ else {
115
+ out[key] = literal(value, propWhere, opts);
116
+ }
117
+ }
118
+ return out;
119
+ }
120
+ function buttonToTf(item, where, opts) {
121
+ const rec = record(item);
122
+ if (rec === undefined)
123
+ return literal(item, where, opts);
124
+ const out = {};
125
+ for (const [key, value] of Object.entries(rec)) {
126
+ const id = key === "viewId" ? unsafeId(value) : undefined;
127
+ out[key] = id !== undefined ? id : literal(value, `${where}.${key}`, opts);
128
+ }
129
+ return out;
130
+ }
131
+ function appRef(value, where, opts) {
132
+ const id = unsafeId(value);
133
+ if (id !== undefined)
134
+ return id;
135
+ const token = value;
136
+ const kind = token.__kind === "element" ? "element" : "app";
137
+ const refName = token.refName ?? "";
138
+ if (kind === "app" && refName === opts.ownerRefName)
139
+ return opts.objectId;
140
+ const sibling = opts.workspaceAspects?.get(workspaceAspectKey(kind, refName));
141
+ if (sibling !== undefined)
142
+ return expr.resourceAttr(kind === "app" ? "elementum_app" : "elementum_element", sibling.tfLabel, ["id"]);
143
+ if (opts.orgRefs === undefined)
144
+ throw new EntityTfError(`${where}: app "${refName}" cannot be resolved here; use unsafePlatformId(...)`);
145
+ return resolveForeignRef(kind, { name: token.name ?? refName, namespace: token.namespace }, opts.orgRefs, opts.diagnostics, where).value;
146
+ }
147
+ function fieldRef(value, where, opts) {
148
+ const id = unsafeId(value);
149
+ if (id !== undefined)
150
+ return id;
151
+ const parent = value.parent;
152
+ if (parent !== undefined && parent !== opts.ownerRefName) {
153
+ throw new EntityTfError(`${where}: field references must belong to this app ("${opts.ownerRefName}"); use unsafePlatformId(...) for "${parent}" fields`);
154
+ }
155
+ const before = opts.diagnostics.length;
156
+ const resolved = opts.resolveField(value, where);
157
+ if (resolved === undefined) {
158
+ const reason = opts.diagnostics.slice(before).join("; ");
159
+ throw new EntityTfError(reason !== "" ? reason : `${where}: field reference does not resolve`);
160
+ }
161
+ return resolved;
162
+ }
163
+ /** Nested filter values: references may sit at any depth. */
164
+ function deepRefs(value, where, opts) {
165
+ if (isReference(value)) {
166
+ const kind = value.__kind;
167
+ return kind === "app" || kind === "element" ? appRef(value, where, opts) : fieldRef(value, where, opts);
168
+ }
169
+ if (Array.isArray(value))
170
+ return value.map((item, i) => deepRefs(item, `${where}[${i}]`, opts));
171
+ const rec = record(value);
172
+ if (rec !== undefined) {
173
+ const out = {};
174
+ for (const [key, child] of Object.entries(rec))
175
+ out[checkKey(key, where)] = deepRefs(child, `${where}.${key}`, opts);
176
+ return out;
177
+ }
178
+ return literal(value, where, opts);
179
+ }
180
+ /** Plain JSON outside the id positions. */
181
+ function literal(value, where, opts) {
182
+ if (value === null || typeof value === "string" || typeof value === "boolean")
183
+ return value;
184
+ if (typeof value === "number") {
185
+ if (!Number.isFinite(value))
186
+ throw new EntityTfError(`${where}: ${value} is not a JSON number`);
187
+ return value;
188
+ }
189
+ if (Array.isArray(value))
190
+ return value.map((item, i) => literal(item, `${where}[${i}]`, opts));
191
+ if (isReference(value)) {
192
+ throw new EntityTfError(`${where}: references are only allowed at the custom view's id props; use a plain value here`);
193
+ }
194
+ const rec = record(value);
195
+ if (rec === undefined)
196
+ throw new EntityTfError(`${where}: ${typeof value} is not a JSON value`);
197
+ const out = {};
198
+ for (const [key, child] of Object.entries(rec))
199
+ out[checkKey(key, where)] = literal(child, `${where}.${key}`, opts);
200
+ return out;
201
+ }
202
+ function checkStructure(root, elements, where, diagnostics) {
203
+ // The platform's schema rejects these, so they fail the build.
204
+ if (!(root in elements))
205
+ throw new EntityTfError(`${where}.root "${root}" is not an element`);
206
+ for (const [id, raw] of Object.entries(elements)) {
207
+ const element = record(raw);
208
+ if (element === undefined || typeof element["type"] !== "string")
209
+ throw new EntityTfError(`${where}.elements.${id} must be a block with a type`);
210
+ if (record(element["props"]) === undefined)
211
+ throw new EntityTfError(`${where}.elements.${id}.props must be an object`);
212
+ const children = element["children"];
213
+ if (children !== undefined && !Array.isArray(children))
214
+ throw new EntityTfError(`${where}.elements.${id}.children must be a list of element ids`);
215
+ }
216
+ // The platform stores these (its schema does not cross-check child ids), but
217
+ // the page fails to render them. Warn rather than fail, so a pulled layout
218
+ // still builds and round-trips; fixing it is the author's call.
219
+ for (const [id, raw] of Object.entries(elements)) {
220
+ const children = record(raw)?.["children"];
221
+ if (!Array.isArray(children))
222
+ continue;
223
+ for (const child of children) {
224
+ if (typeof child !== "string" || !(child in elements)) {
225
+ diagnostics.push(`${where}.elements.${id}.children: "${String(child)}" is not an element; the page will fail to render this custom view`);
226
+ }
227
+ }
228
+ }
229
+ const state = new Map();
230
+ let cycle;
231
+ const visit = (id, path) => {
232
+ if (cycle !== undefined || state.get(id) === "done")
233
+ return;
234
+ if (state.get(id) === "visiting") {
235
+ cycle = [...path, id].join(" → ");
236
+ return;
237
+ }
238
+ state.set(id, "visiting");
239
+ const children = record(elements[id])?.["children"];
240
+ if (Array.isArray(children))
241
+ for (const child of children)
242
+ if (typeof child === "string" && child in elements)
243
+ visit(child, [...path, id]);
244
+ state.set(id, "done");
245
+ };
246
+ for (const id of Object.keys(elements))
247
+ visit(id, []);
248
+ if (cycle !== undefined)
249
+ diagnostics.push(`${where}: children form a cycle (${cycle}); the page will fail to render this custom view`);
250
+ }
251
+ function policyWarnings(type, element, where, diagnostics) {
252
+ const reason = !UI_EDITABLE_BLOCKS.has(type)
253
+ ? `${type} is outside the admin UI editor's block list`
254
+ : type === "DataTableBlock" && record(element["props"])?.["rows"] === undefined
255
+ ? "a DataTableBlock without static rows is outside the admin UI editor's policy"
256
+ : element["on"] !== undefined
257
+ ? "`on` handlers are outside the admin UI editor's policy"
258
+ : hasExpression(element["props"])
259
+ ? "`$` expressions are outside the admin UI editor's policy"
260
+ : undefined;
261
+ if (reason !== undefined) {
262
+ diagnostics.push(`${where}: ${reason}; the platform renders it, but the admin UI's JSON editor will not re-apply it`);
263
+ }
264
+ }
265
+ function hasExpression(value) {
266
+ if (Array.isArray(value))
267
+ return value.some(hasExpression);
268
+ const rec = record(value);
269
+ if (rec === undefined)
270
+ return false;
271
+ return Object.entries(rec).some(([key, child]) => key.startsWith("$") || hasExpression(child));
272
+ }
273
+ function checkKey(key, where) {
274
+ if (key === "$expr")
275
+ throw new EntityTfError(`${where}: the object key "$expr" is reserved and cannot appear in a custom view`);
276
+ return key;
277
+ }
278
+ function isReference(value) {
279
+ const rec = record(value);
280
+ return rec !== undefined && (typeof rec["__kind"] === "string" || rec["__unsafe"] === "platformId");
281
+ }
282
+ function unsafeId(value) {
283
+ const rec = record(value);
284
+ return rec !== undefined && rec["__unsafe"] === "platformId" && typeof rec["value"] === "string" ? rec["value"] : undefined;
285
+ }
286
+ function record(value) {
287
+ return value !== null && typeof value === "object" && !Array.isArray(value) ? value : undefined;
288
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Agent managed view layout customization (PF-5983): the Welcome landing
3
+ * screen an agent view shows before the first message. Authored on
4
+ * `agentViews.<key>.layout`; present means the Welcome template, absent means
5
+ * the standard chat (an apply then clears any live customization).
6
+ *
7
+ * `customView` is an Elementum Blocks (Ragnarok) spec — the generated
8
+ * `ElementumBlocksSpec` — with platform ids replaced by typed references at
9
+ * the per-block id positions listed in `AGENT_VIEW_SPEC_ID_PROPS`.
10
+ */
11
+ import type { AppCatalogRef, FieldRef } from "../../catalog/types.js";
12
+ import type { UnsafePlatformId } from "../../core/platform-id.js";
13
+ import type { SystemFieldKey } from "../../core/system-fields.js";
14
+ import type { ElementumBlocksElement, ElementumBlocksSpec } from "./ragnarok.js";
15
+ /** Font families the agent view renders. */
16
+ export declare const agentViewFontFamily: {
17
+ readonly system: "system";
18
+ readonly sans: "sans";
19
+ readonly serif: "serif";
20
+ readonly mono: "mono";
21
+ };
22
+ export type AgentViewFontFamily = (typeof agentViewFontFamily)[keyof typeof agentViewFontFamily];
23
+ /** A hex colour, `#RGB` to `#RRGGBBAA`. Build checks the digits. */
24
+ export type AgentViewHexColor = `#${string}`;
25
+ /**
26
+ * The hero image: an attachment uploaded to this environment (the admin UI's
27
+ * upload, stored as `/api/v1/attachments/<id>`), or an external URL.
28
+ */
29
+ export type AgentViewImage = {
30
+ readonly attachment: UnsafePlatformId;
31
+ readonly url?: never;
32
+ } | {
33
+ readonly url: string;
34
+ readonly attachment?: never;
35
+ };
36
+ export interface AgentViewBranding {
37
+ readonly backgroundColor?: AgentViewHexColor;
38
+ readonly primaryColor?: AgentViewHexColor;
39
+ /** Defaults to `system`. */
40
+ readonly fontFamily?: AgentViewFontFamily;
41
+ }
42
+ /** A self-fetching block's app: the view's own app, or another app by id. */
43
+ export type AgentViewSpecApp = AppCatalogRef | UnsafePlatformId;
44
+ /** A self-fetching block's field: one of the view's own app fields, or another field by id. */
45
+ export type AgentViewSpecField = FieldRef | SystemFieldKey | UnsafePlatformId;
46
+ /** A reference allowed anywhere inside a nested filter prop. */
47
+ export type AgentViewSpecFilterRef = AgentViewSpecApp | AgentViewSpecField;
48
+ /**
49
+ * Per block type, the top-level props that hold platform ids. Ids are scoped
50
+ * per block, not by prop name: DetailsFieldsBlock's `fields[].fieldId` is a
51
+ * render identity, not a field lookup.
52
+ */
53
+ export declare const AGENT_VIEW_SPEC_ID_PROPS: {
54
+ readonly DataTableBlock: {
55
+ readonly aspectId: "app";
56
+ readonly selectedAspectId: "app";
57
+ };
58
+ readonly ListCollectionBlock: {
59
+ readonly aspectId: "app";
60
+ readonly selectedAspectId: "app";
61
+ };
62
+ readonly ListControlsBlock: {
63
+ readonly filterAspectId: "app";
64
+ };
65
+ readonly ChartBlock: {
66
+ readonly aspectId: "app";
67
+ readonly fieldId: "field";
68
+ readonly groupByFieldId: "field";
69
+ };
70
+ readonly MetricsRowBlock: {
71
+ readonly aspectId: "app";
72
+ };
73
+ };
74
+ /** Props holding nested filter structures, where references may sit at any depth. */
75
+ export declare const AGENT_VIEW_SPEC_FILTER_PROPS: readonly ["filterJson", "filterFields", "quickFilters"];
76
+ type IdPropsFor<T> = T extends keyof typeof AGENT_VIEW_SPEC_ID_PROPS ? (typeof AGENT_VIEW_SPEC_ID_PROPS)[T] : Record<never, never>;
77
+ type FilterProp = (typeof AGENT_VIEW_SPEC_FILTER_PROPS)[number];
78
+ /**
79
+ * Replace the plain-string arm of a prop with a typed reference. `""` stays
80
+ * allowed: the platform accepts an empty id, and unsafePlatformId rejects one.
81
+ */
82
+ type WithRef<V, R> = V extends string ? R | "" : V;
83
+ /** Allow a reference wherever a string sits inside a nested value. */
84
+ type WithDeepRefs<V> = V extends string ? V | AgentViewSpecFilterRef : V extends ReadonlyArray<infer I> ? ReadonlyArray<WithDeepRefs<I>> : V extends object ? {
85
+ readonly [K in keyof V]: WithDeepRefs<V[K]>;
86
+ } : V;
87
+ type MapProps<T, P> = P extends unknown ? {
88
+ readonly [K in keyof P]: K extends keyof IdPropsFor<T> ? WithRef<P[K], IdPropsFor<T>[K] extends "field" ? AgentViewSpecField : AgentViewSpecApp> : K extends FilterProp ? WithDeepRefs<P[K]> : T extends "ButtonBlock" ? K extends "buttons" ? MapButtons<P[K]> : P[K] : P[K];
89
+ } : never;
90
+ /** ButtonBlock link items name another managed view by id. */
91
+ type MapButtons<V> = V extends ReadonlyArray<infer I> ? ReadonlyArray<I extends {
92
+ readonly viewId?: string;
93
+ } ? Omit<I, "viewId"> & {
94
+ readonly viewId?: UnsafePlatformId;
95
+ } : I> : V;
96
+ type MapElement<E> = E extends {
97
+ readonly type: infer T;
98
+ readonly props: infer P;
99
+ } ? Omit<E, "props"> & {
100
+ readonly props: MapProps<T, P>;
101
+ } : E;
102
+ /** One block of an agent view's custom view. */
103
+ export type AgentViewBlock = MapElement<ElementumBlocksElement>;
104
+ /** An agent view's custom view: an Elementum Blocks spec with typed references. */
105
+ export type AgentViewCustomView = Omit<ElementumBlocksSpec, "elements"> & {
106
+ readonly elements: {
107
+ readonly [key: string]: AgentViewBlock;
108
+ };
109
+ };
110
+ /** The layout customization of an agent view (the admin UI's Layout panel). */
111
+ export interface AppAgentViewLayout {
112
+ readonly image?: AgentViewImage;
113
+ /** The UI's "Welcome header". When unset the page shows its default greeting. */
114
+ readonly header?: string;
115
+ /** The UI's "Welcome sub-header". When unset the page shows the agent's first message. */
116
+ readonly subHeader?: string;
117
+ readonly branding?: AgentViewBranding;
118
+ /** The custom JSON view below the chat. When unset there is none. */
119
+ readonly customView?: AgentViewCustomView;
120
+ }
121
+ export {};