You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add hook-style runtime context lifecycle support in `agents` with `onCreateContext` / `onDestroyContext`, typed `this.context`, and context propagation via `getCurrentAgent().context` and `getCurrentContext()`.
7
-
8
-
Also update `@cloudflare/ai-chat` to keep `context` in the async agent scope during chat/tool execution so nested `getCurrentAgent()` reads stay consistent.
5
+
Add hook-style runtime context lifecycle support in `agents` with `onContextStart` / `onContextEnd`, typed `this.context`, and context propagation via `getCurrentAgent().context` and `getCurrentContext()`.
Copy file name to clipboardExpand all lines: design/context-api.md
+54-52Lines changed: 54 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
-
# `onCreateContext` / `onDestroyContext` API
1
+
# `onContextStart` / `onContextEnd` API
2
2
3
3
> Extensible per-entry-point context for tracing, auth, and observability.
4
4
5
5
## Problem
6
6
7
-
The SDK wraps 9 entry points with an internal `AsyncLocalStorage` but the store shape is fixed. Users who need tracing, OTel, auth context, etc. must:
7
+
The SDK wraps lifecycle entry points with an internal `AsyncLocalStorage` but the store shape is fixed. Users who need tracing, OTel, auth context, etc. must:
8
8
9
9
1. Create a **second**`AsyncLocalStorage`
10
10
2. Manually `.run()` it in every lifecycle hook
@@ -20,16 +20,16 @@ The SDK already does the hard work of wrapping every entry point. Users should p
20
20
```typescript
21
21
classAgent<Env, State, Props> {
22
22
/** Override to provide per-entry-point context. */
return result instanceof Promise ? await result : result;
247
247
}
248
248
```
249
249
250
-
For the sync `withAgentContext` wrapper, only sync return values are supported. Async `onCreateContext` in this path logs a warning and falls back to `undefined`.
250
+
For the sync `withAgentContext` wrapper, only sync return values are supported. Async `onContextStart` in this path logs a warning and falls back to `undefined`.
251
251
252
252
## Branch Scope
253
253
254
254
- No migration shims or aliases required on this branch
255
-
- Hook names are updated directly to `onCreateContext` / `onDestroyContext`
255
+
- Hook names are updated directly to `onContextStart` / `onContextEnd`
This design follows tRPC's `createContext` pattern for the hook, OTel's `context.with` for `withContext`, and Fastify's `onRequestAbort` precedent for `onDestroyContext`.
330
+
This design follows tRPC's `createContext` pattern for the hook, OTel's `context.with` for `withContext`, and Fastify's `onRequestAbort` precedent for `onContextEnd`.
Yes. Both `getCurrentAgent().context` and `getCurrentContext()`.
340
342
341
343
**Q: Does OTel need a cleanup hook?**
342
-
Yes. `onDestroyContext` called in `finally`at every entry point. Separate from `onCreateContext` (no `Disposable` coupling).
344
+
Yes. `onContextEnd` runs in `finally`whenever `onContextStart` produced a non-nullish context value. Separate from `onContextStart` (no `Disposable` coupling).
343
345
344
346
**Q: Module augmentation vs generic?**
345
347
Return-type inference primary. Module augmentation opt-in for `getCurrentContext()` typing.
346
348
347
-
**Q: Sync or async `onCreateContext`?**
349
+
**Q: Sync or async `onContextStart`?**
348
350
Allow async. Sync fast path in `withAgentContext` (auto-wrapped methods).
0 commit comments