@widgetic/chat 0.1.25 → 0.1.27

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.
@@ -143,6 +143,8 @@
143
143
  viewingVersion?: number | null;
144
144
  /** Host is running widget codegen — send button becomes stop. */
145
145
  isGenerating?: boolean;
146
+ /** Tail of the model stream while codegen is in flight. */
147
+ generationStream?: string;
146
148
  onStopGeneration?: () => void;
147
149
  /**
148
150
  * Renders the inline design-control card attached to an assistant reply
@@ -216,7 +218,7 @@
216
218
  updateLastUserMessageAttachments: (attachments: Array<{ id?: string; url: string; file_type?: string; file_name?: string | null }>) => void;
217
219
  }
218
220
 
219
- let { context, config = {}, class: className = '', locale = null, messages: languageMessages = null, dir = undefined, style = '', isCompleted = false, onStatsUpdate, onChatReady, onCompletionToggle, onSendMessage, onLoadConversations, onLoadMessages, onCreateConversation, onSaveMessage, onRestoreCheckpointBackend, onTakeScreenshot, onConversationChange, onDraftChange, onRetryMessage, onUpgradePlans, onUploadChatFile, showHeader = true, headerTitle = '', showDescriptionHeader = true, showLogs = true, resizeExpandsContainer = false, onMessagesResize, onMessagesDrag, onMessagesDragEnd, onLoadOlderMessages, widgetHeadCommitSha = null, versionLabelByCommitSha = {}, versionLabelByPrompt = {}, versionTimeline = [], viewingVersion = null, isGenerating = false, onStopGeneration, renderDesignControls, actionBarExtras, completeReplacement, replaceComplete = false, showSelector = false, isSelectorActive = false, selectedElements = [], onToggleSelector, onRemoveSelectedElement, onClearSelectedElements, composerExtras, storedLineLabel }: Props = $props();
221
+ let { context, config = {}, class: className = '', locale = null, messages: languageMessages = null, dir = undefined, style = '', isCompleted = false, onStatsUpdate, onChatReady, onCompletionToggle, onSendMessage, onLoadConversations, onLoadMessages, onCreateConversation, onSaveMessage, onRestoreCheckpointBackend, onTakeScreenshot, onConversationChange, onDraftChange, onRetryMessage, onUpgradePlans, onUploadChatFile, showHeader = true, headerTitle = '', showDescriptionHeader = true, showLogs = true, resizeExpandsContainer = false, onMessagesResize, onMessagesDrag, onMessagesDragEnd, onLoadOlderMessages, widgetHeadCommitSha = null, versionLabelByCommitSha = {}, versionLabelByPrompt = {}, versionTimeline = [], viewingVersion = null, isGenerating = false, generationStream = '', onStopGeneration, renderDesignControls, actionBarExtras, completeReplacement, replaceComplete = false, showSelector = false, isSelectorActive = false, selectedElements = [], onToggleSelector, onRemoveSelectedElement, onClearSelectedElements, composerExtras, storedLineLabel }: Props = $props();
220
222
 
221
223
  // The words of everything inside the chat, in the language the page chose. The page can change the language while the
222
224
  // chat is open: the settings are read again whenever they change (and setting the same language again does nothing).
@@ -1247,7 +1249,24 @@
1247
1249
  const rollback = result?.gitRollback || result?.git_rollback || result?.data?.gitRollback || result?.data?.git_rollback;
1248
1250
  const newSha = String(rollback?.commitId || rollback?.commit_id || '').trim();
1249
1251
  if (rollback?.success && newSha && promptCopy) {
1250
- addUserMessage(promptCopy);
1252
+ const attachmentSource = (checkpointUser?.attachments || []).some((attachment: { url?: string }) => !!attachment?.url)
1253
+ ? checkpointUser
1254
+ : currentMessages.find((message: any) =>
1255
+ message?.messageType === 'user'
1256
+ && String(message.messageContent || '').trim() === promptCopy
1257
+ && (message.attachments || []).some((attachment: { url?: string }) => !!attachment?.url)
1258
+ );
1259
+ const restoredAttachments = (attachmentSource?.attachments || [])
1260
+ .filter((attachment: { url?: string }) => !!attachment?.url)
1261
+ .map((attachment: { id?: string; url: string; file_type?: string; fileType?: string; file_name?: string; fileName?: string; file_size?: number; fileSize?: number }) => ({
1262
+ id: String(attachment.id || attachment.url),
1263
+ url: attachment.url,
1264
+ file_type: attachment.file_type || attachment.fileType || 'image/png',
1265
+ file_name: attachment.file_name || attachment.fileName || 'design-screenshot.png',
1266
+ file_size: attachment.file_size ?? attachment.fileSize,
1267
+ }));
1268
+ console.log('[Chat] Restore echo attachments', restoredAttachments.length);
1269
+ addUserMessage(promptCopy, restoredAttachments.length ? restoredAttachments : undefined);
1251
1270
  addAssistantMessage(`Restored from ${sourceLabel}\n\nCommit: ${newSha.slice(0, 8)}`);
1252
1271
  }
1253
1272
  } catch (backendError) {
@@ -1862,6 +1881,7 @@
1862
1881
  onRequestOlderMessages={() => loadOlderMessages()}
1863
1882
  showCommitMeta={showLogs}
1864
1883
  isGenerating={isGenerating || isLoading}
1884
+ generationStream={isGenerating ? generationStream : ''}
1865
1885
  onSuggestionSelected={handleSuggestionSelected}
1866
1886
  onRestoreCheckpoint={handleRestoreCheckpoint}
1867
1887
  onRetryMessage={handleRetryMessage}
@@ -186,6 +186,8 @@ interface Props {
186
186
  viewingVersion?: number | null;
187
187
  /** Host is running widget codegen — send button becomes stop. */
