MCPcopy
hub / github.com/landing-ai/vision-agent / maybe_run_action

Function maybe_run_action

vision_agent/agent/vision_agent_v2.py:100–156  ·  view source on GitHub ↗
(
    coder: AgentCoder,
    action: Optional[str],
    chat: List[AgentMessage],
    code_interpreter: Optional[CodeInterpreter] = None,
)

Source from the content-addressed store, hash-verified

98
99
100def maybe_run_action(
101 coder: AgentCoder,
102 action: Optional[str],
103 chat: List[AgentMessage],
104 code_interpreter: Optional[CodeInterpreter] = None,
105) -> Optional[List[AgentMessage]]:
106 extracted_chat, final_code = extract_conversation(chat)
107 if action == "generate_or_edit_vision_code":
108 # there's an issue here because coder.generate_code will send it's code_context
109 # to the outside user via it's update_callback, but we don't necessarily have
110 # access to that update_callback here, so we re-create the message using
111 # format_code_context.
112 context = coder.generate_code(extracted_chat, code_interpreter=code_interpreter)
113
114 if isinstance(context, CodeContext):
115 return [
116 AgentMessage(role="coder", content=format_code_context(context)),
117 AgentMessage(
118 role="final_observation", content=context.test_result.text()
119 ),
120 ]
121 elif isinstance(context, InteractionContext):
122 return [
123 AgentMessage(
124 role="interaction",
125 content=json.dumps([elt.model_dump() for elt in context.chat]),
126 )
127 ]
128 elif isinstance(context, ErrorContext):
129 return [
130 AgentMessage(role="error_observation", content=context.error),
131 ]
132 elif action == "edit_code":
133 # We don't want to pass code in plan_context.code so the coder will generate
134 # new code from plan_context.plan
135 plan_context = PlanContext(
136 plan="Edit the latest code observed in the fewest steps possible according to the user's feedback."
137 + ("<code>\n" + final_code + "\n</code>" if final_code is not None else ""),
138 instructions=[
139 chat_i.content
140 for chat_i in extracted_chat
141 if chat_i.role == "user" and "<final_code>" not in chat_i.content
142 ],
143 code="",
144 )
145
146 context = coder.generate_code_from_plan(
147 extracted_chat, plan_context, code_interpreter=code_interpreter
148 )
149 return [
150 AgentMessage(role="coder", content=format_code_context(context)),
151 AgentMessage(role="final_observation", content=context.test_result.text()),
152 ]
153 elif action == "view_image":
154 pass
155
156 return None
157

Callers 1

chatMethod · 0.85

Calls 7

AgentMessageClass · 0.90
format_code_contextFunction · 0.90
PlanContextClass · 0.90
extract_conversationFunction · 0.85
textMethod · 0.80
generate_codeMethod · 0.45

Tested by

no test coverage detected