@galaxycore/ai-chat 0.9.0 → 0.10.1

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 { a as filterSkills, c as slugifySkillName, d as resolveLabels, f as zhCN, i as filterMentions, l as useComposerTrigger, n as useChatBranches, o as parseSkillMarkdown, p as useAiChat, r as detectComposerTrigger, s as replaceComposerTrigger, t as projectChatBranches, u as enUS } from "./branches-M2XjURF5.js";
4
- import { n as ChatAppearanceProvider, r as Icon, t as ChatIconButton } from "./ui-primitives-BzbdVU2_.js";
3
+ import { _ as useAiChat, a as resolveChatModelSelection, c as filterMentions, d as replaceComposerTrigger, f as slugifySkillName, g as zhCN, h as resolveLabels, i as isChatReasoningEffort, l as filterSkills, m as enUS, n as useChatBranches, o as selectChatModel, p as useComposerTrigger, r as CHAT_REASONING_EFFORTS, s as detectComposerTrigger, t as projectChatBranches, u as parseSkillMarkdown } from "./branches-CRxWPjfD.js";
4
+ import { n as ChatAppearanceProvider, r as Icon, t as ChatIconButton } from "./ui-primitives-XhFvDm8o.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";
@@ -329,12 +329,204 @@ function ChatPlusMenu({ open, onClose, actions, labels: overrides, className = "
329
329
  });
330
330
  }
331
331
  //#endregion
