Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

4.0 KB
Raw Blame
/** Production STT adapter: one admitted buffer, sequential broker work, dual consent. */
import { createHash } from "node:crypto";

import {
  resolveAudioBridgeRuntimeSettings,
  resolveVideoAudioTranscriptionRuntimeSettings,
} from "@/shared/constants/modalityBridgeDefaults";

import type { BridgeCacheStore } from "./modalityBridge/bridgeCache";
import {
  orchestrateVideoAudioTranscription,
  type VideoAudioOrchestrationOptions,
} from "./videoBridgeAudioOrchestration";
import { extractVideoFramesViaBroker } from "./videoBridgeBrokerClient";
import { describeVideoPart, type DescribeVideoDependencies } from "./videoBridgeHelpers";
import type { VisionBridgeKeyScope } from "./visionBridgeKeyScope";

export interface VideoSttAdapterDependencies {
  extractAudio?: VideoAudioOrchestrationOptions["extractAudio"];
  selectAudioModel?: VideoAudioOrchestrationOptions["selectModel"];
  transcribeAudio?: VideoAudioOrchestrationOptions["transcribe"];
}

export function createVideoSttAdapter(input: {
  cache: BridgeCacheStore | null;
  dependencies: VideoSttAdapterDependencies;
  /**
   * GHSA-3w2r: the caller API key's scope (null when the key is unrestricted)
   * and the credential check narrowed to it for STT model selection.
   */
  keyScope?: {
    scope: VisionBridgeKeyScope;
    hasUsableCredentials: VideoAudioOrchestrationOptions["hasUsableCredentials"];
  } | null;
  principalId: unknown;
  settings: Record<string, unknown>;
}): typeof describeVideoPart {
  const operator = resolveVideoAudioTranscriptionRuntimeSettings(input.settings);
  const audio = resolveAudioBridgeRuntimeSettings(input.settings);
  return async (part, options, caption, dependencies = {}, preloadedBytes) => {
    if (!audio.enabled || !operator.enabled || part.audioTranscription !== true) {
      return describeVideoPart(part, options, caption, dependencies, preloadedBytes);
    }
    const started = Date.now();
    let outcome: Awaited<ReturnType<typeof orchestrateVideoAudioTranscription>> | undefined;
    const adapted: DescribeVideoDependencies = {
      ...dependencies,
      extractFrames: async (bytes, extraction) => {
        // A secret is never a cache identity. Missing authenticated identity disables STT cache.
        const principal = typeof input.principalId === "string" ? input.principalId : null;
        const digest = createHash("sha256").update(bytes).digest("hex");
        const cacheKeyRef = principal ? JSON.stringify([principal, digest]) : undefined;
        outcome = await orchestrateVideoAudioTranscription({
          cache: input.cache,
          cacheKeyRef,
          extractAudio: input.dependencies.extractAudio,
          ...(input.keyScope
            ? {
                hasUsableCredentials: input.keyScope.hasUsableCredentials,
                keyScope: input.keyScope.scope,
              }
            : {}),
          model: audio.model || "auto",
          operatorOptIn: operator.enabled,
          requestOptIn: part.audioTranscription === true,
          selectModel: input.dependencies.selectAudioModel,
          signal: extraction.signal,
          timeoutMs: Math.max(1, options.timeoutMs - (Date.now() - started)),
          transcribe: input.dependencies.transcribeAudio,
          videoBytes: bytes,
        });
        adapted.serverAudioTranscript = outcome.track?.observations;
        return (dependencies.extractFrames ?? extractVideoFramesViaBroker)(bytes, {
          ...extraction,
          timeoutMs: Math.max(1, options.timeoutMs - (Date.now() - started)),
        });
      },
    };
    const described = await describeVideoPart(part, options, caption, adapted, preloadedBytes);
    if (outcome?.attempted && !outcome.track) {
      return {
        ...described,
        fusion: {
          audioAvailable: false,
          videoAvailable: true,
          partial: true,
          failures: {
            audio:
              outcome.reason === "TIMEOUT" || outcome.reason === "ABORTED" ? "ABORTED" : "FAILED",
          },
        },
      };
    }
    return described;
  };
}