Add confirmation function and re-execution logic in AIProcessor; clean up web server request handling
This commit is contained in:
		@@ -37,6 +37,8 @@ class AIProcessor:
 | 
				
			|||||||
    # -------------------------- main entry -------------------------- #
 | 
					    # -------------------------- main entry -------------------------- #
 | 
				
			||||||
    def process(self, prompt: str, img_data: str | bytes | None = None) -> str | list[str | dict]:
 | 
					    def process(self, prompt: str, img_data: str | bytes | None = None) -> str | list[str | dict]:
 | 
				
			||||||
        outputs = []  # type: list[str | dict]
 | 
					        outputs = []  # type: list[str | dict]
 | 
				
			||||||
 | 
					        reexec = True
 | 
				
			||||||
 | 
					        nextsteps = ""
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            self.session.messages.append(
 | 
					            self.session.messages.append(
 | 
				
			||||||
                aic.Message(role="user", content=prompt, image=img_data)
 | 
					                aic.Message(role="user", content=prompt, image=img_data)
 | 
				
			||||||
@@ -51,6 +53,9 @@ class AIProcessor:
 | 
				
			|||||||
            tool_calls = getattr(response.choices[0].message, "tool_calls", None)
 | 
					            tool_calls = getattr(response.choices[0].message, "tool_calls", None)
 | 
				
			||||||
            if tool_calls:
 | 
					            if tool_calls:
 | 
				
			||||||
                for tc in tool_calls:
 | 
					                for tc in tool_calls:
 | 
				
			||||||
 | 
					                    if tc.function.name == "confirm":
 | 
				
			||||||
 | 
					                        reexec = False
 | 
				
			||||||
 | 
					                        nextsteps = tc.function.arguments.get("goal", "")
 | 
				
			||||||
                    r = ai.compute._execute(
 | 
					                    r = ai.compute._execute(
 | 
				
			||||||
                        name=tc.function.name,
 | 
					                        name=tc.function.name,
 | 
				
			||||||
                        args=json.loads(tc.function.arguments),
 | 
					                        args=json.loads(tc.function.arguments),
 | 
				
			||||||
@@ -72,6 +77,16 @@ class AIProcessor:
 | 
				
			|||||||
            self.session.messages.append(
 | 
					            self.session.messages.append(
 | 
				
			||||||
                aic.Message(role="assistant", content=output_text)
 | 
					                aic.Message(role="assistant", content=output_text)
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # re-execute if needed
 | 
				
			||||||
 | 
					            if reexec:
 | 
				
			||||||
 | 
					                img = ai.compute.screenshot_to_base64(
 | 
				
			||||||
 | 
					                    ai.compute.take_screenshot()
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                outputs.append(
 | 
				
			||||||
 | 
					                    *self.process(nextsteps, img)
 | 
				
			||||||
 | 
					                )
 | 
				
			||||||
            return outputs
 | 
					            return outputs
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            traceback.print_exc()
 | 
					            traceback.print_exc()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -92,6 +92,23 @@ FUNCTIONS = [
 | 
				
			|||||||
                "required": ["nextsteps"],
 | 
					                "required": ["nextsteps"],
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        "type": "function",
 | 
				
			||||||
 | 
					        "function": {
 | 
				
			||||||
 | 
					            "name": "confirm",
 | 
				
			||||||
 | 
					            "description": "Confirm that the task is completed and no further actions are needed. ONLY execute this when no other functions/actions are needed. This can be the only function called.",
 | 
				
			||||||
 | 
					            "parameters": {
 | 
				
			||||||
 | 
					                "type": "object",
 | 
				
			||||||
 | 
					                "properties": {
 | 
				
			||||||
 | 
					                    "goal": {
 | 
				
			||||||
 | 
					                        "type": "string",
 | 
				
			||||||
 | 
					                        "description": "The goal to achieve/that was achieved."
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "required": ["goal"],
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,8 +27,6 @@ class WebServerApp:
 | 
				
			|||||||
            # Process the data as needed
 | 
					            # Process the data as needed
 | 
				
			||||||
            prompt = data.get('prompt', '')
 | 
					            prompt = data.get('prompt', '')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if not prompt:
 | 
					            if not prompt:
 | 
				
			||||||
                return jsonify({"error": "No prompt provided"}), 400
 | 
					                return jsonify({"error": "No prompt provided"}), 400
 | 
				
			||||||
            img_data = None
 | 
					            img_data = None
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user