agents 0.20.0 → 0.21.0

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.
Files changed (87) hide show
  1. package/README.md +50 -12
  2. package/dist/{agent-tool-types-Btk9ETS-.d.ts → agent-tool-types-CzGGB-20.d.ts} +403 -115
  3. package/dist/agent-tool-types.d.ts +1 -1
  4. package/dist/{agent-tools-UuScsJg3.d.ts → agent-tools-zR2d5uij.d.ts} +2 -2
  5. package/dist/agent-tools.d.ts +24 -8
  6. package/dist/agent-tools.js +14 -6
  7. package/dist/agent-tools.js.map +1 -1
  8. package/dist/browser/ai.d.ts +5 -3
  9. package/dist/browser/ai.js +86 -7
  10. package/dist/browser/ai.js.map +1 -1
  11. package/dist/browser/index.d.ts +1 -1
  12. package/dist/browser/index.js +1 -1
  13. package/dist/browser/tanstack-ai.js +13 -1
  14. package/dist/browser/tanstack-ai.js.map +1 -1
  15. package/dist/chat/index.d.ts +31 -2
  16. package/dist/chat/index.js +57 -2
  17. package/dist/chat/index.js.map +1 -1
  18. package/dist/chat/react.d.ts +5 -179
  19. package/dist/chat/react.js +18 -555
  20. package/dist/chat/react.js.map +1 -1
  21. package/dist/chat/transport.d.ts +10 -0
  22. package/dist/chat/transport.js +2 -0
  23. package/dist/chat-sdk/index.d.ts +1 -1
  24. package/dist/client.d.ts +1 -1
  25. package/dist/cloudflare-BduZwmYK.js +204 -0
  26. package/dist/cloudflare-BduZwmYK.js.map +1 -0
  27. package/dist/{connector-v2M1zlZp.d.ts → connector-CkQD4MK3.d.ts} +20 -3
  28. package/dist/{connector-KEJnl6e5.js → connector-CptFKzRh.js} +158 -40
  29. package/dist/connector-CptFKzRh.js.map +1 -0
  30. package/dist/{handler-stateless-8hQN_kC3.js → handler-stateless-CIkKPETH.js} +20 -69
  31. package/dist/handler-stateless-CIkKPETH.js.map +1 -0
  32. package/dist/index.d.ts +21 -11
  33. package/dist/index.js +31 -21
  34. package/dist/index.js.map +1 -1
  35. package/dist/mcp/client.d.ts +20 -20
  36. package/dist/mcp/index.d.ts +35 -35
  37. package/dist/mcp/index.js +32 -98
  38. package/dist/mcp/index.js.map +1 -1
  39. package/dist/mcp/server.js +1 -1
  40. package/dist/observability/ai/index.d.ts +10 -131
  41. package/dist/observability/ai/index.js +209 -444
  42. package/dist/observability/ai/index.js.map +1 -1
  43. package/dist/{wire-types-CU9rLoeS.js → protocol-Dqc2MQxo.js} +2 -46
  44. package/dist/protocol-Dqc2MQxo.js.map +1 -0
  45. package/dist/react.d.ts +1 -1
  46. package/dist/react.js +5 -8
  47. package/dist/react.js.map +1 -1
  48. package/dist/serializable.d.ts +1 -1
  49. package/dist/sub-routing.d.ts +18 -6
  50. package/dist/sub-routing.js +92 -2
  51. package/dist/sub-routing.js.map +1 -1
  52. package/dist/wire-types-CnMt6_HR.js +47 -0
  53. package/dist/wire-types-CnMt6_HR.js.map +1 -0
  54. package/dist/workflow-types.d.ts +25 -25
  55. package/dist/workflow-types.js.map +1 -1
  56. package/dist/workflows.d.ts +22 -22
  57. package/dist/ws-chat-transport-CIoOBbO7.js +561 -0
  58. package/dist/ws-chat-transport-CIoOBbO7.js.map +1 -0
  59. package/dist/ws-chat-transport-UNRIS2xl.d.ts +184 -0
  60. package/docs/adding-to-existing-project.md +4 -2
  61. package/docs/agent-class.md +1 -1
  62. package/docs/agent-tools.md +29 -0
  63. package/docs/browse-the-web.md +16 -1
  64. package/docs/chat-agents.md +3 -1
  65. package/docs/client-sdk.md +12 -8
  66. package/docs/configuration.md +7 -1
  67. package/docs/cross-domain-authentication.md +7 -35
  68. package/docs/email.md +2 -13
  69. package/docs/human-in-the-loop.md +15 -12
  70. package/docs/long-running-agents.md +11 -11
  71. package/docs/mcp-client.md +1 -1
  72. package/docs/mcp-servers.md +7 -1
  73. package/docs/mcp-transports.md +18 -12
  74. package/docs/migration-to-ai-sdk-v5.md +2 -2
  75. package/docs/migration-to-ai-sdk-v6.md +5 -1
  76. package/docs/observability.md +24 -67
  77. package/docs/routing.md +27 -0
  78. package/docs/sub-agents.md +47 -2
  79. package/docs/webhooks.md +109 -136
  80. package/docs/workflows.md +9 -1
  81. package/package.json +19 -10
  82. package/dist/cloudflare-BldFV0Pa.js +0 -117
  83. package/dist/cloudflare-BldFV0Pa.js.map +0 -1
  84. package/dist/connector-KEJnl6e5.js.map +0 -1
  85. package/dist/handler-stateless-8hQN_kC3.js.map +0 -1
  86. package/dist/wire-types-CU9rLoeS.js.map +0 -1
  87. package/dist/workflow-types-Baz_PO5v.d.ts +0 -280
