@astra-spec/ui 0.0.1

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 (259) hide show
  1. package/LICENSE +29 -0
  2. package/README.md +261 -0
  3. package/TOKENS.md +93 -0
  4. package/blocks.css +7 -0
  5. package/components.css +13 -0
  6. package/dist/blocks/analysis-tree.d.ts +13 -0
  7. package/dist/blocks/analysis-tree.d.ts.map +1 -0
  8. package/dist/blocks/analysis-tree.js +19 -0
  9. package/dist/blocks/analysis-tree.js.map +1 -0
  10. package/dist/blocks/decisions-list.d.ts +17 -0
  11. package/dist/blocks/decisions-list.d.ts.map +1 -0
  12. package/dist/blocks/decisions-list.js +56 -0
  13. package/dist/blocks/decisions-list.js.map +1 -0
  14. package/dist/blocks/findings-list.d.ts +9 -0
  15. package/dist/blocks/findings-list.d.ts.map +1 -0
  16. package/dist/blocks/findings-list.js +35 -0
  17. package/dist/blocks/findings-list.js.map +1 -0
  18. package/dist/blocks/index.d.ts +18 -0
  19. package/dist/blocks/index.d.ts.map +1 -0
  20. package/dist/blocks/index.js +10 -0
  21. package/dist/blocks/index.js.map +1 -0
  22. package/dist/blocks/inputs-list.d.ts +9 -0
  23. package/dist/blocks/inputs-list.d.ts.map +1 -0
  24. package/dist/blocks/inputs-list.js +32 -0
  25. package/dist/blocks/inputs-list.js.map +1 -0
  26. package/dist/blocks/outputs-list.d.ts +18 -0
  27. package/dist/blocks/outputs-list.d.ts.map +1 -0
  28. package/dist/blocks/outputs-list.js +47 -0
  29. package/dist/blocks/outputs-list.js.map +1 -0
  30. package/dist/blocks/papers-list.d.ts +16 -0
  31. package/dist/blocks/papers-list.d.ts.map +1 -0
  32. package/dist/blocks/papers-list.js +20 -0
  33. package/dist/blocks/papers-list.js.map +1 -0
  34. package/dist/blocks/prior-insights-list.d.ts +9 -0
  35. package/dist/blocks/prior-insights-list.d.ts.map +1 -0
  36. package/dist/blocks/prior-insights-list.js +29 -0
  37. package/dist/blocks/prior-insights-list.js.map +1 -0
  38. package/dist/blocks/section.d.ts +36 -0
  39. package/dist/blocks/section.d.ts.map +1 -0
  40. package/dist/blocks/section.js +30 -0
  41. package/dist/blocks/section.js.map +1 -0
  42. package/dist/components/artifact-preview.d.ts +71 -0
  43. package/dist/components/artifact-preview.d.ts.map +1 -0
  44. package/dist/components/artifact-preview.js +170 -0
  45. package/dist/components/artifact-preview.js.map +1 -0
  46. package/dist/components/decision-detail.d.ts +17 -0
  47. package/dist/components/decision-detail.d.ts.map +1 -0
  48. package/dist/components/decision-detail.js +21 -0
  49. package/dist/components/decision-detail.js.map +1 -0
  50. package/dist/components/detail-entry.d.ts +15 -0
  51. package/dist/components/detail-entry.d.ts.map +1 -0
  52. package/dist/components/detail-entry.js +14 -0
  53. package/dist/components/detail-entry.js.map +1 -0
  54. package/dist/components/finding-detail.d.ts +19 -0
  55. package/dist/components/finding-detail.d.ts.map +1 -0
  56. package/dist/components/finding-detail.js +39 -0
  57. package/dist/components/finding-detail.js.map +1 -0
  58. package/dist/components/index.d.ts +28 -0
  59. package/dist/components/index.d.ts.map +1 -0
  60. package/dist/components/index.js +15 -0
  61. package/dist/components/index.js.map +1 -0
  62. package/dist/components/input-detail.d.ts +15 -0
  63. package/dist/components/input-detail.d.ts.map +1 -0
  64. package/dist/components/input-detail.js +21 -0
  65. package/dist/components/input-detail.js.map +1 -0
  66. package/dist/components/insight-detail.d.ts +25 -0
  67. package/dist/components/insight-detail.d.ts.map +1 -0
  68. package/dist/components/insight-detail.js +30 -0
  69. package/dist/components/insight-detail.js.map +1 -0
  70. package/dist/components/insight-trigger.d.ts +20 -0
  71. package/dist/components/insight-trigger.d.ts.map +1 -0
  72. package/dist/components/insight-trigger.js +32 -0
  73. package/dist/components/insight-trigger.js.map +1 -0
  74. package/dist/components/output-detail.d.ts +39 -0
  75. package/dist/components/output-detail.d.ts.map +1 -0
  76. package/dist/components/output-detail.js +124 -0
  77. package/dist/components/output-detail.js.map +1 -0
  78. package/dist/components/output-dialog.d.ts +18 -0
  79. package/dist/components/output-dialog.d.ts.map +1 -0
  80. package/dist/components/output-dialog.js +11 -0
  81. package/dist/components/output-dialog.js.map +1 -0
  82. package/dist/components/paper-detail.d.ts +34 -0
  83. package/dist/components/paper-detail.d.ts.map +1 -0
  84. package/dist/components/paper-detail.js +60 -0
  85. package/dist/components/paper-detail.js.map +1 -0
  86. package/dist/components/record-dialog.d.ts +33 -0
  87. package/dist/components/record-dialog.d.ts.map +1 -0
  88. package/dist/components/record-dialog.js +114 -0
  89. package/dist/components/record-dialog.js.map +1 -0
  90. package/dist/components/relation-items.d.ts +9 -0
  91. package/dist/components/relation-items.d.ts.map +1 -0
  92. package/dist/components/relation-items.js +20 -0
  93. package/dist/components/relation-items.js.map +1 -0
  94. package/dist/components/use-detail-stack.d.ts +31 -0
  95. package/dist/components/use-detail-stack.d.ts.map +1 -0
  96. package/dist/components/use-detail-stack.js +49 -0
  97. package/dist/components/use-detail-stack.js.map +1 -0
  98. package/dist/lib/cn.d.ts +3 -0
  99. package/dist/lib/cn.d.ts.map +1 -0
  100. package/dist/lib/cn.js +6 -0
  101. package/dist/lib/cn.js.map +1 -0
  102. package/dist/lib/labels.d.ts +61 -0
  103. package/dist/lib/labels.d.ts.map +1 -0
  104. package/dist/lib/labels.js +79 -0
  105. package/dist/lib/labels.js.map +1 -0
  106. package/dist/lib/slot.d.ts +11 -0
  107. package/dist/lib/slot.d.ts.map +1 -0
  108. package/dist/lib/slot.js +83 -0
  109. package/dist/lib/slot.js.map +1 -0
  110. package/dist/model/doi.d.ts +2 -0
  111. package/dist/model/doi.d.ts.map +1 -0
  112. package/dist/model/doi.js +5 -0
  113. package/dist/model/doi.js.map +1 -0
  114. package/dist/model/index.d.ts +10 -0
  115. package/dist/model/index.d.ts.map +1 -0
  116. package/dist/model/index.js +7 -0
  117. package/dist/model/index.js.map +1 -0
  118. package/dist/model/locate-record.d.ts +8 -0
  119. package/dist/model/locate-record.d.ts.map +1 -0
  120. package/dist/model/locate-record.js +7 -0
  121. package/dist/model/locate-record.js.map +1 -0
  122. package/dist/model/papers.d.ts +38 -0
  123. package/dist/model/papers.d.ts.map +1 -0
  124. package/dist/model/papers.js +91 -0
  125. package/dist/model/papers.js.map +1 -0
  126. package/dist/model/records.d.ts +10 -0
  127. package/dist/model/records.d.ts.map +1 -0
  128. package/dist/model/records.js +32 -0
  129. package/dist/model/records.js.map +1 -0
  130. package/dist/model/relations.d.ts +45 -0
  131. package/dist/model/relations.d.ts.map +1 -0
  132. package/dist/model/relations.js +98 -0
  133. package/dist/model/relations.js.map +1 -0
  134. package/dist/primitives/badge.d.ts +11 -0
  135. package/dist/primitives/badge.d.ts.map +1 -0
  136. package/dist/primitives/badge.js +7 -0
  137. package/dist/primitives/badge.js.map +1 -0
  138. package/dist/primitives/button.d.ts +18 -0
  139. package/dist/primitives/button.d.ts.map +1 -0
  140. package/dist/primitives/button.js +12 -0
  141. package/dist/primitives/button.js.map +1 -0
  142. package/dist/primitives/detail-layout.d.ts +26 -0
  143. package/dist/primitives/detail-layout.d.ts.map +1 -0
  144. package/dist/primitives/detail-layout.js +21 -0
  145. package/dist/primitives/detail-layout.js.map +1 -0
  146. package/dist/primitives/dialog.d.ts +133 -0
  147. package/dist/primitives/dialog.d.ts.map +1 -0
  148. package/dist/primitives/dialog.js +266 -0
  149. package/dist/primitives/dialog.js.map +1 -0
  150. package/dist/primitives/index.d.ts +25 -0
  151. package/dist/primitives/index.d.ts.map +1 -0
  152. package/dist/primitives/index.js +14 -0
  153. package/dist/primitives/index.js.map +1 -0
  154. package/dist/primitives/kind.d.ts +6 -0
  155. package/dist/primitives/kind.d.ts.map +1 -0
  156. package/dist/primitives/kind.js +14 -0
  157. package/dist/primitives/kind.js.map +1 -0
  158. package/dist/primitives/prose.d.ts +36 -0
  159. package/dist/primitives/prose.d.ts.map +1 -0
  160. package/dist/primitives/prose.js +57 -0
  161. package/dist/primitives/prose.js.map +1 -0
  162. package/dist/primitives/record-list.d.ts +31 -0
  163. package/dist/primitives/record-list.d.ts.map +1 -0
  164. package/dist/primitives/record-list.js +17 -0
  165. package/dist/primitives/record-list.js.map +1 -0
  166. package/dist/primitives/relation-list.d.ts +22 -0
  167. package/dist/primitives/relation-list.d.ts.map +1 -0
  168. package/dist/primitives/relation-list.js +13 -0
  169. package/dist/primitives/relation-list.js.map +1 -0
  170. package/dist/primitives/surface-header.d.ts +31 -0
  171. package/dist/primitives/surface-header.d.ts.map +1 -0
  172. package/dist/primitives/surface-header.js +13 -0
  173. package/dist/primitives/surface-header.js.map +1 -0
  174. package/dist/views/index.d.ts +4 -0
  175. package/dist/views/index.d.ts.map +1 -0
  176. package/dist/views/index.js +3 -0
  177. package/dist/views/index.js.map +1 -0
  178. package/dist/views/inventory.d.ts +46 -0
  179. package/dist/views/inventory.d.ts.map +1 -0
  180. package/dist/views/inventory.js +101 -0
  181. package/dist/views/inventory.js.map +1 -0
  182. package/package.json +127 -0
  183. package/primitives.css +15 -0
  184. package/src/blocks/analysis-tree.tsx +109 -0
  185. package/src/blocks/decisions-list.tsx +113 -0
  186. package/src/blocks/findings-list.tsx +61 -0
  187. package/src/blocks/index.ts +23 -0
  188. package/src/blocks/inputs-list.tsx +51 -0
  189. package/src/blocks/outputs-list.tsx +143 -0
  190. package/src/blocks/papers-list.tsx +65 -0
  191. package/src/blocks/prior-insights-list.tsx +52 -0
  192. package/src/blocks/section.tsx +113 -0
  193. package/src/components/artifact-preview.tsx +314 -0
  194. package/src/components/decision-detail.tsx +93 -0
  195. package/src/components/detail-entry.ts +19 -0
  196. package/src/components/finding-detail.tsx +99 -0
  197. package/src/components/index.ts +44 -0
  198. package/src/components/input-detail.tsx +67 -0
  199. package/src/components/insight-detail.tsx +120 -0
  200. package/src/components/insight-trigger.tsx +102 -0
  201. package/src/components/output-detail.tsx +258 -0
  202. package/src/components/output-dialog.tsx +63 -0
  203. package/src/components/paper-detail.tsx +216 -0
  204. package/src/components/record-dialog.tsx +232 -0
  205. package/src/components/relation-items.ts +31 -0
  206. package/src/components/use-detail-stack.ts +78 -0
  207. package/src/lib/cn.ts +5 -0
  208. package/src/lib/labels.tsx +145 -0
  209. package/src/lib/slot.tsx +87 -0
  210. package/src/model/doi.ts +5 -0
  211. package/src/model/index.ts +32 -0
  212. package/src/model/locate-record.ts +13 -0
  213. package/src/model/papers.ts +146 -0
  214. package/src/model/records.ts +43 -0
  215. package/src/model/relations.ts +134 -0
  216. package/src/primitives/badge.tsx +32 -0
  217. package/src/primitives/button.tsx +58 -0
  218. package/src/primitives/detail-layout.tsx +84 -0
  219. package/src/primitives/dialog.tsx +555 -0
  220. package/src/primitives/index.ts +48 -0
  221. package/src/primitives/kind.ts +19 -0
  222. package/src/primitives/prose.tsx +74 -0
  223. package/src/primitives/record-list.tsx +103 -0
  224. package/src/primitives/relation-list.tsx +95 -0
  225. package/src/primitives/surface-header.tsx +87 -0
  226. package/src/views/index.ts +3 -0
  227. package/src/views/inventory.tsx +224 -0
  228. package/styles/base.css +49 -0
  229. package/styles/blocks/analysis-tree.css +64 -0
  230. package/styles/blocks/decisions-list.css +73 -0
  231. package/styles/blocks/outputs-list.css +349 -0
  232. package/styles/blocks/papers-list.css +98 -0
  233. package/styles/blocks/records.css +72 -0
  234. package/styles/components/artifact-preview.css +242 -0
  235. package/styles/components/decision-detail.css +159 -0
  236. package/styles/components/evidence.css +79 -0
  237. package/styles/components/finding-detail.css +63 -0
  238. package/styles/components/input-detail.css +95 -0
  239. package/styles/components/insight-detail.css +138 -0
  240. package/styles/components/insight-list.css +46 -0
  241. package/styles/components/insight-trigger.css +54 -0
  242. package/styles/components/output-detail.css +732 -0
  243. package/styles/components/paper-detail.css +249 -0
  244. package/styles/components/paper-doi.css +36 -0
  245. package/styles/primitives/badge.css +107 -0
  246. package/styles/primitives/button.css +116 -0
  247. package/styles/primitives/count-heading.css +27 -0
  248. package/styles/primitives/detail-layout.css +225 -0
  249. package/styles/primitives/dialog.css +634 -0
  250. package/styles/primitives/empty-state.css +10 -0
  251. package/styles/primitives/kind.css +41 -0
  252. package/styles/primitives/prose.css +15 -0
  253. package/styles/primitives/record-list.css +425 -0
  254. package/styles/primitives/relation-list.css +184 -0
  255. package/styles/primitives/surface-header.css +183 -0
  256. package/styles/tokens.css +119 -0
  257. package/styles/views/inventory.css +186 -0
  258. package/styles.css +2 -0
  259. package/views.css +3 -0
