Skip to content

Self-improving AI Employees for everyone

14.3 KB
Raw Blame
"""RLM execution service.

Follows the same interface as AIService.execute_agent() / execute_chat_agent()
but replaces the agent loop with RLM's REPL-based execution.
"""

import asyncio
import time
from typing import Dict, Any, List, Optional

from core.logging import get_logger
from .adapters import BackendAdapter, ChatModelExtractor, ToolBridgeAdapter
from .constants import DEFAULT_MAX_ITERATIONS, DEFAULT_MAX_DEPTH, DEFAULT_VERBOSE

logger = get_logger(__name__)


class RLMService:
    """Dedicated service for RLM agent execution.

    Injected into AIService via dependency injection,
    following the same pattern as MapsService, TextService, etc.
    """

    def __init__(self, auth=None):
        self.auth = auth

    async def execute(
        self,
        node_id: str,
        parameters: Dict[str, Any],
        memory_data: Optional[Dict[str, Any]] = None,
        context_data: Optional[Dict[str, Any]] = None,
        skill_data: Optional[List[Dict[str, Any]]] = None,
        tool_data: Optional[List[Dict[str, Any]]] = None,
        broadcaster=None,
        workflow_id: Optional[str] = None,
        context: Optional[Dict[str, Any]] = None,
        database=None,
        ai_service=None,
    ) -> Dict[str, Any]:
        """Execute RLM agent completion.

        Same interface as AIService.execute_agent() / execute_chat_agent().
        Reuses existing OpenCompany utilities for shared concerns.
        """
        start_time = time.time()
        context_bridge = None
        route = None
        if (context_data or {}).get("kind") == "context":
            from services.cli_agent.context_bridge import (
                SpecializedAgentContextBridge,
            )

            context_bridge = await SpecializedAgentContextBridge.resolve(
                database,
                context_data,
                provider="rlm",
                agent_node_id=node_id,
            )

        async def broadcast_status(phase: str, details: Dict[str, Any] = None):
            if broadcaster:
                await broadcaster.update_node_status(
                    node_id, "executing", {"phase": phase, "agent_type": "rlm", **(details or {})}, workflow_id=workflow_id
                )

        try:
            # === Parameter extraction (same pattern as execute_chat_agent) ===
            prompt = parameters.get("prompt", "")
            system_message = parameters.get("system_message", "")

            # Personality skills remain eager. Standard skills are added to
            # the same callable tool surface as every other connected tool.
            from services.ai import _build_skill_system_prompt

            supplementary_context = ""
            skill_prompt, has_personality = _build_skill_system_prompt(skill_data, log_prefix="[RLM]")
            if skill_prompt:
                supplementary_context += f"\n\n## Skill Instructions\n{skill_prompt}"

            # Options flattening (same as execute_chat_agent line 1995)
            options = parameters.get("options", {})
            flattened = {**parameters, **options}

            # Provider/model/API key resolution (same as lines 1998-2014)
            from services.llm.config import (
                get_default_model_async,
                is_model_valid_for_provider,
            )

            api_key = flattened.get("api_key")
            provider = parameters.get("provider", "openai")
            model = parameters.get("model", "")

            # RLM builds its own clients from constants.PROVIDER_TO_BACKEND
            # and never reads a user base URL, so any other provider (a
            # local server, a named endpoint) would silently be sent to
            # api.openai.com with that provider's key.
            from .constants import PROVIDER_TO_BACKEND

            if provider not in PROVIDER_TO_BACKEND:
                raise ValueError(
                    f"The RLM Agent cannot run on provider {provider!r}. "
                    f"Choose one of: {', '.join(sorted(PROVIDER_TO_BACKEND))}."
                )

            if not model or not is_model_valid_for_provider(model, provider):
                model = await get_default_model_async(provider, database)

            if not api_key and self.auth:
                api_key = await self.auth.resolve_api_key(provider)
            if not api_key:
                # Own keys first. The RLM library reaches a custom URL only
                # in OpenAI's format, which the gateway serves for every
                # provider it serves.
                from services.gateway.routing import gateway_route

                route = await gateway_route(provider, openai_format=True)
            if not api_key and route is None:
                raise ValueError(f"API key required for RLM Agent (provider: {provider})")

            await broadcast_status(
                "initializing",
                {
                    "message": f"Initializing RLM with {provider}/{model}",
                    "provider": provider,
                    "model": model,
                    "active_skills": [],
                    "last_skills": [],
                    "last_tool_name": None,
                    "last_capability": None,
                },
            )

            # === Memory injection (appended to supplementary context, not system prompt) ===
            if memory_data and memory_data.get("memory_content"):
                memory_text = memory_data["memory_content"]
                supplementary_context += f"\n\n## Conversation History\n{memory_text}"

            # === Adapter: Map provider to RLM backend ===
            backend, backend_kwargs = BackendAdapter.adapt(provider, model, api_key, route=route)

            # === Adapter: Extract chat model nodes as other_backends ===
            other_backends, other_backend_kwargs = await ChatModelExtractor.extract(tool_data, self.auth)

            # === Adapter: Bridge tool nodes as RLM custom_tools ===
            await broadcast_status("building_tools", {"message": "Bridging connected tools..."})
            running_loop = asyncio.get_running_loop()
            from services.skill_runtime import skill_tool_info

            effective_tool_data = list(tool_data or [])
            if (context or {}).get("pi_harness_version") == 1 and any(item.get("node_type") == "coding_agent" for item in effective_tool_data):
                effective_tool_data.append({"node_type": "_builtin_check_delegated_tasks", "node_id": "",
                                            "parameters": {}, "label": "Check Delegated Tasks"})
            progressive_skill_tool = skill_tool_info(skill_data or [], node_id)
            if progressive_skill_tool:
                effective_tool_data.append(progressive_skill_tool)
            if ai_service is not None:
                from services.plugin.tool import node_tool_bindings

                canonical_tool_data = []
                for tool_info in effective_tool_data:
                    if await node_tool_bindings(tool_info) is not None:
                        raise ValueError(
                            f"An RLM agent can't use {tool_info.get('label') or tool_info.get('node_type')!r}: "
                            "it gives several tools. Connect it to an AI Agent instead."
                        )
                    tool, execution = await ai_service._build_tool_from_node(
                        tool_info
                    )
                    if tool is None or execution is None:
                        raise ValueError(
                            "Could not build connected tool "
                            f"{tool_info.get('node_type')!r}"
                        )
                    canonical_tool_data.append(
                        {
                            **tool_info,
                            "_agent_tool_name": tool.name,
                            "_agent_tool_description": tool.description,
                            "_agent_tool_schema": tool.parameters,
                            "_agent_tool_input_model": tool.args_schema,
                            "_agent_tool_execution": execution,
                        }
                    )
                effective_tool_data = canonical_tool_data

            custom_tools = ToolBridgeAdapter.bridge(
                effective_tool_data,
                context,
                loop=running_loop,
                broadcaster=broadcaster,
                parent_node_id=node_id,
                workflow_id=workflow_id,
                provider=provider,
                ambiguous_outcome_sink=None,
            )

            # === RLM-specific parameters ===
            max_iterations = int(flattened.get("max_iterations", DEFAULT_MAX_ITERATIONS))
            max_depth = int(flattened.get("max_depth", DEFAULT_MAX_DEPTH))
            max_budget = flattened.get("max_budget")
            max_timeout = flattened.get("max_timeout")
            max_tokens = flattened.get("max_tokens")
            verbose = flattened.get("verbose", DEFAULT_VERBOSE)

            # === Run RLM in thread pool (RLM is synchronous) ===
            await broadcast_status(
                "executing",
                {
                    "message": f"RLM executing (max {max_iterations} iterations)...",
                    "max_iterations": max_iterations,
                    "max_depth": max_depth,
                    "tool_count": len(custom_tools),
                },
            )

            # Build augmented prompt: OpenCompany context prepended to user prompt.
            # We do NOT pass custom_system_prompt to RLM because RLM_SYSTEM_PROMPT
            # uses Python .format() with {custom_tools_section} placeholder and
            # double-escaped braces {{chunk}} in examples. Appending to the template
            # and letting build_rlm_system_prompt() .format() it would break on any
            # curly braces in the appended text (skills, memory, system message).
            # Instead, let RLM use its default system prompt (with proper tool
            # formatting) and inject OpenCompany context into the user prompt.
            augmented_prompt = prompt
            if system_message:
                augmented_prompt = f"## Instructions\n{system_message}\n\n## Task\n{augmented_prompt}"
            if supplementary_context:
                augmented_prompt = f"{supplementary_context}\n\n{augmented_prompt}"
            if context_bridge is not None:
                augmented_prompt = context_bridge.augment_prompt(
                    augmented_prompt
                )

            def _run_rlm():
                from rlm import RLM
                from rlm.logger import RLMLogger

                rlm_instance = RLM(
                    backend=backend,
                    backend_kwargs=backend_kwargs,
                    environment="local",
                    other_backends=other_backends if other_backends else None,
                    other_backend_kwargs=other_backend_kwargs if other_backend_kwargs else None,
                    max_iterations=max_iterations,
                    max_depth=max_depth,
                    max_budget=float(max_budget) if max_budget else None,
                    max_timeout=float(max_timeout) if max_timeout else None,
                    max_tokens=int(max_tokens) if max_tokens else None,
                    custom_tools=custom_tools if custom_tools else None,
                    logger=RLMLogger(),
                    verbose=verbose,
                )
                return rlm_instance.completion(augmented_prompt)

            result = await asyncio.to_thread(_run_rlm)

            if context_bridge is not None and result.response:
                # Best-effort: the provider already ran and billed, so a
                # save failure must not fail the run.
                try:
                    await context_bridge.record_turn(prompt, result.response)
                except Exception as exc:
                    logger.warning(
                        "[RLM] conversation save failed; continuing: %s", exc
                    )

            # === Memory save (same pattern as execute_chat_agent) ===
            if memory_data and memory_data.get("node_id"):
                from services.memory_store import add_message

                session_id = memory_data.get("session_id", "default")
                add_message(session_id, "user", prompt)
                add_message(session_id, "assistant", result.response or "")

            execution_time = time.time() - start_time
            iterations = len(result.metadata.get("iterations", [])) if result.metadata else 0

            await broadcast_status(
                "completed",
                {
                    "message": f"RLM completed in {execution_time:.1f}s",
                    "iterations": iterations,
                },
            )

            return {
                "success": True,
                "node_id": node_id,
                "node_type": "rlm_agent",
                "result": {
                    "response": result.response,
                    "model": model,
                    "provider": provider,
                    "total_cost": result.usage_summary.total_cost,
                    "total_input_tokens": result.usage_summary.total_input_tokens,
                    "total_output_tokens": result.usage_summary.total_output_tokens,
                    "execution_time": execution_time,
                    "iterations": iterations,
                },
                "execution_time": execution_time,
            }

        except Exception as e:
            public_error = str(e)
            if route is not None:
                from services.gateway.errors import describe

                # The vendor's text would point at the wrong key and account.
                public_error = describe(e) or public_error
            logger.error(
                "[RLM] Execution failed type=%s",
                type(e).__name__,
                exc_info=True,
            )
            execution_time = time.time() - start_time
            await broadcast_status("error", {"message": public_error})
            return {
                "success": False,
                "node_id": node_id,
                "node_type": "rlm_agent",
                "error": public_error,
                "execution_time": execution_time,
            }
        finally:
            from services.skill_runtime import clear_skill_turn

            await clear_skill_turn(
                workflow_id or "",
                str((context or {}).get("execution_id") or ""),
                node_id,
            )