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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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from fastapi import FastAPI, Request, HTTPException, Header
from fastapi.responses import StreamingResponse
import httpx
import json
import uuid
import time
import logging
import random
from typing import List, Dict, Any, AsyncGenerator
from functools import wraps
from pydantic import BaseModel, Field

app = FastAPI()

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


class OpenAIMessage(BaseModel):
    role: str
    content: str | Dict[str, Any] | List[Any] = Field(default="")  # 支持多种类型


class OpenAIRequest(BaseModel):
    messages: List[OpenAIMessage]
    stream: bool = Field(default=False)


class WoHistoryItem(BaseModel):
    query: str
    rewriteQuery: str
    uploadFileUrl: str
    response: str
    reasoningContent: str
    state: str
    key: str


class WoRequest(BaseModel):
    modelId: int
    input: str
    history: List[WoHistoryItem]


def validate_messages(f):
    """Message format validation decorator"""

    @wraps(f)
    async def decorated_function(*args, **kwargs):
        request: Request = kwargs.get('request')  # type: ignore
        if not request:
            raise HTTPException(status_code=500, detail="Missing request object")
        body = await request.json()
        messages = body.get('messages', [])

        for msg in messages:
            if 'role' not in msg or 'content' not in msg:
                raise HTTPException(status_code=400, detail="Invalid message format")
        return await f(*args, **kwargs)

    return decorated_function


def convert_history(messages: List[Dict[str, str]]) -> List[Dict[str, Any]]:
    """Convert OpenAI format messages to WoCloud format"""
    history = []

    for i in range(len(messages) - 1):
        try:
            if messages[i]['role'] == 'user' and i + 1 < len(messages) and messages[i + 1]['role'] == 'assistant':
                query = messages[i]['content']
                response = messages[i + 1]['content']
                
                # Convert JSON content to string if necessary
                if not isinstance(query, str):
                    query = json.dumps(query)
                if not isinstance(response, str):
                    response = json.dumps(response)
                
                query = query.strip()
                response = response.strip()

                history.append({
                    "query": query,
                    "rewriteQuery": query,
                    "uploadFileUrl": "",
                    "response": response,
                    "reasoningContent": "",
                    "state": "finish",
                    "key": str(random.random())
                })
        except (KeyError, IndexError) as e:
            logger.warning(f"Error processing message: {str(e)}")
            continue

    logger.debug(f"Converted history: {json.dumps(history, ensure_ascii=False)}")
    return history


async def handle_wo_error(response: httpx.Response) -> Dict[str, str]:
    """Unified handling of WoCloud error responses"""
    try:
        if response.headers.get('Content-Type', '').startswith('text/event-stream'):
            async for line in response.aiter_lines():
                if line.startswith('data:'):
                    try:
                        error_data = json.loads(line[5:].strip())
                        return {
                            "code": error_data.get('code'),
                            "message": error_data.get('message', 'Unknown error')
                        }
                    except json.JSONDecodeError:
                        return {
                            "code": "PARSE_ERROR",
                            "message": f"Invalid JSON in error response: {line[5:100]}"
                        }
        else:
            try:
                error_data = response.json()
                return {
                    "code": error_data.get('code'),
                    "message": error_data.get('message', error_data.get('response', 'Unknown error'))
                }
            except json.JSONDecodeError:
                return {
                    "code": "PARSE_ERROR",
                    "message": f"Invalid JSON in error response: {response.text[:100]}"
                }
    except Exception as e:
        return {
            "code": "PARSE_ERROR",
            "message": f"Failed to parse error response: {str(e)}"
        }


def create_chunk(content: str, response_id: str, created_time: int, finish_reason: str = None,
                 is_start: bool = False, role: str = None) -> str:
    """Creates a single chunk in the OpenAI streaming format."""
    chunk_data = {
        'id': response_id,
        'object': 'chat.completion.chunk',
        'created': created_time,
        'model': 'DeepSeek-R1',
        'choices': [{
            'index': 0,
            'delta': {},
            'finish_reason': finish_reason
        }]
    }
    if is_start and role:
         chunk_data["choices"][0]["delta"]["role"] = role
    elif content:
        chunk_data['choices'][0]['delta']['content'] = content


    return f"data: {json.dumps(chunk_data)}\n\n"


