Files
openchamber/packages/ui/src/stores/useSnippetsStore.ts
T
Bohdan Triapitsyn 2031e3b4a8 Decouple bundled UI from runtime API and add remote instance tooling (#1228)
Add a packaged-client runtime boundary so the shared UI can talk to local,
desktop, remote, and VS Code runtimes through the right transport instead of
assuming one same-origin web server.

Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and
runtime URL helpers, while keeping official OpenCode traffic on the SDK path.
Support runtime switching, remote host selection, desktop client credentials,
and headless connection links for pairing packaged clients with remote
OpenChamber servers.

Harden the new auth model by moving long-lived client tokens out of browser
URLs, introducing short-lived scoped URL tokens for browser-owned transports,
restricting URL-token access to explicit readable/realtime routes, and making
client-token management session-scoped or self-scoped as appropriate.

Update browser-owned assets and preview proxy flows to work with the split
runtime model, including authenticated project icons, preview token propagation,
CSP-safe preview bridge injection, and preview proxy auth that survives
short-lived URL-token expiry.

Tighten Electron security boundaries for packaged clients by gating privileged
preload state to trusted origins and requiring explicit confirmation before
connect deep-links import or switch remote runtimes.

Also refresh agent guidance and project skills so future runtime/API, auth,
preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new
architecture.
2026-06-02 00:43:05 +03:00

175 lines
7.1 KiB
TypeScript

import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
import type { Snippet } from '@/types/snippet';
import { opencodeClient } from '@/lib/opencode/client';
import { runtimeFetch } from '@/lib/runtime-fetch';
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 runtimeFetch(`/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 runtimeFetch(`/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 runtimeFetch(`/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 runtimeFetch(`/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 runtimeFetch(`/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' },
),
);