@adecore/agents-react 0.20.1 → 0.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/chat/logic/generated-image.d.ts +27 -0
  2. package/dist/chat/logic/generated-image.js +32 -0
  3. package/dist/chat/logic/timeline-target.d.ts +13 -1
  4. package/dist/chat/logic/timeline-target.js +26 -0
  5. package/dist/chat/logic/timeline.js +16 -10
  6. package/dist/chat/subagent-conversation.d.ts +2 -1
  7. package/dist/chat/subagent-conversation.js +7 -1
  8. package/dist/chat/timeline-flash.d.ts +3 -1
  9. package/dist/chat/timeline-flash.js +2 -2
  10. package/dist/chat/ui/ImageView.d.ts +8 -2
  11. package/dist/chat/ui/ImageView.js +12 -6
  12. package/dist/chat/ui/SubagentTimeline.js +6 -2
  13. package/dist/chat/ui/Timeline.js +67 -7
  14. package/dist/chat/ui/intelligent-ui/UiBlockFrame.d.ts +23 -0
  15. package/dist/chat/ui/intelligent-ui/UiBlockFrame.js +103 -0
  16. package/dist/chat/ui/intelligent-ui/UiFallbackPart.d.ts +10 -0
  17. package/dist/chat/ui/intelligent-ui/UiFallbackPart.js +11 -0
  18. package/dist/chat/ui/intelligent-ui/UiReply.d.ts +15 -0
  19. package/dist/chat/ui/intelligent-ui/UiReply.js +329 -0
  20. package/dist/chat/ui/intelligent-ui/block-local.d.ts +11 -0
  21. package/dist/chat/ui/intelligent-ui/block-local.js +42 -0
  22. package/dist/chat/ui/intelligent-ui/chart-data.d.ts +16 -0
  23. package/dist/chat/ui/intelligent-ui/chart-data.js +46 -0
  24. package/dist/chat/ui/intelligent-ui/node-text.d.ts +15 -0
  25. package/dist/chat/ui/intelligent-ui/node-text.js +106 -0
  26. package/dist/chat/ui/intelligent-ui/registry.d.ts +2 -0
  27. package/dist/chat/ui/intelligent-ui/registry.js +51 -0
  28. package/dist/chat/ui/intelligent-ui/render-context.d.ts +53 -0
  29. package/dist/chat/ui/intelligent-ui/render-context.js +3 -0
  30. package/dist/chat/ui/intelligent-ui/renderers/chart.d.ts +3 -0
  31. package/dist/chat/ui/intelligent-ui/renderers/chart.js +123 -0
  32. package/dist/chat/ui/intelligent-ui/renderers/choices.d.ts +4 -0
  33. package/dist/chat/ui/intelligent-ui/renderers/choices.js +45 -0
  34. package/dist/chat/ui/intelligent-ui/renderers/content.d.ts +14 -0
  35. package/dist/chat/ui/intelligent-ui/renderers/content.js +59 -0
  36. package/dist/chat/ui/intelligent-ui/renderers/data.d.ts +7 -0
  37. package/dist/chat/ui/intelligent-ui/renderers/data.js +80 -0
  38. package/dist/chat/ui/intelligent-ui/renderers/inputs.d.ts +8 -0
  39. package/dist/chat/ui/intelligent-ui/renderers/inputs.js +72 -0
  40. package/dist/chat/ui/intelligent-ui/renderers/links.d.ts +10 -0
  41. package/dist/chat/ui/intelligent-ui/renderers/links.js +87 -0
  42. package/dist/chat/ui/intelligent-ui/renderers/structure.d.ts +6 -0
  43. package/dist/chat/ui/intelligent-ui/renderers/structure.js +32 -0
  44. package/dist/chat/ui/intelligent-ui/renderers/text.d.ts +8 -0
  45. package/dist/chat/ui/intelligent-ui/renderers/text.js +61 -0
  46. package/dist/chat/ui/intelligent-ui/reply-parts.d.ts +9 -0
  47. package/dist/chat/ui/intelligent-ui/reply-parts.js +18 -0
  48. package/dist/chat/ui/intelligent-ui/table-data.d.ts +31 -0
  49. package/dist/chat/ui/intelligent-ui/table-data.js +79 -0
  50. package/dist/chat/ui/intelligent-ui/tones.d.ts +7 -0
  51. package/dist/chat/ui/intelligent-ui/tones.js +31 -0
  52. package/dist/chat/ui/intelligent-ui/use-later.d.ts +1 -0
  53. package/dist/chat/ui/intelligent-ui/use-later.js +16 -0
  54. package/dist/chat/ui/intelligent-ui/use-ui-links.d.ts +9 -0
  55. package/dist/chat/ui/intelligent-ui/use-ui-links.js +126 -0
  56. package/dist/chat/ui/intelligent-ui/use-ui-queries.d.ts +21 -0
  57. package/dist/chat/ui/intelligent-ui/use-ui-queries.js +272 -0
  58. package/dist/chat/ui/reply-context.d.ts +8 -0
  59. package/dist/chat/ui/reply-context.js +1 -0
  60. package/dist/chat/ui/rows/GeneratedImageRow.d.ts +5 -0
  61. package/dist/chat/ui/rows/GeneratedImageRow.js +121 -0
  62. package/dist/chat/ui/rows/MessageRows.js +21 -4
  63. package/dist/chat/ui/rows/Rows.js +4 -2
  64. package/dist/chat/ui/rows/SubagentRow.js +5 -0
  65. package/dist/chat/ui/rows/WorkRows.d.ts +4 -1
  66. package/dist/chat/ui/rows/WorkRows.js +4 -3
  67. package/dist/host.d.ts +11 -1
  68. package/dist/locales/en/agent-chat.json +101 -0
  69. package/dist/locales/nl/agent-chat.json +101 -0
  70. package/dist/state/chats.js +13 -0
  71. package/dist/theme.css +19 -0
  72. package/package.json +128 -7
  73. package/src/chat/logic/generated-image.ts +48 -0
  74. package/src/chat/logic/timeline-target.ts +41 -1
  75. package/src/chat/logic/timeline.ts +19 -10
  76. package/src/chat/subagent-conversation.ts +13 -2
  77. package/src/chat/timeline-flash.ts +5 -3
  78. package/src/chat/ui/ImageView.tsx +51 -8
  79. package/src/chat/ui/SubagentTimeline.tsx +24 -1
  80. package/src/chat/ui/Timeline.tsx +97 -21
  81. package/src/chat/ui/intelligent-ui/UiBlockFrame.tsx +219 -0
  82. package/src/chat/ui/intelligent-ui/UiFallbackPart.tsx +21 -0
  83. package/src/chat/ui/intelligent-ui/UiReply.tsx +541 -0
  84. package/src/chat/ui/intelligent-ui/block-local.ts +51 -0
  85. package/src/chat/ui/intelligent-ui/chart-data.ts +67 -0
  86. package/src/chat/ui/intelligent-ui/node-text.ts +119 -0
  87. package/src/chat/ui/intelligent-ui/registry.ts +53 -0
  88. package/src/chat/ui/intelligent-ui/render-context.ts +84 -0
  89. package/src/chat/ui/intelligent-ui/renderers/chart.tsx +319 -0
  90. package/src/chat/ui/intelligent-ui/renderers/choices.tsx +102 -0
  91. package/src/chat/ui/intelligent-ui/renderers/content.tsx +140 -0
  92. package/src/chat/ui/intelligent-ui/renderers/data.tsx +181 -0
  93. package/src/chat/ui/intelligent-ui/renderers/inputs.tsx +144 -0
  94. package/src/chat/ui/intelligent-ui/renderers/links.tsx +158 -0
  95. package/src/chat/ui/intelligent-ui/renderers/structure.tsx +84 -0
  96. package/src/chat/ui/intelligent-ui/renderers/text.tsx +124 -0
  97. package/src/chat/ui/intelligent-ui/reply-parts.ts +22 -0
  98. package/src/chat/ui/intelligent-ui/table-data.ts +104 -0
  99. package/src/chat/ui/intelligent-ui/tones.ts +37 -0
  100. package/src/chat/ui/intelligent-ui/use-later.ts +17 -0
  101. package/src/chat/ui/intelligent-ui/use-ui-links.ts +143 -0
  102. package/src/chat/ui/intelligent-ui/use-ui-queries.ts +308 -0
  103. package/src/chat/ui/reply-context.ts +7 -0
  104. package/src/chat/ui/rows/GeneratedImageRow.tsx +244 -0
  105. package/src/chat/ui/rows/MessageRows.tsx +60 -4
  106. package/src/chat/ui/rows/Rows.tsx +4 -2
  107. package/src/chat/ui/rows/SubagentRow.tsx +5 -0
  108. package/src/chat/ui/rows/WorkRows.tsx +20 -5
  109. package/src/host.ts +27 -1
  110. package/src/locales/en/agent-chat.json +101 -0
  111. package/src/locales/nl/agent-chat.json +101 -0
  112. package/src/state/chats.ts +13 -0
  113. package/src/theme.css +19 -0
