by-framework 0.2.2.dev9__py3-none-any.whl → 0.2.2.dev10__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.
by_framework/__init__.py CHANGED
@@ -8,6 +8,7 @@ from `GatewayWorker` and running `run_worker`.
8
8
  from .admin import WorkerManager
9
9
  from .client.byai_client import ByaiGatewayClient
10
10
  from .client.client import (
11
+ CancelSessionResponse,
11
12
  CancelTaskResponse,
12
13
  DataStreamEntry,
13
14
  GatewayClient,
@@ -148,6 +149,7 @@ __all__ = [
148
149
  "DataStreamEntry",
149
150
  "SendMessageResponse",
150
151
  "CancelTaskResponse",
152
+ "CancelSessionResponse",
151
153
  "run_worker",
152
154
  "logger",
153
155
  "setup_logging",
@@ -37,6 +37,7 @@ from by_framework.core.protocol.commands import (
37
37
  from by_framework.core.protocol.data_message import DataMessage
38
38
  from by_framework.core.protocol.message_header import MessageHeader
39
39
  from by_framework.core.protocol.responses import (
40
+ CancelSessionResponse,
40
41
  CancelTaskResponse,
41
42
  ExecutionStatus,
42
43
  SendMessageResponse,
@@ -581,6 +582,99 @@ class GatewayClient:
581
582
  cancelled_count=len(to_cancel),
582
583
  )
583
584
 
585
+ async def cancel_session(
586
+ self,
587
+ session_id: str,
588
+ reason: str = "",
589
+ requested_by: str = "client",
590
+ cancel_mode: str = CancelMode.GRACEFUL,
591
+ ) -> CancelSessionResponse:
592
+ """Cancel every active execution registered under a session.
593
+
594
+ Unlike cancel_task, this does not require a starting message_id:
595
+ it cancels the whole session's active executions in one call.
596
+ """
597
+ if self.registry is None:
598
+ raise ValueError("GatewayClient requires a WorkerRegistry to cancel tasks")
599
+
600
+ all_executions = await self.registry.get_all_session_executions(session_id)
601
+
602
+ if not all_executions:
603
+ return CancelSessionResponse(
604
+ success=False,
605
+ session_id=session_id,
606
+ status=ExecutionStatus.NOT_FOUND,
607
+ timestamp=int(time.time() * 1000),
608
+ cancelled_count=0,
609
+ already_finished_count=0,
610
+ )
611
+
612
+ terminal_states = {"COMPLETED", "FAILED", "CANCELLED"}
613
+ to_cancel = [
614
+ node
615
+ for node in all_executions
616
+ if node.get("status", "") not in terminal_states
617
+ ]
618
+ terminal_nodes = [
619
+ node for node in all_executions if node.get("status", "") in terminal_states
620
+ ]
621
+
622
+ # Flag terminal executions so a still-running descendant can't wake
623
+ # them back up via callback, without changing their status.
624
+ for node in terminal_nodes:
625
+ await self.registry.mark_cancel_requested(
626
+ node.get("execution_id", ""), session_id, reason
627
+ )
628
+
629
+ if not to_cancel:
630
+ return CancelSessionResponse(
631
+ success=False,
632
+ session_id=session_id,
633
+ status=ExecutionStatus.ALREADY_FINISHED,
634
+ timestamp=int(time.time() * 1000),
635
+ cancelled_count=0,
636
+ already_finished_count=len(terminal_nodes),
637
+ )
638
+
639
+ for node in to_cancel:
640
+ node_execution_id = node.get("execution_id", "")
641
+ node_worker_id = node.get("worker_id", "")
642
+ node_message_id = node.get("message_id", "")
643
+
644
+ await self.registry.mark_execution_cancelling(
645
+ node_execution_id, session_id, reason
646
+ )
647
+
648
+ if node_worker_id:
649
+ cancel_command = CancelTaskCommand(
650
+ header=MessageHeader(
651
+ message_id=f"{CANCEL_MESSAGE_ID_PREFIX}{uuid.uuid4().hex[:8]}",
652
+ session_id=session_id,
653
+ trace_id=node.get("trace_id") or uuid.uuid4().hex,
654
+ target_agent_type=node.get("target_agent_type", ""),
655
+ parent_message_id=node_message_id,
656
+ ),
657
+ target_message_id=node_message_id,
658
+ target_execution_id=node_execution_id,
659
+ target_worker_id=node_worker_id,
660
+ reason=reason,
661
+ requested_by=requested_by,
662
+ cancel_mode=cancel_mode,
663
+ )
664
+ await self.redis.xadd(
665
+ RedisKeys.worker_ctrl_stream(node_worker_id),
666
+ cancel_command.to_redis_payload(),
667
+ )
668
+
669
+ return CancelSessionResponse(
670
+ success=True,
671
+ session_id=session_id,
672
+ status=ExecutionStatus.CANCEL_REQUESTED,
673
+ timestamp=int(time.time() * 1000),
674
+ cancelled_count=len(to_cancel),
675
+ already_finished_count=len(terminal_nodes),
676
+ )
677
+
584
678
  async def send_message(
585
679
  self,
586
680
  target_agent_type: str,
@@ -691,7 +785,28 @@ class GatewayClient:
691
785
  content=params["content"],
692
786
  extra_payload=params["extra_payload"],
693
787
  )
694
- execution_id = f"{EXECUTION_ID_PREFIX}{uuid.uuid4().hex[:8]}"
788
+
789
+ # A RESUME reuses the message_id of the original AskAgentCommand so the
790
+ # worker can look the suspended execution back up. Reuse its
791
+ # execution_id too, and skip re-initializing the registry record for
792
+ # it below -- initialize_execution() would otherwise overwrite the
793
+ # message_id -> execution_id mapping and detach this resume from the
794
+ # execution it's meant to continue.
795
+ resumed_execution = None
796
+ if (
797
+ params["action_type"] == ActionType.RESUME.value
798
+ and self.registry
799
+ and hasattr(self.registry, "get_execution_by_message_id")
800
+ ):
801
+ resumed_execution = await self.registry.get_execution_by_message_id(
802
+ message_id, session_id=params["session_id"]
803
+ )
804
+
805
+ execution_id = (
806
+ resumed_execution["execution_id"]
807
+ if resumed_execution
808
+ else f"{EXECUTION_ID_PREFIX}{uuid.uuid4().hex[:8]}"
809
+ )
695
810
 
696
811
  # 3. Resolve route and optionally probe agent type/liveness
697
812
  should_dispatch_control = True
@@ -837,8 +952,14 @@ class GatewayClient:
837
952
  error_code=ExecutionStatus.ERR_AGENT_TYPE_UNAVAILABLE,
838
953
  )
839
954
 
840
- # Initialize execution tracking
841
- if self.registry and hasattr(self.registry, "initialize_execution"):
955
+ # Initialize execution tracking. Skipped for a RESUME that reattached
956
+ # to an existing execution -- that execution is already tracked, and
957
+ # re-initializing it here would overwrite its message_id mapping.
958
+ if (
959
+ not resumed_execution
960
+ and self.registry
961
+ and hasattr(self.registry, "initialize_execution")
962
+ ):
842
963
  try:
843
964
  await self.registry.initialize_execution(
844
965
  {
@@ -107,6 +107,22 @@ class DefaultSseLayoutBuilder(DataLayoutBuilder):
107
107
  parent_order_id: Optional[str] = None,
108
108
  ) -> Dict[str, Any]:
109
109
  """Build default BaiYing-compatible user input form payload."""
110
+ try:
111
+ parsed_prompt = json.loads(prompt)
112
+ except (json.JSONDecodeError, TypeError):
113
+ parsed_prompt = None
114
+
115
+ # Structured question prompts are already in the format expected by clients.
116
+ if isinstance(parsed_prompt, dict) and "questions" in parsed_prompt:
117
+ return self.build(
118
+ content=prompt,
119
+ role="assistant",
120
+ content_type=SseReasonMessageType.ask_user_question.value,
121
+ source_agent_type=source_agent_type,
122
+ order_id=order_id,
123
+ parent_order_id=parent_order_id,
124
+ )
125
+
110
126
  input_form = {
111
127
  "formStatus": 0,
112
128
  "pluginMachineFields": [
@@ -26,6 +26,7 @@ from .protocol import (
26
26
  BaiYingMessage,
27
27
  BaiYingMessageRole,
28
28
  BaseCommand,
29
+ CancelSessionResponse,
29
30
  CancelTaskCommand,
30
31
  CancelTaskResponse,
31
32
  DataMessage,
@@ -53,6 +54,7 @@ from .workspace import WorkspaceManager
53
54
  __all__ = [
54
55
  "SendMessageResponse",
55
56
  "CancelTaskResponse",
57
+ "CancelSessionResponse",
56
58
  "BaiYingMessage",
57
59
  "BaiYingMessageRole",
58
60
  "MessageContent",
@@ -65,6 +65,7 @@ from .message import (
65
65
  )
66
66
  from .message_header import MessageHeader
67
67
  from .responses import (
68
+ CancelSessionResponse,
68
69
  CancelTaskResponse,
69
70
  CancelTaskResponseDict,
70
71
  SendMessageResponse,
@@ -81,6 +82,7 @@ from .results import (
81
82
  __all__ = [
82
83
  "SendMessageResponse",
83
84
  "CancelTaskResponse",
85
+ "CancelSessionResponse",
84
86
  "SendMessageResponseDict",
85
87
  "CancelTaskResponseDict",
86
88
  "AgentTaskResult",
@@ -33,6 +33,7 @@ class SseReasonMessageType(str, Enum):
33
33
  think_code_result = "3007" # thinking process code execution result
34
34
  task_finished = "3009" # task finished
35
35
  task_user_input = "3013" # user input
36
+ ask_user_question = "3014" # ask user question
36
37
  task_create_file = "3010" # create file
37
38
  task_title = "3011" # task title
38
39
  agent_card = "2015" # agent card
@@ -92,3 +92,14 @@ class CancelTaskResponse:
92
92
  timestamp: int
93
93
  error: str = ""
94
94
  cancelled_count: int = 0
95
+
96
+
97
+ @dataclass(frozen=True)
98
+ class CancelSessionResponse:
99
+ success: bool
100
+ session_id: str
101
+ status: str
102
+ timestamp: int
103
+ cancelled_count: int = 0
104
+ already_finished_count: int = 0
105
+ error: str = ""
@@ -1011,7 +1011,8 @@ class WorkerRegistry:
1011
1011
  old_execution = dict(current)
1012
1012
  current["status"] = status
1013
1013
  now = int(time.time() * 1000)
1014
- current["finished_at"] = now
1014
+ if is_terminal_state(status):
1015
+ current["finished_at"] = now
1015
1016
  current["updated_at"] = now
1016
1017
  if completion:
1017
1018
  for key, value in completion.items():
@@ -505,6 +505,16 @@ class WorkerRunner:
505
505
  header.message_id, session_id=header.session_id
506
506
  )
507
507
 
508
+ if isinstance(command, ResumeCommand) and existing_execution is None:
509
+ logger.warning(
510
+ "[%s] ResumeCommand did not resolve to an existing execution "
511
+ "(message_id=%s, session_id=%s); starting a new, disconnected "
512
+ "execution instead of continuing the suspended one.",
513
+ self.worker.worker_id,
514
+ header.message_id,
515
+ header.session_id,
516
+ )
517
+
508
518
  # Skip terminal state replays
509
519
  if (
510
520
  existing_execution
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: by-framework
3
- Version: 0.2.2.dev9
3
+ Version: 0.2.2.dev10
4
4
  Summary: 分布式 Agent 调度框架
5
5
  License-File: LICENSE
6
6
  Requires-Python: >=3.12
@@ -1,23 +1,23 @@
1
- by_framework/__init__.py,sha256=5Ff4xfX-TNXcp4RMoBLQCWuehPMLpNRHwZrXofgFx5A,5300
1
+ by_framework/__init__.py,sha256=aNq4eeO-5WfFC86JJlfDgCvI11vk90ZWtL3yaREcJl4,5356
2
2
  by_framework/__main__.py,sha256=_vIEieJzSHfZnPp-zna_hbVtWQUgE0I3mrWD41WzXiA,2561
3
3
  by_framework/admin/__init__.py,sha256=MKGQBtKk8YdnMrZ5gkzEemMl4iL8OfeFhEV6HdWR2XI,138
4
4
  by_framework/admin/cli.py,sha256=KBmTHONKdb6BeskW6d3DL6GRnL91xen0PJ80JB0h0lc,11710
5
5
  by_framework/admin/worker_manager.py,sha256=ihXRMlFJAR74T2JSh3xTNCDjzAElWd0-_yVDAZwSTJU,5924
6
6
  by_framework/client/__init__.py,sha256=kGP7uoXIlTJuiJ2Cgl1Q5wqYjnMA4SZqoa7Q7BhQ5JQ,272
7
7
  by_framework/client/byai_client.py,sha256=bB1NyKLvVW82bXefD_3zymqZ1vk2O925CIbxOho9Qww,2426
8
- by_framework/client/client.py,sha256=T_5ahX9enmsaXm-xBGvwpMx-yALzO7wOTgY-qAVj0Q4,40342
8
+ by_framework/client/client.py,sha256=hT_IRR51jsu3MIPFbZDmmL9xQHtNUFJCADBep9iFyQI,45157
9
9
  by_framework/common/__init__.py,sha256=EkfIiVdmb8SCpi7OOUm6Hu84B7Wzekb-TG2hbvD_R1w,1685
10
10
  by_framework/common/config.py,sha256=RGe5Ce-uUgpZv1bgkdT4kCRcaIpaYhzskyvQy17Qn_o,5043
11
11
  by_framework/common/constants.py,sha256=xUp6Nvx31EH7EzkxDNBzXD0GIE9FpPnPmuVbL-BAAoI,22089
12
- by_framework/common/emitter.py,sha256=Mcp6vglFlS7_JVsLd_KVKvV49TKjoyoN4eCM_tm4I8s,11310
12
+ by_framework/common/emitter.py,sha256=bobdekSsA3vnMu8TMHonkjfgK-awW_p11dgcTU_3mQw,11940
13
13
  by_framework/common/exceptions.py,sha256=NKQC7ueLZ4-gVTRz3SFRZNoPHsayu6jxi3-YlW0n_jg,962
14
14
  by_framework/common/logger.py,sha256=DX6S7U5CLcCQIUQ3IWqeeDnSC7QGuHKYaEhiKvKrBpk,5823
15
15
  by_framework/common/redis_client.py,sha256=JfYE-QutI9G27dbehzvLw0LziTM1gHmG4tdVjxd0IoA,4668
16
- by_framework/core/__init__.py,sha256=bEaOfjGoCF0WacR_PoYSb5kUqJv-g59pngdnEE7fJzo,2070
16
+ by_framework/core/__init__.py,sha256=cGG93S1gzd1N64JaTCVD7JLV0ZzbF92Q9C6Sog0yDSE,2126
17
17
  by_framework/core/availability.py,sha256=ZME5S002eNzNpzaW_QuWw8AmUN1JelUxwCt2U3PPAeQ,17624
18
18
  by_framework/core/delivery_gate.py,sha256=yijlCOw8zEou1HCyQzwRHDJZtLBsUnXtQ3x1FZsc4yU,2012
19
19
  by_framework/core/discovery.py,sha256=12mAnPzJph0zqlfJwu8k-IN5AKc7dpC0sXToBGLU2Fw,13116
20
- by_framework/core/registry.py,sha256=Qp_ghu47Wzjf2AKDUpRmzipPUGsbnc6vyB-wb4ubkGI,52864
20
+ by_framework/core/registry.py,sha256=bpthWbuVrUIBO1Wtz5F3A2s2GAon9zX_6FR9zn7fxAw,52906
21
21
  by_framework/core/wakeup_controller.py,sha256=0v3Fr69XzIKO0fesYUPvQvsGLYt0f648Xq7XEM_jk6c,5545
22
22
  by_framework/core/workspace.py,sha256=R9mxghDh_INtPOIXHilBamAIue8jZMrlQoQRI02KSxw,3980
23
23
  by_framework/core/extensions/__init__.py,sha256=MvPh029FXvkR1Lk4bZxiq9SfzUQ57paUfoD17-whvn4,1032
@@ -26,7 +26,7 @@ by_framework/core/extensions/agent_config_audit.py,sha256=Ax67XF2dg_oqz8GvCQ5vsu
26
26
  by_framework/core/extensions/plugin.py,sha256=rmV4wjf_x-v8E55s9_bDQPyvsqZT4ebZ2fnScbmUGkg,9236
27
27
  by_framework/core/extensions/registry.py,sha256=s2UYEOvNzZ3Q8CH1vPBCUw3hhOfQTJ6GKTJ2aXPxVCY,25314
28
28
  by_framework/core/extensions/trace_provider.py,sha256=ShM-aJWfenHD006m5sj3M3fi5zZOiTaQ9MZyKV9yv2o,582
29
- by_framework/core/protocol/__init__.py,sha256=oQeO2E9DD8Wz2qwvBrKOlm4SgIqnEBlpZwa-BuijCnQ,3388
29
+ by_framework/core/protocol/__init__.py,sha256=wxpD8UMYIYmpXtwZZvT9FQZA6hSjHyoEUZ2kFw1_McA,3444
30
30
  by_framework/core/protocol/action_type.py,sha256=5uhwzpaf_PwYMPharJHoX_oc4Cf1dZruCpJ8eyUdwXQ,1112
31
31
  by_framework/core/protocol/agent_state.py,sha256=CtqwisNWuPy91FEPXjQrR3r0jLeOrUF1huu23ODnEPo,1919
32
32
  by_framework/core/protocol/byai_codec.py,sha256=BqCCYS_uEXOpAHogrNl7-3Tce9HBxMKRpO0ccQN80SA,3185
@@ -34,14 +34,14 @@ by_framework/core/protocol/byai_command.py,sha256=FJjU4H9b7S5Q59OZqDpVu3JBln7_P9
34
34
  by_framework/core/protocol/byai_types.py,sha256=szP6yONsMwtErZZUOOgLRnwqIJnPYKM7qcqrgwkEyIc,187
35
35
  by_framework/core/protocol/commands.py,sha256=5MFP0EjElbPBAOUE_D0gErUJsHD06EuWCkmqyLsRX8U,11144
36
36
  by_framework/core/protocol/content_codec.py,sha256=E8uHhRU6jlH16MQ3UmA8jPexm71l3HKxpquF9xJk04o,643
37
- by_framework/core/protocol/content_type.py,sha256=fjzNIQQ_-1z7dEMsyEEz7imv1fTBwO1OFyU2H6sk1d4,1217
37
+ by_framework/core/protocol/content_type.py,sha256=fagsdHPIoPyWwmgYfb3JzP52olxDDzpGbmQ9q3bmQxE,1269
38
38
  by_framework/core/protocol/data_message.py,sha256=UEK9W1lIt_6DLdu0o_CW5YoQJrsEjRUHHEtH2syRO-A,1290
39
39
  by_framework/core/protocol/data_shapes.py,sha256=nA6OnfwXRJB1lmwcME3K5TEn_IwmShmBOlYSmOF7y3k,2245
40
40
  by_framework/core/protocol/event_type.py,sha256=no7rh1Mx_mhyqqPzEM4aK-W8JB8NwzyOdWN6R_CqaPw,1106
41
41
  by_framework/core/protocol/events.py,sha256=SVUXWWVIgxC9VuKjPE1qnvWJgKhWqY48pOHKD0HLcRg,1594
42
42
  by_framework/core/protocol/message.py,sha256=sBLm374jsCNa5UPCBJK7xgxJBw3CjrNsoEAnPSp3rAc,2374
43
43
  by_framework/core/protocol/message_header.py,sha256=udZkcUv-NEz2j-vpxGglrHWaIJy6k3ZJ9M5ajtL3WQo,3140
44
- by_framework/core/protocol/responses.py,sha256=fJhWIgkWoIbrNpS0sHWnVwPsS0pNCav-qGMu9AiTK9g,2376
44
+ by_framework/core/protocol/responses.py,sha256=Q2xNFL1zPAGR0OpAB4uxu6iHHKUMIsBjc7yren4c0UA,2589
45
45
  by_framework/core/protocol/results.py,sha256=xRr0FhTyuck0ANSOD8yuozzJoVOHFp0G2iSK9V69Hec,5297
46
46
  by_framework/core/runtime/__init__.py,sha256=mYbe9D2BvZ3eYTjvU64CZyHLuMoRUU7DGwAIQXf7KR4,991
47
47
  by_framework/core/runtime/agent_config_manager.py,sha256=QpBkBXqWHEgqx8VXaQnRWJsG1TbHNJMHbLBMv0tQqJ8,8301
@@ -88,12 +88,12 @@ by_framework/worker/byai_worker.py,sha256=0NoRqzuTG_MPMxVbZ1ueeOrqI0H8-Adxh7dM3v
88
88
  by_framework/worker/context.py,sha256=XTNeTAhXvzS2OMDRCDKVD0s3uQscE1rFo9eImUw773w,44747
89
89
  by_framework/worker/heartbeat.py,sha256=trHDS8FUALsA6jyzDPa087hpr8_2vY8ZUFn0ld1kI2o,15465
90
90
  by_framework/worker/processor.py,sha256=D5GcV1T21z4mwJnrZFDRNsxdkhW2WZ3TgpygSKKJUfg,8008
91
- by_framework/worker/runner.py,sha256=ZuuhzdXB4oIjSaNyJCjOIYhzvw6L5N7X6kPzvrbWET8,43719
91
+ by_framework/worker/runner.py,sha256=YoFS1K_8wVtCl6PMnhK2r66ujgWMplPGNwmGCCrqAO8,44214
92
92
  by_framework/worker/worker.py,sha256=_RmNkvHEN5kDPlqbHtVQhkhgiKx37xjnCxNkf_tvASM,35441
93
93
  by_framework/worker/sandbox/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
94
94
  by_framework/worker/sandbox/hook_sandbox.py,sha256=XXSdH5YGX814LvV03vkq-zn5DVIBPYYt2NhRv1lT1Bk,2329
95
- by_framework-0.2.2.dev9.dist-info/METADATA,sha256=qmdF2bXuu0_IfQI5_QazjrVOxd8EjIHiqnm0PiIVZVo,26533
96
- by_framework-0.2.2.dev9.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
97
- by_framework-0.2.2.dev9.dist-info/entry_points.txt,sha256=5t6kgMjkR0UgJTpOsbuQDo0fOf7zeXjQW9DhkxfAGFc,56
98
- by_framework-0.2.2.dev9.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
99
- by_framework-0.2.2.dev9.dist-info/RECORD,,
95
+ by_framework-0.2.2.dev10.dist-info/METADATA,sha256=Vg3-Fno6zjGZCliNPIIEBYRoVgiTrRMrKWSua26Vg3U,26534
96
+ by_framework-0.2.2.dev10.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
97
+ by_framework-0.2.2.dev10.dist-info/entry_points.txt,sha256=5t6kgMjkR0UgJTpOsbuQDo0fOf7zeXjQW9DhkxfAGFc,56
98
+ by_framework-0.2.2.dev10.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
99
+ by_framework-0.2.2.dev10.dist-info/RECORD,,