@widgetic/chat 0.1.42 → 0.1.44
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.
- package/README.md +461 -6
- package/dist/components/Chat.svelte +1078 -87
- package/dist/components/Chat.svelte.d.ts +102 -2
- package/dist/components/ChatComposer.svelte +382 -9
- package/dist/components/ChatComposer.svelte.d.ts +66 -3
- package/dist/components/ChatMessageQueue.svelte +56 -0
- package/dist/components/ChatMessageQueue.svelte.d.ts +20 -0
- package/dist/components/ChatMessages.svelte +485 -50
- package/dist/components/ChatMessages.svelte.d.ts +45 -2
- package/dist/components/ComposerAttachmentsTray.svelte +42 -10
- package/dist/components/ComposerAttachmentsTray.svelte.d.ts +5 -0
- package/dist/components/ComposerMentionChips.svelte +53 -0
- package/dist/components/ComposerMentionChips.svelte.d.ts +15 -0
- package/dist/components/FollowUpCard.svelte +134 -0
- package/dist/components/FollowUpCard.svelte.d.ts +17 -0
- package/dist/components/MentionChip.svelte +41 -0
- package/dist/components/MentionChip.svelte.d.ts +12 -0
- package/dist/components/MentionMenu.svelte +114 -0
- package/dist/components/MentionMenu.svelte.d.ts +19 -0
- package/dist/components/agent/AgentAvatar.svelte +6 -4
- package/dist/components/agent/AgentAvatar.svelte.d.ts +1 -1
- package/dist/components/agent/CodeCard.svelte +122 -27
- package/dist/components/agent/CodeCard.svelte.d.ts +3 -0
- package/dist/components/agent/CodeDiff.svelte +180 -0
- package/dist/components/agent/CodeDiff.svelte.d.ts +10 -0
- package/dist/components/agent/GenerationModule.svelte +13 -2
- package/dist/components/agent/GenerationModule.svelte.d.ts +3 -0
- package/dist/components/index.d.ts +4 -0
- package/dist/components/index.js +9 -0
- package/dist/errors/logTechnicalUserError.d.ts +1 -0
- package/dist/errors/logTechnicalUserError.js +37 -0
- package/dist/i18n/messages.d.ts +106 -1
- package/dist/i18n/messages.js +112 -1
- package/dist/i18n/sketchLocales.js +8 -8
- package/dist/messageQueue.d.ts +83 -0
- package/dist/messageQueue.js +84 -0
- package/dist/services/index.d.ts +1 -1
- package/dist/services/sketchAssistantClient.d.ts +27 -0
- package/dist/services/sketchAssistantClient.js +7 -2
- package/dist/services/sketchAssistantErrors.d.ts +4 -0
- package/dist/services/sketchAssistantErrors.js +22 -0
- package/dist/services/sketchAssistantOrchestrator.d.ts +7 -7
- package/dist/services/sketchAssistantOrchestrator.js +11 -24
- package/dist/services/sketchAssistantReply.js +3 -1
- package/dist/stores/chatStore.d.ts +5 -0
- package/dist/stores/chatStore.js +21 -0
- package/dist/types/agent.d.ts +4 -3
- package/dist/types/attachment.d.ts +16 -0
- package/dist/types/config.d.ts +6 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/mention.d.ts +40 -0
- package/dist/types/mention.js +8 -0
- package/dist/types/message.d.ts +60 -1
- package/dist/utils/agentTurn.d.ts +40 -9
- package/dist/utils/agentTurn.js +43 -11
- package/dist/utils/attachmentProgress.d.ts +12 -0
- package/dist/utils/attachmentProgress.js +18 -0
- package/dist/utils/bubbleRichFit.d.ts +68 -0
- package/dist/utils/bubbleRichFit.js +45 -0
- package/dist/utils/bubbleShrinkWrap.d.ts +8 -1
- package/dist/utils/bubbleShrinkWrap.js +113 -17
- package/dist/utils/codeChanges.d.ts +103 -0
- package/dist/utils/codeChanges.js +226 -0
- package/dist/utils/codeHighlight.js +1 -0
- package/dist/utils/codeStreamFormat.d.ts +5 -2
- package/dist/utils/composerMentions.d.ts +96 -0
- package/dist/utils/composerMentions.js +217 -0
- package/dist/utils/followUps.d.ts +74 -0
- package/dist/utils/followUps.js +116 -0
- package/dist/utils/generationArchiveStore.d.ts +6 -0
- package/dist/utils/generationArchiveStore.js +11 -3
- package/dist/utils/index.d.ts +12 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/orderedLine.d.ts +29 -0
- package/dist/utils/orderedLine.js +17 -0
- package/dist/utils/pastedContent.d.ts +99 -0
- package/dist/utils/pastedContent.js +258 -0
- package/dist/utils/questionCards.d.ts +131 -0
- package/dist/utils/questionCards.js +287 -0
- package/dist/utils/referenceItems.d.ts +36 -0
- package/dist/utils/referenceItems.js +56 -0
- package/dist/utils/sentReferenceBadges.d.ts +26 -0
- package/dist/utils/sentReferenceBadges.js +32 -0
- 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,24 +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 {
|
|
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
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';
|
|
17
49
|
import SelectedElementChip from './SelectedElementChip.svelte';
|
|
50
|
+
import ComposerMentionChips from './ComposerMentionChips.svelte';
|
|
18
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';
|
|
19
59
|
import ActionBar from './ActionBar.svelte';
|
|
20
60
|
import PanelChatHeader from './PanelChatHeader.svelte';
|
|
21
61
|
import ComposerAttachmentsTray from './ComposerAttachmentsTray.svelte';
|
|
62
|
+
import ChatMessageQueue from './ChatMessageQueue.svelte';
|
|
22
63
|
import { mergeSuggestionIntoDraft } from '../utils/suggestionDraft.js';
|
|
23
64
|
import { prepareChatImageFileForUpload } from '../utils/chatImageUploadPrep.js';
|
|
24
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';
|
|
25
83
|
import StatusIndicator from './StatusIndicator.svelte';
|
|
26
84
|
import { provideChatLanguage } from '../i18n/language.js';
|
|
27
|
-
import { actionFailureKey, contextTypeLabel } from '../i18n/labels.js';
|
|
85
|
+
import { actionFailureKey, contextTypeLabel, fileSizeText } from '../i18n/labels.js';
|
|
28
86
|
import type { ChatMessageKey } from '../i18n/messages.js';
|
|
29
87
|
|
|
30
88
|
// Props
|
|
@@ -60,7 +118,11 @@
|
|
|
60
118
|
* The parent handles the actual API call and can push assistant messages via addAssistantMessage.
|
|
61
119
|
* `options` carries per-send modifiers — e.g. `skipIntentGate: true` plus the tapped chip's
|
|
62
120
|
* `chipIntent` when the send is an explicit override of a clarification card (see
|
|
63
|
-
* 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. */
|
|
64
126
|
onSendMessage?: (
|
|
65
127
|
content: string,
|
|
66
128
|
attachments?: Array<{
|
|
@@ -70,13 +132,41 @@
|
|
|
70
132
|
fileType: string;
|
|
71
133
|
fileName: string;
|
|
72
134
|
fileSize?: number;
|
|
135
|
+
referenceLabel?: string;
|
|
73
136
|
}>,
|
|
74
137
|
options?: {
|
|
75
138
|
skipIntentGate?: boolean;
|
|
76
139
|
chipIntent?: ClarificationSuggestion['intent'];
|
|
77
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[];
|
|
78
154
|
},
|
|
79
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;
|
|
80
170
|
/** Backend integration callbacks — when provided, replace mock chatService methods */
|
|
81
171
|
onLoadConversations?: (contextId: string) => Promise<any[] | null>;
|
|
82
172
|
onLoadMessages?: (conversationId: string) => Promise<any[]>;
|
|
@@ -134,6 +224,20 @@
|
|
|
134
224
|
showConsoleControl?: boolean;
|
|
135
225
|
/** Increment to bounce the Send button and show the publish-first tooltip (host-driven). */
|
|
136
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;
|
|
137
241
|
consoleOpen?: boolean;
|
|
138
242
|
onToggleConsole?: () => void;
|
|
139
243
|
/** Custom title displayed in the header. If empty/undefined, defaults to context.title or context.id. */
|
|
@@ -184,6 +288,26 @@
|
|
|
184
288
|
generationThinking?: string;
|
|
185
289
|
/** Gateway build plan (`operation.metadata.plan`), or `null` to hide the plan card. */
|
|
186
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;
|
|
187
311
|
onStopGeneration?: () => void;
|
|
188
312
|
/**
|
|
189
313
|
* Renders the inline design-control card attached to an assistant reply
|
|
@@ -234,6 +358,12 @@
|
|
|
234
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.
|
|
235
359
|
*/
|
|
236
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;
|
|
237
367
|
/** Show the Widget Element Selector toggle button in the action bar (disabled by default). */
|
|
238
368
|
showSelector?: boolean;
|
|
239
369
|
/** Whether the Widget Element Selector is currently active for this chat's widget. */
|
|
@@ -256,6 +386,15 @@
|
|
|
256
386
|
* already open). Left out, the chips only say what they hold and are not clickable.
|
|
257
387
|
*/
|
|
258
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;
|
|
259
398
|
/** Model list for the DS composer Assistant menu; hidden when empty. */
|
|
260
399
|
composerModels?: ComposerModelOption[];
|
|
261
400
|
selectedComposerModelId?: string | null;
|
|
@@ -264,10 +403,19 @@
|
|
|
264
403
|
|
|
265
404
|
// Expose methods to parent component
|
|
266
405
|
interface ChatMethods {
|
|
267
|
-
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>;
|
|
268
407
|
addAssistantMessage: (content: string, skipPersist?: boolean, suggestions?: ClarificationSuggestion[], designControls?: DesignControlRequest) => void;
|
|
269
408
|
/** Add an assistant reply carrying an inline design-control card (no generation). */
|
|
270
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;
|
|
271
419
|
addUserMessage: (content: string, attachments?: Array<{ id: string; url: string; file_type: string; file_name: string; file_size?: number }>) => void;
|
|
272
420
|
/** Returns the current conversation ID (null if no conversation is active). */
|
|
273
421
|
getConversationId: () => string | null;
|
|
@@ -301,12 +449,13 @@
|
|
|
301
449
|
updateLastUserMessageAttachments: (attachments: Array<{ id?: string; url: string; file_type?: string; file_name?: string | null }>) => void;
|
|
302
450
|
}
|
|
303
451
|
|
|
304
|
-
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, activeSelectedElementId = null, onSelectedElementClick, 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();
|
|
305
453
|
|
|
306
454
|
// The words of everything inside the chat, in the language the page chose. The page can change the language while the
|
|
307
455
|
// chat is open: the settings are read again whenever they change (and setting the same language again does nothing).
|
|
308
456
|
const language = provideChatLanguage(untrack(() => ({ locale, messages: languageMessages })));
|
|
309
|
-
|
|
457
|
+
// `spokenLocale` is the language's own: the prop `locale` above is only what the page asked for.
|
|
458
|
+
const { t, locale: spokenLocale } = language;
|
|
310
459
|
$effect(() => {
|
|
311
460
|
language.update({ locale, messages: languageMessages });
|
|
312
461
|
});
|
|
@@ -337,6 +486,9 @@
|
|
|
337
486
|
}
|
|
338
487
|
|
|
339
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;
|
|
340
492
|
onDraftChange?.();
|
|
341
493
|
}
|
|
342
494
|
|
|
@@ -376,10 +528,178 @@
|
|
|
376
528
|
let pendingSkipIntentGate = $state(false);
|
|
377
529
|
let skipNextDraftIntentClear = $state(false);
|
|
378
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[]>([]);
|
|
379
533
|
let localIsRecording = $state(false);
|
|
380
534
|
let localIsSending = $state(false);
|
|
381
535
|
let localIsUploading = $state(false);
|
|
382
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
|
+
|
|
383
703
|
// The host hears about the draft when the person types (the setter of the composer binding below), not from an
|
|
384
704
|
// effect: an effect would call the host's callback inside its own tracking, and every piece of host state the
|
|
385
705
|
// callback reads would become a dependency of this effect. The creator's callback reads and writes the panel
|
|
@@ -409,6 +729,10 @@
|
|
|
409
729
|
}
|
|
410
730
|
}
|
|
411
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));
|
|
412
736
|
});
|
|
413
737
|
|
|
414
738
|
// Derived values from chat state
|
|
@@ -686,14 +1010,36 @@
|
|
|
686
1010
|
}
|
|
687
1011
|
}
|
|
688
1012
|
|
|
689
|
-
async function sendMessage(content: string, attachments?: Array<File | AttachmentInput>, options?: { skipIntentGate?: boolean; chipIntent?: ClarificationSuggestion['intent'] }) {
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
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),
|
|
694
1038
|
};
|
|
695
|
-
|
|
696
|
-
|
|
1039
|
+
if (!keepDraft) {
|
|
1040
|
+
pendingChipIntent = null;
|
|
1041
|
+
pendingSkipIntentGate = false;
|
|
1042
|
+
}
|
|
697
1043
|
if (!currentConversationId) {
|
|
698
1044
|
if (initializePromise) {
|
|
699
1045
|
try {
|
|
@@ -707,11 +1053,11 @@
|
|
|
707
1053
|
await createNewConversation();
|
|
708
1054
|
} catch (convErr) {
|
|
709
1055
|
console.error('No active conversation and failed to create one:', convErr);
|
|
710
|
-
return;
|
|
1056
|
+
return false;
|
|
711
1057
|
}
|
|
712
1058
|
if (!currentConversationId) {
|
|
713
1059
|
console.error('No active conversation after create');
|
|
714
|
-
return;
|
|
1060
|
+
return false;
|
|
715
1061
|
}
|
|
716
1062
|
}
|
|
717
1063
|
}
|
|
@@ -719,7 +1065,26 @@
|
|
|
719
1065
|
const activeConversationId = currentConversationId;
|
|
720
1066
|
if (!activeConversationId) {
|
|
721
1067
|
console.error('No active conversation');
|
|
722
|
-
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
|
+
}
|
|
723
1088
|
}
|
|
724
1089
|
|
|
725
1090
|
const normalizedAttachments: AttachmentInput[] = (attachments || []).map((att) => {
|
|
@@ -737,7 +1102,7 @@
|
|
|
737
1102
|
});
|
|
738
1103
|
|
|
739
1104
|
// chatLog('[Chat] Sending message:', content.substring(0, 60));
|
|
740
|
-
const noteLines = (selectedElements ?? [])
|
|
1105
|
+
const noteLines = (keepDraft ? [] : (selectedElements ?? []))
|
|
741
1106
|
.map((element) => element.note?.trim())
|
|
742
1107
|
.filter((note): note is string => !!note);
|
|
743
1108
|
const submittedContent = content.trim()
|
|
@@ -745,7 +1110,9 @@
|
|
|
745
1110
|
: noteLines.join('\n');
|
|
746
1111
|
const submittedAttachments = normalizedAttachments;
|
|
747
1112
|
const submittedSelectedElements =
|
|
748
|
-
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;
|
|
749
1116
|
const displayMessageContent = submittedSelectedElements
|
|
750
1117
|
? `[Selected: ${submittedSelectedElements.map((el) => el.label).join(', ')}]\n${submittedContent}`
|
|
751
1118
|
: submittedContent;
|
|
@@ -755,15 +1122,23 @@
|
|
|
755
1122
|
|
|
756
1123
|
// Clear the controlled input immediately. Waiting for persistence/codegen lets
|
|
757
1124
|
// image thumbnails linger during long generations and can re-save them as drafts.
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
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
|
+
}
|
|
764
1139
|
}
|
|
1140
|
+
notifyDraftChange();
|
|
765
1141
|
}
|
|
766
|
-
notifyDraftChange();
|
|
767
1142
|
|
|
768
1143
|
// Clear head/checkpoint marks — new prompt starts a new turn
|
|
769
1144
|
const existingMsgs = getCurrentMessages(activeConversationId);
|
|
@@ -832,23 +1207,30 @@
|
|
|
832
1207
|
fileType: resolvedFileType,
|
|
833
1208
|
fileName: rawFileName,
|
|
834
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,
|
|
835
1212
|
};
|
|
836
1213
|
});
|
|
837
1214
|
await onSendMessage(
|
|
838
1215
|
submittedContent,
|
|
839
1216
|
outgoing.length > 0 ? outgoing : undefined,
|
|
840
|
-
submittedSelectedElements
|
|
841
|
-
? {
|
|
1217
|
+
submittedSelectedElements || submittedMentions
|
|
1218
|
+
? {
|
|
1219
|
+
...mergedOptions,
|
|
1220
|
+
...(submittedSelectedElements ? { selectedElements: submittedSelectedElements } : {}),
|
|
1221
|
+
...(submittedMentions ? { mentions: submittedMentions } : {}),
|
|
1222
|
+
}
|
|
842
1223
|
: mergedOptions
|
|
843
1224
|
);
|
|
844
1225
|
} else {
|
|
845
1226
|
const aiResponse = await chatService.processAIResponse(activeConversationId, userMessage);
|
|
846
1227
|
chatStore.actions.addMessage(activeConversationId, aiResponse);
|
|
847
1228
|
}
|
|
848
|
-
|
|
1229
|
+
return true;
|
|
849
1230
|
} catch (error) {
|
|
850
1231
|
console.error('Failed to send message:', error);
|
|
851
1232
|
showFailure('chat.error.sendMessage');
|
|
1233
|
+
return false;
|
|
852
1234
|
} finally {
|
|
853
1235
|
localIsSending = false;
|
|
854
1236
|
}
|
|
@@ -857,7 +1239,7 @@
|
|
|
857
1239
|
/** Add an assistant message to the current conversation (called by parent via chatMethods).
|
|
858
1240
|
* When skipPersist is true, the message is shown locally but not saved to DB
|
|
859
1241
|
* (use when the backend already persists its own version). */
|
|
860
|
-
function addAssistantMessage(content: string, skipPersist = false, suggestions?: ClarificationSuggestion[], designControls?: DesignControlRequest) {
|
|
1242
|
+
function addAssistantMessage(content: string, skipPersist = false, suggestions?: ClarificationSuggestion[], designControls?: DesignControlRequest, questionCard?: QuestionCardModel) {
|
|
861
1243
|
if (!currentConversationId) return;
|
|
862
1244
|
const assistantMsg = {
|
|
863
1245
|
id: `assistant_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
|
@@ -873,7 +1255,10 @@
|
|
|
873
1255
|
_failed: false,
|
|
874
1256
|
_retryCount: 0,
|
|
875
1257
|
...(suggestions && suggestions.length > 0 ? { suggestions } : {}),
|
|
876
|
-
...(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 } : {})
|
|
877
1262
|
};
|
|
878
1263
|
chatStore.actions.addMessage(currentConversationId, assistantMsg);
|
|
879
1264
|
|
|
@@ -914,6 +1299,74 @@
|
|
|
914
1299
|
addAssistantMessage(content, false, undefined, designControls);
|
|
915
1300
|
}
|
|
916
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
|
+
|
|
917
1370
|
/** Add a user message to the UI without triggering generation (called by parent via chatMethods).
|
|
918
1371
|
* Also persists to DB via onSaveMessage so multi-turn history is complete. */
|
|
919
1372
|
function addUserMessage(content: string, attachments?: Array<{ id: string; url: string; file_type: string; file_name: string; file_size?: number }>) {
|
|
@@ -975,11 +1428,18 @@
|
|
|
975
1428
|
const screenshotFile = new File([blob], 'screenshot.png', { type: 'image/png' });
|
|
976
1429
|
|
|
977
1430
|
const attachmentInput: AttachmentInput = {
|
|
1431
|
+
// How the tray and the upload find this tile again: see `updateAttachment`.
|
|
1432
|
+
id: generateFileId(),
|
|
978
1433
|
file: screenshotFile,
|
|
979
1434
|
type: 'image',
|
|
980
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,
|
|
981
1439
|
uploadProgress: 0
|
|
982
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]);
|
|
983
1443
|
|
|
984
1444
|
localAttachments = [...localAttachments, attachmentInput];
|
|
985
1445
|
const attachmentIndex = localAttachments.length - 1;
|
|
@@ -989,7 +1449,7 @@
|
|
|
989
1449
|
console.log('[Chat] attachments now', localAttachments.length);
|
|
990
1450
|
|
|
991
1451
|
if (onUploadChatFile) {
|
|
992
|
-
await uploadAttachmentToCdn(screenshotFile, attachmentInput,
|
|
1452
|
+
await uploadAttachmentToCdn(screenshotFile, attachmentInput, {
|
|
993
1453
|
source: 'preview-screenshot',
|
|
994
1454
|
});
|
|
995
1455
|
}
|
|
@@ -1030,10 +1490,12 @@
|
|
|
1030
1490
|
|
|
1031
1491
|
// Create AttachmentInput object
|
|
1032
1492
|
const attachmentInput: AttachmentInput = {
|
|
1493
|
+
id: generateFileId(),
|
|
1033
1494
|
file: mockRecording,
|
|
1034
1495
|
type: 'video',
|
|
1035
1496
|
uploadProgress: 0
|
|
1036
1497
|
};
|
|
1498
|
+
labelNewAttachments([attachmentInput]);
|
|
1037
1499
|
|
|
1038
1500
|
// Add to attachments
|
|
1039
1501
|
localAttachments = [...localAttachments, attachmentInput];
|
|
@@ -1051,47 +1513,73 @@
|
|
|
1051
1513
|
return !!file && file.size > 0;
|
|
1052
1514
|
}
|
|
1053
1515
|
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
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,
|
|
1057
1527
|
);
|
|
1528
|
+
return true;
|
|
1058
1529
|
}
|
|
1059
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
|
+
*/
|
|
1060
1535
|
async function uploadAttachmentToCdn(
|
|
1061
1536
|
file: File,
|
|
1062
1537
|
entry: AttachmentInput,
|
|
1063
|
-
attachmentIndex: number,
|
|
1064
1538
|
options: { source?: string } = {},
|
|
1065
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).
|
|
1066
1547
|
if (!onUploadChatFile || !isCdnUploadableFile(file)) {
|
|
1067
|
-
|
|
1068
|
-
if (!entry.preview) entry.preview = createLocalPreviewUrl(file);
|
|
1548
|
+
if (!entry.preview) updateAttachment(attachmentId, { preview: createLocalPreviewUrl(file) });
|
|
1069
1549
|
return;
|
|
1070
1550
|
}
|
|
1071
1551
|
|
|
1072
1552
|
try {
|
|
1073
|
-
|
|
1553
|
+
updateAttachment(attachmentId, { uploadProgress: 0 });
|
|
1074
1554
|
const uploaded = await onUploadChatFile(file, {
|
|
1075
|
-
onProgress: (percentage) =>
|
|
1555
|
+
onProgress: (percentage) => updateAttachment(attachmentId, { uploadProgress: percentage }),
|
|
1076
1556
|
source: options.source,
|
|
1077
1557
|
});
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
if (
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
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;
|
|
1084
1563
|
}
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
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);
|
|
1088
1573
|
notifyDraftChange();
|
|
1089
1574
|
// chatLog('[Chat] Attachment uploaded to CDN:', uploaded.url.slice(0, 80), 'id:', uploaded.id);
|
|
1090
1575
|
} catch (uploadError) {
|
|
1091
1576
|
console.error('[Chat] Upload failed, using local preview where possible:', uploadError);
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
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
|
+
});
|
|
1095
1583
|
// Soft warning only — local File still works for codegen vision; don't block the chat UI.
|
|
1096
1584
|
const detail =
|
|
1097
1585
|
uploadError instanceof Error ? uploadError.message : 'Attachment upload failed.';
|
|
@@ -1128,7 +1616,8 @@
|
|
|
1128
1616
|
|
|
1129
1617
|
function removeFooterAttachment(index: number) {
|
|
1130
1618
|
const removed = localAttachments[index];
|
|
1131
|
-
|
|
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)) {
|
|
1132
1621
|
try {
|
|
1133
1622
|
URL.revokeObjectURL(removed.preview);
|
|
1134
1623
|
} catch {
|
|
@@ -1136,6 +1625,19 @@
|
|
|
1136
1625
|
}
|
|
1137
1626
|
}
|
|
1138
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
|
+
}
|
|
1139
1641
|
notifyDraftChange();
|
|
1140
1642
|
chatLog('[Chat] Removed footer attachment at index:', index);
|
|
1141
1643
|
}
|
|
@@ -1171,28 +1673,32 @@
|
|
|
1171
1673
|
async function rememberImageForDraft(entry: AttachmentInput): Promise<void> {
|
|
1172
1674
|
const dataUrl = await readImageAsDataUrl(entry.file);
|
|
1173
1675
|
if (!dataUrl) return;
|
|
1174
|
-
entry.persistedDataUrl = dataUrl;
|
|
1175
1676
|
// The attachment may have been removed while the file was read: only a tray that still holds it is saved.
|
|
1176
|
-
//
|
|
1177
|
-
|
|
1178
|
-
if (!inTray) return;
|
|
1179
|
-
inTray.persistedDataUrl = dataUrl;
|
|
1180
|
-
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;
|
|
1181
1679
|
notifyDraftChange();
|
|
1182
1680
|
}
|
|
1183
1681
|
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
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;
|
|
1192
1694
|
|
|
1193
1695
|
const previousCount = localAttachments.length;
|
|
1194
|
-
const newAttachments: AttachmentInput[] =
|
|
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(),
|
|
1195
1700
|
file,
|
|
1701
|
+
label,
|
|
1196
1702
|
type: file.type.startsWith('image/')
|
|
1197
1703
|
? 'image'
|
|
1198
1704
|
: file.type.startsWith('video/')
|
|
@@ -1203,7 +1709,7 @@
|
|
|
1203
1709
|
uploadProgress: 0,
|
|
1204
1710
|
}));
|
|
1205
1711
|
|
|
1206
|
-
|
|
1712
|
+
labelNewAttachments(newAttachments);
|
|
1207
1713
|
localAttachments = [...localAttachments, ...newAttachments];
|
|
1208
1714
|
notifyDraftChange();
|
|
1209
1715
|
expandChatForAttachmentGrowth(previousCount);
|
|
@@ -1211,17 +1717,71 @@
|
|
|
1211
1717
|
console.log('[Chat] attachments now', localAttachments.length);
|
|
1212
1718
|
// The draft keeps each picture as it is attached, before the upload ends (a refresh can come at any time).
|
|
1213
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?.();
|
|
1214
1722
|
|
|
1215
1723
|
try {
|
|
1216
1724
|
localIsUploading = true;
|
|
1217
|
-
for (let i = 0; i <
|
|
1218
|
-
await uploadAttachmentToCdn(
|
|
1725
|
+
for (let i = 0; i < usableEntries.length; i++) {
|
|
1726
|
+
await uploadAttachmentToCdn(usableEntries[i].file, newAttachments[i]);
|
|
1219
1727
|
}
|
|
1220
1728
|
} finally {
|
|
1221
1729
|
localIsUploading = false;
|
|
1222
1730
|
}
|
|
1223
1731
|
|
|
1224
|
-
chatLog('[Chat] Files added as attachments:',
|
|
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
|
+
}
|
|
1225
1785
|
}
|
|
1226
1786
|
|
|
1227
1787
|
async function handleUpload() {
|
|
@@ -1283,6 +1843,30 @@
|
|
|
1283
1843
|
}
|
|
1284
1844
|
}
|
|
1285
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
|
+
|
|
1286
1870
|
/** The sketch's "Undo" chip is used once: take it off the last assistant message that carries it. */
|
|
1287
1871
|
function clearUndoSketchSuggestionChip() {
|
|
1288
1872
|
const convId = currentConversationId;
|
|
@@ -1332,6 +1916,14 @@
|
|
|
1332
1916
|
fillDraftFromSuggestion(suggestion.label, suggestion.intent);
|
|
1333
1917
|
}
|
|
1334
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
|
+
|
|
1335
1927
|
async function handleRetryMessage(content: string) {
|
|
1336
1928
|
const retryContent = content.trim();
|
|
1337
1929
|
if (!retryContent) return;
|
|
@@ -1485,30 +2077,70 @@
|
|
|
1485
2077
|
}
|
|
1486
2078
|
}
|
|
1487
2079
|
|
|
1488
|
-
|
|
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) {
|
|
1489
2089
|
if (!isValidImageDataUrl(dataUrl)) {
|
|
1490
2090
|
chatWarn('[Chat] Skipping invalid image data URL for attachment restore');
|
|
1491
2091
|
return;
|
|
1492
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;
|
|
1493
2096
|
try {
|
|
2097
|
+
// Reading and preparing are done by all the pictures at once; only the tiles wait for their place.
|
|
1494
2098
|
const response = await fetch(dataUrl);
|
|
1495
2099
|
const blob = await response.blob();
|
|
1496
2100
|
const mimeMatch = dataUrl.match(/data:(image\/[^;]+);/);
|
|
1497
2101
|
const mimeType = mimeMatch?.[1] || blob.type || 'image/png';
|
|
1498
|
-
const
|
|
1499
|
-
|
|
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(),
|
|
1500
2122
|
file,
|
|
1501
2123
|
type: 'image',
|
|
1502
|
-
preview:
|
|
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,
|
|
1503
2128
|
uploadProgress: 0,
|
|
1504
2129
|
};
|
|
2130
|
+
labelNewAttachments([attachmentInput], [forcedLabel]);
|
|
1505
2131
|
const attachmentIndex = localAttachments.length;
|
|
1506
2132
|
localAttachments = [...localAttachments, attachmentInput];
|
|
1507
2133
|
notifyDraftChange();
|
|
1508
2134
|
expandChatForAttachmentGrowth(attachmentIndex);
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
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);
|
|
1512
2144
|
// chatLog('Image attachment added from external paste');
|
|
1513
2145
|
} catch (err) {
|
|
1514
2146
|
console.error('[Chat] Failed to add image attachment:', err);
|
|
@@ -1516,13 +2148,24 @@
|
|
|
1516
2148
|
}
|
|
1517
2149
|
|
|
1518
2150
|
function getDraftText(): string {
|
|
1519
|
-
|
|
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;
|
|
1520
2153
|
}
|
|
1521
2154
|
|
|
1522
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();
|
|
1523
2158
|
localInputText = text;
|
|
1524
2159
|
pendingChipIntent = null;
|
|
1525
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;
|
|
1526
2169
|
}
|
|
1527
2170
|
|
|
1528
2171
|
function getAttachmentDataUrls(): string[] {
|
|
@@ -1537,7 +2180,9 @@
|
|
|
1537
2180
|
* `getAttachmentDataUrls` stays the list of addresses to send with a message (a CDN address when the upload ended).
|
|
1538
2181
|
*/
|
|
1539
2182
|
function getDraftAttachmentDataUrls(): string[] {
|
|
1540
|
-
|
|
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
|
|
1541
2186
|
.filter((att) => att.type === 'image' || att.file?.type?.startsWith('image/'))
|
|
1542
2187
|
.map((att) => att.persistedDataUrl || (att.preview?.startsWith('data:') ? att.preview : '') || '')
|
|
1543
2188
|
.filter((url) => !!url);
|
|
@@ -1550,6 +2195,7 @@
|
|
|
1550
2195
|
}
|
|
1551
2196
|
|
|
1552
2197
|
function restoreAttachmentsFromDataUrls(dataUrls: string[]) {
|
|
2198
|
+
if (queueEditingId) discardQueueEdit();
|
|
1553
2199
|
for (const att of localAttachments) {
|
|
1554
2200
|
if (att.preview && att.preview.startsWith('blob:')) {
|
|
1555
2201
|
try {
|
|
@@ -1560,9 +2206,28 @@
|
|
|
1560
2206
|
}
|
|
1561
2207
|
}
|
|
1562
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;
|
|
1563
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;
|
|
1564
2227
|
for (let i = 0; i < validUrls.length; i++) {
|
|
1565
|
-
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
|
+
});
|
|
1566
2231
|
}
|
|
1567
2232
|
if (validUrls.length < dataUrls.length) {
|
|
1568
2233
|
chatWarn('[Chat] Dropped invalid restored attachment(s):', dataUrls.length - validUrls.length);
|
|
@@ -1570,6 +2235,7 @@
|
|
|
1570
2235
|
}
|
|
1571
2236
|
|
|
1572
2237
|
function clearDraft() {
|
|
2238
|
+
if (queueEditingId) discardQueueEdit();
|
|
1573
2239
|
for (const att of localAttachments) {
|
|
1574
2240
|
if (att.preview && att.preview.startsWith('blob:')) {
|
|
1575
2241
|
try {
|
|
@@ -1581,11 +2247,282 @@
|
|
|
1581
2247
|
}
|
|
1582
2248
|
localInputText = '';
|
|
1583
2249
|
localAttachments = [];
|
|
2250
|
+
referenceGenericFloor = 0;
|
|
2251
|
+
highlightedReferenceLabel = null;
|
|
2252
|
+
restoredLabelsToCheck = new Set();
|
|
2253
|
+
localMentions = [];
|
|
1584
2254
|
pendingChipIntent = null;
|
|
1585
2255
|
pendingSkipIntentGate = false;
|
|
1586
2256
|
notifyDraftChange();
|
|
1587
2257
|
}
|
|
1588
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
|
+
|
|
1589
2526
|
const CODEGEN_COMMIT_REGEX = /(?:Commit:?\s+|Code saved\s*\()([a-f0-9]{7,40})/i;
|
|
1590
2527
|
|
|
1591
2528
|
function isPreviewRecompiledMessage(content?: string | null): boolean {
|
|
@@ -1860,9 +2797,10 @@
|
|
|
1860
2797
|
// chatLog('[Chat] Keep local messages — DB reload would regress HEAD commit');
|
|
1861
2798
|
} else {
|
|
1862
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
|
|
1863
2801
|
chatStore.actions.setMessages(
|
|
1864
2802
|
currentConversationId,
|
|
1865
|
-
keepLocalDesignCards(loaded, localMsgs),
|
|
2803
|
+
keepLocalQuestionCards(keepLocalDesignCards(loaded, localMsgs), localMsgs),
|
|
1866
2804
|
);
|
|
1867
2805
|
}
|
|
1868
2806
|
}
|
|
@@ -1915,6 +2853,7 @@
|
|
|
1915
2853
|
sendMessage,
|
|
1916
2854
|
addAssistantMessage,
|
|
1917
2855
|
addDesignControlsMessage,
|
|
2856
|
+
addQuestionCardMessage,
|
|
1918
2857
|
addUserMessage,
|
|
1919
2858
|
getConversationId: () => currentConversationId,
|
|
1920
2859
|
addImageAttachment,
|
|
@@ -2135,13 +3074,18 @@
|
|
|
2135
3074
|
generationStream={isGenerating ? generationStream : ''}
|
|
2136
3075
|
generationThinking={isGenerating ? generationThinking : ''}
|
|
2137
3076
|
generationPlan={isGenerating ? generationPlan : null}
|
|
3077
|
+
generationPlanPending={isGenerating ? generationPlanPending : false}
|
|
3078
|
+
{generationChanges}
|
|
2138
3079
|
onSuggestionSelected={handleSuggestionSelected}
|
|
3080
|
+
draftText={localInputText}
|
|
2139
3081
|
onRestoreCheckpoint={onRestoreCheckpointBackend ? handleRestoreCheckpoint : undefined}
|
|
2140
3082
|
onRateVersion={onRateVersion ? handleRateVersion : undefined}
|
|
2141
3083
|
versionRatings={shownVersionRatings}
|
|
2142
3084
|
onRetryMessage={handleRetryMessage}
|
|
2143
3085
|
{onUpgradePlans}
|
|
2144
3086
|
onClarificationSuggestionSelected={handleClarificationSuggestionSelected}
|
|
3087
|
+
onQuestionSkipped={handleQuestionSkipped}
|
|
3088
|
+
onQuestionCardAnswered={handleQuestionCardAnswered}
|
|
2145
3089
|
{renderDesignControls}
|
|
2146
3090
|
{storedLineLabel}
|
|
2147
3091
|
conversationId={currentConversationId}
|
|
@@ -2150,6 +3094,7 @@
|
|
|
2150
3094
|
{renderAgentAvatar}
|
|
2151
3095
|
{agentSound}
|
|
2152
3096
|
{tightBubbles}
|
|
3097
|
+
{referenceBadges}
|
|
2153
3098
|
/>
|
|
2154
3099
|
</div>
|
|
2155
3100
|
|
|
@@ -2206,11 +3151,26 @@
|
|
|
2206
3151
|
</div>
|
|
2207
3152
|
{/if}
|
|
2208
3153
|
|
|
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
|
+
/>
|
|
3165
|
+
</div>
|
|
3166
|
+
{/if}
|
|
3167
|
+
|
|
2209
3168
|
{#if localAttachments.length > 0}
|
|
2210
3169
|
<ComposerAttachmentsTray
|
|
2211
3170
|
attachments={localAttachments}
|
|
2212
3171
|
previewUrl={getFooterAttachmentPreviewUrl}
|
|
2213
3172
|
isImage={isImageAttachment}
|
|
3173
|
+
highlightedLabel={highlightedReferenceLabel}
|
|
2214
3174
|
onRemove={removeFooterAttachment}
|
|
2215
3175
|
onOpenPreview={(url) => {
|
|
2216
3176
|
footerLightboxSrc = url;
|
|
@@ -2248,9 +3208,35 @@
|
|
|
2248
3208
|
</div>
|
|
2249
3209
|
{/snippet}
|
|
2250
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
|
+
|
|
2251
3229
|
<ChatComposer
|
|
2252
3230
|
{sendNudgeToken}
|
|
2253
|
-
|
|
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}
|
|
2254
3240
|
panelLayout={showPanelHeader}
|
|
2255
3241
|
placeholder={showPanelHeader
|
|
2256
3242
|
? (currentMessages?.length ?? 0) > 0
|
|
@@ -2272,14 +3258,19 @@
|
|
|
2272
3258
|
models={composerModels}
|
|
2273
3259
|
selectedModelId={selectedComposerModelId}
|
|
2274
3260
|
onSelectModel={onSelectComposerModel}
|
|
2275
|
-
onStop={
|
|
2276
|
-
|
|
3261
|
+
onStop={handleStopPressed}
|
|
3262
|
+
onQueue={messageQueueEnabled ? handleQueueMessage : undefined}
|
|
3263
|
+
editing={composerEditing}
|
|
3264
|
+
onSend={(content, sendOptions) => {
|
|
2277
3265
|
const attachmentInputs = localAttachments.length > 0 ? localAttachments : undefined;
|
|
2278
|
-
|
|
3266
|
+
handleComposerSend(content, attachmentInputs, sendOptions);
|
|
2279
3267
|
}}
|
|
2280
3268
|
onFilesDropped={(files) => {
|
|
2281
3269
|
void addFilesAsAttachments(files);
|
|
2282
3270
|
}}
|
|
3271
|
+
onFilesPasted={config?.features?.fileUpload !== false
|
|
3272
|
+
? (pastedAttachments) => void addPastedAttachments(pastedAttachments)
|
|
3273
|
+
: undefined}
|
|
2283
3274
|
onAttach={config?.features?.fileUpload !== false ? () => void handleUpload() : undefined}
|
|
2284
3275
|
canSendWithoutText={(selectedElements ?? []).some((element) => !!element.note?.trim())}
|
|
2285
3276
|
/>
|