Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.
TypeScript agentic-aiaiai-agentsai-videocompilerdslffmpeggenerative-aillmmarkup-languagemonorepoplugin-systemprogramming-languagetext-to-videotypescriptvideovideo-automationvideo-clonevideo-editingvideo-generation
feat(provider-hypihub): direct-upload Gemini media
rponeb45f08fe2493
diff --git a/packages/provider-hypihub/src/gemini.ts b/packages/provider-hypihub/src/gemini.tsindex 1c137b11..17a6e8a7 100644--- a/packages/provider-hypihub/src/gemini.ts+++ b/packages/provider-hypihub/src/gemini.ts@@ -1,4 +1,5 @@import type { GeminiInlinePart } from "@hypit/gemini";+import { HypiHubUploader } from "./upload.js";export type HypiHubGeminiPart = GeminiInlinePart| { readonly fileData: { readonly mimeType?: string; readonly fileUri: string } };@@ -8,6 +9,10 @@ export type HypiHubGeminiGeneratorOptions = {readonly model?: string;readonly baseUrl?: string;readonly requestTimeoutMs?: number;+ /** Timeout for one direct S3 multipart PUT. Defaults to five minutes. */+ readonly uploadPartTimeoutMs?: number;+ /** Attempts per direct S3 part. Defaults to three. */+ readonly uploadPartAttempts?: number;readonly maxRateLimitRetries?: number;readonly rateLimitRetryDelayMs?: number;readonly fetch?: typeof globalThis.fetch;@@ -78,35 +83,29 @@ export function createHypiHubGeminiGenerator(options: HypiHubGeminiGeneratorOpticonst rateLimitRetryDelayMs = positiveInteger(options.rateLimitRetryDelayMs ?? 2_000,"HypiHub Gemini rateLimitRetryDelayMs");const fetcher = options.fetch ?? globalThis.fetch;+ const uploader = new HypiHubUploader({+ baseUrl,+ requestTimeoutMs: timeout,+ ...(options.uploadPartTimeoutMs === undefined ? {} : { uploadPartTimeoutMs: options.uploadPartTimeoutMs }),+ ...(options.uploadPartAttempts === undefined ? {} : { uploadPartAttempts: options.uploadPartAttempts }),+ fetch: fetcher,+ });return async (input: HypiHubGeminiGenerateInput): Promise<string> => {+ const uploaded = new Map<string, Promise<string>>();+ const upload = async (mimeType: string, encoded: string): Promise<string> => {+ let bytes: Uint8Array;+ try { bytes = new Uint8Array(Buffer.from(encoded, "base64")); }+ catch (error) { throw new Error("HypiHub Gemini inline media is not valid base64", { cause: error }); }+ assert(bytes.byteLength > 0, "HypiHub Gemini inline media is empty");+ return uploader.upload({ bytes, mediaType: mimeType, purpose: "reference" }, apiKey);+ };+ // Uploads have their own control-request and per-part timeouts. Start the+ // model-generation timeout only after every media reference is ready.+ const parts = await Promise.all(input.parts.map((part) => partValue(part, uploaded, upload)));const controller = new AbortController();const timer = setTimeout(() => controller.abort(), timeout);try {- const uploaded = new Map<string, Promise<string>>();- const upload = async (mimeType: string, encoded: string): Promise<string> => {- let bytes: Uint8Array;- try { bytes = new Uint8Array(Buffer.from(encoded, "base64")); }- catch (error) { throw new Error("HypiHub Gemini inline media is not valid base64", { cause: error }); }- assert(bytes.byteLength > 0, "HypiHub Gemini inline media is empty");- const form = new FormData();- const copy = new ArrayBuffer(bytes.byteLength);- new Uint8Array(copy).set(bytes);- form.append("file", new Blob([copy], { type: mimeType }), `gemini-reference.${extension(mimeType)}`);- form.append("purpose", "reference");- const uploadedResponse = await fetcher(`${baseUrl}/v1/files`, {- method: "POST", signal: controller.signal,- headers: { authorization: `Bearer ${apiKey}` }, body: form,- });- const uploadedText = await uploadedResponse.text();- if (!uploadedResponse.ok) throw new Error(`HypiHub Gemini file upload returned HTTP ${uploadedResponse.status}: ${uploadedText.slice(0, 300)}`);- let uploadedBody: unknown;- try { uploadedBody = JSON.parse(uploadedText); } catch { throw new Error("HypiHub Gemini file upload returned invalid JSON"); }- const url = (uploadedBody as { readonly url?: unknown }).url;- assert(typeof url === "string" && /^https:\/\//iu.test(url), "HypiHub Gemini file upload returned no HTTPS URL");- return url;- };- const parts = await Promise.all(input.parts.map((part) => partValue(part, uploaded, upload)));let response: Response | undefined;let text = "";for (let attempt = 0; attempt <= maxRateLimitRetries; attempt += 1) {@@ -145,12 +144,3 @@ export function createHypiHubGeminiGenerator(options: HypiHubGeminiGeneratorOpti}};}--function extension(mimeType: string): string {- const value: Readonly<Record<string, string>> = {- "image/jpeg": "jpg", "image/png": "png", "image/webp": "webp",- "audio/mpeg": "mp3", "audio/wav": "wav", "audio/mp4": "m4a",- "video/mp4": "mp4", "video/quicktime": "mov", "video/webm": "webm",- };- return value[mimeType.toLowerCase()] ?? "bin";-}diff --git a/packages/provider-hypihub/src/provider.ts b/packages/provider-hypihub/src/provider.tsindex e0a40b65..3c1e97ad 100644--- a/packages/provider-hypihub/src/provider.ts+++ b/packages/provider-hypihub/src/provider.ts@@ -1,5 +1,3 @@-import { createHash } from "node:crypto";-import type { AsyncEndpoint, EndpointFulfillment, EndpointInvocationContext, EndpointPollContext, EndpointStartContext, EndpointOutcome, ImmediateEndpointHandler } from "@hypit/endpoint-kit";import { defineEndpointPackage, wakeAfter } from "@hypit/endpoint-kit";import { geminiCapabilities, geminiModels, verifyGeminiRequest } from "@hypit/gemini";@@ -14,6 +12,7 @@ import { sealText } from "@hypit/text";import { textTypes } from "@hypit/text";import { createHypiHubGeminiGenerator } from "./gemini.js";import { hypiHubRouteForCapability, hypiHubRoutes } from "./routes.js";+import { HypiHubUploader } from "./upload.js";import { transcribeWithHypiHub, whisperXAlignmentRequest } from "./whisperx.js";export const hypiHubProviderModuleRef = { name: "@hypit/provider-hypihub", version: "1" } as const;@@ -91,40 +90,27 @@ function dataUrl(bytes: Uint8Array, mediaType: string): string {return `data:${mediaType};base64,${Buffer.from(bytes).toString("base64")}`;}-function requiredString(value: unknown, subject: string): string {- assert(typeof value === "string" && value.length > 0, `${subject} is missing`);- return value;-}-function requiredInteger(value: unknown, subject: string): number {- assert(typeof value === "number" && Number.isSafeInteger(value) && value > 0, `${subject} is invalid`);- return value;-}-async function resolveArtifactInline(artifacts: ArtifactStore, artifact: BlobRef): Promise<string> {const bytes = await artifacts.get(artifact.digest);assert(bytes !== undefined, `HypiHub reference artifact ${artifact.digest} is unavailable`);return dataUrl(bytes, artifact.mediaType);}-function mediaExtension(mediaType: string): string {- const known: Readonly<Record<string, string>> = {- "image/jpeg": "jpg", "image/png": "png", "image/webp": "webp",- "video/mp4": "mp4", "video/quicktime": "mov",- "audio/mpeg": "mp3", "audio/wav": "wav", "audio/x-wav": "wav", "audio/mp4": "m4a",- };- return known[mediaType] ?? "bin";-}-class HypiHubClient {readonly baseUrl: string;readonly timeout: number;readonly fetcher: typeof globalThis.fetch;- readonly uploadPartTimeout: number;- readonly uploadPartAttempts: number;+ readonly uploader: HypiHubUploader;constructor(options: { readonly baseUrl: string; readonly timeout: number; readonly uploadPartTimeout: number; readonly uploadPartAttempts: number; readonly fetcher: typeof globalThis.fetch }) {this.baseUrl = options.baseUrl.replace(/\/$/u, ""); this.timeout = options.timeout;- this.uploadPartTimeout = options.uploadPartTimeout; this.uploadPartAttempts = options.uploadPartAttempts;this.fetcher = options.fetcher;+ this.uploader = new HypiHubUploader({+ baseUrl: this.baseUrl,+ requestTimeoutMs: this.timeout,+ uploadPartTimeoutMs: options.uploadPartTimeout,+ uploadPartAttempts: options.uploadPartAttempts,+ fetch: this.fetcher,+ });}async json(path: string, apiKey: string, init: RequestInit = {}): Promise<Record<string, unknown>> {const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), this.timeout);@@ -150,130 +136,16 @@ class HypiHubClient {}throw lastError instanceof Error ? lastError : new Error(String(lastError));}- private async uploadForm(artifact: BlobRef, bytes: Uint8Array, apiKey: string): Promise<string> {- const filename = `${artifact.digest.slice("sha256:".length)}.${mediaExtension(artifact.mediaType)}`;- const form = new FormData();- const copy = new ArrayBuffer(bytes.byteLength);- new Uint8Array(copy).set(bytes);- form.append("file", new Blob([copy], { type: artifact.mediaType }), filename);- form.append("purpose", "reference");- const response = await this.json("/files", apiKey, { method: "POST", body: form });- assert(typeof response.url === "string" && /^https:\/\//iu.test(response.url),- "HypiHub file upload returned no HTTPS URL");- return response.url;- }- private async signParts(uploadId: string, declarations: readonly { readonly part_number: number; readonly bytes: number; readonly checksum_sha256: string }[], apiKey: string): Promise<Map<number, Record<string, unknown>>> {- const response = await this.json(`/files/uploads/${encodeURIComponent(uploadId)}/parts`, apiKey, {- method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ parts: declarations }),- });- assert(Array.isArray(response.parts), "HypiHub part signing response has no parts");- const signed = new Map<number, Record<string, unknown>>();- for (const raw of response.parts) {- const item = object(raw, "HypiHub signed upload part");- const partNumber = requiredInteger(item.part_number, "HypiHub signed upload part number");- requiredString(item.url, "HypiHub signed upload URL");- object(item.headers, "HypiHub signed upload headers");- signed.set(partNumber, item);- }- assert(signed.size === declarations.length, "HypiHub signed an incomplete part set");- return signed;- }- private async putPart(uploadId: string, declaration: { readonly part_number: number; readonly bytes: number; readonly checksum_sha256: string }, initial: Record<string, unknown>, body: Uint8Array, apiKey: string): Promise<{ readonly part_number: number; readonly etag: string; readonly checksum_sha256: string }> {- let capability = initial;- for (let attempt = 0; attempt < this.uploadPartAttempts; attempt += 1) {- if (attempt > 0) {- const refreshed = await this.signParts(uploadId, [declaration], apiKey);- capability = refreshed.get(declaration.part_number) ?? {};- }- const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), this.uploadPartTimeout);- try {- const url = requiredString(capability.url, "HypiHub signed upload URL");- const headers = object(capability.headers, "HypiHub signed upload headers");- // Copy the view into a plain ArrayBuffer so this stays compatible with- // both Node's fetch implementation and the DOM BodyInit type.- const payload = new ArrayBuffer(body.byteLength);- new Uint8Array(payload).set(body);- const response = await this.fetcher(url, { method: "PUT", headers: headers as Record<string, string>, body: payload, signal: controller.signal });- if (!response.ok) throw new Error(`S3 rejected upload part ${declaration.part_number} with HTTP ${response.status}`);- const etag = response.headers.get("etag");- assert(etag !== null && etag.length > 0, `S3 upload part ${declaration.part_number} returned no ETag`);- const verified = response.headers.get("x-amz-checksum-sha256");- assert(verified === null || verified === declaration.checksum_sha256,- `S3 upload part ${declaration.part_number} returned a different checksum`);- return { part_number: declaration.part_number, etag, checksum_sha256: declaration.checksum_sha256 };- } catch {- if (attempt + 1 < this.uploadPartAttempts) await new Promise((resolve) => setTimeout(resolve, 500 * (attempt + 1)));- } finally { clearTimeout(timer); }- }- // Never bubble up a fetch implementation's URL-bearing error: the signed- // URL is a short-lived credential and must not enter agent logs/reports.- throw new Error(`S3 upload part ${declaration.part_number} failed after ${this.uploadPartAttempts} attempts`);- }- private async uploadDirect(bytes: Uint8Array, apiKey: string, policy: Record<string, unknown>): Promise<string> {- const uploadId = requiredString(policy.upload_id, "HypiHub upload id");- const partSize = requiredInteger(policy.part_size, "HypiHub upload part size");- const partCount = requiredInteger(policy.part_count, "HypiHub upload part count");- const requestedConcurrency = requiredInteger(policy.concurrency, "HypiHub upload concurrency");- assert(requestedConcurrency <= 8, "HypiHub upload concurrency exceeds 8");- assert(partCount <= 10_000, "HypiHub upload part count exceeds 10000");- const concurrency = Math.min(requestedConcurrency, partCount);- assert(partCount === Math.ceil(bytes.byteLength / partSize), "HypiHub upload part count differs from the file size");- const declarations = Array.from({ length: partCount }, (_, index) => {- const start = index * partSize; const part = bytes.subarray(start, Math.min(start + partSize, bytes.byteLength));- return { part_number: index + 1, bytes: part.byteLength, checksum_sha256: createHash("sha256").update(part).digest("base64") };- });- try {- const signed = await this.signParts(uploadId, declarations, apiKey);- const completed = new Array<{ readonly part_number: number; readonly etag: string; readonly checksum_sha256: string }>(partCount);- let cursor = 0;- const worker = async (): Promise<void> => {- while (cursor < partCount) {- const index = cursor; cursor += 1;- const declaration = declarations[index];- assert(declaration !== undefined, "HypiHub upload part declaration is missing");- const start = index * partSize; const body = bytes.subarray(start, Math.min(start + partSize, bytes.byteLength));- const capability = signed.get(declaration.part_number);- assert(capability !== undefined, `HypiHub upload part ${declaration.part_number} was not signed`);- completed[index] = await this.putPart(uploadId, declaration, capability, body, apiKey);- }- };- await Promise.all(Array.from({ length: concurrency }, worker));- const response = await this.json(`/files/uploads/${encodeURIComponent(uploadId)}/complete`, apiKey, {- method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ parts: completed }),- });- assert(typeof response.url === "string" && /^https:\/\//iu.test(response.url),- "HypiHub direct upload returned no HTTPS URL");- return response.url;- } catch (error) {- try { await this.json(`/files/uploads/${encodeURIComponent(uploadId)}`, apiKey, { method: "DELETE" }); } catch { /* S3 lifecycle is the final abort fallback. */ }- throw error;- }- }async upload(artifact: BlobRef, artifacts: ArtifactStore, apiKey: string): Promise<string> {const bytes = await artifacts.get(artifact.digest);assert(bytes !== undefined, `HypiHub reference artifact ${artifact.digest} is unavailable`);assert(bytes.byteLength === artifact.size, `HypiHub reference artifact ${artifact.digest} size differs`);- const computed = createHash("sha256").update(bytes).digest("hex");- const declared = artifact.digest.startsWith("sha256:") ? artifact.digest.slice("sha256:".length) : "";- assert(declared === computed, `HypiHub reference artifact ${artifact.digest} failed its SHA-256 check`);- const filename = `${computed}.${mediaExtension(artifact.mediaType)}`;- let policy: Record<string, unknown>;- try {- policy = await this.json("/files/uploads", apiKey, {- method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({- filename, bytes: bytes.byteLength, mime_type: artifact.mediaType, purpose: "reference",- sha256: computed, head_base64: Buffer.from(bytes.subarray(0, 512)).toString("base64"),- }),- });- } catch (error) {- // Rolling upgrades remain safe: an older HypiHub has no policy endpoint,- // so only its exact 404 falls back to the established multipart route.- if (!(error instanceof Error) || !/HypiHub returned HTTP 404:/u.test(error.message)) throw error;- return await this.uploadForm(artifact, bytes, apiKey);- }- if (policy.upload_mode === "api_multipart") return await this.uploadForm(artifact, bytes, apiKey);- assert(policy.upload_mode === "s3_multipart", "HypiHub returned an unknown upload mode");- return await this.uploadDirect(bytes, apiKey, policy);+ return this.uploader.upload({+ bytes,+ mediaType: artifact.mediaType,+ purpose: "reference",+ sha256: artifact.digest,+ }, apiKey);}async transcribe(body: Record<string, unknown>, apiKey: string): Promise<Record<string, unknown>> {return this.json("/audio/transcriptions", apiKey, {@@ -388,6 +260,8 @@ export function createHypiHubProvider(options: CreateHypiHubProviderOptions = {}model: context.need.capability.name,...(options.baseUrl === undefined ? {} : { baseUrl: options.baseUrl }),...(options.requestTimeoutMs === undefined ? {} : { requestTimeoutMs: options.requestTimeoutMs }),+ ...(options.uploadPartTimeoutMs === undefined ? {} : { uploadPartTimeoutMs: options.uploadPartTimeoutMs }),+ ...(options.uploadPartAttempts === undefined ? {} : { uploadPartAttempts: options.uploadPartAttempts }),...(options.fetch === undefined ? {} : { fetch: options.fetch }),maxRateLimitRetries: 5,rateLimitRetryDelayMs: 3_000,diff --git a/packages/provider-hypihub/src/upload.ts b/packages/provider-hypihub/src/upload.tsnew file mode 100644index 00000000..d416b300--- /dev/null+++ b/packages/provider-hypihub/src/upload.ts@@ -0,0 +1,287 @@+import { createHash } from "node:crypto";++function assert(condition: unknown, message: string): asserts condition {+ if (!condition) throw new Error(message);+}++function object(value: unknown, subject: string): Record<string, unknown> {+ assert(value !== null && typeof value === "object" && !Array.isArray(value), `${subject} must be an object`);+ return value as Record<string, unknown>;+}++function requiredString(value: unknown, subject: string): string {+ assert(typeof value === "string" && value.length > 0, `${subject} is missing`);+ return value;+}++function requiredInteger(value: unknown, subject: string): number {+ assert(typeof value === "number" && Number.isSafeInteger(value) && value > 0, `${subject} is invalid`);+ return value;+}++function extension(mediaType: string): string {+ const known: Readonly<Record<string, string>> = {+ "image/jpeg": "jpg", "image/png": "png", "image/webp": "webp",+ "video/mp4": "mp4", "video/quicktime": "mov", "video/webm": "webm",+ "audio/mpeg": "mp3", "audio/wav": "wav", "audio/x-wav": "wav", "audio/mp4": "m4a",+ };+ return known[mediaType.toLowerCase()] ?? "bin";+}++function apiBaseUrl(value: string): string {+ const trimmed = value.trim().replace(/\/+$/u, "");+ assert(trimmed.length > 0, "HypiHub upload base URL is empty");+ return `${trimmed.replace(/\/(?:v1beta|v1)$/iu, "")}/v1`;+}++class HypiHubHTTPError extends Error {+ constructor(readonly status: number, message: string) {+ super(message);+ }+}++export type HypiHubUploaderOptions = {+ readonly baseUrl: string;+ readonly requestTimeoutMs: number;+ readonly uploadPartTimeoutMs?: number;+ readonly uploadPartAttempts?: number;+ readonly fetch: typeof globalThis.fetch;+};++export type HypiHubUploadInput = {+ readonly bytes: Uint8Array;+ readonly mediaType: string;+ readonly filename?: string;+ readonly purpose?: string;+ readonly sha256?: string;+};++type PartDeclaration = {+ readonly part_number: number;+ readonly bytes: number;+ readonly checksum_sha256: string;+};++type CompletedPart = {+ readonly part_number: number;+ readonly etag: string;+ readonly checksum_sha256: string;+};++function assertHTTPS(value: string, subject: string): void {+ let protocol = "";+ try { protocol = new URL(value).protocol; }+ catch { throw new Error(`${subject} is invalid`); }+ assert(protocol === "https:", `${subject} must use HTTPS`);+}++/** Uploads media either through the compatibility form endpoint or directly to private regional S3. */+export class HypiHubUploader {+ readonly baseUrl: string;+ readonly requestTimeout: number;+ readonly uploadPartTimeout: number;+ readonly uploadPartAttempts: number;+ readonly fetcher: typeof globalThis.fetch;++ constructor(options: HypiHubUploaderOptions) {+ this.baseUrl = apiBaseUrl(options.baseUrl);+ this.requestTimeout = requiredInteger(options.requestTimeoutMs, "HypiHub upload request timeout");+ this.uploadPartTimeout = requiredInteger(options.uploadPartTimeoutMs ?? 5 * 60_000,+ "HypiHub upload part timeout");+ this.uploadPartAttempts = requiredInteger(options.uploadPartAttempts ?? 3,+ "HypiHub upload part attempts");+ assert(this.uploadPartAttempts <= 8, "HypiHub upload part attempts must be within 1..8");+ this.fetcher = options.fetch;+ }++ private async json(path: string, apiKey: string, init: RequestInit = {}): Promise<Record<string, unknown>> {+ const controller = new AbortController();+ const timer = setTimeout(() => controller.abort(), this.requestTimeout);+ try {+ const response = await this.fetcher(`${this.baseUrl}${path}`, {+ ...init,+ signal: controller.signal,+ headers: { authorization: `Bearer ${apiKey}`, ...(init.headers ?? {}) },+ });+ const text = await response.text();+ let body: unknown = {};+ try { body = text.length === 0 ? {} : JSON.parse(text); }+ catch { throw new Error(`HypiHub returned invalid JSON (${response.status})`); }+ if (!response.ok) {+ throw new HypiHubHTTPError(response.status, `HypiHub returned HTTP ${response.status}: ${text.slice(0, 300)}`);+ }+ return object(body, "HypiHub response");+ } finally {+ clearTimeout(timer);+ }+ }++ private async uploadForm(input: HypiHubUploadInput, apiKey: string, digest: string): Promise<string> {+ const form = new FormData();+ const copy = new ArrayBuffer(input.bytes.byteLength);+ new Uint8Array(copy).set(input.bytes);+ form.append("file", new Blob([copy], { type: input.mediaType }),+ input.filename ?? `${digest}.${extension(input.mediaType)}`);+ form.append("purpose", input.purpose ?? "reference");+ const response = await this.json("/files", apiKey, { method: "POST", body: form });+ const url = requiredString(response.url, "HypiHub file upload URL");+ assert(/^https:\/\//iu.test(url), "HypiHub file upload returned no HTTPS URL");+ return url;+ }++ private async signParts(uploadId: string, declarations: readonly PartDeclaration[], apiKey: string): Promise<Map<number, Record<string, unknown>>> {+ const response = await this.json(`/files/uploads/${encodeURIComponent(uploadId)}/parts`, apiKey, {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ parts: declarations }),+ });+ assert(Array.isArray(response.parts), "HypiHub part signing response has no parts");+ const signed = new Map<number, Record<string, unknown>>();+ for (const raw of response.parts) {+ const item = object(raw, "HypiHub signed upload part");+ const partNumber = requiredInteger(item.part_number, "HypiHub signed upload part number");+ assertHTTPS(requiredString(item.url, "HypiHub signed upload URL"), "HypiHub signed upload URL");+ object(item.headers, "HypiHub signed upload headers");+ signed.set(partNumber, item);+ }+ assert(signed.size === declarations.length, "HypiHub signed an incomplete part set");+ return signed;+ }++ private signedHeaders(value: unknown, declaration: PartDeclaration): Record<string, string> {+ const raw = object(value, "HypiHub signed upload headers");+ const headers = new Map<string, string>();+ for (const [name, headerValue] of Object.entries(raw)) {+ assert(typeof headerValue === "string", `HypiHub signed upload header ${name} is invalid`);+ headers.set(name.toLowerCase(), headerValue);+ }+ const contentLength = requiredString(headers.get("content-length"), "HypiHub signed Content-Length");+ const checksum = requiredString(headers.get("x-amz-checksum-sha256"), "HypiHub signed checksum");+ assert(contentLength === String(declaration.bytes), "HypiHub signed a different part size");+ assert(checksum === declaration.checksum_sha256, "HypiHub signed a different part checksum");+ return { "content-length": contentLength, "x-amz-checksum-sha256": checksum };+ }++ private async putPart(uploadId: string, declaration: PartDeclaration, initial: Record<string, unknown>, body: Uint8Array, apiKey: string): Promise<CompletedPart> {+ let capability = initial;+ for (let attempt = 0; attempt < this.uploadPartAttempts; attempt += 1) {+ if (attempt > 0) {+ const refreshed = await this.signParts(uploadId, [declaration], apiKey);+ capability = refreshed.get(declaration.part_number) ?? {};+ }+ const controller = new AbortController();+ const timer = setTimeout(() => controller.abort(), this.uploadPartTimeout);+ try {+ const url = requiredString(capability.url, "HypiHub signed upload URL");+ assertHTTPS(url, "HypiHub signed upload URL");+ const payload = new ArrayBuffer(body.byteLength);+ new Uint8Array(payload).set(body);+ const response = await this.fetcher(url, {+ method: "PUT",+ headers: this.signedHeaders(capability.headers, declaration),+ body: payload,+ signal: controller.signal,+ });+ if (!response.ok) throw new Error(`S3 rejected upload part ${declaration.part_number} with HTTP ${response.status}`);+ const etag = response.headers.get("etag");+ assert(etag !== null && etag.length > 0, `S3 upload part ${declaration.part_number} returned no ETag`);+ const verified = response.headers.get("x-amz-checksum-sha256");+ assert(verified === null || verified === declaration.checksum_sha256,+ `S3 upload part ${declaration.part_number} returned a different checksum`);+ return {+ part_number: declaration.part_number,+ etag,+ checksum_sha256: declaration.checksum_sha256,+ };+ } catch {+ if (attempt + 1 < this.uploadPartAttempts) {+ await new Promise((resolve) => setTimeout(resolve, 500 * (attempt + 1)));+ }+ } finally {+ clearTimeout(timer);+ }+ }+ // A presigned URL is a temporary credential, so never include the fetcher's URL-bearing error.+ throw new Error(`S3 upload part ${declaration.part_number} failed after ${this.uploadPartAttempts} attempts`);+ }++ private async uploadDirect(bytes: Uint8Array, apiKey: string, policy: Record<string, unknown>): Promise<string> {+ const uploadId = requiredString(policy.upload_id, "HypiHub upload id");+ const partSize = requiredInteger(policy.part_size, "HypiHub upload part size");+ const partCount = requiredInteger(policy.part_count, "HypiHub upload part count");+ const requestedConcurrency = requiredInteger(policy.concurrency, "HypiHub upload concurrency");+ assert(requestedConcurrency <= 8, "HypiHub upload concurrency exceeds 8");+ assert(partCount <= 10_000, "HypiHub upload part count exceeds 10000");+ assert(partCount === Math.ceil(bytes.byteLength / partSize), "HypiHub upload part count differs from the file size");+ const declarations = Array.from({ length: partCount }, (_, index) => {+ const start = index * partSize;+ const part = bytes.subarray(start, Math.min(start + partSize, bytes.byteLength));+ return {+ part_number: index + 1,+ bytes: part.byteLength,+ checksum_sha256: createHash("sha256").update(part).digest("base64"),+ };+ });+ try {+ const signed = await this.signParts(uploadId, declarations, apiKey);+ const completed = new Array<CompletedPart>(partCount);+ let cursor = 0;+ const worker = async (): Promise<void> => {+ while (cursor < partCount) {+ const index = cursor;+ cursor += 1;+ const declaration = declarations[index];+ assert(declaration !== undefined, "HypiHub upload part declaration is missing");+ const start = index * partSize;+ const body = bytes.subarray(start, Math.min(start + partSize, bytes.byteLength));+ const capability = signed.get(declaration.part_number);+ assert(capability !== undefined, `HypiHub upload part ${declaration.part_number} was not signed`);+ completed[index] = await this.putPart(uploadId, declaration, capability, body, apiKey);+ }+ };+ await Promise.all(Array.from({ length: Math.min(requestedConcurrency, partCount) }, worker));+ const response = await this.json(`/files/uploads/${encodeURIComponent(uploadId)}/complete`, apiKey, {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({ parts: completed }),+ });+ const url = requiredString(response.url, "HypiHub direct upload URL");+ assert(/^https:\/\//iu.test(url), "HypiHub direct upload returned no HTTPS URL");+ return url;+ } catch (error) {+ try { await this.json(`/files/uploads/${encodeURIComponent(uploadId)}`, apiKey, { method: "DELETE" }); }+ catch { /* S3 Lifecycle is the final abort fallback. */ }+ throw error;+ }+ }++ async upload(input: HypiHubUploadInput, apiKey: string): Promise<string> {+ assert(input.bytes.byteLength > 0, "HypiHub reference artifact is empty");+ const digest = createHash("sha256").update(input.bytes).digest("hex");+ if (input.sha256 !== undefined) {+ assert(input.sha256.toLowerCase().replace(/^sha256:/u, "") === digest,+ "HypiHub reference artifact failed its SHA-256 check");+ }+ let policy: Record<string, unknown>;+ try {+ policy = await this.json("/files/uploads", apiKey, {+ method: "POST",+ headers: { "content-type": "application/json" },+ body: JSON.stringify({+ filename: input.filename ?? `${digest}.${extension(input.mediaType)}`,+ bytes: input.bytes.byteLength,+ mime_type: input.mediaType,+ purpose: input.purpose ?? "reference",+ sha256: digest,+ head_base64: Buffer.from(input.bytes.subarray(0, 512)).toString("base64"),+ }),+ });+ } catch (error) {+ if (!(error instanceof HypiHubHTTPError) || error.status !== 404) throw error;+ return this.uploadForm(input, apiKey, digest);+ }+ if (policy.upload_mode === "api_multipart") return this.uploadForm(input, apiKey, digest);+ assert(policy.upload_mode === "s3_multipart", "HypiHub returned an unknown upload mode");+ return this.uploadDirect(input.bytes, apiKey, policy);+ }+}diff --git a/packages/provider-hypihub/test/gemini.test.ts b/packages/provider-hypihub/test/gemini.test.tsindex df3a90ce..0c3c43dd 100644--- a/packages/provider-hypihub/test/gemini.test.ts+++ b/packages/provider-hypihub/test/gemini.test.ts@@ -13,6 +13,9 @@ test("HypiHub Gemini uses native wire format for text, image and video parts", abaseUrl: "https://hypit.ai/v1",fetch: async (input, init) => {seenUrl = String(input);+ if (seenUrl.endsWith("/v1/files/uploads")) {+ return Response.json({ upload_mode: "api_multipart" });+ }if (seenUrl.endsWith("/v1/files")) {uploads += 1;assert.equal(init?.method, "POST");@@ -56,6 +59,9 @@ test("HypiHub Gemini retries upstream rate limits without reuploading files", asrateLimitRetryDelayMs: 1,fetch: async (input) => {const url = String(input);+ if (url.endsWith("/v1/files/uploads")) {+ return Response.json({ upload_mode: "api_multipart" });+ }if (url.endsWith("/v1/files")) {uploads += 1;return Response.json({ url: "https://hypit.ai/files/ref" });@@ -72,3 +78,66 @@ test("HypiHub Gemini retries upstream rate limits without reuploading files", asassert.equal(uploads, 1);assert.equal(generations, 3);});++test("HypiHub Gemini uploads compressed media directly to regional S3", async () => {+ const calls: string[] = [];+ let generationBody: Record<string, unknown> | undefined;+ const generate = createHypiHubGeminiGenerator({+ apiKey: "test-key",+ baseUrl: "https://hypit.ai",+ fetch: async (input, init) => {+ const url = String(input);+ calls.push(url);+ if (url.endsWith("/v1/files/uploads")) {+ const body = JSON.parse(String(init?.body)) as Record<string, unknown>;+ assert.equal(body.bytes, 5);+ assert.equal(body.mime_type, "image/webp");+ return Response.json({+ upload_mode: "s3_multipart",+ upload_id: "up_gemini",+ part_size: 16,+ part_count: 1,+ concurrency: 4,+ }, { status: 201 });+ }+ if (url.endsWith("/v1/files/uploads/up_gemini/parts")) {+ const body = JSON.parse(String(init?.body)) as {+ readonly parts: readonly { readonly checksum_sha256: string }[];+ };+ return Response.json({ parts: [{+ part_number: 1,+ url: "https://hypihub-prod-media-hk.s3.ap-east-1.amazonaws.com/opaque?signature=secret",+ headers: {+ "content-length": "5",+ "x-amz-checksum-sha256": body.parts[0]?.checksum_sha256,+ },+ }] });+ }+ if (url.startsWith("https://hypihub-prod-media-hk.s3.ap-east-1.amazonaws.com/")) {+ assert.equal(init?.method, "PUT");+ assert.deepEqual(new Uint8Array(await new Response(init?.body).arrayBuffer()),+ new Uint8Array(Buffer.from("small")));+ return new Response(null, { status: 200, headers: { etag: "\"gemini-part\"" } });+ }+ if (url.endsWith("/v1/files/uploads/up_gemini/complete")) {+ return Response.json({ url: "https://hypit.ai/files/as_gemini" });+ }+ if (url.includes(":generateContent")) {+ generationBody = JSON.parse(String(init?.body)) as Record<string, unknown>;+ return Response.json({ candidates: [{ content: { parts: [{ text: "OK" }] } }] });+ }+ throw new Error(`Unexpected URL ${url}`);+ },+ });++ assert.equal(await generate({ instruction: "x", parts: [+ { text: "inspect" },+ { inlineData: { mimeType: "image/webp", data: Buffer.from("small").toString("base64") } },+ ] }), "OK");+ assert.equal(calls.some((url) => url.endsWith("/v1/files")), false);+ assert.equal(calls.some((url) => url.includes("s3-accelerate")), false);+ assert.deepEqual(generationBody?.contents, [{ role: "user", parts: [+ { text: "inspect" },+ { fileData: { mimeType: "image/webp", fileUri: "https://hypit.ai/files/as_gemini" } },+ ] }]);+});diff --git a/packages/provider-hypihub/test/provider.test.ts b/packages/provider-hypihub/test/provider.test.tsindex 0e88ada3..1b0ce122 100644--- a/packages/provider-hypihub/test/provider.test.ts+++ b/packages/provider-hypihub/test/provider.test.ts@@ -175,7 +175,7 @@ test("HypiHub fulfills Gemini through the Runtime endpoint and uploads every med] }]);});-test("HypiHub splits a large Artifact across accelerated S3 parts and retries only the failed part", async () => {+test("HypiHub splits an Artifact across regional S3 parts and retries only the failed part", async () => {const artifacts = new MemoryArtifactStore();const reference = await artifacts.put(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]), "video/mp4");const request = need(sealSeedanceRequest("seedance-2-mini", {@@ -202,11 +202,11 @@ test("HypiHub splits a large Artifact across accelerated S3 parts and retries onsignedBatches.push(body.parts.map((part) => part.part_number));return Response.json({ parts: body.parts.map((part) => ({part_number: part.part_number,- url: `https://private.s3-accelerate.amazonaws.com/opaque-part-${part.part_number}?signature=secret`,+ url: `https://private.s3.ap-east-1.amazonaws.com/opaque-part-${part.part_number}?signature=secret`,headers: { "content-length": part.part_number === 3 ? "2" : "5", "x-amz-checksum-sha256": part.checksum_sha256 },})) });}- if (url.startsWith("https://private.s3-accelerate.amazonaws.com/")) {+ if (url.startsWith("https://private.s3.ap-east-1.amazonaws.com/")) {const match = /opaque-part-(\d+)/u.exec(url); assert.ok(match);const partNumber = Number(match[1]); const attempt = (putAttempts.get(partNumber) ?? 0) + 1;putAttempts.set(partNumber, attempt);