@economic/agents-react 1.1.2 → 1.1.4

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 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?: (error: Event) => void;
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,12 +282,14 @@ 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
287
- /** Conversation summary returned by the assistant's `getConversations` RPC. */
288
- type Conversation = {
289
- durable_object_name: string;
290
+ /** Chat summary returned by the assistant's `getChats` RPC. */
291
+ type Chat = {
292
+ chatId: string;
290
293
  title?: string;
291
294
  summary?: string;
292
295
  created_at: number;
@@ -299,10 +302,10 @@ interface UseAssistantOptions<ToolContext extends Record<string, unknown> = Reco
299
302
  declare function useAssistant<ToolContext extends Record<string, unknown>>(options: UseAssistantOptions<ToolContext>): {
300
303
  chatId: string | undefined;
301
304
  status: AgentConnectionStatus;
302
- getConversations: () => Promise<Conversation[]>;
303
- createConversation: () => Promise<string>;
304
- openConversation: (chatId: string) => void;
305
- deleteConversation: (chatId: string) => Promise<void>;
305
+ getChats: () => Promise<Chat[]>;
306
+ createChat: () => Promise<string>;
307
+ openChat: (chatId: string) => void;
308
+ deleteChat: (chatId: string) => Promise<void>;
306
309
  agent: Omit<PartySocket, "path"> & {
307
310
  agent: string;
308
311
  name: string;
@@ -333,4 +336,4 @@ declare function useAssistant<ToolContext extends Record<string, unknown>>(optio
333
336
  }) | undefined;
334
337
  };
335
338
  //#endregion
336
- export { AgentConnectionState, AgentConnectionStatus, AgentConnectionType, Conversation, UseAgentOptions, UseAssistantOptions, UseChatOptions, useAgent, useAssistant, useChatAgent };
339
+ export { AgentConnectionState, AgentConnectionStatus, AgentConnectionType, Chat, 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 {};
@@ -36,22 +36,29 @@ function useAssistant(options) {
36
36
  id: userId,
37
37
  authToken
38
38
  });
39
- const getConversations = async () => {
40
- return await assistant.call("getConversations");
39
+ const getChats = async () => {
40
+ return (await assistant.call("getChats")).map((chat) => {
41
+ const c = {
42
+ ...chat,
43
+ chatId: chat.durable_object_name
44
+ };
45
+ delete c.durable_object_name;
46
+ return c;
47
+ });
41
48
  };
42
- const createConversation = async () => {
43
- const chatId = await assistant.call("createConversation");
49
+ const createChat = async () => {
50
+ const chatId = await assistant.call("createChat");
44
51
  setChatId(chatId);
45
- await getConversations();
52
+ await getChats();
46
53
  return chatId;
47
54
  };
48
- const openConversation = (chatId) => {
55
+ const openChat = (chatId) => {
49
56
  setChatId(chatId);
50
57
  };
51
- const deleteConversation = async (chatId) => {
52
- await assistant.call("deleteConversation", [chatId]);
58
+ const deleteChat = async (chatId) => {
59
+ await assistant.call("deleteChat", [chatId]);
53
60
  setChatId(void 0);
54
- await getConversations();
61
+ await getChats();
55
62
  };
56
63
  const agent = useAgent({
57
64
  enabled: !!chatId,
@@ -74,10 +81,10 @@ function useAssistant(options) {
74
81
  return {
75
82
  chatId,
76
83
  status: (chatId ? agent.state?.status : assistant.state?.status) ?? "connecting",
77
- getConversations,
78
- createConversation,
79
- openConversation,
80
- deleteConversation,
84
+ getChats,
85
+ createChat,
86
+ openChat,
87
+ deleteChat,
81
88
  agent,
82
89
  chat: isChatReady ? chat : void 0
83
90
  };
@@ -85,16 +92,47 @@ function useAssistant(options) {
85
92
  //#endregion
86
93
  //#region src/useChatAgent.ts
87
94
  function useChatAgent(options) {
88
- const { toolContext, ...agentOptions } = options;
95
+ const { toolContext, welcomeMessage, ...agentOptions } = options;
89
96
  const agent = useAgent(agentOptions);
90
97
  const chat = useAgentChat({
91
98
  agent,
92
99
  body: toolContext ?? {}
93
100
  });
101
+ const status = agent.state?.status ?? "connecting";
102
+ const hasSentWelcome = useRef(false);
103
+ useEffect(() => {
104
+ if (welcomeMessage && status === "connected" && chat.messages.length === 0 && !hasSentWelcome.current) {
105
+ hasSentWelcome.current = true;
106
+ chat.setMessages([{
107
+ id: "welcome-message",
108
+ role: "assistant",
109
+ parts: [{
110
+ type: "text",
111
+ text: welcomeMessage
112
+ }]
113
+ }]);
114
+ }
115
+ }, [
116
+ status,
117
+ chat.messages.length,
118
+ welcomeMessage
119
+ ]);
120
+ async function rateMessage(messageId, rating, comment) {
121
+ return agent.call("rateMessage", [
122
+ messageId,
123
+ rating,
124
+ comment
125
+ ]);
126
+ }
127
+ async function getMessageRatings() {
128
+ return agent.call("getMessageRatings");
129
+ }
94
130
  return {
95
- status: agent.state?.status ?? "connecting",
131
+ status,
96
132
  agent,
97
- chat
133
+ chat,
134
+ rateMessage,
135
+ getMessageRatings
98
136
  };
99
137
  }
100
138
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@economic/agents-react",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist"