import type { ContentItemsResponse, DashboardResponse, FlowLedgerApiResponse, FlowLedgerVideoResponse, FlowLedgerVideoWalkResponse, FlowLedgerVideosResponse, FlowLedgerWalkResponse, AigcPushRecordListResponse, QueryListResponse, RunListResponse, TimelineResponse } from "./types"; const DEFAULT_API_BASE_URL = "http://47.245.103.121:8000"; export class ApiError extends Error { status: number; detail: unknown; constructor(message: string, status: number, detail: unknown) { super(message); this.name = "ApiError"; this.status = status; this.detail = detail; } } export function apiBaseUrl() { return ( process.env.NEXT_PUBLIC_CONTENTFIND_API_BASE_URL || process.env.VITE_CONTENTFIND_API_BASE_URL || DEFAULT_API_BASE_URL ).replace(/\/$/, ""); } async function request(path: string): Promise { const response = await fetch(`${apiBaseUrl()}${path}`, { headers: { Accept: "application/json" }, cache: "no-store" }); if (!response.ok) { let detail: unknown = null; try { detail = await response.json(); } catch { detail = await response.text(); } throw new ApiError(`request failed: ${path}`, response.status, detail); } return response.json() as Promise; } export function listRuns(params = new URLSearchParams()) { const query = params.toString(); return request(`/runs${query ? `?${query}` : ""}`); } export function listAigcPushRecords(params = new URLSearchParams()) { const query = params.toString(); return request(`/aigc-push-records${query ? `?${query}` : ""}`); } export function getDashboard(runId: string) { return request(`/runs/${encodeURIComponent(runId)}/dashboard`); } export function getQueries(runId: string) { return request(`/runs/${encodeURIComponent(runId)}/queries`); } export function getTimeline(runId: string) { return request(`/runs/${encodeURIComponent(runId)}/timeline`); } export function getContentItems(runId: string) { return request(`/runs/${encodeURIComponent(runId)}/content-items`); } function debugSuffix(debug = false) { return debug ? "?debug=true" : ""; } export function getFlowLedger(runId: string, debug = false) { return request( `/runs/${encodeURIComponent(runId)}/flow-ledger${debugSuffix(debug)}` ); } export function getFlowLedgerVideos(runId: string, queryId: string, debug = false) { return request( `/runs/${encodeURIComponent(runId)}/flow-ledger/queries/${encodeURIComponent(queryId)}/videos${debugSuffix(debug)}` ); } export function getFlowLedgerWalk(runId: string, queryId: string, debug = false) { return request( `/runs/${encodeURIComponent(runId)}/flow-ledger/queries/${encodeURIComponent(queryId)}/walk${debugSuffix(debug)}` ); } export function getFlowLedgerVideo(runId: string, contentId: string, debug = false) { return request( `/runs/${encodeURIComponent(runId)}/flow-ledger/videos/${encodeURIComponent(contentId)}${debugSuffix(debug)}` ); } export function getRunWalk(runId: string, debug = false) { return request( `/runs/${encodeURIComponent(runId)}/flow-ledger/walk${debugSuffix(debug)}` ); } export function getVideoWalk(runId: string, contentId: string, debug = false) { return request( `/runs/${encodeURIComponent(runId)}/flow-ledger/videos/${encodeURIComponent(contentId)}/walk${debugSuffix(debug)}` ); }