@axiom-lattice/react-sdk 2.1.57 → 2.1.58

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.mjs CHANGED
@@ -1668,6 +1668,7 @@ function AgentThreadProvider({
1668
1668
  }));
1669
1669
  }, [clientAssistantId, tenantId, threadId]);
1670
1670
  const stopStreamingRef = useRef5(null);
1671
+ const sendMessageStreamsRef = useRef5(/* @__PURE__ */ new Map());
1671
1672
  const chunkMessageMerger = useRef5(createSimpleMessageMerger2());
1672
1673
  const lastAgentStateCreatedAtRef = useRef5(null);
1673
1674
  const messageCountRef = useRef5(0);
@@ -1817,19 +1818,61 @@ function AgentThreadProvider({
1817
1818
  }
1818
1819
  try {
1819
1820
  if (options.streaming !== false && streaming !== false) {
1820
- client.chat.stream(
1821
- {
1822
- threadId,
1823
- messages: [userMessage],
1824
- command,
1825
- custom_run_config: customRunConfig,
1826
- ...rest
1827
- },
1828
- () => {
1829
- }
1830
- );
1831
- if (!stopStreamingRef.current) {
1832
- loadMessages();
1821
+ if (stopStreamingRef.current) {
1822
+ client.chat.stream(
1823
+ {
1824
+ threadId,
1825
+ messages: [userMessage],
1826
+ command,
1827
+ custom_run_config: customRunConfig,
1828
+ ...rest
1829
+ },
1830
+ () => {
1831
+ },
1832
+ // Empty handler - resumeStream will handle chunks
1833
+ async () => {
1834
+ if (options.enableReturnStateWhenStreamCompleted) {
1835
+ await fetchAndUpdateAgentState(threadId);
1836
+ }
1837
+ },
1838
+ (error) => {
1839
+ setState((prev) => ({
1840
+ ...prev,
1841
+ isLoading: false,
1842
+ error
1843
+ }));
1844
+ }
1845
+ );
1846
+ } else {
1847
+ const stopStreaming2 = client.chat.stream(
1848
+ {
1849
+ threadId,
1850
+ messages: [userMessage],
1851
+ command,
1852
+ custom_run_config: customRunConfig,
1853
+ ...rest
1854
+ },
1855
+ (chunk) => handleStreamEvent(chunk),
1856
+ async () => {
1857
+ setState((prev) => ({
1858
+ ...prev,
1859
+ isLoading: false
1860
+ }));
1861
+ if (options.enableReturnStateWhenStreamCompleted) {
1862
+ await fetchAndUpdateAgentState(threadId);
1863
+ }
1864
+ sendMessageStreamsRef.current.delete(userMessage.id);
1865
+ },
1866
+ (error) => {
1867
+ setState((prev) => ({
1868
+ ...prev,
1869
+ isLoading: false,
1870
+ error
1871
+ }));
1872
+ sendMessageStreamsRef.current.delete(userMessage.id);
1873
+ }
1874
+ );
1875
+ sendMessageStreamsRef.current.set(userMessage.id, stopStreaming2);
1833
1876
  }
1834
1877
  } else {
1835
1878
  const response = await client.chat.send({
@@ -1865,16 +1908,20 @@ function AgentThreadProvider({
1865
1908
  ]
1866
1909
  );
1867
1910
  const stopStreaming = useCallback7(() => {
1911
+ sendMessageStreamsRef.current.forEach((stopFn) => {
1912
+ stopFn();
1913
+ });
1914
+ sendMessageStreamsRef.current.clear();
1868
1915
  if (stopStreamingRef.current) {
1869
1916
  stopStreamingRef.current();
1870
1917
  stopStreamingRef.current = null;
1871
- const currentMessages = chunkMessageMerger.current.getMessages();
1872
- setState((prev) => ({
1873
- ...prev,
1874
- messages: currentMessages,
1875
- isLoading: false
1876
- }));
1877
1918
  }
1919
+ const currentMessages = chunkMessageMerger.current.getMessages();
1920
+ setState((prev) => ({
1921
+ ...prev,
1922
+ messages: currentMessages,
1923
+ isLoading: false
1924
+ }));
1878
1925
  }, []);
1879
1926
  const resumeStream = useCallback7(
1880
1927
  (messageId, onMessageChunk) => {
@@ -1952,6 +1999,10 @@ function AgentThreadProvider({
1952
1999
  if (!threadId) {
1953
2000
  return;
1954
2001
  }
2002
+ sendMessageStreamsRef.current.forEach((stopFn, messageId) => {
2003
+ stopFn();
2004
+ });
2005
+ sendMessageStreamsRef.current.clear();
1955
2006
  if (stopStreamingRef.current) {
1956
2007
  stopStreamingRef.current();
1957
2008
  stopStreamingRef.current = null;
@@ -1993,7 +2044,7 @@ function AgentThreadProvider({
1993
2044
  ...needUpdateFields,
1994
2045
  isLoading: false
1995
2046
  }));
1996
- if (options.enableResumeStream && needUpdateFields.messages.length > 0) {
2047
+ if (options.enableResumeStream && needUpdateFields.messages.length > 0 && sendMessageStreamsRef.current.size === 0) {
1997
2048
  const lastMessage = needUpdateFields.messages[needUpdateFields.messages.length - 1];
1998
2049
  let lastMessageIsRemoved = false;
1999
2050
  resumeStream(lastMessage.id, (chunk) => {
@@ -2042,6 +2093,10 @@ function AgentThreadProvider({
2042
2093
  clearMessages();
2043
2094
  }
2044
2095
  return () => {
2096
+ sendMessageStreamsRef.current.forEach((stopFn) => {
2097
+ stopFn();
2098
+ });
2099
+ sendMessageStreamsRef.current.clear();
2045
2100
  if (stopStreamingRef.current) {
2046
2101
  stopStreamingRef.current();
2047
2102
  stopStreamingRef.current = null;
@@ -5784,11 +5839,15 @@ var MarkdownErrorBoundary = class extends React11.Component {
5784
5839
  };
5785
5840
  var SafeXMarkdown = React11.memo(({ content, components, className }) => {
5786
5841
  const deferredContent = useDeferredValue(content);
5842
+ if (typeof deferredContent !== "string") {
5843
+ console.warn("[MDResponse] Content is not a string:", typeof deferredContent, deferredContent);
5844
+ }
5845
+ const safeContent = typeof deferredContent === "string" ? deferredContent : String(deferredContent ?? "");
5787
5846
  return /* @__PURE__ */ jsx22("div", { className, children: /* @__PURE__ */ jsx22(
5788
5847
  XMarkdown,
5789
5848
  {
5790
5849
  components,
5791
- content: deferredContent,
5850
+ content: safeContent,
5792
5851
  paragraphTag: "div"
5793
5852
  }
5794
5853
  ) });