@adecore/agents-react 0.70.0 → 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 (95) hide show
  1. package/dist/chat/logic/fork.js +1 -0
  2. package/dist/chat/logic/thread-cards.js +1 -0
  3. package/dist/chat/logic/timeline.js +7 -0
  4. package/dist/chat/ui/CodeBlock.d.ts +6 -1
  5. package/dist/chat/ui/CodeBlock.js +39 -23
  6. package/dist/chat/ui/intelligent-ui/UiReply.js +11 -2
  7. package/dist/chat/ui/intelligent-ui/key-caps.d.ts +1 -0
  8. package/dist/chat/ui/intelligent-ui/key-caps.js +45 -0
  9. package/dist/chat/ui/intelligent-ui/link-action.d.ts +9 -0
  10. package/dist/chat/ui/intelligent-ui/link-action.js +32 -0
  11. package/dist/chat/ui/intelligent-ui/node-text.js +1 -1
  12. package/dist/chat/ui/intelligent-ui/patch-clip.d.ts +4 -0
  13. package/dist/chat/ui/intelligent-ui/patch-clip.js +64 -0
  14. package/dist/chat/ui/intelligent-ui/registry.js +31 -5
  15. package/dist/chat/ui/intelligent-ui/render-context.d.ts +13 -0
  16. package/dist/chat/ui/intelligent-ui/renderers/alternatives.d.ts +6 -0
  17. package/dist/chat/ui/intelligent-ui/renderers/alternatives.js +296 -0
  18. package/dist/chat/ui/intelligent-ui/renderers/changes.d.ts +4 -0
  19. package/dist/chat/ui/intelligent-ui/renderers/changes.js +266 -0
  20. package/dist/chat/ui/intelligent-ui/renderers/chart.js +522 -371
  21. package/dist/chat/ui/intelligent-ui/renderers/content.d.ts +1 -1
  22. package/dist/chat/ui/intelligent-ui/renderers/content.js +94 -43
  23. package/dist/chat/ui/intelligent-ui/renderers/data.js +478 -245
  24. package/dist/chat/ui/intelligent-ui/renderers/diff.d.ts +4 -0
  25. package/dist/chat/ui/intelligent-ui/renderers/diff.js +427 -0
  26. package/dist/chat/ui/intelligent-ui/renderers/inputs.d.ts +1 -0
  27. package/dist/chat/ui/intelligent-ui/renderers/inputs.js +354 -110
  28. package/dist/chat/ui/intelligent-ui/renderers/keys.d.ts +3 -0
  29. package/dist/chat/ui/intelligent-ui/renderers/keys.js +60 -0
  30. package/dist/chat/ui/intelligent-ui/renderers/links.d.ts +13 -2
  31. package/dist/chat/ui/intelligent-ui/renderers/links.js +65 -85
  32. package/dist/chat/ui/intelligent-ui/renderers/meter.d.ts +4 -0
  33. package/dist/chat/ui/intelligent-ui/renderers/meter.js +371 -0
  34. package/dist/chat/ui/intelligent-ui/renderers/quote.d.ts +3 -0
  35. package/dist/chat/ui/intelligent-ui/renderers/quote.js +123 -0
  36. package/dist/chat/ui/intelligent-ui/renderers/radio.d.ts +3 -0
  37. package/dist/chat/ui/intelligent-ui/renderers/radio.js +205 -0
  38. package/dist/chat/ui/intelligent-ui/renderers/structure.d.ts +2 -0
  39. package/dist/chat/ui/intelligent-ui/renderers/structure.js +156 -45
  40. package/dist/chat/ui/intelligent-ui/renderers/text.js +365 -215
  41. package/dist/chat/ui/intelligent-ui/renderers/tree.d.ts +5 -0
  42. package/dist/chat/ui/intelligent-ui/renderers/tree.js +284 -0
  43. package/dist/chat/ui/intelligent-ui/table-data.d.ts +9 -2
  44. package/dist/chat/ui/intelligent-ui/table-data.js +68 -3
  45. package/dist/chat/ui/intelligent-ui/tones.d.ts +1 -1
  46. package/dist/chat/ui/intelligent-ui/tones.js +7 -7
  47. package/dist/chat/ui/intelligent-ui/use-ui-diffs.d.ts +4 -0
  48. package/dist/chat/ui/intelligent-ui/use-ui-diffs.js +201 -0
  49. package/dist/chat/ui/rows/MessageRows.d.ts +4 -1
  50. package/dist/chat/ui/rows/MessageRows.js +92 -1
  51. package/dist/chat/ui/rows/Rows.js +45 -31
  52. package/dist/host/host.d.ts +2 -1
  53. package/dist/host/timeline-row.d.ts +5 -1
  54. package/dist/locales/en/agent-chat.d.ts +62 -0
  55. package/dist/locales/en/agent-chat.js +63 -1
  56. package/dist/locales/nl/agent-chat.d.ts +62 -0
  57. package/dist/locales/nl/agent-chat.js +63 -1
  58. package/dist/theme.css +6 -0
  59. package/package.json +9 -9
  60. package/src/chat/logic/fork.ts +1 -0
  61. package/src/chat/logic/thread-cards.ts +1 -0
  62. package/src/chat/logic/timeline.ts +4 -0
  63. package/src/chat/ui/CodeBlock.tsx +38 -18
  64. package/src/chat/ui/intelligent-ui/UiReply.tsx +13 -2
  65. package/src/chat/ui/intelligent-ui/key-caps.ts +51 -0
  66. package/src/chat/ui/intelligent-ui/link-action.ts +32 -0
  67. package/src/chat/ui/intelligent-ui/node-text.ts +1 -0
  68. package/src/chat/ui/intelligent-ui/patch-clip.ts +83 -0
  69. package/src/chat/ui/intelligent-ui/registry.ts +31 -4
  70. package/src/chat/ui/intelligent-ui/render-context.ts +21 -0
  71. package/src/chat/ui/intelligent-ui/renderers/alternatives.tsx +76 -0
  72. package/src/chat/ui/intelligent-ui/renderers/changes.tsx +97 -0
  73. package/src/chat/ui/intelligent-ui/renderers/chart.tsx +122 -46
  74. package/src/chat/ui/intelligent-ui/renderers/content.tsx +27 -7
  75. package/src/chat/ui/intelligent-ui/renderers/data.tsx +165 -52
  76. package/src/chat/ui/intelligent-ui/renderers/diff.tsx +144 -0
  77. package/src/chat/ui/intelligent-ui/renderers/inputs.tsx +111 -19
  78. package/src/chat/ui/intelligent-ui/renderers/keys.tsx +26 -0
  79. package/src/chat/ui/intelligent-ui/renderers/links.tsx +40 -24
  80. package/src/chat/ui/intelligent-ui/renderers/meter.tsx +105 -0
  81. package/src/chat/ui/intelligent-ui/renderers/quote.tsx +29 -0
  82. package/src/chat/ui/intelligent-ui/renderers/radio.tsx +130 -0
  83. package/src/chat/ui/intelligent-ui/renderers/structure.tsx +54 -7
  84. package/src/chat/ui/intelligent-ui/renderers/text.tsx +70 -36
  85. package/src/chat/ui/intelligent-ui/renderers/tree.tsx +85 -0
  86. package/src/chat/ui/intelligent-ui/table-data.ts +84 -6
  87. package/src/chat/ui/intelligent-ui/tones.ts +7 -7
  88. package/src/chat/ui/intelligent-ui/use-ui-diffs.ts +119 -0
  89. package/src/chat/ui/rows/MessageRows.tsx +23 -0
  90. package/src/chat/ui/rows/Rows.tsx +3 -0
  91. package/src/host/host.ts +4 -0
  92. package/src/host/timeline-row.ts +3 -0
  93. package/src/locales/en/agent-chat.ts +63 -1
  94. package/src/locales/nl/agent-chat.ts +63 -1
  95. package/src/theme.css +6 -0
