Skip to content

Commit 2e0dc1f

Browse files
agents-git-bot[bot]github-actions[bot]claudeopencode-agent[bot]elithrar
authored
Rename Counter class and update type references (#28179)
* Rename Counter class and update type references Syncs changes from cloudflare/agents PR #867: - Rename Counter class to CounterAgent - Export CounterState type for reuse in client code - Update useAgent type parameters to include both agent and state - Update all code examples and configuration to use new names This improves type safety and consistency in the agents SDK examples. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> * Clean rename, one stale ref fixed. Co-authored-by: elithrar <elithrar@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: elithrar <elithrar@users.noreply.github.com>
1 parent ab4254b commit 2e0dc1f

3 files changed

Lines changed: 36 additions & 44 deletions

File tree

src/content/docs/agents/api-reference/callable-methods.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ Add the `@callable()` decorator to any method you want to expose:
6969
```ts
7070
import { Agent, callable } from "agents";
7171

72-
type State = {
72+
export type CounterState = {
7373
count: number;
7474
items: string[];
7575
};
7676

77-
export class CounterAgent extends Agent<Env, State> {
78-
initialState: State = { count: 0, items: [] };
77+
export class CounterAgent extends Agent<Env, CounterState> {
78+
initialState: CounterState = { count: 0, items: [] };
7979

8080
@callable()
8181
increment(): number {

src/content/docs/agents/getting-started/add-to-existing-project.mdx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ Create a new file for your agent (for example, `src/agents/counter.ts`):
3838
```ts
3939
import { Agent } from "agents";
4040

41-
type CounterState = {
41+
export type CounterState = {
4242
count: number;
4343
};
4444

45-
export class Counter extends Agent<Env, CounterState> {
45+
export class CounterAgent extends Agent<Env, CounterState> {
4646
initialState: CounterState = { count: 0 };
4747

4848
increment() {
@@ -75,16 +75,16 @@ Add the Durable Object binding and migration:
7575
"durable_objects": {
7676
"bindings": [
7777
{
78-
"name": "Counter",
79-
"class_name": "Counter",
78+
"name": "CounterAgent",
79+
"class_name": "CounterAgent",
8080
},
8181
],
8282
},
8383

8484
"migrations": [
8585
{
8686
"tag": "v1",
87-
"new_sqlite_classes": ["Counter"],
87+
"new_sqlite_classes": ["CounterAgent"],
8888
},
8989
],
9090
}
@@ -94,7 +94,7 @@ Add the Durable Object binding and migration:
9494

9595
**Key points:**
9696

97-
- `name` in bindings becomes the property on `env` (for example, `env.Counter`)
97+
- `name` in bindings becomes the property on `env` (for example, `env.CounterAgent`)
9898
- `class_name` must exactly match your exported class name
9999
- `new_sqlite_classes` enables SQLite storage for state persistence
100100
- `nodejs_compat` flag is required for the agents package
@@ -107,7 +107,7 @@ Your agent class must be exported from your main entry point. Update your `src/i
107107

108108
```ts
109109
// Export the agent class (required for Durable Objects)
110-
export { Counter } from "./agents/counter";
110+
export { CounterAgent } from "./agents/counter";
111111

112112
// Your existing exports...
113113
export default {
@@ -127,7 +127,7 @@ Choose the approach that matches your project structure:
127127

128128
```ts
129129
import { routeAgentRequest } from "agents";
130-
export { Counter } from "./agents/counter";
130+
export { CounterAgent } from "./agents/counter";
131131

132132
export default {
133133
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
@@ -155,7 +155,7 @@ export default {
155155
```ts
156156
import { Hono } from "hono";
157157
import { agentsMiddleware } from "hono-agents";
158-
export { Counter } from "./agents/counter";
158+
export { CounterAgent } from "./agents/counter";
159159

160160
const app = new Hono<{ Bindings: Env }>();
161161

@@ -178,7 +178,7 @@ If you are serving static assets alongside agents, static assets are served firs
178178

179179
```ts
180180
import { routeAgentRequest } from "agents";
181-
export { Counter } from "./agents/counter";
181+
export { CounterAgent } from "./agents/counter";
182182

183183
export default {
184184
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
@@ -221,15 +221,15 @@ npx wrangler types env.d.ts
221221
This creates an `env.d.ts` file with all your bindings typed. Alternatively, you can manually update your `Env` type to include the agent namespace:
222222

223223
```ts
224-
import type { Counter } from "./agents/counter";
224+
import type { CounterAgent } from "./agents/counter";
225225

226226
interface Env {
227227
// Your existing bindings
228228
MY_KV: KVNamespace;
229229
MY_DB: D1Database;
230230

231231
// Add agent bindings
232-
Counter: DurableObjectNamespace<Counter>;
232+
CounterAgent: DurableObjectNamespace<CounterAgent>;
233233
}
234234
```
235235

@@ -244,14 +244,13 @@ Refer to [Configuration](/agents/api-reference/configuration/#generating-types)
244244
```tsx
245245
import { useState } from "react";
246246
import { useAgent } from "agents/react";
247-
248-
type CounterState = { count: number };
247+
import type { CounterAgent, CounterState } from "./agents/counter";
249248

250249
function CounterWidget() {
251250
const [count, setCount] = useState(0);
252251

253-
const agent = useAgent({
254-
agent: "Counter",
252+
const agent = useAgent<CounterAgent, CounterState>({
253+
agent: "CounterAgent",
255254
onStateUpdate: (state) => setCount(state.count),
256255
});
257256

@@ -275,7 +274,7 @@ function CounterWidget() {
275274
import { AgentClient } from "agents/client";
276275

277276
const agent = new AgentClient({
278-
agent: "Counter",
277+
agent: "CounterAgent",
279278
name: "user-123", // Optional: unique instance name
280279
onStateUpdate: (state) => {
281280
document.getElementById("count").textContent = state.count;
@@ -316,15 +315,15 @@ Update the Wrangler configuration file:
316315
{
317316
"durable_objects": {
318317
"bindings": [
319-
{ "name": "Counter", "class_name": "Counter" },
318+
{ "name": "CounterAgent", "class_name": "CounterAgent" },
320319
{ "name": "Chat", "class_name": "Chat" },
321320
{ "name": "Scheduler", "class_name": "Scheduler" },
322321
],
323322
},
324323
"migrations": [
325324
{
326325
"tag": "v1",
327-
"new_sqlite_classes": ["Counter", "Chat", "Scheduler"],
326+
"new_sqlite_classes": ["CounterAgent", "Chat", "Scheduler"],
328327
},
329328
],
330329
}
@@ -337,7 +336,7 @@ Export all agents from your entry point:
337336
<TypeScriptExample>
338337

339338
```ts
340-
export { Counter } from "./agents/counter";
339+
export { CounterAgent } from "./agents/counter";
341340
export { Chat } from "./agents/chat";
342341
export { Scheduler } from "./agents/scheduler";
343342
```
@@ -404,7 +403,7 @@ export default {
404403
async fetch(request: Request, env: Env) {
405404
if (request.url.endsWith("/api/increment")) {
406405
// Get a specific agent instance
407-
const counter = await getAgentByName(env.Counter, "shared-counter");
406+
const counter = await getAgentByName(env.CounterAgent, "shared-counter");
408407
const newCount = await counter.increment();
409408
return Response.json({ count: newCount });
410409
}

src/content/docs/agents/getting-started/quick-start.mdx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ Build a simple counter agent from scratch. Replace `src/server.ts`:
5353
import { Agent, routeAgentRequest, callable } from "agents";
5454

5555
// Define the state shape
56-
type CounterState = {
56+
export type CounterState = {
5757
count: number;
5858
};
5959

6060
// Create the agent
61-
export class Counter extends Agent<Env, CounterState> {
61+
export class CounterAgent extends Agent<Env, CounterState> {
6262
// Initial state for new instances
6363
initialState: CounterState = { count: 0 };
6464

@@ -107,15 +107,15 @@ Update `wrangler.jsonc` to register the agent:
107107
"durable_objects": {
108108
"bindings": [
109109
{
110-
"name": "Counter",
111-
"class_name": "Counter",
110+
"name": "CounterAgent",
111+
"class_name": "CounterAgent",
112112
},
113113
],
114114
},
115115
"migrations": [
116116
{
117117
"tag": "v1",
118-
"new_sqlite_classes": ["Counter"],
118+
"new_sqlite_classes": ["CounterAgent"],
119119
},
120120
],
121121
}
@@ -130,19 +130,14 @@ Replace `src/client.tsx`:
130130
```tsx title="src/client.tsx"
131131
import { useState } from "react";
132132
import { useAgent } from "agents/react";
133-
import type { Counter } from "./server";
134-
135-
// Match your agent's state type
136-
type CounterState = {
137-
count: number;
138-
};
133+
import type { CounterAgent, CounterState } from "./server";
139134

140135
export default function App() {
141136
const [count, setCount] = useState(0);
142137

143138
// Connect to the Counter agent
144-
const agent = useAgent<Counter, CounterState>({
145-
agent: "Counter",
139+
const agent = useAgent<CounterAgent, CounterState>({
140+
agent: "CounterAgent",
146141
onStateUpdate: (state) => setCount(state.count),
147142
});
148143

@@ -186,7 +181,7 @@ flowchart LR
186181

187182
| Concept | What it means |
188183
| -------------------- | ------------------------------------------------------------------------------------------- |
189-
| **Agent instance** | Each unique name gets its own agent. `Counter:user-123` is separate from `Counter:user-456` |
184+
| **Agent instance** | Each unique name gets its own agent. `CounterAgent:user-123` is separate from `CounterAgent:user-456` |
190185
| **Persistent state** | State survives restarts, deploys, and hibernation. It is stored in SQLite |
191186
| **Real-time sync** | All clients connected to the same agent receive state updates instantly |
192187
| **Hibernation** | When no clients are connected, the agent hibernates (no cost). It wakes on the next request |
@@ -201,7 +196,7 @@ If you are not using React:
201196
import { AgentClient } from "agents/client";
202197

203198
const agent = new AgentClient({
204-
agent: "Counter",
199+
agent: "CounterAgent",
205200
name: "my-counter", // optional, defaults to "default"
206201
onStateUpdate: (state) => {
207202
console.log("New count:", state.count);
@@ -268,13 +263,11 @@ Add the agent and state type parameters:
268263

269264
```ts
270265
import { useAgent } from "agents/react";
271-
import type { Counter } from "./server";
272-
273-
type CounterState = { count: number };
266+
import type { CounterAgent, CounterState } from "./server";
274267

275268
// Pass the agent and state types to useAgent
276-
const agent = useAgent<Counter, CounterState>({
277-
agent: "Counter",
269+
const agent = useAgent<CounterAgent, CounterState>({
270+
agent: "CounterAgent",
278271
onStateUpdate: (state) => setCount(state.count),
279272
});
280273

0 commit comments

Comments
 (0)