@galaxycore/ai-chat 0.7.0 → 0.8.0

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/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  "use client";
3
- import { i as useAiChat, n as resolveLabels, r as zhCN, t as enUS } from "./labels-BM5o9a9K.js";
4
- import { n as ChatAppearanceProvider, r as Icon, t as ChatIconButton } from "./ui-primitives-BCbe9Ds8.js";
3
+ import { a as zhCN, i as resolveLabels, n as useChatBranches, o as useAiChat, r as enUS, t as projectChatBranches } from "./branches-MrtWHEkd.js";
4
+ import { n as ChatAppearanceProvider, r as Icon, t as ChatIconButton } from "./ui-primitives-BJFXxysT.js";
5
5
  import { i as safeUrl, n as asError, r as getMessageText, t as acceptsFile } from "./utils-DqBGBYA1.js";
6
6
  import { SpeechInputError, createBrowserSpeechAdapter, defaultSpeechInputLabels, useSpeechInput } from "./voice.js";
7
7
  import { MarkdownResponse } from "./markdown.js";
@@ -375,6 +375,76 @@ function fitTextarea(element) {
375
375
  element.style.height = `${Math.min(Math.max(element.getBoundingClientRect().height, element.scrollHeight), 180)}px`;
376
376
  }
377
377
  //#endregion
378
+ //#region src/branch-switcher.tsx
379
+ function formatPosition(template, index, count) {
380
+ return template.replace("{current}", String(index)).replace("{total}", String(count));
381
+ }
382
+ /** Compact `< 2 / 3 >` sibling-branch navigation control. */
383
+ function ChatBranchSwitcher({ branch, onSelect, disabled = false, labels: overrides, className }) {
384
+ if (branch.count <= 1) return null;
385
+ const labels = resolveLabels(overrides);
386
+ const status = formatPosition(labels.branchPosition, branch.index, branch.count);
387
+ return /* @__PURE__ */ jsxs("div", {
388
+ className: ["gc-chat__branch-switcher", className].filter(Boolean).join(" "),
389
+ role: "group",
390
+ "aria-label": status,
391
+ children: [
392
+ /* @__PURE__ */ jsx(ChatIconButton, {
393
+ size: "sm",
394
+ icon: "chevronLeft",
395
+ label: labels.previousBranch,
396
+ disabled: disabled || !branch.prevId,
397
+ onClick: () => {
398
+ if (branch.prevId) onSelect(branch.prevId);
399
+ }
400
+ }),
401
+ /* @__PURE__ */ jsxs("span", {
402
+ className: "gc-chat__branch-count",
403
+ "aria-live": "polite",
404
+ children: [
405
+ branch.index,
406
+ "/",
407
+ branch.count
408
+ ]
409
+ }),
410
+ /* @__PURE__ */ jsx(ChatIconButton, {
411
+ size: "sm",
412
+ icon: "chevron",
413
+ label: labels.nextBranch,
414
+ disabled: disabled || !branch.nextId,
415
+ onClick: () => {
416
+ if (branch.nextId) onSelect(branch.nextId);
417
+ }
418
+ })
419
+ ]
420
+ });
421
+ }
422
+ /** Cursor-style checkpoint status bar shown when reverted before a message, with one-click Restore. */
423
+ function ChatCheckpointBanner({ message, onRestore, restoreLabel, disabled = false, labels: overrides, className }) {
424
+ const labels = resolveLabels(overrides);
425
+ return /* @__PURE__ */ jsxs("div", {
426
+ className: ["gc-chat__checkpoint-banner", className].filter(Boolean).join(" "),
427
+ role: "status",
428
+ children: [
429
+ /* @__PURE__ */ jsx(Icon, {
430
+ name: "undo",
431
+ className: "gc-chat__checkpoint-icon"
432
+ }),
433
+ /* @__PURE__ */ jsx("span", {
434
+ className: "gc-chat__checkpoint-text",
435
+ children: message ?? labels.revertedBanner
436
+ }),
437
+ onRestore && /* @__PURE__ */ jsx("button", {
438
+ type: "button",
439
+ className: "gc-chat__checkpoint-restore",
440
+ disabled,
441
+ onClick: onRestore,
442
+ children: restoreLabel ?? labels.restoreBranch
443
+ })
444
+ ]
445
+ });
446
+ }
447
+ //#endregion
378
448
  //#region src/tool-activity.tsx
