Replace the prompt-template workflow with snippet support that is compatible with opencode snippet conventions. Snippets are now stored and loaded from global and project snippet directories, including legacy pluralized paths, with frontmatter metadata for aliases and descriptions. Snippet expansion supports recursive references plus prepend and append sections, while inject sections are treated as unsupported no-ops so OpenChamber remains compatible without requiring an external plugin. Add the snippets settings experience and remove the old prompt-template settings surface. The new settings page and sidebar support creating, editing, deleting, selecting, and describing snippets, with localized copy across every supported locale. The settings navigation now exposes Snippets with a dedicated icon and metadata. Wire snippets into all prompt-entry surfaces that need them. Chat, multi-run groups, and scheduled task prompts now offer hash-trigger snippet autocomplete and expand snippets before sending work to OpenCode. Chat also uses an adaptive compact placeholder on mobile or narrow composer widths so helper trigger guidance stays readable in constrained layouts. Keep multi-run aligned with grouped prompts. Multi-run sessions now use a shared title builder that handles both legacy titles and the newer g1, g2 prompt-group title format. Fusion parsing now recognizes grouped multi-run titles, scopes fusion sources to the same prompt group, and creates fusion sessions under the matching group so outputs from different prompts are not mixed accidentally. Harden the icon sprite pipeline. The sprite generator now discovers icon names used through typed icon maps, JSX icon props, IconName returns, and generated-value flows without scanning unrelated string literals or the generated sprite itself. The generated sprite is strictly typed so invalid icon names are caught by type checking, and existing invalid or unsafe icon references were cleaned up across settings, provider, Git identity, scheduled task, voice, header, and sidebar surfaces. Update backend configuration routes and documentation for snippets. The OpenCode config route layer now exposes snippet CRUD and expansion endpoints, accepts JSON bodies for snippet writes, and removes the old prompt-template provider. Scheduled task runtime expansion now uses snippets before dispatching messages. Add regression coverage for snippet storage and expansion, config-route JSON handling, and multi-run title parsing. Validated with full type checking, full linting, targeted multi-run title tests, and targeted OpenCode snippet/config route tests.
234 lines
8.1 KiB
JavaScript
234 lines
8.1 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import os from 'os';
|
|
import yaml from 'yaml';
|
|
|
|
const OPENCODE_CONFIG_DIR = path.join(os.homedir(), '.config', 'opencode');
|
|
const GLOBAL_SNIPPET_DIR = path.join(OPENCODE_CONFIG_DIR, 'snippet');
|
|
const GLOBAL_SNIPPET_DIR_ALT = path.join(OPENCODE_CONFIG_DIR, 'snippets');
|
|
const SNIPPET_EXTENSION = '.md';
|
|
const SNIPPET_NAME_PATTERN = /^[a-z0-9][a-z0-9_-]{0,79}$/i;
|
|
const HASHTAG_PATTERN = /#([a-z0-9_-]+)/gi;
|
|
const MAX_EXPANSION_COUNT = 15;
|
|
|
|
function getProjectSnippetDirs(workingDirectory) {
|
|
if (!workingDirectory) return [];
|
|
return [
|
|
path.join(workingDirectory, '.opencode', 'snippets'),
|
|
path.join(workingDirectory, '.opencode', 'snippet'),
|
|
];
|
|
}
|
|
|
|
function getGlobalSnippetDirs() {
|
|
return [GLOBAL_SNIPPET_DIR_ALT, GLOBAL_SNIPPET_DIR];
|
|
}
|
|
|
|
function getLoadDirs(workingDirectory) {
|
|
return [
|
|
...getGlobalSnippetDirs().map((dir) => ({ dir, source: 'global' })),
|
|
...getProjectSnippetDirs(workingDirectory).map((dir) => ({ dir, source: 'project' })),
|
|
];
|
|
}
|
|
|
|
function assertValidSnippetName(name) {
|
|
if (typeof name !== 'string' || !SNIPPET_NAME_PATTERN.test(name)) {
|
|
throw new Error('Snippet name must use letters, numbers, dashes, or underscores');
|
|
}
|
|
}
|
|
|
|
function parseMarkdownFile(filePath) {
|
|
const content = fs.readFileSync(filePath, 'utf8');
|
|
const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/);
|
|
if (!match) {
|
|
return { frontmatter: {}, body: content.trim() };
|
|
}
|
|
return {
|
|
frontmatter: yaml.parse(match[1]) || {},
|
|
body: match[2].trim(),
|
|
};
|
|
}
|
|
|
|
function normalizeAliases(frontmatter) {
|
|
const raw = frontmatter.aliases ?? frontmatter.alias;
|
|
if (!raw) return [];
|
|
const aliases = Array.isArray(raw) ? raw : [raw];
|
|
return aliases.map((alias) => String(alias).trim()).filter(Boolean);
|
|
}
|
|
|
|
function writeMarkdownFile(filePath, { content, aliases = [], description }) {
|
|
const frontmatter = {};
|
|
const normalizedAliases = aliases.map((alias) => String(alias).trim()).filter(Boolean);
|
|
if (normalizedAliases.length > 0) frontmatter.aliases = normalizedAliases;
|
|
if (description?.trim()) frontmatter.description = description.trim();
|
|
|
|
const body = content ?? '';
|
|
const output = Object.keys(frontmatter).length > 0
|
|
? `---\n${yaml.stringify(frontmatter)}---\n${body ? `\n${body}` : ''}`
|
|
: body;
|
|
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
fs.writeFileSync(filePath, output, 'utf8');
|
|
}
|
|
|
|
function loadSnippetFile(dir, filename, source) {
|
|
const name = path.basename(filename, SNIPPET_EXTENSION);
|
|
if (!SNIPPET_NAME_PATTERN.test(name)) return null;
|
|
const filePath = path.join(dir, filename);
|
|
const { frontmatter, body } = parseMarkdownFile(filePath);
|
|
return {
|
|
name,
|
|
content: body,
|
|
aliases: normalizeAliases(frontmatter),
|
|
description: typeof frontmatter.description === 'string' ? frontmatter.description : undefined,
|
|
filePath,
|
|
source,
|
|
};
|
|
}
|
|
|
|
function registerSnippet(registry, snippet) {
|
|
const key = snippet.name.toLowerCase();
|
|
const existing = registry.get(key);
|
|
if (existing) {
|
|
for (const alias of existing.aliases) registry.delete(alias.toLowerCase());
|
|
}
|
|
registry.set(key, snippet);
|
|
for (const alias of snippet.aliases) {
|
|
if (SNIPPET_NAME_PATTERN.test(alias)) registry.set(alias.toLowerCase(), snippet);
|
|
}
|
|
}
|
|
|
|
function loadSnippetRegistry(workingDirectory) {
|
|
const registry = new Map();
|
|
for (const { dir, source } of getLoadDirs(workingDirectory)) {
|
|
if (!fs.existsSync(dir)) continue;
|
|
for (const filename of fs.readdirSync(dir)) {
|
|
if (!filename.endsWith(SNIPPET_EXTENSION)) continue;
|
|
try {
|
|
const snippet = loadSnippetFile(dir, filename, source);
|
|
if (snippet) registerSnippet(registry, snippet);
|
|
} catch (error) {
|
|
console.warn(`[Snippets] Failed to load ${path.join(dir, filename)}:`, error);
|
|
}
|
|
}
|
|
}
|
|
return registry;
|
|
}
|
|
|
|
function listUniqueSnippets(registry) {
|
|
const seen = new Set();
|
|
const snippets = [];
|
|
for (const snippet of registry.values()) {
|
|
const key = `${snippet.source}:${snippet.filePath}`;
|
|
if (seen.has(key)) continue;
|
|
seen.add(key);
|
|
snippets.push(snippet);
|
|
}
|
|
return snippets.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
function getWritableSnippetDir(scope, workingDirectory) {
|
|
if (scope === 'project') {
|
|
if (!workingDirectory) throw new Error('Project directory is required for project snippets');
|
|
const preferred = path.join(workingDirectory, '.opencode', 'snippet');
|
|
const alternate = path.join(workingDirectory, '.opencode', 'snippets');
|
|
return fs.existsSync(alternate) && !fs.existsSync(preferred) ? alternate : preferred;
|
|
}
|
|
return fs.existsSync(GLOBAL_SNIPPET_DIR_ALT) && !fs.existsSync(GLOBAL_SNIPPET_DIR)
|
|
? GLOBAL_SNIPPET_DIR_ALT
|
|
: GLOBAL_SNIPPET_DIR;
|
|
}
|
|
|
|
function findSnippetByName(name, workingDirectory) {
|
|
assertValidSnippetName(name);
|
|
const registry = loadSnippetRegistry(workingDirectory);
|
|
return registry.get(name.toLowerCase()) ?? null;
|
|
}
|
|
|
|
function parseSnippetBlocks(content) {
|
|
const blocks = { prepend: [], append: [] };
|
|
let inline = content;
|
|
for (const type of ['prepend', 'append']) {
|
|
const regex = new RegExp(`<${type}>([\\s\\S]*?)(?:<\\/${type}>|$)`, 'gi');
|
|
inline = inline.replace(regex, (_match, value) => {
|
|
const normalized = String(value).trim();
|
|
if (normalized) blocks[type].push(normalized);
|
|
return '';
|
|
});
|
|
}
|
|
inline = inline.replace(/<inject>[\s\S]*?(?:<\/inject>|$)/gi, '').trim();
|
|
return { inline, prepend: blocks.prepend, append: blocks.append };
|
|
}
|
|
|
|
function expandText(text, registry, expansionCounts, collector) {
|
|
let expanded = text;
|
|
let changed = true;
|
|
|
|
while (changed) {
|
|
const previous = expanded;
|
|
let loopDetected = false;
|
|
HASHTAG_PATTERN.lastIndex = 0;
|
|
|
|
expanded = expanded.replace(HASHTAG_PATTERN, (match, name, offset, input) => {
|
|
if (name.toLowerCase() === 'skill' && input[offset + match.length] === '(') return match;
|
|
const snippet = registry.get(name.toLowerCase());
|
|
if (!snippet) return match;
|
|
|
|
const key = snippet.name.toLowerCase();
|
|
const count = (expansionCounts.get(key) || 0) + 1;
|
|
if (count > MAX_EXPANSION_COUNT) {
|
|
loopDetected = true;
|
|
return match;
|
|
}
|
|
expansionCounts.set(key, count);
|
|
|
|
const parsed = parseSnippetBlocks(snippet.content);
|
|
for (const block of parsed.prepend) collector.prepend.push(expandText(block, registry, expansionCounts, collector));
|
|
for (const block of parsed.append) collector.append.push(expandText(block, registry, expansionCounts, collector));
|
|
return expandText(parsed.inline, registry, expansionCounts, collector);
|
|
});
|
|
|
|
changed = expanded !== previous && !loopDetected;
|
|
}
|
|
|
|
return expanded;
|
|
}
|
|
|
|
export function listSnippets(workingDirectory) {
|
|
return listUniqueSnippets(loadSnippetRegistry(workingDirectory));
|
|
}
|
|
|
|
export function getSnippet(name, workingDirectory) {
|
|
return findSnippetByName(name, workingDirectory);
|
|
}
|
|
|
|
export function createSnippet(name, config, workingDirectory, scope = 'global') {
|
|
assertValidSnippetName(name);
|
|
const dir = getWritableSnippetDir(scope, workingDirectory);
|
|
const filePath = path.join(dir, `${name}${SNIPPET_EXTENSION}`);
|
|
if (fs.existsSync(filePath)) throw new Error(`Snippet "${name}" already exists`);
|
|
writeMarkdownFile(filePath, config || {});
|
|
return getSnippet(name, workingDirectory);
|
|
}
|
|
|
|
export function updateSnippet(name, updates, workingDirectory) {
|
|
const existing = findSnippetByName(name, workingDirectory);
|
|
if (!existing) throw new Error(`Snippet "${name}" not found`);
|
|
writeMarkdownFile(existing.filePath, { ...existing, ...(updates || {}) });
|
|
return getSnippet(name, workingDirectory);
|
|
}
|
|
|
|
export function deleteSnippet(name, workingDirectory) {
|
|
const existing = findSnippetByName(name, workingDirectory);
|
|
if (!existing) throw new Error(`Snippet "${name}" not found`);
|
|
fs.unlinkSync(existing.filePath);
|
|
}
|
|
|
|
export function expandSnippets(text, workingDirectory) {
|
|
const registry = loadSnippetRegistry(workingDirectory);
|
|
const collector = { prepend: [], append: [] };
|
|
const expanded = expandText(text || '', registry, new Map(), collector).trim();
|
|
return [...collector.prepend, expanded, ...collector.append].filter(Boolean).join('\n\n');
|
|
}
|
|
|
|
export { assertValidSnippetName };
|