更新
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\adminapi\service;
|
||||
|
||||
use app\common\service\NihaixiaClinicalSkill;
|
||||
use app\common\service\TcmAncientBooksReference;
|
||||
use think\facade\Cache;
|
||||
|
||||
/** Short-lived evidence only. The caller must re-authorize and aggregate before lookup. */
|
||||
final class AiContextCompactionCache
|
||||
{
|
||||
public static function remember(string $scope, string $profile, string $source, callable $produce): array
|
||||
{
|
||||
$config = config('prescription_ai') ?: [];
|
||||
$ttl = max(0, min(600, (int) ($config['context_compaction_cache_ttl'] ?? 300)));
|
||||
$key = self::key($scope, $profile, $source, $config);
|
||||
if ($scope !== '' && $ttl > 0) {
|
||||
try {
|
||||
$cached = Cache::get($key);
|
||||
if (self::valid($cached, strlen($source))) {
|
||||
$cached['cache_hit'] = true;
|
||||
return $cached;
|
||||
}
|
||||
} catch (\Throwable $ignored) {
|
||||
// An unavailable cache must not prevent a complete fresh analysis.
|
||||
}
|
||||
}
|
||||
$result = $produce();
|
||||
if ($scope !== '' && $ttl > 0 && self::valid($result, strlen($source))) {
|
||||
try {
|
||||
Cache::set($key, $result, $ttl);
|
||||
} catch (\Throwable $ignored) {
|
||||
}
|
||||
}
|
||||
return $result + ['cache_hit' => false];
|
||||
}
|
||||
|
||||
private static function key(string $scope, string $profile, string $source, array $config): string
|
||||
{
|
||||
return 'ai-context-evidence:' . hash('sha256', serialize([
|
||||
'evidence-extraction-v2', $scope, $profile, hash('sha256', $source),
|
||||
$config['base_url'] ?? '', $config['models'][$profile] ?? [],
|
||||
NihaixiaClinicalSkill::VERSION, TcmAncientBooksReference::COMMIT,
|
||||
]));
|
||||
}
|
||||
|
||||
private static function valid($value, int $sourceBytes): bool
|
||||
{
|
||||
return is_array($value) && is_string($value['text'] ?? null)
|
||||
&& trim($value['text']) !== '' && strlen($value['text']) <= 240000
|
||||
&& ($value['source_bytes'] ?? null) === $sourceBytes
|
||||
&& is_int($value['chunk_count'] ?? null) && $value['chunk_count'] > 0
|
||||
&& is_int($value['reduction_rounds'] ?? null) && $value['reduction_rounds'] >= 0
|
||||
&& ($value['compacted'] ?? null) === true;
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,10 @@ final class AssistantSseTiming
|
||||
private const NUMBER_FIELDS = [
|
||||
'duration_ms', 'aggregate_ms', 'compaction_ms', 'retry_ms',
|
||||
'source_bytes', 'query_bytes', 'attachment_count',
|
||||
'delta_bytes', 'delta_count', 'first_delta_ms',
|
||||
'delta_bytes', 'delta_count', 'first_delta_ms', 'inputs_bytes',
|
||||
];
|
||||
|
||||
private const BOOLEAN_FIELDS = ['compaction_attempted', 'retry_attempted', 'first_delta_observed'];
|
||||
private const BOOLEAN_FIELDS = ['compaction_attempted', 'retry_attempted', 'first_delta_observed', 'compaction_cache_hit'];
|
||||
|
||||
private const ERROR_CODES = [
|
||||
'AI_ASSISTANT_FAILED', 'UPSTREAM_UNAVAILABLE', 'UPSTREAM_REJECTED',
|
||||
@@ -29,10 +29,12 @@ final class AssistantSseTiming
|
||||
];
|
||||
|
||||
private string $traceId;
|
||||
private int $startedAt;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->traceId = bin2hex(random_bytes(16));
|
||||
$this->startedAt = hrtime(true);
|
||||
}
|
||||
|
||||
public function id(): string
|
||||
@@ -57,6 +59,8 @@ final class AssistantSseTiming
|
||||
$event = [
|
||||
'trace_id' => $this->traceId,
|
||||
'phase' => in_array($phase, self::PHASES, true) ? $phase : 'upstream_done',
|
||||
// Includes authorization, aggregation and compaction before SSE starts.
|
||||
'request_elapsed_ms' => self::elapsedMs($this->startedAt),
|
||||
];
|
||||
foreach (self::NUMBER_FIELDS as $field) {
|
||||
if (isset($metrics[$field]) && is_int($metrics[$field])) {
|
||||
@@ -81,7 +85,8 @@ final class AssistantSseTiming
|
||||
public function log(string $phase, array $metrics = []): void
|
||||
{
|
||||
try {
|
||||
Log::info('diagnosis ai assistant timing ' . json_encode($this->event($phase, $metrics)));
|
||||
// This endpoint exits after SSE and bypasses normal HTTP-end log flushing.
|
||||
Log::write('diagnosis ai assistant timing ' . json_encode($this->event($phase, $metrics)), 'info');
|
||||
} catch (\Throwable $ignored) {
|
||||
// Telemetry must never interrupt the clinical request.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user