@economic/agents-react 1.3.8 → 1.4.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/README.md CHANGED
@@ -163,7 +163,7 @@ All hooks share these connection options (`useAssistant` omits `actorId`):
163
163
  ## Types
164
164
 
165
165
  ```ts
166
- type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized";
166
+ type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized" | "error";
167
167
  type AgentConnectionType = "agent" | "chat" | "assistant";
168
168
 
169
169
  type AgentConnectionState = {
package/dist/index.d.mts CHANGED
@@ -7,7 +7,7 @@ type Agent = Omit<_CfAgent, "call"> & {
7
7
  call<T = unknown>(method: string, args?: unknown[], options?: unknown): Promise<T>;
8
8
  };
9
9
  type Chat = ReturnType<typeof useAgentChat>;
10
- type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized";
10
+ type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized" | "error";
11
11
  type AgentConnectionType = "agent" | "chat" | "assistant";
12
12
  type ChatSummary = {
13
13
  name: string;
package/dist/index.mjs CHANGED
@@ -11,6 +11,24 @@ function getTokenArgs(authToken) {
11
11
  }
12
12
  //#endregion
13
13
  //#region src/useAgent.ts
14
+ function getCloseReason(code) {
15
+ return {
16
+ 1e3: "normal-closure",
17
+ 1001: "going-away",
18
+ 1002: "protocol-error",
19
+ 1003: "unsupported-data",
20
+ 1005: "no-status-received",
21
+ 1006: "abnormal-closure",
22
+ 1007: "invalid-frame-payload-data",
23
+ 1008: "policy-violation",
24
+ 1009: "message-too-big",
25
+ 1010: "missing-extension",
26
+ 1011: "internal-error",
27
+ 1012: "service-restart",
28
+ 1013: "try-again-later",
29
+ 1015: "tls-handshake-failure"
30
+ }[code] ?? "unknown";
31
+ }
14
32
  function useAgent(options) {
15
33
  const { enabled, host, agentName, actorId, name, sub, authToken, onError, onOpen, onClose, onMessage } = options;
16
34
  const [type, setType] = useState(void 0);
@@ -26,7 +44,15 @@ function useAgent(options) {
26
44
  },
27
45
  onClose: (event) => {
28
46
  if (event.code >= 3e3) setStatus("unauthorized");
29
- else setStatus("disconnected");
47
+ else {
48
+ setStatus("disconnected");
49
+ if (!event.wasClean) console.warn("[useAgent] WebSocket closed unexpectedly", {
50
+ code: event.code,
51
+ reason: event.reason,
52
+ closeReason: getCloseReason(event.code),
53
+ wasClean: event.wasClean
54
+ });
55
+ }
30
56
  onClose?.(event);
31
57
  },
32
58
  onMessage: (event) => {
@@ -38,7 +64,7 @@ function useAgent(options) {
38
64
  onMessage?.(event);
39
65
  },
40
66
  onError: (event) => {
41
- setStatus("disconnected");
67
+ setStatus("error");
42
68
  onError?.(event);
43
69
  },
44
70
  ...getTokenArgs(authToken)
@@ -113,21 +139,22 @@ function useAssistant(options) {
113
139
  }
114
140
  });
115
141
  const refreshChats = useCallback(async () => {
116
- setChats((await assistant.call("getChats")).map((chat) => {
142
+ const fetched = (await assistant.call("getChats")).map((chat) => {
117
143
  const c = {
118
144
  ...chat,
119
145
  name: chat.durable_object_name
120
146
  };
121
147
  delete c.durable_object_name;
122
148
  return c;
123
- }));
149
+ });
150
+ setChats(fetched);
151
+ return fetched;
124
152
  }, [assistant]);
125
153
  useEffect(() => {
126
154
  if (assistantStatus === "connected") refreshChats().catch(console.error);
127
155
  }, [assistantStatus, refreshChats]);
128
156
  const getChats = async () => {
129
- await refreshChats();
130
- return chats;
157
+ return refreshChats();
131
158
  };
132
159
  const createChat = async () => {
133
160
  const newChatId = await assistant.call("createChat");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents-react",
3
- "version": "1.3.8",
3
+ "version": "1.4.2",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"