(
state: WorkflowState,
*,
context: WorkflowContext,
agent: Any,
tools: list[Any],
sender_name: str,
)
| 50 | |
| 51 | |
| 52 | async def agent_node( |
| 53 | state: WorkflowState, |
| 54 | *, |
| 55 | context: WorkflowContext, |
| 56 | agent: Any, |
| 57 | tools: list[Any], |
| 58 | sender_name: str, |
| 59 | ) -> dict[str, Any]: |
| 60 | try: |
| 61 | from langchain.agents import AgentExecutor |
| 62 | except ImportError as exc: |
| 63 | raise RuntimeError( |
| 64 | "LangChain is required to execute workflow agent nodes." |
| 65 | ) from exc |
| 66 | |
| 67 | executor = AgentExecutor( |
| 68 | agent=agent, |
| 69 | tools=tools, |
| 70 | verbose=True, |
| 71 | handle_parsing_errors=True, |
| 72 | max_iterations=context.max_iterations_for(sender_name), |
| 73 | return_intermediate_steps=True, |
| 74 | ) |
| 75 | result = await executor.ainvoke({"input": context.problem}) |
| 76 | |
| 77 | message_str = "" |
| 78 | history_entries: list[str] = [] |
| 79 | |
| 80 | intermediate_steps = result.get("intermediate_steps", []) |
| 81 | if intermediate_steps: |
| 82 | tool_input_snapshot = "" |
| 83 | tool_output_snapshot = "" |
| 84 | |
| 85 | for index, step in enumerate(intermediate_steps): |
| 86 | action, observation = step |
| 87 | tool_input_snapshot = str(getattr(action, "tool_input", "")) |
| 88 | tool_output_snapshot = str(observation) |
| 89 | agent_output = str(getattr(action, "log", "")) |
| 90 | history_entries.append(f"{sender_name}{index}_{agent_output}") |
| 91 | history_entries.append(f"{sender_name}{index}_response_{tool_output_snapshot}") |
| 92 | message_str += agent_output + tool_output_snapshot |
| 93 | |
| 94 | context.history.extend(history_entries) |
| 95 | if tool_input_snapshot: |
| 96 | context.commands.append(tool_input_snapshot) |
| 97 | if sender_name == "Inquire" and state["vulns"]: |
| 98 | state["vulns"][0]["information"] = tool_output_snapshot |
| 99 | context.append_information(tool_output_snapshot) |
| 100 | message = _ai_message(message_str) |
| 101 | else: |
| 102 | output = str(result.get("output", "")) |
| 103 | context.history.append(output) |
| 104 | message = _ai_message(output) |
| 105 | |
| 106 | return { |
| 107 | "message": [message], |
| 108 | "sender": sender_name, |
| 109 | "vulns": state["vulns"], |
nothing calls this directly
no test coverage detected