更新
This commit is contained in:
@@ -103,8 +103,17 @@ class DifyChatService
|
||||
return self::error('SKILL_UNAVAILABLE', '临床 AI 参考资料不可用');
|
||||
}
|
||||
|
||||
$strictFiles = !empty($options['strict_files']);
|
||||
$normalized = self::normalizeFiles($files, self::maxFiles($config, $modelConfig), !$strictFiles);
|
||||
if ($shouldAbort !== null && $shouldAbort()) return self::error('AI_ASSISTANT_CANCELLED', '本次请求已取消');
|
||||
$requiredFiles = !empty($options['required_files']);
|
||||
if ($requiredFiles) {
|
||||
$checked = self::requiredFiles($files);
|
||||
if (isset($checked['error_code'])) return self::requiredFilesFailure($options, $checked['error_code'], $checked['error']);
|
||||
$files = $checked['files'];
|
||||
}
|
||||
$strictFiles = !empty($options['strict_files']) || $requiredFiles;
|
||||
// A clinical request declares every file mandatory. Local legacy defaults are
|
||||
// not provider capabilities: submit them all, and surface any real limit.
|
||||
$normalized = self::normalizeFiles($files, $requiredFiles ? count($files) : self::maxFiles($config, $modelConfig), !$strictFiles);
|
||||
if ($strictFiles && !self::strictFilesComplete($files, $normalized)) {
|
||||
return self::error('STRICT_FILES_INVALID_OR_LIMIT', '附件无效或超出单批上限,须分批处理');
|
||||
}
|
||||
@@ -140,6 +149,7 @@ class DifyChatService
|
||||
// The legacy compatible path cannot transmit documents. Strict callers must
|
||||
// receive an explicit gap, never a successful text-only fallback.
|
||||
if ($strictFiles && !self::strictProtocolSupportsFiles($requestSpec['protocol'], $attempt['files'])) {
|
||||
if ($requiredFiles) return self::requiredFilesFailure($options, 'REQUIRED_FILES_UNSUPPORTED', '必需资料提交失败:当前模型接口不支持原始文档等附件');
|
||||
return self::error('FILE_TYPE_UNSUPPORTED', '当前模型接口不支持此类原始附件');
|
||||
}
|
||||
$elapsedSeconds = (int) floor(microtime(true) - $startedAt);
|
||||
@@ -193,6 +203,7 @@ class DifyChatService
|
||||
$formatted['transmitted_file_count'] = count($attempt['files']);
|
||||
$formatted['attachment_transport'] = (string) ($lastSpec['protocol'] ?? '');
|
||||
}
|
||||
if ($requiredFiles) $formatted += self::finalTransferManifest($options, $attempt['files'], (string) ($lastSpec['protocol'] ?? ''));
|
||||
return $formatted;
|
||||
}
|
||||
// Dify 只接受应用中已声明且满足长度约束的 inputs。病例正文已经完整
|
||||
@@ -200,6 +211,7 @@ class DifyChatService
|
||||
if ($inputIndex === 0 && $inputRejected) {
|
||||
continue;
|
||||
}
|
||||
if ($requiredFiles && $fileRejected) return self::requiredFilesFailure($options, 'REQUIRED_FILES_REJECTED', '必需资料提交失败:模型拒绝本次附件,未降级为纯文字请求');
|
||||
break;
|
||||
}
|
||||
// 附件整体被拒时退回纯文本重试,附件清单已在下一轮尝试中补齐。
|
||||
@@ -272,8 +284,15 @@ class DifyChatService
|
||||
return self::error('SKILL_UNAVAILABLE', '临床 AI 参考资料不可用');
|
||||
}
|
||||
|
||||
$strictFiles = !empty($options['strict_files']);
|
||||
$normalized = self::normalizeFiles($files, self::maxFiles($config, $modelConfig), !$strictFiles);
|
||||
if ($shouldAbort !== null && $shouldAbort()) return self::error('AI_ASSISTANT_CANCELLED', '本次请求已取消');
|
||||
$requiredFiles = !empty($options['required_files']);
|
||||
if ($requiredFiles) {
|
||||
$checked = self::requiredFiles($files);
|
||||
if (isset($checked['error_code'])) return self::requiredFilesFailure($options, $checked['error_code'], $checked['error']);
|
||||
$files = $checked['files'];
|
||||
}
|
||||
$strictFiles = !empty($options['strict_files']) || $requiredFiles;
|
||||
$normalized = self::normalizeFiles($files, $requiredFiles ? count($files) : self::maxFiles($config, $modelConfig), !$strictFiles);
|
||||
if ($strictFiles && !self::strictFilesComplete($files, $normalized)) {
|
||||
return self::error('STRICT_FILES_INVALID_OR_LIMIT', '附件无效或超出单批上限,须分批处理');
|
||||
}
|
||||
@@ -309,6 +328,7 @@ class DifyChatService
|
||||
return self::error('AI_ASSISTANT_CANCELLED', '本次请求已取消');
|
||||
}
|
||||
if ($strictFiles && !self::strictProtocolSupportsFiles($requestSpec['protocol'], $attempt['files'])) {
|
||||
if ($requiredFiles) return self::requiredFilesFailure($options, 'REQUIRED_FILES_UNSUPPORTED', '必需资料提交失败:当前模型接口不支持原始文档等附件');
|
||||
return self::error('FILE_TYPE_UNSUPPORTED', '当前模型接口不支持此类原始附件');
|
||||
}
|
||||
$elapsedSeconds = (int) floor(microtime(true) - $startedAt);
|
||||
@@ -367,6 +387,7 @@ class DifyChatService
|
||||
$formatted['transmitted_file_count'] = count($attempt['files']);
|
||||
$formatted['attachment_transport'] = (string) ($lastSpec['protocol'] ?? '');
|
||||
}
|
||||
if ($requiredFiles) $formatted += self::finalTransferManifest($options, $attempt['files'], (string) ($lastSpec['protocol'] ?? ''));
|
||||
return $formatted;
|
||||
}
|
||||
if (
|
||||
@@ -376,6 +397,7 @@ class DifyChatService
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if ($requiredFiles && $fileRejected) return self::requiredFilesFailure($options, 'REQUIRED_FILES_REJECTED', '必需资料提交失败:模型拒绝本次附件,未降级为纯文字请求');
|
||||
break;
|
||||
}
|
||||
// 已经推给医生的文本不能重复输出,因此只在一个字都没发出去时才降级重试。
|
||||
@@ -403,6 +425,74 @@ class DifyChatService
|
||||
&& ($body['result'] ?? '') === 'success';
|
||||
}
|
||||
|
||||
/** Mandatory clinical files have no lossy normalization or text-only fallback. */
|
||||
private static function requiredFiles(array $files): array
|
||||
{
|
||||
$required = [];
|
||||
foreach ($files as $file) {
|
||||
if (!is_array($file) || !is_string($file['type'] ?? null) || !is_string($file['url'] ?? null)
|
||||
|| (isset($file['mime_type']) && !is_string($file['mime_type']))
|
||||
|| (isset($file['mime']) && !is_string($file['mime']))) {
|
||||
return ['error_code' => 'REQUIRED_FILES_INVALID', 'error' => '必需资料提交失败:附件资料无效'];
|
||||
}
|
||||
$type = strtolower(trim((string) ($file['type'] ?? '')));
|
||||
$url = trim((string) ($file['url'] ?? ''));
|
||||
$extension = strtolower(pathinfo((string) (parse_url($url, PHP_URL_PATH) ?? ''), PATHINFO_EXTENSION));
|
||||
$mime = strtolower(trim((string) ($file['mime_type'] ?? $file['mime'] ?? '')));
|
||||
// Recorded consultations are represented exclusively by their existing
|
||||
// transcript. Never send a video, even when its declared type is wrong.
|
||||
if ($type === 'video' || str_starts_with($mime, 'video/')
|
||||
|| in_array($extension, ['mp4', 'mov', 'avi', 'mkv', 'webm', 'm3u8', 'm4v', 'wmv', 'flv', 'mpeg', 'mpg', '3gp'], true)) continue;
|
||||
if (!in_array($type, ['image', 'document', 'audio'], true)
|
||||
|| !self::isValidRemoteFileUrl($url)
|
||||
|| !in_array($file['transfer_method'] ?? 'remote_url', ['remote_url'], true)) {
|
||||
return ['error_code' => 'REQUIRED_FILES_INVALID', 'error' => '必需资料提交失败:存在未知类型或无效附件,未生成不完整报告'];
|
||||
}
|
||||
$required[] = ['type' => $type, 'transfer_method' => 'remote_url', 'url' => $url];
|
||||
}
|
||||
return ['files' => $required];
|
||||
}
|
||||
|
||||
private static function requiredSections(array $options, string $status): array
|
||||
{
|
||||
$sections = $options['required_sections'] ?? [];
|
||||
if (!is_array($sections) || $sections === []) $sections = $options['source_summary']['required_sections'] ?? [];
|
||||
if (!is_array($sections)) return [];
|
||||
$result = [];
|
||||
foreach ($sections as $section) {
|
||||
if (!is_array($section) || !is_string($section['key'] ?? null)) continue;
|
||||
$result[] = ['key' => $section['key'], 'label' => (string) ($section['label'] ?? ''),
|
||||
'count' => max(0, (int) ($section['count'] ?? 0)),
|
||||
'availability' => ($section['availability'] ?? '') === 'empty' ? 'empty' : 'present', 'status' => $status];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function requiredFilesFailure(array $options, string $code, string $message): array
|
||||
{
|
||||
$sections = self::requiredSections($options, 'failed');
|
||||
$summary = is_array($options['source_summary'] ?? null) ? $options['source_summary'] : [];
|
||||
$summary['required_sections'] = $sections;
|
||||
if (is_callable($options['on_progress'] ?? null)) ($options['on_progress'])([
|
||||
'phase' => 'attachments', 'status' => 'failed', 'message' => $message,
|
||||
'required_sections' => $sections, 'source_summary' => $summary,
|
||||
]);
|
||||
return self::error($code, $message);
|
||||
}
|
||||
|
||||
private static function finalTransferManifest(array $options, array $files, string $protocol): array
|
||||
{
|
||||
$sections = self::requiredSections($options, 'submitted');
|
||||
$summary = is_array($options['source_summary'] ?? null) ? $options['source_summary'] : [];
|
||||
$summary['required_sections'] = $sections;
|
||||
$items = self::attachmentItems($files, [], $protocol);
|
||||
foreach ($items as &$item) $item['status'] = 'submitted';
|
||||
unset($item);
|
||||
return ['required_sections' => $sections, 'source_summary' => $summary,
|
||||
'items' => $items, 'attachment_count' => count($files),
|
||||
'attachment_transfer' => 'remote_url_references', 'transfer_manifest_final' => true];
|
||||
}
|
||||
|
||||
/** Safe labels only; submitted means request references sent, never remote file reading. */
|
||||
private static function attachmentItems(array $files, array $omitted, string $protocol): array
|
||||
{
|
||||
@@ -861,21 +951,24 @@ class DifyChatService
|
||||
$curlOptions[CURLOPT_TIMEOUT_MS] = $milliseconds;
|
||||
$curlOptions[CURLOPT_CONNECTTIMEOUT_MS] = min(1000, $milliseconds);
|
||||
}
|
||||
if ($shouldAbort !== null) {
|
||||
$observeTransfer = $shouldAbort !== null || is_callable($options['on_progress'] ?? null);
|
||||
if ($observeTransfer) {
|
||||
// Dify blocking mode exposes no streaming task id. Compatibility mode
|
||||
// can close its transfer promptly, but cannot claim remote stop acceptance.
|
||||
$notify('submitting');
|
||||
$progressOption = self::curlProgressOption();
|
||||
if ($progressOption === null) {
|
||||
if ($progressOption === null && $shouldAbort !== null) {
|
||||
curl_close($ch);
|
||||
return ['body' => '', 'errno' => -3, 'http_code' => 0, 'client_aborted' => true];
|
||||
}
|
||||
$curlOptions[CURLOPT_NOPROGRESS] = false;
|
||||
$curlOptions[$progressOption] = static function (...$values) use ($shouldAbort, &$clientAborted, $markSubmitted): int {
|
||||
if (($values[3] ?? 0) > 0 && ($values[4] ?? 0) >= $values[3]) $markSubmitted();
|
||||
if ($shouldAbort()) { $clientAborted = true; return 1; }
|
||||
return 0;
|
||||
};
|
||||
if ($progressOption !== null) {
|
||||
$curlOptions[CURLOPT_NOPROGRESS] = false;
|
||||
$curlOptions[$progressOption] = static function (...$values) use ($shouldAbort, &$clientAborted, $markSubmitted): int {
|
||||
if (($values[3] ?? 0) > 0 && ($values[4] ?? 0) >= $values[3]) $markSubmitted();
|
||||
if ($shouldAbort !== null && $shouldAbort()) { $clientAborted = true; return 1; }
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
$curlOptions[CURLOPT_HEADERFUNCTION] = static function ($handle, string $line) use ($markSubmitted): int {
|
||||
if (preg_match('/^HTTP\/\S+\s+2\d{2}(?:\s|$)/i', trim($line))) $markSubmitted();
|
||||
return strlen($line);
|
||||
@@ -896,7 +989,7 @@ class DifyChatService
|
||||
} finally {
|
||||
curl_close($ch);
|
||||
}
|
||||
if ($shouldAbort !== null) {
|
||||
if ($observeTransfer) {
|
||||
if ($clientAborted && is_callable($options['on_stop'] ?? null)) ($options['on_stop'])(false);
|
||||
if (is_callable($options['on_transfer_end'] ?? null)) ($options['on_transfer_end'])();
|
||||
if (!$clientAborted && ($errno !== 0 || $httpCode >= 400)) $notify('failed');
|
||||
@@ -919,10 +1012,14 @@ class DifyChatService
|
||||
if ($item['status'] !== 'omitted') $item['status'] = $status;
|
||||
}
|
||||
unset($item);
|
||||
$sections = self::requiredSections($options, $status);
|
||||
$summary = is_array($options['source_summary'] ?? null) ? $options['source_summary'] : [];
|
||||
if ($sections !== []) $summary['required_sections'] = $sections;
|
||||
($options['on_progress'])(['phase' => 'attachments', 'status' => $status,
|
||||
'message' => $status === 'submitted' ? '资料正文与附件引用已提交,等待模型;远程附件读取进度不可观测'
|
||||
: ($status === 'failed' ? '本次资料请求提交失败' : '正在提交资料正文与附件引用'),
|
||||
'source_summary' => $options['source_summary'] ?? [],
|
||||
'source_summary' => $summary,
|
||||
'required_sections' => $sections,
|
||||
'source_form' => $options['source_form'] ?? 'full_context',
|
||||
'evidence_cache_hit' => !empty($options['evidence_cache_hit']),
|
||||
'attachment_transfer' => 'remote_url_references',
|
||||
|
||||
Reference in New Issue
Block a user