90 lines
3.1 KiB
JavaScript
90 lines
3.1 KiB
JavaScript
import crypto from "crypto";
|
|
import { BaseExecutor } from "./base.js";
|
|
import { PROVIDERS } from "../config/providers.js";
|
|
import { injectReasoningContent } from "../utils/reasoningContentInjector.js";
|
|
import { resolveSessionId } from "../utils/sessionManager.js";
|
|
|
|
const OPENCODE_UA = "opencode";
|
|
const MESSAGES_MODELS = new Set();
|
|
|
|
function generateRequestId() {
|
|
return `msg_${crypto.randomUUID().replace(/-/g, "")}`;
|
|
}
|
|
|
|
function generateSessionId() {
|
|
return `ses_${crypto.randomUUID().replace(/-/g, "")}`;
|
|
}
|
|
|
|
// Normalize any resolved id into opencode's ses_ format (stable per-conversation)
|
|
function toOpencodeSession(id) {
|
|
const stripped = String(id || "").replace(/^ses_/, "").replace(/-/g, "");
|
|
return stripped ? `ses_${stripped}` : null;
|
|
}
|
|
|
|
function resolveOpencodeSession(body, credentials) {
|
|
return toOpencodeSession(resolveSessionId({
|
|
headers: credentials?.rawHeaders,
|
|
body,
|
|
connectionId: credentials?.connectionId,
|
|
scope: "opencode",
|
|
}));
|
|
}
|
|
|
|
// OpenCode free tier is limited per egress IP — a 429/403 with a limit-ish
|
|
// body means the POOL's IP is exhausted, not the account. Declare it
|
|
// pool-scoped so chatCore marks the pool unfit, retries via another pool, and
|
|
// it shows up (clearable) on the Proxy Fitness page.
|
|
const IP_LIMIT_BODY = /limit|rate|quota|exhausted|capacity|too many|retry/i;
|
|
|
|
export class OpenCodeExecutor extends BaseExecutor {
|
|
constructor() {
|
|
super("opencode", PROVIDERS.opencode);
|
|
this._currentSessionId = null;
|
|
}
|
|
|
|
transformRequest(model, body, stream, credentials) {
|
|
this._currentSessionId = resolveOpencodeSession(body, credentials);
|
|
return injectReasoningContent({ provider: this.provider, model, body });
|
|
}
|
|
|
|
buildUrl(model) {
|
|
const base = this.config.baseUrl;
|
|
return MESSAGES_MODELS.has(model)
|
|
? `${base}/zen/v1/messages`
|
|
: `${base}/zen/v1/chat/completions`;
|
|
}
|
|
|
|
buildHeaders(credentials, stream = true) {
|
|
const raw = credentials?.rawHeaders || {};
|
|
const lower = {};
|
|
for (const [k, v] of Object.entries(raw)) lower[k.toLowerCase()] = v;
|
|
|
|
const downstreamUa = lower["user-agent"] || "";
|
|
const isOpencodeDownstream = downstreamUa.toLowerCase().includes("opencode");
|
|
|
|
return {
|
|
"Content-Type": "application/json",
|
|
"Authorization": "Bearer public",
|
|
"User-Agent": isOpencodeDownstream ? downstreamUa : OPENCODE_UA,
|
|
"x-opencode-client": lower["x-opencode-client"] || "desktop",
|
|
"x-opencode-session": lower["x-opencode-session"] || this._currentSessionId || generateSessionId(),
|
|
"x-opencode-request": lower["x-opencode-request"] || generateRequestId(),
|
|
"x-opencode-project": lower["x-opencode-project"] || "global",
|
|
"Accept": stream ? "text/event-stream" : "*/*",
|
|
};
|
|
}
|
|
|
|
parseError(response, bodyText) {
|
|
const status = response?.status || 0;
|
|
const text = String(bodyText || "");
|
|
if ((status === 429 || status === 403) && IP_LIMIT_BODY.test(text)) {
|
|
return {
|
|
status,
|
|
message: text.slice(0, 300) || `OpenCode free limit (${status})`,
|
|
poolScoped: { reason: "ip-limit" },
|
|
};
|
|
}
|
|
return null; // fall through to default parsing
|
|
}
|
|
}
|