coze-coding-utils 0.2.3a2__py3-none-any.whl → 0.2.3a3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,24 +7,30 @@ from abc import ABC, abstractmethod
7
7
  from typing import Any, Dict, Iterator, AsyncIterable
8
8
  from langchain_core.runnables import RunnableConfig
9
9
  from langgraph.graph.state import CompiledStateGraph
10
- from coze_coding_utils.runtime_ctx.context import Context
11
10
  from coze_coding_utils.helper.agent_helper import (
12
11
  to_stream_input,
13
- to_client_message,
14
12
  agent_iter_server_messages,
15
13
  )
14
+
15
+ from coze_coding_utils.error import classify_error
16
+ import asyncio
17
+ import time
18
+ import traceback
19
+ from typing import Any, Dict, AsyncGenerator, Callable
20
+ from coze_coding_utils.runtime_ctx.context import Context
16
21
  from coze_coding_utils.messages.server import (
22
+ create_message_end_dict,
23
+ create_message_error_dict,
17
24
  MESSAGE_END_CODE_CANCELED,
18
- create_message_end_dict, create_message_error_dict,
19
25
  )
20
- from coze_coding_utils.error import classify_error
26
+ from coze_coding_utils.helper.agent_helper import to_client_message
27
+ from coze_coding_utils.error.classifier import ErrorClassifier
21
28
 
22
29
  logger = logging.getLogger(__name__)
23
30
 
24
31
  TIMEOUT_SECONDS = 900
25
32
  PING_INTERVAL_SECONDS = 30
26
33
 
27
-
28
34
  class WorkflowEventType:
29
35
  WORKFLOW_START = "workflow_start"
30
36
  WORKFLOW_END = "workflow_end"
@@ -450,6 +456,106 @@ class WorkflowStreamRunner(BaseStreamRunner):
450
456
  pass
451
457
 
452
458
 
459
+ async def agent_stream_handler(
460
+ payload: Dict[str, Any],
461
+ ctx: Context,
462
+ run_id: str,
463
+ stream_sse_func: Callable,
464
+ sse_event_func: Callable,
465
+ error_classifier: ErrorClassifier,
466
+ register_task_func: Callable[[str, asyncio.Task], None],
467
+ ) -> AsyncGenerator[str, None]:
468
+ task = asyncio.current_task()
469
+ if task:
470
+ register_task_func(run_id, task)
471
+ logger.info(f"Registered agent streaming task for run_id: {run_id}")
472
+
473
+ client_msg, _ = to_client_message(payload)
474
+ t0 = time.time()
475
+
476
+ try:
477
+ async for chunk in stream_sse_func(payload, ctx, need_detail=False):
478
+ yield chunk
479
+ except asyncio.CancelledError:
480
+ logger.info(f"Agent stream cancelled for run_id: {run_id}")
481
+ end_msg = create_message_end_dict(
482
+ code=MESSAGE_END_CODE_CANCELED,
483
+ message="Stream cancelled by user",
484
+ session_id=client_msg.session_id,
485
+ query_msg_id=client_msg.local_msg_id,
486
+ log_id=ctx.logid,
487
+ time_cost_ms=int((time.time() - t0) * 1000),
488
+ reply_id="",
489
+ sequence_id=1,
490
+ )
491
+ yield sse_event_func(end_msg)
492
+ raise
493
+ except Exception as ex:
494
+ err = error_classifier.classify(ex, {"node_name": "agent_stream", "run_id": run_id})
495
+ logger.error(
496
+ f"Unexpected error in agent_stream: [{err.code}] {err.message}, "
497
+ f"traceback: {traceback.format_exc()}"
498
+ )
499
+ error_msg = create_message_error_dict(
500
+ code=str(err.code),
501
+ message=str(ex),
502
+ session_id=client_msg.session_id,
503
+ query_msg_id=client_msg.local_msg_id,
504
+ log_id=ctx.logid,
505
+ reply_id="",
506
+ sequence_id=1,
507
+ local_msg_id=client_msg.local_msg_id,
508
+ )
509
+ yield sse_event_func(error_msg)
510
+
511
+
512
+ async def workflow_stream_handler(
513
+ payload: Dict[str, Any],
514
+ ctx: Context,
515
+ run_id: str,
516
+ stream_sse_func: Callable,
517
+ sse_event_func: Callable,
518
+ error_classifier: ErrorClassifier,
519
+ register_task_func: Callable[[str, asyncio.Task], None],
520
+ workflow_debug: bool = False,
521
+ ) -> AsyncGenerator[str, None]:
522
+ task = asyncio.current_task()
523
+ if task:
524
+ register_task_func(run_id, task)
525
+ logger.info(f"Registered workflow streaming task for run_id: {run_id}")
526
+
527
+ try:
528
+ async for chunk in stream_sse_func(payload, ctx, need_detail=workflow_debug):
529
+ yield chunk
530
+ except asyncio.CancelledError:
531
+ logger.info(f"Workflow stream cancelled for run_id: {run_id}")
532
+ cancel_event = {
533
+ "type": "error",
534
+ "timestamp": int(time.time() * 1000),
535
+ "log_id": ctx.logid,
536
+ "run_id": run_id,
537
+ "code": "CANCELED",
538
+ "error_msg": "Stream cancelled by user",
539
+ }
540
+ yield sse_event_func(cancel_event)
541
+ raise
542
+ except Exception as ex:
543
+ err = error_classifier.classify(ex, {"node_name": "workflow_stream", "run_id": run_id})
544
+ logger.error(
545
+ f"Unexpected error in workflow_stream: [{err.code}] {err.message}, "
546
+ f"traceback: {traceback.format_exc()}"
547
+ )
548
+ error_event = {
549
+ "type": "error",
550
+ "timestamp": int(time.time() * 1000),
551
+ "log_id": ctx.logid,
552
+ "run_id": run_id,
553
+ "code": str(err.code),
554
+ "error_msg": str(ex),
555
+ }
556
+ yield sse_event_func(error_event)
557
+
558
+
453
559
  def get_stream_runner(is_agent: bool) -> BaseStreamRunner:
