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.
- package/README.md +50 -12
- package/dist/{agent-tool-types-Btk9ETS-.d.ts → agent-tool-types-CzGGB-20.d.ts} +403 -115
- package/dist/agent-tool-types.d.ts +1 -1
- package/dist/{agent-tools-UuScsJg3.d.ts → agent-tools-zR2d5uij.d.ts} +2 -2
- package/dist/agent-tools.d.ts +24 -8
- package/dist/agent-tools.js +14 -6
- package/dist/agent-tools.js.map +1 -1
- package/dist/browser/ai.d.ts +5 -3
- package/dist/browser/ai.js +86 -7
- package/dist/browser/ai.js.map +1 -1
- package/dist/browser/index.d.ts +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/tanstack-ai.js +13 -1
- package/dist/browser/tanstack-ai.js.map +1 -1
- package/dist/chat/index.d.ts +31 -2
- package/dist/chat/index.js +57 -2
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/react.d.ts +5 -179
- package/dist/chat/react.js +18 -555
- package/dist/chat/react.js.map +1 -1
- package/dist/chat/transport.d.ts +10 -0
- package/dist/chat/transport.js +2 -0
- package/dist/chat-sdk/index.d.ts +1 -1
- package/dist/client.d.ts +1 -1
- package/dist/cloudflare-BduZwmYK.js +204 -0
- package/dist/cloudflare-BduZwmYK.js.map +1 -0
- package/dist/{connector-v2M1zlZp.d.ts → connector-CkQD4MK3.d.ts} +20 -3
- package/dist/{connector-KEJnl6e5.js → connector-CptFKzRh.js} +158 -40
- package/dist/connector-CptFKzRh.js.map +1 -0
- package/dist/{handler-stateless-8hQN_kC3.js → handler-stateless-CIkKPETH.js} +20 -69
- package/dist/handler-stateless-CIkKPETH.js.map +1 -0
- package/dist/index.d.ts +21 -11
- package/dist/index.js +31 -21
- package/dist/index.js.map +1 -1
- package/dist/mcp/client.d.ts +20 -20
- package/dist/mcp/index.d.ts +35 -35
- package/dist/mcp/index.js +32 -98
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/server.js +1 -1
- package/dist/observability/ai/index.d.ts +10 -131
- package/dist/observability/ai/index.js +209 -444
- package/dist/observability/ai/index.js.map +1 -1
- package/dist/{wire-types-CU9rLoeS.js → protocol-Dqc2MQxo.js} +2 -46
- package/dist/protocol-Dqc2MQxo.js.map +1 -0
- package/dist/react.d.ts +1 -1
- package/dist/react.js +5 -8
- package/dist/react.js.map +1 -1
- package/dist/serializable.d.ts +1 -1
- package/dist/sub-routing.d.ts +18 -6
- package/dist/sub-routing.js +92 -2
- package/dist/sub-routing.js.map +1 -1
- package/dist/wire-types-CnMt6_HR.js +47 -0
- package/dist/wire-types-CnMt6_HR.js.map +1 -0
- package/dist/workflow-types.d.ts +25 -25
- package/dist/workflow-types.js.map +1 -1
- package/dist/workflows.d.ts +22 -22
- package/dist/ws-chat-transport-CIoOBbO7.js +561 -0
- package/dist/ws-chat-transport-CIoOBbO7.js.map +1 -0
- package/dist/ws-chat-transport-UNRIS2xl.d.ts +184 -0
- package/docs/adding-to-existing-project.md +4 -2
- package/docs/agent-class.md +1 -1
- package/docs/agent-tools.md +29 -0
- package/docs/browse-the-web.md +16 -1
- package/docs/chat-agents.md +3 -1
- package/docs/client-sdk.md +12 -8
- package/docs/configuration.md +7 -1
- package/docs/cross-domain-authentication.md +7 -35
- package/docs/email.md +2 -13
- package/docs/human-in-the-loop.md +15 -12
- package/docs/long-running-agents.md +11 -11
- package/docs/mcp-client.md +1 -1
- package/docs/mcp-servers.md +7 -1
- package/docs/mcp-transports.md +18 -12
- package/docs/migration-to-ai-sdk-v5.md +2 -2
- package/docs/migration-to-ai-sdk-v6.md +5 -1
- package/docs/observability.md +24 -67
- package/docs/routing.md +27 -0
- package/docs/sub-agents.md +47 -2
- package/docs/webhooks.md +109 -136
- package/docs/workflows.md +9 -1
- package/package.json +19 -10
- package/dist/cloudflare-BldFV0Pa.js +0 -117
- package/dist/cloudflare-BldFV0Pa.js.map +0 -1
- package/dist/connector-KEJnl6e5.js.map +0 -1
- package/dist/handler-stateless-8hQN_kC3.js.map +0 -1
- package/dist/wire-types-CU9rLoeS.js.map +0 -1
- package/dist/workflow-types-Baz_PO5v.d.ts +0 -280
package/docs/mcp-transports.md
CHANGED
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
This guide explains the different transport options for connecting to MCP servers with the Agents SDK.
|
|
4
4
|
|
|
5
|
-
For a primer on MCP servers
|
|
5
|
+
For a primer on MCP servers, see [Creating MCP Servers](./mcp-servers.md).
|
|
6
6
|
|
|
7
7
|
## Streamable HTTP Transport (Recommended)
|
|
8
8
|
|
|
9
|
-
The
|
|
9
|
+
The Agents SDK has two server paths:
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
- Current SDK v2 servers use `createMcpHandler`, a per-request stateless Worker handler with no Durable Object or WebSocket bridge.
|
|
12
|
+
- Retained SDK v1 servers use the deprecated, feature-frozen `McpAgent`, which provides sessionful transports through a Durable Object and internal WebSocket bridge.
|
|
13
|
+
|
|
14
|
+
The current stateless handler also accepts stateless 2025-era Streamable HTTP requests by default. It does not expose standalone legacy SSE.
|
|
15
|
+
|
|
16
|
+
### Legacy `McpAgent` architecture
|
|
12
17
|
|
|
13
18
|
When a client connects to your MCP server:
|
|
14
19
|
|
|
@@ -44,13 +49,13 @@ The `serve()` method returns a Worker with a `fetch` handler that:
|
|
|
44
49
|
|
|
45
50
|
### Connection from clients
|
|
46
51
|
|
|
47
|
-
|
|
52
|
+
The Agents MCP client uses `"auto"` by default. It tries Streamable HTTP first and falls back to legacy SSE only when the endpoint reports that Streamable HTTP is unsupported:
|
|
48
53
|
|
|
49
54
|
```typescript
|
|
50
55
|
await agent.addMcpServer("my-server", "https://your-worker.workers.dev/mcp");
|
|
51
56
|
```
|
|
52
57
|
|
|
53
|
-
## Auto Transport
|
|
58
|
+
## Legacy `McpAgent` Server Auto Transport
|
|
54
59
|
|
|
55
60
|
The **auto** transport serves both Streamable HTTP and legacy SSE on the same endpoint. Capable clients use Streamable HTTP automatically, while older SSE-only clients continue to work.
|
|
56
61
|
|
|
@@ -60,7 +65,7 @@ export default MyMCP.serve("/mcp", { transport: "auto" });
|
|
|
60
65
|
|
|
61
66
|
The handler distinguishes between the two protocols based on the request shape — no configuration or content negotiation is required from clients. This is useful when migrating from SSE to Streamable HTTP without breaking existing clients.
|
|
62
67
|
|
|
63
|
-
## SSE Transport (Deprecated)
|
|
68
|
+
## Legacy `McpAgent` SSE Transport (Deprecated)
|
|
64
69
|
|
|
65
70
|
We also support the legacy **SSE (Server-Sent Events)** transport, but it is deprecated in favor of Streamable HTTP.
|
|
66
71
|
|
|
@@ -294,12 +299,13 @@ export class MyMCP extends McpAgent<Env, State> {
|
|
|
294
299
|
|
|
295
300
|
## Choosing a transport
|
|
296
301
|
|
|
297
|
-
| Transport
|
|
298
|
-
|
|
|
299
|
-
| **Streamable HTTP**
|
|
300
|
-
| **
|
|
301
|
-
| **
|
|
302
|
-
| **
|
|
302
|
+
| Transport | Use when | Pros | Cons |
|
|
303
|
+
| --------------------------------- | --------------------------------------------------- | ------------------------------------------ | -------------------------------------- |
|
|
304
|
+
| **Streamable HTTP** | New servers and external MCP connections | Current standard transport; supports OAuth | Slight network overhead |
|
|
305
|
+
| **Client `auto`** | The remote server's HTTP transport is not known | Tries Streamable HTTP, then legacy SSE | May make a fallback connection attempt |
|
|
306
|
+
| **Legacy `McpAgent` server auto** | An existing server must accept both HTTP transports | Serves both protocols on one endpoint | Uses the deprecated server path |
|
|
307
|
+
| **RPC** | Internal agents | Fastest, simplest setup | No auth, Service Bindings only |
|
|
308
|
+
| **SSE** | Legacy compatibility | Backwards compatible | Deprecated, use Streamable HTTP |
|
|
303
309
|
|
|
304
310
|
## Examples
|
|
305
311
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
This guide covers the changes needed when upgrading from AI SDK v4 to v5 with `@cloudflare/ai-chat`.
|
|
4
4
|
|
|
5
|
-
>
|
|
5
|
+
> The current `agents` and `@cloudflare/ai-chat` packages support AI SDK v6 and v7, not v5. If an application is still on v4, apply the intermediate changes on this page and then continue through the [v6 migration guide](./migration-to-ai-sdk-v6.md). Do not install AI SDK v5 alongside the current Cloudflare packages.
|
|
6
6
|
|
|
7
7
|
## Message format: `content` to `parts`
|
|
8
8
|
|
|
@@ -71,7 +71,7 @@ chunk.type === "text-delta" && chunk.delta;
|
|
|
71
71
|
|
|
72
72
|
## Migration checklist
|
|
73
73
|
|
|
74
|
-
1.
|
|
74
|
+
1. Apply this intermediate checklist without installing v5, then continue through the [v6 migration guide](./migration-to-ai-sdk-v6.md) and install the supported package majors shown there
|
|
75
75
|
2. Replace `import type { Message }` with `import type { UIMessage }`
|
|
76
76
|
3. Replace `"ai/react"` imports with `"@ai-sdk/react"`
|
|
77
77
|
4. Rename `parameters` to `inputSchema` in tool definitions
|
|
@@ -4,10 +4,14 @@ This guide covers the changes needed when upgrading from AI SDK v5 to v6 with `@
|
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
+
Pin the v6-compatible majors so this migration does not also install AI SDK v7:
|
|
8
|
+
|
|
7
9
|
```bash
|
|
8
|
-
npm install ai
|
|
10
|
+
npm install ai@^6 @ai-sdk/react@^3 @ai-sdk/openai@^3
|
|
9
11
|
```
|
|
10
12
|
|
|
13
|
+
The current `agents` and `@cloudflare/ai-chat` releases support both AI SDK v6 and v7. This guide covers v6 only.
|
|
14
|
+
|
|
11
15
|
## Breaking changes
|
|
12
16
|
|
|
13
17
|
### 1. `convertToModelMessages()` is now async
|
package/docs/observability.md
CHANGED
|
@@ -292,9 +292,9 @@ metadata fields for applications with a different identity model. Payload
|
|
|
292
292
|
storage is off by default. Set `storeMessages` and/or `storeTools` on the Think
|
|
293
293
|
agent to opt in; these are wrapper settings, not span attributes.
|
|
294
294
|
|
|
295
|
-
### AI SDK v6
|
|
295
|
+
### AI SDK v6 and v7
|
|
296
296
|
|
|
297
|
-
Wrap the SDK namespace:
|
|
297
|
+
Wrap the SDK namespace and call the wrapped functions:
|
|
298
298
|
|
|
299
299
|
```ts
|
|
300
300
|
import * as ai from "ai";
|
|
@@ -303,14 +303,17 @@ import { wrapAISDK } from "agents/observability/ai";
|
|
|
303
303
|
const { generateText, streamText } = wrapAISDK(ai);
|
|
304
304
|
```
|
|
305
305
|
|
|
306
|
-
`wrapAISDK` instruments `generateText`,
|
|
307
|
-
`
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
`
|
|
306
|
+
`wrapAISDK` supports AI SDK v6 and v7. It instruments `generateText`,
|
|
307
|
+
`streamText`, `generateObject`, and `streamObject`. You do not need to register a
|
|
308
|
+
telemetry integration for Agents tracing on AI SDK v7.
|
|
309
|
+
|
|
310
|
+
Span names use `{operation} {target}` and fall back to the bare operation past
|
|
311
|
+
64 UTF-8 bytes; the full target remains on its semantic attribute. A model
|
|
312
|
+
object is wrapped with the SDK's `wrapLanguageModel` helper, so provider work is
|
|
313
|
+
a `chat {model}` child of the `invoke_agent {agent}` operation span. Tool
|
|
314
|
+
execution is wrapped as `execute_tool {tool}`. Approval lifecycle segments
|
|
315
|
+
appear as bounded `tool_approval {tool}` children of an `execute_tool {tool}`
|
|
316
|
+
span, correlated by `gen_ai.tool.call.id` and carrying
|
|
314
317
|
`cloudflare.agents.tool.approval.state` (`requested`, `approved`, or `denied`).
|
|
315
318
|
They never remain open while waiting for a human across invocations. Stream
|
|
316
319
|
spans close on completion, cancellation, an in-band error, or early consumer
|
|
@@ -329,46 +332,10 @@ const traced = wrapAISDK(ai, {
|
|
|
329
332
|
});
|
|
330
333
|
```
|
|
331
334
|
|
|
332
|
-
### AI SDK v7
|
|
333
|
-
|
|
334
|
-
AI SDK v7 ships a first-class telemetry lifecycle. Register the adapter once and
|
|
335
|
-
every `generateText`, `streamText`, `generateObject`, and `streamObject` call is
|
|
336
|
-
instrumented:
|
|
337
|
-
|
|
338
|
-
```ts
|
|
339
|
-
import { registerTelemetry } from "ai";
|
|
340
|
-
import { createAISDKTelemetry } from "agents/observability/ai";
|
|
341
|
-
|
|
342
|
-
registerTelemetry(
|
|
343
|
-
createAISDKTelemetry({ storeMessages: true, storeTools: true })
|
|
344
|
-
);
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
Or scope it to a single call through `experimental_telemetry`:
|
|
348
|
-
|
|
349
|
-
```ts
|
|
350
|
-
import { createAISDKTelemetry } from "agents/observability/ai";
|
|
351
|
-
|
|
352
|
-
await generateText({
|
|
353
|
-
model,
|
|
354
|
-
prompt: "...",
|
|
355
|
-
experimental_telemetry: {
|
|
356
|
-
integrations: [createAISDKTelemetry()]
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
The v7 adapter uses `cloudflare.agents.call.id` to correlate operation, model,
|
|
362
|
-
and tool spans. Its execution hooks keep provider work under the `chat` span and
|
|
363
|
-
nested work performed by a tool under the `execute_tool` span. It handles both
|
|
364
|
-
`onEnd` and `onAbort` terminal paths. `wrapAISDK` and `createAISDKTelemetry`
|
|
365
|
-
project into the same span schema, so the two SDK versions are dashboard-
|
|
366
|
-
compatible.
|
|
367
|
-
|
|
368
335
|
When a gateway-backed provider exposes its AI Gateway log ID through response
|
|
369
336
|
headers, provider metadata, or the Workers AI binding, the corresponding
|
|
370
337
|
`chat` span includes `cloudflare.ai_gateway.log.id`. The attribute is omitted
|
|
371
|
-
when no actual response exposes an ID; the
|
|
338
|
+
when no actual response exposes an ID; the wrapper does not infer one or make an
|
|
372
339
|
extra request.
|
|
373
340
|
|
|
374
341
|
### Identity
|
|
@@ -445,13 +412,13 @@ await generateText({
|
|
|
445
412
|
| `gen_ai.usage.cache_creation.input_tokens`, `gen_ai.usage.cache_read.input_tokens` | Provider cache usage when reported |
|
|
446
413
|
| `gen_ai.usage.reasoning.output_tokens` | Reasoning output usage when reported |
|
|
447
414
|
| `gen_ai.tool.name`, `gen_ai.tool.type`, `gen_ai.tool.call.id` | Tool identity; call ID also correlates approval lifecycle segments |
|
|
448
|
-
| `cloudflare.agents.tool.approval.state` |
|
|
415
|
+
| `cloudflare.agents.tool.approval.state` | Approval lifecycle segment: `requested`, `approved`, or `denied` |
|
|
449
416
|
| `gen_ai.input.messages`, `gen_ai.output.messages` | Opt-in OTel-schema model messages on `chat`, including tool parts and output finish reasons |
|
|
450
417
|
| `gen_ai.tool.call.arguments`, `gen_ai.tool.call.result` | Opt-in tool arguments/results on `execute_tool` |
|
|
451
418
|
| `user.id` | Explicit v6 metadata key `user.id` |
|
|
452
419
|
| `error.type` | Low-cardinality error class; raw error messages are never recorded |
|
|
453
420
|
|
|
454
|
-
The
|
|
421
|
+
The wrapper also emits a small vendor namespace where no standard equivalent
|
|
455
422
|
exists:
|
|
456
423
|
|
|
457
424
|
| Attribute | Meaning |
|
|
@@ -459,28 +426,27 @@ exists:
|
|
|
459
426
|
| `cloudflare.ai_gateway.log.id` | AI Gateway log reference on `chat`, when exposed by the actual response |
|
|
460
427
|
| `cloudflare.agents.integration.name` | Instrumentation source (`ai-sdk`) |
|
|
461
428
|
| `cloudflare.agents.operation.name` | Original SDK operation (`streamText`, `doStream`, `tool.execute`, etc.) |
|
|
462
|
-
| `cloudflare.agents.call.id` | AI SDK v7 callback correlation ID |
|
|
463
429
|
| `cloudflare.agents.response.finish_reason` | One finish reason as a scalar |
|
|
464
430
|
| `cloudflare.agents.tool.count` | Precomputed tool-call count for dashboards |
|
|
465
431
|
| `cloudflare.agents.usage.total_tokens` | Provider total, or input plus output when both are known |
|
|
466
432
|
| `cloudflare.agents.runtime_context.{key}` | Explicitly included scalar runtime context |
|
|
467
|
-
| `cloudflare.agents.tool_context.{tool}.{key}` | Explicitly included scalar context on the executed tool span |
|
|
468
433
|
| `cloudflare.agents.metadata.{key}` | Other scalar v6 telemetry metadata |
|
|
469
|
-
| `cloudflare.agents.turn.{request_id,trigger,admission,channel,continuation,generation}` | Think turn context
|
|
434
|
+
| `cloudflare.agents.turn.{request_id,trigger,admission,channel,continuation,generation}` | Think turn context, from v6 metadata or v7 runtime context |
|
|
470
435
|
| `cloudflare.agents.canceled` | Recognized cancellation, not a failure |
|
|
436
|
+
| `cloudflare.agents.span.truncated` | Span closed by its invocation ending before the work finished |
|
|
471
437
|
|
|
472
438
|
`gen_ai.response.finish_reasons` and `gen_ai.request.stop_sequences` are arrays
|
|
473
439
|
in OTel. Workers' custom `Span.setAttribute` currently accepts only a string,
|
|
474
|
-
number, or boolean, so the
|
|
440
|
+
number, or boolean, so the wrapper omits those attributes rather than placing
|
|
475
441
|
JSON text under an array-typed key. Similarly, span status is state rather than
|
|
476
|
-
an attribute: failures emit `error.type`, but the
|
|
442
|
+
an attribute: failures emit `error.type`, but the wrapper does not invent an
|
|
477
443
|
`otel.status_code` attribute when Workers exposes no custom-span status setter.
|
|
478
444
|
|
|
479
445
|
### Context and safety
|
|
480
446
|
|
|
481
447
|
Payload storage is explicit and off by default. `storeMessages` writes only
|
|
482
448
|
`gen_ai.input.messages` / `gen_ai.output.messages` on `chat`; when the message
|
|
483
|
-
attribute exceeds its budget, the
|
|
449
|
+
attribute exceeds its budget, the wrapper repeatedly drops the oldest
|
|
484
450
|
unprotected message (index 2), preserving the first two messages and newest
|
|
485
451
|
tail. `storeTools` writes only `gen_ai.tool.call.arguments` /
|
|
486
452
|
`gen_ai.tool.call.result` on `execute_tool`. The flags themselves are never
|
|
@@ -488,7 +454,7 @@ written to telemetry metadata or spans.
|
|
|
488
454
|
|
|
489
455
|
System instructions that the AI SDK presents to the model as a system-role chat
|
|
490
456
|
message remain in `gen_ai.input.messages`, which OTel explicitly permits for
|
|
491
|
-
instructions that are part of chat history. The
|
|
457
|
+
instructions that are part of chat history. The wrapper does not separately
|
|
492
458
|
copy the raw `system` parameter into `gen_ai.system_instructions`. Schemas,
|
|
493
459
|
request headers, provider options, and raw error messages are never recorded.
|
|
494
460
|
The optional AI Gateway reference is a bounded opaque log ID; response headers
|
|
@@ -513,27 +479,18 @@ await traced.generateText({
|
|
|
513
479
|
});
|
|
514
480
|
```
|
|
515
481
|
|
|
516
|
-
For v7,
|
|
517
|
-
|
|
482
|
+
For v7, put application context in `runtimeContext` and select telemetry-visible
|
|
483
|
+
keys with the AI SDK's boolean-map allowlist:
|
|
518
484
|
|
|
519
485
|
```ts
|
|
520
486
|
await generateText({
|
|
521
487
|
model,
|
|
522
488
|
prompt: "Will I need an umbrella?",
|
|
523
489
|
runtimeContext: { requestId: "req-123", tenantId: "tenant-42" },
|
|
524
|
-
toolsContext: {
|
|
525
|
-
weather: { defaultUnit: "celsius", cacheHit: true }
|
|
526
|
-
},
|
|
527
490
|
telemetry: {
|
|
528
491
|
includeRuntimeContext: {
|
|
529
492
|
requestId: true,
|
|
530
493
|
tenantId: true
|
|
531
|
-
},
|
|
532
|
-
includeToolsContext: {
|
|
533
|
-
weather: {
|
|
534
|
-
defaultUnit: true,
|
|
535
|
-
cacheHit: true
|
|
536
|
-
}
|
|
537
494
|
}
|
|
538
495
|
}
|
|
539
496
|
});
|
package/docs/routing.md
CHANGED
|
@@ -96,6 +96,33 @@ export default {
|
|
|
96
96
|
|
|
97
97
|
---
|
|
98
98
|
|
|
99
|
+
## Building Agent URLs
|
|
100
|
+
|
|
101
|
+
Use `buildAgentPath()` when a server, external provider, or client needs the canonical pathname for a known root-first Agent identity. It handles the top-level route and every nested `/sub/...` hop:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { buildAgentPath, buildAgentUrl } from "agents";
|
|
105
|
+
|
|
106
|
+
const address = [
|
|
107
|
+
{ className: "Inbox", name: userId },
|
|
108
|
+
{ className: "Chat", name: chatId }
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
buildAgentPath(address, { leafPath: "/callbacks/job" });
|
|
112
|
+
// /agents/inbox/{userId}/sub/chat/{chatId}/callbacks/job
|
|
113
|
+
|
|
114
|
+
buildAgentUrl("https://app.example.com", address, {
|
|
115
|
+
leafPath: "/callbacks/job"
|
|
116
|
+
});
|
|
117
|
+
// URL("https://app.example.com/agents/inbox/...")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Inside an Agent, `this.selfPath` has the required root-first shape. If the root Durable Object binding name differs from its class name, pass that binding name as `rootBinding`. The pathname works for both HTTP and WebSocket traffic. For a custom top-level prefix, pass the same `prefix` to `buildAgentPath()` and `routeAgentRequest()`.
|
|
121
|
+
|
|
122
|
+
See [Sub-agents](./sub-agents.md#direct-http-and-websocket-urls) for callbacks, webhooks, custom routing, and name-encoding details.
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
99
126
|
## Instance Naming Patterns
|
|
100
127
|
|
|
101
128
|
The instance name (the last part of the URL) determines which agent instance handles the request. Each unique name gets its own isolated agent with its own state.
|
package/docs/sub-agents.md
CHANGED
|
@@ -273,7 +273,52 @@ const chat = useAgent({
|
|
|
273
273
|
|
|
274
274
|
Every other `useAgent` feature works as usual: `state` sync, `stub.method()` calls, `@callable` RPCs, `useAgentChat` on top of the returned socket.
|
|
275
275
|
|
|
276
|
-
### Direct HTTP
|
|
276
|
+
### Direct HTTP and WebSocket URLs
|
|
277
|
+
|
|
278
|
+
Use `buildAgentPath()` to turn a root-first Agent identity into the canonical URL pathname used by both HTTP requests and WebSocket connections:
|
|
279
|
+
|
|
280
|
+
```typescript
|
|
281
|
+
import { buildAgentPath } from "agents";
|
|
282
|
+
|
|
283
|
+
const path = buildAgentPath(
|
|
284
|
+
[
|
|
285
|
+
{ className: "Inbox", name: userId },
|
|
286
|
+
{ className: "Chat", name: chatId }
|
|
287
|
+
],
|
|
288
|
+
{ leafPath: "/callbacks/job" }
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
// /agents/inbox/{userId}/sub/chat/{chatId}/callbacks/job
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
Inside an Agent, pass `this.selfPath` directly. If the root Durable Object binding name differs from its class name, also pass `rootBinding` in the options. `buildAgentUrl()` adds a public origin, which is useful when registering callbacks, webhooks, approval URLs, or asynchronous job-completion URLs with an external system:
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
import { buildAgentUrl } from "agents";
|
|
298
|
+
|
|
299
|
+
export class Chat extends Agent<Env> {
|
|
300
|
+
callbackUrl() {
|
|
301
|
+
return buildAgentUrl(this.env.PUBLIC_ORIGIN, this.selfPath, {
|
|
302
|
+
leafPath: "/callbacks/job"
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
override async onRequest(request: Request) {
|
|
307
|
+
if (new URL(request.url).pathname === "/callbacks/job") {
|
|
308
|
+
return this.handleJobCallback(request);
|
|
309
|
+
}
|
|
310
|
+
return new Response("Not found", { status: 404 });
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
The Worker must pass the incoming request to `routeAgentRequest()`. Each ancestor's `onBeforeSubAgent` hook runs before the destination receives the request. For a sub-agent destination, the nested `/sub/...` routing segments are removed during forwarding, so its pathname is the `leafPath` suffix.
|
|
316
|
+
|
|
317
|
+
`buildAgentUrl()` accepts an HTTP(S) or WS(S) origin without a pathname, query, fragment, or credentials. Set callback query parameters through the returned URL's `searchParams`. If you use a custom routing prefix, pass the same value to both `buildAgentPath()` and `routeAgentRequest()`.
|
|
318
|
+
|
|
319
|
+
Root Agent names follow PartyServer's existing raw pathname-segment behavior and must already be externally routable. The `sub` segment is reserved in routing prefixes, class and binding names, and root Agent names. Descendant names are URL-encoded by the helper, so names containing spaces, Unicode, `/`, or URL-reserved characters round-trip safely.
|
|
320
|
+
|
|
321
|
+
### Custom routing
|
|
277
322
|
|
|
278
323
|
For fetch handlers that do their own top-level URL parsing, use `routeSubAgentRequest` to dispatch a request into a sub-agent from an already-resolved parent stub:
|
|
279
324
|
|
|
@@ -293,7 +338,7 @@ export default {
|
|
|
293
338
|
};
|
|
294
339
|
```
|
|
295
340
|
|
|
296
|
-
`fromPath` takes the sub-agent tail (something like `/sub/chat/chat-abc/...`). The helper parses
|
|
341
|
+
`fromPath` takes any pathname containing the sub-agent tail (something like `/sub/chat/chat-abc/...`). When the destination is already represented as a root-first Agent path, pass the result of `buildAgentPath()` directly. The helper parses the first child hop, runs the parent's `onBeforeSubAgent` hook, and forwards into the facet.
|
|
297
342
|
|
|
298
343
|
### External typed RPC
|
|
299
344
|
|