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]; } }