@@ -0,0 +1,102 @@
1
+ import { useId, useState } from 'react';
2
+ import clsx from 'clsx';
3
+ import { useTranslation } from 'react-i18next';
4
+ import { ArrowRight, Check } from 'lucide-react';
5
+ import type { UiProps } from '@adecore/intelligent-ui';
6
+ import { DisabledReason, Icon, Spinner, Tooltip } from '@adecore/ui';
7
+ import type { UiRendererProps } from '../render-context';
8
+ import { useLater } from '../use-later';
9
+
10
+ /* A context this long may run past its two lines, so the row offers all of it in a tooltip. */
11
+ const LONG_CONTEXT = 120;
12
+ const SPINNER_AFTER_MS = 300;
13
+
14
+ /* Says once, politely, that the choices opened when the reply was done; never on a block that was already done. */
15
+ export function ChoicesRenderer({ children, context }: UiRendererProps<UiProps<'Choices'>>) {
16
+ const { t } = useTranslation('agent-chat');
17
+ const [streamed] = useState(context.phase === 'streaming');
18
+ return (
19
+ <div role="group" aria-label={t('blocks.choices')} className="chat-ui-nodes flex flex-col gap-1.5">
20
+ {children}
21
+ <span className="sr-only" role="status">
22
+ {streamed && context.phase === 'final' ? t('blocks.choice.ready') : ''}
23
+ </span>
24
+ </div>
25
+ );
26
+ }
27
+
28
+ /*
29
+ * A row that shows what it sends: the label, and under it the context the agent receives. It stays
30
+ * closed while the reply streams, once the block is answered and without a host to send it, and
31
+ * remains focusable then so a screen reader hears why.
32
+ */
33
+ export function ChoiceRenderer({ node, children, context }: UiRendererProps<UiProps<'Choice'>>) {
34
+ const { t } = useTranslation('agent-chat');
35
+ const descriptionId = useId();
36
+ const { context: detail, primary, disabled } = node.props;
37
+ const { answer, onChoose } = context;
38
+ const chosen = answer !== null && answer.choiceId === node.id;
39
+ const waiting = context.phase === 'streaming';
40
+ const unavailable = disabled === true || onChoose === undefined;
41
+ const failed = answer === null && context.failedChoiceId === node.id;
42
+ const closed = waiting || unavailable || answer !== null;
43
+ const slow = useLater(chosen && answer.state === 'sending', SPINNER_AFTER_MS);
44
+ const reason = waiting
45
+ ? t('blocks.choice.waiting')
46
+ : answer === null && unavailable
47
+ ? ((disabled === true ? null : context.choiceReason) ?? t('blocks.choice.unavailable'))
48
+ : null;
49
+ const described = detail !== undefined && detail !== '';
50
+
51
+ const row = (
52
+ <button
53
+ type="button"
54
+ aria-disabled={closed || undefined}
55
+ aria-describedby={described ? descriptionId : undefined}
56
+ className={clsx(
57
+ 'flex w-full items-start gap-2 rounded-md border px-2 py-1.5 text-left transition-[opacity,scale,background-color] duration-150 motion-reduce:transition-none',
58
+ chosen ? 'border-accent bg-accent-soft' : failed ? 'border-status-error bg-surface-hover' : 'border-border bg-surface-hover',
59
+ !closed && 'hover:bg-surface-active active:scale-99',
60
+ waiting ? 'opacity-60' : closed && !chosen && 'opacity-50'
61
+ )}
62
+ onClick={() => {
63
+ if (!closed) {
64
+ onChoose?.(node.id);
65
+ }
66
+ }}
67
+ >
68
+ <Icon icon={ArrowRight} size={14} className={clsx('mt-1 shrink-0', primary === true ? 'text-accent' : 'text-text-faint')} />
69
+ <span className="flex min-w-0 grow flex-col">
70
+ <span className="text-sm font-semibold text-text">{children}</span>
71
+ {described && (
72
+ <span id={descriptionId} className="line-clamp-2 text-xs text-text-muted">
73
+ {detail}
74
+ </span>
75
+ )}
76
+ </span>
77
+ {chosen && (
78
+ <span className="mt-0.5 flex shrink-0 items-center gap-1 text-xs text-accent">
79
+ {slow ? <Spinner size={12} /> : <Icon icon={Check} size={12} />}
80
+ {answer.state === 'queued' ? t('blocks.choice.queued') : t('blocks.choice.sent')}
81
+ </span>
82
+ )}
83
+ </button>
84
+ );
85
+
86
+ return (
87
+ <div className="flex flex-col gap-1">
88
+ {reason !== null ? (
89
+ <DisabledReason reason={reason}>{row}</DisabledReason>
90
+ ) : described && detail.length > LONG_CONTEXT ? (
91
+ <Tooltip label={detail}>{row}</Tooltip>
92
+ ) : (
93
+ row
94
+ )}
95
+ {failed && (
96
+ <p role="alert" className="px-2 text-xs text-status-error">
97
+ {t('blocks.choice.failed')}
98
+ </p>
99
+ )}
100
+ </div>
101
+ );
102
+ }
@@ -0,0 +1,140 @@
1
+ import clsx from 'clsx';
2
+ import { createContext, useContext, useId } from 'react';
3
+ import { useTranslation } from 'react-i18next';
4
+ import type { UiProps } from '@adecore/intelligent-ui';
5
+ import { PreviewCard } from '@adecore/ui';
6
+ import { formatNumber } from '@adecore/ui/format';
7
+ import { chatHost } from '../../../../host';
8
+ import { CodeBlock } from '../../CodeBlock';
9
+ import { CodeStreamingContext } from '../../code-streaming';
10
+ import { ImageThumb } from '../../ImageView';
11
+ import { uiChildrenOf, uiNodeLabel, uiNodeText, uiUrlDomain } from '../node-text';
12
+ import type { UiRendererProps } from '../render-context';
13
+
14
+ // The number of every source by its node id, which the list counts in its own order.
15
+ const SourceNumbers = createContext<ReadonlyMap<string, number>>(new Map());
16
+
17
+ /* Plain text in the same measure until the node closes, so highlighting never makes it jump. */
18
+ export function CodeBlockRenderer({ node }: UiRendererProps<UiProps<'CodeBlock'>>) {
19
+ return (
20
+ <CodeStreamingContext.Provider value={!node.complete}>
21
+ <CodeBlock code={uiNodeText(node).replace(/^\n|\n$/g, '')} lang={node.props.language ?? 'text'} />
22
+ </CodeStreamingContext.Provider>
23
+ );
24
+ }
25
+
26
+ /* Its own component, so the host's hook is only called for an image that names an attachment. */
27
+ function AttachedImage({ scopeId, chatId, attachmentId, alt }: { scopeId: string; chatId: string; attachmentId: string; alt: string }) {
28
+ const source = chatHost().attachments.useUrl(scopeId, chatId, attachmentId);
29
+ return <ImageThumb source={source} alt={alt} className="max-h-70 max-w-full object-contain" />;
30
+ }
31
+
32
+ /*
33
+ * An attachment of the chat, never a path or an address the model wrote. An image of the latest
34
+ * generation stays a quiet square until the daemon resolved it to an attachment.
35
+ */
36
+ export function ImageRenderer({ node, children, context }: UiRendererProps<UiProps<'Image'>>) {
37
+ const { t } = useTranslation('agent-chat');
38
+ const { attachment, alt } = node.props;
39
+ const caption = uiNodeLabel(node);
40
+ const name = alt ?? (caption === '' ? t('blocks.image.label') : caption);
41
+ return (
42
+ <figure className="flex flex-col items-start gap-1.5">
43
+ {attachment === undefined ? (
44
+ <ImageThumb source={{ url: null, failure: t('blocks.image.unavailable') }} alt={name} />
45
+ ) : (
46
+ <AttachedImage scopeId={context.scopeId} chatId={context.chatId} attachmentId={attachment} alt={name} />
47
+ )}
48
+ {caption !== '' && <figcaption className="px-2 text-xs text-text-muted">{children}</figcaption>}
49
+ </figure>
50
+ );
51
+ }
52
+
53
+ export function SourcesRenderer({ node, children }: UiRendererProps<UiProps<'Sources'>>) {
54
+ const { t } = useTranslation('agent-chat');
55
+ const headingId = useId();
56
+ const numbers = new Map(uiChildrenOf(node, 'Source').map((source, index) => [source.id, index + 1]));
57
+ return (
58
+ <SourceNumbers value={numbers}>
59
+ <section aria-labelledby={headingId} className="flex flex-col">
60
+ <span id={headingId} className="px-2 pb-1 text-xs font-medium text-text-muted">
61
+ {t('blocks.sources')}
62
+ </span>
63
+ <ol className="chat-ui-nodes flex flex-col">{children}</ol>
64
+ </section>
65
+ </SourceNumbers>
66
+ );
67
+ }
68
+
69
+ export interface UiSourceCitationProps {
70
+ /* The number of the Source, as its row in the list counts it. */
71
+ number: number;
72
+ title: string;
73
+ url: string;
74
+ /* Shows the Source row the number stands for. Without it the number is no button. */
75
+ onReveal?: () => void;
76
+ className?: string;
77
+ }
78
+
79
+ /*
80
+ * A source number in running text. Resting on it or focusing it shows the title and the domain, and
81
+ * a tap reveals the row in the list of sources; only that row opens the address, so nothing loads
82
+ * before a person asks for it.
83
+ */
84
+ export function UiSourceCitation({ number, title, url, onReveal, className }: UiSourceCitationProps) {
85
+ const { t } = useTranslation('agent-chat');
86
+ return (
87
+ <PreviewCard.Root>
88
+ <PreviewCard.Trigger
89
+ delay={300}
90
+ closeDelay={150}
91
+ render={onReveal === undefined ? <span /> : <button type="button" />}
92
+ aria-label={onReveal === undefined ? undefined : t('blocks.citation', { number: formatNumber(number), title })}
93
+ className={clsx(
94
+ 'inline-flex h-4 min-w-4.5 items-center justify-center rounded-sm bg-text/7 px-1 align-[1px] text-2xs leading-4 text-text-muted tabular-nums',
95
+ onReveal !== undefined && 'cursor-pointer hover:bg-text/12 hover:text-text',
96
+ className
97
+ )}
98
+ onClick={onReveal}
99
+ >
100
+ {formatNumber(number)}
101
+ </PreviewCard.Trigger>
102
+ <PreviewCard.Popup side="top" align="center" sideOffset={4} className="flex max-w-72 flex-col px-2.5 py-1.5 text-xs">
103
+ <span className="line-clamp-2 text-text">{title}</span>
104
+ <span className="truncate text-text-faint">{uiUrlDomain(url)}</span>
105
+ </PreviewCard.Popup>
106
+ </PreviewCard.Root>
107
+ );
108
+ }
109
+
110
+ /*
111
+ * Opens through the host and loads nothing before, not even an icon. The row itself takes focus
112
+ * when a citation reveals it, apart from the button that opens it.
113
+ */
114
+ export function SourceRenderer({ node, context }: UiRendererProps<UiProps<'Source'>>) {
115
+ const number = useContext(SourceNumbers).get(node.id);
116
+ const { title, url } = node.props;
117
+ const { openUrl } = context;
118
+ const face = (
119
+ <>
120
+ <span className="w-5 shrink-0 text-right text-text-faint tabular-nums">{number}</span>
121
+ <span className="min-w-0 truncate text-text">{title}</span>
122
+ <span className="shrink-0 text-text-faint">{uiUrlDomain(url)}</span>
123
+ </>
124
+ );
125
+ return (
126
+ <li data-ui-source={node.id} tabIndex={-1} className="chat-ui-source scroll-my-2 rounded-md">
127
+ {openUrl === undefined ? (
128
+ <span className="flex h-8 items-center gap-2 px-2 text-xs">{face}</span>
129
+ ) : (
130
+ <button
131
+ type="button"
132
+ className="flex h-8 w-full items-center gap-2 rounded-md px-2 text-left text-xs hover:bg-surface-hover"
133
+ onClick={() => openUrl(url)}
134
+ >
135
+ {face}
136
+ </button>
137
+ )}
138
+ </li>
139
+ );
140
+ }
@@ -0,0 +1,181 @@
1
+ import clsx from 'clsx';
2
+ import { useTranslation } from 'react-i18next';
3
+ import { ArrowDown, ArrowRight, ArrowUp } from 'lucide-react';
4
+ import type { UiProps } from '@adecore/intelligent-ui';
5
+ import { Button, Icon, KeyValueList, Pill } from '@adecore/ui';
6
+ import { formatBytes, formatDuration, formatMoment, formatNumber, formatPercent, formatRounded } from '@adecore/ui/format';
7
+ import { useBlockLocal } from '../block-local';
8
+ import { uiChildrenOf, uiWithUnit } from '../node-text';
9
+ import type { UiLiveValue, UiRendererProps } from '../render-context';
10
+ import { isNumericColumn, UI_TABLE_ROWS, uiTableCell, uiTableColumns, type UiTableCell, type UiTableColumn } from '../table-data';
11
+ import { UI_TONE_TEXT } from '../tones';
12
+ import { UiFallbackPart } from '../UiFallbackPart';
13
+
14
+ /* EntityList draws a hairline between its rows from this many on. */
15
+ const DIVIDED_FROM = 7;
16
+
17
+ /*
18
+ * A number that live readings keep up to date. Keyed by its text, so a value that changed mounts
19
+ * again and lights up once, even when it changes again before the last light settled.
20
+ */
21
+ function LiveFigure({ text, previous, live, className }: { text: string; previous?: string; live?: UiLiveValue; className?: string }) {
22
+ const lit = live !== undefined && live.changed && !live.stale && previous !== text;
23
+ return (
24
+ <span key={text} className={clsx(live?.stale ? 'text-text-muted' : 'text-text', lit && 'chat-ui-changed', className)}>
25
+ {text}
26
+ </span>
27
+ );
28
+ }
29
+
30
+ export function StatsRenderer({ children }: UiRendererProps<UiProps<'Stats'>>) {
31
+ return <div className="chat-ui-nodes grid grid-cols-[repeat(auto-fit,minmax(128px,1fr))] gap-2">{children}</div>;
32
+ }
33
+
34
+ /* The arrow and the number say which way a value went; only a tone says whether that is good. */
35
+ export function StatRenderer({ node, context }: UiRendererProps<UiProps<'Stat'>>) {
36
+ const { t } = useTranslation('agent-chat');
37
+ const { label, value, previous, unit, tone } = node.props;
38
+ const live = context.liveValue?.(node.id, 'value');
39
+ const change = previous === undefined ? null : value - previous;
40
+ const arrow = change === null || change === 0 ? ArrowRight : change > 0 ? ArrowUp : ArrowDown;
41
+ const from = previous === undefined ? '' : uiWithUnit(formatRounded(previous, 2), unit);
42
+ // A change from zero has no percentage.
43
+ const percent = change === null || previous === undefined || previous === 0 ? null : formatPercent(Math.abs((change / previous) * 100));
44
+ return (
45
+ <div className="flex min-w-0 flex-col gap-0.5 rounded-md bg-surface-hover px-2 py-1.5">
46
+ <span className="truncate text-xs text-text-muted">{label}</span>
47
+ <LiveFigure
48
+ text={uiWithUnit(formatRounded(value, 2), unit)}
49
+ previous={typeof live?.previous === 'number' ? uiWithUnit(formatRounded(live.previous, 2), unit) : undefined}
50
+ live={live}
51
+ className="text-lg font-semibold tabular-nums"
52
+ />
53
+ {change !== null && (
54
+ <span className={clsx('flex items-center gap-1 text-xs tabular-nums', tone === undefined ? 'text-text-faint' : UI_TONE_TEXT[tone])}>
55
+ <Icon icon={arrow} size={12} className="shrink-0" />
56
+ <span className="min-w-0 truncate">
57
+ {percent === null ? t('blocks.stat.from', { previous: from }) : t('blocks.stat.change', { change: percent, previous: from })}
58
+ </span>
59
+ </span>
60
+ )}
61
+ </div>
62
+ );
63
+ }
64
+
65
+ export function EntityListRenderer({ node, children }: UiRendererProps<UiProps<'EntityList'>>) {
66
+ return (
67
+ <KeyValueList.Root divided={uiChildrenOf(node, 'Entry').length >= DIVIDED_FROM} className="chat-ui-nodes px-2">
68
+ {children}
69
+ </KeyValueList.Root>
70
+ );
71
+ }
72
+
73
+ export function EntryRenderer({ node, children }: UiRendererProps<UiProps<'Entry'>>) {
74
+ return (
75
+ <KeyValueList.Item>
76
+ <KeyValueList.Name>{node.props.label}</KeyValueList.Name>
77
+ <KeyValueList.Value>{children}</KeyValueList.Value>
78
+ </KeyValueList.Item>
79
+ );
80
+ }
81
+
82
+ function numberText(cell: Extract<UiTableCell, { kind: 'number' }>, unit: string | undefined): string {
83
+ return cell.as === 'bytes' ? formatBytes(cell.value) : cell.as === 'duration' ? formatDuration(cell.value) : uiWithUnit(formatRounded(cell.value, 2), unit);
84
+ }
85
+
86
+ function Cell({ value, column, live }: { value: unknown; column: UiTableColumn; live?: UiLiveValue }) {
87
+ const cell = uiTableCell(value, column);
88
+ switch (cell.kind) {
89
+ case 'empty':
90
+ return null;
91
+ case 'text':
92
+ return <>{cell.text}</>;
93
+ case 'number': {
94
+ const before = live?.previous === undefined ? undefined : uiTableCell(live.previous, column);
95
+ return (
96
+ <LiveFigure
97
+ text={numberText(cell, column.unit)}
98
+ previous={before?.kind === 'number' ? numberText(before, column.unit) : undefined}
99
+ live={live}
100
+ />
101
+ );
102
+ }
103
+ case 'date':
104
+ return <time dateTime={new Date(cell.at).toISOString()}>{formatMoment(cell.at)}</time>;
105
+ case 'file':
106
+ // A cell is no link target the host checked, so its path stays text.
107
+ return <span className="font-mono text-code break-all">{cell.path}</span>;
108
+ case 'tag':
109
+ return (
110
+ <Pill shape="tag" size="sm">
111
+ {cell.text}
112
+ </Pill>
113
+ );
114
+ }
115
+ }
116
+
117
+ /*
118
+ * Rows under a sticky head, scrolling inside 320px and sideways when wider than the column. Rows
119
+ * arrive one by one while the block streams, without animation; past fifty a person asks for the rest.
120
+ */
121
+ export function TableRenderer({ node, context }: UiRendererProps<UiProps<'Table'>>) {
122
+ const { t } = useTranslation('agent-chat');
123
+ const [all, setAll] = useBlockLocal(context, node.id, () => false);
124
+ const columns = uiTableColumns(node);
125
+ if (columns.length === 0) {
126
+ return <UiFallbackPart fallback={node.fallback} problem={{ kind: 'failed' }} />;
127
+ }
128
+ const { rows } = node.props;
129
+ const shown = all ? rows : rows.slice(0, UI_TABLE_ROWS);
130
+ return (
131
+ <div className="flex flex-col items-start gap-1.5">
132
+ <div className="max-h-80 w-full overflow-auto rounded-md border border-border-soft">
133
+ <table
134
+ aria-label={t('blocks.table.label', { columns: columns.map((column) => column.title).join(', ') })}
135
+ className="w-full border-collapse text-xs tabular-nums"
136
+ >
137
+ <thead className="sticky top-0 z-1 bg-surface-hover">
138
+ <tr>
139
+ {columns.map((column) => (
140
+ <th
141
+ key={column.key}
142
+ scope="col"
143
+ className={clsx(
144
+ 'h-8 px-2 font-medium whitespace-nowrap text-text-muted',
145
+ isNumericColumn(column) ? 'text-right' : 'text-left'
146
+ )}
147
+ >
148
+ {column.title}
149
+ </th>
150
+ ))}
151
+ </tr>
152
+ </thead>
153
+ <tbody>
154
+ {shown.map((row, index) => (
155
+ // Rows only ever grow at the end while a block streams, so the place of a row is a stable key.
156
+ <tr key={index} className="border-t border-border-soft">
157
+ {columns.map((column) => (
158
+ <td
159
+ key={column.key}
160
+ className={clsx('h-8 px-2 text-text', isNumericColumn(column) ? 'text-right whitespace-nowrap' : 'text-left')}
161
+ >
162
+ <Cell
163
+ value={row[column.key]}
164
+ column={column}
165
+ live={isNumericColumn(column) ? context.liveValue?.(node.id, 'rows', [index, column.key]) : undefined}
166
+ />
167
+ </td>
168
+ ))}
169
+ </tr>
170
+ ))}
171
+ </tbody>
172
+ </table>
173
+ </div>
174
+ {!all && rows.length > UI_TABLE_ROWS && (
175
+ <Button size="xs" variant="secondary" onClick={() => setAll(true)}>
176
+ {t('blocks.table.showAll', { total: formatNumber(rows.length) })}
177
+ </Button>
178
+ )}
179
+ </div>
180
+ );
181
+ }
@@ -0,0 +1,144 @@
1
+ import { createContext, useContext, type MouseEvent, type ReactNode } from 'react';
2
+ import clsx from 'clsx';
3
+ import { useTranslation } from 'react-i18next';
4
+ import type { UiBinding, UiProps, UiViewNode } from '@adecore/intelligent-ui';
5
+ import { Button, Checkbox, Segmented, Slider, Switch } from '@adecore/ui';
6
+ import { uiChildrenOf, uiNodeLabel, uiWithUnit } from '../node-text';
7
+ import type { UiRenderContext, UiRendererProps } from '../render-context';
8
+
9
+ type Scalar = UiProps<'Item'>['value'];
10
+
11
+ interface ChecklistState {
12
+ values: readonly Scalar[];
13
+ enabled: boolean;
14
+ toggle(value: Scalar): void;
15
+ }
16
+
17
+ const ChecklistContext = createContext<ChecklistState>({ values: [], enabled: false, toggle: () => undefined });
18
+
19
+ /*
20
+ * The binding of an input, or null for a read-only node. An input works once its own node closed and
21
+ * only through its binding, so the value never leaves the block until a Choice carries it.
22
+ */
23
+ function bindingOf<Value>(node: UiViewNode<{ value: Value }>, context: UiRenderContext): UiBinding<Value> | null {
24
+ const binding = node.bindings.value as UiBinding<Value> | undefined;
25
+ return binding === undefined || !node.complete || context.answer !== null ? null : binding;
26
+ }
27
+
28
+ /* An input whose node has not closed yet stands at 60% and takes no pointer. */
29
+ const PENDING = 'pointer-events-none opacity-60';
30
+
31
+ /* The rows of a checklist as one named group. */
32
+ function ChecklistGroup({ pending, children }: { pending: boolean; children: ReactNode }) {
33
+ const { t } = useTranslation('agent-chat');
34
+ return (
35
+ <div role="group" aria-label={t('blocks.checklist')} className={clsx('chat-ui-nodes flex flex-col', pending && PENDING)}>
36
+ {children}
37
+ </div>
38
+ );
39
+ }
40
+
41
+ export function ChecklistRenderer({ node, children, context }: UiRendererProps<UiProps<'Checklist'>>) {
42
+ const binding = bindingOf(node, context);
43
+ const values = binding?.value ?? node.props.value;
44
+ const toggle = (value: Scalar): void => {
45
+ binding?.onValueChange(values.includes(value) ? values.filter((entry) => entry !== value) : [...values, value]);
46
+ };
47
+ return (
48
+ <ChecklistContext value={{ values, enabled: binding !== null, toggle }}>
49
+ <ChecklistGroup pending={!node.complete}>{children}</ChecklistGroup>
50
+ </ChecklistContext>
51
+ );
52
+ }
53
+
54
+ /* The whole row is the label; a link inside it keeps its own click and checks nothing. */
55
+ export function ItemRenderer({ node, children }: UiRendererProps<UiProps<'Item'>>) {
56
+ const list = useContext(ChecklistContext);
57
+ const { value } = node.props;
58
+ const toggleRow = (event: MouseEvent<HTMLDivElement>): void => {
59
+ if (list.enabled && !(event.target as Element).closest('button, a')) {
60
+ list.toggle(value);
61
+ }
62
+ };
63
+ return (
64
+ <div
65
+ className={clsx('flex min-h-8 items-center gap-2 rounded-md px-2 text-sm text-text', list.enabled && 'hover:bg-surface-hover')}
66
+ onClick={toggleRow}
67
+ >
68
+ <Checkbox checked={list.values.includes(value)} onCheckedChange={() => list.toggle(value)} label={uiNodeLabel(node)} disabled={!list.enabled} />
69
+ <div className="min-w-0 grow">{children}</div>
70
+ </div>
71
+ );
72
+ }
73
+
74
+ export function SwitchRenderer({ node, children, context }: UiRendererProps<UiProps<'Switch'>>) {
75
+ const binding = bindingOf(node, context);
76
+ return (
77
+ <div className={clsx('flex min-h-7 items-center justify-between gap-3 px-2 text-sm', !node.complete && PENDING)}>
78
+ <span className="min-w-0 text-text">{children}</span>
79
+ <Switch
80
+ checked={binding?.value ?? node.props.value}
81
+ onCheckedChange={(next) => binding?.onValueChange(next)}
82
+ label={uiNodeLabel(node)}
83
+ disabled={binding === null}
84
+ />
85
+ </div>
86
+ );
87
+ }
88
+
89
+ /* Its children are the label, read as text: the slider writes its own label line. */
90
+ export function SliderRenderer({ node, context }: UiRendererProps<UiProps<'Slider'>>) {
91
+ const binding = bindingOf(node, context);
92
+ const { value, min, max, step, unit } = node.props;
93
+ return (
94
+ <Slider
95
+ value={binding?.value ?? value}
96
+ onValueChange={(next) => binding?.onValueChange(next)}
97
+ min={min}
98
+ max={max}
99
+ step={step}
100
+ // The slider prints its unit right after the number, so the space a unit stands apart with comes from here.
101
+ unit={uiWithUnit('', unit)}
102
+ label={uiNodeLabel(node)}
103
+ disabled={binding === null}
104
+ className={clsx('px-2', !node.complete && PENDING)}
105
+ />
106
+ );
107
+ }
108
+
109
+ /* Left aligned, never the full width. Each Option is a segment by its place, since a value may be any scalar. */
110
+ export function SegmentedRenderer({ node, context }: UiRendererProps<UiProps<'Segmented'>>) {
111
+ const { t } = useTranslation('agent-chat');
112
+ const binding = bindingOf(node, context);
113
+ const current = binding?.value ?? node.props.value;
114
+ const options = uiChildrenOf(node, 'Option').map((option, index) => ({ id: String(index), label: uiNodeLabel(option), value: option.props.value }));
115
+ const picked = options.find((option) => option.value === current)?.id ?? '';
116
+ return (
117
+ <div className={clsx('px-2', !node.complete && PENDING)}>
118
+ <Segmented
119
+ value={picked}
120
+ options={options}
121
+ label={t('blocks.options')}
122
+ disabled={binding === null}
123
+ onValueChange={(id) => {
124
+ const option = options[Number(id)];
125
+ if (option !== undefined) {
126
+ binding?.onValueChange(option.value);
127
+ }
128
+ }}
129
+ />
130
+ </div>
131
+ );
132
+ }
133
+
134
+ /* Runs its local action once its own node closed; like an input, it closes once the block is answered. */
135
+ export function ButtonRenderer({ node, children, context }: UiRendererProps<UiProps<'Button'>>) {
136
+ const enabled = node.onAction !== undefined && node.complete && context.answer === null && node.props.disabled !== true;
137
+ return (
138
+ <div className={clsx('px-2', !node.complete && PENDING)}>
139
+ <Button variant="secondary" size="sm" disabled={!enabled} onClick={() => node.onAction?.()}>
140
+ {children}
141
+ </Button>
142
+ </div>
143
+ );
144
+ }