Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

24.6 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

// bot_completion.go contains the SSE envelope writer and ChatbotCompletion
// service path for /api/v1/chatbots/<dialog_id>/completions.
//
// Frame shape (one JSON object per `data:` line):
//
//	{"code":0,"message":"success","data":{"answer":"...","reference":{...},
//	 "audio_binary":null,"id":"...","session_id":"..."}, ...}
//
// The final completion marker is `data: {"code":0,"message":"success",
// "data":true}` followed by the OpenAI-style `data: [DONE]` line
// that the existing Go SSE writers emit on the production
// /agents/chat/completions path.

package service

import (
	"context"
	"encoding/json"
	"errors"
	"net/http"
	"ragflow/internal/dao"
	"strings"
	"time"

	"go.uber.org/zap"

	"ragflow/internal/agent/canvas"
	"ragflow/internal/agent/runtime"
	"ragflow/internal/common"
	"ragflow/internal/entity"
	"ragflow/internal/permission"
	permissionresponse "ragflow/internal/permission/response"
)

// ChatbotSSEFrame is one envelope pushed to the SSE writer by the
// chatbot completion path. Err takes precedence over Data and is
// rendered as a python-style {code:500, message:str(e),
// data:{answer:"**ERROR**..."}} frame.
type ChatbotSSEFrame struct {
	// Event is the canvas.RunEvent type ("message",
	// "user_inputs", "workflow_finished", etc.). It is
	// forwarded in the SSE envelope as the `event` field so the
	// front-end can distinguish interactive form pauses from
	// plain assistant text (PR #14589). The field is omitted
	// from the JSON when empty to preserve the original wire
	// shape for callers that do not set it.
	Event     string         `json:"event,omitempty"`
	Data      string         `json:"-"`
	Reference map[string]any `json:"-"`
	SessionID string         `json:"-"`
	Done      bool           `json:"-"`
	Err       error          `json:"-"`
	// Final marks the last answer frame of a turn. It is
	// rendered as `"final": true` in the data payload so the
	// front-end replaces (instead of appends to) the
	// accumulated text — required because the final pipeline
	// result is decorated (citation markers inserted mid-text)
	// and no longer a strict superset of the streamed deltas.
	Final bool `json:"-"`
	// StartToThink / EndToThink bracket the reasoning segment,
	// rendered as start_to_think / end_to_think so the
	// front-end can wrap it in <think> markers like the python
	// side does.
	StartToThink bool `json:"-"`
	EndToThink   bool `json:"-"`
}

// WriteChatbotFrame emits one SSE frame and flushes the underlying
// http.ResponseWriter. The frame is `data: <json>\n\n`.
//
// Error frames sanitize the message — internal errors (gorm stack
// frames, SQL details, storage paths) MUST NOT be echoed to the
// client. The caller is expected to log the real error via
// common.Error / zap before publishing the frame; only a generic
// placeholder is rendered here. Mirrors the python
// `api/db/services/conversation_service.py` error frame shape.
func WriteChatbotFrame(w http.ResponseWriter, f ChatbotSSEFrame) error {
	var payload map[string]any
	if f.Err != nil {
		const clientErrMsg = "an internal error occurred"
		payload = map[string]any{
			"code":    500,
			"message": clientErrMsg,
			"data": map[string]any{
				"answer":    clientErrMsg,
				"reference": map[string]any{},
			},
		}
	} else {
		data := map[string]any{
			"answer":       f.Data,
			"reference":    f.Reference,
			"audio_binary": nil,
			"id":           nil,
			"session_id":   f.SessionID,
		}
		if f.Final {
			data["final"] = true
		}
		if f.StartToThink {
			data["start_to_think"] = true
		}
		if f.EndToThink {
			data["end_to_think"] = true
		}
		// Forward the canvas event type so the front-end can
		// distinguish interactive form pauses ("user_inputs",
		// "workflow_finished") from plain assistant messages
		// (PR #14589). When Event is empty the field is omitted
		// from the JSON so existing message frames stay
		// byte-compatible.
		if f.Event != "" {
			data["event"] = f.Event
		}
		payload = map[string]any{
			"code":    0,
			"message": "success",
			"data":    data,
		}
	}
	// Use SafeJSONMarshal to handle non-serializable values (funcs,
	// channels) that may have leaked into SSE payload maps. Mirrors
	// the Python PR #14210 _canvas_json_default fallback in agent_api.py.
	b, err := runtime.SafeJSONMarshal(payload)
	if err != nil {
		return err
	}
	if _, err := w.Write([]byte("data: ")); err != nil {
		return err
	}
	if _, err := w.Write(b); err != nil {
		return err
	}
	if _, err := w.Write([]byte("\n\n")); err != nil {
		return err
	}
	if flusher, ok := w.(http.Flusher); ok {
		flusher.Flush()
	}
	return nil
}

