Files
aigc-home/calliope-web/src/lib/api.ts
T

1321 lines
36 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { AgentComposerPayload } from "$lib/agentComposer";
import { t } from "$lib/i18n.svelte";
const API_BASE = "";
export interface Project {
id: number;
title: string;
idea: string | null;
genre: string | null;
tone: string | null;
target_duration: string | null;
cover_path: string | null;
status: string;
created_at: string;
updated_at: string;
stats?: ProjectStats;
}
export interface ProjectStats {
scene_count: number;
character_count: number;
asset_ready_count: number;
asset_total_count: number;
/** Five-stage workbench progress (HEL-614) — real stage rows + live jobs. */
stage_confirmed: number;
stage_status: Record<string, string>;
active_jobs: number;
}
export interface ProjectCreate {
title: string;
idea?: string;
genre?: string;
tone?: string;
target_duration?: string;
}
export interface Beat {
id: number;
order_index: number;
title: string;
description: string | null;
}
export interface Character {
id: number;
name: string;
role: string | null;
age: string | null;
appearance: string | null;
personality: string | null;
portrait_path: string | null;
sheet_path: string | null;
consistency_prompt: string | null;
}
export interface Location {
id: number;
name: string;
description: string | null;
reference_image_path: string | null;
consistency_prompt: string | null;
}
export interface Item {
id: number;
name: string;
description: string | null;
reference_image_path: string | null;
consistency_prompt: string | null;
}
export interface StoryData {
project: Project;
beats: Beat[];
characters: Character[];
locations: Location[];
items: Item[];
}
export interface LlmProfile {
id: string;
name: string;
base_url: string;
model: string;
api_key: boolean;
/** Last 4 chars of the stored key, for masked display only. */
api_key_tail?: string | null;
}
export interface ComfyEndpoint {
id: string;
name: string;
base_url: string;
auth_type: "none" | "bearer" | "header";
header_name: string | null;
notes: string | null;
has_token: boolean;
token_tail?: string | null;
}
export interface Settings {
host: string;
port: number;
data_dir: string;
assets_dir: string;
agent_workspace_dir: string;
db_name: string;
llm_base_url: string;
llm_model: string;
llm_api_key: boolean;
llm_profiles: LlmProfile[];
llm_active_id: string | null;
comfyui_base_url: string;
comfyui_endpoints: ComfyEndpoint[];
comfyui_active_id: string | null;
queue_concurrency: number;
queue_poll_interval_sec: number;
queue_poll_timeout_sec: number;
queue_max_retries: number;
agent_max_steps: number;
agent_hardening_prompt: string;
agent_history_char_budget: number;
agent_llm_assignments: Record<string, string | null>;
agent_shell_enabled: boolean;
dry_run: boolean;
}
import type {
Job,
Scene,
SceneVideoSettings,
Clip,
Workflow,
WorkflowUsage,
WorkflowAnalyzeResult,
ComfyDynamicInput,
ComfyDynamicOutput,
WorkflowOutputMapEntry,
WorkflowConnectionCheck,
WorkflowLastTest,
WorkflowMeta,
WorkflowUIFileInput,
WorkflowConvertFileReport,
WorkflowConvertReport,
WorkflowImportResult,
} from "./comfy/types";
export type {
Job,
Scene,
SceneVideoSettings,
Clip,
Workflow,
WorkflowUsage,
WorkflowAnalyzeResult,
ComfyDynamicInput,
ComfyDynamicOutput,
WorkflowOutputMapEntry,
WorkflowConnectionCheck,
WorkflowLastTest,
WorkflowMeta,
WorkflowUIFileInput,
WorkflowConvertFileReport,
WorkflowConvertReport,
WorkflowImportResult,
};
async function api<T>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, {
...init,
headers: {
"Content-Type": "application/json",
...init?.headers,
},
});
if (!res.ok) {
const body = await res.text().catch(() => t("api.unknownError"));
throw new Error(`${res.status}: ${body}`);
}
return res.json() as Promise<T>;
}
/** Readable text for an api() failure: FastAPI `detail`, not the raw "400: {json}". */
export function apiErrorMessage(err: unknown): string {
if (err instanceof TypeError) return t("api.networkError");
const raw = err instanceof Error ? err.message : String(err);
const m = /^(\d{3}): ([\s\S]*)$/.exec(raw);
if (!m) return raw;
try {
const detail = (JSON.parse(m[2]) as { detail?: unknown }).detail;
if (typeof detail === "string" && detail) return detail;
if (Array.isArray(detail) && detail.length) {
return detail
.map((d) =>
d && typeof d === "object" && "msg" in d ? String(d.msg) : String(d),
)
.join(";");
}
} catch {
/* non-JSON body */
}
return t("api.httpError", { status: m[1], detail: m[2].slice(0, 200) });
}
/** Multipart variant of api() — FormData sets its own Content-Type boundary. */
async function apiUpload<T>(path: string, form: FormData): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, { method: "POST", body: form });
if (!res.ok) {
const body = await res.text().catch(() => t("api.unknownError"));
throw new Error(`${res.status}: ${body}`);
}
return res.json() as Promise<T>;
}
export function assetUrl(path: string | null | undefined): string | null {
if (!path) return null;
return `/api/file?path=${encodeURIComponent(path)}`;
}
export const projects = {
list: () => api<Project[]>("/api/projects"),
create: (payload: ProjectCreate) =>
api<Project>("/api/projects", {
method: "POST",
body: JSON.stringify(payload),
}),
get: (id: number) =>
api<Project & { stats: ProjectStats }>(`/api/projects/${id}`),
update: (
id: number,
payload: Partial<
ProjectCreate & { status?: string; cover_path?: string | null }
>,
) =>
api<Project>(`/api/projects/${id}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
delete: (id: number) =>
api<{ ok: boolean }>(`/api/projects/${id}`, { method: "DELETE" }),
getStory: (id: number) => api<StoryData>(`/api/projects/${id}/story`),
updateBeat: (projectId: number, beatId: number, payload: Partial<Beat>) =>
api<Beat>(`/api/projects/${projectId}/beats/${beatId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
updateCharacter: (
projectId: number,
characterId: number,
payload: Partial<Character>,
) =>
api<Character>(`/api/projects/${projectId}/characters/${characterId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
updateLocation: (
projectId: number,
locationId: number,
payload: Partial<Location>,
) =>
api<Location>(`/api/projects/${projectId}/locations/${locationId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
updateItem: (projectId: number, itemId: number, payload: Partial<Item>) =>
api<Item>(`/api/projects/${projectId}/items/${itemId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
deleteBeat: (projectId: number, beatId: number) =>
api<{ ok: boolean }>(`/api/projects/${projectId}/beats/${beatId}`, {
method: "DELETE",
}),
deleteCharacter: (projectId: number, characterId: number) =>
api<{ ok: boolean }>(
`/api/projects/${projectId}/characters/${characterId}`,
{
method: "DELETE",
},
),
deleteLocation: (projectId: number, locationId: number) =>
api<{ ok: boolean }>(`/api/projects/${projectId}/locations/${locationId}`, {
method: "DELETE",
}),
deleteItem: (projectId: number, itemId: number) =>
api<{ ok: boolean }>(`/api/projects/${projectId}/items/${itemId}`, {
method: "DELETE",
}),
getScenes: (id: number) =>
api<{ scenes: Scene[]; estimated_duration_sec: number }>(
`/api/projects/${id}/scenes`,
),
updateScene: (
projectId: number,
sceneId: number,
payload: Record<string, unknown>,
) =>
api<Scene>(`/api/projects/${projectId}/scenes/${sceneId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
createScene: (projectId: number, payload: Record<string, unknown>) =>
api<Scene>(`/api/projects/${projectId}/scenes`, {
method: "POST",
body: JSON.stringify(payload),
}),
deleteScene: (projectId: number, sceneId: number) =>
api<{ ok: boolean }>(`/api/projects/${projectId}/scenes/${sceneId}`, {
method: "DELETE",
}),
reorderScenes: (projectId: number, sceneIds: number[]) =>
api<{ scenes: Scene[] }>(`/api/projects/${projectId}/scenes/reorder`, {
method: "POST",
body: JSON.stringify({ scene_ids: sceneIds }),
}),
createClip: (
projectId: number,
sceneId: number,
payload: Record<string, unknown>,
) =>
api<Clip>(`/api/projects/${projectId}/scenes/${sceneId}/clips`, {
method: "POST",
body: JSON.stringify(payload),
}),
updateClip: (
projectId: number,
clipId: number,
payload: Record<string, unknown>,
) =>
api<Clip>(`/api/projects/${projectId}/clips/${clipId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
deleteClip: (projectId: number, clipId: number) =>
api<{ ok: boolean }>(`/api/projects/${projectId}/clips/${clipId}`, {
method: "DELETE",
}),
reorderClips: (projectId: number, sceneId: number, clipIds: number[]) =>
api<{ ok: boolean }>(
`/api/projects/${projectId}/scenes/${sceneId}/clips/reorder`,
{
method: "POST",
body: JSON.stringify({ clip_ids: clipIds }),
},
),
expandClips: (
projectId: number,
payload: { scene_ids?: number[]; all?: boolean; guidance?: string } = {},
) =>
api<{
ok: boolean;
scenes: Array<{
scene_id: number;
clip_count: number;
total_duration_sec: number;
}>;
total_clips: number;
}>(`/api/projects/${projectId}/expand-clips`, {
method: "POST",
body: JSON.stringify(payload),
}),
generateAssets: (
id: number,
payload: {
missing_only?: boolean;
character_ids?: number[];
location_ids?: number[];
item_ids?: number[];
workflow_id?: number;
input_values?: Record<string, unknown>;
asset_target?: "sheet";
prompt?: string;
} = {},
) =>
api<{ ok: boolean; jobs: Job[] }>(`/api/projects/${id}/generate-assets`, {
method: "POST",
body: JSON.stringify(payload),
}),
getAssets: (id: number) =>
api<{ characters: Character[]; locations: Location[]; items: Item[] }>(
`/api/projects/${id}/assets`,
),
};
export const settings = {
get: () => api<Settings>("/api/settings"),
update: (payload: Record<string, unknown>) =>
api<Settings>("/api/settings", {
method: "POST",
body: JSON.stringify(payload),
}),
testLlm: (payload: {
profile_id?: string;
base_url?: string;
model?: string;
api_key?: string;
timeout_sec?: number;
}) =>
api<{
ok: boolean;
elapsed_ms?: number;
error?: string;
detail?: string;
timeout_sec?: number;
}>("/api/settings/llm-test", {
method: "POST",
body: JSON.stringify(payload),
}),
};
export const comfy = {
listEndpoints: () =>
api<{ endpoints: ComfyEndpoint[]; active_id: string | null }>(
"/api/comfyui/endpoints",
),
createEndpoint: (
payload: Partial<ComfyEndpoint> & { base_url: string; auth_token?: string },
) =>
api<{ endpoints: ComfyEndpoint[]; active_id: string | null }>(
"/api/comfyui/endpoints",
{
method: "POST",
body: JSON.stringify(payload),
},
),
updateEndpoint: (
id: string,
payload: Partial<ComfyEndpoint> & { auth_token?: string },
) =>
api<{ endpoints: ComfyEndpoint[]; active_id: string | null }>(
`/api/comfyui/endpoints/${id}`,
{
method: "PATCH",
body: JSON.stringify(payload),
},
),
deleteEndpoint: (id: string) =>
api<{ endpoints: ComfyEndpoint[]; active_id: string | null }>(
`/api/comfyui/endpoints/${id}`,
{
method: "DELETE",
},
),
activateEndpoint: (id: string) =>
api<{ endpoints: ComfyEndpoint[]; active_id: string | null }>(
`/api/comfyui/endpoints/${id}/activate`,
{ method: "POST" },
),
testEndpoint: (payload: {
endpoint_id?: string;
base_url?: string;
auth_type?: string;
auth_token?: string;
header_name?: string;
timeout_sec?: number;
}) =>
api<{
ok: boolean;
elapsed_ms?: number;
comfyui_version?: string;
device_name?: string;
error?: string;
message?: string;
detail?: string;
timeout_sec?: number;
}>("/api/comfyui/endpoints/test", {
method: "POST",
body: JSON.stringify(payload),
}),
};
export const workflows = {
list: () => api<Workflow[]>("/api/workflows"),
/** HEL-682: metadata-only list — 150+ rows without any workflow_json. */
listMeta: () => api<WorkflowMeta[]>("/api/workflows?view=meta"),
/** HEL-682: dry-run UI→API conversion for a batch of files (nothing stored). */
convert: (
files: WorkflowUIFileInput[],
opts?: { endpoint_id?: string | null; refresh?: boolean },
) =>
api<WorkflowConvertReport>("/api/workflows/convert", {
method: "POST",
body: JSON.stringify({
files,
...(opts?.endpoint_id ? { endpoint_id: opts.endpoint_id } : {}),
...(opts?.refresh ? { refresh: opts.refresh } : {}),
}),
}),
/** HEL-682: commit a batch import (converts server-side, conflicts skipped). */
importBatch: (files: WorkflowUIFileInput[]) =>
api<WorkflowImportResult>("/api/workflows/import-batch", {
method: "POST",
body: JSON.stringify({ files }),
}),
/** HEL-682: scan a server-side directory of UI-format JSONs — dry-run report. */
importScanDry: (root: string) =>
api<WorkflowConvertReport>("/api/workflows/import-scan", {
method: "POST",
body: JSON.stringify({ root, dry_run: true }),
}),
/** HEL-682: scan a server-side directory of UI-format JSONs — commit the import. */
importScanCommit: (root: string) =>
api<WorkflowImportResult>("/api/workflows/import-scan", {
method: "POST",
body: JSON.stringify({ root, dry_run: false }),
}),
/** HEL-682: record a use (最近使用). */
touch: (id: number) =>
api<{ ok: boolean }>(`/api/workflows/${id}/touch`, { method: "POST" }),
/** HEL-682: delete byte-identical duplicates, keeping the lowest id. */
dedupe: () =>
api<{
ok: boolean;
removed: number;
kept_ids: number[];
removed_ids: number[];
}>("/api/workflows/dedupe", { method: "POST" }),
analyze: (
workflow_json: Record<string, unknown>,
endpoint_id?: string | null,
refresh = false,
) =>
api<WorkflowAnalyzeResult>("/api/workflows/analyze", {
method: "POST",
body: JSON.stringify({
workflow_json,
...(endpoint_id ? { endpoint_id } : {}),
...(refresh ? { refresh } : {}),
}),
}),
reanalyze: (id: number) =>
api<Workflow>(`/api/workflows/${id}/reanalyze`, { method: "POST" }),
/** L2 re-check against a live ComfyUI (HEL-643): node/model availability. */
check: (
id: number,
payload?: { endpoint_id?: string | null; refresh?: boolean },
) =>
api<WorkflowConnectionCheck>(`/api/workflows/${id}/check`, {
method: "POST",
body: JSON.stringify(payload ?? {}),
}),
/** Launch (or re-attach to) one REAL test run (HEL-643 L3). */
test: (
id: number,
payload: {
endpoint_id?: string | null;
input_values?: Record<string, unknown>;
},
) =>
api<{ ok: boolean; job: Job; reused: boolean }>(
`/api/workflows/${id}/test`,
{
method: "POST",
body: JSON.stringify(payload),
},
),
tests: (id: number) => api<Job[]>(`/api/workflows/${id}/tests`),
clearTests: (id: number) =>
api<{ ok: boolean; removed: number }>(`/api/workflows/${id}/clear-tests`, {
method: "POST",
}),
create: (payload: {
name: string;
workflow_json: Record<string, unknown>;
kind?: "image" | "video";
usage?: WorkflowUsage;
usage_source?: "auto" | "user";
input_schema?: ComfyDynamicInput[];
output_map?: WorkflowOutputMapEntry[];
output_map_source?: "auto" | "user";
description?: string;
prompt_profile?: string;
endpoint_id?: string | null;
}) =>
api<Workflow>("/api/workflows", {
method: "POST",
body: JSON.stringify(payload),
}),
update: (
id: number,
payload: Partial<{
name: string;
kind: string;
is_enabled: boolean;
description: string;
prompt_profile: string;
endpoint_id: string | null;
usage: WorkflowUsage;
usage_source: "auto" | "user";
input_schema: ComfyDynamicInput[];
output_map: WorkflowOutputMapEntry[];
output_map_source: "auto" | "user";
favorite: boolean;
tags: string[];
category: string;
model_family: string;
}>,
) =>
api<Workflow>(`/api/workflows/${id}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
delete: (id: number) =>
api<{ ok: boolean }>(`/api/workflows/${id}`, { method: "DELETE" }),
get: (id: number) => api<Workflow>(`/api/workflows/${id}`),
};
export const jobsApi = {
list: (projectId?: number) =>
api<Job[]>(
`/api/jobs${projectId != null ? `?project_id=${projectId}` : ""}`,
),
retry: (id: number) => api<Job>(`/api/jobs/${id}/retry`, { method: "POST" }),
cancel: (id: number) =>
api<{ ok: boolean }>(`/api/jobs/${id}/cancel`, { method: "POST" }),
pause: () =>
api<{ ok: boolean; paused: boolean }>("/api/jobs/pause", {
method: "POST",
}),
resume: () =>
api<{ ok: boolean; paused: boolean }>("/api/jobs/resume", {
method: "POST",
}),
queueStatus: () => api<{ paused: boolean }>("/api/jobs/queue-status"),
generateVideos: (
projectId: number,
payload: {
scene_ids?: number[];
clip_ids?: number[];
workflow_id?: number;
input_values?: Record<string, unknown>;
prompts?: Record<string, string>;
} = {},
) =>
api<{ ok: boolean; jobs: Job[] }>(
`/api/jobs/projects/${projectId}/generate-videos`,
{
method: "POST",
body: JSON.stringify(payload),
},
),
previewPrompt: (
projectId: number,
payload: {
clip_id?: number;
scene_id?: number;
workflow_id?: number;
input_values?: Record<string, unknown>;
force?: boolean;
},
) =>
api<{
prompt: string;
profile: string;
from_draft: boolean;
based_on: string;
critic?: { ok: boolean; notes: string[] };
}>(`/api/jobs/projects/${projectId}/preview-prompt`, {
method: "POST",
body: JSON.stringify(payload),
}),
exportFilm: (projectId: number) =>
api<{ ok: boolean; job: Job }>(`/api/jobs/projects/${projectId}/export`, {
method: "POST",
}),
};
export type PlaygroundAttachTarget =
"character_sheet" | "location" | "item" | "scene";
export type UploadKind = "image" | "video" | "audio" | "document";
export interface PlaygroundUpload {
ok: boolean;
path: string;
name: string;
kind: UploadKind;
}
export interface PlaygroundUploadListItem {
name: string;
path: string;
kind: UploadKind;
size: number;
mtime: string;
}
export const playgroundApi = {
project: () => api<Project>("/api/playground/project"),
jobs: () => api<Job[]>("/api/playground/jobs"),
generate: (payload: {
workflow_id: number;
input_values?: Record<string, unknown>;
/** Target connection; omitted → workflow binding → active (backend). */
endpoint_id?: string | null;
/** Origin recorded on the job payload (tasks page labels). */
source?: "playground" | "studio-image" | "studio-video";
/** Optional real workbench source (HEL-684) — validated server-side and
* stamped on the job so finished takes can flow back to their row. */
context?: {
project_id: number;
stage: "assets" | "shot";
source_kind: "asset" | "shot";
source_id: string;
label?: string;
};
}) =>
api<{ ok: boolean; job: Job }>("/api/playground/generate", {
method: "POST",
body: JSON.stringify(payload),
}),
upload: (file: File) => {
const form = new FormData();
form.append("file", file);
return apiUpload<PlaygroundUpload>("/api/playground/uploads", form);
},
listUploads: () => api<PlaygroundUploadListItem[]>("/api/playground/uploads"),
deleteJob: (jobId: number) =>
api<{
ok: boolean;
job_id: number;
deleted_files: string[];
missing_files: string[];
removed_workbench_links?: number[];
}>(`/api/playground/jobs/${jobId}`, { method: "DELETE" }),
/** Flow a finished take back to the workbench row it came from (HEL-684).
* The server derives the target from the job's own recorded context —
* idempotent, never overwrites, refuses stale source rows. */
attachWorkbench: (jobId: number) =>
api<{
ok: boolean;
already_attached: boolean;
job_id: number;
attached_job_id: number;
project_id: number;
stage: "assets" | "shot";
target_id: string;
}>("/api/playground/attach-workbench", {
method: "POST",
body: JSON.stringify({ job_id: jobId }),
}),
attach: (payload: {
path: string;
project_id: number;
target: PlaygroundAttachTarget;
character_id?: number;
location_id?: number;
item_id?: number;
scene_id?: number;
name?: string;
}) =>
api<{
ok: boolean;
path: string;
target: string;
project_id: number;
item_id?: number;
}>("/api/playground/attach", {
method: "POST",
body: JSON.stringify(payload),
}),
};
// ── Asset Library (unlinked Playground media) ───────────────────────────
export interface LibraryMediaItem {
path: string;
name: string;
kind: "image" | "video";
size: number;
mtime: string;
}
// ── Five-stage creation workbench (HEL-614) ─────────────────────────────
export type WorkbenchStageKey =
"script" | "style" | "storyboard" | "assets" | "shot";
export type WorkbenchStageStatus =
"todo" | "running" | "awaiting_confirm" | "confirmed";
export interface StageRun {
id: number;
status: "running" | "done" | "failed";
error: string | null;
instruction: string | null;
created_at: string;
completed_at: string | null;
}
export interface WorkbenchJobBrief {
id: number;
status: string;
kind: string;
output: string | null;
error: string | null;
}
export interface StyleDirection {
id: string;
name: string;
description: string;
prompt: string;
negative_prompt: string;
}
export interface StoryboardShot {
id: string;
scene: string;
shot_size: string;
camera: string;
description: string;
dialog: string;
duration_sec: number;
}
export interface WorkbenchAsset {
id: string;
kind: "character" | "location" | "prop";
name: string;
description: string;
prompt: string;
negative_prompt: string;
job?: WorkbenchJobBrief;
}
export interface VideoShot {
id: string;
shot_no: string;
description: string;
prompt: string;
duration_sec: number;
job?: WorkbenchJobBrief;
}
export interface WorkbenchStage {
assistant_note?: string;
stage_key: WorkbenchStageKey;
status: WorkbenchStageStatus;
content: Record<string, unknown> | null;
confirmed_content: Record<string, unknown> | null;
chosen_style_id: string | null;
draft_edited: boolean;
error: string | null;
updated_at: string;
last_run: StageRun | null;
}
export interface WorkbenchOverview {
project: {
id: number;
title: string;
idea: string | null;
status: string;
updated_at: string;
};
stages: WorkbenchStage[];
}
export const workbenchApi = {
saveNote: (projectId: number, stageKey: WorkbenchStageKey, text: string) =>
api<{ ok: boolean; stage: WorkbenchStage }>(
`/api/workbench/projects/${projectId}/stages/${stageKey}/note`,
{ method: "PATCH", body: JSON.stringify({ text }) },
),
overview: (projectId: number) =>
api<WorkbenchOverview>(`/api/workbench/projects/${projectId}`),
patchStage: (
projectId: number,
stageKey: WorkbenchStageKey,
payload: {
content: Record<string, unknown>;
chosen_style_id?: string | null;
},
) =>
api<{ ok: boolean; stage: WorkbenchStage }>(
`/api/workbench/projects/${projectId}/stages/${stageKey}`,
{ method: "PATCH", body: JSON.stringify(payload) },
),
runStage: (
projectId: number,
stageKey: WorkbenchStageKey,
instruction?: string,
) =>
api<{ ok: boolean; run_id: number }>(
`/api/workbench/projects/${projectId}/stages/${stageKey}/run`,
{
method: "POST",
body: JSON.stringify({ instruction: instruction ?? null }),
},
),
confirmStage: (
projectId: number,
stageKey: WorkbenchStageKey,
chosenStyleId?: string | null,
) =>
api<{ ok: boolean; stage: WorkbenchStage }>(
`/api/workbench/projects/${projectId}/stages/${stageKey}/confirm`,
{
method: "POST",
body: JSON.stringify(
chosenStyleId ? { chosen_style_id: chosenStyleId } : {},
),
},
),
restoreStage: (projectId: number, stageKey: WorkbenchStageKey) =>
api<{ ok: boolean; stage: WorkbenchStage }>(
`/api/workbench/projects/${projectId}/stages/${stageKey}/restore`,
{ method: "POST" },
),
importScript: (projectId: number, text: string) =>
api<{ ok: boolean; stage: WorkbenchStage }>(
`/api/workbench/projects/${projectId}/stages/script/import`,
{ method: "POST", body: JSON.stringify({ text }) },
),
generate: (
projectId: number,
payload: {
kind: "image" | "video";
stage_key: WorkbenchStageKey;
target_id?: string;
prompt: string;
negative_prompt?: string;
duration_sec?: number;
},
) =>
api<{ ok: boolean; job: Job }>(
`/api/workbench/projects/${projectId}/generate`,
{
method: "POST",
body: JSON.stringify(payload),
},
),
};
export interface LibraryDeleteResult {
path: string;
status: "deleted" | "kept";
reason?: string;
}
export const libraryApi = {
list: () => api<LibraryMediaItem[]>("/api/library/media"),
deleteMany: (paths: string[]) =>
api<{
ok: boolean;
deleted: string[];
results: LibraryDeleteResult[];
}>("/api/library/media", {
method: "DELETE",
body: JSON.stringify({ paths }),
}),
};
// ── Agents (agentic harness) ────────────────────────────────────────────
export interface AgentSession {
id: number;
project_id: number | null;
title: string;
status: "idle" | "running" | "error";
/** WHERE the session was born: 'chat' (AI Canvas) or 'scene' (Build Scene rail). */
origin: "chat" | "scene";
/** WHICH named agent owns the session (HEL-613); null = plain harness chat. */
agent_key?: string | null;
created_at: string;
updated_at: string;
running?: boolean;
project?: { id: number; title: string; status: string } | null;
}
export interface NamedAgent {
key: string;
name: string;
char: string;
avatar: number;
avatar_dark: boolean;
llm_role: string;
tagline: string;
skill_count: number;
model_profile_id?: string | null;
}
export interface SkillAdminEntry {
name: string;
description: string;
version: string;
tags: string[];
dir: string;
type: string;
updated_at: number | null;
is_enabled: boolean;
bindings: Record<string, boolean>;
assigned_count: number;
}
export interface SkillVersion {
id: number;
skill: string;
created_at: string;
length: number;
preview: string;
}
export interface AgentMessage {
id: number;
session_id: number;
role: "user" | "assistant" | "tool" | "system";
content: string;
agent_name?: string | null;
tool_name?: string | null;
tool_args?: Record<string, unknown> | null;
tool_result?: unknown;
status?: string | null;
reasoning?: string | null;
created_at: string;
mentions?: AgentComposerPayload["mentions"];
attachments?: AgentComposerPayload["attachments"];
}
export interface AgentTask {
role: string;
goal: string;
status: "pending" | "running" | "done" | "failed" | "skipped";
index?: number;
}
export interface AgentPlan {
tasks: AgentTask[];
note?: string | null;
}
export const agentApi = {
listSessions: (projectId?: number) =>
api<AgentSession[]>(
`/api/agent/sessions${projectId != null ? `?project_id=${projectId}` : ""}`,
),
listSceneSessions: () =>
api<AgentSession[]>("/api/agent/sessions?origin=scene"),
listAgentSessions: (agentKey: string) =>
api<AgentSession[]>(
`/api/agent/sessions?agent_key=${encodeURIComponent(agentKey)}`,
),
listNamedAgents: () => api<NamedAgent[]>("/api/agent/agents"),
listLinkableProjects: () =>
api<{ id: number; title: string; status: string }[]>("/api/agent/projects"),
createSession: (payload: {
title?: string;
project_id?: number | null;
origin?: "chat" | "scene";
agent_key?: string | null;
}) =>
api<AgentSession>("/api/agent/sessions", {
method: "POST",
body: JSON.stringify(payload),
}),
getSession: (id: number) =>
api<AgentSession & { messages: AgentMessage[]; plan?: AgentPlan | null }>(
`/api/agent/sessions/${id}`,
),
patchSession: (
id: number,
payload: { title?: string; project_id?: number | null; unlink?: boolean },
) =>
api<AgentSession>(`/api/agent/sessions/${id}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
deleteSession: (id: number) =>
api<{ ok: boolean }>(`/api/agent/sessions/${id}`, { method: "DELETE" }),
postMessage: (id: number, payload: AgentComposerPayload | string) => {
const body =
typeof payload === "string"
? { content: payload, mentions: [], attachments: [] }
: payload;
return api<{ ok: boolean; steered?: boolean; message: AgentMessage }>(
`/api/agent/sessions/${id}/messages`,
{
method: "POST",
body: JSON.stringify(body),
},
);
},
cancel: (id: number) =>
api<{ ok: boolean }>(`/api/agent/sessions/${id}/cancel`, {
method: "POST",
}),
listSkills: () =>
api<
{
name: string;
description: string;
version: string;
tags: string[];
dir: string;
}[]
>("/api/agent/skills"),
listSkillsAdmin: () => api<SkillAdminEntry[]>("/api/agent/skills?all=true"),
upsertSkill: (
dir: string,
payload: {
description: string;
version?: string;
tags?: string[];
skill_type?: string;
content: string;
bindings?: Record<string, boolean>;
},
) =>
api<{ ok: boolean; skill: SkillAdminEntry }>(
`/api/agent/skills/${encodeURIComponent(dir)}`,
{ method: "PUT", body: JSON.stringify(payload) },
),
patchSkill: (dir: string, isEnabled: boolean) =>
api<{ ok: boolean }>(`/api/agent/skills/${encodeURIComponent(dir)}`, {
method: "PATCH",
body: JSON.stringify({ is_enabled: isEnabled }),
}),
putSkillBindings: (dir: string, bindings: Record<string, boolean>) =>
api<{ ok: boolean }>(
`/api/agent/skills/${encodeURIComponent(dir)}/bindings`,
{
method: "PUT",
body: JSON.stringify({ bindings }),
},
),
deleteSkill: (dir: string) =>
api<{ ok: boolean }>(`/api/agent/skills/${encodeURIComponent(dir)}`, {
method: "DELETE",
}),
listSkillVersions: (dir: string) =>
api<SkillVersion[]>(
`/api/agent/skills/${encodeURIComponent(dir)}/versions`,
),
restoreSkillVersion: (dir: string, versionId: number) =>
api<{ ok: boolean; skill: SkillAdminEntry }>(
`/api/agent/skills/${encodeURIComponent(dir)}/versions/${versionId}/restore`,
{ method: "POST" },
),
listMemories: () =>
api<
{
id: number;
scope: "global" | "project";
project_id: number | null;
project_title: string | null;
content: string;
kind: string;
source: string;
use_count: number;
created_at: string;
}[]
>("/api/agent/memories"),
addMemory: (payload: {
content: string;
scope: "global" | "project";
project_id?: number | null;
kind?: string;
}) =>
api<{ ok: boolean; id: number }>("/api/agent/memories", {
method: "POST",
body: JSON.stringify(payload),
}),
deleteMemory: (id: number) =>
api<{ ok: boolean }>(`/api/agent/memories/${id}`, { method: "DELETE" }),
skillsPath: () => api<{ path: string }>("/api/agent/skills/path"),
skillFiles: (name: string) =>
api<{ skill: string; files: string[] }>(
`/api/agent/skills/${encodeURIComponent(name)}/files`,
),
readSkillFile: (name: string, path: string) =>
api<{ ok: boolean; content: string; truncated: boolean }>(
`/api/agent/skills/${encodeURIComponent(name)}/file?path=${encodeURIComponent(path)}`,
),
};
export interface CanvasInfo {
id: number;
title: string;
project_id: number | null;
agent_session_id: number | null;
updated_at: string;
}
export interface CanvasNode {
id: number;
canvas_id: number;
type: "entity" | "image" | "video" | "text" | "workflow";
title: string | null;
x: number;
y: number;
width: number | null;
height: number | null;
workflow_id: number | null;
artifact_path: string | null;
job_id: number | null;
status: string;
input_values_json: string;
entity_type: string | null;
entity_id: number | null;
}
export interface CanvasEdge {
id: number;
canvas_id: number;
src_node_id: number;
dst_node_id: number;
kind: "data" | "link";
label: string | null;
dst_role: string | null;
dst_comfy_node_id: string | null;
}
export interface CanvasGraph {
canvas: CanvasInfo & {
project?: { id: number; title: string } | null;
viewport_json?: string | null;
};
nodes: CanvasNode[];
edges: CanvasEdge[];
}
export const canvasApi = {
list: () => api<CanvasInfo[]>("/api/canvas"),
ensureForProject: (projectId: number, title?: string) =>
api<CanvasGraph>("/api/canvas", {
method: "POST",
body: JSON.stringify({
project_id: projectId,
...(title ? { title } : {}),
}),
}),
ensureForSession: (sessionId: number) =>
api<CanvasGraph>("/api/canvas", {
method: "POST",
body: JSON.stringify({ agent_session_id: sessionId }),
}),
get: (id: number) => api<CanvasGraph>(`/api/canvas/${id}`),
patchCanvas: (
id: number,
payload: { title?: string; viewport_json?: string },
) =>
api<
CanvasInfo & {
project?: { id: number; title: string } | null;
viewport_json?: string | null;
}
>(`/api/canvas/${id}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
patchNode: (
canvasId: number,
nodeId: number,
payload: {
x?: number;
y?: number;
title?: string;
input_values_json?: string;
status?: string;
job_id?: number;
artifact_path?: string;
},
) =>
api<CanvasNode>(`/api/canvas/${canvasId}/nodes/${nodeId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
createNode: (
canvasId: number,
payload: {
type: "workflow" | "image" | "video";
title?: string;
x?: number;
y?: number;
workflow_id?: number;
artifact_path?: string;
job_id?: number;
status?: string;
input_values_json?: string;
},
) =>
api<CanvasNode>(`/api/canvas/${canvasId}/nodes`, {
method: "POST",
body: JSON.stringify(payload),
}),
deleteNode: (canvasId: number, nodeId: number) =>
api<{ ok: boolean; file_deleted?: boolean; reason?: string | null }>(
`/api/canvas/${canvasId}/nodes/${nodeId}`,
{ method: "DELETE" },
),
tidy: (canvasId: number) =>
api<{ ok: boolean; moved: number }>(`/api/canvas/${canvasId}/tidy`, {
method: "POST",
}),
createEdge: (
canvasId: number,
payload: {
src_node_id: number;
dst_node_id: number;
kind: "data" | "link";
label?: string;
dst_role?: string;
dst_comfy_node_id?: string;
},
) =>
api<CanvasEdge>(`/api/canvas/${canvasId}/edges`, {
method: "POST",
body: JSON.stringify(payload),
}),
patchEdge: (canvasId: number, edgeId: number, payload: { label: string }) =>
api<CanvasEdge>(`/api/canvas/${canvasId}/edges/${edgeId}`, {
method: "PATCH",
body: JSON.stringify(payload),
}),
deleteEdge: (canvasId: number, edgeId: number) =>
api<{ ok: boolean }>(`/api/canvas/${canvasId}/edges/${edgeId}`, {
method: "DELETE",
}),
};