mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-12 21:53:00 -06:00
fb702e5cae
* fix(memory): harden embedding batch workflows Co-authored-by: 徐闻涵0668001344 <xu.wenhan1@xydigit.com> Co-authored-by: ben.li <li.yang6@xydigit.com> * test(memory): align batch redaction expectation * test(memory): tighten batch dependency types * refactor(google): clarify batch state control flow * test(google): make batch stage fallback lint-safe * build(plugin-sdk): refresh memory batch surface budget --------- Co-authored-by: Peter Steinberger <steipete@gmail.com> Co-authored-by: ben.li <li.yang6@xydigit.com>
85 lines
3.0 KiB
TypeScript
85 lines
3.0 KiB
TypeScript
// Batch status helpers shared by remote embedding providers.
|
|
import type { EmbeddingBatchStatus } from "./batch-provider-common.js";
|
|
|
|
const TERMINAL_FAILURE_STATES = new Set(["failed", "expired", "cancelled", "canceled"]);
|
|
|
|
/** File ids returned once a batch has completed. */
|
|
export type BatchCompletionResult = {
|
|
outputFileId: string;
|
|
errorFileId?: string;
|
|
};
|
|
|
|
/** Convert a completed provider status payload into output/error file ids. */
|
|
export function resolveBatchCompletionFromStatus(params: {
|
|
provider: string;
|
|
batchId: string;
|
|
status: EmbeddingBatchStatus;
|
|
}): BatchCompletionResult {
|
|
if (!params.status.output_file_id) {
|
|
throw new Error(`${params.provider} batch ${params.batchId} completed without output file`);
|
|
}
|
|
return {
|
|
outputFileId: params.status.output_file_id,
|
|
errorFileId: params.status.error_file_id ?? undefined,
|
|
};
|
|
}
|
|
|
|
/** Fail a completed partial/all-error batch before requiring its success file. */
|
|
export async function throwIfBatchCompletionError(params: {
|
|
provider: string;
|
|
status: EmbeddingBatchStatus;
|
|
readError: (errorFileId: string) => Promise<string | undefined>;
|
|
}): Promise<void> {
|
|
if (params.status.status !== "completed" || !params.status.error_file_id) {
|
|
return;
|
|
}
|
|
const detail = await params.readError(params.status.error_file_id);
|
|
throw new Error(
|
|
`${params.provider} batch ${params.status.id ?? "<unknown>"} completed: ${detail ?? "provider error file present"}`,
|
|
);
|
|
}
|
|
|
|
/** Throw when a provider reports a terminal failure, including error-file detail if available. */
|
|
export async function throwIfBatchTerminalFailure(params: {
|
|
provider: string;
|
|
status: EmbeddingBatchStatus;
|
|
readError: (errorFileId: string) => Promise<string | undefined>;
|
|
}): Promise<void> {
|
|
const state = params.status.status ?? "unknown";
|
|
if (!TERMINAL_FAILURE_STATES.has(state)) {
|
|
return;
|
|
}
|
|
const detail = params.status.error_file_id
|
|
? await params.readError(params.status.error_file_id)
|
|
: undefined;
|
|
const suffix = detail ? `: ${detail}` : "";
|
|
throw new Error(`${params.provider} batch ${params.status.id ?? "<unknown>"} ${state}${suffix}`);
|
|
}
|
|
|
|
/** Resolve the completed batch files, optionally waiting according to caller policy. */
|
|
export async function resolveCompletedBatchResult(params: {
|
|
provider: string;
|
|
status: EmbeddingBatchStatus;
|
|
wait: boolean;
|
|
waitForBatch: () => Promise<BatchCompletionResult>;
|
|
}): Promise<BatchCompletionResult> {
|
|
const batchId = params.status.id ?? "<unknown>";
|
|
if (!params.wait && params.status.status !== "completed") {
|
|
throw new Error(
|
|
`${params.provider} batch ${batchId} submitted; enable remote.batch.wait to await completion`,
|
|
);
|
|
}
|
|
const completed =
|
|
params.status.status === "completed"
|
|
? resolveBatchCompletionFromStatus({
|
|
provider: params.provider,
|
|
batchId,
|
|
status: params.status,
|
|
})
|
|
: await params.waitForBatch();
|
|
if (!completed.outputFileId) {
|
|
throw new Error(`${params.provider} batch ${batchId} completed without output file`);
|
|
}
|
|
return completed;
|
|
}
|