Add streaming terminal chat UX
This commit is contained in:
15
src/agent.ts
15
src/agent.ts
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user