// WriteDoneFrame emits the completion marker
// `data: {"code":0,"message":"success","data":true}\n\n` followed by the
// OpenAI-style `data: [DONE]\n\n` terminator. Used by both bot
// completion paths.
func WriteDoneFrame(w http.ResponseWriter) error {
	if _, err := w.Write([]byte(`data: {"code":0,"message":"success","data":true}` + "\n\n")); err != nil {
		return err
	}
	if _, err := w.Write([]byte("data: [DONE]\n\n")); err != nil {
		return err
	}
	if flusher, ok := w.(http.Flusher); ok {
		flusher.Flush()
	}
	return nil
}

// WriteChatbotRunEvent translates one canvas.RunEvent into the flat
// Python agent-canvas SSE envelope:
//
//	data: {"event":"message","message_id":"...","task_id":"session-id",
//	  "session_id":"session-id","created_at":123,"data":{"content":"..."}}\n\n
//
// This is intentionally different from WriteChatbotFrame's legacy
// chatbot `{code,data:{answer:"..."}}` shape. The agent React page's
// use-send-message.ts parser appends each parsed object directly to
// answerList and expects top-level `event` / `message_id`, plus a
// typed `data` payload. If RunEvent frames are double-wrapped in
// data.answer, the browser receives bytes but cannot render the
// assistant message or correlate the current Log panel.
//
// The "done" event type emits `data:[DONE]\n\n` (no envelope),
// matching the Python agent API terminator.
//
// Returns the write error so callers can short-circuit; both nil
// and io.ErrClosedPipe are tolerated because the client may have
// disconnected mid-stream.
func WriteChatbotRunEvent(w http.ResponseWriter, ev canvas.RunEvent) error {
	if ev.Type == "done" {
		_, err := w.Write([]byte("data:[DONE]\n\n"))
		if err != nil {
			return err
		}
		if flusher, ok := w.(http.Flusher); ok {
			flusher.Flush()
		}
		return nil
	}

	var data any = map[string]any{}
	if ev.Data != "" {
		if err := json.Unmarshal([]byte(ev.Data), &data); err != nil {
			data = ev.Data
		}
	}
	if ev.Type == "error" {
		msg := "an internal error occurred"
		if m, ok := data.(map[string]any); ok {
			if kind, _ := m["kind"].(string); kind == canvas.RunErrorKindInternal {
				msg = canvas.InternalRunErrorMessage
			} else if s, _ := m["message"].(string); s != "" {
				msg = s
			}
		}
		payload := map[string]any{
			"code":    500,
			"message": msg,
			"data":    false,
		}
		// Keep the error envelope wire-compatible while still correlating the
		// failed run. task_id is only the legacy alias; both values are the
		// same session identity used by the Go runtime and cancel endpoint.
		if ev.SessionID != "" {
			payload["task_id"] = ev.SessionID
			payload["session_id"] = ev.SessionID
		}
		return writeSSEJSON(w, payload)
	}

	payload := map[string]any{
		"data":       data,
		"created_at": ev.CreatedAt,
	}
	if ev.Type != "" {
		payload["event"] = ev.Type
	}
	if ev.MessageID != "" {
		payload["message_id"] = ev.MessageID
	}
	if ev.SessionID != "" {
		// task_id is retained only as a wire-compatible alias for existing Go
		// Agent clients. It carries session_id and has no independent runtime,
		// Redis, or cancellation identity.
		payload["task_id"] = ev.SessionID
		payload["session_id"] = ev.SessionID
	}
	return writeSSEJSON(w, payload)
}

