Skip to content

Commit 67aa9ce

Browse files
fix(opencode): support Luna Responses Lite over OAuth (#36685)
Co-authored-by: AidenGeunGeun <[email protected]>
1 parent 7ec3d67 commit 67aa9ce

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎packages/opencode/src/plugin/openai/codex.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import { setTimeout as sleep } from "node:timers/promises"
66
import { createServer } from "http"
77
import { OpenAIWebSocketPool } from "./ws-pool"
88
import { OauthCallbackPage } from "@opencode-ai/core/oauth/page"
9+
import { isRecord } from "@/util/record"
910

1011
const CLIENT_ID = "app_EMoamEEZ73f0CkXaXp7hrann"
1112
const ISSUER = "https://auth.openai.com"
1213
const CODEX_API_ENDPOINT = "https://chatgpt.com/backend-api/codex/responses"
1314
const OAUTH_PORT = 1455
1415
const OAUTH_POLLING_SAFETY_MARGIN_MS = 3000
16+
const CODEX_COMPATIBILITY_VERSION = "0.144.0"
17+
const RESPONSES_LITE_MODEL = "gpt-5.6-luna"
1518
const ALLOWED_MODELS = new Set(["gpt-5.5", "gpt-5.3-codex-spark", "gpt-5.4", "gpt-5.4-mini"])
1619
const DISALLOWED_MODELS = new Set(["gpt-5.5-pro"])
1720

@@ -417,6 +420,9 @@ export async function CodexAuthPlugin(input: PluginInput, options: CodexAuthPlug
417420

418421
const requestInit = {
419422
...init,
423+
body: parsed.pathname.endsWith("/responses")
424+
? prepareResponsesLiteRequest(init?.body, headers)
425+
: init?.body,
420426
headers,
421427
}
422428
if (websocketFetch && parsed.pathname.endsWith("/responses")) return websocketFetch(url, requestInit)
@@ -561,3 +567,61 @@ export async function CodexAuthPlugin(input: PluginInput, options: CodexAuthPlug
561567
},
562568
}
563569
}
570+
571+
function prepareResponsesLiteRequest(body: BodyInit | null | undefined, headers: Headers) {
572+
if (typeof body !== "string") return body
573+
const request: unknown = JSON.parse(body)
574+
if (!isRecord(request)) return body
575+
if (request.model !== RESPONSES_LITE_MODEL) return body
576+
if (!Array.isArray(request.input)) throw new Error("Responses Lite requires an input array")
577+
if (request.tools !== undefined && !Array.isArray(request.tools)) {
578+
throw new Error("Responses Lite requires a tools array")
579+
}
580+
if (request.instructions !== undefined && typeof request.instructions !== "string") {
581+
throw new Error("Responses Lite requires string instructions")
582+
}
583+
584+
const sessionID = headers.get("session-id")
585+
if (!sessionID) throw new Error("Responses Lite requires a session-id header")
586+
587+
function stripImageDetail(value: unknown) {
588+
if (Array.isArray(value)) {
589+
value.forEach(stripImageDetail)
590+
return
591+
}
592+
if (!isRecord(value)) return
593+
if (value.type === "input_image") delete value.detail
594+
Object.values(value).forEach(stripImageDetail)
595+
}
596+
597+
stripImageDetail(request.input)
598+
request.input = [
599+
{ type: "additional_tools", role: "developer", tools: request.tools ?? [] },
600+
...(request.instructions
601+
? [
602+
{
603+
type: "message",
604+
role: "developer",
605+
content: [{ type: "input_text", text: request.instructions }],
606+
},
607+
]
608+
: []),
609+
...request.input,
610+
]
611+
delete request.tools
612+
delete request.instructions
613+
request.tool_choice = "auto"
614+
request.parallel_tool_calls = false
615+
request.prompt_cache_key = sessionID
616+
request.reasoning = {
617+
...(isRecord(request.reasoning) ? request.reasoning : {}),
618+
context: "all_turns",
619+
}
620+
621+
headers.set("session-id", sessionID)
622+
headers.set("x-session-affinity", sessionID)
623+
headers.set("version", CODEX_COMPATIBILITY_VERSION)
624+
headers.set(OpenAIWebSocketPool.RESPONSES_LITE_HEADER, "true")
625+
headers.delete("content-length")
626+
return JSON.stringify(request)
627+
}

‎packages/opencode/src/plugin/openai/ws-pool.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { isRecord } from "@/util/record"
44
import { OpenAIWebSocket } from "./ws"
55

66
export const TITLE_HEADER = "x-opencode-title"
7+
export const RESPONSES_LITE_HEADER = "x-openai-internal-codex-responses-lite"
8+
const RESPONSES_LITE_CLIENT_METADATA = "ws_request_header_x_openai_internal_codex_responses_lite"
79

810
export interface CreateWebSocketFetchOptions {
911
httpFetch?: typeof globalThis.fetch
@@ -98,7 +100,16 @@ export function createWebSocketFetch(options?: CreateWebSocketFetchOptions) {
98100
})
99101
const response = OpenAIWebSocket.streamResponsesWebSocket({
100102
socket: entry.socket,
101-
body,
103+
body:
104+
internalHeaders[RESPONSES_LITE_HEADER] === "true"
105+
? {
106+
...body,
107+
client_metadata: {
108+
...(isRecord(body.client_metadata) ? body.client_metadata : {}),
109+
[RESPONSES_LITE_CLIENT_METADATA]: "true",
110+
},
111+
}
112+
: body,
102113
idleTimeout,
103114
signal: init?.signal ?? undefined,
104115
onFirstEvent: (error) => resolveFirstEvent(error ?? true),

0 commit comments

Comments
 (0)