@widgetic/chat 0.1.45 → 0.1.48

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 (69) hide show
  1. package/README.md +148 -20
  2. package/dist/components/AttachmentDisplay.svelte +22 -130
  3. package/dist/components/AttachmentPreviewDialog.svelte +428 -0
  4. package/dist/components/AttachmentPreviewDialog.svelte.d.ts +14 -0
  5. package/dist/components/Chat.svelte +514 -94
  6. package/dist/components/Chat.svelte.d.ts +19 -2
  7. package/dist/components/ChatComposer.svelte +126 -72
  8. package/dist/components/ChatComposer.svelte.d.ts +13 -3
  9. package/dist/components/ChatDropOverlay.svelte +58 -0
  10. package/dist/components/ChatDropOverlay.svelte.d.ts +8 -0
  11. package/dist/components/ChatInput.svelte +42 -1
  12. package/dist/components/ChatInput.svelte.d.ts +2 -0
  13. package/dist/components/ChatMessages.svelte +322 -98
  14. package/dist/components/ChatMessages.svelte.d.ts +11 -2
  15. package/dist/components/ComposerAttachmentsTray.svelte +144 -56
  16. package/dist/components/ComposerAttachmentsTray.svelte.d.ts +11 -1
  17. package/dist/components/FileGlyphIcon.svelte +34 -0
  18. package/dist/components/FileGlyphIcon.svelte.d.ts +8 -0
  19. package/dist/components/RestoreCheckpointDialog.svelte +66 -0
  20. package/dist/components/RestoreCheckpointDialog.svelte.d.ts +11 -0
  21. package/dist/components/SentAttachments.svelte +118 -0
  22. package/dist/components/SentAttachments.svelte.d.ts +12 -0
  23. package/dist/components/agent/AgentTurn.svelte +17 -2
  24. package/dist/components/agent/CodeCard.svelte +9 -3
  25. package/dist/i18n/messages.d.ts +45 -3
  26. package/dist/i18n/messages.js +51 -5
  27. package/dist/services/index.d.ts +4 -2
  28. package/dist/services/index.js +2 -1
  29. package/dist/services/limitRefusal.d.ts +42 -0
  30. package/dist/services/limitRefusal.js +60 -0
  31. package/dist/services/sketchAssistantClient.d.ts +11 -3
  32. package/dist/services/sketchAssistantClient.js +37 -10
  33. package/dist/services/sketchAssistantErrors.d.ts +16 -1
  34. package/dist/services/sketchAssistantErrors.js +25 -4
  35. package/dist/services/sketchAssistantOrchestrator.d.ts +7 -1
  36. package/dist/services/sketchAssistantOrchestrator.js +2 -2
  37. package/dist/types/attachment.d.ts +11 -0
  38. package/dist/utils/agentTurn.d.ts +32 -1
  39. package/dist/utils/agentTurn.js +46 -3
  40. package/dist/utils/attachmentDuplicates.d.ts +48 -0
  41. package/dist/utils/attachmentDuplicates.js +70 -0
  42. package/dist/utils/attachmentPolicy.d.ts +88 -0
  43. package/dist/utils/attachmentPolicy.js +135 -0
  44. package/dist/utils/attachmentPreview.d.ts +146 -0
  45. package/dist/utils/attachmentPreview.js +224 -0
  46. package/dist/utils/attachmentTray.d.ts +35 -0
  47. package/dist/utils/attachmentTray.js +27 -0
  48. package/dist/utils/chatFileDrag.d.ts +39 -0
  49. package/dist/utils/chatFileDrag.js +29 -0
  50. package/dist/utils/composerPreview.d.ts +33 -0
  51. package/dist/utils/composerPreview.js +31 -0
  52. package/dist/utils/generationArchiveStore.d.ts +5 -1
  53. package/dist/utils/generationArchiveStore.js +9 -4
  54. package/dist/utils/index.d.ts +1 -1
  55. package/dist/utils/index.js +1 -1
  56. package/dist/utils/limitFailureLine.d.ts +20 -0
  57. package/dist/utils/limitFailureLine.js +40 -0
  58. package/dist/utils/panelListPadding.d.ts +20 -0
  59. package/dist/utils/panelListPadding.js +31 -0
  60. package/dist/utils/pasteInsertion.d.ts +43 -0
  61. package/dist/utils/pasteInsertion.js +94 -0
  62. package/dist/utils/pastedContent.d.ts +13 -0
  63. package/dist/utils/pastedContent.js +18 -1
  64. package/dist/utils/referenceItems.js +11 -3
  65. package/dist/utils/sentAttachments.d.ts +43 -0
  66. package/dist/utils/sentAttachments.js +38 -0
  67. package/dist/utils/sentReferenceBadges.d.ts +11 -1
  68. package/dist/utils/sentReferenceBadges.js +47 -2
  69. package/package.json +3 -3