332
+ //#region src/model-picker-control.tsx
333
+ function effortLabel(effort, labels) {
334
+ switch (effort) {
335
+ case "none": return labels.reasoningNone;
336
+ case "low": return labels.reasoningLow;
337
+ case "medium": return labels.reasoningMedium;
338
+ case "high": return labels.reasoningHigh;
339
+ case "xhigh": return labels.reasoningXhigh;
340
+ }
341
+ }
342
+ /**
343
+ * Compact composer selector for choosing the active AI model and reasoning effort.
344
+ * Trigger stays visually quiet (`Model Effort ⌄`) and opens an anchored popover above the composer.
345
+ */
346
+ function ChatModelPicker({ models, value, onChange, disabled = false, labels: overrides, className = "" }) {
347
+ const labels = resolveLabels(overrides);
348
+ const [open, setOpen] = useState(false);
349
+ const rootRef = useRef(null);
350
+ const triggerRef = useRef(null);
351
+ const dialogId = useId();
352
+ const resolved = resolveChatModelSelection(models, value);
353
+ const activeModel = resolved.model;
354
+ const activeEffort = resolved.reasoningEffort;
355
+ const efforts = resolved.supportedEfforts;
356
+ useEffect(() => {
357
+ if (!open) return;
358
+ function onPointerDown(event) {
359
+ if (rootRef.current && !rootRef.current.contains(event.target)) setOpen(false);
360
+ }
361
+ function onKeyDown(event) {
362
+ if (event.key === "Escape") {
363
+ event.preventDefault();
364
+ setOpen(false);
365
+ triggerRef.current?.focus();
366
+ }
367
+ }
368
+ document.addEventListener("pointerdown", onPointerDown);
369
+ document.addEventListener("keydown", onKeyDown);
370
+ return () => {
371
+ document.removeEventListener("pointerdown", onPointerDown);
372
+ document.removeEventListener("keydown", onKeyDown);
373
+ };
374
+ }, [open]);
375
+ useEffect(() => {
376
+ if (disabled && open) setOpen(false);
377
+ }, [disabled, open]);
378
+ if (!activeModel) return null;
379
+ const canInteract = !disabled && (models.length > 1 || efforts.length > 0);
380
+ const effortText = activeEffort ? effortLabel(activeEffort, labels) : void 0;
381
+ const triggerAccessibleName = effortText ? `${labels.selectModel}: ${activeModel.label} (${effortText})` : `${labels.selectModel}: ${activeModel.label}`;
382
+ function handleSelectModel(option) {
383
+ if (option.disabled) return;
384
+ onChange(selectChatModel(option, activeEffort));
385
+ if (!option.reasoningEfforts?.length) {
386
+ setOpen(false);
387
+ triggerRef.current?.focus();
388
+ }
389
+ }
390
+ function handleSelectEffort(effort) {
391
+ if (!activeModel) return;
392
+ onChange({
393
+ modelId: activeModel.id,
394
+ ...effort !== void 0 ? { reasoningEffort: effort } : {}
395
+ });
396
+ }
397
+ return /* @__PURE__ */ jsxs("div", {
398
+ ref: rootRef,
399
+ className: `gc-chat__model-picker ${className}`.trim(),
400
+ "data-open": open || void 0,
401
+ children: [/* @__PURE__ */ jsxs("button", {
402
+ ref: triggerRef,
403
+ type: "button",
404
+ className: "gc-chat__model-trigger",
405
+ disabled: disabled || !canInteract,
406
+ "aria-haspopup": "dialog",
407
+ "aria-expanded": open,
408
+ "aria-controls": open ? dialogId : void 0,
409
+ "aria-label": triggerAccessibleName,
410
+ onClick: () => setOpen((current) => !current),
411
+ children: [
412
+ /* @__PURE__ */ jsx("span", {
413
+ className: "gc-chat__model-trigger-name",
414
+ children: activeModel.label
415
+ }),
416
+ effortText ? /* @__PURE__ */ jsx("span", {
417
+ className: "gc-chat__model-trigger-effort",
418
+ children: effortText
419
+ }) : null,
420
+ canInteract ? /* @__PURE__ */ jsx("span", {
421
+ className: "gc-chat__model-trigger-chevron",
422
+ "aria-hidden": "true",
423
+ children: /* @__PURE__ */ jsx(Icon, {
424
+ name: "chevronDown",
425
+ width: 14,
426
+ height: 14
427
+ })
428
+ }) : null
429
+ ]
430
+ }), open ? /* @__PURE__ */ jsxs("div", {
431
+ id: dialogId,
432
+ role: "dialog",
433
+ "aria-label": labels.selectModel,
434
+ className: "gc-chat__popover gc-chat__model-popover",
435
+ children: [/* @__PURE__ */ jsxs("div", {
436
+ className: "gc-chat__model-section",
437
+ children: [/* @__PURE__ */ jsx("div", {
438
+ className: "gc-chat__popover-group-label",
439
+ children: labels.selectModel
440
+ }), /* @__PURE__ */ jsx("div", {
441
+ className: "gc-chat__popover-list",
442
+ role: "listbox",
443
+ "aria-label": labels.selectModel,
444
+ children: models.map((option) => {
445
+ const selected = option.id === activeModel.id;
446
+ return /* @__PURE__ */ jsxs("button", {
447
+ type: "button",
448
+ role: "option",
449
+ "aria-selected": selected,
450
+ disabled: option.disabled,
451
+ className: "gc-chat__popover-item gc-chat__model-option",
452
+ "data-selected": selected || void 0,
453
+ onClick: () => handleSelectModel(option),
454
+ children: [
455
+ option.icon ? /* @__PURE__ */ jsx("span", {
456
+ className: "gc-chat__popover-icon",
457
+ "aria-hidden": "true",
458
+ children: option.icon
459
+ }) : null,
460
+ /* @__PURE__ */ jsxs("span", {
461
+ className: "gc-chat__popover-copy",
462
+ children: [/* @__PURE__ */ jsxs("span", {
463
+ className: "gc-chat__popover-label",
464
+ children: [option.label, option.badge ? /* @__PURE__ */ jsx("span", {
465
+ className: "gc-chat__popover-badge",
466
+ children: option.badge
467
+ }) : null]
468
+ }), option.description ? /* @__PURE__ */ jsx("span", {
469
+ className: "gc-chat__popover-desc",
470
+ children: option.description
471
+ }) : null]
472
+ }),
473
+ /* @__PURE__ */ jsx("span", {
474
+ className: "gc-chat__popover-trailing",
475
+ "aria-hidden": "true",
476
+ children: selected ? /* @__PURE__ */ jsx(Icon, {
477
+ name: "check",
478
+ width: 14,
479
+ height: 14
480
+ }) : null
481
+ })
482
+ ]
483
+ }, option.id);
484
+ })
485
+ })]
486
+ }), efforts.length > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("div", {
487
+ className: "gc-chat__popover-divider",
488
+ role: "separator"
489
+ }), /* @__PURE__ */ jsxs("div", {
490
+ className: "gc-chat__model-section gc-chat__reasoning-section",
491
+ children: [/* @__PURE__ */ jsxs("div", {
492
+ className: "gc-chat__reasoning-header",
493
+ children: [/* @__PURE__ */ jsx("span", {
494
+ className: "gc-chat__popover-group-label",
495
+ children: labels.reasoningEffort
496
+ }), activeEffort && !activeModel.defaultReasoningEffort ? /* @__PURE__ */ jsx("button", {
497
+ type: "button",
498
+ className: "gc-chat__reasoning-reset",
499
+ onClick: () => handleSelectEffort(void 0),
500
+ children: labels.reasoningDefault
501
+ }) : null]
502
+ }), /* @__PURE__ */ jsx("div", {
503
+ className: "gc-chat__reasoning-segments",
504
+ role: "radiogroup",
505
+ "aria-label": labels.reasoningEffort,
506
+ children: efforts.map((effort) => {
507
+ const selected = activeEffort === effort;
508
+ return /* @__PURE__ */ jsx("button", {
509
+ type: "button",
510
+ role: "radio",
511
+ "aria-checked": selected,
512
+ className: "gc-chat__reasoning-segment",
513
+ "data-selected": selected || void 0,
514
+ onClick: () => handleSelectEffort(effort),
515
+ children: effortLabel(effort, labels)
516
+ }, effort);
517
+ })
518
+ })]
519
+ })] }) : null]
520
+ }) : null]
521
+ });
522
+ }
523
+ //#endregion
332
524
  //#region src/chat-composer.tsx
