import { apiGet, apiPost, apiPatch, apiDelete } from "@/api/client"; /** One canonical location of a reusable thing. A snippet that unifies several * one-offs carries several — one per call site. */ export interface SnippetLocation { repo: string; path: string; symbol: string; } /** Structured fields parsed out of a snippet note (mirrors the backend * `parse_snippet_fields` — see services/snippets.py). `repo`/`path`/`symbol` * mirror the first location for back-compat; `locations` is the full list. */ export interface SnippetFields { name: string; when_to_use: string; signature: string; language: string; repo: string; path: string; symbol: string; locations: SnippetLocation[]; code: string; } /** A full snippet record: the note dict plus the parsed `snippet` sub-object, * as returned by the backend `snippet_to_dict`. */ export interface Snippet { id: number; title: string; body: string; tags: string[]; note_type: string; project_id: number | null; permission?: string; created_at: string; updated_at: string; snippet: SnippetFields; systems?: { id: number; name: string }[]; } /** Lightweight list item from the knowledge preview feed. Note: the `snippet` * field here is a truncated *body preview* (the knowledge feed's naming), not * the parsed fields above. */ export interface SnippetListItem { id: number; title: string; tags: string[]; note_type: string; snippet: string; created_at: string; updated_at: string; } /** Create/update payload — discrete fields the backend serializes into the * note (title/body/tags). Empty strings are applied; omitted keys are left * unchanged on update. */ export interface SnippetInput { name: string; code: string; language?: string; signature?: string; when_to_use?: string; locations?: SnippetLocation[]; tags?: string[]; project_id?: number | null; system_ids?: number[]; /** Create only: record it even though a near-duplicate already exists. */ force?: boolean; } export async function listSnippets( params: { q?: string; tag?: string; projectId?: number | null } = {}, ): Promise<{ snippets: SnippetListItem[]; total: number }> { const qs = new URLSearchParams(); if (params.q) qs.set("q", params.q); if (params.tag) qs.set("tag", params.tag); if (params.projectId) qs.set("project_id", String(params.projectId)); const query = qs.toString(); return apiGet(`/api/snippets${query ? `?${query}` : ""}`); } export async function getSnippet(id: number): Promise { return apiGet(`/api/snippets/${id}`); } export async function createSnippet(data: SnippetInput): Promise { return apiPost("/api/snippets", data); } export async function updateSnippet( id: number, data: Partial, ): Promise { return apiPatch(`/api/snippets/${id}`, data); } export async function deleteSnippet(id: number): Promise { return apiDelete(`/api/snippets/${id}`); } /** Unify `sourceIds` into the canonical snippet `targetId`. Returns the merged * survivor plus `merged_ids` — the sources actually folded in and trashed. */ export async function mergeSnippets( targetId: number, sourceIds: number[], ): Promise { return apiPost(`/api/snippets/${targetId}/merge`, { source_ids: sourceIds }); }