# AI Model Comparison Pipeline
# Loads context via a service, calls AI models in parallel, picks the best result.

flowmarkup:
  title: AI Comparison Pipeline
  version: 1
  description: Compare Claude and Gemini outputs and pick the winner
  # Security: Declare all capabilities this flow needs.
  # SERVICES — the three external services called during execution.
  # No REQUEST, MAIL, SECRET, ENV, or SUBFLOWS needed by this flow.
  requires:
    SERVICES: [context_loader, claude, gemini]

  input:
    prompt_text:
      $kind: TEXT
      _notes_: The prompt to send to AI models
    temperature:
      $kind: NUMBER
      $default: 0.7
    context_key:
      $kind: STRING
      $default: default
      _notes_: Key identifying which context to load

  output:
    required:
      best_result:
        $kind: TEXT
        _notes_: The winning model's output
      best_model:
        $kind: STRING
        _notes_: Name of the winning model
    optional:
      comparison_data: JSON
  throws:
  - ServiceError
  examples:
    claude_wins:
      _notes_: Claude produces a longer response and is selected as winner
      input:
        prompt_text: Explain the benefits of microservices architecture
      output:
        best_result: Microservices architecture offers independent deployability, scalability per service, technology heterogeneity, and fault isolation...
        best_model: Claude
    gemini_wins:
      _notes_: Gemini produces a longer response for a short factual question
      input:
        prompt_text: What is 2+2?
        temperature: 0.2
        context_key: math_context
      output:
        best_result: The answer to 2+2 is 4, a fundamental arithmetic result.
        best_model: Gemini

  vars:
    retry_count: 0

  do:
    # 1. Load context via a service
  - call:
      _label_: Load context
      service: context_loader
      operation: load
      params:
        key: =context_key
      result:
        context_text: =RESULT.content
      errors:
      - ServiceError

    # 2. Call AI services in parallel
  - parallel:
      _label_: Fetch AI results
      failPolicy: COMPLETE
      claude_branch:
      - call:
          service: claude
          operation: generate
          timeout: 60s
          retry: 3/2s/EXPONENTIAL
          params:
            prompt: =prompt_text
            context: =context_text
            temperature: =temperature
          result:
            claude_result: =RESULT.text
            claude_usage: =RESULT.usage
          errors:
          - TimeoutError
          - RateLimitError
          - AuthenticationError
      gemini_branch:
      - call:
          service: gemini
          operation: generate
          timeout: 60s
          params:
            prompt: =prompt_text
            context: =context_text
            temperature: =temperature
          result:
            gemini_result: =RESULT.text
            gemini_usage: =RESULT.usage
          errors:
          - TimeoutError
          - RateLimitError

    # 3. Log results
  - log: "Claude: {{claude_result}}"
  - log: "Gemini: {{gemini_result}}"

    # 4. Pick the best result
  - if:
      condition: =claude_result.size() > gemini_result.size()
      then:
      - set:
          best_result: =claude_result
          best_model: Claude
      else:
      - set:
          best_result: =gemini_result
          best_model: Gemini

  - log: "Winner: {{best_model}}"

  catch:
    default:
    - logError: "Process failed: {{ERROR.MESSAGE}}"
    - throw:
        error: ServiceError
        message: "All AI services failed"
