Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

2.2 KB
Raw Blame
import {
  normalizeVoiceConversationSelection,
  resolveSpeechSpeed,
  speechSpeedRange,
  voiceModelKey,
  type VoiceConversationModel,
  type VoiceConversationSelection,
} from '@/../shared/voiceConversation';

/** Settings and call startup must resolve the same route without saving inferred defaults. */
export function resolveVoiceConfiguration(
  models: readonly VoiceConversationModel[],
  preference: VoiceConversationSelection | null,
  voice?: string,
  speed = preference?.speed,
) {
  const selectedModel = preference
    ? models.find((model) => voiceModelKey(model) === voiceModelKey(preference))
    : models.find((model) => model.pickerVisible !== false);
  const protocolOverride = selectedModel?.protocolLocked ? undefined : preference?.protocol;
  const incompatibleSavedProtocol = Boolean(
    selectedModel?.protocolLocked &&
    preference?.protocol !== undefined &&
    preference.protocol !== selectedModel.protocol,
  );
  // Voices do not transfer between adapters. Preserve the stored choice until
  // the user corrects it, but never start a call with a mismatched voice.
  const selectedVoice =
    voice ??
    (incompatibleSavedProtocol ? '' : (preference?.voice ?? selectedModel?.defaultVoice ?? ''));
  const protocol =
    protocolOverride ??
    selectedModel?.protocol ??
    ((selectedModel?.providerId ?? 'xd') === 'xd' ? 'openai' : undefined);
  const resolvedSpeed = selectedModel
    ? resolveSpeechSpeed(selectedModel.id, protocol, speed, selectedModel.speedRange)
    : undefined;
  const selection = selectedModel
    ? normalizeVoiceConversationSelection({
        providerId: selectedModel.providerId,
        agent: selectedModel.agent,
        modelId: selectedModel.id,
        protocol: protocolOverride,
        voice: selectedVoice,
        ...(resolvedSpeed !== undefined ? { speed: resolvedSpeed } : {}),
      })
    : null;
  return {
    selectedModel,
    protocolOverride,
    incompatibleSavedProtocol,
    selectedVoice,
    protocol,
    selection,
    speedRange: selectedModel ? speechSpeedRange(selectedModel.id, protocol, selectedModel.speedRange) : null,
    speed: resolvedSpeed,
    speedAdjusted: speed !== undefined && speed !== resolvedSpeed,
  };
}