333
525
  /** Drafts and upload requests are isolated by conversation id. */
334
526
  function ChatComposer(props) {
335
527
  return /* @__PURE__ */ jsx(Composer, { ...props }, props.chat.id);
336
528
  }
337
- function Composer({ chat, labels: overrides, attachments, speech, skills, onSelectSkill, onAddSkill, onBrowseSkills, mentions, onSelectMention, plusMenuActions, toolbar, context, requestOptions, disabled, className = "", onActionError }) {
529
+ function Composer({ chat, labels: overrides, attachments, speech, skills, onSelectSkill, onAddSkill, onBrowseSkills, mentions, onSelectMention, plusMenuActions, modelPicker, toolbar, context, requestOptions, disabled, className = "", onActionError }) {
338
530
  const labels = resolveLabels(overrides);
339
531
  const [text, setText] = useState("");
340
532
  const [cursor, setCursor] = useState(0);
@@ -756,28 +948,36 @@ function Composer({ chat, labels: overrides, attachments, speech, skills, onSele
756
948
  ]
757
949
  }), /* @__PURE__ */ jsxs("div", {
758
950
  className: "gc-chat__composer-actions",
759
- children: [speech && /* @__PURE__ */ jsx(ChatIconButton, {
760
- label: voiceLabels.start,
761
- icon: "mic",
762
- tooltip: voice.support.reason ?? voiceLabels.start,
763
- "aria-describedby": !voice.support.supported ? `${inputId}-voice-support` : void 0,
764
- disabled: disabled || chat.isBusy || voice.isActive || voice.state === "consent" || !voice.support.supported,
765
- onClick: voice.start
766
- }), chat.isBusy ? /* @__PURE__ */ jsx(ChatIconButton, {
767
- className: "gc-chat__send",
768
- label: labels.stop,
769
- icon: "stop",
770
- onClick: (event) => {
771
- event.preventDefault();
772
- chat.stop().catch(report);
773
- }
774
- }) : /* @__PURE__ */ jsx(ChatIconButton, {
775
- className: "gc-chat__send",
776
- type: "submit",
777
- disabled: !canSend,
778
- label: labels.send,
779
- icon: "send"
780
- })]
951
+ children: [
952
+ modelPicker ? /* @__PURE__ */ jsx(ChatModelPicker, {
953
+ ...modelPicker,
954
+ labels: overrides,
955
+ disabled: disabled || chat.isBusy || modelPicker.disabled
956
+ }) : null,
957
+ speech && /* @__PURE__ */ jsx(ChatIconButton, {
958
+ label: voiceLabels.start,
959
+ icon: "mic",
960
+ tooltip: voice.support.reason ?? voiceLabels.start,
961
+ "aria-describedby": !voice.support.supported ? `${inputId}-voice-support` : void 0,
962
+ disabled: disabled || chat.isBusy || voice.isActive || voice.state === "consent" || !voice.support.supported,
963
+ onClick: voice.start
964
+ }),
965
+ chat.isBusy ? /* @__PURE__ */ jsx(ChatIconButton, {
966
+ className: "gc-chat__send",
967
+ label: labels.stop,
968
+ icon: "stop",
969
+ onClick: (event) => {
970
+ event.preventDefault();
971
+ chat.stop().catch(report);
972
+ }
973
+ }) : /* @__PURE__ */ jsx(ChatIconButton, {
974
+ className: "gc-chat__send",
975
+ type: "submit",
976
+ disabled: !canSend,
977
+ label: labels.send,
978
+ icon: "send"
979
+ })
980
+ ]
781
981
  })]
