@widgetic/chat 0.1.41 → 0.1.43

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 (87) hide show
  1. package/README.md +461 -6
  2. package/dist/components/Chat.svelte +1117 -119
  3. package/dist/components/Chat.svelte.d.ts +112 -2
  4. package/dist/components/ChatComposer.svelte +389 -8
  5. package/dist/components/ChatComposer.svelte.d.ts +71 -2
  6. package/dist/components/ChatMessageQueue.svelte +56 -0
  7. package/dist/components/ChatMessageQueue.svelte.d.ts +20 -0
  8. package/dist/components/ChatMessages.svelte +477 -46
  9. package/dist/components/ChatMessages.svelte.d.ts +45 -2
  10. package/dist/components/ComposerAttachmentsTray.svelte +42 -10
  11. package/dist/components/ComposerAttachmentsTray.svelte.d.ts +5 -0
  12. package/dist/components/ComposerMentionChips.svelte +53 -0
  13. package/dist/components/ComposerMentionChips.svelte.d.ts +15 -0
  14. package/dist/components/FollowUpCard.svelte +134 -0
  15. package/dist/components/FollowUpCard.svelte.d.ts +17 -0
  16. package/dist/components/MentionChip.svelte +41 -0
  17. package/dist/components/MentionChip.svelte.d.ts +12 -0
  18. package/dist/components/MentionMenu.svelte +114 -0
  19. package/dist/components/MentionMenu.svelte.d.ts +19 -0
  20. package/dist/components/SelectedElementChip.svelte +73 -0
  21. package/dist/components/SelectedElementChip.svelte.d.ts +20 -0
  22. package/dist/components/agent/AgentAvatar.svelte +6 -4
  23. package/dist/components/agent/AgentAvatar.svelte.d.ts +1 -1
  24. package/dist/components/agent/CodeCard.svelte +122 -27
  25. package/dist/components/agent/CodeCard.svelte.d.ts +3 -0
  26. package/dist/components/agent/CodeDiff.svelte +180 -0
  27. package/dist/components/agent/CodeDiff.svelte.d.ts +10 -0
  28. package/dist/components/agent/GenerationModule.svelte +13 -2
  29. package/dist/components/agent/GenerationModule.svelte.d.ts +3 -0
  30. package/dist/components/index.d.ts +5 -0
  31. package/dist/components/index.js +12 -0
  32. package/dist/errors/logTechnicalUserError.d.ts +1 -0
  33. package/dist/errors/logTechnicalUserError.js +37 -0
  34. package/dist/i18n/messages.d.ts +106 -1
  35. package/dist/i18n/messages.js +112 -1
  36. package/dist/i18n/sketchLocales.js +8 -8
  37. package/dist/messageQueue.d.ts +83 -0
  38. package/dist/messageQueue.js +84 -0
  39. package/dist/services/sketchAssistantClient.js +6 -2
  40. package/dist/services/sketchAssistantErrors.d.ts +4 -0
  41. package/dist/services/sketchAssistantErrors.js +22 -0
  42. package/dist/services/sketchAssistantOrchestrator.d.ts +1 -6
  43. package/dist/services/sketchAssistantOrchestrator.js +10 -24
  44. package/dist/services/sketchAssistantReply.js +3 -1
  45. package/dist/stores/chatStore.d.ts +5 -0
  46. package/dist/stores/chatStore.js +21 -0
  47. package/dist/types/agent.d.ts +4 -3
  48. package/dist/types/attachment.d.ts +16 -0
  49. package/dist/types/config.d.ts +6 -0
  50. package/dist/types/index.d.ts +1 -0
  51. package/dist/types/index.js +2 -0
  52. package/dist/types/mention.d.ts +40 -0
  53. package/dist/types/mention.js +8 -0
  54. package/dist/types/message.d.ts +60 -1
  55. package/dist/utils/agentTurn.d.ts +40 -9
  56. package/dist/utils/agentTurn.js +43 -11
  57. package/dist/utils/attachmentProgress.d.ts +12 -0
  58. package/dist/utils/attachmentProgress.js +18 -0
  59. package/dist/utils/bubbleRichFit.d.ts +68 -0
  60. package/dist/utils/bubbleRichFit.js +45 -0
  61. package/dist/utils/bubbleShrinkWrap.d.ts +8 -1
  62. package/dist/utils/bubbleShrinkWrap.js +113 -17
  63. package/dist/utils/codeChanges.d.ts +103 -0
  64. package/dist/utils/codeChanges.js +226 -0
  65. package/dist/utils/codeHighlight.js +1 -0
  66. package/dist/utils/codeStreamFormat.d.ts +5 -2
  67. package/dist/utils/composerMentions.d.ts +96 -0
  68. package/dist/utils/composerMentions.js +217 -0
  69. package/dist/utils/followUps.d.ts +74 -0
  70. package/dist/utils/followUps.js +116 -0
  71. package/dist/utils/generationArchiveStore.d.ts +6 -0
  72. package/dist/utils/generationArchiveStore.js +11 -3
  73. package/dist/utils/index.d.ts +14 -0
  74. package/dist/utils/index.js +22 -0
  75. package/dist/utils/orderedLine.d.ts +29 -0
  76. package/dist/utils/orderedLine.js +17 -0
  77. package/dist/utils/pastedContent.d.ts +99 -0
  78. package/dist/utils/pastedContent.js +258 -0
  79. package/dist/utils/questionCards.d.ts +131 -0
  80. package/dist/utils/questionCards.js +287 -0
  81. package/dist/utils/referenceItems.d.ts +36 -0
  82. package/dist/utils/referenceItems.js +56 -0
  83. package/dist/utils/selectedElementChip.d.ts +47 -0
  84. package/dist/utils/selectedElementChip.js +73 -0
  85. package/dist/utils/sentReferenceBadges.d.ts +26 -0
  86. package/dist/utils/sentReferenceBadges.js +32 -0
  87. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { onMount, onDestroy, untrack } from 'svelte';
2
+ import { onMount, onDestroy, tick, untrack } from 'svelte';
3
3
  import type { Snippet } from 'svelte';
4
4
  import { writable } from 'svelte/store';
5
5
  import { shareTextDirection, type TextDirection } from '@widgetic/design-system/components/textDirection';
@@ -7,23 +7,82 @@
7
7
  import { createChatService } from '../services/chatService.js';
8
8
  import { setLogsEnabled, chatLog, chatWarn } from '../utils/logger.js';
9
9
  import { ratingAfterPress, ratingOfVersion, type VersionThumb } from '../utils/versionRatings.js';
10
+ import { generateFileId } from '../utils/fileUtils.js';
11
+ import {
12
+ prepareImageWithTimeLimit,
13
+ prepareImagesOfPaste,
14
+ withUniqueFileNames,
15
+ type PastedAttachment,
16
+ } from '../utils/pastedContent.js';
17
+ import { createOrderedLine } from '../utils/orderedLine.js';
10
18
  import type { ChatConfig, ChatContext } from '../types/index.js';
11
- import type { ClarificationSuggestion, DesignControlRequest, VersionRating } from '../types/message.js';
19
+ import type {
20
+ ClarificationSuggestion,
21
+ DesignControlRequest,
22
+ QuestionCardAnswer,
23
+ QuestionCardModel,
24
+ VersionRating
25
+ } from '../types/message.js';
26
+ import {
27
+ keepLocalQuestionCards,
28
+ openQuestionCardOf,
29
+ questionCardPlainText,
30
+ skippedAnswersOf
31
+ } from '../utils/questionCards.js';
12
32
  import type { AttachmentInput, SelectedElementChatReference } from '../types/attachment.js';
33
+ import type { ComposerMention, MentionSource } from '../types/mention.js';
13
34
  import type { AgentAvatarRenderer, AgentSoundPlayer, ChatMember } from '../types/agent.js';
14
35
  import ChatMessages from './ChatMessages.svelte';
15
36
  import ChatComposer, { type ComposerModelOption } from './ChatComposer.svelte';
16
- import { Chip } from '@widgetic/design-system/components/chat/index';
17
37
  import { showToast } from '@widgetic/design-system/components/toast/index';
38
+ import {
39
+ assignReferenceLabel,
40
+ genericLabelNumber,
41
+ referenceOccurrences,
42
+ removeTokensOfLabel,
43
+ unbadgeTokensWithoutFiles,
44
+ type ComposerCompletion,
45
+ type ComposerEditing,
46
+ type ComposerSendOptions,
47
+ type ReferenceHoverEvent,
48
+ } from '@widgetic/design-system/components/chat/index';
49
+ import SelectedElementChip from './SelectedElementChip.svelte';
50
+ import ComposerMentionChips from './ComposerMentionChips.svelte';
51
+ import { selectedElementChipLabel, selectedElementChipTitle } from '../utils/selectedElementChip.js';
52
+ import {
53
+ DEFAULT_MENTION_LIMIT,
54
+ addMention,
55
+ findMentionLinkHints,
56
+ removeMention,
57
+ type MentionLinkHint,
58
+ } from '../utils/composerMentions.js';
18
59
  import ActionBar from './ActionBar.svelte';
19
60
  import PanelChatHeader from './PanelChatHeader.svelte';
20
61
  import ComposerAttachmentsTray from './ComposerAttachmentsTray.svelte';
62
+ import ChatMessageQueue from './ChatMessageQueue.svelte';
21
63
  import { mergeSuggestionIntoDraft } from '../utils/suggestionDraft.js';
22
64
  import { prepareChatImageFileForUpload } from '../utils/chatImageUploadPrep.js';
23
65
  import { createLocalPreviewUrl, readAttachmentPreviewUrl } from '../utils/attachmentPreviewUrl.js';
66
+ import {
67
+ QUEUE_SEND_NOW_TIMEOUT_MS,
68
+ QUEUE_SETTLE_MS,
69
+ appendQueuedMessage,
70
+ blobPreviewsOf,
71
+ createQueuedMessageId,
72
+ decideQueueStep,
73
+ findQueuedMessage,
74
+ previewsAddedByEdit,
75
+ previewsDroppedByEdit,
76
+ removeQueuedMessage,
77
+ updateQueuedMessage,
78
+ type QueuedMessage,
79
+ type StashedDraft,
80
+ } from '../messageQueue.js';
81
+ import { buildTransformedUrl } from '../utils/imageTransform.js';
82
+ import { buildReferenceItems, isReferenceablePicture, labelsOfAttachments } from '../utils/referenceItems.js';
24
83
  import StatusIndicator from './StatusIndicator.svelte';
25
84
  import { provideChatLanguage } from '../i18n/language.js';
26
- import { actionFailureKey, contextTypeLabel } from '../i18n/labels.js';
85
+ import { actionFailureKey, contextTypeLabel, fileSizeText } from '../i18n/labels.js';
27
86
  import type { ChatMessageKey } from '../i18n/messages.js';
28
87
 
29
88
  // Props
@@ -59,7 +118,11 @@
59
118
  * The parent handles the actual API call and can push assistant messages via addAssistantMessage.
60
119
  * `options` carries per-send modifiers — e.g. `skipIntentGate: true` plus the tapped chip's
61
120
  * `chipIntent` when the send is an explicit override of a clarification card (see
62
- * handleClarificationSuggestionSelected). */
121
+ * handleClarificationSuggestionSelected).
122
+ *
123
+ * `content` is the text a person reads: a reference to an attached file is the word `#hero_banner_final`. Each attachment carries
124
+ * the `referenceLabel` it was called by (a picture has one), and `options.references` lists the files the text names, once each
125
+ * and in order of first appearance: `id` is the id of the file's upload when it has ended, its label until then. */
63
126
  onSendMessage?: (
64
127
  content: string,
65
128
  attachments?: Array<{
@@ -69,13 +132,41 @@
69
132
  fileType: string;
70
133
  fileName: string;
71
134
  fileSize?: number;
135
+ referenceLabel?: string;
72
136
  }>,
73
137
  options?: {
74
138
  skipIntentGate?: boolean;
75
139
  chipIntent?: ClarificationSuggestion['intent'];
76
140
  selectedElements?: SelectedElementChatReference[];
141
+ /**
142
+ * Set when the person wrote this message while a card of questions was waiting (`ChatMessage.questionCard`) instead of
143
+ * answering it: one answer for each question of the card, every one with an EMPTY `answer` (skipped). The card is shown as
144
+ * skipped, the message is what the assistant goes on with, and the host sends these along with it (the gateway's `answers`,
145
+ * with the message as the prompt) so the assistant decides the skipped questions itself and does not ask them again.
146
+ */
147
+ questionAnswers?: QuestionCardAnswer[];
148
+ references?: Array<{ id: string; label: string }>;
149
+ /**
150
+ * What the person picked with `@` for this message (see `mentionSources`), in the order they picked it. Left out when nothing
151
+ * was picked. Only `kind` and `id` say what was picked; `name` is the row's name as it was shown.
152
+ */
153
+ mentions?: ComposerMention[];
77
154
  },
78
155
  ) => Promise<void>;
