# CleverAgents: Scientific Paper Writer v2.0 - LangGraph Version
# Complete implementation using pure LangGraph (no RxPy streams)
#
# This configuration orchestrates a multi-stage workflow for writing long-form
# scientific papers using a network of LLM and Tool actors.
#
# USAGE:
#   cleveragents interactive -c examples/scientific_paper_writer.yaml --unsafe
#
# COMMANDS:
#   !help     - Shows available commands
#   !next     - Advances to next stage
#   !accept   - Accepts current content
#   !stage    - Shows current stage description
#   !stages   - Lists all stages
#   !context  - Shows current context
#   !finish   - Automatically completes all remaining stages using first-pass suggestions

cleveragents:
  version: "3.0"
  logging:
    level: "INFO"
  template_engine: "JINJA2"
  unsafe: true
  default_actor: workflow_controller

# Actor definitions - renamed from agents
actors:


  # ============================================
  # Workflow & Command Control Agents
  # ============================================
  workflow_controller:
    type: tool
    config:
      tools:
        - name: workflow_controller
          code: |
            import sys
            print(f"DEBUG: workflow_controller called with: {input_data}", file=sys.stderr)
            print(f"DEBUG: current stage: {context.get('writing_stage')}", file=sys.stderr)

            # Store initial message
            context['initial_message'] = input_data

            # Remove escape characters
            clean_input = input_data.replace('\\!', '!').strip() if input_data else ''

            if clean_input.startswith('!'):
                # Include the original command in the output for command_handler
                result = f"GOTO_COMMAND_HANDLER:{clean_input}"
            else:
                stage = context.get('writing_stage') or 'intro'
                routed = False

                if stage == 'section_writing' and clean_input:
                    graph_state = context.get('graph_state', {}) or {}
                    metadata = graph_state.get('metadata', {}) or {}
                    last_agent_node = metadata.get('last_agent_node')
                    # Map actor nodes and their savers to the correct target
                    interactive_nodes = {
                        'source_selector': 'SOURCE_SELECTOR',
                        'source_finder': 'SOURCE_FINDER',
                        'source_finder_saver': 'SOURCE_FINDER',
                        'section_writer': 'SECTION_WRITER',
                        'section_writer_saver': 'SECTION_WRITER',
                        'section_proofreader': 'SECTION_PROOFREADER',
                        'section_proofreader_saver': 'SECTION_PROOFREADER',
                    }
                    if last_agent_node:
                        normalized = last_agent_node.strip().lower()
                        if normalized in interactive_nodes:
                            target_node = interactive_nodes[normalized]
                            result = f"GOTO_{target_node}:{clean_input}"
                            routed = True
                            print(
                                f"DEBUG: routed section writing input to {target_node}",
                                file=sys.stderr,
                            )

                if not routed:
                    # Pass the user input along with the stage routing
                    result = f"GOTO_{stage.upper()}:{clean_input}"

            print(f"DEBUG: returning: {result}", file=sys.stderr)


  # Command handler - processes all commands
  command_handler:
    type: tool
    config:
      tools:
        - name: command_processor
          code: |
            # Debug: print what we received
            import sys
            print(f"DEBUG command_handler: input_data='{input_data}'", file=sys.stderr)

            # Extract the actual command from the input_data
            # It comes as "GOTO_COMMAND_HANDLER:!next" from workflow_controller
            msg = ''
            if input_data and ':' in input_data:
                parts = input_data.split(':', 1)
                if len(parts) > 1:
                    msg = parts[1].strip()

            # Fallback to checking context
            if not msg:
                msg = context.get('initial_message', '').strip()

            # Last fallback
            if not msg and input_data:
                msg = input_data.strip() if input_data != 'GOTO_COMMAND_HANDLER' else ''

            print(f"DEBUG command_handler: using msg='{msg}'", file=sys.stderr)

            if not msg:
                result = "COMMAND_OUTPUT:Error: No command provided"
            else:
                parts = msg.split(maxsplit=1)
                command = parts[0]
                args = parts[1] if len(parts) > 1 else ''

                if command == '!help':
                    result = 'COMMAND_OUTPUT:Available Commands:\n!help                     - Shows this help message.\n!next [stage_name]        - Advances to the next stage, or the one specified.\n!start                    - Start/run the current stage.\n!write                    - (section_writing) Proceed from source selection to writing the section.\n!proofread                - (section_writing) Proceed from writing to proofreading.\n!accept                   - Accepts current content and proceeds to next section/stage.\n!stage                    - Describes the current stage.\n!stages                   - Lists all stages and marks the current one.\n!context [hops|all]       - Shows current context. \'hops\' shows recent history, \'all\' shows full history.\n!finish                   - Automatically completes remaining stages using first-pass suggestions and citations.'

                elif command == '!next':
                    current_stage = context.get('writing_stage') or 'intro'
                    stage_order = context.get('stage_order')
                    target_stage = None

                    # Handle intro stage specially
                    if current_stage == 'intro':
                        target_stage = 'discovery' if 'discovery' in stage_order else stage_order[0] if stage_order else None
                    elif args and args in stage_order:
                        target_stage = args
                    elif current_stage in stage_order:
                        current_index = stage_order.index(current_stage)
                        if current_index + 1 < len(stage_order):
                            target_stage = stage_order[current_index + 1]
                        else:
                            result = "COMMAND_OUTPUT:Already at the final stage."
                    else:
                        result = f"COMMAND_OUTPUT:Error: Current stage '{current_stage}' is invalid."

                    if target_stage:
                        # Update the stage
                        context['writing_stage'] = target_stage
                        # Trigger the stage flow immediately by routing to it with empty input
                        result = f"GOTO_{target_stage.upper()}:"

                elif command == '!start':
                    # Start/run the current stage (useful after transitioning to a new stage)
                    current_stage = context.get('writing_stage') or 'intro'
                    result = f"GOTO_{current_stage.upper()}:"

                elif command == '!write':
                    # Proceed from source selection to section writing
                    if context.get('writing_stage') == 'section_writing':
                        result = "GOTO_SECTION_WRITER:"
                    else:
                        result = "COMMAND_OUTPUT:!write is only available during section_writing stage"

                elif command == '!proofread':
                    # Proceed from section writing to proofreading
                    if context.get('writing_stage') == 'section_writing':
                        result = "GOTO_SECTION_PROOFREADER:"
                    else:
                        result = "COMMAND_OUTPUT:!proofread is only available during section_writing stage"

                elif command == '!accept':
                    # Accept current section and move to next
                    if context.get('writing_stage') == 'section_writing':
                        # Increment section index and route to next section
                        current_index = context.get('current_section_index', 0)
                        section_paths = context.get('section_paths', [])
                        new_index = current_index + 1
                        context['current_section_index'] = new_index

                        # Check if we've completed all sections
                        if new_index >= len(section_paths):
                            # Keep stage as section_writing so !next advances to paper_review
                            result = "COMMAND_OUTPUT:All sections written! Type !next to proceed to paper review stage."
                        else:
                            # Update current section path for the next section
                            context['current_section_path'] = section_paths[new_index]
                            result = f"GOTO_SOURCE_SELECTOR:{section_paths[new_index]}"
                    elif context.get('writing_stage') == 'core_content':
                        context.setdefault('core_content_progress', {})['accept_current_section'] = True
                        result = "Content accepted. The next section will be generated on your next input."
                    else:
                        current_stage = context.get('writing_stage')
                        stage_order = context.get('stage_order')
                        try:
                            current_index = stage_order.index(current_stage)
                            if current_index + 1 < len(stage_order):
                                target_stage = stage_order[current_index + 1]
                                context['writing_stage'] = target_stage
                                # Trigger the next stage flow immediately
                                result = f"GOTO_{target_stage.upper()}:"
                            else:
                                result = "Final stage complete."
                        except (ValueError, IndexError):
                            result = "Error advancing stage."

                elif command == '!stage':
                    stage_descriptions = {
                        'intro': 'Introduction to the writing system.',
                        'discovery': 'Interactive setup where we collect all paper requirements.',
                        'brainstorming': 'Refines the high-level idea for the paper.',
                        'vetting': 'Interactive research stage where you work with the assistant to compile high-quality sources.',
                        'structure': 'Defines the complete table of contents for the paper.',
                        'section_writing': 'Writes each section of the paper individually. Selects relevant sources, finds additional sources if needed, then writes the content.',
                        'paper_review': 'Reviews the complete assembled paper for consistency, grammar, style, citations, and logical flow.',
                        'latex_generation': 'Generates LaTeX structure, converts sections to LaTeX, assembles the document, compiles with pdflatex, and fixes any compilation errors.'
                    }
                    current_stage = context.get('writing_stage', 'unknown')
                    description = stage_descriptions.get(current_stage, 'No description available.')
                    result = f"COMMAND_OUTPUT:Current Stage: {current_stage}\nPurpose: {description}"

                elif command == '!stages':
                    stage_order = context.get('stage_order', [])
                    current_stage = context.get('writing_stage', 'unknown')
                    lines = ["Workflow Stages:"]
                    for stage in stage_order:
                        marker = '-->' if stage == current_stage else '   '
                        lines.append(f"{marker} {stage}")
                    result = 'COMMAND_OUTPUT:' + '\n'.join(lines)

                elif command == '!context':
                    import json
                    context_copy = dict(context)
                    history = context_copy.pop('history', [])
                    context_copy['history_size'] = len(history)
                    output_str = json.dumps(context_copy, indent=2, default=str)
                    if args:
                        if args == 'all':
                            history_to_show = history
                        elif args.isdigit():
                            history_to_show = history[-int(args):]
                        else:
                            history_to_show = []
                        if history_to_show:
                            output_str += "\n\n-- History --\n"
                            output_str += json.dumps(history_to_show, indent=2, default=str)
                    result = 'COMMAND_OUTPUT:' + output_str

                elif command == '!finish':
                    required_fields = ['topic', 'length', 'audience', 'publication', 'format']
                    paper_details = context.get('paper_details', {}) or {}
                    missing = [field for field in required_fields if not paper_details.get(field)]
                    current_stage = context.get('writing_stage') or 'intro'
                    if missing or current_stage in ('intro', 'discovery'):
                        missing_text = ', '.join(missing) if missing else 'initial discovery details'
                        result = (
                            "COMMAND_OUTPUT:!finish requires the discovery stage to be complete before automation can begin. "
                            f"Missing: {missing_text}."
                        )
                    elif context.get('auto_finish_active'):
                        result = "COMMAND_OUTPUT:Auto-finish mode is already active."
                    else:
                        context['auto_finish_active'] = True
                        context['auto_finish_state'] = {'expect': None}
                        result = "GOTO_AUTO_FINISH:START"

                else:
                    result = f"COMMAND_OUTPUT:Unknown command: {command}"

  auto_finish_driver:
    type: tool
    config:
      tools:
        - name: auto_finish_driver
          code: |
            message = input_data or ''
            auto_active = bool(context.get('auto_finish_active'))
            state = context.setdefault('auto_finish_state', {}) or {}

            import sys
            context.setdefault('auto_finish_debug', []).append({
                'event': 'driver_call',
                'active': auto_active,
                'message': str(message)[:200]
            })
            print(f"AUTO_FINISH_DRIVER_CALLED active={auto_active} msg={str(message)[:80]}", file=sys.stderr)

            if not auto_active:
                result = "COMMAND_OUTPUT:"
            else:
                text = message if isinstance(message, str) else str(message)
                sanitized = text.strip()

                import sys
                print(f"AUTO_FINISH_DEBUG expect={state.get('expect')} msg={sanitized[:120]}", file=sys.stderr)

                per_section = int(context.get('auto_finish_citations_per_section') or 5)
                expect = state.get('expect')
                stage_order = context.get('stage_order') or []
                writing_stage = context.get('writing_stage')
                if not writing_stage and stage_order:
                    writing_stage = stage_order[0]
                if not writing_stage:
                    writing_stage = 'brainstorming'
                if writing_stage in ('intro', 'discovery'):
                    writing_stage = 'brainstorming'

                def set_expect(value):
                    state['expect'] = value
                    context['auto_finish_state'] = state

                def current_section_label():
                    path = context.get('current_section_path')
                    if path:
                        return path
                    section_paths = context.get('section_paths') or []
                    idx = context.get('current_section_index', 0)
                    if idx < len(section_paths):
                        return section_paths[idx]
                    return 'Current section'

                def has_structure():
                    return bool(context.get('table_of_contents'))

                def report_progress(stage_name: str, note: str = ""):
                    try:
                        from cleveragents.core.progress import ProgressBarManager

                        ProgressBarManager.update(
                            stage=stage_name,
                            context=context,
                            message=note,
                        )
                    except Exception as progress_err:  # pylint: disable=broad-except
                        print(
                            f"PROGRESS_BAR_ERROR {progress_err}",
                            file=sys.stderr,
                        )

                def start_stage(stage_name):
                    context['writing_stage'] = stage_name
                    report_progress(stage_name, "Auto-finish in progress")
                    if stage_name == 'brainstorming':
                        set_expect('brainstorming_output')
                        return ("GOTO_BRAINSTORMING:Auto-finish mode is active. Using the captured requirements, "
                                "deliver a complete brainstorming summary without requesting additional input.")
                    if stage_name == 'vetting':
                        set_expect('vetting_output')
                        return ("GOTO_VETTING:Auto-finish mode is active. Compile the strongest possible vetted source list "
                                "based on the brainstorming summary and requirements.")
                    if stage_name == 'structure':
                        set_expect('structure_output')
                        return ("GOTO_STRUCTURE:Auto-finish mode is active. Produce a finalized, detailed table of contents "
                                "ready for section writing.")
                    if stage_name == 'section_writing':
                        if not has_structure():
                            context['auto_finish_active'] = False
                            context['auto_finish_state'] = {}
                            return ("COMMAND_OUTPUT:Auto-finish cannot start section writing because the structure stage "
                                    "is incomplete. Please finish the structure before running !finish.")
                        label = current_section_label()
                        section_paths = context.get('section_paths') or []
                        report_progress(
                            stage_name,
                            f"Sections {context.get('current_section_index', 0)}/{len(section_paths)}",
                        )
                        try:
                            from cleveragents.core.progress import ProgressBarManager

                            ProgressBarManager.update(
                                stage=stage_name,
                                current=context.get('current_section_index', 0),
                                total=len(section_paths) if section_paths else 1,
                                message=f"Sections {context.get('current_section_index', 0)}/{len(section_paths)}",
                                context=context,
                            )
                        except Exception:
                            pass
                        set_expect('source_selector_output')
                        return f"GOTO_SECTION_WRITING:{label or ''}"
                    if stage_name == 'paper_review':
                        set_expect('paper_review_output')
                        return ("GOTO_PAPER_REVIEW:Auto-finish mode is active. Review the assembled paper once and share "
                                "actionable feedback without waiting for more input.")
                    if stage_name == 'latex_generation':
                        # Keep auto_finish_active=True to maintain high depth limit
                        # Mark that we're in final stage
                        context['auto_finish_final_stage'] = True
                        report_progress(stage_name, "Generating LaTeX and compiling")
                        return "GOTO_LATEX_GENERATION:"
                    # Only set auto_finish_active=False at the very end
                    context['auto_finish_active'] = False
                    context['auto_finish_state'] = {}
                    return "COMMAND_OUTPUT:Auto-finish completed."

                next_action = None

                if not expect or (sanitized and sanitized.upper() == 'START'):
                    next_action = start_stage(writing_stage)
                elif isinstance(text, str) and ('AUTO_SECTIONS_COMPLETE' in text.upper() or 'sections written' in text.lower()):
                    next_action = start_stage('paper_review')
                elif isinstance(text, str) and 'ROUTE_NEXT_SECTION' in text.upper():
                    # Section was accepted, move to next section
                    # The section_accept_handler already incremented current_section_index
                    section_paths = context.get('section_paths') or []
                    current_idx = context.get('current_section_index', 0)
                    if current_idx >= len(section_paths):
                        # All sections done
                        next_action = start_stage('paper_review')
                    else:
                        # Start next section - go to source selection
                        label = current_section_label()
                        context['current_section_path'] = label
                        set_expect('source_selector_output')
                        report_progress(
                            'section_writing',
                            f"Completed section {current_idx} of {len(section_paths)}",
                        )
                        try:
                            from cleveragents.core.progress import ProgressBarManager

                            ProgressBarManager.update(
                                stage='section_writing',
                                current=current_idx,
                                total=len(section_paths) if section_paths else 1,
                                message=f"Completed section {current_idx} of {len(section_paths)}",
                                context=context,
                            )
                        except Exception:
                            pass
                        next_action = f"GOTO_SECTION_WRITING:{label}"
                elif expect == 'brainstorming_output':
                    next_action = start_stage('vetting')
                elif expect == 'vetting_output':
                    next_action = start_stage('structure')
                elif expect == 'structure_output':
                    next_action = start_stage('section_writing')
                elif expect == 'source_selector_output':
                    set_expect('source_finder_output')
                    section_label = current_section_label()
                    next_action = (
                        "GOTO_SOURCE_FINDER:"
                        f"Auto-finish request: locate exactly {per_section} new, high-quality citations tailored to "
                        f"\"{section_label}\". Merge them with the existing vetted sources when emitting the SOURCES_JSON "
                        "block and respond with FIND_COMPLETE when done."
                    )
                elif expect == 'source_finder_output':
                    set_expect('section_writer_output')
                    section_label = current_section_label()
                    next_action = f"GOTO_SECTION_WRITER:Auto-finish drafting section '{section_label}' using the gathered citations."
                elif expect == 'section_writer_output':
                    set_expect('section_proofreader_output')
                    section_label = current_section_label()
                    next_action = f"GOTO_SECTION_PROOFREADER:Auto-finish proofreading section '{section_label}'."
                elif expect == 'section_proofreader_output':
                    section_label = current_section_label()
                    set_expect('source_selector_output')
                    next_action = f"GOTO_ACCEPT_SECTION:{section_label or ''}"
                elif expect == 'paper_review_output':
                    next_action = start_stage('latex_generation')
                else:
                    next_action = "COMMAND_OUTPUT:Auto-finish is waiting for the next stage output."

                context.setdefault('auto_finish_debug', []).append({
                    'event': 'driver_next',
                    'action': next_action[:500],
                    'expect': state.get('expect'),
                    'stage': context.get('writing_stage')
                })

                result = next_action



  auto_finish_passthrough:
    type: tool
    config:
      tools:
        - name: auto_finish_passthrough
          code: |
            message = input_data or ''
            context['auto_finish_last_output'] = message
            auto_active = context.get('auto_finish_active', False)
            final_stage = context.get('auto_finish_final_stage', False)

            # Check if this is the final output (latex source was just saved)
            # We detect this by checking if latex_source exists and message contains latex-related content
            latex_source = context.get('latex_source', '')
            is_latex_completion = bool(latex_source) and ('document' in message.lower() or 'latex' in message.lower() or 'assembled' in message.lower() or 'compile' in message.lower())

            # If auto-finish just completed (final stage done), return the full paper content
            if final_stage and is_latex_completion:
                # Mark as complete now
                context['auto_finish_active'] = False
                context['auto_finish_final_stage'] = False
                context['auto_finish_state'] = {}

                section_count = len(context.get('section_paths') or [])
                topic = context.get('paper_details', {}).get('topic', 'Unknown')

                # Build the complete paper output
                output_parts = []
                output_parts.append("=" * 80)
                output_parts.append("AUTO-FINISH COMPLETE!")
                output_parts.append("=" * 80)
                output_parts.append(f"\nPaper: {topic}")
                output_parts.append(f"Sections: {section_count}")
                output_parts.append("")

                # Include the paper content from section_content
                section_content = context.get('section_content', {})
                section_paths = context.get('section_paths', [])

                if section_content:
                    output_parts.append("\n" + "=" * 80)
                    output_parts.append("PAPER CONTENT")
                    output_parts.append("=" * 80 + "\n")

                    for path in section_paths:
                        content = section_content.get(path, '')
                        if content:
                            # Determine header level based on path depth
                            depth = path.count(' > ')
                            if depth == 0:
                                output_parts.append(f"\n{'#' * (depth + 1)} {path}\n")
                            else:
                                output_parts.append(f"\n{'#' * (depth + 1)} {path.split(' > ')[-1]}\n")
                            output_parts.append(content)
                            output_parts.append("")

                # Include LaTeX if available
                if latex_source:
                    output_parts.append("\n" + "=" * 80)
                    output_parts.append("LATEX SOURCE")
                    output_parts.append("=" * 80 + "\n")
                    output_parts.append(latex_source)

                output_parts.append("\n" + "=" * 80)
                output_parts.append("All stages completed: brainstorming, vetting, structure, section writing, paper review, and LaTeX generation.")
                output_parts.append("=" * 80)

                result = '\n'.join(output_parts)
            elif not auto_active:
                # Auto-finish was manually deactivated or never active - just pass through
                result = message
            else:
                result = message

  # ============================================
  # Introduction Agent
  # ============================================
  intro_actor:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      system_prompt: |
        You are a friendly onboarding assistant for the Scientific Paper Writer Application.
        Your goal is to welcome the user, briefly explain how the system works, and instruct them to type `!next` to begin the process once they are ready to begin. Be sure after you give a brief intro to the process to encourage the user to ask you any questions they may have before you begin.

        The Scientific Paper Writer Application was written and works based on the following description given to the developers instructing them how to build the application, provided for context:

        Write a configuration, placed in `src/examples/`, for a network of LLM agents whose purpose is to write very long scientific papers that can't fit in a single context-window of sufficient quality to be ready for submission to peer-reviewed scientific journals and other publications. This should be done using only the configuration file, no additional code should need to be written to accomplish this. It will have two unique features, a multi-stage creation process that goes through various stages of writing, and a set of special commands that can be issued to interact with the process.

        The stages during the writing process will be the following:
        - Intro stage, this is you, the stage where you introduce the system.
        - Discovery stage, during this stage a conversation with one or more agents will help define the parameters for the paper, stuff like the subject (or at least general category), interesting insights or points, the overall length of the paper, who the audience will be, what publications, if any, will be submitted to, etc. Basically all the requirements that are going to restrain the process up front. This stage should always ask the user what format they want the paper written in, which expects answers like latex, plain text, Typst, markdown or some other documentation format. At first only latex format will be supported so the agent should only accept latex as an answer and if any other answer is given then the llm should say "that format isn't supported yet the only supported formats are: latex".
        - Brainstorming stage, during this stage there will be a conversation between the user and one or more agents where they refine the high-level idea for the paper and the fundamental components.
        - Vetting stage, during this stage the topic is researched at a high level and high-quality sources are compiled in a list with a one page summary for each source, as well as the link to retrieve the full, and other metadata information (like author, full citations format, etc). This should not be done with our built-in web search tool but instead using an LLM agent with the ability to search the web (or do deep research) to compile this for us.
        - Structure stage, during this stage the complete table of contents will be defined, along with a few sentences describing the focus and purpose of each section which will later be replaced with its full context.
        - Deep Research stage, during this stage each section of the document is explicitly researched in detail. Similar to the vetting stage we use LLM's with the capability to search the internet rather than our own internal web search tool. It should compile a list of sources similar to before with all the same metadata, including a summary of the material. However it should also include an additional section which specifically summarizes the content of the source as it relates to the topic and section being written.
        - Core Content stage, during this stage the core content is written. Each section should be filled in from the top down using LLM agent's that are explicitly told what the overall table of contents for the document is, and other relevant data collected so far (like the summary of the paper obtained during brainstorming, and all the relevant sources found during the research phase). It should be instructed to write the sections in the order they will appear in the paper. When instructed to write each section they should be told to only write the section instructed to write and to not attempt to write any other sections, including subsections under its current section, and then each subsection and later other sections will be addressed one at a time to keep each LLM focused on just a small portion of the work at any one time.
        - Framing Content stage, finally the Conclusion section followed by the Abstract section should be written in their own stage. This section should reflect what is learned and expressed in the main body of the text, therefore these two sections are to be written last.
        - Proofreading stage, during this final stage we use LLMs to proofread the paper and find any inconsistencies, typos, and logical fallacies and similar issues with the paper and correct it. We also look for things like "active voice", and other stylistic details.
        - Formatting stage, during this stage the full text created so far will be converted into the desired format, which should have been specified during the requirements stage. The stage should use google's Gemini pro 2.5 to leverage its very large context window so it is capable of reading in the entire document without needing to split it up into smaller chunks. During this stage the agent or agents should automatically take the output, now formatted in the desired format (at first we will only support latex) and try to compile it. If it compiles correctly then it waits for more input from the user, if it doesn't then it should iteratively try and correct itself in a loop by informing the agent that produced the formatting of its error and to try again. Likewise there should be a seperate agent which checks that the output pdf has the full content of the original text to be formatted. If not it should instruct the original agent which generates the latex of what is missing and tell it to correct its mistake. Only once the format is able to compile correctly and is determined to have the full content only then should it be presented to the user for additional input and refinement. Once the user is happy with this final step it should save both the latex source, and the generated pdf to the filesystem for the user's final acceptance.

        There should also be a command processor. This processor will look for any text provided by the user that begins with an exclamation point, for example "!next". When such a command is seen then it will be routed to an agent that processes the command and affects the flow of the system. The following commands need to be supported:
        - !next - This command takes one optional argument which will be the name of one of the stages. When this command is issued it tells the system to proceed through all the stages and stop at the stage specified in the argument. If no argument is issued then it stops at the next stage in the order of stages. If it passes through multiple stages then at each stage whatever decision the LLM makes is just accepted without any user interaction, saving it to the context and moving on. When at the stage specified the user has the ability to interact with the llm to perfect the response before moving on.
        - !accept - This command for most stages will act the same as the "!next" command without any arguments passed. However during the Core Content stage this command will be needed to tell the system that we accept some verbiage for a particular section and move on to the next section (rather than the next stage). As the user interacts with the llm it should try and detect phrases from the user that indicate they are happy with the content, even if they did not issue the "!accept" command and when that occurs the llm should give instructions to the user that if they are happy with the current content then they should issue the command. Otherwise interactions should be seen as critiques to refine the text further.
        - !help - This just lists all the available commands, their arguments, and what they do and how to use them.
        - !stage - This indicates the current stage the user is in, and describes, in detail, the purpose of that stage.
        - !stages - This lists all the possible stages, including completed stages, and marks the current stage in the list so its clear what stage the user is in.
        - !context - This should take one optional argument that is an integer or the word "all". If no integer is given it will just print out the current context, excluding the full history. It should just indicate how much history (the size) of it there is. If an argument is given that is an integer then it should also show the history back to that many hops. If the word "all" is used instead of an integer for this argument then all of the history should be displayed along with the context as well.

        In general at each stage the user will be expected to interact with the agent or agents at that stage and refine the creative process offering feedback and tweaks to the proposed output at any step. The llm should constantly be saving the latest and best result from a stage in the context, overriding older results as its improved. This way when the user finally likes the result and moves on the information is already in the context so the next stage can use it to perform its tasks as needed.

  # ============================================
  # Discovery Stage Agents
  # ============================================
  discovery_controller:
    type: tool
    config:
      tools:
        - name: discovery_router
          code: |
            import sys
            msg = input_data
            details = context.get('paper_details', {})

            print(f"DEBUG discovery_controller: input_data='{input_data}'", file=sys.stderr)
            print(f"DEBUG discovery_controller: paper_details={details}", file=sys.stderr)

            # Extract actual user message if it has GOTO_DISCOVERY prefix
            if msg.startswith('GOTO_DISCOVERY:'):
                msg = msg.replace('GOTO_DISCOVERY:', '', 1).strip()
                print(f"DEBUG discovery_controller: extracted msg='{msg}'", file=sys.stderr)

            # Process SET_ responses from ask agents
            if 'SET_TOPIC:' in msg:
                topic = msg.replace('SET_TOPIC:', '').strip()
                context['paper_details']['topic'] = topic
                result = f"DISCOVERY_RESPONSE:Thank you, topic has been set to \"{topic}\"\n\nCan you please specify the length, you would like to target for this paper."
            elif 'SET_LENGTH:' in msg:
                length = msg.replace('SET_LENGTH:', '').strip()
                context['paper_details']['length'] = length
                result = f"DISCOVERY_RESPONSE:Thank you, target word count has been set to {length}\n\nCan you please specify the target audience for this paper."
            elif 'SET_AUDIENCE:' in msg:
                audience = msg.replace('SET_AUDIENCE:', '').strip()
                context['paper_details']['audience'] = audience
                result = f"DISCOVERY_RESPONSE:Thank you, intended audience has been set to \"{audience}\"\n\nCan you please specify the target publication for this paper."
            elif 'SET_PUBLICATION:' in msg:
                publication = msg.replace('SET_PUBLICATION:', '').strip()
                context['paper_details']['publication'] = publication
                result = f"DISCOVERY_RESPONSE:Thank you, intended publication has been set to \"{publication}\"\n\nCan you please specify the target file format for this paper."
            elif 'SET_FORMAT:' in msg:
                format_val = msg.replace('SET_FORMAT:', '').strip()
                context['paper_details']['format'] = format_val
                result = f"DISCOVERY_RESPONSE:Thank you, intended file format has been set to \"{format_val}\"\n\nCan you please specify any other additional requirements you would like to be considered."
            elif 'SET_OTHER:' in msg:
                other = msg.replace('SET_OTHER:', '').strip()
                context['paper_details']['other'] = other
                context['writing_stage'] = 'brainstorming'
                result = f"DISCOVERY_RESPONSE:Thank you, additional requirements has been set to \"{other}\"\n\nDiscovery complete! The next stage is brainstorming.\n\nPlease tell us in a bit more detail an idea or ideas you'd like to explore for the content, direction, tone, or any other aspect you'd like to incorporate into this paper and we will help you brainstorm a high level summary and plan of action."
            else:
                # Route to appropriate ask actor - pass along the user input
                if details.get('topic') is None:
                    result = f"ROUTE_ASK_TOPIC:{msg}"
                elif details.get('length') is None:
                    result = f"ROUTE_ASK_LENGTH:{msg}"
                elif details.get('audience') is None:
                    result = f"ROUTE_ASK_AUDIENCE:{msg}"
                elif details.get('publication') is None:
                    result = f"ROUTE_ASK_PUBLICATION:{msg}"
                elif details.get('format') is None:
                    normalized = msg.strip().lower()
                    if normalized in ("latex", "la tex", "la-tex"):
                        context['paper_details']['format'] = 'latex'
                        result = (
                            "DISCOVERY_RESPONSE:Thank you, intended file format has been set to \"latex\""
                            "\n\nCan you please specify any other additional requirements you would like to be considered."
                        )
                    else:
                        result = f"ROUTE_ASK_FORMAT:{msg}"
                elif details.get('other') is None:
                    normalized = msg.strip().lower()
                    if normalized in ("", "none", "no", "n/a", "na", "no other requirements", "no other additional requirements", "none."):
                        context['paper_details']['other'] = 'None'
                        context['writing_stage'] = 'brainstorming'
                        result = (
                            "DISCOVERY_RESPONSE:Thank you, additional requirements has been set to \"None\""
                            "\n\nDiscovery complete! The next stage is brainstorming."
                            "\n\nPlease tell us in a bit more detail an idea or ideas you'd like to explore for the content, direction, tone, or any other aspect you'd like to incorporate into this paper and we will help you brainstorm a high level summary and plan of action."
                        )
                    else:
                        result = f"ROUTE_ASK_OTHER:{msg}"
                else:
                    result = "DISCOVERY_RESPONSE:Discovery complete! All parameters set. Type !next to proceed to brainstorming."

            print(f"DEBUG discovery_controller: returning: {result}", file=sys.stderr)

  ask_topic:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        You are an assistant helping to define a scientific paper. Ask the user "What topic would you like to write about?"
        If their answer isn't clear, then ask clarifying questions until it is clear. Once you get a clear
        response then reply with only the string "SET_TOPIC: " followed by a one sentence summary of the topic the user
        desires.

  ask_length:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        The topic has just been set. Now, please ask for the desired length of the paper, the user may specify this
        however they like, such as pages, paragraphs, or total number of words. If the user doesnt give an answer
        sufficiently detailed for you to answer with an approximate desired word count, then ask clarifying questions
        until this is clear. Once it is clear enough for you to give an approximate desired word cound then reply with
        only the string "SET_LENGTH: " followed by an integer number (using digits not words) indicating the approximate
        desired word count; this response must not have any words other than "SET_LENGTH:" and then a numerical number.

  ask_audience:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        The length has just been set. Now, please ask who the target audience is (e.g., general public, experts). If the
        user doesnt give an answer sufficiently detailed for you to answer with an intended audience, then ask
        clarifying questions until this is clear. Once it is clear enough for you to give the intended audience then
        reply with only the string "SET_AUDIENCE: " followed by a short summary of who the intended audience is for this
        paper.

  ask_publication:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        The audience has just been set. Now, please ask if they plan to submit it to a specific publication. If the
        user doesnt give an answer sufficiently detailed for you to answer with an intended publication, then ask
        clarifying questions until this is clear, or it is determined it wont be published to a publication. Once it is
        clear enough for you to give the intended publication then reply with only the string "SET_PUBLICATION: " followed by
        the name of the publication, or if no publication will be submitted to then reply with just the string
        "SET_PUBLICATION: none" and nothing else.

  ask_format:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        The intended publication has just been set. Now, ask what format the paper should be in.
        Currently, only 'latex' is supported. If they provide any other answer, you MUST state that the format is not
        supported and that only 'latex' is available. Only accept 'latex' as a valid answer.
        - Example user input: "markdown"
        - Your response: "That format isn't supported yet. The only supported formats are: latex."

        If the user doesnt give an answer sufficiently detailed for you to determine their desired format, then ask
        clarifying questions until this is clear. Once it is clear enough for you to give the intended format then reply
        with only the string "SET_FORMAT: " followed by the desired format, which should be latex.

  ask_other:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        You just asked the user if there are any other requirements that should be
        considered that we didnt cover. If the user doesnt give an answer sufficiently detailed for you to determine
        what these additional requirements are then ask clarifying questions until this is clear. Once it is clear
        enough for you to effectively summarize these additional requirements then reply with only the string
        "SET_OTHER: " followed by a summary of the additional requirements the user has explained throughout the
        conversations.

  # ============================================
  # Brainstorming Agent
  # ============================================
  brainstorming_actor:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: true
      max_history: 20
      system_prompt: |
        You are a creative partner for brainstorming. If the user has not yet provided specific ideas, start by asking them to share their thoughts on the direction, tone, or specific aspects they'd like to incorporate into the paper. Engage in a conversation to refine the high-level idea and key
        arguments. Be careful not to respond with anything that describes the actual sections of the document
        explicitly. We will define the structure later. Focus only on summarizing, in paragraph and bullet list form,
        the content and ideas to write about, and not the order or structure. Summarize the the current brainstormed
        idea with each response in full, repeating the relevant content from previous responses when still relevant.

        The paper must be written to meet the following requirements:
          - The topic of the paper must be: {{ context.paper_details.topic | tojson }}
          - The length of the paper must be no more than: {{ context.paper_details.length | tojson }} words
          - The paper must be written for the following audience: {{ context.paper_details.audience | tojson }}
          - The paper must be written with the intention of submitting it to the following publication: {{ context.paper_details.publication | tojson }}
          - The paper must be written in the following file format: {{ context.paper_details.format | tojson }}
          - The paper must meet the following additional requirements: {{ context.paper_details.other | tojson }}

  brainstorming_saver:
    type: tool
    config:
      tools:
        - name: save_brainstorming
          code: |
            import sys
            print(f"DEBUG brainstorming_saver: input_data='{input_data[:200] if input_data else 'NONE'}...'", file=sys.stderr)
            # Save the brainstorming summary to context
            context['brainstorming_summary'] = input_data
            result = input_data

  # ============================================
  # Vetting Stage Agent
  # ============================================
  vetting_actor:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      max_tokens: 4096
      memory_enabled: true
      max_history: 20
      system_prompt: |
        You are a research assistant helping compile sources for a scientific paper.

        Paper topic: {{ context.paper_details.topic | tojson }}
        Paper focus: {{ context.brainstorming_summary | tojson }}

        IMPORTANT INSTRUCTIONS:
        {% if context.auto_finish_active %}
        1. Auto-finish mode is active. Do not ask the user any questions or wait for additional input. Immediately compile the strongest possible list of vetted sources using the brainstorming summary and paper requirements. Cover multiple angles that downstream stages will need (e.g., systems, policy, technology) and surface at least 6 high-quality citations.
        2. Provide a concise narrative summary of the sources you selected before emitting the SOURCES_JSON block so later stages can continue without any follow-up.
        {% else %}
        1. First introduction: When you first interact with the user, introduce yourself and explain your role:
           "Hello! I'm your research assistant for the vetting stage. My role is to help you compile high-quality sources for your paper on {{ context.paper_details.topic }}. I can search for academic papers, articles, and other scholarly sources based on your requirements. Please tell me what kind of sources you'd like me to find - you can specify the number of sources, the type (journal articles, conference papers, books, etc.), quality requirements, publication dates, or any other criteria. We'll work together to build and refine the list of citations until you're satisfied."
        2. Interactive refinement: The user may want to add, remove, or modify sources. Engage in a back-and-forth discussion to refine the list. Keep track of all sources discussed and maintain an updated list.
        {% endif %}

        3. When searching: When the user asks you to find sources, you should use your knowledge to suggest realistic, high-quality academic sources that would be appropriate for the paper topic. Include:

           - Full citation in appropriate academic format (APA, MLA, Chicago, etc. - ask the user if they have a preference)
           - A brief summary of the source content (1-2 paragraphs)
           - The actual URL/DOI to access the source (use real DOIs or URLs when possible, such as doi.org links, arxiv.org links, or actual journal websites)

        4. CRITICAL - Saving sources: After each interaction where you provide or update sources, you MUST include the complete current list of sources in this EXACT format at the very end of your response:

        SOURCES_JSON_START
        [{"citation": "Author, A. A., & Author, B. B. (Year). Title of article. Journal Name, volume(issue), pages. https://doi.org/xx.xxxx/xxxxx", "summary": "Detailed summary of the source...", "link": "https://doi.org/10.1234/example"}]
        SOURCES_JSON_END

        The link should be a real DOI (https://doi.org/10.xxxx/xxxxx), arXiv link (https://arxiv.org/abs/xxxx.xxxxx), PubMed link (https://pubmed.ncbi.nlm.nih.gov/xxxxxxx/), or other legitimate academic source URL. Never use placeholder URLs like example.com.

        5. When the user is satisfied: When the user indicates they are happy with the citations, remind them they can use !next or !accept to proceed to the next stage.

  vetting_saver:
    type: tool
    config:
      tools:
        - name: save_vetting
          code: |
            import json

            # Extract sources from the special markers
            sources = []
            message = input_data

            if 'SOURCES_JSON_START' in message and 'SOURCES_JSON_END' in message:
                start_marker = 'SOURCES_JSON_START'
                end_marker = 'SOURCES_JSON_END'

                start_pos = message.find(start_marker)
                end_pos = message.find(end_marker)

                if start_pos != -1 and end_pos != -1:
                    json_start = start_pos + len(start_marker)
                    json_content = message[json_start:end_pos].strip()

                    try:
                        sources = json.loads(json_content)
                        if not isinstance(sources, list):
                            sources = []
                    except json.JSONDecodeError:
                        sources = []

            # Update context
            if sources:
                context['vetting_sources'] = sources
                clean_message = message[:message.find('SOURCES_JSON_START')] if 'SOURCES_JSON_START' in message else message
                result = f"{clean_message.strip()}\n\n✅ Successfully extracted and saved {len(sources)} sources to context!"
            else:
                result = message

  # ============================================
  # Structure Agent
  # ============================================
  structure_actor:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: true
      max_history: 20
      system_prompt: |
        You are an expert academic writer.
        {% if context.auto_finish_active %}
        Auto-finish mode is active. Without asking the user any questions, immediately generate a finalized, detailed table of contents that can be handed directly to the section-writing stage. Include every section and subsection needed to fulfill the requirements, and provide a 1-2 sentence description for each entry.
        {% else %}
        Start by offering to create a complete table of contents based on the requirements and research gathered, then create it. Based on the users input, requirements, summary and vetted
        sources, create a complete, logical table of contents. For each section/subsection, write a 1-2 sentence
        description of its purpose.

        The user may then have feedback or additional directions, engage in a conversation and modify your proposed
        table of contents accordingly. Each time you respond make sure you respond with a complete updated version of
        the table of contents along with the descriptions of each sentence. Never give a partial answer that only
        describes the additions or changes without providing the complete updated table of contents.
        {% endif %}


        The paper must be written to meet the following requirements:
          - The topic of the paper must be: {{ context.paper_details.topic | tojson }}
          - The length of the paper must be no more than: {{ context.paper_details.length | tojson }} words
          - The paper must be written for the following audience: {{ context.paper_details.audience | tojson }}
          - The paper must be written with the intention of submitting it to the following publication: {{ context.paper_details.publication | tojson }}
          - The paper must be written in the following file format: {{ context.paper_details.format | tojson }}
          - The paper must meet the following additional requirements: {{ context.paper_details.other | tojson }}

        A high-level summary and intent for the content of the paper is the following:

        {{ context.brainstorming_summary | tojson }}

        The vetted sources we have so far are the following:

        {% if context.vetting_sources and context.vetting_sources|length > 0 %}
        {% for source in context.vetting_sources %}
        {% if source is mapping %}
        - Citation: {{ source.get('citation', 'No citation') }}
        - Summary: {{ source.get('summary', 'No summary')[:300] }}{% if source.get('summary', '')|length > 300 %}...{% endif %}
        {% if source.get('link') %}
        - Link: {{ source.link }}
        {% endif %}

        {% endif %}
        {% endfor %}
        {% else %}
        No vetted sources available yet.
        {% endif %}

  structure_saver:
    type: tool
    config:
      tools:
        - name: save_structure
          code: |
            # Save the table of contents to context
            context['table_of_contents'] = input_data
            result = input_data


  # ============================================
  # SECTION WRITING STAGE
  # ============================================

  toc_parser:
    type: llm
    config:
      provider: google
      model: gemini-2.0-flash
      memory_enabled: false
      response_format:
        type: json_schema
        json_schema:
          name: table_of_contents
          strict: true
          schema:
            type: object
            properties:
              sections:
                type: array
                items:
                  type: object
                  properties:
                    title:
                      type: string
                      description: The section title without numbering
                    subsections:
                      type: array
                      description: Subsections within this section (if any)
                      items:
                        type: object
                        properties:
                          title:
                            type: string
                            description: The subsection title without numbering
                          subsections:
                            type: array
                            description: Sub-subsections (if any)
                            items:
                              type: object
                              properties:
                                title:
                                  type: string
                                  description: The sub-subsection title without numbering
                              required:
                                - title
                              additionalProperties: false
                        required:
                          - title
                        additionalProperties: false
                  required:
                    - title
                  additionalProperties: false
            required:
              - sections
            additionalProperties: false
      system_prompt: |
        You are parsing a table of contents into a structured JSON format.

        Given a table of contents (which may be in various formats - numbered, bulleted, markdown, etc.),
        extract the section titles and their hierarchy and return them as a nested JSON structure.

        Rules:
        - Extract section titles WITHOUT numbering (remove "1.", "1.1.", etc.)
        - Skip the table of contents header itself
        - Skip empty lines or decorative elements
        - Preserve the ORDER of sections
        - PRESERVE THE HIERARCHY: main sections should have their subsections nested inside them
        - Do NOT include descriptions, just titles
        - Each section can have optional "subsections" array for nested items

        Example input:
        1. Introduction
        2. Methods
           2.1 Data Collection
           2.2 Analysis
        3. Results

        Example output:
        {"sections": [
          {"title": "Introduction"},
          {"title": "Methods", "subsections": [
            {"title": "Data Collection"},
            {"title": "Analysis"}
          ]},
          {"title": "Results"}
        ]}

        Table of Contents to parse:
        {{ context.table_of_contents }}

  section_writing_controller:
    type: tool
    config:
      tools:
        - name: section_controller
          code: |
            import sys
            import json
            print(f"DEBUG section_writing_controller", file=sys.stderr)

            # Check if we need to parse the TOC first
            section_paths = context.get('section_paths')
            fallback_sections = ['Introduction', 'Methods', 'Results', 'Discussion']

            # Re-parse if section_paths is empty, None, or still the generic fallback
            needs_parse = (
                not section_paths or
                section_paths == fallback_sections
            )

            if needs_parse:
                toc_text = context.get('table_of_contents', '')
                if not toc_text:
                    result = "ERROR: No table of contents found. Please complete structure stage first."
                else:
                    # Route to TOC parser to get structured JSON
                    result = "ROUTE_PARSE_TOC:Please parse the current table of contents into JSON."
            else:
                # We have parsed sections, proceed with section writing
                section_paths = context.get('section_paths', [])
                current_index = context.get('current_section_index', 0)

                if not section_paths:
                    result = "ERROR: No section paths available after parsing."
                elif current_index >= len(section_paths):
                    # All sections complete
                    context['writing_stage'] = 'paper_review'
                    result = "AUTO_SECTIONS_COMPLETE:All sections written! Moving to paper review stage."
                else:
                    current_path = section_paths[current_index]
                    if not current_path:
                        result = f"ERROR: Empty section path at index {current_index}."
                    else:
                        context['current_section_path'] = current_path
                        result = f"ROUTE_SELECT_SOURCES:{current_path}"

  toc_parser_saver:
    type: tool
    config:
      tools:
        - name: save_parsed_toc
          code: |
             import sys
             import json
             print("DEBUG toc_parser_saver", file=sys.stderr)

             def flatten_sections(sections):
                 """Flatten nested sections into a list with full paths using iteration."""
                 flat_list = []
                 stack = [(sections, "", 0)]

                 while stack:
                     current_sections, parent_path, idx = stack.pop()
                     while idx < len(current_sections):
                         section = current_sections[idx]
                         title = section.get("title", "")
                         if title:
                             current_path = f"{parent_path} > {title}" if parent_path else title
                             flat_list.append(current_path)
                             subsections = section.get("subsections", [])
                             if subsections:
                                 stack.append((current_sections, parent_path, idx + 1))
                                 stack.append((subsections, current_path, 0))
                                 break
                         idx += 1

                 return flat_list

             try:
                 json_text = input_data.strip()
                 if json_text.startswith("```"):
                     lines = json_text.split("\n")
                     if lines and lines[0].startswith("```"):
                         lines = lines[1:]
                     if lines and lines[-1].strip() == "```":
                         lines = lines[:-1]
                     json_text = "\n".join(lines)

                 toc_data = json.loads(json_text)

                 if isinstance(toc_data, list):
                     sections = toc_data
                 elif isinstance(toc_data, dict):
                     sections = toc_data.get("sections", [])
                 else:
                     sections = []

                 section_paths = flatten_sections(sections)
                 context["section_paths"] = section_paths
                 context["current_section_index"] = 0

                 try:
                     from cleveragents.core.progress import ProgressBarManager

                     ProgressBarManager.update(
                         stage=context.get("writing_stage", "section_writing") or "section_writing",
                         current=0,
                         total=len(section_paths) if section_paths else 1,
                         message="Preparing sections",
                         context=context,
                     )
                 except Exception:
                     pass

                 if section_paths:
                     first_section = section_paths[0]
                     context["current_section_path"] = first_section
                     result = f"ROUTE_SELECT_SOURCES:{first_section}"
                 else:
                     result = "ERROR: No sections found in table of contents"

             except Exception:
                 context["section_paths"] = ["Introduction", "Methods", "Results", "Discussion"]
                 context["current_section_index"] = 0
                 first_section = context["section_paths"][0]
                 context["current_section_path"] = first_section
                 result = f"ROUTE_SELECT_SOURCES:{first_section}"



  source_selector:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: true
      max_history: 10
      system_prompt: |
        You are helping select relevant sources for writing ONE SPECIFIC section of the paper.

        CURRENT SECTION TO WRITE: "{{ context.current_section_path }}"

        Section progress: {{ context.current_section_index + 1 }} of {{ context.section_paths|length if context.section_paths else 'unknown' }}

        {% if ' > ' in context.current_section_path %}
        NOTE: This is a SUBSECTION - you will be writing detailed content for this specific topic.
        {% else %}
        NOTE: This is a TOP-LEVEL SECTION - you will be writing a brief introduction that sets up its subsections.
        {% endif %}

        Available vetted sources:
        {% if context.vetting_sources and context.vetting_sources|length > 0 %}
        {% for source in context.vetting_sources %}
        {{ loop.index }}. Citation: {{ source.citation if source.citation else 'No citation provided' }}
           Summary: {{ source.summary if source.summary else 'No summary available' }}
           Link: {{ source.link if source.link else 'No link available' }}
        {% endfor %}
        {% else %}
        No vetted sources have been captured yet. Let the user know and suggest running !next after the vetting stage completes.
        {% endif %}

        YOUR TASK:
        When you receive ANY message (including just a section name, "suggest something", or any other input):
        1. First, announce which section we're working on: "Now working on section: [section name]"
        2. List the available sources with their numbers and brief descriptions
        3. Recommend which sources are most relevant for THIS specific section and explain why
        4. Ask the user if they want to use these sources, find additional ones, or have questions

        You MUST always show the actual source citations and your recommendations - never skip this step!

        If the user asks for changes to source selection or has questions, help them.

        IMPORTANT: When the user is satisfied with the source selection, tell them to type !write to proceed to writing.

        Example ending: "These sources should work well for this section. When you're ready, type !write to proceed to writing."


  source_finder:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      max_tokens: 4096
      memory_enabled: true
      max_history: 15
      system_prompt: |
        You are a research specialist finding additional sources for a specific section of a scientific paper.

        Paper topic: {{ context.paper_details.topic }}
        Current section: {{ context.current_section_path }}
        Paper focus: {{ context.brainstorming_summary }}

        Existing vetted sources:
        {% if context.vetting_sources and context.vetting_sources|length > 0 %}
        {% for source in context.vetting_sources %}
        {{ loop.index }}. {{ source.citation if source.citation else 'No citation' }}
        {% endfor %}
        {% else %}
        No sources have been collected yet.
        {% endif %}

        IMPORTANT INSTRUCTIONS:
        1. Your role: You are an expert research assistant who finds HIGH-QUALITY, REAL academic sources. You have extensive knowledge of scientific literature and can suggest real papers, articles, and studies.

        2. When searching for sources: Use your knowledge to suggest realistic, high-quality academic sources that would be appropriate for this specific section. Include:
           - Full citation in APA format
           - A brief summary of the source content (2-3 sentences)
           - The actual URL/DOI to access the source (use real DOIs like https://doi.org/10.xxxx/xxxxx, arXiv links like https://arxiv.org/abs/xxxx.xxxxx, or PubMed links like https://pubmed.ncbi.nlm.nih.gov/xxxxxxx/)

        3. Focus on section relevance: Find sources specifically relevant to "{{ context.current_section_path }}" - not just general sources about the topic.

        4. CRITICAL - Saving sources: After providing sources, you MUST include ALL sources (existing + new) in this EXACT format at the very end of your response:

        SOURCES_JSON_START
        [{"citation": "Author, A. A., & Author, B. B. (Year). Title of article. Journal Name, volume(issue), pages. https://doi.org/xx.xxxx/xxxxx", "summary": "Brief summary of the source...", "link": "https://doi.org/10.1234/example"}]
        SOURCES_JSON_END

        Include BOTH the existing sources AND any new sources you find in this JSON block. This ensures all sources are preserved.

        5. Interactive refinement: The user may want more sources, different types, or sources from specific time periods. Engage in discussion to find exactly what they need.

        6. When the user is satisfied: When they confirm they have enough sources, output: FIND_COMPLETE

  source_finder_saver:
    type: tool
    config:
      tools:
        - name: save_found_sources
          code: |
            import json

            # Extract sources from the special markers
            sources = []
            message = input_data

            if 'SOURCES_JSON_START' in message and 'SOURCES_JSON_END' in message:
                start_marker = 'SOURCES_JSON_START'
                end_marker = 'SOURCES_JSON_END'

                start_pos = message.find(start_marker)
                end_pos = message.find(end_marker)

                if start_pos != -1 and end_pos != -1:
                    json_start = start_pos + len(start_marker)
                    json_content = message[json_start:end_pos].strip()

                    try:
                        sources = json.loads(json_content)
                        if not isinstance(sources, list):
                            sources = []
                    except json.JSONDecodeError:
                        sources = []

            # Update context with new sources (replaces existing since source_finder includes all)
            if sources:
                context['vetting_sources'] = sources
                clean_message = message[:message.find('SOURCES_JSON_START')] if 'SOURCES_JSON_START' in message else message
                result = f"{clean_message.strip()}\n\n✅ Successfully saved {len(sources)} sources to context!"
            else:
                result = message

  section_writer:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: true
      max_history: 20
      system_prompt: |
        You are writing EXACTLY ONE section: "{{ context.current_section_path }}"

        Paper topic: {{ context.paper_details.topic }}
        Paper focus: {{ context.brainstorming_summary }}
        Target length: {{ context.paper_details.length }} words total
        Target audience: {{ context.paper_details.audience }}

        Complete section structure (for reference only - write ONLY the current section):
        {% if context.section_paths %}
        {% for path in context.section_paths %}
        {{ ">>> " if path == context.current_section_path else "    " }}{{ path }}
        {% endfor %}
        {% endif %}

        Available sources:
        {% if context.vetting_sources %}
        {% for source in context.vetting_sources %}
        - {{ source.citation if source.citation else 'No citation' }}
        {% endfor %}
        {% endif %}

        CRITICAL INSTRUCTIONS:
        1. Write ONLY the content for "{{ context.current_section_path }}" - nothing else.

        2. Section type guidance:
           {% if ' > ' in context.current_section_path %}
           - This is a SUBSECTION ({{ context.current_section_path }})
           - Write the full detailed content for this specific subsection only
           - This should be several paragraphs of substantive content
           {% else %}
           - This is a TOP-LEVEL SECTION ({{ context.current_section_path }})
           - Write a brief introductory paragraph (2-4 sentences) that introduces what this section will cover
           - Do NOT write the subsection content here - those will be written separately
           - Just provide a roadmap/overview of what the subsections will address
           {% endif %}

        3. Do NOT include:
           - Content from other sections
           - Subsection headers or subsection content (those are separate sections)
           - A full treatment of the topic if this is a parent section

        4. Use academic tone and cite sources appropriately.

        YOUR TASK:
        - Write the section content for "{{ context.current_section_path }}"
        - Include proper academic citations from the available sources (e.g., Author et al., Year)
        - Work with the user to refine the content based on their feedback
        - If the user asks for changes, make them and present the revised content

        RESPONSE FORMAT (MANDATORY):
        SECTION_CONTENT:
        <final section text>

        NEXT_ACTION:
        Type !proofread when you are satisfied with this section.

        Do NOT include any other commentary, explanations, or conversational text. The SECTION_CONTENT block must contain only the section prose that will be stored in the paper.

  section_writer_saver:
    type: tool
    config:
      tools:
        - name: save_section
          code: |
            path = context.get('current_section_path', 'unknown')
            # Only save actual content, not routing commands
            if input_data and not input_data.startswith('GOTO_'):
              text = input_data.strip()
              section_text = text
              marker = 'SECTION_CONTENT:'
              if marker in text:
                remainder = text.split(marker, 1)[1]
                next_marker = 'NEXT_ACTION:'
                if next_marker in remainder:
                  section_text = remainder.split(next_marker, 1)[0].strip()
                else:
                  section_text = remainder.strip()
              if section_text:
                context.setdefault('section_content', {})[path] = section_text
                context.setdefault('section_drafts', {})[path] = section_text
                context['last_written_section_content'] = section_text
            result = input_data

  section_accept_handler:
    type: tool
    config:
      tools:
        - name: accept_section
          code: |
            current_index = context.get('current_section_index', 0)
            section_paths = context.get('section_paths') or []
            completed_section = (
                section_paths[current_index] if current_index < len(section_paths) else 'Unknown'
            )

            # Advance to the next section and update the current path so auto-finish progresses
            context['current_section_index'] = current_index + 1
            new_index = current_index + 1

            # Progress update after completing a section
            try:
                from cleveragents.core.progress import ProgressBarManager

                ProgressBarManager.update(
                    stage=context.get('writing_stage', 'section_writing') or 'section_writing',
                    current=new_index,
                    total=len(section_paths) if section_paths else 1,
                    message=f"Completed {new_index}/{len(section_paths)} sections",
                    context=context,
                )
            except Exception:
                pass

            if new_index >= len(section_paths):
                # All sections complete; clear current section path
                context['current_section_path'] = None
                result = f"AUTO_SECTIONS_COMPLETE:All {len(section_paths)} sections written"
            else:
                next_section = section_paths[new_index]
                context['current_section_path'] = next_section
                # Include unique info in message to avoid loop detection
                result = f"ROUTE_NEXT_SECTION:{next_section}|completed={completed_section}|idx={new_index}"



  # ============================================
  # SECTION PROOFREADING
  # ============================================

  section_proofreader:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: true
      max_history: 15
      system_prompt: |
        You are a meticulous academic proofreader reviewing a SINGLE section of a scientific paper.

        CURRENT SECTION: "{{ context.current_section_path }}"

        Paper topic: {{ context.paper_details.topic }}
        Target audience: {{ context.paper_details.audience }}

        Section content to proofread:
        {{ context.section_content.get(context.current_section_path, 'No content available') }}

        Complete table of contents (for context about where this section fits):
        {% if context.section_paths %}
        {% for path in context.section_paths %}
        {{ ">>> " if path == context.current_section_path else "    " }}{{ path }}
        {% endfor %}
        {% endif %}

        YOUR PROOFREADING TASKS:
        1. Check for grammatical errors, typos, and spelling mistakes
        2. Ensure the writing uses active voice where appropriate
        3. Verify academic tone and style consistency
        4. Check that citations are properly formatted and used
        5. Look for logical inconsistencies or unclear arguments
        6. Ensure smooth transitions and flow
        7. Verify the content matches what the section should cover (based on the TOC)

        YOUR TASK:
        - Present your detailed proofreading feedback on the section content shown above
        - List any issues found or confirm the section is well-written
        - If the user asks you to make changes, make them and present the corrected version
        - When you provide a fully revised section, respond with **only** `UPDATED_SECTION_CONTENT:` followed by the final section text. This allows the system to store the new content verbatim.
        - Work with the user until they are satisfied with the section

        IMPORTANT: When the user is satisfied with the proofread section, tell them to type !accept to accept the section and move to the next one.

        Example ending: "The section looks good overall. Type !accept when you're ready to move to the next section."


  section_proofreader_saver:
    type: tool
    config:
      tools:
        - name: save_proofread_section
          code: |
            # Save proofreading feedback separately so original section content remains intact
            path = context.get('current_section_path', 'unknown')
            if input_data and not input_data.startswith('GOTO_'):
              text = input_data.strip()
              updated_prefix = 'UPDATED_SECTION_CONTENT:'
              if text.startswith(updated_prefix):
                new_content = text[len(updated_prefix):].lstrip()
                context.setdefault('section_content', {})[path] = new_content
                context.setdefault('section_drafts', {})[path] = new_content
                context.setdefault('section_feedback', {})[path] = text
                context['last_written_section_content'] = new_content
              else:
                context.setdefault('section_feedback', {})[path] = input_data
            result = input_data

  # ============================================
  # PAPER REVIEW STAGE
  # ============================================

  paper_review_controller:
    type: tool
    config:
      tools:
        - name: review_controller
          code: |
            import sys
            print(f"DEBUG paper_review_controller", file=sys.stderr)

            section_content = context.get('section_content', {})
            section_paths = context.get('section_paths', [])

            # Debug: show what sections we have
            print(f"DEBUG section_content has {len(section_content)} sections", file=sys.stderr)
            print(f"DEBUG section_paths has {len(section_paths)} paths", file=sys.stderr)
            for p in section_paths[:5]:
                has_content = p in section_content
                print(f"DEBUG   path={p[:50]} has_content={has_content}", file=sys.stderr)

            if not section_content:
                result = "ERROR: No sections found. Please complete section writing stage first."
            else:
                # Build full paper text
                paper_topic = context.get('paper_details', {}).get('topic', 'Scientific Paper')
                full_text = f"# {paper_topic}\n\n"

                for path in section_paths:
                    text = section_content.get(path)
                    if isinstance(text, str) and text.strip():
                        full_text += f"## {path}\n\n{text.strip()}\n\n"

                assembled = full_text.strip()
                context['assembled_paper'] = assembled

                auto_active = context.get('auto_finish_active', False)
                already_printed_auto = context.get('paper_review_printed_auto', False)
                preview_output = (
                    f"# Full Paper\n\n{assembled}" if assembled else "# Full Paper\n\n(No content available.)"
                )

                # Print the assembled paper once; avoid duplicates in auto-finish
                if not (auto_active and already_printed_auto):
                    print(preview_output, flush=True)
                    if auto_active:
                        context['paper_review_printed_auto'] = True

                if auto_active:
                    result = (
                        "AUTO_PAPER_REVIEW_COMPLETE:Auto-finish mode printed the full paper; "
                        "skipping LLM review and proceeding to LaTeX generation."
                    )
                else:
                    instruction = (
                        "Please review the assembled paper stored in context['assembled_paper'] "
                        "for coherence, grammar, citations, and overall flow."
                    )
                    result = f"ROUTE_REVIEW_PAPER:{instruction}"

  paper_review_actor:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      max_tokens: 4096
      memory_enabled: true
      max_history: 20
      system_prompt: |
        You are reviewing the complete assembled paper.

        Here is the full paper content:

        {{ context.get('assembled_paper', '') }}

        The system already displayed the full paper verbatim before your turn.
        DO NOT repeat or summarize the paper content. Focus solely on analysis.
        Structure your response under a single top-level heading "Review Feedback" with clear bullets or subheadings.

        When providing feedback, review the paper for:

        - Consistency and logical flow between sections
        - Grammar, spelling, and style
        - Proper citation format and usage
        - Logical coherence and argument strength
        - Transitions between sections

        Provide specific, actionable feedback tied to the relevant sections. The user can:
        - Discuss refinements to specific sections
        - Make changes across the document
        - Use !accept or !next when satisfied to proceed to LaTeX generation

  paper_review_saver:
    type: tool
    config:
      tools:
        - name: save_review
          code: |
            context['reviewed_paper'] = input_data
            result = input_data


  # ============================================
  # LATEX GENERATION STAGE
  # ============================================

  latex_controller:
    type: tool
    config:
      tools:
        - name: latex_ctrl
          code: |
            import sys
            print(f"DEBUG latex_controller", file=sys.stderr)

            # Multi-step workflow
            if not context.get('latex_structure'):
                result = "ROUTE_GEN_STRUCTURE:"
            elif not context.get('latex_sections_complete'):
                # Convert sections one by one
                section_paths = context.get('section_paths', [])
                latex_sections = context.get('latex_sections', {})
                current_latex_index = context.get('current_latex_index', 0)

                if current_latex_index < len(section_paths):
                    section_path = section_paths[current_latex_index]
                    context['current_latex_section'] = section_path
                    result = f"ROUTE_CONVERT_SECTION:{section_path}"
                else:
                    context['latex_sections_complete'] = True
                    result = "ROUTE_ASSEMBLE_LATEX:"
            elif not context.get('latex_source'):
                result = "ROUTE_ASSEMBLE_LATEX:"
            else:
                # Skip compilation for now (sandbox doesn't support file operations)
                # Just mark as complete
                context['latex_compiled'] = True
                result = f"LaTeX generation complete. Source saved ({len(context.get('latex_source', ''))} characters)."


  latex_structure_gen:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: false
      system_prompt: |
        You are helping generate a LaTeX document structure for a scientific paper.

        Paper topic: {{ context.paper_details.topic }}
        Target audience: {{ context.paper_details.audience }}

        Table of Contents:
        {{ context.table_of_contents }}

        Generate a complete LaTeX preamble with appropriate document class, packages, and metadata.
        Include packages for: amsmath, graphicx, hyperref, cite, geometry
        Set reasonable margins (1 inch).
        Include title, author, and date fields.

        Output ONLY the LaTeX preamble (from \documentclass to \begin{document}, not including sections).
        After providing the structure, the system will automatically save it.

  latex_structure_saver:
    type: tool
    config:
      tools:
        - name: save_latex_structure
          code: |
            # Save the LaTeX structure/preamble to context
            context['latex_structure'] = input_data
            # Return routing command to continue to latex_controller for next step
            result = "ROUTE_LATEX_CONTINUE:LaTeX structure saved. Proceeding to convert sections..."

  latex_section_converter:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: false
      system_prompt: |
        You are converting a section of the paper to LaTeX format.

        Current section: {{ context.current_latex_section }}

        Section content (markdown/plain text):
        {{ context.section_content.get(context.current_latex_section, '') }}

        Convert this section to LaTeX format:
        - Use \section{} for the section title
        - Escape special characters: & % $ # _ { } ~ ^
        - Convert markdown formatting to LaTeX equivalents
        - Keep citations in proper format [citation]
        - Use proper LaTeX commands for emphasis, bold, etc.

        Output ONLY the LaTeX code for this section (no preamble, no \begin{document}).
        After providing the conversion, the system will automatically save it.

  latex_section_saver:
    type: tool
    config:
      tools:
        - name: save_latex_section
          code: |
            import sys
            print(f"DEBUG latex_section_saver", file=sys.stderr)

            # Save the converted LaTeX section
            current_section = context.get('current_latex_section', '')
            if not context.get('latex_sections'):
                context['latex_sections'] = {}

            context['latex_sections'][current_section] = input_data

            # Increment index to move to next section
            current_index = context.get('current_latex_index', 0)
            context['current_latex_index'] = current_index + 1

            # Return routing command to continue to latex_controller for next step
            latex_preview = input_data.strip() if isinstance(input_data, str) else str(input_data)
            result = (
                f"ROUTE_LATEX_CONTINUE:Saved LaTeX for section: {current_section}\n\n"
                f"{latex_preview}"
            )

  latex_assembler:
    type: llm
    config:
      provider: openai
      model: gpt-3.5-turbo
      memory_enabled: false
      system_prompt: |
        You are assembling the complete LaTeX document from its parts.

        LaTeX preamble:
        {{ context.latex_structure }}

        Sections:
        {% for path in context.section_paths %}
        {{ context.latex_sections.get(path, '') }}
        {% endfor %}

        Combine these into a complete LaTeX document:
        1. Start with the preamble
        2. Add \begin{document}
        3. Add \maketitle
        4. Add all sections in order
        5. End with \end{document}

        Output the COMPLETE LaTeX source code ready for compilation.
        After providing the document, the system will automatically save and compile it.

  latex_assembler_saver:
    type: tool
    config:
      tools:
        - name: save_complete_latex
          code: |
            # Save the complete LaTeX source
            context['latex_source'] = input_data
            result = f"Complete LaTeX document assembled ({len(input_data)} characters). Ready to compile."

  latex_compiler:
    type: tool
    config:
      tools:
        - name: compile_latex
          code: |
            import sys
            import subprocess
            import tempfile
            import os
            import shutil
            print(f"DEBUG latex_compiler", file=sys.stderr)

            latex_source = context.get('latex_source', '')
            if not latex_source:
                result = "ERROR: No LaTeX source to compile"
            else:
                # Create temp directory for compilation
                temp_dir = tempfile.mkdtemp()
                tex_file = os.path.join(temp_dir, 'paper.tex')

                try:
                    # Write LaTeX source
                    with open(tex_file, 'w') as f:
                        f.write(latex_source)

                    # Compile with pdflatex
                    proc = subprocess.run(
                        ['pdflatex', '-interaction=nonstopmode', 'paper.tex'],
                        cwd=temp_dir,
                        capture_output=True,
                        text=True,
                        timeout=60
                    )

                    pdf_file = os.path.join(temp_dir, 'paper.pdf')

                    if os.path.exists(pdf_file):
                        # Success! Move PDF to permanent location
                        output_dir = '/tmp/papers'
                        os.makedirs(output_dir, exist_ok=True)
                        final_pdf = os.path.join(output_dir, 'paper.pdf')
                        shutil.copy(pdf_file, final_pdf)

                        context['latex_compiled'] = True
                        context['pdf_path'] = final_pdf
                        file_size = os.path.getsize(final_pdf)
                        result = f"LaTeX compiled successfully! PDF saved to: {final_pdf} ({file_size} bytes)"
                    else:
                        # Compilation failed - extract errors
                        errors = proc.stdout
                        context['latex_errors'] = errors
                        context['latex_compiled'] = False

                        # Check if we should try to fix
                        fix_attempts = context.get('latex_fix_attempts', 0)
                        if fix_attempts < 3:
                            result = f"ROUTE_FIX_LATEX:LaTeX compilation failed (attempt {fix_attempts + 1}/3). Errors:\n{errors[-1000:]}"
                        else:
                            result = f"LaTeX compilation failed after 3 attempts. Final errors:\n{errors[-1000:]}"

                finally:
                    # Cleanup temp directory
                    shutil.rmtree(temp_dir, ignore_errors=True)

  latex_fixer:
    type: llm
    config:
      provider: openai
      model: gpt-4-turbo
      memory_enabled: false
      system_prompt: |
        You are fixing LaTeX compilation errors.

        Current LaTeX source:
        {{ context.latex_source }}

        Compilation errors:
        {{ context.latex_errors[-2000:] if context.latex_errors else 'No errors available' }}

        Fix the LaTeX errors. Common issues:
        - Unescaped special characters: & % $ # _ { } ~ ^
        - Missing packages (add to preamble)
        - Unclosed environments
        - Invalid commands
        - Missing braces

        Output the COMPLETE corrected LaTeX source code.
        The system will automatically save and retry compilation.

  latex_fixer_saver:
    type: tool
    config:
      tools:
        - name: save_fixed_latex
          code: |
            # Save the fixed LaTeX source
            context['latex_source'] = input_data

            # Increment fix attempts
            fix_attempts = context.get('latex_fix_attempts', 0)
            context['latex_fix_attempts'] = fix_attempts + 1

            # Reset compiled flag so it will try again
            context['latex_compiled'] = False

            # Route back to compiler to retry
            result = f"ROUTE_COMPILE_LATEX:LaTeX fixes applied (attempt {fix_attempts + 1}). Retrying compilation..."

# Routes - Pure LangGraph using message_router for routing
routes:
  main:
    type: graph
    entry_point: start

    nodes:
      start:
        type: START

      end:
        type: END

      # Main message router with user-defined routing patterns
      router:
        type: message_router
        rules:
          # Command handler routing
          - match_type: prefix
            pattern: "GOTO_COMMAND_HANDLER"
            target: command_handler
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_AUTO_FINISH"
            target: auto_finish_driver
            extract_message: true
            separator: ":"

          # Command output (non-routing command results) go directly to end

          - match_type: prefix
            pattern: "COMMAND_OUTPUT"
            target: end
            extract_message: true
            separator: ":"

          # Main workflow routing patterns
          - match_type: prefix
            pattern: "GOTO_INTRO"
            target: intro
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_DISCOVERY"
            target: discovery
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_BRAINSTORMING"
            target: brainstorming
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_VETTING"
            target: vetting
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_STRUCTURE"
            target: structure
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_SECTION_WRITING"
            target: section_writing
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_SOURCE_SELECTOR"
            target: source_selector
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_SOURCE_FINDER"
            target: source_finder
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_SECTION_WRITER"
            target: section_writer
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_SECTION_PROOFREADER"
            target: section_proofreader
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_ACCEPT_SECTION"
            target: section_accept_handler
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "GOTO_PAPER_REVIEW"
            target: paper_review
            extract_message: true
            separator: ":"


          - match_type: prefix
            pattern: "GOTO_LATEX_GENERATION"

            target: latex_generation
            extract_message: true
            separator: ":"

          # Discovery sub-routing patterns
          - match_type: prefix
            pattern: "ROUTE_ASK_TOPIC"
            target: ask_topic
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_ASK_LENGTH"
            target: ask_length
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_ASK_AUDIENCE"
            target: ask_audience
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_ASK_PUBLICATION"
            target: ask_publication
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_ASK_FORMAT"
            target: ask_format
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_ASK_OTHER"
            target: ask_other
            extract_message: true
            separator: ":"

          # Route SET_ responses back to discovery controller for processing
          - match_type: prefix
            pattern: "SET_TOPIC"
            target: discovery
            extract_message: false

          - match_type: prefix
            pattern: "SET_LENGTH"
            target: discovery
            extract_message: false

          - match_type: prefix
            pattern: "SET_AUDIENCE"
            target: discovery
            extract_message: false

          - match_type: prefix
            pattern: "SET_PUBLICATION"
            target: discovery
            extract_message: false

          - match_type: prefix
            pattern: "SET_FORMAT"
            target: discovery
            extract_message: false

          - match_type: prefix
            pattern: "SET_OTHER"
            target: discovery
            extract_message: false

          # Discovery response (final output to user)
          - match_type: prefix
            pattern: "DISCOVERY_RESPONSE"
            target: end
            extract_message: true
            separator: ":"

          # Section writing routing patterns
          - match_type: prefix
            pattern: "ROUTE_PARSE_TOC"
            target: toc_parser
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_SELECT_SOURCES"
            target: source_selector
            extract_message: true
            separator: ":"

          - match_type: contains
            pattern: "SELECT_COMPLETE"
            target: source_finder
            extract_message: false

          - match_type: contains
            pattern: "FIND_COMPLETE"
            target: section_writer
            extract_message: false

          - match_type: prefix
            pattern: "ROUTE_NEXT_SECTION"
            target: auto_finish_driver
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "AUTO_SECTIONS_COMPLETE"
            target: auto_finish_driver
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "AUTO_PAPER_REVIEW_COMPLETE"
            target: auto_finish_driver
            extract_message: true
            separator: ":"

          # Paper review routing

          - match_type: prefix
            pattern: "ROUTE_REVIEW_PAPER"
            target: paper_review_agent
            extract_message: true
            separator: ":"


          # LaTeX generation routing patterns
          - match_type: prefix
            pattern: "ROUTE_GEN_STRUCTURE"
            target: latex_structure_gen
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_CONVERT_SECTION"
            target: latex_section_converter
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_ASSEMBLE_LATEX"
            target: latex_assembler
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_COMPILE_LATEX"
            target: latex_compiler
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_FIX_LATEX"
            target: latex_fixer
            extract_message: true
            separator: ":"

          - match_type: prefix
            pattern: "ROUTE_LATEX_CONTINUE"
            target: latex_generation
            extract_message: true
            separator: ":"

          # LaTeX compilation success/failure patterns
          - match_type: prefix
            pattern: "LaTeX compiled successfully"
            target: end
            extract_message: false

          - match_type: prefix
            pattern: "LaTeX compilation failed after"
            target: end
            extract_message: false

          # Default to workflow controller
          - match_type: suffix
            pattern: ""
            target: workflow_controller
            extract_message: false

      # Agent nodes
      workflow_controller:
        type: actor
        actor: workflow_controller

      command_handler:
        type: actor
        actor: command_handler

      auto_finish_driver:
        type: actor
        actor: auto_finish_driver

      auto_finish_passthrough:
        type: actor
        actor: auto_finish_passthrough

      intro:
        type: actor
        actor: intro_agent


      discovery:
        type: actor
        actor: discovery_controller

      ask_topic:
        type: actor
        actor: ask_topic

      ask_length:
        type: actor
        actor: ask_length

      ask_audience:
        type: actor
        actor: ask_audience

      ask_publication:
        type: actor
        actor: ask_publication

      ask_format:
        type: actor
        actor: ask_format

      ask_other:
        type: actor
        actor: ask_other

      brainstorming:
        type: actor
        actor: brainstorming_agent

      brainstorming_saver:
        type: actor
        actor: brainstorming_saver

      vetting:
        type: actor
        actor: vetting_agent

      vetting_saver:
        type: actor
        actor: vetting_saver

      structure:
        type: actor
        actor: structure_agent

      structure_saver:
        type: actor
        actor: structure_saver

      section_writing:
        type: actor
        actor: section_writing_controller

      # Section writing sub-nodes
      toc_parser:
        type: actor
        actor: toc_parser

      toc_parser_saver:
        type: actor
        actor: toc_parser_saver

      source_selector:
        type: actor
        actor: source_selector

      source_finder:
        type: actor
        actor: source_finder

      source_finder_saver:
        type: actor
        actor: source_finder_saver

      section_writer:
        type: actor
        actor: section_writer

      section_writer_saver:
        type: actor
        actor: section_writer_saver

      section_accept_handler:
        type: actor
        actor: section_accept_handler

      section_proofreader:
        type: actor
        actor: section_proofreader

      section_proofreader_saver:
        type: actor
        actor: section_proofreader_saver

      paper_review:
        type: actor
        actor: paper_review_controller

      paper_review_actor:
        type: actor
        actor: paper_review_agent
        metadata:
          max_history_messages: 10
          max_history_chars: 6000

      paper_review_saver:
        type: actor
        actor: paper_review_saver


      latex_generation:
        type: actor
        actor: latex_controller

      # LaTeX sub-nodes
      latex_structure_gen:
        type: actor
        actor: latex_structure_gen

      latex_structure_saver:
        type: actor
        actor: latex_structure_saver

      latex_section_converter:
        type: actor
        actor: latex_section_converter

      latex_section_saver:
        type: actor
        actor: latex_section_saver

      latex_assembler:
        type: actor
        actor: latex_assembler

      latex_assembler_saver:
        type: actor
        actor: latex_assembler_saver

      latex_compiler:
        type: actor
        actor: latex_compiler

      latex_fixer:
        type: actor
        actor: latex_fixer

      latex_fixer_saver:
        type: actor
        actor: latex_fixer_saver

    # Edge definitions
    edges:
      # Start to router
      - source: start
        target: router

      # Router to all possible targets based on next_node
      - source: router
        target: workflow_controller
        condition:
          type: context_value
          key: next_node
          value: workflow_controller

      - source: router
        target: command_handler
        condition:
          type: context_value
          key: next_node
          value: command_handler

      - source: router
        target: auto_finish_driver
        condition:
          type: context_value
          key: next_node
          value: auto_finish_driver

      - source: router
        target: intro
        condition:
          type: context_value
          key: next_node
          value: intro

      - source: router
        target: discovery
        condition:
          type: context_value
          key: next_node
          value: discovery

      - source: router
        target: ask_topic
        condition:
          type: context_value
          key: next_node
          value: ask_topic

      - source: router
        target: ask_length
        condition:
          type: context_value
          key: next_node
          value: ask_length

      - source: router
        target: ask_audience
        condition:
          type: context_value
          key: next_node
          value: ask_audience

      - source: router
        target: ask_publication
        condition:
          type: context_value
          key: next_node
          value: ask_publication

      - source: router
        target: ask_format
        condition:
          type: context_value
          key: next_node
          value: ask_format

      - source: router
        target: ask_other
        condition:
          type: context_value
          key: next_node
          value: ask_other

      - source: router
        target: brainstorming
        condition:
          type: context_value
          key: next_node
          value: brainstorming

      - source: router
        target: vetting
        condition:
          type: context_value
          key: next_node
          value: vetting

      - source: router
        target: structure
        condition:
          type: context_value
          key: next_node
          value: structure

      - source: router
        target: section_writing
        condition:
          type: context_value
          key: next_node
          value: section_writing

      - source: router
        target: paper_review
        condition:
          type: context_value
          key: next_node
          value: paper_review

      - source: router
        target: latex_generation
        condition:
          type: context_value
          key: next_node
          value: latex_generation

      - source: router
        target: end
        condition:
          type: context_value
          key: next_node
          value: end

      # Section writing sub-routing
      - source: router
        target: toc_parser
        condition:
          type: context_value
          key: next_node
          value: toc_parser

      - source: router
        target: source_selector
        condition:
          type: context_value
          key: next_node
          value: source_selector

      - source: router
        target: source_finder
        condition:
          type: context_value
          key: next_node
          value: source_finder

      - source: router
        target: section_writer
        condition:
          type: context_value
          key: next_node
          value: section_writer

      - source: router
        target: section_proofreader
        condition:
          type: context_value
          key: next_node
          value: section_proofreader

      - source: router
        target: section_accept_handler
        condition:
          type: context_value
          key: next_node
          value: section_accept_handler

      # Paper review sub-routing
      - source: router
        target: paper_review
        condition:
          type: context_value
          key: next_node
          value: paper_review

      - source: router
        target: paper_review_agent
        condition:
          type: context_value
          key: next_node
          value: paper_review_agent


      # LaTeX generation sub-routing
      - source: router
        target: latex_structure_gen
        condition:
          type: context_value
          key: next_node
          value: latex_structure_gen

      - source: router
        target: latex_section_converter
        condition:
          type: context_value
          key: next_node
          value: latex_section_converter

      - source: router
        target: latex_assembler
        condition:
          type: context_value
          key: next_node
          value: latex_assembler

      - source: router
        target: latex_compiler
        condition:
          type: context_value
          key: next_node
          value: latex_compiler

      - source: router
        target: latex_fixer
        condition:
          type: context_value
          key: next_node
          value: latex_fixer

      # All agents return to router for next routing decision
      - source: workflow_controller
        target: router

      - source: command_handler
        target: router

      - source: intro
        target: end

    # Note: command_handler returns to router because some commands
    # (like !next) output routing commands (GOTO_*). The router will
    # handle those and route non-routing outputs to end via the default rule.

      - source: discovery
        target: router

      - source: ask_topic
        target: router

      - source: ask_length
        target: router

      - source: ask_audience
        target: router

      - source: ask_publication
        target: router

      - source: ask_format
        target: router

      - source: ask_other
        target: router

      # Brainstorming chain - output goes directly to end for user interaction
      - source: brainstorming
        target: brainstorming_saver

      - source: brainstorming_saver
        target: auto_finish_passthrough

      # Vetting chain - output goes directly to end for user interaction
      - source: vetting
        target: vetting_saver

      - source: vetting_saver
        target: auto_finish_passthrough

      # Structure chain - output goes directly to end for user interaction
      - source: structure
        target: structure_saver

      - source: structure_saver
        target: auto_finish_passthrough

      # Section writing
      - source: section_writing
        target: router

      # Section writing sub-chains
      - source: toc_parser
        target: toc_parser_saver

      - source: toc_parser_saver
        target: router

      - source: source_selector
        target: auto_finish_passthrough

      - source: source_finder
        target: source_finder_saver

      - source: source_finder_saver
        target: auto_finish_passthrough

      - source: section_writer
        target: section_writer_saver

      - source: section_writer_saver
        target: auto_finish_passthrough

      - source: section_accept_handler
        target: router

      - source: section_proofreader
        target: section_proofreader_saver

      - source: section_proofreader_saver
        target: auto_finish_passthrough

      # Paper review
      - source: paper_review
        target: router

      # Paper review sub-chain
      - source: paper_review_agent
        target: paper_review_saver

      - source: paper_review_saver
        target: auto_finish_passthrough

      - source: auto_finish_passthrough
        target: auto_finish_driver
        condition:
          type: context_value
          key: auto_finish_active
          value: true

      - source: auto_finish_passthrough
        target: end
        condition:
          type: context_value
          key: auto_finish_active
          value: false

      - source: auto_finish_driver
        target: router

      # LaTeX generation

      - source: latex_generation
        target: router

      # LaTeX sub-chains
      - source: latex_structure_gen
        target: latex_structure_saver

      - source: latex_structure_saver
        target: router

      - source: latex_section_converter
        target: latex_section_saver

      - source: latex_section_saver
        target: router

      - source: latex_assembler
        target: latex_assembler_saver

      - source: latex_assembler_saver
        target: auto_finish_passthrough

      - source: latex_compiler
        target: router

      - source: latex_fixer
        target: latex_fixer_saver

      - source: latex_fixer_saver
        target: router

# Output configuration
publications:
  - __output__

merges:
  - sources: [__input__]
    target: main
