Skip to content

Commit e61aef9

Browse files
committed
Merge remote-tracking branch 'origin/main' into enhance-instrumentation
2 parents 51ea2e7 + 7f84d28 commit e61aef9

File tree

11 files changed

+292
-221
lines changed

11 files changed

+292
-221
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/agents/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# @cloudflare/agents
22

3+
## 0.0.82
4+
5+
### Patch Changes
6+
7+
- [`04d925e`](https://github.com/cloudflare/agents/commit/04d925ee6795b907de19bcd40940062fb9e99b1b) Thanks [@threepointone](https://github.com/threepointone)! - convert two missed #methods to a private \_methods
8+
9+
## 0.0.81
10+
11+
### Patch Changes
12+
13+
- [#265](https://github.com/cloudflare/agents/pull/265) [`ac0e999`](https://github.com/cloudflare/agents/commit/ac0e999652919600f087f0314ce61c98d3eaf069) Thanks [@threepointone](https://github.com/threepointone)! - refactor #method/#property to private method/private property
14+
15+
- [#267](https://github.com/cloudflare/agents/pull/267) [`385f0b2`](https://github.com/cloudflare/agents/commit/385f0b29c716f8fa1c9719b0c68e5c830767953e) Thanks [@threepointone](https://github.com/threepointone)! - prefix private methods/properties with \_
16+
317
## 0.0.80
418

519
### Patch Changes

packages/agents/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "agents",
3-
"version": "0.0.80",
3+
"version": "0.0.82",
44
"main": "src/index.ts",
55
"types": "dist/index.d.ts",
66
"type": "module",

packages/agents/src/ai-chat-agent.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
2121
* Map of message `id`s to `AbortController`s
2222
* useful to propagate request cancellation signals for any external calls made by the agent
2323
*/
24-
#chatMessageAbortControllers: Map<string, AbortController>;
24+
private _chatMessageAbortControllers: Map<string, AbortController>;
2525
/** Array of chat messages for the current conversation */
2626
messages: ChatMessage[];
2727
constructor(ctx: AgentContext, env: Env) {
@@ -37,10 +37,10 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
3737
return JSON.parse(row.message as string);
3838
});
3939

40-
this.#chatMessageAbortControllers = new Map();
40+
this._chatMessageAbortControllers = new Map();
4141
}
4242

43-
#broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {
43+
private _broadcastChatMessage(message: OutgoingMessage, exclude?: string[]) {
4444
this.broadcast(JSON.stringify(message), exclude);
4545
}
4646

@@ -74,7 +74,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
7474
// duplex
7575
} = data.init;
7676
const { messages } = JSON.parse(body as string);
77-
this.#broadcastChatMessage(
77+
this._broadcastChatMessage(
7878
{
7979
type: "cf_agent_chat_messages",
8080
messages,
@@ -84,9 +84,9 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
8484
await this.persistMessages(messages, [connection.id]);
8585

8686
const chatMessageId = data.id;
87-
const abortSignal = this.#getAbortSignal(chatMessageId);
87+
const abortSignal = this._getAbortSignal(chatMessageId);
8888

89-
return this.#tryCatch(async () => {
89+
return this._tryCatchChat(async () => {
9090
const response = await this.onChatMessage(
9191
async ({ response }) => {
9292
const finalMessages = appendResponseMessages({
@@ -95,21 +95,21 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
9595
});
9696

9797
await this.persistMessages(finalMessages, [connection.id]);
98-
this.#removeAbortController(chatMessageId);
98+
this._removeAbortController(chatMessageId);
9999
},
100100
abortSignal ? { abortSignal } : undefined
101101
);
102102

103103
if (response) {
104-
await this.#reply(data.id, response);
104+
await this._reply(data.id, response);
105105
}
106106
});
107107
}
108108
if (data.type === "cf_agent_chat_clear") {
109-
this.#destroyAbortControllers();
109+
this._destroyAbortControllers();
110110
this.sql`delete from cf_ai_chat_agent_messages`;
111111
this.messages = [];
112-
this.#broadcastChatMessage(
112+
this._broadcastChatMessage(
113113
{
114114
type: "cf_agent_chat_clear",
115115
},
@@ -120,13 +120,13 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
120120
await this.persistMessages(data.messages, [connection.id]);
121121
} else if (data.type === "cf_agent_chat_request_cancel") {
122122
// propagate an abort signal for the associated request
123-
this.#cancelChatRequest(data.id);
123+
this._cancelChatRequest(data.id);
124124
}
125125
}
126126
}
127127