156
+ /**
157
+ * The person answered the card of questions of a reply in the card itself (Submit on its last question, or Skip), as the
158
+ * gateway's `answers`: one for each question in the card's order, an empty `answer` for a question that was skipped (Next with
159
+ * nothing picked, or Skip); several picks of one question are one per line. `messageId` is the reply that holds the card and
160
+ * `skippedAll` is true when no question has a pick. The card is drawn closed at once (with what was picked, or "Skipped").
161
+ * The host sends the request that asked the questions again, with these answers: nothing is added to the conversation
162
+ * here. Reject (or throw) only when the answers could not be sent, because nothing was started: the card opens again. A
163
+ * build that starts and then fails is not that (the host shows the failure as it does for any build; the answers were sent).
164
+ */
165
+ onQuestionsAnswered?: (detail: {
166
+ messageId: string;
167
+ answers: QuestionCardAnswer[];
168
+ skippedAll: boolean;
169
+ }) => Promise<void> | void;
79
170
  /** Backend integration callbacks — when provided, replace mock chatService methods */
80
171
  onLoadConversations?: (contextId: string) => Promise<any[] | null>;
81
172
  onLoadMessages?: (conversationId: string) => Promise<any[]>;
@@ -133,6 +224,20 @@
133
224
  showConsoleControl?: boolean;
134
225
  /** Increment to bounce the Send button and show the publish-first tooltip (host-driven). */
135
226
  sendNudgeToken?: number;
227
+ /**
228
+ * Ghost text after the caret in the message box: a likely continuation of what is typed, taken with Tab (or a tap on it).
229
+ * The host builds the `source` (a call to its own service, or a fixed list) and may hear what happened to each suggestion
230
+ * through `onEvent`. Nothing is drawn or asked while this is not given; Enter still sends and the suggestion goes with it.
231
+ */
232
+ completion?: ComposerCompletion;
233
+ /**
234
+ * Which attached files the message may refer to: after a `#` the person gets a menu of them, with their pictures, and a choice
235
+ * becomes a badge (picture, name, X) in the text. By default the pictures; a host that sends other files to a model that can use
236
+ * them says so here. It is asked when a file is attached: the file that passes is given the label it is called by.
237
+ */
238
+ isReferenceableAttachment?: (attachment: AttachmentInput) => boolean;
239
+ /** The most files one message may refer to (the host's request has a cap): later references stay plain text. Default: no cap. */
240
+ maxReferences?: number;
136
241
  consoleOpen?: boolean;
137
242
  onToggleConsole?: () => void;
138
243
  /** Custom title displayed in the header. If empty/undefined, defaults to context.title or context.id. */
@@ -183,6 +288,26 @@
183
288
  generationThinking?: string;
184
289
  /** Gateway build plan (`operation.metadata.plan`), or `null` to hide the plan card. */
185
290
  generationPlan?: import('../utils/generationPlan.js').GenerationPlanWire | null;
291
+ /**
292
+ * The host asked the gateway for a build plan and it has not arrived yet. While `generationPlan` is still `null` the plan card is
293
+ * drawn closed, saying "Planning" in a shimmer, and it opens when the plan comes. Lower it when the plan arrives or when no plan is
294
+ * coming; it also ends by itself once code starts to stream. Left `false` (the default), nothing is drawn until `generationPlan`
295
+ * arrives, as before.
296
+ */
297
+ generationPlanPending?: boolean;
298
+ /**
299
+ * What the last run changed in the widget's files (`operation.metadata.codeChanges`, read with `readCodeChangesFromMetadata`), or
300
+ * `null` when it changed nothing worth showing: the first message of a conversation, a build that was not kept, a gateway that does
301
+ * not send it. The code card under that run's conclusion then opens on a read-only diff, the lines taken out in red and the lines
302
+ * put in in green, with "Code" one press away.
303
+ *
304
+ * Unlike the live props above it is NOT given up when `isGenerating` ends: it is what the run left behind. Give it when the
305
+ * operation completes, and clear it (`null`) when the next run starts. It may come AFTER `isGenerating` fell (a host that lets the
306
+ * person write again as soon as the preview is up lowers the flag before the completed operation arrives): the chat then puts it
307
+ * into the work of the run that just ended. A page that forgets to clear it is safe: the chat ignores the very value it held when
308
+ * a run began.
309
+ */
310
+ generationChanges?: import('../utils/codeChanges.js').CodeChanges | null;
186
311
  onStopGeneration?: () => void;
187
312
  /**
188
313
  * Renders the inline design-control card attached to an assistant reply
@@ -233,6 +358,12 @@
233
358
  * does not wrap, and a message with code blocks, keep the width CSS gives them. On by default; pass `false` for CSS's widths.
234
359
  */
235
360
  tightBubbles?: boolean;
361
+ /**
362
+ * Draws a file badge (the picture and `#name`, no X) in a sent message for each word that names one of the files attached to it,
363
+ * as the message box drew it while the message was written. A word that cannot be matched to a file for sure stays plain text.
364
+ * On by default; `false` shows the text as it was sent.
365
+ */
366
+ referenceBadges?: boolean;
236
367
  /** Show the Widget Element Selector toggle button in the action bar (disabled by default). */
237
368
  showSelector?: boolean;
238
369
  /** Whether the Widget Element Selector is currently active for this chat's widget. */
@@ -245,6 +376,25 @@
245
376
  onRemoveSelectedElement?: (elementId: string) => void;
246
377
  /** Fired when all selected widget elements should be cleared (e.g. after sending). */
247
378
  onClearSelectedElements?: () => void;
379
+ /**
380
+ * The part whose note is open over the preview, if any. Its chip is drawn as active. Together with
381
+ * `onSelectedElementClick` it lets a chip and the part's marker on the preview open and fold the same note.
382
+ */
383
+ activeSelectedElementId?: string | null;
384
+ /**
385
+ * Fired when the body of a part's chip is clicked: the host shows that part's note over the preview (or folds it when it is
386
+ * already open). Left out, the chips only say what they hold and are not clickable.
387
+ */
388
+ onSelectedElementClick?: (elementId: string) => void;
389
+ /**
390
+ * What the person can pick with `@` in the message box: lists of rows that the page hands over (connected apps, compositions, ...;
391
+ * see `MentionSource`). A pick is drawn as a chip in the message box and goes out with the message as `mentions` in the options of
392
+ * `onSendMessage`; its name is written into the text as well, as the person sees it. A link in the text that a row is made for
393
+ * (`linkHosts`) is offered as a one-tap pick. Left out or empty, `@` is an ordinary character and the box is as it always was.
394
+ */
395
+ mentionSources?: readonly MentionSource[];
396
+ /** The most picks one message holds (5 when left out). Once it is reached the menu shows the other rows dimmed and says why. */
397
+ mentionLimit?: number;
248
398
  /** Model list for the DS composer Assistant menu; hidden when empty. */
249
399
  composerModels?: ComposerModelOption[];
250
400
  selectedComposerModelId?: string | null;
@@ -253,10 +403,19 @@
253
403
 
254
404
  // Expose methods to parent component
255
405
  interface ChatMethods {
256
- sendMessage: (content: string, attachments?: Array<File | AttachmentInput>, options?: { skipIntentGate?: boolean; chipIntent?: ClarificationSuggestion['intent'] }) => Promise<void>;
406
+ sendMessage: (content: string, attachments?: Array<File | AttachmentInput>, options?: { skipIntentGate?: boolean; chipIntent?: ClarificationSuggestion['intent']; questionAnswers?: QuestionCardAnswer[]; references?: Array<{ id: string; label: string }> }) => Promise<void>;
257
407
  addAssistantMessage: (content: string, skipPersist?: boolean, suggestions?: ClarificationSuggestion[], designControls?: DesignControlRequest) => void;
258
408
  /** Add an assistant reply carrying an inline design-control card (no generation). */
259
409
  addDesignControlsMessage: (content: string, designControls: DesignControlRequest) => void;
410
+ /**
411
+ * Add an assistant reply carrying the card of questions the assistant puts before it builds (the gateway's
412
+ * `metadata.clarification`, made a `QuestionCardModel` with `questionCardFromClarification`). The card is open while it is the
413
+ * newest message: the person answers it in the card (`onQuestionsAnswered`), or writes in the chat instead (the message is
414
+ * sent with every question skipped, `questionAnswers`). `content` is the plain words of the card, and what the conversation
415
+ * shows when the card is closed with its picks unknown; left empty it is made from the card. Pass `skipPersist` true when the
416
+ * backend already saved this reply itself (the gateway does), so it is not saved twice.
417
+ */
418
+ addQuestionCardMessage: (card: QuestionCardModel, content?: string, skipPersist?: boolean) => void;
260
419
  addUserMessage: (content: string, attachments?: Array<{ id: string; url: string; file_type: string; file_name: string; file_size?: number }>) => void;
261
420
  /** Returns the current conversation ID (null if no conversation is active). */
262
421
  getConversationId: () => string | null;
@@ -290,12 +449,13 @@
290
449
  updateLastUserMessageAttachments: (attachments: Array<{ id?: string; url: string; file_type?: string; file_name?: string | null }>) => void;
291
450
  }
292
451
 
293
- let { context, config = {}, class: className = '', locale = null, messages: languageMessages = null, dir = undefined, style = '', isCompleted = false, onStatsUpdate, onChatReady, onCompletionToggle, onSendMessage, onLoadConversations, onLoadMessages, onCreateConversation, onSaveMessage, onRestoreCheckpointBackend, onRateVersion, versionRatings: ratingsFromHost = {}, onTakeScreenshot, onConversationChange, onDraftChange, onRetryMessage, onSketchUndo, onUpgradePlans, onUploadChatFile, showHeader = true, showPanelHeader = false, showConsoleControl = false, sendNudgeToken = 0, consoleOpen = false, onToggleConsole, headerTitle = '', showDescriptionHeader = true, showLogs = true, resizeExpandsContainer = false, onMessagesResize, onMessagesDrag, onMessagesDragEnd, onLoadOlderMessages, widgetHeadCommitSha = null, versionLabelByCommitSha = {}, versionLabelByPrompt = {}, versionTimeline = [], isGenerating = false, generationStream = '', generationThinking = '', generationPlan = null, onStopGeneration, members = [], currentUser = null, renderAgentAvatar, agentSound, tightBubbles = true, renderDesignControls, actionBarExtras, completeReplacement, replaceComplete = false, showSelector = false, isSelectorActive = false, selectedElements = [], onToggleSelector, onRemoveSelectedElement, onClearSelectedElements, composerExtras, storedLineLabel, composerModels = [], selectedComposerModelId = null, onSelectComposerModel }: Props = $props();
452
+ let { context, config = {}, class: className = '', locale = null, messages: languageMessages = null, dir = undefined, style = '', isCompleted = false, onStatsUpdate, onChatReady, onCompletionToggle, onSendMessage, onQuestionsAnswered, onLoadConversations, onLoadMessages, onCreateConversation, onSaveMessage, onRestoreCheckpointBackend, onRateVersion, versionRatings: ratingsFromHost = {}, onTakeScreenshot, onConversationChange, onDraftChange, onRetryMessage, onSketchUndo, onUpgradePlans, onUploadChatFile, showHeader = true, showPanelHeader = false, showConsoleControl = false, sendNudgeToken = 0, completion, isReferenceableAttachment = isReferenceablePicture, maxReferences, consoleOpen = false, onToggleConsole, headerTitle = '', showDescriptionHeader = true, showLogs = true, resizeExpandsContainer = false, onMessagesResize, onMessagesDrag, onMessagesDragEnd, onLoadOlderMessages, widgetHeadCommitSha = null, versionLabelByCommitSha = {}, versionLabelByPrompt = {}, versionTimeline = [], isGenerating = false, generationStream = '', generationThinking = '', generationPlan = null, generationPlanPending = false, generationChanges = null, onStopGeneration, members = [], currentUser = null, renderAgentAvatar, agentSound, tightBubbles = true, referenceBadges = true, renderDesignControls, actionBarExtras, completeReplacement, replaceComplete = false, showSelector = false, isSelectorActive = false, selectedElements = [], onToggleSelector, onRemoveSelectedElement, onClearSelectedElements, activeSelectedElementId = null, onSelectedElementClick, mentionSources = [], mentionLimit = DEFAULT_MENTION_LIMIT, composerExtras, storedLineLabel, composerModels = [], selectedComposerModelId = null, onSelectComposerModel }: Props = $props();
294
453
 
295
454
  // The words of everything inside the chat, in the language the page chose. The page can change the language while the
296
455
  // chat is open: the settings are read again whenever they change (and setting the same language again does nothing).
297
456
  const language = provideChatLanguage(untrack(() => ({ locale, messages: languageMessages })));
298
- const { t } = language;
457
+ // `spokenLocale` is the language's own: the prop `locale` above is only what the page asked for.
458
+ const { t, locale: spokenLocale } = language;
299
459
  $effect(() => {
300
460
  language.update({ locale, messages: languageMessages });
301
461
  });
@@ -326,6 +486,9 @@
326
486
  }
327
487
 
328
488
  function notifyDraftChange() {
489
+ // While a queued message is open in the composer, the composer holds that message and not the person's draft: the host keeps the
490
+ // draft it was told of before the edit, and hears of it again when the edit ends (see restoreStashedDraft).
491
+ if (queueEditingId) return;
329
492
  onDraftChange?.();
330
493
  }
331
494
 
@@ -365,10 +528,178 @@
365
528
  let pendingSkipIntentGate = $state(false);
366
529
  let skipNextDraftIntentClear = $state(false);
367
530
  let localAttachments = $state<AttachmentInput[]>([]);
531
+ /** What the person picked with `@` for the message they are writing: a chip each. Sent with the message, then cleared. */
532
+ let localMentions = $state<readonly ComposerMention[]>([]);
368
533
  let localIsRecording = $state(false);
369
534
  let localIsSending = $state(false);
370
535
  let localIsUploading = $state(false);
