@xemahq/ui-kernel 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/dist/errors/http-error-status.d.ts +17 -0
  2. package/dist/errors/http-error-status.d.ts.map +1 -0
  3. package/dist/errors/http-error-status.js +59 -0
  4. package/dist/errors/http-error-status.js.map +1 -0
  5. package/dist/errors/index.d.ts +2 -0
  6. package/dist/errors/index.d.ts.map +1 -0
  7. package/dist/errors/index.js +2 -0
  8. package/dist/errors/index.js.map +1 -0
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1 -0
  12. package/dist/index.js.map +1 -1
  13. package/dist/lib/biome-host/errors.d.ts.map +1 -1
  14. package/dist/lib/biome-host/errors.js +1 -19
  15. package/dist/lib/biome-host/errors.js.map +1 -1
  16. package/dist/offline/index.d.ts.map +1 -1
  17. package/dist/offline/index.js.map +1 -1
  18. package/dist/offline/online-controller.d.ts.map +1 -1
  19. package/dist/offline/online-controller.js +6 -1
  20. package/dist/offline/online-controller.js.map +1 -1
  21. package/dist/offline/outbox.d.ts.map +1 -1
  22. package/dist/offline/outbox.js +2 -24
  23. package/dist/offline/outbox.js.map +1 -1
  24. package/package.json +1 -2
  25. package/src/index.ts +0 -17
  26. package/src/lib/biome-host/agent-validation.ts +0 -239
  27. package/src/lib/biome-host/biome-builders.ts +0 -112
  28. package/src/lib/biome-host/biome-mode.ts +0 -6
  29. package/src/lib/biome-host/biome-navigation.ts +0 -37
  30. package/src/lib/biome-host/biome-registry.ts +0 -299
  31. package/src/lib/biome-host/biome-scope-hooks.ts +0 -41
  32. package/src/lib/biome-host/biome-scope.tsx +0 -52
  33. package/src/lib/biome-host/biome-scoped-query.ts +0 -134
  34. package/src/lib/biome-host/create-biome-orval-config.ts +0 -82
  35. package/src/lib/biome-host/define-web-biome.spec.ts +0 -221
  36. package/src/lib/biome-host/define-web-biome.ts +0 -222
  37. package/src/lib/biome-host/errors.ts +0 -220
  38. package/src/lib/biome-host/frontend-biome.ts +0 -256
  39. package/src/lib/biome-host/host-bridge.ts +0 -281
  40. package/src/lib/biome-host/host-sources.ts +0 -54
  41. package/src/lib/biome-host/i18n-hooks.ts +0 -51
  42. package/src/lib/biome-host/i18n-port.test.ts +0 -50
  43. package/src/lib/biome-host/i18n-port.ts +0 -96
  44. package/src/lib/biome-host/index.ts +0 -35
  45. package/src/lib/biome-host/realtime-hooks.ts +0 -75
  46. package/src/lib/biome-host/realtime-port.ts +0 -115
  47. package/src/lib/biome-host/session-contributions.ts +0 -296
  48. package/src/lib/biome-host/use-mutation-with-error-toast.ts +0 -90
  49. package/src/lib/biome-host/use-page-state.ts +0 -248
  50. package/src/lib/capabilities/capability-context.ts +0 -25
  51. package/src/lib/capabilities/capability-provider.tsx +0 -73
  52. package/src/lib/capabilities/index.ts +0 -17
  53. package/src/lib/capabilities/types.ts +0 -69
  54. package/src/lib/capabilities/use-capability.ts +0 -79
  55. package/src/lib/contribution-registry/contribution-registry.spec.ts +0 -141
  56. package/src/lib/contribution-registry/contribution-registry.ts +0 -204
  57. package/src/lib/contribution-registry/index.ts +0 -9
  58. package/src/lib/contribution-registry/renderable-registry.ts +0 -142
  59. package/src/lib/system-bus/capability-invoke.ts +0 -92
  60. package/src/lib/system-bus/deeplink.ts +0 -210
  61. package/src/lib/system-bus/enums.ts +0 -86
  62. package/src/lib/system-bus/host-ports.ts +0 -93
  63. package/src/lib/system-bus/index.ts +0 -16
  64. package/src/lib/system-bus/intent-registry.ts +0 -109
  65. package/src/lib/system-bus/intents.ts +0 -113
  66. package/src/lib/system-bus/palette.ts +0 -78
  67. package/src/lib/system-bus/system-bus-builder.ts +0 -159
  68. package/src/lib/system-bus/system-bus.ts +0 -37
  69. package/src/lib/system-bus/windows.ts +0 -51
  70. package/src/offline/id.ts +0 -16
  71. package/src/offline/index.ts +0 -54
  72. package/src/offline/mutation.ts +0 -75
  73. package/src/offline/offline-status-badge.tsx +0 -51
  74. package/src/offline/online-controller.ts +0 -105
  75. package/src/offline/outbox.test.ts +0 -172
  76. package/src/offline/outbox.ts +0 -348
  77. package/src/offline/react-hooks.ts +0 -40
  78. package/src/offline/react.tsx +0 -32
  79. package/src/offline/storage.ts +0 -112
  80. package/src/offline/types.ts +0 -77
  81. package/src/org-db/components/DbResultTable.tsx +0 -176
  82. package/src/org-db/index.ts +0 -1
  83. package/src/pdf/index.ts +0 -11
  84. package/src/registry/index.ts +0 -9
  85. package/src/registry/lib/agent-validation-host.ts +0 -36
  86. package/src/registry/lib/biome-slot.tsx +0 -50
  87. package/src/registry/lib/biomes-enabled-context.ts +0 -20
  88. package/src/registry/lib/extension-points.ts +0 -134
  89. package/src/registry/lib/primitives/SessionEventCard.tsx +0 -164
  90. package/src/registry/lib/primitives/SessionEventTimeline.tsx +0 -95
  91. package/src/registry/lib/primitives/SessionMutationBar.tsx +0 -76
  92. package/src/registry/lib/primitives/SessionTabbedDrawer.tsx +0 -165
  93. package/src/registry/lib/primitives/index.ts +0 -18
  94. package/src/registry/lib/session-context-builder.ts +0 -84
  95. package/src/registry/lib/session-context-provider.tsx +0 -33
  96. package/src/registry/lib/session-context.ts +0 -21
  97. package/src/registry/lib/session-selectors.ts +0 -272
  98. package/src/session/comments/CommentRail.tsx +0 -164
  99. package/src/session/index.ts +0 -3
  100. package/src/session/lib/cn.ts +0 -11
  101. package/src/session/shell/SessionWorkspaceShell.tsx +0 -147
  102. package/src/session-kit/approvals/ApprovalButton.tsx +0 -89
  103. package/src/session-kit/approvals/ApprovalCard.tsx +0 -341
  104. package/src/session-kit/approvals/ApprovalsCenter.tsx +0 -336
  105. package/src/session-kit/approvals/CapabilityApprovalStickyBar.tsx +0 -118
  106. package/src/session-kit/approvals/Obligation.tsx +0 -111
  107. package/src/session-kit/approvals/ScopedBody.tsx +0 -392
  108. package/src/session-kit/approvals/approval-icons.ts +0 -31
  109. package/src/session-kit/approvals/approval-model.ts +0 -204
  110. package/src/session-kit/approvals/index.ts +0 -22
  111. package/src/session-kit/approvals/obligation-display.ts +0 -100
  112. package/src/session-kit/approvals/risk-accent.ts +0 -47
  113. package/src/session-kit/approvals/scope-icons.ts +0 -19
  114. package/src/session-kit/combobox/Combobox.tsx +0 -358
  115. package/src/session-kit/combobox/use-click-outside.ts +0 -21
  116. package/src/session-kit/display/ContextHeader.tsx +0 -148
  117. package/src/session-kit/display/FileDiffCard.tsx +0 -140
  118. package/src/session-kit/display/MD.tsx +0 -162
  119. package/src/session-kit/display/MessageTurn.tsx +0 -157
  120. package/src/session-kit/display/ThinkingPanel.tsx +0 -81
  121. package/src/session-kit/display/TodoChecklist.tsx +0 -120
  122. package/src/session-kit/display/TokenMeter.tsx +0 -89
  123. package/src/session-kit/display/ToolStrip.tsx +0 -278
  124. package/src/session-kit/display/TypingDots.tsx +0 -24
  125. package/src/session-kit/index.ts +0 -44
  126. package/src/session-kit/lib/enums.ts +0 -66
  127. package/src/session-kit/lib/portal-accent.ts +0 -30
  128. package/src/session-kit/lib/status-dot.ts +0 -68
  129. package/src/session-kit/primitives/Avatar.tsx +0 -44
  130. package/src/session-kit/primitives/PortalGlyph.tsx +0 -51
  131. package/src/session-kit/primitives/RiskPill.tsx +0 -95
  132. package/src/session-kit/primitives/ScopeDot.tsx +0 -47
  133. package/src/session-kit/primitives/Segmented.tsx +0 -71
  134. package/src/ui/branding/app-branding-context.ts +0 -20
  135. package/src/ui/branding/app-branding-provider.tsx +0 -123
  136. package/src/ui/branding/sanitize.ts +0 -89
  137. package/src/ui/breakpoints.ts +0 -14
  138. package/src/ui/capture/document-preview.tsx +0 -166
  139. package/src/ui/capture/field-spec-to-zod.test.ts +0 -81
  140. package/src/ui/capture/field-spec-to-zod.ts +0 -174
  141. package/src/ui/capture/field-spec-validate.ts +0 -236
  142. package/src/ui/capture/field-spec.test.ts +0 -49
  143. package/src/ui/capture/field-spec.ts +0 -274
  144. package/src/ui/capture/index.ts +0 -51
  145. package/src/ui/capture/schema-field-input.tsx +0 -620
  146. package/src/ui/capture/schema-form.tsx +0 -149
  147. package/src/ui/capture/submit-values.test.ts +0 -70
  148. package/src/ui/capture/submit-values.ts +0 -63
  149. package/src/ui/capture/use-prefill.ts +0 -186
  150. package/src/ui/capture/use-schema-validation.ts +0 -145
  151. package/src/ui/capture/use-wizard.ts +0 -180
  152. package/src/ui/capture/value-format.ts +0 -32
  153. package/src/ui/capture/wizard.tsx +0 -105
  154. package/src/ui/charts/bar-chart.tsx +0 -139
  155. package/src/ui/charts/chart-frame.tsx +0 -125
  156. package/src/ui/charts/chart-types.ts +0 -52
  157. package/src/ui/charts/chart-utils.ts +0 -190
  158. package/src/ui/charts/charts.test.tsx +0 -204
  159. package/src/ui/charts/donut-chart.tsx +0 -129
  160. package/src/ui/charts/index.ts +0 -27
  161. package/src/ui/charts/line-chart.tsx +0 -202
  162. package/src/ui/charts/sparkline.tsx +0 -109
  163. package/src/ui/chrome/AsyncBoundary.tsx +0 -74
  164. package/src/ui/chrome/EmptyState.tsx +0 -257
  165. package/src/ui/chrome/ErrorCard.tsx +0 -63
  166. package/src/ui/chrome/LoadingState.tsx +0 -61
  167. package/src/ui/chrome/PageHeader.tsx +0 -137
  168. package/src/ui/chrome/RecordCardList.tsx +0 -98
  169. package/src/ui/chrome/StateCard.tsx +0 -150
  170. package/src/ui/chrome/confirm-delete-dialog.tsx +0 -59
  171. package/src/ui/chrome/page-shell.spec.tsx +0 -117
  172. package/src/ui/chrome/page-shell.tsx +0 -221
  173. package/src/ui/chrome/scope-badge.tsx +0 -40
  174. package/src/ui/chrome/status-badge.tsx +0 -79
  175. package/src/ui/cn.ts +0 -32
  176. package/src/ui/design-tokens.ts +0 -372
  177. package/src/ui/hooks/use-debounced-value.ts +0 -16
  178. package/src/ui/hooks/use-is-mobile.ts +0 -28
  179. package/src/ui/index.ts +0 -110
  180. package/src/ui/pdf/pdf-preview.tsx +0 -122
  181. package/src/ui/primitives/alert-dialog.tsx +0 -104
  182. package/src/ui/primitives/async-combobox.tsx +0 -182
  183. package/src/ui/primitives/badge-variants.ts +0 -21
  184. package/src/ui/primitives/badge.tsx +0 -25
  185. package/src/ui/primitives/button-variants.ts +0 -31
  186. package/src/ui/primitives/button.tsx +0 -29
  187. package/src/ui/primitives/card.tsx +0 -43
  188. package/src/ui/primitives/checkbox.tsx +0 -26
  189. package/src/ui/primitives/collapsible.tsx +0 -9
  190. package/src/ui/primitives/dialog.tsx +0 -103
  191. package/src/ui/primitives/dropdown-menu.tsx +0 -179
  192. package/src/ui/primitives/form-stepper.tsx +0 -94
  193. package/src/ui/primitives/form-stepper.types.ts +0 -15
  194. package/src/ui/primitives/input.tsx +0 -22
  195. package/src/ui/primitives/label.tsx +0 -17
  196. package/src/ui/primitives/overflow-tabs.tsx +0 -331
  197. package/src/ui/primitives/popover.tsx +0 -33
  198. package/src/ui/primitives/preview-device-frame.tsx +0 -213
  199. package/src/ui/primitives/radio-group.tsx +0 -36
  200. package/src/ui/primitives/resizable.tsx +0 -67
  201. package/src/ui/primitives/scroll-area.tsx +0 -38
  202. package/src/ui/primitives/select.tsx +0 -143
  203. package/src/ui/primitives/separator.tsx +0 -20
  204. package/src/ui/primitives/sheet.tsx +0 -107
  205. package/src/ui/primitives/skeleton.tsx +0 -99
  206. package/src/ui/primitives/switch.tsx +0 -27
  207. package/src/ui/primitives/table.tsx +0 -72
  208. package/src/ui/primitives/tabs.tsx +0 -53
  209. package/src/ui/primitives/tag-multi-select.tsx +0 -261
  210. package/src/ui/primitives/textarea.tsx +0 -21
  211. package/src/ui/primitives/tooltip.tsx +0 -30
  212. package/src/ui/primitives/url-preview-chrome.tsx +0 -103
  213. package/src/vista/GenericCatalogueSurface.tsx +0 -179
  214. package/src/vista/SurfaceFallbackCard.tsx +0 -109
  215. package/src/vista/VistaPanel.tsx +0 -163
  216. package/src/vista/form-payload.spec.ts +0 -139
  217. package/src/vista/html/CustomUiFrame.spec.tsx +0 -127
  218. package/src/vista/html/CustomUiFrame.tsx +0 -206
  219. package/src/vista/html/custom-ui-frame-bridge.spec.ts +0 -191
  220. package/src/vista/html/custom-ui-frame-bridge.ts +0 -98
  221. package/src/vista/html/custom-ui-frame-contract.ts +0 -88
  222. package/src/vista/html/custom-ui-intents.spec.ts +0 -135
  223. package/src/vista/html/custom-ui-intents.ts +0 -155
  224. package/src/vista/html/custom-ui-srcdoc.spec.ts +0 -151
  225. package/src/vista/html/custom-ui-srcdoc.ts +0 -193
  226. package/src/vista/html/index.ts +0 -13
  227. package/src/vista/index.ts +0 -52
  228. package/src/vista/surface-catalogue.spec.ts +0 -140
  229. package/src/vista/surface-catalogue.ts +0 -90
  230. package/src/vista/surface-icons.ts +0 -57
  231. package/src/vista/surface-registry.ts +0 -56
  232. package/src/vista/surfaces/ChartSurface.tsx +0 -34
  233. package/src/vista/surfaces/FilesSurface.tsx +0 -202
  234. package/src/vista/surfaces/FormSurface.tsx +0 -380
  235. package/src/vista/surfaces/HtmlSurface.tsx +0 -43
  236. package/src/vista/surfaces/TableSurface.tsx +0 -100
  237. package/src/vista/surfaces/UrlSurface.tsx +0 -40
  238. package/src/vista/surfaces/blob-surfaces.tsx +0 -139
  239. package/src/vista/surfaces/elicitation-transport.spec.ts +0 -369
  240. package/src/vista/surfaces/elicitation-transport.ts +0 -367
  241. package/src/vista/surfaces/file-surface-utils.ts +0 -69
  242. package/src/vista/surfaces/index.ts +0 -24
  243. package/src/vista/surfaces/register-surfaces.tsx +0 -139
  244. package/src/vista/surfaces/text-surfaces.tsx +0 -132
  245. package/src/vista/use-vista-tabs.ts +0 -196
  246. package/src/vista/vista-code-renderer-context.ts +0 -16
  247. package/src/vista/vista-code-renderer.tsx +0 -35
  248. package/src/vista/vista-file-context.ts +0 -14
  249. package/src/vista/vista-file-source.tsx +0 -141
  250. package/src/vista/vista-form-context.ts +0 -20
  251. package/src/vista/vista-form-host.tsx +0 -48
  252. package/src/vista/vista-model.ts +0 -137
  253. package/src/vista/vista-open-context.tsx +0 -27
  254. package/src/vista/vista-open-state.ts +0 -11
  255. package/src/vista/vista-payloads.ts +0 -635
  256. package/src/vista/vista-tabs-manifest.ts +0 -138
  257. package/src/vista/vista-tools.ts +0 -232
