refactor(shared): dedupe protocol schema typing and session/media helpers

This commit is contained in:
Peter Steinberger
2026-03-02 15:39:18 +00:00
parent ee0d7ba6d6
commit 8768487aee
10 changed files with 224 additions and 353 deletions

View File

@@ -1,6 +1,6 @@
import { normalizeGoogleModelId } from "../../../agents/models-config.providers.js";
import { parseGeminiAuth } from "../../../infra/gemini-auth.js";
import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js";
import { assertOkOrThrowHttpError, normalizeBaseUrl, postJsonRequest } from "../shared.js";
export async function generateGeminiInlineDataText(params: {
buffer: Buffer;
@@ -61,17 +61,14 @@ export async function generateGeminiInlineDataText(params: {
],
};
const { response: res, release } = await fetchWithTimeoutGuarded(
const { response: res, release } = await postJsonRequest({
url,
{
method: "POST",
headers,
body: JSON.stringify(body),
},
params.timeoutMs,
headers,
body,
timeoutMs: params.timeoutMs,
fetchFn,
allowPrivate ? { ssrfPolicy: { allowPrivateNetwork: true } } : undefined,
);
allowPrivateNetwork: allowPrivate,
});
try {
await assertOkOrThrowHttpError(res, params.httpErrorLabel);

View File

@@ -1,5 +1,5 @@
import type { VideoDescriptionRequest, VideoDescriptionResult } from "../../types.js";
import { assertOkOrThrowHttpError, fetchWithTimeoutGuarded, normalizeBaseUrl } from "../shared.js";
import { assertOkOrThrowHttpError, normalizeBaseUrl, postJsonRequest } from "../shared.js";
export const DEFAULT_MOONSHOT_VIDEO_BASE_URL = "https://api.moonshot.ai/v1";
const DEFAULT_MOONSHOT_VIDEO_MODEL = "kimi-k2.5";
@@ -84,16 +84,13 @@ export async function describeMoonshotVideo(
],
};
const { response: res, release } = await fetchWithTimeoutGuarded(
const { response: res, release } = await postJsonRequest({
url,
{
method: "POST",
headers,
body: JSON.stringify(body),
},
params.timeoutMs,
headers,
body,
timeoutMs: params.timeoutMs,
fetchFn,
);
});
try {
await assertOkOrThrowHttpError(res, "Moonshot video description failed");

View File

@@ -53,6 +53,27 @@ export async function postTranscriptionRequest(params: {
);
}
export async function postJsonRequest(params: {
url: string;
headers: Headers;
body: unknown;
timeoutMs: number;
fetchFn: typeof fetch;
allowPrivateNetwork?: boolean;
}) {
return fetchWithTimeoutGuarded(
params.url,
{
method: "POST",
headers: params.headers,
body: JSON.stringify(params.body),
},
params.timeoutMs,
params.fetchFn,
params.allowPrivateNetwork ? { ssrfPolicy: { allowPrivateNetwork: true } } : undefined,
);
}
export async function readErrorResponse(res: Response): Promise<string | undefined> {
try {
const text = await res.text();