371
536
 
537
+ // ===== The messages that wait for the agent (the rules are in messageQueue.ts) =====
538
+ /** Messages sent while the agent works, the oldest first. Kept in memory only: a refresh drops them. */
539
+ let queuedMessages = $state.raw<QueuedMessage[]>([]);
540
+ /** The queued message that is open in the composer, if one is. */
541
+ let queueEditingId = $state<string | null>(null);
542
+ /** The person pressed Stop while messages waited: nothing is sent by itself until one is sent now. */
543
+ let queuePaused = $state(false);
544
+ /** The message the person asked to send now, while the agent has not stopped yet. */
545
+ let queueSendNowId = $state<string | null>(null);
546
+ /** What the composer held before a queued message was opened in it; it comes back when the edit ends. */
547
+ let queueStashedDraft: StashedDraft | null = null;
548
+ /**
549
+ * The pictures of the message that is being edited, as they were. Taking one out of the tray must not give its blob address
550
+ * back, because closing the edit brings the picture back with the message.
551
+ */
552
+ let queueEditProtectedPreviews = new Set<string>();
553
+ /** The files the message had when its edit began. */
554
+ let queueEditOriginalAttachments: AttachmentInput[] = [];
555
+ let queueSendNowTimer: ReturnType<typeof setTimeout> | null = null;
556
+ /** The conversation the waiting messages belong to. */
557
+ let queueConversationId: string | null = null;
558
+
559
+ // --- References to attached files: `#hero_banner` in the message ------------------------------------------------------------------
560
+ // A file that can be referenced is given a label when it joins the tray (`referenceLabel`) and keeps it as long as it stays. The box
561
+ // offers the files after a `#`, and a choice becomes a badge in the text; what is sent is the plain text, with the files it names.
562
+
563
+ /** The picture of a file at twice the size the menu draws it at (a badge draws it far smaller): a small copy when the file is on the CDN. */
564
+ const REFERENCE_PREVIEW_WIDTH_PX = 320;
565
+ /** The highest `Image_N` this draft has handed out: the next nameless file is above it even when that file has left the tray. */
566
+ let referenceGenericFloor = 0;
567
+ /** The label of the file that a badge in the text, or a row of the `#` menu, is on: the tray rings its tile. */
568
+ let highlightedReferenceLabel = $state<string | null>(null);
569
+ /** How many pictures a restored draft is still bringing in: until they are all in, a badge for a missing file may be waiting for one. */
570
+ let pendingAttachmentRestores = $state(0);
571
+ /** A text the host set (a draft restored) that has not been checked yet against the files of the tray. */
572
+ let draftBadgesNeedCheck = $state(false);
573
+ /**
574
+ * The labels (lower case) a restore handed out to its pictures, until the restored text has been checked against them.
575
+ * A plain Set on purpose (the autofixer would have a SvelteSet): this is bookkeeping that the check reads inside an effect and
576
+ * empties when it is done, so it must not be something that effect depends on, or the check would wake itself.
577
+ */
578
+ let restoredLabelsToCheck = new Set<string>();
579
+
580
+ /** The address a picture of the file is drawn from in the menu and in a badge: a small copy when it can be had, else the file's own. */
581
+ function referencePreviewUrl(attachment: AttachmentInput): string {
582
+ const ownUrl = readAttachmentPreviewUrl(attachment);
583
+ return buildTransformedUrl(ownUrl, { width: REFERENCE_PREVIEW_WIDTH_PX, fit: 'scale-down', quality: 80 }) || ownUrl;
584
+ }
585
+
586
+ /** The files the message may refer to, in tray order, for the box's `#` menu and its badges. */
587
+ const referenceItems = $derived(
588
+ buildReferenceItems(localAttachments, {
589
+ isReferenceable: isReferenceableAttachment,
590
+ previewUrlOf: referencePreviewUrl,
591
+ sizeLabelOf: (attachment) => fileSizeText(attachment.file?.size, $t, $spokenLocale),
592
+ }),
593
+ );
594
+
595
+ /**
596
+ * Gives each file that can be referenced the label it is called by, as it joins the tray: a name that says something gives its label,
597
+ * a name that says nothing (a screenshot, a camera's name, a paste) is `Image_N`, N above every number this draft has handed out.
598
+ * `forcedLabels` are labels that were chosen beforehand (a restored draft's pictures, numbered in the order they were asked for).
599
+ * It is called in the same breath as the files are added to the tray, so two batches that arrive together never share a label.
600
+ */
601
+ function labelNewAttachments(entries: AttachmentInput[], forcedLabels?: Array<string | undefined>): void {
602
+ try {
603
+ const takenLabels = labelsOfAttachments(localAttachments);
604
+ entries.forEach((entry, entryIndex) => {
605
+ if (!isReferenceableAttachment(entry)) return;
606
+ const label =
607
+ forcedLabels?.[entryIndex] ??
608
+ assignReferenceLabel(entry.file?.name ?? '', { takenLabels, genericNumberFloor: referenceGenericFloor });
609
+ entry.referenceLabel = label;
610
+ takenLabels.push(label);
611
+ const genericNumber = genericLabelNumber(label);
612
+ if (genericNumber !== null) referenceGenericFloor = Math.max(referenceGenericFloor, genericNumber);
613
+ chatLog('[Chat] File can be referenced as', `#${label}`, '←', entry.file?.name);
614
+ });
615
+ } catch (error) {
616
+ // A file without a label is a file the message cannot refer to; it is still attached and sent.
617
+ console.warn('[Chat] Could not give the attached file(s) a label to refer to:', error);
618
+ }
619
+ }
620
+
621
+ /** A badge in the text, or a row of the `#` menu, is on a file (or has left it): the tray rings the same file. */
622
+ function handleReferenceHover(event: ReferenceHoverEvent): void {
623
+ highlightedReferenceLabel = event.label;
624
+ }
625
+
626
+ /** The composer sends: the message goes with the files its text names (`sendOptions.references`), when it names any. */
627
+ function handleComposerSend(
628
+ content: string,
629
+ attachmentInputs: AttachmentInput[] | undefined,
630
+ sendOptions?: ComposerSendOptions,
631
+ ): void {
632
+ const references = sendOptions?.references ?? [];
633
+ void sendMessage(content, attachmentInputs, references.length > 0 ? { references } : undefined);
634
+ }
635
+
636
+ /** The highest `Image_N` number a text names, as a badge or as a `#Image_N` typed by hand (0 when it names none). */
637
+ function highestGenericNumberIn(text: string): number {
638
+ let highestNumber = 0;
639
+ for (const occurrence of referenceOccurrences(text)) {
640
+ highestNumber = Math.max(highestNumber, genericLabelNumber(occurrence.label) ?? 0);
641
+ }
642
+ return highestNumber;
643
+ }
644
+
645
+ /** Raises the floor of the `Image_N` numbers over the ones a text already names: a `#Image_2` of a draft must never mean a later file. */
646
+ function raiseGenericFloorOver(text: string): void {
647
+ referenceGenericFloor = Math.max(referenceGenericFloor, highestGenericNumberIn(text));
648
+ }
649
+
650
+ /**
651
+ * The restored text names files the way the tray had them before the refresh, and a restore gives its pictures `Image_N` again, from
652
+ * the number one: `#Image_1` of the text may be a different picture than the first one now. A restored picture that answers to a
653
+ * word the text already names is therefore called something else, so no word of a restored draft is ever tied to a picture by luck.
654
+ */
655
+ function renameRestoredPicturesNamedByText(text: string): void {
656
+ if (restoredLabelsToCheck.size === 0) return;
657
+ const namedInText = new Set(referenceOccurrences(text).map((occurrence) => occurrence.label.toLowerCase()));
658
+ const takenLabels = labelsOfAttachments(localAttachments);
659
+ const numberFloor = Math.max(referenceGenericFloor, highestGenericNumberIn(text));
660
+ let renamedCount = 0;
661
+ const checkedAttachments = localAttachments.map((attachment) => {
662
+ const label = attachment.referenceLabel?.toLowerCase();
663
+ if (!label || !restoredLabelsToCheck.has(label) || !namedInText.has(label)) return attachment;
664
+ const newLabel = assignReferenceLabel('', {
665
+ takenLabels,
666
+ genericNumberFloor: Math.max(numberFloor, referenceGenericFloor),
667
+ });
668
+ takenLabels.push(newLabel);
669
+ referenceGenericFloor = Math.max(referenceGenericFloor, genericLabelNumber(newLabel) ?? 0);
670
+ renamedCount += 1;
671
+ chatLog('[Chat] A restored picture answered to a word the restored text names; it is now', `#${newLabel}`, 'not', `#${attachment.referenceLabel}`);
672
+ // A copy, as the tray's other updates are: the entry the tray holds is not changed in place
673
+ return { ...attachment, referenceLabel: newLabel };
674
+ });
675
+ if (renamedCount > 0) localAttachments = checkedAttachments;
676
+ }
677
+
678
+ // A draft the host restores can hold badges for files that are not in the tray (they were not kept, or are not back yet): once the
679
+ // pictures that are on their way are in, a badge whose file is missing is plain `#label` again, so no badge shows for a file that is
680
+ // not there. The text is not read here, only the files: typing does not run this. It is an effect, and not a step of setDraftText,
681
+ // because a host sets the text and asks for the pictures in the same tick (in either order): the check runs after both calls.
682
+ $effect(() => {
683
+ if (!draftBadgesNeedCheck || pendingAttachmentRestores > 0) return;
684
+ untrack(() => {
685
+ try {
686
+ // First the pictures that came back: none answers to a word the restored text already names ...
687
+ renameRestoredPicturesNamedByText(localInputText);
688
+ // ... so a badge in that text has no file by now, unless the person made it since: it is plain `#label` again
689
+ const checkedText = unbadgeTokensWithoutFiles(localInputText, labelsOfAttachments(localAttachments));
690
+ if (checkedText !== localInputText) {
691
+ chatLog('[Chat] A restored draft had badges for files that are not attached: they are plain text again');
692
+ localInputText = checkedText;
693
+ }
694
+ } catch (error) {
695
+ console.warn('[Chat] Could not check the restored draft against the attached files:', error);
696
+ } finally {
697
+ restoredLabelsToCheck = new Set();
698
+ draftBadgesNeedCheck = false;
699
+ }
700
+ });
701
+ });
702
+
372
703
  // The host hears about the draft when the person types (the setter of the composer binding below), not from an
373
704
  // effect: an effect would call the host's callback inside its own tracking, and every piece of host state the
374
705
  // callback reads would become a dependency of this effect. The creator's callback reads and writes the panel
@@ -398,6 +729,10 @@
398
729
  }
399
730
  }
400
731
  }
732
+ // The waiting messages go with the chat: their pictures are given back and nothing is sent once the chat is gone
733
+ clearQueueSendNowTimer();
734
+ releasePreviewAddresses(blobPreviewsOf(queuedMessages.flatMap((message) => message.attachments)));
735
+ if (queueStashedDraft) releasePreviewAddresses(blobPreviewsOf(queueStashedDraft.attachments));
401
736
  });
402
737
 
403
738
  // Derived values from chat state
@@ -675,14 +1010,36 @@
675
1010
  }
676
1011
  }
677
1012
 
