Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A9T9 RPA MacroAgentService

From Leeroopedia
Knowledge Sources
Domains AI_Agent, Conversation_Orchestration
Last Updated 2026-10-22 00:00 GMT

Overview

Concrete tool for orchestrating AI-driven macro generation conversations with tool-calling loops, provided by the Ui.Vision macro agent service.

Description

The MacroAgentService class (`src/services/ai/macro_agent/service.ts`) is the conversation orchestration layer for the built-in AI macro agent. It manages conversation history (in the configured provider's format), a generation counter (to orphan abandoned turns), and the sampling loop that calls the AI provider, processes tool calls via `MacroAgentTools`, feeds results back, and iterates until the AI produces a final text response or the loop limit (50 turns) is reached.

Key methods:

  • `getSystemPrompt(xmoduleStatus, localOcrAvailable)`: assembles the system prompt with the default macro agent prompt, JS-first instructions, OCR engine guidance, and an environment line (browser, OS, XModule status)
  • `detectXModuleStatus()`: probes the XModule2 native host for install state (refreshed per turn)
  • `run(userPrompt)`: the main conversation loop — builds initial context (current macro + recent logs), calls the AI provider, dispatches tool calls, iterates
  • `createNewChat()`: reset conversation history and generation
  • `buildInitialContext(prompt)`: gather the current editor macro and recent log tail as context for the AI

The service supports multiple AI providers: Anthropic (Claude), OpenRouter (OpenAI-compatible), local models, and the Ui.Vision free tier.

Usage

Instantiate `MacroAgentService` with a `logMessage` callback, `shouldStop` checker, and screenshot function. Call `run(prompt)` to start a conversation turn. The service drives the tool-calling loop internally.

Code Reference

Source Location

  • Repository: A9T9/RPA
  • File: src/services/ai/macro_agent/service.ts
  • Lines: L79 (agentToolDefs), L81-L127 (getSystemPrompt), L128-L153 (detectXModuleStatus), L154-L944 (MacroAgentService class, run at L235)

Signature

export class MacroAgentService {
  constructor(params: MacroAgentServiceParams)

  run: (prompt: string) => Promise<void>
  createNewChat: () => void
  saveConversation: () => void
  private getSystemPrompt(xmoduleStatus: string, localOcrAvailable: boolean): string
  private detectXModuleStatus(): Promise<string>
  private buildInitialContext(prompt: string): string
}

export const getSystemPrompt: (xmoduleStatus: string, localOcrAvailable: boolean) => string
export const detectXModuleStatus: () => Promise<string>
export const agentToolDefs: () => ToolDefinition[]

Import

import { MacroAgentService, getSystemPrompt, detectXModuleStatus } from '@/services/ai/macro_agent/service'

I/O Contract

Inputs

Name Type Required Description
prompt string Yes User's natural language automation request
logMessage Function Yes Callback to log messages to the chat UI
shouldStop Function Yes Callback to check if the run should abort
captureScreenShotFunction Function Yes Screenshot capture for visual tools

Outputs

Name Type Description
conversation void The run drives tool calls and logs results via callbacks; no direct return

Usage Examples

Run an AI Macro Agent Conversation

import { MacroAgentService } from '@/services/ai/macro_agent/service'

const service = new MacroAgentService({
  logMessage: (msg, type, actionOrResult, image) => {
    // Render message in the AI chat panel
    appendChatMessage(msg, type, actionOrResult, image)
  },
  shouldStop: () => stopRequested,
  captureScreenShotFunction: async (opts) => {
    return await screenshotService.captureScreen(tabId, dpr)
  }
})

// Start a conversation turn:
await service.run('Log into the demo site and take a screenshot of the dashboard')

Reset Conversation

service.createNewChat() // Clear history and start fresh

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment