@economic/agents-react 1.1.2 → 1.1.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/dist/index.d.mts +12 -9
- package/dist/index.mjs +35 -4
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -14,20 +14,20 @@ interface IntermediateEventTarget<EventMap> extends EventTarget {
|
|
|
14
14
|
removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void;
|
|
15
15
|
} //#endregion
|
|
16
16
|
//#region src/ws.d.ts
|
|
17
|
-
declare class ErrorEvent extends Event {
|
|
17
|
+
declare class ErrorEvent$1 extends Event {
|
|
18
18
|
message: string;
|
|
19
19
|
error: Error;
|
|
20
20
|
constructor(error: Error, target: any);
|
|
21
21
|
}
|
|
22
|
-
declare class CloseEvent extends Event {
|
|
22
|
+
declare class CloseEvent$1 extends Event {
|
|
23
23
|
code: number;
|
|
24
24
|
reason: string;
|
|
25
25
|
wasClean: boolean;
|
|
26
26
|
constructor(code: number | undefined, reason: string | undefined, target: any);
|
|
27
27
|
}
|
|
28
28
|
interface WebSocketEventMap {
|
|
29
|
-
close: CloseEvent;
|
|
30
|
-
error: ErrorEvent;
|
|
29
|
+
close: CloseEvent$1;
|
|
30
|
+
error: ErrorEvent$1;
|
|
31
31
|
message: MessageEvent;
|
|
32
32
|
open: Event;
|
|
33
33
|
}
|
|
@@ -110,11 +110,11 @@ declare class ReconnectingWebSocket extends ReconnectingWebSocket_base {
|
|
|
110
110
|
/**
|
|
111
111
|
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
|
|
112
112
|
*/
|
|
113
|
-
onclose: ((event: CloseEvent) => void) | null;
|
|
113
|
+
onclose: ((event: CloseEvent$1) => void) | null;
|
|
114
114
|
/**
|
|
115
115
|
* An event listener to be called when an error occurs
|
|
116
116
|
*/
|
|
117
|
-
onerror: ((event: ErrorEvent) => void) | null;
|
|
117
|
+
onerror: ((event: ErrorEvent$1) => void) | null;
|
|
118
118
|
/**
|
|
119
119
|
* An event listener to be called when a message is received from the server
|
|
120
120
|
*/
|
|
@@ -224,9 +224,9 @@ interface UseAgentOptions {
|
|
|
224
224
|
agent: string;
|
|
225
225
|
name: string;
|
|
226
226
|
}[];
|
|
227
|
-
onOpen?: () => void;
|
|
228
|
-
onClose?: () => void;
|
|
229
|
-
onError?: (
|
|
227
|
+
onOpen?: (event: Event) => void;
|
|
228
|
+
onClose?: (event: CloseEvent) => void;
|
|
229
|
+
onError?: (event: ErrorEvent) => void;
|
|
230
230
|
authToken?: string;
|
|
231
231
|
userId?: string;
|
|
232
232
|
id: string;
|
|
@@ -250,6 +250,7 @@ declare function useAgent<T extends AgentConnectionState>(options: UseAgentOptio
|
|
|
250
250
|
//#region src/useChatAgent.d.ts
|
|
251
251
|
interface UseChatOptions<ToolContext extends Record<string, unknown>> extends UseAgentOptions {
|
|
252
252
|
toolContext?: ToolContext;
|
|
253
|
+
welcomeMessage?: string;
|
|
253
254
|
}
|
|
254
255
|
declare function useChatAgent<ToolContext extends Record<string, unknown>>(options: UseChatOptions<ToolContext>): {
|
|
255
256
|
status: AgentConnectionStatus;
|
|
@@ -281,6 +282,8 @@ declare function useChatAgent<ToolContext extends Record<string, unknown>>(optio
|
|
|
281
282
|
isStreaming: boolean;
|
|
282
283
|
isToolContinuation: boolean;
|
|
283
284
|
};
|
|
285
|
+
rateMessage: (messageId: string, rating: number, comment?: string) => Promise<unknown>;
|
|
286
|
+
getMessageRatings: () => Promise<unknown>;
|
|
284
287
|
};
|
|
285
288
|
//#endregion
|
|
286
289
|
//#region src/useAssistant.d.ts
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useAgent as useAgent$1 } from "agents/react";
|
|
2
2
|
import { useAgentChat } from "@cloudflare/ai-chat/react";
|
|
3
|
-
import { useState } from "react";
|
|
3
|
+
import { useEffect, useRef, useState } from "react";
|
|
4
4
|
//#region src/util.ts
|
|
5
5
|
function getTokenArgs(authToken) {
|
|
6
6
|
if (!authToken) return {};
|
|
@@ -85,16 +85,47 @@ function useAssistant(options) {
|
|
|
85
85
|
//#endregion
|
|
86
86
|
//#region src/useChatAgent.ts
|
|
87
87
|
function useChatAgent(options) {
|
|
88
|
-
const { toolContext, ...agentOptions } = options;
|
|
88
|
+
const { toolContext, welcomeMessage, ...agentOptions } = options;
|
|
89
89
|
const agent = useAgent(agentOptions);
|
|
90
90
|
const chat = useAgentChat({
|
|
91
91
|
agent,
|
|
92
92
|
body: toolContext ?? {}
|
|
93
93
|
});
|
|
94
|
+
const status = agent.state?.status ?? "connecting";
|
|
95
|
+
const hasSentWelcome = useRef(false);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
if (welcomeMessage && status === "connected" && chat.messages.length === 0 && !hasSentWelcome.current) {
|
|
98
|
+
hasSentWelcome.current = true;
|
|
99
|
+
chat.setMessages([{
|
|
100
|
+
id: "welcome-message",
|
|
101
|
+
role: "assistant",
|
|
102
|
+
parts: [{
|
|
103
|
+
type: "text",
|
|
104
|
+
text: welcomeMessage
|
|
105
|
+
}]
|
|
106
|
+
}]);
|
|
107
|
+
}
|
|
108
|
+
}, [
|
|
109
|
+
status,
|
|
110
|
+
chat.messages.length,
|
|
111
|
+
welcomeMessage
|
|
112
|
+
]);
|
|
113
|
+
async function rateMessage(messageId, rating, comment) {
|
|
114
|
+
return agent.call("rateMessage", [
|
|
115
|
+
messageId,
|
|
116
|
+
rating,
|
|
117
|
+
comment
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
async function getMessageRatings() {
|
|
121
|
+
return agent.call("getMessageRatings");
|
|
122
|
+
}
|
|
94
123
|
return {
|
|
95
|
-
status
|
|
124
|
+
status,
|
|
96
125
|
agent,
|
|
97
|
-
chat
|
|
126
|
+
chat,
|
|
127
|
+
rateMessage,
|
|
128
|
+
getMessageRatings
|
|
98
129
|
};
|
|
99
130
|
}
|
|
100
131
|
//#endregion
|