678
- async function sendMessage(content: string, attachments?: Array<File | AttachmentInput>, options?: { skipIntentGate?: boolean; chipIntent?: ClarificationSuggestion['intent'] }) {
679
- const mergedOptions = {
680
- ...options,
681
- chipIntent: options?.chipIntent ?? pendingChipIntent ?? undefined,
682
- skipIntentGate: options?.skipIntentGate === true || pendingSkipIntentGate,
1013
+ async function sendMessage(content: string, attachments?: Array<File | AttachmentInput>, options?: { skipIntentGate?: boolean; chipIntent?: ClarificationSuggestion['intent']; questionAnswers?: QuestionCardAnswer[]; references?: Array<{ id: string; label: string }> }) {
1014
+ await sendMessageAndReport(content, attachments, options);
1015
+ }
1016
+
1017
+ /**
1018
+ * Does what sendMessage does, and says whether the message was handed over to the host. A message that waited in the queue is
1019
+ * sent with `keepDraft`: the person may be writing the next one meanwhile, so it takes nothing from the composer (the text,
1020
+ * the files, the clarification chip and the selected parts of the widget stay as they are).
1021
+ */
1022
+ async function sendMessageAndReport(
1023
+ content: string,
1024
+ attachments?: Array<File | AttachmentInput>,
1025
+ options?: { skipIntentGate?: boolean; chipIntent?: ClarificationSuggestion['intent']; questionAnswers?: QuestionCardAnswer[]; references?: Array<{ id: string; label: string }>; keepDraft?: boolean },
1026
+ ): Promise<boolean> {
1027
+ const keepDraft = options?.keepDraft === true;
1028
+ const mergedOptions: {
1029
+ skipIntentGate: boolean;
1030
+ chipIntent: ClarificationSuggestion['intent'] | undefined;
1031
+ questionAnswers?: QuestionCardAnswer[];
1032
+ references?: Array<{ id: string; label: string }>;
1033
+ } = {
1034
+ questionAnswers: options?.questionAnswers,
1035
+ references: options?.references,
1036
+ chipIntent: options?.chipIntent ?? (keepDraft ? undefined : pendingChipIntent) ?? undefined,
1037
+ skipIntentGate: options?.skipIntentGate === true || (!keepDraft && pendingSkipIntentGate),
683
1038
  };
684
- pendingChipIntent = null;
685
- pendingSkipIntentGate = false;
1039
+ if (!keepDraft) {
1040
+ pendingChipIntent = null;
1041
+ pendingSkipIntentGate = false;
1042
+ }
686
1043
  if (!currentConversationId) {
687
1044
  if (initializePromise) {
688
1045
  try {
@@ -696,11 +1053,11 @@
696
1053
  await createNewConversation();
697
1054
  } catch (convErr) {
698
1055
  console.error('No active conversation and failed to create one:', convErr);
699
- return;
1056
+ return false;
700
1057
  }
701
1058
  if (!currentConversationId) {
702
1059
  console.error('No active conversation after create');
703
- return;
1060
+ return false;
704
1061
  }
705
1062
  }
706
1063
  }
@@ -708,7 +1065,26 @@
708
1065
  const activeConversationId = currentConversationId;
709
1066
  if (!activeConversationId) {
710
1067
  console.error('No active conversation');
711
- return;
1068
+ return false;
1069
+ }
1070
+
1071
+ // A card of questions is waiting and the person writes in the chat instead of answering it: every question is skipped, and what
1072
+ // they wrote is what the assistant goes on with (the host sends the message with these empty answers). The card has to be looked
1073
+ // for BEFORE the message is added below: once it is, the card is no longer the newest message (and it is drawn as skipped by
1074
+ // that very fact, in this same pass).
1075
+ if (mergedOptions.questionAnswers === undefined) {
1076
+ try {
1077
+ const waitingCard = openQuestionCardOf(getCurrentMessages(activeConversationId) ?? []);
1078
+ if (waitingCard) {
1079
+ mergedOptions.questionAnswers = skippedAnswersOf(waitingCard.card);
1080
+ chatLog('[Chat] A message was written while a card of questions waited: every question is skipped.', {
1081
+ 'Card': waitingCard.messageId,
1082
+ 'Questions': mergedOptions.questionAnswers.length
1083
+ });
1084
+ }
1085
+ } catch (cardError) {
1086
+ chatWarn('[Chat] The card of questions that waits could not be looked for:', cardError);
1087
+ }
712
1088
  }
713
1089
 
714
1090
  const normalizedAttachments: AttachmentInput[] = (attachments || []).map((att) => {
@@ -726,7 +1102,7 @@
726
1102
  });
727
1103
 
728
1104
  // chatLog('[Chat] Sending message:', content.substring(0, 60));
729
- const noteLines = (selectedElements ?? [])
1105
+ const noteLines = (keepDraft ? [] : (selectedElements ?? []))
730
1106
  .map((element) => element.note?.trim())
731
1107
  .filter((note): note is string => !!note);
732
1108
  const submittedContent = content.trim()
@@ -734,7 +1110,9 @@
734
1110
  : noteLines.join('\n');
735
1111
  const submittedAttachments = normalizedAttachments;
736
1112
  const submittedSelectedElements =
737
- selectedElements && selectedElements.length > 0 ? [...selectedElements] : undefined;
1113
+ !keepDraft && selectedElements && selectedElements.length > 0 ? [...selectedElements] : undefined;
1114
+ // What the person picked with @ goes out with the message, in the order they picked it. Nothing picked, nothing sent.
1115
+ const submittedMentions = !keepDraft && localMentions.length > 0 ? localMentions.map((mention) => ({ ...mention })) : undefined;
738
1116
  const displayMessageContent = submittedSelectedElements
739
1117
  ? `[Selected: ${submittedSelectedElements.map((el) => el.label).join(', ')}]\n${submittedContent}`
740
1118
  : submittedContent;
@@ -744,15 +1122,23 @@
744
1122
 
745
1123
  // Clear the controlled input immediately. Waiting for persistence/codegen lets
746
1124
  // image thumbnails linger during long generations and can re-save them as drafts.
747
- localInputText = '';
748
- localAttachments = [];
749
- if (submittedSelectedElements) {
750
- onClearSelectedElements?.();
751
- if (isSelectorActive) {
752
- onToggleSelector?.(false);
1125
+ // (A message that waited in the queue leaves the composer alone: what is in it now is the person's next message.)
1126
+ if (!keepDraft) {
1127
+ localInputText = '';
1128
+ localAttachments = [];
1129
+ localMentions = [];
1130
+ // The next message starts a new tray: its `Image_N` numbers start again, and no tile is rung for a file that left with this one
1131
+ referenceGenericFloor = 0;
1132
+ highlightedReferenceLabel = null;
1133
+ restoredLabelsToCheck = new Set();
1134
+ if (submittedSelectedElements) {
1135
+ onClearSelectedElements?.();
1136
+ if (isSelectorActive) {
1137
+ onToggleSelector?.(false);
1138
+ }
753
1139
  }
1140
+ notifyDraftChange();
754
1141
  }
755
- notifyDraftChange();
756
1142
 
757
1143
  // Clear head/checkpoint marks — new prompt starts a new turn
758
1144
  const existingMsgs = getCurrentMessages(activeConversationId);
@@ -821,23 +1207,30 @@
821
1207
  fileType: resolvedFileType,
822
1208
  fileName: rawFileName,
823
1209
  fileSize: att.file?.size,
1210
+ // The word the text calls this file by, if it has one: how the host ties a `#word` to the file it is sent with
1211
+ referenceLabel: att.referenceLabel,
824
1212
  };
825
1213
  });
826
1214
  await onSendMessage(
827
1215
  submittedContent,
828
1216
  outgoing.length > 0 ? outgoing : undefined,
829
- submittedSelectedElements
830
- ? { ...mergedOptions, selectedElements: submittedSelectedElements }
1217
+ submittedSelectedElements || submittedMentions
1218
+ ? {
1219
+ ...mergedOptions,
1220
+ ...(submittedSelectedElements ? { selectedElements: submittedSelectedElements } : {}),
1221
+ ...(submittedMentions ? { mentions: submittedMentions } : {}),
1222
+ }
831
1223
  : mergedOptions
832
1224
  );
833
1225
  } else {
834
1226
  const aiResponse = await chatService.processAIResponse(activeConversationId, userMessage);
835
1227
  chatStore.actions.addMessage(activeConversationId, aiResponse);
836
1228
  }
837
-
1229
+ return true;
838
1230
  } catch (error) {
839
1231
  console.error('Failed to send message:', error);
840
1232
  showFailure('chat.error.sendMessage');
1233
+ return false;
841
1234
  } finally {
842
1235
  localIsSending = false;
843
1236
  }
@@ -846,7 +1239,7 @@
846
1239
  /** Add an assistant message to the current conversation (called by parent via chatMethods).
847
1240
  * When skipPersist is true, the message is shown locally but not saved to DB
848
1241
  * (use when the backend already persists its own version). */
