Add streaming terminal chat UX
This commit is contained in:
+67
-7
@@ -37,13 +37,73 @@ export class OpenAIResponsesClient implements ModelClient {
|
||||
}))
|
||||
});
|
||||
|
||||
const toolCalls: ToolCall[] = response.output
|
||||
.filter((item): item is OpenAI.Responses.ResponseFunctionToolCall => item.type === "function_call")
|
||||
.map((item) => ({
|
||||
id: item.call_id,
|
||||
name: item.name,
|
||||
arguments: item.arguments
|
||||
}));
|
||||
const toolCalls: ToolCall[] = response.output.flatMap((item) =>
|
||||
item.type === "function_call"
|
||||
? [
|
||||
{
|
||||
id: item.call_id,
|
||||
name: item.name,
|
||||
arguments: item.arguments
|
||||
}
|
||||
]
|
||||
: []
|
||||
);
|
||||
|
||||
await this.events?.emit("model.response", {
|
||||
outputItems: response.output.length,
|
||||
toolCalls: toolCalls.map((toolCall) => toolCall.name),
|
||||
hasText: response.output_text.length > 0
|
||||
});
|
||||
|
||||
return {
|
||||
output: response.output,
|
||||
outputText: response.output_text,
|
||||
toolCalls
|
||||
};
|
||||
}
|
||||
|
||||
async respondStream(
|
||||
input: AgentMessage[],
|
||||
tools: ToolDefinition[],
|
||||
onTextDelta: (delta: string) => void
|
||||
): Promise<ModelTurn> {
|
||||
await this.events?.emit("model.request", {
|
||||
model: this.model,
|
||||
messages: input.length,
|
||||
tools: tools.map((tool) => tool.name)
|
||||
});
|
||||
|
||||
const stream = this.client.responses.stream({
|
||||
model: this.model,
|
||||
instructions: this.instructions,
|
||||
input,
|
||||
tools: tools.map((tool) => ({
|
||||
type: "function",
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: tool.parameters,
|
||||
strict: true
|
||||
}))
|
||||
});
|
||||
|
||||
for await (const event of stream) {
|
||||
if (event.type === "response.output_text.delta") {
|
||||
onTextDelta(event.delta);
|
||||
}
|
||||
}
|
||||
|
||||
const response = await stream.finalResponse();
|
||||
const toolCalls: ToolCall[] = response.output.flatMap((item) =>
|
||||
item.type === "function_call"
|
||||
? [
|
||||
{
|
||||
id: item.call_id,
|
||||
name: item.name,
|
||||
arguments: item.arguments
|
||||
}
|
||||
]
|
||||
: []
|
||||
);
|
||||
|
||||
await this.events?.emit("model.response", {
|
||||
outputItems: response.output.length,
|
||||
|
||||
Reference in New Issue
Block a user