188
188
  isGenerating?: boolean;
189
+ /** Tail of the model stream while codegen is in flight. */
190
+ generationStream?: string;
189
191
  onStopGeneration?: () => void;
190
192
  /**
191
193
  * Renders the inline design-control card attached to an assistant reply
@@ -63,6 +63,8 @@
63
63
  showCommitMeta?: boolean;
64
64
  /** True while codegen / send is in flight — suppresses premature Retry. */
65
65
  isGenerating?: boolean;
66
+ /** Tail of the model stream for the generation in flight. Empty when it is only thinking. */
67
+ generationStream?: string;
66
68
  onSuggestionSelected?: (suggestion: string) => void;
67
69
  onRestoreCheckpoint?: (messageId: string, messageIndex: number) => void;
68
70
  onRetryMessage?: (content: string) => void;
@@ -102,6 +104,7 @@
102
104
  viewingVersion = null,
103
105
  showCommitMeta = true,
104
106
  isGenerating = false,
107
+ generationStream = '',
105
108
  onSuggestionSelected,
106
109
  onRestoreCheckpoint,
107
110
  onRetryMessage,
@@ -340,8 +343,15 @@
340
343
  return expanded;
341
344
  }
342
345
 
346
+ function turnIsRestoreEcho(turn: ChatTurn): boolean {
347
+ return turn.replies.some((reply) => isRestoreForkContent(reply.messageContent));
348
+ }
349
+
343
350
  function visibleUserAttachments(turn: ChatTurn, turnIndex: number) {
344
351
  const attachments = turn.user?.attachments || [];
352
+ // Restore adds a copy of the prompt. That copy should keep the sketch image
353
+ // the original turn showed, even though it is not the first message.
354
+ if (turnIsRestoreEcho(turn)) return attachments;
345
355
  const hasEarlierUser = chatTurns.slice(0, turnIndex).some((candidate) => candidate.user);
346
356
  if (!hasEarlierUser) return attachments;
347
357
  // The opening sketch stays on the first turn. Later prompts must not
@@ -551,7 +561,27 @@
551
561
  function versionLabelFromPrompt(turn: ChatTurn): string | null {
552
562
  const key = versionPromptKey(turn.user?.messageContent);
553
563
  if (!key) return null;
554
- return versionLabelByPrompt[key] ?? null;
564
+ const label = versionLabelByPrompt[key] ?? null;
565
+ if (!label) return null;
566
+ // The same sentence is sent again on Retry. The prompt map only has one
567
+ // number, so the new bubble was wearing the previous version until its
568
+ // own commit landed.
569
+ const index = chatTurns.indexOf(turn);
570
+ if (index > 0) {
571
+ const earlierSamePrompt = chatTurns.slice(0, index).some((candidate) =>
572
+ versionPromptKey(candidate.user?.messageContent) === key
573
+ );
574
+ if (earlierSamePrompt) return null;
575
+ }
576
+ return label;
577
+ }
578
+
579
+ function earlierTurnRepeatsThisPrompt(turn: ChatTurn, turnIndex: number): boolean {
580
+ const key = versionPromptKey(turn.user?.messageContent);
581
+ if (!key) return false;
582
+ return chatTurns.slice(0, turnIndex).some((candidate) =>
583
+ versionPromptKey(candidate.user?.messageContent) === key
584
+ );
555
585
  }
556
586
 
557
587
  /** The dropdown's version, else the commit sha, decides which turn is current. */
@@ -754,6 +784,9 @@
754
784
  }
