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.
174 lines
7.0 KiB
TypeScript
174 lines
7.0 KiB
TypeScript
import { create } from 'zustand';
|
|
import { devtools } from 'zustand/middleware';
|
|
import type { Snippet } from '@/types/snippet';
|
|
import { opencodeClient } from '@/lib/opencode/client';
|
|
import { useProjectsStore } from '@/stores/useProjectsStore';
|
|
|
|
export type SnippetScope = 'global' | 'project';
|
|
|
|
export interface SnippetDraft {
|
|
name: string;
|
|
scope: SnippetScope;
|
|
content?: string;
|
|
aliases?: string[];
|
|
description?: string;
|
|
}
|
|
|
|
interface SnippetsStore {
|
|
snippets: Snippet[];
|
|
isLoading: boolean;
|
|
selectedSnippetName: string | null;
|
|
snippetDraft: SnippetDraft | null;
|
|
|
|
setSelectedSnippet: (name: string | null) => void;
|
|
setSnippetDraft: (draft: SnippetDraft | null) => void;
|
|
loadSnippets: () => Promise<boolean>;
|
|
createSnippet: (name: string, content: string, options?: { aliases?: string[]; description?: string; scope?: SnippetScope }) => Promise<boolean>;
|
|
updateSnippet: (name: string, updates: { content?: string; aliases?: string[]; description?: string }) => Promise<boolean>;
|
|
deleteSnippet: (name: string) => Promise<boolean>;
|
|
expandText: (text: string) => Promise<string>;
|
|
getSnippetByName: (name: string) => Snippet | undefined;
|
|
}
|
|
|
|
const SNIPPETS_LOAD_CACHE_TTL_MS = 5000;
|
|
let lastLoadedAt = 0;
|
|
let loadInFlight: Promise<boolean> | null = null;
|
|
|
|
const getRequestDirectory = (): string | null => {
|
|
try {
|
|
const activeProject = useProjectsStore.getState().getActiveProject?.();
|
|
if (activeProject?.path?.trim()) return activeProject.path.trim();
|
|
const clientDir = opencodeClient.getDirectory();
|
|
if (clientDir?.trim()) return clientDir.trim();
|
|
} catch (error) {
|
|
console.warn('[SnippetsStore] Error resolving config directory:', error);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
export const useSnippetsStore = create<SnippetsStore>()(
|
|
devtools(
|
|
(set, get) => ({
|
|
snippets: [],
|
|
isLoading: false,
|
|
selectedSnippetName: null,
|
|
snippetDraft: null,
|
|
|
|
setSelectedSnippet: (name) => set({ selectedSnippetName: name }),
|
|
setSnippetDraft: (draft) => set({ snippetDraft: draft }),
|
|
|
|
loadSnippets: async () => {
|
|
const now = Date.now();
|
|
if (get().snippets.length > 0 && now - lastLoadedAt < SNIPPETS_LOAD_CACHE_TTL_MS) return true;
|
|
if (loadInFlight) return loadInFlight;
|
|
|
|
const request = (async () => {
|
|
set({ isLoading: true });
|
|
try {
|
|
const directory = getRequestDirectory();
|
|
const queryParams = directory ? `?directory=${encodeURIComponent(directory)}` : '';
|
|
const response = await fetch(`/api/config/snippets${queryParams}`, {
|
|
headers: { 'Cache-Control': 'no-cache', ...(directory ? { 'x-opencode-directory': directory } : {}) },
|
|
});
|
|
if (!response.ok) throw new Error('Failed to load snippets');
|
|
const snippets: Snippet[] = await response.json();
|
|
set({ snippets, isLoading: false });
|
|
lastLoadedAt = Date.now();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('[SnippetsStore] Failed to load:', error);
|
|
set({ isLoading: false });
|
|
return false;
|
|
}
|
|
})();
|
|
|
|
loadInFlight = request;
|
|
try {
|
|
return await request;
|
|
} finally {
|
|
loadInFlight = null;
|
|
}
|
|
},
|
|
|
|
createSnippet: async (name, content, options = {}) => {
|
|
try {
|
|
const directory = getRequestDirectory();
|
|
const queryParams = directory ? `?directory=${encodeURIComponent(directory)}` : '';
|
|
const response = await fetch(`/api/config/snippets/${encodeURIComponent(name)}${queryParams}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...(directory ? { 'x-opencode-directory': directory } : {}) },
|
|
body: JSON.stringify({ content, aliases: options.aliases, description: options.description, scope: options.scope }),
|
|
});
|
|
if (!response.ok) {
|
|
const payload = await response.json().catch(() => null);
|
|
if (response.status === 409) {
|
|
return await get().updateSnippet(name, { content, aliases: options.aliases, description: options.description });
|
|
}
|
|
throw new Error(payload?.error || 'Failed to create snippet');
|
|
}
|
|
lastLoadedAt = 0;
|
|
await get().loadSnippets();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('[SnippetsStore] Failed to create:', error);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
updateSnippet: async (name, updates) => {
|
|
try {
|
|
const directory = getRequestDirectory();
|
|
const queryParams = directory ? `?directory=${encodeURIComponent(directory)}` : '';
|
|
const response = await fetch(`/api/config/snippets/${encodeURIComponent(name)}${queryParams}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json', ...(directory ? { 'x-opencode-directory': directory } : {}) },
|
|
body: JSON.stringify(updates),
|
|
});
|
|
if (!response.ok) throw new Error((await response.json().catch(() => null))?.error || 'Failed to update snippet');
|
|
lastLoadedAt = 0;
|
|
await get().loadSnippets();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('[SnippetsStore] Failed to update:', error);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
deleteSnippet: async (name) => {
|
|
try {
|
|
const directory = getRequestDirectory();
|
|
const queryParams = directory ? `?directory=${encodeURIComponent(directory)}` : '';
|
|
const response = await fetch(`/api/config/snippets/${encodeURIComponent(name)}${queryParams}`, {
|
|
method: 'DELETE',
|
|
headers: directory ? { 'x-opencode-directory': directory } : undefined,
|
|
});
|
|
if (!response.ok) throw new Error((await response.json().catch(() => null))?.error || 'Failed to delete snippet');
|
|
if (get().selectedSnippetName === name) set({ selectedSnippetName: null });
|
|
lastLoadedAt = 0;
|
|
await get().loadSnippets();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('[SnippetsStore] Failed to delete:', error);
|
|
return false;
|
|
}
|
|
},
|
|
|
|
expandText: async (text) => {
|
|
if (!/#[a-z0-9_-]+/i.test(text)) return text;
|
|
const directory = getRequestDirectory();
|
|
const queryParams = directory ? `?directory=${encodeURIComponent(directory)}` : '';
|
|
const response = await fetch(`/api/config/snippets/expand${queryParams}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...(directory ? { 'x-opencode-directory': directory } : {}) },
|
|
body: JSON.stringify({ text }),
|
|
});
|
|
if (!response.ok) throw new Error((await response.json().catch(() => null))?.error || 'Failed to expand snippets');
|
|
return (await response.json()).text ?? text;
|
|
},
|
|
|
|
getSnippetByName: (name) => get().snippets.find((snippet) => snippet.name === name || snippet.aliases.includes(name)),
|
|
}),
|
|
{ name: 'snippets-store' },
|
|
),
|
|
);
|