const response = await fetch('/api/explore/chat', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: 'Compare Nike and Adidas',
selected_competitor_ids: ['uuid1', 'uuid2'],
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value);
const lines = chunk.split('\n');
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = JSON.parse(line.slice(6));
if (data.type === 'token') {
// Append token to response
console.log(data.content);
} else if (data.type === 'search') {
// Show search indicator
console.log('Searching:', data.query);
} else if (data.type === 'done') {
// Generation complete
console.log('Done:', data.usage);
}
}
}
}