@workbench-kit/shell-react 0.0.2-prototype.0.2.6

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 (93) hide show
  1. package/package.json +49 -0
  2. package/src/chat-ai-mock-proposals.ts +39 -0
  3. package/src/chat-command-input.ts +52 -0
  4. package/src/chat-command-policy.ts +54 -0
  5. package/src/chat-command-surface.tsx +155 -0
  6. package/src/chat-view-data.ts +20 -0
  7. package/src/chat-view.tsx +220 -0
  8. package/src/command-inspector-surface.tsx +46 -0
  9. package/src/commands-view-data.ts +17 -0
  10. package/src/commands-view.tsx +43 -0
  11. package/src/createCommandWorkspaceExplorerPort.ts +86 -0
  12. package/src/devtools/WorkbenchDevtoolsPanel.tsx +124 -0
  13. package/src/devtools/WorkbenchDevtoolsShell.tsx +17 -0
  14. package/src/devtools/index.ts +11 -0
  15. package/src/devtools/use-workbench-devtools-snapshot.ts +84 -0
  16. package/src/devtools/workbench-devtools-snapshot.ts +140 -0
  17. package/src/devtools/workbench-devtools.css +82 -0
  18. package/src/editor-area-dnd.ts +69 -0
  19. package/src/editor-area-model.ts +71 -0
  20. package/src/editor-area.css +454 -0
  21. package/src/editor-area.tsx +676 -0
  22. package/src/editor-host-surface.tsx +634 -0
  23. package/src/editor-pane-visibility.ts +130 -0
  24. package/src/editor-resource.ts +36 -0
  25. package/src/editor-state-storage.ts +193 -0
  26. package/src/editor-tab-context-menu.ts +118 -0
  27. package/src/editor-view-providers.tsx +552 -0
  28. package/src/editor-workspace-file-availability.ts +15 -0
  29. package/src/editor-workspace-reconcile.tsx +28 -0
  30. package/src/explorer-context-menu.ts +114 -0
  31. package/src/explorer-reveal.ts +112 -0
  32. package/src/explorer-view-data.ts +58 -0
  33. package/src/explorer-view.tsx +224 -0
  34. package/src/extension-context-menu.ts +52 -0
  35. package/src/extension-management-model.ts +313 -0
  36. package/src/extensions-view-data.ts +19 -0
  37. package/src/extensions-view.tsx +38 -0
  38. package/src/field-remap-demo.tsx +18 -0
  39. package/src/field-remap-editor-surface.tsx +36 -0
  40. package/src/field-remap-flow-adapter.ts +240 -0
  41. package/src/field-remap-flow.tsx +369 -0
  42. package/src/field-remap-graph.tsx +559 -0
  43. package/src/field-remap-panel.tsx +175 -0
  44. package/src/field-remap-samples.ts +352 -0
  45. package/src/field-remap-tree.tsx +404 -0
  46. package/src/field-remap-view-data.ts +16 -0
  47. package/src/field-remap-view.css +676 -0
  48. package/src/field-remap-view.tsx +86 -0
  49. package/src/index.ts +199 -0
  50. package/src/jdw-lab-view-data.ts +25 -0
  51. package/src/jdw-lab-view.css +21 -0
  52. package/src/jdw-lab-view.tsx +79 -0
  53. package/src/jdw-widget-form-view.tsx +65 -0
  54. package/src/jdw-widget-preview-view.tsx +51 -0
  55. package/src/json-form-source-patch.ts +323 -0
  56. package/src/jsonata-transform.ts +36 -0
  57. package/src/keybinding-management-settings.tsx +16 -0
  58. package/src/keybinding-overrides-storage.ts +57 -0
  59. package/src/management-palette-commands.ts +38 -0
  60. package/src/management-settings-ids.ts +12 -0
  61. package/src/management-settings.tsx +75 -0
  62. package/src/preference-settings-storage.ts +58 -0
  63. package/src/provider.tsx +573 -0
  64. package/src/search-view-data.ts +15 -0
  65. package/src/search-view.tsx +62 -0
  66. package/src/shell-model.tsx +149 -0
  67. package/src/shell-secondary-actions.tsx +59 -0
  68. package/src/shell-settings-constants.ts +9 -0
  69. package/src/shell-settings.tsx +599 -0
  70. package/src/shell-titlebar-layout-controls.tsx +4 -0
  71. package/src/shell-view-host.tsx +199 -0
  72. package/src/shell.tsx +649 -0
  73. package/src/table-mapping-adapter.ts +59 -0
  74. package/src/use-active-workspace-path.ts +15 -0
  75. package/src/use-command-management.ts +191 -0
  76. package/src/use-context-key-revision.ts +18 -0
  77. package/src/use-editor.ts +107 -0
  78. package/src/use-extension-management.ts +193 -0
  79. package/src/use-keybinding-management.ts +130 -0
  80. package/src/use-persisted-workbench-appearance.ts +33 -0
  81. package/src/use-workbench-chat-command-proposals.ts +194 -0
  82. package/src/use-workbench-command-descriptors.ts +41 -0
  83. package/src/workbench-appearance-storage.ts +115 -0
  84. package/src/workbench-command-host.tsx +243 -0
  85. package/src/workbench-command-palette.ts +208 -0
  86. package/src/workbench-keybinding-bridge.ts +54 -0
  87. package/src/workbench-layout-storage.ts +145 -0
  88. package/src/workbench-profile-modal.tsx +118 -0
  89. package/src/workbench-shell-command-registration.ts +58 -0
  90. package/src/workbench-startup-gate.tsx +136 -0
  91. package/src/workbench-status-sections.ts +43 -0
  92. package/src/workbench-user-commands.ts +46 -0
  93. package/src/workspace-view-state.ts +35 -0
