|
| 1 | +""" |
| 2 | +Generic Workflow Generator |
| 3 | +
|
| 4 | +This script creates a workflow from a task description and stores it in the workflow storage. |
| 5 | +Simply modify the TASK_NAME and TASK_DESCRIPTION at the top of this file and run it. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + cd workflows |
| 9 | + uv run python examples/scripts/generate_workflow.py |
| 10 | +
|
| 11 | + Or with custom API key: |
| 12 | + BROWSER_USE_API_KEY=your_key uv run python examples/scripts/generate_workflow.py |
| 13 | +""" |
| 14 | + |
| 15 | +import asyncio |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +from browser_use.llm import ChatBrowserUse |
| 19 | + |
| 20 | +from workflow_use.healing.service import HealingService |
| 21 | +from workflow_use.storage.service import WorkflowStorageService |
| 22 | + |
| 23 | +# ============================================================================ |
| 24 | +# CONFIGURE YOUR TASK HERE |
| 25 | +# ============================================================================ |
| 26 | + |
| 27 | +TASK_NAME = 'Get GitHub Repository Stars' |
| 28 | +TASK_DESCRIPTION = """ |
| 29 | +Go to GitHub, search for the browser-use repository, click on it, |
| 30 | +and extract the star count. |
| 31 | +""" |
| 32 | + |
| 33 | +# ============================================================================ |
| 34 | +# CONFIGURATION OPTIONS |
| 35 | +# ============================================================================ |
| 36 | + |
| 37 | +# Enable variable extraction (uses LLM to identify reusable variables) |
| 38 | +ENABLE_VARIABLE_EXTRACTION = True |
| 39 | + |
| 40 | +# Use deterministic conversion (no LLM for step creation - faster, cheaper) |
| 41 | +USE_DETERMINISTIC_CONVERSION = True |
| 42 | + |
| 43 | +# Storage directory for workflows |
| 44 | +STORAGE_DIR = Path(__file__).parent.parent.parent / 'storage' |
| 45 | + |
| 46 | + |
| 47 | +# ============================================================================ |
| 48 | +# MAIN SCRIPT - DO NOT MODIFY BELOW UNLESS YOU KNOW WHAT YOU'RE DOING |
| 49 | +# ============================================================================ |
| 50 | + |
| 51 | + |
| 52 | +async def generate_and_store_workflow(): |
| 53 | + """Generate a workflow from the task description and store it.""" |
| 54 | + |
| 55 | + print('=' * 80) |
| 56 | + print('WORKFLOW GENERATOR') |
| 57 | + print('=' * 80) |
| 58 | + print(f'\nTask Name: {TASK_NAME}') |
| 59 | + print(f'Task Description: {TASK_DESCRIPTION.strip()}\n') |
| 60 | + |
| 61 | + # Initialize LLM |
| 62 | + print('Step 1: Initializing LLM...') |
| 63 | + llm = ChatBrowserUse(model='bu-latest') |
| 64 | + |
| 65 | + # Create HealingService for workflow generation |
| 66 | + print('Step 2: Setting up workflow generation service...') |
| 67 | + healing_service = HealingService( |
| 68 | + llm=llm, enable_variable_extraction=ENABLE_VARIABLE_EXTRACTION, use_deterministic_conversion=USE_DETERMINISTIC_CONVERSION |
| 69 | + ) |
| 70 | + |
| 71 | + # Generate workflow |
| 72 | + print('\nStep 3: Recording browser interactions and generating workflow...') |
| 73 | + print('(This will open a browser and execute the task)\n') |
| 74 | + |
| 75 | + workflow = await healing_service.generate_workflow_from_prompt( |
| 76 | + prompt=TASK_DESCRIPTION, agent_llm=llm, extraction_llm=llm, use_cloud=True |
| 77 | + ) |
| 78 | + |
| 79 | + print('✅ Workflow generated successfully!\n') |
| 80 | + |
| 81 | + # Initialize storage service |
| 82 | + print('Step 4: Storing workflow...') |
| 83 | + storage = WorkflowStorageService(storage_dir=STORAGE_DIR) |
| 84 | + |
| 85 | + # Save to storage |
| 86 | + metadata = storage.save_workflow( |
| 87 | + workflow=workflow, |
| 88 | + generation_mode='browser_use', |
| 89 | + original_task=TASK_DESCRIPTION.strip(), |
| 90 | + ) |
| 91 | + |
| 92 | + print(f'✅ Workflow saved to storage!\n') |
| 93 | + |
| 94 | + # Display summary |
| 95 | + print('=' * 80) |
| 96 | + print('WORKFLOW SUMMARY') |
| 97 | + print('=' * 80) |
| 98 | + |
| 99 | + print(f'\nWorkflow ID: {metadata.id}') |
| 100 | + print(f'Name: {metadata.name}') |
| 101 | + print(f'Description: {metadata.description}') |
| 102 | + print(f'Version: {metadata.version}') |
| 103 | + print(f'File Path: {metadata.file_path}') |
| 104 | + print(f'Generation Mode: {metadata.generation_mode}') |
| 105 | + print(f'Created At: {metadata.created_at}') |
| 106 | + |
| 107 | + # Show input schema |
| 108 | + if workflow.input_schema: |
| 109 | + print(f'\nInput Variables ({len(workflow.input_schema)}):') |
| 110 | + for var in workflow.input_schema: |
| 111 | + required = '(required)' if var.required else '(optional)' |
| 112 | + format_info = f' - format: {var.format}' if var.format else '' |
| 113 | + print(f' - {var.name}: {var.type} {required}{format_info}') |
| 114 | + else: |
| 115 | + print('\nNo input variables') |
| 116 | + |
| 117 | + # Show steps |
| 118 | + if workflow.steps: |
| 119 | + print(f'\nWorkflow Steps ({len(workflow.steps)}):') |
| 120 | + |
| 121 | + step_types = {} |
| 122 | + for i, step in enumerate(workflow.steps, 1): |
| 123 | + step_type = step.type |
| 124 | + step_types[step_type] = step_types.get(step_type, 0) + 1 |
| 125 | + |
| 126 | + print(f'\n Step {i}: {step_type}') |
| 127 | + if hasattr(step, 'description') and step.description: |
| 128 | + print(f' Description: {step.description}') |
| 129 | + |
| 130 | + # Show key fields based on step type |
| 131 | + if step_type == 'navigation' and hasattr(step, 'url'): |
| 132 | + print(f' URL: {step.url}') |
| 133 | + elif step_type == 'input' and hasattr(step, 'target_text'): |
| 134 | + print(f' Target: {step.target_text}') |
| 135 | + if hasattr(step, 'value'): |
| 136 | + print(f' Value: {step.value}') |
| 137 | + elif step_type == 'click' and hasattr(step, 'target_text'): |
| 138 | + print(f' Target: {step.target_text}') |
| 139 | + elif step_type == 'key_press' and hasattr(step, 'key'): |
| 140 | + print(f' Key: {step.key}') |
| 141 | + elif step_type == 'extract' and hasattr(step, 'extractionGoal'): |
| 142 | + print(f' Goal: {step.extractionGoal}') |
| 143 | + if hasattr(step, 'output'): |
| 144 | + print(f' Output Variable: {step.output}') |
| 145 | + |
| 146 | + # Show step type summary |
| 147 | + print('\n' + '-' * 80) |
| 148 | + print('Step Type Summary:') |
| 149 | + for step_type, count in sorted(step_types.items()): |
| 150 | + print(f' {step_type}: {count}') |
| 151 | + |
| 152 | + # Check for agent steps |
| 153 | + agent_steps = step_types.get('agent', 0) |
| 154 | + if agent_steps == 0: |
| 155 | + print('\n✅ Pure semantic workflow (no agent steps)') |
| 156 | + print(' This workflow will execute fast and cost $0 per run!') |
| 157 | + else: |
| 158 | + print(f'\n⚠️ Contains {agent_steps} agent step(s)') |
| 159 | + print(' Agent steps may be slower and cost money per execution') |
| 160 | + |
| 161 | + # Usage instructions |
| 162 | + print('\n' + '=' * 80) |
| 163 | + print('HOW TO USE THIS WORKFLOW') |
| 164 | + print('=' * 80) |
| 165 | + |
| 166 | + print(f'\nWorkflow ID: {metadata.id}') |
| 167 | + print('\n1. List all workflows:') |
| 168 | + print(' cd workflows') |
| 169 | + print(' BROWSER_USE_API_KEY=your_key uv run python cli.py list-workflows') |
| 170 | + |
| 171 | + print('\n2. Run by ID:') |
| 172 | + print(' cd workflows') |
| 173 | + print(f' BROWSER_USE_API_KEY=your_key uv run python cli.py run-workflow {metadata.id}') |
| 174 | + |
| 175 | + if workflow.input_schema: |
| 176 | + print('\n3. Run with input variables:') |
| 177 | + print(' cd workflows') |
| 178 | + var_args = ' '.join([f'--{v.name} <value>' for v in workflow.input_schema]) |
| 179 | + print(f' BROWSER_USE_API_KEY=your_key uv run python cli.py run-workflow {metadata.id} {var_args}') |
| 180 | + |
| 181 | + print('\n' + '=' * 80) |
| 182 | + print('DONE!') |
| 183 | + print('=' * 80 + '\n') |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == '__main__': |
| 187 | + asyncio.run(generate_and_store_workflow()) |
0 commit comments