@adecore/agents-react 0.70.1 → 0.71.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 (82) hide show
  1. package/dist/chat/ui/CodeBlock.d.ts +6 -1
  2. package/dist/chat/ui/CodeBlock.js +39 -23
  3. package/dist/chat/ui/intelligent-ui/UiReply.js +11 -2
  4. package/dist/chat/ui/intelligent-ui/key-caps.d.ts +1 -0
  5. package/dist/chat/ui/intelligent-ui/key-caps.js +45 -0
  6. package/dist/chat/ui/intelligent-ui/link-action.d.ts +9 -0
  7. package/dist/chat/ui/intelligent-ui/link-action.js +32 -0
  8. package/dist/chat/ui/intelligent-ui/node-text.js +1 -1
  9. package/dist/chat/ui/intelligent-ui/patch-clip.d.ts +4 -0
  10. package/dist/chat/ui/intelligent-ui/patch-clip.js +64 -0
  11. package/dist/chat/ui/intelligent-ui/registry.js +31 -5
  12. package/dist/chat/ui/intelligent-ui/render-context.d.ts +13 -0
  13. package/dist/chat/ui/intelligent-ui/renderers/alternatives.d.ts +6 -0
  14. package/dist/chat/ui/intelligent-ui/renderers/alternatives.js +296 -0
  15. package/dist/chat/ui/intelligent-ui/renderers/changes.d.ts +4 -0
  16. package/dist/chat/ui/intelligent-ui/renderers/changes.js +266 -0
  17. package/dist/chat/ui/intelligent-ui/renderers/chart.js +522 -371
  18. package/dist/chat/ui/intelligent-ui/renderers/content.d.ts +1 -1
  19. package/dist/chat/ui/intelligent-ui/renderers/content.js +94 -43
  20. package/dist/chat/ui/intelligent-ui/renderers/data.js +478 -245
  21. package/dist/chat/ui/intelligent-ui/renderers/diff.d.ts +4 -0
  22. package/dist/chat/ui/intelligent-ui/renderers/diff.js +427 -0
  23. package/dist/chat/ui/intelligent-ui/renderers/inputs.d.ts +1 -0
  24. package/dist/chat/ui/intelligent-ui/renderers/inputs.js +354 -110
  25. package/dist/chat/ui/intelligent-ui/renderers/keys.d.ts +3 -0
  26. package/dist/chat/ui/intelligent-ui/renderers/keys.js +60 -0
  27. package/dist/chat/ui/intelligent-ui/renderers/links.d.ts +13 -2
  28. package/dist/chat/ui/intelligent-ui/renderers/links.js +65 -85
  29. package/dist/chat/ui/intelligent-ui/renderers/meter.d.ts +4 -0
  30. package/dist/chat/ui/intelligent-ui/renderers/meter.js +371 -0
  31. package/dist/chat/ui/intelligent-ui/renderers/quote.d.ts +3 -0
  32. package/dist/chat/ui/intelligent-ui/renderers/quote.js +123 -0
  33. package/dist/chat/ui/intelligent-ui/renderers/radio.d.ts +3 -0
  34. package/dist/chat/ui/intelligent-ui/renderers/radio.js +205 -0
  35. package/dist/chat/ui/intelligent-ui/renderers/structure.d.ts +2 -0
  36. package/dist/chat/ui/intelligent-ui/renderers/structure.js +156 -45
  37. package/dist/chat/ui/intelligent-ui/renderers/text.js +365 -215
  38. package/dist/chat/ui/intelligent-ui/renderers/tree.d.ts +5 -0
  39. package/dist/chat/ui/intelligent-ui/renderers/tree.js +284 -0
  40. package/dist/chat/ui/intelligent-ui/table-data.d.ts +9 -2
  41. package/dist/chat/ui/intelligent-ui/table-data.js +68 -3
  42. package/dist/chat/ui/intelligent-ui/tones.d.ts +1 -1
  43. package/dist/chat/ui/intelligent-ui/tones.js +7 -7
  44. package/dist/chat/ui/intelligent-ui/use-ui-diffs.d.ts +4 -0
  45. package/dist/chat/ui/intelligent-ui/use-ui-diffs.js +201 -0
  46. package/dist/host/host.d.ts +2 -1
  47. package/dist/locales/en/agent-chat.d.ts +56 -0
  48. package/dist/locales/en/agent-chat.js +57 -1
  49. package/dist/locales/nl/agent-chat.d.ts +56 -0
  50. package/dist/locales/nl/agent-chat.js +57 -1
  51. package/dist/theme.css +6 -0
  52. package/package.json +9 -9
  53. package/src/chat/ui/CodeBlock.tsx +38 -18
  54. package/src/chat/ui/intelligent-ui/UiReply.tsx +13 -2
  55. package/src/chat/ui/intelligent-ui/key-caps.ts +51 -0
  56. package/src/chat/ui/intelligent-ui/link-action.ts +32 -0
  57. package/src/chat/ui/intelligent-ui/node-text.ts +1 -0
  58. package/src/chat/ui/intelligent-ui/patch-clip.ts +83 -0
  59. package/src/chat/ui/intelligent-ui/registry.ts +31 -4
  60. package/src/chat/ui/intelligent-ui/render-context.ts +21 -0
  61. package/src/chat/ui/intelligent-ui/renderers/alternatives.tsx +76 -0
  62. package/src/chat/ui/intelligent-ui/renderers/changes.tsx +97 -0
  63. package/src/chat/ui/intelligent-ui/renderers/chart.tsx +122 -46
  64. package/src/chat/ui/intelligent-ui/renderers/content.tsx +27 -7
  65. package/src/chat/ui/intelligent-ui/renderers/data.tsx +165 -52
  66. package/src/chat/ui/intelligent-ui/renderers/diff.tsx +144 -0
  67. package/src/chat/ui/intelligent-ui/renderers/inputs.tsx +111 -19
  68. package/src/chat/ui/intelligent-ui/renderers/keys.tsx +26 -0
  69. package/src/chat/ui/intelligent-ui/renderers/links.tsx +40 -24
  70. package/src/chat/ui/intelligent-ui/renderers/meter.tsx +105 -0
  71. package/src/chat/ui/intelligent-ui/renderers/quote.tsx +29 -0
  72. package/src/chat/ui/intelligent-ui/renderers/radio.tsx +130 -0
  73. package/src/chat/ui/intelligent-ui/renderers/structure.tsx +54 -7
  74. package/src/chat/ui/intelligent-ui/renderers/text.tsx +70 -36
  75. package/src/chat/ui/intelligent-ui/renderers/tree.tsx +85 -0
  76. package/src/chat/ui/intelligent-ui/table-data.ts +84 -6
  77. package/src/chat/ui/intelligent-ui/tones.ts +7 -7
  78. package/src/chat/ui/intelligent-ui/use-ui-diffs.ts +119 -0
  79. package/src/host/host.ts +4 -0
  80. package/src/locales/en/agent-chat.ts +57 -1
  81. package/src/locales/nl/agent-chat.ts +57 -1
  82. package/src/theme.css +6 -0
