From b792f44556f5015f67acbb8997b71de565eb68f4 Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 26 Oct 2026 09:47:05 +0800 Subject: [PATCH] feat(web): auto sse-live when chat body has stream:true Upgrade json invoke routes to the M02 EventBus forwarder for OpenAI-compatible stream requests, and normalize LLM delta frames to message.delta.payload.delta so tokens flush before run.completed. Co-authored-by: Cursor --- plugins/web/internal/httpx/helpers.go | 29 ++++++++++ plugins/web/internal/httpx/routes_invoke.go | 59 ++++++++++++++++----- 2 files changed, 74 insertions(+), 14 deletions(-) diff --git a/plugins/web/internal/httpx/helpers.go b/plugins/web/internal/httpx/helpers.go index b6c25e0..62b4a30 100644 --- a/plugins/web/internal/httpx/helpers.go +++ b/plugins/web/internal/httpx/helpers.go @@ -198,6 +198,35 @@ func invokeArgsFromPayload(payload map[string]any) map[string]any { return args } +// payloadWantsStream detects OpenAI-compatible stream:true on the request body. +func payloadWantsStream(payload map[string]any) bool { + if payload == nil { + return false + } + if v, ok := payload["stream"]; ok { + return boolishAny(v) + } + return false +} + +func boolishAny(v any) bool { + switch t := v.(type) { + case bool: + return t + case string: + s := strings.ToLower(strings.TrimSpace(t)) + return s == "1" || s == "true" || s == "yes" || s == "on" + case float64: + return t != 0 + case int: + return t != 0 + case int64: + return t != 0 + default: + return false + } +} + func readJSONBody(r *http.Request, max int64) (map[string]any, error) { r.Body = http.MaxBytesReader(nil, r.Body, max) defer r.Body.Close() diff --git a/plugins/web/internal/httpx/routes_invoke.go b/plugins/web/internal/httpx/routes_invoke.go index fb2ca9f..155d341 100644 --- a/plugins/web/internal/httpx/routes_invoke.go +++ b/plugins/web/internal/httpx/routes_invoke.go @@ -122,7 +122,12 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo // M02: live SSE — stream host_stream_publish events while CallLibPath runs. // return=sse-live|sse_live uses MARQDO_EVENT_FORWARD NDJSON bridge (request-local). + // Also auto-upgrade json routes when the OpenAI-compatible body asks stream:true + // so token deltas flush before the lib call returns (Q06 / workbench). liveSSE := route.ReturnMode == "sse-live" || route.ReturnMode == "sse_live" + if !liveSSE && payloadWantsStream(payload) { + liveSSE = true + } if liveSSE { st.handleInvokeLiveSSE(w, r, route, args) return @@ -283,13 +288,7 @@ func (st *state) handleInvokeLiveSSE(w http.ResponseWriter, r *http.Request, rou mu.Lock() offset = drainForward(fwdPath, offset, func(line string) { seq++ - var obj any - if json.Unmarshal([]byte(line), &obj) == nil { - writeEvent(map[string]any{ - "schema": "RuntimeEventV1", "schema_version": 1, - "seq": seq, "type": "message.delta", "payload": obj, - }) - } + writeEvent(normalizeLiveSSELine(seq, line)) }) mu.Unlock() if res.err != nil { @@ -312,19 +311,51 @@ func (st *state) handleInvokeLiveSSE(w http.ResponseWriter, r *http.Request, rou mu.Lock() offset = drainForward(fwdPath, offset, func(line string) { seq++ - var obj any - if json.Unmarshal([]byte(line), &obj) == nil { - writeEvent(map[string]any{ - "schema": "RuntimeEventV1", "schema_version": 1, - "seq": seq, "type": "message.delta", "payload": obj, - }) - } + writeEvent(normalizeLiveSSELine(seq, line)) }) mu.Unlock() } } } +// normalizeLiveSSELine maps Marqdo EventBus frames (LLM delta/text, workbench +// envelopes) onto RuntimeEventV1 so chat UIs see message.delta.payload.delta +// while tokens still arrive before run.completed. +func normalizeLiveSSELine(seq uint64, line string) map[string]any { + base := map[string]any{ + "schema": "RuntimeEventV1", "schema_version": 1, "seq": seq, + } + var obj map[string]any + if err := json.Unmarshal([]byte(line), &obj); err != nil || obj == nil { + base["type"] = "message.delta" + base["payload"] = map[string]any{"raw": line} + return base + } + // Already workbench-shaped. + if t, _ := obj["type"].(string); t == "message.delta" || t == "turn.started" || t == "model.resolved" || t == "usage.updated" || t == "turn.completed" || t == "turn.failed" { + for k, v := range obj { + base[k] = v + } + base["seq"] = seq + return base + } + // LLM plugin / net SSE: {type:delta|reasoning, text:...} + if t, _ := obj["type"].(string); t == "delta" || t == "reasoning" { + text, _ := obj["text"].(string) + base["type"] = "message.delta" + base["payload"] = map[string]any{"delta": text, "kind": t} + return base + } + if text, ok := obj["text"].(string); ok && text != "" { + base["type"] = "message.delta" + base["payload"] = map[string]any{"delta": text} + return base + } + base["type"] = "message.delta" + base["payload"] = obj + return base +} + func drainForward(path string, offset int64, onLine func(string)) int64 { f, err := os.Open(path) if err != nil {