@jsb188/ai-chat 0.0.0-stage → 1.1.12

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 (51) hide show
  1. package/README.md +109 -2
  2. package/VERIFICATION.md +118 -0
  3. package/benchmarks/browser.mjs +204 -0
  4. package/benchmarks/streaming.mjs +89 -0
  5. package/package.json +36 -5
  6. package/src/main.ts +5 -0
  7. package/src/modules/AIChatContent.tsx +84 -0
  8. package/src/modules/AgentNoteActionsMenu.tsx +121 -0
  9. package/src/modules/AgentNoteBootstrapProgress.tsx +41 -0
  10. package/src/modules/AgentNoteItem.helpers.ts +115 -0
  11. package/src/modules/AgentNoteItem.tsx +232 -0
  12. package/src/modules/AgentNotes.constants.ts +10 -0
  13. package/src/modules/AgentNotes.helpers.ts +274 -0
  14. package/src/modules/AgentNotes.hooks.ts +449 -0
  15. package/src/modules/AgentNotes.tsx +74 -0
  16. package/src/modules/AgentNotes.types.ts +50 -0
  17. package/src/modules/AgentNotesCollection.hooks.ts +69 -0
  18. package/src/modules/AgentNotesSection.tsx +145 -0
  19. package/src/parser.ts +4 -0
  20. package/src/types/content.ts +103 -0
  21. package/src/ui/AIChatUI.css +557 -0
  22. package/src/ui/AIChatUI.tsx +816 -0
  23. package/src/ui/AgentNotesUI.tsx +375 -0
  24. package/src/ui/AgentNotesUI.types.ts +19 -0
  25. package/src/ui/ChatBlocks.tsx +92 -0
  26. package/src/ui/ChatCodeBlock.tsx +16 -0
  27. package/src/ui/ChatInline.helpers.ts +20 -0
  28. package/src/ui/ChatInline.tsx +89 -0
  29. package/src/ui/ChatTable.helpers.ts +37 -0
  30. package/src/ui/ChatTable.tsx +126 -0
  31. package/src/utils/contentIdentity.ts +108 -0
  32. package/src/utils/parseBlocks.helpers.ts +50 -0
  33. package/src/utils/parseBlocks.ts +266 -0
  34. package/src/utils/parseContent.helpers.ts +102 -0
  35. package/src/utils/parseContent.ts +33 -0
  36. package/src/utils/parseContentIncrementally.ts +46 -0
  37. package/src/utils/parseCsv.ts +99 -0
  38. package/src/utils/parseInline.helpers.ts +48 -0
  39. package/src/utils/parseInline.ts +396 -0
  40. package/src/utils/parseTable.ts +202 -0
  41. package/tests/AgentNotes.test.tsx +1672 -0
  42. package/tests/fixtures/content.ts +13 -0
  43. package/tests/parseContent.test.ts +227 -0
  44. package/tests/parseCsv.test.ts +82 -0
  45. package/tests/parseInline.test.ts +222 -0
  46. package/tests/renderContent.ssr.test.tsx +19 -0
  47. package/tests/renderContent.test.tsx +282 -0
  48. package/tests/streaming.test.ts +51 -0
  49. package/tests/streamingEquivalence.test.ts +265 -0
  50. package/tsconfig.json +16 -0
  51. package/vitest.config.ts +18 -0
