refactor(open-sse): DRY SSE primitives into utils/sseConstants.js

Gom SSE_DONE + SSE_HEADERS + SSE_HEADERS_NO_BUFFER vào 1 file không
phụ thuộc (tránh kéo usageDb vào executor). Áp cho kiro, cursor, qoder,
github, commandcode, perplexity-web, grok-web. Byte-for-byte verified
(headers/DONE giữ nguyên; biến thể no-buffer dùng const riêng).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decolua
2026-06-13 21:42:59 +07:00
parent 0a8d92a6ba
commit 6ee4555821
9 changed files with 42 additions and 25 deletions

View File

@@ -1,5 +1,6 @@
import { BaseExecutor } from "./base.js";
import { PROVIDERS } from "../config/providers.js";
import { SSE_DONE, SSE_HEADERS_NO_BUFFER } from "../utils/sseConstants.js";
const GROK_CHAT_API = PROVIDERS["grok-web"].baseUrl;
const GROK_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36";
@@ -175,13 +176,13 @@ function buildStreamingResponse(eventStream, model, cid, created, isThinkingMode
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: fp || null,
choices: [{ index: 0, delta: {}, finish_reason: "stop", logprobs: null }],
})));
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
controller.enqueue(encoder.encode(SSE_DONE));
} catch (err) {
controller.enqueue(encoder.encode(sseChunk({
id: cid, object: "chat.completion.chunk", created, model, system_fingerprint: null,
choices: [{ index: 0, delta: { content: `[Stream error: ${err.message || String(err)}]` }, finish_reason: "stop", logprobs: null }],
})));
controller.enqueue(encoder.encode("data: [DONE]\n\n"));
controller.enqueue(encoder.encode(SSE_DONE));
} finally {
controller.close();
}
@@ -333,7 +334,7 @@ export class GrokWebExecutor extends BaseExecutor {
const sseStream = buildStreamingResponse(response.body, model, cid, created, isThinking, signal);
finalResponse = new Response(sseStream, {
status: 200,
headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "X-Accel-Buffering": "no" },
headers: { ...SSE_HEADERS_NO_BUFFER },
});
} else {
finalResponse = await buildNonStreamingResponse(response.body, model, cid, created, isThinking, signal);