@app.post('/v1/chat/completions')
@validate_messages
async def chat_completions(request: Request, authorization: str = Header(...)):
    if not authorization.startswith('Bearer '):
        raise HTTPException(status_code=401, detail="Invalid Authorization header format")

    access_token = authorization[7:]
    openai_request = await request.json()
    stream_mode = openai_request.get('stream', False)

    # Extract last user message
    user_message = next(
        (msg['content'] for msg in reversed(openai_request['messages'])
         if msg['role'] == 'user' and msg.get('content')),
        None
    )
    if not user_message:
        raise HTTPException(status_code=400, detail="No valid user message found")
    
    # Convert JSON content to string if necessary
    if not isinstance(user_message, str):
        user_message = json.dumps(user_message)
    
    # Build WoCloud request
    wo_headers = {
        'content-type': 'application/json',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'connection': 'keep-alive',
        'dnt': '1',
        'origin': 'https://panservice.mail.wo.cn',
        'referer': f'https://panservice.mail.wo.cn/h5/wocloud_ai/?token={access_token}&modelType=1&platform=yunpanWeb&clientId=1001000021',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1 Edg/134.0.0.0',
        'x-yp-access-token': access_token,
        'x-yp-app-version': '',  # 注意保持空值
        'x-yp-client-id': '1001000021',  # 原值为1001000035
        'accept': 'text/event-stream' if stream_mode else 'application/json'
    }

    # Convert history
    history = convert_history(openai_request['messages'][:-1])  # Exclude last user message
    wo_data = {
        "modelId": 1,
        "input": user_message,
        "history": history
    }
    logger.debug(f"Forwarding request to WoCloud: {json.dumps(wo_data, ensure_ascii=False)}")

    async def stream_response() -> AsyncGenerator[bytes, None]:
        response_id = f"chatcmpl-{uuid.uuid4()}"
        created_time = int(time.time())
        full_content = ""

        async with httpx.AsyncClient() as client:
            try:
                async with client.stream(
                        'POST',
                        'https://panservice.mail.wo.cn/wohome/ai/assistant/query',
                        headers=wo_headers,
                        json=wo_data,
                        timeout=30
                ) as response:

                    logger.debug(f"WoCloud stream response status: {response.status_code}")

                    if response.status_code != 200:
                        error_info = await handle_wo_error(response)
                        logger.error(f"WoCloud API error: {error_info}")
                        yield f"data: {json.dumps({'error': error_info})}\n\n".encode('utf-8')
                        return

                    # Send initial chunk with role
                    yield create_chunk("", response_id, created_time, is_start=True, role="assistant").encode("utf-8")

                    async for line in response.aiter_lines():
                        if line:
                            try:
                                if line.startswith('data:'):
                                    data = json.loads(line[5:])

                                    if data.get('code') and data['code'] != 0 and data['code'] != '0':
                                        logger.error(f"WoCloud stream error: {data}")
                                        yield f"data: {json.dumps({'error': {'code': data['code'], 'message': data.get('message', 'Unknown error')}})}\n\n".encode(
                                            'utf-8')
                                        return

                                    content = data.get('response', '')
                                    reasoning = data.get('reasoningContent', '')

                                    if reasoning:
                                        if not full_content:
                                             yield create_chunk("<think>\n", response_id, created_time).encode("utf-8")

                                        full_content += reasoning
                                        yield create_chunk(reasoning, response_id, created_time).encode("utf-8")


                                    if content:
                                        if full_content:  # If there's thinking content, end thinking first
                                            yield create_chunk("\n</think>\n\n", response_id, created_time).encode("utf-8")
                                            full_content = ""
                                        yield create_chunk(content, response_id, created_time).encode("utf-8")


                                    if data.get("finish") == 1:
                                        break

                            except Exception as e:
                                logger.error(f"Stream parsing error: {str(e)}")
                                yield f"data: {json.dumps({'error': {'code': 'PARSE_ERROR', 'message': str(e)}})}\n\n".encode(
                                    'utf-8')
            except httpx.RequestError as e:
                logger.error(f"Request failed: {str(e)}")
                yield f"data: {json.dumps({'error': {'code': 'CONNECTION_ERROR', 'message': str(e)}})}\n\n".encode(
                    'utf-8')

            yield create_chunk("", response_id, created_time, finish_reason="stop").encode("utf-8")
            yield "data: [DONE]\n\n".encode('utf-8')

    if stream_mode:
        return StreamingResponse(stream_response(), media_type="text/event-stream")

    else:
        async with httpx.AsyncClient() as client:
            try:
                response = await client.post(
                    'https://panservice.mail.wo.cn/wohome/ai/assistant/query',
                    headers=wo_headers,
                    json=wo_data,
                    timeout=30
                )
                logger.debug(f"WoCloud response status: {response.status_code}")
                logger.debug(f"WoCloud response headers: {response.headers}")
                if response.status_code != 200:
                    error_info = await handle_wo_error(response)
                    logger.error(f"WoCloud API error: {error_info}")
                    raise HTTPException(status_code=502, detail=error_info)

                content = ""

                if response.headers.get('Content-Type', '').startswith('application/json'):
                    try:
                        response_data = response.json()
                        if response_data.get('code') and response_data['code'] != 0 and response_data['code'] != '0':
                            logger.error(f"WoCloud API error: {response_data}")
                            raise HTTPException(status_code=502, detail={
                                "code": response_data['code'],
                                "message": response_data.get("message", "Unknown error")
                            })

                        content = response_data.get('response', '')
                        reasoning = response_data.get('reasoningContent', '')

                        final_content = ""
                        if reasoning:
                            final_content += f"<think>\n{reasoning}\n</think>\n\n"
                        final_content += content
                    except json.JSONDecodeError:
                        logger.error(f"Invalid JSON Response:{response.text[:200]}")
                        raise HTTPException(status_code=502,
                                            detail={"code": "PARSE_ERROR", "message": "Failed to parse JSON response"})
                else:
                    response_data = {"response": "", "reasoningContent": ""}
                    try:
                        for line in response.text.split('\n'):
                            if line.startswith('data:'):
                                try:
                                    data = json.loads(line[5:])
                                    if data.get('code') and data['code'] != 0 and data['code'] != '0':
                                        logger.error(f"WoCloud API error in stream:{data}")
                                        raise HTTPException(status_code=502, detail={
                                            "code": data['code'],
                                            "message": data.get('message', 'Unknown error')
                                        })
                                    response_data['response'] += data.get('response', '')
                                    response_data['reasoningContent'] += data.get('reasoningContent', '')
                                except json.JSONDecodeError:
                                    logger.warning(f"Invalid JSON in stream line: {line[:100]}")
                        final_content = ''
                        if response_data['reasoningContent']:
                            final_content += f"<think>\n{response_data['reasoningContent']}\n</think>\n\n"
                        final_content += response_data['response']

                    except Exception as e:
                        logger.error(f"Error parsing stream response:{str(e)}")
                        raise HTTPException(status_code=502, detail={
                            "code": "PARSE_ERROR",
                            "message": f"Failed to parse stream response: {str(e)}"
                        })

                if not final_content:
                    logger.warning("Empty content in response")
                    final_content = "抱歉,没有收到有效的回复。"

                return {
                    "id": f"chatcmpl-{uuid.uuid4()}",
                    "object": "chat.completion",
                    "created": int(time.time()),
                    "model": "DeepSeek-R1",
                    "choices": [{
                        "index": 0,
                        "message": {
                            "role": "assistant",
                            "content": final_content
                        },
                        "finish_reason": "stop"
                    }],
                    "usage": {
                        "prompt_tokens": len(user_message),
                        "completion_tokens": len(final_content),
                        "total_tokens": len(user_message) + len(final_content)
                    }
                }


            except httpx.RequestError as e:
                logger.error(f"Request failed: {str(e)}")
                raise HTTPException(status_code=502, detail={
                    "code": "NETWORK_ERROR",
                    "message": str(e)
                })
            except Exception as e:
                logger.exception("Unexpected error")
                raise HTTPException(status_code=500, detail={
                    "code": "INTERNAL_ERROR",
                    "message": str(e)
                })


@app.get('/v1/models')
async def list_models():
    return {
        "object": "list",
        "data": [{
            "id": "DeepSeek-R1",
            "object": "model",
            "created": int(time.time()),
            "owned_by": "ChinaUnicom",
            "capabilities": ["chat", "completions"]
        }]
    }


if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8080)