-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Agent streaming #1302
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
Agent streaming #1302
Conversation
|
Greptile OverviewGreptile SummaryAdded streaming support to V3 non-CUA agents by implementing a new Major Changes
Issues Found
Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant User
participant V3
participant V3AgentHandler
participant LLMClient
participant AI_SDK
User->>V3: agent(options).stream(instruction)
V3->>V3AgentHandler: new V3AgentHandler()
V3->>V3AgentHandler: stream(instruction)
V3AgentHandler->>V3AgentHandler: prepareAgent()
Note over V3AgentHandler: Build system prompt,<br/>wrap model, create tools
V3AgentHandler->>V3AgentHandler: Create AgentState
Note over V3AgentHandler: collectedReasoning,<br/>actions, completed, etc.
V3AgentHandler->>V3AgentHandler: Create result promise<br/>(resolveResult, rejectResult)
V3AgentHandler->>LLMClient: streamText(config + onStepFinish + onFinish)
LLMClient->>AI_SDK: streamText()
AI_SDK-->>User: Return StreamTextResult<br/>(textStream, fullStream)
loop For each step
AI_SDK->>V3AgentHandler: onStepFinish(event)
V3AgentHandler->>V3AgentHandler: createStepHandler(state)
Note over V3AgentHandler: Process tool calls,<br/>update state (reasoning, actions),<br/>check for "close" tool
end
AI_SDK->>V3AgentHandler: onFinish(event)
V3AgentHandler->>V3AgentHandler: consolidateMetricsAndResult()
Note over V3AgentHandler: Build final AgentResult,<br/>update metrics
V3AgentHandler->>V3AgentHandler: resolveResult(AgentResult)
User->>User: await result.result
User-->>User: Receive final AgentResult
|
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.
3 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.
4 files reviewed, 3 comments
| }); | ||
|
|
||
| const result = await agent.stream({ | ||
| instruction: "go to amazon, and seach for shampoo, stop after searching", |
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.
syntax: typo: "seach" should be "search"
| instruction: "go to amazon, and seach for shampoo, stop after searching", | |
| instruction: "go to amazon, and search for shampoo, stop after searching", |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/examples/agent_stream_example.ts
Line: 29:29
Comment:
**syntax:** typo: "seach" should be "search"
```suggestion
instruction: "go to amazon, and search for shampoo, stop after searching",
```
How can I resolve this? If you propose a fix, please make it concise.| let resolveResult: (value: AgentResult | PromiseLike<AgentResult>) => void; | ||
| let rejectResult: (reason?: string) => void; | ||
| const resultPromise = new Promise<AgentResult>((resolve, reject) => { | ||
| resolveResult = resolve; | ||
| rejectResult = reject; | ||
| }); |
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.
syntax: uninitialized variables resolveResult and rejectResult are used before assignment
The variables are declared but not initialized, then passed to the Promise constructor. TypeScript should complain about this with strict mode enabled.
| let resolveResult: (value: AgentResult | PromiseLike<AgentResult>) => void; | |
| let rejectResult: (reason?: string) => void; | |
| const resultPromise = new Promise<AgentResult>((resolve, reject) => { | |
| resolveResult = resolve; | |
| rejectResult = reject; | |
| }); | |
| const resultPromise = new Promise<AgentResult>((resolve, reject) => { | |
| resolveResult = resolve; | |
| rejectResult = reject; | |
| }); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/lib/v3/handlers/v3AgentHandler.ts
Line: 218:223
Comment:
**syntax:** uninitialized variables `resolveResult` and `rejectResult` are used before assignment
The variables are declared but not initialized, then passed to the Promise constructor. TypeScript should complain about this with strict mode enabled.
```suggestion
const resultPromise = new Promise<AgentResult>((resolve, reject) => {
resolveResult = resolve;
rejectResult = reject;
});
```
How can I resolve this? If you propose a fix, please make it concise.| } catch (error) { | ||
| rejectResult(error); | ||
| } |
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.
syntax: rejectResult is called with error parameter but declared to take reason?: string
The type of error in the catch block is unknown, but rejectResult expects a string. This could cause type issues.
| } catch (error) { | |
| rejectResult(error); | |
| } | |
| } catch (error) { | |
| rejectResult(String(error)); | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/lib/v3/handlers/v3AgentHandler.ts
Line: 242:244
Comment:
**syntax:** `rejectResult` is called with `error` parameter but declared to take `reason?: string`
The type of `error` in the catch block is `unknown`, but `rejectResult` expects a string. This could cause type issues.
```suggestion
} catch (error) {
rejectResult(String(error));
}
```
How can I resolve this? If you propose a fix, please make it concise.
why
We wanted to add streaming support to the non-CUA agent in V3
what changed
Added
streammethod toV3AgentHandler:streamTextinstead ofgenerateTextAgentStreamResult, which extendsStreamTextResultwith an addedresult: Promise<AgentResult>propertyonFinishcallback to resolve the result promise with metrics and final stateConsolidated duplicate logic:
prepareAgent()helper to build context (system prompt, tools, wrapped model) for bothexecuteandstreamcreateStepHandler()to generate theonStepFinishcallback used by both methodsconsolidateMetricsAndResult()to handle metrics updates andAgentResultconstructionhandleStop()helper to check for "close" tool calls before falling back to step count limitMoved types to public agent types:
AgentContextandAgentStateinterfaces frompackages/core/lib/v3/types/public/agent.tsAgentStreamResulttype that combinesStreamTextResult<ToolSet, never>withresult: Promise<AgentResult>Updated
V3.agent()return type:streammethod alongsideexecutefor non-CUA agentsexecute(no streaming support)notes : context handling will likely be moved to prepareStep callback in the very near future, currently we have to maintain a state of the messages which is not ideal. prepareStep should eliminate the need for this