coze-coding-utils 0.2.3a3__py3-none-any.whl → 0.2.4__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.
- coze_coding_utils/helper/stream_runner.py +31 -22
- coze_coding_utils/log/node_log.py +0 -1
- {coze_coding_utils-0.2.3a3.dist-info → coze_coding_utils-0.2.4.dist-info}/METADATA +1 -1
- {coze_coding_utils-0.2.3a3.dist-info → coze_coding_utils-0.2.4.dist-info}/RECORD +6 -6
- {coze_coding_utils-0.2.3a3.dist-info → coze_coding_utils-0.2.4.dist-info}/WHEEL +0 -0
- {coze_coding_utils-0.2.3a3.dist-info → coze_coding_utils-0.2.4.dist-info}/licenses/LICENSE +0 -0
|
@@ -4,7 +4,8 @@ import threading
|
|
|
4
4
|
import contextvars
|
|
5
5
|
import logging
|
|
6
6
|
from abc import ABC, abstractmethod
|
|
7
|
-
from
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Any, Dict, Iterator, AsyncIterable, Optional, Literal
|
|
8
9
|
from langchain_core.runnables import RunnableConfig
|
|
9
10
|
from langgraph.graph.state import CompiledStateGraph
|
|
10
11
|
from coze_coding_utils.helper.agent_helper import (
|
|
@@ -31,6 +32,15 @@ logger = logging.getLogger(__name__)
|
|
|
31
32
|
TIMEOUT_SECONDS = 900
|
|
32
33
|
PING_INTERVAL_SECONDS = 30
|
|
33
34
|
|
|
35
|
+
|
|
36
|
+
@dataclass
|
|
37
|
+
class RunOpt:
|
|
38
|
+
workflow_debug: bool = False
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def stream_mode(self) -> Literal["debug", "updates"]:
|
|
42
|
+
return "debug" if self.workflow_debug else "updates"
|
|
43
|
+
|
|
34
44
|
class WorkflowEventType:
|
|
35
45
|
WORKFLOW_START = "workflow_start"
|
|
36
46
|
WORKFLOW_END = "workflow_end"
|
|
@@ -47,16 +57,16 @@ class WorkflowErrorCode:
|
|
|
47
57
|
|
|
48
58
|
class BaseStreamRunner(ABC):
|
|
49
59
|
@abstractmethod
|
|
50
|
-
def stream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context) -> Iterator[Any]:
|
|
60
|
+
def stream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context, run_opt: Optional[RunOpt] = None) -> Iterator[Any]:
|
|
51
61
|
pass
|
|
52
62
|
|
|
53
63
|
@abstractmethod
|
|
54
|
-
async def astream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context) -> AsyncIterable[Any]:
|
|
64
|
+
async def astream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context, run_opt: Optional[RunOpt] = None) -> AsyncIterable[Any]:
|
|
55
65
|
pass
|
|
56
66
|
|
|
57
67
|
|
|
58
68
|
class AgentStreamRunner(BaseStreamRunner):
|
|
59
|
-
def stream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context) -> Iterator[Any]:
|
|
69
|
+
def stream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context, run_opt: Optional[RunOpt] = None) -> Iterator[Any]:
|
|
60
70
|
client_msg, session_id = to_client_message(payload)
|
|
61
71
|
run_config["recursion_limit"] = 100
|
|
62
72
|
run_config["configurable"] = {"thread_id": session_id}
|
|
@@ -101,7 +111,7 @@ class AgentStreamRunner(BaseStreamRunner):
|
|
|
101
111
|
)
|
|
102
112
|
yield end_msg
|
|
103
113
|
|
|
104
|
-
async def astream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context) -> AsyncIterable[Any]:
|
|
114
|
+
async def astream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context, run_opt: Optional[RunOpt] = None) -> AsyncIterable[Any]:
|
|
105
115
|
client_msg, session_id = to_client_message(payload)
|
|
106
116
|
run_config["recursion_limit"] = 100
|
|
107
117
|
run_config["configurable"] = {"thread_id": session_id}
|
|
@@ -194,9 +204,6 @@ class AgentStreamRunner(BaseStreamRunner):
|
|
|
194
204
|
|
|
195
205
|
|
|
196
206
|
class WorkflowStreamRunner(BaseStreamRunner):
|
|
197
|
-
def __init__(self):
|
|
198
|
-
self._node_start_times: Dict[str, float] = {}
|
|
199
|
-
|
|
200
207
|
def _serialize_data(self, data: Any) -> Any:
|
|
201
208
|
if isinstance(data, dict):
|
|
202
209
|
return {k: self._serialize_data(v) for k, v in data.items()}
|
|
@@ -221,7 +228,9 @@ class WorkflowStreamRunner(BaseStreamRunner):
|
|
|
221
228
|
result.update(kwargs)
|
|
222
229
|
return result
|
|
223
230
|
|
|
224
|
-
def stream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context) -> Iterator[Any]:
|
|
231
|
+
def stream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context, run_opt: Optional[RunOpt] = None) -> Iterator[Any]:
|
|
232
|
+
if run_opt is None:
|
|
233
|
+
run_opt = RunOpt()
|
|
225
234
|
run_config["recursion_limit"] = 100
|
|
226
235
|
if "configurable" not in run_config:
|
|
227
236
|
run_config["configurable"] = {}
|
|
@@ -232,21 +241,19 @@ class WorkflowStreamRunner(BaseStreamRunner):
|
|
|
232
241
|
node_start_times: Dict[str, float] = {}
|
|
233
242
|
final_output = {}
|
|
234
243
|
seq = 0
|
|
235
|
-
is_debug = run_config.get("configurable", {}).get("workflow_debug", False)
|
|
236
|
-
stream_mode = "debug" if is_debug else "updates"
|
|
237
244
|
|
|
238
245
|
try:
|
|
239
246
|
seq += 1
|
|
240
247
|
yield (seq, self._build_event(WorkflowEventType.WORKFLOW_START, ctx))
|
|
241
248
|
|
|
242
|
-
for event in graph.stream(payload, stream_mode=stream_mode, config=run_config, context=ctx):
|
|
249
|
+
for event in graph.stream(payload, stream_mode=run_opt.stream_mode, config=run_config, context=ctx):
|
|
243
250
|
current_time = time.time()
|
|
244
251
|
if current_time - last_ping_time >= PING_INTERVAL_SECONDS:
|
|
245
252
|
seq += 1
|
|
246
253
|
yield (seq, self._build_event(WorkflowEventType.PING, ctx))
|
|
247
254
|
last_ping_time = current_time
|
|
248
255
|
|
|
249
|
-
if not
|
|
256
|
+
if not run_opt.workflow_debug:
|
|
250
257
|
if isinstance(event, dict):
|
|
251
258
|
logger.info(f"Debug event: {event}")
|
|
252
259
|
for node_name, node_output in event.items():
|
|
@@ -311,7 +318,9 @@ class WorkflowStreamRunner(BaseStreamRunner):
|
|
|
311
318
|
seq += 1
|
|
312
319
|
yield (seq, self._build_event(WorkflowEventType.ERROR, ctx, code=str(err.code), error_msg=err.message))
|
|
313
320
|
|
|
314
|
-
async def astream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context) -> AsyncIterable[Any]:
|
|
321
|
+
async def astream(self, payload: Dict[str, Any], graph: CompiledStateGraph, run_config: RunnableConfig, ctx: Context, run_opt: Optional[RunOpt] = None) -> AsyncIterable[Any]:
|
|
322
|
+
if run_opt is None:
|
|
323
|
+
run_opt = RunOpt()
|
|
315
324
|
run_config["recursion_limit"] = 100
|
|
316
325
|
if "configurable" not in run_config:
|
|
317
326
|
run_config["configurable"] = {}
|
|
@@ -323,9 +332,7 @@ class WorkflowStreamRunner(BaseStreamRunner):
|
|
|
323
332
|
start_time = time.time()
|
|
324
333
|
cancelled = threading.Event()
|
|
325
334
|
last_ping_time = [start_time]
|
|
326
|
-
|
|
327
|
-
stream_mode = "debug" if is_debug else "updates"
|
|
328
|
-
logger.info(f"Stream mode: {stream_mode}")
|
|
335
|
+
logger.info(f"Stream mode: {run_opt.stream_mode}")
|
|
329
336
|
seq = [0]
|
|
330
337
|
|
|
331
338
|
def producer():
|
|
@@ -339,7 +346,7 @@ class WorkflowStreamRunner(BaseStreamRunner):
|
|
|
339
346
|
seq[0] += 1
|
|
340
347
|
loop.call_soon_threadsafe(q.put_nowait, (seq[0], self._build_event(WorkflowEventType.WORKFLOW_START, ctx)))
|
|
341
348
|
|
|
342
|
-
for event in graph.stream(payload, stream_mode=stream_mode, config=run_config, context=ctx):
|
|
349
|
+
for event in graph.stream(payload, stream_mode=run_opt.stream_mode, config=run_config, context=ctx):
|
|
343
350
|
if cancelled.is_set():
|
|
344
351
|
logger.info(f"Workflow producer cancelled during iteration for run_id: {ctx.run_id}")
|
|
345
352
|
seq[0] += 1
|
|
@@ -358,7 +365,7 @@ class WorkflowStreamRunner(BaseStreamRunner):
|
|
|
358
365
|
loop.call_soon_threadsafe(q.put_nowait, (seq[0], self._build_event(WorkflowEventType.PING, ctx)))
|
|
359
366
|
last_ping_time[0] = current_time
|
|
360
367
|
|
|
361
|
-
if not
|
|
368
|
+
if not run_opt.workflow_debug:
|
|
362
369
|
if isinstance(event, dict):
|
|
363
370
|
for node_name, node_output in event.items():
|
|
364
371
|
logger.info(f"Node output: {node_name}")
|
|
@@ -474,7 +481,7 @@ async def agent_stream_handler(
|
|
|
474
481
|
t0 = time.time()
|
|
475
482
|
|
|
476
483
|
try:
|
|
477
|
-
async for chunk in stream_sse_func(payload, ctx
|
|
484
|
+
async for chunk in stream_sse_func(payload, ctx):
|
|
478
485
|
yield chunk
|
|
479
486
|
except asyncio.CancelledError:
|
|
480
487
|
logger.info(f"Agent stream cancelled for run_id: {run_id}")
|
|
@@ -517,15 +524,17 @@ async def workflow_stream_handler(
|
|
|
517
524
|
sse_event_func: Callable,
|
|
518
525
|
error_classifier: ErrorClassifier,
|
|
519
526
|
register_task_func: Callable[[str, asyncio.Task], None],
|
|
520
|
-
|
|
527
|
+
run_opt: Optional[RunOpt] = None,
|
|
521
528
|
) -> AsyncGenerator[str, None]:
|
|
529
|
+
if run_opt is None:
|
|
530
|
+
run_opt = RunOpt()
|
|
522
531
|
task = asyncio.current_task()
|
|
523
532
|
if task:
|
|
524
533
|
register_task_func(run_id, task)
|
|
525
534
|
logger.info(f"Registered workflow streaming task for run_id: {run_id}")
|
|
526
535
|
|
|
527
536
|
try:
|
|
528
|
-
async for chunk in stream_sse_func(payload, ctx,
|
|
537
|
+
async for chunk in stream_sse_func(payload, ctx, run_opt):
|
|
529
538
|
yield chunk
|
|
530
539
|
except asyncio.CancelledError:
|
|
531
540
|
logger.info(f"Workflow stream cancelled for run_id: {run_id}")
|
|
@@ -10,13 +10,13 @@ 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=
|
|
13
|
+
coze_coding_utils/helper/stream_runner.py,sha256=K446dPUdwemA0_FwbtyscQz5hGmhRxUCG0XYUTFVSI8,24329
|
|
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
|
|
17
17
|
coze_coding_utils/log/err_trace.py,sha256=iwt5g8-AX0N2KuUpuLXjk5PocL6NZdAGddJh27Sxp_o,2954
|
|
18
18
|
coze_coding_utils/log/loop_trace.py,sha256=68sI1AHKd8oeLti-7trBygTZqIiUu_TcHt93DLItkXQ,2129
|
|
19
|
-
coze_coding_utils/log/node_log.py,sha256=
|
|
19
|
+
coze_coding_utils/log/node_log.py,sha256=cqx0l8hgDj8adydj5lYQbpnFwyd3k_QyNeq43Qsvl1I,18011
|
|
20
20
|
coze_coding_utils/log/parser.py,sha256=XuzOkO8SzTbBP_XL1RMFzjqG4IG0iSRStAA2M6latt8,9728
|
|
21
21
|
coze_coding_utils/log/write_log.py,sha256=lDu4_Tjk6eYtWBXI-OOYLqe5ejqTtGnDcrH6M7fMQ2A,7223
|
|
22
22
|
coze_coding_utils/messages/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -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.
|
|
36
|
-
coze_coding_utils-0.2.
|
|
37
|
-
coze_coding_utils-0.2.
|
|
38
|
-
coze_coding_utils-0.2.
|
|
35
|
+
coze_coding_utils-0.2.4.dist-info/METADATA,sha256=clx-L3s4LykCyhZxHNxKCc_Ip_u1AY3cpdyxqQkVL74,977
|
|
36
|
+
coze_coding_utils-0.2.4.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
37
|
+
coze_coding_utils-0.2.4.dist-info/licenses/LICENSE,sha256=lzckZhAjHlpSJcWvppoST095IHFpBwKiB2pKcBv7vP4,1078
|
|
38
|
+
coze_coding_utils-0.2.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|