@economic/agents-react 1.6.1 → 1.6.4

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
@@ -111,7 +111,12 @@ function Chat({ userId, authToken }: { userId: string; authToken: string }) {
111
111
  ))}
112
112
 
113
113
  <Composer
114
- onSend={(text) => chat.chat.sendMessage({ role: "user", parts: [{ type: "text", text }] })}
114
+ onSend={(text) =>
115
+ chat.chat.sendMessage({
116
+ role: "user",
117
+ parts: [{ type: "text", text }],
118
+ })
119
+ }
115
120
  />
116
121
  </div>
117
122
  );
@@ -168,7 +173,13 @@ All hooks share these connection options (`useAssistant` omits `actorId`):
168
173
  ## Types
169
174
 
170
175
  ```ts
171
- type AgentConnectionStatus = "connecting" | "connected" | "disconnected" | "unauthorized" | "error";
176
+ type AgentConnectionStatus =
177
+ | "connecting"
178
+ | "reconnecting"
179
+ | "connected"
180
+ | "disconnected"
181
+ | "unauthorized"
182
+ | "error";
172
183
  type AgentConnectionType = "agent" | "chat" | "assistant";
173
184
 
174
185
  type AgentConnectionState = {
@@ -202,5 +213,3 @@ The package also exports the handler types `AssistantActions`, `GetChatsHandler`
202
213
  ## Authentication
203
214
 
204
215
  Pass a JWT as `authToken`. It is sent both as an `Authorization: Bearer …` header and as a WebSocket subprotocol (`["bearer", token]`), matching the server-side [`getJwtAuthConfig`](../agents/README.md#authentication). When verification fails, the connection status becomes `"unauthorized"`.
205
-
206
- .
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" | "error";
10
+ type AgentConnectionStatus = "connecting" | "reconnecting" | "connected" | "disconnected" | "unauthorized" | "error";
11
11
  type AgentConnectionType = "agent" | "chat" | "assistant";
12
12
  type ChatSummary = {
13
13
  name: string; /** True when this entry only represents the active, unregistered chat facet. */
package/dist/index.mjs CHANGED
@@ -1,26 +1,22 @@
1
+ import { n as isPublicChatMessage, t as getTokenArgs } from "./util-CPk3RN3P.mjs";
1
2
  import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
2
3
  import { useAgent as useAgent$1 } from "agents/react";
3
4
  import { getToolApproval, getToolCallId, getToolPartState, useAgentChat } from "@cloudflare/ai-chat/react";
4
- //#region src/util.ts
5
- function getTokenArgs(authToken) {
6
- if (!authToken) return {};
7
- return {
8
- headers: { Authorization: `Bearer ${authToken}` },
9
- protocols: ["bearer", authToken]
10
- };
11
- }
12
- //#endregion
13
5
  //#region src/useAgent.ts
14
6
  function getAgentConnectionStatus(state, connectionId) {
15
7
  return state.connectionId === connectionId ? state.status : "connecting";
16
8
  }
17
9
  function getNextAgentConnectionState(state, connectionId, status) {
18
- if (status === "disconnected" && state.connectionId === connectionId && state.status === "error") return state;
10
+ if ((status === "disconnected" || status === "reconnecting") && state.connectionId === connectionId && state.status === "error") return state;
19
11
  return {
20
12
  connectionId,
21
13
  status
22
14
  };
23
15
  }
16
+ function getAgentCloseStatus(code, shouldReconnect) {
17
+ if (code >= 3e3) return "unauthorized";
18
+ return shouldReconnect ? "reconnecting" : "disconnected";
19
+ }
24
20
  function getCloseReason(code) {
25
21
  return {
26
22
  1e3: "normal-closure",
@@ -142,16 +138,14 @@ function useAgent(options) {
142
138
  }, [isCurrentConnectionEvent]);
143
139
  const handleClose = useCallback((event) => {
144
140
  if (!isCurrentConnectionEvent(event)) return;
145
- if (event.code >= 3e3) setStatus("unauthorized");
146
- else {
147
- setStatus("disconnected");
148
- if (!event.wasClean) console.warn("[useAgent] WebSocket closed unexpectedly", {
149
- code: event.code,
150
- reason: event.reason,
151
- closeReason: getCloseReason(event.code),
152
- wasClean: event.wasClean
153
- });
154
- }
141
+ const shouldReconnect = event.target.shouldReconnect;
142
+ setStatus(getAgentCloseStatus(event.code, shouldReconnect));
143
+ if (event.code < 3e3 && !event.wasClean) console.warn("[useAgent] WebSocket closed unexpectedly", {
144
+ code: event.code,
145
+ reason: event.reason,
146
+ closeReason: getCloseReason(event.code),
147
+ wasClean: event.wasClean
148
+ });
155
149
  committedConnection.current.callbacks.onClose?.(event);
156
150
  }, [isCurrentConnectionEvent, setStatus]);
157
151
  const handleMessage = useCallback((event) => {
@@ -169,7 +163,7 @@ function useAgent(options) {
169
163
  ]);
170
164
  const handleError = useCallback((event) => {
171
165
  if (!isCurrentConnectionEvent(event)) return;
172
- setStatus("error");
166
+ setStatus(event.target.shouldReconnect ? "reconnecting" : "error");
173
167
  committedConnection.current.callbacks.onError?.(event);
174
168
  }, [isCurrentConnectionEvent, setStatus]);
175
169
  const agent = useAgent$1({
@@ -251,7 +245,10 @@ function useChat(options) {
251
245
  return {
252
246
  status,
253
247
  agent,
254
- chat,
248
+ chat: {
249
+ ...chat,
250
+ messages: chat.messages.filter(isPublicChatMessage)
251
+ },
255
252
  submitMessageFeedback,
256
253
  getMessageFeedback
257
254
  };
@@ -0,0 +1,13 @@
1
+ //#region src/util.ts
2
+ function getTokenArgs(authToken) {
3
+ if (!authToken) return {};
4
+ return {
5
+ headers: { Authorization: `Bearer ${authToken}` },
6
+ protocols: ["bearer", authToken]
7
+ };
8
+ }
9
+ function isPublicChatMessage(message) {
10
+ return !message.id.startsWith("compaction_");
11
+ }
12
+ //#endregion
13
+ export { isPublicChatMessage as n, getTokenArgs as t };
package/dist/v1.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { n as isPublicChatMessage } from "./util-CPk3RN3P.mjs";
1
2
  import React from "react";
2
3
  import { useAgent } from "agents/react";
3
4
  import { useAgentChat } from "@cloudflare/ai-chat/react";
@@ -113,7 +114,10 @@ function useAIChatAgent(options) {
113
114
  ]);
114
115
  return {
115
116
  agent,
116
- chat,
117
+ chat: {
118
+ ...chat,
119
+ messages: chat.messages.filter(isPublicChatMessage)
120
+ },
117
121
  connectionStatus
118
122
  };
119
123
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents-react",
3
- "version": "1.6.1",
3
+ "version": "1.6.4",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"