func writeSSEJSON(w http.ResponseWriter, payload map[string]any) error {
	b, err := runtime.SafeJSONMarshal(payload)
	if err != nil {
		return err
	}
	if _, err := w.Write([]byte("data:")); err != nil {
		return err
	}
	if _, err := w.Write(b); err != nil {
		return err
	}
	if _, err := w.Write([]byte("\n\n")); err != nil {
		return err
	}
	if flusher, ok := w.(http.Flusher); ok {
		flusher.Flush()
	}
	return nil
}

// ChatbotCompletion streams an SSE response for
// /api/v1/chatbots/<dialog_id>/completions.
//
// The completion runs through ChatPipelineService.AsyncChat — the
// same RAG pipeline that serves the regular chat endpoints — so
// knowledge-base retrieval, the dialog's configured empty_response
// fallback, citations and the system prompt all behave identically
// to the in-app chat. Mirrors the python
// api/db/services/conversation_service.py::async_iframe_completion,
// which delegates to the same async_chat used by regular sessions.
//
// Authorization is checked against the Chat resource before the
// completion pipeline or session state is touched.
func (s *BotService) ChatbotCompletion(
	ctx context.Context, dialogID string, req ChatbotCompletionRequest,
) (<-chan ChatbotSSEFrame, common.ErrorCode, error) {
	receivedAt := float64(time.Now().UnixNano()) / 1e9
	question, err := ResolveCompletionQuestion(req.Question, req.Query, req.Messages)
	if err != nil {
		return nil, common.CodeArgumentError, err
	}
	req.Question = question
	// 1. Authorize and load the dialog before touching session state.
	if err := permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, permission.ResourceRef{Kind: permission.ResourceKindChat, ID: dialogID}, permission.OperationRun); err != nil {
		code, normalizedErr := permissionresponse.NormalizeHidden(err)
		return nil, code, normalizedErr
	}
	userID := permission.UserIDFromContext(ctx)
	dialog, err := s.chatDAO.GetDialogByID(ctx, dao.DB, dialogID)
	if err != nil {
		if dao.IsNotFoundErr(err) {
			code, normalizedErr := permissionresponse.NormalizeHidden(permission.ErrResourceNotFound)
			return nil, code, normalizedErr
		}
		return nil, common.CodeServerError, err
	}
	if dialog == nil {
		code, normalizedErr := permissionresponse.NormalizeHidden(permission.ErrResourceNotFound)
		return nil, code, normalizedErr
	}

	// 2. Resolve or create the session row.
	//
	// API4ConversationDAO.GetBySessionID returns (nil, nil) on miss
	// (not an error) — see internal/dao/api_token.go:146. We MUST
	// check the pointer before dereferencing, otherwise the
	// session-tenant check below nil-derefs. Plan Risk R7.
	//
	// API4 conversation history is owned by the caller's user ID.
	if req.SessionID == "" {
		// Seed a new session. An empty question is the opening handshake;
		// a supplied question continues through the normal generation path.
		// Mirrors python
		// async_iframe_completion (conversation_service.py:324-334):
		// the share page calls this endpoint once with an empty
		// question to obtain a session_id, then sends the real
		// questions with that session_id.
		prologue := stringFromMap(dialog.PromptConfig, "prologue")
		seedMsg, _ := json.Marshal([]map[string]any{
			{
				"role":       "assistant",
				"content":    prologue,
				"created_at": time.Now().Unix(),
			},
		})
		session := &entity.API4Conversation{
			ID:       common.GenerateUUID(),
			DialogID: dialogID,
			UserID:   userID,
			Message:  seedMsg,
		}
		if err = s.api4ConversationDAO.Create(ctx, dao.DB, session); err != nil {
			return nil, common.CodeServerError, err
		}

		req.SessionID = session.ID
		if req.Question == "" {
			// Mirror python async_iframe_completion
			// (conversation_service.py:324-334): a request without a
			// session_id is the share page's opening handshake — the
			// front-end sends an empty question only to obtain a session.
			// Persist the prologue-seeded session and stream the prologue
			// back WITHOUT invoking the pipeline; running the model here
			// would fabricate a reply to a message the user never sent.
			out := make(chan ChatbotSSEFrame, 2)
			go func() {
				defer close(out)
				out <- ChatbotSSEFrame{
					Data:      prologue,
					Reference: map[string]any{},
					SessionID: session.ID,
				}
				out <- ChatbotSSEFrame{Done: true}
			}()
			return out, common.CodeSuccess, nil
		}
	}

	session, err := s.api4ConversationDAO.GetBySessionID(ctx, dao.DB, req.SessionID, dialogID, true)
	if err != nil {
		return nil, common.CodeServerError, err
	}
	if session == nil || session.UserID != userID {
		return nil, common.CodeDataError, errors.New("session not found")
	}

	// 3. Guard rails mirroring the previous implementation: surface
	// a sanitised error before any SSE byte is written when the
	// service is unwired (test boot path) or the dialog has no LLM
	// configured — see WriteChatbotFrame's sanitization contract.
	// NewBotService wires the pipeline; the nil check only guards a
	// hand-rolled zero-value BotService against panicking.
	if s.pipeline == nil {
		return nil, common.CodeServerError, errors.New("bot: chat pipeline not wired")
	}
	if dialog.LLMID == "" {
		return nil, common.CodeDataError, errors.New("no LLM configured for this chatbot")
	}

	// 4. Build the pipeline input. The hydrated Message value is a JSON array of
	// {role, content, created_at} dicts; the pipeline expects the
	// same filtered shape python builds in async_iframe_completion
	// (drop system turns, drop the leading assistant prologue,
	// append the new user turn last).
	messageID := common.GenerateUUID()
	messages := buildChatbotPipelineMessages(session.Message, req.Question, messageID)

	// python bot_api.py:72-73 defaults quote to False for chatbot
	// completions when the caller omits it.
	kwargs := map[string]interface{}{
		"quote": req.Quote != nil && *req.Quote,
	}
	// Pass the raw reasoning level (0..4) straight through to the pipeline.
	// The previous normalizeBotBoolFlag coercion only accepted a bool or 0/1,
	// silently dropping medium/high/ultra agentic-RAG levels (2/3/4) so the
	// new-tab/embed chat fell back to plain RAG. resolveReasoningLevel
	// (chat_pipeline.go) reads this value first and tolerates float64/int/bool/
	// string/json.Number via asInt64, then falls back to the dialog's
	// prompt_config when the key is absent — so omitting it keeps the old
	// default behaviour.
	if req.Reasoning != nil {
		kwargs["reasoning"] = req.Reasoning
	}
	if req.Internet != nil {
		kwargs["internet"] = req.Internet
	}
	if req.DocIDs != "" {
		kwargs["doc_ids"] = req.DocIDs
	}

	if err := s.persistChatbotQuestion(ctx, session, req.Question, messageID, receivedAt); err != nil {
		return nil, common.CodeServerError, err
	}
	results, err := s.pipeline.AsyncChat(ctx, dialog, messages, true, kwargs)
	if err != nil {
		return nil, common.CodeServerError, err
	}

	// 5. Translate pipeline results into chatbot frames. The
	// pipeline streams deltas; the python iframe contract also yields
	// deltas (async_chat yields the value from _stream_with_think_delta).
	// We accumulate the full answer locally for persistence and for the
	// final frame, but forward each result's own delta in the SSE frame
	// so the front-end can build the <think>-wrapped answer correctly.
	// Sending accumulated full text on every frame interacts badly with
	// the front-end's start_to_think/end_to_think marker append, causing
	// reasoning content to leak into the visible answer.
	return s.streamChatbotTurn(ctx, session, req.Question, messageID, results), common.CodeSuccess, nil
}