@@ -0,0 +1,313 @@
1
+ import {
2
+ BUILTIN_WORKBENCH_EXTENSIONS,
3
+ SAMPLE_WORKBENCH_EXTENSIONS,
4
+ createExtensionFeatureSpec,
5
+ createExtensionInstallPlan,
6
+ resolveBundledExtensionByManifestUrl,
7
+ type ExtensionCatalogEntry,
8
+ type ExtensionFeatureInspection,
9
+ type ExtensionFeatureSpec,
10
+ type ExtensionInstallPlan,
11
+ type ExtensionInstallPlanInstallSource,
12
+ type ExtensionRegistry,
13
+ type InstalledExtensionRecord,
14
+ type WorkbenchExtensionDescription,
15
+ } from '@workbench-kit/workbench-core';
16
+ import type {
17
+ ExtensionCatalogBrowseEntry,
18
+ ExtensionManagementDiagnosticSummary,
19
+ ExtensionManagementEntry,
20
+ ExtensionManagementFeatureSummary,
21
+ } from '@workbench-kit/react/workbench/management';
22
+
23
+ export const BUNDLED_EXTENSIONS = [
24
+ ...BUILTIN_WORKBENCH_EXTENSIONS,
25
+ ...SAMPLE_WORKBENCH_EXTENSIONS,
26
+ ] as const;
27
+
28
+ export interface CreateExtensionManagementEntriesInput {
29
+ readonly extensionRegistry: ExtensionRegistry;
30
+ readonly installedRecords: readonly InstalledExtensionRecord[];
31
+ }
32
+
33
+ export interface CreateExtensionCatalogBrowseEntriesInput extends CreateExtensionManagementEntriesInput {
34
+ readonly catalogEntries: readonly ExtensionCatalogEntry[];
35
+ }
36
+
37
+ export interface ExtensionInstallPlanningContext extends CreateExtensionManagementEntriesInput {
38
+ readonly availableExtensions: readonly WorkbenchExtensionDescription[];
39
+ readonly enabledExtensionIds: readonly string[];
40
+ readonly hostCapabilityIds: readonly string[];
41
+ readonly installSources: readonly ExtensionInstallPlanInstallSource[];
42
+ }
43
+
44
+ export function createExtensionManagementEntries({
45
+ extensionRegistry,
46
+ installedRecords,
47
+ }: CreateExtensionManagementEntriesInput): readonly ExtensionManagementEntry[] {
48
+ const installedById = new Map(installedRecords.map((record) => [record.id, record]));
49
+ const extensionFeatures = createExtensionManagementFeatureMaps(extensionRegistry);
50
+ const bundledEntries = BUNDLED_EXTENSIONS.map((extension) => {
51
+ const installed = installedById.get(extension.manifest.id);
52
+ const isBuiltin = extension.manifest.id.startsWith('workbench-kit.builtin.');
53
+ const featureState = resolveExtensionManagementFeatureState(
54
+ extension.manifest.id,
55
+ extensionFeatures,
56
+ );
57
+
58
+ return {
59
+ category: installed?.category ?? (isBuiltin ? 'builtin' : 'sample'),
60
+ description: extension.manifest.displayName,
61
+ diagnostics: featureState.diagnostics,
62
+ displayName: extension.manifest.displayName,
63
+ enabled: isBuiltin ? true : (installed?.enabled ?? false),
64
+ features: featureState.features,
65
+ id: extension.manifest.id,
66
+ installedAt: installed?.installedAt,
67
+ manifestUrl: installed?.manifestUrl,
68
+ source: isBuiltin ? ('bundled' as const) : ('installed' as const),
69
+ } satisfies ExtensionManagementEntry;
70
+ }).filter((entry) => entry.source === 'bundled' || installedById.has(entry.id));
71
+
72
+ const activeExtensions = extensionRegistry
73
+ .getExtensions()
74
+ .filter(
75
+ (extension) =>
76
+ !BUNDLED_EXTENSIONS.some((bundled) => bundled.manifest.id === extension.manifest.id),
77
+ )
78
+ .map((extension) => {
79
+ const installed = installedById.get(extension.manifest.id);
80
+ const featureState = resolveExtensionManagementFeatureState(
81
+ extension.manifest.id,
82
+ extensionFeatures,
83
+ );
84
+
85
+ return {
86
+ category: installed?.category ?? 'installed',
87
+ description: extension.manifest.displayName,
88
+ diagnostics: featureState.diagnostics,
89
+ displayName: extension.manifest.displayName,
90
+ enabled: installed?.enabled ?? true,
91
+ features: featureState.features,
92
+ id: extension.manifest.id,
93
+ installedAt: installed?.installedAt,
94
+ manifestUrl: installed?.manifestUrl,
95
+ source: 'installed',
96
+ } satisfies ExtensionManagementEntry;
97
+ });
98
+
99
+ return [...bundledEntries, ...activeExtensions].sort((left, right) =>
100
+ left.displayName.localeCompare(right.displayName),
101
+ );
102
+ }
103
+
104
+ export function createExtensionCatalogBrowseEntries({
105
+ catalogEntries,
106
+ extensionRegistry,
107
+ installedRecords,
108
+ }: CreateExtensionCatalogBrowseEntriesInput): readonly ExtensionCatalogBrowseEntry[] {
109
+ const installedIds = new Set(installedRecords.map((record) => record.id));
110
+ const installContext = createExtensionInstallPlanningContext({
111
+ catalogEntries,
112
+ extensionRegistry,
113
+ installedRecords,
114
+ });
115
+
116
+ return catalogEntries.map((entry) => {
117
+ const bundledExtension = resolveBundledExtensionByManifestUrl(
118
+ entry.manifestUrl,
119
+ installContext.availableExtensions,
120
+ );
121
+ const extensionId = bundledExtension?.manifest.id ?? entry.id;
122
+ const plan = createCatalogEntryInstallPlan(entry, installContext);
123
+
124
+ return {
125
+ category: entry.category,
126
+ description: entry.description,
127
+ displayName: entry.displayName,
128
+ icon: entry.icon,
129
+ id: extensionId,
130
+ installPlan: plan ? toExtensionInstallPlanSummary(plan) : undefined,
131
+ installed: installedIds.has(extensionId),
132
+ manifestUrl: entry.manifestUrl,
133
+ };
134
+ });
135
+ }
136
+
137
+ export function createExtensionInstallPlanningContext({
138
+ catalogEntries,
139
+ extensionRegistry,
140
+ installedRecords,
141
+ }: CreateExtensionCatalogBrowseEntriesInput): ExtensionInstallPlanningContext {
142
+ const availableExtensions = mergeUniqueExtensionDescriptions([
143
+ ...BUNDLED_EXTENSIONS,
144
+ ...extensionRegistry.getExtensions(),
145
+ ]);
146
+
147
+ return {
148
+ availableExtensions,
149
+ enabledExtensionIds: extensionRegistry
150
+ .getExtensions()
151
+ .map((extension) => extension.manifest.id),
152
+ extensionRegistry,
153
+ hostCapabilityIds: extensionRegistry.capabilityRegistry.listProviderIds(),
154
+ installSources: createExtensionInstallSources(catalogEntries, availableExtensions),
155
+ installedRecords,
156
+ };
157
+ }
158
+
159
+ export function createCatalogEntryInstallPlan(
160
+ entry: Pick<ExtensionCatalogBrowseEntry, 'manifestUrl'>,
161
+ {
162
+ availableExtensions,
163
+ enabledExtensionIds,
164
+ hostCapabilityIds,
165
+ installedRecords,
166
+ installSources,
167
+ }: ExtensionInstallPlanningContext,
168
+ ): ExtensionInstallPlan | undefined {
169
+ const bundledExtension = resolveBundledExtensionByManifestUrl(
170
+ entry.manifestUrl,
171
+ availableExtensions,
172
+ );
173
+ if (!bundledExtension) {
174
+ return undefined;
175
+ }
176
+
177
+ return createExtensionInstallPlan({
178
+ availableExtensions,
179
+ enabledExtensionIds,
180
+ hostCapabilityIds,
181
+ installSources,
182
+ installedRecords,
183
+ targetExtensionId: bundledExtension.manifest.id,
184
+ });
185
+ }
186
+
187
+ function createExtensionInstallSources(
188
+ catalogEntries: readonly ExtensionCatalogEntry[],
189
+ availableExtensions: readonly WorkbenchExtensionDescription[],
190
+ ): readonly ExtensionInstallPlanInstallSource[] {
191
+ const byId = new Map<string, ExtensionInstallPlanInstallSource>();
192
+ for (const entry of catalogEntries) {
193
+ const bundledExtension = resolveBundledExtensionByManifestUrl(
194
+ entry.manifestUrl,
195
+ availableExtensions,
196
+ );
197
+ const extensionId = bundledExtension?.manifest.id ?? entry.id;
198
+ byId.set(extensionId, {
199
+ category: entry.category,
200
+ id: extensionId,
201
+ manifestUrl: entry.manifestUrl,
202
+ });
203
+ }
204
+
205
+ return [...byId.values()];
206
+ }
207
+
208
+ function mergeUniqueExtensionDescriptions(
209
+ descriptions: readonly WorkbenchExtensionDescription[],
210
+ ): readonly WorkbenchExtensionDescription[] {
211
+ const byId = new Map<string, WorkbenchExtensionDescription>();
212
+ for (const description of descriptions) {
213
+ byId.set(description.manifest.id, description);
214
+ }
215
+ return [...byId.values()];
216
+ }
217
+
218
+ function createExtensionManagementFeatureMaps(extensionRegistry: ExtensionRegistry) {
219
+ return {
220
+ bundledFeaturesById: new Map(
221
+ BUNDLED_EXTENSIONS.map((extension) => [
222
+ extension.manifest.id,
223
+ createExtensionFeatureSpec(extension),
224
+ ]),
225
+ ),
226
+ inspectionsById: new Map(
227
+ extensionRegistry
228
+ .getFeatureInspections()
229
+ .map((inspection) => [inspection.feature.id, inspection]),
230
+ ),
231
+ };
232
+ }
233
+
234
+ function resolveExtensionManagementFeatureState(
235
+ extensionId: string,
236
+ {
237
+ bundledFeaturesById,
238
+ inspectionsById,
239
+ }: {
240
+ bundledFeaturesById: ReadonlyMap<string, ExtensionFeatureSpec>;
241
+ inspectionsById: ReadonlyMap<string, ExtensionFeatureInspection>;
242
+ },
243
+ ): {
244
+ readonly diagnostics?: readonly ExtensionManagementDiagnosticSummary[] | undefined;
245
+ readonly features?: ExtensionManagementFeatureSummary | undefined;
246
+ } {
247
+ const inspection = inspectionsById.get(extensionId);
248
+ const feature = inspection?.feature ?? bundledFeaturesById.get(extensionId);
249
+
250
+ return {
251
+ diagnostics: inspection?.diagnostics.map(({ message, severity }) => ({ message, severity })),
252
+ features: feature ? toExtensionManagementFeatureSummary(feature) : undefined,
253
+ };
254
+ }
255
+
256
+ function toExtensionManagementFeatureSummary(
257
+ feature: ExtensionFeatureSpec,
258
+ ): ExtensionManagementFeatureSummary {
259
+ const commandTitlesById = new Map(feature.commands.map((command) => [command.id, command.title]));
260
+
261
+ return {
262
+ capabilities: feature.capabilities,
263
+ commands: feature.commands.map((command) => ({
264
+ description: command.description,
265
+ id: command.id,
266
+ label: command.title,
267
+ })),
268
+ documentViews: feature.documentViews.map((view) => ({
269
+ id: view.id,
270
+ label: view.label,
271
+ })),
272
+ menus: feature.menus.map((menu) => ({
273
+ description: formatMenuContributionDescription(menu.group, menu.order),
274
+ id: `${menu.menu}:${menu.command}`,
275
+ label: `${menu.menu}: ${commandTitlesById.get(menu.command) ?? menu.command}`,
276
+ })),
277
+ permissions: feature.permissions,
278
+ settings: feature.settings.map((setting) => ({
279
+ description: setting.description,
280
+ id: setting.key,
281
+ label: setting.key,
282
+ })),
283
+ views: feature.views.map((view) => ({
284
+ id: view.id,
285
+ label: view.name,
286
+ })),
287
+ };
288
+ }
289
+
290
+ function toExtensionInstallPlanSummary(
291
+ plan: ExtensionInstallPlan,
292
+ ): ExtensionCatalogBrowseEntry['installPlan'] {
293
+ return {
294
+ blocked: plan.blocked,
295
+ diagnostics: plan.diagnostics.map(({ message, severity }) => ({ message, severity })),
296
+ enableExtensionIds: plan.enableExtensionIds,
297
+ installExtensionIds: plan.installExtensionIds,
298
+ permissions: plan.permissions,
299
+ requiresApproval: plan.requiresApproval,
300
+ };
301
+ }
302
+
303
+ function formatMenuContributionDescription(
304
+ group: string | undefined,
305
+ order: number | undefined,
306
+ ): string | undefined {
307
+ const parts = [
308
+ group ? `group: ${group}` : undefined,
309
+ order !== undefined ? `order: ${order}` : undefined,
310
+ ].filter((part): part is string => part !== undefined);
311
+
312
+ return parts.length > 0 ? parts.join(', ') : undefined;
313
+ }
@@ -0,0 +1,19 @@
1
+ export const BUILTIN_EXTENSIONS_VIEW_RENDER_KIND = 'workbench-kit.builtin.extensions.view' as const;
2
+ export const BUILTIN_EXTENSIONS_VIEW_CONTAINER_ID = 'extensions' as const;
3
+ export const BUILTIN_EXTENSIONS_FOCUS_COMMAND_ID =
4
+ 'workbench-kit.builtin.extensions.focus' as const;
5
+
6
+ export interface BuiltinExtensionsViewRenderData {
7
+ readonly kind: typeof BUILTIN_EXTENSIONS_VIEW_RENDER_KIND;
8
+ }
9
+
10
+ export function isBuiltinExtensionsViewRenderData(
11
+ value: unknown,
12
+ ): value is BuiltinExtensionsViewRenderData {
13
+ return (
14
+ typeof value === 'object' &&
15
+ value !== null &&
16
+ 'kind' in value &&
17
+ (value as BuiltinExtensionsViewRenderData).kind === BUILTIN_EXTENSIONS_VIEW_RENDER_KIND
18
+ );
19
+ }
@@ -0,0 +1,38 @@
1
+ import { ExtensionManagementSidebar } from '@workbench-kit/react/workbench/management';
2
+
3
+ import {
4
+ BUILTIN_EXTENSIONS_VIEW_RENDER_KIND,
5
+ isBuiltinExtensionsViewRenderData,
6
+ type BuiltinExtensionsViewRenderData,
7
+ } from './extensions-view-data.js';
8
+ import { useExtensionManagementModel } from './use-extension-management.js';
9
+ import { useWorkbench } from './provider.js';
10
+
11
+ export type { BuiltinExtensionsViewRenderData };
12
+ export { BUILTIN_EXTENSIONS_VIEW_RENDER_KIND, isBuiltinExtensionsViewRenderData };
13
+
14
+ export function BuiltinExtensionsView({ catalogUrl }: { catalogUrl?: string | undefined }) {
15
+ const { missingExtensionIds } = useWorkbench();
16
+ const {
17
+ browseEntries,
18
+ catalogError,
19
+ catalogLoading,
20
+ installCatalogEntry,
21
+ installedEntries,
22
+ pendingAction,
23
+ toggleInstalledEntry,
24
+ } = useExtensionManagementModel({ catalogUrl });
25
+
26
+ return (
27
+ <ExtensionManagementSidebar
28
+ browseEntries={browseEntries}
29
+ catalogError={catalogError}
30
+ catalogLoading={catalogLoading}
31
+ installedEntries={installedEntries}
32
+ missingExtensionIds={missingExtensionIds}
33
+ pendingAction={pendingAction}
34
+ onInstall={installCatalogEntry}
35
+ onToggleEnabled={toggleInstalledEntry}
36
+ />
37
+ );
38
+ }
@@ -0,0 +1,18 @@
1
+ import type { JSX } from 'react';
2
+
3
+ import { FieldRemapPanel } from './field-remap-panel.js';
4
+ import { getFieldRemapSample, type FieldRemapSampleId } from './field-remap-samples.js';
5
+
6
+ export interface SampleFieldRemapDemoProps {
7
+ readonly sampleId?: FieldRemapSampleId | string | undefined;
8
+ }
9
+
10
+ /**
11
+ * Sample host wrapper around {@link FieldRemapPanel}.
12
+ */
13
+ export function SampleFieldRemapDemo({
14
+ sampleId = 'nested-ab',
15
+ }: SampleFieldRemapDemoProps = {}): JSX.Element {
16
+ const sample = getFieldRemapSample(sampleId);
17
+ return <FieldRemapPanel key={sample.id} sample={sample} />;
18
+ }
@@ -0,0 +1,36 @@
1
+ import type { JSX } from 'react';
2
+ import { ScrollArea } from '@workbench-kit/react/primitives';
3
+
4
+ import { SampleFieldRemapDemo } from './field-remap-demo.js';
5
+ import { resolveFieldRemapSampleId } from './field-remap-samples.js';
6
+ import './field-remap-view.css';
7
+
8
+ export type FieldRemapEditorSurfaceId = string;
9
+
10
+ export interface FieldRemapEditorSurfaceProps {
11
+ readonly resourceUri: string;
12
+ readonly surfaceId: FieldRemapEditorSurfaceId;
13
+ readonly tabId: string;
14
+ }
15
+
16
+ export function FieldRemapEditorSurface({
17
+ resourceUri,
18
+ surfaceId,
19
+ tabId,
20
+ }: FieldRemapEditorSurfaceProps): JSX.Element {
21
+ const sampleId = resolveFieldRemapSampleId(surfaceId);
22
+
23
+ return (
24
+ <ScrollArea
25
+ aria-label={`${sampleId} field remap`}
26
+ className="workbench-field-remap-editor-surface"
27
+ data-editor-host-id={tabId}
28
+ data-resource-uri={resourceUri}
29
+ data-sample-id={sampleId}
30
+ data-testid="field-remap-editor-surface"
31
+ orientation="vertical"
32
+ >
33
+ <SampleFieldRemapDemo sampleId={sampleId} />
34
+ </ScrollArea>
35
+ );
36
+ }
@@ -0,0 +1,240 @@
1
+ import type { Edge, Node } from '@xyflow/react';
2
+ import {
3
+ flattenSourceFields,
4
+ flattenTargetSlots,
5
+ type MappingEdge,
6
+ type SourceField,
7
+ type TargetSlot,
8
+ type ValueTransformRegistry,
9
+ } from '@workbench-kit/field-remap';
10
+
11
+ export type FieldRemapFlowNodeKind = 'source-object' | 'target-object' | 'transform';
12
+
13
+ export type FieldRemapPort = {
14
+ fieldId: string;
15
+ label: string;
16
+ dataType?: string;
17
+ };
18
+
19
+ /** XYFlow requires node/edge `data` to extend `Record<string, unknown>`. */
20
+ export type FieldRemapSourceObjectNodeData = {
21
+ kind: 'source-object';
22
+ title: string;
23
+ ports: FieldRemapPort[];
24
+ } & Record<string, unknown>;
25
+
26
+ export type FieldRemapTargetObjectNodeData = {
27
+ kind: 'target-object';
28
+ title: string;
29
+ ports: FieldRemapPort[];
30
+ } & Record<string, unknown>;
31
+
32
+ export type FieldRemapTransformNodeData = {
33
+ kind: 'transform';
34
+ mappingEdgeId: string;
35
+ stepIndex: number;
36
+ transformId: string;
37
+ label: string;
38
+ } & Record<string, unknown>;
39
+
40
+ export type FieldRemapFlowNodeData =
41
+ FieldRemapSourceObjectNodeData | FieldRemapTargetObjectNodeData | FieldRemapTransformNodeData;
42
+
43
+ export type FieldRemapFlowEdgeData = {
44
+ mappingEdgeId: string;
45
+ segment: 'in' | 'mid' | 'out' | 'direct';
46
+ } & Record<string, unknown>;
47
+
48
+ export const SOURCE_OBJECT_NODE_ID = 'obj:source' as const;
49
+ export const TARGET_OBJECT_NODE_ID = 'obj:target' as const;
50
+
51
+ const SOURCE_X = 24;
52
+ const TRANSFORM_X = 320;
53
+ const TARGET_X = 620;
54
+ const TRANSFORM_ROW_GAP = 72;
55
+
56
+ export function sourceObjectNodeId(): string {
57
+ return SOURCE_OBJECT_NODE_ID;
58
+ }
59
+
60
+ export function targetObjectNodeId(): string {
61
+ return TARGET_OBJECT_NODE_ID;
62
+ }
63
+
64
+ /** Handle id for a field/slot port on an object node. */
65
+ export function portHandleId(fieldOrSlotId: string): string {
66
+ return fieldOrSlotId;
67
+ }
68
+
69
+ export function transformNodeId(mappingEdgeId: string, stepIndex: number): string {
70
+ return `xf:${mappingEdgeId}:${stepIndex}`;
71
+ }
72
+
73
+ function fieldLabel(field: SourceField | TargetSlot): string {
74
+ return field.path ?? field.label;
75
+ }
76
+
77
+ function toPorts(fields: readonly (SourceField | TargetSlot)[]): FieldRemapPort[] {
78
+ return fields.map((field) => ({
79
+ fieldId: field.id,
80
+ label: fieldLabel(field),
81
+ dataType: field.dataType,
82
+ }));
83
+ }
84
+
85
+ /**
86
+ * Build React Flow nodes/edges from kit shapes + MappingEdge[].
87
+ * Layout: one multi-port source object, transform chain, one multi-port target object.
88
+ */
89
+ export function mappingToFlowGraph(input: {
90
+ readonly sources: readonly SourceField[];
91
+ readonly targets: readonly TargetSlot[];
92
+ readonly edges: readonly MappingEdge[];
93
+ readonly transforms: ValueTransformRegistry;
94
+ readonly sourceTitle?: string;
95
+ readonly targetTitle?: string;
96
+ }): { nodes: Node<FieldRemapFlowNodeData>[]; edges: Edge<FieldRemapFlowEdgeData>[] } {
97
+ const sourcePorts = toPorts(flattenSourceFields(input.sources));
98
+ const targetPorts = toPorts(flattenTargetSlots(input.targets));
99
+
100
+ const nodes: Node<FieldRemapFlowNodeData>[] = [
101
+ {
102
+ id: SOURCE_OBJECT_NODE_ID,
103
+ type: 'fieldRemapSourceObject',
104
+ position: { x: SOURCE_X, y: 24 },
105
+ data: {
106
+ kind: 'source-object',
107
+ title: input.sourceTitle?.trim() || 'Source',
108
+ ports: sourcePorts,
109
+ },
110
+ draggable: true,
111
+ },
112
+ {
113
+ id: TARGET_OBJECT_NODE_ID,
114
+ type: 'fieldRemapTargetObject',
115
+ position: { x: TARGET_X, y: 24 },
116
+ data: {
117
+ kind: 'target-object',
118
+ title: input.targetTitle?.trim() || 'Target',
119
+ ports: targetPorts,
120
+ },
121
+ draggable: true,
122
+ },
123
+ ];
124
+
125
+ const flowEdges: Edge<FieldRemapFlowEdgeData>[] = [];
126
+
127
+ input.edges.forEach((edge, edgeIndex) => {
128
+ const chain = edge.transformIds ?? [];
129
+ const baseY = 40 + edgeIndex * TRANSFORM_ROW_GAP;
130
+
131
+ if (chain.length === 0) {
132
+ flowEdges.push({
133
+ id: `fe:${edge.id}:direct`,
134
+ source: SOURCE_OBJECT_NODE_ID,
135
+ sourceHandle: portHandleId(edge.sourceFieldId),
136
+ target: TARGET_OBJECT_NODE_ID,
137
+ targetHandle: portHandleId(edge.targetSlotId),
138
+ type: 'smoothstep',
139
+ data: { mappingEdgeId: edge.id, segment: 'direct' },
140
+ });
141
+ return;
142
+ }
143
+
144
+ chain.forEach((transformId, stepIndex) => {
145
+ const definition = input.transforms.get(transformId);
146
+ nodes.push({
147
+ id: transformNodeId(edge.id, stepIndex),
148
+ type: 'fieldRemapTransform',
149
+ position: {
150
+ x: TRANSFORM_X + stepIndex * 36,
151
+ y: baseY + stepIndex * 48,
152
+ },
153
+ data: {
154
+ kind: 'transform',
155
+ mappingEdgeId: edge.id,
156
+ stepIndex,
157
+ transformId,
158
+ label: definition?.label ?? transformId,
159
+ },
160
+ draggable: true,
161
+ });
162
+ });
163
+
164
+ flowEdges.push({
165
+ id: `fe:${edge.id}:in`,
166
+ source: SOURCE_OBJECT_NODE_ID,
167
+ sourceHandle: portHandleId(edge.sourceFieldId),
168
+ target: transformNodeId(edge.id, 0),
169
+ targetHandle: 'in',
170
+ type: 'smoothstep',
171
+ data: { mappingEdgeId: edge.id, segment: 'in' },
172
+ });
173
+
174
+ for (let stepIndex = 0; stepIndex < chain.length - 1; stepIndex += 1) {
175
+ flowEdges.push({
176
+ id: `fe:${edge.id}:mid:${stepIndex}`,
177
+ source: transformNodeId(edge.id, stepIndex),
178
+ sourceHandle: 'out',
179
+ target: transformNodeId(edge.id, stepIndex + 1),
180
+ targetHandle: 'in',
181
+ type: 'smoothstep',
182
+ data: { mappingEdgeId: edge.id, segment: 'mid' },
183
+ });
184
+ }
185
+
186
+ flowEdges.push({
187
+ id: `fe:${edge.id}:out`,
188
+ source: transformNodeId(edge.id, chain.length - 1),
189
+ sourceHandle: 'out',
190
+ target: TARGET_OBJECT_NODE_ID,
191
+ targetHandle: portHandleId(edge.targetSlotId),
192
+ type: 'smoothstep',
193
+ data: { mappingEdgeId: edge.id, segment: 'out' },
194
+ });
195
+ });
196
+
197
+ return { nodes, edges: flowEdges };
198
+ }
199
+
200
+ /** Parse React Flow connection (object ports or legacy) into a kit MappingEdge. */
201
+ export function connectionToMappingEdge(input: {
202
+ readonly sourceNodeId: string;
203
+ readonly targetNodeId: string;
204
+ readonly sourceHandle?: string | null;
205
+ readonly targetHandle?: string | null;
206
+ readonly existing: readonly MappingEdge[];
207
+ }): MappingEdge | null {
208
+ let sourceFieldId: string | undefined;
209
+ let targetSlotId: string | undefined;
210
+
211
+ if (input.sourceNodeId === SOURCE_OBJECT_NODE_ID && input.sourceHandle) {
212
+ sourceFieldId = input.sourceHandle;
213
+ } else if (input.sourceNodeId.startsWith('src:')) {
214
+ // Legacy single-field node ids (older graphs / tests).
215
+ sourceFieldId = input.sourceNodeId.slice('src:'.length);
216
+ }
217
+
218
+ if (input.targetNodeId === TARGET_OBJECT_NODE_ID && input.targetHandle) {
219
+ targetSlotId = input.targetHandle;
220
+ } else if (input.targetNodeId.startsWith('tgt:')) {
221
+ targetSlotId = input.targetNodeId.slice('tgt:'.length);
222
+ }
223
+
224
+ if (!sourceFieldId || !targetSlotId) {
225
+ return null;
226
+ }
227
+
228
+ const existing = input.existing.find((edge) => edge.targetSlotId === targetSlotId);
229
+ return {
230
+ id: existing?.id ?? `e-${sourceFieldId}-${targetSlotId}-${Date.now()}`,
231
+ sourceFieldId,
232
+ targetSlotId,
233
+ ...(existing?.transformIds ? { transformIds: existing.transformIds } : {}),
234
+ ...(existing?.transformOptionSteps
235
+ ? { transformOptionSteps: existing.transformOptionSteps }
236
+ : {}),
237
+ ...(existing?.itemSourcePath ? { itemSourcePath: existing.itemSourcePath } : {}),
238
+ ...(existing?.itemEdges ? { itemEdges: existing.itemEdges } : {}),
239
+ };
240
+ }