agents 0.0.0-5ebaeb2 → 0.0.0-603b825
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 +108 -8
- package/dist/ai-chat-agent.d.ts +91 -19
- package/dist/ai-chat-agent.js +789 -343
- 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 +7 -4
- package/dist/ai-chat-v5-migration.js +3 -19
- package/dist/ai-react.d.ts +25 -23
- package/dist/ai-react.js +390 -304
- 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 -91
- package/dist/ai-types.js +3 -7
- 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-BZ-xTxF5.js +901 -0
- package/dist/client-BZ-xTxF5.js.map +1 -0
- package/dist/client-ctTw3KHG.d.ts +1440 -0
- package/dist/client.d.ts +11 -92
- package/dist/client.js +4 -12
- 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-Cs9QpXYp.js +93 -0
- package/dist/do-oauth-client-provider-Cs9QpXYp.js.map +1 -0
- package/dist/do-oauth-client-provider-UhQDpDb8.d.ts +134 -0
- package/dist/index-BUle9RiP.d.ts +58 -0
- package/dist/index-D2dnUH0r.d.ts +578 -0
- package/dist/index.d.ts +56 -550
- package/dist/index.js +7 -31
- package/dist/mcp/client.d.ts +4 -11
- package/dist/mcp/client.js +4 -9
- package/dist/mcp/do-oauth-client-provider.d.ts +2 -42
- package/dist/mcp/do-oauth-client-provider.js +3 -7
- package/dist/mcp/index.d.ts +188 -92
- package/dist/mcp/index.js +1426 -1012
- 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 -46
- package/dist/observability/index.js +7 -11
- package/dist/react-B_ENAdCe.d.ts +113 -0
- package/dist/react.d.ts +10 -123
- package/dist/react.js +183 -112
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +13 -10
- package/dist/schedule.js +43 -31
- 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-C6rC6ZpH.js +1248 -0
- package/dist/src-C6rC6ZpH.js.map +1 -0
- package/package.json +71 -33
- package/dist/ai-chat-v5-migration.js.map +0 -1
- package/dist/ai-types.js.map +0 -1
- package/dist/chunk-AVYJQSLW.js +0 -17
- package/dist/chunk-AVYJQSLW.js.map +0 -1
- package/dist/chunk-IJPBZOSS.js +0 -1296
- package/dist/chunk-IJPBZOSS.js.map +0 -1
- package/dist/chunk-LL2AFX7V.js +0 -109
- package/dist/chunk-LL2AFX7V.js.map +0 -1
- package/dist/chunk-QEVM4BVL.js +0 -116
- package/dist/chunk-QEVM4BVL.js.map +0 -1
- package/dist/chunk-UJVEAURM.js +0 -150
- package/dist/chunk-UJVEAURM.js.map +0 -1
- package/dist/chunk-VYENMKFS.js +0 -612
- package/dist/chunk-VYENMKFS.js.map +0 -1
- package/dist/client-CcIORE73.d.ts +0 -4607
- 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 -1947
package/README.md
CHANGED
|
@@ -342,16 +342,114 @@ export class DialogueAgent extends AIChatAgent {
|
|
|
342
342
|
// messages: this.messages,
|
|
343
343
|
// });
|
|
344
344
|
//
|
|
345
|
-
// //
|
|
346
|
-
//
|
|
347
|
-
//
|
|
348
|
-
//
|
|
349
|
-
//
|
|
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
|
+
// }
|
|
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
|
+
});
|
|
351
400
|
}
|
|
352
401
|
}
|
|
353
402
|
```
|
|
354
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
|
+
|
|
355
453
|
#### Creating the Interface
|
|
356
454
|
|
|
357
455
|
Connect with your agent through a React interface:
|
|
@@ -451,10 +549,12 @@ export class MyMCP extends McpAgent<Env, State, {}> {
|
|
|
451
549
|
};
|
|
452
550
|
});
|
|
453
551
|
|
|
454
|
-
this.server.
|
|
552
|
+
this.server.registerTool(
|
|
455
553
|
"add",
|
|
456
|
-
|
|
457
|
-
|
|
554
|
+
{
|
|
555
|
+
description: "Add to the counter, stored in the MCP",
|
|
556
|
+
inputSchema: { a: z.number() }
|
|
557
|
+
},
|
|
458
558
|
async ({ a }) => {
|
|
459
559
|
this.setState({ ...this.state, counter: this.state.counter + a });
|
|
460
560
|
|
package/dist/ai-chat-agent.d.ts
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import "
|
|
5
|
-
import "
|
|
6
|
-
import "
|
|
7
|
-
import "
|
|
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";
|
|
1
|
+
import "./client-ctTw3KHG.js";
|
|
2
|
+
import "./mcp-BwPscEiF.js";
|
|
3
|
+
import "./do-oauth-client-provider-UhQDpDb8.js";
|
|
4
|
+
import "./index-BUle9RiP.js";
|
|
5
|
+
import "./ai-types-81H_-Uxh.js";
|
|
6
|
+
import { n as AgentContext, t as Agent } from "./index-D2dnUH0r.js";
|
|
7
|
+
import { StreamTextOnFinishCallback, ToolSet, UIMessage } from "ai";
|
|
17
8
|
|
|
9
|
+
//#region src/ai-chat-agent.d.ts
|
|
18
10
|
/**
|
|
19
11
|
* Extension of Agent with built-in chat capabilities
|
|
20
12
|
* @template Env Environment type containing bindings
|
|
@@ -28,11 +20,85 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
28
20
|
* useful to propagate request cancellation signals for any external calls made by the agent
|
|
29
21
|
*/
|
|
30
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;
|
|
31
49
|
/** Array of chat messages for the current conversation */
|
|
32
50
|
messages: UIMessage[];
|
|
33
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;
|
|
34
100
|
private _broadcastChatMessage;
|
|
35
|
-
|
|
101
|
+
private _loadMessagesFromDb;
|
|
36
102
|
onRequest(request: Request): Promise<Response>;
|
|
37
103
|
private _tryCatchChat;
|
|
38
104
|
/**
|
|
@@ -57,6 +123,11 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
57
123
|
excludeBroadcastIds?: string[]
|
|
58
124
|
): Promise<void>;
|
|
59
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;
|
|
60
131
|
/**
|
|
61
132
|
* For the given message id, look up its associated AbortController
|
|
62
133
|
* If the AbortController does not exist, create and store one in memory
|
|
@@ -77,9 +148,10 @@ declare class AIChatAgent<Env = unknown, State = unknown> extends Agent<
|
|
|
77
148
|
*/
|
|
78
149
|
private _destroyAbortControllers;
|
|
79
150
|
/**
|
|
80
|
-
* When the DO is destroyed, cancel all pending requests
|
|
151
|
+
* When the DO is destroyed, cancel all pending requests and clean up resources
|
|
81
152
|
*/
|
|
82
153
|
destroy(): Promise<void>;
|
|
83
154
|
}
|
|
84
|
-
|
|
155
|
+
//#endregion
|
|
85
156
|
export { AIChatAgent };
|
|
157
|
+
//# sourceMappingURL=ai-chat-agent.d.ts.map
|