Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

2.3 KB
Raw Blame
import { benchmarkProfileInteractive, completeVoiceProfileInteractiveBenchmark } from './apiVoice';

/** Prepare a fresh probe; its returned play action needs a second user gesture. */
export async function prepareProfilePlayback(profileId, payload = {}, options) {
  const startedAt = performance.now();
  const { benchmark } = await benchmarkProfileInteractive(profileId, payload, options);
  const renderRequestLatencyMs = Math.ceil(performance.now() - startedAt);
  if (!benchmark?.benchmarkId || !benchmark?.audioBase64) throw new Error('No playable benchmark was returned');
  return {
    profileId, profileRevision: benchmark.profileRevision,
    // No await before play(): this runs directly in the operator's click gesture.
    play: async () => {
      const playbackStartedAt = performance.now();
      const audio = new Audio(`data:audio/wav;base64,${benchmark.audioBase64}`);
      const playbackStartupMs = await new Promise((resolve, reject) => {
        let settled = false;
        let firstPlaybackMs = null;
        const finish = (error) => {
          if (settled) return;
          settled = true;
          clearTimeout(timeout);
          audio.removeEventListener('playing', onPlaying);
          audio.removeEventListener('error', onError);
          audio.removeEventListener('ended', onEnded);
          audio.pause();
          audio.removeAttribute('src');
          if (error || firstPlaybackMs === null) reject(error || new Error('The benchmark ended without starting playback'));
          else resolve(firstPlaybackMs);
        };
        const onPlaying = () => { firstPlaybackMs ??= Math.ceil(performance.now() - playbackStartedAt); };
        const onEnded = () => finish();
        const onError = () => finish(new Error('The benchmark audio could not play'));
        const timeout = setTimeout(() => finish(new Error('Benchmark playback timed out')), 30000);
        audio.addEventListener('playing', onPlaying);
        audio.addEventListener('error', onError);
        audio.addEventListener('ended', onEnded);
        // Browser play() reports playback denial through its promise.
        audio.play().catch(finish);
      });
      return completeVoiceProfileInteractiveBenchmark(profileId, {
        benchmarkId: benchmark.benchmarkId, renderRequestLatencyMs, playbackStartupMs,
      }, options);
    },
  };
}