@widgetic/chat 0.1.14 → 0.1.16

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.
@@ -98,6 +98,8 @@
98
98
  versionLabelByCommitSha?: Record<string, string>;
99
99
  /** User prompt text → "Version N", when the turn has no commit line. */
100
100
  versionLabelByPrompt?: Record<string, string>;
101
+ /** Saved versions, so a prompt that produced two commits can show two turns. */
102
+ versionTimeline?: Array<{ version: number; sha: string; prompt: string }>;
101
103
  /** Version selected in the dropdown. You-are-here follows this number. */
102
104
  viewingVersion?: number | null;
103
105
  /** Host is running widget codegen — send button becomes stop. */
@@ -140,7 +142,7 @@
140
142
  updateLastUserMessageAttachments: (attachments: Array<{ id?: string; url: string; file_type?: string; file_name?: string | null }>) => void;
141
143
  }
142
144
 
143
- let { context, config = {}, class: className = '', style = '', isCompleted = false, onStatsUpdate, onChatReady, onCompletionToggle, onSendMessage, onLoadConversations, onLoadMessages, onCreateConversation, onSaveMessage, onRestoreCheckpointBackend, onTakeScreenshot, onConversationChange, onDraftChange, onRetryMessage, onUploadChatFile, showHeader = true, headerTitle = '', showDescriptionHeader = true, showLogs = true, resizeExpandsContainer = false, onMessagesResize, widgetHeadCommitSha = null, versionLabelByCommitSha = {}, versionLabelByPrompt = {}, viewingVersion = null, isGenerating = false, onStopGeneration }: Props = $props();
145
+ let { context, config = {}, class: className = '', style = '', isCompleted = false, onStatsUpdate, onChatReady, onCompletionToggle, onSendMessage, onLoadConversations, onLoadMessages, onCreateConversation, onSaveMessage, onRestoreCheckpointBackend, onTakeScreenshot, onConversationChange, onDraftChange, onRetryMessage, onUploadChatFile, showHeader = true, headerTitle = '', showDescriptionHeader = true, showLogs = true, resizeExpandsContainer = false, onMessagesResize, widgetHeadCommitSha = null, versionLabelByCommitSha = {}, versionLabelByPrompt = {}, versionTimeline = [], viewingVersion = null, isGenerating = false, onStopGeneration }: Props = $props();
144
146
 
