35 lines
3.0 KiB
PHP
35 lines
3.0 KiB
PHP
<?php
|
|
$directory = getenv('FOLLOWUP_AUDIO_DIFY_MOCK_DIR');
|
|
$uri = $_SERVER['REQUEST_URI'];
|
|
$audio = str_ends_with($uri, '/audio-to-text'); $chat = str_ends_with($uri, '/chat-messages');
|
|
$valid = ($_SERVER['HTTP_AUTHORIZATION'] ?? '') === 'Bearer synthetic-dify-key';
|
|
if ($audio) {
|
|
$valid = $valid && array_keys($_POST) === ['user'] && preg_match('/^fa-opaque-[a-f0-9]{48}$/D', $_POST['user'] ?? '')
|
|
&& isset($_FILES['file']) && ($_FILES['file']['type'] ?? '') === 'audio/wav' && is_file($_FILES['file']['tmp_name']);
|
|
$meta = ['stage' => 'asr', 'valid' => (bool) $valid, 'uri' => $uri, 'bytes' => isset($_FILES['file']) ? filesize($_FILES['file']['tmp_name']) : 0];
|
|
} elseif ($chat) {
|
|
$raw = file_get_contents('php://input'); $object = json_decode($raw); $body = json_decode($raw, true);
|
|
$valid = $valid && is_object($object->inputs ?? null) && (array) $object->inputs === [] && is_string($body['query'] ?? null)
|
|
&& ($body['response_mode'] ?? '') === 'blocking' && ($body['auto_generate_name'] ?? null) === false
|
|
&& preg_match('/^fa-opaque-[a-f0-9]{48}$/D', $body['user'] ?? '')
|
|
&& array_diff(array_keys($body), ['inputs', 'query', 'response_mode', 'user', 'auto_generate_name']) === [];
|
|
$meta = ['stage' => 'extraction', 'valid' => (bool) $valid, 'uri' => $uri];
|
|
} else { http_response_code(404); echo '{}'; return; }
|
|
file_put_contents($directory . '/requests.jsonl', json_encode($meta) . "\n", FILE_APPEND);
|
|
header('Content-Type: application/json');
|
|
if (!$valid) { http_response_code(400); echo '{"code":"bad_contract"}'; return; }
|
|
if (str_contains($uri, '/slow/')) { sleep(12); }
|
|
if (str_contains($uri, '/redirect/')) { http_response_code(302); header('Location: /success/v1/audio-to-text'); echo '{}'; return; }
|
|
if (str_contains($uri, '/reject/')) { http_response_code(429); echo '{"message_id":"known-reject-id"}'; return; }
|
|
if (str_contains($uri, '/unknown/')) { http_response_code(500); echo '{"message_id":"unknown-observed-id"}'; return; }
|
|
if (str_contains($uri, '/oversize/')) { echo json_encode(['text' => str_repeat('x', 200000)]); return; }
|
|
if ($audio) { echo json_encode(['text' => '今天早晨空腹血糖六点一。'], JSON_UNESCAPED_UNICODE); return; }
|
|
preg_match('/c_[a-f0-9]{16,64}/', $body['query'], $match);
|
|
$answer = ['schema_version' => 'followup-audio-transcript-v2', 'summary' => '合成摘要', 'uncertainties' => [], 'items' => [[
|
|
'kind' => 'blood', 'values' => ['fasting_blood_sugar' => 6.1], 'record_date' => '2026-09-29', 'record_time' => null,
|
|
'date_text' => '今天', 'time_text' => '早晨', 'time_period' => '早晨', 'time_estimated' => true, 'needs_review' => true,
|
|
'evidence_ids' => [$match[0]],
|
|
]]];
|
|
echo json_encode(['event' => 'message', 'message_id' => 'dify-message-success', 'task_id' => 'dify-task-success', 'conversation_id' => 'dify-conversation-never-reused',
|
|
'answer' => json_encode($answer, JSON_UNESCAPED_UNICODE), 'metadata' => ['finish_reason' => str_contains($uri, '/length/') ? 'length' : 'stop']]);
|