-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Agent callbacks #1316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Agent callbacks #1316
Conversation
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
🦋 Changeset detectedLatest commit: bc1d2a6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Greptile OverviewGreptile SummaryAdds lifecycle callback support to Stagehand agents, enabling users to hook into various stages of agent execution for both streaming and non-streaming modes. Key Changes:
Implementation Quality:
Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant V3
participant V3AgentHandler
participant LLMClient
participant AISDKCallbacks
User->>V3: agent({ stream: true/false })
V3-->>User: AgentInstance
User->>V3: agent.execute({ instruction, callbacks })
V3->>V3: prepareAgentExecution()
V3->>V3AgentHandler: new V3AgentHandler()
alt Non-streaming (stream: false)
V3->>V3AgentHandler: execute(instructionOrOptions)
V3AgentHandler->>V3AgentHandler: prepareAgent()
V3AgentHandler->>V3AgentHandler: extract callbacks from options
V3AgentHandler->>LLMClient: generateText({ prepareStep, onStepFinish })
loop Each step
LLMClient->>AISDKCallbacks: prepareStep(context)
AISDKCallbacks-->>LLMClient: modified context
LLMClient->>LLMClient: execute step
LLMClient->>AISDKCallbacks: onStepFinish(event)
AISDKCallbacks->>V3AgentHandler: createStepHandler(event)
V3AgentHandler->>User: callbacks.onStepFinish(event)
end
V3AgentHandler-->>V3: AgentResult
V3-->>User: AgentResult
else Streaming (stream: true)
V3->>V3AgentHandler: stream(instructionOrOptions)
V3AgentHandler->>V3AgentHandler: prepareAgent()
V3AgentHandler->>V3AgentHandler: extract callbacks from options
V3AgentHandler->>LLMClient: streamText({ prepareStep, onStepFinish, onChunk, onFinish, onError })
loop Each step
LLMClient->>AISDKCallbacks: prepareStep(context)
AISDKCallbacks-->>LLMClient: modified context
loop Each chunk
LLMClient->>User: callbacks.onChunk(chunk)
end
LLMClient->>AISDKCallbacks: onStepFinish(event)
AISDKCallbacks->>V3AgentHandler: createStepHandler(event)
V3AgentHandler->>User: callbacks.onStepFinish(event)
end
LLMClient->>User: callbacks.onFinish(event)
V3AgentHandler-->>V3: AgentStreamResult
V3-->>User: AgentStreamResult
User->>User: iterate textStream
User->>User: await result promise
end
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 6 files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 7 files
Summary
Adds callback support to the Stagehand agent for both streaming and non-streaming execution modes, allowing users to hook into various stages of agent execution.
Changes
New Types (
lib/v3/types/public/agent.ts)Added callback interfaces for agent execution:
AgentCallbacks- Base callbacks shared between modes:prepareStep- Modify settings before each LLM steponStepFinish- Called after each step completesAgentExecuteCallbacks- Non-streaming mode callbacks (extendsAgentCallbacks)AgentStreamCallbacks- Streaming mode callbacks (extendsAgentCallbacks):onChunk- Called for each streamed chunkonFinish- Called when stream completesonError- Called on stream errorsonAbort- Called when stream is abortedAgentExecuteOptionsBase- Base options without callbacksAgentExecuteOptions- Non-streaming options withAgentExecuteCallbacksAgentStreamExecuteOptions- Streaming options withAgentStreamCallbacksHandler Updates (
lib/v3/handlers/v3AgentHandler.ts)createStepHandlerto accept optional user callbackexecute()to pass callbacks togenerateTextstream()to pass callbacks tostreamTextType Safety
Added compile-time enforcement that streaming-only callbacks (
onChunk,onFinish,onError,onAbort) can only be used withstream: true:Type Castings Explained
Several type assertions were necessary due to TypeScript's limitations with conditional types:
1. Callback extraction in handlers
Why:
instructionOrOptionscan bestring | AgentExecuteOptions. When it's a string, there are no callbacks. We cast after theprepareAgentcall because at that point we know it's been resolved to options.2. Streaming vs non-streaming branch in v3.ts
Why: The implementation signature accepts
string | AgentExecuteOptions | AgentStreamExecuteOptionsto satisfy both overloads, but within the non-streaming branch we know it's the non-streaming type. TypeScript can't narrow based on theisStreamingruntime check.3. Error fallback in stream()
Why: When
prepareAgentfails in streaming mode, we return a minimal object with justtextStreamandresult. This doesn't satisfy all properties ofStreamTextResult, but theresultpromise will reject with the actual error. The double cast (as unknown as) is needed because TypeScript knows this partial object doesn't match the full type.Usage Example
Testing
agent-callbacks.spec.tswith tests for:onStepFinish,prepareStep)onChunk,onFinish,prepareStep,onStepFinish)Summary by cubic
Adds lifecycle callbacks to the Stagehand agent for both non-streaming and streaming modes, so users can hook into steps, chunks, finish, and errors. Strong type safety prevents using streaming-only callbacks without stream: true.
Why:
What:
Test Plan:
Written for commit bc1d2a6. Summary will update automatically on new commits.