@economic/agents-react 0.1.0 → 0.2.1
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 +3 -0
- package/dist/index.mjs +43 -9
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -9,8 +9,10 @@ interface UseAIChatAgentOptions {
|
|
|
9
9
|
host: string;
|
|
10
10
|
basePath?: string;
|
|
11
11
|
chatId: string;
|
|
12
|
+
authToken?: string;
|
|
12
13
|
toolContext?: Record<string, unknown>;
|
|
13
14
|
connectionParams?: Record<string, string>;
|
|
15
|
+
welcomeMessage?: string;
|
|
14
16
|
onConnectionStatusChange?: (status: AgentConnectionStatus) => void;
|
|
15
17
|
onOpen?: (event: Event) => void;
|
|
16
18
|
onClose?: (event: CloseEvent) => void;
|
|
@@ -19,6 +21,7 @@ interface UseAIChatAgentOptions {
|
|
|
19
21
|
type UseAIChatAgentResult = {
|
|
20
22
|
agent: ReturnType<typeof useAgent>;
|
|
21
23
|
chat: ReturnType<typeof useAgentChat<unknown, UIMessage>>;
|
|
24
|
+
connectionStatus: AgentConnectionStatus;
|
|
22
25
|
};
|
|
23
26
|
declare function useAIChatAgent(options: UseAIChatAgentOptions): UseAIChatAgentResult;
|
|
24
27
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
import { useAgentChat } from "@cloudflare/ai-chat/react";
|
|
2
2
|
import { useAgent } from "agents/react";
|
|
3
|
+
import React from "react";
|
|
3
4
|
//#region src/index.ts
|
|
4
5
|
function useAIChatAgent(options) {
|
|
5
|
-
const { agent: agentName, host, basePath, chatId, toolContext, connectionParams, onConnectionStatusChange, onOpen: onOpenProp, onClose: onCloseProp, onError: onErrorProp } = options;
|
|
6
|
+
const { agent: agentName, host, basePath, chatId, authToken, toolContext, connectionParams, welcomeMessage, onConnectionStatusChange, onOpen: onOpenProp, onClose: onCloseProp, onError: onErrorProp } = options;
|
|
7
|
+
const [connectionStatus, setConnectionStatus] = React.useState("connecting");
|
|
8
|
+
const updateConnectionStatus = React.useCallback((status) => {
|
|
9
|
+
setConnectionStatus(status);
|
|
10
|
+
onConnectionStatusChange?.(status);
|
|
11
|
+
}, [onConnectionStatusChange]);
|
|
12
|
+
let headers = {};
|
|
13
|
+
let protocols = [];
|
|
14
|
+
if (authToken) {
|
|
15
|
+
headers["Authorization"] = `Bearer ${authToken}`;
|
|
16
|
+
protocols.push("bearer", authToken);
|
|
17
|
+
}
|
|
6
18
|
const agent = useAgent({
|
|
7
19
|
agent: agentName,
|
|
8
20
|
host,
|
|
@@ -11,27 +23,49 @@ function useAIChatAgent(options) {
|
|
|
11
23
|
query: connectionParams ?? {},
|
|
12
24
|
queryDeps: Object.keys(connectionParams ?? {}),
|
|
13
25
|
onOpen: (event) => {
|
|
14
|
-
|
|
26
|
+
updateConnectionStatus("connected");
|
|
15
27
|
onOpenProp?.(event);
|
|
16
28
|
},
|
|
17
29
|
onClose: (event) => {
|
|
18
30
|
if (event.code >= 3e3) {
|
|
19
|
-
|
|
31
|
+
updateConnectionStatus("unauthorized");
|
|
20
32
|
agent.close();
|
|
21
33
|
onCloseProp?.(event);
|
|
22
34
|
return;
|
|
23
35
|
}
|
|
24
|
-
|
|
36
|
+
updateConnectionStatus("disconnected");
|
|
25
37
|
onCloseProp?.(event);
|
|
26
38
|
},
|
|
27
|
-
onError: onErrorProp
|
|
39
|
+
onError: onErrorProp,
|
|
40
|
+
protocols
|
|
28
41
|
});
|
|
42
|
+
const chat = useAgentChat({
|
|
43
|
+
agent,
|
|
44
|
+
body: toolContext ?? {},
|
|
45
|
+
headers
|
|
46
|
+
});
|
|
47
|
+
const hasSentWelcome = React.useRef(false);
|
|
48
|
+
React.useEffect(() => {
|
|
49
|
+
if (welcomeMessage && connectionStatus === "connected" && chat.messages.length === 0 && !hasSentWelcome.current) {
|
|
50
|
+
hasSentWelcome.current = true;
|
|
51
|
+
chat.setMessages([{
|
|
52
|
+
id: "welcome-message",
|
|
53
|
+
role: "assistant",
|
|
54
|
+
parts: [{
|
|
55
|
+
type: "text",
|
|
56
|
+
text: welcomeMessage
|
|
57
|
+
}]
|
|
58
|
+
}]);
|
|
59
|
+
}
|
|
60
|
+
}, [
|
|
61
|
+
connectionStatus,
|
|
62
|
+
chat.messages.length,
|
|
63
|
+
welcomeMessage
|
|
64
|
+
]);
|
|
29
65
|
return {
|
|
30
66
|
agent,
|
|
31
|
-
chat
|
|
32
|
-
|
|
33
|
-
body: toolContext ?? {}
|
|
34
|
-
})
|
|
67
|
+
chat,
|
|
68
|
+
connectionStatus
|
|
35
69
|
};
|
|
36
70
|
}
|
|
37
71
|
//#endregion
|