@@ -21,7 +21,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
21
21
* Map of message `id`s to `AbortController`s
22
22
* useful to propagate request cancellation signals for any external calls made by the agent
23
23
*/
24
- #chatMessageAbortControllers : Map < string , AbortController > ;
24
+ private _chatMessageAbortControllers : Map < string , AbortController > ;
25
25
/** Array of chat messages for the current conversation */
26
26
messages : ChatMessage [ ] ;
27
27
constructor ( ctx : AgentContext , env : Env ) {
@@ -37,10 +37,10 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
37
37
return JSON . parse ( row . message as string ) ;
38
38
} ) ;
39
39
40
- this . #chatMessageAbortControllers = new Map ( ) ;
40
+ this . _chatMessageAbortControllers = new Map ( ) ;
41
41
}
42
42
43
- #broadcastChatMessage ( message : OutgoingMessage , exclude ?: string [ ] ) {
43
+ private _broadcastChatMessage ( message : OutgoingMessage , exclude ?: string [ ] ) {
44
44
this . broadcast ( JSON . stringify ( message ) , exclude ) ;
45
45
}
46
46
@@ -74,7 +74,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
74
74
// duplex
75
75
} = data . init ;
76
76
const { messages } = JSON . parse ( body as string ) ;
77
- this . #broadcastChatMessage (
77
+ this . _broadcastChatMessage (
78
78
{
79
79
type : "cf_agent_chat_messages" ,
80
80
messages,
@@ -84,9 +84,9 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
84
84
await this . persistMessages ( messages , [ connection . id ] ) ;
85
85
86
86
const chatMessageId = data . id ;
87
- const abortSignal = this . #getAbortSignal ( chatMessageId ) ;
87
+ const abortSignal = this . _getAbortSignal ( chatMessageId ) ;
88
88
89
- return this . #tryCatch ( async ( ) => {
89
+ return this . _tryCatchChat ( async ( ) => {
90
90
const response = await this . onChatMessage (
91
91
async ( { response } ) => {
92
92
const finalMessages = appendResponseMessages ( {
@@ -95,21 +95,21 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
95
95
} ) ;
96
96
97
97
await this . persistMessages ( finalMessages , [ connection . id ] ) ;
98
- this . #removeAbortController ( chatMessageId ) ;
98
+ this . _removeAbortController ( chatMessageId ) ;
99
99
} ,
100
100
abortSignal ? { abortSignal } : undefined
101
101
) ;
102
102
103
103
if ( response ) {
104
- await this . #reply ( data . id , response ) ;
104
+ await this . _reply ( data . id , response ) ;
105
105
}
106
106
} ) ;
107
107
}
108
108
if ( data . type === "cf_agent_chat_clear" ) {
109
- this . #destroyAbortControllers ( ) ;
109
+ this . _destroyAbortControllers ( ) ;
110
110
this . sql `delete from cf_ai_chat_agent_messages` ;
111
111
this . messages = [ ] ;
112
- this . #broadcastChatMessage (
112
+ this . _broadcastChatMessage (
113
113
{
114
114
type : "cf_agent_chat_clear" ,
115
115
} ,
@@ -120,13 +120,13 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
120
120
await this . persistMessages ( data . messages , [ connection . id ] ) ;
121
121
} else if ( data . type === "cf_agent_chat_request_cancel" ) {
122
122
// propagate an abort signal for the associated request
123
- this . #cancelChatRequest ( data . id ) ;
123
+ this . _cancelChatRequest ( data . id ) ;
124
124
}
125
125
}
126
126
}
127
127
128
128
override async onRequest ( request : Request ) : Promise < Response > {
129
- return this . #tryCatch ( ( ) => {
129
+ return this . _tryCatchChat ( ( ) => {
130
130
const url = new URL ( request . url ) ;
131
131
if ( url . pathname . endsWith ( "/get-messages" ) ) {
132
132
const messages = (
@@ -140,7 +140,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
140
140
} ) ;
141
141
}
142
142
143
- async #tryCatch < T > ( fn : ( ) => T | Promise < T > ) {
143
+ private async _tryCatchChat < T > ( fn : ( ) => T | Promise < T > ) {
144
144
try {
145
145
return await fn ( ) ;
146
146
} catch ( e ) {
@@ -198,7 +198,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
198
198
} ,${ JSON . stringify ( message ) } )`;
199
199
}
200
200
this . messages = messages ;
201
- this . #broadcastChatMessage (
201
+ this . _broadcastChatMessage (
202
202
{
203
203
type : "cf_agent_chat_messages" ,
204
204
messages : messages ,
@@ -207,22 +207,22 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
207
207
) ;
208
208
}
209
209
210
- async #reply ( id : string , response : Response ) {
210
+ private async _reply ( id : string , response : Response ) {
211
211
// now take chunks out from dataStreamResponse and send them to the client
212
- return this . #tryCatch ( async ( ) => {
212
+ return this . _tryCatchChat ( async ( ) => {
213
213
// @ts -expect-error TODO: fix this type error
214
214
for await ( const chunk of response . body ! ) {
215
215
const body = decoder . decode ( chunk ) ;
216
216
217
- this . #broadcastChatMessage ( {
217
+ this . _broadcastChatMessage ( {
218
218
id,
219
219
type : "cf_agent_use_chat_response" ,
220
220
body,
221
221
done : false ,
222
222
} ) ;
223
223
}
224
224
225
- this . #broadcastChatMessage ( {
225
+ this . _broadcastChatMessage ( {
226
226
id,
227
227
type : "cf_agent_use_chat_response" ,
228
228
body : "" ,
@@ -237,51 +237,51 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
237
237
*
238
238
* returns the AbortSignal associated with the AbortController
239
239
*/
240
- #getAbortSignal ( id : string ) : AbortSignal | undefined {
240
+ private _getAbortSignal ( id : string ) : AbortSignal | undefined {
241
241
// Defensive check, since we're coercing message types at the moment
242
242
if ( typeof id !== "string" ) {
243
243
return undefined ;
244
244
}
245
245
246
- if ( ! this . #chatMessageAbortControllers . has ( id ) ) {
247
- this . #chatMessageAbortControllers . set ( id , new AbortController ( ) ) ;
246
+ if ( ! this . _chatMessageAbortControllers . has ( id ) ) {
247
+ this . _chatMessageAbortControllers . set ( id , new AbortController ( ) ) ;
248
248
}
249
249
250
- return this . #chatMessageAbortControllers . get ( id ) ?. signal ;
250
+ return this . _chatMessageAbortControllers . get ( id ) ?. signal ;
251
251
}
252
252
253
253
/**
254
254
* Remove an abort controller from the cache of pending message responses
255
255
*/
256
- #removeAbortController ( id : string ) {
257
- this . #chatMessageAbortControllers . delete ( id ) ;
256
+ private _removeAbortController ( id : string ) {
257
+ this . _chatMessageAbortControllers . delete ( id ) ;
258
258
}
259
259
260
260
/**
261
261
* Propagate an abort signal for any requests associated with the given message id
262
262
*/
263
- #cancelChatRequest ( id : string ) {
264
- if ( this . #chatMessageAbortControllers . has ( id ) ) {
265
- const abortController = this . #chatMessageAbortControllers . get ( id ) ;
263
+ private _cancelChatRequest ( id : string ) {
264
+ if ( this . _chatMessageAbortControllers . has ( id ) ) {
265
+ const abortController = this . _chatMessageAbortControllers . get ( id ) ;
266
266
abortController ?. abort ( ) ;
267
267
}
268
268
}
269
269
270
270
/**
271
271
* Abort all pending requests and clear the cache of AbortControllers
272
272
*/
273
- #destroyAbortControllers ( ) {
274
- for ( const controller of this . #chatMessageAbortControllers . values ( ) ) {
273
+ private _destroyAbortControllers ( ) {
274
+ for ( const controller of this . _chatMessageAbortControllers . values ( ) ) {
275
275
controller ?. abort ( ) ;
276
276
}
277
- this . #chatMessageAbortControllers . clear ( ) ;
277
+ this . _chatMessageAbortControllers . clear ( ) ;
278
278
}
279
279
280
280
/**
281
281
* When the DO is destroyed, cancel all pending requests
282
282
*/
283
283
async destroy ( ) {
284
- this . #destroyAbortControllers ( ) ;
284
+ this . _destroyAbortControllers ( ) ;
285
285
await super . destroy ( ) ;
286
286
}
287
287
}
0 commit comments