@economic/agents-react 1.3.0 → 1.3.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 +1 -0
- package/dist/index.d.mts +25 -23
- package/dist/index.mjs +39 -16
- package/dist/v1.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -196,4 +196,5 @@ The package also exports the handler types `AssistantActions`, `GetChatsHandler`
|
|
|
196
196
|
## Authentication
|
|
197
197
|
|
|
198
198
|
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"`.
|
|
199
|
+
|
|
199
200
|
.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import * as _$agents_client0 from "agents/client";
|
|
2
|
-
import * as _$ai from "ai";
|
|
3
|
-
import * as _$_ai_sdk_react0 from "@ai-sdk/react";
|
|
4
|
-
|
|
5
1
|
//#region ../../node_modules/partysocket/ws-Cg2f-sDL.d.ts
|
|
6
2
|
//#region src/type-helper.d.ts
|
|
7
3
|
type TypedEventTarget<EventMap extends object> = {
|
|
@@ -257,20 +253,24 @@ interface UseAgentOptions {
|
|
|
257
253
|
onClose?: (event: CloseEvent) => void;
|
|
258
254
|
onError?: (event: ErrorEvent) => void;
|
|
259
255
|
}
|
|
260
|
-
declare function useAgent(options: UseAgentOptions):
|
|
261
|
-
agent:
|
|
262
|
-
name: string;
|
|
263
|
-
path: ReadonlyArray<{
|
|
256
|
+
declare function useAgent(options: UseAgentOptions): {
|
|
257
|
+
agent: Omit<PartySocket, "path"> & {
|
|
264
258
|
agent: string;
|
|
265
259
|
name: string;
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
260
|
+
path: ReadonlyArray<{
|
|
261
|
+
agent: string;
|
|
262
|
+
name: string;
|
|
263
|
+
}>;
|
|
264
|
+
identified: boolean;
|
|
265
|
+
ready: Promise<void>;
|
|
266
|
+
state: AgentConnectionState | undefined;
|
|
267
|
+
setState: (state: AgentConnectionState) => void;
|
|
268
|
+
call: <T = unknown>(method: string, args?: unknown[], options?: import("agents/client").CallOptions | import("agents/client").StreamOptions) => Promise<T>;
|
|
269
|
+
stub: import("agents/client").UntypedAgentStub;
|
|
270
|
+
getHttpUrl: () => string;
|
|
271
|
+
};
|
|
272
|
+
status: AgentConnectionStatus;
|
|
273
|
+
type: AgentConnectionType | undefined;
|
|
274
274
|
};
|
|
275
275
|
//#endregion
|
|
276
276
|
//#region src/useChat.d.ts
|
|
@@ -292,11 +292,11 @@ declare function useChat<ToolContext extends Record<string, unknown>>(options: U
|
|
|
292
292
|
ready: Promise<void>;
|
|
293
293
|
state: AgentConnectionState | undefined;
|
|
294
294
|
setState: (state: AgentConnectionState) => void;
|
|
295
|
-
call: <T = unknown>(method: string, args?: unknown[], options?:
|
|
296
|
-
stub:
|
|
295
|
+
call: <T = unknown>(method: string, args?: unknown[], options?: import("agents/client").CallOptions | import("agents/client").StreamOptions) => Promise<T>;
|
|
296
|
+
stub: import("agents/client").UntypedAgentStub;
|
|
297
297
|
getHttpUrl: () => string;
|
|
298
298
|
};
|
|
299
|
-
chat: Omit<
|
|
299
|
+
chat: Omit<import("@ai-sdk/react").UseChatHelpers<import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>>, "addToolOutput"> & {
|
|
300
300
|
clearHistory: () => void;
|
|
301
301
|
addToolOutput: (opts: {
|
|
302
302
|
toolCallId: string;
|
|
@@ -315,7 +315,9 @@ declare function useChat<ToolContext extends Record<string, unknown>>(options: U
|
|
|
315
315
|
};
|
|
316
316
|
//#endregion
|
|
317
317
|
//#region src/useAssistant.d.ts
|
|
318
|
-
interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Record<string, unknown>> extends Omit<UseChatOptions<ToolContext>, "actorId" | "getInitialMessages"> {
|
|
318
|
+
interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Record<string, unknown>> extends Omit<UseChatOptions<ToolContext>, "actorId" | "getInitialMessages"> {
|
|
319
|
+
initialChatId?: string;
|
|
320
|
+
}
|
|
319
321
|
declare function useAssistant<ToolContext extends Record<string, unknown>>(options: UseAssistantOptions<ToolContext>): {
|
|
320
322
|
currentChatName: string | undefined;
|
|
321
323
|
chats: ChatSummary[];
|
|
@@ -339,11 +341,11 @@ declare function useAssistant<ToolContext extends Record<string, unknown>>(optio
|
|
|
339
341
|
ready: Promise<void>;
|
|
340
342
|
state: AgentConnectionState | undefined;
|
|
341
343
|
setState: (state: AgentConnectionState) => void;
|
|
342
|
-
call: <T = unknown>(method: string, args?: unknown[], options?:
|
|
343
|
-
stub:
|
|
344
|
+
call: <T = unknown>(method: string, args?: unknown[], options?: import("agents/client").CallOptions | import("agents/client").StreamOptions) => Promise<T>;
|
|
345
|
+
stub: import("agents/client").UntypedAgentStub;
|
|
344
346
|
getHttpUrl: () => string;
|
|
345
347
|
};
|
|
346
|
-
chat: Omit<
|
|
348
|
+
chat: Omit<import("@ai-sdk/react").UseChatHelpers<import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>>, "addToolOutput"> & {
|
|
347
349
|
clearHistory: () => void;
|
|
348
350
|
addToolOutput: (opts: {
|
|
349
351
|
toolCallId: string;
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useAgent as useAgent$1 } from "agents/react";
|
|
2
1
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { useAgent as useAgent$1 } from "agents/react";
|
|
3
3
|
import { useAgentChat } from "@cloudflare/ai-chat/react";
|
|
4
4
|
//#region src/util.ts
|
|
5
5
|
function getTokenArgs(authToken) {
|
|
@@ -13,27 +13,49 @@ function getTokenArgs(authToken) {
|
|
|
13
13
|
//#region src/useAgent.ts
|
|
14
14
|
function useAgent(options) {
|
|
15
15
|
const { enabled, host, agentName, actorId, name, sub, authToken, onError, onOpen, onClose } = options;
|
|
16
|
-
|
|
16
|
+
const [type, setType] = useState(void 0);
|
|
17
|
+
const [status, setStatus] = useState("connecting");
|
|
18
|
+
const agent = useAgent$1({
|
|
17
19
|
enabled,
|
|
18
20
|
host,
|
|
19
21
|
agent: agentName,
|
|
20
22
|
name: actorId ? `${actorId}:${name}` : name,
|
|
21
23
|
sub,
|
|
22
|
-
onOpen
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
onOpen: (event) => {
|
|
25
|
+
onOpen?.(event);
|
|
26
|
+
},
|
|
27
|
+
onClose: (event) => {
|
|
28
|
+
if (event.code >= 3e3) setStatus("unauthorized");
|
|
29
|
+
else setStatus("disconnected");
|
|
30
|
+
onClose?.(event);
|
|
31
|
+
},
|
|
32
|
+
onMessage: (event) => {
|
|
33
|
+
const msg = JSON.parse(event.data);
|
|
34
|
+
if (msg.type === "connection_status") {
|
|
35
|
+
if (msg.status === "unauthorized") agent.close(3e3);
|
|
36
|
+
setStatus(msg.status);
|
|
37
|
+
} else if (msg.type === "agent_type") setType(msg.agentType);
|
|
38
|
+
},
|
|
39
|
+
onError: (event) => {
|
|
40
|
+
setStatus("disconnected");
|
|
41
|
+
onError?.(event);
|
|
42
|
+
},
|
|
25
43
|
...getTokenArgs(authToken)
|
|
26
44
|
});
|
|
45
|
+
return {
|
|
46
|
+
agent,
|
|
47
|
+
status,
|
|
48
|
+
type
|
|
49
|
+
};
|
|
27
50
|
}
|
|
28
51
|
//#endregion
|
|
29
52
|
//#region src/useChat.ts
|
|
30
53
|
function useChat(options) {
|
|
31
54
|
const { toolContext, welcomeMessage, ...agentOptions } = options;
|
|
32
|
-
const agent = useAgent({
|
|
55
|
+
const { agent, status } = useAgent({
|
|
33
56
|
...agentOptions,
|
|
34
57
|
enabled: agentOptions.enabled ?? !!agentOptions.name
|
|
35
58
|
});
|
|
36
|
-
const status = agent.state?.status ?? "connecting";
|
|
37
59
|
const chat = useAgentChat({
|
|
38
60
|
agent,
|
|
39
61
|
body: toolContext ?? {},
|
|
@@ -78,10 +100,10 @@ function useChat(options) {
|
|
|
78
100
|
//#endregion
|
|
79
101
|
//#region src/useAssistant.ts
|
|
80
102
|
function useAssistant(options) {
|
|
81
|
-
const { toolContext, welcomeMessage, ...agentOptions } = options;
|
|
82
|
-
const [chatId, setChatId] = useState();
|
|
103
|
+
const { toolContext, welcomeMessage, initialChatId, ...agentOptions } = options;
|
|
104
|
+
const [chatId, setChatId] = useState(initialChatId);
|
|
83
105
|
const [chats, setChats] = useState([]);
|
|
84
|
-
const assistant = useAgent(agentOptions);
|
|
106
|
+
const { agent: assistant, status: assistantStatus } = useAgent(agentOptions);
|
|
85
107
|
const refreshChats = useCallback(async () => {
|
|
86
108
|
setChats((await assistant.call("getChats")).map((chat) => {
|
|
87
109
|
const c = {
|
|
@@ -92,7 +114,6 @@ function useAssistant(options) {
|
|
|
92
114
|
return c;
|
|
93
115
|
}));
|
|
94
116
|
}, [assistant]);
|
|
95
|
-
const assistantStatus = assistant.state?.status;
|
|
96
117
|
useEffect(() => {
|
|
97
118
|
if (assistantStatus === "connected") refreshChats().catch(console.error);
|
|
98
119
|
}, [assistantStatus, refreshChats]);
|
|
@@ -114,12 +135,14 @@ function useAssistant(options) {
|
|
|
114
135
|
setChatId(void 0);
|
|
115
136
|
await refreshChats();
|
|
116
137
|
};
|
|
117
|
-
const
|
|
138
|
+
const subAgentName = assistant.state?.subAgentName ?? "";
|
|
139
|
+
const isChatEnabled = !!chatId && !!subAgentName;
|
|
140
|
+
const isChatReady = isChatEnabled && assistant.state?.status === "connected";
|
|
118
141
|
const chat = useChat({
|
|
119
142
|
...agentOptions,
|
|
120
|
-
enabled:
|
|
121
|
-
sub:
|
|
122
|
-
agent:
|
|
143
|
+
enabled: isChatEnabled,
|
|
144
|
+
sub: isChatEnabled ? [{
|
|
145
|
+
agent: subAgentName,
|
|
123
146
|
name: chatId
|
|
124
147
|
}] : void 0,
|
|
125
148
|
getInitialMessages: isChatReady ? void 0 : null,
|
|
@@ -129,7 +152,7 @@ function useAssistant(options) {
|
|
|
129
152
|
return {
|
|
130
153
|
currentChatName: chatId,
|
|
131
154
|
chats,
|
|
132
|
-
status:
|
|
155
|
+
status: isChatEnabled && chat.status !== "connecting" ? chat.status : assistantStatus,
|
|
133
156
|
assistant: {
|
|
134
157
|
getChats,
|
|
135
158
|
createChat,
|
package/dist/v1.mjs
CHANGED