// streamChatbotTurn translates pipeline results into python-shaped
// chatbot SSE frames and persists the finished turn. It mirrors
// conversation_service.py structure_answer:
//
//   - Every non-final frame forwards only its own delta with an empty
//     reference object; only the final frame carries the retrieval
//     reference.
//   - The final frame's `answer` is empty whenever text was already
//     streamed as deltas (python async_chat sets final["answer"] = ""),
//     so accumulating consumers do not double the text. The full text
//     is sent when nothing was streamed (single-shot results such as
//     the structured-SQL path) and on error finals, so a mid-stream
//     pipeline failure still reaches the wire after partial deltas.
//   - The persisted assistant turn is the raw streamed text, NOT the
//     decorated final answer. The decorated text carries server-side
//     [ID:n] citation markers; persisting it feeds fabricated markers
//     back into the next turn's prompt, and models that imitate the
//     history format then emit markers for turns whose retrieval
//     returned nothing — which the widget renders as a citation icon
//     with "Reference unavailable".
func (s *BotService) streamChatbotTurn(
	ctx context.Context,
	session *entity.API4Conversation,
	question, messageID string,
	results <-chan AsyncChatResult,
) <-chan ChatbotSSEFrame {
	out := make(chan ChatbotSSEFrame, 16)
	go func() {
		defer close(out)
		// rawAnswer is the accumulated wire text (deltas plus the
		// <think>/</think> tags the marker frames stand for), i.e. what
		// the client already rendered. fullAnswer additionally tracks
		// the decorated final answer for error detection and the
		// no-delta fallback.
		var rawAnswer, fullAnswer string
		var finalRef map[string]any

		sendFrame := func(frame ChatbotSSEFrame) bool {
			if ctx.Err() != nil {
				return false
			}
			select {
			case out <- frame:
				return true
			case <-ctx.Done():
				return false
			}
		}

		for res := range results {
			if res.Final {
				completedAt := float64(time.Now().UnixNano()) / 1e9
				if res.Answer != "" {
					// Decorated full answer (citations
					// resolved). Replaces the accumulated
					// deltas; the empty_response fallback
					// path yields an empty final answer, in
					// which case the accumulated fallback
					// text stands.
					fullAnswer = res.Answer
				}
				if res.Reference != nil {
					finalRef = res.Reference
				}
				errored := strings.HasPrefix(fullAnswer, "**ERROR**")
				// Save the completed answer before delivering the final SSE frame.
				if !errored {
					persisted := rawAnswer
					if persisted == "" {
						persisted = fullAnswer
					}
					saveCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
					if pErr := s.persistChatbotTurn(saveCtx, session, question, persisted, messageID, finalRef, completedAt); pErr != nil {
						common.Error("bot: ChatbotCompletion session update failed", pErr, zap.String("dialog_id", session.DialogID), zap.String("session_id", session.ID))
					}
					cancel()
				}
				finalData := ""
				if rawAnswer == "" || errored {
					// The final frame is the only carrier of the text
					// when nothing was streamed as deltas; on a
					// pipeline-level error it must still carry the
					// error text even after partial deltas, or the
					// client would see a silently truncated answer.
					finalData = fullAnswer
				}
				if !sendFrame(ChatbotSSEFrame{
					Data:      finalData,
					Reference: referenceOrEmpty(finalRef),
					SessionID: session.ID,
					Final:     true,
				}) {
					return
				}
				continue
			}
			if res.StartToThink || res.EndToThink {
				// Marker frames carry no text; the front-end appends
				// <think> / </think> to the accumulated answer.
				if res.StartToThink {
					rawAnswer += "<think>"
				} else {
					rawAnswer += "</think>"
				}
				if !sendFrame(ChatbotSSEFrame{
					Data:         "",
					Reference:    map[string]any{},
					SessionID:    session.ID,
					StartToThink: res.StartToThink,
					EndToThink:   res.EndToThink,
				}) {
					return
				}
				continue
			}
			// Reasoning text arrives through two delivery modes: the
			// plain streaming path emits it as Answer deltas between
			// the StartToThink/EndToThink markers (chat_pipeline.go
			// think-state machine), while the tool path
			// (chat_pipeline.go ChatStreamlyWithTools callback) routes
			// in-think text through the Reasoning field so the
			// OpenAI-compat SSE handler can map it to
			// delta.reasoning_content. Python delivers reasoning as
			// <think>-wrapped answer stream text in both modes
			// (rag/llm/chat_model.py), so forward it as stream text
			// here; dropping it would leave the widget's think block
			// empty and persist history without the reasoning.
			delta := res.Answer
			if delta == "" {
				delta = res.Reasoning
			}
			rawAnswer += delta
			fullAnswer += delta
			if len(res.Reference) > 0 {
				// The pipeline only populates Reference on the final
				// result today; tracking it here keeps finalRef
				// correct if a mid-stream result ever carries one.
				// Intermediate frames still send an empty reference
				// object for wire parity with python
				// async_iframe_completion — only the final frame
				// carries the retrieval reference.
				finalRef = res.Reference
			}
			if !sendFrame(ChatbotSSEFrame{
				Data:      delta,
				Reference: map[string]any{},
				SessionID: session.ID,
			}) {
				return
			}
		}

		sendFrame(ChatbotSSEFrame{Done: true})
	}()
	return out
}