@@ -0,0 +1,375 @@
1
+ import { ActivityIndicator } from '@jsb188/react-web/ui/Loading';
2
+ import i18n from '@jsb188/app/i18n/index.ts';
3
+ import { getFullDateTime } from '@jsb188/app/utils/datetime.ts';
4
+ import { cn } from '@jsb188/app/utils/string.ts';
5
+ import { Textarea } from '@jsb188/form/modules/Textarea';
6
+ import { getAgentNoteDisplayEmoji } from '@jsb188/mday/utils/agentNote.ts';
7
+ import { ToggleExpand, ToggleExpandHeader } from '@jsb188/react-web/modules/Accordian';
8
+ import { SmartLink } from '@jsb188/react-web/modules/Link';
9
+ import { TooltipButton } from '@jsb188/react-web/modules/PopOver';
10
+ import { Icon } from '@jsb188/react-web/svgs/Icon';
11
+ import { type ChangeEvent, type KeyboardEvent, type MouseEvent, memo, type ReactNode, useId } from 'react';
12
+ import type { AgentNoteActionState } from './AgentNotesUI.types';
13
+
14
+ /*
15
+ * Display a concise review status and dates without exposing agent notes or source data.
16
+ */
17
+ export const AgentNoteBootstrapProgressUI = (p: {
18
+ title: string;
19
+ description: string;
20
+ lastReviewedAt: string;
21
+ nextReviewAt: string | null;
22
+ }) => (
23
+ <div
24
+ className="p_sm mb_sm bg_fade r_smw"
25
+ role="status"
26
+ >
27
+ <p className="ft_medium">{p.title}</p>
28
+ <p className="mt_xs cl_md">{p.description}</p>
29
+ <p className="mt_xs ft_sm cl_md">
30
+ {i18n.t('agent_note.agent_note_discovery_last_reviewed', {
31
+ date: getFullDateTime(p.lastReviewedAt, { textDateStyle: 'short' }),
32
+ })}
33
+ </p>
34
+ {p.nextReviewAt ? (
35
+ <p className="ft_sm cl_md">
36
+ {i18n.t('agent_note.agent_note_discovery_next_review', { date: getFullDateTime(p.nextReviewAt, { textDateStyle: 'short' }) })}
37
+ </p>
38
+ ) : null}
39
+ <p className="mt_xs ft_sm cl_md">{i18n.t('agent_note.agent_note_discovery_schedule')}</p>
40
+ </div>
41
+ );
42
+
43
+ /*
44
+ * Wrap a note list in its own disclosure with a title and expansion chevron.
45
+ */
46
+ export const AgentNotesSectionUI = (p: {
47
+ title: string;
48
+ icon: string;
49
+ controlsId: string;
50
+ expanded: boolean;
51
+ onToggle: () => void;
52
+ children: ReactNode;
53
+ }) => (
54
+ <ToggleExpand
55
+ id={p.controlsId}
56
+ expanded={p.expanded}
57
+ animateContentHeight
58
+ // Reserve the expanded note border's 10px outset inside the disclosure's clipping area.
59
+ className={cn('trans -mx_10', p.expanded ? 'mb_sm' : 'mb_n')}
60
+ contentClassName="px_10"
61
+ HeaderComponent={
62
+ <ToggleExpandHeader
63
+ expanded={p.expanded}
64
+ expandedStateStyle="right-edge"
65
+ controlsId={p.controlsId}
66
+ label={p.title}
67
+ onToggle={p.onToggle}
68
+ className="bg_fade pl_sm pr_xs py_xs r_smw mx_10 mb_xs"
69
+ linkHoverClassName="op_50_hv bg_alt_hv"
70
+ >
71
+ <span
72
+ className="ic_sm no_shrink"
73
+ aria-hidden="true"
74
+ >
75
+ <Icon name={p.icon} />
76
+ </span>
77
+ <span className="ellip ft_medium">{p.title}</span>
78
+ </ToggleExpandHeader>
79
+ }
80
+ >
81
+ {p.children}
82
+ </ToggleExpand>
83
+ );
84
+
85
+ /*
86
+ * Reveal more already-loaded notes below a group's list.
87
+ */
88
+ export const AgentNotesShowMoreUI = (p: { onClick: () => void }) => (
89
+ <button
90
+ type="button"
91
+ className="mx_auto bl my_10 px_md h_34 r ft_xs ft_medium cl_md a_c bd_1 bd_lt bg bg_assist_fd_hv bd_assist_hv"
92
+ onClick={p.onClick}
93
+ >
94
+ {i18n.t('c.show_more')}
95
+ </button>
96
+ );
97
+
98
+ /*
99
+ * Render note actions with consistent busy states, disabled explanations, and optional refresh warnings.
100
+ */
101
+ const AgentNoteActionButton = (p: {
102
+ action: AgentNoteActionState;
103
+ label: string;
104
+ icon: string;
105
+ active?: boolean;
106
+ warning?: string;
107
+ }) => {
108
+ const disabled = !!p.action.disabledReason || p.action.busy;
109
+
110
+ /*
111
+ * Claim action clicks so the surrounding disclosure remains open.
112
+ */
113
+ const handleClick = (event: MouseEvent) => {
114
+ event.preventDefault();
115
+ if (!disabled) p.action.onClick();
116
+ };
117
+
118
+ return (
119
+ <TooltipButton
120
+ type="button"
121
+ className={cn('rel v_center no_shrink r no_link w_32 h_32', disabled ? 'cl_lt' : 'cl_md', !disabled && 'bg_hv')}
122
+ ariaLabel={p.label}
123
+ message={p.action.disabledReason ?? p.warning ?? p.label}
124
+ tooltipClassName="a_c"
125
+ position="top"
126
+ offsetY={-2}
127
+ aria-busy={p.action.busy}
128
+ aria-pressed={p.active}
129
+ disabled={disabled}
130
+ showTooltipWhenDisabled
131
+ onClick={handleClick}
132
+ >
133
+ <span
134
+ className={cn('ic_sm', p.active && 'cl_contrast')}
135
+ aria-hidden="true"
136
+ >
137
+ {p.action.busy ? <ActivityIndicator size="tn" /> : <Icon name={p.icon} />}
138
+ </span>
139
+ {p.warning ? (
140
+ <span
141
+ className="abs_tr_xs -mt_4 -mr_4 w_6 h_6 r bg_err noclick"
142
+ aria-hidden="true"
143
+ />
144
+ ) : null}
145
+ </TooltipButton>
146
+ );
147
+ };
148
+
149
+ /*
150
+ * Keep note actions inside the disclosure row and expanded content outside its click target.
151
+ * Collapsed, the row shows the headline; expanded, the header shows the last update time and the body starts with
152
+ * the headline. A note that needs attention
153
+ * carries a marker in its row and says so in the row's accessible name. Without a refresh action
154
+ * (an archived note), the expanded header offers no refresh.
155
+ */
156
+ export const AgentNoteItemUI = memo(
157
+ (p: {
158
+ emoji: string;
159
+ headline: string;
160
+ expanded: boolean;
161
+ needsAttention: boolean;
162
+ publishedAt: string;
163
+ refreshLabel?: string;
164
+ refreshAction: AgentNoteActionState | null;
165
+ pinAction: AgentNoteActionState | null;
166
+ pinned: boolean;
167
+ refreshWarning?: string;
168
+ ActionsComponent?: ReactNode;
169
+ ReferencesComponent?: ReactNode;
170
+ controlsId: string;
171
+ onToggle: () => void;
172
+ children: ReactNode;
173
+ FooterComponent?: ReactNode;
174
+ expandedHeight?: number;
175
+ }) => {
176
+ // The disclosure's own label names it, so the attention state must be part of that label to be announced
177
+ const label = p.expanded
178
+ ? p.refreshLabel || i18n.t('c.last_updated', { date: getFullDateTime(p.publishedAt, { textDateStyle: 'short' }) })
179
+ : p.headline;
180
+ return (
181
+ <ToggleExpand
182
+ id={p.controlsId}
183
+ expanded={p.expanded}
184
+ expandedHeight={p.expandedHeight}
185
+ className={cn('an_item r_df trans', p.expanded && 'expanded my_20')}
186
+ contentClassName="px_24 py_14"
187
+ FooterComponent={p.FooterComponent}
188
+ HeaderComponent={
189
+ <ToggleExpandHeader
190
+ expanded={p.expanded}
191
+ expandedStateStyle="right-edge"
192
+ controlsId={p.controlsId}
193
+ label={p.needsAttention ? `${label}, ${i18n.t('c.needs_attention')}` : label}
194
+ onToggle={p.onToggle}
195
+ className={cn('h_44 pl_sm pr_xs r_smw trans_color spd_1 bg_primary_fd_hv', p.expanded && 'bg_fade')}
196
+ ActionsComponent={
197
+ p.pinAction ? (
198
+ <AgentNoteActionButton
199
+ action={p.pinAction}
200
+ label={i18n.t(p.pinned ? 'agent_note.unpin_note' : 'agent_note.pin_note')}
201
+ icon={p.pinned ? 'pinned-filled' : 'pinned'}
202
+ active={p.pinned}
203
+ />
204
+ ) : undefined
205
+ }
206
+ RightComponent={
207
+ p.expanded ? (
208
+ p.ActionsComponent
209
+ ) : p.needsAttention ? (
210
+ <TooltipButton
211
+ type="button"
212
+ className="v_center no_shrink w_32 h_32 ic_sm cl_err no_link"
213
+ ariaLabel={i18n.t('c.needs_attention')}
214
+ message={i18n.t('c.needs_attention')}
215
+ tooltipClassName="a_c"
216
+ position="top"
217
+ offsetY={-2}
218
+ closeWhilePointerDown
219
+ >
220
+ <Icon name="alert-circle" />
221
+ </TooltipButton>
222
+ ) : undefined
223
+ }
224
+ InlineActionsComponent={
225
+ p.expanded ? (
226
+ <div className="row no_shrink h_item ml_xs">
227
+ {p.ReferencesComponent}
228
+ {p.refreshAction ? (
229
+ <AgentNoteActionButton
230
+ action={p.refreshAction}
231
+ label={i18n.t('agent_note.refresh_note')}
232
+ icon="reload"
233
+ warning={p.refreshWarning}
234
+ />
235
+ ) : null}
236
+ </div>
237
+ ) : undefined
238
+ }
239
+ >
240
+ <span
241
+ className="noto_emoji no_shrink shift_up"
242
+ aria-hidden="true"
243
+ >
244
+ {getAgentNoteDisplayEmoji(p.emoji)}
245
+ </span>
246
+ <span className={cn('ellip f', p.expanded && (p.refreshLabel ? 'cl_assist' : 'cl_md'))}>{label}</span>
247
+ </ToggleExpandHeader>
248
+ }
249
+ >
250
+ <p className="ft_md ofw mt_sm mb_20">{p.headline}</p>
251
+ {p.children}
252
+ </ToggleExpand>
253
+ );
254
+ },
255
+ );
256
+
257
+ /*
258
+ * Frame one saved query result below the note body: its title (a link when its listed table can be opened),
259
+ * the rendered result and a line explaining a result that is missing or could not be updated.
260
+ */
261
+ export const AgentNoteResultBlockUI = memo(
262
+ (p: {
263
+ title: string | null;
264
+ to: string | null;
265
+ status: string | null;
266
+ computedAt: string | null;
267
+ timeZone: string;
268
+ shownCount: number | null;
269
+ matchedCount: number | null;
270
+ children?: ReactNode;
271
+ }) => (
272
+ <div className="mt_md">
273
+ {p.title ? (
274
+ <h4 className="ft_sm ft_medium mb_xs">
275
+ {p.to ? (
276
+ <SmartLink
277
+ to={p.to}
278
+ className="cl_df cl_primary_hv"
279
+ >
280
+ {p.title}
281
+ </SmartLink>
282
+ ) : (
283
+ p.title
284
+ )}
285
+ </h4>
286
+ ) : null}
287
+ {p.children}
288
+ {p.shownCount != null && p.matchedCount != null && p.shownCount < p.matchedCount ? (
289
+ <p className="cl_lt mt_md">
290
+ {i18n.t('agent_note.result_showing', { shown: p.shownCount.toLocaleString(), matched: p.matchedCount.toLocaleString() })}
291
+ </p>
292
+ ) : null}
293
+ {p.computedAt ? (
294
+ <p className="cl_lt mt_md">
295
+ {i18n.t('agent_note.result_as_of', {
296
+ date: new Intl.DateTimeFormat('en-US', {
297
+ year: 'numeric',
298
+ month: 'short',
299
+ day: 'numeric',
300
+ hour: 'numeric',
301
+ minute: '2-digit',
302
+ timeZone: p.timeZone,
303
+ timeZoneName: 'short',
304
+ }).format(new Date(p.computedAt)),
305
+ })}
306
+ </p>
307
+ ) : null}
308
+ {p.status ? <p className="mt_xs ft_sm cl_md">{p.status}</p> : null}
309
+ </div>
310
+ ),
311
+ );
312
+
313
+ /*
314
+ * Render an editable note question with submission progress.
315
+ */
316
+ export const AgentNoteComposer = memo(
317
+ (p: { value: string; sending: boolean; disabled: boolean; onChange: (value: string) => void; onSend: () => void }) => {
318
+ const inputId = useId();
319
+ const disabled = p.disabled || p.sending;
320
+ const canSend = !disabled && !!p.value.trim();
321
+
322
+ /*
323
+ * Pass the textarea value to the note's draft state.
324
+ */
325
+ const handleChange = (event: ChangeEvent<HTMLTextAreaElement>) => {
326
+ p.onChange(event.currentTarget.value);
327
+ };
328
+
329
+ /*
330
+ * Apply the same submission guard to mouse and keyboard activation.
331
+ */
332
+ const handleSend = () => {
333
+ if (canSend) p.onSend();
334
+ };
335
+
336
+ /*
337
+ * Send on Enter while allowing Shift+Enter for a new line and composition input to finish normally.
338
+ */
339
+ const handleKeyDown = (event: KeyboardEvent) => {
340
+ if (event.defaultPrevented || event.key !== 'Enter' || event.shiftKey || event.nativeEvent.isComposing) return;
341
+ event.preventDefault();
342
+ handleSend();
343
+ };
344
+
345
+ return (
346
+ <div className="v_stretch gap_xs mx_10 mt_10 p_sm r_smw bg_fade">
347
+ <Textarea
348
+ id={inputId}
349
+ name="agent_note_message"
350
+ className="bg r_sm"
351
+ textareaClassName="bg no_resize stock focus_bg bd_primary_fc"
352
+ aria-label={i18n.t('agent_note.agent_note_message')}
353
+ placeholder={i18n.t('agent_note.agent_note_message_placeholder')}
354
+ minHeight={96}
355
+ value={p.value}
356
+ disabled={disabled}
357
+ onChange={handleChange}
358
+ onKeyDown={handleKeyDown}
359
+ />
360
+
361
+ <div className="h_right">
362
+ <button
363
+ type="button"
364
+ className="px_sm py_xs r_sm bg_primary"
365
+ disabled={!canSend}
366
+ aria-busy={p.sending}
367
+ onClick={handleSend}
368
+ >
369
+ {i18n.t(p.sending ? 'c.sending' : 'c.send')}
370
+ </button>
371
+ </div>
372
+ </div>
373
+ );
374
+ },
375
+ );
@@ -0,0 +1,19 @@
1
+ /*
2
+ * Describe one note action after permissions, in-flight work, and cooldowns are resolved by its owner.
3
+ */
4
+ export interface AgentNoteActionState {
5
+ disabledReason?: string;
6
+ busy: boolean;
7
+ onClick: () => void;
8
+ }
9
+
10
+ /*
11
+ * Keep archive and refresh actions visible with permission-specific disabled explanations.
12
+ */
13
+ export interface AgentNoteActions {
14
+ priority: AgentNoteActionState;
15
+ archive: AgentNoteActionState;
16
+ deleteScheduledTask: AgentNoteActionState | null;
17
+ pin: AgentNoteActionState | null;
18
+ refresh: AgentNoteActionState;
19
+ }
@@ -0,0 +1,92 @@
1
+ import { memo, type ComponentType } from 'react';
2
+ import type { ContentBlock } from '../types/content.ts';
3
+ import { getContentBlockKey } from '../utils/contentIdentity.ts';
4
+ import { ChatCodeBlock } from './ChatCodeBlock.tsx';
5
+ import { ChatInline } from './ChatInline.tsx';
6
+ import type { ChatTableContentProps } from './ChatTable.tsx';
7
+
8
+ interface ChatBlockOptions {
9
+ TableComponent: ComponentType<ChatTableContentProps>;
10
+ tableClassName?: string;
11
+ tableScrollIndicatorOffset?: number;
12
+ }
13
+
14
+ /*
15
+ * Render one block as a semantic sibling, keeping tables and code outside paragraph elements.
16
+ */
17
+ const ChatBlock = memo((p: { block: ContentBlock } & ChatBlockOptions) => {
18
+ const { block, TableComponent } = p;
19
+ switch (block.kind) {
20
+ case 'paragraph':
21
+ return (
22
+ <p className='ofw'>
23
+ <ChatInline nodes={block.children} />
24
+ </p>
25
+ );
26
+ case 'heading': {
27
+ const Heading = `h${block.level}` as const;
28
+ return (
29
+ <Heading className='ofw'>
30
+ <ChatInline nodes={block.children} />
31
+ </Heading>
32
+ );
33
+ }
34
+ case 'list': {
35
+ const List = block.ordered ? 'ol' : 'ul';
36
+ return (
37
+ <List
38
+ start={block.ordered ? block.startNumber : undefined}
39
+ style={{ paddingInlineStart: '1.5em', marginBlock: '0.7rem', listStyleType: block.ordered ? 'decimal' : 'disc' }}
40
+ >
41
+ {block.items.map((item) => (
42
+ <li key={item.start}>
43
+ <ChatBlocks
44
+ blocks={item.blocks}
45
+ TableComponent={TableComponent}
46
+ tableClassName={p.tableClassName}
47
+ tableScrollIndicatorOffset={p.tableScrollIndicatorOffset}
48
+ />
49
+ </li>
50
+ ))}
51
+ </List>
52
+ );
53
+ }
54
+ case 'code':
55
+ return <ChatCodeBlock block={block} />;
56
+ case 'table':
57
+ return (
58
+ <TableComponent
59
+ block={block}
60
+ className={p.tableClassName}
61
+ scrollIndicatorOffset={p.tableScrollIndicatorOffset}
62
+ />
63
+ );
64
+ case 'literal':
65
+ return (
66
+ <pre
67
+ className='ofw max_w_f pre_wrap'
68
+ style={{
69
+ fontFamily: 'inherit',
70
+ marginBlock: '0.7rem',
71
+ }}
72
+ >
73
+ <span>{block.source}</span>
74
+ </pre>
75
+ );
76
+ }
77
+ });
78
+
79
+ /*
80
+ * Render stable block siblings without a text-keyed root or another paragraph wrapper.
81
+ */
82
+ export const ChatBlocks = memo((p: { blocks: ContentBlock[] } & ChatBlockOptions) => {
83
+ return p.blocks.map((block) => (
84
+ <ChatBlock
85
+ key={getContentBlockKey(block)}
86
+ block={block}
87
+ TableComponent={p.TableComponent}
88
+ tableClassName={p.tableClassName}
89
+ tableScrollIndicatorOffset={p.tableScrollIndicatorOffset}
90
+ />
91
+ ));
92
+ });
@@ -0,0 +1,16 @@
1
+ import { memo } from 'react';
2
+ import type { CodeBlock } from '../types/content.ts';
3
+
4
+ /*
5
+ * Keep code and unpromoted CSV readable, including fence text for incomplete or rejected CSV data.
6
+ */
7
+ export const ChatCodeBlock = memo((p: { block: CodeBlock }) => {
8
+ const { block } = p;
9
+ return (
10
+ <pre className='bg_alt r_sm p_sm my_sm ofw max_w_f pre_wrap'>
11
+ <code>
12
+ <span>{block.language.toLowerCase() === 'csv' || block.fallbackReason ? block.source : block.body}</span>
13
+ </code>
14
+ </pre>
15
+ );
16
+ });
@@ -0,0 +1,20 @@
1
+ import { nameToEmoji } from '@jsb188/app/constants/emoji.ts';
2
+ import type { InlineNode } from '../types/content.ts';
3
+
4
+ /*
5
+ * Resolve displayed inline text consistently for rendering and CSV export, preserving unsupported emoji names literally.
6
+ */
7
+ export function getChatInlineText(node: InlineNode): string {
8
+ switch (node.kind) {
9
+ case 'text':
10
+ case 'code':
11
+ return node.value;
12
+ case 'break':
13
+ return '\n';
14
+ case 'emoji':
15
+ return Object.hasOwn(nameToEmoji, node.name) ? nameToEmoji[node.name] : node.source;
16
+ case 'style':
17
+ case 'link':
18
+ return node.children.map(getChatInlineText).join('');
19
+ }
20
+ }
@@ -0,0 +1,89 @@
1
+ import parseEmojiText from '@jsb188/app/utils/emoji.ts';
2
+ import { memo } from 'react';
3
+ import type { InlineNode } from '../types/content.ts';
4
+ import { getChatInlineText } from './ChatInline.helpers.ts';
5
+
6
+ /*
7
+ * Apply the shared deterministic emoji font while keeping text nodes protected from browser translation rewrites.
8
+ */
9
+ const ChatText = memo((p: { text: string }) => {
10
+ const style = /[\r\n]/.test(p.text) ? { whiteSpace: 'pre-wrap' as const } : undefined;
11
+ return parseEmojiText(p.text).chunks.map((chunk, index) => (
12
+ <span
13
+ key={index}
14
+ className={typeof chunk === 'string' ? undefined : 'noto_emoji'}
15
+ style={style}
16
+ >
17
+ {typeof chunk === 'string' ? chunk : chunk[2]}
18
+ </span>
19
+ ));
20
+ });
21
+
22
+ /*
23
+ * Render one typed inline node without interpreting model-provided HTML or class names.
24
+ */
25
+ const ChatInlineNode = memo((p: { node: InlineNode }) => {
26
+ const { node } = p;
27
+ switch (node.kind) {
28
+ case 'break':
29
+ return <br />;
30
+ case 'code':
31
+ return (
32
+ <code className='bg_alt r_4 px_4 ofw pre_wrap'>
33
+ <span>{node.value}</span>
34
+ </code>
35
+ );
36
+ case 'link': {
37
+ const external = /^(?:https?:)?\/\//i.test(node.destination);
38
+ return (
39
+ <a
40
+ href={node.destination}
41
+ target={external ? '_blank' : undefined}
42
+ rel={external ? 'noopener noreferrer' : undefined}
43
+ >
44
+ <ChatInline nodes={node.children} />
45
+ </a>
46
+ );
47
+ }
48
+ case 'style': {
49
+ const Tag = node.styles.includes('italic') ? 'i' : 'span';
50
+ const className =
51
+ [
52
+ node.styles.includes('medium') && 'ft_medium',
53
+ node.styles.includes('semibold') && 'ft_semibold',
54
+ node.styles.includes('underline') && 'u',
55
+ node.styles.includes('strike') && 'strikethrough',
56
+ node.styles.includes('highlight') && 'em_text',
57
+ ]
58
+ .filter(Boolean)
59
+ .join(' ') || undefined;
60
+ const children = <ChatInline nodes={node.children} />;
61
+ return (
62
+ <Tag className={className}>{node.styles.includes('highlight') ? <span className='rel z1'>{children}</span> : children}</Tag>
63
+ );
64
+ }
65
+ }
66
+ });
67
+
68
+ /*
69
+ * Render supported inline content in source order, keeping text memoized without an extra component for each table cell.
70
+ */
71
+ export const ChatInline = memo((p: { nodes: InlineNode[] }) => {
72
+ return p.nodes.map((node) => {
73
+ const key = `${node.kind}:${node.start}`;
74
+ if (node.kind === 'text' || node.kind === 'emoji') {
75
+ return (
76
+ <ChatText
77
+ key={key}
78
+ text={getChatInlineText(node)}
79
+ />
80
+ );
81
+ }
82
+ return (
83
+ <ChatInlineNode
84
+ key={key}
85
+ node={node}
86
+ />
87
+ );
88
+ });
89
+ });
@@ -0,0 +1,37 @@
1
+ import { escapeCSVValue } from '@jsb188/app/utils/string.ts';
2
+ import type { TableBlock } from '../types/content.ts';
3
+ import { getChatInlineText } from './ChatInline.helpers.ts';
4
+
5
+ /*
6
+ * Preserve original CSV text or serialize the committed Markdown table cells as CSV.
7
+ * Quote empty fields so even a single empty cell remains a record when imported.
8
+ */
9
+ export function getChatTableCsv(block: TableBlock): string {
10
+ if (block.origin === 'csv' && block.csvSource !== undefined) return block.csvSource;
11
+ const rows = [block.headers, ...block.rows.map((row) => row.cells)].map((cells) =>
12
+ cells
13
+ .map((cell) => {
14
+ const value = cell.kind === 'literal' ? cell.value : cell.children.map(getChatInlineText).join('');
15
+ return value === '' ? '""' : escapeCSVValue(value);
16
+ })
17
+ .join(','),
18
+ );
19
+ return `${rows.join('\r\n')}\r\n`;
20
+ }
21
+
22
+ /*
23
+ * Download a UTF-8 CSV text file locally and release its temporary URL after the browser starts the download.
24
+ */
25
+ export function downloadChatTableCsv(block: TableBlock) {
26
+ const url = URL.createObjectURL(new Blob([getChatTableCsv(block)], { type: 'text/csv;charset=utf-8' }));
27
+ const anchor = document.createElement('a');
28
+ anchor.href = url;
29
+ anchor.download = 'chat-table.csv';
30
+ document.body.appendChild(anchor);
31
+ try {
32
+ anchor.click();
33
+ } finally {
34
+ anchor.remove();
35
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
36
+ }
37
+ }