-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathadapter-transforms.js
More file actions
303 lines (255 loc) · 11.2 KB
/
adapter-transforms.js
File metadata and controls
303 lines (255 loc) · 11.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* Adapter Transform Functions
*
* Shared transforms for converting Claude Code plugin content into
* OpenCode and Codex adapter formats. Used by:
* - bin/cli.js (npm installer)
* - scripts/dev-install.js (development installer)
* - scripts/gen-adapters.js (static adapter generation)
*
* @module adapter-transforms
* @author Avi Fenesh
* @license MIT
*/
const discovery = require('./discovery');
function transformBodyForOpenCode(content, repoRoot) {
content = content.replace(/\$\{CLAUDE_PLUGIN_ROOT\}/g, '${PLUGIN_ROOT}');
content = content.replace(/\$CLAUDE_PLUGIN_ROOT/g, '$PLUGIN_ROOT');
// Replace .claude/ paths with .opencode/ but preserve platform documentation lists
// that enumerate all three platforms (Claude Code: .claude/, OpenCode: .opencode/, Codex: .codex/)
// Also preserve {AI_STATE_DIR} references which are platform-agnostic
content = content.replace(/\.claude\//g, (match, offset) => {
const context = content.substring(Math.max(0, offset - 60), offset + match.length + 10);
// Skip if inside a platform enumeration (e.g., "Claude Code: `.claude/`")
if (/Claude Code:/.test(context)) return match;
return '.opencode/';
});
content = content.replace(/\.claude'/g, (match, offset) => {
const context = content.substring(Math.max(0, offset - 60), offset + match.length + 10);
if (/Claude Code:/.test(context)) return match;
return ".opencode'";
});
content = content.replace(/\.claude"/g, (match, offset) => {
const context = content.substring(Math.max(0, offset - 60), offset + match.length + 10);
if (/Claude Code:/.test(context)) return match;
return '.opencode"';
});
content = content.replace(/\.claude`/g, (match, offset) => {
const context = content.substring(Math.max(0, offset - 60), offset + match.length + 10);
if (/Claude Code:/.test(context)) return match;
return '.opencode`';
});
const plugins = discovery.discoverPlugins(repoRoot);
if (plugins.length > 0) {
const pluginNames = plugins.join('|');
content = content.replace(new RegExp('`(' + pluginNames + '):([a-z-]+)`', 'g'), '`$2`');
content = content.replace(new RegExp('(' + pluginNames + '):([a-z-]+)', 'g'), '$2');
}
content = content.replace(
/```(\w*)\n([\s\S]*?)```/g,
(match, lang, code) => {
const langLower = (lang || '').toLowerCase();
if (langLower === 'bash' || langLower === 'shell' || langLower === 'sh') {
if (code.includes('node -e') && code.includes('require(')) {
return '*(Bash command with Node.js require - adapt for OpenCode)*';
}
return match;
}
if (!lang && (code.trim().startsWith('gh ') || code.trim().startsWith('glab ') ||
code.trim().startsWith('git ') || code.trim().startsWith('#!'))) {
return match;
}
if (code.includes('require(') || code.includes('Task(') ||
/^\s*const\s+[a-zA-Z_$[{]/m.test(code) || /^\s*let\s+[a-zA-Z_$[{]/m.test(code) ||
code.includes('function ') || code.includes('=>') ||
code.includes('async ') || code.includes('await ') ||
code.includes('completePhase')) {
let instructions = '';
const taskMatches = [...code.matchAll(/(?:await\s+)?Task\s*\(\s*\{[^}]*subagent_type:\s*["'](?:[^"':]+:)?([^"']+)["'][^}]*\}\s*\)/gs)];
for (const taskMatch of taskMatches) {
const agent = taskMatch[1];
instructions += `- Invoke \`@${agent}\` agent\n`;
}
const phaseMatches = code.match(/startPhase\s*\(\s*['"]([^'"]+)['"]\s*\)/g);
if (phaseMatches) {
for (const pm of phaseMatches) {
const phase = pm.match(/['"]([^'"]+)['"]/)[1];
instructions += `- Phase: ${phase}\n`;
}
}
if (code.includes('AskUserQuestion')) {
instructions += '- Use AskUserQuestion tool for user input\n';
}
if (code.includes('EnterPlanMode')) {
instructions += '- Use EnterPlanMode for user approval\n';
}
if (code.includes('completePhase')) {
instructions += '- Call `workflowState.completePhase(result)` to advance workflow state\n';
}
if (instructions) {
return instructions;
}
return '*(JavaScript reference - not executable in OpenCode)*';
}
return match;
}
);
content = content.replace(/\*\(Reference - adapt for OpenCode\)\*/g, '');
content = content.replace(/await\s+Task\s*\(\s*\{[\s\S]*?\}\s*\);?/g, (match) => {
const agentMatch = match.match(/subagent_type:\s*["'](?:[^"':]+:)?([^"']+)["']/);
if (agentMatch) {
return `Invoke \`@${agentMatch[1]}\` agent`;
}
return '*(Task call - use @agent-name syntax)*';
});
content = content.replace(/(?:const|let|var)\s+\{?[^}=\n]+\}?\s*=\s*require\s*\([^)]+\);?/g, '');
content = content.replace(/require\s*\(['"][^'"]+['"]\)/g, '');
if (content.includes('agent')) {
const note = `
> **OpenCode Note**: Invoke agents using \`@agent-name\` syntax.
> Available agents: task-discoverer, exploration-agent, planning-agent,
> implementation-agent, deslop-agent, delivery-validator, sync-docs-agent, consult-agent
> Example: \`@exploration-agent analyze the codebase\`
`;
content = content.replace(/^(---\n[\s\S]*?---\n)/, `$1${note}`);
}
if (content.includes('Master Workflow Orchestrator') && content.includes('No Shortcuts Policy')) {
const policySection = `
## Phase 1: Policy Selection (Built-in Options)
Ask the user these questions using AskUserQuestion:
**Question 1 - Source**: "Where should I look for tasks?"
- GitHub Issues - Use \`gh issue list\` to find issues
- GitLab Issues - Use \`glab issue list\` to find issues
- Local tasks.md - Read from PLAN.md, tasks.md, or TODO.md in the repo
- Custom - User specifies their own source
- Other - User describes source, you figure it out
**Question 2 - Priority**: "What type of tasks to prioritize?"
- All - Consider all tasks, pick by score
- Bugs - Focus on bug fixes
- Security - Security issues first
- Features - New feature development
**Question 3 - Stop Point**: "How far should I take this task?"
- Merged - Until PR is merged to main
- PR Created - Stop after creating PR
- Implemented - Stop after local implementation
- Deployed - Deploy to staging
- Production - Full production deployment
After user answers, proceed to Phase 2 with the selected policy.
`;
if (content.includes('OpenCode Note')) {
content = content.replace(/(Example:.*analyze the codebase\`\n\n)/, `$1${policySection}`);
}
}
return content;
}
function transformCommandFrontmatterForOpenCode(content) {
return content.replace(
/^---\n([\s\S]*?)^---/m,
(match, frontmatter) => {
// Parse existing frontmatter
const lines = frontmatter.trim().split('\n');
const parsed = {};
for (const line of lines) {
const colonIdx = line.indexOf(':');
if (colonIdx > 0) {
const key = line.substring(0, colonIdx).trim();
const value = line.substring(colonIdx + 1).trim();
parsed[key] = value;
}
}
// Build OpenCode command frontmatter
let opencodeFrontmatter = '---\n';
if (parsed.description) opencodeFrontmatter += `description: ${parsed.description}\n`;
opencodeFrontmatter += 'agent: general\n';
// Don't include argument-hint or allowed-tools (not supported)
opencodeFrontmatter += '---';
return opencodeFrontmatter;
}
);
}
function transformAgentFrontmatterForOpenCode(content, options) {
const { stripModels = true } = options || {};
return content.replace(
/^---\n([\s\S]*?)^---/m,
(match, frontmatter) => {
// Parse existing frontmatter
const lines = frontmatter.trim().split('\n');
const parsed = {};
for (const line of lines) {
const colonIdx = line.indexOf(':');
if (colonIdx > 0) {
const key = line.substring(0, colonIdx).trim();
const value = line.substring(colonIdx + 1).trim();
parsed[key] = value;
}
}
// Build OpenCode frontmatter
let opencodeFrontmatter = '---\n';
if (parsed.name) opencodeFrontmatter += `name: ${parsed.name}\n`;
if (parsed.description) opencodeFrontmatter += `description: ${parsed.description}\n`;
opencodeFrontmatter += 'mode: subagent\n';
// Map model names - only include if NOT stripping
if (parsed.model && !stripModels) {
const modelMap = {
'sonnet': 'anthropic/claude-sonnet-4',
'opus': 'anthropic/claude-opus-4',
'haiku': 'anthropic/claude-haiku-3-5'
};
opencodeFrontmatter += `model: ${modelMap[parsed.model] || parsed.model}\n`;
}
// Convert tools to permissions
if (parsed.tools) {
opencodeFrontmatter += 'permission:\n';
const tools = parsed.tools.toLowerCase();
opencodeFrontmatter += ` read: ${tools.includes('read') ? 'allow' : 'deny'}\n`;
opencodeFrontmatter += ` edit: ${tools.includes('edit') || tools.includes('write') ? 'allow' : 'deny'}\n`;
opencodeFrontmatter += ` bash: ${tools.includes('bash') ? 'allow' : 'ask'}\n`;
opencodeFrontmatter += ` glob: ${tools.includes('glob') ? 'allow' : 'deny'}\n`;
opencodeFrontmatter += ` grep: ${tools.includes('grep') ? 'allow' : 'deny'}\n`;
}
opencodeFrontmatter += '---';
return opencodeFrontmatter;
}
);
}
function transformSkillBodyForOpenCode(content, repoRoot) {
return transformBodyForOpenCode(content, repoRoot);
}
function transformForCodex(content, options) {
const { skillName, description, pluginInstallPath } = options;
// Escape description for YAML: wrap in double quotes, escape backslashes and internal quotes
const escapedDescription = description.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
const yamlDescription = `"${escapedDescription}"`;
if (content.startsWith('---')) {
// Replace existing frontmatter with Codex-compatible format
content = content.replace(
/^---\n[\s\S]*?\n---\n/,
`---\nname: ${skillName}\ndescription: ${yamlDescription}\n---\n`
);
} else {
// Add new frontmatter
content = `---\nname: ${skillName}\ndescription: ${yamlDescription}\n---\n\n${content}`;
}
// Transform PLUGIN_ROOT to actual installed path (or placeholder) for Codex
content = content.replace(/\$\{CLAUDE_PLUGIN_ROOT\}/g, pluginInstallPath);
content = content.replace(/\$CLAUDE_PLUGIN_ROOT/g, pluginInstallPath);
content = content.replace(/\$\{PLUGIN_ROOT\}/g, pluginInstallPath);
content = content.replace(/\$PLUGIN_ROOT/g, pluginInstallPath);
// Transform AskUserQuestion → request_user_input for Codex native tool
content = content.replace(/AskUserQuestion/g, 'request_user_input');
// Remove multiSelect lines (not supported in Codex)
content = content.replace(/^[ \t]*multiSelect:.*\n?/gm, '');
// Inject Codex note about required id field after request_user_input blocks
content = content.replace(
/^([ \t]*request_user_input:\s*)$/gm,
'$1\n> **Codex**: Each question MUST include a unique `id` field (e.g., `id: "q1"`).'
);
return content;
}
module.exports = {
transformBodyForOpenCode,
transformCommandFrontmatterForOpenCode,
transformAgentFrontmatterForOpenCode,
transformSkillBodyForOpenCode,
transformForCodex
};