@assistant-ui/react 0.5.11 → 0.5.14

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