refactor: centralize source API requests, polling, and path parsing

- Add request_source_response and request_source_json helpers with 429 retry logic
- Introduce shared split_path_segments and parse_provider_model_url utilities
- Migrate CivitAI, CivArchive, HuggingFace, and ModelList sources to centralized helpers
- Add generic pollBackgroundTask utility for frontend progress polling
- Add tests for request_source_json, split_path_segments, parse_provider_model_url
- Add huggingface-hub to requirements
This commit is contained in:
Dariusz L
2026-07-14 12:03:35 +02:00
parent a5b54bb7de
commit 8cf688eba6
14 changed files with 376 additions and 204 deletions
+40
View File
@@ -119,3 +119,43 @@ export function sanitizeDescriptionHtml(html) {
Array.from(root.childNodes).forEach(child => sanitizeNode(child));
return root.innerHTML;
}
/**
* Generic background task progress poller with token cancellation checks.
*/
export async function pollBackgroundTask({
endpoint,
tokenCheck,
onProgress,
isTerminal,
onTerminal,
onError,
intervalMs = 250,
fetchJson,
filterIgnoredStatus
}) {
while (tokenCheck()) {
try {
const data = await fetchJson(endpoint, { silent: true }, 'Poll background task');
if (filterIgnoredStatus && filterIgnoredStatus(data)) {
await new Promise(resolve => setTimeout(resolve, intervalMs));
continue;
}
if (tokenCheck()) {
onProgress(data);
if (isTerminal(data)) {
onTerminal(data);
return;
}
}
} catch (error) {
if (tokenCheck()) {
onError(error);
return;
}
}
await new Promise(resolve => setTimeout(resolve, intervalMs));
}
}