// buildChatbotPipelineMessages projects the session.Message JSON
// array plus the new user question onto the message shape
// ChatPipelineService.AsyncChat expects. Mirrors the filtering in
// python async_iframe_completion (conversation_service.py:341-356):
// system turns are dropped and a leading assistant turn (the seeded
// prologue) is dropped so the first message the LLM sees is a user
// turn. Tolerates an empty / malformed Message column by starting
// from just the new question.
func buildChatbotPipelineMessages(raw json.RawMessage, question, messageID string) []map[string]interface{} {
	turns := parseChatbotTurns(raw)
	turns = append(turns, map[string]any{
		"role":    "user",
		"content": question,
		"id":      messageID,
	})
	msg := make([]map[string]interface{}, 0, len(turns))
	for _, m := range turns {
		role, _ := m["role"].(string)
		if role == "system" {
			continue
		}
		if role == "assistant" && len(msg) == 0 {
			continue
		}
		message := make(map[string]interface{}, len(m))
		for key, value := range m {
			if key == "prompt" || key == "usage" || key == "elapsed_ms" || key == "reference" {
				continue
			}
			message[key] = value
		}
		msg = append(msg, message)
	}
	return msg
}

// parseChatbotTurns decodes the session.Message JSON array.
// Returns an empty (non-nil) slice on empty or malformed input so
// callers can always append.
func parseChatbotTurns(raw json.RawMessage) []map[string]any {
	turns := make([]map[string]any, 0)
	if len(raw) == 0 {
		return turns
	}
	if err := json.Unmarshal(raw, &turns); err != nil || turns == nil {
		return make([]map[string]any, 0)
	}
	return turns
}

