refactor(memory): share batch output parsing

This commit is contained in:
Peter Steinberger
2026-02-14 14:36:59 +00:00
parent 4f61a3f527
commit 7bd073340a
3 changed files with 59 additions and 52 deletions

View File

@@ -1,5 +1,6 @@
import type { OpenAiEmbeddingClient } from "./embeddings-openai.js";
import { retryAsync } from "../infra/retry.js";
import { applyEmbeddingBatchOutputLine } from "./batch-output.js";
import { hashText, runWithConcurrency } from "./internal.js";
export type OpenAiBatchRequest = {
@@ -313,32 +314,7 @@ export async function runOpenAiEmbeddingBatches(params: {
const remaining = new Set(group.map((request) => request.custom_id));
for (const line of outputLines) {
const customId = line.custom_id;
if (!customId) {
continue;
}
remaining.delete(customId);
if (line.error?.message) {
errors.push(`${customId}: ${line.error.message}`);
continue;
}
const response = line.response;
const statusCode = response?.status_code ?? 0;
if (statusCode >= 400) {
const message =
response?.body?.error?.message ??
(typeof response?.body === "string" ? response.body : undefined) ??
"unknown error";
errors.push(`${customId}: ${message}`);
continue;
}
const data = response?.body?.data ?? [];
const embedding = data[0]?.embedding ?? [];
if (embedding.length === 0) {
errors.push(`${customId}: empty embedding`);
continue;
}
byCustomId.set(customId, embedding);
applyEmbeddingBatchOutputLine({ line, remaining, errors, byCustomId });
}
if (errors.length > 0) {