128128
override async onRequest(request: Request): Promise<Response> {
129-
return this.#tryCatch(() => {
129+
return this._tryCatchChat(() => {
130130
const url = new URL(request.url);
131131
if (url.pathname.endsWith("/get-messages")) {
132132
const messages = (
@@ -140,7 +140,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
140140
});
141141
}
142142

143-
async #tryCatch<T>(fn: () => T | Promise<T>) {
143+
private async _tryCatchChat<T>(fn: () => T | Promise<T>) {
144144
try {
145145
return await fn();
146146
} catch (e) {
@@ -198,7 +198,7 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
198198
},${JSON.stringify(message)})`;
199199
}
200200
this.messages = messages;
201-
this.#broadcastChatMessage(
201+
this._broadcastChatMessage(
202202
{
203203
type: "cf_agent_chat_messages",
204204
messages: messages,
@@ -207,22 +207,22 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
207207
);
208208
}
209209

210-
async #reply(id: string, response: Response) {
210+
private async _reply(id: string, response: Response) {
211211
// now take chunks out from dataStreamResponse and send them to the client
212-
return this.#tryCatch(async () => {
212+
return this._tryCatchChat(async () => {
213213
// @ts-expect-error TODO: fix this type error
214214
for await (const chunk of response.body!) {
215215
const body = decoder.decode(chunk);
216216

217-
this.#broadcastChatMessage({
217+
this._broadcastChatMessage({
218218
id,
219219
type: "cf_agent_use_chat_response",
220220
body,
221221
done: false,
222222
});
223223
}
224224

225-
this.#broadcastChatMessage({
225+
this._broadcastChatMessage({
226226
id,
227227
type: "cf_agent_use_chat_response",
228228
body: "",
@@ -237,51 +237,51 @@ export class AIChatAgent<Env = unknown, State = unknown> extends Agent<
237237
*
238238
* returns the AbortSignal associated with the AbortController
239239
*/
240-
#getAbortSignal(id: string): AbortSignal | undefined {
240+
private _getAbortSignal(id: string): AbortSignal | undefined {
241241
// Defensive check, since we're coercing message types at the moment
242242
if (typeof id !== "string") {
243243
return undefined;
244244
}
245245

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());
248248
}
249249

250-
return this.#chatMessageAbortControllers.get(id)?.signal;
250+
return this._chatMessageAbortControllers.get(id)?.signal;
251251
}
252252

253253
/**
254254
* Remove an abort controller from the cache of pending message responses
255255
*/
256-
#removeAbortController(id: string) {
257-
this.#chatMessageAbortControllers.delete(id);
256+
private _removeAbortController(id: string) {
257+
this._chatMessageAbortControllers.delete(id);
258258
}
259259

260260
/**
261261
* Propagate an abort signal for any requests associated with the given message id
262262
*/
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);
266266
abortController?.abort();
267267
}
268268
}
269269

270270
/**
271271
* Abort all pending requests and clear the cache of AbortControllers
272272
*/
273-
#destroyAbortControllers() {
274-
for (const controller of this.#chatMessageAbortControllers.values()) {
273+
private _destroyAbortControllers() {
274+
for (const controller of this._chatMessageAbortControllers.values()) {
275275
controller?.abort();
276276
}
277-
this.#chatMessageAbortControllers.clear();
277+
this._chatMessageAbortControllers.clear();
278278
}
279279

280280
/**
281281
* When the DO is destroyed, cancel all pending requests
282282
*/
283283
async destroy() {
284-
this.#destroyAbortControllers();
284+
this._destroyAbortControllers();
285285
await super.destroy();
286286
}
287287
}

packages/agents/src/client.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ export class AgentClient<State = unknown> extends PartySocket {
8080
}
8181
agent: string;
8282
name: string;
83-
#options: AgentClientOptions<State>;
84-
#pendingCalls = new Map<
83+
private options: AgentClientOptions<State>;
84+
private _pendingCalls = new Map<
8585
string,
8686
{
8787
resolve: (value: unknown) => void;
@@ -101,7 +101,7 @@ export class AgentClient<State = unknown> extends PartySocket {
101101
});
102102
this.agent = agentNamespace;
103103
this.name = options.name || "default";
104-
this.#options = options;
104+
this.options = options;
105105

106106
this.addEventListener("message", (event) => {
107107
if (typeof event.data === "string") {
@@ -114,17 +114,17 @@ export class AgentClient<State = unknown> extends PartySocket {
114114
return;
115115
}
116116
if (parsedMessage.type === "cf_agent_state") {
117-
this.#options.onStateUpdate?.(parsedMessage.state as State, "server");
117+
this.options.onStateUpdate?.(parsedMessage.state as State, "server");
118118
return;
119119
}
120120
if (parsedMessage.type === "rpc") {
121121
const response = parsedMessage as RPCResponse;
122-
const pending = this.#pendingCalls.get(response.id);
122+
const pending = this._pendingCalls.get(response.id);
123123
if (!pending) return;
124124

125125
if (!response.success) {
126126
pending.reject(new Error(response.error));
127-
this.#pendingCalls.delete(response.id);
127+
this._pendingCalls.delete(response.id);
128128
pending.stream?.onError?.(response.error);
129129
return;
130130
}
@@ -133,15 +133,15 @@ export class AgentClient<State = unknown> extends PartySocket {
133133
if ("done" in response) {
134134
if (response.done) {
135135
pending.resolve(response.result);
136-
this.#pendingCalls.delete(response.id);
136+
this._pendingCalls.delete(response.id);
137137
pending.stream?.onDone?.(response.result);
138138
} else {
139139
pending.stream?.onChunk?.(response.result);
140140
}
141141
} else {
142142
// Non-streaming response
143143
pending.resolve(response.result);
144-
this.#pendingCalls.delete(response.id);
144+
this._pendingCalls.delete(response.id);
145145
}
146146
}
147147
}
@@ -150,7 +150,7 @@ export class AgentClient<State = unknown> extends PartySocket {
150150

151151
setState(state: State) {
152152
this.send(JSON.stringify({ type: "cf_agent_state", state }));
153-
this.#options.onStateUpdate?.(state, "client");
153+
this.options.onStateUpdate?.(state, "client");
154154
}
155155

156156
/**
@@ -167,7 +167,7 @@ export class AgentClient<State = unknown> extends PartySocket {
167167
): Promise<T> {
168168
return new Promise<T>((resolve, reject) => {
169169
const id = Math.random().toString(36).slice(2);
170-
this.#pendingCalls.set(id, {
170+
this._pendingCalls.set(id, {
171171
resolve: (value: unknown) => resolve(value as T),
172172
reject,
173173
stream: streamOptions,

0 commit comments

Comments
 (0)