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