z
File size: 11,856 Bytes
712a3c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Request log persistence and OpenAI stream logging helpers."""

from __future__ import annotations

import json
import time
from typing import Any, AsyncGenerator, Dict, Optional, Tuple

from app.services.request_log_dao import get_request_log_dao
from app.utils.format import format_compact_number
from app.utils.logger import logger, log_exception
from app.utils.request_source import RequestSourceInfo
from app.utils.token_pool import get_token_pool
from app.utils.utlis import mask_token

CACHE_CREATION_FLOOR = 1024
CACHE_STRIDE = 128


def _empty_usage() -> Dict[str, int]:
    return {
        "input_tokens": 0,
        "output_tokens": 0,
        "cache_creation_tokens": 0,
        "cache_read_tokens": 0,
        "total_tokens": 0,
    }


def _coerce_int_or_none(value: Any) -> Optional[int]:
    try:
        if value is None:
            return None
        return int(value)
    except (TypeError, ValueError):
        return None


def _coerce_int(value: Any, default: int = 0) -> int:
    coerced = _coerce_int_or_none(value)
    return default if coerced is None else coerced


def _get_first_present(

    mapping: Dict[str, Any],

    *keys: str,

) -> Tuple[Optional[Any], Optional[str]]:
    for key in keys:
        if key in mapping and mapping[key] is not None:
            return mapping[key], key
    return None, None


def _estimate_cache_creation_tokens(

    input_tokens: int,

    cache_read_tokens: int,

) -> int:
    if input_tokens < CACHE_CREATION_FLOOR:
        return 0
    cacheable = (input_tokens // CACHE_STRIDE) * CACHE_STRIDE
    return max(0, cacheable - cache_read_tokens)


def _merge_openai_usage(

    current: Dict[str, int],

    update: Dict[str, int],

) -> Dict[str, int]:
    merged = dict(current)

    for key in (
        "input_tokens",
        "output_tokens",
        "cache_creation_tokens",
        "cache_read_tokens",
    ):
        value = _coerce_int(update.get(key))
        if value > 0:
            merged[key] = value

    total_tokens = _coerce_int(update.get("total_tokens"))
    if total_tokens > 0:
        merged["total_tokens"] = total_tokens
    else:
        merged["total_tokens"] = merged["input_tokens"] + merged["output_tokens"]

    return merged


def extract_openai_usage(response: Dict[str, Any]) -> Dict[str, int]:
    """Extract OpenAI-compatible usage, with cache fallback estimation."""
    usage = response.get("usage") or {}
    prompt_details = usage.get("prompt_tokens_details") or {}
    input_details = usage.get("input_token_details") or {}

    raw, _ = _get_first_present(usage, "prompt_tokens", "input_tokens")
    input_tokens = _coerce_int(raw)

    raw, _ = _get_first_present(usage, "completion_tokens", "output_tokens")
    output_tokens = _coerce_int(raw)

    raw, _ = _get_first_present(usage, "total_tokens")
    total_tokens = _coerce_int(raw)
    if total_tokens <= 0:
        total_tokens = input_tokens + output_tokens

    raw, source = _get_first_present(
        usage,
        "cache_read_input_tokens",
        "cache_read_tokens",
        "cached_tokens",
    )
    if not source:
        raw, source = _get_first_present(
            prompt_details,
            "cached_tokens",
            "cache_read_tokens",
            "cache_read_input_tokens",
        )
    if not source:
        raw, source = _get_first_present(
            input_details,
            "cached_tokens",
            "cache_read_input_tokens",
            "cache_read_tokens",
        )
    cache_read_tokens = max(0, _coerce_int(raw))

    raw, source = _get_first_present(
        usage,
        "cache_creation_input_tokens",
        "cache_creation_tokens",
    )
    if not source:
        raw, source = _get_first_present(
            prompt_details,
            "cache_creation_tokens",
            "cache_creation_input_tokens",
        )
    if not source:
        raw, source = _get_first_present(
            input_details,
            "cache_creation_input_tokens",
            "cache_creation_tokens",
        )
    reported_cache_creation = _coerce_int_or_none(raw)

    if reported_cache_creation is not None:
        cache_creation_tokens = max(0, reported_cache_creation)
    else:
        cache_creation_tokens = _estimate_cache_creation_tokens(
            input_tokens,
            cache_read_tokens,
        )

    return {
        "input_tokens": input_tokens,
        "output_tokens": output_tokens,
        "cache_creation_tokens": cache_creation_tokens,
        "cache_read_tokens": cache_read_tokens,
        "total_tokens": total_tokens,
    }


async def write_request_log(

    *,

    provider: str,

    model: str,

    source_info: RequestSourceInfo,

    auth_token: Optional[str] = None,

    upstream_auth_token: Optional[str] = None,

    success: bool,

    started_at: float,

    status_code: int = 200,

    first_token_time: float = 0.0,

    input_tokens: int = 0,

    output_tokens: int = 0,

    cache_creation_tokens: int = 0,

    cache_read_tokens: int = 0,

    total_tokens: Optional[int] = None,

    error_message: Optional[str] = None,

) -> None:
    """Persist a request log entry without breaking request handling."""
    duration = max(0.0, time.perf_counter() - started_at)

    status_tag = "OK" if success else "ERR"
    masked_auth_token = mask_token(auth_token)
    masked_upstream_auth_token = mask_token(upstream_auth_token)
    auth_segment = f" | Auth: {masked_auth_token}" if masked_auth_token else ""
    upstream_auth_segment = (
        f" | Upstream Auth: {masked_upstream_auth_token}"
        if masked_upstream_auth_token
        else ""
    )
    logger.info(
        "[%s] [%s] %s%s%s | In: %s | Out: %s | %.2fs",
        status_tag,
        provider,
        model,
        auth_segment,
        upstream_auth_segment,
        format_compact_number(input_tokens),
        format_compact_number(output_tokens),
        duration,
    )

    try:
        dao = get_request_log_dao()
        dao.add_log_nowait(
            provider=provider,
            endpoint=source_info.endpoint,
            source=source_info.source,
            protocol=source_info.protocol,
            client_name=source_info.client_name,
            auth_token=auth_token,
            upstream_auth_token=upstream_auth_token,
            model=model,
            status_code=status_code,
            success=success,
            duration=duration,
            first_token_time=first_token_time,
            input_tokens=input_tokens,
            output_tokens=output_tokens,
            cache_creation_tokens=cache_creation_tokens,
            cache_read_tokens=cache_read_tokens,
            total_tokens=total_tokens,
            error_message=error_message,
        )
    except Exception as exc:
        log_exception(logger, "Failed to write request log")


def _openai_response_has_output(response: Dict[str, Any]) -> bool:
    """Check whether a complete (non-stream) OpenAI response has visible output."""
    choices = response.get("choices") if isinstance(response, dict) else None
    if not choices:
        return False
    message = choices[0].get("message") or {}
    if message.get("content") or message.get("reasoning_content"):
        return True
    if message.get("tool_calls"):
        return True
    return False


def _sse_chunk_has_output(chunk_text: str) -> bool:
    """Check whether a formatted SSE chunk string has visible output content."""
    for line in chunk_text.splitlines():
        if not line.startswith("data: "):
            continue
        payload_text = line[6:].strip()
        if not payload_text or payload_text == "[DONE]":
            continue
        try:
            payload = json.loads(payload_text)
        except json.JSONDecodeError:
            continue
        if isinstance(payload, dict):
            return _openai_payload_has_output(payload)
    return False


def _openai_payload_has_output(payload: Dict[str, Any]) -> bool:
    """Treat only visible content/tool deltas as first-token output."""
    choice = ((payload.get("choices") or [{}])[0]) if isinstance(payload, dict) else {}
    delta = choice.get("delta") or {}

    if delta.get("content") or delta.get("reasoning_content"):
        return True
    if delta.get("tool_calls"):
        return True
    return False


def _iter_sse_payloads(chunk: str):
    for line in chunk.splitlines():
        if not line.startswith("data: "):
            continue
        payload_text = line[6:].strip()
        if payload_text:
            yield payload_text


async def wrap_openai_stream_with_logging(

    stream: AsyncGenerator[str, None],

    *,

    provider: str,

    model: str,

    source_info: RequestSourceInfo,

    auth_token: Optional[str] = None,

    upstream_auth_token: Optional[str] = None,

    started_at: float,

) -> AsyncGenerator[str, None]:
    """Wrap an OpenAI-compatible SSE stream and persist completion metadata."""
    success = True
    status_code = 200
    error_message: Optional[str] = None
    first_token_time = 0.0
    has_output = False
    usage = _empty_usage()

    try:
        async for chunk in stream:
            for payload_text in _iter_sse_payloads(chunk):
                if payload_text == "[DONE]":
                    continue

                try:
                    payload = json.loads(payload_text)
                except json.JSONDecodeError:
                    payload = None

                if not isinstance(payload, dict):
                    continue

                if "error" in payload:
                    success = False
                    error = payload.get("error") or {}
                    error_message = error.get("message") or "Unknown stream error"
                    status_code = _coerce_int(error.get("code"), 500)
                    continue

                if _openai_payload_has_output(payload):
                    has_output = True
                    if not first_token_time:
                        first_token_time = max(0.0, time.perf_counter() - started_at)

                if payload.get("usage"):
                    usage = _merge_openai_usage(
                        usage,
                        extract_openai_usage(payload),
                    )

            yield chunk
    except Exception as exc:
        success = False
        status_code = 500
        error_message = str(exc)
        raise
    finally:
        if success and not has_output:
            success = False
            error_message = error_message or "Empty response: no content in stream"
            token_pool = get_token_pool()
            if token_pool and upstream_auth_token:
                await token_pool.record_token_failure(
                    upstream_auth_token,
                    Exception(error_message),
                )
        await write_request_log(
            provider=provider,
            model=model,
            source_info=source_info,
            auth_token=auth_token,
            upstream_auth_token=upstream_auth_token,
            success=success,
            started_at=started_at,
            status_code=status_code,
            first_token_time=first_token_time,
            input_tokens=usage["input_tokens"],
            output_tokens=usage["output_tokens"],
            cache_creation_tokens=usage["cache_creation_tokens"],
            cache_read_tokens=usage["cache_read_tokens"],
            total_tokens=usage["total_tokens"],
            error_message=error_message,
        )