From 4bff21cb80305cabd526f34779eae1313eccbc14 Mon Sep 17 00:00:00 2001 From: decolua Date: Tue, 14 Apr 2026 10:18:27 +0700 Subject: [PATCH] Enhance CodexExecutor with compact URL support --- open-sse/executors/codex.js | 7 +++++ src/app/api/v1/responses/compact/route.js | 37 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/app/api/v1/responses/compact/route.js diff --git a/open-sse/executors/codex.js b/open-sse/executors/codex.js index 20e6c8b4..b8842474 100644 --- a/open-sse/executors/codex.js +++ b/open-sse/executors/codex.js @@ -87,10 +87,17 @@ export class CodexExecutor extends BaseExecutor { return headers; } + buildUrl(model, stream, urlIndex = 0, credentials = null) { + const base = super.buildUrl(model, stream, urlIndex, credentials); + return this._isCompact ? `${base}/compact` : base; + } + /** * Transform request before sending - inject default instructions if missing */ transformRequest(model, body, stream, credentials) { + this._isCompact = !!body._compact; + delete body._compact; // Resolve conversation-stable session_id from input history + machineId this._currentSessionId = resolveConversationSessionId(body.input, cachedMachineId); // Convert string input to array format (Codex API requires input as array) diff --git a/src/app/api/v1/responses/compact/route.js b/src/app/api/v1/responses/compact/route.js new file mode 100644 index 00000000..7016c071 --- /dev/null +++ b/src/app/api/v1/responses/compact/route.js @@ -0,0 +1,37 @@ +import { handleChat } from "@/sse/handlers/chat.js"; +import { initTranslators } from "open-sse/translator/index.js"; + +let initialized = false; + +async function ensureInitialized() { + if (!initialized) { + await initTranslators(); + initialized = true; + } +} + +export async function OPTIONS() { + return new Response(null, { + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET, POST, OPTIONS", + "Access-Control-Allow-Headers": "*" + } + }); +} + +/** + * POST /v1/responses/compact - Compact conversation context + * Reuses the same handleChat pipeline, signals compact via body._compact + */ +export async function POST(request) { + await ensureInitialized(); + const body = await request.json(); + body._compact = true; + const newRequest = new Request(request.url, { + method: "POST", + headers: request.headers, + body: JSON.stringify(body) + }); + return await handleChat(newRequest); +}