@economic/agents-react 1.6.1 → 1.6.3
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 +13 -4
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +14 -12
- package/package.json +1 -1
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) =>
|
|
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 =
|
|
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
|
@@ -15,12 +15,16 @@ function getAgentConnectionStatus(state, connectionId) {
|
|
|
15
15
|
return state.connectionId === connectionId ? state.status : "connecting";
|
|
16
16
|
}
|
|
17
17
|
function getNextAgentConnectionState(state, connectionId, status) {
|
|
18
|
-
if (status === "disconnected" && state.connectionId === connectionId && state.status === "error") return state;
|
|
18
|
+
if ((status === "disconnected" || status === "reconnecting") && state.connectionId === connectionId && state.status === "error") return state;
|
|
19
19
|
return {
|
|
20
20
|
connectionId,
|
|
21
21
|
status
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
|
+
function getAgentCloseStatus(code, shouldReconnect) {
|
|
25
|
+
if (code >= 3e3) return "unauthorized";
|
|
26
|
+
return shouldReconnect ? "reconnecting" : "disconnected";
|
|
27
|
+
}
|
|
24
28
|
function getCloseReason(code) {
|
|
25
29
|
return {
|
|
26
30
|
1e3: "normal-closure",
|
|
@@ -142,16 +146,14 @@ function useAgent(options) {
|
|
|
142
146
|
}, [isCurrentConnectionEvent]);
|
|
143
147
|
const handleClose = useCallback((event) => {
|
|
144
148
|
if (!isCurrentConnectionEvent(event)) return;
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
154
|
-
}
|
|
149
|
+
const shouldReconnect = event.target.shouldReconnect;
|
|
150
|
+
setStatus(getAgentCloseStatus(event.code, shouldReconnect));
|
|
151
|
+
if (event.code < 3e3 && !event.wasClean) console.warn("[useAgent] WebSocket closed unexpectedly", {
|
|
152
|
+
code: event.code,
|
|
153
|
+
reason: event.reason,
|
|
154
|
+
closeReason: getCloseReason(event.code),
|
|
155
|
+
wasClean: event.wasClean
|
|
156
|
+
});
|
|
155
157
|
committedConnection.current.callbacks.onClose?.(event);
|
|
156
158
|
}, [isCurrentConnectionEvent, setStatus]);
|
|
157
159
|
const handleMessage = useCallback((event) => {
|
|
@@ -169,7 +171,7 @@ function useAgent(options) {
|
|
|
169
171
|
]);
|
|
170
172
|
const handleError = useCallback((event) => {
|
|
171
173
|
if (!isCurrentConnectionEvent(event)) return;
|
|
172
|
-
setStatus("error");
|
|
174
|
+
setStatus(event.target.shouldReconnect ? "reconnecting" : "error");
|
|
173
175
|
committedConnection.current.callbacks.onError?.(event);
|
|
174
176
|
}, [isCurrentConnectionEvent, setStatus]);
|
|
175
177
|
const agent = useAgent$1({
|