func (s *BotService) persistChatbotQuestion(ctx context.Context, session *entity.API4Conversation, question, messageID string, receivedAt float64) error {
	message := map[string]interface{}{"role": "user", "content": question, "id": messageID, "created_at": receivedAt}
	return s.api4ConversationDAO.UpdateHistory(ctx, dao.DB, session.ID, session.DialogID, session.UserID, nil, dao.ConversationHistoryUpdate{Message: message})
}

// persistChatbotTurn completes an already-persisted user turn with its
// assistant message and immutable retrieval reference.
func (s *BotService) persistChatbotTurn(
	ctx context.Context, session *entity.API4Conversation, question, answer, messageID string, reference map[string]any,
	completedAt float64,
) error {
	if reference == nil {
		reference = map[string]any{"chunks": []any{}, "doc_aggs": []any{}}
	}
	message := map[string]interface{}{"role": "assistant", "content": answer, "id": messageID, "created_at": completedAt}
	message["reference"] = reference
	return s.api4ConversationDAO.UpdateHistory(ctx, dao.DB, session.ID, session.DialogID, session.UserID, nil, dao.ConversationHistoryUpdate{Message: message, QuestionID: messageID})
}

// normalizeBotBoolFlag coerces the JSON-encoded reasoning / internet
// flags to a bool. Widgets send them as true/false or 0/1 numbers;
// ok=false means the value was absent or unrecognised and the caller
// should leave the pipeline default in place.
func normalizeBotBoolFlag(v any) (value, ok bool) {
	switch x := v.(type) {
	case bool:
		return x, true
	case float64:
		if x == 0 || x == 1 {
			return x == 1, true
		}
	case int:
		if x == 0 || x == 1 {
			return x == 1, true
		}
	}
	return false, false
}

// referenceOrEmpty returns ref or an empty map so SSE frames always
// carry a JSON object in the reference field, never null.
func referenceOrEmpty(ref map[string]any) map[string]any {
	if ref == nil {
		return map[string]any{}
	}
	return ref
}