import json
from .models import Message
from django.http import StreamingHttpResponse

def stream_generator(stream_response: bytes, conversation_id: str):
    full_content = ''
    for chunk in stream_response.iter_content(chunk_size=1024):
        if chunk:
            full_content += chunk.decode('utf-8')
            try:
                data = json.loads(full_content)
                content = data.get("choices", [{}])[0].get("delta", {}).get("content", "")
                Message.objects.create(
                    conversation_id=conversation_id,
                    content=content
                )
                yield f" data:{content}\n\n"
                full_content = ''
            except json.JSONDecodeError:
                continue
    return StreamingHttpResponse(stream_generator(stream_response, conversation_id), content_type='text/event-stream')
    
