Spaces:
Paused
fix: remove unsupported previous_response_id parameter (#30)
Browse files* fix: remove unsupported previous_response_id parameter (#29)
The Codex backend no longer accepts `previous_response_id`.
Sending it causes HTTP 400: "Unsupported parameter: previous_response_id".
Remove the injection logic in proxy-handler and clean up related
dead code across translation functions and session manager.
Closes #29
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor: remove dead SessionManager (was only used for previous_response_id)
SessionManager, session hashing, and onResponseId session storage
were only used to track previous_response_id across turns. Since the
backend no longer supports this parameter, the entire session system
is dead code. Remove it.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
---------
Co-authored-by: icebear0828 <icebear0828@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
- src/index.ts +4 -6
- src/proxy/codex-api.ts +0 -2
- src/routes/chat.ts +0 -11
- src/routes/gemini.ts +0 -11
- src/routes/messages.ts +0 -33
- src/routes/shared/proxy-handler.ts +1 -34
- src/session/manager.ts +0 -121
- src/translation/anthropic-to-codex.ts +0 -6
- src/translation/gemini-to-codex.ts +0 -6
- src/translation/openai-to-codex.ts +0 -6
|
@@ -3,7 +3,7 @@ import { serve } from "@hono/node-server";
|
|
| 3 |
import { loadConfig, loadFingerprint, getConfig } from "./config.js";
|
| 4 |
import { AccountPool } from "./auth/account-pool.js";
|
| 5 |
import { RefreshScheduler } from "./auth/refresh-scheduler.js";
|
| 6 |
-
|
| 7 |
import { requestId } from "./middleware/request-id.js";
|
| 8 |
import { logger } from "./middleware/logger.js";
|
| 9 |
import { errorHandler } from "./middleware/error-handler.js";
|
|
@@ -53,7 +53,6 @@ export async function startServer(options?: StartOptions): Promise<ServerHandle>
|
|
| 53 |
// Initialize managers
|
| 54 |
const accountPool = new AccountPool();
|
| 55 |
const refreshScheduler = new RefreshScheduler(accountPool);
|
| 56 |
-
const sessionManager = new SessionManager();
|
| 57 |
const cookieJar = new CookieJar();
|
| 58 |
|
| 59 |
// Create Hono app
|
|
@@ -67,9 +66,9 @@ export async function startServer(options?: StartOptions): Promise<ServerHandle>
|
|
| 67 |
// Mount routes
|
| 68 |
const authRoutes = createAuthRoutes(accountPool, refreshScheduler);
|
| 69 |
const accountRoutes = createAccountRoutes(accountPool, refreshScheduler, cookieJar);
|
| 70 |
-
const chatRoutes = createChatRoutes(accountPool,
|
| 71 |
-
const messagesRoutes = createMessagesRoutes(accountPool,
|
| 72 |
-
const geminiRoutes = createGeminiRoutes(accountPool,
|
| 73 |
const webRoutes = createWebRoutes(accountPool);
|
| 74 |
|
| 75 |
app.route("/", authRoutes);
|
|
@@ -126,7 +125,6 @@ export async function startServer(options?: StartOptions): Promise<ServerHandle>
|
|
| 126 |
stopUpdateChecker();
|
| 127 |
stopModelRefresh();
|
| 128 |
refreshScheduler.destroy();
|
| 129 |
-
sessionManager.destroy();
|
| 130 |
cookieJar.destroy();
|
| 131 |
accountPool.destroy();
|
| 132 |
resolve();
|
|
|
|
| 3 |
import { loadConfig, loadFingerprint, getConfig } from "./config.js";
|
| 4 |
import { AccountPool } from "./auth/account-pool.js";
|
| 5 |
import { RefreshScheduler } from "./auth/refresh-scheduler.js";
|
| 6 |
+
|
| 7 |
import { requestId } from "./middleware/request-id.js";
|
| 8 |
import { logger } from "./middleware/logger.js";
|
| 9 |
import { errorHandler } from "./middleware/error-handler.js";
|
|
|
|
| 53 |
// Initialize managers
|
| 54 |
const accountPool = new AccountPool();
|
| 55 |
const refreshScheduler = new RefreshScheduler(accountPool);
|
|
|
|
| 56 |
const cookieJar = new CookieJar();
|
| 57 |
|
| 58 |
// Create Hono app
|
|
|
|
| 66 |
// Mount routes
|
| 67 |
const authRoutes = createAuthRoutes(accountPool, refreshScheduler);
|
| 68 |
const accountRoutes = createAccountRoutes(accountPool, refreshScheduler, cookieJar);
|
| 69 |
+
const chatRoutes = createChatRoutes(accountPool, cookieJar);
|
| 70 |
+
const messagesRoutes = createMessagesRoutes(accountPool, cookieJar);
|
| 71 |
+
const geminiRoutes = createGeminiRoutes(accountPool, cookieJar);
|
| 72 |
const webRoutes = createWebRoutes(accountPool);
|
| 73 |
|
| 74 |
app.route("/", authRoutes);
|
|
|
|
| 125 |
stopUpdateChecker();
|
| 126 |
stopModelRefresh();
|
| 127 |
refreshScheduler.destroy();
|
|
|
|
| 128 |
cookieJar.destroy();
|
| 129 |
accountPool.destroy();
|
| 130 |
resolve();
|
|
@@ -32,8 +32,6 @@ export interface CodexResponsesRequest {
|
|
| 32 |
tools?: unknown[];
|
| 33 |
/** Optional: tool choice strategy */
|
| 34 |
tool_choice?: string | { type: string; name: string };
|
| 35 |
-
/** Optional: previous response ID for multi-turn */
|
| 36 |
-
previous_response_id?: string | null;
|
| 37 |
}
|
| 38 |
|
| 39 |
/** Structured content part for multimodal Codex input. */
|
|
|
|
| 32 |
tools?: unknown[];
|
| 33 |
/** Optional: tool choice strategy */
|
| 34 |
tool_choice?: string | { type: string; name: string };
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
/** Structured content part for multimodal Codex input. */
|
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import { Hono } from "hono";
|
| 2 |
import { ChatCompletionRequestSchema } from "../types/openai.js";
|
| 3 |
import type { AccountPool } from "../auth/account-pool.js";
|
| 4 |
-
import type { SessionManager } from "../session/manager.js";
|
| 5 |
import type { CookieJar } from "../proxy/cookie-jar.js";
|
| 6 |
import { translateToCodexRequest } from "../translation/openai-to-codex.js";
|
| 7 |
import {
|
|
@@ -52,7 +51,6 @@ function makeOpenAIFormat(wantReasoning: boolean): FormatAdapter {
|
|
| 52 |
|
| 53 |
export function createChatRoutes(
|
| 54 |
accountPool: AccountPool,
|
| 55 |
-
sessionManager: SessionManager,
|
| 56 |
cookieJar?: CookieJar,
|
| 57 |
): Hono {
|
| 58 |
const app = new Hono();
|
|
@@ -127,18 +125,9 @@ export function createChatRoutes(
|
|
| 127 |
return handleProxyRequest(
|
| 128 |
c,
|
| 129 |
accountPool,
|
| 130 |
-
sessionManager,
|
| 131 |
cookieJar,
|
| 132 |
{
|
| 133 |
codexRequest,
|
| 134 |
-
sessionMessages: req.messages.map((m) => ({
|
| 135 |
-
role: m.role,
|
| 136 |
-
content: typeof m.content === "string"
|
| 137 |
-
? m.content
|
| 138 |
-
: m.content == null
|
| 139 |
-
? ""
|
| 140 |
-
: m.content.filter((p) => p.type === "text" && p.text).map((p) => p.text!).join("\n"),
|
| 141 |
-
})),
|
| 142 |
model: codexRequest.model,
|
| 143 |
isStreaming: req.stream,
|
| 144 |
},
|
|
|
|
| 1 |
import { Hono } from "hono";
|
| 2 |
import { ChatCompletionRequestSchema } from "../types/openai.js";
|
| 3 |
import type { AccountPool } from "../auth/account-pool.js";
|
|
|
|
| 4 |
import type { CookieJar } from "../proxy/cookie-jar.js";
|
| 5 |
import { translateToCodexRequest } from "../translation/openai-to-codex.js";
|
| 6 |
import {
|
|
|
|
| 51 |
|
| 52 |
export function createChatRoutes(
|
| 53 |
accountPool: AccountPool,
|
|
|
|
| 54 |
cookieJar?: CookieJar,
|
| 55 |
): Hono {
|
| 56 |
const app = new Hono();
|
|
|
|
| 125 |
return handleProxyRequest(
|
| 126 |
c,
|
| 127 |
accountPool,
|
|
|
|
| 128 |
cookieJar,
|
| 129 |
{
|
| 130 |
codexRequest,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
model: codexRequest.model,
|
| 132 |
isStreaming: req.stream,
|
| 133 |
},
|
|
@@ -10,11 +10,9 @@ import type { GeminiErrorResponse } from "../types/gemini.js";
|
|
| 10 |
import { GEMINI_STATUS_MAP } from "../types/gemini.js";
|
| 11 |
import { GeminiGenerateContentRequestSchema } from "../types/gemini.js";
|
| 12 |
import type { AccountPool } from "../auth/account-pool.js";
|
| 13 |
-
import type { SessionManager } from "../session/manager.js";
|
| 14 |
import type { CookieJar } from "../proxy/cookie-jar.js";
|
| 15 |
import {
|
| 16 |
translateGeminiToCodexRequest,
|
| 17 |
-
geminiContentsToMessages,
|
| 18 |
} from "../translation/gemini-to-codex.js";
|
| 19 |
import {
|
| 20 |
streamCodexToGemini,
|
|
@@ -74,7 +72,6 @@ const GEMINI_FORMAT: FormatAdapter = {
|
|
| 74 |
|
| 75 |
export function createGeminiRoutes(
|
| 76 |
accountPool: AccountPool,
|
| 77 |
-
sessionManager: SessionManager,
|
| 78 |
cookieJar?: CookieJar,
|
| 79 |
): Hono {
|
| 80 |
const app = new Hono();
|
|
@@ -143,12 +140,6 @@ export function createGeminiRoutes(
|
|
| 143 |
}
|
| 144 |
const req = validationResult.data;
|
| 145 |
|
| 146 |
-
// Session lookup for multi-turn
|
| 147 |
-
const sessionMessages = geminiContentsToMessages(
|
| 148 |
-
req.contents,
|
| 149 |
-
req.systemInstruction,
|
| 150 |
-
);
|
| 151 |
-
|
| 152 |
const codexRequest = translateGeminiToCodexRequest(
|
| 153 |
req,
|
| 154 |
geminiModel,
|
|
@@ -161,11 +152,9 @@ export function createGeminiRoutes(
|
|
| 161 |
return handleProxyRequest(
|
| 162 |
c,
|
| 163 |
accountPool,
|
| 164 |
-
sessionManager,
|
| 165 |
cookieJar,
|
| 166 |
{
|
| 167 |
codexRequest,
|
| 168 |
-
sessionMessages,
|
| 169 |
model: geminiModel,
|
| 170 |
isStreaming,
|
| 171 |
},
|
|
|
|
| 10 |
import { GEMINI_STATUS_MAP } from "../types/gemini.js";
|
| 11 |
import { GeminiGenerateContentRequestSchema } from "../types/gemini.js";
|
| 12 |
import type { AccountPool } from "../auth/account-pool.js";
|
|
|
|
| 13 |
import type { CookieJar } from "../proxy/cookie-jar.js";
|
| 14 |
import {
|
| 15 |
translateGeminiToCodexRequest,
|
|
|
|
| 16 |
} from "../translation/gemini-to-codex.js";
|
| 17 |
import {
|
| 18 |
streamCodexToGemini,
|
|
|
|
| 72 |
|
| 73 |
export function createGeminiRoutes(
|
| 74 |
accountPool: AccountPool,
|
|
|
|
| 75 |
cookieJar?: CookieJar,
|
| 76 |
): Hono {
|
| 77 |
const app = new Hono();
|
|
|
|
| 140 |
}
|
| 141 |
const req = validationResult.data;
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
const codexRequest = translateGeminiToCodexRequest(
|
| 144 |
req,
|
| 145 |
geminiModel,
|
|
|
|
| 152 |
return handleProxyRequest(
|
| 153 |
c,
|
| 154 |
accountPool,
|
|
|
|
| 155 |
cookieJar,
|
| 156 |
{
|
| 157 |
codexRequest,
|
|
|
|
| 158 |
model: geminiModel,
|
| 159 |
isStreaming,
|
| 160 |
},
|
|
@@ -8,7 +8,6 @@ import type { StatusCode } from "hono/utils/http-status";
|
|
| 8 |
import { AnthropicMessagesRequestSchema } from "../types/anthropic.js";
|
| 9 |
import type { AnthropicErrorBody, AnthropicErrorType } from "../types/anthropic.js";
|
| 10 |
import type { AccountPool } from "../auth/account-pool.js";
|
| 11 |
-
import type { SessionManager } from "../session/manager.js";
|
| 12 |
import type { CookieJar } from "../proxy/cookie-jar.js";
|
| 13 |
import { translateAnthropicToCodexRequest } from "../translation/anthropic-to-codex.js";
|
| 14 |
import {
|
|
@@ -28,19 +27,6 @@ function makeError(
|
|
| 28 |
return { type: "error", error: { type, message } };
|
| 29 |
}
|
| 30 |
|
| 31 |
-
/**
|
| 32 |
-
* Extract text from Anthropic message content for session hashing.
|
| 33 |
-
*/
|
| 34 |
-
function contentToString(
|
| 35 |
-
content: string | Array<{ type: string; text?: string }>,
|
| 36 |
-
): string {
|
| 37 |
-
if (typeof content === "string") return content;
|
| 38 |
-
return content
|
| 39 |
-
.filter((b) => b.type === "text" && b.text)
|
| 40 |
-
.map((b) => b.text!)
|
| 41 |
-
.join("\n");
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
function makeAnthropicFormat(wantThinking: boolean): FormatAdapter {
|
| 45 |
return {
|
| 46 |
tag: "Messages",
|
|
@@ -61,7 +47,6 @@ function makeAnthropicFormat(wantThinking: boolean): FormatAdapter {
|
|
| 61 |
|
| 62 |
export function createMessagesRoutes(
|
| 63 |
accountPool: AccountPool,
|
| 64 |
-
sessionManager: SessionManager,
|
| 65 |
cookieJar?: CookieJar,
|
| 66 |
): Hono {
|
| 67 |
const app = new Hono();
|
|
@@ -108,33 +93,15 @@ export function createMessagesRoutes(
|
|
| 108 |
}
|
| 109 |
const req = parsed.data;
|
| 110 |
|
| 111 |
-
// Build session-compatible messages for multi-turn lookup
|
| 112 |
-
const sessionMessages: Array<{ role: string; content: string }> = [];
|
| 113 |
-
if (req.system) {
|
| 114 |
-
const sysText =
|
| 115 |
-
typeof req.system === "string"
|
| 116 |
-
? req.system
|
| 117 |
-
: req.system.map((b) => b.text).join("\n");
|
| 118 |
-
sessionMessages.push({ role: "system", content: sysText });
|
| 119 |
-
}
|
| 120 |
-
for (const msg of req.messages) {
|
| 121 |
-
sessionMessages.push({
|
| 122 |
-
role: msg.role,
|
| 123 |
-
content: contentToString(msg.content),
|
| 124 |
-
});
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
const codexRequest = translateAnthropicToCodexRequest(req);
|
| 128 |
const wantThinking = req.thinking?.type === "enabled" || req.thinking?.type === "adaptive";
|
| 129 |
|
| 130 |
return handleProxyRequest(
|
| 131 |
c,
|
| 132 |
accountPool,
|
| 133 |
-
sessionManager,
|
| 134 |
cookieJar,
|
| 135 |
{
|
| 136 |
codexRequest,
|
| 137 |
-
sessionMessages,
|
| 138 |
model: req.model,
|
| 139 |
isStreaming: req.stream,
|
| 140 |
},
|
|
|
|
| 8 |
import { AnthropicMessagesRequestSchema } from "../types/anthropic.js";
|
| 9 |
import type { AnthropicErrorBody, AnthropicErrorType } from "../types/anthropic.js";
|
| 10 |
import type { AccountPool } from "../auth/account-pool.js";
|
|
|
|
| 11 |
import type { CookieJar } from "../proxy/cookie-jar.js";
|
| 12 |
import { translateAnthropicToCodexRequest } from "../translation/anthropic-to-codex.js";
|
| 13 |
import {
|
|
|
|
| 27 |
return { type: "error", error: { type, message } };
|
| 28 |
}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
function makeAnthropicFormat(wantThinking: boolean): FormatAdapter {
|
| 31 |
return {
|
| 32 |
tag: "Messages",
|
|
|
|
| 47 |
|
| 48 |
export function createMessagesRoutes(
|
| 49 |
accountPool: AccountPool,
|
|
|
|
| 50 |
cookieJar?: CookieJar,
|
| 51 |
): Hono {
|
| 52 |
const app = new Hono();
|
|
|
|
| 93 |
}
|
| 94 |
const req = parsed.data;
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
const codexRequest = translateAnthropicToCodexRequest(req);
|
| 97 |
const wantThinking = req.thinking?.type === "enabled" || req.thinking?.type === "adaptive";
|
| 98 |
|
| 99 |
return handleProxyRequest(
|
| 100 |
c,
|
| 101 |
accountPool,
|
|
|
|
| 102 |
cookieJar,
|
| 103 |
{
|
| 104 |
codexRequest,
|
|
|
|
| 105 |
model: req.model,
|
| 106 |
isStreaming: req.stream,
|
| 107 |
},
|
|
@@ -9,19 +9,16 @@
|
|
| 9 |
import type { Context } from "hono";
|
| 10 |
import type { StatusCode } from "hono/utils/http-status";
|
| 11 |
import { stream } from "hono/streaming";
|
| 12 |
-
import { randomUUID } from "crypto";
|
| 13 |
import { CodexApi, CodexApiError } from "../../proxy/codex-api.js";
|
| 14 |
import type { CodexResponsesRequest } from "../../proxy/codex-api.js";
|
| 15 |
import { EmptyResponseError } from "../../translation/codex-event-extractor.js";
|
| 16 |
import type { AccountPool } from "../../auth/account-pool.js";
|
| 17 |
-
import type { SessionManager } from "../../session/manager.js";
|
| 18 |
import type { CookieJar } from "../../proxy/cookie-jar.js";
|
| 19 |
import { withRetry } from "../../utils/retry.js";
|
| 20 |
|
| 21 |
/** Data prepared by each route after parsing and translating the request. */
|
| 22 |
export interface ProxyRequest {
|
| 23 |
codexRequest: CodexResponsesRequest;
|
| 24 |
-
sessionMessages: Array<{ role: string; content: string }>;
|
| 25 |
model: string;
|
| 26 |
isStreaming: boolean;
|
| 27 |
}
|
|
@@ -59,7 +56,6 @@ export interface FormatAdapter {
|
|
| 59 |
export async function handleProxyRequest(
|
| 60 |
c: Context,
|
| 61 |
accountPool: AccountPool,
|
| 62 |
-
sessionManager: SessionManager,
|
| 63 |
cookieJar: CookieJar | undefined,
|
| 64 |
req: ProxyRequest,
|
| 65 |
fmt: FormatAdapter,
|
|
@@ -76,15 +72,6 @@ export async function handleProxyRequest(
|
|
| 76 |
// Tracks which account the outer catch should release (updated by retry loop)
|
| 77 |
let activeEntryId = entryId;
|
| 78 |
|
| 79 |
-
// 2. Session lookup for multi-turn
|
| 80 |
-
const existingSession = sessionManager.findSession(req.sessionMessages);
|
| 81 |
-
const previousResponseId = existingSession?.responseId ?? null;
|
| 82 |
-
if (previousResponseId) {
|
| 83 |
-
req.codexRequest.previous_response_id = previousResponseId;
|
| 84 |
-
console.log(
|
| 85 |
-
`[${fmt.tag}] Account ${entryId} | Multi-turn: previous_response_id=${previousResponseId}`,
|
| 86 |
-
);
|
| 87 |
-
}
|
| 88 |
console.log(
|
| 89 |
`[${fmt.tag}] Account ${entryId} | Codex request:`,
|
| 90 |
JSON.stringify(req.codexRequest).slice(0, 300),
|
|
@@ -111,7 +98,6 @@ export async function handleProxyRequest(
|
|
| 111 |
|
| 112 |
return stream(c, async (s) => {
|
| 113 |
s.onAbort(() => abortController.abort());
|
| 114 |
-
let sessionTaskId: string | null = null;
|
| 115 |
try {
|
| 116 |
for await (const chunk of fmt.streamTranslator(
|
| 117 |
codexApi,
|
|
@@ -120,17 +106,7 @@ export async function handleProxyRequest(
|
|
| 120 |
(u) => {
|
| 121 |
usageInfo = u;
|
| 122 |
},
|
| 123 |
-
(
|
| 124 |
-
if (!sessionTaskId) {
|
| 125 |
-
sessionTaskId = `task-${randomUUID()}`;
|
| 126 |
-
sessionManager.storeSession(
|
| 127 |
-
sessionTaskId,
|
| 128 |
-
"turn-1",
|
| 129 |
-
req.sessionMessages,
|
| 130 |
-
);
|
| 131 |
-
}
|
| 132 |
-
sessionManager.updateResponseId(sessionTaskId, respId);
|
| 133 |
-
},
|
| 134 |
)) {
|
| 135 |
await s.write(chunk);
|
| 136 |
}
|
|
@@ -161,15 +137,6 @@ export async function handleProxyRequest(
|
|
| 161 |
currentRawResponse,
|
| 162 |
req.model,
|
| 163 |
);
|
| 164 |
-
if (result.responseId) {
|
| 165 |
-
const taskId = `task-${randomUUID()}`;
|
| 166 |
-
sessionManager.storeSession(
|
| 167 |
-
taskId,
|
| 168 |
-
"turn-1",
|
| 169 |
-
req.sessionMessages,
|
| 170 |
-
);
|
| 171 |
-
sessionManager.updateResponseId(taskId, result.responseId);
|
| 172 |
-
}
|
| 173 |
accountPool.release(currentEntryId, result.usage);
|
| 174 |
return c.json(result.response);
|
| 175 |
} catch (collectErr) {
|
|
|
|
| 9 |
import type { Context } from "hono";
|
| 10 |
import type { StatusCode } from "hono/utils/http-status";
|
| 11 |
import { stream } from "hono/streaming";
|
|
|
|
| 12 |
import { CodexApi, CodexApiError } from "../../proxy/codex-api.js";
|
| 13 |
import type { CodexResponsesRequest } from "../../proxy/codex-api.js";
|
| 14 |
import { EmptyResponseError } from "../../translation/codex-event-extractor.js";
|
| 15 |
import type { AccountPool } from "../../auth/account-pool.js";
|
|
|
|
| 16 |
import type { CookieJar } from "../../proxy/cookie-jar.js";
|
| 17 |
import { withRetry } from "../../utils/retry.js";
|
| 18 |
|
| 19 |
/** Data prepared by each route after parsing and translating the request. */
|
| 20 |
export interface ProxyRequest {
|
| 21 |
codexRequest: CodexResponsesRequest;
|
|
|
|
| 22 |
model: string;
|
| 23 |
isStreaming: boolean;
|
| 24 |
}
|
|
|
|
| 56 |
export async function handleProxyRequest(
|
| 57 |
c: Context,
|
| 58 |
accountPool: AccountPool,
|
|
|
|
| 59 |
cookieJar: CookieJar | undefined,
|
| 60 |
req: ProxyRequest,
|
| 61 |
fmt: FormatAdapter,
|
|
|
|
| 72 |
// Tracks which account the outer catch should release (updated by retry loop)
|
| 73 |
let activeEntryId = entryId;
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
console.log(
|
| 76 |
`[${fmt.tag}] Account ${entryId} | Codex request:`,
|
| 77 |
JSON.stringify(req.codexRequest).slice(0, 300),
|
|
|
|
| 98 |
|
| 99 |
return stream(c, async (s) => {
|
| 100 |
s.onAbort(() => abortController.abort());
|
|
|
|
| 101 |
try {
|
| 102 |
for await (const chunk of fmt.streamTranslator(
|
| 103 |
codexApi,
|
|
|
|
| 106 |
(u) => {
|
| 107 |
usageInfo = u;
|
| 108 |
},
|
| 109 |
+
() => {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
)) {
|
| 111 |
await s.write(chunk);
|
| 112 |
}
|
|
|
|
| 137 |
currentRawResponse,
|
| 138 |
req.model,
|
| 139 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
accountPool.release(currentEntryId, result.usage);
|
| 141 |
return c.json(result.response);
|
| 142 |
} catch (collectErr) {
|
|
@@ -1,121 +0,0 @@
|
|
| 1 |
-
import { createHash } from "crypto";
|
| 2 |
-
import { getConfig } from "../config.js";
|
| 3 |
-
|
| 4 |
-
const MAX_SESSIONS = 10000;
|
| 5 |
-
|
| 6 |
-
interface Session {
|
| 7 |
-
taskId: string;
|
| 8 |
-
turnId: string;
|
| 9 |
-
messageHash: string;
|
| 10 |
-
responseId: string | null;
|
| 11 |
-
createdAt: number;
|
| 12 |
-
}
|
| 13 |
-
|
| 14 |
-
export class SessionManager {
|
| 15 |
-
private sessions = new Map<string, Session>();
|
| 16 |
-
private ttlMs: number;
|
| 17 |
-
private cleanupTimer: ReturnType<typeof setInterval>;
|
| 18 |
-
|
| 19 |
-
constructor() {
|
| 20 |
-
const { ttl_minutes, cleanup_interval_minutes } = getConfig().session;
|
| 21 |
-
this.ttlMs = ttl_minutes * 60 * 1000;
|
| 22 |
-
this.cleanupTimer = setInterval(
|
| 23 |
-
() => this.cleanup(),
|
| 24 |
-
cleanup_interval_minutes * 60 * 1000,
|
| 25 |
-
);
|
| 26 |
-
if (this.cleanupTimer.unref) this.cleanupTimer.unref();
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
destroy(): void {
|
| 30 |
-
clearInterval(this.cleanupTimer);
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
/**
|
| 34 |
-
* Hash the message history to create a session key
|
| 35 |
-
*/
|
| 36 |
-
hashMessages(
|
| 37 |
-
messages: Array<{ role: string; content: string }>,
|
| 38 |
-
): string {
|
| 39 |
-
const data = JSON.stringify(messages.map((m) => [m.role, m.content]));
|
| 40 |
-
return createHash("sha256").update(data).digest("hex").slice(0, 32);
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
/**
|
| 44 |
-
* Find an existing session that matches the message history prefix
|
| 45 |
-
*/
|
| 46 |
-
findSession(
|
| 47 |
-
messages: Array<{ role: string; content: string }>,
|
| 48 |
-
): Session | null {
|
| 49 |
-
// Try matching all messages except the last (the new user message)
|
| 50 |
-
if (messages.length < 2) return null;
|
| 51 |
-
const prefix = messages.slice(0, -1);
|
| 52 |
-
const hash = this.hashMessages(prefix);
|
| 53 |
-
|
| 54 |
-
for (const session of this.sessions.values()) {
|
| 55 |
-
if (
|
| 56 |
-
session.messageHash === hash &&
|
| 57 |
-
Date.now() - session.createdAt < this.ttlMs
|
| 58 |
-
) {
|
| 59 |
-
return session;
|
| 60 |
-
}
|
| 61 |
-
}
|
| 62 |
-
return null;
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
/**
|
| 66 |
-
* Store a session after task creation
|
| 67 |
-
*/
|
| 68 |
-
storeSession(
|
| 69 |
-
taskId: string,
|
| 70 |
-
turnId: string,
|
| 71 |
-
messages: Array<{ role: string; content: string }>,
|
| 72 |
-
): void {
|
| 73 |
-
const hash = this.hashMessages(messages);
|
| 74 |
-
// P2-10: O(1) LRU eviction using Map insertion order
|
| 75 |
-
if (this.sessions.size >= MAX_SESSIONS) {
|
| 76 |
-
const oldestKey = this.sessions.keys().next().value;
|
| 77 |
-
if (oldestKey) this.sessions.delete(oldestKey);
|
| 78 |
-
}
|
| 79 |
-
this.sessions.set(taskId, {
|
| 80 |
-
taskId,
|
| 81 |
-
turnId,
|
| 82 |
-
messageHash: hash,
|
| 83 |
-
responseId: null,
|
| 84 |
-
createdAt: Date.now(),
|
| 85 |
-
});
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
/**
|
| 89 |
-
* Update the response ID for an existing session (for multi-turn previous_response_id)
|
| 90 |
-
*/
|
| 91 |
-
updateResponseId(taskId: string, responseId: string): void {
|
| 92 |
-
const session = this.sessions.get(taskId);
|
| 93 |
-
if (session) session.responseId = responseId;
|
| 94 |
-
}
|
| 95 |
-
|
| 96 |
-
/**
|
| 97 |
-
* Update turn ID for an existing session
|
| 98 |
-
*/
|
| 99 |
-
updateTurn(taskId: string, turnId: string): void {
|
| 100 |
-
const session = this.sessions.get(taskId);
|
| 101 |
-
if (session) {
|
| 102 |
-
session.turnId = turnId;
|
| 103 |
-
}
|
| 104 |
-
}
|
| 105 |
-
|
| 106 |
-
/**
|
| 107 |
-
* Get session by explicit task ID
|
| 108 |
-
*/
|
| 109 |
-
getSession(taskId: string): Session | null {
|
| 110 |
-
return this.sessions.get(taskId) ?? null;
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
private cleanup(): void {
|
| 114 |
-
const now = Date.now();
|
| 115 |
-
for (const [key, session] of this.sessions) {
|
| 116 |
-
if (now - session.createdAt > this.ttlMs) {
|
| 117 |
-
this.sessions.delete(key);
|
| 118 |
-
}
|
| 119 |
-
}
|
| 120 |
-
}
|
| 121 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -151,7 +151,6 @@ function contentToInputItems(
|
|
| 151 |
*/
|
| 152 |
export function translateAnthropicToCodexRequest(
|
| 153 |
req: AnthropicMessagesRequest,
|
| 154 |
-
previousResponseId?: string | null,
|
| 155 |
): CodexResponsesRequest {
|
| 156 |
// Extract system instructions
|
| 157 |
let userInstructions: string;
|
|
@@ -205,11 +204,6 @@ export function translateAnthropicToCodexRequest(
|
|
| 205 |
request.tool_choice = codexToolChoice;
|
| 206 |
}
|
| 207 |
|
| 208 |
-
// Add previous response ID for multi-turn conversations
|
| 209 |
-
if (previousResponseId) {
|
| 210 |
-
request.previous_response_id = previousResponseId;
|
| 211 |
-
}
|
| 212 |
-
|
| 213 |
// Always request reasoning summary (translation layer filters output on demand)
|
| 214 |
const thinkingEffort = mapThinkingToEffort(req.thinking);
|
| 215 |
const effort =
|
|
|
|
| 151 |
*/
|
| 152 |
export function translateAnthropicToCodexRequest(
|
| 153 |
req: AnthropicMessagesRequest,
|
|
|
|
| 154 |
): CodexResponsesRequest {
|
| 155 |
// Extract system instructions
|
| 156 |
let userInstructions: string;
|
|
|
|
| 204 |
request.tool_choice = codexToolChoice;
|
| 205 |
}
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
// Always request reasoning summary (translation layer filters output on demand)
|
| 208 |
const thinkingEffort = mapThinkingToEffort(req.thinking);
|
| 209 |
const effort =
|
|
@@ -161,7 +161,6 @@ export function geminiContentsToMessages(
|
|
| 161 |
export function translateGeminiToCodexRequest(
|
| 162 |
req: GeminiGenerateContentRequest,
|
| 163 |
geminiModel: string,
|
| 164 |
-
previousResponseId?: string | null,
|
| 165 |
): CodexResponsesRequest {
|
| 166 |
// Extract system instructions
|
| 167 |
let userInstructions: string;
|
|
@@ -212,11 +211,6 @@ export function translateGeminiToCodexRequest(
|
|
| 212 |
request.tool_choice = codexToolChoice;
|
| 213 |
}
|
| 214 |
|
| 215 |
-
// Add previous response ID for multi-turn conversations
|
| 216 |
-
if (previousResponseId) {
|
| 217 |
-
request.previous_response_id = previousResponseId;
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
// Always request reasoning summary (translation layer filters output on demand)
|
| 221 |
const thinkingEffort = budgetToEffort(
|
| 222 |
req.generationConfig?.thinkingConfig?.thinkingBudget,
|
|
|
|
| 161 |
export function translateGeminiToCodexRequest(
|
| 162 |
req: GeminiGenerateContentRequest,
|
| 163 |
geminiModel: string,
|
|
|
|
| 164 |
): CodexResponsesRequest {
|
| 165 |
// Extract system instructions
|
| 166 |
let userInstructions: string;
|
|
|
|
| 211 |
request.tool_choice = codexToolChoice;
|
| 212 |
}
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
// Always request reasoning summary (translation layer filters output on demand)
|
| 215 |
const thinkingEffort = budgetToEffort(
|
| 216 |
req.generationConfig?.thinkingConfig?.thinkingBudget,
|
|
@@ -80,7 +80,6 @@ function extractContent(
|
|
| 80 |
*/
|
| 81 |
export function translateToCodexRequest(
|
| 82 |
req: ChatCompletionRequest,
|
| 83 |
-
previousResponseId?: string | null,
|
| 84 |
): CodexResponsesRequest {
|
| 85 |
// Collect system/developer messages as instructions
|
| 86 |
const systemMessages = req.messages.filter(
|
|
@@ -174,11 +173,6 @@ export function translateToCodexRequest(
|
|
| 174 |
request.tool_choice = codexToolChoice;
|
| 175 |
}
|
| 176 |
|
| 177 |
-
// Add previous response ID for multi-turn conversations
|
| 178 |
-
if (previousResponseId) {
|
| 179 |
-
request.previous_response_id = previousResponseId;
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
// Always request reasoning summary (translation layer filters output on demand)
|
| 183 |
const effort =
|
| 184 |
req.reasoning_effort ??
|
|
|
|
| 80 |
*/
|
| 81 |
export function translateToCodexRequest(
|
| 82 |
req: ChatCompletionRequest,
|
|
|
|
| 83 |
): CodexResponsesRequest {
|
| 84 |
// Collect system/developer messages as instructions
|
| 85 |
const systemMessages = req.messages.filter(
|
|
|
|
| 173 |
request.tool_choice = codexToolChoice;
|
| 174 |
}
|
| 175 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
// Always request reasoning summary (translation layer filters output on demand)
|
| 177 |
const effort =
|
| 178 |
req.reasoning_effort ??
|