@assistant-ui/react 0.5.11 → 0.5.12

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -3582,7 +3582,7 @@ var getExternalStoreMessage = (message) => {
3582
3582
  };
3583
3583
 
3584
3584
  // src/runtimes/external-store/useExternalStoreSync.tsx
3585
- import { useEffect as useEffect11, useMemo as useMemo3 } from "react";
3585
+ import { useEffect as useEffect11, useInsertionEffect as useInsertionEffect4, useMemo as useMemo3, useRef as useRef6 } from "react";
3586
3586
 
3587
3587
  // src/runtimes/external-store/ThreadMessageConverter.ts
3588
3588
  var ThreadMessageConverter = class {
@@ -3598,13 +3598,104 @@ var ThreadMessageConverter = class {
3598
3598
  }
3599
3599
  };
3600
3600
 
3601
+ // src/runtimes/external-store/auto-status.tsx
3602
+ var AUTO_STATUS_RUNNING = Object.freeze({ type: "running" });
3603
+ var AUTO_STATUS_COMPLETE = Object.freeze({
3604
+ type: "complete",
3605
+ reason: "unknown"
3606
+ });
3607
+ var isAutoStatus = (status) => status === AUTO_STATUS_RUNNING || status === AUTO_STATUS_COMPLETE;
3608
+ var getAutoStatus = (isLast, isRunning) => isLast && isRunning ? AUTO_STATUS_RUNNING : AUTO_STATUS_COMPLETE;
3609
+
3610
+ // src/runtimes/external-store/ThreadMessageLike.tsx
3611
+ var fromThreadMessageLike = (like, fallbackId, fallbackStatus) => {
3612
+ const { role, content, id, createdAt, status } = like;
3613
+ const common = {
3614
+ id: id ?? fallbackId,
3615
+ createdAt: createdAt ?? /* @__PURE__ */ new Date()
3616
+ };
3617
+ switch (role) {
3618
+ case "assistant":
3619
+ return {
3620
+ ...common,
3621
+ role,
3622
+ content: content.map((part) => {
3623
+ const type = part.type;
3624
+ switch (type) {
3625
+ case "text":
3626
+ case "ui":
3627
+ return part;
3628
+ case "tool-call": {
3629
+ if ("argsText" in part) return part;
3630
+ return {
3631
+ ...part,
3632
+ argsText: JSON.stringify(part.args)
3633
+ };
3634
+ }
3635
+ default: {
3636
+ const unhandledType = type;
3637
+ throw new Error(`Unknown content part type: ${unhandledType}`);
3638
+ }
3639
+ }
3640
+ }),
3641
+ status: status ?? fallbackStatus
3642
+ };
3643
+ case "user":
3644
+ return {
3645
+ ...common,
3646
+ role,
3647
+ content: content.map((part) => {
3648
+ const type = part.type;
3649
+ switch (type) {
3650
+ case "text":
3651
+ case "ui":
3652
+ case "image":
3653
+ return part;
3654
+ default: {
3655
+ const unhandledType = type;
3656
+ throw new Error(`Unknown content part type: ${unhandledType}`);
3657
+ }
3658
+ }
3659
+ })
3660
+ };
3661
+ case "system":
3662
+ if (content.length !== 1 || content[0].type !== "text")
3663
+ throw new Error(
3664
+ "System messages must have exactly one text content part."
3665
+ );
3666
+ return {
3667
+ ...common,
3668
+ role,
3669
+ content
3670
+ };
3671
+ default: {
3672
+ const unsupportedRole = role;
3673
+ throw new Error(`Unknown message role: ${unsupportedRole}`);
3674
+ }
3675
+ }
3676
+ };
3677
+
3601
3678
  // src/runtimes/external-store/useExternalStoreSync.tsx
3602
3679
  var useExternalStoreSync = (adapter, updateData) => {
3680
+ const adapterRef = useRef6(adapter);
3681
+ useInsertionEffect4(() => {
3682
+ adapterRef.current = adapter;
3683
+ });
3603
3684
  const [converter, convertCallback] = useMemo3(() => {
3604
3685
  const converter2 = adapter.convertMessage ?? ((m) => m);
3605
3686
  const convertCallback2 = (cache, m, idx) => {
3687
+ const autoStatus = getAutoStatus(
3688
+ adapterRef.current.messages.at(-1) === m,
3689
+ adapterRef.current.isRunning ?? false
3690
+ );
3691
+ if (cache && (cache.role !== "assistant" || !isAutoStatus(cache.status) || cache.status.type === autoStatus.type))
3692
+ return cache;
3606
3693
  if (cache) return cache;
3607
- const newMessage = converter2(m, idx);
3694
+ const newMessage = fromThreadMessageLike(
3695
+ converter2(m, idx),
3696
+ idx.toString(),
3697
+ autoStatus
3698
+ );
3608
3699
  newMessage[symbolInnerMessage] = m;
3609
3700
  return newMessage;
3610
3701
  };
@@ -3770,10 +3861,10 @@ var ExternalStoreRuntime = class extends BaseAssistantRuntime {
3770
3861
  };
3771
3862
 
3772
3863
  // src/runtimes/external-store/useExternalStoreRuntime.tsx
3773
- import { useEffect as useEffect12, useInsertionEffect as useInsertionEffect4, useState as useState9 } from "react";
3864
+ import { useEffect as useEffect12, useInsertionEffect as useInsertionEffect5, useState as useState9 } from "react";
3774
3865
  var useExternalStoreRuntime = (store) => {
3775
3866
  const [runtime] = useState9(() => new ExternalStoreRuntime(store));
3776
- useInsertionEffect4(() => {
3867
+ useInsertionEffect5(() => {
3777
3868
  runtime.store = store;
3778
3869
  });
3779
3870
  useEffect12(() => {