849
- function addAssistantMessage(content: string, skipPersist = false, suggestions?: ClarificationSuggestion[], designControls?: DesignControlRequest) {
1242
+ function addAssistantMessage(content: string, skipPersist = false, suggestions?: ClarificationSuggestion[], designControls?: DesignControlRequest, questionCard?: QuestionCardModel) {
850
1243
  if (!currentConversationId) return;
851
1244
  const assistantMsg = {
852
1245
  id: `assistant_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
@@ -862,7 +1255,10 @@
862
1255
  _failed: false,
863
1256
  _retryCount: 0,
864
1257
  ...(suggestions && suggestions.length > 0 ? { suggestions } : {}),
865
- ...(designControls ? { designControls } : {})
1258
+ ...(designControls ? { designControls } : {}),
1259
+ // The card of questions rides on the message in memory; the backend keeps its own copy (the gateway saves the card with the
1260
+ // reply), which is what a reload brings back
1261
+ ...(questionCard ? { questionCard } : {})
866
1262
  };
867
1263
  chatStore.actions.addMessage(currentConversationId, assistantMsg);
868
1264
 
@@ -903,6 +1299,74 @@
903
1299
  addAssistantMessage(content, false, undefined, designControls);
904
1300
  }
905
1301
 
1302
+ /** Add an assistant reply that carries the card of questions the assistant puts before it builds (see `ChatMethods`). Nothing is
1303
+ * generated; the card is open until the person answers it or writes in the chat. */
1304
+ function addQuestionCardMessage(card: QuestionCardModel, content?: string, skipPersist = false) {
1305
+ try {
1306
+ const words = (content ?? '').trim() || questionCardPlainText(card);
1307
+ chatLog('[Chat] A card of questions was added:', { 'Questions': card.questions.length, 'Saved by the host': skipPersist });
1308
+ // The run that asked built nothing: the status line it put in the turn ("Code generation in progress…") has nothing more to
1309
+ // say, and would stay above the card for good
1310
+ dropCodegenProgressOfTurn();
1311
+ addAssistantMessage(words, skipPersist, undefined, undefined, card);
1312
+ } catch (error) {
1313
+ chatWarn('[Chat] The card of questions could not be added:', error);
1314
+ }
1315
+ }
1316
+
1317
+ /**
1318
+ * Takes the in-flight status lines ("Code generation in progress…", "Planning…") out of the turn that is on. They are local
1319
+ * (never saved) and are updated in place while a build goes; a run that ends by asking questions has no result to put in them.
1320
+ */
1321
+ function dropCodegenProgressOfTurn() {
1322
+ if (!currentConversationId) return;
1323
+ const conversationId = currentConversationId;
1324
+ const messagesOfConversation = getCurrentMessages(conversationId);
1325
+ const turnStart = getLastUserMessageIndex(messagesOfConversation) + 1;
1326
+ const messagesKept = messagesOfConversation.filter(
1327
+ (message, index) =>
1328
+ index < turnStart || message.messageType !== 'assistant' || !isCodegenProgressMessage(message.messageContent)
1329
+ );
1330
+ if (messagesKept.length === messagesOfConversation.length) return;
1331
+ chatStore.actions.setMessages(conversationId, messagesKept);
1332
+ chatLog('[Chat] The status lines of the run that asked were taken out of the turn:', messagesOfConversation.length - messagesKept.length);
1333
+ }
1334
+
1335
+ /**
1336
+ * The person answered the card of a reply in the card itself (Submit on its last question, or Skip). The answers are kept on the
1337
+ * message first, so the card is drawn closed in this very pass (what was picked, or "Skipped"), and then the host is told, which
1338
+ * sends the request that asked the questions again with these answers. If the host cannot, the card opens again.
1339
+ */
1340
+ async function handleQuestionCardAnswered(detail: { messageId: string; answers: QuestionCardAnswer[]; skippedAll: boolean }) {
1341
+ const conversationId = currentConversationId;
1342
+ if (!conversationId) return;
1343
+ const message = (getCurrentMessages(conversationId) ?? []).find((candidate) => candidate.id === detail.messageId);
1344
+ const card = message?.questionCard;
1345
+ if (!message || !card) {
1346
+ chatWarn('[Chat] The card that was answered is not in the conversation any more:', detail.messageId);
1347
+ return;
1348
+ }
1349
+ // Answered (or being answered) already: nothing more to do. This is also what makes a double press harmless.
1350
+ if (card.result) return;
1351
+ // A run is on: the answers would be sent next to another request. The card stays open.
1352
+ if (isGenerating || localIsSending) {
1353
+ chatWarn('[Chat] A card of questions was answered while a request was on its way: it stays open.');
1354
+ return;
1355
+ }
1356
+ chatStore.actions.updateMessageQuestionCard(conversationId, message.id, { ...card, result: { answers: detail.answers } });
1357
+ try {
1358
+ await onQuestionsAnswered?.(detail);
1359
+ } catch (error) {
1360
+ // The answers did not go: put the card back as it was, so the person can answer it again
1361
+ chatWarn('[Chat] The answers of the card of questions could not be sent:', error);
1362
+ const current = (getCurrentMessages(conversationId) ?? []).find((candidate) => candidate.id === detail.messageId);
1363
+ if (current?.questionCard) {
1364
+ chatStore.actions.updateMessageQuestionCard(conversationId, current.id, { ...current.questionCard, result: null });
1365
+ }
1366
+ showFailure('chat.error.sendMessage');
1367
+ }
1368
+ }
1369
+
906
1370
  /** Add a user message to the UI without triggering generation (called by parent via chatMethods).
907
1371
  * Also persists to DB via onSaveMessage so multi-turn history is complete. */
908
1372
  function addUserMessage(content: string, attachments?: Array<{ id: string; url: string; file_type: string; file_name: string; file_size?: number }>) {
@@ -964,11 +1428,18 @@
964
1428
  const screenshotFile = new File([blob], 'screenshot.png', { type: 'image/png' });
965
1429
 
966
1430
  const attachmentInput: AttachmentInput = {
1431
+ // How the tray and the upload find this tile again: see `updateAttachment`.
1432
+ id: generateFileId(),
967
1433
  file: screenshotFile,
968
1434
  type: 'image',
969
1435
  preview: screenshotDataUrl,
1436
+ // The tile keeps drawing this `data:` URL, and the draft keeps it too: the stored address that comes when the upload ends
1437
+ // is only the picture's second choice (see `readAttachmentPreviewUrl`).
1438
+ persistedDataUrl: screenshotDataUrl,
970
1439
  uploadProgress: 0
971
1440
  };
1441
+ // The label the message can call the screenshot by (`#Image_1`), given in the same breath as it joins the tray
1442
+ labelNewAttachments([attachmentInput]);
972
1443
 
973
1444
  localAttachments = [...localAttachments, attachmentInput];
974
1445
  const attachmentIndex = localAttachments.length - 1;
@@ -978,7 +1449,7 @@
978
1449
  console.log('[Chat] attachments now', localAttachments.length);
979
1450
 
980
1451
  if (onUploadChatFile) {
981
- await uploadAttachmentToCdn(screenshotFile, attachmentInput, attachmentIndex, {
1452
+ await uploadAttachmentToCdn(screenshotFile, attachmentInput, {
982
1453
  source: 'preview-screenshot',
983
1454
  });
984
1455
  }
@@ -1019,10 +1490,12 @@
1019
1490
 
1020
1491
  // Create AttachmentInput object
1021
1492
  const attachmentInput: AttachmentInput = {
1493
+ id: generateFileId(),
1022
1494
  file: mockRecording,
1023
1495
  type: 'video',
1024
1496
  uploadProgress: 0
1025
1497
  };
1498
+ labelNewAttachments([attachmentInput]);
1026
1499
 
1027
1500
  // Add to attachments
1028
1501
  localAttachments = [...localAttachments, attachmentInput];
@@ -1040,47 +1513,73 @@
1040
1513
  return !!file && file.size > 0;
1041
1514
  }
1042
1515
 
1043
- function bumpAttachmentProgress(attachmentIndex: number, percentage: number) {
1044
- localAttachments = localAttachments.map((att, idx) =>
1045
- idx === attachmentIndex ? { ...att, uploadProgress: percentage } : att,
1516
+ /**
1517
+ * Changes one tile of the tray. The tile is found by its `id`, never by its place: a tile is taken out of the tray (or the
1518
+ * message is sent, which empties it) while other files still upload, and the place a tile had when its upload began is then
1519
+ * another tile's. A tile that is no longer in the tray is left alone, so what its upload brings back is dropped, not handed
1520
+ * to a neighbor. The tile is replaced by a changed copy: that is what makes the tray draw it again. Returns whether the tile
1521
+ * was still in the tray.
1522
+ */
1523
+ function updateAttachment(attachmentId: string, changes: Partial<AttachmentInput>): boolean {
1524
+ if (!localAttachments.some((attachment) => attachment.id === attachmentId)) return false;
1525
+ localAttachments = localAttachments.map((attachment) =>
1526
+ attachment.id === attachmentId ? { ...attachment, ...changes } : attachment,
1046
1527
  );
1528
+ return true;
1047
1529
  }
1048
1530
 
1531
+ /**
1532
+ * Uploads the file of one tile and writes what came back (the stored address and the upload id, which the host needs) on that
1533
+ * tile, found by its id. Uploads run side by side, one for each paste, and none of them touches another's tile.
1534
+ */
1049
1535
  async function uploadAttachmentToCdn(
1050
1536
  file: File,
1051
1537
  entry: AttachmentInput,
1052
- attachmentIndex: number,
1053
1538
  options: { source?: string } = {},
1054
1539
  ): Promise<void> {
1540
+ const attachmentId = entry.id;
1541
+ if (!attachmentId) {
1542
+ chatWarn('[Chat] An attachment with no id cannot be followed while it uploads:', file.name);
1543
+ return;
1544
+ }
1545
+ // Nothing to upload: the tile keeps showing the file itself. A tile that was attached without a picture gets one now:
1546
+ // one `blob:` address per file, never a second one (`removeFooterAttachment` and `clearDraft` give it back).
1055
1547
  if (!onUploadChatFile || !isCdnUploadableFile(file)) {
1056
- // Only when the file was attached without a picture: one `blob:` address per file, never a second one.
1057
- if (!entry.preview) entry.preview = createLocalPreviewUrl(file);
1548
+ if (!entry.preview) updateAttachment(attachmentId, { preview: createLocalPreviewUrl(file) });
1058
1549
  return;
1059
1550
  }
1060
1551
 
1061
1552
  try {
1062
- bumpAttachmentProgress(attachmentIndex, 0);
1553
+ updateAttachment(attachmentId, { uploadProgress: 0 });
1063
1554
  const uploaded = await onUploadChatFile(file, {
1064
- onProgress: (percentage) => bumpAttachmentProgress(attachmentIndex, percentage),
1555
+ onProgress: (percentage) => updateAttachment(attachmentId, { uploadProgress: percentage }),
1065
1556
  source: options.source,
1066
1557
  });
1067
- entry.uploadedUrl = uploaded.url;
1068
- entry.uploadId = uploaded.id;
1069
- if (file.type.startsWith('image/') || file.type.startsWith('video/') || file.type === 'application/pdf') {
1070
- // The local picture is not given back here: the tray holds a copy of this attachment (the progress update
1071
- // replaces it) and keeps drawing that picture. `removeFooterAttachment` and `clearDraft` give it back.
1072
- entry.preview = uploaded.url;
1558
+ // The tile as the tray has it now: while the file uploaded it may have been taken out, or the message may have been sent.
1559
+ const tileInTray = localAttachments.find((attachment) => attachment.id === attachmentId);
1560
+ if (!tileInTray) {
1561
+ chatLog('[Chat] The upload ended after its attachment left the tray; what it brought is not used:', uploaded.id);
1562
+ return;
1073
1563
  }
1074
- entry.uploadProgress = 100;
1075
- bumpAttachmentProgress(attachmentIndex, 100);
1076
- localAttachments = [...localAttachments];
1564
+ // The local picture is not given back here: the tray keeps drawing it (the stored address is its second choice, see
1565
+ // `readAttachmentPreviewUrl`). A tile that has no picture yet gets the local one, once.
1566
+ const uploadedChanges: Partial<AttachmentInput> = {
1567
+ uploadedUrl: uploaded.url,
1568
+ uploadId: uploaded.id,
1569
+ uploadProgress: 100,
1570
+ };
1571
+ if (!tileInTray.preview) uploadedChanges.preview = createLocalPreviewUrl(file);
1572
+ updateAttachment(attachmentId, uploadedChanges);
1077
1573
  notifyDraftChange();
1078
1574
  // chatLog('[Chat] Attachment uploaded to CDN:', uploaded.url.slice(0, 80), 'id:', uploaded.id);
1079
1575
  } catch (uploadError) {
1080
1576
  console.error('[Chat] Upload failed, using local preview where possible:', uploadError);
1081
- if (!entry.preview) entry.preview = createLocalPreviewUrl(file);
1082
- entry.uploadProgress = 0;
1083
- bumpAttachmentProgress(attachmentIndex, 0);
1577
+ // The tile keeps the preview it was made with (the local file), so only its progress bar goes.
1578
+ const tileInTray = localAttachments.find((attachment) => attachment.id === attachmentId);
1579
+ updateAttachment(attachmentId, {
1580
+ uploadProgress: 0,
1581
+ ...(tileInTray && !tileInTray.preview ? { preview: createLocalPreviewUrl(file) } : {}),
1582
+ });
1084
1583
  // Soft warning only — local File still works for codegen vision; don't block the chat UI.
1085
1584
  const detail =
1086
1585
  uploadError instanceof Error ? uploadError.message : 'Attachment upload failed.';
@@ -1117,7 +1616,8 @@
1117
1616
 
1118
1617
  function removeFooterAttachment(index: number) {
1119
1618
  const removed = localAttachments[index];
1120
- if (removed?.preview && removed.preview.startsWith('blob:')) {
1619
+ // A picture of the queued message that is being edited stays alive when it is taken out of the tray: closing the edit brings it back
1620
+ if (removed?.preview && removed.preview.startsWith('blob:') && !queueEditProtectedPreviews.has(removed.preview)) {
1121
1621
  try {
1122
1622
  URL.revokeObjectURL(removed.preview);
1123
1623
  } catch {
@@ -1125,6 +1625,19 @@
1125
1625
  }
1126
1626
  }
1127
1627
  localAttachments = localAttachments.filter((_, i) => i !== index);
1628
+ // A badge never stands for a file that is not attached: the badges of the file go with it (a `#label` typed by hand stays)
1629
+ if (removed?.referenceLabel) {
1630
+ try {
1631
+ const withoutBadges = removeTokensOfLabel(localInputText, removed.referenceLabel);
1632
+ if (withoutBadges.removed > 0) {
1633
+ localInputText = withoutBadges.text;
1634
+ chatLog('[Chat] Took the badge(s) of the removed file out of the text:', removed.referenceLabel, withoutBadges.removed);
1635
+ }
1636
+ } catch (error) {
1637
+ console.warn('[Chat] Could not take the badges of the removed file out of the text:', error);
1638
+ }
1639
+ if (highlightedReferenceLabel === removed.referenceLabel) highlightedReferenceLabel = null;
1640
+ }
1128
1641
  notifyDraftChange();
1129
1642
  chatLog('[Chat] Removed footer attachment at index:', index);
1130
1643
  }
@@ -1160,28 +1673,32 @@
1160
1673
  async function rememberImageForDraft(entry: AttachmentInput): Promise<void> {
1161
1674
  const dataUrl = await readImageAsDataUrl(entry.file);
1162
1675
  if (!dataUrl) return;
1163
- entry.persistedDataUrl = dataUrl;
1164
1676
  // The attachment may have been removed while the file was read: only a tray that still holds it is saved.
1165
- // It is set on the object the tray holds now, not on a copy: the upload keeps writing to that object.
1166
- const inTray = localAttachments.find((att) => att.file === entry.file);
1167
- if (!inTray) return;
1168
- inTray.persistedDataUrl = dataUrl;
1169
- localAttachments = [...localAttachments];
1677
+ // The tile is found by its id and changed the way an upload changes it, so the two never write over each other.
1678
+ if (!entry.id || !updateAttachment(entry.id, { persistedDataUrl: dataUrl })) return;
1170
1679
  notifyDraftChange();
1171
1680
  }
1172
1681
 
1173
- async function addFilesAsAttachments(files: File[]) {
1174
- const incoming = (files || []).filter((file) => file && file.size > 0);
1175
- if (incoming.length === 0) return;
1176
- const usableFiles = await Promise.all(
1177
- incoming.map(async (file) =>
1178
- file.type.startsWith('image/') ? await prepareChatImageFileForUpload(file) : file,
1179
- ),
1180
- );
1682
+ /**
1683
+ * Attaches files to the message being written: the ones dropped on the box, picked with "+", or pasted into it. A file may
1684
+ * come with the words its tile says in place of its name (the first words of a pasted text). The tiles are put in the tray
1685
+ * first and the files are uploaded after, one by one; `onTilesAdded` is called between the two, so a caller that keeps a
1686
+ * line of pastes (see `addPastedAttachments`) can let the next paste in while these files are still uploading.
1687
+ */
1688
+ async function attachFiles(
1689
+ entries: Array<{ file: File; label?: string }>,
1690
+ options: { onTilesAdded?: () => void } = {},
1691
+ ) {
1692
+ const usableEntries = (entries || []).filter(({ file }) => file && file.size > 0);
1693
+ if (usableEntries.length === 0) return;
1181
1694
 
1182
1695
  const previousCount = localAttachments.length;
1183
- const newAttachments: AttachmentInput[] = usableFiles.map((file) => ({
1696
+ const newAttachments: AttachmentInput[] = usableEntries.map(({ file, label }) => ({
1697
+ // Tells the tiles apart while the tray shows them (two pasted screenshots are both "image.png"), and is how the upload
1698
+ // finds its tile again while other uploads run: see `updateAttachment`.
1699
+ id: generateFileId(),
1184
1700
  file,
1701
+ label,
1185
1702
  type: file.type.startsWith('image/')
1186
1703
  ? 'image'
1187
1704
  : file.type.startsWith('video/')
@@ -1192,7 +1709,7 @@
1192
1709
  uploadProgress: 0,
1193
1710
  }));
1194
1711
 
1195
- const startIndex = localAttachments.length;
1712
+ labelNewAttachments(newAttachments);
1196
1713
  localAttachments = [...localAttachments, ...newAttachments];
1197
1714
  notifyDraftChange();
1198
1715
  expandChatForAttachmentGrowth(previousCount);
@@ -1200,17 +1717,71 @@
1200
1717
  console.log('[Chat] attachments now', localAttachments.length);
1201
1718
  // The draft keeps each picture as it is attached, before the upload ends (a refresh can come at any time).
1202
1719
  for (const entry of newAttachments) void rememberImageForDraft(entry);
1720
+ // Everything the next paste has to see is in the tray now; the uploads below are not waited for.
1721
+ options.onTilesAdded?.();
1203
1722
 
1204
1723
  try {
1205
1724
  localIsUploading = true;
1206
- for (let i = 0; i < usableFiles.length; i++) {
1207
- await uploadAttachmentToCdn(usableFiles[i], newAttachments[i], startIndex + i);
1725
+ for (let i = 0; i < usableEntries.length; i++) {
1726
+ await uploadAttachmentToCdn(usableEntries[i].file, newAttachments[i]);
1208
1727
  }
1209
1728
  } finally {
1210
1729
  localIsUploading = false;
1211
1730
  }
1212
1731
 
1213
- chatLog('[Chat] Files added as attachments:', usableFiles.length);
1732
+ chatLog('[Chat] Files added as attachments:', usableEntries.length);
1733
+ }
1734
+
1735
+ /**
1736
+ * The files dropped on the box or picked with "+". A picture is made smaller first when it is large (see
1737
+ * `prepareChatImageFileForUpload`); a paste gets the same preparation, in the line of the pastes (see `addPastedAttachments`).
1738
+ */
1739
+ async function addFilesAsAttachments(files: File[]) {
1740
+ const incoming = (files || []).filter((file) => file && file.size > 0);
1741
+ if (incoming.length === 0) return;
1742
+ const usableFiles = await Promise.all(
1743
+ incoming.map(async (file) =>
1744
+ file.type.startsWith('image/') ? await prepareChatImageFileForUpload(file) : file,
1745
+ ),
1746
+ );
1747
+ await attachFiles(usableFiles.map((file) => ({ file })));
1748
+ }
1749
+
1750
+ /**
1751
+ * The line every paste stands in, whether the box took it from the clipboard (`addPastedAttachments`) or the page handed it
1752
+ * in (`addImageAttachment`): a paste takes its place when it arrives, and its tiles come once the paste before it has its
1753
+ * own. Making a picture smaller takes the longer the bigger it is, so without this a big screenshot pasted just before a
1754
+ * small one (or before a text) would get its tile after it, and so would the pictures of a draft that is restored. The pastes
1755
+ * are prepared all at once; only their tiles wait for their place, and their uploads do not wait at all.
1756
+ */
1757
+ const pasteTileLine = createOrderedLine();
1758
+
1759
+ /**
1760
+ * What a paste into the box brought in (see `attachmentsFromPaste`): a screenshot or a file, or a long text as a text file.
1761
+ * It is attached the way a dropped file is, so it is uploaded and sent like one, and a large picture is made smaller first
1762
+ * (see `prepareImagesOfPaste`). The tile of a pasted text says its first words, not "pasted-text.txt". Every paste is an
1763
+ * attachment of its own, with a name of its own among the files already in the tray: two screenshots are both "image.png"
1764
+ * and two texts both "pasted-text.txt", and the builder names the text files it reads (and the person is told which it
1765
+ * could not read) by those names. The pastes get their tiles in the order they were made in (see `pasteTileLine`).
1766
+ */
1767
+ async function addPastedAttachments(pastedAttachments: PastedAttachment[]) {
1768
+ // The place in the line is taken now, before anything is waited for: it follows the order the pastes arrived in.
1769
+ const placeInLine = pasteTileLine.takePlace();
1770
+ try {
1771
+ chatLog('[Chat] Attaching pasted content:', pastedAttachments.length);
1772
+ // Preparing is done by all the pastes at once; only the tiles wait for their place.
1773
+ const preparedAttachments = await prepareImagesOfPaste(pastedAttachments, prepareChatImageFileForUpload);
1774
+ await placeInLine.untilReached;
1775
+ // The tray is read in the same step that the tiles are added, with no wait between: the paste after this one sees them.
1776
+ const namesInTray = localAttachments.map((attachment) => attachment.file?.name ?? '').filter(Boolean);
1777
+ // The place is left as soon as the tiles are in the tray, not when the uploads end: they run side by side.
1778
+ await attachFiles(withUniqueFileNames(preparedAttachments, namesInTray), { onTilesAdded: placeInLine.leave });
1779
+ } catch (error) {
1780
+ console.error('[Chat] Could not attach the pasted content:', error);
1781
+ } finally {
1782
+ // The next paste's tiles may come now, even when this one could not be attached (leaving a second time changes nothing).
1783
+ placeInLine.leave();
1784
+ }
1214
1785
  }
1215
1786
 
1216
1787
  async function handleUpload() {
@@ -1272,6 +1843,30 @@
1272
1843
  }
1273
1844
  }
1274
1845
 
1846
+ // --- What the person mentions with @ ----------------------------------------------------------------------------------------
1847
+
1848
+ /** The links of the text that one of the page's lists is made for, to offer as a one-tap pick beside the chips. */
1849
+ const mentionLinkHints = $derived(findMentionLinkHints(localInputText, mentionSources, localMentions, mentionLimit));
1850
+
1851
+ /** Whether the message box has anything to show above the text: a part picked on the preview, a pick, or an offer. With none it looks as it always did. */
1852
+ const composerHasChips = $derived((selectedElements?.length ?? 0) > 0 || localMentions.length > 0 || mentionLinkHints.length > 0);
1853
+
1854
+ function addMentionPick(mention: ComposerMention) {
1855
+ localMentions = addMention(localMentions, mention, mentionLimit);
1856
+ }
1857
+
1858
+ function removeMentionPick(mention: ComposerMention) {
1859
+ localMentions = removeMention(localMentions, mention.kind, mention.id);
1860
+ }
1861
+
1862
+ /** An offer for a link was tapped: the row is picked and the text stays as it is. When the tap took the focus, it goes back to the box. */
1863
+ function pickMentionHint(hint: MentionLinkHint) {
1864
+ addMentionPick({ kind: hint.kind, id: hint.id, name: hint.name });
1865
+ if (typeof document !== 'undefined' && !(document.activeElement instanceof HTMLTextAreaElement)) {
1866
+ void tick().then(focusComposerTextarea);
1867
+ }
1868
+ }
1869
+
1275
1870
  /** The sketch's "Undo" chip is used once: take it off the last assistant message that carries it. */
1276
1871
  function clearUndoSketchSuggestionChip() {
1277
1872
  const convId = currentConversationId;
@@ -1321,6 +1916,14 @@
1321
1916
  fillDraftFromSuggestion(suggestion.label, suggestion.intent);
1322
1917
  }
1323
1918
 
1919
+ /**
1920
+ * Skip was pressed on a question card. The card has gone and nothing is sent: the person goes on in their own words, so the
1921
+ * cursor returns to the message box (the button that held it is no longer there to keep it).
1922
+ */
1923
+ function handleQuestionSkipped() {
1924
+ queueMicrotask(() => focusComposerTextarea());
1925
+ }
1926
+
1324
1927
  async function handleRetryMessage(content: string) {
1325
1928
  const retryContent = content.trim();
1326
1929
  if (!retryContent) return;
@@ -1474,30 +2077,70 @@
1474
2077
  }
1475
2078
  }
1476
2079
 
1477
- async function addImageAttachment(dataUrl: string, fileName = 'pasted-image.png') {
2080
+ /**
2081
+ * A picture the page hands over: one pasted inside the page's own panel (the Widget Creator catches those), or one of a
2082
+ * draft that is restored (see `restoreAttachmentsFromDataUrls`). Its tile comes in the order the pictures were handed in
2083
+ * (see `pasteTileLine`), and a large picture is made smaller first, the way a dropped or a pasted one is.
2084
+
2085
+ * `forcedLabel` is the label a restored picture was given when the restore was asked for (the pictures are numbered in the order
2086
+ * they were asked for, not in the order they come back).
2087
+ */
2088
+ async function addImageAttachment(dataUrl: string, fileName = 'pasted-image.png', forcedLabel?: string) {
1478
2089
  if (!isValidImageDataUrl(dataUrl)) {
1479
2090
  chatWarn('[Chat] Skipping invalid image data URL for attachment restore');
1480
2091
  return;
1481
2092
  }
2093
+ // The place in the line is taken now, before anything is waited for: it follows the order the pictures were handed in.
2094
+ const placeInLine = pasteTileLine.takePlace();
2095
+ let attachmentInput: AttachmentInput | undefined;
1482
2096
  try {
2097
+ // Reading and preparing are done by all the pictures at once; only the tiles wait for their place.
1483
2098
  const response = await fetch(dataUrl);
1484
2099
  const blob = await response.blob();
1485
2100
  const mimeMatch = dataUrl.match(/data:(image\/[^;]+);/);
1486
2101
  const mimeType = mimeMatch?.[1] || blob.type || 'image/png';
1487
- const file = new File([blob], fileName, { type: mimeType });
1488
- const attachmentInput: AttachmentInput = {
2102
+ const handedOverFile = new File([blob], fileName, { type: mimeType });
2103
+ // The same file comes back when there is nothing to do (a small picture) or when it could not be made ready in time (see
2104
+ // `prepareImageWithTimeLimit`: a picture that never gets ready must not hold up the pastes after it); a large one comes
2105
+ // back smaller, under a new name.
2106
+ const preparedFile = await prepareImageWithTimeLimit(handedOverFile, prepareChatImageFileForUpload);
2107
+ // The tile draws, and the draft keeps, the picture that is uploaded: the page's own `data:` URL when the picture is as it
2108
+ // came, a new one when it was made smaller (the page's own stays when the new one cannot be read, as it did before).
2109
+ let pictureDataUrl = dataUrl;
2110
+ if (preparedFile !== handedOverFile) {
2111
+ pictureDataUrl = (await readImageAsDataUrl(preparedFile)) || dataUrl;
2112
+ chatLog('[Chat] A picture handed over was made smaller:', handedOverFile.size, '→', preparedFile.size);
2113
+ }
2114
+ await placeInLine.untilReached;
2115
+ // Two screenshots are both "image.png": the name is taken from the tray as it is now, in the same step as the tile.
2116
+ const namesInTray = localAttachments.map((attachment) => attachment.file?.name ?? '').filter(Boolean);
2117
+ const { file } = withUniqueFileNames([{ file: preparedFile }], namesInTray)[0];
2118
+ attachmentInput = {
2119
+ // Two pictures pasted one after the other can be the same picture, and then have the same preview: the id is what
2120
+ // tells their tiles apart, and how the upload finds its tile again (see `updateAttachment`).
2121
+ id: generateFileId(),
1489
2122
  file,
1490
2123
  type: 'image',
1491
- preview: dataUrl,
2124
+ preview: pictureDataUrl,
2125
+ // The tile keeps drawing this `data:` URL, and the draft keeps it too: the stored address that comes when the upload
2126
+ // ends is only the picture's second choice (see `readAttachmentPreviewUrl`).
2127
+ persistedDataUrl: pictureDataUrl,
1492
2128
  uploadProgress: 0,
1493
2129
  };
2130
+ labelNewAttachments([attachmentInput], [forcedLabel]);
1494
2131
  const attachmentIndex = localAttachments.length;
1495
2132
  localAttachments = [...localAttachments, attachmentInput];
1496
2133
  notifyDraftChange();
1497
2134
  expandChatForAttachmentGrowth(attachmentIndex);
1498
- if (onUploadChatFile) {
1499
- await uploadAttachmentToCdn(file, attachmentInput, attachmentIndex);
1500
- }
2135
+ } catch (err) {
2136
+ console.error('[Chat] Failed to add image attachment:', err);
2137
+ } finally {
2138
+ // The next picture's tile may come now, even when this one could not be read. It does not wait for this upload.
2139
+ placeInLine.leave();
2140
+ }
2141
+ if (!attachmentInput || !onUploadChatFile) return;
2142
+ try {
2143
+ await uploadAttachmentToCdn(attachmentInput.file, attachmentInput);
1501
2144
  // chatLog('Image attachment added from external paste');
1502
2145
  } catch (err) {
1503
2146
  console.error('[Chat] Failed to add image attachment:', err);
@@ -1505,13 +2148,24 @@
1505
2148
  }
1506
2149
 
1507
2150
  function getDraftText(): string {
1508
- return localInputText;
2151
+ // While a queued message is open in the composer, the person's draft is the one that was set aside
2152
+ return queueEditingId && queueStashedDraft ? queueStashedDraft.text : localInputText;
1509
2153
  }
1510
2154
 
1511
2155
  function setDraftText(text: string) {
2156
+ // The host gives the composer a draft: an edit of a queued message ends first, so the draft lands where the person writes
2157
+ if (queueEditingId) discardQueueEdit();
1512
2158
  localInputText = text;
1513
2159
  pendingChipIntent = null;
1514
2160
  pendingSkipIntentGate = false;
2161
+ // The text may carry references from before (a draft restored after a refresh): the `Image_N` numbers it names are spent, so a
2162
+ // file attached from now on never takes one of them, and its badges are checked against the files once those are in
2163
+ try {
2164
+ raiseGenericFloorOver(text);
2165
+ } catch (error) {
2166
+ console.warn('[Chat] Could not read the references of the restored draft text:', error);
2167
+ }
2168
+ draftBadgesNeedCheck = true;
1515
2169
  }
1516
2170
 
1517
2171
  function getAttachmentDataUrls(): string[] {
@@ -1526,7 +2180,9 @@
1526
2180
  * `getAttachmentDataUrls` stays the list of addresses to send with a message (a CDN address when the upload ended).
1527
2181
  */
1528
2182
  function getDraftAttachmentDataUrls(): string[] {
1529
- return localAttachments
2183
+ // The draft is the one that was set aside while a queued message is open in the composer
2184
+ const draftAttachments = queueEditingId && queueStashedDraft ? queueStashedDraft.attachments : localAttachments;
2185
+ return draftAttachments
1530
2186
  .filter((att) => att.type === 'image' || att.file?.type?.startsWith('image/'))
1531
2187
  .map((att) => att.persistedDataUrl || (att.preview?.startsWith('data:') ? att.preview : '') || '')
1532
2188
  .filter((url) => !!url);
@@ -1539,6 +2195,7 @@
1539
2195
  }
1540
2196
 
1541
2197
  function restoreAttachmentsFromDataUrls(dataUrls: string[]) {
2198
+ if (queueEditingId) discardQueueEdit();
1542
2199
  for (const att of localAttachments) {
1543
2200
  if (att.preview && att.preview.startsWith('blob:')) {
1544
2201
  try {
@@ -1549,9 +2206,28 @@
1549
2206
  }
1550
2207
  }
1551
2208
  localAttachments = [];
2209
+ // A restored tray starts its numbers again, above what the draft's text (when the host has set it already) names; the pictures are
2210
+ // numbered here, in the order they were asked for, because they come back in whatever order their data is read
2211
+ try {
2212
+ referenceGenericFloor = highestGenericNumberIn(localInputText);
2213
+ } catch (error) {
2214
+ console.warn('[Chat] Could not read the references of the draft text:', error);
2215
+ referenceGenericFloor = 0;
2216
+ }
2217
+ highlightedReferenceLabel = null;
1552
2218
  const validUrls = dataUrls.filter(isValidImageDataUrl);
2219
+ restoredLabelsToCheck = new Set();
2220
+ const restoredLabels = validUrls.map(() => {
2221
+ const label = assignReferenceLabel('', { genericNumberFloor: referenceGenericFloor });
2222
+ referenceGenericFloor = Math.max(referenceGenericFloor, genericLabelNumber(label) ?? 0);
2223
+ restoredLabelsToCheck.add(label.toLowerCase());
2224
+ return label;
2225
+ });
2226
+ pendingAttachmentRestores += validUrls.length;
1553
2227
  for (let i = 0; i < validUrls.length; i++) {
1554
- addImageAttachment(validUrls[i], `restored-image-${i + 1}.png`);
2228
+ void addImageAttachment(validUrls[i], `restored-image-${i + 1}.png`, restoredLabels[i]).finally(() => {
2229
+ pendingAttachmentRestores = Math.max(0, pendingAttachmentRestores - 1);
2230
+ });
1555
2231
  }
1556
2232
  if (validUrls.length < dataUrls.length) {
1557
2233
  chatWarn('[Chat] Dropped invalid restored attachment(s):', dataUrls.length - validUrls.length);
@@ -1559,6 +2235,7 @@
1559
2235
  }
1560
2236
 
1561
2237
  function clearDraft() {
2238
+ if (queueEditingId) discardQueueEdit();
1562
2239
  for (const att of localAttachments) {
1563
2240
  if (att.preview && att.preview.startsWith('blob:')) {
1564
2241
  try {
@@ -1570,11 +2247,282 @@
1570
2247
  }
1571
2248
  localInputText = '';
1572
2249
  localAttachments = [];
2250
+ referenceGenericFloor = 0;
2251
+ highlightedReferenceLabel = null;
2252
+ restoredLabelsToCheck = new Set();
2253
+ localMentions = [];
1573
2254
  pendingChipIntent = null;
1574
2255
  pendingSkipIntentGate = false;
1575
2256
  notifyDraftChange();
1576
2257
  }
1577
2258
 
2259
+ // ===== The messages that wait for the agent =====
2260
+ //
2261
+ // While the agent works, a message the person sends waits above the composer (the queue card) instead of being sent into the
2262
+ // run. The oldest waiting message is sent when the agent is done, one at a time. The person can send one now (which stops the
2263
+ // agent first), edit it in the composer, or delete it. What happens next is decided by messageQueue.ts; this is the state it acts on.
2264
+
2265
+ /** Sending while the agent works queues the message (on, unless the host turns it off with `features.messageQueue`). */
2266
+ const messageQueueEnabled = $derived(config?.features?.messageQueue !== false);
2267
+
2268
+ /** The agent is working: the host runs a generation, a message is loading, or one is being sent. */
2269
+ const agentIsBusy = $derived(isGenerating || isSendingOrLoading);
2270
+
2271
+ /** Gives back the blob addresses the page made for pictures that nothing draws any more. */
2272
+ function releasePreviewAddresses(addresses: readonly string[]) {
2273
+ for (const address of addresses) {
2274
+ try {
2275
+ URL.revokeObjectURL(address);
2276
+ } catch {
2277
+ /* ignore error */
2278
+ }
2279
+ }
2280
+ }
2281
+
2282
+ function clearQueueSendNowTimer() {
2283
+ if (queueSendNowTimer) {
2284
+ clearTimeout(queueSendNowTimer);
2285
+ queueSendNowTimer = null;
2286
+ }
2287
+ }
2288
+
2289
+ /** "Send now" is no longer wanted: the message was sent, edited or deleted, or the agent did not stop in time. */
2290
+ function cancelQueueSendNow() {
2291
+ clearQueueSendNowTimer();
2292
+ queueSendNowId = null;
2293
+ }
2294
+
2295
+ /** The send control of the composer while the agent works: the message goes to the bottom of the queue, and the composer is empty again. */
2296
+ function handleQueueMessage(text: string) {
2297
+ const trimmed = text.trim();
2298
+ if (!trimmed || queueEditingId) return;
2299
+ const queuedMessage: QueuedMessage = {
2300
+ id: createQueuedMessageId(),
2301
+ text: trimmed,
2302
+ // The files move with the message: they are not given back, they belong to the queue now
2303
+ attachments: [...localAttachments],
2304
+ chipIntent: pendingChipIntent ?? undefined,
2305
+ skipIntentGate: pendingSkipIntentGate,
2306
+ };
2307
+ queuedMessages = appendQueuedMessage(queuedMessages, queuedMessage);
2308
+ localInputText = '';
2309
+ localAttachments = [];
2310
+ pendingChipIntent = null;
2311
+ pendingSkipIntentGate = false;
2312
+ notifyDraftChange();
2313
+ chatLog('[Chat] Message queued behind the agent. Waiting:', queuedMessages.length);
2314
+ }
2315
+
2316
+ /** The stop control of the composer. With messages waiting, the queue holds still until the person sends one. */
2317
+ function handleStopPressed() {
2318
+ if (queuedMessages.length > 0) {
2319
+ queuePaused = true;
2320
+ cancelQueueSendNow();
2321
+ chatLog('[Chat] Stop pressed with messages waiting: the queue is paused');
2322
+ }
2323
+ onStopGeneration?.();
2324
+ }
2325
+
2326
+ /** What happens next to the queue, from the state as it is at this moment. */
2327
+ function currentQueueStep() {
2328
+ return decideQueueStep({
2329
+ queueLength: queuedMessages.length,
2330
+ agentBusy: agentIsBusy,
2331
+ paused: queuePaused,
2332
+ editing: queueEditingId !== null,
2333
+ sendNowPending: findQueuedMessage(queuedMessages, queueSendNowId) !== undefined,
2334
+ });
2335
+ }
2336
+
2337
+ /** Sends one waiting message into the chat as pressing Send would have, without taking anything from the composer. */
2338
+ async function sendQueuedMessage(queuedMessage: QueuedMessage) {
2339
+ // It leaves the queue first, so no second turn of the effect below can send it twice
2340
+ queuedMessages = removeQueuedMessage(queuedMessages, queuedMessage.id);
2341
+ if (queuedMessages.length === 0) queuePaused = false;
2342
+ chatLog('[Chat] Sending a queued message. Still waiting:', queuedMessages.length);
2343
+ const wasDelivered = await sendMessageAndReport(queuedMessage.text, queuedMessage.attachments, {
2344
+ keepDraft: true,
2345
+ chipIntent: queuedMessage.chipIntent,
2346
+ skipIntentGate: queuedMessage.skipIntentGate,
2347
+ });
2348
+ if (!wasDelivered && queuedMessages.length > 0) {
2349
+ // The send failed (the banner says so): the rest of the queue holds still, so a fault of the connection does not use up the whole queue
2350
+ queuePaused = true;
2351
+ console.warn('[Chat] A queued message could not be sent: the queue is paused');
2352
+ }
2353
+ }
2354
+
2355
+ /** "Send now" on a waiting message: the agent is stopped and that message is sent as soon as it has stopped. */
2356
+ function handleQueueSendNow(id: string) {
2357
+ if (queueEditingId || !findQueuedMessage(queuedMessages, id)) return;
2358
+ queuePaused = false;
2359
+ clearQueueSendNowTimer();
2360
+ queueSendNowId = id;
2361
+ if (isGenerating || isLoading) {
2362
+ chatLog('[Chat] Send now: stopping the agent so the queued message can go');
2363
+ // Past the time limit the agent has not stopped: the message stays in the queue and the person is told
2364
+ queueSendNowTimer = setTimeout(() => {
2365
+ queueSendNowTimer = null;
2366
+ if (queueSendNowId !== id) return;
2367
+ queueSendNowId = null;
2368
+ console.warn('[Chat] Send now: the agent did not stop in time, so the message stays in the queue');
2369
+ showFailure('chat.queue.sendNowTimeout');
2370
+ }, QUEUE_SEND_NOW_TIMEOUT_MS);
2371
+ onStopGeneration?.();
2372
+ }
2373
+ }
2374
+
2375
+ // The queue drains by itself: when the agent has been idle for a moment (the host can be idle between two phases of a run) the
2376
+ // oldest message is sent; "Send now" does not wait for the moment. Both read the state again before they send, because the person
2377
+ // can press Stop, edit or delete meanwhile.
2378
+ $effect(() => {
2379
+ const step = decideQueueStep({
2380
+ queueLength: queuedMessages.length,
2381
+ agentBusy: agentIsBusy,
2382
+ paused: queuePaused,
2383
+ editing: queueEditingId !== null,
2384
+ sendNowPending: findQueuedMessage(queuedMessages, queueSendNowId) !== undefined,
2385
+ });
2386
+ if (step === 'sendNow') {
2387
+ // Not inside the effect: sending writes the state this effect reads
2388
+ queueMicrotask(() => {
2389
+ if (currentQueueStep() !== 'sendNow') return;
2390
+ const chosenMessage = findQueuedMessage(queuedMessages, queueSendNowId);
2391
+ cancelQueueSendNow();
2392
+ if (chosenMessage) void sendQueuedMessage(chosenMessage);
2393
+ });
2394
+ return;
2395
+ }
2396
+ if (step === 'drain') {
2397
+ const settleTimer = setTimeout(() => {
2398
+ if (currentQueueStep() !== 'drain') return;
2399
+ const oldestMessage = queuedMessages[0];
2400
+ if (oldestMessage) void sendQueuedMessage(oldestMessage);
2401
+ }, QUEUE_SETTLE_MS);
2402
+ return () => clearTimeout(settleTimer);
2403
+ }
2404
+ });
2405
+
2406
+ /** Puts back what the composer held before a queued message was opened in it, and ends the edit. */
2407
+ function restoreStashedDraft() {
2408
+ const stashedDraft = queueStashedDraft;
2409
+ queueStashedDraft = null;
2410
+ queueEditProtectedPreviews = new Set();
2411
+ queueEditOriginalAttachments = [];
2412
+ queueEditingId = null;
2413
+ localInputText = stashedDraft?.text ?? '';
2414
+ localAttachments = stashedDraft?.attachments ?? [];
2415
+ pendingChipIntent = stashedDraft?.chipIntent ?? null;
2416
+ pendingSkipIntentGate = stashedDraft?.skipIntentGate ?? false;
2417
+ // The composer writes the text back once after the page sets it: that write must not clear the chip that was kept
2418
+ skipNextDraftIntentClear = pendingChipIntent !== null;
2419
+ // The host hears of the draft again (it was not told while the edit lasted)
2420
+ notifyDraftChange();
2421
+ }
2422
+
2423
+ /** The pencil of a waiting message: its text and files replace what is in the composer, which is set aside until the edit ends. */
2424
+ function handleQueueEdit(id: string) {
2425
+ const queuedMessage = findQueuedMessage(queuedMessages, id);
2426
+ if (!queuedMessage || queueEditingId) return;
2427
+ // The person changed their mind about "Send now": a message that is being edited is not sent from under them
2428
+ cancelQueueSendNow();
2429
+ queueStashedDraft = {
2430
+ text: localInputText,
2431
+ attachments: localAttachments,
2432
+ chipIntent: pendingChipIntent,
2433
+ skipIntentGate: pendingSkipIntentGate,
2434
+ };
2435
+ queueEditOriginalAttachments = [...queuedMessage.attachments];
2436
+ queueEditProtectedPreviews = new Set(blobPreviewsOf(queuedMessage.attachments));
2437
+ // Set before the draft changes below: from here on the host is not told of what the composer holds
2438
+ queueEditingId = id;
2439
+ localInputText = queuedMessage.text;
2440
+ localAttachments = [...queuedMessage.attachments];
2441
+ pendingChipIntent = queuedMessage.chipIntent ?? null;
2442
+ pendingSkipIntentGate = queuedMessage.skipIntentGate === true;
2443
+ skipNextDraftIntentClear = pendingChipIntent !== null;
2444
+ chatLog('[Chat] Editing a queued message');
2445
+ queueMicrotask(() => focusComposerTextarea());
2446
+ }
2447
+
2448
+ /** The check of the composer: the message keeps its turn and takes the text and the files that are in the composer. */
2449
+ function confirmQueueEdit(text: string) {
2450
+ const editedId = queueEditingId;
2451
+ const trimmed = text.trim();
2452
+ if (!editedId || !trimmed) return;
2453
+ const editedFiles = [...localAttachments];
2454
+ const original = findQueuedMessage(queuedMessages, editedId);
2455
+ if (original) {
2456
+ queuedMessages = updateQueuedMessage(queuedMessages, editedId, {
2457
+ text: trimmed,
2458
+ attachments: editedFiles,
2459
+ chipIntent: pendingChipIntent ?? undefined,
2460
+ skipIntentGate: pendingSkipIntentGate,
2461
+ });
2462
+ // The pictures the person took out of the message are not attached to anything now
2463
+ releasePreviewAddresses(previewsDroppedByEdit(queueEditOriginalAttachments, editedFiles));
2464
+ }
2465
+ chatLog('[Chat] Queued message edited');
2466
+ restoreStashedDraft();
2467
+ }
2468
+
2469
+ /** The cross of the pill (and Escape): the changes are dropped, the message stays as it was, and the composer gets its draft back. */
2470
+ function discardQueueEdit() {
2471
+ if (!queueEditingId) return;
2472
+ // The pictures added during the edit belong to nothing now
2473
+ releasePreviewAddresses(previewsAddedByEdit(queueEditOriginalAttachments, [...localAttachments]));
2474
+ chatLog('[Chat] Edit of a queued message closed without saving');
2475
+ restoreStashedDraft();
2476
+ }
2477
+
2478
+ /** The bin of a waiting message. */
2479
+ function handleQueueDelete(id: string) {
2480
+ const queuedMessage = findQueuedMessage(queuedMessages, id);
2481
+ if (!queuedMessage || queueEditingId) return;
2482
+ queuedMessages = removeQueuedMessage(queuedMessages, id);
2483
+ if (queueSendNowId === id) cancelQueueSendNow();
2484
+ if (queuedMessages.length === 0) queuePaused = false;
2485
+ releasePreviewAddresses(blobPreviewsOf(queuedMessage.attachments));
2486
+ chatLog('[Chat] Queued message deleted. Still waiting:', queuedMessages.length);
2487
+ }
2488
+
2489
+ /** The waiting messages belong to the conversation they were written in: another conversation is never sent them. */
2490
+ function dropQueueOfPreviousConversation() {
2491
+ const droppedCount = queuedMessages.length;
2492
+ if (queueEditingId) discardQueueEdit();
2493
+ releasePreviewAddresses(blobPreviewsOf(queuedMessages.flatMap((message) => message.attachments)));
2494
+ queuedMessages = [];
2495
+ queuePaused = false;
2496
+ cancelQueueSendNow();
2497
+ if (droppedCount > 0) {
2498
+ console.warn('[Chat] The conversation changed: the waiting messages were not sent:', droppedCount);
2499
+ chatStore.actions.updateUI({ errorMessage: $t('chat.queue.discarded', { count: droppedCount }) });
2500
+ }
2501
+ }
2502
+
2503
+ $effect(() => {
2504
+ const conversationId = currentConversationId;
2505
+ untrack(() => {
2506
+ if (queueConversationId !== null && conversationId !== queueConversationId) {
2507
+ dropQueueOfPreviousConversation();
2508
+ }
2509
+ queueConversationId = conversationId;
2510
+ });
2511
+ });
2512
+
2513
+ /** What the composer shows while a waiting message is open in it: the pill, and the check in place of Send. */
2514
+ const composerEditing = $derived<ComposerEditing | null>(
2515
+ queueEditingId
2516
+ ? {
2517
+ label: $t('chat.queue.editing'),
2518
+ closeLabel: $t('chat.queue.stopEditing'),
2519
+ confirmLabel: $t('chat.queue.confirmEdit'),
2520
+ onClose: discardQueueEdit,
2521
+ onConfirm: confirmQueueEdit,
2522
+ }
2523
+ : null,
2524
+ );
2525
+
1578
2526
  const CODEGEN_COMMIT_REGEX = /(?:Commit:?\s+|Code saved\s*\()([a-f0-9]{7,40})/i;
1579
2527
 
1580
2528
  function isPreviewRecompiledMessage(content?: string | null): boolean {
@@ -1849,9 +2797,10 @@
1849
2797
  // chatLog('[Chat] Keep local messages — DB reload would regress HEAD commit');
1850
2798
  } else {
1851
2799
  hasOlderMessages = messageListHasOlder(loaded);
2800
+ // The picks made on a card of questions are not saved with the card: they are carried over from this session
1852
2801
  chatStore.actions.setMessages(
1853
2802
  currentConversationId,
1854
- keepLocalDesignCards(loaded, localMsgs),
2803
+ keepLocalQuestionCards(keepLocalDesignCards(loaded, localMsgs), localMsgs),
1855
2804
  );
1856
2805
  }
1857
2806
  }
@@ -1904,6 +2853,7 @@
1904
2853
  sendMessage,
1905
2854
  addAssistantMessage,
1906
2855
  addDesignControlsMessage,
2856
+ addQuestionCardMessage,
1907
2857
  addUserMessage,
1908
2858
  getConversationId: () => currentConversationId,
1909
2859
  addImageAttachment,
@@ -2124,13 +3074,18 @@
2124
3074
  generationStream={isGenerating ? generationStream : ''}
2125
3075
  generationThinking={isGenerating ? generationThinking : ''}
2126
3076
  generationPlan={isGenerating ? generationPlan : null}
3077
+ generationPlanPending={isGenerating ? generationPlanPending : false}
3078
+ {generationChanges}
2127
3079
  onSuggestionSelected={handleSuggestionSelected}
3080
+ draftText={localInputText}
2128
3081
  onRestoreCheckpoint={onRestoreCheckpointBackend ? handleRestoreCheckpoint : undefined}
2129
3082
  onRateVersion={onRateVersion ? handleRateVersion : undefined}
2130
3083
  versionRatings={shownVersionRatings}
2131
3084
  onRetryMessage={handleRetryMessage}
2132
3085
  {onUpgradePlans}
2133
3086
  onClarificationSuggestionSelected={handleClarificationSuggestionSelected}
3087
+ onQuestionSkipped={handleQuestionSkipped}
3088
+ onQuestionCardAnswered={handleQuestionCardAnswered}
2134
3089
  {renderDesignControls}
2135
3090
  {storedLineLabel}
2136
3091
  conversationId={currentConversationId}
@@ -2139,6 +3094,7 @@
2139
3094
  {renderAgentAvatar}
2140
3095
  {agentSound}
2141
3096
  {tightBubbles}
3097
+ {referenceBadges}
2142
3098
  />
2143
3099
  </div>
2144
3100
 
@@ -2195,38 +3151,17 @@
2195
3151
  </div>
2196
3152
  {/if}
2197
3153
 
2198
- {#if selectedElements && selectedElements.length > 0}
2199
- <div
2200
- class="prompt-chat-selected-elements-tray flex flex-wrap items-center gap-1.5 border-t border-gray-100 bg-[#F7FAF5] px-3 py-2 shrink-0"
2201
- data-testid="chat-selected-elements-tray"
2202
- >
2203
- {#each selectedElements as item, index (item.id)}
2204
- {@const chipLabel = `<${item.tagName}${item.idAttr ? `#${item.idAttr}` : ''}>${item.note?.trim() ? ` · ${item.note.trim()}` : item.textSnippet ? ` · ${item.textSnippet}` : item.selector ? ` · ${item.selector}` : ''}`}
2205
- <span class="prompt-chat-selected-element-chip max-w-full" title={item.selector} data-testid="chat-selected-element-chip">
2206
- <Chip
2207
- class="max-w-full"
2208
- label={chipLabel}
2209
- showRemove={!!onRemoveSelectedElement}
2210
- removeLabel={$t('chat.selector.removeElement')}
2211
- onRemove={() => onRemoveSelectedElement?.(item.id)}
2212
- >
2213
- {#snippet leading()}
2214
- <span class="bg-primary text-primary-foreground flex h-4 w-4 shrink-0 items-center justify-center rounded-full text-[10px] font-bold">
2215
- {index + 1}
2216
- </span>
2217
- {/snippet}
2218
- </Chip>
2219
- </span>
2220
- {/each}
2221
- {#if selectedElements.length > 1 && onClearSelectedElements}
2222
- <button
2223
- type="button"
2224
- class="ms-auto text-[11px] font-medium text-gray-500 hover:text-gray-800 cursor-pointer"
2225
- onclick={() => onClearSelectedElements?.()}
2226
- >
2227
- {$t('chat.selector.clearAll')}
2228
- </button>
2229
- {/if}
3154
+ <!-- The messages that wait for the agent, above the composer as the suggestions are above the chat (newest at the bottom) -->
3155
+ {#if queuedMessages.length > 0}
3156
+ <div class="prompt-chat-queue-ct shrink-0 {showPanelHeader ? 'mb-2' : 'px-3 pt-2'}" data-testid="chat-message-queue">
3157
+ <ChatMessageQueue
3158
+ items={queuedMessages}
3159
+ editingId={queueEditingId}
3160
+ paused={queuePaused}
3161
+ onSendNow={onStopGeneration ? handleQueueSendNow : undefined}
3162
+ onEdit={handleQueueEdit}
3163
+ onDelete={handleQueueDelete}
3164
+ />
2230
3165
  </div>
2231
3166
  {/if}
2232
3167
 
@@ -2235,6 +3170,7 @@
2235
3170
  attachments={localAttachments}
2236
3171
  previewUrl={getFooterAttachmentPreviewUrl}
2237
3172
  isImage={isImageAttachment}
3173
+ highlightedLabel={highlightedReferenceLabel}
2238
3174
  onRemove={removeFooterAttachment}
2239
3175
  onOpenPreview={(url) => {
2240
3176
  footerLightboxSrc = url;
@@ -2242,8 +3178,65 @@
2242
3178
  />
2243
3179
  {/if}
2244
3180
 
3181
+ <!--
3182
+ The parts the person picked on the preview, as chips inside the message box (Figma: Selecting elements, frames 2 to 4).
3183
+ A chip says the first words of the part's note; its body opens that note over the part on the preview, and the cross takes
3184
+ the part out of the message. The number on the badge is the number of the marker on the part.
3185
+ -->
3186
+ {#snippet selectedElementChips()}
3187
+ <div class="prompt-chat-selected-elements-tray flex w-full min-w-0 flex-wrap items-center gap-1.5" data-testid="chat-selected-elements-tray">
3188
+ {#each selectedElements ?? [] as item, index (item.id)}
3189
+ <SelectedElementChip
3190
+ index={index + 1}
3191
+ label={selectedElementChipLabel(item)}
3192
+ title={selectedElementChipTitle(item)}
3193
+ active={activeSelectedElementId === item.id}
3194
+ removeLabel={$t('chat.selector.removeElement')}
3195
+ onOpen={onSelectedElementClick ? () => onSelectedElementClick?.(item.id) : undefined}
3196
+ onRemove={onRemoveSelectedElement ? () => onRemoveSelectedElement?.(item.id) : undefined}
3197
+ />
3198
+ {/each}
3199
+ {#if (selectedElements?.length ?? 0) > 1 && onClearSelectedElements}
3200
+ <button
3201
+ type="button"
3202
+ class="text-caption text-muted-foreground hover:text-foreground ms-auto cursor-pointer"
3203
+ onclick={() => onClearSelectedElements?.()}
3204
+ >
3205
+ {$t('chat.selector.clearAll')}
3206
+ </button>
3207
+ {/if}
3208
+ </div>
3209
+ {/snippet}
3210
+
3211
+ <!--
3212
+ All the chips of the message box, above the text: what the person picked with @ (and the offers for links in the text), then the
3213
+ parts picked on the preview. Each kind has its own tray, so the chips of one never push those of the other out of place.
3214
+ -->
3215
+ {#snippet composerChips()}
3216
+ {#if localMentions.length > 0 || mentionLinkHints.length > 0}
3217
+ <ComposerMentionChips
3218
+ mentions={localMentions}
3219
+ hints={mentionLinkHints}
3220
+ onRemove={removeMentionPick}
3221
+ onPickHint={pickMentionHint}
3222
+ />
3223
+ {/if}
3224
+ {#if selectedElements && selectedElements.length > 0}
3225
+ {@render selectedElementChips()}
3226
+ {/if}
3227
+ {/snippet}
3228
+
2245
3229
  <ChatComposer
2246
3230
  {sendNudgeToken}
3231
+ {completion}
3232
+ {referenceItems}
3233
+ {maxReferences}
3234
+ onReferenceHover={(event) => handleReferenceHover(event)}
3235
+ attachments={composerHasChips ? composerChips : undefined}
3236
+ {mentionSources}
3237
+ mentions={localMentions}
3238
+ {mentionLimit}
3239
+ onMentionPick={addMentionPick}
2247
3240
  panelLayout={showPanelHeader}
2248
3241
  placeholder={showPanelHeader
2249
3242
  ? (currentMessages?.length ?? 0) > 0
@@ -2265,14 +3258,19 @@
2265
3258
  models={composerModels}
2266
3259
  selectedModelId={selectedComposerModelId}
2267
3260
  onSelectModel={onSelectComposerModel}
2268
- onStop={() => onStopGeneration?.()}
2269
- onSend={(content) => {
3261
+ onStop={handleStopPressed}
3262
+ onQueue={messageQueueEnabled ? handleQueueMessage : undefined}
3263
+ editing={composerEditing}
3264
+ onSend={(content, sendOptions) => {
2270
3265
  const attachmentInputs = localAttachments.length > 0 ? localAttachments : undefined;
2271
- void sendMessage(content, attachmentInputs);
3266
+ handleComposerSend(content, attachmentInputs, sendOptions);
2272
3267
  }}
2273
3268
  onFilesDropped={(files) => {
2274
3269
  void addFilesAsAttachments(files);
2275
3270
  }}
3271
+ onFilesPasted={config?.features?.fileUpload !== false
3272
+ ? (pastedAttachments) => void addPastedAttachments(pastedAttachments)
3273
+ : undefined}
2276
3274
  onAttach={config?.features?.fileUpload !== false ? () => void handleUpload() : undefined}
2277
3275
  canSendWithoutText={(selectedElements ?? []).some((element) => !!element.note?.trim())}
2278
3276
  />