agents 0.0.0-d40512c → 0.0.0-d520fe8
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 +236 -6
- package/dist/ai-chat-agent.d.ts +95 -20
- package/dist/ai-chat-agent.js +798 -260
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration-BSiGZmYU.js +155 -0
- package/dist/ai-chat-v5-migration-BSiGZmYU.js.map +1 -0
- package/dist/ai-chat-v5-migration.d.ts +155 -0
- package/dist/ai-chat-v5-migration.js +3 -0
- package/dist/ai-react.d.ts +78 -85
- package/dist/ai-react.js +396 -199
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types-81H_-Uxh.d.ts +103 -0
- package/dist/ai-types-CrMqkwc_.js +24 -0
- package/dist/ai-types-CrMqkwc_.js.map +1 -0
- package/dist/ai-types.d.ts +6 -74
- package/dist/ai-types.js +3 -1
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +27 -0
- package/dist/cli.js.map +1 -0
- package/dist/client-B3SR12TQ.js +117 -0
- package/dist/client-B3SR12TQ.js.map +1 -0
- package/dist/client-BAQA84dr.d.ts +104 -0
- package/dist/client-BZq9qau2.js +1093 -0
- package/dist/client-BZq9qau2.js.map +1 -0
- package/dist/client-CsaP9Irq.d.ts +1528 -0
- package/dist/client.d.ts +11 -92
- package/dist/client.js +4 -11
- package/dist/codemode/ai.d.ts +27 -0
- package/dist/codemode/ai.js +151 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/do-oauth-client-provider-C2CHH5x-.d.ts +55 -0
- package/dist/do-oauth-client-provider-CwqK5SXm.js +94 -0
- package/dist/do-oauth-client-provider-CwqK5SXm.js.map +1 -0
- package/dist/index-BUle9RiP.d.ts +58 -0
- package/dist/{index-CITGJflw.d.ts → index-Bx5KK3VJ.d.ts} +256 -155
- package/dist/index.d.ts +69 -30
- package/dist/index.js +7 -22
- package/dist/mcp/client.d.ts +4 -1055
- package/dist/mcp/client.js +4 -9
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -41
- package/dist/mcp/do-oauth-client-provider.js +3 -7
- package/dist/mcp/index.d.ts +190 -81
- package/dist/mcp/index.js +1434 -770
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +34 -0
- package/dist/mcp/x402.js +198 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-BwPscEiF.d.ts +61 -0
- package/dist/observability/index.d.ts +3 -12
- package/dist/observability/index.js +7 -10
- package/dist/react-CbwD4fBf.d.ts +113 -0
- package/dist/react.d.ts +10 -119
- package/dist/react.js +183 -110
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +91 -14
- package/dist/schedule.js +46 -21
- package/dist/schedule.js.map +1 -1
- package/dist/serializable-faDkMCai.d.ts +39 -0
- package/dist/serializable.d.ts +7 -32
- package/dist/serializable.js +1 -1
- package/dist/src-D_KKH_4c.js +1184 -0
- package/dist/src-D_KKH_4c.js.map +1 -0
- package/package.json +78 -28
- package/dist/ai-types.js.map +0 -1
- package/dist/chunk-4RBEYCWK.js +0 -469
- package/dist/chunk-4RBEYCWK.js.map +0 -1
- package/dist/chunk-KUH345EY.js +0 -116
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-LU2RSO54.js +0 -910
- package/dist/chunk-LU2RSO54.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js +0 -106
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/client.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mcp/client.js.map +0 -1
- package/dist/mcp/do-oauth-client-provider.js.map +0 -1
- package/dist/observability/index.js.map +0 -1
- package/dist/serializable.js.map +0 -1
- package/src/index.ts +0 -1365
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
### 🧠 `agents` - A Framework for Digital Intelligence
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
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
6
|
|
|
@@ -166,7 +166,7 @@ export class DialogueAgent extends Agent {
|
|
|
166
166
|
}
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
-
#### Client
|
|
169
|
+
#### Client Communication
|
|
170
170
|
|
|
171
171
|
For direct connection to your agent:
|
|
172
172
|
|
|
@@ -317,24 +317,139 @@ Create meaningful conversations with intelligence:
|
|
|
317
317
|
```ts
|
|
318
318
|
import { AIChatAgent } from "agents/ai-chat-agent";
|
|
319
319
|
import { openai } from "@ai-sdk/openai";
|
|
320
|
+
import { streamText, generateText, createDataStreamResponse } from "ai";
|
|
320
321
|
|
|
321
322
|
export class DialogueAgent extends AIChatAgent {
|
|
322
323
|
async onChatMessage(onFinish) {
|
|
324
|
+
// Option 1: Streaming responses (recommended for real-time interaction)
|
|
323
325
|
return createDataStreamResponse({
|
|
324
326
|
execute: async (dataStream) => {
|
|
325
327
|
const stream = streamText({
|
|
326
328
|
model: openai("gpt-4o"),
|
|
327
329
|
messages: this.messages,
|
|
328
|
-
|
|
330
|
+
// Optional: onFinish is invoked by the AI SDK when generation completes.
|
|
331
|
+
// Persistence is handled automatically by AIChatAgent after streaming completes.
|
|
332
|
+
onFinish
|
|
329
333
|
});
|
|
330
334
|
|
|
331
335
|
stream.mergeIntoDataStream(dataStream);
|
|
332
336
|
}
|
|
333
337
|
});
|
|
338
|
+
|
|
339
|
+
// Option 2: Non-streaming responses (simpler, but no real-time updates)
|
|
340
|
+
// const result = await generateText({
|
|
341
|
+
// model: openai("gpt-4o"),
|
|
342
|
+
// messages: this.messages,
|
|
343
|
+
// });
|
|
344
|
+
//
|
|
345
|
+
// // For non-streaming with metadata, use toUIMessage:
|
|
346
|
+
// const message = result.toUIMessage({
|
|
347
|
+
// metadata: {
|
|
348
|
+
// model: 'gpt-4o',
|
|
349
|
+
// totalTokens: result.usage?.totalTokens,
|
|
350
|
+
// }
|
|
351
|
+
// });
|
|
352
|
+
//
|
|
353
|
+
// return new Response(JSON.stringify(message), {
|
|
354
|
+
// headers: { 'Content-Type': 'application/json' }
|
|
355
|
+
// });
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### Metadata Support
|
|
361
|
+
|
|
362
|
+
The AI SDK provides native support for message metadata through the `messageMetadata` callback. This allows you to attach custom information to messages at the message level.
|
|
363
|
+
|
|
364
|
+
##### AIChatAgent Integration
|
|
365
|
+
|
|
366
|
+
In the context of `AIChatAgent`, you can use metadata like this:
|
|
367
|
+
|
|
368
|
+
```typescript
|
|
369
|
+
import { AIChatAgent } from "agents/ai-chat-agent";
|
|
370
|
+
import { streamText } from "ai";
|
|
371
|
+
import { openai } from "@ai-sdk/openai";
|
|
372
|
+
|
|
373
|
+
export class MyAgent extends AIChatAgent<Env> {
|
|
374
|
+
async onChatMessage(onFinish) {
|
|
375
|
+
const startTime = Date.now();
|
|
376
|
+
|
|
377
|
+
const result = streamText({
|
|
378
|
+
model: openai("gpt-4o"),
|
|
379
|
+
messages: this.messages,
|
|
380
|
+
onFinish
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
return result.toUIMessageStreamResponse({
|
|
384
|
+
messageMetadata: ({ part }) => {
|
|
385
|
+
if (part.type === "start") {
|
|
386
|
+
return {
|
|
387
|
+
model: "gpt-4o",
|
|
388
|
+
createdAt: Date.now(),
|
|
389
|
+
messageCount: this.messages.length
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
if (part.type === "finish") {
|
|
393
|
+
return {
|
|
394
|
+
responseTime: Date.now() - startTime,
|
|
395
|
+
totalTokens: part.totalUsage?.totalTokens
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
});
|
|
334
400
|
}
|
|
335
401
|
}
|
|
336
402
|
```
|
|
337
403
|
|
|
404
|
+
##### Accessing Metadata on the Client
|
|
405
|
+
|
|
406
|
+
Access metadata through the `message.metadata` property:
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
'use client';
|
|
410
|
+
|
|
411
|
+
import { useChat } from '@ai-sdk/react';
|
|
412
|
+
import { DefaultChatTransport } from 'ai';
|
|
413
|
+
import type { MyUIMessage } from '@/types';
|
|
414
|
+
|
|
415
|
+
export default function Chat() {
|
|
416
|
+
const { messages } = useChat<MyUIMessage>({
|
|
417
|
+
transport: new DefaultChatTransport({
|
|
418
|
+
api: '/api/chat',
|
|
419
|
+
}),
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
return (
|
|
423
|
+
<div>
|
|
424
|
+
{messages.map(message => (
|
|
425
|
+
<div key={message.id}>
|
|
426
|
+
<div>
|
|
427
|
+
{message.role === 'user' ? 'User: ' : 'AI: '}
|
|
428
|
+
{message.metadata?.createdAt && (
|
|
429
|
+
<span className="text-sm text-gray-500">
|
|
430
|
+
{new Date(message.metadata.createdAt).toLocaleTimeString()}
|
|
431
|
+
</span>
|
|
432
|
+
)}
|
|
433
|
+
</div>
|
|
434
|
+
{/* Render message content */}
|
|
435
|
+
{message.parts.map((part, index) =>
|
|
436
|
+
part.type === 'text' ? <div key={index}>{part.text}</div> : null,
|
|
437
|
+
)}
|
|
438
|
+
{/* Display additional metadata */}
|
|
439
|
+
{message.metadata?.totalTokens && (
|
|
440
|
+
<div className="text-xs text-gray-400">
|
|
441
|
+
{message.metadata.totalTokens} tokens
|
|
442
|
+
</div>
|
|
443
|
+
)}
|
|
444
|
+
</div>
|
|
445
|
+
))}
|
|
446
|
+
</div>
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
For more details, see the [AI SDK Message Metadata documentation](https://ai-sdk.dev/docs/ai-sdk-ui/message-metadata).
|
|
452
|
+
|
|
338
453
|
#### Creating the Interface
|
|
339
454
|
|
|
340
455
|
Connect with your agent through a React interface:
|
|
@@ -363,7 +478,14 @@ function ChatInterface() {
|
|
|
363
478
|
{messages.map((message) => (
|
|
364
479
|
<div key={message.id} className="message">
|
|
365
480
|
<div className="role">{message.role}</div>
|
|
366
|
-
<div className="content">
|
|
481
|
+
<div className="content">
|
|
482
|
+
{message.parts.map((part, i) => {
|
|
483
|
+
if (part.type === "text")
|
|
484
|
+
return <span key={i}>{part.text}</span>;
|
|
485
|
+
// Render other part types (e.g., files, tool calls) as desired
|
|
486
|
+
return null;
|
|
487
|
+
})}
|
|
488
|
+
</div>
|
|
367
489
|
</div>
|
|
368
490
|
))}
|
|
369
491
|
</div>
|
|
@@ -393,6 +515,114 @@ This creates:
|
|
|
393
515
|
- Intuitive input handling
|
|
394
516
|
- Easy conversation reset
|
|
395
517
|
|
|
518
|
+
### 🔗 MCP (Model Context Protocol) Integration
|
|
519
|
+
|
|
520
|
+
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).
|
|
521
|
+
|
|
522
|
+
#### Creating an MCP Server
|
|
523
|
+
|
|
524
|
+
```typescript
|
|
525
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
526
|
+
import { McpAgent } from "agents/mcp";
|
|
527
|
+
import { z } from "zod";
|
|
528
|
+
|
|
529
|
+
type Env = {
|
|
530
|
+
MyMCP: DurableObjectNamespace<MyMCP>;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
type State = { counter: number };
|
|
534
|
+
|
|
535
|
+
export class MyMCP extends McpAgent<Env, State, {}> {
|
|
536
|
+
server = new McpServer({
|
|
537
|
+
name: "Demo",
|
|
538
|
+
version: "1.0.0"
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
initialState: State = {
|
|
542
|
+
counter: 1
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
async init() {
|
|
546
|
+
this.server.resource("counter", "mcp://resource/counter", (uri) => {
|
|
547
|
+
return {
|
|
548
|
+
contents: [{ text: String(this.state.counter), uri: uri.href }]
|
|
549
|
+
};
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
this.server.registerTool(
|
|
553
|
+
"add",
|
|
554
|
+
{
|
|
555
|
+
description: "Add to the counter, stored in the MCP",
|
|
556
|
+
inputSchema: { a: z.number() }
|
|
557
|
+
},
|
|
558
|
+
async ({ a }) => {
|
|
559
|
+
this.setState({ ...this.state, counter: this.state.counter + a });
|
|
560
|
+
|
|
561
|
+
return {
|
|
562
|
+
content: [
|
|
563
|
+
{
|
|
564
|
+
text: String(`Added ${a}, total is now ${this.state.counter}`),
|
|
565
|
+
type: "text"
|
|
566
|
+
}
|
|
567
|
+
]
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
onStateUpdate(state: State) {
|
|
574
|
+
console.log({ stateUpdate: state });
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// HTTP Streamable transport (recommended)
|
|
579
|
+
export default MyMCP.serve("/mcp", {
|
|
580
|
+
binding: "MyMCP"
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
// Or SSE transport for legacy compatibility
|
|
584
|
+
// export default MyMCP.serveSSE("/mcp", { binding: "MyMCP" });
|
|
585
|
+
```
|
|
586
|
+
|
|
587
|
+
#### Using MCP Tools
|
|
588
|
+
|
|
589
|
+
```typescript
|
|
590
|
+
import { MCPClientManager } from "agents/mcp";
|
|
591
|
+
|
|
592
|
+
const client = new MCPClientManager("my-app", "1.0.0");
|
|
593
|
+
|
|
594
|
+
// Connect to an MCP server
|
|
595
|
+
await client.connect("https://weather-service.com/mcp", {
|
|
596
|
+
transport: { type: "streamable-http" }
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
// Use tools from the server
|
|
600
|
+
const weather = await client.callTool({
|
|
601
|
+
serverId: "weather-service",
|
|
602
|
+
name: "getWeather",
|
|
603
|
+
arguments: { location: "San Francisco" }
|
|
604
|
+
});
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
#### AI SDK Integration
|
|
608
|
+
|
|
609
|
+
```typescript
|
|
610
|
+
import { generateText } from "ai";
|
|
611
|
+
|
|
612
|
+
// Convert MCP tools for AI use
|
|
613
|
+
const result = await generateText({
|
|
614
|
+
model: openai("gpt-4"),
|
|
615
|
+
tools: client.getAITools(),
|
|
616
|
+
prompt: "What's the weather in Tokyo?"
|
|
617
|
+
});
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
**Transport Options:**
|
|
621
|
+
|
|
622
|
+
- **Auto**: Automatically determine the correct transport
|
|
623
|
+
- **HTTP Streamable**: Best performance, batch requests, session management
|
|
624
|
+
- **SSE**: Simple setup, legacy compatibility
|
|
625
|
+
|
|
396
626
|
### 💬 The Path Forward
|
|
397
627
|
|
|
398
628
|
We're developing new dimensions of agent capability:
|
|
@@ -418,8 +648,8 @@ Welcome to the future of intelligent agents. Create something meaningful. 🌟
|
|
|
418
648
|
Contributions are welcome, but are especially welcome when:
|
|
419
649
|
|
|
420
650
|
- You have opened an issue as a Request for Comment (RFC) to discuss your proposal, show your thinking, and iterate together.
|
|
421
|
-
-
|
|
422
|
-
- You're willing to accept feedback and make sure the changes fit the goals of the `agents`
|
|
651
|
+
- 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.
|
|
652
|
+
- 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.
|
|
423
653
|
|
|
424
654
|
Small fixes, type bugs, and documentation improvements can be raised directly as PRs.
|
|
425
655
|
|
package/dist/ai-chat-agent.d.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import "
|
|
5
|
-
import "
|
|
6
|
-
import "./
|
|
7
|
-
import "
|
|
8
|
-
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
9
|
-
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
10
|
-
import "./mcp/do-oauth-client-provider.js";
|
|
11
|
-
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
12
|
-
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
1
|
+
import "./client-CsaP9Irq.js";
|
|
2
|
+
import "./mcp-BwPscEiF.js";
|
|
3
|
+
import "./do-oauth-client-provider-C2CHH5x-.js";
|
|
4
|
+
import "./index-BUle9RiP.js";
|
|
5
|
+
import "./ai-types-81H_-Uxh.js";
|
|
6
|
+
import { n as AgentContext, t as Agent } from "./index-Bx5KK3VJ.js";
|
|
7
|
+
import { StreamTextOnFinishCallback, ToolSet, UIMessage } from "ai";
|
|
13
8
|
|
|
9
|
+
//#region src/ai-chat-agent.d.ts
|
|
14
10
|
/**
|
|
15
11
|
* Extension of Agent with built-in chat capabilities
|
|
16
12
|
* @template Env Environment type containing bindings
|
|
@@ -24,11 +20,85 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
24
20
|
* useful to propagate request cancellation signals for any external calls made by the agent
|
|
25
21
|
*/
|
|
26
22
|
private _chatMessageAbortControllers;
|
|
23
|
+
/**
|
|
24
|
+
* Currently active stream ID for resumable streaming.
|
|
25
|
+
* Stored in memory for quick access; persisted in stream_metadata table.
|
|
26
|
+
*/
|
|
27
|
+
private _activeStreamId;
|
|
28
|
+
/**
|
|
29
|
+
* Request ID associated with the active stream.
|
|
30
|
+
*/
|
|
31
|
+
private _activeRequestId;
|
|
32
|
+
/**
|
|
33
|
+
* Current chunk index for the active stream
|
|
34
|
+
*/
|
|
35
|
+
private _streamChunkIndex;
|
|
36
|
+
/**
|
|
37
|
+
* Buffer for stream chunks pending write to SQLite.
|
|
38
|
+
* Chunks are batched and flushed when buffer reaches CHUNK_BUFFER_SIZE.
|
|
39
|
+
*/
|
|
40
|
+
private _chunkBuffer;
|
|
41
|
+
/**
|
|
42
|
+
* Lock to prevent concurrent flush operations
|
|
43
|
+
*/
|
|
44
|
+
private _isFlushingChunks;
|
|
45
|
+
/**
|
|
46
|
+
* Timestamp of the last cleanup operation for old streams
|
|
47
|
+
*/
|
|
48
|
+
private _lastCleanupTime;
|
|
27
49
|
/** Array of chat messages for the current conversation */
|
|
28
|
-
messages:
|
|
50
|
+
messages: UIMessage[];
|
|
29
51
|
constructor(ctx: AgentContext, env: Env);
|
|
52
|
+
/**
|
|
53
|
+
* Restore active stream state if the agent was restarted during streaming.
|
|
54
|
+
* Called during construction to recover any interrupted streams.
|
|
55
|
+
* Validates stream freshness to avoid sending stale resume notifications.
|
|
56
|
+
*/
|
|
57
|
+
private _restoreActiveStream;
|
|
58
|
+
/**
|
|
59
|
+
* Notify a connection about an active stream that can be resumed.
|
|
60
|
+
* The client should respond with CF_AGENT_STREAM_RESUME_ACK to receive chunks.
|
|
61
|
+
* Uses in-memory state for request ID - no extra DB lookup needed.
|
|
62
|
+
* @param connection - The WebSocket connection to notify
|
|
63
|
+
*/
|
|
64
|
+
private _notifyStreamResuming;
|
|
65
|
+
/**
|
|
66
|
+
* Send stream chunks to a connection after receiving ACK.
|
|
67
|
+
* @param connection - The WebSocket connection
|
|
68
|
+
* @param streamId - The stream to replay
|
|
69
|
+
* @param requestId - The original request ID
|
|
70
|
+
*/
|
|
71
|
+
private _sendStreamChunks;
|
|
72
|
+
/**
|
|
73
|
+
* Buffer a stream chunk for batch write to SQLite.
|
|
74
|
+
* @param streamId - The stream this chunk belongs to
|
|
75
|
+
* @param body - The serialized chunk body
|
|
76
|
+
*/
|
|
77
|
+
private _storeStreamChunk;
|
|
78
|
+
/**
|
|
79
|
+
* Flush buffered chunks to SQLite in a single batch.
|
|
80
|
+
* Uses a lock to prevent concurrent flush operations.
|
|
81
|
+
*/
|
|
82
|
+
private _flushChunkBuffer;
|
|
83
|
+
/**
|
|
84
|
+
* Start tracking a new stream for resumable streaming.
|
|
85
|
+
* Creates metadata entry in SQLite and sets up tracking state.
|
|
86
|
+
* @param requestId - The unique ID of the chat request
|
|
87
|
+
* @returns The generated stream ID
|
|
88
|
+
*/
|
|
89
|
+
private _startStream;
|
|
90
|
+
/**
|
|
91
|
+
* Mark a stream as completed and flush any pending chunks.
|
|
92
|
+
* @param streamId - The stream to mark as completed
|
|
93
|
+
*/
|
|
94
|
+
private _completeStream;
|
|
95
|
+
/**
|
|
96
|
+
* Clean up old completed streams if enough time has passed since last cleanup.
|
|
97
|
+
* This prevents database growth while avoiding cleanup overhead on every stream completion.
|
|
98
|
+
*/
|
|
99
|
+
private _maybeCleanupOldStreams;
|
|
30
100
|
private _broadcastChatMessage;
|
|
31
|
-
|
|
101
|
+
private _loadMessagesFromDb;
|
|
32
102
|
onRequest(request: Request): Promise<Response>;
|
|
33
103
|
private _tryCatchChat;
|
|
34
104
|
/**
|
|
@@ -44,16 +114,20 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
44
114
|
}
|
|
45
115
|
): Promise<Response | undefined>;
|
|
46
116
|
/**
|
|
47
|
-
* Save messages on the server side
|
|
117
|
+
* Save messages on the server side
|
|
48
118
|
* @param messages Chat messages to save
|
|
49
119
|
*/
|
|
50
|
-
saveMessages(messages:
|
|
120
|
+
saveMessages(messages: UIMessage[]): Promise<void>;
|
|
51
121
|
persistMessages(
|
|
52
|
-
messages:
|
|
122
|
+
messages: UIMessage[],
|
|
53
123
|
excludeBroadcastIds?: string[]
|
|
54
124
|
): Promise<void>;
|
|
55
|
-
private _messagesNotAlreadyInAgent;
|
|
56
125
|
private _reply;
|
|
126
|
+
/**
|
|
127
|
+
* Mark a stream as errored and clean up state.
|
|
128
|
+
* @param streamId - The stream to mark as errored
|
|
129
|
+
*/
|
|
130
|
+
private _markStreamError;
|
|
57
131
|
/**
|
|
58
132
|
* For the given message id, look up its associated AbortController
|
|
59
133
|
* If the AbortController does not exist, create and store one in memory
|
|
@@ -74,9 +148,10 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
74
148
|
*/
|
|
75
149
|
private _destroyAbortControllers;
|
|
76
150
|
/**
|
|
77
|
-
* When the DO is destroyed, cancel all pending requests
|
|
151
|
+
* When the DO is destroyed, cancel all pending requests and clean up resources
|
|
78
152
|
*/
|
|
79
153
|
destroy(): Promise<void>;
|
|
80
154
|
}
|
|
81
|
-
|
|
155
|
+
//#endregion
|
|
82
156
|
export { AIChatAgent };
|
|
157
|
+
//# sourceMappingURL=ai-chat-agent.d.ts.map
|