145
147
  function notifyDraftChange() {
146
148
  onDraftChange?.();
@@ -1623,6 +1625,7 @@
1623
1625
  widgetHeadCommitSha={widgetHeadCommitSha}
1624
1626
  versionLabelByCommitSha={versionLabelByCommitSha}
1625
1627
  versionLabelByPrompt={promptVersionLabels}
1628
+ {versionTimeline}
1626
1629
  viewingVersion={selectedViewingVersion}
1627
1630
  showCommitMeta={showLogs}
1628
1631
  isGenerating={isGenerating || isLoading}
@@ -143,6 +143,12 @@ interface Props {
143
143
  versionLabelByCommitSha?: Record<string, string>;
144
144
  /** User prompt text → "Version N", when the turn has no commit line. */
145
145
  versionLabelByPrompt?: Record<string, string>;
146
+ /** Saved versions, so a prompt that produced two commits can show two turns. */
147
+ versionTimeline?: Array<{
148
+ version: number;
149
+ sha: string;
150
+ prompt: string;
151
+ }>;
146
152
  /** Version selected in the dropdown. You-are-here follows this number. */
147
153
  viewingVersion?: number | null;
148
154
  /** Host is running widget codegen — send button becomes stop. */
@@ -41,6 +41,12 @@
41
41
  versionLabelByCommitSha?: Record<string, string>;
42
42
  /** User prompt text → "Version N", for turns that never saved a commit line. */
43
43
  versionLabelByPrompt?: Record<string, string>;
44
+ /**
45
+ * Every saved version for this widget. When one prompt produced two versions
46
+ * but the chat stored only one user message, the missing older version is
47
+ * shown as its own turn so "You are here" can land on it.
48
+ */
49
+ versionTimeline?: Array<{ version: number; sha: string; prompt: string }>;
44
50
  /** Dropdown selection. The badge follows this version, not only a commit sha. */
45
51
  viewingVersion?: number | null;
46
52
  /** When false, hide commit hashes next to the version label (user mode). */
@@ -68,6 +74,7 @@
68
74
  widgetHeadCommitSha = null,
69
75
  versionLabelByCommitSha = {},
70
76
  versionLabelByPrompt = {},
77
+ versionTimeline = [],
71
78
  viewingVersion = null,
72
79
  showCommitMeta = true,
73
80
  isGenerating = false,
@@ -207,6 +214,101 @@
207
214
  return turn.replies.some(isCodegenFailure);
208
215
  }
209
216
 
217
+ function commitsMatch(a: string, b: string): boolean {
218
+ const left = a.toLowerCase();
219
+ const right = b.toLowerCase();
220
+ return left === right || left.startsWith(right.slice(0, 7)) || right.startsWith(left.slice(0, 7));
221
+ }
222
+
223
+ /**
224
+ * A retry that reused one user bubble leaves two commits on that turn, so
225
+ * Version 7 and Version 8 share a message and "You are here" cannot land on
226
+ * the older one. Split at each new commit. Later copies keep the prompt text
227
+ * and drop attachments — the sketch stays on the turn that originally had it.
228
+ */
229
+ function splitTurnsByCommit(turns: ChatTurn[]): ChatTurn[] {
230
+ const split: ChatTurn[] = [];
231
+ for (const turn of turns) {
232
+ if (!turn.user) {
233
+ split.push(turn);
234
+ continue;
235
+ }
236
+ let segment: ChatTurn = { user: turn.user, replies: [] };
237
+ let segmentSha: string | null = null;
238
+ for (const reply of turn.replies) {
239
+ const sha = messageHasCommit(reply) ? extractCommitFromContent(reply.messageContent || '') : null;
240
+ if (sha && segmentSha && !commitsMatch(sha, segmentSha)) {
241
+ split.push(segment);
242
+ segment = {
243
+ user: { ...turn.user, id: `retry-fork-${sha}`, attachments: [] },
244
+ replies: [],
245
+ };
246
+ }
247
+ if (sha) segmentSha = sha;
248
+ segment.replies.push(reply);
249
+ }
250
+ split.push(segment);
251
+ }
252
+ return split;
253
+ }
254
+
255
+ /**
256
+ * One user row can carry only the newest commit after a retry. Older versions
257
+ * of that same prompt still exist in history. Give each of them a turn so
258
+ * selecting Version 7 has a place to show "You are here".
259
+ */
260
+ function expandTurnsForSharedPrompts(turns: ChatTurn[]): ChatTurn[] {
261
+ if (!versionTimeline.length) return turns;
262
+ const represented = new Set<string>();
263
+ for (const turn of turns) {
264
+ const short = getTurnOwnCommitShort(turn);
265
+ if (short) represented.add(short.slice(0, 7).toLowerCase());
266
+ }
267
+ const expanded: ChatTurn[] = [];
268
+ for (const turn of turns) {
269
+ const key = versionPromptKey(turn.user?.messageContent);
270
+ if (!turn.user || !key) {
271
+ expanded.push(turn);
272
+ continue;
273
+ }
274
+ const matches = versionTimeline
275
+ .filter((entry) => versionPromptKey(entry.prompt) === key && entry.sha)
276
+ .sort((a, b) => a.version - b.version);
277
+ const latest = matches[matches.length - 1];
278
+ const older = matches.filter((entry) => {
279
+ const sha7 = entry.sha.slice(0, 7).toLowerCase();
280
+ if (latest && sha7 === latest.sha.slice(0, 7).toLowerCase()) return false;
281
+ return !represented.has(sha7);
282
+ });
283
+ for (const entry of older) {
284
+ const sha7 = entry.sha.slice(0, 7).toLowerCase();
285
+ represented.add(sha7);
286
+ const sha8 = entry.sha.slice(0, 8);
287
+ expanded.push({
288
+ user: { ...turn.user, id: `version-fork-${sha7}`, attachments: [] },
289
+ replies: [{
290
+ ...turn.user,
291
+ id: `version-fork-reply-${sha7}`,
292
+ messageType: 'assistant',
293
+ messageContent: `Code generation completed! Commit: ${sha8}`,
294
+ attachments: [],
295
+ }],
296
+ });
297
+ }
298
+ expanded.push(turn);
299
+ }
300
+ return expanded;
301
+ }
302
+
303
+ function visibleUserAttachments(turn: ChatTurn, turnIndex: number) {
304
+ const attachments = turn.user?.attachments || [];
305
+ const hasEarlierUser = chatTurns.slice(0, turnIndex).some((candidate) => candidate.user);
306
+ if (!hasEarlierUser) return attachments;
307
+ // The opening sketch stays on the first turn. Later prompts must not
308
+ // show it again — the model still receives it as standing context.
309
+ return attachments.filter((attachment) => !/design-screenshot/i.test(attachment.file_name || ''));
310
+ }
311
+
210
312
  function rebalancePreviewRecompiledAcrossTurns(turns: ChatTurn[]): ChatTurn[] {
211
313
  const result = turns.map((t) => ({ user: t.user, replies: [...t.replies] }));
212
314
 
@@ -323,6 +425,7 @@
323
425
  }
324
426
 
325
427
  function canRestoreToMessage(message: ChatMessage): boolean {
428
+ if (message.id?.startsWith('version-fork-')) return false;
326
429
  if (isRestoreForkContent(message.messageContent)) return false;
327
430
  if (isHeadCommitReply(message)) return false;
328
431
  if (!message.messageContent) return false;
@@ -576,7 +679,10 @@
576
679
  }
577
680
  }
