@axiom-lattice/react-sdk 2.1.1 → 2.1.2

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
@@ -43,6 +43,7 @@ function useChat(threadId, options = {}) {
43
43
  isLoading: false,
44
44
  error: null,
45
45
  streamingMessage: null,
46
+ interrupts: void 0,
46
47
  ...options.enableReturnStateWhenStreamCompleted ? { agentState: null } : {}
47
48
  });
48
49
  const stopStreamingRef = useRef(null);
@@ -55,7 +56,15 @@ function useChat(threadId, options = {}) {
55
56
  setState(
56
57
  (prev) => ({
57
58
  ...prev,
58
- agentState
59
+ agentState,
60
+ interrupts: agentState?.tasks?.flatMap((task) => {
61
+ return task.interrupts.map((interrupt) => {
62
+ return {
63
+ id: interrupt.id,
64
+ value: interrupt.value
65
+ };
66
+ });
67
+ })
59
68
  })
60
69
  );
61
70
  } catch (error) {
@@ -69,29 +78,38 @@ function useChat(threadId, options = {}) {
69
78
  chunkMessageMerger.current.initialMessages(options.initialMessages);
70
79
  }
71
80
  }, [options.initialMessages]);
72
- const handleStreamEvent = useCallback((chunk) => {
73
- chunkMessageMerger.current.push(chunk);
74
- let todos;
75
- if (chunk.type === "tool" && chunk.data && typeof chunk.data.content === "string" && chunk.data.content.startsWith("```todo_list")) {
76
- try {
77
- const content = chunk.data.content;
78
- const match = content.match(/```todo_list\s*([\s\S]*?)\s*```/);
79
- if (match && match[1]) {
80
- todos = JSON.parse(match[1]);
81
+ const handleStreamEvent = useCallback(
82
+ (chunk) => {
83
+ let interrupt;
84
+ if (chunk.type === "interrupt") {
85
+ interrupt = chunk;
86
+ } else {
87
+ chunkMessageMerger.current.push(chunk);
88
+ }
89
+ let todos;
90
+ if (chunk.type === "tool" && chunk.data && typeof chunk.data.content === "string" && chunk.data.content.startsWith("```todo_list")) {
91
+ try {
92
+ const content = chunk.data.content;
93
+ const match = content.match(/```todo_list\s*([\s\S]*?)\s*```/);
94
+ if (match && match[1]) {
95
+ todos = JSON.parse(match[1]);
96
+ }
97
+ } catch (e) {
98
+ console.error("Failed to parse todo list from chunk", e);
81
99
  }
82
- } catch (e) {
83
- console.error("Failed to parse todo list from chunk", e);
84
100
  }
85
- }
86
- const updatedMessages = chunkMessageMerger.current.getMessages();
87
- setState((prev) => ({
88
- ...prev,
89
- todos: todos || prev.todos,
90
- messages: updatedMessages,
91
- isLoading: true,
92
- streamingMessage: null
93
- }));
94
- }, []);
101
+ const updatedMessages = chunkMessageMerger.current.getMessages();
102
+ setState((prev) => ({
103
+ ...prev,
104
+ interrupts: interrupt ? [interrupt] : void 0,
105
+ todos: todos || prev.todos,
106
+ messages: updatedMessages,
107
+ isLoading: true,
108
+ streamingMessage: null
109
+ }));
110
+ },
111
+ []
112
+ );
95
113
  const sendMessage = useCallback(
96
114
  async (data) => {
97
115
  if (!threadId) {
@@ -218,12 +236,21 @@ function useChat(threadId, options = {}) {
218
236
  setState((prev) => ({ ...prev, isLoading: true, error: null }));
219
237
  try {
220
238
  const agentState = await client.getAgentState(threadId);
239
+ const interrupts = agentState?.tasks?.flatMap((task) => {
240
+ return task.interrupts.map((interrupt) => {
241
+ return {
242
+ id: interrupt.id,
243
+ value: interrupt.value
244
+ };
245
+ });
246
+ });
221
247
  const fetchedMessages = await client.getMessages({ threadId });
222
248
  chunkMessageMerger.current.reset();
223
249
  chunkMessageMerger.current.initialMessages(fetchedMessages);
224
250
  setState((prev) => ({
225
251
  ...prev,
226
252
  agentState,
253
+ interrupts,
227
254
  todos: agentState?.values?.todos,
228
255
  messages: chunkMessageMerger.current.getMessages(),
229
256
  isLoading: false
@@ -278,6 +305,7 @@ function useChat(threadId, options = {}) {
278
305
  setState((prev) => ({
279
306
  ...prev,
280
307
  messages: [],
308
+ interrupts: void 0,
281
309
  streamingMessage: null
282
310
  }));
283
311
  }, []);
@@ -2685,6 +2713,7 @@ MemoizedBubbleList.displayName = "MemoizedBubbleList";
2685
2713
  var Chating = ({
2686
2714
  avatar,
2687
2715
  name,
2716
+ interrupts,
2688
2717
  description,
2689
2718
  default_submit_message,
2690
2719
  tenant_id,
@@ -3096,9 +3125,18 @@ ${JSON.stringify(tool_call)}
3096
3125
  message: `${error.message}`
3097
3126
  }
3098
3127
  ) }),
3128
+ interrupts && interrupts.length > 0 && /* @__PURE__ */ jsx19(Space10, { direction: "vertical", style: { width: "100%" }, children: interrupts.map((interrupt) => /* @__PURE__ */ jsx19(
3129
+ MDResponse,
3130
+ {
3131
+ content: interrupt.value,
3132
+ eventHandler: handleMDResponseEvent
3133
+ },
3134
+ interrupt.id
3135
+ )) }),
3099
3136
  /* @__PURE__ */ jsx19(
3100
3137
  Sender,
3101
3138
  {
3139
+ disabled: interrupts && interrupts.length > 0,
3102
3140
  allowSpeech: true,
3103
3141
  ref: senderRef,
3104
3142
  value: content,