refactor(memory): add guarded remote HTTP helper

This commit is contained in:
Peter Steinberger
2026-02-22 18:13:29 +01:00
parent 99cfb3dab2
commit f6feb4144c
3 changed files with 86 additions and 28 deletions

View File

@@ -1,21 +1,31 @@
import type { SsrFPolicy } from "../infra/net/ssrf.js";
import { withRemoteHttpResponse } from "./remote-http.js";
export async function fetchRemoteEmbeddingVectors(params: {
url: string;
headers: Record<string, string>;
ssrfPolicy?: SsrFPolicy;
body: unknown;
errorPrefix: string;
}): Promise<number[][]> {
const res = await fetch(params.url, {
method: "POST",
headers: params.headers,
body: JSON.stringify(params.body),
return await withRemoteHttpResponse({
url: params.url,
ssrfPolicy: params.ssrfPolicy,
init: {
method: "POST",
headers: params.headers,
body: JSON.stringify(params.body),
},
onResponse: async (res) => {
if (!res.ok) {
const text = await res.text();
throw new Error(`${params.errorPrefix}: ${res.status} ${text}`);
}
const payload = (await res.json()) as {
data?: Array<{ embedding?: number[] }>;
};
const data = payload.data ?? [];
return data.map((entry) => entry.embedding ?? []);
},
});
if (!res.ok) {
const text = await res.text();
throw new Error(`${params.errorPrefix}: ${res.status} ${text}`);
}
const payload = (await res.json()) as {
data?: Array<{ embedding?: number[] }>;
};
const data = payload.data ?? [];
return data.map((entry) => entry.embedding ?? []);
}