Implementation:A9T9 RPA MacroAgentService
| 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
- Principle:A9T9_RPA_System_Prompt_Assembly
- Principle:A9T9_RPA_AI_Conversation_Initiation
- Principle:A9T9_RPA_Macro_Finalization
Requires Environment
- Environment:A9T9_RPA_Chromium_Browser_Environment
- Environment:A9T9_RPA_Firefox_Browser_Environment
- Environment:A9T9_RPA_Desktop_Automation_XModule2