@@ -193,8 +193,11 @@ interface Props {
193
193
  /** Undo the last sketch-assistant canvas turn (one engine undo step). */
194
194
  onSketchUndo?: () => void | Promise<void>;
195
195
  /**
196
- * What the "Upgrade" button of a failure about the plan or the credits does. Without it the button opens the
197
- * billing page of the Widgetic site; a host that is a guest on somebody else's page gives its own function.
196
+ * What the "Upgrade" button of a failure about the plan or the credits does. The button is drawn right under the failure,
197
+ * in the chat: under a code generation the host saved a failure for ("Code generation failed: … Upgrade your plan …"), and
198
+ * under the sketch assistant's own sentences for a refusal because of money (`chat.sketch.noCredits`, `.planRequired`,
199
+ * `.limitReached`). Without this function the button opens the billing page of the Widgetic site; a host that is a guest on
200
+ * somebody else's page gives its own function.
198
201
  */
199
202
  onUpgradePlans?: () => void;
200
203
  /**
@@ -306,6 +309,16 @@ interface Props {
306
309
  * a run began.
307
310
  */
308
311
  generationChanges?: import('../utils/codeChanges.js').CodeChanges | null;
312
+ /**
313
+ * The agent's own words about what the last run changed (`operation.metadata.summary`: a sentence or two of plain text), or `null`
314
+ * when it said nothing: a gateway that does not send one, a build that was not kept. The conclusion under that run says it in
315
+ * place of "Done. Your changes are ready.".
316
+ *
317
+ * Like `generationChanges` it is what the run left behind, so it is NOT given up when `isGenerating` ends: give it when the
318
+ * operation completes (it may come after `isGenerating` fell) and clear it (`null`) when the next run starts. A page that forgets
319
+ * to clear it is safe: the chat ignores the very value it held when a run began.
320
+ */
321
+ generationSummary?: string | null;
309
322
  onStopGeneration?: () => void;
310
323
  /**
311
324
  * Renders the inline design-control card attached to an assistant reply
@@ -397,6 +410,10 @@ interface Props {
397
410
  composerModels?: ComposerModelOption[];
398
411
  selectedComposerModelId?: string | null;
399
412
  onSelectComposerModel?: (modelId: string) => void;
413
+ /**
414
+ * Rewrites the text in the composer (Magic Enhance). Left out, the sparkle control is not drawn.
415
+ */
416
+ onEnhancePrompt?: (prompt: string) => Promise<string>;
400
417
  }
401
418
  declare const Chat: import("svelte").Component<Props, {}, "">;
402
419
  type Chat = ReturnType<typeof Chat>;
@@ -4,6 +4,8 @@
4
4
  import type { ComposerMention, MentionSource } from '../types/mention.js';
5
5
  import { useChatLanguage } from '../i18n/language.js';
6
6
  import { dirForUserText, useTextDirection } from '@widgetic/design-system/components/textDirection';
7
+ import { HintTooltip } from '@widgetic/design-system/components';
8
+ import IconSparkles from '@tabler/icons-svelte/icons/sparkles';
7
9
  import {
8
10
  Composer,
9
11
  IconButton,
@@ -15,6 +17,7 @@
15
17
  } from '@widgetic/design-system/components/chat/index';
16
18
  import { chatLog, chatError } from '../utils/logger.js';
17
19
  import { attachmentsFromPaste, type PastedAttachment } from '../utils/pastedContent.js';
20
+ import type { PasteSelection } from '../utils/pasteInsertion.js';
18
21
  import {
19
22
  DEFAULT_MENTION_LIMIT,
20
23
  buildMentionRows,
@@ -62,13 +65,15 @@
62
65
  */
63
66
  editing?: ComposerEditing | null;
64
67
  onInputChange?: (text: string) => void;
65
- onFilesDropped?: (files: File[]) => void;
66
68
  /**
67
69
  * What a paste brings in as files to attach: a screenshot or a file on the clipboard, or a long text (which comes as a text
68
70
  * file, with the first words of the text as its label). Without it the composer pastes as the browser does: a page that
69
71
  * cannot attach files gets no attachment from a paste, and the text goes into the box.
72
+ *
73
+ * `selection` is where the caret was when the paste arrived (or the part of the text the paste replaces): a long text is
74
+ * named in the message by a badge written there.
70
75
  */
71
- onFilesPasted?: (pasted: PastedAttachment[]) => void;
76
+ onFilesPasted?: (pasted: PastedAttachment[], selection?: PasteSelection) => void;
72
77
  /** Opens the file picker. The "+" of the toolbar is drawn only when the page can attach files. */
73
78
  onAttach?: () => void;
74
79
  /** Figma side-panel composer: no top border on the shell; DS Composer keeps its own border. */
@@ -123,6 +128,11 @@
123
128
  * chip and sends it with the message.
124
129
  */
125
130
  onMentionPick?: (mention: ComposerMention) => void;
131
+ /**
132
+ * Rewrites the text in the box (Magic Enhance). Given, a sparkle control sits next to attach.
133
+ * The host calls the planner and returns the new text; this component puts it in the box.
134
+ */
135
+ onEnhancePrompt?: (prompt: string) => Promise<string>;
126
136
  }
127
137
 
128
138
  let {
@@ -141,7 +151,6 @@
141
151
  onQueue,
142
152
  editing = null,
143
153
  onInputChange,
144
- onFilesDropped,
145
154
  onFilesPasted,
146
155
  onAttach,
147
156
  panelLayout = false,
@@ -160,8 +169,11 @@
160
169
  mentions = [],
161
170
  mentionLimit = DEFAULT_MENTION_LIMIT,
162
171
  onMentionPick,
172
+ onEnhancePrompt,
163
173
  }: Props = $props();
164
174
 
175
+ let isEnhancing = $state(false);
176
+
165
177
  const composerSendIcon = $derived(panelLayout ? 'arrow-up' : sendIcon);
166
178
 
167
179
  // The shell, to reach the message box in it. The design system's Composer draws the box (a textarea) and does not hand it out,
@@ -174,6 +186,25 @@
174
186
  return shellElement?.querySelector('textarea') ?? null;
175
187
  }
176
188
 
189
+ /**
190
+ * Puts the caret at `position` in the text (the box is given the focus). The page calls it after it wrote into the text by itself
191
+ * (a badge for a pasted text): a text set from outside leaves the caret at its end, and the person goes on typing after the badge.
192
+ * It waits for the new text to reach the box first.
193
+ */
194
+ export async function placeCaret(position: number): Promise<void> {
195
+ try {
196
+ await tick();
197
+ const messageBox = findMessageBox();
198
+ if (!messageBox) return;
199
+ const caretPosition = Math.max(0, Math.min(Math.floor(position), messageBox.value.length));
200
+ messageBox.focus({ preventScroll: true });
201
+ messageBox.setSelectionRange(caretPosition, caretPosition);
202
+ chatLog('[ChatComposer] Caret placed at', caretPosition);
203
+ } catch (error) {
204
+ chatError('[ChatComposer] Could not place the caret:', error);
205
+ }
206
+ }
207
+
177
208
  // The box takes the focus once it is drawn (the note of a part opens ready to be written in), caret after what is written.
178
209
  $effect(() => {
179
210
  if (!autofocus) return;
@@ -214,12 +245,12 @@
214
245
  });
215
246
 
216
247
  // The files the message may refer to, with the words the box says about them. `{name}` is filled by the box with the file's own name.
248
+ // No hint about how to refer to a file ("Type # to …"): a person who attached a file is not told, in any way, how to use it.
217
249
  const composerReferences = $derived(
218
250
  referenceItems
219
251
  ? {
220
252
  items: referenceItems,
221
253
  messages: {
222
- hint: $t('chat.input.referenceHint'),
223
254
  menuLabel: $t('chat.input.referenceMenuLabel'),
224
255
  remove: $t('chat.input.referenceRemove', { name: '{name}' }),
225
256
  },
@@ -233,7 +264,6 @@
233
264
  // that is used then), never a generic word such as "Assistant": the person knows what they are talking to.
234
265
  const fallbackModelLabel = $derived(models?.[0]?.label?.trim() ?? '');
235
266
 
236
- let isFileDragOver = $state(false);
237
267
  let sendNudgePhase = $state<SendNudgePhase>('idle');
238
268
  let sendNudgeBounce = $state(false);
239
269
  let sendNudgeHideTimer: ReturnType<typeof setTimeout> | null = null;
@@ -291,40 +321,8 @@
291
321
  const busy = $derived(isGenerating || isSending);
292
322
  const allowEmptySend = $derived(canSendWithoutText);
293
323
 
294
- function isOsFileDrag(event: DragEvent): boolean {
295
- return !!event.dataTransfer?.types?.includes('Files');
296
- }
297
-
298
- function handleFileDragEnter(event: DragEvent) {
299
- if (!isOsFileDrag(event)) return;
300
- event.preventDefault();
301
- isFileDragOver = true;
302
- }
303
-
304
- function handleFileDragOver(event: DragEvent) {
305
- if (!isOsFileDrag(event)) return;
306
- event.preventDefault();
307
- if (event.dataTransfer) event.dataTransfer.dropEffect = 'copy';
308
- isFileDragOver = true;
309
- }
310
-
311
- function handleFileDragLeave(event: DragEvent) {
312
- if (!isOsFileDrag(event)) return;
313
- const related = event.relatedTarget as Node | null;
314
- if (related && (event.currentTarget as HTMLElement).contains(related)) return;
315
- isFileDragOver = false;
316
- }
317
-
318
- function handleFileDrop(event: DragEvent) {
319
- if (!isOsFileDrag(event)) return;
320
- event.preventDefault();
321
- event.stopPropagation();
322
- isFileDragOver = false;
323
- const droppedFiles = Array.from(event.dataTransfer?.files || []).filter((file) => file && file.size > 0);
324
- if (droppedFiles.length === 0) return;
325
- chatLog('[ChatComposer] OS files dropped:', droppedFiles.length);
326
- onFilesDropped?.(droppedFiles);
327
- }
324
+ // Files dragged over the chat, and dropped on it, are taken by the chat itself (the whole chat is the drop area, not only this
325
+ // box): see `handleChatFileDrag` in `Chat.svelte`.
328
326
 
329
327
  /**
330
328
  * A paste into the box. A picture or a file on the clipboard, or a long text, is attached the way a dropped file is. A short
@@ -339,8 +337,13 @@
339
337
  const pastedAttachments = attachmentsFromPaste(event.clipboardData);
340
338
  if (pastedAttachments.length === 0) return;
341
339
  event.preventDefault();
342
- chatLog('[ChatComposer] Pasted content attached:', pastedAttachments.length);
343
- onFilesPasted(pastedAttachments);
340
+ // Where the caret is now (read before anything is waited for): a long text is named in the message right there.
341
+ const selection =
342
+ event.target instanceof HTMLTextAreaElement
343
+ ? { start: event.target.selectionStart ?? 0, end: event.target.selectionEnd ?? 0 }
344
+ : undefined;
345
+ chatLog('[ChatComposer] Pasted content attached:', pastedAttachments.length, 'caret at', selection?.start);
346
+ onFilesPasted(pastedAttachments, selection);
344
347
  } catch (error) {
345
348
  chatError('[ChatComposer] Could not read what was pasted:', error);
346
349
  }
@@ -512,6 +515,25 @@
512
515
  onQueue?.(trimmed);
513
516
  }
514
517
 
518
+ async function handleEnhancePromptClick() {
519
+ const trimmed = (value ?? '').trim();
520
+ if (!trimmed || !onEnhancePrompt || isEnhancing) return;
521
+ if (isSending || isGenerating) return;
522
+ isEnhancing = true;
523
+ try {
524
+ const next = await onEnhancePrompt(trimmed);
525
+ if (typeof next === 'string' && next.trim()) {
526
+ value = next.trim();
527
+ onInputChange?.(value);
528
+ chatLog('[ChatComposer] Prompt enhanced', { length: value.length });
529
+ }
530
+ } catch (error) {
531
+ chatError('[ChatComposer] Prompt enhance failed:', error);
532
+ } finally {
533
+ isEnhancing = false;
534
+ }
535
+ }
536
+
515
537
  function handleComposerSend(text: string, options?: ComposerSendOptions) {
516
538
  const trimmed = text.trim();
517
539
  if (!trimmed && !allowEmptySend) return;
@@ -525,44 +547,68 @@
525
547
  }
526
548
  </script>
527
549
 
528
- <!-- Figma: Composer toolbar, left side: attach ("+"), then the mode. -->
529
- {#snippet attachControl()}
550
+ <!-- Figma: Composer toolbar, left side: attach ("+"), Magic Enhance, then the mode. -->
551
+ {#snippet composerLeading()}
530
552
  {#if onAttach}
531
- <IconButton
532
- variant="ghost"
533
- data-testid="chat-composer-attach"
534
- label={$t('chat.actions.uploadLabel')}
535
- disabled={isSending && !isGenerating}
536
- onclick={() => onAttach?.()}
537
- >
538
- <svg
539
- xmlns="http://www.w3.org/2000/svg"
540
- class="size-4 stroke-2"
541
- viewBox="0 0 24 24"
542
- fill="none"
543
- stroke="currentColor"
544
- stroke-linecap="round"
545
- stroke-linejoin="round"
546
- aria-hidden="true"
547
- >
548
- <path d="M12 5l0 14" />
549
- <path d="M5 12l14 0" />
550
- </svg>
551
- </IconButton>
553
+ <!-- Resting the pointer on the "+" (the tooltip's usual delay) says what it adds and which kinds of files go in. -->
554
+ <HintTooltip content={$t('chat.actions.uploadTooltip')} side="top" align="start">
555
+ {#snippet trigger({ props })}
556
+ <IconButton
557
+ {...props}
558
+ variant="ghost"
559
+ data-testid="chat-composer-attach"
560
+ label={$t('chat.actions.uploadLabel')}
561
+ disabled={isSending && !isGenerating}
562
+ onclick={() => onAttach?.()}
563
+ >
564
+ <svg
565
+ xmlns="http://www.w3.org/2000/svg"
566
+ class="size-4 stroke-2"
567
+ viewBox="0 0 24 24"
568
+ fill="none"
569
+ stroke="currentColor"
570
+ stroke-linecap="round"
571
+ stroke-linejoin="round"
572
+ aria-hidden="true"
573
+ >
574
+ <path d="M12 5l0 14" />
575
+ <path d="M5 12l14 0" />
576
+ </svg>
577
+ </IconButton>
578
+ {/snippet}
579
+ </HintTooltip>
580
+ {/if}
581
+ {#if onEnhancePrompt}
582
+ <HintTooltip content={isEnhancing ? $t('chat.input.enhancing') : $t('chat.input.enhance')} side="top" align="start">
583
+ {#snippet trigger({ props })}
584
+ <IconButton
585
+ {...props}
586
+ variant="ghost"
587
+ class="chat-composer-enhance-btn cursor-pointer"
588
+ data-testid="chat-composer-enhance"
589
+ label={isEnhancing ? $t('chat.input.enhancing') : $t('chat.input.enhance')}
590
+ disabled={!(value ?? '').trim() || isEnhancing || (isSending && !isGenerating) || isGenerating}
591
+ onclick={() => void handleEnhancePromptClick()}
592
+ >
593
+ {#if isEnhancing}
594
+ <svg class="size-4 animate-spin" viewBox="0 0 24 24" aria-hidden="true">
595
+ <circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="2" opacity="0.3"/>
596
+ <path d="M22 12a10 10 0 0 1-10 10" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
597
+ </svg>
598
+ {:else}
599
+ <IconSparkles class="size-4 stroke-2" aria-hidden="true" />
600
+ {/if}
601
+ </IconButton>
602
+ {/snippet}
603
+ </HintTooltip>
552
604
  {/if}
553
605
  {/snippet}
554
606
 
555
607
  <!-- svelte-ignore a11y_no_static_element_interactions -->
556
608
  <div
557
- class="chat-composer-shell border-border bg-background relative shrink-0 {panelLayout ? '' : 'border-t'} {className} {isFileDragOver
558
- ? 'outline-primary outline-2 outline-dashed'
559
- : ''}"
609
+ class="chat-composer-shell border-border bg-background relative shrink-0 {panelLayout ? '' : 'border-t'} {className}"
560
610
  {style}
561
611
  bind:this={shellElement}
562
- ondragenter={handleFileDragEnter}
563
- ondragover={handleFileDragOver}
564
- ondragleave={handleFileDragLeave}
565
- ondrop={handleFileDrop}
566
612
  onpaste={handlePaste}
567
613
  onkeydowncapture={handleMentionKeydown}
568
614
  oninput={handleMentionInput}
@@ -595,7 +641,7 @@
595
641
  onStop={() => onStop?.()}
596
642
  onQueue={onQueue ? handleComposerQueue : undefined}
597
643
  {editing}
598
- leading={attachControl}
644
+ leading={composerLeading}
599
645
  {attachments}
600
646
  {completion}
601
647
  references={composerReferences}
@@ -612,3 +658,11 @@
612
658
  />
613
659
  {/if}
614
660
  </div>
661
+
662
+ <style>
663
+ :global(.chat-composer-enhance-btn:focus),
664
+ :global(.chat-composer-enhance-btn:focus-visible) {
665
+ outline: none;
666
+ box-shadow: none;
667
+ }
668
+ </style>
@@ -2,6 +2,7 @@ import { type Snippet } from 'svelte';
2
2
  import type { ComposerMention, MentionSource } from '../types/mention.js';
3
3
  import { type ComposerCompletion, type ComposerEditing, type ComposerSendOptions, type ReferenceHoverEvent, type ReferenceItem } from '@widgetic/design-system/components/chat/index';
4
4
  import { type PastedAttachment } from '../utils/pastedContent.js';
5
+ import type { PasteSelection } from '../utils/pasteInsertion.js';
5
6
  export type ComposerModelOption = {
6
7
  id: string;
7
8
  label: string;
@@ -34,13 +35,15 @@ interface Props {
34
35
  */
35
36
  editing?: ComposerEditing | null;
36
37
  onInputChange?: (text: string) => void;
37
- onFilesDropped?: (files: File[]) => void;
38
38
  /**
39
39
  * What a paste brings in as files to attach: a screenshot or a file on the clipboard, or a long text (which comes as a text
40
40
  * file, with the first words of the text as its label). Without it the composer pastes as the browser does: a page that
41
41
  * cannot attach files gets no attachment from a paste, and the text goes into the box.
42
+ *
43
+ * `selection` is where the caret was when the paste arrived (or the part of the text the paste replaces): a long text is
44
+ * named in the message by a badge written there.
42
45
  */
43
- onFilesPasted?: (pasted: PastedAttachment[]) => void;
46
+ onFilesPasted?: (pasted: PastedAttachment[], selection?: PasteSelection) => void;
44
47
  /** Opens the file picker. The "+" of the toolbar is drawn only when the page can attach files. */
45
48
  onAttach?: () => void;
46
49
  /** Figma side-panel composer: no top border on the shell; DS Composer keeps its own border. */
@@ -95,7 +98,14 @@ interface Props {
95
98
  * chip and sends it with the message.
96
99
  */
97
100
  onMentionPick?: (mention: ComposerMention) => void;
101
+ /**
102
+ * Rewrites the text in the box (Magic Enhance). Given, a sparkle control sits next to attach.
103
+ * The host calls the planner and returns the new text; this component puts it in the box.
104
+ */
105
+ onEnhancePrompt?: (prompt: string) => Promise<string>;
98
106
  }
99
- declare const ChatComposer: import("svelte").Component<Props, {}, "value">;
107
+ declare const ChatComposer: import("svelte").Component<Props, {
108
+ placeCaret: (position: number) => Promise<void>;
109
+ }, "value">;
100
110
  type ChatComposer = ReturnType<typeof ChatComposer>;
101
111
  export default ChatComposer;
@@ -0,0 +1,58 @@
1
+ <script lang="ts">
2
+ /**
3
+ * What covers the whole chat while files are dragged over it (Figma: "Chat / Drop files" and "Chat / Drop not allowed"): the chat
4
+ * dims behind a white veil, and a dashed frame says what releasing the files would do. In green it invites the drop; in red it says
5
+ * the files are of a kind the box does not take (only the type of a file is known while it is dragged: its size is judged on the
6
+ * drop, and a file that is too large is told about then). The veil lets the pointer through, so the chat below keeps receiving the
7
+ * drag, and a drop anywhere on the chat is taken by the chat itself (see `Chat.svelte`).
8
+ */
9
+ import { fade } from 'svelte/transition';
10
+ import IconAlertTriangle from '@tabler/icons-svelte/icons/alert-triangle';
11
+ import IconCloudUpload from '@tabler/icons-svelte/icons/cloud-upload';
12
+ import { useChatLanguage } from '../i18n/language.js';
13
+ import type { FileDragOverlay } from '../utils/chatFileDrag.js';
14
+
15
+ interface Props {
16
+ /** What covers the chat now: nothing, the invitation, or the refusal. */
17
+ overlay: FileDragOverlay;
18
+ }
19
+
20
+ let { overlay }: Props = $props();
21
+
22
+ const { t } = useChatLanguage();
23
+
24
+ const isRefusal = $derived(overlay === 'refuse');
25
+
26
+ // The veil fades in and out quickly, and not at all for a person who asked for less motion
27
+ const fadeDurationMs =
28
+ typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ? 0 : 120;
29
+ </script>
30
+
31
+ {#if overlay !== 'hidden'}
32
+ <div
33
+ class="chat-drop-overlay bg-background/80 pointer-events-none absolute inset-0 z-40 flex rounded-[inherit] p-3"
34
+ data-testid="chat-drop-overlay"
35
+ data-verdict={overlay}
36
+ role="status"
37
+ aria-live="polite"
38
+ transition:fade={{ duration: fadeDurationMs }}
39
+ >
40
+ <div
41
+ class="chat-drop-overlay-target flex min-w-0 flex-1 flex-col items-center justify-center gap-2 rounded-[18px] border-2 border-dashed px-6 text-center {isRefusal
42
+ ? 'border-destructive bg-destructive/7'
43
+ : 'border-brand bg-brand-subtle/55'}"
44
+ >
45
+ {#if isRefusal}
46
+ <IconAlertTriangle class="text-destructive size-6" aria-hidden="true" />
47
+ {:else}
48
+ <IconCloudUpload class="text-brand size-6" aria-hidden="true" />
49
+ {/if}
50
+ <p class="text-label text-foreground" data-testid="chat-drop-overlay-title">
51
+ {isRefusal ? $t('chat.drop.refusedTitle') : $t('chat.drop.title')}
52
+ </p>
53
+ <p class="text-label-small text-muted-foreground" data-testid="chat-drop-overlay-hint">
54
+ {isRefusal ? $t('chat.drop.refusedHint') : $t('chat.drop.hint')}
55
+ </p>
56
+ </div>
57
+ </div>
58
+ {/if}
@@ -0,0 +1,8 @@
1
+ import type { FileDragOverlay } from '../utils/chatFileDrag.js';
2
+ interface Props {
3
+ /** What covers the chat now: nothing, the invitation, or the refusal. */
4
+ overlay: FileDragOverlay;
5
+ }
6
+ declare const ChatDropOverlay: import("svelte").Component<Props, {}, "">;
7
+ type ChatDropOverlay = ReturnType<typeof ChatDropOverlay>;
8
+ export default ChatDropOverlay;
@@ -24,6 +24,8 @@
24
24
  */
25
25
  showAttachmentsTray?: boolean;
26
26
  onSend: (content: string, attachments?: AttachmentInput[]) => void;
27
+ /** Rewrites the text in the box. Left out, Magic Enhance is not drawn. */
28
+ onEnhancePrompt?: (prompt: string) => Promise<string>;
27
29
  onStop?: () => void;
28
30
  onInputChange: (text: string) => void;
29
31
  onAttachmentsChange: (attachments: AttachmentInput[]) => void;
@@ -46,6 +48,7 @@
46
48
  isGenerating = false,
47
49
  showAttachmentsTray = true,
48
50
  onSend,
51
+ onEnhancePrompt,
49
52
  onStop,
50
53
  onInputChange,
51
54
  onAttachmentsChange,
@@ -57,6 +60,7 @@
57
60
  }: Props = $props();
58
61
 
59
62
  let isFileDragOver = $state(false);
63
+ let isEnhancing = $state(false);
60
64
 
61
65
  // What the send button is called (and says in its tooltip): it turns into Stop while the assistant answers
62
66
  let sendLabel = $derived(
@@ -114,6 +118,20 @@
114
118
  return value.trim().length > 0 || attachments.length > 0 || canSendWithoutText;
115
119
  }
116
120
 
121
+ async function handleEnhancePromptClick() {
122
+ const trimmed = value.trim();
123
+ if (!trimmed || !onEnhancePrompt || isEnhancing || isSending || isGenerating) return;
124
+ isEnhancing = true;
125
+ try {
126
+ const next = await onEnhancePrompt(trimmed);
127
+ if (typeof next === 'string' && next.trim()) {
128
+ onInputChange(next.trim());
129
+ }
130
+ } finally {
131
+ isEnhancing = false;
132
+ }
133
+ }
134
+
117
135
  function handleSubmit() {
118
136
  if (!hasSomethingToSend()) return;
119
137
  if (isSending) return;
@@ -339,6 +357,27 @@
339
357
  ></textarea>
340
358
 
341
359
  <div class="send-button-ct flex items-center gap-2">
360
+ {#if onEnhancePrompt}
361
+ <button
362
+ type="button"
363
+ class="enhance-prompt-btn w-8 h-8 rounded-lg flex items-center justify-center cursor-pointer text-gray-600 hover:bg-gray-200 dark:text-gray-200 dark:hover:bg-gray-600"
364
+ onclick={() => void handleEnhancePromptClick()}
365
+ disabled={!value.trim() || isEnhancing || isSending || isGenerating}
366
+ aria-label={isEnhancing ? $t('chat.input.enhancing') : $t('chat.input.enhance')}
367
+ title={isEnhancing ? $t('chat.input.enhancing') : $t('chat.input.enhance')}
368
+ >
369
+ {#if isEnhancing}
370
+ <svg class="w-4 h-4 animate-spin" viewBox="0 0 24 24" aria-hidden="true">
371
+ <circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="2" opacity="0.3"/>
372
+ <path d="M22 12a10 10 0 0 1-10 10" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
373
+ </svg>
374
+ {:else}
375
+ <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" class="w-4 h-4" aria-hidden="true">
376
+ <path d="M16 18a2 2 0 0 1 2 2 2 2 0 0 1 2-2 2 2 0 0 1-2-2 2 2 0 0 1-2 2m0-12a2 2 0 0 1 2 2 2 2 0 0 1 2-2 2 2 0 0 1-2-2 2 2 0 0 1-2 2m-7 12a6 6 0 0 1 6-6 6 6 0 0 1-6-6 6 6 0 0 1-6 6 6 6 0 0 1 6 6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
377
+ </svg>
378
+ {/if}
379
+ </button>
380
+ {/if}
342
381
  <!-- Send Button -->
343
382
  <button
344
383
  class="send-button-btn w-8 h-8 rounded-lg flex items-center justify-center transition-colors"
@@ -397,7 +436,9 @@
397
436
 
398
437
  <style>
399
438
  .send-button-btn:focus,
400
- .send-button-btn:focus-visible {
439
+ .send-button-btn:focus-visible,
440
+ .enhance-prompt-btn:focus,
441
+ .enhance-prompt-btn:focus-visible {
401
442
  outline: none;
402
443
  box-shadow: none;
403
444
  }
@@ -12,6 +12,8 @@ interface Props {
12
12
  */
13
13
  showAttachmentsTray?: boolean;
14
14
  onSend: (content: string, attachments?: AttachmentInput[]) => void;
15
+ /** Rewrites the text in the box. Left out, Magic Enhance is not drawn. */
16
+ onEnhancePrompt?: (prompt: string) => Promise<string>;
15
17
  onStop?: () => void;
16
18
  onInputChange: (text: string) => void;
17
19
  onAttachmentsChange: (attachments: AttachmentInput[]) => void;