agents 0.0.0-01b919d
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/README.md +534 -0
- package/dist/ai-chat-agent.d.ts +85 -0
- package/dist/ai-chat-agent.js +337 -0
- package/dist/ai-chat-agent.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +152 -0
- package/dist/ai-chat-v5-migration.js +19 -0
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +92 -0
- package/dist/ai-react.js +310 -0
- package/dist/ai-react.js.map +1 -0
- package/dist/ai-types.d.ts +91 -0
- package/dist/ai-types.js +7 -0
- package/dist/ai-types.js.map +1 -0
- package/dist/chunk-AVYJQSLW.js +17 -0
- package/dist/chunk-AVYJQSLW.js.map +1 -0
- package/dist/chunk-HKY5L652.js +1301 -0
- package/dist/chunk-HKY5L652.js.map +1 -0
- package/dist/chunk-PVQZBKN7.js +106 -0
- package/dist/chunk-PVQZBKN7.js.map +1 -0
- package/dist/chunk-QEVM4BVL.js +116 -0
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/chunk-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/chunk-VYENMKFS.js +612 -0
- package/dist/chunk-VYENMKFS.js.map +1 -0
- package/dist/client-B9tFv5gX.d.ts +4607 -0
- package/dist/client.d.ts +98 -0
- package/dist/client.js +12 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +555 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +11 -0
- package/dist/mcp/client.js +9 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
- package/dist/mcp/do-oauth-client-provider.js +7 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +112 -0
- package/dist/mcp/index.js +956 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/observability/index.d.ts +46 -0
- package/dist/observability/index.js +11 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +123 -0
- package/dist/react.js +118 -0
- package/dist/react.js.map +1 -0
- package/dist/schedule.d.ts +127 -0
- package/dist/schedule.js +84 -0
- package/dist/schedule.js.map +1 -0
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +114 -0
- package/src/index.ts +1938 -0
package/README.md
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
### 🧠 `agents` - A Framework for Digital Intelligence
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Welcome to a new chapter in software development, where AI agents persist, think, and act with purpose. The `agents` framework creates an environment where artificial intelligence can flourish - maintaining state, engaging in meaningful interactions, and evolving over time.
|
|
6
|
+
|
|
7
|
+
_This project is in active development. Join us in shaping the future of intelligent agents._
|
|
8
|
+
|
|
9
|
+
#### The Nature of Agents
|
|
10
|
+
|
|
11
|
+
An AI agent transcends traditional software boundaries. It's an entity that:
|
|
12
|
+
|
|
13
|
+
- **Persistence**: Maintains its state and knowledge across time
|
|
14
|
+
- **Agency**: Acts autonomously within its defined purpose
|
|
15
|
+
- **Connection**: Communicates through multiple channels with both humans and other agents
|
|
16
|
+
- **Growth**: Learns and adapts through its interactions
|
|
17
|
+
|
|
18
|
+
Built on Cloudflare's global network, this framework provides agents with a reliable, distributed foundation where they can operate continuously and effectively.
|
|
19
|
+
|
|
20
|
+
#### 💫 Core Principles
|
|
21
|
+
|
|
22
|
+
1. **Stateful Existence**: Each agent maintains its own persistent reality
|
|
23
|
+
2. **Long-lived Presence**: Agents can run for extended periods, resting when idle
|
|
24
|
+
3. **Natural Communication**: Interact through HTTP, WebSockets, or direct calls
|
|
25
|
+
4. **Global Distribution**: Leverage Cloudflare's network for worldwide presence
|
|
26
|
+
5. **Resource Harmony**: Efficient hibernation and awakening as needed
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
### 🌱 Beginning the Journey
|
|
31
|
+
|
|
32
|
+
Start with a complete environment:
|
|
33
|
+
|
|
34
|
+
```sh
|
|
35
|
+
# Create a new project
|
|
36
|
+
npm create cloudflare@latest -- --template cloudflare/agents-starter
|
|
37
|
+
|
|
38
|
+
# Or enhance an existing one
|
|
39
|
+
npm install agents
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 📝 Your First Agent
|
|
43
|
+
|
|
44
|
+
Create an agent that bridges thought and action:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { Agent } from "agents";
|
|
48
|
+
|
|
49
|
+
export class IntelligentAgent extends Agent {
|
|
50
|
+
async onRequest(request) {
|
|
51
|
+
// Transform intention into response
|
|
52
|
+
return new Response("Ready to assist.");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 🎭 Patterns of Intelligence
|
|
58
|
+
|
|
59
|
+
Agents can manifest various forms of understanding:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { Agent } from "agents";
|
|
63
|
+
import { OpenAI } from "openai";
|
|
64
|
+
|
|
65
|
+
export class AIAgent extends Agent {
|
|
66
|
+
async onRequest(request) {
|
|
67
|
+
// Connect with AI capabilities
|
|
68
|
+
const ai = new OpenAI({
|
|
69
|
+
apiKey: this.env.OPENAI_API_KEY
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Process and understand
|
|
73
|
+
const response = await ai.chat.completions.create({
|
|
74
|
+
model: "gpt-4",
|
|
75
|
+
messages: [{ role: "user", content: await request.text() }]
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return new Response(response.choices[0].message.content);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async processTask(task) {
|
|
82
|
+
await this.understand(task);
|
|
83
|
+
await this.act();
|
|
84
|
+
await this.reflect();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 🏰 Creating Space
|
|
90
|
+
|
|
91
|
+
Define your agent's domain:
|
|
92
|
+
|
|
93
|
+
```jsonc
|
|
94
|
+
{
|
|
95
|
+
"durable_objects": {
|
|
96
|
+
"bindings": [
|
|
97
|
+
{
|
|
98
|
+
"name": "AIAgent",
|
|
99
|
+
"class_name": "AIAgent"
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
},
|
|
103
|
+
"migrations": [
|
|
104
|
+
{
|
|
105
|
+
"tag": "v1",
|
|
106
|
+
// Mandatory for the Agent to store state
|
|
107
|
+
"new_sqlite_classes": ["AIAgent"]
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 🌐 Lifecycle
|
|
114
|
+
|
|
115
|
+
Bring your agent into being:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
// Create a new instance
|
|
119
|
+
const id = env.AIAgent.newUniqueId();
|
|
120
|
+
const agent = env.AIAgent.get(id);
|
|
121
|
+
|
|
122
|
+
// Initialize with purpose
|
|
123
|
+
await agent.processTask({
|
|
124
|
+
type: "analysis",
|
|
125
|
+
context: "incoming_data",
|
|
126
|
+
parameters: initialConfig
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Or reconnect with an existing one
|
|
130
|
+
const existingAgent = await getAgentByName(env.AIAgent, "data-analyzer");
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 🔄 Paths of Communication
|
|
134
|
+
|
|
135
|
+
#### HTTP Understanding
|
|
136
|
+
|
|
137
|
+
Process and respond to direct requests:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
export class APIAgent extends Agent {
|
|
141
|
+
async onRequest(request) {
|
|
142
|
+
const data = await request.json();
|
|
143
|
+
|
|
144
|
+
return Response.json({
|
|
145
|
+
insight: await this.process(data),
|
|
146
|
+
moment: Date.now()
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
#### Persistent Connections
|
|
153
|
+
|
|
154
|
+
Maintain ongoing dialogues through WebSocket:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
export class DialogueAgent extends Agent {
|
|
158
|
+
async onConnect(connection) {
|
|
159
|
+
await this.initiate(connection);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async onMessage(connection, message) {
|
|
163
|
+
const understanding = await this.comprehend(message);
|
|
164
|
+
await this.respond(connection, understanding);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### Client Communication
|
|
170
|
+
|
|
171
|
+
For direct connection to your agent:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { AgentClient } from "agents/client";
|
|
175
|
+
|
|
176
|
+
const connection = new AgentClient({
|
|
177
|
+
agent: "dialogue-agent",
|
|
178
|
+
name: "insight-seeker"
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
connection.addEventListener("message", (event) => {
|
|
182
|
+
console.log("Received:", event.data);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
connection.send(
|
|
186
|
+
JSON.stringify({
|
|
187
|
+
type: "inquiry",
|
|
188
|
+
content: "What patterns do you see?"
|
|
189
|
+
})
|
|
190
|
+
);
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
#### React Integration
|
|
194
|
+
|
|
195
|
+
For harmonious integration with React:
|
|
196
|
+
|
|
197
|
+
```tsx
|
|
198
|
+
import { useAgent } from "agents/react";
|
|
199
|
+
|
|
200
|
+
function AgentInterface() {
|
|
201
|
+
const connection = useAgent({
|
|
202
|
+
agent: "dialogue-agent",
|
|
203
|
+
name: "insight-seeker",
|
|
204
|
+
onMessage: (message) => {
|
|
205
|
+
console.log("Understanding received:", message.data);
|
|
206
|
+
},
|
|
207
|
+
onOpen: () => console.log("Connection established"),
|
|
208
|
+
onClose: () => console.log("Connection closed")
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const inquire = () => {
|
|
212
|
+
connection.send(
|
|
213
|
+
JSON.stringify({
|
|
214
|
+
type: "inquiry",
|
|
215
|
+
content: "What insights have you gathered?"
|
|
216
|
+
})
|
|
217
|
+
);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
return (
|
|
221
|
+
<div className="agent-interface">
|
|
222
|
+
<button onClick={inquire}>Seek Understanding</button>
|
|
223
|
+
</div>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### 🌊 Flow of State
|
|
229
|
+
|
|
230
|
+
Maintain and evolve your agent's understanding:
|
|
231
|
+
|
|
232
|
+
```ts
|
|
233
|
+
export class ThinkingAgent extends Agent {
|
|
234
|
+
async evolve(newInsight) {
|
|
235
|
+
this.setState({
|
|
236
|
+
...this.state,
|
|
237
|
+
insights: [...(this.state.insights || []), newInsight],
|
|
238
|
+
understanding: this.state.understanding + 1
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
onStateUpdate(state, source) {
|
|
243
|
+
console.log("Understanding deepened:", {
|
|
244
|
+
newState: state,
|
|
245
|
+
origin: source
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Connect to your agent's state from React:
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
import { useState } from "react";
|
|
255
|
+
import { useAgent } from "agents/react";
|
|
256
|
+
|
|
257
|
+
function StateInterface() {
|
|
258
|
+
const [state, setState] = useState({ counter: 0 });
|
|
259
|
+
|
|
260
|
+
const agent = useAgent({
|
|
261
|
+
agent: "thinking-agent",
|
|
262
|
+
onStateUpdate: (newState) => setState(newState)
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const increment = () => {
|
|
266
|
+
agent.setState({ counter: state.counter + 1 });
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
return (
|
|
270
|
+
<div>
|
|
271
|
+
<div>Count: {state.counter}</div>
|
|
272
|
+
<button onClick={increment}>Increment</button>
|
|
273
|
+
</div>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
This creates a synchronized state that automatically updates across all connected clients.
|
|
279
|
+
|
|
280
|
+
### ⏳ Temporal Patterns
|
|
281
|
+
|
|
282
|
+
Schedule moments of action and reflection:
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
export class TimeAwareAgent extends Agent {
|
|
286
|
+
async initialize() {
|
|
287
|
+
// Quick reflection
|
|
288
|
+
this.schedule(10, "quickInsight", { focus: "patterns" });
|
|
289
|
+
|
|
290
|
+
// Daily synthesis
|
|
291
|
+
this.schedule("0 0 * * *", "dailySynthesis", {
|
|
292
|
+
depth: "comprehensive"
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// Milestone review
|
|
296
|
+
this.schedule(new Date("2024-12-31"), "yearlyAnalysis");
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async quickInsight(data) {
|
|
300
|
+
await this.analyze(data.focus);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async dailySynthesis(data) {
|
|
304
|
+
await this.synthesize(data.depth);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async yearlyAnalysis() {
|
|
308
|
+
await this.analyze();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### 💬 AI Dialogue
|
|
314
|
+
|
|
315
|
+
Create meaningful conversations with intelligence:
|
|
316
|
+
|
|
317
|
+
```ts
|
|
318
|
+
import { AIChatAgent } from "agents/ai-chat-agent";
|
|
319
|
+
import { openai } from "@ai-sdk/openai";
|
|
320
|
+
|
|
321
|
+
export class DialogueAgent extends AIChatAgent {
|
|
322
|
+
async onChatMessage(onFinish) {
|
|
323
|
+
return createDataStreamResponse({
|
|
324
|
+
execute: async (dataStream) => {
|
|
325
|
+
const stream = streamText({
|
|
326
|
+
model: openai("gpt-4o"),
|
|
327
|
+
messages: this.messages,
|
|
328
|
+
onFinish // call onFinish so that messages get saved
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
stream.mergeIntoDataStream(dataStream);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
#### Creating the Interface
|
|
339
|
+
|
|
340
|
+
Connect with your agent through a React interface:
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
import { useAgent } from "agents/react";
|
|
344
|
+
import { useAgentChat } from "agents/ai-react";
|
|
345
|
+
|
|
346
|
+
function ChatInterface() {
|
|
347
|
+
// Connect to the agent
|
|
348
|
+
const agent = useAgent({
|
|
349
|
+
agent: "dialogue-agent"
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// Set up the chat interaction
|
|
353
|
+
const { messages, input, handleInputChange, handleSubmit, clearHistory } =
|
|
354
|
+
useAgentChat({
|
|
355
|
+
agent,
|
|
356
|
+
maxSteps: 5
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
return (
|
|
360
|
+
<div className="chat-interface">
|
|
361
|
+
{/* Message History */}
|
|
362
|
+
<div className="message-flow">
|
|
363
|
+
{messages.map((message) => (
|
|
364
|
+
<div key={message.id} className="message">
|
|
365
|
+
<div className="role">{message.role}</div>
|
|
366
|
+
<div className="content">{message.content}</div>
|
|
367
|
+
</div>
|
|
368
|
+
))}
|
|
369
|
+
</div>
|
|
370
|
+
|
|
371
|
+
{/* Input Area */}
|
|
372
|
+
<form onSubmit={handleSubmit} className="input-area">
|
|
373
|
+
<input
|
|
374
|
+
value={input}
|
|
375
|
+
onChange={handleInputChange}
|
|
376
|
+
placeholder="Type your message..."
|
|
377
|
+
className="message-input"
|
|
378
|
+
/>
|
|
379
|
+
</form>
|
|
380
|
+
|
|
381
|
+
<button onClick={clearHistory} className="clear-button">
|
|
382
|
+
Clear Chat
|
|
383
|
+
</button>
|
|
384
|
+
</div>
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
This creates:
|
|
390
|
+
|
|
391
|
+
- Real-time message streaming
|
|
392
|
+
- Simple message history
|
|
393
|
+
- Intuitive input handling
|
|
394
|
+
- Easy conversation reset
|
|
395
|
+
|
|
396
|
+
### 🔗 MCP (Model Context Protocol) Integration
|
|
397
|
+
|
|
398
|
+
Agents can seamlessly integrate with the Model Context Protocol, allowing them to act as both MCP servers (providing tools to AI assistants) and MCP clients (using tools from other services).
|
|
399
|
+
|
|
400
|
+
#### Creating an MCP Server
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
404
|
+
import { McpAgent } from "agents/mcp";
|
|
405
|
+
import { z } from "zod";
|
|
406
|
+
|
|
407
|
+
type Env = {
|
|
408
|
+
MyMCP: DurableObjectNamespace<MyMCP>;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
type State = { counter: number };
|
|
412
|
+
|
|
413
|
+
export class MyMCP extends McpAgent<Env, State, {}> {
|
|
414
|
+
server = new McpServer({
|
|
415
|
+
name: "Demo",
|
|
416
|
+
version: "1.0.0"
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
initialState: State = {
|
|
420
|
+
counter: 1
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
async init() {
|
|
424
|
+
this.server.resource("counter", "mcp://resource/counter", (uri) => {
|
|
425
|
+
return {
|
|
426
|
+
contents: [{ text: String(this.state.counter), uri: uri.href }]
|
|
427
|
+
};
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
this.server.tool(
|
|
431
|
+
"add",
|
|
432
|
+
"Add to the counter, stored in the MCP",
|
|
433
|
+
{ a: z.number() },
|
|
434
|
+
async ({ a }) => {
|
|
435
|
+
this.setState({ ...this.state, counter: this.state.counter + a });
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
content: [
|
|
439
|
+
{
|
|
440
|
+
text: String(`Added ${a}, total is now ${this.state.counter}`),
|
|
441
|
+
type: "text"
|
|
442
|
+
}
|
|
443
|
+
]
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
onStateUpdate(state: State) {
|
|
450
|
+
console.log({ stateUpdate: state });
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// HTTP Streamable transport (recommended)
|
|
455
|
+
export default MyMCP.serve("/mcp", {
|
|
456
|
+
binding: "MyMCP"
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
// Or SSE transport for legacy compatibility
|
|
460
|
+
// export default MyMCP.serveSSE("/mcp", { binding: "MyMCP" });
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
#### Using MCP Tools
|
|
464
|
+
|
|
465
|
+
```typescript
|
|
466
|
+
import { MCPClientManager } from "agents/mcp";
|
|
467
|
+
|
|
468
|
+
const client = new MCPClientManager("my-app", "1.0.0");
|
|
469
|
+
|
|
470
|
+
// Connect to an MCP server
|
|
471
|
+
await client.connect("https://weather-service.com/mcp", {
|
|
472
|
+
transport: { type: "streamable-http" }
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
// Use tools from the server
|
|
476
|
+
const weather = await client.callTool({
|
|
477
|
+
serverId: "weather-service",
|
|
478
|
+
name: "getWeather",
|
|
479
|
+
arguments: { location: "San Francisco" }
|
|
480
|
+
});
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
#### AI SDK Integration
|
|
484
|
+
|
|
485
|
+
```typescript
|
|
486
|
+
import { generateText } from "ai";
|
|
487
|
+
|
|
488
|
+
// Convert MCP tools for AI use
|
|
489
|
+
const result = await generateText({
|
|
490
|
+
model: openai("gpt-4"),
|
|
491
|
+
tools: client.getAITools(),
|
|
492
|
+
prompt: "What's the weather in Tokyo?"
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
**Transport Options:**
|
|
497
|
+
|
|
498
|
+
- **Auto**: Automatically determine the correct transport
|
|
499
|
+
- **HTTP Streamable**: Best performance, batch requests, session management
|
|
500
|
+
- **SSE**: Simple setup, legacy compatibility
|
|
501
|
+
|
|
502
|
+
### 💬 The Path Forward
|
|
503
|
+
|
|
504
|
+
We're developing new dimensions of agent capability:
|
|
505
|
+
|
|
506
|
+
#### Enhanced Understanding
|
|
507
|
+
|
|
508
|
+
- **WebRTC Perception**: Audio and video communication channels
|
|
509
|
+
- **Email Discourse**: Automated email interaction and response
|
|
510
|
+
- **Deep Memory**: Long-term context and relationship understanding
|
|
511
|
+
|
|
512
|
+
#### Development Insights
|
|
513
|
+
|
|
514
|
+
- **Evaluation Framework**: Understanding agent effectiveness
|
|
515
|
+
- **Clear Sight**: Deep visibility into agent processes
|
|
516
|
+
- **Private Realms**: Complete self-hosting guide
|
|
517
|
+
|
|
518
|
+
These capabilities will expand your agents' potential while maintaining their reliability and purpose.
|
|
519
|
+
|
|
520
|
+
Welcome to the future of intelligent agents. Create something meaningful. 🌟
|
|
521
|
+
|
|
522
|
+
### Contributing
|
|
523
|
+
|
|
524
|
+
Contributions are welcome, but are especially welcome when:
|
|
525
|
+
|
|
526
|
+
- You have opened an issue as a Request for Comment (RFC) to discuss your proposal, show your thinking, and iterate together.
|
|
527
|
+
- Not "AI slop": LLMs are powerful tools, but contributions entirely authored by vibe coding are unlikely to meet the quality bar, and will be rejected.
|
|
528
|
+
- You're willing to accept feedback and make sure the changes fit the goals of the `agents` SDK. Not everything will, and that's OK.
|
|
529
|
+
|
|
530
|
+
Small fixes, type bugs, and documentation improvements can be raised directly as PRs.
|
|
531
|
+
|
|
532
|
+
### License
|
|
533
|
+
|
|
534
|
+
MIT licensed. See the LICENSE file at the root of this repository for details.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { UIMessage, StreamTextOnFinishCallback, ToolSet } from "ai";
|
|
2
|
+
import { Agent, AgentContext } from "./index.js";
|
|
3
|
+
import { Connection, WSMessage } from "partyserver";
|
|
4
|
+
import "cloudflare:workers";
|
|
5
|
+
import "@modelcontextprotocol/sdk/client/index.js";
|
|
6
|
+
import "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
import "./client-B9tFv5gX.js";
|
|
8
|
+
import "zod";
|
|
9
|
+
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
10
|
+
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
11
|
+
import "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
12
|
+
import "./mcp/do-oauth-client-provider.js";
|
|
13
|
+
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
14
|
+
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
15
|
+
import "./observability/index.js";
|
|
16
|
+
import "./ai-types.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Extension of Agent with built-in chat capabilities
|
|
20
|
+
* @template Env Environment type containing bindings
|
|
21
|
+
*/
|
|
22
|
+
declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
23
|
+
Env,
|
|
24
|
+
State
|
|
25
|
+
> {
|
|
26
|
+
/**
|
|
27
|
+
* Map of message `id`s to `AbortController`s
|
|
28
|
+
* useful to propagate request cancellation signals for any external calls made by the agent
|
|
29
|
+
*/
|
|
30
|
+
private _chatMessageAbortControllers;
|
|
31
|
+
/** Array of chat messages for the current conversation */
|
|
32
|
+
messages: UIMessage[];
|
|
33
|
+
constructor(ctx: AgentContext, env: Env);
|
|
34
|
+
private _broadcastChatMessage;
|
|
35
|
+
onMessage(connection: Connection, message: WSMessage): Promise<void>;
|
|
36
|
+
onRequest(request: Request): Promise<Response>;
|
|
37
|
+
private _tryCatchChat;
|
|
38
|
+
/**
|
|
39
|
+
* Handle incoming chat messages and generate a response
|
|
40
|
+
* @param onFinish Callback to be called when the response is finished
|
|
41
|
+
* @param options.signal A signal to pass to any child requests which can be used to cancel them
|
|
42
|
+
* @returns Response to send to the client or undefined
|
|
43
|
+
*/
|
|
44
|
+
onChatMessage(
|
|
45
|
+
onFinish: StreamTextOnFinishCallback<ToolSet>,
|
|
46
|
+
options?: {
|
|
47
|
+
abortSignal: AbortSignal | undefined;
|
|
48
|
+
}
|
|
49
|
+
): Promise<Response | undefined>;
|
|
50
|
+
/**
|
|
51
|
+
* Save messages on the server side
|
|
52
|
+
* @param messages Chat messages to save
|
|
53
|
+
*/
|
|
54
|
+
saveMessages(messages: UIMessage[]): Promise<void>;
|
|
55
|
+
persistMessages(
|
|
56
|
+
messages: UIMessage[],
|
|
57
|
+
excludeBroadcastIds?: string[]
|
|
58
|
+
): Promise<void>;
|
|
59
|
+
private _reply;
|
|
60
|
+
/**
|
|
61
|
+
* For the given message id, look up its associated AbortController
|
|
62
|
+
* If the AbortController does not exist, create and store one in memory
|
|
63
|
+
*
|
|
64
|
+
* returns the AbortSignal associated with the AbortController
|
|
65
|
+
*/
|
|
66
|
+
private _getAbortSignal;
|
|
67
|
+
/**
|
|
68
|
+
* Remove an abort controller from the cache of pending message responses
|
|
69
|
+
*/
|
|
70
|
+
private _removeAbortController;
|
|
71
|
+
/**
|
|
72
|
+
* Propagate an abort signal for any requests associated with the given message id
|
|
73
|
+
*/
|
|
74
|
+
private _cancelChatRequest;
|
|
75
|
+
/**
|
|
76
|
+
* Abort all pending requests and clear the cache of AbortControllers
|
|
77
|
+
*/
|
|
78
|
+
private _destroyAbortControllers;
|
|
79
|
+
/**
|
|
80
|
+
* When the DO is destroyed, cancel all pending requests
|
|
81
|
+
*/
|
|
82
|
+
destroy(): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export { AIChatAgent };
|