@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,816 @@
1
+ import { getContainedDimensions } from '@jsb188/app/utils/number.ts';
2
+ import { cn, truncateFileName } from '@jsb188/app/utils/string.ts';
3
+ import { makeUploadsUrl } from '@jsb188/app/utils/url_client.ts';
4
+ import { parseAIChatAttachmentText } from '@jsb188/mday/utils/storage.ts';
5
+ import { DashedBackgroundArea } from '@jsb188/react-web/modules/DashedBackgroundArea';
6
+ import { TooltipButton } from '@jsb188/react-web/modules/PopOver';
7
+ import { getFileTypeIconName, Icon } from '@jsb188/react-web/svgs/Icon';
8
+ import type { ReactDivElement } from '@jsb188/react-web/types/dom.d';
9
+ import { AvatarImg } from '@jsb188/react-web/ui/Avatar';
10
+ import { ProgressCircle } from '@jsb188/react-web/ui/Loading';
11
+ import Markdown, { type RenderMappedCodeFn } from '@jsb188/react-web/ui/Markdown';
12
+ import { createContext, forwardRef, Fragment, memo, useContext } from 'react';
13
+ import { createPortal } from 'react-dom';
14
+ import { AIChatContent } from '../modules/AIChatContent';
15
+ import type { ChatContentStatus } from '../types/content';
16
+ import './AIChatUI.css';
17
+
18
+ /**
19
+ * Constants
20
+ */
21
+
22
+ const FILE_CONSTANTS = {
23
+ textareaDropOverlayStyle: { background: 'rgba(var(--color-bg), 0.75)' },
24
+ };
25
+
26
+ /**
27
+ * Max-width class paired with .cw on every chat bubble and composer; a screen that embeds
28
+ * the chat in a different column width provides its own class (default: 780px .df)
29
+ */
30
+
31
+ export const AIChatWidthContext = createContext('df');
32
+
33
+ /*
34
+ * Return the max-width class for chat bubbles and the composer in the current chat column.
35
+ */
36
+ export function useAIChatWidthClassName() {
37
+ return useContext(AIChatWidthContext);
38
+ }
39
+
40
+ /**
41
+ * Element a screen provides to render the chat composer outside its message scroller (e.g. so the scroller can
42
+ * fade out with a mask); null while that element is mounting. Without a provider, the composer renders inline.
43
+ */
44
+
45
+ export const AIChatTextareaSlotContext = createContext<HTMLElement | null | undefined>(undefined);
46
+
47
+ /*
48
+ * Render the chat composer into the screen's textarea slot when one is provided, or inline otherwise.
49
+ */
50
+ export function AIChatTextareaPortal(p: { children: React.ReactNode }) {
51
+ const slotEl = useContext(AIChatTextareaSlotContext);
52
+
53
+ if (slotEl === undefined) {
54
+ return p.children;
55
+ }
56
+
57
+ // Render nothing until the slot mounts, so the composer never mounts inline and then moves (and remounts)
58
+ return slotEl ? createPortal(p.children, slotEl) : null;
59
+ }
60
+
61
+ /**
62
+ * Types
63
+ */
64
+
65
+ export interface MessageContainerProps extends ReactDivElement {
66
+ domId?: string;
67
+ className: string;
68
+ isSelf: boolean;
69
+ isMock: boolean;
70
+ showAvatar: boolean;
71
+ errored: boolean;
72
+ spaced: boolean;
73
+ bubble: boolean;
74
+ multiLined: boolean;
75
+ children: React.ReactNode;
76
+ }
77
+
78
+ interface MessageOptionsProps {
79
+ id?: string;
80
+ hidden: boolean;
81
+ showAvatar: boolean;
82
+ displayName: string;
83
+ photoUri: string;
84
+ }
85
+
86
+ interface MessagesMockProps {
87
+ longer?: boolean;
88
+ size?: number;
89
+ }
90
+
91
+ interface MockMessageBubblesProps {
92
+ longer?: boolean;
93
+ isSelf?: boolean;
94
+ }
95
+
96
+ /**
97
+ * Chat timeline separator shown before the first message from a local calendar day.
98
+ */
99
+
100
+ export const AIChatDateSeparator = memo((p: { dayText: string; timeText: string }) => {
101
+ return (
102
+ <div className='v_center py_md mt_md ft_xs cl_lt'>
103
+ <span>
104
+ <span className='ft_medium'>{p.dayText}</span> {p.timeText}
105
+ </span>
106
+ </div>
107
+ );
108
+ });
109
+
110
+ AIChatDateSeparator.displayName = 'AIChatDateSeparator';
111
+
112
+ /**
113
+ * Chat message; container
114
+ */
115
+
116
+ export const MessageContainer = memo(
117
+ forwardRef((p: Partial<MessageContainerProps>, ref: React.ForwardedRef<HTMLDivElement>) => {
118
+ const { domId, isSelf, showAvatar, bubble, isMock, errored, spaced, multiLined, className, ...other } = p;
119
+
120
+ return (
121
+ <div
122
+ ref={ref}
123
+ id={domId}
124
+ className={cn(
125
+ 'mdy_msg_item',
126
+ !errored && (isSelf ? 'self' : bubble ? 'bubble' : 'other'),
127
+ isMock && !isSelf && 'mocked',
128
+ !showAvatar && 'no_avatar',
129
+ errored ? 'err' : 'active',
130
+ spaced && 'mt_md',
131
+ multiLined && 'multilined',
132
+ className,
133
+ )}
134
+ {...other}
135
+ />
136
+ );
137
+ }),
138
+ );
139
+
140
+ MessageContainer.displayName = 'MessageContainer';
141
+
142
+ /**
143
+ * Mock left side (other's) messages
144
+ */
145
+
146
+ function MockMessageBubbles(p: MockMessageBubblesProps) {
147
+ const { longer, isSelf } = p;
148
+ const widthClassName = useAIChatWidthClassName();
149
+
150
+ let mockTexts;
151
+ if (isSelf && longer) {
152
+ mockTexts = [
153
+ {
154
+ top: true,
155
+ bottom: true,
156
+ texts: ['..... ..... ..... .....', '..... ..... ..... .....'],
157
+ },
158
+ ];
159
+ } else if (isSelf) {
160
+ mockTexts = [
161
+ {
162
+ top: true,
163
+ bottom: true,
164
+ texts: ['.... ....', '.... ....'],
165
+ },
166
+ ];
167
+ } else if (longer) {
168
+ mockTexts = [
169
+ {
170
+ top: true,
171
+ texts: ['... ... ... ... ... ... ... ... ... ...', '... ... ... ... ... ... ... ..', '... ... ... ... ... ... ... ... ...'],
172
+ },
173
+ {
174
+ texts: [
175
+ '... ...',
176
+ '... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ..',
177
+ '... ... ... ... ... ...',
178
+ '... ... ... ... ... ..',
179
+ ],
180
+ },
181
+ {
182
+ texts: ['... ... ... ... ... ..', '... ... ... ... ... ... ... ... ...', '... ... ... ... ... ... ... ... ...'],
183
+ },
184
+ {
185
+ texts: [
186
+ '... ... ... ... ... ... ... ... .....',
187
+ '... ... ... ... ...',
188
+ '... ... ... ... ... ... .....',
189
+ '... ... ... ... ...',
190
+ '... ...',
191
+ ],
192
+ },
193
+ {
194
+ texts: ['... ... ... ... ... ... ... ... ... ... ... ... ....', '... ... ... ... ....', '... ... ... ... ....'],
195
+ },
196
+ {
197
+ bottom: true,
198
+ texts: ['... ... ... ..', '... ... ... ... ... ... ... ... ...', '... ... ... ..', '... ... ... ... ... ... ... ... ...'],
199
+ },
200
+ ];
201
+ } else {
202
+ mockTexts = [
203
+ {
204
+ top: true,
205
+ texts: ['... ... ... ... ... ... ... ... ... ... ...', '... ... ... ... ...', '... ... ... ... ... ... ... ... ... ...'],
206
+ },
207
+ {
208
+ texts: ['... ... ...', '... ... ... ... ... ... ... ... ... ... ... ... ...', '... ... ...', '... ... ...'],
209
+ },
210
+ {
211
+ texts: ['... ... ... ... ... ... ... ... ... ...', '... ... ... ... ... ... ... ...', '... ... ... ... ... ... ... ...'],
212
+ },
213
+ {
214
+ bottom: true,
215
+ texts: ['... ... ... ... ... ... ... ... ...', '... ... ...', '... ... ... ... ... ... ...'],
216
+ },
217
+ ];
218
+ }
219
+
220
+ return mockTexts.map((obj, i) => {
221
+ const { top, bottom, texts } = obj;
222
+ const lastMockTextIx = texts.length - 1;
223
+
224
+ return (
225
+ <MessageContainer
226
+ key={i}
227
+ className={cn('cw', widthClassName)}
228
+ isSelf={isSelf}
229
+ isMock
230
+ spaced={top}
231
+ >
232
+ <div className={cn('mdy_msg', isSelf ? 'bg_alt' : '', top ? 'top' : '', bottom ? 'bottom' : '')}>
233
+ {texts.map((mockText, ii) => (
234
+ <span
235
+ className={cn('active mock ft_xs', ii !== lastMockTextIx ? 'mr_xs' : '')}
236
+ key={ii}
237
+ >
238
+ {mockText}
239
+ </span>
240
+ ))}
241
+ </div>
242
+ </MessageContainer>
243
+ );
244
+ });
245
+ }
246
+
247
+ /**
248
+ * Chat message; mock
249
+ * NOTE: The container is "column-reversed" so the UI is reversed too.
250
+ */
251
+
252
+ export function MessagesMock(p: MessagesMockProps) {
253
+ const { longer } = p;
254
+ const size = p.size ?? 1; // allow 0 for "nothing" state until the window calculation is ready from outer component
255
+
256
+ return Array.from({ length: size }, (_, i) => (
257
+ <Fragment key={i}>
258
+ <MockMessageBubbles
259
+ longer={longer}
260
+ isSelf
261
+ />
262
+ <MockMessageBubbles longer={longer} />
263
+ </Fragment>
264
+ ));
265
+ }
266
+
267
+ /**
268
+ * Chat message; body
269
+ */
270
+
271
+ interface MessageBodyProps {
272
+ contentStatus?: ChatContentStatus;
273
+ presetClassName?: string;
274
+ className?: string;
275
+ backgroundClassName?: string;
276
+ errored?: boolean;
277
+ isSelf?: boolean;
278
+ top?: boolean;
279
+ bottom?: boolean;
280
+ bubble?: boolean;
281
+ children: string;
282
+ codesMap?: [string, string][];
283
+ MappedCodeComponent?: RenderMappedCodeFn;
284
+ LastComponent?: React.ReactNode;
285
+ }
286
+
287
+ export const MessageBody = memo((p: MessageBodyProps) => {
288
+ const {
289
+ children,
290
+ contentStatus,
291
+ presetClassName = 'mdy_msg',
292
+ className,
293
+ backgroundClassName,
294
+ isSelf,
295
+ bubble,
296
+ errored,
297
+ top,
298
+ bottom,
299
+ codesMap,
300
+ MappedCodeComponent,
301
+ LastComponent,
302
+ } = p;
303
+
304
+ let bgClassName;
305
+ if (backgroundClassName) {
306
+ bgClassName = backgroundClassName;
307
+ } else if (isSelf) {
308
+ bgClassName = errored ? 'bg_err_md' : 'bg_self';
309
+ } else if (bubble) {
310
+ bgClassName = 'bg_alt';
311
+ }
312
+
313
+ return (
314
+ <div
315
+ className={cn(
316
+ presetClassName,
317
+ className,
318
+ bgClassName,
319
+ contentStatus && 'ofw',
320
+ errored ? 'cl_err' : '',
321
+ top ? 'top' : '',
322
+ bottom ? 'bottom' : '',
323
+ )}
324
+ style={contentStatus ? { minWidth: 0, flex: '1 1 0%' } : undefined}
325
+ >
326
+ {contentStatus && !isSelf ? (
327
+ <>
328
+ <AIChatContent
329
+ text={children}
330
+ status={contentStatus}
331
+ />
332
+ {LastComponent ? <div>{LastComponent}</div> : null}
333
+ </>
334
+ ) : (
335
+ <Markdown
336
+ as='p'
337
+ preset={isSelf ? undefined : 'article'}
338
+ codesMap={codesMap}
339
+ MappedCodeComponent={MappedCodeComponent}
340
+ LastComponent={LastComponent}
341
+ >
342
+ {children}
343
+ </Markdown>
344
+ )}
345
+ </div>
346
+ );
347
+ });
348
+
349
+ MessageBody.displayName = 'MessageBody';
350
+
351
+ /**
352
+ * Chat message; options with avatar
353
+ */
354
+
355
+ export const MessageOptions = memo((p: Partial<MessageOptionsProps>) => {
356
+ const { showAvatar, hidden, displayName, photoUri } = p;
357
+
358
+ return (
359
+ <div className={cn('mdy_msg_opts', hidden ? 'cmo_hidden' : '')}>
360
+ {!showAvatar || hidden ? null : (
361
+ <AvatarImg
362
+ size='small'
363
+ displayName={displayName}
364
+ urlPath={photoUri}
365
+ />
366
+ )}
367
+ </div>
368
+ );
369
+ });
370
+
371
+ MessageOptions.displayName = 'MessageOptions';
372
+
373
+ /**
374
+ * Loading indicator for AI chat (message body text)
375
+ */
376
+
377
+ export const AIGeneratingIndicator = memo(() => {
378
+ return (
379
+ <span className='cm_loader_cnt'>
380
+ <span className='cm_loader' />
381
+ </span>
382
+ );
383
+ });
384
+
385
+ AIGeneratingIndicator.displayName = 'AIGeneratingIndicator';
386
+
387
+ /**
388
+ * AI chat textarea
389
+ */
390
+
391
+ export const MessageTextarea = memo(
392
+ (p: {
393
+ notReady?: boolean;
394
+ locked?: boolean;
395
+ disabled?: boolean;
396
+ formIsEnabled?: boolean;
397
+ submitTooltipMessage?: string;
398
+ generating: boolean;
399
+ // Reflows the input onto its own row with the buttons beneath it once the
400
+ // text wraps past a single line
401
+ expanded?: boolean;
402
+ className?: string;
403
+ LeftComponent?: React.ReactNode;
404
+ AboveInputComponent?: React.ReactNode;
405
+ children: React.ReactNode;
406
+ onSubmit: (e: React.MouseEvent) => void;
407
+ onStopGenerating: (e: React.MouseEvent) => void;
408
+ }) => {
409
+ const {
410
+ className,
411
+ notReady,
412
+ locked,
413
+ disabled,
414
+ formIsEnabled,
415
+ submitTooltipMessage,
416
+ generating,
417
+ expanded,
418
+ LeftComponent,
419
+ AboveInputComponent,
420
+ children,
421
+ onSubmit,
422
+ onStopGenerating,
423
+ } = p;
424
+
425
+ const submitButton = (
426
+ <button
427
+ type='button'
428
+ onClick={generating ? onStopGenerating : onSubmit}
429
+ disabled={!generating && (!formIsEnabled || disabled)}
430
+ className={cn(
431
+ 'cm_btn r v_center of trans_color spd_1 no_shrink',
432
+ !submitTooltipMessage && 'cm_btn_right',
433
+ formIsEnabled || generating ? 'bg_primary link' : 'bg_alt cl_lt',
434
+ locked ? 'shift_up' : generating ? 'ic_sm' : '',
435
+ )}
436
+ >
437
+ {notReady ? null : <Icon name={locked ? 'lock-filled' : generating ? 'player-stop-filled' : 'arrow-up'} />}
438
+ </button>
439
+ );
440
+
441
+ return (
442
+ <div className={cn('cm_textarea_cnt z4 mdy_msg bg', className)}>
443
+ <div className={cn('cm_textarea_body', expanded && 'expanded')}>
444
+ {AboveInputComponent}
445
+ {children}
446
+ {LeftComponent}
447
+
448
+ {submitTooltipMessage ? (
449
+ <TooltipButton
450
+ as='span'
451
+ className='cm_btn_right'
452
+ tooltipClassName='a_c'
453
+ position='top'
454
+ offsetY={-6}
455
+ message={submitTooltipMessage}
456
+ >
457
+ {submitButton}
458
+ </TooltipButton>
459
+ ) : (
460
+ submitButton
461
+ )}
462
+ </div>
463
+ </div>
464
+ );
465
+ },
466
+ );
467
+
468
+ MessageTextarea.displayName = 'MessageTextarea';
469
+
470
+ /**
471
+ * AI chat textarea; row of attached file chips shown above the input
472
+ */
473
+
474
+ export const TextareaAttachmentsRow = memo((p: { children: React.ReactNode }) => {
475
+ return <div className='rel z1 h_wrap gap_5 pt_6 pb_4 px_6'>{p.children}</div>;
476
+ });
477
+
478
+ TextareaAttachmentsRow.displayName = 'TextareaAttachmentsRow';
479
+
480
+ /**
481
+ * AI chat textarea; thumbnail for an attached file and its upload progress.
482
+ */
483
+
484
+ const TextareaAttachmentThumbnail = memo(
485
+ (p: {
486
+ name: string;
487
+ contentType: string;
488
+ sizingClassName?: string;
489
+ isImage: boolean;
490
+ previewUrl?: string | null;
491
+ progress?: number | null;
492
+ errored?: boolean;
493
+ }) => {
494
+ const { name, contentType, isImage, previewUrl, progress, errored, sizingClassName } = p;
495
+ const uploading = typeof progress === 'number';
496
+ const hasThumbnail = isImage && previewUrl;
497
+ const previewOpacityClassName = errored || uploading ? 'op_50' : '';
498
+
499
+ return (
500
+ <span className={cn('rel v_center bg_strong no_shrink of', hasThumbnail ? 'link' : '', sizingClassName ?? 'w_56 h_56 r_smw')}>
501
+ {hasThumbnail ? (
502
+ <img
503
+ src={previewUrl}
504
+ alt={name}
505
+ className={cn('w_100_pc h_100_pc img_cover trans_op noclick unsel', previewOpacityClassName)}
506
+ />
507
+ ) : (
508
+ <span className={cn('abs_full v_center trans_op', previewOpacityClassName)}>
509
+ <Icon name={getFileTypeIconName(contentType, name)} />
510
+ </span>
511
+ )}
512
+
513
+ {uploading && (
514
+ <span className='abs_full z1 v_center'>
515
+ <ProgressCircle
516
+ className='cl_primary'
517
+ trackColor='rgba(var(--color-bg), .3)'
518
+ percent={progress}
519
+ size={22}
520
+ strokeWidth={4}
521
+ />
522
+ </span>
523
+ )}
524
+ </span>
525
+ );
526
+ },
527
+ );
528
+
529
+ TextareaAttachmentThumbnail.displayName = 'TextareaAttachmentThumbnail';
530
+
531
+ /**
532
+ * AI chat textarea; one attached file chip with upload progress and remove
533
+ */
534
+
535
+ export const TextareaAttachmentChip = memo(
536
+ (p: {
537
+ name: string;
538
+ contentType: string;
539
+ previewUrl?: string | null;
540
+ progress?: number | null;
541
+ errored?: boolean;
542
+ onRemove: () => void;
543
+ }) => {
544
+ const { name, contentType, previewUrl, progress, errored, onRemove } = p;
545
+ const isImage = contentType.startsWith('image/');
546
+ const hasMediaPreview = isImage && !!previewUrl;
547
+ const uploading = typeof progress === 'number';
548
+
549
+ return (
550
+ <div className={cn('rel no_shrink anim_appear on_mount', hasMediaPreview ? '' : 'h_item gap_xs p_5 r_smw bg_alt')}>
551
+ <TextareaAttachmentThumbnail
552
+ name={name}
553
+ sizingClassName={hasMediaPreview ? undefined : 'w_46 h_46 r_sm'}
554
+ contentType={contentType}
555
+ isImage={isImage}
556
+ previewUrl={previewUrl}
557
+ progress={progress}
558
+ errored={errored}
559
+ />
560
+
561
+ {hasMediaPreview ? null : <span className='ft_xs'>{truncateFileName(name, 20)}</span>}
562
+
563
+ {!uploading ? (
564
+ <button
565
+ type='button'
566
+ className={cn(
567
+ 'link w_18 h_18 ic_xs r v_center no_shrink',
568
+ hasMediaPreview ? 'abs_tr z2 bg_contrast_hv cl_solid mt_3 mr_3' : 'mx_2 bg_active bg_medium_hv',
569
+ errored ? 'bg_err bd_1 bd_solid' : hasMediaPreview ? 'bg_darker_5 bd_1 bd_lighter_3' : '',
570
+ )}
571
+ onClick={onRemove}
572
+ >
573
+ <Icon name='x' />
574
+ </button>
575
+ ) : !hasMediaPreview ? (
576
+ <span />
577
+ ) : null}
578
+ </div>
579
+ );
580
+ },
581
+ );
582
+
583
+ TextareaAttachmentChip.displayName = 'TextareaAttachmentChip';
584
+
585
+ /**
586
+ * AI chat textarea; full screen overlay shown while dragging files over the window
587
+ */
588
+
589
+ export const TextareaDropOverlay = memo((p: { text: string }) => {
590
+ return (
591
+ <div
592
+ className='fixed_full z10 v_center bg_blur_sm anim_appear on_mount spd_2'
593
+ style={FILE_CONSTANTS.textareaDropOverlayStyle}
594
+ >
595
+ <DashedBackgroundArea
596
+ className='fs v_center m_md z1 fade_bg_radial'
597
+ contentClassName='h_center gap_sm ic_lg'
598
+ radius={10}
599
+ strokeColor='rgba(var(--color-darker-rgb), .05)'
600
+ strokeWidth={6}
601
+ dashLength={12}
602
+ dashGapMultiplier={3}
603
+ >
604
+ <Icon name='drag-drop-hand' />
605
+ <span className='ft_lg ls_5 shift_down'>{p.text}</span>
606
+ </DashedBackgroundArea>
607
+ </div>
608
+ );
609
+ });
610
+
611
+ TextareaDropOverlay.displayName = 'TextareaDropOverlay';
612
+
613
+ /*
614
+ * Render a storage-backed attachment or fall back to the legacy message text
615
+ * format "oaiFileId;storageUri;fileName".
616
+ */
617
+
618
+ export const MessageAttachmentBody = memo(
619
+ (p: {
620
+ className?: string;
621
+ isSelf?: boolean;
622
+ bubble?: boolean;
623
+ top?: boolean;
624
+ bottom?: boolean;
625
+ type: string;
626
+ text: string;
627
+ file?: {
628
+ contentType: string;
629
+ imageData?: {
630
+ width: number;
631
+ height: number;
632
+ averageColor?: string | null;
633
+ } | null;
634
+ name?: string | null;
635
+ uri: string;
636
+ previewUrl?: string | null;
637
+ } | null;
638
+ onClick?: () => void;
639
+ }) => {
640
+ const { className, top, bottom, type, text, file, onClick } = p;
641
+ const legacyFile = parseAIChatAttachmentText(text);
642
+ const fileUri = file?.uri || legacyFile.fileUri;
643
+ const fileName = file?.name || legacyFile.fileName;
644
+ const isImage = file ? file.contentType.startsWith('image/') : type === 'IMAGE' || type === 'SMS_IMAGE';
645
+ const displayName = fileName || (fileUri ? fileUri.substring(fileUri.lastIndexOf('/') + 1) : '');
646
+ const fileExtension = displayName.includes('.') ? displayName.substring(displayName.lastIndexOf('.') + 1).toUpperCase() : '';
647
+ const imageDimensions = file?.imageData ? getContainedDimensions(file.imageData.width, file.imageData.height, 320, 320) : null;
648
+ const averageColor = file?.imageData?.averageColor;
649
+
650
+ if (isImage && fileUri) {
651
+ // Use the image-specific background rather than the standard message bubble background
652
+ return (
653
+ <button
654
+ type='button'
655
+ className={cn('mdy_msg p_n link of no_shrink', className, top ? 'top' : '', bottom ? 'bottom' : '')}
656
+ style={imageDimensions || undefined}
657
+ onClick={onClick}
658
+ disabled={!onClick}
659
+ >
660
+ <div
661
+ className={cn('w_f h_f v_center', !averageColor && 'bg_contrast')}
662
+ style={averageColor ? { backgroundColor: averageColor } : undefined}
663
+ >
664
+ <img
665
+ src={file?.previewUrl || makeUploadsUrl(fileUri, 'medium')}
666
+ alt={displayName}
667
+ className='img_auto img_contain max_w_320 max_h_320 noclick unsel'
668
+ />
669
+ </div>
670
+ </button>
671
+ );
672
+ }
673
+
674
+ return (
675
+ <button
676
+ type='button'
677
+ className={cn('mdy_msg attachment_file link bg_alt_hv', className, top ? 'top' : '', bottom ? 'bottom' : '')}
678
+ onClick={onClick}
679
+ disabled={!onClick}
680
+ >
681
+ <div className='h_item gap_sm p_xs r_sm bd_1 bd_lt bg bg_alt_hv max_w_330'>
682
+ <span className='w_46 h_46 r_xs bg_fade bd_1 bd_lt v_center ic_df no_shrink'>
683
+ <Icon
684
+ name={getFileTypeIconName('', displayName)}
685
+ tryColor
686
+ />
687
+ </span>
688
+
689
+ <span className='ellip_cnt ft_sm f lh_2 shift_down'>
690
+ <span className='ellip ft_medium'>{displayName}</span>
691
+ {fileExtension ? <span className='bl cl_md'>{fileExtension}</span> : null}
692
+ </span>
693
+ </div>
694
+ </button>
695
+ );
696
+ },
697
+ );
698
+
699
+ MessageAttachmentBody.displayName = 'MessageAttachmentBody';
700
+
701
+ /*
702
+ * Render multiple image attachments from one message in a compact gallery.
703
+ */
704
+
705
+ export const MessageImageAttachmentGroup = memo(
706
+ (p: {
707
+ top?: boolean;
708
+ bottom?: boolean;
709
+ files: {
710
+ id: string;
711
+ imageData?: {
712
+ averageColor?: string | null;
713
+ } | null;
714
+ name?: string | null;
715
+ uri: string;
716
+ previewUrl?: string | null;
717
+ }[];
718
+ onClick: (fileId: string) => void;
719
+ }) => {
720
+ const { top, bottom, files, onClick } = p;
721
+ const columnCount = Math.min(files.length, 3);
722
+
723
+ return (
724
+ <div
725
+ className={cn('mdy_msg attachment_grp h_wrap gap_4', top ? 'top' : '', bottom ? 'bottom' : '')}
726
+ style={{
727
+ justifyContent: 'flex-end',
728
+ width: `${columnCount * 140 + (columnCount - 1) * 8}px`,
729
+ }}
730
+ >
731
+ {files.map((file) => (
732
+ <button
733
+ key={file.id}
734
+ type='button'
735
+ className={cn(
736
+ 'link attachment_btn p_n of w_140 h_140 no_shrink no_link op_90_hv',
737
+ !file.imageData?.averageColor && 'bg_contrast',
738
+ )}
739
+ style={file.imageData?.averageColor ? { backgroundColor: file.imageData.averageColor } : undefined}
740
+ onClick={() => onClick(file.id)}
741
+ >
742
+ <img
743
+ src={file.previewUrl || makeUploadsUrl(file.uri, 'medium')}
744
+ alt={file.name || file.uri.substring(file.uri.lastIndexOf('/') + 1)}
745
+ className='img_auto noclick unsel'
746
+ />
747
+ </button>
748
+ ))}
749
+ </div>
750
+ );
751
+ },
752
+ );
753
+
754
+ MessageImageAttachmentGroup.displayName = 'MessageImageAttachmentGroup';
755
+
756
+ /**
757
+ * Memoized content editable so the text area content *never* resets
758
+ */
759
+
760
+ export const MemoizedContentEditable = memo(
761
+ forwardRef(
762
+ (
763
+ p: {
764
+ domId: string;
765
+ notReady?: boolean;
766
+ locked?: boolean;
767
+ disabled?: boolean;
768
+ __html: string;
769
+ placeholder?: string;
770
+ onKeyDown: (e: React.KeyboardEvent) => void;
771
+ onKeyUp: (e: React.KeyboardEvent) => void;
772
+ onInput: (e: React.FormEvent) => void;
773
+ onPaste: (e: React.ClipboardEvent) => void;
774
+ onCompositionStart?: (e: React.CompositionEvent) => void;
775
+ onCompositionEnd?: (e: React.CompositionEvent) => void;
776
+ },
777
+ ref: React.ForwardedRef<HTMLDivElement>,
778
+ ) => {
779
+ const {
780
+ domId,
781
+ __html,
782
+ placeholder,
783
+ notReady,
784
+ locked,
785
+ disabled,
786
+ onKeyDown,
787
+ onKeyUp,
788
+ onInput,
789
+ onPaste,
790
+ onCompositionStart,
791
+ onCompositionEnd,
792
+ } = p;
793
+
794
+ return (
795
+ <div
796
+ ref={ref}
797
+ id={domId}
798
+ className='cm_textarea stock y_scr subtle f'
799
+ role='textbox'
800
+ tabIndex={0}
801
+ contentEditable={locked || disabled ? 'false' : 'true'}
802
+ data-placeholder={notReady ? undefined : placeholder}
803
+ onKeyDown={onKeyDown}
804
+ onKeyUp={onKeyUp}
805
+ onInput={onInput}
806
+ onPaste={onPaste}
807
+ onCompositionStart={onCompositionStart}
808
+ onCompositionEnd={onCompositionEnd}
809
+ dangerouslySetInnerHTML={{ __html }}
810
+ />
811
+ );
812
+ },
813
+ ),
814
+ );
815
+
816
+ MemoizedContentEditable.displayName = 'MemoizedContentEditable';