@@ -1,11 +1,12 @@
1
1
  import type { UiProps } from '@adecore/intelligent-ui';
2
- import { Icon, Tabs } from '@adecore/ui';
2
+ import { Icon, Pill, Tabs } from '@adecore/ui';
3
3
  import clsx from 'clsx';
4
4
  import { ChevronRight } from 'lucide-react';
5
- import { createContext, useContext, useId } from 'react';
5
+ import { createContext, useContext, useId, useState } from 'react';
6
6
  import { useBlockLocal } from '../block-local';
7
7
  import { uiChildrenOf } from '../node-text';
8
8
  import type { UiRendererProps } from '../render-context';
9
+ import { UI_TONE_PILL } from '../tones';
9
10
 
10
11
  interface SectionsFold {
11
12
  /* Whether the person opened or closed a section of this set, after which nothing opens by itself. */
@@ -15,7 +16,7 @@ interface SectionsFold {
15
16
 
16
17
  const SectionsContext = createContext<SectionsFold>({ touched: false, touch: () => undefined });
17
18
 
18
- /* A tab that arrives while the block streams is not picked: the one that was open stays open. */
19
+ /* A tab that arrives while the block streams is not picked: the one that was open stays open. A badge is a small count or label beside the title. */
19
20
  export function TabsRenderer({ node, children, context }: UiRendererProps<UiProps<'Tabs'>>) {
20
21
  const tabs = uiChildrenOf(node, 'Tab');
21
22
  const [picked, setPicked] = useBlockLocal<string | null>(context, node.id, () => null);
@@ -27,6 +28,11 @@ export function TabsRenderer({ node, children, context }: UiRendererProps<UiProp
27
28
  {tabs.map((tab) => (
28
29
  <Tabs.Tab key={tab.id} value={tab.id} data-ui-reveal={tab.id}>
29
30
  {tab.props.title}
31
+ {tab.props.badge !== undefined && tab.props.badge !== '' && (
32
+ <Pill size="sm" className="tabular-nums">
33
+ {tab.props.badge}
34
+ </Pill>
35
+ )}
30
36
  </Tabs.Tab>
31
37
  ))}
32
38
  </Tabs.List>
@@ -53,10 +59,17 @@ export function SectionsRenderer({ node, children, context }: UiRendererProps<Ui
53
59
  );
54
60
  }
55
61
 
56
- /* Arrives open until the person folds a section of the set themselves; the content keeps one margin on both sides. */
62
+ /*
63
+ * Arrives as `open` says. Without it a section arrives open until the person folds one of the set themselves.
64
+ * The prop is read on every draw, since a section stands before its tag has streamed in whole; only what the
65
+ * person chose is kept. The badge is a tag at the end of the header, in the section's tone.
66
+ */
57
67
  export function SectionRenderer({ node, children, context }: UiRendererProps<UiProps<'Section'>>) {
58
68
  const fold = useContext(SectionsContext);
59
- const [open, setOpen] = useBlockLocal(context, node.id, () => !fold.touched);
69
+ const { title, badge, tone } = node.props;
70
+ const [arrivedOpen] = useState(() => !fold.touched);
71
+ const [chosen, setChosen] = useBlockLocal<boolean | null>(context, node.id, () => null);
72
+ const open = chosen ?? node.props.open ?? arrivedOpen;
60
73
  const contentId = useId();
61
74
 
62
75
  return (
@@ -69,7 +82,7 @@ export function SectionRenderer({ node, children, context }: UiRendererProps<UiP
69
82
  className="flex min-h-10 w-full items-center gap-2 rounded-xl px-3 text-left text-xs font-medium text-text hover:bg-text/4"
70
83
  onClick={() => {
71
84
  fold.touch();
72
- setOpen(!open);
85
+ setChosen(!open);
73
86
  }}
74
87
  >
75
88
  <Icon
@@ -77,7 +90,12 @@ export function SectionRenderer({ node, children, context }: UiRendererProps<UiP
77
90
  size={14}
78
91
  className={clsx('shrink-0 text-text-faint transition-transform duration-150 motion-reduce:transition-none', open && 'rotate-90')}
79
92
  />
80
- <span className="min-w-0 truncate">{node.props.title}</span>
93
+ <span className="min-w-0 grow truncate">{title}</span>
94
+ {badge !== undefined && badge !== '' && (
95
+ <Pill shape="tag" size="sm" tone={tone === undefined ? 'muted' : UI_TONE_PILL[tone]}>
96
+ {badge}
97
+ </Pill>
98
+ )}
81
99
  </button>
82
100
  <div id={contentId} hidden={!open} className="chat-ui-section-body chat-ui-nodes flex flex-col gap-3 px-3 pb-3">
83
101
  {children}
@@ -85,3 +103,32 @@ export function SectionRenderer({ node, children, context }: UiRendererProps<UiP
85
103
  </section>
86
104
  );
87
105
  }
106
+
107
+ /* The one component that divides the width: its panes stand side by side and stack in a narrow block. */
108
+ export function ColumnsRenderer({ node, children }: UiRendererProps<UiProps<'Columns'>>) {
109
+ const panes = uiChildrenOf(node, 'Pane').length;
110
+
111
+ return (
112
+ <div className="@container">
113
+ <div className={clsx('chat-ui-nodes grid gap-3', panes >= 3 ? '@min-[420px]:grid-cols-3' : '@min-[420px]:grid-cols-2')}>{children}</div>
114
+ </div>
115
+ );
116
+ }
117
+
118
+ /* Stacks its children as a tab panel does, under a small title where it has one. */
119
+ export function PaneRenderer({ node, children }: UiRendererProps<UiProps<'Pane'>>) {
120
+ const { title } = node.props;
121
+ const titleId = useId();
122
+ const named = title !== undefined && title !== '';
123
+
124
+ return (
125
+ <section aria-labelledby={named ? titleId : undefined} className="chat-ui-nodes flex min-w-0 flex-col gap-2">
126
+ {named && (
127
+ <div id={titleId} className="px-0.5 text-2xs font-semibold text-text-muted">
128
+ {title}
129
+ </div>
130
+ )}
131
+ {children}
132
+ </section>
133
+ );
134
+ }
@@ -7,7 +7,7 @@ import { useContext } from 'react';
7
7
  import { useTranslation } from 'react-i18next';
8
8
  import { uiEventTime, uiNodeLabel } from '../node-text';
9
9
  import { UiHeadContext, type UiRenderContext, type UiRendererProps } from '../render-context';
10
- import { UI_STEP_PILL, UI_TONE_FILL, UI_TONE_ICONS, UI_TONE_PILL, UI_TONE_SURFACE, UI_TONE_TEXT } from '../tones';
10
+ import { UI_TONE_FILL, UI_TONE_ICONS, UI_TONE_PILL, UI_TONE_SURFACE, UI_TONE_TEXT } from '../tones';
11
11
 
12
12
  type StepState = UiProps<'Step'>['state'];
13
13
 
@@ -40,15 +40,18 @@ const ICON_SQUARE = 'grid size-8.5 shrink-0 place-items-center rounded-lg';
40
40
  const ICON_TEXT = 'flex min-h-8.5 min-w-0 grow flex-col justify-center gap-0.5';
41
41
 
42
42
  /*
43
- * A tile in its tone; the badge stands under the title in the tone's color. The head of the block
44
- * carries its icon in a square of its own, a Summary further down is a subheading without one. The
45
- * icon is the ground's ink in the dark theme, where its tones are too light for white.
43
+ * A tile in its tone. A short badge is a tag at the end of the row; a longer one stands under the
44
+ * title, since a tag cannot wrap. The head of the block carries its icon in a square of its own, a
45
+ * Summary further down is a subheading without one. The icon is the ground's ink in the dark theme,
46
+ * where its tones are too light for white.
46
47
  */
47
48
  export function SummaryRenderer({ node, children }: UiRendererProps<UiProps<'Summary'>>) {
48
49
  const headId = useContext(UiHeadContext);
49
50
  const head = headId === undefined || headId === node.id;
50
- const { tone, badge } = node.props;
51
+ const { tone, badge, detail } = node.props;
51
52
  const shown = tone ?? 'neutral';
53
+ const badged = badge !== undefined && badge !== '';
54
+ const tagged = badged && badge.length <= DETAIL_AS_TAG;
52
55
 
53
56
  return (
54
57
  <div className={clsx(ICON_ROW, tone === undefined ? 'bg-ui-tile' : UI_TONE_SURFACE[tone])}>
@@ -59,26 +62,40 @@ export function SummaryRenderer({ node, children }: UiRendererProps<UiProps<'Sum
59
62
  )}
60
63
  <div className={ICON_TEXT}>
61
64
  <div className="text-base font-semibold text-balance wrap-break-word text-text">{children}</div>
62
- {badge !== undefined && badge !== '' && (
65
+ {/* Muted text does not reach 4.5 to 1 on the surface of a tone, so the detail is muted only on the gray tile. */}
66
+ {detail !== undefined && detail !== '' && (
67
+ <p className={clsx('text-xs wrap-break-word', tone === undefined ? 'text-text-muted' : 'text-text')}>{detail}</p>
68
+ )}
69
+ {badged && !tagged && (
63
70
  <p className={clsx('text-2xs font-medium wrap-anywhere', tone === undefined ? 'text-text-muted' : UI_TONE_TEXT[tone])}>{badge}</p>
64
71
  )}
65
72
  </div>
73
+ {tagged && (
74
+ <Pill shape="tag" size="sm" tone={UI_TONE_PILL[shown]} className="mt-0.5 shrink-0">
75
+ {badge}
76
+ </Pill>
77
+ )}
66
78
  </div>
67
79
  );
68
80
  }
69
81
 
70
- /* A quiet tile whatever its tone: only the square of its icon wears the tone. */
82
+ /*
83
+ * A quiet tile whose icon square wears the tone. A warning and a danger take the surface of their tone
84
+ * for the whole tile, and keep it inside a Section, where a quiet tile goes flat. Their words are the full
85
+ * text color: muted text does not reach 4.5 to 1 on a tinted surface.
86
+ */
71
87
  export function CalloutRenderer({ node, children }: UiRendererProps<UiProps<'Callout'>>) {
72
88
  const { tone, title } = node.props;
89
+ const tinted = tone === 'warning' || tone === 'danger';
73
90
 
74
91
  return (
75
- <div className={clsx('chat-ui-flat bg-ui-tile text-xs', ICON_ROW)}>
92
+ <div className={clsx('text-xs', ICON_ROW, tinted ? UI_TONE_SURFACE[tone] : 'chat-ui-flat bg-ui-tile')}>
76
93
  <span aria-hidden className={clsx(ICON_SQUARE, UI_TONE_SURFACE[tone], UI_TONE_TEXT[tone])}>
77
94
  <Icon icon={UI_TONE_ICONS[tone]} size={16} />
78
95
  </span>
79
96
  <div className={ICON_TEXT}>
80
97
  {title !== undefined && title !== '' && <div className="font-semibold text-text">{title}</div>}
81
- <div className="text-text-muted">{children}</div>
98
+ <div className={tinted ? 'text-text' : 'text-text-muted'}>{children}</div>
82
99
  </div>
83
100
  </div>
84
101
  );
@@ -94,63 +111,80 @@ export function TagRenderer({ node, children }: UiRendererProps<UiProps<'Tag'>>)
94
111
  );
95
112
  }
96
113
 
97
- /* Without a value the progress is indeterminate; without a max the value is a percentage. */
114
+ /*
115
+ * Without a value the progress is indeterminate; without a max the value is a percentage. The tone colors
116
+ * the reading and gives it the tone's icon; the bar stays the accent.
117
+ */
98
118
  export function ProgressRenderer({ node, children, context }: UiRendererProps<UiProps<'Progress'>>) {
99
119
  const { t } = useTranslation('agent-chat');
100
- const { value, max } = node.props;
120
+ const { value, max, tone, detail } = node.props;
101
121
  const limit = max ?? 100;
122
+ const toned = tone !== undefined && tone !== 'neutral';
102
123
 
103
124
  const reading =
104
125
  value === undefined ? null : limit === 100 ? formatPercent(value) : t('blocks.progress.of', { value: formatNumber(value), max: formatNumber(limit) });
105
126
 
127
+ const said = [reading ?? '', toned ? t(`blocks.event.tone.${tone}`) : ''].filter((part) => part !== '').join(', ');
106
128
  const label = uiNodeLabel(node);
107
129
 
108
130
  return (
109
131
  <div className="chat-ui-flat flex flex-col gap-2 rounded-xl bg-ui-tile p-3">
110
132
  <div className="flex items-baseline justify-between gap-3 text-xs">
111
133
  <span className="min-w-0 text-text">{children}</span>
112
- {reading !== null ? (
113
- <span className="shrink-0 text-text-muted tabular-nums">{reading}</span>
114
- ) : (
115
- isLive(context) && <Spinner size={12} className="shrink-0 self-center text-text-faint" />
116
- )}
134
+ {/* The meter under it says the same to a screen reader, with the tone in words. */}
135
+ <span aria-hidden className="flex shrink-0 items-baseline gap-1 tabular-nums">
136
+ {toned && <Icon icon={UI_TONE_ICONS[tone]} size={12} className={clsx('self-center', UI_TONE_TEXT[tone])} />}
137
+ {reading !== null ? (
138
+ <span className={toned ? UI_TONE_TEXT[tone] : 'text-text-muted'}>{reading}</span>
139
+ ) : (
140
+ isLive(context) && <Spinner size={12} className="self-center text-text-faint" />
141
+ )}
142
+ </span>
117
143
  </div>
118
- <Meter value={value ?? null} max={limit} label={label === '' ? t('blocks.progress.label') : label} valueText={reading ?? undefined} />
144
+ <Meter value={value ?? null} max={limit} label={label === '' ? t('blocks.progress.label') : label} valueText={said === '' ? undefined : said} />
145
+ {detail !== undefined && detail !== '' && <p className="text-2xs wrap-anywhere text-text-muted">{detail}</p>}
119
146
  </div>
120
147
  );
121
148
  }
122
149
 
123
150
  export function StepsRenderer({ children }: UiRendererProps<UiProps<'Steps'>>) {
124
- return <ol className="chat-ui-nodes flex flex-col gap-1.5">{children}</ol>;
151
+ return <ol className="chat-ui-nodes chat-ui-flat flex flex-col rounded-xl bg-ui-tile p-3">{children}</ol>;
125
152
  }
126
153
 
127
- /* A tile per step with its state in words at the end. A running step spins only while the block is live; in a quiet block it is a still dot. */
154
+ /*
155
+ * A row of the sequence's tile: the state as an icon on a rail that runs to the next step, the label, and
156
+ * the detail at the end, or under the label once it does not fit beside it. Only a failed step says its
157
+ * state in sight; every other one says it to a screen reader. A running step spins only while the block
158
+ * is live; in a quiet block it is a still dot.
159
+ */
128
160
  export function StepRenderer({ node, children, context }: UiRendererProps<UiProps<'Step'>>) {
129
161
  const { t } = useTranslation('agent-chat');
130
162
  const { state, detail } = node.props;
131
163
  const quiet = state === 'pending' || state === 'skipped';
132
164
 
133
165
  return (
134
- <li className="flex min-h-10 items-start gap-3 rounded-xl bg-ui-tile px-3 py-2.5 text-xs">
135
- <span className={clsx('mt-0.5 grid size-4 shrink-0 place-items-center', STEP_TONES[state])}>
136
- {state === 'running' ? (
137
- isLive(context) ? (
138
- <Spinner size={14} />
166
+ <li className="group flex items-stretch gap-3 text-xs">
167
+ <span className="flex w-4 shrink-0 flex-col items-center">
168
+ <span className={clsx('mt-0.5 grid size-4 shrink-0 place-items-center', STEP_TONES[state])}>
169
+ {state === 'running' ? (
170
+ isLive(context) ? (
171
+ <Spinner size={14} />
172
+ ) : (
173
+ <Icon icon={CircleDot} size={14} />
174
+ )
139
175
  ) : (
140
- <Icon icon={CircleDot} size={14} />
141
- )
142
- ) : (
143
- <Icon icon={STEP_ICONS[state]} size={14} />
144
- )}
176
+ <Icon icon={STEP_ICONS[state]} size={14} />
177
+ )}
178
+ </span>
179
+ <span aria-hidden className="my-1 w-px grow bg-border group-last:hidden" />
145
180
  </span>
146
- {/* A short detail stays beside the title; one that does not fit beside it takes the line under it. */}
147
- <span className="flex min-w-0 grow flex-wrap items-baseline gap-x-2">
148
- <span className={clsx('max-w-full min-w-0 font-medium wrap-break-word', quiet ? 'text-text-muted' : 'text-text')}>{children}</span>
149
- {detail !== undefined && detail !== '' && <span className="max-w-full text-2xs wrap-anywhere text-text-muted tabular-nums">{detail}</span>}
181
+ <span className="flex min-w-0 grow flex-col gap-0.5 pb-3 group-last:pb-0">
182
+ <span className="flex min-w-0 flex-wrap items-baseline justify-between gap-x-3">
183
+ <span className={clsx('max-w-full min-w-0 font-medium wrap-break-word', quiet ? 'text-text-muted' : 'text-text')}>{children}</span>
184
+ {detail !== undefined && detail !== '' && <span className="max-w-full text-2xs wrap-anywhere text-text-muted tabular-nums">{detail}</span>}
185
+ </span>
186
+ <span className={state === 'failed' ? 'text-2xs text-status-error' : 'sr-only'}>{t(`blocks.step.${state}`)}</span>
150
187
  </span>
151
- <Pill shape="tag" size="sm" tone={UI_STEP_PILL[state]} className="shrink-0">
152
- {t(`blocks.step.${state}`)}
153
- </Pill>
154
188
  </li>
155
189
  );
156
190
  }
@@ -0,0 +1,85 @@
1
+ import type { UiProps } from '@adecore/intelligent-ui';
2
+ import { FileIcon, Icon } from '@adecore/ui';
3
+ import clsx from 'clsx';
4
+ import { Folder, FolderOpen } from 'lucide-react';
5
+ import { useId } from 'react';
6
+ import { useTranslation } from 'react-i18next';
7
+ import { useBlockLocal } from '../block-local';
8
+ import { uiBaseName, uiLinkAction } from '../link-action';
9
+ import { uiNodeLabel } from '../node-text';
10
+ import type { UiRendererProps } from '../render-context';
11
+ import { UI_TONE_TEXT } from '../tones';
12
+
13
+ const ROW = 'flex min-h-7 min-w-0 items-center gap-2 font-mono text-code';
14
+
15
+ /* Nested lists, so a screen reader hears the depth and the count without a tree's own keyboard model. */
16
+ export function TreeRenderer({ children }: UiRendererProps<UiProps<'Tree'>>) {
17
+ const { t } = useTranslation('agent-chat');
18
+
19
+ return (
20
+ <ul aria-label={t('blocks.tree.label')} className="chat-ui-nodes chat-ui-flat flex flex-col rounded-xl bg-ui-tile px-3 py-2">
21
+ {children}
22
+ </ul>
23
+ );
24
+ }
25
+
26
+ /* Open unless `open` says closed, read on every draw since the tag may still be streaming in; what a person folds stays folded as long as the page lasts. */
27
+ export function BranchRenderer({ node, children, context }: UiRendererProps<UiProps<'Branch'>>) {
28
+ const [chosen, setChosen] = useBlockLocal<boolean | null>(context, node.id, () => null);
29
+ const open = chosen ?? node.props.open !== false;
30
+ const contentId = useId();
31
+
32
+ return (
33
+ <li className="flex min-w-0 flex-col">
34
+ <button
35
+ type="button"
36
+ aria-expanded={open}
37
+ aria-controls={contentId}
38
+ className={clsx(ROW, '-mx-1.5 rounded-md px-1.5 text-left text-text hover:bg-text/4')}
39
+ onClick={() => setChosen(!open)}
40
+ >
41
+ <Icon icon={open ? FolderOpen : Folder} size={14} className="shrink-0 text-text-muted" />
42
+ <span className="min-w-0 truncate">{node.props.label}</span>
43
+ </button>
44
+ <ul id={contentId} hidden={!open} className="chat-ui-nodes flex flex-col pl-4">
45
+ {children}
46
+ </ul>
47
+ </li>
48
+ );
49
+ }
50
+
51
+ /*
52
+ * Named by its children, or by its file without them. With a path the host can open, the name opens it
53
+ * as a File link does; one it cannot stays the same text, since a tree shows a structure before it links.
54
+ */
55
+ export function LeafRenderer({ node, children, context }: UiRendererProps<UiProps<'Leaf'>>) {
56
+ const { t } = useTranslation('agent-chat');
57
+ const { path, detail, tone } = node.props;
58
+ const written = uiNodeLabel(node) !== '';
59
+ const name = written ? children : path === undefined ? null : uiBaseName(path);
60
+ const open = path === undefined ? null : uiLinkAction(t, { type: 'File', path }, context).open;
61
+ const look = clsx('min-w-0 truncate', tone === undefined ? 'text-text' : UI_TONE_TEXT[tone]);
62
+
63
+ return (
64
+ <li className={ROW}>
65
+ {path === undefined ? (
66
+ <span aria-hidden className="grid size-3.5 shrink-0 place-items-center">
67
+ <span className="size-1 rounded-full bg-text-faint" />
68
+ </span>
69
+ ) : (
70
+ <FileIcon path={path} size={14} className="shrink-0" />
71
+ )}
72
+ {open === null ? (
73
+ <span className={look}>{name}</span>
74
+ ) : (
75
+ <button type="button" className={clsx(look, 'rounded-sm text-left hover:underline')} onClick={open}>
76
+ {name}
77
+ </button>
78
+ )}
79
+ {tone !== undefined && tone !== 'neutral' && <span className="sr-only">{t(`blocks.event.tone.${tone}`)}</span>}
80
+ {detail !== undefined && detail !== '' && (
81
+ <span className="ml-auto max-w-1/2 shrink-0 truncate pl-2 font-sans text-2xs text-text-muted">{detail}</span>
82
+ )}
83
+ </li>
84
+ );
85
+ }
@@ -1,4 +1,5 @@
1
- import type { UiProps, UiViewNode } from '@adecore/intelligent-ui';
1
+ import type { UiProps, UiTone, UiViewNode } from '@adecore/intelligent-ui';
2
+ import { labelCollator } from '@adecore/ui/format';
2
3
  import { uiChildrenOf } from './node-text';
3
4
 
4
5
  type UiColumnKind = NonNullable<UiProps<'Column'>['as']>;
@@ -8,13 +9,16 @@ export interface UiTableColumn {
8
9
  title: string;
9
10
  unit?: string;
10
11
  as: UiColumnKind;
12
+
13
+ /* The tone of a tag cell, by the text it shows. */
14
+ tones?: Readonly<Record<string, UiTone>>;
11
15
  }
12
16
 
13
17
  /* What one cell shows, decided before drawing so a value of the wrong kind never reaches a formatter. */
14
18
  export type UiTableCell =
15
19
  | { kind: 'empty' }
16
20
  | { kind: 'text'; text: string }
17
- | { kind: 'number'; as: 'number' | 'bytes' | 'duration'; value: number }
21
+ | { kind: 'number'; as: 'number' | 'bytes' | 'duration' | 'percent'; value: number }
18
22
  | { kind: 'date'; at: number }
19
23
  | { kind: 'file'; path: string }
20
24
  | { kind: 'tag'; text: string };
@@ -24,7 +28,7 @@ export const UI_TABLE_ROWS = 50;
24
28
 
25
29
  /* The furthest a JavaScript Date reaches from the epoch either way, in milliseconds. */
26
30
  const DATE_RANGE = 8.64e15;
27
- const NUMERIC_KINDS: ReadonlySet<UiColumnKind> = new Set(['number', 'bytes', 'duration']);
31
+ const NUMERIC_KINDS: ReadonlySet<UiColumnKind> = new Set(['number', 'bytes', 'duration', 'percent']);
28
32
 
29
33
  export function isNumericColumn(column: UiTableColumn): boolean {
30
34
  return NUMERIC_KINDS.has(column.as);
@@ -52,7 +56,7 @@ export function uiTableColumns(node: UiViewNode<UiProps<'Table'>>): UiTableColum
52
56
 
53
57
  seen.add(props.key);
54
58
 
55
- return [{ key: props.key, title: props.title ?? props.key, unit: props.unit, as: props.as ?? 'text' }];
59
+ return [{ key: props.key, title: props.title ?? props.key, unit: props.unit, as: props.as ?? 'text', tones: props.tones }];
56
60
  });
57
61
  }
58
62
 
@@ -92,7 +96,10 @@ function numberOf(value: unknown): number | null {
92
96
  return null;
93
97
  }
94
98
 
95
- /* A duration is in milliseconds and a date is milliseconds since the epoch or an ISO string; anything else reads as text. */
99
+ /*
100
+ * A duration is in milliseconds, a percent is the number before the sign (`12.5` reads 12.5%) and a date is
101
+ * milliseconds since the epoch or an ISO string; anything else reads as text.
102
+ */
96
103
  export function uiTableCell(value: unknown, column: UiTableColumn): UiTableCell {
97
104
  if (value === undefined || value === null || value === '') {
98
105
  return { kind: 'empty' };
@@ -101,7 +108,8 @@ export function uiTableCell(value: unknown, column: UiTableColumn): UiTableCell
101
108
  switch (column.as) {
102
109
  case 'number':
103
110
  case 'bytes':
104
- case 'duration': {
111
+ case 'duration':
112
+ case 'percent': {
105
113
  const number = numberOf(value);
106
114
 
107
115
  return number === null ? { kind: 'text', text: textOf(value) } : { kind: 'number', as: column.as, value: number };
@@ -119,3 +127,73 @@ export function uiTableCell(value: unknown, column: UiTableColumn): UiTableCell
119
127
  return { kind: 'text', text: textOf(value) };
120
128
  }
121
129
  }
130
+
131
+ /* The order of a table: one of its columns, the largest first when `descending`. */
132
+ export interface UiTableSort {
133
+ key: string;
134
+ descending: boolean;
135
+ }
136
+
137
+ /* What `sort` on a Table asks for: a column key, with ` desc` behind it for the largest first. Null for a key the table draws no column of. */
138
+ export function uiTableSort(sort: string | undefined, columns: readonly UiTableColumn[]): UiTableSort | null {
139
+ if (sort === undefined) {
140
+ return null;
141
+ }
142
+
143
+ const written = sort.trim();
144
+ const drawn = (key: string): boolean => columns.some((column) => column.key === key);
145
+
146
+ // A key that itself ends in a direction is that key.
147
+ if (drawn(written)) {
148
+ return { key: written, descending: false };
149
+ }
150
+
151
+ const [, key, direction] = /^(.*?)\s+(asc|desc)$/i.exec(written) ?? [];
152
+
153
+ return key !== undefined && drawn(key) ? { key, descending: direction!.toLowerCase() === 'desc' } : null;
154
+ }
155
+
156
+ function sortValue(cell: UiTableCell): number | string | null {
157
+ switch (cell.kind) {
158
+ case 'empty':
159
+ return null;
160
+ case 'number':
161
+ return cell.value;
162
+ case 'date':
163
+ return cell.at;
164
+ case 'file':
165
+ return cell.path;
166
+ case 'text':
167
+ case 'tag':
168
+ return cell.text;
169
+ }
170
+ }
171
+
172
+ /*
173
+ * The places of `rows` in the order of one column, so a row keeps the index live readings address it by.
174
+ * Numbers, bytes, durations, percentages and dates order by value and text by the reader's language. In
175
+ * either direction a number stands before text and an empty cell last, and equal rows keep their order.
176
+ */
177
+ export function uiTableOrder(rows: UiProps<'Table'>['rows'], column: UiTableColumn, descending: boolean): number[] {
178
+ const collator = labelCollator();
179
+ const direction = descending ? -1 : 1;
180
+
181
+ return rows
182
+ .map((row, index) => ({ index, value: sortValue(uiTableCell(row[column.key], column)) }))
183
+ .sort((left, right) => {
184
+ if (left.value === null || right.value === null) {
185
+ return left.value === right.value ? left.index - right.index : left.value === null ? 1 : -1;
186
+ }
187
+
188
+ if (typeof left.value === 'string' || typeof right.value === 'string') {
189
+ if (typeof left.value !== typeof right.value) {
190
+ return typeof left.value === 'number' ? -1 : 1;
191
+ }
192
+
193
+ return direction * collator.compare(left.value as string, right.value as string) || left.index - right.index;
194
+ }
195
+
196
+ return direction * (left.value - right.value) || left.index - right.index;
197
+ })
198
+ .map((entry) => entry.index);
199
+ }
@@ -45,11 +45,11 @@ export const UI_TONE_FILL: Readonly<Record<UiTone, string>> = {
45
45
  danger: 'bg-status-error'
46
46
  };
47
47
 
48
- /* The tag at the end of a step, in words, so a state never rests on its icon alone. */
49
- export const UI_STEP_PILL: Readonly<Record<'done' | 'running' | 'pending' | 'failed' | 'skipped', NonNullable<PillProps['tone']>>> = {
50
- done: 'idle',
51
- running: 'accent',
52
- pending: 'muted',
53
- failed: 'error',
54
- skipped: 'muted'
48
+ /* The tone as a CSS color, for a shape that takes its color through `style`, such as a part of a bar. */
49
+ export const UI_TONE_COLOR: Readonly<Record<UiTone, string>> = {
50
+ neutral: 'var(--text-muted)',
51
+ info: 'var(--status-running)',
52
+ success: 'var(--positive)',
53
+ warning: 'var(--status-needs-you)',
54
+ danger: 'var(--status-error)'
55
55
  };
@@ -0,0 +1,119 @@
1
+ import type { ChatUiDiffPayload, ChatUiDiffReading } from '@adecore/agent-contracts';
2
+ import { uiInputValues, type UiBlock, type UiProps, type UiState, type UiViewNode } from '@adecore/intelligent-ui';
3
+ import { useEffect, useRef, useState } from 'react';
4
+ import { useChatHost } from '../../../host/host';
5
+ import type { UiDiff, UiRenderContext } from './render-context';
6
+
7
+ /* The open Diffs one block asks its host for; a Diff past these stays a chip, so no block reads a whole repository. */
8
+ export const UI_OPEN_DIFFS = 8;
9
+
10
+ /* What a node asks to see. Only its id goes to the host, which reads the rest from the stored block; here it tells one request from the next. */
11
+ type DiffRequest = Pick<ChatUiDiffPayload, 'nodeId'> & Pick<UiProps<'Diff'>, 'path' | 'from' | 'to' | 'whitespace'>;
12
+
13
+ interface DiffState {
14
+ /* The request the reading answers, so one for a path the node has since left is not shown. */
15
+ request: string;
16
+ diff: UiDiff;
17
+ }
18
+
19
+ function openDiffs(nodes: readonly UiViewNode[], into: DiffRequest[] = []): DiffRequest[] {
20
+ for (const node of nodes) {
21
+ if (node.type === 'Diff' && node.error === undefined && node.complete) {
22
+ const { path, open, from, to, whitespace } = node.props as UiProps<'Diff'>;
23
+
24
+ if (open === true) {
25
+ into.push({ nodeId: node.id, path, from, to, whitespace });
26
+ }
27
+ }
28
+
29
+ openDiffs(node.children, into);
30
+ }
31
+
32
+ return into;
33
+ }
34
+
35
+ function diffOf(reading: ChatUiDiffReading): UiDiff {
36
+ return reading.state === 'patch' && reading.patch !== undefined
37
+ ? { state: 'patch', patch: reading.patch, truncated: reading.truncated === true }
38
+ : { state: 'refused', code: reading.code, reason: reading.reason };
39
+ }
40
+
41
+ /*
42
+ * Asks the host for the change of every open Diff of a finished block, once per request. The agent
43
+ * writes only the path, so what is shown is what git holds and never what a model copied.
44
+ */
45
+ export function useUiDiffs(
46
+ block: UiBlock,
47
+ state: UiState,
48
+ context: Pick<UiRenderContext, 'scopeId' | 'chatId' | 'itemId' | 'phase'>,
49
+ reads: Readonly<Record<string, string>>,
50
+ nodes: readonly UiViewNode[]
51
+ ): UiRenderContext['diff'] {
52
+ const [answered, setAnswered] = useState<Record<string, DiffState>>({});
53
+ // What each node was last asked as, so a redraw asks nothing twice.
54
+ const asked = useRef(new Map<string, string>());
55
+ const read = useChatHost().intelligentUi?.diff;
56
+ const wanted = block.complete ? openDiffs(nodes).slice(0, UI_OPEN_DIFFS) : [];
57
+ const signature = JSON.stringify(wanted);
58
+ const input = JSON.stringify(uiInputValues(block, state));
59
+ const tickets = JSON.stringify(reads);
60
+ const ready = read !== undefined && block.revision !== undefined && context.phase === 'final';
61
+
62
+ useEffect(() => {
63
+ const requests = asked.current;
64
+
65
+ return () => requests.clear();
66
+ }, [block.id, block.revision, context.scopeId, context.chatId, context.itemId, read]);
67
+
68
+ useEffect(() => {
69
+ if (!read || !block.revision || context.phase !== 'final') {
70
+ return;
71
+ }
72
+
73
+ for (const request of JSON.parse(signature) as DiffRequest[]) {
74
+ const key = JSON.stringify(request);
75
+
76
+ if (asked.current.get(request.nodeId) === key) {
77
+ continue;
78
+ }
79
+
80
+ asked.current.set(request.nodeId, key);
81
+
82
+ const record = (diff: UiDiff): void => {
83
+ // An answer to a request the node has left, or of a block that is gone, is dropped.
84
+ if (asked.current.get(request.nodeId) === key) {
85
+ setAnswered((previous) => ({ ...previous, [request.nodeId]: { request: key, diff } }));
86
+ }
87
+ };
88
+
89
+ read(context.scopeId, {
90
+ chatId: context.chatId,
91
+ itemId: context.itemId,
92
+ blockId: block.id,
93
+ revision: block.revision,
94
+ nodeId: request.nodeId,
95
+ values: JSON.parse(input),
96
+ reads: JSON.parse(tickets)
97
+ }).then(
98
+ (reading) => record(diffOf(reading)),
99
+ () => record({ state: 'refused' })
100
+ );
101
+ }
102
+ }, [block.id, block.revision, context.scopeId, context.chatId, context.itemId, context.phase, signature, input, tickets, read]);
103
+
104
+ if (!read) {
105
+ return undefined;
106
+ }
107
+
108
+ return (nodeId) => {
109
+ const request = wanted.find((entry) => entry.nodeId === nodeId);
110
+
111
+ if (request === undefined || !ready) {
112
+ return undefined;
113
+ }
114
+
115
+ const reading = answered[nodeId];
116
+
117
+ return reading?.request === JSON.stringify(request) ? reading.diff : { state: 'reading' };
118
+ };
119
+ }