adding continuous
This commit is contained in:
@@ -66,3 +66,81 @@ export function getFeedback(): Promise<FeedbackEntry[]> {
|
||||
export function deleteRecommendation(id: string): Promise<{ ok: boolean }> {
|
||||
return request(`/recommendations/${id}`, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
export function createContinuousRecommendation(body: {
|
||||
liked_shows: string;
|
||||
disliked_shows?: string;
|
||||
themes?: string;
|
||||
requirements?: string;
|
||||
avoid?: string;
|
||||
total_count: number;
|
||||
media_type: MediaType;
|
||||
use_web_search?: boolean;
|
||||
validate_results?: boolean;
|
||||
}): Promise<{ id: string }> {
|
||||
return (async () => {
|
||||
const res = await fetch(`${BASE}/recommendations/continuous`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'text/event-stream',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const text = await res.text();
|
||||
throw new Error(`HTTP ${res.status}: ${text}`);
|
||||
}
|
||||
|
||||
if (!res.body) {
|
||||
throw new Error('Missing response stream for continuous recommendation');
|
||||
}
|
||||
|
||||
const reader = res.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
|
||||
if (value) {
|
||||
buffer += decoder.decode(value, { stream: !done });
|
||||
}
|
||||
|
||||
let boundary = buffer.indexOf('\n\n');
|
||||
while (boundary !== -1) {
|
||||
const rawEvent = buffer.slice(0, boundary).trim();
|
||||
buffer = buffer.slice(boundary + 2);
|
||||
|
||||
if (rawEvent) {
|
||||
const dataLine = rawEvent
|
||||
.split('\n')
|
||||
.find((line) => line.startsWith('data:'));
|
||||
|
||||
if (dataLine) {
|
||||
const payload = dataLine.slice(5).trim();
|
||||
const event = JSON.parse(payload) as { type?: string; id?: string };
|
||||
|
||||
if (event.type === 'created' && event.id) {
|
||||
await reader.cancel();
|
||||
return { id: event.id };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boundary = buffer.indexOf('\n\n');
|
||||
}
|
||||
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
|
||||
throw new Error('Continuous recommendation stream ended before returning an id');
|
||||
})();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user