This commit is contained in:
Your Name
2026-10-23 11:39:56 +08:00
parent 86476920cf
commit ff98014fa2
11 changed files with 766 additions and 20 deletions
@@ -969,7 +969,8 @@ class DiagnosisController extends BaseAdminController
$profile = (string) $metadata['model_key'];
$emit = $this->openAssistantSse();
$sseStartedAt = hrtime(true);
$started = $emit('start', $metadata + ['request_id' => $requestId, 'message' => '已连接,正在整理资料']);
$started = $emit('start', $metadata + ['request_id' => $requestId,
'trace_id' => $timing->id(), 'message' => '已连接,正在整理资料']);
$timing->log('sse_start', ['status' => $started ? 'ok' : 'disconnected']);
return true;
},
@@ -533,6 +533,13 @@ class DiagnosisAiLogic extends BaseLogic
$retryMs = 0;
$status = 'error';
$upstreamStartedAt = hrtime(true);
if ($timing !== null) {
$priorObserver = $options['on_upstream_timing'] ?? null;
$options['on_upstream_timing'] = static function (\app\common\service\DifyStreamTiming $stages) use ($timing, $priorObserver): void {
$timing->logUpstream($stages);
if (is_callable($priorObserver)) $priorObserver($stages);
};
}
$forwardDelta = static function (string $delta) use (&$deliveredDelta, &$deltaCount, &$deltaBytes, $onDelta) {
$accepted = $onDelta($delta);
if ($accepted !== false) {
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace app\adminapi\service;
use app\common\service\DifyStreamTiming;
use think\facade\Log;
/** A request-local, patient-free timing record for the diagnosis assistant SSE path. */
@@ -91,4 +92,15 @@ final class AssistantSseTiming
// Telemetry must never interrupt the clinical request.
}
}
/** The typed collector exposes only bounded numeric timing and enum metadata. */
public function logUpstream(DifyStreamTiming $upstream): void
{
try {
Log::write('diagnosis ai upstream stages ' . json_encode([
'trace_id' => $this->traceId, 'request_elapsed_ms' => self::elapsedMs($this->startedAt),
'timing' => $upstream->snapshot(),
]), 'info');
} catch (\Throwable $ignored) {}
}
}
+21 -1
View File
@@ -1350,6 +1350,8 @@ class DifyChatService
$buffer = '';
$state = self::newStreamState();
$upstreamTiming = is_callable($options['on_upstream_timing'] ?? null) ? new DifyStreamTiming() : null;
$state['timing'] = $upstreamTiming;
$state['on_task'] = $options['on_task'] ?? null;
$state['on_terminal'] = $options['on_terminal'] ?? null;
$notify = self::transferProgressNotifier($options);
@@ -1416,9 +1418,10 @@ class DifyChatService
$responseCode = 0;
$errorBody = '';
$errorBodyOverflow = false;
$header = static function ($handle, string $line) use (&$responseCode, &$errorBody, &$errorBodyOverflow, $markSubmitted): int {
$header = static function ($handle, string $line) use (&$responseCode, &$errorBody, &$errorBodyOverflow, $markSubmitted, $upstreamTiming): int {
if (preg_match('/^HTTP\/\S+\s+(\d{3})(?:\s|$)/i', trim($line), $matches) === 1) {
$responseCode = (int) $matches[1];
$upstreamTiming?->headers($responseCode);
$errorBody = '';
$errorBodyOverflow = false;
if ($responseCode >= 200 && $responseCode < 300) $markSubmitted();
@@ -1432,9 +1435,11 @@ class DifyChatService
&$responseCode,
&$errorBody,
&$errorBodyOverflow,
$upstreamTiming,
$forward,
$abort
): int {
$upstreamTiming?->body(strlen($chunk));
if ($responseCode < 200 || $responseCode >= 300) {
// Keep a bounded JSON error only until its safe code/reason has
// been extracted. Never feed HTTP error bodies to the SSE parser.
@@ -1499,6 +1504,15 @@ class DifyChatService
// A callback failure may escape libcurl before the regular cancel
// poll. Make the coordinated stop attempt before closing this handle.
try { $abort(); } catch (\Throwable $ignored) {}
if ($upstreamTiming !== null) {
try {
$upstreamTiming->transport(['connect_ms' => curl_getinfo($ch, CURLINFO_CONNECT_TIME) * 1000,
'tls_ms' => curl_getinfo($ch, CURLINFO_APPCONNECT_TIME) * 1000,
'header_ttfb_ms' => curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME) * 1000,
'transfer_ms' => curl_getinfo($ch, CURLINFO_TOTAL_TIME) * 1000,
'http_status' => $httpCode, 'curl_errno' => $errno]);
} catch (\Throwable $ignored) {}
}
curl_close($ch);
}
@@ -1522,6 +1536,9 @@ class DifyChatService
$state['upstream_reason'] = $metadata['upstream_reason'];
}
unset($errorBody);
if ($upstreamTiming !== null) {
try { ($options['on_upstream_timing'])($upstreamTiming); } catch (\Throwable $ignored) {}
}
return [
'errno' => $errno,
@@ -1686,6 +1703,7 @@ class DifyChatService
}
}
$event = strtolower((string) ($decoded['event'] ?? ''));
if (($state['timing'] ?? null) instanceof DifyStreamTiming) $state['timing']->frame($decoded, $protocol);
if ($event === 'workflow_finished') {
$rawStatus = $decoded['data']['status'] ?? '';
$status = is_string($rawStatus) ? strtolower($rawStatus) : '';
@@ -1720,6 +1738,7 @@ class DifyChatService
$state['message_id'] = (string) ($decoded['message_id'] ?? $state['message_id']);
} else {
if ($state['finished'] || $state['upstream_error']) return;
if (($state['timing'] ?? null) instanceof DifyStreamTiming) $state['timing']->frame($decoded, $protocol);
if (is_array($decoded['error'] ?? null)) {
$state['upstream_error'] = true;
$metadata = self::upstreamErrorMetadata($decoded);
@@ -1745,6 +1764,7 @@ class DifyChatService
return;
}
if ($state['discard_deltas']) return;
if (($state['timing'] ?? null) instanceof DifyStreamTiming) $state['timing']->delivered();
$state['content'] .= $delta;
$state['emitted'] = true;
}
@@ -42,6 +42,7 @@ final class DifyRequiredAttachmentUpload
array $options = []
): array {
$temporary = [];
$multi = null;
$skipped = [];
$index = 0;
$total = 0;
@@ -78,6 +79,11 @@ final class DifyRequiredAttachmentUpload
}
}
unset($item);
// Keep libcurl's connection cache only for this preparation. Each
// transfer still owns a fresh easy handle, so source downloads never
// inherit the Dify upload headers or request body.
$multi = curl_multi_init();
if ($multi === false) self::fail('CURL_UNAVAILABLE');
$prepared = [];
$uploadedIndices = [];
$cache = [];
@@ -99,7 +105,7 @@ final class DifyRequiredAttachmentUpload
if (isset($item['path'])) {
self::copyLocal($item['path'], $path, $maxBytes, $deadline, $shouldAbort);
} else {
$exists = self::download($item['url'], $item['parts'], $path, $maxBytes, $deadline, $shouldAbort, $options);
$exists = self::download($item['url'], $item['parts'], $path, $maxBytes, $deadline, $shouldAbort, $options, $multi);
}
self::check($deadline, $shouldAbort);
if (!$exists) {
@@ -113,7 +119,7 @@ final class DifyRequiredAttachmentUpload
'audio/wav' => 'wav', 'audio/mpeg' => 'mp3', 'text/plain' => 'txt'][$mime] ?? 'bin';
}
$cache[$cacheKey] = self::upload($endpoint, $apiKey, $user, $path, $mime,
'attachment-' . $index . '.' . $extension, $deadline, $shouldAbort, $options['ca_file']);
'attachment-' . $index . '.' . $extension, $deadline, $shouldAbort, $options['ca_file'], $multi);
}
}
if ($cache[$cacheKey] === null) {
@@ -155,6 +161,7 @@ final class DifyRequiredAttachmentUpload
'failed_index' => $index, 'attachment_count' => $total, 'skipped' => $skipped];
} finally {
foreach ($temporary as $path) if (is_file($path)) @unlink($path);
if ($multi !== null && $multi !== false) curl_multi_close($multi);
}
}
@@ -340,7 +347,7 @@ final class DifyRequiredAttachmentUpload
}
}
private static function download(string $url, array $parts, string $path, int $maxBytes, float $deadline, ?callable $shouldAbort, array $options): bool
private static function download(string $url, array $parts, string $path, int $maxBytes, float $deadline, ?callable $shouldAbort, array $options, $multi): bool
{
self::check($deadline, $shouldAbort);
$host = trim($parts['host'], '[]');
@@ -391,7 +398,7 @@ final class DifyRequiredAttachmentUpload
return $written;
};
if (!curl_setopt_array($ch, $curlOptions)) self::fail('REQUIRED_FILES_DOWNLOAD_FAILED');
$result = self::execute($ch, $deadline, $shouldAbort);
$result = self::execute($multi, $ch, $deadline, $shouldAbort);
if ($writeError !== '') self::fail($writeError);
if ($result['errno'] === CURLE_OPERATION_TIMEDOUT) self::fail('REQUIRED_FILES_TIMEOUT');
if ($result['errno'] !== CURLE_OK) self::fail('REQUIRED_FILES_DOWNLOAD_FAILED');
@@ -426,7 +433,7 @@ final class DifyRequiredAttachmentUpload
&& ($remaining === 0 || (ord($packed[$bytes]) & (255 << (8 - $remaining))) === (ord($base[$bytes]) & (255 << (8 - $remaining))));
}
private static function upload(string $endpoint, string $apiKey, string $user, string $path, string $mime, string $name, float $deadline, ?callable $shouldAbort, string $caFile): string
private static function upload(string $endpoint, string $apiKey, string $user, string $path, string $mime, string $name, float $deadline, ?callable $shouldAbort, string $caFile, $multi): string
{
self::check($deadline, $shouldAbort);
$ch = curl_init();
@@ -444,7 +451,7 @@ final class DifyRequiredAttachmentUpload
return strlen($chunk);
};
if (!curl_setopt_array($ch, $options)) self::fail('REQUIRED_FILES_UPLOAD_FAILED');
$result = self::execute($ch, $deadline, $shouldAbort);
$result = self::execute($multi, $ch, $deadline, $shouldAbort);
if ($result['errno'] === CURLE_OPERATION_TIMEDOUT) self::fail('REQUIRED_FILES_TIMEOUT');
if ($result['errno'] !== CURLE_OK || $tooLarge || !in_array($result['status'], [200, 201], true)) self::fail('REQUIRED_FILES_UPLOAD_FAILED');
$decoded = json_decode($body, true);
@@ -475,10 +482,9 @@ final class DifyRequiredAttachmentUpload
return $options;
}
/** curl_multi polling observes cancellation even while no headers/body arrive. */
private static function execute($ch, float $deadline, ?callable $shouldAbort): array
/** Request-local pool retains connections; polling also observes silent cancellation. */
private static function execute($multi, $ch, float $deadline, ?callable $shouldAbort): array
{
$multi = curl_multi_init();
$added = false;
try {
if (curl_multi_add_handle($multi, $ch) !== CURLM_OK) self::fail('REQUIRED_FILES_PREPARE_FAILED');
@@ -495,7 +501,6 @@ final class DifyRequiredAttachmentUpload
return ['errno' => $errno, 'status' => (int) curl_getinfo($ch, CURLINFO_HTTP_CODE)];
} finally {
if ($added) curl_multi_remove_handle($multi, $ch);
curl_multi_close($multi);
}
}
@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace app\common\service;
/** Request-local numeric telemetry. Raw events, prompts, titles, URLs and IDs never leave it. */
final class DifyStreamTiming
{
private const EVENTS = ['workflow_started', 'workflow_finished', 'node_started', 'node_finished',
'message', 'agent_message', 'agent_thought', 'message_end', 'error', 'ping', 'choices'];
private const TYPES = ['start', 'llm', 'knowledge-retrieval', 'tool', 'code', 'template-transform',
'if-else', 'answer', 'end', 'agent', 'http-request', 'iteration', 'question-classifier',
'variable-aggregator', 'parameter-extractor'];
private \Closure $clock;
private float $started;
private array $metrics = [];
private array $events = [];
private array $nodes = [];
private array $nodePositions = [];
public function __construct(?callable $clock = null)
{
$this->clock = $clock === null ? static fn (): float => hrtime(true) / 1e6 : \Closure::fromCallable($clock);
$this->started = ($this->clock)();
}
private function elapsed(): int { return max(0, (int) floor(($this->clock)() - $this->started)); }
public function headers(int $status): void
{
$this->metrics['first_header_ms'] ??= $this->elapsed();
if ($status >= 200 && $status <= 599) $this->metrics['final_header_ms'] ??= $this->elapsed();
}
public function body(int $bytes): void
{
if ($bytes <= 0) return;
$this->metrics['first_body_ms'] ??= $this->elapsed();
}
public function frame(array $decoded, string $protocol): void
{
$this->metrics['first_frame_ms'] ??= $this->elapsed();
$name = $protocol === 'dify' ? ($decoded['event'] ?? 'other') : (isset($decoded['choices']) ? 'choices' : 'other');
if (!is_string($name) || !in_array($name, self::EVENTS, true)) $name = 'other';
$now = $this->elapsed();
$this->events[$name] ??= ['first_ms' => $now, 'last_ms' => $now, 'count' => 0];
++$this->events[$name]['count']; $this->events[$name]['last_ms'] = $now;
if (in_array($name, ['message', 'agent_message', 'choices'], true)) {
$answer = $protocol === 'dify' ? ($decoded['answer'] ?? '') : ($decoded['choices'][0]['delta']['content'] ?? '');
$visible = is_string($answer) && $answer !== '';
// Chat Completions also supports text-part arrays. Match the parser's
// accepted shape without retaining or concatenating clinical content.
if ($protocol !== 'dify' && is_array($answer)) {
foreach ($answer as $part) {
if (is_array($part) && ($part['type'] ?? '') === 'text'
&& is_string($part['text'] ?? null) && $part['text'] !== '') { $visible = true; break; }
}
}
if ($visible) $this->metrics['first_answer_ms'] ??= $now;
}
if (in_array($name, ['node_started', 'node_finished'], true)) {
$data = is_array($decoded['data'] ?? null) ? $decoded['data'] : [];
$id = $data['id'] ?? $data['node_id'] ?? '';
if (is_string($id) && $id !== '' && strlen($id) <= 512) {
$key = hash('sha256', $id);
if (!isset($this->nodePositions[$key]) && count($this->nodes) >= 128) return;
if (!isset($this->nodePositions[$key])) {
$this->nodePositions[$key] = count($this->nodes);
$type = $data['node_type'] ?? 'other';
$this->nodes[] = ['type' => is_string($type) && in_array($type, self::TYPES, true) ? $type : 'other'];
}
$position = $this->nodePositions[$key];
$field = $name === 'node_started' ? 'start_ms' : 'finish_ms';
$this->nodes[$position][$field] ??= $now;
$elapsed = $data['elapsed_time'] ?? null;
if (is_numeric($elapsed) && is_finite((float) $elapsed) && (float) $elapsed >= 0) {
$this->nodes[$position]['provider_elapsed_ms'] = (int) min(3600000, (float) $elapsed * 1000);
}
}
}
$usage = $name === 'message_end' ? ($decoded['metadata']['usage'] ?? []) : ($decoded['usage'] ?? []);
foreach (['prompt_tokens', 'completion_tokens', 'total_tokens', 'reasoning_tokens'] as $key) {
$value = is_array($usage) ? ($usage[$key] ?? null) : null;
if (is_numeric($value) && is_finite((float) $value) && (float) $value >= 0) {
$this->metrics[$key] = (int) min(1000000000, (float) $value);
}
}
}
public function delivered(): void { $this->metrics['first_delivered_ms'] ??= $this->elapsed(); }
public function transport(array $values): void
{
foreach (['connect_ms', 'tls_ms', 'header_ttfb_ms', 'transfer_ms', 'http_status', 'curl_errno'] as $key) {
$value = $values[$key] ?? null;
if ((is_float($value) || is_int($value)) && is_finite((float) $value) && $value >= 0) {
$this->metrics[$key] = (int) min(3600000, $value);
}
}
}
public function snapshot(): array
{
return ['duration_ms' => $this->elapsed()] + $this->metrics + ['events' => $this->events, 'nodes' => $this->nodes];
}
}