feat: add scoped read-only audio preview and isolated Dify transport

This commit is contained in:
2026-10-09 16:10:59 +08:00
parent 27772bec61
commit 4d0af7f3f4
36 changed files with 1517 additions and 68 deletions
@@ -14,6 +14,8 @@ final class FollowupAudioProviderConfig
$legacy = $legacy ?? (array) config('prescription_ai', []);
$provider = (array) ($settings['providers'][$profile] ?? []);
$driver = (string) ($provider['driver'] ?? 'dify');
$httpTransport = $settings['http_transport'] ?? 'curl';
if (!in_array($httpTransport, ['curl', 'openssl_stream'], true) || ($driver !== 'asr_then_llm' && $httpTransport !== 'curl')) { throw new FollowupAudioException('CONFIG_INVALID'); }
if (!in_array($driver, ['dify', 'openai_audio', 'asr_then_llm'], true)) { throw new FollowupAudioException('CONFIG_INVALID'); }
if ($driver === 'asr_then_llm') { return self::pipeline($provider, $settings, $profile); }
$base = (string) ($provider['base_url'] ?? '');
@@ -80,6 +82,29 @@ final class FollowupAudioProviderConfig
&& hash_equals($provider['fingerprint'], $verified);
}
/** Synthetic connection readiness is not the full audio/accuracy acceptance gate. */
public static function previewVerified(string $profile, ?array $settings = null, ?array $legacy = null): bool
{
$settings = $settings ?? (array) config('followup_audio', []);
$legacy = $legacy ?? (array) config('prescription_ai', []);
try { $provider = self::resolve($profile, $settings, $legacy); }
catch (FollowupAudioException $error) { return false; }
$fingerprint = (string) ($settings['providers'][$profile]['preview_verified_fingerprint'] ?? '');
$secure = $provider['driver'] === 'asr_then_llm'
? self::secureEndpoint($provider['asr']['base_url'], $provider['allow_loopback_tunnel']) && self::secureEndpoint($provider['extraction']['base_url'], $provider['allow_loopback_tunnel'])
: str_starts_with($provider['base_url'], 'https://');
return $secure && preg_match('/^[a-f0-9]{64}$/D', $fingerprint) && hash_equals($provider['fingerprint'], $fingerprint);
}
public static function readyModels(): array
{
$models = [];
foreach (['qwen', 'openai'] as $profile) {
if (FollowupAudioGate::ready($profile)) { $models[] = ['value' => $profile, 'label' => self::resolve($profile)['label']]; }
}
return $models;
}
/** Literal loopback only, explicitly configured for a locally established SSH tunnel. No DNS exception. */
public static function secureEndpoint(string $base, bool $allowLoopback): bool
{
@@ -94,15 +119,30 @@ final class FollowupAudioProviderConfig
$resolved = ['driver' => 'asr_then_llm', 'allow_loopback_tunnel' => $allowLoopback];
foreach (['asr' => '/audio/transcriptions', 'extraction' => '/chat/completions'] as $stage => $endpoint) {
$input = (array) ($provider[$stage] ?? []);
// Use existing endpoint/key/model syntax validation without inheriting any legacy service.
$part = self::resolve($profile, ['providers' => [$profile => array_merge($input, ['driver' => 'openai_audio'])]], []);
$protocol = $input['protocol'] ?? 'openai';
$allowed = $stage === 'asr' ? ['openai', 'dify'] : ['openai', 'dify_chat'];
if (!in_array($protocol, $allowed, true)) { throw new FollowupAudioException('CONFIG_INVALID'); }
$revision = $input['binding_revision'] ?? '';
if ($protocol !== 'openai' && (!is_string($revision) || !preg_match('/^[A-Za-z0-9_.:-]{1,128}$/D', $revision))) {
throw new FollowupAudioException('CONFIG_INVALID');
}
$checked = $input;
if ($protocol !== 'openai') {
$endpoint = $stage === 'asr' ? '/audio-to-text' : '/chat-messages';
$original = rtrim((string) ($input['base_url'] ?? ''), '/');
if (str_ends_with($original, $endpoint)) { $checked['base_url'] = substr($original, 0, -strlen($endpoint)); }
}
// Model is a bound expected deployment identity for Dify, not a fake request parameter.
$part = self::resolve($profile, ['providers' => [$profile => array_merge($checked, ['driver' => 'openai_audio'])]], []);
$base = $part['base_url'];
if ($stage === 'asr') {
if ($stage === 'asr' && $protocol === 'openai') {
$original = rtrim((string) ($input['base_url'] ?? ''), '/');
if (str_ends_with($original, $endpoint)) { $base = substr($original, 0, -strlen($endpoint)); }
}
if (!self::secureEndpoint($base, $allowLoopback)) { throw new FollowupAudioException('HTTPS_REQUIRED'); }
$resolved[$stage] = ['base_url' => $base, 'api_key' => $part['api_key'], 'model' => $part['model']];
// Default/explicit OpenAI retains its previous exact fingerprint representation.
if ($protocol !== 'openai') { $resolved[$stage] += ['protocol' => $protocol, 'binding_revision' => $revision]; }
}
$chunkSeconds = (int) ($settings['asr_chunk_seconds'] ?? 120);
if ($chunkSeconds < 1 || $chunkSeconds > 120) { throw new FollowupAudioException('CONFIG_INVALID'); }
@@ -118,7 +158,11 @@ final class FollowupAudioProviderConfig
if (!in_array($mode, ['json_schema', 'json_object', 'prompt_json'], true)
|| !is_int($maxTokens) || $maxTokens < 256 || $maxTokens > 8192
|| ($thinking !== null && !is_bool($thinking))) { throw new FollowupAudioException('CONFIG_INVALID'); }
$resolved['extraction'] += ['response_format' => $mode, 'max_tokens' => $maxTokens, 'enable_thinking' => $thinking];
if (($resolved['extraction']['protocol'] ?? 'openai') === 'dify_chat') {
if (($provider['extraction']['response_format'] ?? null) !== 'prompt_json' || $thinking !== null) { throw new FollowupAudioException('CONFIG_INVALID'); }
$resolved['extraction']['response_format'] = 'prompt_json';
// Dify App owns model generation parameters. Local max_tokens is not transmitted or claimed effective.
} else { $resolved['extraction'] += ['response_format' => $mode, 'max_tokens' => $maxTokens, 'enable_thinking' => $thinking]; }
$resolved['output_schema'] = FollowupAudioExtractionSchema::VERSION;
$resolved['citation_policy'] = FollowupAudioTranscriptPrompt::CITATION_POLICY;
$resolved['schema_dialect'] = FollowupAudioExtractionSchema::DIALECT;
@@ -126,8 +170,13 @@ final class FollowupAudioProviderConfig
if ($resolved['label'] === '' || strlen($resolved['label']) > 200 || preg_match('/[\x00-\x1f\x7f]/', $resolved['label'])) {
throw new FollowupAudioException('CONFIG_INVALID');
}
$resolved['fingerprint'] = hash('sha256', json_encode([$resolved['driver'], $resolved['asr'], $resolved['extraction'],
$allowLoopback, $chunkSeconds, 'pcm-s16le-mono-16000-v1', $resolved['output_schema'], $resolved['citation_policy'], $resolved['schema_dialect'], $resolved['channel_policy'], $stereoSeconds, $resolved['silence_policy'], 'checkpoint-v2'], JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
$identity = [$resolved['driver'], $resolved['asr'], $resolved['extraction'],
$allowLoopback, $chunkSeconds, 'pcm-s16le-mono-16000-v1', $resolved['output_schema'], $resolved['citation_policy'], $resolved['schema_dialect'], $resolved['channel_policy'], $stereoSeconds, $resolved['silence_policy'], 'checkpoint-v2'];
if (($settings['http_transport'] ?? 'curl') !== 'curl') {
$resolved['http_transport'] = 'openssl_stream';
$identity[] = 'openssl-stream-child-v1';
}
$resolved['fingerprint'] = hash('sha256', json_encode($identity, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
return $resolved;
}