-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathprompt.ts
More file actions
201 lines (170 loc) · 5.48 KB
/
Copy pathprompt.ts
File metadata and controls
201 lines (170 loc) · 5.48 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
import { existsSync } from "node:fs";
import { join } from "node:path";
import { loadBoundaries, loadProjectContext, loadRules } from "../config/loader.ts";
import { getBrowserInstructions, isBrowserAvailable } from "./browser.ts";
interface PromptOptions {
task: string;
autoCommit?: boolean;
workDir?: string;
browserEnabled?: "auto" | "true" | "false";
skipTests?: boolean;
skipLint?: boolean;
prdFile?: string;
}
/**
* Detect skill/playbook directories that can guide the agent.
* We keep this engine-agnostic: OpenCode can load skills via `skill` tool,
* other engines can still read these docs as repo guidance.
*/
function detectAgentSkills(workDir: string): string[] {
const candidates = [
join(workDir, ".opencode", "skills"),
join(workDir, ".claude", "skills"),
join(workDir, ".github", "skills"),
join(workDir, ".skills"),
];
return candidates.filter((p) => existsSync(p));
}
/**
* Build the full prompt with project context, rules, boundaries, and task
*/
export function buildPrompt(options: PromptOptions): string {
const {
task,
autoCommit = true,
workDir = process.cwd(),
browserEnabled = "auto",
skipTests = false,
skipLint = false,
prdFile,
} = options;
const parts: string[] = [];
// Add project context if available
const context = loadProjectContext(workDir);
if (context) {
parts.push(`## Project Context\n${context}`);
}
// Add rules if available
const rules = loadRules(workDir);
if (rules.length > 0) {
parts.push(`## Rules (you MUST follow these)\n${rules.join("\n")}`);
}
// Add boundaries
const boundaries = loadBoundaries(workDir);
if (boundaries.length > 0) {
parts.push(`## Boundaries\nDo NOT modify these files/directories:\n${boundaries.join("\n")}`);
}
// Agent skills/playbooks (optional)
const skillRoots = detectAgentSkills(workDir);
if (skillRoots.length > 0) {
parts.push(
[
"## Agent Skills",
"This repo includes skill/playbook docs that describe preferred patterns, workflows, or tooling:",
...skillRoots.map((p) => `- ${p}`),
"",
"Before you start coding:",
"- Read and follow any relevant skill docs from the paths above.",
"- If your engine supports a `skill` tool (e.g. OpenCode), use it to load the relevant skills before implementing.",
"- If none apply, continue normally.",
].join("\n"),
);
}
// Add browser instructions if available
if (isBrowserAvailable(browserEnabled)) {
parts.push(getBrowserInstructions());
}
// Add the task
parts.push(`## Task\n${task}`);
// Add instructions
const instructions = ["1. Implement the task described above"];
let step = 2;
if (!skipTests) {
instructions.push(`${step}. Write tests for the feature`);
step++;
instructions.push(`${step}. Run tests and ensure they pass before proceeding`);
step++;
}
if (!skipLint) {
instructions.push(`${step}. Run linting and ensure it passes`);
step++;
}
instructions.push(`${step}. Ensure the code works correctly`);
step++;
if (autoCommit) {
instructions.push(`${step}. Commit your changes with a descriptive message`);
}
parts.push(`## Instructions\n${instructions.join("\n")}`);
// Add final note
const prdNote = prdFile ? `Do NOT modify ${prdFile}.` : "Do NOT modify the PRD file.";
parts.push(
[
prdNote,
"Do NOT modify .ralphy/progress.txt, .ralphy-worktrees, or .ralphy-sandboxes.",
"Keep changes focused and minimal. Do not refactor unrelated code.",
].join(" "),
);
return parts.join("\n\n");
}
interface ParallelPromptOptions {
task: string;
progressFile: string;
prdFile?: string;
skipTests?: boolean;
skipLint?: boolean;
browserEnabled?: "auto" | "true" | "false";
allowCommit?: boolean;
}
/**
* Build a prompt for parallel agent execution
*/
export function buildParallelPrompt(options: ParallelPromptOptions): string {
const {
task,
progressFile,
prdFile,
skipTests = false,
skipLint = false,
browserEnabled = "auto",
allowCommit = true,
} = options;
// Parallel execution typically runs in a worktree; we still try to detect skills from CWD.
// If callers pass a workDir in the future, prefer that instead.
const skillRoots = detectAgentSkills(process.cwd());
const skillsSection =
skillRoots.length > 0
? `\n\nAgent Skills:\nThis repo includes skill/playbook docs:\n${skillRoots
.map((p) => `- ${p}`)
.join(
"\n",
)}\nBefore coding, read relevant skills. If your engine supports a \`skill\` tool, load them before implementing.`
: "";
const browserSection = isBrowserAvailable(browserEnabled)
? `\n\n${getBrowserInstructions()}`
: "";
const instructions = ["1. Implement this specific task completely"];
let step = 2;
if (!skipTests) {
instructions.push(`${step}. Write tests for the feature`);
step++;
instructions.push(`${step}. Run tests and ensure they pass before proceeding`);
step++;
}
if (!skipLint) {
instructions.push(`${step}. Run linting and ensure it passes`);
step++;
}
if (allowCommit) {
instructions.push(`${step}. Commit your changes with a descriptive message`);
} else {
instructions.push(`${step}. Do NOT run git commit; changes will be collected automatically`);
}
return `You are working on a specific task. Focus ONLY on this task:
TASK: ${task}${browserSection}${skillsSection}
Instructions:
${instructions.join("\n")}
${prdFile ? `Do NOT modify ${prdFile}.` : "Do NOT modify the PRD file."}
Do NOT modify .ralphy/progress.txt, .ralphy-worktrees, or .ralphy-sandboxes.
Do NOT mark tasks complete - that will be handled separately.
Focus only on implementing: ${task}`;
}