remove empty content continue; prettier then reformatted many things

This commit is contained in:
Rob Colbert 2026-05-19 11:48:56 -04:00
parent 1758bb2db7
commit d47ef96916

View File

@ -199,7 +199,8 @@ export class OpenAiApi extends AiApi {
} }
const startTime = Date.now(); const startTime = Date.now();
const response = await this.client.chat.completions.create({ const response = await this.client.chat.completions.create(
{
model: model.modelId, model: model.modelId,
messages: [ messages: [
...(options.systemPrompt ...(options.systemPrompt
@ -222,7 +223,9 @@ export class OpenAiApi extends AiApi {
| "high", | "high",
} }
: {}), : {}),
}, options.signal ? { signal: options.signal } : undefined); },
options.signal ? { signal: options.signal } : undefined,
);
let accumulatedResponse = ""; let accumulatedResponse = "";
let accumulatedThinking = ""; let accumulatedThinking = "";
@ -300,7 +303,12 @@ export class OpenAiApi extends AiApi {
); );
if (this.isEmptyIteration(iteration)) { if (this.isEmptyIteration(iteration)) {
iteration = await this.readNonStreamingChatCompletion(model, messages, tools, options.signal); iteration = await this.readNonStreamingChatCompletion(
model,
messages,
tools,
options.signal,
);
if (streamCallback && iteration.response) { if (streamCallback && iteration.response) {
await streamCallback({ type: "response", data: iteration.response }); await streamCallback({ type: "response", data: iteration.response });
} }
@ -321,14 +329,19 @@ export class OpenAiApi extends AiApi {
done: true, done: true,
response: iteration.response, response: iteration.response,
thinking: iteration.thinking, thinking: iteration.thinking,
toolCalls: iteration.toolCalls.length > 0 ? iteration.toolCalls : undefined, toolCalls:
iteration.toolCalls.length > 0 ? iteration.toolCalls : undefined,
stats: this.buildStats(startTime, iteration.usage), stats: this.buildStats(startTime, iteration.usage),
}; };
this.assertNonEmptyChatResponse(finalResponse); this.assertNonEmptyChatResponse(finalResponse);
return finalResponse; return finalResponse;
} }
private extractUsage(usage: { prompt_tokens: number; completion_tokens: number; completion_tokens_details?: { reasoning_tokens?: number } }): OpenAiUsage { private extractUsage(usage: {
prompt_tokens: number;
completion_tokens: number;
completion_tokens_details?: { reasoning_tokens?: number };
}): OpenAiUsage {
return { return {
promptTokens: usage.prompt_tokens, promptTokens: usage.prompt_tokens,
completionTokens: usage.completion_tokens, completionTokens: usage.completion_tokens,
@ -342,7 +355,6 @@ export class OpenAiApi extends AiApi {
messages.push({ role: "system", content: options.systemPrompt }); messages.push({ role: "system", content: options.systemPrompt });
} }
for (const msg of options.context || []) { for (const msg of options.context || []) {
if (!msg.content?.trim()) continue;
if (msg.role === "tool") { if (msg.role === "tool") {
messages.push({ messages.push({
role: "tool", role: "tool",
@ -351,7 +363,11 @@ export class OpenAiApi extends AiApi {
}); });
continue; continue;
} }
if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) { if (
msg.role === "assistant" &&
msg.toolCalls &&
msg.toolCalls.length > 0
) {
messages.push({ messages.push({
role: "assistant", role: "assistant",
content: msg.content, content: msg.content,
@ -371,14 +387,16 @@ export class OpenAiApi extends AiApi {
} }
private buildTools(options: IAiChatOptions): ChatCompletionTool[] { private buildTools(options: IAiChatOptions): ChatCompletionTool[] {
return (options.tools || []).map((tool): ChatCompletionFunctionTool => ({ return (options.tools || []).map(
(tool): ChatCompletionFunctionTool => ({
type: tool.definition.type, type: tool.definition.type,
function: { function: {
name: tool.definition.function.name, name: tool.definition.function.name,
description: tool.definition.function.description, description: tool.definition.function.description,
parameters: tool.definition.function.parameters, parameters: tool.definition.function.parameters,
}, },
})); }),
);
} }
private async readStreamingChatCompletion( private async readStreamingChatCompletion(
@ -392,7 +410,8 @@ export class OpenAiApi extends AiApi {
throw new DOMException("The operation was aborted", "AbortError"); throw new DOMException("The operation was aborted", "AbortError");
} }
const response = await this.client.chat.completions.create({ const response = await this.client.chat.completions.create(
{
model: model.modelId, model: model.modelId,
messages, messages,
tools, tools,
@ -411,7 +430,9 @@ export class OpenAiApi extends AiApi {
| "high", | "high",
} }
: {}), : {}),
}, signal ? { signal } : undefined); },
signal ? { signal } : undefined,
);
let content = ""; let content = "";
let thinking = ""; let thinking = "";
@ -448,7 +469,10 @@ export class OpenAiApi extends AiApi {
if ("reasoning" in delta && delta.reasoning) { if ("reasoning" in delta && delta.reasoning) {
thinking += delta.reasoning as string; thinking += delta.reasoning as string;
if (streamCallback) { if (streamCallback) {
await streamCallback({ type: "thinking", data: delta.reasoning as string }); await streamCallback({
type: "thinking",
data: delta.reasoning as string,
});
} }
} }
if (delta.tool_calls) { if (delta.tool_calls) {
@ -481,7 +505,8 @@ export class OpenAiApi extends AiApi {
throw new DOMException("The operation was aborted", "AbortError"); throw new DOMException("The operation was aborted", "AbortError");
} }
const response = await this.client.chat.completions.create({ const response = await this.client.chat.completions.create(
{
model: model.modelId, model: model.modelId,
messages, messages,
tools, tools,
@ -499,12 +524,16 @@ export class OpenAiApi extends AiApi {
| "high", | "high",
} }
: {}), : {}),
}, signal ? { signal } : undefined); },
signal ? { signal } : undefined,
);
const choice = response.choices[0]; const choice = response.choices[0];
const message = choice?.message; const message = choice?.message;
const content = typeof message?.content === "string" ? message.content : ""; const content = typeof message?.content === "string" ? message.content : "";
const usage = response.usage ? this.extractUsage(response.usage) : undefined; const usage = response.usage
? this.extractUsage(response.usage)
: undefined;
const assistantToolCalls = (message?.tool_calls || []) const assistantToolCalls = (message?.tool_calls || [])
.filter((tc) => tc.type === "function") .filter((tc) => tc.type === "function")
.map((tc) => ({ .map((tc) => ({