782
982
  }),
783
983
  error ? /* @__PURE__ */ jsxs("div", {
@@ -1043,7 +1243,7 @@ function ToolActivity({ part, chat, labels, active, presentation, requestOptions
1043
1243
  //#region src/use-message-scroll.ts
1044
1244
  const useClientLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
1045
1245
  /** One scroll writer. Resize observations change the target, never the reader's intent. */
1046
- function useMessageScroll(messages) {
1246
+ function useMessageScroll(messages, isBusy = false) {
1047
1247
  const scroller = useRef(null);
1048
1248
  const content = useRef(null);
1049
1249
  const following = useRef(true);
@@ -1054,6 +1254,7 @@ function useMessageScroll(messages) {
1054
1254
  const endScrollbarGesture = useRef(void 0);
1055
1255
  const touchY = useRef(void 0);
1056
1256
  const previousMessages = useRef(messages.map(({ id }) => id));
1257
+ const previousBusy = useRef(isBusy);
1057
1258
  const [awayFromBottom, setAwayFromBottom] = useState(false);
1058
1259
  const cancel = useCallback(() => {
1059
1260
  if (frame.current !== void 0) cancelAnimationFrame(frame.current);
@@ -1141,18 +1342,32 @@ function useMessageScroll(messages) {
1141
1342
  ]);
1142
1343
  useClientLayoutEffect(() => {
1143
1344
  const previous = previousMessages.current;
1144
- const tail = previous.at(-1);
1145
- const tailIndex = tail === void 0 ? -1 : messages.findIndex((message) => message.id === tail);
1146
1345
  const known = new Set(previous);
1147
- const appendedUser = (tail === void 0 || tailIndex !== -1) && messages.slice(tailIndex + 1).some((message) => message.role === "user" && !known.has(message.id));
1346
+ let anchorIndex = -1;
1347
+ for (let index = messages.length - 1; index >= 0; index--) if (known.has(messages[index].id)) {
1348
+ anchorIndex = index;
1349
+ break;
1350
+ }
1351
+ const appendedUser = (previous.length === 0 || anchorIndex !== -1) && messages.slice(anchorIndex + 1).some((message) => message.role === "user");
1352
+ const startedRun = !previousBusy.current && isBusy;
1148
1353
  previousMessages.current = messages.map(({ id }) => id);
1149
- if (appendedUser) scrollToLatest();
1354
+ previousBusy.current = isBusy;
1355
+ if (appendedUser || startedRun) scrollToLatest();
1150
1356
  schedule();
1151
1357
  }, [
1152
1358
  messages,
1359
+ isBusy,
1153
1360
  scrollToLatest,
1154
1361
  schedule
1155
1362
  ]);
1363
+ function resumeAtBottom(element) {
1364
+ if (following.current) return false;
1365
+ if (Math.max(0, element.scrollHeight - element.clientHeight) - element.scrollTop > 4) return false;
1366
+ following.current = true;
1367
+ setAwayFromBottom(false);
1368
+ schedule();
1369
+ return true;
1370
+ }
1156
1371
  function onScroll() {
1157
1372
  const element = scroller.current;
1158
1373
  if (!element) return;
@@ -1161,12 +1376,13 @@ function useMessageScroll(messages) {
1161
1376
  lastTop.current = top;
1162
1377
  if (writtenTop.current !== void 0 && Math.abs(top - writtenTop.current) < 1) return;
1163
1378
  writtenTop.current = void 0;
1379
+ const maxTop = Math.max(0, element.scrollHeight - element.clientHeight);
1164
1380
  if (top < previous - 1) {
1165
- if (endScrollbarGesture.current) pause();
1381
+ if (endScrollbarGesture.current && maxTop - top > 1) pause();
1166
1382
  else schedule();
1167
1383
  return;
1168
1384
  }
1169
- if (top > previous && element.scrollHeight - top - element.clientHeight <= 4) {
1385
+ if (top > previous && maxTop - top <= 4) {
1170
1386
  following.current = true;
1171
1387
  setAwayFromBottom(false);
1172
1388
  schedule();
@@ -1204,7 +1420,13 @@ function useMessageScroll(messages) {
1204
1420
  return false;
1205
1421
  }
1206
1422
  function onWheel(event) {
1207
- if (event.deltaY >= 0 || event.ctrlKey || consumedByNestedScroll(event)) return;
1423
+ if (event.ctrlKey || Math.abs(event.deltaX) > Math.abs(event.deltaY)) return;
1424
+ if (event.deltaY > 0) {
1425
+ const element = scroller.current;
1426
+ if (element) resumeAtBottom(element);
1427
+ return;
1428
+ }
1429
+ if (event.deltaY >= 0 || event.deltaMode === 0 && event.deltaY > -2 || consumedByNestedScroll(event)) return;
1208
1430
  pause();
1209
1431
  }
1210
1432
  function onTouchStart(event) {
@@ -1212,7 +1434,10 @@ function useMessageScroll(messages) {
1212
1434
  }
1213
1435
  function onTouchMove(event) {
1214
1436
  const y = event.touches[0]?.clientY;
1215
- if (y !== void 0 && touchY.current !== void 0 && y > touchY.current + 2 && !consumedByNestedScroll(event)) pause();
1437
+ if (y !== void 0 && touchY.current !== void 0) {
1438
+ if (y > touchY.current + 2 && !consumedByNestedScroll(event)) pause();
1439
+ else if (y < touchY.current - 2 && scroller.current) resumeAtBottom(scroller.current);
1440
+ }
1216
1441
  touchY.current = y;
1217
1442
  }
1218
1443
  function onKeyDown(event) {
@@ -1335,8 +1560,11 @@ function ChatMessages(props) {
1335
1560
  }
1336
1561
  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 }) {
1337
1562
  const labels = resolveLabels(labelOverrides);
1338
- const { scroller, content, awayFromBottom, scrollToLatest, reveal, scrollEvents } = useMessageScroll(chat.messages);
1339
- useImperativeHandle(scrollRef, () => ({ reveal }), [reveal]);
1563
+ const { scroller, content, awayFromBottom, scrollToLatest, reveal, scrollEvents } = useMessageScroll(chat.messages, chat.isBusy);
1564
+ useImperativeHandle(scrollRef, () => ({
1565
+ reveal,
1566
+ scrollToLatest
1567
+ }), [reveal, scrollToLatest]);
1340
1568
  const copyTimeout = useRef(void 0);
1341
1569
  const actionInFlight = useRef(false);
1342
1570
  const [copiedId, setCopiedId] = useState();
@@ -2058,6 +2286,6 @@ function ChatHistoryMenu({ conversations, currentId, onSelect, onNewChat, disabl
2058
2286
  });
2059
2287
  }
2060
2288
  //#endregion
2061
- export { AiChat, ChatAppearanceProvider, ChatBranchSwitcher, ChatCheckpointBanner, ChatComposer, ChatHistoryMenu, Icon as ChatIcon, ChatIconButton, ChatMentionMenu, ChatMessages, ChatPanel, ChatPlusMenu, ChatSlashMenu, ChatTargetingOverlay, ContextTray, SpeechInputError, createBrowserSpeechAdapter, createTextSelectionAdapter, defaultSpeechInputLabels, detectComposerTrigger, enUS, enUSTargetingLabels, filterMentions, filterSkills, getMessageText, parseSkillMarkdown, projectChatBranches, replaceComposerTrigger, slugifySkillName, toChatContextItem, useAiChat, useChatBranches, useChatContext, useChatTargeting, useComposerTrigger, useSpeechInput, zhCN, zhCNTargetingLabels };
2289
+ export { AiChat, CHAT_REASONING_EFFORTS, ChatAppearanceProvider, ChatBranchSwitcher, ChatCheckpointBanner, ChatComposer, ChatHistoryMenu, Icon as ChatIcon, ChatIconButton, ChatMentionMenu, ChatMessages, ChatModelPicker, ChatPanel, ChatPlusMenu, ChatSlashMenu, ChatTargetingOverlay, ContextTray, SpeechInputError, createBrowserSpeechAdapter, createTextSelectionAdapter, defaultSpeechInputLabels, detectComposerTrigger, enUS, enUSTargetingLabels, filterMentions, filterSkills, getMessageText, isChatReasoningEffort, parseSkillMarkdown, projectChatBranches, replaceComposerTrigger, resolveChatModelSelection, selectChatModel, slugifySkillName, toChatContextItem, useAiChat, useChatBranches, useChatContext, useChatTargeting, useComposerTrigger, useSpeechInput, zhCN, zhCNTargetingLabels };
2062
2290
 
2063
2291
  //# sourceMappingURL=index.js.map