#!/usr/bin/env python3 """Save all action mappings to a clean JSON file""" import sys import json import re sys.path.insert(0, r'C:\DEV\COPILOT\geutebruck-api\src\api') import grpc from protos import configuration_pb2 from protos import configuration_pb2_grpc def parse_action_string(action_str): """Parse action string into structured format""" # Try to parse as function call: action_name(param1, param2, ...) match = re.match(r'^([^(]+)\(([^)]*)\)$', action_str.strip()) if match: action_name = match.group(1).strip() params_str = match.group(2).strip() # Parse parameters parameters = [] if params_str: params = [p.strip() for p in params_str.split(',')] parameters = params return { "action": action_name, "parameters": parameters } else: # Not a function call format - return as-is return { "action": action_str.strip(), "parameters": [] } def save_action_mappings(): """Save all action mappings to JSON file""" print("Connecting to SDK Bridge...") with grpc.insecure_channel('localhost:50051') as channel: stub = configuration_pb2_grpc.ConfigurationServiceStub(channel) request = configuration_pb2.ReadActionMappingsRequest() try: response = stub.ReadActionMappings(request) # Create clean output with structured actions clean_mappings = [] for idx, mapping in enumerate(response.mappings, 1): parsed_actions = [] for action_str in mapping.actions: parsed_action = parse_action_string(action_str) parsed_actions.append(parsed_action) clean_mappings.append({ "id": idx, "actions": parsed_actions }) output = { "action_mappings": clean_mappings, "total_count": len(clean_mappings), "total_actions": sum(len(m["actions"]) for m in clean_mappings) } # Save to file output_file = "action_mappings_clean.json" with open(output_file, 'w', encoding='utf-8') as f: json.dump(output, f, indent=2, ensure_ascii=False) print(f"\nSaved {output['total_count']} action mappings to: {output_file}") print(f"Total actions: {output['total_actions']}") print(f"\nYou can now edit this file and use it to update the configuration.") return True except grpc.RpcError as e: print(f"\nError: {e.code()}") print(f"Details: {e.details()}") return False if __name__ == '__main__': save_action_mappings()