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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# -*- coding: utf-8 -*-
# Copyright (c) Alibaba, Inc. and its affiliates.

import time
from http import HTTPStatus
from typing import Dict, List, Optional

from dashscope.client.base_api import (
    CancelMixin,
    CreateMixin,
    GetStatusMixin,
    ListObjectMixin,
    UpdateMixin,
)
from dashscope.common.error import (
    AssistantError,
    InputRequired,
    TimeoutException,
)
from dashscope.common.logging import logger
from dashscope.threads.thread_types import (
    Run,
    RunList,
    RunStep,
    RunStepDelta,
    Thread,
    ThreadMessage,
    ThreadMessageDelta,
)

__all__ = ["Runs"]


class Runs(
    CreateMixin,
    CancelMixin,
    ListObjectMixin,
    GetStatusMixin,
    UpdateMixin,
):
    SUB_PATH = "RUNS"  # useless

    @classmethod
    def create_thread_and_run(
        cls,
        *,
        assistant_id: str,
        thread: Optional[Dict] = None,
        model: Optional[str] = None,
        instructions: Optional[str] = None,
        additional_instructions: Optional[str] = None,
        tools: Optional[List[Dict]] = None,
        stream: Optional[bool] = False,
        metadata: Optional[Dict] = None,
        workspace: str = None,
        extra_body: Optional[Dict] = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        if not assistant_id:
            raise InputRequired("assistant_id is required")
        data = {"assistant_id": assistant_id}
        if thread:
            data["thread"] = thread
        if model:
            data["model"] = model
        if instructions:
            data["instructions"] = instructions
        if additional_instructions:
            data["additional_instructions"] = additional_instructions
        if tools:
            data["tools"] = tools
        if metadata:
            data["metadata"] = metadata
        data["stream"] = stream
        if extra_body is not None and extra_body:
            data = {**data, **extra_body}

        response = super().call(
            data=data,
            path="threads/runs",
            api_key=api_key,
            flattened_output=True,
            stream=stream,  # type: ignore[arg-type]
            workspace=workspace,
            **kwargs,
        )
        if stream:
            return (  # type: ignore[return-value]
                (event_type, cls.convert_stream_object(event_type, item))
                for event_type, item in response
            )
        else:
            return Run(**response)

    @classmethod
    def create(  # pylint: disable=too-many-branches
        cls,
        thread_id: str,
        *,
        assistant_id: str,
        model: Optional[str] = None,
        instructions: Optional[str] = None,
        additional_instructions: Optional[str] = None,
        tools: Optional[List[Dict]] = None,
        metadata: Optional[Dict] = None,
        stream: Optional[bool] = False,
        workspace: str = None,
        extra_body: Optional[Dict] = None,
        api_key: str = None,
        top_p: Optional[float] = None,
        top_k: Optional[int] = None,
        temperature: Optional[float] = None,
        max_tokens: Optional[int] = None,
        **kwargs,
    ) -> Run:
        """Create a run.

        Args:
            thread_id (str): The thread to run.
            assistant_id (str): The assistant id to run.
            model (str): The model to use.
            instructions (str, optional):
                The system instructions this assistant uses. Defaults to None.
            additional_instructions (Optional[str], optional):
                Appends additional
                instructions at the end of the instructions for the run. This is
                useful for modifying the behavior on a per-run basis without
                overriding other instructions.. Defaults to None.
            tools (Optional[str], optional): List of tools to use.. Defaults to [].  # noqa: E501
            metadata (Dict, optional):
                Custom key-value pairs associate with run. Defaults to None.
            workspace (str): The DashScope workspace id.
            api_key (str, optional):
                The DashScope workspace id. Defaults to None.

        Raises:
            InputRequired: thread and assistant is required.

        Returns:
            Run: The `Run` object.
        """
        if not thread_id or not assistant_id:
            raise InputRequired("thread_id and assistant_id is required")
        data = {"assistant_id": assistant_id}
        if model:
            data["model"] = model
        if instructions:
            data["instructions"] = instructions
        if additional_instructions:
            data["additional_instructions"] = additional_instructions
        if tools:
            data["tools"] = tools
        if metadata:
            data["metadata"] = metadata
        data["stream"] = stream
        if extra_body is not None and extra_body:
            data = {**data, **extra_body}

        if top_p is not None:
            data["top_p"] = top_p
        if top_k is not None:
            data["top_k"] = top_k
        if temperature is not None:
            data["temperature"] = temperature
        if max_tokens is not None:
            data["max_tokens"] = max_tokens

        response = super().call(
            data=data,
            path=f"threads/{thread_id}/runs",
            api_key=api_key,
            flattened_output=True,
            stream=stream,  # type: ignore[arg-type]
            workspace=workspace,
            **kwargs,
        )
        if stream:
            return (  # type: ignore[return-value]
                (event_type, cls.convert_stream_object(event_type, item))
                for event_type, item in response
            )
        else:
            return Run(**response)

    @classmethod
    def convert_stream_object(cls, event, item):
        event_object_map = {
            "thread.created": Thread,
            "thread.run.created": Run,
            "thread.run.queued": Run,
            "thread.run.in_progress": Run,
            "thread.run.requires_action": Run,
            "thread.run.completed": Run,
            "thread.run.failed": Run,
            "thread.run.cancelled": Run,
            "thread.run.expired": Run,
            "thread.run.step.created": RunStep,
            "thread.run.step.in_progress": RunStep,
            "thread.run.step.delta": RunStepDelta,
            "thread.run.step.completed": RunStep,
            "thread.run.step.failed": RunStep,
            "thread.run.step.cancelled": RunStep,
            "thread.run.step.expired": RunStep,
            "thread.message.created": ThreadMessage,
            "thread.message.in_progress": ThreadMessage,
            "thread.message.delta": ThreadMessageDelta,
            "thread.message.completed": ThreadMessage,
            "thread.message.incomplete": ThreadMessage,
            "error": AssistantError,
        }
        if event in event_object_map:
            return event_object_map[event](**item)
        else:
            return item

    @classmethod
    def call(  # type: ignore[override]
        cls,
        thread_id: str,
        *,
        assistant_id: str,
        model: Optional[str] = None,
        instructions: Optional[str] = None,
        additional_instructions: Optional[str] = None,
        tools: Optional[List[Dict]] = None,
        stream: Optional[bool] = False,
        metadata: Optional[Dict] = None,
        workspace: str = None,
        extra_body: Optional[Dict] = None,
        api_key: str = None,
        top_p: Optional[float] = None,
        top_k: Optional[int] = None,
        temperature: Optional[float] = None,
        max_tokens: Optional[int] = None,
        **kwargs,
    ) -> Run:
        """Create a run.

        Args:
            thread_id (str): The thread to run.
            assistant_id (str): The assistant id to run.
            model (str): The model to use.
            instructions (str, optional):
                The system instructions this assistant uses. Defaults to None.
            additional_instructions (Optional[str], optional):
                Appends additional
                instructions at the end of the instructions for the run. This is
                useful for modifying the behavior on a per-run basis without
                overriding other instructions.. Defaults to None.
            tools (Optional[str], optional): List of tools to use.. Defaults to [].  # noqa: E501
            metadata (Dict, optional):
                Custom key-value pairs associate with run. Defaults to None.
            workspace (str): The DashScope workspace id.
            api_key (str, optional):
                The DashScope workspace id. Defaults to None.

        Raises:
            InputRequired: thread and assistant is required.

        Returns:
            Run: The `Run` object.
        """
        return cls.create(
            thread_id,
            assistant_id=assistant_id,
            model=model,
            instructions=instructions,
            additional_instructions=additional_instructions,
            tools=tools,
            stream=stream,
            metadata=metadata,
            workspace=workspace,
            extra_body=extra_body,
            api_key=api_key,
            top_p=top_p,
            top_k=top_k,
            temperature=temperature,
            max_tokens=max_tokens,
            **kwargs,
        )

    @classmethod
    def list(  # type: ignore[override]
        cls,
        thread_id: str,
        *,
        limit: int = None,
        order: str = None,
        after: str = None,
        before: str = None,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> RunList:
        """List `Run`.

        Args:
            thread_id (str): The thread id.
            limit (int, optional):
                How many assistant to retrieve. Defaults to None.
            order (str, optional): Sort order by created_at. Defaults to None.
            after (str, optional): Assistant id after. Defaults to None.
            before (str, optional): Assistant id before. Defaults to None.
            workspace (str, optional):
                The DashScope workspace id. Defaults to None.
            api_key (str, optional): Your DashScope api key. Defaults to None.

        Returns:
            RunList: The list of runs.
        """
        if not thread_id:
            raise InputRequired("thread_id is required!")
        response = super().list(
            limit=limit,
            order=order,
            after=after,
            before=before,
            path=f"threads/{thread_id}/runs",
            workspace=workspace,
            api_key=api_key,
            flattened_output=True,
            **kwargs,
        )
        return RunList(**response)

    @classmethod
    def retrieve(
        cls,
        run_id: str,
        *,
        thread_id: str,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        """Retrieve the `Run`.

        Args:
            thread_id (str): The thread id.
            run_id (str): The run id.
            workspace (str): The dashscope workspace id.
            api_key (str, optional): The api key. Defaults to None.

        Returns:
            Run: The `Run` object.
        """
        if not thread_id or not run_id:
            raise InputRequired("thread_id and run_id are required!")
        response = super().get(
            run_id,
            path=f"threads/{thread_id}/runs/{run_id}",
            workspace=workspace,
            api_key=api_key,
            flattened_output=True,
            **kwargs,
        )
        return Run(**response)

    @classmethod
    def get(  # type: ignore[override]
        cls,
        run_id: str,
        *,
        thread_id: str,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        """Retrieve the `Run`.

        Args:
            thread_id (str): The thread id.
            run_id (str): The run id.
            workspace (str): The dashscope workspace id.
            api_key (str, optional): The api key. Defaults to None.

        Returns:
            Run: The `Run` object.
        """
        return cls.retrieve(
            run_id,
            thread_id=thread_id,
            workspace=workspace,
            api_key=api_key,
            **kwargs,
        )

    @classmethod
    def submit_tool_outputs(
        cls,
        run_id: str,
        *,
        thread_id: str,
        tool_outputs: List[Dict],
        stream: Optional[bool] = False,
        workspace: str = None,
        extra_body: Optional[Dict] = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        """_summary_

        Args:
            thread_id (str): The thread id.
            run_id (str): The run id.
            tool_outputs (List[Dict]): The tool outputs.
            workspace (str): The dashscope workspace id.
            api_key (str, optional): The api key. Defaults to None.

        Raises:
            InputRequired: The tool output thread id run id
                are required.

        Returns:
            Run: The 'Run`.
        """
        if not tool_outputs:
            raise InputRequired("tool_outputs is required!")
        if not thread_id or not run_id:
            raise InputRequired("thread_id and run_id are required!")

        data = {"tool_outputs": tool_outputs}
        data["stream"] = stream
        if extra_body is not None and extra_body:
            data = {**data, **extra_body}

        response = super().call(
            data,
            path=f"threads/{thread_id}/runs/{run_id}/submit_tool_outputs",
            workspace=workspace,
            api_key=api_key,
            stream=stream,  # type: ignore[arg-type]
            flattened_output=True,
            **kwargs,
        )
        if stream:
            return (  # type: ignore[return-value]
                (event_type, cls.convert_stream_object(event_type, item))
                for event_type, item in response
            )
        else:
            return Run(**response)

    @classmethod
    def wait(  # pylint: disable=unused-argument
        cls,
        run_id: str,
        *,
        thread_id: str,
        timeout_seconds: float = float("inf"),
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        """Wait for run to complete.

        Args:
            thread_id (str): The thread id.
            run_id (str): The run id.
            timeout_seconds (int): The timeout seconds. Defaults inf.
            workspace (str): The dashscope workspace id.
            api_key (str, optional): The api key. Defaults to None.

        Returns:
            Run: The run final status.
        """
        if not run_id or not thread_id:
            raise InputRequired("run_id and thread_id are required!")
        start_time = time.perf_counter()
        while True:
            run = cls.get(
                run_id,
                thread_id=thread_id,
                workspace=workspace,
                api_key=api_key,
            )
            if run.status_code == HTTPStatus.OK:
                if hasattr(run, "status"):
                    if run.status in [
                        "cancelled",
                        "failed",
                        "completed",
                        "expired",
                        "requires_action",
                    ]:
                        break
                    time_eclipsed = (
                        time.perf_counter() - start_time
                    )  # pylint: disable=no-else-break
                    if time_eclipsed > timeout_seconds:
                        raise TimeoutException("Wait run complete timeout")
                    time.sleep(1)
                else:
                    logger.error("run has no status")
                    break
            else:
                logger.error(
                    "Get run thread_id: %s, run_id: %s failed, message: %s",
                    thread_id,
                    run_id,
                    run.message,
                )
                break
        return run

    @classmethod
    def update(  # type: ignore[override]
        cls,
        run_id: str,
        *,
        thread_id: str,
        metadata: Optional[Dict] = None,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        """Create a run.

        Args:
            thread_id (str): The thread of the run id to be updated.
            run_id (str): The run id to update.
            model (str): The model to use.
            metadata (Dict, optional):
                Custom key-value pairs associate with run. Defaults to None.
            workspace (str): The DashScope workspace id.
            api_key (str, optional):
                The DashScope workspace id. Defaults to None.

        Raises:
            InputRequired: thread id and run is required.

        Returns:
            Run: The `Run` object.
        """
        if not thread_id or not run_id:
            raise InputRequired("thread id and run id are required!")
        response = super().update(
            run_id,
            json={"metadata": metadata},
            path=f"threads/{thread_id}/runs/{run_id}",
            api_key=api_key,
            workspace=workspace,
            flattened_output=True,
            method="post",
            **kwargs,
        )
        return Run(**response)

    @classmethod
    def cancel(  # type: ignore[override]
        cls,
        run_id: str,
        *,
        thread_id: str,
        workspace: str = None,
        api_key: str = None,
        **kwargs,
    ) -> Run:
        """Cancel the `Run`.

        Args:
            thread_id (str): The thread id.
            run_id (str): The run id.
            workspace (str): The dashscope workspace id.
            api_key (str, optional): The api key. Defaults to None.

        Returns:
            Run: The `Run` object.
        """
        if not thread_id or not run_id:
            raise InputRequired("thread id and run id are required!")
        response = super().cancel(
            run_id,
            path=f"threads/{thread_id}/runs/{run_id}/cancel",
            api_key=api_key,
            workspace=workspace,
            flattened_output=True,
            **kwargs,
        )

        return Run(**response)