@@ -1,138 +0,0 @@
1
- /**
2
- * `.tabs.json` — the agent-authored tab manifest for a file-backed Vista panel.
3
- *
4
- * Lifted VERBATIM (guards included) out of the 4970-line Design-System-Builder
5
- * page, generalised over the artifact prefix, and re-keyed onto the kernel
6
- * {@link SurfaceKind} (Canopy §8.5). It is the ONE parser for this format; the
7
- * page no longer carries its own.
8
- *
9
- * ── The guards (all preserved, all fail-closed) ─────────────────────────────
10
- * 1. `version` MUST be exactly `1`.
11
- * 2. `tabs` MUST be an array.
12
- * 3. Every entry's `path` MUST be a string INSIDE the caller's artifact
13
- * prefix — an agent cannot point a tab at an arbitrary workspace file.
14
- * 4. No `..` anywhere in a path — no traversal out of the prefix.
15
- * 5. A declared `kind` MUST be a member of {@link DECLARABLE_SURFACE_KINDS}.
16
- * {@link SurfaceKind.Files} is UI-owned (the pinned browser tab) and can
17
- * NEVER be declared by an agent.
18
- * 6. ANY violation rejects the WHOLE manifest (returns `null`) — the caller
19
- * falls back to auto-discovery. A malformed file behaves exactly as an
20
- * absent one; there is no partial acceptance and no silent coercion.
21
- *
22
- * Kind vocabulary note: the legacy `plain` literal is GONE. Plain text is
23
- * {@link SurfaceKind.Code} (a mono pane) — one kind, one renderer, no alias.
24
- */
25
- import { SurfaceKind } from '@xemahq/kernel-contracts/surface';
26
-
27
- import { surfaceKindForPath } from './vista-model';
28
-
29
- /**
30
- * The kinds an agent may declare in `.tabs.json`. Every one is file-backed.
31
- * `files` is deliberately absent (UI-owned); so are the host-coupled kinds
32
- * (`web`, `biome-route`, `object`, `graph`, `app`, `form`), which are not
33
- * addressable by a workspace path and are opened through `vista_open` instead.
34
- */
35
- export const DECLARABLE_SURFACE_KINDS: readonly SurfaceKind[] = [
36
- SurfaceKind.Html,
37
- SurfaceKind.Markdown,
38
- SurfaceKind.Code,
39
- SurfaceKind.Json,
40
- SurfaceKind.Diff,
41
- SurfaceKind.Pdf,
42
- SurfaceKind.Image,
43
- SurfaceKind.Media,
44
- ];
45
-
46
- /** One declared tab. */
47
- export interface VistaTabSpec {
48
- readonly path: string;
49
- readonly kind: SurfaceKind;
50
- readonly label?: string;
51
- }
52
-
53
- /** The parsed manifest. */
54
- export interface VistaTabsManifest {
55
- readonly version: 1;
56
- readonly tabs: readonly VistaTabSpec[];
57
- /** Path of the tab the agent wants selected on open. */
58
- readonly selectedPath?: string;
59
- }
60
-
61
- /** Options for {@link parseVistaTabsManifest}. */
62
- export interface VistaTabsManifestOptions {
63
- /**
64
- * Prefix every declared path MUST start with (e.g. `'deliverables/'`). The
65
- * containment guard — an agent can only curate tabs over its own artifacts.
66
- */
67
- readonly pathPrefix: string;
68
- }
69
-
70
- function parseEntry(entry: unknown, pathPrefix: string): VistaTabSpec | null {
71
- if (!entry || typeof entry !== 'object') {
72
- return null;
73
- }
74
- const record = entry as Record<string, unknown>;
75
- if (typeof record.path !== 'string') {
76
- return null;
77
- }
78
- // Containment: inside the artifact prefix, and no traversal out of it.
79
- if (!record.path.startsWith(pathPrefix)) {
80
- return null;
81
- }
82
- if (record.path.includes('..')) {
83
- return null;
84
- }
85
- const kind =
86
- typeof record.kind === 'string'
87
- ? (record.kind as SurfaceKind)
88
- : surfaceKindForPath(record.path);
89
- // `files` is UI-owned (the pinned file browser) and must never be declared by
90
- // the agent — an out-of-set kind rejects the whole manifest.
91
- if (!DECLARABLE_SURFACE_KINDS.includes(kind)) {
92
- return null;
93
- }
94
- const label = typeof record.label === 'string' ? record.label : undefined;
95
- return label === undefined
96
- ? { path: record.path, kind }
97
- : { path: record.path, kind, label };
98
- }
99
-
100
- /**
101
- * Parse a `.tabs.json` body. Returns `null` on ANY shape violation — the caller
102
- * falls back to auto-discovery. No silent coercion: a malformed file is as if
103
- * it were absent (the user sees the default view instead of a broken state).
104
- */
105
- export function parseVistaTabsManifest(
106
- raw: string,
107
- { pathPrefix }: VistaTabsManifestOptions,
108
- ): VistaTabsManifest | null {
109
- let parsed: unknown;
110
- try {
111
- parsed = JSON.parse(raw) as unknown;
112
- } catch {
113
- return null;
114
- }
115
- if (!parsed || typeof parsed !== 'object') {
116
- return null;
117
- }
118
- const record = parsed as Record<string, unknown>;
119
- if (record.version !== 1) {
120
- return null;
121
- }
122
- if (!Array.isArray(record.tabs)) {
123
- return null;
124
- }
125
- const tabs: VistaTabSpec[] = [];
126
- for (const entry of record.tabs) {
127
- const tab = parseEntry(entry, pathPrefix);
128
- if (tab === null) {
129
- return null;
130
- }
131
- tabs.push(tab);
132
- }
133
- const selectedPath =
134
- typeof record.selectedPath === 'string' ? record.selectedPath : undefined;
135
- return selectedPath === undefined
136
- ? { version: 1, tabs }
137
- : { version: 1, tabs, selectedPath };
138
- }
@@ -1,232 +0,0 @@
1
- /**
2
- * The agent-facing Vista tool contract — `vista_open` / `vista_update` /
3
- * `vista_close` / `vista_list` (Canopy §8.2).
4
- *
5
- * This module is the BROWSER-side definition of those four calls: their names,
6
- * their argument shapes, their result shapes, and the dispatcher that performs
7
- * them against a {@link VistaTabsController}.
8
- *
9
- * ── One wire contract, two projections ──────────────────────────────────────
10
- * The server half lives in `canopy-api`'s `vista/vista-tool.contract.ts`. The
11
- * two files are VALUE-IDENTICAL by construction — same tool names, same
12
- * argument shapes, same {@link VistaToolError} members, same result envelope —
13
- * and both derive the `vista_open` argument from the kernel's
14
- * `VistaSurfaceSchema`, which is the single structural source of truth. They
15
- * are two projections rather than one import because this package is a React
16
- * UI package (a NestJS service must not depend on it) and the kernel contracts
17
- * package does not yet carry the three tiny argument schemas. Consolidating
18
- * them into `@xemahq/kernel-contracts/surface` is the correct end state; until
19
- * then, a change to either file MUST be mirrored in the other in the same
20
- * change-set, exactly like the curated `ConnectorKind` parity rule.
21
- *
22
- * ── How a call gets here ────────────────────────────────────────────────────
23
- * Vista is a BROWSER surface: no server can render a tab. So the backend never
24
- * "performs" these tools — it VALIDATES, applies its own tab projection,
25
- * RELAYS the call to the browser over the EXISTING agent-session realtime
26
- * channel, and returns the browser's {@link VistaToolResult} to the model
27
- * verbatim. A session where no panel answers returns
28
- * `{ ok: false, error: 'no-panel' }` — an explicit refusal, never a silent
29
- * success.
30
- *
31
- * The host binds `dispatchVistaTool` to its panel's controller and feeds it the
32
- * relayed `{ tool, args }`; see the host's `use-vista-tool-bridge`.
33
- */
34
- import { VistaSurfaceSchema } from '@xemahq/kernel-contracts/surface';
35
- import { z } from 'zod';
36
-
37
- import type { VistaSurfacePatch, VistaTabsController } from './use-vista-tabs';
38
-
39
- /** The closed set of Vista tools. Values are the wire tool names. */
40
- export enum VistaToolName {
41
- Open = 'vista_open',
42
- Update = 'vista_update',
43
- Close = 'vista_close',
44
- List = 'vista_list',
45
- }
46
-
47
- /**
48
- * `vista_open(surface) -> { tabId }`.
49
- *
50
- * The argument IS the kernel tab envelope — not a wrapper around it — so the
51
- * JSON Schema the model is shown is exactly `VistaSurface`. `kind` is an open
52
- * string on purpose: an unrecognised kind is NOT an error, it opens a tab that
53
- * renders the fallback card.
54
- */
55
- export const VistaOpenArgsSchema = VistaSurfaceSchema;
56
- export type VistaOpenArgs = z.infer<typeof VistaOpenArgsSchema>;
57
-
58
- /**
59
- * `vista_update(tabId, …) -> { updated: true }`.
60
- *
61
- * Flat, and deliberately narrower than {@link VistaSurfacePatch}: `kind` and
62
- * `version` are NOT patchable, because the browser has already resolved a
63
- * renderer for the live tab and swapping the kind under it would strand that
64
- * renderer. Close and re-open instead. Every remaining field is optional; an
65
- * empty patch is a no-op, not an error.
66
- */
67
- export const VistaUpdateArgsSchema = z.object({
68
- tabId: z.string().min(1),
69
- title: z.string().min(1).optional(),
70
- payload: z.unknown().optional(),
71
- pinned: z.boolean().optional(),
72
- });
73
- export type VistaUpdateArgs = z.infer<typeof VistaUpdateArgsSchema>;
74
-
75
- /** `vista_close(tabId) -> { closed: true }`. A pinned tab refuses to close. */
76
- export const VistaCloseArgsSchema = z.object({ tabId: z.string().min(1) });
77
- export type VistaCloseArgs = z.infer<typeof VistaCloseArgsSchema>;
78
-
79
- /** `vista_list() -> { tabs }`. No arguments. */
80
- export const VistaListArgsSchema = z.object({});
81
- export type VistaListArgs = z.infer<typeof VistaListArgsSchema>;
82
-
83
- /** Why a Vista tool call could not be performed. Closed set. */
84
- export enum VistaToolError {
85
- /** No Vista panel answered the relay — nothing is on screen to drive. */
86
- NoPanel = 'no-panel',
87
- /** The arguments failed schema validation at a wire boundary. */
88
- InvalidArgs = 'invalid-args',
89
- /** The tool name is not a member of {@link VistaToolName}. */
90
- UnknownTool = 'unknown-tool',
91
- /** `tabId` names a tab that is not open. */
92
- UnknownTab = 'unknown-tab',
93
- /**
94
- * The tab exists but refuses the operation — it is pinned (close), or it is
95
- * a HOST tab whose content the host's own data flow owns (update). `detail`
96
- * names which.
97
- */
98
- TabRefused = 'tab-refused',
99
- }
100
-
101
- /** The result envelope every Vista tool returns. */
102
- export type VistaToolResult<TValue = unknown> =
103
- | { readonly ok: true; readonly value: TValue }
104
- | {
105
- readonly ok: false;
106
- readonly error: VistaToolError;
107
- readonly detail?: string;
108
- };
109
-
110
- /**
111
- * Wire validator for {@link VistaToolResult}. The browser's answer crosses back
112
- * into the server and then into the model's context, so the server validates
113
- * the ENVELOPE with this shape before relaying it on — a browser cannot inject
114
- * an arbitrary object where a typed result is expected.
115
- */
116
- export const VistaToolResultSchema: z.ZodType<VistaToolResult> = z.union([
117
- z.object({ ok: z.literal(true), value: z.unknown() }),
118
- z.object({
119
- ok: z.literal(false),
120
- error: z.nativeEnum(VistaToolError),
121
- detail: z.string().optional(),
122
- }),
123
- ]);
124
-
125
- /**
126
- * Perform a relayed Vista tool call against a mounted panel's controller.
127
- *
128
- * Fail-fast on bad input: arguments are re-validated here (the frontend never
129
- * trusts that the backend validated), and a violation returns
130
- * {@link VistaToolError.InvalidArgs} naming the offending path rather than a
131
- * partially-applied change. Pass `controller: null` for a document with no
132
- * mounted panel — that returns {@link VistaToolError.NoPanel}.
133
- */
134
- export function dispatchVistaTool(
135
- controller: VistaTabsController | null,
136
- tool: string,
137
- args: unknown,
138
- ): VistaToolResult {
139
- if (controller === null) {
140
- return { ok: false, error: VistaToolError.NoPanel };
141
- }
142
- switch (tool) {
143
- case VistaToolName.Open: {
144
- const parsed = VistaOpenArgsSchema.safeParse(args);
145
- if (!parsed.success) {
146
- return invalid(parsed.error);
147
- }
148
- return { ok: true, value: { tabId: controller.open(parsed.data) } };
149
- }
150
- case VistaToolName.Update: {
151
- const parsed = VistaUpdateArgsSchema.safeParse(args);
152
- if (!parsed.success) {
153
- return invalid(parsed.error);
154
- }
155
- const { tabId, ...patch } = parsed.data;
156
- const refusal = refusalFor(controller, tabId);
157
- if (refusal !== null) {
158
- return refusal;
159
- }
160
- if (!controller.update(tabId, patch)) {
161
- // The tab is listed but not opened-tab-owned: it is a HOST tab, and a
162
- // patch to it would be overwritten by the host's next reconcile.
163
- return {
164
- ok: false,
165
- error: VistaToolError.TabRefused,
166
- detail: `Tab "${tabId}" is host-owned; its content is driven by the host, not by vista_update.`,
167
- };
168
- }
169
- return { ok: true, value: { updated: true } };
170
- }
171
- case VistaToolName.Close: {
172
- const parsed = VistaCloseArgsSchema.safeParse(args);
173
- if (!parsed.success) {
174
- return invalid(parsed.error);
175
- }
176
- const { tabId } = parsed.data;
177
- const refusal = refusalFor(controller, tabId);
178
- if (refusal !== null) {
179
- return refusal;
180
- }
181
- if (!controller.close(tabId)) {
182
- return {
183
- ok: false,
184
- error: VistaToolError.TabRefused,
185
- detail: `Tab "${tabId}" is pinned and cannot be closed.`,
186
- };
187
- }
188
- return { ok: true, value: { closed: true } };
189
- }
190
- case VistaToolName.List: {
191
- const parsed = VistaListArgsSchema.safeParse(args ?? {});
192
- if (!parsed.success) {
193
- return invalid(parsed.error);
194
- }
195
- return { ok: true, value: { tabs: controller.list() } };
196
- }
197
- default:
198
- return {
199
- ok: false,
200
- error: VistaToolError.UnknownTool,
201
- detail: `'${tool}' is not a Vista tool.`,
202
- };
203
- }
204
- }
205
-
206
- /**
207
- * `unknown-tab` when the panel does not hold `tabId` at all. Returning `null`
208
- * means the tab exists and the operation may proceed.
209
- */
210
- function refusalFor(
211
- controller: VistaTabsController,
212
- tabId: string,
213
- ): VistaToolResult | null {
214
- if (controller.list().some((tab) => tab.tabId === tabId)) {
215
- return null;
216
- }
217
- return {
218
- ok: false,
219
- error: VistaToolError.UnknownTab,
220
- detail: `No tab "${tabId}" is open.`,
221
- };
222
- }
223
-
224
- function invalid(error: z.ZodError): VistaToolResult {
225
- return {
226
- ok: false,
227
- error: VistaToolError.InvalidArgs,
228
- detail: error.issues
229
- .map((i) => `${i.path.join('.') || '(root)'}: ${i.message}`)
230
- .join('; '),
231
- };
232
- }