@@ -0,0 +1,314 @@
1
+ import type { ResolvedOutput } from '@astra-spec/sdk';
2
+ import { forwardRef, useEffect, useState, type HTMLAttributes, type ReactNode } from 'react';
3
+ import { cn } from '../lib/cn.js';
4
+
5
+ export interface TablePreviewData {
6
+ kind: 'table';
7
+ headers: string[];
8
+ rows: (string | number | boolean | null)[][];
9
+ /** Exact number of rows in the source. Leave unset when the source was sampled and the total is unknown. */
10
+ totalRows?: number | undefined;
11
+ totalColumns?: number | undefined;
12
+ /** The preview does not show everything: rows or columns were cut, or the source itself was a sample. */
13
+ truncated?: boolean | undefined;
14
+ }
15
+
16
+ export interface ImagePreviewData {
17
+ kind: 'image';
18
+ url: string;
19
+ alt?: string | undefined;
20
+ width?: number | undefined;
21
+ height?: number | undefined;
22
+ }
23
+
24
+ export interface MetricPreviewData {
25
+ kind: 'metric';
26
+ value: number | string;
27
+ uncertainty?: number | string | undefined;
28
+ unit?: string | undefined;
29
+ label?: string | undefined;
30
+ }
31
+
32
+ export interface TextPreviewData {
33
+ kind: 'text';
34
+ text: string;
35
+ language?: string | undefined;
36
+ truncated?: boolean | undefined;
37
+ }
38
+
39
+ export interface LoadingPreviewData {
40
+ kind: 'loading';
41
+ message?: string | undefined;
42
+ }
43
+
44
+ export interface UnavailablePreviewData {
45
+ kind: 'unavailable';
46
+ reason?: string | undefined;
47
+ }
48
+
49
+ /** Host-safe data that a portable component can render without reading a file. */
50
+ export type ArtifactPreviewData =
51
+ | TablePreviewData
52
+ | ImagePreviewData
53
+ | MetricPreviewData
54
+ | TextPreviewData
55
+ | LoadingPreviewData
56
+ | UnavailablePreviewData;
57
+
58
+ export interface ArtifactRenderOptions {
59
+ compact: boolean;
60
+ }
61
+
62
+ /** Controlled host slot. Reading, decoding, URLs, and caching remain outside astra-ui. */
63
+ export type ArtifactRenderer = (
64
+ output: ResolvedOutput,
65
+ options: ArtifactRenderOptions,
66
+ ) => ReactNode;
67
+
68
+ export interface ArtifactPreviewProps extends Omit<HTMLAttributes<HTMLElement>, 'children'> {
69
+ output: ResolvedOutput;
70
+ preview?: ArtifactPreviewData | undefined;
71
+ compact?: boolean | undefined;
72
+ caption?: ReactNode | undefined;
73
+ /** Pure helpers exported below build the default preview data. */
74
+ locale?: string | undefined;
75
+ }
76
+
77
+ function compactValue(value: string | number | undefined, locale: string | undefined): string {
78
+ if (value == null || value === '') return 'Value unavailable';
79
+ return typeof value === 'number'
80
+ ? value.toLocaleString(locale, { maximumSignificantDigits: 5 })
81
+ : value;
82
+ }
83
+
84
+ function unavailableReason(output: ResolvedOutput): string {
85
+ if (!output.active) return 'This output is not active in the selected universe.';
86
+ if (!output.artifact) return 'This output has not been materialized.';
87
+ return 'This host has not supplied an artifact preview.';
88
+ }
89
+
90
+ /** Pure renderer for a resolved output and optional host-supplied preview data. */
91
+ export const ArtifactPreview = forwardRef<HTMLElement, ArtifactPreviewProps>(function ArtifactPreview({
92
+ output,
93
+ preview,
94
+ compact = false,
95
+ caption = output.label ?? output.id,
96
+ locale,
97
+ className,
98
+ ...props
99
+ }, ref) {
100
+ const [imageFailed, setImageFailed] = useState(false);
101
+ const imageUrl = preview?.kind === 'image' ? preview.url : undefined;
102
+ useEffect(() => { setImageFailed(false); }, [imageUrl, output.canonicalPath]);
103
+
104
+ const type = preview?.kind ?? output.type;
105
+ const shared = {
106
+ ...props,
107
+ 'data-slot': 'artifact-preview',
108
+ 'data-type': type,
109
+ ...(compact ? { 'data-compact': '' } : {}),
110
+ };
111
+ const rootClass = cn('astra-artifact', className);
112
+
113
+ if (preview?.kind === 'loading') {
114
+ return (
115
+ <div {...shared} ref={ref as never} className={cn(rootClass, 'astra-artifact__placeholder')} role="status" aria-busy="true">
116
+ <span aria-hidden="true">…</span>
117
+ <span>{preview.message ?? 'Loading preview…'}</span>
118
+ </div>
119
+ );
120
+ }
121
+
122
+ if (!preview || preview.kind === 'unavailable') {
123
+ return (
124
+ <div {...shared} ref={ref as never} className={cn(rootClass, 'astra-artifact__placeholder')} role="status">
125
+ <span aria-hidden="true">{output.active ? '↳' : '○'}</span>
126
+ <span>{preview?.reason ?? unavailableReason(output)}</span>
127
+ </div>
128
+ );
129
+ }
130
+
131
+ if (preview.kind === 'image') {
132
+ if (imageFailed) {
133
+ return (
134
+ <div {...shared} ref={ref as never} className={cn(rootClass, 'astra-artifact__placeholder')} role="status">
135
+ <span aria-hidden="true">▦</span>
136
+ <span>Figure preview unavailable</span>
137
+ </div>
138
+ );
139
+ }
140
+ return (
141
+ <figure {...shared} ref={ref} className={rootClass}>
142
+ <img
143
+ src={preview.url}
144
+ alt={preview.alt ?? `Preview of ${output.label ?? output.id}`}
145
+ width={preview.width}
146
+ height={preview.height}
147
+ onError={() => { setImageFailed(true); }}
148
+ />
149
+ {!compact && caption != null ? <figcaption>{caption}</figcaption> : null}
150
+ </figure>
151
+ );
152
+ }
153
+
154
+ if (preview.kind === 'table') {
155
+ const columnLimit = compact ? 5 : 30;
156
+ const rowLimit = compact ? 4 : 30;
157
+ const headers = preview.headers.slice(0, columnLimit);
158
+ const rows = preview.rows.slice(0, rowLimit);
159
+ const totalRows = preview.totalRows;
160
+ const totalColumns = preview.totalColumns ?? preview.headers.length;
161
+ const moreRows = totalRows === undefined ? Boolean(preview.truncated) : totalRows > rows.length;
162
+ const moreColumns = totalColumns > headers.length;
163
+ const rowsNote = totalRows === undefined
164
+ ? `the first ${rows.length} rows (total unknown)`
165
+ : `${rows.length} of ${totalRows} rows`;
166
+ return (
167
+ // The full-size table scrolls and must be keyboard-reachable; the compact one sits inside a card button.
168
+ <div {...shared} ref={ref as never} className={cn(rootClass, 'astra-artifact__table')} {...(compact ? {} : { tabIndex: 0 })}>
169
+ <table>
170
+ <thead>
171
+ <tr>{headers.map((header, index) => <th key={`${header}-${index}`}>{header}</th>)}</tr>
172
+ </thead>
173
+ <tbody>
174
+ {rows.map((row, rowIndex) => (
175
+ <tr key={rowIndex}>
176
+ {row.slice(0, columnLimit).map((cell, columnIndex) => (
177
+ <td key={columnIndex}>{cell === null ? '—' : String(cell)}</td>
178
+ ))}
179
+ </tr>
180
+ ))}
181
+ </tbody>
182
+ </table>
183
+ {!compact && (preview.truncated || moreRows || moreColumns) ? (
184
+ <p>Showing {rowsNote} and {headers.length} of {totalColumns} columns.</p>
185
+ ) : null}
186
+ </div>
187
+ );
188
+ }
189
+
190
+ if (preview.kind === 'metric') {
191
+ return (
192
+ <div {...shared} ref={ref as never} className={cn(rootClass, 'astra-artifact__metric')}>
193
+ {preview.label ? <span>{preview.label}</span> : null}
194
+ <strong className="astra-artifact__metric-value">{compactValue(preview.value, locale)}</strong>
195
+ {preview.uncertainty !== undefined ? (
196
+ <span className="astra-artifact__metric-uncertainty">± {preview.uncertainty}</span>
197
+ ) : null}
198
+ {preview.unit ? <span className="astra-artifact__metric-unit">{preview.unit}</span> : null}
199
+ </div>
200
+ );
201
+ }
202
+
203
+ return (
204
+ <div {...shared} ref={ref as never} className={rootClass}>
205
+ <pre><code>{preview.text}</code></pre>
206
+ {preview.truncated ? <p>Preview truncated.</p> : null}
207
+ </div>
208
+ );
209
+ });
210
+
211
+ /* ------------------------------------------------------------------ */
212
+ /* Pure helpers hosts can use to build preview data (no I/O) */
213
+ /* ------------------------------------------------------------------ */
214
+
215
+ export interface DelimitedPreviewOptions {
216
+ delimiter?: string | undefined;
217
+ maxRows?: number | undefined;
218
+ maxColumns?: number | undefined;
219
+ /** Set when the source text was itself cut short by the host. */
220
+ sourceTruncated?: boolean | undefined;
221
+ }
222
+
223
+ /** RFC 4180-style rows: quoted cells may hold delimiters, doubled quotes, and line breaks. Blank rows are dropped. */
224
+ function parseDelimited(text: string, delimiter: string): { rows: string[][]; unterminated: boolean } {
225
+ const rows: string[][] = [];
226
+ let row: string[] = [];
227
+ let cell = '';
228
+ let quoted = false;
229
+ const endCell = () => { row.push(cell.trim()); cell = ''; };
230
+ const endRow = () => {
231
+ endCell();
232
+ if (row.some((value) => value.length > 0)) rows.push(row);
233
+ row = [];
234
+ };
235
+ for (let index = 0; index < text.length; index += 1) {
236
+ const character = text.charAt(index);
237
+ if (quoted) {
238
+ if (character !== '"') cell += character;
239
+ else if (text.charAt(index + 1) === '"') { cell += '"'; index += 1; } else quoted = false;
240
+ } else if (character === '"') quoted = true;
241
+ else if (character === delimiter) endCell();
242
+ else if (character === '\n' || character === '\r') {
243
+ if (character === '\r' && text.charAt(index + 1) === '\n') index += 1;
244
+ endRow();
245
+ } else cell += character;
246
+ }
247
+ endRow();
248
+ return { rows, unterminated: quoted };
249
+ }
250
+
251
+ /** Turns delimited text (CSV, TSV) into table preview data. */
252
+ export function tablePreviewFromDelimited(text: string, options: DelimitedPreviewOptions = {}): TablePreviewData {
253
+ const delimiter = options.delimiter ?? ',';
254
+ const maxRows = options.maxRows ?? 30;
255
+ const maxColumns = options.maxColumns ?? 30;
256
+ const parsed = parseDelimited(text, delimiter);
257
+ const [allHeaders = [], ...records] = parsed.rows;
258
+ // A byte-limited sample may end inside a record (or inside a quoted cell);
259
+ // that record is dropped, and the total row count is unknown.
260
+ const sampled = Boolean(options.sourceTruncated);
261
+ const lastComplete = /\r?\n$/.test(text) && !parsed.unterminated;
262
+ const body = sampled && !lastComplete ? records.slice(0, -1) : records;
263
+ const headers = allHeaders.slice(0, maxColumns);
264
+ const rows = body.slice(0, maxRows).map((cells) => cells.slice(0, maxColumns));
265
+ return {
266
+ kind: 'table',
267
+ headers,
268
+ rows,
269
+ ...(sampled ? {} : { totalRows: body.length }),
270
+ totalColumns: allHeaders.length,
271
+ truncated: sampled || body.length > maxRows || allHeaders.length > maxColumns,
272
+ };
273
+ }
274
+
275
+ /** Turns an array of flat objects (e.g. parsed JSON records) into table preview data. */
276
+ export function tablePreviewFromRows(
277
+ records: readonly Record<string, unknown>[],
278
+ options: Pick<DelimitedPreviewOptions, 'maxRows' | 'maxColumns' | 'sourceTruncated'> = {},
279
+ ): TablePreviewData {
280
+ const maxRows = options.maxRows ?? 30;
281
+ const maxColumns = options.maxColumns ?? 30;
282
+ const allHeaders = [...new Set(records.flatMap((record) => Object.keys(record)))];
283
+ const headers = allHeaders.slice(0, maxColumns);
284
+ const rows = records.slice(0, maxRows).map((record) => headers.map((header) => {
285
+ const value = record[header];
286
+ if (value == null) return null;
287
+ if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'string') return value;
288
+ return JSON.stringify(value);
289
+ }));
290
+ return {
291
+ kind: 'table',
292
+ headers,
293
+ rows,
294
+ totalRows: records.length,
295
+ totalColumns: allHeaders.length,
296
+ truncated: Boolean(options.sourceTruncated) || records.length > maxRows || allHeaders.length > maxColumns,
297
+ };
298
+ }
299
+
300
+ /** Reads a metric from a scalar or a `{ value, uncertainty?, unit?, label? }` object; `undefined` when the shape is not a metric. */
301
+ export function metricPreviewFromJson(value: unknown): MetricPreviewData | undefined {
302
+ if (typeof value === 'number' || typeof value === 'string') return { kind: 'metric', value };
303
+ if (value && typeof value === 'object' && 'value' in value) {
304
+ const record = value as Record<string, unknown>;
305
+ const metric = record.value;
306
+ if (typeof metric !== 'number' && typeof metric !== 'string') return undefined;
307
+ const result: MetricPreviewData = { kind: 'metric', value: metric };
308
+ if (typeof record.uncertainty === 'number' || typeof record.uncertainty === 'string') result.uncertainty = record.uncertainty;
309
+ if (typeof record.unit === 'string') result.unit = record.unit;
310
+ if (typeof record.label === 'string') result.label = record.label;
311
+ return result;
312
+ }
313
+ return undefined;
314
+ }
@@ -0,0 +1,93 @@
1
+ import type { ResolvedDecision, ResolvedInsight } from '@astra-spec/sdk';
2
+ import { forwardRef, useId, type HTMLAttributes } from 'react';
3
+ import { recordTitle } from '../model/records.js';
4
+ import { useLabels } from '../lib/labels.js';
5
+ import { CountHeading, DetailLayout, DetailMain, DetailSection } from '../primitives/detail-layout.js';
6
+ import { DetailDialog, type DetailDialogProps } from '../primitives/dialog.js';
7
+ import { Prose, type TextRenderer } from '../primitives/prose.js';
8
+ import { InsightTrigger } from './insight-trigger.js';
9
+
10
+ export interface DecisionDetailProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
11
+ record: ResolvedDecision;
12
+ /** Insights cited by the decision's options; see `decisionInsights()`. */
13
+ insights: ResolvedInsight[];
14
+ renderText?: TextRenderer | undefined;
15
+ onOpenInsight?: ((insight: ResolvedInsight) => void) | undefined;
16
+ }
17
+
18
+ /** Rationale, options with the selected one marked, and the insights that informed the decision. */
19
+ export const DecisionDetail = forwardRef<HTMLDivElement, DecisionDetailProps>(function DecisionDetail({
20
+ record,
21
+ insights,
22
+ renderText,
23
+ onOpenInsight,
24
+ className,
25
+ ...props
26
+ }, ref) {
27
+ const optionsId = useId();
28
+ return (
29
+ <DetailLayout data-slot="decision-detail" {...props} ref={ref} layout="single" className={className}>
30
+ <DetailMain>
31
+ {record.rationale ? (
32
+ <DetailSection label="Rationale" heading="section">
33
+ <Prose text={record.rationale} field="rationale" renderText={renderText} />
34
+ </DetailSection>
35
+ ) : null}
36
+ <section className="astra-decision-options" aria-labelledby={optionsId}>
37
+ <h4 id={optionsId}>Options</h4>
38
+ <ul>
39
+ {record.options.map((option) => {
40
+ const selected = option.id === record.selectedOptionId;
41
+ return (
42
+ <li key={option.id} {...(selected ? { 'data-selected': '' } : {})}>
43
+ <span className="astra-decision-options__marker" aria-hidden="true">
44
+ {selected ? '●' : '○'}
45
+ </span>
46
+ <span>
47
+ <strong>{option.label}</strong>
48
+ <code>{option.id}</code>
49
+ </span>
50
+ {selected ? <small>Selected</small> : null}
51
+ </li>
52
+ );
53
+ })}
54
+ </ul>
55
+ </section>
56
+ <section className="astra-insight-list">
57
+ <CountHeading title="Insights that informed this" count={insights.length} />
58
+ {insights.length ? (
59
+ <ul className="astra-decision-insights">
60
+ {insights.map((insight) => (
61
+ <li key={insight.canonicalPath}>
62
+ <InsightTrigger
63
+ insight={insight}
64
+ variant="claim"
65
+ renderText={renderText}
66
+ onOpen={() => onOpenInsight?.(insight)}
67
+ />
68
+ </li>
69
+ ))}
70
+ </ul>
71
+ ) : <p>No prior insights are linked to this decision.</p>}
72
+ </section>
73
+ </DetailMain>
74
+ </DetailLayout>
75
+ );
76
+ });
77
+
78
+ export interface DecisionDialogProps extends Pick<DetailDialogProps, 'mode' | 'backText' | 'className' | 'onBack' | 'onClose'>, Omit<DecisionDetailProps, 'className'> {}
79
+
80
+ export function DecisionDialog({ record, insights, renderText, onOpenInsight, ...dialog }: DecisionDialogProps) {
81
+ const labels = useLabels();
82
+ return (
83
+ <DetailDialog
84
+ {...dialog}
85
+ kind="decision"
86
+ kindLabel={labels.kinds.decision}
87
+ title={recordTitle(record)}
88
+ closeLabel={labels.closeRecord(labels.kinds.decision)}
89
+ >
90
+ <DecisionDetail record={record} insights={insights} renderText={renderText} onOpenInsight={onOpenInsight} />
91
+ </DetailDialog>
92
+ );
93
+ }
@@ -0,0 +1,19 @@
1
+ /** What a detail surface is showing: a record, or a paper (optionally focused on an insight). */
2
+ export type DetailEntry =
3
+ | { kind: 'record'; canonicalPath: string; analysisPath: string }
4
+ | { kind: 'paper'; doi: string; analysisPath: string; focusInsightPath?: string | undefined };
5
+
6
+ export function recordEntry(canonicalPath: string, analysisPath: string): DetailEntry {
7
+ return { kind: 'record', canonicalPath, analysisPath };
8
+ }
9
+
10
+ export function paperEntry(doi: string, analysisPath: string, focusInsightPath?: string): DetailEntry {
11
+ return { kind: 'paper', doi, analysisPath, ...(focusInsightPath ? { focusInsightPath } : {}) };
12
+ }
13
+
14
+ export function sameEntry(left: DetailEntry, right: DetailEntry): boolean {
15
+ if (left.kind !== right.kind || left.analysisPath !== right.analysisPath) return false;
16
+ return left.kind === 'record'
17
+ ? left.canonicalPath === (right as Extract<DetailEntry, { kind: 'record' }>).canonicalPath
18
+ : left.doi === (right as Extract<DetailEntry, { kind: 'paper' }>).doi;
19
+ }
@@ -0,0 +1,99 @@
1
+ import type { ResolvedInsight } from '@astra-spec/sdk';
2
+ import { Fragment, forwardRef, type HTMLAttributes } from 'react';
3
+ import { recordTitle } from '../model/records.js';
4
+ import { doiHref } from '../model/doi.js';
5
+ import { findingLiterature, type FindingEvidenceLink } from '../model/relations.js';
6
+ import { cn } from '../lib/cn.js';
7
+ import { useLabels } from '../lib/labels.js';
8
+ import { DetailLayout, DetailMain } from '../primitives/detail-layout.js';
9
+ import { DetailDialog, type DetailDialogProps } from '../primitives/dialog.js';
10
+ import { Prose, type TextRenderer } from '../primitives/prose.js';
11
+ import { RelationList } from '../primitives/relation-list.js';
12
+ import type { OpenRecordHandler } from './relation-items.js';
13
+
14
+ export interface FindingDetailProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
15
+ record: ResolvedInsight;
16
+ /** Artifact-backed evidence resolved to outputs; see `findingEvidence()`. */
17
+ evidence: FindingEvidenceLink[];
18
+ renderText?: TextRenderer | undefined;
19
+ onOpenRecord?: OpenRecordHandler | undefined;
20
+ }
21
+
22
+ /** Notes, the supporting results a finding cites, and its literature sources. */
23
+ export const FindingDetail = forwardRef<HTMLDivElement, FindingDetailProps>(function FindingDetail({
24
+ record,
25
+ evidence,
26
+ renderText,
27
+ onOpenRecord,
28
+ className,
29
+ ...props
30
+ }, ref) {
31
+ const literature = findingLiterature(record);
32
+ return (
33
+ <DetailLayout data-slot="finding-detail" {...props} ref={ref} layout="single" className={cn('astra-finding-detail', className)}>
34
+ <DetailMain>
35
+ {record.notes ? (
36
+ <section className="astra-finding-detail__notes">
37
+ <h4>Notes</h4>
38
+ <div><Prose text={record.notes} field="notes" renderText={renderText} /></div>
39
+ </section>
40
+ ) : null}
41
+ <RelationList
42
+ className="astra-finding-detail__results"
43
+ title="Supporting results"
44
+ empty="No supporting results are linked to this finding."
45
+ items={evidence.map((item, index) => {
46
+ const title = item.output
47
+ ? recordTitle(item.output)
48
+ : item.evidence.artifact ?? `Result ${index + 1}`;
49
+ const { output, analysis } = item;
50
+ return {
51
+ key: `${item.evidence.resolvedOutputPath ?? item.evidence.artifact ?? 'result'}-${index}`,
52
+ label: title,
53
+ identifier: output?.canonicalPath ?? item.evidence.artifact,
54
+ detail: output?.type ?? 'Unavailable',
55
+ kind: 'output' as const,
56
+ accessibleLabel: output ? `View supporting result: ${title}` : undefined,
57
+ onOpen: output && analysis && onOpenRecord ? () => { onOpenRecord(output, analysis); } : undefined,
58
+ };
59
+ })}
60
+ />
61
+ {literature.map((source, index) => {
62
+ const doi = source.doi ?? '';
63
+ const location = source.location?.page ? ` · page ${source.location.page}` : '';
64
+ return (
65
+ <Fragment key={`${doi}-${index}`}>
66
+ <section className="astra-insight-detail__paper astra-paper-doi">
67
+ <h4>Source paper</h4>
68
+ <a href={doiHref(doi)} target="_blank" rel="noreferrer">{doi}{location} ↗</a>
69
+ </section>
70
+ {source.quote ? (
71
+ <section className="astra-insight-detail__source-quote">
72
+ <h4>Source passage</h4>
73
+ <blockquote><Prose text={source.quote.exact} field="quote" renderText={renderText} /></blockquote>
74
+ </section>
75
+ ) : null}
76
+ </Fragment>
77
+ );
78
+ })}
79
+ </DetailMain>
80
+ </DetailLayout>
81
+ );
82
+ });
83
+
84
+ export interface FindingDialogProps extends Pick<DetailDialogProps, 'mode' | 'backText' | 'className' | 'onBack' | 'onClose'>, Omit<FindingDetailProps, 'className'> {}
85
+
86
+ export function FindingDialog({ record, evidence, renderText, onOpenRecord, ...dialog }: FindingDialogProps) {
87
+ const labels = useLabels();
88
+ return (
89
+ <DetailDialog
90
+ {...dialog}
91
+ kind="finding"
92
+ kindLabel={labels.kinds.finding}
93
+ title={record.claim}
94
+ closeLabel={labels.closeRecord(labels.kinds.finding)}
95
+ >
96
+ <FindingDetail record={record} evidence={evidence} renderText={renderText} onOpenRecord={onOpenRecord} />
97
+ </DetailDialog>
98
+ );
99
+ }
@@ -0,0 +1,44 @@
1
+ /** ASTRA-aware components: one detail body and one dialog per record kind (input, output, decision, finding, prior insight) plus papers, the RecordDialog router, and the detail stack. */
2
+ export { recordEntry, paperEntry, sameEntry } from './detail-entry.js';
3
+ export type { DetailEntry } from './detail-entry.js';
4
+ export { useDetailStack } from './use-detail-stack.js';
5
+ export type { DetailStack, DetailStackOptions } from './use-detail-stack.js';
6
+ export { relationItemForRecord, relationItemsForLinks } from './relation-items.js';
7
+ export type { OpenRecordHandler } from './relation-items.js';
8
+ export { OutputDetail, OutputDialogActions, OutputPreview, useOutputExpanded } from './output-detail.js';
9
+ export type { OutputDetailProps, OutputDialogActionsProps, OutputPreviewProps } from './output-detail.js';
10
+ export { OutputDialog } from './output-dialog.js';
11
+ export type { OutputDialogProps } from './output-dialog.js';
12
+ export { DecisionDetail, DecisionDialog } from './decision-detail.js';
13
+ export type { DecisionDetailProps, DecisionDialogProps } from './decision-detail.js';
14
+ export { FindingDetail, FindingDialog } from './finding-detail.js';
15
+ export type { FindingDetailProps, FindingDialogProps } from './finding-detail.js';
16
+ export { InputDetail, InputDialog, inputSourceLabel } from './input-detail.js';
17
+ export type { InputDetailProps, InputDialogProps } from './input-detail.js';
18
+ export { InsightDetail, InsightDialog, primaryLiteratureEvidence } from './insight-detail.js';
19
+ export type { InsightDetailProps, InsightDialogProps } from './insight-detail.js';
20
+ export { InsightEvidenceTitle, InsightTrigger } from './insight-trigger.js';
21
+ export type { InsightEvidenceTitleProps, InsightTriggerProps } from './insight-trigger.js';
22
+ export { PaperDetail, PaperDialog, PaperDialogActions } from './paper-detail.js';
23
+ export type { PaperDetailProps, PaperDialogActionsProps, PaperDialogProps, PaperRenderOptions, PaperRenderer } from './paper-detail.js';
24
+ export { RecordDialog } from './record-dialog.js';
25
+ export type { RecordDialogProps } from './record-dialog.js';
26
+ export {
27
+ ArtifactPreview,
28
+ metricPreviewFromJson,
29
+ tablePreviewFromDelimited,
30
+ tablePreviewFromRows,
31
+ } from './artifact-preview.js';
32
+ export type {
33
+ ArtifactPreviewData,
34
+ ArtifactPreviewProps,
35
+ ArtifactRenderOptions,
36
+ ArtifactRenderer,
37
+ DelimitedPreviewOptions,
38
+ ImagePreviewData,
39
+ LoadingPreviewData,
40
+ MetricPreviewData,
41
+ TablePreviewData,
42
+ TextPreviewData,
43
+ UnavailablePreviewData,
44
+ } from './artifact-preview.js';
@@ -0,0 +1,67 @@
1
+ import type { ResolvedInput } from '@astra-spec/sdk';
2
+ import { forwardRef, type HTMLAttributes } from 'react';
3
+ import { recordTitle } from '../model/records.js';
4
+ import { useLabels } from '../lib/labels.js';
5
+ import { DetailLayout, DetailMain, DetailSection } from '../primitives/detail-layout.js';
6
+ import { DetailDialog, type DetailDialogProps } from '../primitives/dialog.js';
7
+ import { Prose, type TextRenderer } from '../primitives/prose.js';
8
+
9
+ export function inputSourceLabel(record: ResolvedInput): string {
10
+ return record.source ?? record.ref ?? record.resolvedFrom ?? 'Source not declared';
11
+ }
12
+
13
+ export interface InputDetailProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
14
+ record: ResolvedInput;
15
+ renderText?: TextRenderer | undefined;
16
+ }
17
+
18
+ /** Description, the record an alias resolves from, and the declared source of an input. */
19
+ export const InputDetail = forwardRef<HTMLDivElement, InputDetailProps>(function InputDetail({
20
+ record,
21
+ renderText,
22
+ className,
23
+ ...props
24
+ }, ref) {
25
+ const declared = record.source ?? record.ref;
26
+ const primary = record.resolvedFrom ?? declared ?? 'Source not declared';
27
+ return (
28
+ <DetailLayout data-slot="input-detail" {...props} ref={ref} layout="single" className={className}>
29
+ <DetailMain>
30
+ {record.description ? (
31
+ <DetailSection label="Description" heading="section">
32
+ <Prose text={record.description} field="description" renderText={renderText} />
33
+ </DetailSection>
34
+ ) : null}
35
+ <section className="astra-input-source">
36
+ <h4>{record.resolvedFrom ? 'Resolved from' : 'Source'}</h4>
37
+ {/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- long paths scroll horizontally */}
38
+ <code tabIndex={0} title={primary}>{primary}</code>
39
+ </section>
40
+ {record.resolvedFrom && declared ? (
41
+ <section className="astra-input-source">
42
+ <h4>Source</h4>
43
+ {/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- long paths scroll horizontally */}
44
+ <code tabIndex={0} title={declared}>{declared}</code>
45
+ </section>
46
+ ) : null}
47
+ </DetailMain>
48
+ </DetailLayout>
49
+ );
50
+ });
51
+
52
+ export interface InputDialogProps extends Pick<DetailDialogProps, 'mode' | 'backText' | 'className' | 'onBack' | 'onClose'>, Omit<InputDetailProps, 'className'> {}
53
+
54
+ export function InputDialog({ record, renderText, ...dialog }: InputDialogProps) {
55
+ const labels = useLabels();
56
+ return (
57
+ <DetailDialog
58
+ {...dialog}
59
+ kind="input"
60
+ kindLabel={labels.kinds.input}
61
+ title={recordTitle(record)}
62
+ closeLabel={labels.closeRecord(labels.kinds.input)}
63
+ >
64
+ <InputDetail record={record} renderText={renderText} />
65
+ </DetailDialog>
66
+ );
67
+ }