379
449
  function readableValue(value) {
380
450
  if (typeof value === "string") return value;
@@ -839,7 +909,7 @@ function StreamingText({ text, isStreaming, enabled = true, renderText }) {
839
909
  function ChatMessages(props) {
840
910
  return /* @__PURE__ */ jsx(Messages, { ...props }, props.chat.id);
841
911
  }
842
- function Messages({ chat, labels: labelOverrides, renderText, smoothStreaming = true, scrollRef, renderers, tools, toolPresentation, suggestedActions, suggestionsTitle, suggestionsDescription, renderMessageActions, renderMessageHeader, empty, className, requestOptions, onActionError, capabilities }) {
912
+ function Messages({ chat, labels: labelOverrides, renderText, smoothStreaming = true, scrollRef, renderers, tools, toolPresentation, suggestedActions, suggestionsTitle, suggestionsDescription, renderMessageActions, renderUserMessageFooter, branches, onSelectBranch, onRevertMessage, onBranchMessage, renderMessageHeader, empty, className, requestOptions, onActionError, capabilities }) {
843
913
  const labels = resolveLabels(labelOverrides);
844
914
  const { scroller, content, awayFromBottom, scrollToLatest, reveal, scrollEvents } = useMessageScroll(chat.messages);
845
915
  useImperativeHandle(scrollRef, () => ({ reveal }), [reveal]);
@@ -883,6 +953,44 @@ function Messages({ chat, labels: labelOverrides, renderText, smoothStreaming =
883
953
  setActionPending(false);
884
954
  }
885
955
  }
956
+ async function revertMessage(message) {
957
+ if (chat.isBusy || actionInFlight.current || !onRevertMessage) return;
958
+ setActionError(void 0);
959
+ try {
960
+ const pending = onRevertMessage(message);
961
+ if (pending && typeof pending === "object" && "then" in pending) {
962
+ actionInFlight.current = true;
963
+ setActionPending(true);
964
+ await pending;
965
+ }
966
+ } catch (cause) {
967
+ reportError(cause);
968
+ } finally {
969
+ if (actionInFlight.current) {
970
+ actionInFlight.current = false;
971
+ setActionPending(false);
972
+ }
973
+ }
974
+ }
975
+ async function branchMessage(message) {
976
+ if (chat.isBusy || actionInFlight.current || !onBranchMessage) return;
977
+ setActionError(void 0);
978
+ try {
979
+ const pending = onBranchMessage(message);
980
+ if (pending && typeof pending === "object" && "then" in pending) {
981
+ actionInFlight.current = true;
982
+ setActionPending(true);
983
+ await pending;
984
+ }
985
+ } catch (cause) {
986
+ reportError(cause);
987
+ } finally {
988
+ if (actionInFlight.current) {
989
+ actionInFlight.current = false;
990
+ setActionPending(false);
991
+ }
992
+ }
993
+ }
886
994
  function focusComposer() {
887
995
  content.current?.closest(".gc-chat")?.querySelector(".gc-chat__input")?.focus();
888
996
  }
@@ -1057,87 +1165,131 @@ function Messages({ chat, labels: labelOverrides, renderText, smoothStreaming =
1057
1165
  /* @__PURE__ */ jsx("p", { children: labels.emptyDescription })
1058
1166
  ]
1059
1167
  })),
1060
- visibleMessages.map((message) => /* @__PURE__ */ jsxs("article", {
1061
- className: `gc-chat__message gc-chat__message--${message.role}`,
1062
- "aria-label": message.role === "user" ? labels.user : labels.assistant,
1063
- children: [
1064
- message.role === "assistant" && (renderMessageHeader ? renderMessageHeader({
1065
- message,
1066
- isStreaming: chat.status === "streaming" && message === last,
1067
- chat
1068
- }) : /* @__PURE__ */ jsxs("div", {
1069
- className: "gc-chat__message-heading",
1070
- children: [/* @__PURE__ */ jsx("span", {
1071
- className: "gc-chat__assistant-mark",
1072
- children: /* @__PURE__ */ jsx(Icon, { name: "sparkle" })
1073
- }), /* @__PURE__ */ jsx("strong", {
1074
- className: "gc-chat__assistant-name",
1075
- children: labels.assistant
1076
- })]
1077
- })),
1078
- /* @__PURE__ */ jsx("div", {
1079
- className: "gc-chat__message-body",
1080
- children: message.parts.map((part, partIndex) => /* @__PURE__ */ jsx("div", {
1081
- className: "gc-chat__part",
1082
- "data-part": isToolUIPart(part) ? "tool" : part.type,
1083
- children: renderPart(part, message, chat.status === "streaming" && message === last)
1084
- }, isToolUIPart(part) ? `tool-${part.toolCallId}` : `${part.type}-${partIndex}`))
1085
- }),
1086
- message === last && message.role === "assistant" && !chat.isBusy && /* @__PURE__ */ jsxs(Fragment, { children: [actions.length > 0 && /* @__PURE__ */ jsxs("section", {
1087
- className: "gc-chat__suggestions",
1088
- "aria-label": labels.suggestions ?? "Suggested next steps",
1089
- "aria-busy": pendingSuggestion !== void 0,
1090
- children: [/* @__PURE__ */ jsxs("div", {
1091
- className: "gc-chat__suggestions-heading",
1092
- children: [/* @__PURE__ */ jsx("h3", { children: suggestionsTitle ?? labels.suggestions ?? "Suggested next steps" }), suggestionsDescription != null && /* @__PURE__ */ jsx("span", {
1093
- className: "gc-chat__suggestions-reason",
1094
- children: suggestionsDescription
1168
+ visibleMessages.map((message) => {
1169
+ const branch = branches?.[message.id];
1170
+ const hasBranchSwitcher = Boolean(branch && branch.count > 1 && onSelectBranch);
1171
+ return /* @__PURE__ */ jsxs("article", {
1172
+ className: `gc-chat__message gc-chat__message--${message.role}`,
1173
+ "aria-label": message.role === "user" ? labels.user : labels.assistant,
1174
+ children: [
1175
+ message.role === "assistant" && (renderMessageHeader ? renderMessageHeader({
1176
+ message,
1177
+ isStreaming: chat.status === "streaming" && message === last,
1178
+ chat
1179
+ }) : /* @__PURE__ */ jsxs("div", {
1180
+ className: "gc-chat__message-heading",
1181
+ children: [/* @__PURE__ */ jsx("span", {
1182
+ className: "gc-chat__assistant-mark",
1183
+ children: /* @__PURE__ */ jsx(Icon, { name: "sparkle" })
1184
+ }), /* @__PURE__ */ jsx("strong", {
1185
+ className: "gc-chat__assistant-name",
1186
+ children: labels.assistant
1187
+ })]
1188
+ })),
1189
+ /* @__PURE__ */ jsx("div", {
1190
+ className: "gc-chat__message-body",
1191
+ children: message.parts.map((part, partIndex) => /* @__PURE__ */ jsx("div", {
1192
+ className: "gc-chat__part",
1193
+ "data-part": isToolUIPart(part) ? "tool" : part.type,
1194
+ children: renderPart(part, message, chat.status === "streaming" && message === last)
1195
+ }, isToolUIPart(part) ? `tool-${part.toolCallId}` : `${part.type}-${partIndex}`))
1196
+ }),
1197
+ message.role === "user" && (renderUserMessageFooter ? renderUserMessageFooter({
1198
+ message,
1199
+ chat,
1200
+ focusComposer
1201
+ }) : (hasBranchSwitcher || onRevertMessage) && /* @__PURE__ */ jsx("div", {
1202
+ className: "gc-chat__message-footer gc-chat__message-footer--user",
1203
+ children: /* @__PURE__ */ jsxs("div", {
1204
+ className: "gc-chat__message-actions",
1205
+ role: "group",
1206
+ "aria-label": labels.user,
1207
+ children: [hasBranchSwitcher && /* @__PURE__ */ jsx(ChatBranchSwitcher, {
1208
+ branch,
1209
+ onSelect: onSelectBranch,
1210
+ disabled: chat.isBusy || actionPending,
1211
+ labels: labelOverrides
1212
+ }), onRevertMessage && /* @__PURE__ */ jsx(ChatIconButton, {
1213
+ size: "sm",
1214
+ label: labels.revert,
1215
+ icon: "undo",
1216
+ disabled: chat.isBusy || actionPending,
1217
+ onClick: () => void revertMessage(message)
1218
+ })]
1219
+ })
1220
+ })),
1221
+ message === last && message.role === "assistant" && !chat.isBusy && /* @__PURE__ */ jsxs(Fragment, { children: [actions.length > 0 && /* @__PURE__ */ jsxs("section", {
1222
+ className: "gc-chat__suggestions",
1223
+ "aria-label": labels.suggestions ?? "Suggested next steps",
1224
+ "aria-busy": pendingSuggestion !== void 0,
1225
+ children: [/* @__PURE__ */ jsxs("div", {
1226
+ className: "gc-chat__suggestions-heading",
1227
+ children: [/* @__PURE__ */ jsx("h3", { children: suggestionsTitle ?? labels.suggestions ?? "Suggested next steps" }), suggestionsDescription != null && /* @__PURE__ */ jsx("span", {
1228
+ className: "gc-chat__suggestions-reason",
1229
+ children: suggestionsDescription
1230
+ })]
1231
+ }), /* @__PURE__ */ jsx("div", {
1232
+ className: "gc-chat__suggestions-actions",
1233
+ children: actions.map((action, index) => /* @__PURE__ */ jsxs("button", {
1234
+ type: "button",
1235
+ className: "gc-chat__suggestion",
1236
+ "data-emphasis": action.emphasis === "recommended" && index !== recommendedIndex ? "secondary" : action.emphasis ?? "secondary",
1237
+ "data-pending": pendingSuggestion === action.id || void 0,
1238
+ disabled: actionPending || action.disabled,
1239
+ onClick: () => void selectSuggestion(action, message),
1240
+ children: [
1241
+ action.icon,
1242
+ pendingSuggestion === action.id ? action.pendingLabel ?? action.label : action.label,
1243
+ action.trailingIcon
1244
+ ]
1245
+ }, action.id))
1095
1246
  })]
1096
- }), /* @__PURE__ */ jsx("div", {
1097
- className: "gc-chat__suggestions-actions",
1098
- children: actions.map((action, index) => /* @__PURE__ */ jsxs("button", {
1099
- type: "button",
1100
- className: "gc-chat__suggestion",
1101
- "data-emphasis": action.emphasis === "recommended" && index !== recommendedIndex ? "secondary" : action.emphasis ?? "secondary",
1102
- "data-pending": pendingSuggestion === action.id || void 0,
1103
- disabled: actionPending || action.disabled,
1104
- onClick: () => void selectSuggestion(action, message),
1247
+ }), renderMessageActions?.({
1248
+ message,
1249
+ chat,
1250
+ focusComposer
1251
+ })] }),
1252
+ message.role === "assistant" && /* @__PURE__ */ jsx("div", {
1253
+ className: "gc-chat__message-footer",
1254
+ children: showThinking && message === last ? thinking : /* @__PURE__ */ jsxs("div", {
1255
+ className: "gc-chat__message-actions",
1256
+ role: "group",
1257
+ "aria-label": labels.assistant,
1105
1258
  children: [
1106
- action.icon,
1107
- pendingSuggestion === action.id ? action.pendingLabel ?? action.label : action.label,
1108
- action.trailingIcon
1259
+ hasBranchSwitcher && /* @__PURE__ */ jsx(ChatBranchSwitcher, {
1260
+ branch,
1261
+ onSelect: onSelectBranch,
1262
+ disabled: chat.isBusy || actionPending,
1263
+ labels: labelOverrides
1264
+ }),
1265
+ getMessageText(message) && /* @__PURE__ */ jsx(ChatIconButton, {
1266
+ size: "sm",
1267
+ label: copiedId === message.id ? labels.copied : labels.copy,
1268
+ icon: copiedId === message.id ? "check" : "copy",
1269
+ "data-copied": copiedId === message.id || void 0,
1270
+ disabled: chat.isBusy && message === last,
1271
+ onClick: () => void copy(message)
1272
+ }),
1273
+ onBranchMessage && /* @__PURE__ */ jsx(ChatIconButton, {
1274
+ size: "sm",
1275
+ label: labels.branch,
1276
+ icon: "branch",
1277
+ disabled: chat.isBusy || actionPending,
1278
+ onClick: () => void branchMessage(message)
1279
+ }),
1280
+ message === last && capabilities?.regenerate !== false && /* @__PURE__ */ jsx(ChatIconButton, {
1281
+ size: "sm",
1282
+ label: labels.regenerate,
1283
+ icon: "refresh",
1284
+ disabled: chat.isBusy || actionPending,
1285
+ onClick: () => void regenerate(message)
1286
+ })
1109
1287
  ]
1110
- }, action.id))
1111
- })]
1112
- }), renderMessageActions?.({
1113
- message,
1114
- chat,
1115
- focusComposer
1116
- })] }),
1117
- message.role === "assistant" && /* @__PURE__ */ jsx("div", {
1118
- className: "gc-chat__message-footer",
1119
- children: showThinking && message === last ? thinking : /* @__PURE__ */ jsxs("div", {
1120
- className: "gc-chat__message-actions",
1121
- role: "group",
1122
- "aria-label": labels.assistant,
1123
- children: [getMessageText(message) && /* @__PURE__ */ jsx(ChatIconButton, {
1124
- size: "sm",
1125
- label: copiedId === message.id ? labels.copied : labels.copy,
1126
- icon: copiedId === message.id ? "check" : "copy",
1127
- "data-copied": copiedId === message.id || void 0,
1128
- disabled: chat.isBusy && message === last,
1129
- onClick: () => void copy(message)
1130
- }), message === last && capabilities?.regenerate !== false && /* @__PURE__ */ jsx(ChatIconButton, {
1131
- size: "sm",
1132
- label: labels.regenerate,
1133
- icon: "refresh",
1134
- disabled: chat.isBusy || actionPending,
1135
- onClick: () => void regenerate(message)
1136
- })]
1288
+ })
1137
1289
  })
1138
- })
1139
- ]
1140
- }, message.id)),
1290
+ ]
1291
+ }, message.id);
1292
+ }),
1141
1293
  showThinking && last?.role !== "assistant" && /* @__PURE__ */ jsx("article", {
1142
1294
  className: "gc-chat__message gc-chat__message--assistant",
1143
1295
  "aria-label": labels.assistant,
@@ -1178,7 +1330,7 @@ function Messages({ chat, labels: labelOverrides, renderText, smoothStreaming =
1178
1330
  function ChatPanel(props) {
1179
1331
  return /* @__PURE__ */ jsx(Panel, { ...props }, props.chat.id);
1180
1332
  }
1181
- function Panel({ chat, labels: labelOverrides, title, subtitle, headerIcon, header, headerActions, footer, composer, composerProps, theme = "auto", variant = "panel", density = "comfortable", capabilities, className, renderText, smoothStreaming, scrollRef, renderers, tools, toolPresentation, suggestedActions, suggestionsTitle, suggestionsDescription, renderMessageActions, renderMessageHeader, empty, requestOptions, onActionError, ...elementProps }) {
1333
+ function Panel({ chat, labels: labelOverrides, title, subtitle, headerIcon, header, headerActions, footer, composer, composerProps, theme = "auto", variant = "panel", density = "comfortable", capabilities, className, renderText, smoothStreaming, scrollRef, renderers, tools, toolPresentation, suggestedActions, suggestionsTitle, suggestionsDescription, renderMessageActions, renderUserMessageFooter, branches, onSelectBranch, onRevertMessage, onBranchMessage, renderMessageHeader, empty, requestOptions, onActionError, ...elementProps }) {
1182
1334
  const labels = resolveLabels(labelOverrides);
1183
1335
  const [retrying, setRetrying] = useState(false);
1184
1336
  const [retryError, setRetryError] = useState();
@@ -1249,6 +1401,11 @@ function Panel({ chat, labels: labelOverrides, title, subtitle, headerIcon, head
1249
1401
  suggestionsTitle,
1250
1402
  suggestionsDescription,
1251
1403
  renderMessageActions,
1404
+ renderUserMessageFooter,
1405
+ branches,
1406
+ onSelectBranch,
1407
+ onRevertMessage,
1408
+ onBranchMessage,
1252
1409
  renderMessageHeader,
1253
1410
  empty,
1254
1411
  requestOptions,
@@ -1477,6 +1634,6 @@ function ChatHistoryMenu({ conversations, currentId, onSelect, onNewChat, disabl
1477
1634
  });
1478
1635
  }
1479
1636
  //#endregion
1480
- export { AiChat, ChatAppearanceProvider, ChatComposer, ChatHistoryMenu, Icon as ChatIcon, ChatIconButton, ChatMessages, ChatPanel, ChatTargetingOverlay, ContextTray, SpeechInputError, createBrowserSpeechAdapter, createTextSelectionAdapter, defaultSpeechInputLabels, enUS, enUSTargetingLabels, getMessageText, toChatContextItem, useAiChat, useChatContext, useChatTargeting, useSpeechInput, zhCN, zhCNTargetingLabels };
1637
+ export { AiChat, ChatAppearanceProvider, ChatBranchSwitcher, ChatCheckpointBanner, ChatComposer, ChatHistoryMenu, Icon as ChatIcon, ChatIconButton, ChatMessages, ChatPanel, ChatTargetingOverlay, ContextTray, SpeechInputError, createBrowserSpeechAdapter, createTextSelectionAdapter, defaultSpeechInputLabels, enUS, enUSTargetingLabels, getMessageText, projectChatBranches, toChatContextItem, useAiChat, useChatBranches, useChatContext, useChatTargeting, useSpeechInput, zhCN, zhCNTargetingLabels };
1481
1638
 
1482
1639
  //# sourceMappingURL=index.js.map