@economic/agents-react 1.1.9 → 1.1.10

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.d.mts CHANGED
@@ -317,6 +317,7 @@ declare function useChat<ToolContext extends Record<string, unknown>>(options: U
317
317
  interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Record<string, unknown>> extends Omit<UseChatOptions<ToolContext>, "actorId" | "getInitialMessages"> {}
318
318
  declare function useAssistant<ToolContext extends Record<string, unknown>>(options: UseAssistantOptions<ToolContext>): {
319
319
  currentChatName: string | undefined;
320
+ chats: ChatSummary[];
320
321
  status: AgentConnectionStatus;
321
322
  assistant: {
322
323
  getChats: () => Promise<ChatSummary[]>;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { useAgent as useAgent$1 } from "agents/react";
2
- import { useEffect, useRef, useState } from "react";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
3
  import { useAgentChat } from "@cloudflare/ai-chat/react";
4
4
  //#region src/util.ts
5
5
  function getTokenArgs(authToken) {
@@ -80,22 +80,31 @@ function useChat(options) {
80
80
  function useAssistant(options) {
81
81
  const { toolContext, welcomeMessage, ...agentOptions } = options;
82
82
  const [chatId, setChatId] = useState();
83
+ const [chats, setChats] = useState([]);
83
84
  const assistant = useAgent(agentOptions);
84
- const getChats = async () => {
85
- return (await assistant.call("getChats")).map((chat) => {
85
+ const refreshChats = useCallback(async () => {
86
+ setChats((await assistant.call("getChats")).map((chat) => {
86
87
  const c = {
87
88
  ...chat,
88
89
  name: chat.durable_object_name
89
90
  };
90
91
  delete c.durable_object_name;
91
92
  return c;
92
- });
93
+ }));
94
+ }, [assistant]);
95
+ const assistantStatus = assistant.state?.status;
96
+ useEffect(() => {
97
+ if (assistantStatus === "connected") refreshChats().catch(console.error);
98
+ }, [assistantStatus, refreshChats]);
99
+ const getChats = async () => {
100
+ await refreshChats();
101
+ return chats;
93
102
  };
94
103
  const createChat = async () => {
95
- const chatId = await assistant.call("createChat");
96
- setChatId(chatId);
97
- await getChats();
98
- return chatId;
104
+ const newChatId = await assistant.call("createChat");
105
+ setChatId(newChatId);
106
+ await refreshChats();
107
+ return newChatId;
99
108
  };
100
109
  const openChat = (chatId) => {
101
110
  setChatId(chatId);
@@ -103,7 +112,7 @@ function useAssistant(options) {
103
112
  const deleteChat = async (chatId) => {
104
113
  await assistant.call("deleteChat", [chatId]);
105
114
  setChatId(void 0);
106
- await getChats();
115
+ await refreshChats();
107
116
  };
108
117
  const isChatReady = !!chatId && assistant.state?.status === "connected";
109
118
  const chat = useChat({
@@ -119,6 +128,7 @@ function useAssistant(options) {
119
128
  });
120
129
  return {
121
130
  currentChatName: chatId,
131
+ chats,
122
132
  status: (chatId ? chat.agent.state?.status : assistant.state?.status) ?? "connecting",
123
133
  assistant: {
124
134
  getChats,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents-react",
3
- "version": "1.1.9",
3
+ "version": "1.1.10",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"