755
785
 
756
786
  function shouldPinChatToBottom(): boolean {
787
+ // A live generation grows under the list. Stay on that tail even when an
788
+ // older version is the one marked "you are here".
789
+ if (isGenerating) return true;
757
790
  if (viewingVersion != null) {
758
791
  const hereIndex = viewingTurnIndex();
759
792
  if (hereIndex < 0 || hereIndex < chatTurns.length - 1) return false;
@@ -761,6 +794,11 @@
761
794
  return autoScroll;
762
795
  }
763
796
 
797
+ function scrollGenerationTail(): void {
798
+ const tail = messagesContainer?.querySelector('.generation-stream');
799
+ if (tail) tail.scrollTop = tail.scrollHeight;
800
+ }
801
+
764
802
  /**
765
803
  * Scroll to the selected version only when that selection changes.
766
804
  * A page of older messages must not pull the list back to the latest turn.
@@ -771,10 +809,11 @@
771
809
  const sha = widgetHeadCommitSha ?? '';
772
810
  const key = `${version ?? ''}|${sha}`;
773
811
  const turnCount = chatTurns.length;
774
- if ((version == null && !sha) || !messagesContainer || turnCount === 0) return;
812
+ const generating = isGenerating;
813
+ if (generating || (version == null && !sha) || !messagesContainer || turnCount === 0) return;
775
814
  if (key === scrolledViewKey || pendingOlderScroll) return;
776
815
  void tick().then(() => {
777
- if (!messagesContainer || pendingOlderScroll) return;
816
+ if (!messagesContainer || pendingOlderScroll || isGenerating) return;
778
817
  const here = messagesContainer.querySelector('[data-you-are-here="true"]');
779
818
  if (!here) return;
780
819
  scrollIntoViewWithin(messagesContainer, here);
@@ -892,13 +931,15 @@
892
931
  }
893
932
  const pinToBottom = shouldPinChatToBottom();
894
933
  const messageCount = visibleMessages.length;
934
+ const streamLength = generationStream.length;
895
935
  if (!initialBottomPinned && messagesContainer && messageCount > 0 && !pendingOlderScroll) {
896
936
  void tick().then(() => settleMessagesToEnd());
897
937
  }
898
- if (messagesContainer && pinToBottom && messageCount > 0 && !pendingOlderScroll && initialBottomPinned) {
938
+ if (messagesContainer && pinToBottom && (messageCount > 0 || streamLength > 0) && !pendingOlderScroll && initialBottomPinned) {
899
939
  tick().then(() => {
900
940
  if (!messagesContainer || pendingOlderScroll || !shouldPinChatToBottom()) return;
901
941
  scrollMessagesToEnd();
942
+ if (isGenerating) scrollGenerationTail();
902
943
  });
903
944
  }
904
945
  });
@@ -1371,7 +1412,7 @@
1371
1412
  <div class="message-attachments-container">
1372
1413
  <AttachmentDisplay attachments={shownAttachments} variant="message" showPreview={true} />
1373
1414
  </div>
1374
- {:else if userMsg.messageContent && messageImpliesAttachedFile(userMsg.messageContent)}
1415
+ {:else if userMsg.messageContent && messageImpliesAttachedFile(userMsg.messageContent) && !earlierTurnRepeatsThisPrompt(turn, turnIndex)}
1375
1416
  <div class="wc-missing-attachment-placeholder message-attachments-container flex flex-col items-center gap-1 px-3 pb-3">
1376
1417
  <div class="wc-missing-attachment-thumb flex h-20 w-28 flex-col items-center justify-center gap-1 rounded-md border border-dashed border-gray-300 bg-gray-100 text-gray-400" title={$t('chat.messages.attachmentMissingTitle')} aria-label={$t('chat.messages.attachmentMissingLabel')}>
1377
1418
  <svg class="h-8 w-8 shrink-0" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" aria-hidden="true">
@@ -1564,8 +1605,8 @@
1564
1605
  <p class="text-gray-500 text-xs">{$t('chat.messages.loadingHistory')}</p>
1565
1606
  </div>
1566
1607
  </div>
1567
- {:else if isLoading}
1568
- <div class="loading-indicator-ct flex items-center justify-start p-3">
1608
+ {:else if isLoading || isGenerating}
1609
+ <div class="loading-indicator-ct flex flex-col items-start gap-2 p-3">
1569
1610
  <div class="rounded-lg shadow-sm text-sm leading-relaxed p-3"
1570
1611
  style="background-color: {MESSAGE_COLORS.ASSISTANT}; color: {MESSAGE_COLORS.ASSISTANT_TEXT}"
1571
1612
  >
@@ -1576,11 +1617,30 @@
1576
1617
  size="md"
1577
1618
  />
1578
1619
  </div>
1620
+ {#if isGenerating && generationStream}
1621
+ <pre class="generation-stream m-0 max-h-40 w-full overflow-auto whitespace-pre-wrap break-words rounded-lg bg-gray-900 p-3 font-mono text-[11px] leading-relaxed text-gray-100">{generationStream}</pre>
1622
+ {/if}
1579
1623
  </div>
1580
1624
  {/if}
1581
1625
  </div>
1582
1626
 
1583
1627
  <style>
1628
+ .generation-stream {
1629
+ scrollbar-width: thin;
1630
+ scrollbar-color: rgba(255, 255, 255, 0.35) transparent;
1631
+ }
1632
+ .generation-stream::-webkit-scrollbar {
1633
+ width: 8px;
1634
+ height: 8px;
1635
+ background: transparent;
1636
+ }
1637
+ .generation-stream::-webkit-scrollbar-track {
1638
+ background: transparent;
1639
+ }
1640
+ .generation-stream::-webkit-scrollbar-thumb {
1641
+ background: rgba(255, 255, 255, 0.35);
1642
+ border-radius: 4px;
1643
+ }
1584
1644
  .loading {
1585
1645
  opacity: 0.75;
1586
1646
  }
@@ -49,6 +49,8 @@ interface Props {
49
49
  showCommitMeta?: boolean;
50
50
  /** True while codegen / send is in flight — suppresses premature Retry. */
51
51
  isGenerating?: boolean;
52
+ /** Tail of the model stream for the generation in flight. Empty when it is only thinking. */
53
+ generationStream?: string;
52
54
  onSuggestionSelected?: (suggestion: string) => void;
53
55
  onRestoreCheckpoint?: (messageId: string, messageIndex: number) => void;
54
56
  onRetryMessage?: (content: string) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/chat",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "license": "SEE LICENSE IN LICENSE",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",