This commit is contained in:
gr
2026-10-23 09:27:45 +08:00
34 changed files with 2866 additions and 225 deletions
@@ -550,8 +550,24 @@ class PatientAiReportLogic extends BaseLogic
*
* @return array{text:string,chunk_count:int,reduction_rounds:int,source_bytes:int,compacted:bool}
*/
public static function compactSourceForPrompt(string $modelKey, string $sourceJson): array
public static function compactSourceForPrompt(string $modelKey, string $sourceJson, array $options = []): array
{
$abort = $options['should_abort'] ?? null;
$check = static function () use ($abort): void {
if (is_callable($abort) && $abort()) throw new \RuntimeException('Context operation cancelled');
};
$progress = static function (string $message, int $completed, int $total) use ($options): void {
if (is_callable($options['on_progress'] ?? null)) ($options['on_progress'])([
'phase' => 'preparing', 'status' => 'compacting', 'message' => $message,
'completed' => $completed, 'total' => $total,
]);
};
$transportOptions = array_intersect_key($options, array_flip([
'should_abort', 'on_task', 'on_stop', 'on_transfer_end', 'on_cancel_task', 'on_terminal',
]));
$transportOptions['knowledge_mode'] = 'evidence';
$upstreamUser = (string) ($options['upstream_user'] ?? 'patient-longitudinal-context');
$check();
$sourceBytes = strlen($sourceJson);
if ($sourceBytes <= self::MAX_PROMPT_CHUNK_BYTES) {
return [
@@ -567,6 +583,8 @@ class PatientAiReportLogic extends BaseLogic
$chunkCount = count($chunks);
$summaries = [];
foreach ($chunks as $index => $chunk) {
$check();
$progress('正在向模型提交资料分片并整理证据', $index, $chunkCount);
$part = DifyChatService::chat(
$modelKey,
[
@@ -576,8 +594,9 @@ class PatientAiReportLogic extends BaseLogic
'prompt_version' => self::PROMPT_VERSION,
],
self::buildChunkPrompt($chunk, $index + 1, $chunkCount),
'patient-longitudinal-context', [], ['knowledge_mode' => 'evidence']
$upstreamUser, [], $transportOptions
);
$check();
if (empty($part['ok']) || trim((string) ($part['content'] ?? '')) === '') {
throw new \RuntimeException('Patient context chunk analysis failed');
}
@@ -586,6 +605,7 @@ class PatientAiReportLogic extends BaseLogic
'total' => $chunkCount,
'summary' => self::cleanSourceText($part['content'], true),
];
$progress('资料分片证据已返回', $index + 1, $chunkCount);
}
$reductionRounds = 0;
@@ -598,6 +618,8 @@ class PatientAiReportLogic extends BaseLogic
$summaryChunks = self::splitUtf8ByBytes($summaryJson, self::MAX_PROMPT_CHUNK_BYTES);
$reduced = [];
foreach ($summaryChunks as $index => $chunk) {
$check();
$progress('正在归并第' . $reductionRounds . '轮资料证据', $index, count($summaryChunks));
$part = DifyChatService::chat(
$modelKey,
[
@@ -608,8 +630,9 @@ class PatientAiReportLogic extends BaseLogic
'prompt_version' => self::PROMPT_VERSION,
],
self::buildReductionPrompt($chunk, $index + 1, count($summaryChunks)),
'patient-longitudinal-context', [], ['knowledge_mode' => 'evidence']
$upstreamUser, [], $transportOptions
);
$check();
if (empty($part['ok']) || trim((string) ($part['content'] ?? '')) === '') {
throw new \RuntimeException('Patient context reduction failed');
}
@@ -618,6 +641,7 @@ class PatientAiReportLogic extends BaseLogic
'total' => count($summaryChunks),
'summary' => self::cleanSourceText($part['content'], true),
];
$progress('资料证据归并已返回', $index + 1, count($summaryChunks));
}
$nextJson = self::encodeJson($reduced, true);
if (strlen($nextJson) >= strlen($summaryJson) && count($reduced) >= count($summaries)) {
@@ -627,6 +651,7 @@ class PatientAiReportLogic extends BaseLogic
$summaryJson = $nextJson;
}
$check();
return [
'text' => '患者纵向完整资料(服务端已逐片读取全部来源后归并的证据摘要,覆盖 '
. $chunkCount . ' 个来源片段):' . $summaryJson,