454
560
  if is_agent:
455
561
  return AgentStreamRunner()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: coze-coding-utils
3
- Version: 0.2.3a2
3
+ Version: 0.2.3a3
4
4
  Summary: Utilities for Coze coding client runtime context and helpers.
5
5
  Project-URL: Homepage, https://code.byted.org/stone/coze-coding-client
6
6
  Author: Bytedance Stone Team
@@ -10,7 +10,7 @@ coze_coding_utils/file/file.py,sha256=fBda18EGSQZ3Xl8OqEaGAb5Rd90_SmhJ1k0jgQk2v7
10
10
  coze_coding_utils/helper/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
11
  coze_coding_utils/helper/agent_helper.py,sha256=q1ZM30xLXoW-m0NJmJ_Y0M-kUAQCBstG_j7xkqsyRSU,22546
12
12
  coze_coding_utils/helper/graph_helper.py,sha256=UNtqqiQNAQ4319qcC1vHiLYIL2eGzvGQRgXu3mgLq8Y,8893
13
- coze_coding_utils/helper/stream_runner.py,sha256=q8lLo3i-PMVgbhWiv3X51NoiHkmyhT0-25hI_W1BDi0,20384
13
+ coze_coding_utils/helper/stream_runner.py,sha256=f66n6QJ3zCakhk7Fe4Vz9vTZ2KJuM9v9UJfqX5S3nDA,24050
14
14
  coze_coding_utils/log/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
15
  coze_coding_utils/log/common.py,sha256=mUNkCm68oaPaI6-a5UwLf87AfhrMnVPkEuri16guqKc,168
16
16
  coze_coding_utils/log/config.py,sha256=Qkw3JRuGUKJ6CBY7WqHJOFeyCU47cArvUtMsSBifFMo,195
@@ -32,7 +32,7 @@ coze_coding_utils/openai/types/request.py,sha256=IuNMT2Ce1--_32R30Q2q7Lb2dAwKNy3
32
32
  coze_coding_utils/openai/types/response.py,sha256=pjHHVR8LSMVFCc3fGzKqXrdoKDIfSCJEfICd_X9Nohc,4808
33
33
  coze_coding_utils/runtime_ctx/__init__.py,sha256=4W8VliAYUP1KY2gLJ_YDy2TmcXYVm-PY7XikQD_bFwA,2
34
34
  coze_coding_utils/runtime_ctx/context.py,sha256=G8ld-WnQ1pTJe5OOXC_dTbagXj9IxmpRiPM4X_jWW6o,3992
35
- coze_coding_utils-0.2.3a2.dist-info/METADATA,sha256=cGQN_B0A1fz_AH5FqH_yjgcCaQkjzURl6bJxQurKRVk,979
36
- coze_coding_utils-0.2.3a2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
37
- coze_coding_utils-0.2.3a2.dist-info/licenses/LICENSE,sha256=lzckZhAjHlpSJcWvppoST095IHFpBwKiB2pKcBv7vP4,1078
38
- coze_coding_utils-0.2.3a2.dist-info/RECORD,,
35
+ coze_coding_utils-0.2.3a3.dist-info/METADATA,sha256=BpT4ybyeGIjxSW4G62Z56WZ05ITL24mGEkKh-zDG47E,979
36
+ coze_coding_utils-0.2.3a3.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
37
+ coze_coding_utils-0.2.3a3.dist-info/licenses/LICENSE,sha256=lzckZhAjHlpSJcWvppoST095IHFpBwKiB2pKcBv7vP4,1078
38
+ coze_coding_utils-0.2.3a3.dist-info/RECORD,,