@economic/agents-react 1.1.1 → 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 +39 -40
- package/dist/index.mjs +52 -20
- 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
|
*/
|
|
@@ -219,13 +219,17 @@ type AgentConnectionState = {
|
|
|
219
219
|
interface UseAgentOptions {
|
|
220
220
|
host: string;
|
|
221
221
|
agentName: string;
|
|
222
|
-
id: string;
|
|
223
222
|
enabled?: boolean;
|
|
224
223
|
sub?: {
|
|
225
224
|
agent: string;
|
|
226
225
|
name: string;
|
|
227
226
|
}[];
|
|
227
|
+
onOpen?: (event: Event) => void;
|
|
228
|
+
onClose?: (event: CloseEvent) => void;
|
|
229
|
+
onError?: (event: ErrorEvent) => void;
|
|
228
230
|
authToken?: string;
|
|
231
|
+
userId?: string;
|
|
232
|
+
id: string;
|
|
229
233
|
}
|
|
230
234
|
declare function useAgent<T extends AgentConnectionState>(options: UseAgentOptions): Omit<PartySocket, "path"> & {
|
|
231
235
|
agent: string;
|
|
@@ -243,30 +247,13 @@ declare function useAgent<T extends AgentConnectionState>(options: UseAgentOptio
|
|
|
243
247
|
getHttpUrl: () => string;
|
|
244
248
|
};
|
|
245
249
|
//#endregion
|
|
246
|
-
//#region src/
|
|
247
|
-
|
|
248
|
-
type Conversation = {
|
|
249
|
-
durable_object_name: string;
|
|
250
|
-
title?: string;
|
|
251
|
-
summary?: string;
|
|
252
|
-
created_at: number;
|
|
253
|
-
updated_at: number;
|
|
254
|
-
};
|
|
255
|
-
interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Record<string, unknown>> {
|
|
256
|
-
host: string;
|
|
257
|
-
assistantName: string;
|
|
258
|
-
userId: string;
|
|
259
|
-
chatId?: string;
|
|
260
|
-
authToken?: string;
|
|
250
|
+
//#region src/useChatAgent.d.ts
|
|
251
|
+
interface UseChatOptions<ToolContext extends Record<string, unknown>> extends UseAgentOptions {
|
|
261
252
|
toolContext?: ToolContext;
|
|
253
|
+
welcomeMessage?: string;
|
|
262
254
|
}
|
|
263
|
-
declare function
|
|
264
|
-
chatId: string | undefined;
|
|
255
|
+
declare function useChatAgent<ToolContext extends Record<string, unknown>>(options: UseChatOptions<ToolContext>): {
|
|
265
256
|
status: AgentConnectionStatus;
|
|
266
|
-
getConversations: () => Promise<Conversation[]>;
|
|
267
|
-
createConversation: () => Promise<string>;
|
|
268
|
-
openConversation: (chatId: string) => void;
|
|
269
|
-
deleteConversation: (chatId: string) => Promise<void>;
|
|
270
257
|
agent: Omit<PartySocket, "path"> & {
|
|
271
258
|
agent: string;
|
|
272
259
|
name: string;
|
|
@@ -282,7 +269,7 @@ declare function useAssistant<ToolContext extends Record<string, unknown>>(optio
|
|
|
282
269
|
stub: _$agents_client0.UntypedAgentStub;
|
|
283
270
|
getHttpUrl: () => string;
|
|
284
271
|
};
|
|
285
|
-
chat:
|
|
272
|
+
chat: Omit<_$_ai_sdk_react0.UseChatHelpers<_$ai.UIMessage<unknown, _$ai.UIDataTypes, _$ai.UITools>>, "addToolOutput"> & {
|
|
286
273
|
clearHistory: () => void;
|
|
287
274
|
addToolOutput: (opts: {
|
|
288
275
|
toolCallId: string;
|
|
@@ -294,19 +281,31 @@ declare function useAssistant<ToolContext extends Record<string, unknown>>(optio
|
|
|
294
281
|
isServerStreaming: boolean;
|
|
295
282
|
isStreaming: boolean;
|
|
296
283
|
isToolContinuation: boolean;
|
|
297
|
-
}
|
|
284
|
+
};
|
|
285
|
+
rateMessage: (messageId: string, rating: number, comment?: string) => Promise<unknown>;
|
|
286
|
+
getMessageRatings: () => Promise<unknown>;
|
|
298
287
|
};
|
|
299
288
|
//#endregion
|
|
300
|
-
//#region src/
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
289
|
+
//#region src/useAssistant.d.ts
|
|
290
|
+
/** Conversation summary returned by the assistant's `getConversations` RPC. */
|
|
291
|
+
type Conversation = {
|
|
292
|
+
durable_object_name: string;
|
|
293
|
+
title?: string;
|
|
294
|
+
summary?: string;
|
|
295
|
+
created_at: number;
|
|
296
|
+
updated_at: number;
|
|
297
|
+
};
|
|
298
|
+
interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Record<string, unknown>> extends Omit<UseChatOptions<ToolContext>, "id" | "userId"> {
|
|
299
|
+
userId: string;
|
|
300
|
+
initialChatId?: string;
|
|
307
301
|
}
|
|
308
|
-
declare function
|
|
302
|
+
declare function useAssistant<ToolContext extends Record<string, unknown>>(options: UseAssistantOptions<ToolContext>): {
|
|
303
|
+
chatId: string | undefined;
|
|
309
304
|
status: AgentConnectionStatus;
|
|
305
|
+
getConversations: () => Promise<Conversation[]>;
|
|
306
|
+
createConversation: () => Promise<string>;
|
|
307
|
+
openConversation: (chatId: string) => void;
|
|
308
|
+
deleteConversation: (chatId: string) => Promise<void>;
|
|
310
309
|
agent: Omit<PartySocket, "path"> & {
|
|
311
310
|
agent: string;
|
|
312
311
|
name: string;
|
|
@@ -322,7 +321,7 @@ declare function useChatAgent<ToolContext extends Record<string, unknown>>(optio
|
|
|
322
321
|
stub: _$agents_client0.UntypedAgentStub;
|
|
323
322
|
getHttpUrl: () => string;
|
|
324
323
|
};
|
|
325
|
-
chat: Omit<_$_ai_sdk_react0.UseChatHelpers<_$ai.UIMessage<unknown, _$ai.UIDataTypes, _$ai.UITools>>, "addToolOutput"> & {
|
|
324
|
+
chat: (Omit<_$_ai_sdk_react0.UseChatHelpers<_$ai.UIMessage<unknown, _$ai.UIDataTypes, _$ai.UITools>>, "addToolOutput"> & {
|
|
326
325
|
clearHistory: () => void;
|
|
327
326
|
addToolOutput: (opts: {
|
|
328
327
|
toolCallId: string;
|
|
@@ -334,7 +333,7 @@ declare function useChatAgent<ToolContext extends Record<string, unknown>>(optio
|
|
|
334
333
|
isServerStreaming: boolean;
|
|
335
334
|
isStreaming: boolean;
|
|
336
335
|
isToolContinuation: boolean;
|
|
337
|
-
};
|
|
336
|
+
}) | undefined;
|
|
338
337
|
};
|
|
339
338
|
//#endregion
|
|
340
339
|
export { AgentConnectionState, AgentConnectionStatus, AgentConnectionType, Conversation, UseAgentOptions, UseAssistantOptions, UseChatOptions, useAgent, useAssistant, useChatAgent };
|
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 {};
|
|
@@ -12,28 +12,30 @@ function getTokenArgs(authToken) {
|
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/useAgent.ts
|
|
14
14
|
function useAgent(options) {
|
|
15
|
-
const { host, agentName, id, enabled, sub, authToken } = options;
|
|
15
|
+
const { host, agentName, userId, id, enabled, sub, authToken, onError, onOpen, onClose } = options;
|
|
16
16
|
return useAgent$1({
|
|
17
17
|
enabled,
|
|
18
18
|
host,
|
|
19
19
|
agent: agentName,
|
|
20
|
-
name: id,
|
|
20
|
+
name: userId ? `${userId}:${id}` : id,
|
|
21
21
|
sub,
|
|
22
|
+
onOpen,
|
|
23
|
+
onClose,
|
|
24
|
+
onError,
|
|
22
25
|
...getTokenArgs(authToken)
|
|
23
26
|
});
|
|
24
27
|
}
|
|
25
28
|
//#endregion
|
|
26
29
|
//#region src/useAssistant.ts
|
|
27
30
|
function useAssistant(options) {
|
|
28
|
-
const { host,
|
|
29
|
-
const
|
|
31
|
+
const { host, agentName, userId, authToken, toolContext, ...agentOptions } = options;
|
|
32
|
+
const [chatId, setChatId] = useState();
|
|
33
|
+
const assistant = useAgent({
|
|
30
34
|
host,
|
|
31
|
-
agentName
|
|
35
|
+
agentName,
|
|
32
36
|
id: userId,
|
|
33
37
|
authToken
|
|
34
|
-
};
|
|
35
|
-
const [chatId, setChatId] = useState(initialChatId);
|
|
36
|
-
const assistant = useAgent(config);
|
|
38
|
+
});
|
|
37
39
|
const getConversations = async () => {
|
|
38
40
|
return await assistant.call("getConversations");
|
|
39
41
|
};
|
|
@@ -52,12 +54,16 @@ function useAssistant(options) {
|
|
|
52
54
|
await getConversations();
|
|
53
55
|
};
|
|
54
56
|
const agent = useAgent({
|
|
55
|
-
...config,
|
|
56
57
|
enabled: !!chatId,
|
|
58
|
+
host,
|
|
59
|
+
agentName,
|
|
60
|
+
id: userId,
|
|
61
|
+
authToken,
|
|
57
62
|
sub: chatId ? [{
|
|
58
63
|
agent: assistant.state?.subAgentName ?? "",
|
|
59
64
|
name: chatId
|
|
60
|
-
}] : void 0
|
|
65
|
+
}] : void 0,
|
|
66
|
+
...agentOptions
|
|
61
67
|
});
|
|
62
68
|
const isChatReady = !!chatId && agent.state?.status === "connected";
|
|
63
69
|
const chat = useAgentChat({
|
|
@@ -79,21 +85,47 @@ function useAssistant(options) {
|
|
|
79
85
|
//#endregion
|
|
80
86
|
//#region src/useChatAgent.ts
|
|
81
87
|
function useChatAgent(options) {
|
|
82
|
-
const {
|
|
83
|
-
const agent = useAgent(
|
|
84
|
-
host,
|
|
85
|
-
agentName,
|
|
86
|
-
id,
|
|
87
|
-
authToken
|
|
88
|
-
});
|
|
88
|
+
const { toolContext, welcomeMessage, ...agentOptions } = options;
|
|
89
|
+
const agent = useAgent(agentOptions);
|
|
89
90
|
const chat = useAgentChat({
|
|
90
91
|
agent,
|
|
91
92
|
body: toolContext ?? {}
|
|
92
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
|
+
}
|
|
93
123
|
return {
|
|
94
|
-
status
|
|
124
|
+
status,
|
|
95
125
|
agent,
|
|
96
|
-
chat
|
|
126
|
+
chat,
|
|
127
|
+
rateMessage,
|
|
128
|
+
getMessageRatings
|
|
97
129
|
};
|
|
98
130
|
}
|
|
99
131
|
//#endregion
|