open-agreements
Version:
Open-source legal template filling CLI and library
91 lines (79 loc) • 3.15 kB
JavaScript
export class ClaudeCodeAdapter {
name = 'claude-code';
generateSkillFile(metadata, templateId) {
const priorityFields = new Set(metadata.priority_fields);
const fieldsBySection = this.groupFieldsBySection(metadata);
const questionRounds = this.buildQuestionRounds(fieldsBySection);
return `# OpenAgreements: Fill ${metadata.name}
Fill the **${metadata.name}** template with user-provided values and render a DOCX file.
Source: ${metadata.source_url}
License: ${metadata.license}
${metadata.attribution_text}
## Instructions
1. Greet the user and explain you'll help them fill out a ${metadata.name}.
2. Collect field values using AskUserQuestion in multiple rounds (up to 4 questions each).
3. After collecting all values, run the template engine to produce the filled DOCX.
## Field Collection Rounds
${questionRounds}
## Execution
After collecting all field values, run:
\`\`\`bash
open-agreements fill ${templateId} ${metadata.fields
.filter((f) => priorityFields.has(f.name))
.map((f) => `--${f.name} "\${${f.name}}"`)
.join(' ')}
\`\`\`
The filled DOCX will be saved to the current directory.
## Important
- All fields should be confirmed with the user before rendering.
- If the user is unsure about a field (e.g., governing law), suggest common defaults.
- This template is licensed under ${metadata.license}. ${metadata.attribution_text}
`;
}
getOutputPath(_templateId) {
return '.claude/commands/open-agreements.md';
}
groupFieldsBySection(metadata) {
const priorityFields = new Set(metadata.priority_fields);
const sections = new Map();
for (const field of metadata.fields) {
const fieldWithRequired = {
...field,
required: priorityFields.has(field.name),
};
const section = field.section ?? 'General';
if (!sections.has(section))
sections.set(section, []);
sections.get(section).push(fieldWithRequired);
}
return sections;
}
buildQuestionRounds(fieldsBySection) {
let round = 1;
let output = '';
let currentBatch = [];
for (const [section, fields] of fieldsBySection) {
for (const field of fields) {
currentBatch.push(field);
if (currentBatch.length === 4) {
output += this.formatRound(round, section, currentBatch);
round++;
currentBatch = [];
}
}
if (currentBatch.length > 0) {
output += this.formatRound(round, section, currentBatch);
round++;
currentBatch = [];
}
}
return output;
}
formatRound(round, section, fields) {
const fieldList = fields
.map((f) => ` - **${f.name}** (${f.type}${f.required ? ', required' : ', optional'}): ${f.description}`)
.join('\n');
return `### Round ${round}: ${section}\n${fieldList}\n\n`;
}
}
//# sourceMappingURL=claude.js.map