@@ -1,4 +1,4 @@
1
- import type { UiProps } from '@adecore/intelligent-ui';
1
+ import type { UiProps, UiTone } from '@adecore/intelligent-ui';
2
2
  import { Button, Icon, KeyValueList, Pill, ScrollArea, Tooltip } from '@adecore/ui';
3
3
  import { formatBytes, formatDuration, formatMoment, formatNumber, formatPercent, formatRounded } from '@adecore/ui/format';
4
4
  import clsx from 'clsx';
@@ -6,10 +6,20 @@ import { ArrowDown, ArrowRight, ArrowUp } from 'lucide-react';
6
6
  import { createContext, useContext, type ReactNode } from 'react';
7
7
  import { useTranslation } from 'react-i18next';
8
8
  import { useBlockLocal } from '../block-local';
9
- import { uiChildrenOf, uiWithUnit } from '../node-text';
9
+ import { uiChildrenOf, uiNodeLabel, uiWithUnit } from '../node-text';
10
10
  import type { UiLiveValue, UiRendererProps } from '../render-context';
11
- import { isNumericColumn, UI_TABLE_ROWS, uiTableCell, uiTableColumns, type UiTableCell, type UiTableColumn } from '../table-data';
12
- import { UI_TONE_PILL } from '../tones';
11
+ import {
12
+ isNumericColumn,
13
+ UI_TABLE_ROWS,
14
+ uiTableCell,
15
+ uiTableColumns,
16
+ uiTableOrder,
17
+ uiTableSort,
18
+ type UiTableCell,
19
+ type UiTableColumn,
20
+ type UiTableSort
21
+ } from '../table-data';
22
+ import { UI_TONE_PILL, UI_TONE_TEXT } from '../tones';
13
23
  import { UiFallbackPart } from '../UiFallbackPart';
