Add streaming terminal chat UX

This commit is contained in:
Adolfo Reyna
2026-05-15 14:20:08 -04:00
parent d8d79efe83
commit a04fc0a0b5
5 changed files with 127 additions and 15 deletions

View File

@@ -8,6 +8,10 @@ export type AgentOptions = {
events?: EventBus;
};
export type RunTurnOptions = {
onTextDelta?: (delta: string) => void;
};
export class Agent {
private readonly history: AgentMessage[] = [];
@@ -17,7 +21,7 @@ export class Agent {
private readonly options: AgentOptions
) {}
async runTurn(prompt: string): Promise<string> {
async runTurn(prompt: string, runOptions: RunTurnOptions = {}): Promise<string> {
const events = this.options.events;
const maxTurns = this.options.maxTurns ?? 20;
@@ -26,7 +30,14 @@ export class Agent {
for (let turn = 0; turn < maxTurns; turn += 1) {
await events?.emit("agent.turn.started", { turn: turn + 1 });
const result = await this.model.respond(this.history, this.tools.list());
const result =
runOptions.onTextDelta && this.model.respondStream
? await this.model.respondStream(
this.history,
this.tools.list(),
runOptions.onTextDelta
)
: await this.model.respond(this.history, this.tools.list());
this.history.push(...(result.output as AgentMessage[]));
if (result.toolCalls.length === 0) {