-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathralph-loop-tool.ts
More file actions
221 lines (196 loc) · 7.13 KB
/
ralph-loop-tool.ts
File metadata and controls
221 lines (196 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
import { Type } from "@sinclair/typebox";
import type { Static } from "@sinclair/typebox";
import { PtyTerminalSession } from "pi-interactive-shell/pty-session";
import { access, readFile } from "node:fs/promises";
import { join } from "node:path";
import type { LoopManager } from "./loop-manager";
export const ralphLoopSchema = Type.Object({
prompt: Type.String({ description: "What you want ralph to do" }),
directory: Type.String({ description: "Project directory to run ralph in" }),
config: Type.Optional(Type.String({ description: "ralph config source (default: ralph.yml)" })),
maxIterations: Type.Optional(Type.Integer({ minimum: 1, description: "Maximum iterations" })),
backend: Type.Optional(Type.String({ description: "Backend/agent selector (passed to --agent)" })),
customArgs: Type.Optional(Type.Array(Type.String(), { description: "Extra args passed after --" })),
});
export type RalphLoopInput = Static<typeof ralphLoopSchema>;
function shQuote(s: string): string {
// POSIX-ish single-quote escaping.
return `'${s.replace(/'/g, `'"'"'`)}'`;
}
function buildRalphRunCommand(params: RalphLoopInput): string {
// TUI is enabled by default in ralph v2.4.4+; no --tui flag needed.
const parts: string[] = ["ralph", "run", "-p", params.prompt];
if (params.config) {
parts.push("--config", params.config);
}
if (params.maxIterations) {
parts.push("--max-iterations", String(params.maxIterations));
}
if (params.backend) {
parts.push("--agent", params.backend);
}
if (params.customArgs && params.customArgs.length > 0) {
parts.push("--", ...params.customArgs);
}
// Quote every arg except the program itself.
const [bin, ...args] = parts;
return `${bin} ${args.map(shQuote).join(" ")}`;
}
function extractLoopIds(jsonText: string): string[] {
const trimmed = jsonText.trim();
if (!trimmed) return [];
const data = JSON.parse(trimmed);
if (!Array.isArray(data)) return [];
const ids: string[] = [];
for (const item of data as any[]) {
const id = typeof item?.id === "string" ? item.id : typeof item?.loop_id === "string" ? item.loop_id : undefined;
if (id) ids.push(id);
}
return ids;
}
/**
* Try to discover a loop ID after spawning ralph.
*
* Strategy:
* 1. Check `ralph loops list --json` for new parallel loops (worktree-based).
* 2. Read `.ralph/current-loop-id` for the primary loop ID.
*
* `ralph loops list` only tracks parallel/worktree loops, not the primary loop
* started by `ralph run`. The primary loop writes its ID to `.ralph/current-loop-id`.
*/
async function waitForNewLoopId(
pi: ExtensionAPI,
beforeIds: Set<string>,
directory: string,
beforePrimaryId: string | null,
timeoutMs = 5000,
): Promise<string | null> {
const started = Date.now();
while (Date.now() - started < timeoutMs) {
// Check parallel loops first.
const res = await pi.exec("ralph", ["loops", "list", "--json"], { timeout: 5000 });
if (res.code === 0) {
try {
const afterIds = extractLoopIds(res.stdout ?? "");
const diff = afterIds.filter((id) => !beforeIds.has(id));
if (diff.length > 0) return diff[0]!;
} catch {
// ignore parse errors, retry
}
}
// Check primary loop ID file.
try {
const idFile = join(directory, ".ralph", "current-loop-id");
const currentId = (await readFile(idFile, "utf-8")).trim();
if (currentId && currentId !== beforePrimaryId) return currentId;
} catch {
// file doesn't exist yet, retry
}
await new Promise((r) => setTimeout(r, 400));
}
return null;
}
export function registerRalphLoopTool(pi: ExtensionAPI, opts: { getLoopManager: () => LoopManager | null; isRalphAvailable: () => boolean }) {
pi.registerTool({
name: "ralph_loop",
label: "Ralph Loop",
description: "Start a ralph loop in the background (ralph run).",
parameters: ralphLoopSchema,
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
if (!opts.isRalphAvailable()) {
return {
content: [{ type: "text", text: "ralph not found in PATH. Install ralph to use ralph_loop." }],
details: { error: "ralph-not-found" },
isError: true,
};
}
const loopManager = opts.getLoopManager();
if (!loopManager) {
return {
content: [{ type: "text", text: "pi-ralph not initialized (LoopManager missing)." }],
details: { error: "loop-manager-missing" },
isError: true,
};
}
// Validate directory exists/readable.
try {
await access(params.directory);
} catch {
return {
content: [{ type: "text", text: `Directory not found or not accessible: ${params.directory}` }],
details: { error: "invalid-directory", directory: params.directory },
isError: true,
};
}
if (signal?.aborted) {
return { content: [{ type: "text", text: "Cancelled" }], details: { cancelled: true } };
}
// Snapshot existing loop IDs so we can detect the new one.
const before = await pi.exec("ralph", ["loops", "list", "--json"], { timeout: 5000, signal });
const beforeIds = new Set<string>();
if (before.code === 0) {
try {
for (const id of extractLoopIds(before.stdout ?? "")) beforeIds.add(id);
} catch {
// ignore
}
}
// Read current primary loop ID before spawning.
let beforePrimaryId: string | null = null;
try {
const idFile = join(params.directory, ".ralph", "current-loop-id");
beforePrimaryId = (await readFile(idFile, "utf-8")).trim() || null;
} catch {
// no previous primary loop
}
const command = buildRalphRunCommand(params);
// Spawn in a PTY so we can embed the native TUI later.
const session = new PtyTerminalSession(
{
command,
cwd: params.directory,
cols: 120,
rows: 40,
scrollback: 5000,
ansiReemit: true,
},
{
onExit: () => {
// LoopManager polling will pick up state change. Keep session for viewing.
// No-op here for now.
},
},
);
// Try to discover loop ID shortly after spawn.
const loopId = await waitForNewLoopId(pi, beforeIds, params.directory, beforePrimaryId);
const id = loopId ?? `pid-${session.pid}`;
loopManager.upsertToolLoop({
id,
pid: session.pid,
directory: params.directory,
ptySession: session,
});
if (ctx.hasUI) {
ctx.ui.notify(`Started ralph loop ${id}`, "info");
}
return {
content: [
{
type: "text",
text:
loopId
? `Started ralph loop ${loopId} (pid ${session.pid}) in ${params.directory}`
: `Started ralph (pid ${session.pid}) in ${params.directory}. Loop ID not detected yet; using ${id}.`,
},
],
details: {
loopId: id,
pid: session.pid,
directory: params.directory,
command,
},
};
},
});
}