MCPcopy Index your code
hub / github.com/OpenBMB/ChatDev / execute

Method execute

runtime/node/executor/python_executor.py:35–79  ·  view source on GitHub ↗
(self, node: Node, inputs: List[Message])

Source from the content-addressed store, hash-verified

33 COUNTER_KEY = "python_node_run_counters"
34
35 def execute(self, node: Node, inputs: List[Message]) -> List[Message]:
36 if node.node_type != "python":
37 raise ValueError(f"Node {node.id} is not a python node")
38
39 workspace = self._ensure_workspace_root()
40 last_message = inputs[-1] if inputs else None
41 code_payload = self._extract_code(last_message)
42 if not code_payload:
43 return [self._build_failure_message(
44 node,
45 workspace,
46 error_text="No executable code segment found",
47 )]
48
49 script_path = self._write_script_file(node, workspace, code_payload)
50 config = node.as_config(PythonRunnerConfig)
51 if not config:
52 raise ValueError(f"Node {node.id} missing PythonRunnerConfig")
53
54 result = self._run_process(config, script_path, workspace, node)
55 metadata = {
56 "workspace": str(workspace),
57 "script_path": str(script_path),
58 }
59 if result.success:
60 if result.stderr:
61 self.log_manager.debug(
62 f"Python node {node.id} stderr", node_id=node.id, details={"stderr": result.stderr}
63 )
64 return [self._build_message(
65 role=MessageRole.ASSISTANT,
66 content=result.stdout,
67 source=node.id,
68 metadata=metadata,
69 )]
70
71 error_text = result.error or "Script execution failed"
72 return [self._build_failure_message(
73 node,
74 workspace,
75 error_text=error_text,
76 exit_code=result.exit_code,
77 stderr=result.stderr,
78 script_path=script_path,
79 )]
80
81 def _ensure_workspace_root(self) -> Path:
82 root = self.context.global_state.setdefault(self.WORKSPACE_KEY, None)

Callers

nothing calls this directly

Calls 8

_extract_codeMethod · 0.95
_write_script_fileMethod · 0.95
_run_processMethod · 0.95
_build_messageMethod · 0.80
as_configMethod · 0.45
debugMethod · 0.45

Tested by

no test coverage detected