578
681
  if (current) turns.push(current);
579
- chatTurns = rebalancePreviewRecompiledAcrossTurns(turns);
682
+ void versionTimeline;
683
+ chatTurns = expandTurnsForSharedPrompts(
684
+ splitTurnsByCommit(rebalancePreviewRecompiledAcrossTurns(turns)),
685
+ );
580
686
  });
581
687
 
582
688
  /** Version dropdown changes which commit is "here" — bring that turn into view. */
@@ -598,20 +704,32 @@
598
704
  );
599
705
  }
600
706
 
601
- function turnNeedsRetry(turn: ChatTurn): boolean {
707
+ function promptWasRetriedLater(turn: ChatTurn, turnIndex: number): boolean {
708
+ const key = versionPromptKey(turn.user?.messageContent);
709
+ if (!key) return false;
710
+ for (let index = turnIndex + 1; index < chatTurns.length; index++) {
711
+ if (versionPromptKey(chatTurns[index]?.user?.messageContent) === key) return true;
712
+ }
713
+ return false;
714
+ }
715
+
716
+ function isPlanLimitMessage(message: string | null | undefined): boolean {
717
+ if (!message) return false;
718
+ return /credit balance|free trial|not available in your plan|not available on the free plan|upgrade your plan|upgrade to a solo/i.test(message);
719
+ }
720
+
721
+ function openUpgradePlans() {
722
+ window.location.assign('/workspaces/personal/settings/billing');
723
+ }
724
+
725
+ function turnNeedsRetry(turn: ChatTurn, turnIndex: number): boolean {
602
726
  // Never show Retry while a send/codegen is still running.
603
727
  if (isLoading || isGenerating) return false;
604
- if (turnHasCodegenFailure(turn)) return true;
605
728
  const userMsg = turn.user;
606
- if (!userMsg?.messageContent) return false;
607
- const noAttachments = !userMsg.attachments || userMsg.attachments.length === 0;
608
- if (noAttachments && messageImpliesAttachedFile(userMsg.messageContent) && !getTurnCommitReply(turn)) {
609
- return true;
610
- }
611
- // Stopped / incomplete last prompt only — not mid-flight before the first assistant reply.
612
- // Incomplete without failure/stop is handled by the host via lastGenerationFailed /
613
- // "Generation stopped by user" assistant text (turnHasCodegenFailure).
614
- return false;
729
+ if (!userMsg?.messageContent?.trim()) return false;
730
+ // The earlier copy of a prompt that was already run again stays without Retry.
731
+ if (promptWasRetriedLater(turn, turnIndex)) return false;
732
+ return true;
615
733
  }
616
734
 
617
735
  let visibleMessages = $derived(
@@ -1034,11 +1152,12 @@
1034
1152
  >
1035
1153
  {#if turn.user}
1036
1154
  {@const userMsg = turn.user}
1155
+ {@const shownAttachments = visibleUserAttachments(turn, turnIndex)}
1037
1156
  {@const commitReply = getTurnHeadOrCommitReply(turn)}
1038
1157
  <div class="message-container wc-chat-turn-user w-full flex flex-col items-stretch"
1039
1158
  class:opacity-75={userMsg._optimistic}
1040
1159
  class:opacity-50={userMsg._failed}
1041
- class:has-attachments={userMsg.attachments && userMsg.attachments.length > 0}
1160
+ class:has-attachments={shownAttachments.length > 0}
1042
1161
  >
1043
1162
  <div class="wc-user-bubble-row flex w-full items-start gap-2">
1044
1163
  {#if isViewingTurn(turn)}
@@ -1052,9 +1171,9 @@
1052
1171
  {/if}
1053
1172
  <div
1054
1173
  class="message-bubble relative ml-auto w-fit max-w-[80%] rounded-lg shadow-sm text-sm leading-relaxed select-text"
1055
- class:p-3={!userMsg.attachments || userMsg.attachments.length === 0}
1056
- class:p-0={userMsg.attachments && userMsg.attachments.length > 0}
1057
- class:bubble-fit-content={userMsg.attachments && userMsg.attachments.length > 0}
1174
+ class:p-3={shownAttachments.length === 0}
1175
+ class:p-0={shownAttachments.length > 0}
1176
+ class:bubble-fit-content={shownAttachments.length > 0}
1058
1177
  style="background-color: {isCompleted ? MESSAGE_COLORS.USER_COMPLETED : MESSAGE_COLORS.USER_ACTIVE}; color: {MESSAGE_COLORS.USER_TEXT};"
1059
1178
  use:bubbleScrollPadding
1060
1179
  >
@@ -1079,9 +1198,9 @@
1079
1198
  <p class="whitespace-pre-wrap p-3">{userMsg.messageContent}</p>
1080
1199
  {/if}
1081
1200
  {/if}
1082
- {#if userMsg.attachments && userMsg.attachments.length > 0}
1201
+ {#if shownAttachments.length > 0}
1083
1202
  <div class="message-attachments-container">
1084
- <AttachmentDisplay attachments={userMsg.attachments} variant="message" showPreview={true} />
1203
+ <AttachmentDisplay attachments={shownAttachments} variant="message" showPreview={true} />
1085
1204
  </div>
1086
1205
  {:else if userMsg.messageContent && messageImpliesAttachedFile(userMsg.messageContent)}
1087
1206
  <div class="wc-missing-attachment-placeholder message-attachments-container flex flex-col items-center gap-1 px-3 pb-3">
@@ -1096,12 +1215,12 @@
1096
1215
  {/if}
1097
1216
  </div>
1098
1217
  </div>
1099
- {#if turnNeedsRetry(turn) && !isLoading}
1218
+ {#if turnNeedsRetry(turn, turnIndex) && !isLoading}
1100
1219
  <div class="retry-codegen-row flex justify-end mt-1">
1101
1220
  <button
1102
1221
  class="retry-codegen-btn"
1103
1222
  onclick={() => handleRetryTurn(turn, turnIndex)}
1104
- title="Retry this prompt (same as Retry on the error below)"
1223
+ title="Run this prompt again as its own message. The sketch stays with the model, not on the bubble."
1105
1224
  >
1106
1225
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
1107
1226
  <path d="M21 12a9 9 0 1 1-2.64-6.36"/>
@@ -1133,7 +1252,7 @@
1133
1252
  </span>
1134
1253
  {/if}
1135
1254
  </div>
1136
- {#if !isLoading && ((commitReply && (canRestoreToMessage(commitReply) || isHeadCommitReply(commitReply))) || canRestoreLegacyUserTurn(turn) || canRestoreFailedLastTurn(turn))}
1255
+ {#if !turn.user?.id?.startsWith('version-fork-') && !isLoading && ((commitReply && (canRestoreToMessage(commitReply) || isHeadCommitReply(commitReply))) || canRestoreLegacyUserTurn(turn) || canRestoreFailedLastTurn(turn))}
1137
1256
  {@const restoreIsCurrentHead = !!(commitReply && isHeadCommitReply(commitReply))}
1138
1257
  <button
1139
1258
  class="restore-checkpoint-btn shrink-0"
@@ -1210,8 +1329,17 @@
1210
1329
  </div>
1211
1330
  {/if}
1212
1331
 
1213
- {#if isCodegenFailure(reply) && isLastCodegenFailureInTurn(turn, reply) && getRetryPromptForTurn(turn, turnIndex) && !isLoading}
1332
+ {#if isCodegenFailure(reply) && isLastCodegenFailureInTurn(turn, reply) && turnNeedsRetry(turn, turnIndex)}
1214
1333
  <div class="retry-codegen-row flex justify-start mt-1">
1334
+ {#if isPlanLimitMessage(reply.messageContent)}
1335
+ <button
1336
+ class="retry-codegen-btn"
1337
+ onclick={openUpgradePlans}
1338
+ title="Open plans and billing"
1339
+ >
1340
+ Upgrade
1341
+ </button>
1342
+ {:else}
1215
1343
  <button
1216
1344
  class="retry-codegen-btn"
1217
1345
  onclick={() => handleRetryTurn(turn, turnIndex)}
@@ -1223,6 +1351,7 @@
1223
1351
  </svg>
1224
1352
  Retry
1225
1353
  </button>
1354
+ {/if}
1226
1355
  </div>
1227
1356
  {/if}
1228
1357
  </div>
@@ -32,6 +32,16 @@ interface Props {
32
32
  versionLabelByCommitSha?: Record<string, string>;
33
33
  /** User prompt text → "Version N", for turns that never saved a commit line. */
34
34
  versionLabelByPrompt?: Record<string, string>;
35
+ /**
36
+ * Every saved version for this widget. When one prompt produced two versions
37
+ * but the chat stored only one user message, the missing older version is
38
+ * shown as its own turn so "You are here" can land on it.
39
+ */
40
+ versionTimeline?: Array<{
41
+ version: number;
42
+ sha: string;
43
+ prompt: string;
44
+ }>;
35
45
  /** Dropdown selection. The badge follows this version, not only a commit sha. */
36
46
  viewingVersion?: number | null;
37
47
  /** When false, hide commit hashes next to the version label (user mode). */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@widgetic/chat",
3
- "version": "0.1.14",
3
+ "version": "0.1.16",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",