Generator for SSE events.
()
| 2981 | ) |
| 2982 | |
| 2983 | def generate(): |
| 2984 | """Generator for SSE events.""" |
| 2985 | import secrets as py_secrets |
| 2986 | from pgadmin.llm.models import Message, Role |
| 2987 | |
| 2988 | try: |
| 2989 | # Send thinking status |
| 2990 | yield _nlq_sse_event({ |
| 2991 | 'type': 'thinking', |
| 2992 | 'message': gettext('Analyzing your request...') |
| 2993 | }) |
| 2994 | |
| 2995 | # Deserialize conversation history if provided |
| 2996 | conversation_history = None |
| 2997 | if history_data: |
| 2998 | conversation_history = [] |
| 2999 | for item in (history_data or []): |
| 3000 | if not isinstance(item, dict): |
| 3001 | continue |
| 3002 | role_str = item.get('role', '') |
| 3003 | content = item.get('content', '') |
| 3004 | try: |
| 3005 | role = Role(role_str) |
| 3006 | except ValueError: |
| 3007 | continue |
| 3008 | conversation_history.append( |
| 3009 | Message(role=role, content=content) |
| 3010 | ) |
| 3011 | |
| 3012 | # Stream the LLM response with database tools |
| 3013 | response_text = '' |
| 3014 | updated_messages = [] |
| 3015 | for item in chat_with_database_stream( |
| 3016 | user_message=user_message, |
| 3017 | sid=trans_obj.sid, |
| 3018 | did=trans_obj.did, |
| 3019 | system_prompt=NLQ_SYSTEM_PROMPT, |
| 3020 | conversation_history=conversation_history |
| 3021 | ): |
| 3022 | if isinstance(item, str): |
| 3023 | # Text chunk from streaming LLM response |
| 3024 | yield _nlq_sse_event({ |
| 3025 | 'type': 'text_delta', |
| 3026 | 'content': item |
| 3027 | }) |
| 3028 | elif isinstance(item, tuple) and \ |
| 3029 | item[0] == 'tool_use': |
| 3030 | # Tool execution in progress - reset streaming |
| 3031 | yield _nlq_sse_event({ |
| 3032 | 'type': 'thinking', |
| 3033 | 'message': gettext( |
| 3034 | 'Querying the database...' |
| 3035 | ) |
| 3036 | }) |
| 3037 | elif isinstance(item, tuple) and \ |
| 3038 | item[0] == 'complete': |
| 3039 | # Final result: ('complete', response_text, messages) |
| 3040 | response_text = item[1] |
no test coverage detected