package/README.md CHANGED
@@ -16,6 +16,11 @@ Or add to an existing project:
16
16
  npm install agents
17
17
  ```
18
18
 
19
+ The examples below use `@callable()` decorators. Vite projects must extend
20
+ `agents/tsconfig` and add the `agents/vite` plugin; other bundlers must support
21
+ TC39 decorators (`2023-11`). See
22
+ [Adding Agents to an Existing Project](../../docs/agents/adding-to-existing-project.md#4-configure-typescript-and-vite).
23
+
19
24
  ---
20
25
 
21
26
  ## Why Agents, Why Now
@@ -44,7 +49,7 @@ A counter agent with real-time state sync and callable methods:
44
49
  // server.ts
45
50
  import { Agent, callable } from "agents";
46
51
 
47
- type State = { count: number };
52
+ export type State = { count: number };
48
53
 
49
54
  export class CounterAgent extends Agent<Env, State> {
50
55
  initialState: State = { count: 0 };
@@ -67,11 +72,12 @@ export class CounterAgent extends Agent<Env, State> {
67
72
  // client.tsx
68
73
  import { useAgent } from "agents/react";
69
74
  import { useState } from "react";
75
+ import type { CounterAgent, State } from "./server";
70
76
 
71
77
  function Counter() {
72
78
  const [count, setCount] = useState(0);
73
79
 
74
- const agent = useAgent<State>({
80
+ const agent = useAgent<CounterAgent, State>({
75
81
  agent: "counter-agent",
76
82
  name: "my-counter",
77
83
  onStateUpdate: (state) => setCount(state.count)
@@ -120,8 +126,12 @@ Platform Observability · Cross-domain auth · Resumable streams
120
126
  State persists across requests and syncs to all connected clients:
121
127
 
122
128
  ```typescript
123
- export class MyAgent extends Agent<Env, { items: string[] }> {
124
- initialState = { items: [] };
129
+ import { Agent, callable, type Connection } from "agents";
130
+
131
+ type State = { items: string[] };
132
+
133
+ export class MyAgent extends Agent<Env, State> {
134
+ initialState: State = { items: [] };
125
135
 
126
136
  @callable()
127
137
  addItem(item: string) {
@@ -262,6 +272,29 @@ export class Assistant extends Think<Env> {
262
272
  }
263
273
  ```
264
274
 
275
+ `inputSchema` accepts schemas supported by the AI SDK, including Zod, Standard
276
+ JSON Schema-compatible schemas, raw JSON Schema through `jsonSchema()`, and
277
+ schemas exposed through AI SDK adapters. For example, use `@ai-sdk/valibot` v2
278
+ with AI SDK 6 or v3 with AI SDK 7:
279
+
280
+ ```typescript
281
+ import { valibotSchema } from "@ai-sdk/valibot";
282
+ import * as v from "valibot";
283
+
284
+ const researchInput = valibotSchema(
285
+ v.object({ query: v.pipe(v.string(), v.minLength(3)) })
286
+ );
287
+
288
+ agentTool(Researcher, {
289
+ description: "Research one topic in depth.",
290
+ inputSchema: researchInput
291
+ });
292
+ ```
293
+
294
+ Tool inputs need model-facing JSON Schema in addition to runtime validation. A
295
+ validation-only Standard Schema is therefore insufficient; use its Standard
296
+ JSON Schema extension or an AI SDK adapter.
297
+
265
298
  For deterministic fan-out, call `this.runAgentTool(Researcher, { input })`
266
299
  directly. Parent recovery reconciles stale child rows after restarts and marks
267
300
  unrecoverable runs `interrupted` instead of hanging. In React, use
@@ -294,7 +327,9 @@ async onClose(connection: Connection) {
294
327
  Agents can receive and respond to emails:
295
328
 
296
329
  ```typescript
297
- async onEmail(email: EmailMessage) {
330
+ import type { AgentEmail } from "agents/email";
331
+
332
+ async onEmail(email: AgentEmail) {
298
333
  const from = email.from;
299
334
  const subject = email.headers.get("subject");
300
335
  // Process incoming email
@@ -392,15 +427,18 @@ For AI-powered chat experiences with persistent conversations, streaming respons
392
427
 
393
428
  ```typescript
394
429
  import { AIChatAgent } from "@cloudflare/ai-chat";
430
+ import { createWorkersAI } from "workers-ai-provider";
431
+ import { convertToModelMessages, streamText } from "ai";
395
432
 
396
433
  export class ChatAgent extends AIChatAgent<Env> {
397
- async onChatMessage(onFinish) {
398
- return streamText({
399
- model: openai("gpt-4o"),
400
- messages: this.messages,
401
- tools: this.tools,
402
- onFinish
434
+ async onChatMessage() {
435
+ const workersai = createWorkersAI({ binding: this.env.AI });
436
+ const result = streamText({
437
+ model: workersai("@cf/moonshotai/kimi-k2.7-code"),
438
+ messages: await convertToModelMessages(this.messages)
403
439
  });
440
+
441
+ return result.toUIMessageStreamResponse();
404
442
  }
405
443
  }
406
444
  ```
@@ -409,7 +447,7 @@ export class ChatAgent extends AIChatAgent<Env> {
409
447
  // Client
410
448
  import { useAgentChat } from "@cloudflare/ai-chat/react";
411
449
 
412
- const { messages, input, handleSubmit } = useAgentChat({
450
+ const { messages, sendMessage } = useAgentChat({
413
451
  agent: useAgent({ agent: "chat-agent" })
414
452
  });
415
453
  ```