feat: add Agent.keepAliveWhile() scoped callback API#1033
Merged
Conversation
|
| Name | Type |
|---|---|
| agents | Minor |
| @cloudflare/ai-chat | Patch |
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
keepAliveWhile(fn)to theAgentclass — a scoped version ofkeepAlive()that runs an async function while keeping the Durable Object alive, and automatically cleans up the heartbeat when the function completes (or throws).This builds on the
keepAlive()method (from #1029) and is now the recommended way to prevent idle eviction during long-running work.Motivation
The manual
keepAlive()+ disposer pattern requires developers to remember to call the disposer in afinallyblock.keepAliveWhile(fn)makes this structural — you cannot forget to clean up:Both APIs remain available.
keepAlive()is still useful when the lifetime spans multiple event callbacks (e.g.,onConnect→onClose).Changes
packages/agents/src/index.tskeepAliveWhile<T>(fn)methodpackages/ai-chat/src/index.ts_reply()now useskeepAliveWhile()instead of manualkeepAlive()+.finally()packages/agents/src/experimental/forever.tskeepAliveWhiletoAgentLikePick typepackages/ai-chat/src/experimental/forever.tskeepAliveWhiletoAIChatAgentLikePick typepackages/agents/src/tests/agents/keep-alive.tsrunWithKeepAliveWhile()andrunWithKeepAliveWhileError()test methodspackages/agents/src/tests/keep-alive.test.tsdocs/scheduling.mdkeepAliveWhile()in the "Keeping the Agent Alive" section and API referencedocs/agent-class.mdkeepAliveWhile.changeset/keep-alive-agent.mdNotes for reviewers
keepAliveWhileis intentionally simple — 7 lines, delegates entirely tokeepAlive()with try/finally. No new scheduling primitives._reply()is now cleaner:this.keepAliveWhile(() => this._tryCatchChat(...))replaces the previouskeepAlive()+.finally(() => dispose())pattern.keepAlive()directly in_startFiber— this is intentional because the disposer needs to be passed through the retry loop across multiple_runFiberiterations.keepAliveWhiledoes not fit that pattern.keepAliveWhile<T>(fn: () => Promise<T>): Promise<T>preserves the caller's return type, soconst result = await this.keepAliveWhile(async () => 42)correctly infersnumber.Testing
keepAlive(), 2 forkeepAliveWhile())