diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index 70380ccc..82ad05ba 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -126,26 +126,30 @@ def get_hook(self, name: str, api_key: str = ""): raise Exception('No nodes found in the workflow') def format_data(self, inputted, api_key: str, workflow_name): - work_flow = self.get_workflows(100, api_key=api_key, workflow_name=workflow_name) - print("Got workflow") - values = [] - if isinstance(work_flow, dict) and 'nodes' in work_flow: - for node in work_flow['nodes']: - if node['name'] == 'n8n Form Trigger': - values = node['parameters']['formFields']['values'] - data = {} - inputted = json.loads(inputted) - inputted = dict(inputted) - for i, value in enumerate(values): - field_name = 'field-' + str(i) - data[value['fieldLabel']] = field_name - new_data = {} - for k, v in inputted.items(): - if isinstance(v, list): - new_data[data[k]] = json.dumps(v) - else: - new_data[data[k]] = v - return new_data + try: + work_flow = self.get_workflows(100, api_key=api_key, workflow_name=workflow_name) + print("Got workflow") + values = [] + if isinstance(work_flow, dict) and 'nodes' in work_flow: + for node in work_flow['nodes']: + if node['name'] == 'n8n Form Trigger': + values = node['parameters']['formFields']['values'] + data = {} + # Check if inputted is already a dict, if not, try to load it as JSON + if not isinstance(inputted, dict): + inputted = json.loads(inputted) + for i, value in enumerate(values): + field_name = 'field-' + str(i) + data[value['fieldLabel']] = field_name + new_data = {} + for k, v in inputted.items(): + if isinstance(v, list): + new_data[data[k]] = json.dumps(v) + else: + new_data[data[k]] = v + return new_data + except Exception as e: + print("Error in format_data: ", e) # TODO: activate and disactivate workflows diff --git a/ai_ta_backend/main.py b/ai_ta_backend/main.py index 40cb34ba..1ea0237a 100644 --- a/ai_ta_backend/main.py +++ b/ai_ta_backend/main.py @@ -17,7 +17,7 @@ from flask_cors import CORS from flask_executor import Executor from posthog import Posthog -import ray +# import ray import sentry_sdk from ai_ta_backend.canvas import CanvasAPI @@ -45,7 +45,7 @@ # load API keys from globally-availabe .env file load_dotenv() -ray.init() +# ray.init() print("NUM ACTIVE THREADS (top of main):", threading.active_count()) @@ -764,17 +764,17 @@ def switch_workflow() -> Response: abort(400, description=f"Bad request: {e}") -@app.route('/run_flow', methods=['GET']) +@app.route('/run_flow', methods=['POST']) def run_flow() -> Response: """ Run flow for a user and return results. """ - api_key = request.args.get('api_key', default='', type=str) - name = request.args.get('name', default='', type=str) - data = request.args.get('data', default='', type=str) + api_key = request.json.get('api_key', '') + name = request.json.get('name', '') + data = request.json.get('data', '') - print(request.args) + print("Got /run_flow request:", request.json) if api_key == '': # proper web error "400 Bad request"