import { request, toQueryString } from './core'; import type { PromptTemplate, PromptVersion, PromptVersionAnalytics, PromptDeployment, PromptEnvironment, PromptAgentUsage, DeployLedgerVerifyResult, } from '@agentkitai/agentlens-core'; // Re-export core types for convenience export type { PromptTemplate, PromptVersion, PromptVersionAnalytics, PromptDeployment, PromptEnvironment, PromptAgentUsage, DeployLedgerVerifyResult, }; // ─── Response types ───────────────────────────────────────────── export interface PromptListResponse { templates: PromptTemplate[]; total: number; } export interface PromptFingerprint { contentHash: string; tenantId: string; agentId: string; firstSeenAt: string; lastSeenAt: string; callCount: number; templateId?: string; sampleContent?: string; } export interface PromptDiffResponse { v1: { id: string; versionNumber: number; content: string }; v2: { id: string; versionNumber: number; content: string }; diff: string; } // ─── API Functions ────────────────────────────────────────────── export async function getPrompts(params?: { category?: string; search?: string; limit?: number; offset?: number; }): Promise { const qs = toQueryString({ category: params?.category, search: params?.search, limit: params?.limit, offset: params?.offset, }); return request(`/api/prompts${qs}`); } export async function getPrompt(id: string): Promise<{ template: PromptTemplate; versions: PromptVersion[]; /** environment → live version id (#120). */ liveVersions?: Record; }> { return request(`/api/prompts/${encodeURIComponent(id)}`); } export async function createPrompt(data: { name: string; content: string; description?: string; category?: string; variables?: { name: string; description?: string; defaultValue?: string; required?: boolean }[]; }): Promise { return request('/api/prompts', { method: 'POST', body: JSON.stringify(data), }); } export async function createPromptVersion( templateId: string, data: { content: string; changelog?: string; createdBy?: string }, ): Promise { return request( `/api/prompts/${encodeURIComponent(templateId)}/versions`, { method: 'POST', body: JSON.stringify(data) }, ); } export async function getPromptVersion( templateId: string, versionId: string, ): Promise { return request( `/api/prompts/${encodeURIComponent(templateId)}/versions/${encodeURIComponent(versionId)}`, ); } export async function getPromptAnalytics( templateId: string, params?: { from?: string; to?: string }, ): Promise { const qs = toQueryString({ from: params?.from, to: params?.to }); // Server wraps the array as { analytics: [...] } — unwrap it (else the page // crashes on analytics.map). const res = await request<{ analytics: PromptVersionAnalytics[] }>( `/api/prompts/${encodeURIComponent(templateId)}/analytics${qs}`, ); return res.analytics ?? []; } export async function getPromptDiff( templateId: string, v1: string, v2: string, ): Promise { const qs = toQueryString({ v1, v2 }); return request( `/api/prompts/${encodeURIComponent(templateId)}/diff${qs}`, ); } export async function deletePrompt(id: string): Promise { await request(`/api/prompts/${encodeURIComponent(id)}`, { method: 'DELETE' }); } export async function getPromptFingerprints(params?: { agentId?: string; }): Promise { const qs = toQueryString({ agentId: params?.agentId }); // Server wraps the array as { fingerprints: [...] } — unwrap like the sibling endpoints. const r = await request<{ fingerprints: PromptFingerprint[] }>(`/api/prompts/fingerprints${qs}`); return r.fingerprints ?? []; } export async function linkFingerprintToTemplate( hash: string, templateId: string, ): Promise { await request(`/api/prompts/fingerprints/${encodeURIComponent(hash)}/link`, { method: 'POST', body: JSON.stringify({ templateId }), }); } // ─── Deploy lifecycle (#120) ──────────────────────────────────── export async function getPromptEnvironments(): Promise { const r = await request<{ environments: PromptEnvironment[] }>('/api/prompts/environments'); return r.environments; } export async function getPromptDeployments( templateId: string, environment?: string, ): Promise { const qs = toQueryString({ environment }); const r = await request<{ deployments: PromptDeployment[] }>( `/api/prompts/${encodeURIComponent(templateId)}/deployments${qs}`, ); return r.deployments; } export async function deployPromptVersion( templateId: string, data: { environment: string; versionId: string; note?: string }, ): Promise { const r = await request<{ deployment: PromptDeployment }>( `/api/prompts/${encodeURIComponent(templateId)}/deploy`, { method: 'POST', body: JSON.stringify(data) }, ); return r.deployment; } export async function rollbackPromptVersion( templateId: string, data: { environment: string; toVersionId: string; note?: string }, ): Promise { const r = await request<{ deployment: PromptDeployment }>( `/api/prompts/${encodeURIComponent(templateId)}/rollback`, { method: 'POST', body: JSON.stringify(data) }, ); return r.deployment; } export async function verifyDeployLedger(environment: string): Promise { const qs = toQueryString({ environment }); return request(`/api/prompts/deployments/verify${qs}`); } export async function getPromptAgentUsage( templateId: string, params?: { from?: string; to?: string }, ): Promise { const qs = toQueryString({ from: params?.from, to: params?.to }); const r = await request<{ usage: PromptAgentUsage[] }>( `/api/prompts/${encodeURIComponent(templateId)}/analytics/by-agent${qs}`, ); return r.usage; }