|
| 1 | +import z from "zod" |
| 2 | +import { Tool } from "./tool" |
| 3 | +import { Session } from "../session" |
| 4 | +import { SessionPrompt } from "../session/prompt" |
| 5 | +import { Agent } from "../agent/agent" |
| 6 | +import { Identifier } from "../id/id" |
| 7 | +import { Log } from "../util/log" |
| 8 | + |
| 9 | +const log = Log.create({ service: "rlm-repl" }) |
| 10 | + |
| 11 | +const DESCRIPTION = `Execute JavaScript code with access to sub-LLM calls for recursive processing. |
| 12 | +
|
| 13 | +This tool enables the Recursive Language Model (RLM) pattern - writing code that programmatically |
| 14 | +invokes sub-LLM calls in loops, rather than requiring explicit tool calls for each invocation. |
| 15 | +
|
| 16 | +## Available Functions |
| 17 | +
|
| 18 | +- \`sub_llm(prompt, agent?)\` - Invoke a sub-LLM call, returns promise with string result |
| 19 | +- \`sub_llm_parallel(prompts[], agent?)\` - Invoke multiple sub-LLM calls in parallel |
| 20 | +- \`context.store(key, data)\` - Store data externally (not in LLM context), returns the key |
| 21 | +- \`context.load(key)\` - Load data by key |
| 22 | +- \`context.chunk(key, chunkSize)\` - Split stored data into chunks, returns array of keys |
| 23 | +- \`context.keys()\` - List all stored keys |
| 24 | +
|
| 25 | +## Example: Processing Large Data |
| 26 | +
|
| 27 | +\`\`\`javascript |
| 28 | +// Store large data externally |
| 29 | +context.store("input", largeDataString) |
| 30 | +
|
| 31 | +// Chunk it into manageable pieces |
| 32 | +const chunkKeys = context.chunk("input", 5000) |
| 33 | +
|
| 34 | +// Process each chunk with a sub-LLM |
| 35 | +const results = await sub_llm_parallel( |
| 36 | + chunkKeys.map(k => \`Analyze this chunk: \${context.load(k)}\`) |
| 37 | +) |
| 38 | +
|
| 39 | +// Return aggregated result |
| 40 | +return results.join("\\n") |
| 41 | +\`\`\` |
| 42 | +
|
| 43 | +## Example: Recursive Search |
| 44 | +
|
| 45 | +\`\`\`javascript |
| 46 | +async function search(query, depth = 0) { |
| 47 | + if (depth > 3) return "Max depth reached" |
| 48 | + const result = await sub_llm(\`Search for: \${query}\`) |
| 49 | + if (result.includes("FOUND")) return result |
| 50 | + return search(query + " (refined)", depth + 1) |
| 51 | +} |
| 52 | +return await search("API endpoints") |
| 53 | +\`\`\` |
| 54 | +
|
| 55 | +## Security Notes |
| 56 | +- Code runs in a sandboxed environment with limited globals |
| 57 | +- Maximum 50 sub_llm calls per execution |
| 58 | +- 5 minute timeout on total execution |
| 59 | +- Context store limited to 10MB total |
| 60 | +` |
| 61 | + |
| 62 | +const parameters = z.object({ |
| 63 | + code: z.string().describe("JavaScript code to execute. Must return a value or use 'return' statement."), |
| 64 | + agent: z.string().optional().describe("Default agent for sub_llm calls. Defaults to 'explore'."), |
| 65 | +}) |
| 66 | + |
| 67 | +// Context store for pointer-based data access |
| 68 | +class RLMContext { |
| 69 | + private store: Map<string, string> = new Map() |
| 70 | + private totalSize: number = 0 |
| 71 | + private readonly maxSize: number = 10 * 1024 * 1024 // 10MB |
| 72 | + |
| 73 | + storeData(key: string, data: string): string { |
| 74 | + if (this.store.has(key)) { |
| 75 | + this.totalSize -= this.store.get(key)!.length |
| 76 | + } |
| 77 | + if (this.totalSize + data.length > this.maxSize) { |
| 78 | + throw new Error(`Context store limit exceeded (max ${this.maxSize / 1024 / 1024}MB)`) |
| 79 | + } |
| 80 | + this.store.set(key, data) |
| 81 | + this.totalSize += data.length |
| 82 | + return key |
| 83 | + } |
| 84 | + |
| 85 | + loadData(key: string): string | undefined { |
| 86 | + return this.store.get(key) |
| 87 | + } |
| 88 | + |
| 89 | + chunkData(key: string, chunkSize: number): string[] { |
| 90 | + const data = this.store.get(key) |
| 91 | + if (!data) throw new Error(`Key not found: ${key}`) |
| 92 | + |
| 93 | + const chunks: string[] = [] |
| 94 | + for (let i = 0; i < data.length; i += chunkSize) { |
| 95 | + const chunkKey = `${key}_chunk_${chunks.length}` |
| 96 | + const chunkData = data.slice(i, i + chunkSize) |
| 97 | + this.storeData(chunkKey, chunkData) |
| 98 | + chunks.push(chunkKey) |
| 99 | + } |
| 100 | + return chunks |
| 101 | + } |
| 102 | + |
| 103 | + listKeys(): string[] { |
| 104 | + return Array.from(this.store.keys()) |
| 105 | + } |
| 106 | + |
| 107 | + clear(): void { |
| 108 | + this.store.clear() |
| 109 | + this.totalSize = 0 |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export const RLMReplTool = Tool.define("rlm_repl", async () => { |
| 114 | + return { |
| 115 | + description: DESCRIPTION, |
| 116 | + parameters, |
| 117 | + async execute(params: z.infer<typeof parameters>, ctx) { |
| 118 | + const defaultAgent = params.agent ?? "explore" |
| 119 | + const rlmContext = new RLMContext() |
| 120 | + let subLLMCallCount = 0 |
| 121 | + const maxSubLLMCalls = 50 |
| 122 | + const startTime = Date.now() |
| 123 | + const maxExecutionTime = 5 * 60 * 1000 // 5 minutes |
| 124 | + |
| 125 | + // Check if we're within time limit |
| 126 | + const checkTimeout = () => { |
| 127 | + if (Date.now() - startTime > maxExecutionTime) { |
| 128 | + throw new Error("Execution timeout exceeded (5 minutes)") |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Sub-LLM call function |
| 133 | + const sub_llm = async (prompt: string, agent?: string): Promise<string> => { |
| 134 | + checkTimeout() |
| 135 | + subLLMCallCount++ |
| 136 | + if (subLLMCallCount > maxSubLLMCalls) { |
| 137 | + throw new Error(`Maximum sub_llm calls exceeded (${maxSubLLMCalls})`) |
| 138 | + } |
| 139 | + |
| 140 | + const agentName = agent ?? defaultAgent |
| 141 | + const agentInfo = await Agent.get(agentName) |
| 142 | + if (!agentInfo) { |
| 143 | + throw new Error(`Unknown agent: ${agentName}`) |
| 144 | + } |
| 145 | + |
| 146 | + log.info("sub_llm call", { |
| 147 | + callNumber: subLLMCallCount, |
| 148 | + agent: agentName, |
| 149 | + promptLength: prompt.length, |
| 150 | + }) |
| 151 | + |
| 152 | + // Create a child session for the sub-LLM call |
| 153 | + const session = await Session.create({ |
| 154 | + parentID: ctx.sessionID, |
| 155 | + title: `RLM sub-call #${subLLMCallCount}`, |
| 156 | + permission: [ |
| 157 | + { permission: "task", pattern: "*", action: "deny" as const }, |
| 158 | + { permission: "rlm_repl", pattern: "*", action: "deny" as const }, |
| 159 | + { permission: "todowrite", pattern: "*", action: "deny" as const }, |
| 160 | + { permission: "todoread", pattern: "*", action: "deny" as const }, |
| 161 | + ], |
| 162 | + }) |
| 163 | + |
| 164 | + try { |
| 165 | + const result = await SessionPrompt.prompt({ |
| 166 | + messageID: Identifier.ascending("message"), |
| 167 | + sessionID: session.id, |
| 168 | + agent: agentName, |
| 169 | + parts: [{ type: "text", text: prompt }], |
| 170 | + }) |
| 171 | + |
| 172 | + // Extract text from result |
| 173 | + const textPart = result.parts.find((p: any) => p.type === "text") |
| 174 | + return (textPart as any)?.text ?? "" |
| 175 | + } catch (error) { |
| 176 | + log.error("sub_llm call failed", { error, callNumber: subLLMCallCount }) |
| 177 | + throw error |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // Parallel sub-LLM calls |
| 182 | + const sub_llm_parallel = async (prompts: string[], agent?: string): Promise<string[]> => { |
| 183 | + checkTimeout() |
| 184 | + if (subLLMCallCount + prompts.length > maxSubLLMCalls) { |
| 185 | + throw new Error( |
| 186 | + `Would exceed maximum sub_llm calls (${maxSubLLMCalls}). Current: ${subLLMCallCount}, Requested: ${prompts.length}`, |
| 187 | + ) |
| 188 | + } |
| 189 | + return Promise.all(prompts.map((p) => sub_llm(p, agent))) |
| 190 | + } |
| 191 | + |
| 192 | + // Context API for pointer-based access |
| 193 | + const context = { |
| 194 | + store: (key: string, data: string) => rlmContext.storeData(key, data), |
| 195 | + load: (key: string) => rlmContext.loadData(key), |
| 196 | + chunk: (key: string, chunkSize: number) => rlmContext.chunkData(key, chunkSize), |
| 197 | + keys: () => rlmContext.listKeys(), |
| 198 | + } |
| 199 | + |
| 200 | + // Build the sandbox with limited globals |
| 201 | + const sandbox = { |
| 202 | + sub_llm, |
| 203 | + sub_llm_parallel, |
| 204 | + context, |
| 205 | + console: { |
| 206 | + log: (...args: any[]) => log.info("rlm_repl console.log", { args }), |
| 207 | + error: (...args: any[]) => log.error("rlm_repl console.error", { args }), |
| 208 | + warn: (...args: any[]) => log.info("rlm_repl console.warn", { args }), |
| 209 | + }, |
| 210 | + JSON, |
| 211 | + Array, |
| 212 | + Object, |
| 213 | + String, |
| 214 | + Number, |
| 215 | + Boolean, |
| 216 | + Date, |
| 217 | + Math, |
| 218 | + Promise, |
| 219 | + Map, |
| 220 | + Set, |
| 221 | + RegExp, |
| 222 | + Error, |
| 223 | + parseInt, |
| 224 | + parseFloat, |
| 225 | + isNaN, |
| 226 | + isFinite, |
| 227 | + encodeURIComponent, |
| 228 | + decodeURIComponent, |
| 229 | + setTimeout: undefined, // Disabled for safety |
| 230 | + setInterval: undefined, // Disabled for safety |
| 231 | + fetch: undefined, // Disabled - use sub_llm for external calls |
| 232 | + require: undefined, // Disabled |
| 233 | + import: undefined, // Disabled |
| 234 | + eval: undefined, // Disabled |
| 235 | + Function: undefined, // Disabled to prevent sandbox escape |
| 236 | + } |
| 237 | + |
| 238 | + try { |
| 239 | + // Wrap code in async function to support await and return |
| 240 | + const wrappedCode = ` |
| 241 | + return (async () => { |
| 242 | + ${params.code} |
| 243 | + })() |
| 244 | + ` |
| 245 | + |
| 246 | + // Create function with sandbox as scope |
| 247 | + // Note: This is a simplified sandbox. For production, consider using |
| 248 | + // isolated-vm or vm2 for stronger isolation. |
| 249 | + const sandboxKeys = Object.keys(sandbox) |
| 250 | + const sandboxValues = Object.values(sandbox) |
| 251 | + |
| 252 | + const asyncFn = new Function(...sandboxKeys, wrappedCode) |
| 253 | + const result = await asyncFn(...sandboxValues) |
| 254 | + |
| 255 | + const output = |
| 256 | + typeof result === "string" ? result : result === undefined ? "(no return value)" : JSON.stringify(result, null, 2) |
| 257 | + |
| 258 | + log.info("rlm_repl completed", { |
| 259 | + subLLMCalls: subLLMCallCount, |
| 260 | + executionTimeMs: Date.now() - startTime, |
| 261 | + contextKeys: rlmContext.listKeys().length, |
| 262 | + }) |
| 263 | + |
| 264 | + return { |
| 265 | + title: `RLM execution (${subLLMCallCount} sub-calls)`, |
| 266 | + metadata: { |
| 267 | + subLLMCalls: subLLMCallCount, |
| 268 | + executionTimeMs: Date.now() - startTime, |
| 269 | + contextKeys: rlmContext.listKeys(), |
| 270 | + }, |
| 271 | + output: [ |
| 272 | + output, |
| 273 | + "", |
| 274 | + `<rlm_metadata>`, |
| 275 | + `sub_llm_calls: ${subLLMCallCount}`, |
| 276 | + `execution_time_ms: ${Date.now() - startTime}`, |
| 277 | + `context_keys: ${rlmContext.listKeys().length}`, |
| 278 | + `</rlm_metadata>`, |
| 279 | + ].join("\n"), |
| 280 | + } |
| 281 | + } catch (error) { |
| 282 | + const message = error instanceof Error ? error.message : String(error) |
| 283 | + log.error("rlm_repl execution failed", { error: message }) |
| 284 | + |
| 285 | + return { |
| 286 | + title: "RLM execution failed", |
| 287 | + metadata: { |
| 288 | + error: message, |
| 289 | + subLLMCalls: subLLMCallCount, |
| 290 | + executionTimeMs: Date.now() - startTime, |
| 291 | + contextKeys: rlmContext.listKeys(), |
| 292 | + }, |
| 293 | + output: `Error: ${message}\n\nSub-LLM calls made before error: ${subLLMCallCount}`, |
| 294 | + } |
| 295 | + } finally { |
| 296 | + rlmContext.clear() |
| 297 | + } |
| 298 | + }, |
| 299 | + } |
| 300 | +}) |
0 commit comments