@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,8 +1,8 @@
1
1
  import type { UiProps } from '@adecore/intelligent-ui';
2
- import { Button, Checkbox, Input, Segmented, Select, Slider, Switch, Tooltip } from '@adecore/ui';
3
- import { formatNumber } from '@adecore/ui/format';
2
+ import { Button, Checkbox, Input, Segmented, Select, Slider, Switch, TextArea, Tooltip } from '@adecore/ui';
3
+ import { formatNumber, formatRounded } from '@adecore/ui/format';
4
4
  import clsx from 'clsx';
5
- import { createContext, useContext, useId, type MouseEvent, type ReactNode } from 'react';
5
+ import { createContext, useContext, useId, useState, type MouseEvent, type ReactNode } from 'react';
6
6
  import { useTranslation } from 'react-i18next';
7
7
  import { uiChildrenOf, uiNodeLabel, uiOwnLabel, uiWithUnit } from '../node-text';
8
8
  import type { UiRendererProps } from '../render-context';
@@ -47,7 +47,7 @@ export function ChecklistRenderer({ node, children, context }: UiRendererProps<U
47
47
  /* The whole tile is the label, ringed in the accent while checked; a link inside it keeps its own click and checks nothing. */
48
48
  export function ItemRenderer({ node, children }: UiRendererProps<UiProps<'Item'>>) {
49
49
  const list = useContext(ChecklistContext);
50
- const { value } = node.props;
50
+ const { value, detail } = node.props;
51
51
  const checked = list.values.includes(value);
52
52
 
53
53
  const toggleRow = (event: MouseEvent<HTMLDivElement>): void => {
@@ -66,7 +66,10 @@ export function ItemRenderer({ node, children }: UiRendererProps<UiProps<'Item'>
66
66
  onClick={toggleRow}
67
67
  >
68
68
  <Checkbox className="mt-0.5" checked={checked} onCheckedChange={() => list.toggle(value)} label={uiNodeLabel(node)} disabled={!list.enabled} />
69
- <div className="min-w-0 grow">{children}</div>
69
+ <div className="flex min-w-0 grow flex-col gap-0.5">
70
+ <div className="min-w-0">{children}</div>
71
+ {detail !== undefined && detail !== '' && <div className="text-2xs wrap-break-word text-text-muted">{detail}</div>}
72
+ </div>
70
73
  </div>
71
74
  );
72
75
  }
@@ -113,20 +116,23 @@ export function SliderRenderer({ node, context }: UiRendererProps<UiProps<'Slide
113
116
  /* A label longer than this reads as a sentence, which wraps badly beside another; the options then stand in a column. */
114
117
  const SIDE_BY_SIDE_LABEL = 24;
115
118
 
116
- /* Left aligned, never the full width. Each Option is a segment by its place, since a value may be any scalar. */
119
+ /* Left aligned, never the full width, under its label where it has one. Each Option is a segment by its place, since a value may be any scalar. */
117
120
  export function SegmentedRenderer({ node, context }: UiRendererProps<UiProps<'Segmented'>>) {
118
121
  const { t } = useTranslation('agent-chat');
119
122
  const binding = bindingOf(node, context);
120
123
  const current = binding?.value ?? node.props.value;
121
124
  const options = uiChildrenOf(node, 'Option').map((option, index) => ({ id: String(index), label: uiNodeLabel(option), value: option.props.value }));
122
125
  const picked = options.find((option) => option.value === current)?.id ?? '';
126
+ const { label } = node.props;
127
+ const named = label !== undefined && label !== '';
123
128
 
124
129
  return (
125
- <div className={clsx(!node.complete && PENDING)}>
130
+ <div className={clsx('flex flex-col items-start gap-1.5', !node.complete && PENDING)}>
131
+ {named && <span className="px-0.5 text-2xs text-text-muted">{label}</span>}
126
132
  <Segmented
127
133
  value={picked}
128
134
  options={options}
129
- label={t('blocks.options')}
135
+ label={named ? label : t('blocks.options')}
130
136
  orientation={options.some((option) => option.label.length > SIDE_BY_SIDE_LABEL) ? 'vertical' : 'horizontal'}
131
137
  disabled={binding === null}
132
138
  onValueChange={(id) => {
@@ -148,18 +154,90 @@ const DEFAULT_MAX_LENGTH = 256;
148
154
  const COUNT_FROM = 0.8;
149
155
 
150
156
  /*
151
- * A line of text: the label above the field, which stops taking characters at its limit. Nothing here binds
152
- * a key or submits on Enter, so the value reaches the agent only through a Choice.
157
+ * A line of text, or with `multiline` a few lines that grow with what is typed: the label above the field,
158
+ * which stops taking characters at its limit. Nothing here binds a key or submits on Enter, so the value
159
+ * reaches the agent only through a Choice.
153
160
  */
154
161
  export function TextFieldRenderer({ node, children, context }: UiRendererProps<UiProps<'TextField'>>) {
155
162
  const { t } = useTranslation('agent-chat');
156
163
  const id = useId();
157
164
  const binding = bindingOf(node, context);
158
- const { maxLength = DEFAULT_MAX_LENGTH, placeholder } = node.props;
165
+ const { maxLength = DEFAULT_MAX_LENGTH, placeholder, multiline } = node.props;
159
166
  const value = binding?.value ?? node.props.value;
160
167
  const countId = `${id}-count`;
161
168
  const counting = value.length >= maxLength * COUNT_FROM;
162
169
 
170
+ return (
171
+ <div className={clsx('flex flex-col gap-1.5', !node.complete && PENDING)}>
172
+ <label htmlFor={id} className="px-0.5 text-2xs text-text-muted">
173
+ {children}
174
+ </label>
175
+ {multiline === true ? (
176
+ <TextArea
177
+ id={id}
178
+ value={value}
179
+ size="sm"
180
+ rows={2}
181
+ maxLength={maxLength}
182
+ placeholder={placeholder}
183
+ disabled={binding === null}
184
+ aria-describedby={counting ? countId : undefined}
185
+ className="max-h-40 field-sizing-content border-transparent bg-ui-tile"
186
+ onChange={(event) => binding?.onValueChange(event.target.value)}
187
+ />
188
+ ) : (
189
+ <Input
190
+ id={id}
191
+ value={value}
192
+ size="sm"
193
+ maxLength={maxLength}
194
+ placeholder={placeholder}
195
+ disabled={binding === null}
196
+ aria-describedby={counting ? countId : undefined}
197
+ className="border-transparent bg-ui-tile"
198
+ onChange={(event) => binding?.onValueChange(event.target.value)}
199
+ />
200
+ )}
201
+ {counting && (
202
+ <span id={countId} className="text-xs text-text-muted tabular-nums">
203
+ {t('blocks.textField.count', { used: formatNumber(value.length), max: formatNumber(maxLength) })}
204
+ </span>
205
+ )}
206
+ </div>
207
+ );
208
+ }
209
+
210
+ /* What a person typed as a number, or null for text that is none or lies outside the field's range. */
211
+ function typedNumber(text: string, min: number | undefined, max: number | undefined): number | null {
212
+ const value = text.trim() === '' ? Number.NaN : Number(text);
213
+
214
+ return Number.isFinite(value) && (min === undefined || value >= min) && (max === undefined || value <= max) ? value : null;
215
+ }
216
+
217
+ /*
218
+ * A number a person types: the label above the field, the unit inside it at the end. What is typed stays in
219
+ * the field while it has the focus, and reaches the local state only as a number inside the range, so a
220
+ * half-typed or refused value never leaves the block. Leaving the field puts the last good number back.
221
+ */
222
+ export function NumberFieldRenderer({ node, children, context }: UiRendererProps<UiProps<'NumberField'>>) {
223
+ const { t } = useTranslation('agent-chat');
224
+ const id = useId();
225
+ const binding = bindingOf(node, context);
226
+ const { min, max, step, unit } = node.props;
227
+ const value = binding?.value ?? node.props.value;
228
+ const [typed, setTyped] = useState<string | null>(null);
229
+ const refused = typed !== null && typedNumber(typed, min, max) === null;
230
+ const hintId = `${id}-hint`;
231
+
232
+ const range =
233
+ min !== undefined && max !== undefined
234
+ ? t('blocks.numberField.between', { min: formatRounded(min, 6), max: formatRounded(max, 6) })
235
+ : min !== undefined
236
+ ? t('blocks.numberField.atLeast', { min: formatRounded(min, 6) })
237
+ : max !== undefined
238
+ ? t('blocks.numberField.atMost', { max: formatRounded(max, 6) })
239
+ : t('blocks.numberField.number');
240
+
163
241
  return (
164
242
  <div className={clsx('flex flex-col gap-1.5', !node.complete && PENDING)}>
165
243
  <label htmlFor={id} className="px-0.5 text-2xs text-text-muted">
@@ -167,18 +245,32 @@ export function TextFieldRenderer({ node, children, context }: UiRendererProps<U
167
245
  </label>
168
246
  <Input
169
247
  id={id}
170
- value={value}
248
+ type="number"
249
+ inputMode="decimal"
250
+ value={typed ?? String(value)}
171
251
  size="sm"
172
- maxLength={maxLength}
173
- placeholder={placeholder}
252
+ min={min}
253
+ max={max}
254
+ step={step ?? 'any'}
174
255
  disabled={binding === null}
175
- aria-describedby={counting ? countId : undefined}
256
+ aria-invalid={refused || undefined}
257
+ aria-describedby={refused ? hintId : undefined}
176
258
  className="border-transparent bg-ui-tile"
177
- onChange={(event) => binding?.onValueChange(event.target.value)}
259
+ end={unit !== undefined && unit !== '' ? <span className="pr-1.5 text-2xs text-text-muted">{unit}</span> : undefined}
260
+ onChange={(event) => {
261
+ const text = event.target.value;
262
+ const number = typedNumber(text, min, max);
263
+ setTyped(text);
264
+
265
+ if (number !== null) {
266
+ binding?.onValueChange(number);
267
+ }
268
+ }}
269
+ onBlur={() => setTyped(null)}
178
270
  />
179
- {counting && (
180
- <span id={countId} className="text-xs text-text-muted tabular-nums">
181
- {t('blocks.textField.count', { used: formatNumber(value.length), max: formatNumber(maxLength) })}
271
+ {refused && (
272
+ <span id={hintId} role="status" className="text-2xs text-status-error">
273
+ {range}
182
274
  </span>
183
275
  )}
184
276
  </div>
@@ -0,0 +1,26 @@
1
+ import type { UiProps } from '@adecore/intelligent-ui';
2
+ import { KeyCap } from '@adecore/ui';
3
+ import { useChatHost } from '../../../../host/host';
4
+ import { uiKeyCaps } from '../key-caps';
5
+ import { uiNodeLabel } from '../node-text';
6
+ import type { UiRendererProps } from '../render-context';
7
+
8
+ /* Inline, so a shortcut stands in the sentence that names it. */
9
+ export function KeysRenderer({ node }: UiRendererProps<UiProps<'Keys'>>) {
10
+ const host = useChatHost();
11
+ const caps = uiKeyCaps(uiNodeLabel(node), host.isApplePlatform());
12
+
13
+ if (caps.length === 0) {
14
+ return null;
15
+ }
16
+
17
+ return (
18
+ <span className="mx-0.5 inline-flex items-center gap-0.5 align-middle">
19
+ {caps.map((cap, index) => (
20
+ <KeyCap key={`${cap}-${index}`} size="sm">
21
+ {cap}
22
+ </KeyCap>
23
+ ))}
24
+ </span>
25
+ );
26
+ }
@@ -1,22 +1,26 @@
1
1
  import type { UiProps } from '@adecore/intelligent-ui';
2
2
  import { FileIcon, Icon, Tooltip } from '@adecore/ui';
3
3
  import clsx from 'clsx';
4
+ import type { TFunction } from 'i18next';
4
5
  import { FileDiff, GitCommitHorizontal, SquareDashed } from 'lucide-react';
5
6
  import type { ReactNode } from 'react';
6
7
  import { useTranslation } from 'react-i18next';
7
8
  import { CHIP_IN_MESSAGE } from '../../chips';
8
- import { uiReasonText } from '../node-text';
9
+ import { UI_PLAIN_LINK, uiBaseName, uiPlainLinkReason } from '../link-action';
9
10
  import type { UiLink, UiLinkTarget, UiRenderContext, UiRendererProps } from '../render-context';
10
11
 
11
- const PLAIN: UiLink = { state: 'plain' };
12
-
13
12
  /* A link in a block is a quiet card on its tile, unlike a mention in a message, which wears the accent. */
14
13
  const UI_CHIP = 'bg-ui-inset font-mono text-text shadow-[inset_0_0_0_1px_var(--border)]';
15
14
 
16
- function baseName(path: string): string {
17
- const trimmed = path.replace(/\/+$/, '');
15
+ /* The states of a node a chip shows as a dot, each in the color the rest of the app gives that state. */
16
+ const NODE_STATUS = {
17
+ running: 'bg-status-running',
18
+ needsYou: 'bg-status-needs-you',
19
+ done: 'bg-status-idle'
20
+ } as const;
18
21
 
19
- return trimmed.slice(trimmed.lastIndexOf('/') + 1) || path;
22
+ function nodeStatus(link: UiLink): keyof typeof NODE_STATUS | null {
23
+ return link.status !== undefined && Object.hasOwn(NODE_STATUS, link.status) ? (link.status as keyof typeof NODE_STATUS) : null;
20
24
  }
21
25
 
22
26
  function plainText(target: UiLinkTarget, link: UiLink): string {
@@ -32,14 +36,14 @@ function plainText(target: UiLinkTarget, link: UiLink): string {
32
36
  }
33
37
  }
34
38
 
35
- function chipFace(target: UiLinkTarget, link: UiLink): { face: ReactNode; hint: string | null } {
39
+ function chipFace(target: UiLinkTarget, link: UiLink, t: TFunction<'agent-chat'>): { face: ReactNode; hint: string | null } {
36
40
  switch (target.type) {
37
41
  case 'File':
38
42
  return {
39
43
  face: (
40
44
  <>
41
45
  <FileIcon path={target.path} size={14} />
42
- <span className="truncate">{baseName(target.path)}</span>
46
+ <span className="truncate">{uiBaseName(target.path)}</span>
43
47
  {target.line !== undefined && <span className="opacity-70">:{target.line}</span>}
44
48
  </>
45
49
  ),
@@ -50,7 +54,7 @@ function chipFace(target: UiLinkTarget, link: UiLink): { face: ReactNode; hint:
50
54
  face: (
51
55
  <>
52
56
  <Icon icon={FileDiff} size={12} />
53
- <span className="truncate">{baseName(target.path)}</span>
57
+ <span className="truncate">{uiBaseName(target.path)}</span>
54
58
  </>
55
59
  ),
56
60
  hint: target.path
@@ -65,16 +69,27 @@ function chipFace(target: UiLinkTarget, link: UiLink): { face: ReactNode; hint:
65
69
  ),
66
70
  hint: link.label ?? target.sha
67
71
  };
68
- case 'Node':
72
+ case 'Node': {
73
+ const status = nodeStatus(link);
74
+ const said = status === null ? null : t(`blocks.node.status.${status}`);
75
+
69
76
  return {
70
77
  face: (
71
78
  <>
72
79
  <Icon icon={SquareDashed} size={12} />
73
80
  <span className="truncate">{link.label ?? target.id}</span>
81
+ {status !== null && (
82
+ <>
83
+ <span aria-hidden className={clsx('size-1.5 shrink-0 rounded-full', NODE_STATUS[status])} />
84
+ <span className="sr-only">{said}</span>
85
+ </>
86
+ )}
74
87
  </>
75
88
  ),
76
- hint: null
89
+ // The dot says the state by its color alone, so the chip says it in words too.
90
+ hint: said
77
91
  };
92
+ }
78
93
  }
79
94
  }
80
95
 
@@ -83,12 +98,21 @@ function chipFace(target: UiLinkTarget, link: UiLink): { face: ReactNode; hint:
83
98
  * can open the target; one it cannot, outside the project or gone, stays mono text with the reason
84
99
  * in a tooltip. A chip fades in, since a link reads as text until the host checked it.
85
100
  */
86
- function UiLinkChip({ target, context }: { target: UiLinkTarget; context: UiRenderContext }) {
101
+ export function UiLinkChip({
102
+ target,
103
+ context,
104
+ refused
105
+ }: {
106
+ target: UiLinkTarget;
107
+ context: UiRenderContext;
108
+ /* Why the link stays text whatever the host says of its target, such as an open Diff the host would not read. */
109
+ refused?: string;
110
+ }) {
87
111
  const { t } = useTranslation('agent-chat');
88
- const link = context.link?.(target) ?? PLAIN;
112
+ const link = refused === undefined ? (context.link?.(target) ?? UI_PLAIN_LINK) : UI_PLAIN_LINK;
89
113
 
90
114
  if (link.state === 'plain') {
91
- const reason = uiReasonText(t, link.code, link.reason) ?? (context.link === undefined ? null : t('blocks.outsideProject'));
115
+ const reason = refused ?? uiPlainLinkReason(t, link, context);
92
116
 
93
117
  // Focusable only with a reason, so a keyboard reaches the tooltip.
94
118
  const text = (
@@ -100,7 +124,7 @@ function UiLinkChip({ target, context }: { target: UiLinkTarget; context: UiRend
100
124
  return reason === null ? text : <Tooltip label={reason}>{text}</Tooltip>;
101
125
  }
102
126
 
103
- const { face, hint } = chipFace(target, link);
127
+ const { face, hint } = chipFace(target, link, t);
104
128
  const { openLink } = context;
105
129
 
106
130
  const chip =
@@ -120,7 +144,7 @@ function UiLinkChip({ target, context }: { target: UiLinkTarget; context: UiRend
120
144
  }
121
145
 
122
146
  /* A link with children is a row: the chip and what the agent says about it. */
123
- function LinkRow({ target, context, described, children }: { target: UiLinkTarget; context: UiRenderContext; described: boolean; children: ReactNode }) {
147
+ export function LinkRow({ target, context, described, children }: { target: UiLinkTarget; context: UiRenderContext; described: boolean; children: ReactNode }) {
124
148
  if (!described) {
125
149
  return <UiLinkChip target={target} context={context} />;
126
150
  }
@@ -145,14 +169,6 @@ export function FileRenderer({ node, children, context }: UiRendererProps<UiProp
145
169
  );
146
170
  }
147
171
 
148
- export function DiffRenderer({ node, children, context }: UiRendererProps<UiProps<'Diff'>>) {
149
- return (
150
- <LinkRow target={{ type: 'Diff', path: node.props.path }} context={context} described={node.children.length > 0}>
151
- {children}
152
- </LinkRow>
153
- );
154
- }
155
-
156
172
  export function CommitRenderer({ node, children, context }: UiRendererProps<UiProps<'Commit'>>) {
157
173
  return (
158
174
  <LinkRow target={{ type: 'Commit', sha: node.props.sha }} context={context} described={node.children.length > 0}>
@@ -0,0 +1,105 @@
1
+ import type { UiProps } from '@adecore/intelligent-ui';
2
+ import { Icon, Meter, SegmentBar } from '@adecore/ui';
3
+ import { formatPercent, formatRounded } from '@adecore/ui/format';
4
+ import clsx from 'clsx';
5
+ import { useTranslation } from 'react-i18next';
6
+ import { uiChildrenOf, uiNodeLabel, uiWithUnit } from '../node-text';
7
+ import type { UiRendererProps } from '../render-context';
8
+ import { UI_TONE_COLOR, UI_TONE_ICONS, UI_TONE_TEXT } from '../tones';
9
+
10
+ /*
11
+ * A value against its limit, unlike Progress no word on work that runs. The tone colors the reading and
12
+ * gives it the tone's icon; the bar stays the accent. Without a max the value stands alone, as a
13
+ * percentage would.
14
+ */
15
+ export function MeterRenderer({ node, children }: UiRendererProps<UiProps<'Meter'>>) {
16
+ const { t } = useTranslation('agent-chat');
17
+ const { value, max, mark, unit, tone, detail } = node.props;
18
+ const figure = uiWithUnit(formatRounded(value, 2), unit);
19
+ const limit = max === undefined ? null : uiWithUnit(formatRounded(max, 2), unit);
20
+ const toned = tone !== undefined && tone !== 'neutral';
21
+ const label = uiNodeLabel(node);
22
+
23
+ const said = [
24
+ limit === null ? figure : t('blocks.meter.reading', { value: figure, max: limit }),
25
+ mark === undefined ? '' : t('blocks.meter.mark', { mark: uiWithUnit(formatRounded(mark, 2), unit) }),
26
+ toned ? t(`blocks.event.tone.${tone}`) : ''
27
+ ]
28
+ .filter((part) => part !== '')
29
+ .join(', ');
30
+
31
+ return (
32
+ <div className="chat-ui-flat flex flex-col gap-2 rounded-xl bg-ui-tile p-3">
33
+ <div className="flex items-baseline justify-between gap-3 text-xs">
34
+ <span className="min-w-0 text-text-muted">{children}</span>
35
+ {/* The meter under it says the same to a screen reader, with the mark and the tone in words. */}
36
+ <span aria-hidden className="flex shrink-0 items-baseline gap-1 tabular-nums">
37
+ {toned && <Icon icon={UI_TONE_ICONS[tone]} size={12} className={clsx('self-center', UI_TONE_TEXT[tone])} />}
38
+ <span className={clsx('font-semibold', toned ? UI_TONE_TEXT[tone] : 'text-text')}>{figure}</span>
39
+ {limit !== null && <span className="text-text-muted">{t('blocks.meter.of', { max: limit })}</span>}
40
+ </span>
41
+ </div>
42
+ <Meter
43
+ value={value}
44
+ max={max ?? 100}
45
+ marks={mark === undefined ? undefined : [mark]}
46
+ label={label === '' ? t('blocks.meter.label') : label}
47
+ valueText={said}
48
+ />
49
+ {detail !== undefined && detail !== '' && <p className="text-2xs wrap-anywhere text-text-muted">{detail}</p>}
50
+ </div>
51
+ );
52
+ }
53
+
54
+ /* The most parts one bar holds, which is as many colors as the chart palette has. */
55
+ const PARTS = 6;
56
+
57
+ /*
58
+ * One bar divided by the values of its parts, and under it what each part is and its share. A part
59
+ * wears the next color of the chart palette, or its tone. The bar is the list a screen reader hears,
60
+ * with every value in it; the legend repeats it for the eye.
61
+ */
62
+ export function PartsRenderer({ node }: UiRendererProps<UiProps<'Parts'>>) {
63
+ const { t } = useTranslation('agent-chat');
64
+ const { unit } = node.props;
65
+ const children = uiChildrenOf(node, 'Part').slice(0, PARTS);
66
+ const total = children.reduce((sum, part) => sum + part.props.value, 0);
67
+
68
+ const parts = children.map((part, index) => {
69
+ const { value, tone } = part.props;
70
+ const share = formatPercent(total > 0 ? (value / total) * 100 : 0);
71
+ const name = uiNodeLabel(part);
72
+
73
+ return {
74
+ id: part.id,
75
+ name,
76
+ share,
77
+ value,
78
+ color: tone === undefined ? `var(--chart-${index + 1})` : UI_TONE_COLOR[tone],
79
+ said: t('blocks.parts.part', { label: name, value: uiWithUnit(formatRounded(value, 2), unit), share })
80
+ };
81
+ });
82
+
83
+ return (
84
+ <div className="chat-ui-flat flex flex-col gap-2.5 rounded-xl bg-ui-tile p-3">
85
+ {total > 0 ? (
86
+ <SegmentBar
87
+ size="sm"
88
+ label={t('blocks.parts.label')}
89
+ parts={parts.filter((part) => part.value > 0).map((part) => ({ value: part.value, label: part.said, color: part.color }))}
90
+ />
91
+ ) : (
92
+ <div aria-hidden className="h-1.5 rounded-full bg-track" />
93
+ )}
94
+ <ul aria-hidden={total > 0 || undefined} className="flex flex-wrap gap-x-4 gap-y-1 text-2xs">
95
+ {parts.map((part) => (
96
+ <li key={part.id} className="flex min-w-0 items-center gap-1.5">
97
+ <span className="size-2 shrink-0 rounded-xs" style={{ backgroundColor: part.color }} />
98
+ <span className="min-w-0 truncate text-text-muted">{part.name}</span>
99
+ <span className="shrink-0 font-semibold text-text tabular-nums">{part.share}</span>
100
+ </li>
101
+ ))}
102
+ </ul>
103
+ </div>
104
+ );
105
+ }
@@ -0,0 +1,29 @@
1
+ import type { UiProps } from '@adecore/intelligent-ui';
2
+ import clsx from 'clsx';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { uiNodeText } from '../node-text';
5
+ import type { UiRendererProps } from '../render-context';
6
+ import { UI_TONE_FILL } from '../tones';
7
+
8
+ /*
9
+ * Words from somewhere else, read as written: a line of a log keeps its stars and underscores, and with
10
+ * `mono` its line breaks too. The bar at the start edge wears the tone, which a screen reader hears as a word.
11
+ */
12
+ export function QuoteRenderer({ node }: UiRendererProps<UiProps<'Quote'>>) {
13
+ const { t } = useTranslation('agent-chat');
14
+ const { tone, source, mono } = node.props;
15
+ const shown = tone ?? 'neutral';
16
+
17
+ return (
18
+ <figure className="flex gap-3 rounded-xl bg-ui-tile px-3 py-2.5">
19
+ <span aria-hidden className={clsx('w-0.75 shrink-0 rounded-full', UI_TONE_FILL[shown])} />
20
+ <div className="flex min-w-0 flex-col gap-1">
21
+ {shown !== 'neutral' && <span className="sr-only">{t(`blocks.event.tone.${shown}`)}</span>}
22
+ <blockquote className={clsx('wrap-anywhere text-text', mono === true ? 'font-mono text-code whitespace-pre-wrap' : 'text-xs')}>
23
+ {uiNodeText(node).trim()}
24
+ </blockquote>
25
+ {source !== undefined && source !== '' && <figcaption className="text-2xs wrap-anywhere text-text-muted">{source}</figcaption>}
26
+ </div>
27
+ </figure>
28
+ );
29
+ }
@@ -0,0 +1,130 @@
1
+ import type { UiProps } from '@adecore/intelligent-ui';
2
+ import clsx from 'clsx';
3
+ import { useId, useRef, type KeyboardEvent } from 'react';
4
+ import { useTranslation } from 'react-i18next';
5
+ import { uiChildrenOf, uiNodeLabel } from '../node-text';
6
+ import type { UiRendererProps } from '../render-context';
7
+ import { bindingOf, PENDING } from './binding';
8
+
9
+ const NEXT_KEYS = new Set(['ArrowDown', 'ArrowRight']);
10
+ const PREVIOUS_KEYS = new Set(['ArrowUp', 'ArrowLeft']);
11
+
12
+ /*
13
+ * One value out of a list whose options each take a line of explanation. Like Segmented, an Option is a
14
+ * radio by its place, since a value may be any scalar. The group is one tab stop, and an arrow key moves
15
+ * the focus and the choice together, as a radio group does.
16
+ */
17
+ export function RadioRenderer({ node, context }: UiRendererProps<UiProps<'Radio'>>) {
18
+ const { t } = useTranslation('agent-chat');
19
+ const id = useId();
20
+ const radios = useRef<(HTMLDivElement | null)[]>([]);
21
+ const binding = bindingOf(node, context);
22
+ const current = binding?.value ?? node.props.value;
23
+ const { label } = node.props;
24
+ const named = label !== undefined && label !== '';
25
+
26
+ const options = uiChildrenOf(node, 'Option').map((option) => ({
27
+ id: option.id,
28
+ label: uiNodeLabel(option),
29
+ detail: option.props.detail,
30
+ value: option.props.value
31
+ }));
32
+
33
+ const picked = options.findIndex((option) => option.value === current);
34
+ const enabled = binding !== null;
35
+
36
+ const pick = (index: number): void => {
37
+ const option = options[index];
38
+
39
+ if (option !== undefined && enabled) {
40
+ binding.onValueChange(option.value);
41
+ }
42
+ };
43
+
44
+ const onKeyDown = (event: KeyboardEvent<HTMLDivElement>, index: number): void => {
45
+ if (!enabled) {
46
+ return;
47
+ }
48
+
49
+ if (event.key === ' ' || event.key === 'Enter') {
50
+ event.preventDefault();
51
+ pick(index);
52
+
53
+ return;
54
+ }
55
+
56
+ const step = NEXT_KEYS.has(event.key) ? 1 : PREVIOUS_KEYS.has(event.key) ? -1 : 0;
57
+
58
+ if (step === 0) {
59
+ return;
60
+ }
61
+
62
+ event.preventDefault();
63
+ const next = (index + step + options.length) % options.length;
64
+ pick(next);
65
+ radios.current[next]?.focus();
66
+ };
67
+
68
+ return (
69
+ <div className={clsx('flex flex-col gap-1.5', !node.complete && PENDING)}>
70
+ {named && (
71
+ <span id={`${id}-label`} className="px-0.5 text-2xs text-text-muted">
72
+ {label}
73
+ </span>
74
+ )}
75
+ <div
76
+ role="radiogroup"
77
+ aria-labelledby={named ? `${id}-label` : undefined}
78
+ aria-label={named ? undefined : t('blocks.options')}
79
+ aria-disabled={enabled ? undefined : true}
80
+ className="flex flex-col gap-1.5"
81
+ >
82
+ {options.map((option, index) => {
83
+ const checked = index === picked;
84
+ const described = option.detail !== undefined && option.detail !== '';
85
+
86
+ return (
87
+ <div
88
+ key={option.id}
89
+ ref={(element) => {
90
+ radios.current[index] = element;
91
+ }}
92
+ role="radio"
93
+ aria-checked={checked}
94
+ aria-disabled={enabled ? undefined : true}
95
+ aria-labelledby={`${id}-${index}-label`}
96
+ aria-describedby={described ? `${id}-${index}-detail` : undefined}
97
+ // One tab stop for the group: the checked radio, or the first while nothing is checked.
98
+ tabIndex={checked || (picked === -1 && index === 0) ? 0 : -1}
99
+ className={clsx(
100
+ 'focus-ring flex min-h-10 items-start gap-3 rounded-xl bg-ui-tile px-3 py-2.5 text-xs text-text',
101
+ checked && 'ring-2 ring-accent ring-inset',
102
+ enabled && 'cursor-pointer hover:bg-surface-active'
103
+ )}
104
+ onClick={() => pick(index)}
105
+ onKeyDown={(event) => onKeyDown(event, index)}
106
+ >
107
+ <span
108
+ aria-hidden
109
+ className={clsx(
110
+ 'mt-0.5 size-4 shrink-0 rounded-full',
111
+ checked ? 'border-5 border-accent bg-accent-text' : 'border border-border-strong bg-ui-inset'
112
+ )}
113
+ />
114
+ <span className="flex min-w-0 grow flex-col gap-0.5">
115
+ <span id={`${id}-${index}-label`} className="font-medium wrap-break-word">
116
+ {option.label}
117
+ </span>
118
+ {described && (
119
+ <span id={`${id}-${index}-detail`} className="text-2xs wrap-break-word text-text-muted">
120
+ {option.detail}
121
+ </span>
122
+ )}
123
+ </span>
124
+ </div>
125
+ );
126
+ })}
127
+ </div>
128
+ </div>
129
+ );
130
+ }