Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 161 additions & 55 deletions open-sse/executors/theoldllm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ export function mapModel(model: string): string {
const TOKEN_SEED = "oldllm-client-2026";
const UA_PREFIX = CHROME_UA.slice(0, 20); // "Mozilla/5.0 (Windows"

type TheOldLlmProxy = {
type?: string;
host: string;
port: number;
username?: string | null;
password?: string | null;
} | null;

interface TheOldLlmFetchDependencies {
resolveProxy: () => Promise<TheOldLlmProxy>;
runWithProxy: <T>(proxy: TheOldLlmProxy, request: () => Promise<T>) => Promise<T>;
fetch: typeof fetch;
hasBlockingProxyAssignment?: () => boolean;
}

class TheOldLlmProxyUnavailableError extends Error {}

export function generateRequestToken(): string {
const n = Date.now();
const e = `${n}-${TOKEN_SEED}-${UA_PREFIX}`;
Expand All @@ -127,17 +144,31 @@ export const tokenCache: { value: string; expiresAt: number } = { value: "", exp

// ── Direct Node.js fetch ──────────────────────────────────────────────────

async function directFetch(
export async function fetchTheOldLlmWithProviderProxy(
reqBody: Record<string, unknown>,
signal?: AbortSignal | null
signal: AbortSignal,
dependencies?: TheOldLlmFetchDependencies
): Promise<Response> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 120_000);
const onSignal = signal ? () => controller.abort(signal.reason) : undefined;
signal?.addEventListener("abort", onSignal!, { once: true });
let deps = dependencies;
if (!deps) {
const [
{ resolveProxyForProvider, hasBlockingProxyAssignmentForProvider },
{ runWithProxyContext },
] = await Promise.all([import("../../src/lib/db/proxies"), import("../utils/proxyFetch.ts")]);
deps = {
resolveProxy: () => resolveProxyForProvider("theoldllm"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve API-key proxy precedence for The Old LLM

When a The Old LLM chat is made with a per-API-key proxy and a provider/global proxy also exists, the outer chat path has already resolved the higher-priority API-key proxy via safeResolveProxy(..., apiKeyInfo.id, provider), but this new resolver fetches the provider/global proxy again and passes it as a non-null argument to runWithProxyContext. Since runWithProxyContext only inherits the current context when its argument is null, the inner provider/global proxy overrides the per-key proxy, breaking per-client egress isolation for this provider; reuse the already-resolved proxy context or only self-resolve when no context is present.

Useful? React with 👍 / 👎.

runWithProxy: runWithProxyContext,
fetch: globalThis.fetch,
hasBlockingProxyAssignment: () => hasBlockingProxyAssignmentForProvider("theoldllm"),
};
}

try {
return await fetch(API_URL, {
const proxy = await deps.resolveProxy();
if (!proxy && deps.hasBlockingProxyAssignment?.()) {
throw new TheOldLlmProxyUnavailableError("No active proxy is available for The Old LLM");
}
return deps.runWithProxy(proxy, () =>
deps.fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -146,14 +177,41 @@ async function directFetch(
"User-Agent": CHROME_UA,
},
body: JSON.stringify(reqBody),
signal: controller.signal,
});
signal,
})
);
}

async function directFetch(
reqBody: Record<string, unknown>,
signal?: AbortSignal | null
): Promise<Response> {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 120_000);
const onSignal = signal ? () => controller.abort(signal.reason) : undefined;
signal?.addEventListener("abort", onSignal!, { once: true });

try {
// No-auth providers do not have a connection row, so chatCore cannot apply
// a connection-scoped proxy context for them. Resolve the provider/global
// assignment explicitly; otherwise The Old LLM always leaks out through the
// VPS address and Vercel's bot protection denies every model.
return await fetchTheOldLlmWithProviderProxy(reqBody, controller.signal);
} finally {
clearTimeout(timer);
if (onSignal) signal?.removeEventListener("abort", onSignal);
}
}

export function isVercelMitigationResponse(response: Response, body: string): boolean {
const mitigation = response.headers.get("x-vercel-mitigated")?.toLowerCase();
if (mitigation === "deny" || mitigation === "challenge") return true;
return (
(response.status === 403 || response.status === 429) &&
/vercel security checkpoint|"message"\s*:\s*"forbidden"/i.test(body)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Narrow Vercel denial detection to Vercel signals

When The Old LLM returns a normal app/auth 403 body such as {"error":{"message":"Forbidden"}} without the x-vercel-mitigated header, this broad body match classifies it as Vercel mitigation before isTokenRejected() runs. In that scenario fetchUpstreamWithRetry() skips the fresh-token retry and surfaces a proxy/Vercel error even though the request-token retry path was meant to handle 403s; require the Vercel header or a Vercel-specific body marker here.

Useful? React with 👍 / 👎.

);
}

function isTokenRejected(status: number, body: string): boolean {
if (status === 401 || status === 403) return true;
try {
Expand Down Expand Up @@ -211,6 +269,45 @@ function buildErrorResponse(status: number, body: string): string {
});
}

function buildVercelMitigationError(): string {
return JSON.stringify({
error: {
message:
"The Old LLM is blocked by Vercel for this server egress IP. Configure a residential provider or global proxy for 'theoldllm' and retry.",
type: "upstream_access_denied",
code: "THEOLDLLM_VERCEL_MITIGATED",
},
});
}

function buildProxyUnavailableError(): string {
return JSON.stringify({
error: {
message:
"The Old LLM proxy assignment has no active proxies. Configure or enable a proxy and retry.",
type: "proxy_unavailable",
code: "THEOLDLLM_PROXY_UNAVAILABLE",
},
});
}

async function fetchUpstreamWithRetry(
reqBody: Record<string, unknown>,
signal: AbortSignal | null | undefined,
log: ExecuteInput["log"]
): Promise<{ response: Response; body: string; vercelMitigated: boolean }> {
let response = await directFetch(reqBody, signal);
let body = await response.text();
let vercelMitigated = isVercelMitigationResponse(response, body);
if (!vercelMitigated && isTokenRejected(response.status, body)) {
log?.warn?.("THEOLDLLM", `Token rejected (${response.status}), retrying with fresh token…`);
response = await directFetch(reqBody, signal);
body = await response.text();
vercelMitigated = isVercelMitigationResponse(response, body);
}
return { response, body, vercelMitigated };
}

// ── Executor ──────────────────────────────────────────────────────────────

export class TheOldLlmExecutor extends BaseExecutor {
Expand All @@ -237,27 +334,37 @@ export class TheOldLlmExecutor extends BaseExecutor {
return body;
}

private executionResult(input: ExecuteInput, response: Response, body: unknown) {
return {
response,
url: API_URL,
headers: this.buildHeaders(input.credentials),
transformedBody: body,
};
}

async testConnection(
_credentials: ProviderCredentials,
_signal?: AbortSignal | null,
log?: ExecuteInput["log"]
): Promise<boolean> {
try {
const resp = await fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Client-Version": "3.8.4",
"X-Request-Token": generateRequestToken(),
"User-Agent": CHROME_UA,
},
body: JSON.stringify({
const resp = await directFetch(
{
model: "GPT_5_4",
messages: [{ role: "user", content: "ping" }],
stream: false,
}),
signal: _signal ?? undefined,
});
},
_signal
);
const body = await resp.text();
if (!resp.ok && isVercelMitigationResponse(resp, body)) {
log?.warn?.(
"THEOLDLLM",
"Vercel blocked this egress IP; configure a residential provider proxy"
);
return false;
}
return resp.status === 200;
} catch {
log?.warn?.("THEOLDLLM", "testConnection network error");
Expand Down Expand Up @@ -297,56 +404,55 @@ export class TheOldLlmExecutor extends BaseExecutor {
stream: true,
};

let upstream = await directFetch(reqBody, signal);
let finalBody = await upstream.text();

if (isTokenRejected(upstream.status, finalBody)) {
log?.warn?.("THEOLDLLM", `Token rejected (${upstream.status}), retrying with fresh token…`);
upstream = await directFetch(reqBody, signal);
finalBody = await upstream.text();
}
const {
response: upstream,
body: finalBody,
vercelMitigated,
} = await fetchUpstreamWithRetry(reqBody, signal, log);

if (upstream.status === 200 && finalBody) {
const payload = stream ? finalBody : buildChatCompletion(parseSseContent(finalBody), model);
return {
response: new Response(encoder.encode(payload), {
return this.executionResult(
input,
new Response(encoder.encode(payload), {
status: 200,
headers: {
"Content-Type": stream ? "text/event-stream" : "application/json",
"Cache-Control": "no-cache",
},
}),
url: API_URL,
headers: this.buildHeaders(input.credentials),
transformedBody: body,
};
body
);
}

return {
response: new Response(encoder.encode(buildErrorResponse(upstream.status, finalBody)), {
const errorPayload = vercelMitigated
? buildVercelMitigationError()
: buildErrorResponse(upstream.status, finalBody);
return this.executionResult(
input,
new Response(encoder.encode(errorPayload), {
status: upstream.status,
headers: { "Content-Type": "application/json" },
}),
url: API_URL,
headers: this.buildHeaders(input.credentials),
transformedBody: body,
};
body
);
} catch (err) {
const proxyUnavailable = err instanceof TheOldLlmProxyUnavailableError;
const msg = err instanceof Error ? err.message : String(err);
log?.error?.("THEOLDLLM", `Executor error: ${msg}`);
return {
response: new Response(
encoder.encode(
JSON.stringify({
error: { message: msg, type: "upstream_error", code: "EXECUTOR_ERROR" },
})
),
{ status: 502, headers: { "Content-Type": "application/json" } }
),
url: API_URL,
headers: this.buildHeaders(input.credentials),
transformedBody: body,
};
const errorPayload = proxyUnavailable
? buildProxyUnavailableError()
: JSON.stringify({
error: { message: msg, type: "upstream_error", code: "EXECUTOR_ERROR" },
});
return this.executionResult(
input,
new Response(encoder.encode(errorPayload), {
status: proxyUnavailable ? 503 : 502,
headers: { "Content-Type": "application/json" },
}),
body
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,6 @@ export default function ProviderDetailPageClient() {
providerNode,
});

// renderModelsSection → components/ProviderModelsSection.tsx (Phase 1m)

if (loading) {
return (
<div className="flex flex-col gap-8">
Expand All @@ -447,7 +445,6 @@ export default function ProviderDetailPageClient() {

return (
<div className="flex flex-col gap-8">
{/* Header — Phase 1t.1: extracted to components/ProviderPageHeader.tsx */}
<ProviderPageHeader
providerId={providerId}
providerInfo={providerInfo}
Expand All @@ -461,8 +458,6 @@ export default function ProviderDetailPageClient() {
{providerId === "zed" && (
<ZedImportCard fetchConnections={fetchConnections} notify={notify} />
)}

{/* CompatibleNodeCard — Phase 1t.2: extracted to components/CompatibleNodeCard.tsx */}
{isCompatible && providerNode && (
<CompatibleNodeCard
providerId={providerId}
Expand All @@ -476,17 +471,26 @@ export default function ProviderDetailPageClient() {
t={t}
/>
)}

{/* Connections */}
{!isUpstreamProxyProvider && isFreeNoAuth && (
<NoAuthProviderControls
providerId={providerId}
providerName={providerInfo?.name || providerId}
providerProxy={proxyConfig?.providers?.[providerId]}
onConfigureProviderProxy={() =>
setProxyTarget({
level: "provider",
id: providerId,
label: providerInfo?.name || providerId,
})
}
/>
)}
{!isUpstreamProxyProvider && !isFreeNoAuth && (
<Card>
<ProviderAccountRoutingCard providerKey={providerId} connectionCount={connections.length} />
<ProviderAccountRoutingCard
providerKey={providerId}
connectionCount={connections.length}
/>
<ConnectionsHeaderToolbar
providerId={providerId}
providerInfo={providerInfo}
Expand Down
Loading
Loading