| |
| |
| |
| |
| |
| |
| |
| |
| |
| function authHeaders(apiKey) { |
| return apiKey ? { 'X-API-Key': apiKey } : {}; |
| } |
| |
| |
| |
| |
| |
| |
| export async function uploadToEditSession(params) { |
| const { backendUrl, apiKey, conversationId, file, instruction } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}/image`; |
| const fd = new FormData(); |
| fd.append('file', file); |
| if (instruction?.trim()) { |
| fd.append('instruction', instruction.trim()); |
| } |
| const res = await fetch(url, { |
| method: 'POST', |
| headers: { ...authHeaders(apiKey) }, |
| body: fd, |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| export async function sendEditMessage(params) { |
| const { backendUrl, apiKey, conversationId, ...body } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}/message`; |
| const res = await fetch(url, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| ...authHeaders(apiKey), |
| }, |
| body: JSON.stringify(body), |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| export async function selectActiveImage(params) { |
| const { backendUrl, apiKey, conversationId, image_url } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}/select`; |
| const res = await fetch(url, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| ...authHeaders(apiKey), |
| }, |
| body: JSON.stringify({ image_url }), |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| export async function getEditSession(params) { |
| const { backendUrl, apiKey, conversationId } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}`; |
| const res = await fetch(url, { |
| method: 'GET', |
| headers: { ...authHeaders(apiKey) }, |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| export async function clearEditSession(params) { |
| const { backendUrl, apiKey, conversationId } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}`; |
| const res = await fetch(url, { |
| method: 'DELETE', |
| headers: { ...authHeaders(apiKey) }, |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| export async function deleteVersion(params) { |
| const { backendUrl, apiKey, conversationId, imageUrl } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}/version?image_url=${encodeURIComponent(imageUrl)}`; |
| const res = await fetch(url, { |
| method: 'DELETE', |
| headers: { ...authHeaders(apiKey) }, |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| export async function revertToHistory(params) { |
| const { backendUrl, apiKey, conversationId, index } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const url = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}/revert?index=${index}`; |
| const res = await fetch(url, { |
| method: 'POST', |
| headers: { ...authHeaders(apiKey) }, |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function extractImages(raw) { |
| const out = []; |
| const add = (u) => { |
| if (typeof u === 'string' && (u.startsWith('http://') || u.startsWith('https://'))) { |
| out.push(u); |
| } |
| }; |
| |
| const media = raw?.media; |
| if (media?.images && Array.isArray(media.images)) { |
| media.images.forEach(add); |
| } |
| |
| if (raw?.images && Array.isArray(raw.images)) { |
| raw.images.forEach(add); |
| } |
| |
| const data = raw?.data; |
| if (data?.media) { |
| const dataMedia = data.media; |
| if (dataMedia?.images && Array.isArray(dataMedia.images)) { |
| dataMedia.images.forEach(add); |
| } |
| } |
| |
| return Array.from(new Set(out)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function importImageByUrl(params) { |
| const { backendUrl, apiKey, url } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const endpoint = `${base}/v1/import-image`; |
| const res = await fetch(endpoint, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| ...authHeaders(apiKey), |
| }, |
| body: JSON.stringify({ url }), |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function uploadToEditSessionByUrl(params) { |
| const { backendUrl, apiKey, conversationId, imageUrl } = params; |
| const base = backendUrl.replace(/\/+$/, ''); |
| const endpoint = `${base}/v1/edit-sessions/${encodeURIComponent(conversationId)}/image`; |
| const res = await fetch(endpoint, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| ...authHeaders(apiKey), |
| }, |
| body: JSON.stringify({ image_url: imageUrl }), |
| }); |
| if (!res.ok) { |
| const text = await res.text().catch(() => `HTTP ${res.status}`); |
| throw new Error(text); |
| } |
| return res.json(); |
| } |
|
|