mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-09 15:59:30 +00:00
Some checks are pending
CI / check-additional-runtime-topology-architecture (push) Blocked by required conditions
CI / check-session-accessor-boundary (push) Blocked by required conditions
CI / check-session-transcript-reader-boundary (push) Blocked by required conditions
CI / check-docs (push) Blocked by required conditions
CI / runner-admission (push) Waiting to run
CI / preflight (push) Blocked by required conditions
CI / security-fast (push) Blocked by required conditions
CI / pnpm-store-warmup (push) Blocked by required conditions
CI / build-artifacts (push) Blocked by required conditions
CI / (push) Blocked by required conditions
CI / -1 (push) Blocked by required conditions
CI / -2 (push) Blocked by required conditions
CI / checks-node-compat-node22 (push) Blocked by required conditions
CI / -3 (push) Blocked by required conditions
CI / check-dependencies (push) Blocked by required conditions
CI / check-guards (push) Blocked by required conditions
CI / check-lint (push) Blocked by required conditions
CI / check-prod-types (push) Blocked by required conditions
CI / check-shrinkwrap (push) Blocked by required conditions
CI / check-test-types (push) Blocked by required conditions
CI / check-additional-boundaries-a (push) Blocked by required conditions
CI / check-additional-boundaries-bcd (push) Blocked by required conditions
CI / check-additional-extension-bundled (push) Blocked by required conditions
CI / check-additional-extension-channels (push) Blocked by required conditions
CI / check-additional-extension-package-boundary (push) Blocked by required conditions
CI / skills-python (push) Blocked by required conditions
CI / -4 (push) Blocked by required conditions
CI / -5 (push) Blocked by required conditions
CI / macos-swift (push) Blocked by required conditions
CI / ios-build (push) Blocked by required conditions
CI / -6 (push) Blocked by required conditions
CI / ci-timings-summary (push) Blocked by required conditions
ClawSweeper Dispatch / dispatch (push) Waiting to run
CodeQL / Security High (mcp-process-tool-boundary) (push) Waiting to run
CodeQL / Security High (actions) (push) Waiting to run
CodeQL / Security High (channel-runtime-boundary) (push) Waiting to run
CodeQL / Security High (core-auth-secrets) (push) Waiting to run
CodeQL / Security High (network-ssrf-boundary) (push) Waiting to run
CodeQL / Security High (plugin-trust-boundary) (push) Waiting to run
Docs Sync Publish Repo / sync-publish-repo (push) Waiting to run
Docs / docs (push) Waiting to run
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Waiting to run
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Blocked by required conditions
Plugin Init Scaffold Validation / Validate provider scaffold (push) Waiting to run
Plugin NPM Release / preview_plugins_npm (push) Waiting to run
Plugin NPM Release / Validate release publish approval (push) Blocked by required conditions
Plugin NPM Release / preview_plugin_pack (push) Blocked by required conditions
Plugin NPM Release / publish_plugins_npm (push) Blocked by required conditions
Workflow Sanity / no-tabs (push) Waiting to run
Workflow Sanity / actionlint (push) Waiting to run
Workflow Sanity / generated-doc-baselines (push) Waiting to run
* fix(google): bound embedding-batch JSON response reads * fix(google): repair embedding-batch test types and lint --------- Co-authored-by: NIO <nocodet@mail.com>
246 lines
7.1 KiB
TypeScript
246 lines
7.1 KiB
TypeScript
// Google tests cover embedding batch bounded JSON response reads.
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { runGeminiEmbeddingBatches } from "./embedding-batch.js";
|
|
import type { GeminiEmbeddingClient } from "./embedding-provider.js";
|
|
|
|
// Pass-through so onResponse receives real Response objects (required by
|
|
// readProviderJsonResponse which needs a real .body ReadableStream).
|
|
vi.mock("openclaw/plugin-sdk/memory-core-host-engine-embeddings", async (importOriginal) => {
|
|
const actual =
|
|
await importOriginal<typeof import("openclaw/plugin-sdk/memory-core-host-engine-embeddings")>();
|
|
return {
|
|
...actual,
|
|
withRemoteHttpResponse: async <T>(params: {
|
|
url: string;
|
|
ssrfPolicy?: unknown;
|
|
init?: RequestInit;
|
|
onResponse: (response: Response) => Promise<T>;
|
|
}): Promise<T> => {
|
|
const response = await fetch(params.url, params.init);
|
|
return await params.onResponse(response);
|
|
},
|
|
};
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
function fetchInputUrl(input: RequestInfo | URL): string {
|
|
if (typeof input === "string") {
|
|
return input;
|
|
}
|
|
if (input instanceof URL) {
|
|
return input.href;
|
|
}
|
|
return input.url;
|
|
}
|
|
|
|
function jsonResponse(body: unknown, status = 200): Response {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
|
|
function makeGeminiClient(): GeminiEmbeddingClient {
|
|
return {
|
|
baseUrl: "https://gemini-compatible.example/v1beta",
|
|
model: "text-embedding-004",
|
|
modelPath: "models/text-embedding-004",
|
|
headers: { "x-goog-api-key": "test-key" },
|
|
apiKeys: ["test-key"],
|
|
ssrfPolicy: undefined,
|
|
};
|
|
}
|
|
|
|
type GeminiBatchRequest = Parameters<typeof runGeminiEmbeddingBatches>[0]["requests"][number];
|
|
|
|
function singleRequest(): GeminiBatchRequest[] {
|
|
return [
|
|
{
|
|
custom_id: "r0",
|
|
request: {
|
|
model: "models/text-embedding-004",
|
|
content: { parts: [{ text: "hello" }] },
|
|
taskType: "RETRIEVAL_DOCUMENT",
|
|
},
|
|
},
|
|
];
|
|
}
|
|
|
|
function makeOversizedResponse(): {
|
|
response: Response;
|
|
getReadCount: () => number;
|
|
wasCanceled: () => boolean;
|
|
} {
|
|
const chunkSize = 1024 * 1024;
|
|
const chunkCount = 20; // 20 MiB — over 16 MiB cap
|
|
let readCount = 0;
|
|
let canceled = false;
|
|
return {
|
|
response: new Response(
|
|
new ReadableStream<Uint8Array>({
|
|
pull(controller) {
|
|
if (readCount >= chunkCount) {
|
|
controller.close();
|
|
return;
|
|
}
|
|
readCount += 1;
|
|
controller.enqueue(new Uint8Array(chunkSize));
|
|
},
|
|
cancel() {
|
|
canceled = true;
|
|
},
|
|
}),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
),
|
|
getReadCount: () => readCount,
|
|
wasCanceled: () => canceled,
|
|
};
|
|
}
|
|
|
|
describe("Google embedding-batch bounded JSON reads", () => {
|
|
it("bounds oversized file-upload JSON response and cancels the stream", async () => {
|
|
const streamed = makeOversizedResponse();
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
if (fetchInputUrl(input).includes("/upload/")) {
|
|
return streamed.response;
|
|
}
|
|
return new Response("unexpected", { status: 500 });
|
|
}),
|
|
);
|
|
|
|
await expect(
|
|
runGeminiEmbeddingBatches({
|
|
gemini: makeGeminiClient(),
|
|
agentId: "main",
|
|
requests: singleRequest(),
|
|
wait: true,
|
|
concurrency: 1,
|
|
pollIntervalMs: 50,
|
|
timeoutMs: 5_000,
|
|
}),
|
|
).rejects.toThrow(/gemini\.batch-file-upload/);
|
|
|
|
expect(streamed.wasCanceled()).toBe(true);
|
|
expect(streamed.getReadCount()).toBeLessThan(20);
|
|
});
|
|
|
|
it("bounds oversized batch-create JSON response and cancels the stream", async () => {
|
|
const streamed = makeOversizedResponse();
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = fetchInputUrl(input);
|
|
if (url.includes("/upload/")) {
|
|
return jsonResponse({ name: "files/f-ok" });
|
|
}
|
|
if (url.includes(":asyncBatchEmbedContent")) {
|
|
return streamed.response;
|
|
}
|
|
return new Response("unexpected", { status: 500 });
|
|
}),
|
|
);
|
|
|
|
await expect(
|
|
runGeminiEmbeddingBatches({
|
|
gemini: makeGeminiClient(),
|
|
agentId: "main",
|
|
requests: singleRequest(),
|
|
wait: true,
|
|
concurrency: 1,
|
|
pollIntervalMs: 50,
|
|
timeoutMs: 5_000,
|
|
}),
|
|
).rejects.toThrow(/gemini\.batch-create/);
|
|
|
|
expect(streamed.wasCanceled()).toBe(true);
|
|
expect(streamed.getReadCount()).toBeLessThan(20);
|
|
});
|
|
|
|
it("bounds oversized batch-status poll JSON response and cancels the stream", async () => {
|
|
const streamed = makeOversizedResponse();
|
|
let statusCalled = false;
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = fetchInputUrl(input);
|
|
if (url.includes("/upload/")) {
|
|
return jsonResponse({ name: "files/f-ok" });
|
|
}
|
|
if (url.includes(":asyncBatchEmbedContent")) {
|
|
return jsonResponse({ name: "batches/b-0", state: "PENDING" });
|
|
}
|
|
if (url.includes("/batches/") && !statusCalled) {
|
|
statusCalled = true;
|
|
return streamed.response;
|
|
}
|
|
return new Response("unexpected", { status: 500 });
|
|
}),
|
|
);
|
|
|
|
await expect(
|
|
runGeminiEmbeddingBatches({
|
|
gemini: makeGeminiClient(),
|
|
agentId: "main",
|
|
requests: singleRequest(),
|
|
wait: true,
|
|
concurrency: 1,
|
|
pollIntervalMs: 50,
|
|
timeoutMs: 5_000,
|
|
}),
|
|
).rejects.toThrow(/gemini\.batch-status/);
|
|
|
|
expect(streamed.wasCanceled()).toBe(true);
|
|
expect(streamed.getReadCount()).toBeLessThan(20);
|
|
});
|
|
|
|
it("parses small responses on all three JSON paths correctly", async () => {
|
|
// Use a unit-length vector so sanitizeAndNormalizeEmbedding preserves values.
|
|
const outputLine = JSON.stringify({
|
|
key: "r0",
|
|
embedding: { values: [1, 0, 0] },
|
|
});
|
|
let statusCalled = false;
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(async (input: RequestInfo | URL) => {
|
|
const url = fetchInputUrl(input);
|
|
if (url.includes("/upload/")) {
|
|
return jsonResponse({ name: "files/f-ok" });
|
|
}
|
|
if (url.includes(":asyncBatchEmbedContent")) {
|
|
return jsonResponse({ name: "batches/b-0", state: "PENDING" });
|
|
}
|
|
if (url.includes("/batches/") && !statusCalled) {
|
|
statusCalled = true;
|
|
return jsonResponse({
|
|
name: "batches/b-0",
|
|
state: "SUCCEEDED",
|
|
outputConfig: { file: "files/out-0" },
|
|
});
|
|
}
|
|
if (url.includes(":download")) {
|
|
return new Response(outputLine, { status: 200 });
|
|
}
|
|
return new Response("unexpected", { status: 500 });
|
|
}),
|
|
);
|
|
|
|
const result = await runGeminiEmbeddingBatches({
|
|
gemini: makeGeminiClient(),
|
|
agentId: "main",
|
|
requests: singleRequest(),
|
|
wait: true,
|
|
concurrency: 1,
|
|
pollIntervalMs: 50,
|
|
timeoutMs: 5_000,
|
|
});
|
|
|
|
expect(result.get("r0")).toEqual([1, 0, 0]);
|
|
});
|
|
});
|