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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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.

import asyncio
import json
import uuid
from http import HTTPStatus
from typing import Tuple, Union

import aiohttp

from dashscope.api_entities.base_request import AioBaseRequest
from dashscope.api_entities.dashscope_response import DashScopeAPIResponse
from dashscope.common.constants import (
    DEFAULT_REQUEST_TIMEOUT_SECONDS,
    SERVICE_503_MESSAGE,
    WEBSOCKET_ERROR_CODE,
)
from dashscope.common.error import (
    RequestFailure,
    UnexpectedMessageReceived,
    UnknownMessageReceived,
)
from dashscope.common.logging import logger
from dashscope.common.utils import async_to_sync
from dashscope.protocol.websocket import (
    ACTION_KEY,
    ERROR_MESSAGE,
    ERROR_NAME,
    EVENT_KEY,
    HEADER,
    TASK_ID,
    ActionType,
    EventType,
    WebsocketStreamingMode,
)


class WebSocketRequest(AioBaseRequest):
    def __init__(
        self,
        url: str,
        api_key: str,
        stream: bool = True,
        ws_stream_mode: str = WebsocketStreamingMode.OUT,
        is_binary_input: bool = False,
        timeout: int = DEFAULT_REQUEST_TIMEOUT_SECONDS,
        flattened_output: bool = False,
        pre_task_id=None,
        user_agent: str = "",
    ) -> None:
        super().__init__(user_agent=user_agent)
        # pylint: disable=pointless-string-statement
        """HttpRequest.

        Args:
            url (str): The request url.
            api_key (str): The api key.
            method (str): The http method(GET|POST).
            stream (bool, optional): Is stream request. Defaults to False.
            timeout (int, optional): Total request timeout.
                Defaults to DEFAULT_REQUEST_TIMEOUT_SECONDS.
        """
        self.url = url
        self.stream = stream
        self.flattened_output = flattened_output
        if timeout is None:
            self.timeout = DEFAULT_REQUEST_TIMEOUT_SECONDS
        else:
            self.timeout = timeout  # type: ignore[has-type]
        self.ws_stream_mode = ws_stream_mode
        self.is_binary_input = is_binary_input

        self.headers = {
            "Authorization": f"bearer {api_key}",
            **self.headers,
        }

        self.task_headers = {
            "streaming": self.ws_stream_mode,
        }
        self.pre_task_id = pre_task_id

    def add_headers(self, headers):
        self.headers = {**self.headers, **headers}

    def call(self):
        response = async_to_sync(self.connection_handler())
        if self.stream:
            return (item for item in response)
        else:
            output = next(response)
            try:
                next(response)
            except StopIteration:
                pass
            return output

    async def close(self):
        if self.ws is not None and not self.ws.closed:
            await self.ws.close()

    async def aio_call(self):
        response = self.connection_handler()
        if self.stream:
            return (item async for item in response)
        else:
            result = await response.__anext__()
            try:
                await response.__anext__()
            except StopAsyncIteration:
                pass
            return result

    async def connection_handler(self):  # pylint: disable=too-many-branches
        try:
            task_id = None
            async with aiohttp.ClientSession(
                timeout=aiohttp.ClientTimeout(
                    total=self.timeout,
                ),
            ) as session:
                async with session.ws_connect(
                    self.url,
                    headers=self.headers,
                    heartbeat=6000,
                ) as ws:
                    await self._start_task(ws)  # send start task action.
                    task_id = self.task_headers["task_id"]
                    await self._wait_for_task_started(
                        ws,
                    )  # wait for task started event. # noqa E501
                    if self.ws_stream_mode == WebsocketStreamingMode.NONE:
                        if self.is_binary_input:  # send the binary package
                            data = self.data.get_batch_binary_data()
                            await ws.send_bytes(list(data.values())[0])
                        (
                            is_binary,
                            result,
                        ) = await self._receive_batch_data_task(  # noqa E501
                            ws,
                        )
                        # do not need send finished task message.
                        yield self._to_DashScopeAPIResponse(
                            task_id,
                            is_binary,
                            result,
                        )
                    elif self.ws_stream_mode == WebsocketStreamingMode.IN:
                        # server is in, we send streaming out.
                        await self._send_continue_task_data(ws)
                        (
                            is_binary,
                            result,
                        ) = await self._receive_batch_data_task(  # noqa E501
                            ws,
                        )
                        # do not need send finished task message.
                        yield self._to_DashScopeAPIResponse(
                            task_id,
                            is_binary,
                            result,
                        )
                    elif self.ws_stream_mode == WebsocketStreamingMode.OUT:
                        # we send batch data, server streaming output data.
                        if self.is_binary_input:  # send only binary package.
                            data = self.data.get_batch_binary_data()
                            await ws.send_bytes(list(data.values())[0])
                        async for is_binary, message in self._receive_streaming_data_task(  # noqa E501  # pylint: disable=line-too-long
                            ws,
                        ):
                            yield self._to_DashScopeAPIResponse(
                                task_id,
                                is_binary,
                                message,
                            )
                    else:  # duplex mode
                        asyncio.create_task(self._send_continue_task_data(ws))
                        async for is_binary, message in self._receive_streaming_data_task(  # noqa E501  # pylint: disable=line-too-long
                            ws,
                        ):
                            yield self._to_DashScopeAPIResponse(
                                task_id,
                                is_binary,
                                message,
                            )
        except RequestFailure as e:
            yield DashScopeAPIResponse(
                request_id=e.request_id,
                status_code=e.http_code,
                output=None,
                code=e.name,
                message=e.message,
            )
        except aiohttp.ClientConnectorError as e:
            logger.exception(e)
            yield DashScopeAPIResponse(
                request_id="",
                status_code=-1,
                code="ClientConnectorError",
                message=str(e),
            )
        except aiohttp.WSServerHandshakeError as e:
            code = e.status
            msg = e.message
            if e.status in [HTTPStatus.FORBIDDEN, HTTPStatus.UNAUTHORIZED]:
                msg = "Unauthorized, your api-key is invalid!"
            elif e.status == HTTPStatus.SERVICE_UNAVAILABLE:
                msg = SERVICE_503_MESSAGE
            else:
                pass
            yield DashScopeAPIResponse(
                request_id=task_id,
                status_code=code,
                code=code,
                message=msg,
            )
        except BaseException as e:
            logger.exception(e)
            yield DashScopeAPIResponse(
                request_id="",
                status_code=-1,
                code="Unknown",
                message=f"Error type: {type(e)}, message: {e}",
            )

    def _to_DashScopeAPIResponse(self, task_id, is_binary, result):
        if is_binary:
            return DashScopeAPIResponse(
                request_id=task_id,
                status_code=HTTPStatus.OK,
                output=result,
            )
        else:
            # get output and usage.
            output = {}
            usage = {}
            if "output" in result:
                output = result["output"]
            if "usage" in result:
                usage = result["usage"]
            return DashScopeAPIResponse(
                request_id=task_id,
                status_code=HTTPStatus.OK,
                output=output,
                usage=usage,
            )

    async def _receive_streaming_data_task(self, ws):
        # check if request stream data, re return an iterator,
        # otherwise we collect data and return user.
        # no matter what, the response is streaming
        is_binary_output = False
        while True:  # pylint: disable=R1702
            msg = await ws.receive()
            await self._check_websocket_unexpected_message(msg)
            if msg.type == aiohttp.WSMsgType.TEXT:
                msg_json = msg.json()
                logger.debug("Receive %s event", msg_json[HEADER][EVENT_KEY])
                if msg_json[HEADER][EVENT_KEY] == EventType.GENERATED:
                    payload = msg_json["payload"]
                    yield False, payload
                elif msg_json[HEADER][EVENT_KEY] == EventType.FINISHED:
                    payload = None
                    if "payload" in msg_json:
                        payload = msg_json["payload"]
                    logger.debug(payload)
                    if payload:
                        yield False, payload
                    else:
                        if not self.stream:
                            if is_binary_output:
                                yield True, payload
                            else:
                                yield False, payload
                    break
                elif msg_json[HEADER][EVENT_KEY] == EventType.FAILED:
                    self._on_failed(msg_json)
                else:
                    error = f"Receive unknown message: {msg_json}"
                    logger.error(error)
                    raise UnknownMessageReceived(error)
            elif msg.type == aiohttp.WSMsgType.BINARY:
                is_binary_output = True
                yield True, msg.data

    def _on_failed(self, details):
        error = RequestFailure(
            request_id=details[HEADER][TASK_ID],
            http_code=WEBSOCKET_ERROR_CODE,
            name=details[HEADER][ERROR_NAME],
            message=details[HEADER][ERROR_MESSAGE],
        )
        logger.error(error)
        raise error

    async def _start_task(self, ws):
        if self.pre_task_id is not None:
            self.task_headers["task_id"] = self.pre_task_id
        else:
            self.task_headers["task_id"] = uuid.uuid4().hex  # create task id.
        task_header = {**self.task_headers, ACTION_KEY: ActionType.START}
        # for binary data, the start action has no input, only parameters.
        start_data = self.data.get_websocket_start_data()
        message = self._build_up_message(task_header, start_data)
        logger.debug("Send start task: %s", message)
        await ws.send_str(message)

    async def _send_finished_task(self, ws):
        task_header = {**self.task_headers, ACTION_KEY: ActionType.FINISHED}
        payload = {"input": {}}
        message = self._build_up_message(task_header, payload)
        logger.debug("Send finish task: %s", message)
        await ws.send_str(message)

    async def _send_continue_task_data(self, ws):
        headers = {
            "task_id": self.task_headers["task_id"],
            "action": "continue-task",
        }
        for input_item in self.data.get_websocket_continue_data():
            if self.is_binary_input:
                if len(input_item) > 0:
                    if isinstance(input_item, bytes):
                        await ws.send_bytes(input_item)
                        logger.debug(
                            "Send continue task with bytes: %s",
                            len(input_item),
                        )
                    else:
                        await ws.send_bytes(list(input_item.values())[0])
                        logger.debug(
                            "Send continue task with list[byte]: %s",
                            len(input_item),
                        )
            else:
                if len(input_item) > 0:
                    message = self._build_up_message(
                        headers=headers,
                        payload=input_item,
                    )
                    logger.debug("Send continue task: %s", message)
                    await ws.send_str(message)
            await asyncio.sleep(0.000001)

        # data send completed, and send task completed.
        await self._send_finished_task(ws)

    async def _receive_batch_data_task(
        self,
        ws,
    ) -> Tuple[bool, Union[str, bytes]]:
        """_summary_

        Args:
            ws (connection): The ws connection.

        Raises:
            UnknownMessageReceived: The message is unexpected.

        Returns:
            Tuple[bool, str]: is output is binary, output
        """
        while True:
            msg = await ws.receive()
            await self._check_websocket_unexpected_message(msg)
            if msg.type == aiohttp.WSMsgType.TEXT:
                msg_json = msg.json()
                logger.debug("Receive %s event", msg_json[HEADER][EVENT_KEY])
                if msg_json[HEADER][EVENT_KEY] == EventType.GENERATED:
                    payload = msg_json["payload"]
                    return False, payload
                elif msg_json[HEADER][EVENT_KEY] == EventType.FINISHED:
                    payload = msg_json["payload"]
                    return False, payload
                elif msg_json[HEADER][EVENT_KEY] == EventType.FAILED:
                    self._on_failed(msg_json)
                else:
                    error = f"Receive unknown message: {msg_json}"
                    logger.error(error)
                    raise UnknownMessageReceived(error)
            elif msg.type == aiohttp.WSMsgType.BINARY:
                return True, msg.data  # get binary result data.

    async def _wait_for_task_started(self, ws):
        while True:
            msg = await ws.receive()
            await self._check_websocket_unexpected_message(msg)
            if msg.type == aiohttp.WSMsgType.TEXT:
                msg_json = msg.json()
                logger.debug("Receive %s event", msg_json[HEADER][EVENT_KEY])
                if msg_json[HEADER][EVENT_KEY] == EventType.STARTED:
                    return
                elif msg_json[HEADER][EVENT_KEY] == EventType.FAILED:
                    self._on_failed(msg_json)
                else:
                    raise UnexpectedMessageReceived(
                        "Receive unexpected message, expect task-started, "
                        f"real: {msg_json[HEADER][EVENT_KEY]}.",
                    )
            elif msg.type == aiohttp.WSMsgType.BINARY:
                raise UnexpectedMessageReceived(
                    "Receive unexpected binary message when wait for task-started",  # noqa E501
                )

    async def _check_websocket_unexpected_message(self, msg):
        if msg.type == aiohttp.WSMsgType.CLOSED:
            details = f"WSMsgType.CLOSE, data: {msg.data}, extra: {msg.extra}"
            logger.error("Connection unexpected closed!")
            raise UnexpectedMessageReceived(
                f"Receive unexpected websocket close message, "
                f"details: {details}",
            )
        if msg.type == aiohttp.WSMsgType.ERROR:
            details = f"WSMsgType.ERROR, data: {msg.data}, extra: {msg.extra}"
            logger.error("Connection error: %s", details)
            raise UnexpectedMessageReceived(
                f"Receive unexpected websocket error message "
                f"details: {details}.",
            )

    def _build_up_message(self, headers, payload):
        message = {"header": headers, "payload": payload}
        return json.dumps(message)