14
24
 
15
25
  /* EntityList draws a hairline between its rows from this many on. */
@@ -52,52 +62,97 @@ export function StatsRenderer({ children }: UiRendererProps<UiProps<'Stats'>>) {
52
62
  return <div className="chat-ui-nodes grid grid-cols-[repeat(auto-fit,minmax(128px,1fr))] gap-2">{children}</div>;
53
63
  }
54
64
 
55
- /* The arrow and the number say which way a value went; only a tone says whether that is good. The tag says the change, its tooltip from what. */
65
+ const TREND_WIDTH = 100;
66
+ const TREND_HEIGHT = 24;
67
+ /* Room above and below the line, so its round ends are not cut at the highest and the lowest point. */
68
+ const TREND_PAD = 2;
69
+
70
+ /* The course of a value as one line over the width of its tile, on a scale of its own; a course that never moves is a level line. */
71
+ function TrendLine({ points, label, className }: { points: readonly number[]; label: string; className: string }) {
72
+ const low = Math.min(...points);
73
+ const span = Math.max(...points) - low;
74
+
75
+ const path = points
76
+ .map((point, index) => {
77
+ const x = (index / (points.length - 1)) * TREND_WIDTH;
78
+ const y = span === 0 ? TREND_HEIGHT / 2 : TREND_PAD + (1 - (point - low) / span) * (TREND_HEIGHT - 2 * TREND_PAD);
79
+
80
+ return `${index === 0 ? 'M' : 'L'}${Math.round(x * 100) / 100},${Math.round(y * 100) / 100}`;
81
+ })
82
+ .join(' ');
83
+
84
+ return (
85
+ <svg role="img" aria-label={label} viewBox={`0 0 ${TREND_WIDTH} ${TREND_HEIGHT}`} preserveAspectRatio="none" className={clsx('h-6 w-full', className)}>
86
+ <path d={path} fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" vectorEffect="non-scaling-stroke" />
87
+ </svg>
88
+ );
89
+ }
90
+
91
+ /*
92
+ * The arrow and the sign say which way a value went; only a tone says whether that is good. The change
93
+ * stands at the end of the value's line, with what it changed from in its tooltip. A value in words
94
+ * stands as written and compares with nothing.
95
+ */
56
96
  export function StatRenderer({ node, context }: UiRendererProps<UiProps<'Stat'>>) {
57
97
  const { t } = useTranslation('agent-chat');
58
- const { label, value, previous, unit, tone } = node.props;
98
+ const { label, value, previous, unit, tone, trend } = node.props;
59
99
  const live = context.liveValue?.(node.id, 'value');
60
- const change = previous === undefined ? null : value - previous;
100
+ // A percent sign hugs its number; any other unit stands apart from it, smaller.
101
+ const apart = typeof value === 'number' && unit !== undefined && unit !== '' && unit !== '%';
102
+ const withUnit = (number: number): string => uiWithUnit(formatRounded(number, 2), unit);
103
+ const figureOf = (number: number): string => (apart ? formatRounded(number, 2) : withUnit(number));
104
+ const change = previous === undefined || typeof value !== 'number' ? null : value - previous;
61
105
  const arrow = change === null || change === 0 ? ArrowRight : change > 0 ? ArrowUp : ArrowDown;
62
- const from = previous === undefined ? '' : uiWithUnit(formatRounded(previous, 2), unit);
106
+ const from = previous === undefined ? '' : withUnit(previous);
63
107
  // A change from zero has no percentage.
64
108
  const percent = change === null || previous === undefined || previous === 0 ? null : formatPercent(Math.abs((change / previous) * 100));
65
109
  const said = percent === null ? t('blocks.stat.from', { previous: from }) : t('blocks.stat.change', { change: percent, previous: from });
110
+ const sign = change === null || change === 0 ? '' : change > 0 ? '+' : '-';
66
111
 
67
112
  return (
68
- <div className="flex min-w-0 flex-col items-start gap-1.5 rounded-xl bg-ui-tile p-3">
113
+ <div className="flex min-w-0 flex-col gap-1.5 rounded-xl bg-ui-tile p-3">
69
114
  <span className="max-w-full truncate text-2xs text-text-muted">{label}</span>
70
- <LiveFigure
71
- text={uiWithUnit(formatRounded(value, 2), unit)}
72
- previous={typeof live?.previous === 'number' ? uiWithUnit(formatRounded(live.previous, 2), unit) : undefined}
73
- live={live}
74
- className="max-w-full truncate text-lg font-semibold text-text tabular-nums"
75
- />
76
- {change !== null && (
77
- <Tooltip label={said}>
78
- <span tabIndex={0} className="max-w-full rounded-md">
79
- <Pill
80
- shape="tag"
81
- size="sm"
82
- tone={tone === undefined ? 'muted' : UI_TONE_PILL[tone]}
83
- icon={<Icon icon={arrow} size={12} className="shrink-0" />}
115
+ <div className="flex min-w-0 items-baseline gap-1.5">
116
+ <LiveFigure
117
+ text={typeof value === 'number' ? figureOf(value) : value}
118
+ previous={typeof live?.previous === 'number' ? figureOf(live.previous) : undefined}
119
+ live={live}
120
+ className="min-w-0 truncate text-lg font-semibold text-text tabular-nums"
121
+ />
122
+ {apart && <span className="max-w-1/2 shrink-0 truncate text-xs text-text-muted">{unit}</span>}
123
+ {change !== null && (
124
+ <Tooltip label={said}>
125
+ <span
126
+ tabIndex={0}
127
+ className={clsx(
128
+ 'ml-auto flex shrink-0 items-center gap-0.5 rounded-sm text-2xs font-semibold tabular-nums',
129
+ UI_TONE_TEXT[tone ?? 'neutral']
130
+ )}
84
131
  >
85
- <span aria-hidden className="tabular-nums">
86
- {percent ?? from}
87
- </span>
132
+ <Icon icon={arrow} size={12} className="shrink-0" />
133
+ <span aria-hidden>{percent === null ? from : `${sign}${percent}`}</span>
88
134
  <span className="sr-only">{said}</span>
89
- </Pill>
90
- </span>
91
- </Tooltip>
135
+ </span>
136
+ </Tooltip>
137
+ )}
138
+ </div>
139
+ {trend !== undefined && (
140
+ <TrendLine
141
+ points={trend}
142
+ label={t('blocks.stat.trend', { first: withUnit(trend[0]!), last: withUnit(trend.at(-1)!) })}
143
+ className={UI_TONE_TEXT[tone ?? 'neutral']}
144
+ />
92
145
  )}
93
146
  </div>
94
147
  );
95
148
  }
96
149
 
150
+ /* Tiles up to four entries and a list from five, unless `layout` says which. */
97
151
  export function EntityListRenderer({ node, children }: UiRendererProps<UiProps<'EntityList'>>) {
98
152
  const count = uiChildrenOf(node, 'Entry').length;
153
+ const { layout } = node.props;
99
154
 
100
- if (count <= TILED_UP_TO) {
155
+ if (layout === undefined ? count <= TILED_UP_TO : layout === 'tiles') {
101
156
  return (
102
157
  <EntityTiles value>
103
158
  <div className="chat-ui-nodes grid grid-cols-[repeat(auto-fit,minmax(140px,1fr))] gap-2">{children}</div>
@@ -114,12 +169,13 @@ export function EntityListRenderer({ node, children }: UiRendererProps<UiProps<'
114
169
 
115
170
  export function EntryRenderer({ node, children }: UiRendererProps<UiProps<'Entry'>>) {
116
171
  const tiled = useContext(EntityTiles);
172
+ const value = entryValue(node.props, uiNodeLabel(node)) ?? children;
117
173
 
118
174
  if (tiled) {
119
175
  return (
120
176
  <div className="flex min-w-0 flex-col gap-0.5 rounded-xl bg-ui-tile px-3 py-2">
121
177
  <span className="truncate text-2xs text-text-muted">{node.props.label}</span>
122
- <span className="text-2xs font-semibold wrap-break-word text-text">{children}</span>
178
+ <span className="text-2xs font-semibold wrap-break-word text-text">{value}</span>
123
179
  </div>
124
180
  );
125
181
  }
@@ -127,13 +183,22 @@ export function EntryRenderer({ node, children }: UiRendererProps<UiProps<'Entry
127
183
  return (
128
184
  <KeyValueList.Item>
129
185
  <KeyValueList.Name>{node.props.label}</KeyValueList.Name>
130
- <KeyValueList.Value>{children}</KeyValueList.Value>
186
+ <KeyValueList.Value>{value}</KeyValueList.Value>
131
187
  </KeyValueList.Item>
132
188
  );
133
189
  }
134
190
 
135
191
  function numberText(cell: Extract<UiTableCell, { kind: 'number' }>, unit: string | undefined): string {
136
- return cell.as === 'bytes' ? formatBytes(cell.value) : cell.as === 'duration' ? formatDuration(cell.value) : uiWithUnit(formatRounded(cell.value, 2), unit);
192
+ switch (cell.as) {
193
+ case 'bytes':
194
+ return formatBytes(cell.value);
195
+ case 'duration':
196
+ return formatDuration(cell.value);
197
+ case 'percent':
198
+ return formatPercent(cell.value);
199
+ case 'number':
200
+ return uiWithUnit(formatRounded(cell.value, 2), unit);
201
+ }
137
202
  }
138
203
 
139
204
  /* What a cell reads as, so two readings that draw the same do not light up. */
@@ -153,6 +218,11 @@ function cellText(cell: UiTableCell, column: UiTableColumn): string {
153
218
  }
154
219
  }
155
220
 
221
+ /* The tone a column gives a tag by its text; a text the column does not name stays quiet. */
222
+ function tagTone(text: string, column: UiTableColumn): UiTone | undefined {
223
+ return column.tones !== undefined && Object.hasOwn(column.tones, text) ? column.tones[text] : undefined;
224
+ }
225
+
156
226
  function cellContent(cell: Exclude<UiTableCell, { kind: 'empty' }>, column: UiTableColumn): ReactNode {
157
227
  switch (cell.kind) {
158
228
  case 'text':
@@ -164,13 +234,29 @@ function cellContent(cell: Exclude<UiTableCell, { kind: 'empty' }>, column: UiTa
164
234
  case 'file':
165
235
  // A cell is no link target the host checked, so its path stays text.
166
236
  return <span className="font-mono text-code break-all">{cell.path}</span>;
167
- case 'tag':
237
+ case 'tag': {
238
+ const tone = tagTone(cell.text, column);
239
+
168
240
  return (
169
- <Pill shape="tag" size="sm">
241
+ <Pill shape="tag" size="sm" tone={tone === undefined ? 'muted' : UI_TONE_PILL[tone]}>
170
242
  {cell.text}
171
243
  </Pill>
172
244
  );
245
+ }
246
+ }
247
+ }
248
+
249
+ /* The value of an Entry drawn as a table cell of its kind, or null where it stands as written: without a kind, and for text the kind cannot read. */
250
+ function entryValue({ label, as }: UiProps<'Entry'>, written: string): ReactNode {
251
+ if (as === undefined || as === 'text') {
252
+ return null;
173
253
  }
254
+
255
+ const column: UiTableColumn = { key: 'value', title: label, as };
256
+ // A date in milliseconds arrives here as its digits, which a table would get as a number.
257
+ const cell = uiTableCell(as === 'date' && /^\d+$/.test(written) ? Number(written) : written, column);
258
+
259
+ return cell.kind === 'empty' || cell.kind === 'text' ? null : cellContent(cell, column);
174
260
  }
175
261
 
176
262
  function Cell({ value, column, live }: { value: unknown; column: UiTableColumn; live?: UiLiveValue }) {
@@ -193,10 +279,12 @@ function Cell({ value, column, live }: { value: unknown; column: UiTableColumn;
193
279
  /*
194
280
  * Rows under a sticky head, scrolling inside 320px and sideways when wider than the column. Rows
195
281
  * arrive one by one while the block streams, without animation; past fifty a person asks for the rest.
282
+ * A head sorts by its column, from the order `sort` asked for until a person picks one.
196
283
  */
197
284
  export function TableRenderer({ node, context }: UiRendererProps<UiProps<'Table'>>) {
198
285
  const { t } = useTranslation('agent-chat');
199
286
  const [all, setAll] = useBlockLocal(context, node.id, () => false);
287
+ const [picked, setPicked] = useBlockLocal<UiTableSort | null>(context, `${node.id}:sort`, () => null);
200
288
  const columns = uiTableColumns(node);
201
289
 
202
290
  if (columns.length === 0) {
@@ -204,7 +292,11 @@ export function TableRenderer({ node, context }: UiRendererProps<UiProps<'Table'
204
292
  }
205
293
 
206
294
  const { rows } = node.props;
207
- const shown = all ? rows : rows.slice(0, UI_TABLE_ROWS);
295
+ const sort = picked !== null && columns.some((column) => column.key === picked.key) ? picked : uiTableSort(node.props.sort, columns);
296
+ const sorted = columns.find((column) => column.key === sort?.key);
297
+ // A row keeps its place in `rows` whatever the order: live readings address it by that.
298
+ const order = sorted === undefined || sort === null ? rows.map((_, index) => index) : uiTableOrder(rows, sorted, sort.descending);
299
+ const shown = all ? order : order.slice(0, UI_TABLE_ROWS);
208
300
 
209
301
  return (
210
302
  <div className="flex flex-col items-start gap-1.5">
@@ -220,35 +312,56 @@ export function TableRenderer({ node, context }: UiRendererProps<UiProps<'Table'
220
312
  >
221
313
  <thead className="sticky top-0 z-1 bg-ui-tile">
222
314
  <tr>
223
- {columns.map((column) => (
224
- <th
225
- key={column.key}
226
- scope="col"
227
- className={clsx(
228
- 'h-8 px-2 text-2xs font-medium whitespace-nowrap text-text-muted',
229
- isNumericColumn(column) ? 'text-right' : 'text-left'
230
- )}
231
- >
232
- {column.title}
233
- </th>
234
- ))}
315
+ {columns.map((column) => {
316
+ const descending = sort?.key === column.key ? sort.descending : null;
317
+
318
+ return (
319
+ <th
320
+ key={column.key}
321
+ scope="col"
322
+ aria-sort={descending === null ? undefined : descending ? 'descending' : 'ascending'}
323
+ className={clsx(
324
+ 'h-8 px-2 text-2xs font-medium whitespace-nowrap',
325
+ isNumericColumn(column) ? 'text-right' : 'text-left'
326
+ )}
327
+ >
328
+ <button
329
+ type="button"
330
+ // The arrow stands on the side away from the cells, so a title stays in line with its column.
331
+ className={clsx(
332
+ 'inline-flex items-center gap-1 rounded-sm text-left hover:text-text',
333
+ descending === null ? 'text-text-muted' : 'text-text',
334
+ isNumericColumn(column) && 'flex-row-reverse'
335
+ )}
336
+ onClick={() => setPicked({ key: column.key, descending: descending === false })}
337
+ >
338
+ {column.title}
339
+ {descending !== null && <Icon icon={descending ? ArrowDown : ArrowUp} size={12} className="shrink-0" />}
340
+ </button>
341
+ </th>
342
+ );
343
+ })}
235
344
  </tr>
236
345
  </thead>
237
346
  <tbody>
238
- {shown.map((row, index) => (
239
- // Rows only ever grow at the end while a block streams, so the place of a row is a stable key.
347
+ {shown.map((index, place) => (
348
+ // Rows only ever grow at the end while a block streams, so the place of a row in `rows` is a stable key.
240
349
  <tr key={index}>
241
350
  {columns.map((column, at) => (
242
351
  <td
243
352
  key={column.key}
244
353
  className={clsx(
245
354
  'h-9 bg-ui-inset px-2 text-text',
246
- index > 0 && 'border-t border-border-soft',
355
+ place > 0 && 'border-t border-border-soft',
247
356
  at === 0 && 'font-medium',
248
357
  isNumericColumn(column) ? 'text-right whitespace-nowrap' : 'text-left'
249
358
  )}
250
359
  >
251
- <Cell value={row[column.key]} column={column} live={context.liveValue?.(node.id, 'rows', [index, column.key])} />
360
+ <Cell
361
+ value={rows[index]![column.key]}
362
+ column={column}
363
+ live={context.liveValue?.(node.id, 'rows', [index, column.key])}
364
+ />
252
365
  </td>
253
366
  ))}
254
367
  </tr>
@@ -0,0 +1,144 @@
1
+ import type { UiProps } from '@adecore/intelligent-ui';
2
+ import { Button, LazyBoundary, lazyNamed, Spinner } from '@adecore/ui';
3
+ import type { ReactNode } from 'react';
4
+ import { useTranslation } from 'react-i18next';
5
+ import { uiReasonText } from '../node-text';
6
+ import { uiClipPatch } from '../patch-clip';
7
+ import type { UiDiff, UiLinkTarget, UiRenderContext, UiRendererProps } from '../render-context';
8
+ import { LinkRow, UiLinkChip } from './links';
9
+
10
+ const UnifiedDiff = lazyNamed(() => import('../../UnifiedDiff'), 'default');
11
+
12
+ /* The lines of change and context an open Diff shows in its block; the whole change opens in the host's own diff. */
13
+ export const UI_DIFF_LINES = 200;
14
+
15
+ /* The tallest the patch stands in its tile, in pixels, past which it scrolls. */
16
+ const DIFF_HEIGHT = 320;
17
+
18
+ function changeKind(patch: string): 'add' | 'update' | 'delete' {
19
+ return /^new file mode /m.test(patch) ? 'add' : /^deleted file mode /m.test(patch) ? 'delete' : 'update';
20
+ }
21
+
22
+ /* The chip of the file and what the agent says about it, with whatever the state adds between them. */
23
+ function DiffHead({ chip, described, children }: { chip: ReactNode; described: boolean; children: ReactNode }) {
24
+ return (
25
+ <div className="flex min-w-0 flex-wrap items-baseline gap-x-2 gap-y-1 text-xs">
26
+ <span className="flex max-w-full min-w-0 items-center gap-1.5">{chip}</span>
27
+ {described && <span className="min-w-0 wrap-break-word text-text-muted">{children}</span>}
28
+ </div>
29
+ );
30
+ }
31
+
32
+ /*
33
+ * The change of one file as the host read it from git, in a tile headed by the chip a closed Diff is. The
34
+ * block shows the first `UI_DIFF_LINES` lines; past them, or where the host cut the patch itself, a button
35
+ * opens the whole change as the chip does.
36
+ */
37
+ function OpenDiff({
38
+ target,
39
+ diff,
40
+ hideWhitespace,
41
+ described,
42
+ context,
43
+ children
44
+ }: {
45
+ target: UiLinkTarget & { type: 'Diff' };
46
+ diff: UiDiff & { state: 'patch' };
47
+ hideWhitespace: boolean;
48
+ described: boolean;
49
+ context: UiRenderContext;
50
+ children: ReactNode;
51
+ }) {
52
+ const { t } = useTranslation('agent-chat');
53
+ const { patch, clipped } = uiClipPatch(diff.patch, UI_DIFF_LINES);
54
+ const partial = clipped || diff.truncated;
55
+ const { openLink } = context;
56
+ const openable = openLink !== undefined && context.link?.(target).state === 'chip';
57
+
58
+ return (
59
+ <figure aria-label={t('blocks.diff.label', { path: target.path })} className="flex min-w-0 flex-col gap-2 rounded-xl bg-ui-tile p-2">
60
+ <figcaption className="px-1 pt-0.5">
61
+ <DiffHead chip={<UiLinkChip target={target} context={context} />} described={described}>
62
+ {children}
63
+ </DiffHead>
64
+ </figcaption>
65
+ {patch.trim() === '' ? (
66
+ <p className="px-1 pb-0.5 text-xs text-text-muted">{t('blocks.diff.unchanged')}</p>
67
+ ) : (
68
+ <div className="overflow-hidden rounded-lg bg-ui-inset">
69
+ <LazyBoundary fallback={<div className="px-3 py-2 text-xs text-text-faint">{t('work.loadingDiff')}</div>}>
70
+ <UnifiedDiff
71
+ change={{ path: target.path, kind: changeKind(patch), diff: patch }}
72
+ ignoreWhitespace={hideWhitespace}
73
+ maxHeight={DIFF_HEIGHT}
74
+ />
75
+ </LazyBoundary>
76
+ </div>
77
+ )}
78
+ {partial && (
79
+ <div className="flex flex-wrap items-center gap-2 px-1 pb-0.5 text-2xs text-text-muted">
80
+ <span>{t('blocks.diff.partial')}</span>
81
+ {openable && (
82
+ <Button size="xs" variant="secondary" onClick={() => openLink(target)}>
83
+ {t('blocks.diff.showAll')}
84
+ </Button>
85
+ )}
86
+ </div>
87
+ )}
88
+ </figure>
89
+ );
90
+ }
91
+
92
+ /*
93
+ * A changed file as a chip. With `open` it shows the change itself once the host read it: the chip waits
94
+ * quietly until then, and stays text with the reason where the host would not read it. A host that reads no
95
+ * diffs, a block that still streams and a Diff past the few a block may open leave the chip as it is.
96
+ */
97
+ export function DiffRenderer({ node, children, context }: UiRendererProps<UiProps<'Diff'>>) {
98
+ const { t } = useTranslation('agent-chat');
99
+ const { path, open, whitespace } = node.props;
100
+ const target = { type: 'Diff' as const, path };
101
+ const described = node.children.length > 0;
102
+ const diff = open === true ? context.diff?.(node.id) : undefined;
103
+
104
+ if (diff === undefined) {
105
+ return (
106
+ <LinkRow target={target} context={context} described={described}>
107
+ {children}
108
+ </LinkRow>
109
+ );
110
+ }
111
+
112
+ if (diff.state === 'patch') {
113
+ return (
114
+ <OpenDiff target={target} diff={diff} hideWhitespace={whitespace === 'hide'} described={described} context={context}>
115
+ {children}
116
+ </OpenDiff>
117
+ );
118
+ }
119
+
120
+ if (diff.state === 'reading') {
121
+ return (
122
+ <DiffHead
123
+ chip={
124
+ <>
125
+ <UiLinkChip target={target} context={context} />
126
+ <Spinner size={12} label={t('work.loadingDiff')} className="shrink-0 text-text-faint" />
127
+ </>
128
+ }
129
+ described={described}
130
+ >
131
+ {children}
132
+ </DiffHead>
133
+ );
134
+ }
135
+
136
+ return (
137
+ <DiffHead
138
+ chip={<UiLinkChip target={target} context={context} refused={uiReasonText(t, diff.code, diff.reason) ?? t('blocks.diff.unavailable')} />}
139
+ described={described}
140
+ >
141
+ {children}
142
+ </DiffHead>
143
+ );
144
+ }