File size: 2,821 Bytes
801f06c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import type {
Asset,
Character,
CharacterInput,
Conversation,
ConversationInput,
Scene,
SceneInput,
Generation,
SystemStatus,
Turn,
} from './types';
const root = '/api/v1';
export async function request<T>(path: string, options: RequestInit = {}): Promise<T> {
const response = await fetch(root + path, {
...options,
headers: {
...(options.body instanceof FormData ? {} : { 'Content-Type': 'application/json' }),
...options.headers,
},
});
if (!response.ok) {
let message = `Request failed (${response.status})`;
try {
const body = await response.json();
message =
typeof body.detail === 'string'
? body.detail
: body.detail?.map((e: { msg: string }) => e.msg).join('; ') || message;
} catch {}
throw new Error(message);
}
return response.status === 204 ? (undefined as T) : response.json();
}
const body = (value: unknown) => ({ method: 'POST', body: JSON.stringify(value) });
export const assetUrl = (id: string) => `${root}/assets/${encodeURIComponent(id)}/file`;
export const api = {
characters: () => request<Character[]>('/characters'),
saveCharacter: (data: CharacterInput, id?: string) =>
request<Character>(`/characters${id ? '/' + id : ''}`, {
...body(data),
method: id ? 'PUT' : 'POST',
}),
conversations: () => request<Conversation[]>('/conversations'),
saveConversation: (data: ConversationInput, id?: string) =>
request<Conversation>(`/conversations${id ? '/' + id : ''}`, {
...body(data),
method: id ? 'PUT' : 'POST',
}),
draft: (topic: string, character_ids: string[]) =>
request<{ turns: Turn[] }>('/conversations/draft', body({ topic, character_ids })),
scenes: () => request<Scene[]>('/scenes'),
saveScene: (data: SceneInput, id?: string) =>
request<Scene>(`/scenes${id ? '/' + id : ''}`, { ...body(data), method: id ? 'PUT' : 'POST' }),
assets: () => request<Asset[]>('/assets'),
upload: (form: FormData) => request<Asset>('/assets', { method: 'POST', body: form }),
revoke: (id: string) => request(`/assets/${id}/revoke`, body({})),
jobs: () => request<Generation[]>('/generations'),
generate: (conversation_id: string, scene_id: string, kind: string, target_seconds: number) =>
request<Generation>('/generations', body({ conversation_id, scene_id, kind, target_seconds })),
cancel: (id: string) => request(`/generations/${id}/cancel`, body({})),
retry: (id: string) => request(`/generations/${id}/retry`, body({})),
review: (id: string, review: Record<string, boolean | string>) =>
request(`/generations/${id}/review`, body(review)),
status: () => request<SystemStatus>('/system/status'),
check: () =>
request<Record<string, { ready: boolean; message: string }>>('/system/check', body({})),
};
|