Build extensible agent harness with interactive chat loop
This commit is contained in:
60
src/model/openai.ts
Normal file
60
src/model/openai.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import OpenAI from "openai";
|
||||
import type {
|
||||
AgentMessage,
|
||||
ModelClient,
|
||||
ModelTurn,
|
||||
ToolCall,
|
||||
ToolDefinition
|
||||
} from "../types.js";
|
||||
import type { EventBus } from "../events.js";
|
||||
|
||||
export class OpenAIResponsesClient implements ModelClient {
|
||||
private readonly client = new OpenAI();
|
||||
|
||||
constructor(
|
||||
private readonly model = "gpt-5",
|
||||
private readonly instructions?: string,
|
||||
private readonly events?: EventBus
|
||||
) {}
|
||||
|
||||
async respond(input: AgentMessage[], tools: ToolDefinition[]): Promise<ModelTurn> {
|
||||
await this.events?.emit("model.request", {
|
||||
model: this.model,
|
||||
messages: input.length,
|
||||
tools: tools.map((tool) => tool.name)
|
||||
});
|
||||
|
||||
const response = await this.client.responses.create({
|
||||
model: this.model,
|
||||
instructions: this.instructions,
|
||||
input,
|
||||
tools: tools.map((tool) => ({
|
||||
type: "function",
|
||||
name: tool.name,
|
||||
description: tool.description,
|
||||
parameters: tool.parameters,
|
||||
strict: true
|
||||
}))
|
||||
});
|
||||
|
||||
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
|
||||
}));
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user