File size: 16,812 Bytes
95f8c4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gemini-compatible application service handlers."""

from __future__ import annotations

import json

from fastapi import HTTPException
from fastapi.responses import StreamingResponse

from aistudio_api.api.response_models import (
    GeminiCandidateResponse,
    GeminiContentResponse,
    GeminiGenerateContentResponse,
)
from aistudio_api.api.responses import to_gemini_parts, to_gemini_usage_metadata
from aistudio_api.api.schemas import GeminiGenerateContentRequest
from aistudio_api.api.state import runtime_state
from aistudio_api.application.api_service_common import (
    MAX_RETRIES,
    ensure_active_account,
    logger,
    record_rotator_event,
    require_busy_lock,
    try_switch_account,
)
from aistudio_api.application.chat_service import (
    cleanup_files,
    normalize_gemini_request,
)
from aistudio_api.domain.errors import (
    AistudioError,
    AuthError,
    RequestError,
    UsageLimitExceeded,
)
from aistudio_api.infrastructure.gateway.client import AIStudioClient


async def handle_gemini_generate_content(
    model_path: str,
    req: GeminiGenerateContentRequest,
    client: AIStudioClient,
    *,
    stream: bool,
):
    busy_lock = require_busy_lock()
    if stream:
        return _build_gemini_streaming_response(
            client=client, req=req, model_path=model_path
        )

    last_error = None
    for attempt in range(MAX_RETRIES):
        async with busy_lock:
            await ensure_active_account(attempt)
            normalized = None
            try:
                normalized = normalize_gemini_request(req, model_path)
                logger.info(
                    "Gemini: model=%s, contents=%s, stream=False, attempt=%d",
                    normalized.model,
                    len(req.contents),
                    attempt + 1,
                )
                output = await client.generate_content(
                    model=normalized.model,
                    capture_prompt=normalized.capture_prompt,
                    capture_images=normalized.capture_images,
                    contents=normalized.contents,
                    system_instruction_content=normalized.system_instruction,
                    tools=normalized.tools,
                    safety_settings=normalized.safety_settings,
                    temperature=normalized.temperature,
                    top_p=normalized.top_p,
                    top_k=normalized.top_k,
                    max_tokens=normalized.max_tokens,
                    generation_config_overrides=normalized.generation_config_overrides,
                    sanitize_plain_text=False,
                )

                record_rotator_event("success")
                runtime_state.record(normalized.model, "success", output.usage)
                return GeminiGenerateContentResponse(
                    candidates=[
                        GeminiCandidateResponse(
                            content=GeminiContentResponse(
                                parts=to_gemini_parts(
                                    output.text,
                                    function_calls=output.function_calls,
                                    function_responses=output.function_responses,
                                    thinking=output.thinking,
                                    images=output.images,
                                    reasoning_images=output.reasoning_images,
                                ),
                            ),
                            finishReason="STOP"
                            if not output.function_calls
                            else "FUNCTION_CALL",
                        )
                    ],
                    usageMetadata=to_gemini_usage_metadata(output.usage),
                )
            except ValueError as exc:
                raise HTTPException(
                    400, detail={"message": str(exc), "type": "bad_request"}
                ) from exc
            except UsageLimitExceeded as exc:
                runtime_state.record(model_path, "rate_limited")
                last_error = exc

                record_rotator_event("rate_limited")
                if await try_switch_account():
                    logger.info(
                        "Gemini 429 限流,已切换账号,重试 %d/%d",
                        attempt + 1,
                        MAX_RETRIES,
                    )
                    continue
                logger.warning("Gemini 429 限流,无法切换账号")
                raise HTTPException(
                    429, detail={"message": str(exc), "type": "rate_limit_exceeded"}
                ) from exc
            except AistudioError as exc:
                runtime_state.record(model_path, "errors")
                record_rotator_event("error")
                raise HTTPException(
                    500, detail={"message": str(exc), "type": "server_error"}
                ) from exc
            except Exception as exc:
                runtime_state.record(model_path, "errors")
                record_rotator_event("error")
                logger.error("Gemini error: %s", exc, exc_info=True)
                raise HTTPException(
                    500, detail={"message": str(exc), "type": "server_error"}
                ) from exc
            finally:
                if normalized is not None and not stream:
                    cleanup_files(normalized.cleanup_paths)

    raise HTTPException(
        429, detail={"message": str(last_error), "type": "rate_limit_exceeded"}
    ) from last_error


def _build_gemini_streaming_response(
    *,
    client: AIStudioClient,
    req: GeminiGenerateContentRequest,
    model_path: str,
) -> StreamingResponse:
    async def stream_response():
        busy_lock = runtime_state.busy_lock
        if busy_lock is None:
            yield (
                "data: "
                + json.dumps(
                    {"error": {"message": "Server not ready"}}, ensure_ascii=False
                )
                + "\n\n"
            )
            return

        async with busy_lock:
            normalized = None
            try:
                await ensure_active_account(0)
                normalized = normalize_gemini_request(req, model_path)
                logger.info(
                    "Gemini stream: model=%s, contents=%s",
                    normalized.model,
                    len(req.contents),
                )
                final_usage = None
                has_yielded_data = False
                for stream_attempt in range(MAX_RETRIES):
                    try:
                        has_yielded_data = False
                        async for event_type, text in client.stream_generate_content(
                            model=normalized.model,
                            capture_prompt=normalized.capture_prompt,
                            capture_images=normalized.capture_images,
                            contents=normalized.contents,
                            system_instruction_content=normalized.system_instruction,
                            tools=normalized.tools,
                            safety_settings=normalized.safety_settings,
                            temperature=normalized.temperature,
                            top_p=normalized.top_p,
                            top_k=normalized.top_k,
                            max_tokens=normalized.max_tokens,
                            generation_config_overrides=normalized.generation_config_overrides,
                            sanitize_plain_text=False,
                            force_refresh_capture=stream_attempt > 0,
                        ):
                            has_yielded_data = True
                            if event_type == "body" and text:
                                yield (
                                    "data: "
                                    + json.dumps(
                                        {
                                            "candidates": [
                                                {
                                                    "content": {
                                                        "role": "model",
                                                        "parts": [{"text": text}],
                                                    },
                                                    "finishReason": None,
                                                }
                                            ]
                                        },
                                        ensure_ascii=False,
                                    )
                                    + "\n\n"
                                )
                            elif event_type == "images" and text:
                                img_list = text if isinstance(text, list) else []
                                yield (
                                    "data: "
                                    + json.dumps(
                                        {
                                            "candidates": [
                                                {
                                                    "content": {
                                                        "role": "model",
                                                        "parts": [
                                                            part.model_dump(
                                                                mode="json",
                                                                exclude_none=True,
                                                            )
                                                            for part in to_gemini_parts(
                                                                "", images=img_list
                                                            )
                                                        ],
                                                    },
                                                    "finishReason": None,
                                                }
                                            ]
                                        },
                                        ensure_ascii=False,
                                    )
                                    + "\n\n"
                                )
                            elif event_type == "reasoning_images" and text:
                                r_img_list = text if isinstance(text, list) else []
                                yield (
                                    "data: "
                                    + json.dumps(
                                        {
                                            "candidates": [
                                                {
                                                    "content": {
                                                        "role": "model",
                                                        "parts": [
                                                            part.model_dump(
                                                                mode="json",
                                                                exclude_none=True,
                                                            )
                                                            for part in to_gemini_parts(
                                                                "",
                                                                reasoning_images=r_img_list,
                                                            )
                                                        ],
                                                    },
                                                    "finishReason": None,
                                                }
                                            ]
                                        },
                                        ensure_ascii=False,
                                    )
                                    + "\n\n"
                                )
                            elif event_type == "thinking" and text:
                                yield (
                                    "data: "
                                    + json.dumps(
                                        {
                                            "candidates": [
                                                {
                                                    "content": {
                                                        "role": "model",
                                                        "parts": [
                                                            {
                                                                "text": text,
                                                                "thought": True,
                                                            }
                                                        ],
                                                    },
                                                    "finishReason": None,
                                                }
                                            ]
                                        },
                                        ensure_ascii=False,
                                    )
                                    + "\n\n"
                                )
                            elif event_type == "usage":
                                final_usage = text if isinstance(text, dict) else None
                        break
                    except UsageLimitExceeded:
                        if normalized is not None:
                            runtime_state.record(normalized.model, "rate_limited")
                        record_rotator_event("rate_limited")
                        if not has_yielded_data and await try_switch_account():
                            logger.warning(
                                "Gemini stream 429 限流,已切换账号,重试 %d/%d",
                                stream_attempt + 1,
                                MAX_RETRIES,
                            )
                            continue
                        raise
                    except RequestError as exc:
                        if exc.status == 204 and stream_attempt == 0:
                            logger.warning(
                                "Gemini stream 收到 204,清理 snapshot 缓存后重试一次"
                            )
                            client.clear_snapshot_cache()
                            continue
                        raise
                    except AuthError as exc:
                        if stream_attempt == 0:
                            logger.warning(
                                "Gemini stream 鉴权异常,清理 snapshot 缓存后重试一次: %s",
                                exc,
                            )
                            client.clear_snapshot_cache()
                            continue
                        raise
                record_rotator_event("success")
                if normalized is not None:
                    runtime_state.record(normalized.model, "success", final_usage)
                if final_usage:
                    yield (
                        "data: "
                        + json.dumps(
                            {
                                "candidates": [],
                                "usageMetadata": to_gemini_usage_metadata(
                                    final_usage
                                ).model_dump(mode="json"),
                            },
                            ensure_ascii=False,
                        )
                        + "\n\n"
                    )
                yield "data: [DONE]\n\n"
            except Exception as exc:
                logger.error("Gemini stream error: %s", exc, exc_info=True)
                if not isinstance(exc, UsageLimitExceeded):
                    record_rotator_event("error")
                if normalized is not None:
                    runtime_state.record(normalized.model, "errors")
                yield (
                    "data: "
                    + json.dumps({"error": {"message": str(exc)}}, ensure_ascii=False)
                    + "\n\n"
                )
            finally:
                if normalized is not None:
                    cleanup_files(normalized.cleanup_paths)

    return StreamingResponse(
        stream_response(),
        media_type="text/event-stream",
        headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
    )