const response = await fetch('/api/daily-fortune-stream', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
saju,
userName: '김철수',
date: '2025-01-16',
model: 'haiku'
})
});
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.chunk) {
// 텍스트 청크 - UI에 추가
console.log(data.chunk);
}
if (data.done) {
// 스트리밍 완료 - 전체 데이터 사용 가능
console.log('완료:', data.fortune);
}
}
}
}