Spaces:
Sleeping
Sleeping
github-actions[bot] commited on
Commit ·
f6266b9
1
Parent(s): eab1530
Deploy from GitHub: 7495fde758f0be655f95e6331fec2898267f790c
Browse filesFiles changed (34)
- .dockerignoreAdded+7
- .env.exampleAdded+58
- .gitattributesDeleted-35
- DockerfileAdded+33
- README.md+53-4
- entrypoint.shAdded+71
- package.jsonAdded+14
- src/auth.jsAdded+958
- src/cache.jsAdded+83
- src/client.jsAdded+677
- src/config.jsAdded+61
- src/connect.jsAdded+145
- src/conversation-pool.jsAdded+157
- src/dashboard/api.jsAdded+640
- src/dashboard/index.htmlAdded
- src/dashboard/logger.jsAdded+158
- src/dashboard/model-access.jsAdded+79
- src/dashboard/proxy-config.jsAdded+76
- src/dashboard/stats.jsAdded+130
- src/dashboard/windsurf-login.jsAdded+294
- src/grpc.jsAdded+199
- src/handlers/chat.jsAdded+806
- src/handlers/messages.jsAdded+431
- src/handlers/models.jsAdded+5
- src/handlers/tool-emulation.jsAdded+363
- src/index.jsAdded+88
- src/langserver.jsAdded+266
- src/models.jsAdded+287
- src/proto.jsAdded+146
- src/runtime-config.jsAdded+140
- src/sanitize.jsAdded+150
- src/server.jsAdded+303
- src/windsurf-api.jsAdded+298
- src/windsurf.jsAdded+941
1234567.git.gitignore.githubdocstestnode_modulesnpm-debug.log*12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# ========== 本地开发 / 非 HF 通用 ==========# 本地默认端口。注意:HF Space 运行时会被强制写成 PORT=7860PORT=3001# API key for incoming requests (leave empty for open access)API_KEY=
# ========== Windsurf Auth(可选预置;不填也可启动,后续可通过 Dashboard / Token / 批量添加账号) ==========# 方式 1:直接填写 Windsurf / Codeium API KeyCODEIUM_API_KEY=
# 方式 2:填写 windsurf.com/show-auth-token 获取的 tokenCODEIUM_AUTH_TOKEN=
# ========== Language Server(HF Space 会强制覆盖 LS_BINARY_PATH) ==========# HF Space 运行时固定为 /opt/windsurf/language_server_linux_x64LS_BINARY_PATH=/opt/windsurf/language_server_linux_x64# 可选:Language Server gRPC portLS_PORT=42100
# ========== 可选运行参数(HF Space Variables / Secrets 推荐填写) ==========CODEIUM_API_URL=https://server.self-serve.windsurf.comDEFAULT_MODEL=claude-4.5-sonnet-thinkingMAX_TOKENS=8192LOG_LEVEL=infoDASHBOARD_PASSWORD=
# ========== GitHub -> HF Space 发布配置(GitHub 仓库使用,不是 HF Space 运行时) ==========# GitHub Secret:推送到 HF Space 仓库所需 tokenHF_TOKEN=# GitHub Secret:目标 Space 仓库HF_SPACE_REPO=DanielleNguyen/WindsurfAPI# GitHub Variable:Docker 构建期下载官方 Linux x64 tar.gz 安装包的地址HF_LS_DOWNLOAD_URL=
# ========== HF Space 最终清单 ==========# HF Space 必填 Secrets:# 1. API_KEY## HF Space 可选预置 Secrets:# 1. CODEIUM_API_KEY# 2. CODEIUM_AUTH_TOKEN# 不预置也能启动,后续可通过 Dashboard / Token / 批量添加账号## HF Space 推荐 Variables / Secrets:# 1. DEFAULT_MODEL# 2. MAX_TOKENS# 3. LOG_LEVEL# 4. DASHBOARD_PASSWORD# 5. LS_PORT# 6. CODEIUM_API_URL## HF Space 不需要填写:# 1. PORT(会固定写成 7860)# 2. LS_BINARY_PATH(会固定写成 /opt/windsurf/language_server_linux_x64)# 3. HF_TOKEN / HF_SPACE_REPO / HF_LS_DOWNLOAD_URL(这些只给 GitHub Actions 用)## 当前推荐的 HF_LS_DOWNLOAD_URL 形态:# https://windsurf-stable.codeiumdata.com/linux-x64/stable/<version>/Windsurf-linux-x64-<version>.tar.gz1234567891011121314151617181920212223242526272829303132333435*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text123456789101112131415161718192021222324252627282930313233FROM node:20-bookworm-slim
ARG LS_DOWNLOAD_URL=https://windsurf-stable.codeiumdata.com/linux-x64/stable/abcd9c8664da5af505557f3b327b5537400635f2/Windsurf-linux-x64-2.0.61.tar.gzARG LS_ARCHIVE_ENTRY_PATH="Windsurf/resources/app/extensions/windsurf/bin/language_server_linux_x64"
ENV DEBIAN_FRONTEND=noninteractive \ NODE_ENV=production
RUN apt-get update \ && apt-get install -y --no-install-recommends bash ca-certificates curl tar gzip \ && rm -rf /var/lib/apt/lists/* \ && test -n "$LS_DOWNLOAD_URL" \ && test "$LS_DOWNLOAD_URL" != "__LS_DOWNLOAD_URL__" \ && mkdir -p /opt/windsurf /opt/windsurf/data/db /tmp/windsurf-workspace \ && curl -fL "$LS_DOWNLOAD_URL" -o /tmp/windsurf-linux-x64.tar.gz \ && tar -xzf /tmp/windsurf-linux-x64.tar.gz -C /tmp "$LS_ARCHIVE_ENTRY_PATH" \ && mv "/tmp/$LS_ARCHIVE_ENTRY_PATH" /opt/windsurf/language_server_linux_x64 \ && rm -rf /tmp/windsurf-linux-x64.tar.gz /tmp/Windsurf \ && chmod +x /opt/windsurf/language_server_linux_x64
WORKDIR /app
COPY package.json ./package.jsonCOPY .env.example ./.env.exampleCOPY src ./srcCOPY entrypoint.sh ./entrypoint.shCOPY README.md ./README.md
RUN chmod +x /app/entrypoint.sh
EXPOSE 7860
ENTRYPOINT ["bash", "/app/entrypoint.sh"]12345678910---title: WindsurfAPIemoji: 📉colorFrom: purplecolorTo: purplesdk: dockerpinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859---title: WindsurfAPIemoji: "🌊"colorFrom: indigocolorTo: bluesdk: dockerpinned: false---# WindsurfAPI on Hugging Face Spaces这个 Space 运行的是 `WindsurfAPI` 的 Docker 版本。## 运行说明1. 镜像构建时会下载官方 Linux x64 `tar.gz` 安装包,并自动提取 `language_server_linux_x64`2. 运行时从 Hugging Face Space Secrets / Variables 生成 `.env`3. 持久化文件保存在挂载桶的 `/data/windsurf/state/`4. 外部服务端口固定为 `7860`## HF Space 最终填写清单| 名称 | 位置 | 是否必填 | 说明 ||---|---|---|---|| `API_KEY` | Secret | 必填 | 对外 API 的访问密钥 || `CODEIUM_API_KEY` | Secret | 可选预置 | Windsurf / Codeium API Key;不填也可启动 || `CODEIUM_AUTH_TOKEN` | Secret | 可选预置 | `windsurf.com/show-auth-token` 获取的 token;不填也可启动 || `DEFAULT_MODEL` | Variable 或 Secret | 推荐 | 默认模型,不填走 `claude-4.5-sonnet-thinking` || `MAX_TOKENS` | Variable 或 Secret | 推荐 | 默认最大输出 token,不填走 `8192` || `LOG_LEVEL` | Variable 或 Secret | 推荐 | 日志级别,不填走 `info` || `DASHBOARD_PASSWORD` | Secret | 推荐 | Dashboard 访问密码 || `LS_PORT` | Variable 或 Secret | 可选 | LS gRPC 端口,不填走 `42100` || `CODEIUM_API_URL` | Variable 或 Secret | 可选 | 上游接口地址,不填走官方默认值 |如果没有预先填写 `CODEIUM_API_KEY` 或 `CODEIUM_AUTH_TOKEN`,服务仍然可以启动,之后可以通过以下方式添加账号:1. Dashboard 一键登录2. Token 登录3. 批量导入## 不要在 HF Space 里填写这些- `PORT`:启动时会强制写成 `7860`- `LS_BINARY_PATH`:启动时会强制写成 `/opt/windsurf/language_server_linux_x64`- `HF_TOKEN`:这是 GitHub Actions 推送 Space 仓库用的,不是运行时变量- `HF_SPACE_REPO`:这是 GitHub Actions 发布目标,不是运行时变量- `HF_LS_DOWNLOAD_URL`:这是 GitHub Actions 构建发布包时替换 `Dockerfile` 用的,值应为官方 Linux x64 `tar.gz` 下载链接,不是运行时变量## 持久化文件- `/data/windsurf/state/.env`- `/data/windsurf/state/accounts.json`- `/data/windsurf/state/proxy.json`- `/data/windsurf/state/model-access.json`- `/data/windsurf/state/runtime-config.json`## 部署来源这个 Space 仓库由 GitHub Actions 从主仓库的 `huggingface/` 发布目录自动同步生成。
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071#!/usr/bin/env bashset -euo pipefail
APP_DIR="/app"STATE_DIR="/data/windsurf/state"OPT_DIR="/opt/windsurf"LS_PATH="${LS_BINARY_PATH:-/opt/windsurf/language_server_linux_x64}"
write_env_file() { cat > "$1" <<EOFPORT=7860API_KEY=${API_KEY}${CODEIUM_API_KEY:+CODEIUM_API_KEY=${CODEIUM_API_KEY}}${CODEIUM_AUTH_TOKEN:+CODEIUM_AUTH_TOKEN=${CODEIUM_AUTH_TOKEN}}${CODEIUM_EMAIL:+CODEIUM_EMAIL=${CODEIUM_EMAIL}}${CODEIUM_PASSWORD:+CODEIUM_PASSWORD=${CODEIUM_PASSWORD}}CODEIUM_API_URL=${CODEIUM_API_URL:-https://server.self-serve.windsurf.com}DEFAULT_MODEL=${DEFAULT_MODEL:-claude-4.5-sonnet-thinking}MAX_TOKENS=${MAX_TOKENS:-8192}LOG_LEVEL=${LOG_LEVEL:-info}LS_BINARY_PATH=/opt/windsurf/language_server_linux_x64LS_PORT=${LS_PORT:-42100}${DASHBOARD_PASSWORD:+DASHBOARD_PASSWORD=${DASHBOARD_PASSWORD}}EOF}
require_env() { local name="$1" if [ -z "${!name:-}" ]; then echo "[entrypoint] Missing required environment variable: $name" >&2 exit 1 fi}
ensure_json_file() { local path="$1" local content="$2" if [ ! -f "$path" ]; then printf '%s\n' "$content" > "$path" fi}
require_env API_KEYif [ -z "${CODEIUM_API_KEY:-}" ] && [ -z "${CODEIUM_AUTH_TOKEN:-}" ]; then echo "[entrypoint] No preloaded Windsurf credentials found. You can add accounts later via Dashboard, token login, or batch import."fi
mkdir -p "$STATE_DIR" "$OPT_DIR/data/db" /tmp/windsurf-workspace
if [ ! -f "$LS_PATH" ]; then echo "[entrypoint] Language server binary not found at $LS_PATH" >&2 exit 1fichmod +x "$LS_PATH"
ENV_OUTPUT="$STATE_DIR/.env.tmp"write_env_file "$ENV_OUTPUT"mv "$ENV_OUTPUT" "$STATE_DIR/.env"
ensure_json_file "$STATE_DIR/accounts.json" '[]'ensure_json_file "$STATE_DIR/proxy.json" '{"global":null,"perAccount":{}}'ensure_json_file "$STATE_DIR/model-access.json" '{"mode":"all","list":[]}'ensure_json_file "$STATE_DIR/runtime-config.json" '{}'
ln -sfn "$STATE_DIR/.env" "$APP_DIR/.env"ln -sfn "$STATE_DIR/accounts.json" "$APP_DIR/accounts.json"ln -sfn "$STATE_DIR/proxy.json" "$APP_DIR/proxy.json"ln -sfn "$STATE_DIR/model-access.json" "$APP_DIR/model-access.json"ln -sfn "$STATE_DIR/runtime-config.json" "$APP_DIR/runtime-config.json"
exec node src/index.js1234567891011121314{ "name": "windsurf-api", "version": "1.3.0", "description": "Windsurf to OpenAI-compatible API proxy", "type": "module", "main": "src/index.js", "scripts": { "start": "node src/index.js", "dev": "node --watch src/index.js" }, "engines": { "node": ">=20.0.0" }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958/** * Multi-account authentication pool for Codeium/Windsurf. * * Features: * - Multiple accounts with round-robin load balancing * - Account health tracking (error count, auto-disable) * - Dynamic add/remove via API * - Token-based registration via api.codeium.com */
import { randomUUID } from 'crypto';import { readFileSync, writeFileSync, existsSync } from 'fs';import { config, log } from './config.js';import { getEffectiveProxy } from './dashboard/proxy-config.js';import { getTierModels, getModelKeysByEnum, MODELS } from './models.js';
import { join } from 'path';const ACCOUNTS_FILE = join(process.cwd(), 'accounts.json');
// ─── Account pool ──────────────────────────────────────────
const accounts = [];let _roundRobinIndex = 0;
// Per-tier requests-per-minute limits. Used for both filter-by-cap and// weighted selection (accounts with more headroom are preferred).const TIER_RPM = { pro: 60, free: 10, unknown: 20, expired: 0 };const RPM_WINDOW_MS = 60 * 1000;
function rpmLimitFor(account) { return TIER_RPM[account.tier || 'unknown'] ?? 20;}
function pruneRpmHistory(account, now) { if (!account._rpmHistory) account._rpmHistory = []; const cutoff = now - RPM_WINDOW_MS; while (account._rpmHistory.length && account._rpmHistory[0] < cutoff) { account._rpmHistory.shift(); } return account._rpmHistory.length;}
function saveAccounts() { try { const data = accounts.map(a => ({ id: a.id, email: a.email, apiKey: a.apiKey, apiServerUrl: a.apiServerUrl, method: a.method, status: a.status, addedAt: a.addedAt, tier: a.tier, capabilities: a.capabilities, lastProbed: a.lastProbed, credits: a.credits || null, blockedModels: a.blockedModels || [], refreshToken: a.refreshToken || '', // From GetUserStatus — the authoritative tier/entitlement snapshot. userStatus: a.userStatus || null, userStatusLastFetched: a.userStatusLastFetched || 0, })); writeFileSync(ACCOUNTS_FILE, JSON.stringify(data, null, 2)); } catch (e) { log.error('Failed to save accounts:', e.message); }}
function loadAccounts() { try { if (!existsSync(ACCOUNTS_FILE)) return; const data = JSON.parse(readFileSync(ACCOUNTS_FILE, 'utf-8')); for (const a of data) { if (accounts.find(x => x.apiKey === a.apiKey)) continue; accounts.push({ id: a.id || randomUUID().slice(0, 8), email: a.email, apiKey: a.apiKey, apiServerUrl: a.apiServerUrl || '', method: a.method || 'api_key', status: a.status || 'active', lastUsed: 0, errorCount: 0, refreshToken: a.refreshToken || '', expiresAt: 0, refreshTimer: null, addedAt: a.addedAt || Date.now(), tier: a.tier || 'unknown', capabilities: a.capabilities || {}, lastProbed: a.lastProbed || 0, credits: a.credits || null, blockedModels: Array.isArray(a.blockedModels) ? a.blockedModels : [], userStatus: a.userStatus || null, userStatusLastFetched: a.userStatusLastFetched || 0, }); } if (data.length > 0) log.info(`Loaded ${data.length} account(s) from disk`); } catch (e) { log.error('Failed to load accounts:', e.message); }}
// ─── Dynamic model catalog from cloud ─────────────────────
async function fetchAndMergeModelCatalog() { // Use the first active account to fetch the catalog. const acct = accounts.find(a => a.status === 'active' && a.apiKey); if (!acct) { log.debug('No active account for model catalog fetch'); return; } try { const { getCascadeModelConfigs } = await import('./windsurf-api.js'); const { mergeCloudModels } = await import('./models.js'); const proxy = getEffectiveProxy(acct.id) || null; const { configs } = await getCascadeModelConfigs(acct.apiKey, proxy); const added = mergeCloudModels(configs); log.info(`Model catalog: ${configs.length} cloud models, ${added} new entries merged`); } catch (e) { log.warn(`Model catalog fetch failed: ${e.message}`); }}
async function registerWithCodeium(idToken) { const { WindsurfClient } = await import('./client.js'); const client = new WindsurfClient('', 0, ''); const result = await client.registerUser(idToken); return result; // { apiKey, name, apiServerUrl }}
// ─── Account management ───────────────────────────────────
/** * Add account via API key. */export function addAccountByKey(apiKey, label = '') { const existing = accounts.find(a => a.apiKey === apiKey); if (existing) return existing;
const account = { id: randomUUID().slice(0, 8), email: label || `key-${apiKey.slice(0, 8)}`, apiKey, apiServerUrl: '', method: 'api_key', status: 'active', lastUsed: 0, errorCount: 0, refreshToken: '', expiresAt: 0, refreshTimer: null, addedAt: Date.now(), tier: 'unknown', capabilities: {}, lastProbed: 0, blockedModels: [], }; account.credits = null; accounts.push(account); saveAccounts(); log.info(`Account added: ${account.id} (${account.email}) [api_key]`); return account;}
/** * Add account via auth token. */export async function addAccountByToken(token, label = '') { const reg = await registerWithCodeium(token); const existing = accounts.find(a => a.apiKey === reg.apiKey); if (existing) return existing;
const account = { id: randomUUID().slice(0, 8), email: label || reg.name || `token-${reg.apiKey.slice(0, 8)}`, apiKey: reg.apiKey, apiServerUrl: reg.apiServerUrl || '', method: 'token', status: 'active', lastUsed: 0, errorCount: 0, refreshToken: '', expiresAt: 0, refreshTimer: null, addedAt: Date.now(), tier: 'unknown', capabilities: {}, lastProbed: 0, blockedModels: [], credits: null, }; accounts.push(account); saveAccounts(); log.info(`Account added: ${account.id} (${account.email}) [token] server=${account.apiServerUrl}`); return account;}
/** * Add account via email/password is not supported for direct Firebase login. * Use token-based auth instead: get a token from windsurf.com/show-auth-token */export async function addAccountByEmail(email, password) { throw new Error('Direct email/password login is not supported. Use token-based auth: get token from windsurf.com, then POST /auth/login {"token":"..."}');}
/** * Per-account blocklist: hide specific models from this account so the * selector won't route matching requests here. Useful when one key has * burned its claude quota but still serves gpt just fine. */export function setAccountBlockedModels(id, blockedModels) { const account = accounts.find(a => a.id === id); if (!account) return false; account.blockedModels = Array.isArray(blockedModels) ? blockedModels.slice() : []; saveAccounts(); log.info(`Account ${id} blockedModels updated: ${account.blockedModels.length} blocked`); return true;}
/** * Resolve whether `modelKey` is callable on this account: * tier entitlement ∩ (models.js catalog) − account.blockedModels */export function isModelAllowedForAccount(account, modelKey) { const tierModels = getTierModels(account.tier || 'unknown'); if (!tierModels.includes(modelKey)) return false; const blocked = account.blockedModels || []; if (blocked.includes(modelKey)) return false; return true;}
/** List of model keys this account is currently allowed to call. */export function getAvailableModelsForAccount(account) { const tierModels = getTierModels(account.tier || 'unknown'); const blocked = new Set(account.blockedModels || []); return tierModels.filter(m => !blocked.has(m));}
/** * Set account status (active, disabled, error). */export function setAccountStatus(id, status) { const account = accounts.find(a => a.id === id); if (!account) return false; account.status = status; if (status === 'active') account.errorCount = 0; saveAccounts(); log.info(`Account ${id} status set to ${status}`); return true;}
/** * Reset error count for an account. */export function resetAccountErrors(id) { const account = accounts.find(a => a.id === id); if (!account) return false; account.errorCount = 0; account.status = 'active'; saveAccounts(); log.info(`Account ${id} errors reset`); return true;}
/** * Update account label. */export function updateAccountLabel(id, label) { const account = accounts.find(a => a.id === id); if (!account) return false; account.email = label; saveAccounts(); return true;}
/** * Persist tokens (apiKey / refreshToken / idToken) onto an account. * Fields with undefined are left unchanged. Always flushes to disk so the * rotation survives a restart even if the caller never saves explicitly. *//** * Manually force an account's tier. Used when automatic probing mis- * classifies an account — e.g. 14-day Pro trials whose planName doesn't * match our regex, or accounts whose initial probe was blocked by an * upstream bug and now carry a stale "free" tag even though the real * subscription is Pro. */export function setAccountTier(id, tier) { if (!['pro', 'free', 'unknown', 'expired'].includes(tier)) return false; const account = accounts.find(a => a.id === id); if (!account) return false; account.tier = tier; account.tierManual = true; saveAccounts(); log.info(`Account ${id} tier manually set to ${tier}`); return true;}
export function setAccountTokens(id, { apiKey, refreshToken, idToken } = {}) { const account = accounts.find(a => a.id === id); if (!account) return false; if (apiKey != null) account.apiKey = apiKey; if (refreshToken != null) account.refreshToken = refreshToken; if (idToken != null) account.idToken = idToken; saveAccounts(); return true;}
/** * Remove an account by ID. */export function removeAccount(id) { const idx = accounts.findIndex(a => a.id === id); if (idx === -1) return false; const account = accounts[idx]; accounts.splice(idx, 1); saveAccounts(); // Drop any Cascade conversations owned by this key so future requests // don't try to resume on an account that no longer exists. import('./conversation-pool.js').then(m => m.invalidateFor({ apiKey: account.apiKey })).catch(() => {}); log.info(`Account removed: ${id} (${account.email})`); return true;}
// ─── Account selection (tier-weighted RPM) ─────────────────
/** * Pick the next available account based on per-tier RPM headroom. * * Strategy: * 1. Keep only active, non-excluded, non-rate-limited accounts. * 2. Drop accounts whose 60s request count already equals their tier cap. * 3. Pick the account with the highest remaining-ratio (most idle). * 4. Record the selection timestamp on that account's sliding window. * * Returns null when every account is temporarily full — callers should * wait a moment and retry (see handlers/chat.js queue loop). */export function getApiKey(excludeKeys = [], modelKey = null) { const now = Date.now(); const candidates = []; for (const a of accounts) { if (a.status !== 'active') continue; if (excludeKeys.includes(a.apiKey)) continue; if (isRateLimitedForModel(a, modelKey, now)) continue; const limit = rpmLimitFor(a); if (limit <= 0) continue; // expired tier const used = pruneRpmHistory(a, now); if (used >= limit) continue; // Tier entitlement + per-account blocklist filter if (modelKey && !isModelAllowedForAccount(a, modelKey)) continue; candidates.push({ account: a, used, limit }); } if (candidates.length === 0) return null;
// Pick the account with the highest remaining ratio. Ties broken by // least-recently-used so a burst spreads across accounts evenly. candidates.sort((x, y) => { const rx = (x.limit - x.used) / x.limit; const ry = (y.limit - y.used) / y.limit; if (ry !== rx) return ry - rx; return (x.account.lastUsed || 0) - (y.account.lastUsed || 0); });
const { account } = candidates[0]; account._rpmHistory.push(now); account.lastUsed = now; return { id: account.id, email: account.email, apiKey: account.apiKey, apiServerUrl: account.apiServerUrl || '', proxy: getEffectiveProxy(account.id) || null, };}
/** * Try to re-check-out a specific account by apiKey, applying the same * rate-limit / status guards as getApiKey(). Used by the conversation pool * when a pool hit requires routing back to the exact account that owns the * upstream cascade_id — if that account is momentarily unavailable we fall * back to a fresh cascade on a different account instead of queuing. */export function acquireAccountByKey(apiKey, modelKey = null) { const now = Date.now(); const a = accounts.find(x => x.apiKey === apiKey); if (!a) return null; if (a.status !== 'active') return null; if (isRateLimitedForModel(a, modelKey, now)) return null; const limit = rpmLimitFor(a); if (limit <= 0) return null; const used = pruneRpmHistory(a, now); if (used >= limit) return null; if (modelKey && !isModelAllowedForAccount(a, modelKey)) return null; a._rpmHistory.push(now); a.lastUsed = now; return { id: a.id, email: a.email, apiKey: a.apiKey, apiServerUrl: a.apiServerUrl || '', proxy: getEffectiveProxy(a.id) || null, };}
/** * Snapshot of per-account RPM usage, for dashboard display. */export function getRpmStats() { const now = Date.now(); const out = {}; for (const a of accounts) { const limit = rpmLimitFor(a); const used = pruneRpmHistory(a, now); out[a.id] = { used, limit, tier: a.tier || 'unknown' }; } return out;}
/** * Ensure an LS instance exists for an account's proxy. * Used on startup and after adding new accounts so chat requests don't race * the first-time LS spawn. */export async function ensureLsForAccount(accountId) { const { ensureLs } = await import('./langserver.js'); const account = accounts.find(a => a.id === accountId); const proxy = getEffectiveProxy(accountId) || null; try { const ls = await ensureLs(proxy); // Pre-warm the Cascade workspace init so the first real request on this // LS doesn't pay the 3-roundtrip setup cost. Fire-and-forget — chat // requests still await the same Promise if it hasn't finished yet. if (ls && account?.apiKey) { const { WindsurfClient } = await import('./client.js'); const client = new WindsurfClient(account.apiKey, ls.port, ls.csrfToken); client.warmupCascade().catch(e => log.warn(`Cascade warmup failed: ${e.message}`)); } } catch (e) { log.error(`Failed to start LS for account ${accountId}: ${e.message}`); }}
/** * Mark an account as rate-limited for a duration (default 5 min). * When `modelKey` is provided, only that model is blocked on this account — * other models remain routable. When omitted, the entire account is blocked * (legacy behaviour, used by generic 429 responses). */export function markRateLimited(apiKey, durationMs = 5 * 60 * 1000, modelKey = null) { const account = accounts.find(a => a.apiKey === apiKey); if (!account) return; const until = Date.now() + durationMs; if (modelKey) { if (!account._modelRateLimits) account._modelRateLimits = {}; account._modelRateLimits[modelKey] = until; log.warn(`Account ${account.id} (${account.email}) rate-limited on ${modelKey} for ${Math.round(durationMs / 60000)} min`); } else { account.rateLimitedUntil = until; log.warn(`Account ${account.id} (${account.email}) rate-limited (all models) for ${Math.round(durationMs / 60000)} min`); }}
/** * Check if an account is rate-limited for a specific model. */function isRateLimitedForModel(account, modelKey, now) { // Global rate limit if (account.rateLimitedUntil && account.rateLimitedUntil > now) return true; // Per-model rate limit if (modelKey && account._modelRateLimits) { const until = account._modelRateLimits[modelKey]; if (until && until > now) return true; // Clean up expired entries if (until && until <= now) delete account._modelRateLimits[modelKey]; } return false;}
/** * Report an error for an API key (increment error count, auto-disable). */export function reportError(apiKey) { const account = accounts.find(a => a.apiKey === apiKey); if (!account) return; account.errorCount++; if (account.errorCount >= 3) { account.status = 'error'; log.warn(`Account ${account.id} (${account.email}) disabled after ${account.errorCount} errors`); }}
/** * Reset error count for an API key (call on success). */export function reportSuccess(apiKey) { const account = accounts.find(a => a.apiKey === apiKey); if (!account) return; if (account.errorCount > 0) { account.errorCount = 0; account.status = 'active'; } account.internalErrorStreak = 0;}
/** * Report an upstream "internal error occurred (error ID: ...)" from Windsurf. * These are account-specific backend errors — a given key will keep hitting * them until we stop using it. Quarantine the key for 5 minutes after 2 * consecutive hits so we stop burning user-visible retries on a dead key. */export function reportInternalError(apiKey) { const account = accounts.find(a => a.apiKey === apiKey); if (!account) return; account.internalErrorStreak = (account.internalErrorStreak || 0) + 1; if (account.internalErrorStreak >= 2) { account.rateLimitedUntil = Date.now() + 5 * 60 * 1000; log.warn(`Account ${account.id} (${account.email}) quarantined 5min after ${account.internalErrorStreak} consecutive upstream internal errors`); }}
// ─── Status ────────────────────────────────────────────────
/** * Check if every eligible account is currently rate-limited for a given model. * Returns { allLimited, retryAfterMs } — callers can use retryAfterMs to set * a Retry-After header for 429 responses. */export function isAllRateLimited(modelKey) { const now = Date.now(); let soonestExpiry = Infinity; let anyEligible = false; for (const a of accounts) { if (a.status !== 'active') continue; if (modelKey && !isModelAllowedForAccount(a, modelKey)) continue; anyEligible = true; if (!isRateLimitedForModel(a, modelKey, now)) return { allLimited: false }; // Track the soonest expiry across both global and per-model limits if (a.rateLimitedUntil && a.rateLimitedUntil > now) { soonestExpiry = Math.min(soonestExpiry, a.rateLimitedUntil); } if (modelKey && a._modelRateLimits?.[modelKey] > now) { soonestExpiry = Math.min(soonestExpiry, a._modelRateLimits[modelKey]); } } if (!anyEligible) return { allLimited: false }; const retryAfterMs = soonestExpiry === Infinity ? 60000 : Math.max(1000, soonestExpiry - now); return { allLimited: true, retryAfterMs };}
export function isAuthenticated() { return accounts.some(a => a.status === 'active');}
export function getAccountList() { const now = Date.now(); return accounts.map(a => { const rpmLimit = rpmLimitFor(a); const rpmUsed = pruneRpmHistory(a, now); return { id: a.id, email: a.email, method: a.method, status: a.status, errorCount: a.errorCount, lastUsed: a.lastUsed ? new Date(a.lastUsed).toISOString() : null, addedAt: new Date(a.addedAt).toISOString(), keyPrefix: a.apiKey.slice(0, 8) + '...', apiKey: a.apiKey, tier: a.tier || 'unknown', capabilities: a.capabilities || {}, lastProbed: a.lastProbed || 0, rateLimitedUntil: a.rateLimitedUntil || 0, rateLimited: !!(a.rateLimitedUntil && a.rateLimitedUntil > now), modelRateLimits: a._modelRateLimits ? Object.fromEntries( Object.entries(a._modelRateLimits).filter(([, v]) => v > now) ) : {}, rpmUsed, rpmLimit, credits: a.credits || null, blockedModels: a.blockedModels || [], availableModels: getAvailableModelsForAccount(a), tierModels: getTierModels(a.tier || 'unknown'), userStatus: a.userStatus || null, userStatusLastFetched: a.userStatusLastFetched || 0, }; });}
/** * Fetch live credit balance + plan info from server.codeium.com and stash it * on the account. Used by manual refresh and by the 15-minute background loop. * Errors are returned in-band so the dashboard can show them without throwing. */export async function refreshCredits(id) { const account = accounts.find(a => a.id === id); if (!account) return { ok: false, error: 'Account not found' }; try { const { getUserStatus } = await import('./windsurf-api.js'); const proxy = getEffectiveProxy(account.id) || null; const status = await getUserStatus(account.apiKey, proxy); // Drop the huge raw payload before persisting — keep it only in memory for // downstream callers (e.g. model catalog cache) to inspect once. const { raw, ...persist } = status; account.credits = persist; // Tier hint: if the plan info is explicit, prefer it over capability probing. // Trial / individual accounts also count as pro — Windsurf returns // "INDIVIDUAL" / "TRIAL" / similar for paid-tier trials (issue #8 follow-up: // motto1's 14-day Pro trial was misclassified as free because planName // wasn't "Pro"). const pn = status.planName || ''; if (/pro|teams|enterprise|trial|individual|premium|paid/i.test(pn)) { if (account.tier !== 'pro') account.tier = 'pro'; } else if (/free/i.test(pn)) { if (account.tier === 'unknown') account.tier = 'free'; } saveAccounts(); // Surface the raw response once so the caller can decide whether to mine // the bundled model catalog from it. return { ok: true, credits: persist, raw }; } catch (e) { const msg = e.message || String(e); log.warn(`refreshCredits ${id} failed: ${msg}`); // Stash the error on the account so the dashboard can show "last refresh // failed" without losing the previously successful snapshot. if (account.credits) account.credits.lastError = msg; else account.credits = { lastError: msg, fetchedAt: Date.now() }; return { ok: false, error: msg }; }}
export async function refreshAllCredits() { const results = []; for (const a of accounts) { if (a.status !== 'active') continue; const r = await refreshCredits(a.id); results.push({ id: a.id, email: a.email, ok: r.ok, error: r.error }); } return results;}
/** * Update the capability of an account for a specific model. * reason: 'success' | 'model_error' | 'rate_limit' | 'transport_error' */export function updateCapability(apiKey, modelKey, ok, reason = '') { const account = accounts.find(a => a.apiKey === apiKey); if (!account) return; if (!account.capabilities) account.capabilities = {}; // Don't overwrite a confirmed failure with a transient error if (reason === 'transport_error') return; // rate_limit is temporary — don't mark as permanently failed if (!ok && reason === 'rate_limit') return; account.capabilities[modelKey] = { ok, lastCheck: Date.now(), reason, }; account.tier = inferTier(account.capabilities); saveAccounts();}
/** * Infer subscription tier from which canary models work. Fallback only — * probeAccount prefers GetUserStatus which returns the authoritative tier. */function inferTier(caps) { const works = (m) => caps[m]?.ok === true; if (works('claude-opus-4.6') || works('claude-sonnet-4.6')) return 'pro'; if (works('gemini-2.5-flash') || works('gpt-4o-mini')) return 'free'; const checked = Object.keys(caps); if (checked.length > 0 && checked.every(m => caps[m].ok === false)) return 'expired'; return 'unknown';}
/** * Fetch authoritative user status from the LS → account fields. * Returns the parsed UserStatus object on success, null on failure. */export async function fetchUserStatus(id) { const account = accounts.find(a => a.id === id); if (!account) return null;
const { WindsurfClient } = await import('./client.js'); const { ensureLs, getLsFor } = await import('./langserver.js'); const proxy = getEffectiveProxy(account.id) || null; await ensureLs(proxy); const ls = getLsFor(proxy); if (!ls) { log.warn(`No LS for GetUserStatus on ${account.id}`); return null; }
const client = new WindsurfClient(account.apiKey, ls.port, ls.csrfToken); let status; try { status = await client.getUserStatus(); } catch (err) { log.warn(`GetUserStatus ${account.id} (${account.email}) failed: ${err.message}`); return null; }
// Apply to account — authoritative tier + entitlement snapshot. const prevTier = account.tier; account.tier = status.tierName; account.userStatus = { teamsTier: status.teamsTier, pro: status.pro, planName: status.planName, email: status.email, displayName: status.displayName, teamId: status.teamId, isTeams: status.isTeams, isEnterprise: status.isEnterprise, hasPaidFeatures: status.hasPaidFeatures, trialEndMs: status.trialEndMs, promptCreditsUsed: status.userUsedPromptCredits, flowCreditsUsed: status.userUsedFlowCredits, monthlyPromptCredits: status.monthlyPromptCredits, monthlyFlowCredits: status.monthlyFlowCredits, maxPremiumChatMessages: status.maxPremiumChatMessages, allowedModels: status.allowedModels, }; account.userStatusLastFetched = Date.now(); if (status.email && !account.email.includes('@')) account.email = status.email;
// Mark every cascade-allowed enum as capable; every catalog enum NOT in the // allowlist as not-entitled. Pure-UID models (no enum) are left to the // canary probe since the server returns allowlists by enum only. if (status.allowedModels.length > 0) { if (!account.capabilities) account.capabilities = {}; const allowedEnums = new Set(status.allowedModels.map(m => m.modelEnum).filter(e => e > 0)); for (const [key, info] of Object.entries(MODELS)) { if (!info.enumValue || info.enumValue <= 0) continue; if (allowedEnums.has(info.enumValue)) { account.capabilities[key] = { ok: true, lastCheck: Date.now(), reason: 'user_status' }; } else { const prev = account.capabilities[key]; if (!prev || prev.reason !== 'success') { // Respect a previously-validated success (can happen if allowlist is // cascade-only while the model was reached via legacy endpoint). account.capabilities[key] = { ok: false, lastCheck: Date.now(), reason: 'not_entitled' }; } } } }
if (prevTier !== account.tier) { log.info(`Tier change ${account.id} (${account.email}): ${prevTier} → ${account.tier} (plan="${status.planName}", ${status.allowedModels.length} allowed models)`); } else { log.info(`UserStatus ${account.id} (${account.email}): tier=${account.tier} plan="${status.planName}" allowed=${status.allowedModels.length}`); } saveAccounts(); return status;}
// Expanded canary set — one representative per routing path / provider family.// Order matters: free-tier models first so tier can be inferred early even if// later requests rate-limit. modelUid-only entries cover the 4.6 series since// GetUserStatus's allowlist is enum-keyed.const PROBE_CANARIES = [ 'gpt-4o-mini', 'gemini-2.5-flash', 'claude-sonnet-4.6', 'claude-opus-4.6', 'gemini-3.0-flash', 'claude-4.5-sonnet',];
/** * Probe an account's tier and model capabilities. * * Strategy (2026-04-21): * 1. GetUserStatus — authoritative tier + enum-keyed allowlist with credit * multipliers + trial end time + credit usage. One RPC, no quota burn. * 2. Canary probe — fills in capabilities for modelUid-only models (claude * 4.6 series etc.) which don't appear in the enum allowlist, and serves * as a fallback if GetUserStatus fails on this LS/account combo. */export async function probeAccount(id) { const account = accounts.find(a => a.id === id); if (!account) return null;
// ── Step 1: authoritative tier via GetUserStatus ── const status = await fetchUserStatus(id);
const { WindsurfClient } = await import('./client.js'); const { getModelInfo } = await import('./models.js'); const { ensureLs, getLsFor } = await import('./langserver.js');
const proxy = getEffectiveProxy(account.id) || null; await ensureLs(proxy); const ls = getLsFor(proxy); if (!ls) { log.error(`No LS available for account ${account.id}`); return null; } const port = ls.port; const csrf = ls.csrfToken;
// ── Step 2: canary probe, skipping models already classified by GetUserStatus ── // When allowlist is available we only need to probe UID-only models (no enum, // so server can't include them in allowlist) to get their actual status. const needsProbe = PROBE_CANARIES.filter(key => { const info = getModelInfo(key); if (!info) return false; // If GetUserStatus already gave us a definitive answer, skip. if (status && info.enumValue > 0) { const cap = account.capabilities?.[key]; if (cap && cap.reason === 'user_status') return false; if (cap && cap.reason === 'not_entitled') return false; } return true; });
if (needsProbe.length > 0) { log.info(`Probing account ${account.id} (${account.email}) across ${needsProbe.length} canary models (GetUserStatus ${status ? 'OK' : 'unavailable'})`);
for (const modelKey of needsProbe) { const info = getModelInfo(modelKey); if (!info) continue; const useCascade = !!info.modelUid; const client = new WindsurfClient(account.apiKey, port, csrf); try { if (useCascade) { await client.cascadeChat([{ role: 'user', content: 'hi' }], info.enumValue, info.modelUid); } else { await client.rawGetChatMessage([{ role: 'user', content: 'hi' }], info.enumValue, info.modelUid); } updateCapability(account.apiKey, modelKey, true, 'success'); log.info(` ${modelKey}: OK`); } catch (err) { const isRateLimit = /rate limit|rate_limit|too many requests|quota/i.test(err.message); if (isRateLimit) { log.info(` ${modelKey}: RATE_LIMITED (skipped)`); } else { updateCapability(account.apiKey, modelKey, false, 'model_error'); log.info(` ${modelKey}: FAIL (${err.message.slice(0, 80)})`); } } } }
// If GetUserStatus succeeded, its tier decision wins over the inferred one // (updateCapability rewrites tier via inferTier, so restore it afterwards). if (status) account.tier = status.tierName;
account.lastProbed = Date.now(); saveAccounts(); log.info(`Probe complete for ${account.id}: tier=${account.tier}${status ? ` plan="${status.planName}"` : ''}`); return { tier: account.tier, capabilities: account.capabilities };}
export function getAccountCount() { return { total: accounts.length, active: accounts.filter(a => a.status === 'active').length, error: accounts.filter(a => a.status === 'error').length, };}
// ─── Incoming request API key validation ───────────────────
export function validateApiKey(key) { if (!config.apiKey) return true; return key === config.apiKey;}
// ─── Firebase token refresh ──────────────────────────────────
/** * Refresh Firebase tokens for all accounts that have a stored refreshToken. * Re-registers with Codeium to get a fresh API key and updates the account. */async function refreshAllFirebaseTokens() { const { refreshFirebaseToken, reRegisterWithCodeium } = await import('./dashboard/windsurf-login.js'); for (const a of accounts) { if (a.status !== 'active' || !a.refreshToken) continue; try { const proxy = getEffectiveProxy(a.id) || null; const { idToken, refreshToken: newRefresh } = await refreshFirebaseToken(a.refreshToken, proxy); a.refreshToken = newRefresh; // Re-register to get a fresh API key (may be the same key) const { apiKey } = await reRegisterWithCodeium(idToken, proxy); if (apiKey && apiKey !== a.apiKey) { log.info(`Firebase refresh: ${a.email} got new API key`); a.apiKey = apiKey; } saveAccounts(); } catch (e) { log.warn(`Firebase refresh ${a.email} failed: ${e.message}`); } }}
// ─── Init from .env ────────────────────────────────────────
export async function initAuth() { // Load persisted accounts first loadAccounts();
const promises = [];
// Load API keys from env (comma-separated) if (config.codeiumApiKey) { for (const key of config.codeiumApiKey.split(',').map(k => k.trim()).filter(Boolean)) { addAccountByKey(key); } }
// Load auth tokens from env (comma-separated) if (config.codeiumAuthToken) { for (const token of config.codeiumAuthToken.split(',').map(t => t.trim()).filter(Boolean)) { promises.push( addAccountByToken(token).catch(err => log.error(`Token auth failed: ${err.message}`)) ); } }
// Note: email/password login removed (Firebase API key not valid for direct login) // Use token-based auth instead
if (promises.length > 0) await Promise.allSettled(promises);
// Periodic re-probe so tier/capability info doesn't drift as quotas reset. const REPROBE_INTERVAL = 6 * 60 * 60 * 1000; setInterval(async () => { for (const a of accounts) { if (a.status !== 'active') continue; try { await probeAccount(a.id); } catch (e) { log.warn(`Scheduled probe ${a.id} failed: ${e.message}`); } } }, REPROBE_INTERVAL).unref?.();
// Periodic credit refresh (every 15 min). First run is fire-and-forget so // startup isn't blocked by cloud round-trips. const CREDIT_INTERVAL = 15 * 60 * 1000; refreshAllCredits().catch(e => log.warn(`Initial credit refresh: ${e.message}`)); setInterval(() => { refreshAllCredits().catch(e => log.warn(`Scheduled credit refresh: ${e.message}`)); }, CREDIT_INTERVAL).unref?.();
// Fetch live model catalog from cloud and merge into hardcoded catalog. // Fire-and-forget — the hardcoded catalog is sufficient until this completes. fetchAndMergeModelCatalog().catch(e => log.warn(`Model catalog fetch: ${e.message}`));
// Periodic Firebase token refresh (every 50 min). Firebase ID tokens expire // after 60 min; refreshing at 50 keeps a comfortable margin. const hasRefreshTokens = accounts.some(a => !!a.refreshToken); if (hasRefreshTokens) { const TOKEN_REFRESH_INTERVAL = 50 * 60 * 1000; refreshAllFirebaseTokens().catch(e => log.warn(`Initial token refresh: ${e.message}`)); setInterval(() => { refreshAllFirebaseTokens().catch(e => log.warn(`Scheduled token refresh: ${e.message}`)); }, TOKEN_REFRESH_INTERVAL).unref?.(); }
// Warm up an LS instance for each account's configured proxy so the first // chat request doesn't pay the spawn cost. const { ensureLs } = await import('./langserver.js'); const uniqueProxies = new Map(); for (const a of accounts) { const p = getEffectiveProxy(a.id); const k = p ? `${p.host}:${p.port}` : 'default'; if (!uniqueProxies.has(k)) uniqueProxies.set(k, p || null); } for (const p of uniqueProxies.values()) { try { await ensureLs(p); } catch (e) { log.warn(`LS warmup failed: ${e.message}`); } }
const counts = getAccountCount(); if (counts.total > 0) { log.info(`Auth pool: ${counts.active} active, ${counts.error} error, ${counts.total} total`); } else { log.warn('No accounts configured. Add via POST /auth/login'); }}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283/** * Local response cache for chat completions. * * Cascade/Windsurf upstream does not expose Anthropic-style prompt caching, * so we add an in-memory, exact-match cache keyed on the normalized request * body. This only helps with duplicate requests (Claude Code retries, parallel * identical calls), not prefix-caching. */
import { createHash } from 'crypto';import { log } from './config.js';
const TTL_MS = 5 * 60 * 1000;const MAX_ENTRIES = 500;
// Map preserves insertion order → we evict the oldest when over capacity.const _store = new Map();const _stats = { hits: 0, misses: 0, stores: 0, evictions: 0 };
function normalize(body) { // Only the semantically meaningful fields — ignore stream flag, user id, etc. return { model: body.model || '', messages: body.messages || [], tools: body.tools || null, tool_choice: body.tool_choice || null, temperature: body.temperature ?? null, top_p: body.top_p ?? null, max_tokens: body.max_tokens ?? null, };}
export function cacheKey(body) { const json = JSON.stringify(normalize(body)); return createHash('sha256').update(json).digest('hex');}
export function cacheGet(key) { const entry = _store.get(key); if (!entry) { _stats.misses++; return null; } if (entry.expiresAt < Date.now()) { _store.delete(key); _stats.misses++; return null; } // Refresh LRU position _store.delete(key); _store.set(key, entry); _stats.hits++; return entry.value;}
export function cacheSet(key, value) { // Don't cache empty or partial results if (!value || (!value.text && !(value.chunks && value.chunks.length))) return; _store.set(key, { value, expiresAt: Date.now() + TTL_MS }); _stats.stores++; while (_store.size > MAX_ENTRIES) { const oldest = _store.keys().next().value; _store.delete(oldest); _stats.evictions++; }}
export function cacheStats() { const total = _stats.hits + _stats.misses; return { size: _store.size, maxSize: MAX_ENTRIES, ttlMs: TTL_MS, hits: _stats.hits, misses: _stats.misses, stores: _stats.stores, evictions: _stats.evictions, hitRate: total > 0 ? ((_stats.hits / total) * 100).toFixed(1) : '0.0', };}
export function cacheClear() { _store.clear(); _stats.hits = 0; _stats.misses = 0; _stats.stores = 0; _stats.evictions = 0; log.info('Response cache cleared');}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677/** * WindsurfClient — talks to the local language server binary via gRPC (HTTP/2). * * Two flows: * Legacy → RawGetChatMessage (streaming, for enum-only models) * Cascade → StartCascade → SendUserCascadeMessage → poll (for modelUid models) */
import https from 'https';import { randomUUID } from 'crypto';import { log } from './config.js';import { grpcFrame, grpcUnary, grpcStream } from './grpc.js';import { getLsEntryByPort } from './langserver.js';import { buildRawGetChatMessageRequest, parseRawResponse, buildInitializePanelStateRequest, buildAddTrackedWorkspaceRequest, buildUpdateWorkspaceTrustRequest, buildStartCascadeRequest, parseStartCascadeResponse, buildSendCascadeMessageRequest, buildGetTrajectoryRequest, parseTrajectoryStatus, buildGetTrajectoryStepsRequest, parseTrajectorySteps, buildGetGeneratorMetadataRequest, parseGeneratorMetadata, buildGetUserStatusRequest, parseGetUserStatusResponse,} from './windsurf.js';
const LS_SERVICE = '/exa.language_server_pb.LanguageServerService';
function contentToString(content) { if (typeof content === 'string') return content; if (Array.isArray(content)) { return content.map(p => (typeof p?.text === 'string' ? p.text : JSON.stringify(p))).join(''); } return content == null ? '' : JSON.stringify(content);}
// ─── WindsurfClient ────────────────────────────────────────
export class WindsurfClient { /** * @param {string} apiKey - Codeium API key * @param {number} port - Language server gRPC port * @param {string} csrfToken - CSRF token for auth */ constructor(apiKey, port, csrfToken) { this.apiKey = apiKey; this.port = port; this.csrfToken = csrfToken; }
// ─── Legacy: RawGetChatMessage (streaming) ───────────────
/** * Stream chat via RawGetChatMessage. * Used for models without a string UID (enum < 280 generally). * * @param {Array} messages - OpenAI-format messages * @param {number} modelEnum - Model enum value * @param {string} [modelName] - Optional model name * @param {object} opts - { onChunk, onEnd, onError } */ rawGetChatMessage(messages, modelEnum, modelName, opts = {}) { const { onChunk, onEnd, onError } = opts; const proto = buildRawGetChatMessageRequest(this.apiKey, messages, modelEnum, modelName); const body = grpcFrame(proto);
log.debug(`RawGetChatMessage: enum=${modelEnum} msgs=${messages.length}`);
return new Promise((resolve, reject) => { const chunks = [];
grpcStream(this.port, this.csrfToken, `${LS_SERVICE}/RawGetChatMessage`, body, { onData: (payload) => { try { const parsed = parseRawResponse(payload); if (parsed.text) { // Detect server-side errors returned as text const errMatch = /^(permission_denied|failed_precondition|not_found|unauthenticated):/.test(parsed.text.trim()); if (parsed.isError || errMatch) { const err = new Error(parsed.text.trim()); // Mark model-level errors so they don't count against the account err.isModelError = /permission_denied|failed_precondition/.test(parsed.text); reject(err); return; } chunks.push(parsed); onChunk?.(parsed); } } catch (e) { log.error('RawGetChatMessage parse error:', e.message); } }, onEnd: () => { onEnd?.(chunks); resolve(chunks); }, onError: (err) => { onError?.(err); reject(err); }, }); }); }
/** * Run (or wait for) the one-shot Cascade workspace init for this LS. * Idempotent — the LS entry caches the in-flight Promise so concurrent * callers share one init round. Safe to call from a startup warmup path * so the first real chat request skips these 3 gRPC round-trips. */ warmupCascade(force = false) { const lsEntry = getLsEntryByPort(this.port); if (!lsEntry) return Promise.resolve(); if (force) { lsEntry.workspaceInit = null; lsEntry.sessionId = randomUUID(); } if (!lsEntry.sessionId) lsEntry.sessionId = randomUUID(); if (lsEntry.workspaceInit) return lsEntry.workspaceInit;
const sessionId = lsEntry.sessionId; const workspacePath = '/tmp/windsurf-workspace'; const workspaceUri = 'file:///tmp/windsurf-workspace';
lsEntry.workspaceInit = (async () => { try { const initProto = buildInitializePanelStateRequest(this.apiKey, sessionId); await grpcUnary(this.port, this.csrfToken, `${LS_SERVICE}/InitializeCascadePanelState`, grpcFrame(initProto), 5000); } catch (e) { log.warn(`InitializeCascadePanelState: ${e.message}`); } try { const addWsProto = buildAddTrackedWorkspaceRequest(this.apiKey, workspacePath, sessionId); await grpcUnary(this.port, this.csrfToken, `${LS_SERVICE}/AddTrackedWorkspace`, grpcFrame(addWsProto), 5000); } catch (e) { log.warn(`AddTrackedWorkspace: ${e.message}`); } try { const trustProto = buildUpdateWorkspaceTrustRequest(this.apiKey, workspaceUri, true, sessionId); await grpcUnary(this.port, this.csrfToken, `${LS_SERVICE}/UpdateWorkspaceTrust`, grpcFrame(trustProto), 5000); } catch (e) { log.warn(`UpdateWorkspaceTrust: ${e.message}`); } log.info(`Cascade workspace init complete for LS port=${this.port}`); })().catch(e => { lsEntry.workspaceInit = null; throw e; }); return lsEntry.workspaceInit; }
// ─── Cascade flow ────────────────────────────────────────
/** * Chat via Cascade flow (for premium models with string UIDs). * * 1. StartCascade → cascade_id * 2. SendUserCascadeMessage (with model config) * 3. Poll GetCascadeTrajectorySteps until IDLE * * @param {Array} messages * @param {number} modelEnum * @param {string} modelUid * @param {object} opts - { onChunk, onEnd, onError } */ async cascadeChat(messages, modelEnum, modelUid, opts = {}) { const { onChunk, onEnd, onError, signal, reuseEntry, toolPreamble } = opts; const aborted = () => signal?.aborted; const inputChars = messages.reduce((n, m) => n + contentToString(m?.content).length, 0);
log.debug(`CascadeChat: uid=${modelUid} enum=${modelEnum} msgs=${messages.length} reuse=${!!reuseEntry}`);
// One-shot per-LS workspace init (idempotent; typically pre-warmed at // LS startup). Falls back to a local session id if the LS entry is gone. const lsEntry = getLsEntryByPort(this.port); await this.warmupCascade().catch(() => {}); let sessionId = reuseEntry?.sessionId || lsEntry?.sessionId || randomUUID();
// "panel state not found" means the LS forgot the panel for our sessionId // (LS restarted, TTL expired, etc.). Re-run warmupCascade with a fresh // sessionId and retry the handshake once. const isPanelMissing = (e) => /panel state not found|not_found.*panel/i.test(e?.message || '');
try { // Step 1: Start cascade — with retry on panel-state-not-found let cascadeId; const openCascade = async () => { if (reuseEntry?.cascadeId) { log.debug(`Cascade resumed: ${reuseEntry.cascadeId}`); return reuseEntry.cascadeId; } const startProto = buildStartCascadeRequest(this.apiKey, sessionId); const startResp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/StartCascade`, grpcFrame(startProto) ); const id = parseStartCascadeResponse(startResp); if (!id) throw new Error('StartCascade returned empty cascade_id'); log.debug(`Cascade started: ${id}`); return id; }; try { cascadeId = await openCascade(); } catch (e) { if (!isPanelMissing(e)) throw e; log.warn(`Panel state missing, re-warming LS port=${this.port}`); await this.warmupCascade(true).catch(() => {}); sessionId = getLsEntryByPort(this.port)?.sessionId || randomUUID(); if (reuseEntry) reuseEntry.cascadeId = null; // force StartCascade cascadeId = await openCascade(); }
// Build the text payload. Two cases: // - Resuming an existing cascade: the backend already has the prior // turns cached, so we only send the newest user message. // - Fresh cascade: we have to pack the entire history into one shot // (Cascade doesn't accept a messages array). System blocks go on // top, then we render u/a turns as a labeled transcript so the // model can see its own prior replies — previously we dropped // assistant turns entirely and multi-turn context was broken. // // The caller (handlers/chat.js) is responsible for any tool-protocol // preamble that needs to sit in front of the user text (client-defined // OpenAI tools are serialized into a '<tool_call>{...}</tool_call>' // emission contract there). This function just stitches system + u/a // turns into the single text payload Cascade accepts. let text; if (reuseEntry?.cascadeId) { const lastUser = [...messages].reverse().find(m => m.role === 'user'); text = lastUser ? contentToString(lastUser.content) : ''; } else { const systemMsgs = messages.filter(m => m.role === 'system'); const convo = messages.filter(m => m.role === 'user' || m.role === 'assistant'); const sysText = systemMsgs.map(m => contentToString(m.content)).join('\n').trim();
if (convo.length <= 1) { const last = convo[convo.length - 1]; text = last ? contentToString(last.content) : ''; } else { const lines = []; for (let i = 0; i < convo.length - 1; i++) { const m = convo[i]; const label = m.role === 'user' ? 'User' : 'Assistant'; lines.push(`${label}: ${contentToString(m.content)}`); } const latest = convo[convo.length - 1]; const latestText = latest ? contentToString(latest.content) : ''; text = `[Conversation so far]\n${lines.join('\n\n')}\n\n[Current user message]\n${latestText}`; } if (sysText) text = sysText + '\n\n' + text; }
// Step 2: Send message (retry once on panel-state-not-found) const sendMessage = async () => { const sendProto = buildSendCascadeMessageRequest(this.apiKey, cascadeId, text, modelEnum, modelUid, sessionId, { toolPreamble }); await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/SendUserCascadeMessage`, grpcFrame(sendProto) ); }; try { await sendMessage(); } catch (e) { if (!isPanelMissing(e)) throw e; log.warn(`Panel state missing on Send, re-warming + restarting cascade port=${this.port}`); await this.warmupCascade(true).catch(() => {}); sessionId = getLsEntryByPort(this.port)?.sessionId || randomUUID(); const startProto = buildStartCascadeRequest(this.apiKey, sessionId); const startResp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/StartCascade`, grpcFrame(startProto) ); cascadeId = parseStartCascadeResponse(startResp); if (!cascadeId) throw new Error('StartCascade returned empty cascade_id after re-warm'); await sendMessage(); }
// Step 3: Poll for response. // Track per-step text cursors instead of a single global `lastYielded`. // The cascade trajectory can contain MULTIPLE PLANNER_RESPONSE steps // (thinking step + final response, or multi-turn). The old single-cursor // code silently dropped any step whose text was shorter than the longest // step seen so far — which showed up as "30k in / 200 out" where the real // answer was split across two steps and only one was emitted. const chunks = []; const yieldedByStep = new Map(); // stepIndex → emitted text length const thinkingByStep = new Map(); // stepIndex → emitted thinking length // Server-reported token usage, one entry per step keyed by step index. // Each value is the latest {inputTokens, outputTokens, cacheReadTokens, // cacheWriteTokens} observed on that step's CortexStepMetadata.model_usage. // Summed across all steps at return time → the response's real usage. const usageByStep = new Map(); const seenToolCallIds = new Set(); const toolCalls = []; let totalYielded = 0; let totalThinking = 0; let idleCount = 0; let pollCount = 0; let sawActive = false; // true once we've seen a non-IDLE status let sawText = false; // true once at least one PLANNER_RESPONSE with text arrived let lastStatus = -1; // "Progress" is ANY forward motion on the trajectory — text, thinking, // new tool call, or a new step appearing. Using this (instead of text // alone) for stall detection fixes the false-positive warm stalls where // Cascade is legitimately mid-thinking but `responseText` hasn't moved. let lastGrowthAt = Date.now(); let lastStepCount = 0; const maxWait = 180_000; const pollInterval = 250; const IDLE_GRACE_MS = 8_000; // minimum time before idle-break allowed // 25s no progress on any signal = genuine stall. Was 15s + text-only, // which misfired on long thinking phases and returned tiny "Let me…" // preambles as if they were complete replies. const NO_GROWTH_STALL_MS = 25_000; const STALL_RETRY_MIN_TEXT = 300; // stalls shorter than this → retryable error, not partial success const startTime = Date.now(); let endReason = 'unknown';
while (Date.now() - startTime < maxWait) { if (aborted()) { endReason = 'aborted'; break; } await new Promise(r => setTimeout(r, pollInterval)); if (aborted()) { endReason = 'aborted'; break; } pollCount++;
// Get steps const stepsProto = buildGetTrajectoryStepsRequest(cascadeId, 0); const stepsResp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/GetCascadeTrajectorySteps`, grpcFrame(stepsProto) ); const steps = parseTrajectorySteps(stepsResp);
// CORTEX_STEP_TYPE_ERROR_MESSAGE = 17. An error step means the cascade // refused the request (permission denied, model unavailable, etc.) — // raise it as a model-level error so the account isn't blamed. for (const step of steps) { if (step.type === 17 && step.errorText) { // Log the full trajectory context so we can see WHICH tool call // (if any) the error refers to. "invalid tool call" without // context is useless for debugging. const trail = steps.map(s => ({ type: s.type, status: s.status, textLen: s.text?.length || 0, tools: (s.toolCalls || []).map(tc => tc.name).join(','), })); log.warn('Cascade error step', { errorText: step.errorText.trim(), trail }); const err = new Error(step.errorText.trim()); err.isModelError = true; throw err; } }
// Stall detection — two flavors: // (a) "cold stall": 30s+ ACTIVE but never saw any text or tool // call → planner is deadlocked before even starting to // produce output. Rotate account, don't make the user wait. // (b) "warm stall": we already streamed some text, but it hasn't // grown for 15s while status is still non-IDLE → planner is // stuck in a tool round-trip or upstream throttle. Accept // what we have as a complete response rather than waiting // out the full 180s maxWait with the client hanging. const elapsed = Date.now() - startTime; // Cap at maxWait (180s): long-context requests can legitimately take // that long to emit the first token from Cascade. Was 90s which // still tripped on very long prompts (issue #5). const coldStallMs = Math.min(maxWait, 30_000 + Math.floor(inputChars / 1500) * 5_000); if (elapsed > coldStallMs && sawActive && !sawText && seenToolCallIds.size === 0) { log.warn(`Cascade cold stall: ${elapsed}ms active without any text or tool call (threshold=${coldStallMs}ms, inputChars=${inputChars}), bailing`); endReason = 'stall_cold'; const err = new Error(`Cascade planner stalled — no output after ${Math.round(coldStallMs / 1000)}s`); err.isModelError = true; throw err; } if (sawText && lastStatus !== 1 && (Date.now() - lastGrowthAt) > NO_GROWTH_STALL_MS) { const diag = { msSinceGrowth: Date.now() - lastGrowthAt, textLen: totalYielded, thinkingLen: totalThinking, stepCount: yieldedByStep.size, toolCalls: seenToolCallIds.size, lastStatus, }; // Short-reply stall → treat as error so handlers/chat.js retries on // another account. A 50-char preamble is worse than no reply at all // because the client accepts it as "successful" and shows it to the // user. Retry only if we haven't streamed anything substantial yet // (if we did, partial delivery + idle end is fine). if (totalYielded < STALL_RETRY_MIN_TEXT) { log.warn('Cascade warm stall (short, retrying on next account)', diag); endReason = 'stall_warm_retry'; const err = new Error('Cascade planner stalled after preamble — no progress for 25s'); err.isModelError = true; throw err; } log.warn('Cascade warm stall (accepting partial)', diag); endReason = 'stall_warm'; break; // return what we have as a successful response }
// Any trajectory change counts as forward progress. A new step, a new // tool call proposal, or thinking growth all reset the stall timer so // Cascade's slow silent planning phases don't get cut off mid-think. if (steps.length > lastStepCount) { lastStepCount = steps.length; lastGrowthAt = Date.now(); }
for (let i = 0; i < steps.length; i++) { const step = steps[i];
// Per-step token usage. Overwrite on every poll so the map always // holds the latest reported numbers (they grow monotonically as // the generator emits more output). We sum across steps at the // end to compute the response's total usage. if (step.usage) usageByStep.set(i, step.usage);
// Collect tool calls — dedupe by id so the same step seen across // polls only emits once. A tool call with an existing `result` // means the LS already executed it (built-in Cascade tool); we // pass it through to the client for visibility. if (step.toolCalls && step.toolCalls.length) { for (const tc of step.toolCalls) { const key = tc.id || `${tc.name}:${tc.argumentsJson}`; if (seenToolCallIds.has(key)) continue; seenToolCallIds.add(key); toolCalls.push(tc); lastGrowthAt = Date.now(); } }
// Thinking delta: the LS keeps `thinking` as the cumulative // reasoning text for the step. Track a per-step cursor and emit // only the tail as reasoning_content. Crucially, thinking growth // *also* resets lastGrowthAt — prior code only watched response // text, so long silent thinking phases got falsely flagged as // stalls and 20% of Cascade requests came back as 50-char // preambles (`/tmp/...` style "let me analyze" stubs). const liveThink = step.thinking || ''; if (liveThink) { const prevThink = thinkingByStep.get(i) || 0; if (liveThink.length > prevThink) { const thinkDelta = liveThink.slice(prevThink); thinkingByStep.set(i, liveThink.length); totalThinking += thinkDelta.length; lastGrowthAt = Date.now(); const tchunk = { text: '', thinking: thinkDelta, isError: false }; chunks.push(tchunk); onChunk?.(tchunk); } }
// Text delta rule: prefer `responseText` (append-only stream) over // `modifiedText` (LS post-pass rewrite) while we're streaming. The // LS periodically swaps `response` → `modified_response` mid-turn // with slightly different wording; if we blindly `entry.text = // modifiedText || responseText` and take a length-based slice, the // rewritten middle bytes vanish because we already advanced the // cursor past them in an earlier poll. Using responseText keeps the // slice monotonic. At turn end we top up with `modifiedText` (see // below) so the final accumulated text is still the LS's polished // version when one exists. const liveText = step.responseText || step.text || ''; if (!liveText) continue; const prev = yieldedByStep.get(i) || 0; if (liveText.length > prev) { const delta = liveText.slice(prev); yieldedByStep.set(i, liveText.length); totalYielded += delta.length; lastGrowthAt = Date.now(); sawText = true; const chunk = { text: delta, thinking: '', isError: false }; chunks.push(chunk); onChunk?.(chunk); } }
// Check status const statusProto = buildGetTrajectoryRequest(cascadeId); const statusResp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/GetCascadeTrajectory`, grpcFrame(statusProto) ); const status = parseTrajectoryStatus(statusResp); lastStatus = status;
if (status !== 1) sawActive = true;
if (status === 1) { // IDLE // Don't allow idle-break during the warmup window unless we've // already seen the planner go non-IDLE at least once. Without this // guard, cascades whose trajectory hasn't kicked off yet (status // stuck at 1 for the first ~600ms) terminate after only 2 polls // and the client sees a near-empty reply. const elapsed = Date.now() - startTime; const graceOver = elapsed > IDLE_GRACE_MS; if (!sawActive && !graceOver) { continue; // still warming up — don't count this as idle } idleCount++; // Require at least a little text OR a long idle streak before // accepting "done", so we don't race the first visible chunk. const canBreak = sawText ? idleCount >= 2 : idleCount >= 4; if (canBreak) { // Final sweep const finalResp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/GetCascadeTrajectorySteps`, grpcFrame(stepsProto) ); const finalSteps = parseTrajectorySteps(finalResp); for (let i = 0; i < finalSteps.length; i++) { const step = finalSteps[i]; const responseText = step.responseText || ''; const modifiedText = step.modifiedText || ''; const prev = yieldedByStep.get(i) || 0;
// Normal top-up: responseText grew past what we streamed. if (responseText.length > prev) { const delta = responseText.slice(prev); yieldedByStep.set(i, responseText.length); totalYielded += delta.length; chunks.push({ text: delta, thinking: '', isError: false }); onChunk?.({ text: delta, thinking: '', isError: false }); }
// Modified-response top-up: only if it's a strict extension of // what we already emitted. If modifiedText rewrites the prefix // (common when LS polishes), emitting the tail would splice // wrong content onto the stream, so we skip it and keep the // raw responseText we already showed. const cursor = yieldedByStep.get(i) || 0; if (modifiedText.length > cursor && modifiedText.startsWith(responseText)) { const delta = modifiedText.slice(cursor); yieldedByStep.set(i, modifiedText.length); totalYielded += delta.length; chunks.push({ text: delta, thinking: '', isError: false }); onChunk?.({ text: delta, thinking: '', isError: false }); } } endReason = sawText ? 'idle_done' : 'idle_empty'; break; } } else { idleCount = 0; } } if (endReason === 'unknown') endReason = 'max_wait';
// Structured summary so we can diagnose short/empty completions after // the fact. sawActive=false + sawText=false + idle_empty = the planner // never actually ran on this cascade — likely an upstream starvation. const summary = { cascadeId: cascadeId.slice(0, 8), reason: endReason, polls: pollCount, textLen: totalYielded, thinkingLen: totalThinking, stepCount: Math.max(yieldedByStep.size, thinkingByStep.size, lastStepCount), toolCalls: seenToolCallIds.size, sawActive, sawText, lastStatus, ms: Date.now() - startTime, }; if (totalYielded < 20 && endReason !== 'aborted') { log.warn('Cascade short reply', summary); } else { log.info('Cascade done', summary); }
onEnd?.(chunks);
// ── Real token usage via GetCascadeTrajectoryGeneratorMetadata ── // CortexStepMetadata.model_usage (the per-step field) is usually empty // in the step trajectory response — the LS only populates the real // token counts in a separate RPC keyed off cascade_id. We fire this // once after the polling loop ends. Keep it non-fatal: a network blip // here just drops usage back to the chars/4 estimator, the response // itself is already formed. let serverUsage = null; try { const metaReq = buildGetGeneratorMetadataRequest(cascadeId, 0); const metaResp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/GetCascadeTrajectoryGeneratorMetadata`, grpcFrame(metaReq), 5000 ); serverUsage = parseGeneratorMetadata(metaResp); } catch (e) { log.debug(`GetCascadeTrajectoryGeneratorMetadata failed: ${e.message}`); } // Fallback: if the generator metadata RPC didn't give us anything, // check the per-step metadata we collected during polling (some LS // versions do populate CortexStepMetadata.model_usage directly). if (!serverUsage && usageByStep.size > 0) { let inT = 0, outT = 0, cacheR = 0, cacheW = 0; for (const u of usageByStep.values()) { inT += u.inputTokens || 0; outT += u.outputTokens || 0; cacheR += u.cacheReadTokens || 0; cacheW += u.cacheWriteTokens || 0; } if (inT || outT || cacheR || cacheW) { serverUsage = { inputTokens: inT, outputTokens: outT, cacheReadTokens: cacheR, cacheWriteTokens: cacheW, }; } }
// Attach cascade metadata so the caller can check it back into the // conversation pool. We still return the array so existing callers // that iterate over it keep working. chunks.cascadeId = cascadeId; chunks.sessionId = sessionId; chunks.toolCalls = toolCalls; chunks.usage = serverUsage; if (serverUsage) { log.info(`Cascade usage: in=${serverUsage.inputTokens} out=${serverUsage.outputTokens} cache_r=${serverUsage.cacheReadTokens} cache_w=${serverUsage.cacheWriteTokens}`); } if (toolCalls.length) log.info(`Cascade tool calls: ${toolCalls.length}`, { names: toolCalls.map(t => t.name) }); return chunks;
} catch (err) { onError?.(err); throw err; } }
// ─── Register user (JSON REST, unchanged) ────────────────
async registerUser(firebaseToken) { return new Promise((resolve, reject) => { const postData = JSON.stringify({ firebase_id_token: firebaseToken }); const req = https.request({ hostname: 'api.codeium.com', port: 443, path: '/register_user/', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData), }, }, (res) => { let raw = ''; res.on('data', d => raw += d); res.on('end', () => { try { const json = JSON.parse(raw); if (res.statusCode >= 400) { reject(new Error(`RegisterUser failed (${res.statusCode}): ${raw}`)); return; } if (!json.api_key) { reject(new Error(`RegisterUser response missing api_key: ${raw}`)); return; } resolve({ apiKey: json.api_key, name: json.name, apiServerUrl: json.api_server_url }); } catch { reject(new Error(`RegisterUser parse error: ${raw}`)); } }); res.on('error', reject); }); req.on('error', reject); req.write(postData); req.end(); }); }
// ── GetUserStatus ──────────────────────────────────────── // // One-shot RPC that returns the account's canonical tier + cascade // model allowlist + credit usage + trial end time. Replaces the // probe-based tier inference for accounts where this call succeeds. async getUserStatus() { const proto = buildGetUserStatusRequest(this.apiKey); const resp = await grpcUnary( this.port, this.csrfToken, `${LS_SERVICE}/GetUserStatus`, grpcFrame(proto), 10000, ); return parseGetUserStatusResponse(resp); }}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import { readFileSync, existsSync } from 'fs';import { resolve, dirname } from 'path';import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));const ROOT = resolve(__dirname, '..');
// Load .env file manually (zero dependencies)function loadEnv() { const envPath = resolve(ROOT, '.env'); if (!existsSync(envPath)) return; const content = readFileSync(envPath, 'utf-8'); for (const line of content.split('\n')) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith('#')) continue; const eqIdx = trimmed.indexOf('='); if (eqIdx === -1) continue; const key = trimmed.slice(0, eqIdx).trim(); let val = trimmed.slice(eqIdx + 1).trim(); if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) { val = val.slice(1, -1); } if (!process.env[key]) { process.env[key] = val; } }}
loadEnv();
export const config = { port: parseInt(process.env.PORT || '3003', 10), apiKey: process.env.API_KEY || '',
codeiumAuthToken: process.env.CODEIUM_AUTH_TOKEN || '', codeiumApiKey: process.env.CODEIUM_API_KEY || '', codeiumEmail: process.env.CODEIUM_EMAIL || '', codeiumPassword: process.env.CODEIUM_PASSWORD || '',
codeiumApiUrl: process.env.CODEIUM_API_URL || 'https://server.self-serve.windsurf.com', defaultModel: process.env.DEFAULT_MODEL || 'claude-4.5-sonnet-thinking', maxTokens: parseInt(process.env.MAX_TOKENS || '8192', 10), logLevel: process.env.LOG_LEVEL || 'info',
// Language server lsBinaryPath: process.env.LS_BINARY_PATH || '/opt/windsurf/language_server_linux_x64', lsPort: parseInt(process.env.LS_PORT || '42100', 10),
// Dashboard dashboardPassword: process.env.DASHBOARD_PASSWORD || '',};
const levels = { debug: 0, info: 1, warn: 2, error: 3 };const currentLevel = levels[config.logLevel] ?? 1;
export const log = { debug: (...args) => currentLevel <= 0 && console.log('[DEBUG]', ...args), info: (...args) => currentLevel <= 1 && console.log('[INFO]', ...args), warn: (...args) => currentLevel <= 2 && console.warn('[WARN]', ...args), error: (...args) => currentLevel <= 3 && console.error('[ERROR]', ...args),};123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145/** * Connect-RPC envelope framing and compression. * * Connect-RPC frame format: * [1 byte flags] [4 bytes big-endian length] [N bytes payload] * * Flags: * 0x01 = gzip compressed * 0x02 = end-of-stream (trailer frame, JSON payload) * 0x03 = compressed + end-of-stream * * IMPORTANT: Connect-RPC uses HTTP/1.1 POST, NOT HTTP/2 gRPC. * Content-Type: application/connect+proto */
import { gzipSync, gunzipSync } from 'zlib';
// ─── Compression helpers ───────────────────────────────────
export function gzip(buf) { return gzipSync(buf); }
export function gunzip(buf) { return gunzipSync(buf); }
export function tryGunzip(buf) { try { return gunzipSync(buf); } catch { return null; }}
// ─── Envelope wrapping ─────────────────────────────────────
/** * Wrap protobuf bytes in a Connect-RPC envelope frame. */export function wrapEnvelope(protoBuf, { compress = true } = {}) { let payload = protoBuf; let flags = 0; if (compress && payload.length > 0) { payload = gzipSync(payload); flags |= 0x01; } const frame = Buffer.alloc(5 + payload.length); frame[0] = flags; frame.writeUInt32BE(payload.length, 1); payload.copy(frame, 5); return frame;}
/** * Wrap a request for sending (single envelope, gzipped). */export function wrapRequest(protoBuf) { return wrapEnvelope(protoBuf, { compress: true });}
/** * Build the end-of-stream trailer frame (JSON {}). */export function endOfStreamEnvelope() { const trailer = Buffer.from('{}'); const frame = Buffer.alloc(5 + trailer.length); frame[0] = 0x02; // end-of-stream, not compressed frame.writeUInt32BE(trailer.length, 1); trailer.copy(frame, 5); return frame;}
// ─── Request unwrapping ────────────────────────────────────
/** * Unwrap a Connect-RPC request body → raw protobuf bytes. * Handles both envelope-wrapped and HTTP-level gzip. */export function unwrapRequest(body, headers = {}) { let buf = Buffer.isBuffer(body) ? body : Buffer.from(body);
// HTTP-level content-encoding gzip const encoding = headers['content-encoding'] || headers['connect-content-encoding'] || ''; if (encoding === 'gzip') { buf = gunzipSync(buf); }
// Check if it's envelope-wrapped (flags byte + 4-byte length) if (buf.length >= 5) { const flags = buf[0]; const len = buf.readUInt32BE(1); if (len === buf.length - 5 && (flags === 0 || flags === 1)) { let payload = buf.subarray(5); if (flags & 0x01) payload = gunzipSync(payload); return payload; } }
return buf;}
// ─── Streaming frame parser ───────────────────────────────
/** * Stateful parser that buffers incoming data and yields complete frames. */export class StreamingFrameParser { constructor() { this.buffer = Buffer.alloc(0); }
push(chunk) { this.buffer = Buffer.concat([this.buffer, chunk]); }
/** Drain all complete frames. Returns [{ flags, isEndStream, payload }]. */ drain() { const frames = []; while (this.buffer.length >= 5) { const len = this.buffer.readUInt32BE(1); if (this.buffer.length < 5 + len) break;
const flags = this.buffer[0]; let payload = this.buffer.subarray(5, 5 + len); if (flags & 0x01) { try { payload = gunzipSync(payload); } catch { this.buffer = this.buffer.subarray(5 + len); continue; } }
frames.push({ flags, isEndStream: !!(flags & 0x02), payload, }); this.buffer = this.buffer.subarray(5 + len); } return frames; }}
// ─── Connect-RPC headers ──────────────────────────────────
export function connectHeaders(extra = {}) { return { 'Content-Type': 'application/connect+proto', 'Connect-Protocol-Version': '1', 'Connect-Accept-Encoding': 'gzip', 'User-Agent': 'connect-es/2.0.0', ...extra, };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157/** * Cascade conversation reuse pool (experimental). * * Goal: when a multi-turn chat continues a previous exchange, reuse the same * Windsurf `cascade_id` instead of starting a fresh one. This lets the * Windsurf backend keep its own per-cascade context cached — we avoid * resending the full history on each turn and the server responds faster. * * The key is a "fingerprint" of the conversation up to (but not including) * the newest user message. A client sending [u1, a1, u2] looks up fp([u1, a1]); * a hit means we already drove the cascade to exactly that state. We then * `SendUserCascadeMessage(u2)` on the stored cascade_id and, on success, * re-store the entry under fp([u1, a1, u2, a2]) for the next turn. * * Safety rails: * - Entries are pinned to a specific (apiKey, lsPort) pair. We must reuse * the same LS and the same account or the cascade_id is meaningless. * - A checked-out entry is removed from the pool. Concurrent second request * with the same fingerprint falls back to a fresh cascade. * - TTL 10 min; LRU eviction at 500 entries. */
import { createHash } from 'crypto';
const POOL_TTL_MS = 10 * 60 * 1000;const POOL_MAX = 500;
// fingerprint -> { cascadeId, sessionId, lsPort, apiKey, createdAt, lastAccess }const _pool = new Map();
const stats = { hits: 0, misses: 0, stores: 0, evictions: 0, expired: 0 };
function sha256(s) { return createHash('sha256').update(s).digest('hex');}
/** * Canonicalise a message list for hashing. Strips anything that could drift * between turns (id, name, tool metadata) and normalises content to a * string so array/string forms collide correctly. */function canonicalise(messages) { return messages.map(m => ({ role: m.role, content: typeof m.content === 'string' ? m.content : Array.isArray(m.content) ? m.content.map(p => (typeof p?.text === 'string' ? p.text : JSON.stringify(p))).join('') : JSON.stringify(m.content ?? ''), }));}
/** * Fingerprint for "resume this conversation". Uses all messages except the * latest user turn, which is the one we're about to forward. * Returns null when there's nothing to resume (first turn or no prior * assistant reply). */export function fingerprintBefore(messages) { if (!Array.isArray(messages) || messages.length < 2) return null; // Must have at least one assistant turn in the history — otherwise the // previous "cascade" never actually existed from our side. const history = messages.slice(0, -1); if (!history.some(m => m.role === 'assistant')) return null; return sha256(JSON.stringify(canonicalise(history)));}
/** * Fingerprint for the full conversation after we append our assistant turn. * This is what the *next* request's `fingerprintBefore` will look up. */export function fingerprintAfter(messages, assistantText) { const full = [...messages, { role: 'assistant', content: assistantText || '' }]; return sha256(JSON.stringify(canonicalise(full)));}
function prune(now) { if (_pool.size <= POOL_MAX) return; // Drop oldest entries until back under the cap. const entries = [..._pool.entries()].sort((a, b) => a[1].lastAccess - b[1].lastAccess); const toDrop = entries.length - POOL_MAX; for (let i = 0; i < toDrop; i++) { _pool.delete(entries[i][0]); stats.evictions++; }}
/** * Check out a conversation if we have a matching fingerprint AND the caller * is willing to use the same (apiKey, lsPort) we stored. Removes the entry * from the pool — caller is expected to call `checkin()` with a new * fingerprint on success (or just drop it on failure and a fresh cascade * will be created next turn). */export function checkout(fingerprint) { if (!fingerprint) { stats.misses++; return null; } const entry = _pool.get(fingerprint); if (!entry) { stats.misses++; return null; } _pool.delete(fingerprint); if (Date.now() - entry.lastAccess > POOL_TTL_MS) { stats.expired++; return null; } stats.hits++; return entry;}
/** * Store (or restore) a conversation entry under a new fingerprint. */export function checkin(fingerprint, entry) { if (!fingerprint || !entry) return; const now = Date.now(); _pool.set(fingerprint, { cascadeId: entry.cascadeId, sessionId: entry.sessionId, lsPort: entry.lsPort, apiKey: entry.apiKey, createdAt: entry.createdAt || now, lastAccess: now, }); stats.stores++; prune(now);}
/** * Drop any entries that belong to a (apiKey, lsPort) pair that just went * away (account removed, LS restarted). Keeps the pool honest. */export function invalidateFor({ apiKey, lsPort }) { let dropped = 0; for (const [fp, e] of _pool) { if ((apiKey && e.apiKey === apiKey) || (lsPort && e.lsPort === lsPort)) { _pool.delete(fp); dropped++; } } return dropped;}
export function poolStats() { return { size: _pool.size, maxSize: POOL_MAX, ttlMs: POOL_TTL_MS, ...stats, hitRate: stats.hits + stats.misses > 0 ? ((stats.hits / (stats.hits + stats.misses)) * 100).toFixed(1) : '0.0', };}
export function poolClear() { const n = _pool.size; _pool.clear(); return n;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640/** * Dashboard API route handlers. * All routes are under /dashboard/api/*. */
import { config, log } from '../config.js';import { getAccountList, getAccountCount, addAccountByKey, addAccountByToken, removeAccount, setAccountStatus, resetAccountErrors, updateAccountLabel, isAuthenticated, probeAccount, ensureLsForAccount, refreshCredits, refreshAllCredits, setAccountBlockedModels, setAccountTokens, setAccountTier,} from '../auth.js';import { restartLsForProxy } from '../langserver.js';import { getLsStatus, stopLanguageServer, startLanguageServer, isLanguageServerRunning } from '../langserver.js';import { getStats, resetStats, recordRequest } from './stats.js';import { cacheStats, cacheClear } from '../cache.js';import { getExperimental, setExperimental, getIdentityPrompts, setIdentityPrompts, resetIdentityPrompt, DEFAULT_IDENTITY_PROMPTS } from '../runtime-config.js';import { poolStats as convPoolStats, poolClear as convPoolClear } from '../conversation-pool.js';import { getLogs, subscribeToLogs, unsubscribeFromLogs } from './logger.js';import { getProxyConfig, setGlobalProxy, setAccountProxy, removeProxy, getEffectiveProxy } from './proxy-config.js';import { MODELS, MODEL_TIER_ACCESS as _TIER_TABLE, getTierModels as _getTierModels } from '../models.js';import { windsurfLogin, refreshFirebaseToken, reRegisterWithCodeium } from './windsurf-login.js';import { getModelAccessConfig, setModelAccessMode, setModelAccessList, addModelToList, removeModelFromList } from './model-access.js';import { checkMessageRateLimit } from '../windsurf-api.js';
function json(res, status, body) { const data = JSON.stringify(body); res.writeHead(status, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, PATCH, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, X-Dashboard-Password', }); res.end(data);}
function checkAuth(req) { // Header is preferred (set by fetch). EventSource can't set custom headers, // so /logs/stream etc. also accept ?pwd=... as fallback. let pw = req.headers['x-dashboard-password'] || ''; if (!pw) { try { const qs = new URL(req.url, 'http://x').searchParams; pw = qs.get('pwd') || ''; } catch {} } if (config.dashboardPassword) return pw === config.dashboardPassword; if (config.apiKey) return pw === config.apiKey; return true; // No password and no API key = open access}
/** * Handle all /dashboard/api/* requests. */export async function handleDashboardApi(method, subpath, body, req, res) { if (method === 'OPTIONS') return json(res, 204, '');
// Auth check (except for auth verification endpoint) if (subpath !== '/auth' && !checkAuth(req)) { return json(res, 401, { error: 'Unauthorized. Set X-Dashboard-Password header.' }); }
// ─── Auth ───────────────────────────────────────────── if (subpath === '/auth') { const needsAuth = !!(config.dashboardPassword || config.apiKey); if (!needsAuth) return json(res, 200, { required: false }); return json(res, 200, { required: true, valid: checkAuth(req) }); }
// ─── Overview ───────────────────────────────────────── if (subpath === '/overview' && method === 'GET') { const stats = getStats(); return json(res, 200, { uptime: process.uptime(), startedAt: stats.startedAt, accounts: getAccountCount(), authenticated: isAuthenticated(), langServer: getLsStatus(), totalRequests: stats.totalRequests, successCount: stats.successCount, errorCount: stats.errorCount, successRate: stats.totalRequests > 0 ? ((stats.successCount / stats.totalRequests) * 100).toFixed(1) : '0.0', cache: cacheStats(), }); }
// ─── Experimental features ──────────────────────────── if (subpath === '/experimental' && method === 'GET') { return json(res, 200, { flags: getExperimental(), conversationPool: convPoolStats() }); } if (subpath === '/experimental' && method === 'PUT') { const flags = setExperimental(body || {}); // Dropping the toggle should also drop any live entries so nothing // resumes against a disabled feature on the next request. if (!flags.cascadeConversationReuse) convPoolClear(); return json(res, 200, { success: true, flags }); } if (subpath === '/experimental/conversation-pool' && method === 'DELETE') { const n = convPoolClear(); return json(res, 200, { success: true, cleared: n }); }
// ─── Identity prompts (per-provider editable templates) ─ if (subpath === '/identity-prompts' && method === 'GET') { return json(res, 200, { prompts: getIdentityPrompts(), defaults: DEFAULT_IDENTITY_PROMPTS, }); } if (subpath === '/identity-prompts' && method === 'PUT') { const prompts = setIdentityPrompts(body || {}); return json(res, 200, { success: true, prompts }); } if (subpath.match(/^\/identity-prompts\/[^/]+$/) && method === 'DELETE') { const provider = subpath.split('/').pop(); const prompts = resetIdentityPrompt(provider); return json(res, 200, { success: true, prompts }); }
// ─── Proxy test — try an HTTP CONNECT through the given proxy ── if (subpath === '/test-proxy' && method === 'POST') { const { host, port, username, password, type = 'http' } = body || {}; if (!host || !port) return json(res, 400, { ok: false, error: '缺少 host 或 port' }); const startTime = Date.now(); try { const result = await testProxy({ host, port: Number(port), username, password, type }); return json(res, 200, { ok: true, ...result, latencyMs: Date.now() - startTime }); } catch (err) { return json(res, 200, { ok: false, error: err.message, latencyMs: Date.now() - startTime }); } }
// ─── Self-update: pull latest code + restart PM2 ────── if (subpath === '/self-update/check' && method === 'GET') { try { const info = await gitStatus(); return json(res, 200, { ok: true, ...info }); } catch (err) { return json(res, 200, { ok: false, error: err.message }); } } if (subpath === '/self-update' && method === 'POST') { try { const before = await gitStatus(); // Guard: working tree must be clean (ignoring untracked files like // accounts.json, stats.json, runtime-config.json which live in the // repo root but aren't checked in). If the tracked files were edited // manually (or pushed via SFTP without a corresponding commit), // `git pull --ff-only` would refuse — surface a friendly error // instead of a raw git message. const dirty = (await runShell('git status --porcelain -uno')).trim(); if (dirty) { const allowForce = !!(body && body.forceReset); if (!allowForce) { return json(res, 200, { ok: false, dirty: true, error: '工作区有未提交的修改(SFTP 部署或手动改过代码)。确定要覆盖本地修改用远程最新版本吗?', dirtyFiles: dirty.split('\n').slice(0, 20), }); } await runShell(`git fetch origin ${before.branch || 'master'}`); await runShell(`git reset --hard origin/${before.branch || 'master'}`); } const pullCmd = `git pull origin ${before.branch || 'master'} --ff-only 2>&1`; const pull = dirty ? 'hard-reset applied' : await runShell(pullCmd); const after = await gitStatus(); const changed = before.commit !== after.commit; // Schedule process exit so PM2 auto-restarts us. This is far simpler // and port/env-agnostic compared to spawning update.sh (which hardcodes // PORT=3003 default). Requires PM2 autorestart: true (the default). if (changed) { setTimeout(() => { log.info('self-update: exiting for PM2 auto-restart'); process.exit(0); }, 800); } return json(res, 200, { ok: true, changed, before: before.commit, after: after.commit, pullOutput: pull.trim(), restarting: changed, }); } catch (err) { return json(res, 200, { ok: false, error: err.message }); } }
// ─── Cache ──────────────────────────────────────────── if (subpath === '/cache' && method === 'GET') { return json(res, 200, cacheStats()); } if (subpath === '/cache' && method === 'DELETE') { cacheClear(); return json(res, 200, { success: true }); }
// ─── Accounts ───────────────────────────────────────── if (subpath === '/accounts' && method === 'GET') { return json(res, 200, { accounts: getAccountList() }); }
if (subpath === '/accounts' && method === 'POST') { try { let account; if (body.api_key) { account = addAccountByKey(body.api_key, body.label); } else if (body.token) { account = await addAccountByToken(body.token, body.label); } else { return json(res, 400, { error: 'Provide api_key or token' }); } // Fire-and-forget probe so the UI gets tier info shortly after add probeAccount(account.id).catch(e => log.warn(`Auto-probe failed: ${e.message}`)); return json(res, 200, { success: true, account: { id: account.id, email: account.email, method: account.method, status: account.status }, ...getAccountCount(), }); } catch (err) { return json(res, 400, { error: err.message }); } }
// POST /accounts/probe-all — probe every active account if (subpath === '/accounts/probe-all' && method === 'POST') { const list = getAccountList().filter(a => a.status === 'active'); const results = []; for (const a of list) { try { const r = await probeAccount(a.id); results.push({ id: a.id, email: a.email, tier: r?.tier || 'unknown' }); } catch (err) { results.push({ id: a.id, email: a.email, error: err.message }); } } return json(res, 200, { success: true, results }); }
// POST /accounts/:id/probe — manually trigger capability probe const accountProbe = subpath.match(/^\/accounts\/([^/]+)\/probe$/); if (accountProbe && method === 'POST') { try { const result = await probeAccount(accountProbe[1]); if (!result) return json(res, 404, { error: 'Account not found' }); return json(res, 200, { success: true, ...result }); } catch (err) { return json(res, 500, { error: err.message }); } }
// POST /accounts/refresh-credits — refresh every active account's balance if (subpath === '/accounts/refresh-credits' && method === 'POST') { const results = await refreshAllCredits(); return json(res, 200, { success: true, results }); }
// POST /accounts/:id/refresh-credits — single-account refresh const creditRefresh = subpath.match(/^\/accounts\/([^/]+)\/refresh-credits$/); if (creditRefresh && method === 'POST') { const r = await refreshCredits(creditRefresh[1]); return json(res, r.ok ? 200 : 400, r); }
// PATCH /accounts/:id const accountPatch = subpath.match(/^\/accounts\/([^/]+)$/); if (accountPatch && method === 'PATCH') { const id = accountPatch[1]; if (body.status) setAccountStatus(id, body.status); if (body.label) updateAccountLabel(id, body.label); if (body.resetErrors) resetAccountErrors(id); if (Array.isArray(body.blockedModels)) setAccountBlockedModels(id, body.blockedModels); if (body.tier) setAccountTier(id, body.tier); return json(res, 200, { success: true }); }
// GET /tier-access — hardcoded FREE/PRO model entitlement tables. // The dashboard uses this to render the full per-account model grid // (every row in the tier's list is shown, blocked models are dimmed). if (subpath === '/tier-access' && method === 'GET') { return json(res, 200, { free: _TIER_TABLE.free, pro: _TIER_TABLE.pro, unknown: _TIER_TABLE.unknown, expired: _TIER_TABLE.expired, allModels: Object.keys(MODELS), }); }
// DELETE /accounts/:id const accountDel = subpath.match(/^\/accounts\/([^/]+)$/); if (accountDel && method === 'DELETE') { const ok = removeAccount(accountDel[1]); return json(res, ok ? 200 : 404, { success: ok }); }
// ─── Stats ──────────────────────────────────────────── if (subpath === '/stats' && method === 'GET') { return json(res, 200, getStats()); }
if (subpath === '/stats' && method === 'DELETE') { resetStats(); return json(res, 200, { success: true }); }
// ─── Logs ───────────────────────────────────────────── if (subpath === '/logs' && method === 'GET') { const url = new URL(req.url, 'http://localhost'); const since = parseInt(url.searchParams.get('since') || '0', 10); const level = url.searchParams.get('level') || null; return json(res, 200, { logs: getLogs(since, level) }); }
if (subpath === '/logs/stream' && method === 'GET') { req.socket.setKeepAlive(true); req.setTimeout(0); res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'X-Accel-Buffering': 'no', }); res.write('retry: 3000\n\n');
// Send existing logs first const existing = getLogs(); for (const entry of existing.slice(-50)) { res.write(`data: ${JSON.stringify(entry)}\n\n`); }
const heartbeat = setInterval(() => { if (!res.writableEnded) res.write(': heartbeat\n\n'); }, 15000);
const cb = (entry) => { if (!res.writableEnded) res.write(`data: ${JSON.stringify(entry)}\n\n`); }; subscribeToLogs(cb);
req.on('close', () => { clearInterval(heartbeat); unsubscribeFromLogs(cb); }); return; }
// ─── Proxy ──────────────────────────────────────────── if (subpath === '/proxy' && method === 'GET') { return json(res, 200, getProxyConfig()); }
if (subpath === '/proxy/global' && method === 'PUT') { setGlobalProxy(body); return json(res, 200, { success: true, config: getProxyConfig() }); }
if (subpath === '/proxy/global' && method === 'DELETE') { removeProxy('global'); return json(res, 200, { success: true }); }
const proxyAccount = subpath.match(/^\/proxy\/accounts\/([^/]+)$/); if (proxyAccount && method === 'PUT') { setAccountProxy(proxyAccount[1], body); // Spawn (or adopt) the LS instance for this proxy so chat routes immediately ensureLsForAccount(proxyAccount[1]).catch(e => log.warn(`LS ensure failed: ${e.message}`)); return json(res, 200, { success: true }); } if (proxyAccount && method === 'DELETE') { removeProxy('account', proxyAccount[1]); return json(res, 200, { success: true }); }
// ─── Config ─────────────────────────────────────────── if (subpath === '/config' && method === 'GET') { return json(res, 200, { port: config.port, defaultModel: config.defaultModel, maxTokens: config.maxTokens, logLevel: config.logLevel, lsBinaryPath: config.lsBinaryPath, lsPort: config.lsPort, codeiumApiUrl: config.codeiumApiUrl, hasApiKey: !!config.apiKey, hasDashboardPassword: !!config.dashboardPassword, }); }
// ─── Language Server ────────────────────────────────── if (subpath === '/langserver/restart' && method === 'POST') { if (!body.confirm) { return json(res, 400, { error: 'Send { confirm: true } to restart language server' }); } stopLanguageServer(); setTimeout(async () => { await startLanguageServer({ binaryPath: config.lsBinaryPath, port: config.lsPort, apiServerUrl: config.codeiumApiUrl, }); }, 2000); return json(res, 200, { success: true, message: 'Restarting language server...' }); }
// ─── Models list ────────────────────────────────────── if (subpath === '/models' && method === 'GET') { const models = Object.entries(MODELS).map(([id, info]) => ({ id, name: info.name, provider: info.provider, })); return json(res, 200, { models }); }
// ─── Model Access Control ────────────────────────────── if (subpath === '/model-access' && method === 'GET') { return json(res, 200, getModelAccessConfig()); }
if (subpath === '/model-access' && method === 'PUT') { if (body.mode) setModelAccessMode(body.mode); if (body.list) setModelAccessList(body.list); return json(res, 200, { success: true, config: getModelAccessConfig() }); }
if (subpath === '/model-access/add' && method === 'POST') { if (!body.model) return json(res, 400, { error: 'model is required' }); addModelToList(body.model); return json(res, 200, { success: true, config: getModelAccessConfig() }); }
if (subpath === '/model-access/remove' && method === 'POST') { if (!body.model) return json(res, 400, { error: 'model is required' }); removeModelFromList(body.model); return json(res, 200, { success: true, config: getModelAccessConfig() }); }
// ─── Windsurf Login ──────────────────────────────────── if (subpath === '/windsurf-login' && method === 'POST') { try { const { email, password, proxy: loginProxy, autoAdd } = body; if (!email || !password) return json(res, 400, { error: 'email 和 password 為必填' });
// Use provided proxy, or global proxy const proxy = loginProxy?.host ? loginProxy : getProxyConfig().global;
const result = await windsurfLogin(email, password, proxy);
// Auto-add to account pool if requested let account = null; if (autoAdd !== false) { account = addAccountByKey(result.apiKey, result.name || email); // Persist refresh token via the setter so it survives restart and // the background Firebase-renewal loop can find it. if (result.refreshToken) { setAccountTokens(account.id, { refreshToken: result.refreshToken, idToken: result.idToken }); } // Persist the per-account proxy we used for login so chat requests // also egress through the same IP, then warm up a matching LS. if (loginProxy?.host) setAccountProxy(account.id, loginProxy); ensureLsForAccount(account.id) .then(() => probeAccount(account.id)) .catch(e => log.warn(`Auto-probe failed: ${e.message}`)); }
return json(res, 200, { success: true, apiKey: result.apiKey, name: result.name, email: result.email, apiServerUrl: result.apiServerUrl, account: account ? { id: account.id, email: account.email, status: account.status } : null, }); } catch (err) { return json(res, 400, { error: err.message, isAuthFail: !!err.isAuthFail, firebaseCode: err.firebaseCode }); } }
// ─── OAuth login (Google / GitHub via Firebase) ──────── // POST /oauth-login — accepts Firebase idToken from client-side OAuth if (subpath === '/oauth-login' && method === 'POST') { try { const { idToken, refreshToken, email, provider, autoAdd } = body; if (!idToken) return json(res, 400, { error: '缺少 idToken' });
const proxy = getProxyConfig().global; const { apiKey, name } = await reRegisterWithCodeium(idToken, proxy);
let account = null; if (autoAdd !== false) { account = addAccountByKey(apiKey, name || email || provider || 'OAuth'); if (refreshToken) { setAccountTokens(account.id, { refreshToken, idToken }); } ensureLsForAccount(account.id) .then(() => probeAccount(account.id)) .catch(e => log.warn(`OAuth auto-probe failed: ${e.message}`)); }
return json(res, 200, { success: true, apiKey, name, email: email || '', account: account ? { id: account.id, email: account.email, status: account.status } : null, }); } catch (err) { return json(res, 400, { error: err.message }); } }
// ─── Rate Limit Check ────────────────────────────────── // POST /accounts/:id/rate-limit — check capacity for a single account const rateLimitCheck = subpath.match(/^\/accounts\/([^/]+)\/rate-limit$/); if (rateLimitCheck && method === 'POST') { const list = getAccountList(); const acct = list.find(a => a.id === rateLimitCheck[1]); if (!acct) return json(res, 404, { error: 'Account not found' }); try { const proxy = getEffectiveProxy(acct.id) || null; const result = await checkMessageRateLimit(acct.apiKey, proxy); return json(res, 200, { success: true, account: acct.email, ...result }); } catch (err) { return json(res, 500, { error: err.message }); } }
// ─── Firebase Token Refresh ─────────────────────────────── // POST /accounts/:id/refresh-token — manually refresh Firebase token const tokenRefresh = subpath.match(/^\/accounts\/([^/]+)\/refresh-token$/); if (tokenRefresh && method === 'POST') { const list = getAccountList(); const acct = list.find(a => a.id === tokenRefresh[1]); if (!acct) return json(res, 404, { error: 'Account not found' }); if (!acct.refreshToken) return json(res, 400, { error: 'Account has no refresh token' }); try { const proxy = getEffectiveProxy(acct.id) || null; const { idToken, refreshToken: newRefresh } = await refreshFirebaseToken(acct.refreshToken, proxy); const { apiKey } = await reRegisterWithCodeium(idToken, proxy); const keyChanged = apiKey && apiKey !== acct.apiKey; // Persist the fresh credentials back onto the account. Without this, the // in-memory apiKey stays on the now-stale value until the next server // restart — every subsequent request from this account will fail auth. setAccountTokens(acct.id, { apiKey: apiKey || acct.apiKey, refreshToken: newRefresh || acct.refreshToken, idToken }); return json(res, 200, { success: true, keyChanged, email: acct.email }); } catch (err) { return json(res, 400, { error: err.message }); } }
json(res, 404, { error: `Dashboard API: ${method} ${subpath} not found` });}
// ─── Proxy connectivity test ──────────────────────────────// HTTP CONNECT tunnel to api.ipify.org:443 → GET / → the returned IP is the// proxy's egress IP. Confirms the proxy works AND that auth is accepted.// ─── Self-update helpers ───────────────────────────────function runShell(cmd, opts = {}) { return new Promise((resolve, reject) => { import('node:child_process').then(({ exec }) => { exec(cmd, { timeout: 30_000, maxBuffer: 1024 * 1024, ...opts }, (err, stdout, stderr) => { if (err) return reject(new Error((stderr || err.message).toString().slice(0, 500))); resolve(stdout.toString()); }); }).catch(reject); });}
async function gitStatus() { const commit = (await runShell('git rev-parse HEAD')).trim(); const branch = (await runShell('git rev-parse --abbrev-ref HEAD')).trim(); let remote = ''; try { await runShell('git fetch --quiet origin'); remote = (await runShell(`git rev-parse origin/${branch}`)).trim(); } catch {} const localMsg = (await runShell('git log -1 --pretty=format:%s')).trim(); const behind = remote && remote !== commit; const remoteMsg = behind ? (await runShell(`git log -1 --pretty=format:%s ${remote}`).catch(() => '')).trim() : ''; return { commit: commit.slice(0, 7), commitFull: commit, branch, localMessage: localMsg, remoteCommit: remote ? remote.slice(0, 7) : '', remoteMessage: remoteMsg, behind, };}
async function testProxy({ host, port, username, password, type }) { const http = await import('node:http'); const tls = await import('node:tls'); return new Promise((resolve, reject) => { const targetHost = 'api.ipify.org'; const targetPort = 443; const authHeader = username ? { 'Proxy-Authorization': 'Basic ' + Buffer.from(`${username}:${password || ''}`).toString('base64') } : {}; const req = http.request({ host, port, method: 'CONNECT', path: `${targetHost}:${targetPort}`, headers: { Host: `${targetHost}:${targetPort}`, ...authHeader }, timeout: 10000, }); req.on('connect', (res, socket) => { if (res.statusCode !== 200) { socket.destroy(); return reject(new Error(`代理返回 HTTP ${res.statusCode}`)); } // Do a quick TLS handshake + GET to verify the tunnel actually works const tlsSock = tls.connect({ socket, servername: targetHost, rejectUnauthorized: false }, () => { tlsSock.write(`GET / HTTP/1.1\r\nHost: ${targetHost}\r\nConnection: close\r\nUser-Agent: WindsurfAPI/ProxyTest\r\n\r\n`); }); const chunks = []; tlsSock.on('data', c => chunks.push(c)); tlsSock.on('end', () => { const body = Buffer.concat(chunks).toString('utf-8'); const match = body.match(/\r\n\r\n([^\r\n]+)/); const ip = match ? match[1].trim() : ''; tlsSock.destroy(); if (!ip || !/^\d+\.\d+\.\d+\.\d+$/.test(ip)) { return reject(new Error('TLS 隧道建立但返回内容异常')); } resolve({ egressIp: ip, type }); }); tlsSock.on('error', (err) => reject(new Error(`TLS 失败: ${err.message}`))); }); req.on('error', (err) => reject(new Error(`连接失败: ${err.message}`))); req.on('timeout', () => { req.destroy(); reject(new Error('超时(10s)')); }); req.end(); });}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158/** * Structured logging with ring buffer, SSE, and on-disk JSONL persistence. * * Patches the primitive `log` object from config.js so every log call also: * 1. lands in an in-memory ring buffer (dashboard "recent logs") * 2. fans out to live SSE subscribers * 3. appends a structured JSONL line to logs/app.jsonl (daily-rotated) * 4. errors/warns also go to logs/error.jsonl * * Structured context: the last argument to log.*() may be a plain object. * It is stripped from the message and attached as `ctx`, so callers can do: * log.info('Chat request', { requestId, model, account: acct.email }); * and the dashboard can filter/group by ctx fields. */
import { mkdirSync, createWriteStream, existsSync } from 'fs';import { join } from 'path';import { randomUUID } from 'crypto';import { log } from '../config.js';
const MAX_BUFFER = 1000;const _buffer = [];const _subscribers = new Set();
const LOG_DIR = join(process.cwd(), 'logs');try { mkdirSync(LOG_DIR, { recursive: true }); } catch {}
// Rotate by UTC date. One stream per day, lazily recreated at midnight.let _appStream = null;let _errStream = null;let _streamDate = '';
function today() { const d = new Date(); return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, '0')}-${String(d.getUTCDate()).padStart(2, '0')}`;}
function getStreams() { const date = today(); if (date !== _streamDate) { try { _appStream?.end(); } catch {} try { _errStream?.end(); } catch {} _appStream = createWriteStream(join(LOG_DIR, `app-${date}.jsonl`), { flags: 'a' }); _errStream = createWriteStream(join(LOG_DIR, `error-${date}.jsonl`), { flags: 'a' }); _streamDate = date; } return { app: _appStream, err: _errStream };}
function formatArg(a) { if (typeof a === 'string') return a; if (a instanceof Error) return a.stack || a.message; try { return JSON.stringify(a); } catch { return String(a); }}
// Detect "context object": plain object, not array, not Error, reasonable size.function isCtx(x) { return x && typeof x === 'object' && !Array.isArray(x) && !(x instanceof Error) && Object.getPrototypeOf(x) === Object.prototype;}
// Save originals before patchingconst _orig = { debug: log.debug, info: log.info, warn: log.warn, error: log.error,};
for (const level of ['debug', 'info', 'warn', 'error']) { log[level] = (...args) => { // Pull trailing context object out of args. let ctx = null; if (args.length > 1 && isCtx(args[args.length - 1])) { ctx = args[args.length - 1]; args = args.slice(0, -1); } const msg = args.map(formatArg).join(' ');
const entry = { ts: Date.now(), level, msg }; if (ctx) entry.ctx = ctx;
_buffer.push(entry); if (_buffer.length > MAX_BUFFER) _buffer.shift();
for (const fn of _subscribers) { try { fn(entry); } catch {} }
// Persist to disk try { const { app, err } = getStreams(); const line = JSON.stringify(entry) + '\n'; app.write(line); if (level === 'error' || level === 'warn') err.write(line); } catch {}
// Also print to console so pm2 logs still work if (ctx) { const ctxStr = Object.entries(ctx) .map(([k, v]) => `${k}=${typeof v === 'string' ? v : JSON.stringify(v)}`) .join(' '); _orig[level](...args, ctxStr ? `{${ctxStr}}` : ''); } else { _orig[level](...args); } };}
/** * Return a logger bound to a fixed context (e.g. { requestId }). * Later args to .info/.warn/.error can still add more context fields. */export function withCtx(baseCtx) { const bind = (level) => (...args) => { let extra = null; if (args.length > 1 && isCtx(args[args.length - 1])) { extra = args[args.length - 1]; args = args.slice(0, -1); } log[level](...args, { ...baseCtx, ...(extra || {}) }); }; return { debug: bind('debug'), info: bind('info'), warn: bind('warn'), error: bind('error'), requestId: baseCtx.requestId, };}
/** Generate a short request id for tracing a single chat call end-to-end. */export function newRequestId() { return 'r_' + randomUUID().replace(/-/g, '').slice(0, 10);}
/** Get recent logs, optionally filtered by since/level/ctx. */export function getLogs(since = 0, level = null, ctxFilter = null) { let result = _buffer; if (since > 0) result = result.filter(e => e.ts > since); if (level) result = result.filter(e => e.level === level); if (ctxFilter && typeof ctxFilter === 'object') { result = result.filter(e => { if (!e.ctx) return false; for (const [k, v] of Object.entries(ctxFilter)) { if (e.ctx[k] !== v) return false; } return true; }); } return result;}
export function subscribeToLogs(callback) { _subscribers.add(callback); }export function unsubscribeFromLogs(callback) { _subscribers.delete(callback); }
/** Get current log directory (for dashboard to display). */export function getLogDir() { return LOG_DIR; }12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879/** * Model access control — allow/block specific models. * Persisted to model-access.json. */
import { readFileSync, writeFileSync, existsSync } from 'fs';import { join } from 'path';
const ACCESS_FILE = join(process.cwd(), 'model-access.json');
// mode: 'allowlist' (only listed models allowed) | 'blocklist' (listed models blocked) | 'all' (no restrictions)const _config = { mode: 'all', list: [], // model IDs in the list};
// Loadtry { if (existsSync(ACCESS_FILE)) { Object.assign(_config, JSON.parse(readFileSync(ACCESS_FILE, 'utf-8'))); }} catch {}
function save() { try { writeFileSync(ACCESS_FILE, JSON.stringify(_config, null, 2)); } catch {}}
export function getModelAccessConfig() { return { ..._config };}
export function setModelAccessMode(mode) { if (!['all', 'allowlist', 'blocklist'].includes(mode)) return; _config.mode = mode; save();}
export function setModelAccessList(list) { _config.list = Array.isArray(list) ? list : []; save();}
export function addModelToList(modelId) { if (!_config.list.includes(modelId)) { _config.list.push(modelId); save(); }}
export function removeModelFromList(modelId) { _config.list = _config.list.filter(m => m !== modelId); save();}
/** * Check if a model is allowed. * @returns {{ allowed: boolean, reason?: string }} */export function isModelAllowed(modelId) { if (_config.mode === 'all') return { allowed: true };
if (_config.mode === 'allowlist') { const allowed = _config.list.includes(modelId); return allowed ? { allowed: true } : { allowed: false, reason: `模型 ${modelId} 不在允許清單中` }; }
if (_config.mode === 'blocklist') { const blocked = _config.list.includes(modelId); return blocked ? { allowed: false, reason: `模型 ${modelId} 已被封鎖` } : { allowed: true }; }
return { allowed: true };}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576/** * Outbound proxy configuration manager. * Supports per-account and global HTTP proxy settings. */
import { readFileSync, writeFileSync, existsSync } from 'fs';import { join } from 'path';
const PROXY_FILE = join(process.cwd(), 'proxy.json');
const _config = { global: null, // { type, host, port, username, password } perAccount: {}, // { accountId: { type, host, port, username, password } }};
// Loadtry { if (existsSync(PROXY_FILE)) { Object.assign(_config, JSON.parse(readFileSync(PROXY_FILE, 'utf-8'))); }} catch {}
function save() { try { writeFileSync(PROXY_FILE, JSON.stringify(_config, null, 2)); } catch {}}
export function getProxyConfig() { return { ..._config };}
export function setGlobalProxy(cfg) { _config.global = cfg && cfg.host ? { type: cfg.type || 'http', host: cfg.host, port: parseInt(cfg.port, 10) || 8080, username: cfg.username || '', password: cfg.password || '', } : null; save();}
export function setAccountProxy(accountId, cfg) { if (cfg && cfg.host) { _config.perAccount[accountId] = { type: cfg.type || 'http', host: cfg.host, port: parseInt(cfg.port, 10) || 8080, username: cfg.username || '', password: cfg.password || '', }; } else { delete _config.perAccount[accountId]; } save();}
export function removeProxy(scope, accountId) { if (scope === 'global') { _config.global = null; } else if (scope === 'account' && accountId) { delete _config.perAccount[accountId]; } save();}
/** * Get effective proxy for an account (per-account takes priority over global). */export function getEffectiveProxy(accountId) { if (accountId && _config.perAccount[accountId]) { return _config.perAccount[accountId]; } return _config.global;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130/** * Request statistics collector with debounced JSON persistence. */
import { readFileSync, writeFileSync, existsSync } from 'fs';import { join } from 'path';
const STATS_FILE = join(process.cwd(), 'stats.json');
const _state = { startedAt: Date.now(), totalRequests: 0, successCount: 0, errorCount: 0, modelCounts: {}, // { "gpt-4o-mini": { requests, success, errors, totalMs } } accountCounts: {}, // { "abc123": { requests, success, errors } } hourlyBuckets: [], // [{ hour: "2026-04-09T07:00:00Z", requests, errors }]};
// Load persisted statstry { if (existsSync(STATS_FILE)) { const saved = JSON.parse(readFileSync(STATS_FILE, 'utf-8')); Object.assign(_state, saved); }} catch {}
// Debounced savelet _saveTimer = null;function scheduleSave() { clearTimeout(_saveTimer); _saveTimer = setTimeout(() => { try { writeFileSync(STATS_FILE, JSON.stringify(_state, null, 2)); } catch {} }, 5000);}
function getHourKey() { const d = new Date(); d.setMinutes(0, 0, 0); return d.toISOString();}
/** * Record a completed request. */export function recordRequest(model, success, durationMs, accountId) { _state.totalRequests++; if (success) _state.successCount++; else _state.errorCount++;
// Per-model stats (includes a small ring buffer for p50/p95 latency) if (!_state.modelCounts[model]) { _state.modelCounts[model] = { requests: 0, success: 0, errors: 0, totalMs: 0, recentMs: [] }; } const mc = _state.modelCounts[model]; mc.requests++; if (success) mc.success++; else mc.errors++; mc.totalMs += durationMs; if (!mc.recentMs) mc.recentMs = []; if (durationMs > 0) { mc.recentMs.push(durationMs); if (mc.recentMs.length > 200) mc.recentMs.shift(); }
// Per-account stats if (accountId) { const aid = typeof accountId === 'string' ? accountId.slice(0, 8) : String(accountId); if (!_state.accountCounts[aid]) { _state.accountCounts[aid] = { requests: 0, success: 0, errors: 0 }; } const ac = _state.accountCounts[aid]; ac.requests++; if (success) ac.success++; else ac.errors++; }
// Hourly bucket const hourKey = getHourKey(); let bucket = _state.hourlyBuckets.find(b => b.hour === hourKey); if (!bucket) { bucket = { hour: hourKey, requests: 0, errors: 0 }; _state.hourlyBuckets.push(bucket); // Keep last 72 hours if (_state.hourlyBuckets.length > 72) _state.hourlyBuckets.shift(); } bucket.requests++; if (!success) bucket.errors++;
scheduleSave();}
function percentile(sortedArr, p) { if (!sortedArr.length) return 0; const idx = Math.min(sortedArr.length - 1, Math.floor(sortedArr.length * p)); return sortedArr[idx];}
/** Get all stats, with computed latency percentiles per model. */export function getStats() { const out = { ..._state }; out.modelCounts = {}; for (const [m, s] of Object.entries(_state.modelCounts)) { const sorted = (s.recentMs || []).slice().sort((a, b) => a - b); out.modelCounts[m] = { requests: s.requests, success: s.success, errors: s.errors, totalMs: s.totalMs, avgMs: s.requests > 0 ? Math.round(s.totalMs / s.requests) : 0, p50Ms: Math.round(percentile(sorted, 0.5)), p95Ms: Math.round(percentile(sorted, 0.95)), }; } return out;}
/** Reset all stats. */export function resetStats() { _state.totalRequests = 0; _state.successCount = 0; _state.errorCount = 0; _state.modelCounts = {}; _state.accountCounts = {}; _state.hourlyBuckets = []; _state.startedAt = Date.now(); scheduleSave();}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294/** * Windsurf direct login — Firebase auth + Codeium registration. * Supports proxy tunneling and fingerprint randomization. */
import http from 'http';import https from 'https';import { log } from '../config.js';
const FIREBASE_API_KEY = 'AIzaSyDsOl-1XpT5err0Tcnx8FFod1H8gVGIycY';const FIREBASE_AUTH_URL = `https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${FIREBASE_API_KEY}`;const FIREBASE_REFRESH_URL = `https://securetoken.googleapis.com/v1/token?key=${FIREBASE_API_KEY}`;const CODEIUM_REGISTER_URL = 'https://api.codeium.com/register_user/';
// ─── Fingerprint randomization ────────────────────────────
const OS_VERSIONS = [ 'Windows NT 10.0; Win64; x64', 'Windows NT 10.0; WOW64', 'Macintosh; Intel Mac OS X 10_15_7', 'Macintosh; Intel Mac OS X 11_6_0', 'Macintosh; Intel Mac OS X 12_3_1', 'Macintosh; Intel Mac OS X 13_4_1', 'Macintosh; Intel Mac OS X 14_2_1', 'X11; Linux x86_64', 'X11; Ubuntu; Linux x86_64',];
const CHROME_VERSIONS = [ '120.0.0.0', '121.0.0.0', '122.0.0.0', '123.0.0.0', '124.0.0.0', '125.0.0.0', '126.0.0.0', '127.0.0.0', '128.0.0.0', '129.0.0.0', '130.0.0.0', '131.0.0.0', '132.0.0.0', '133.0.0.0', '134.0.0.0',];
const ACCEPT_LANGUAGES = [ 'en-US,en;q=0.9', 'en-GB,en;q=0.9', 'zh-TW,zh;q=0.9,en;q=0.8', 'zh-CN,zh;q=0.9,en;q=0.8', 'ja,en-US;q=0.9,en;q=0.8', 'ko,en-US;q=0.9,en;q=0.8', 'de,en-US;q=0.9,en;q=0.8', 'fr,en-US;q=0.9,en;q=0.8', 'es,en-US;q=0.9,en;q=0.8', 'pt-BR,pt;q=0.9,en;q=0.8',];
function pick(arr) { return arr[Math.floor(Math.random() * arr.length)]; }
function generateFingerprint() { const os = pick(OS_VERSIONS); const chromeVer = pick(CHROME_VERSIONS); const major = chromeVer.split('.')[0]; const ua = `Mozilla/5.0 (${os}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${chromeVer} Safari/537.36`;
return { 'User-Agent': ua, 'Accept-Language': pick(ACCEPT_LANGUAGES), 'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'identity', 'sec-ch-ua': `"Chromium";v="${major}", "Google Chrome";v="${major}", "Not-A.Brand";v="99"`, 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': os.includes('Windows') ? '"Windows"' : os.includes('Mac') ? '"macOS"' : '"Linux"', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'cross-site', 'Origin': 'https://windsurf.com', 'Referer': 'https://windsurf.com/', };}
// ─── Proxy tunnel (HTTP CONNECT) ──────────────────────────
function createProxyTunnel(proxy, targetHost, targetPort) { return new Promise((resolve, reject) => { const proxyHost = proxy.host.replace(/:\d+$/, ''); const proxyPort = proxy.port || 8080;
const authHeader = proxy.username ? `Proxy-Authorization: Basic ${Buffer.from(`${proxy.username}:${proxy.password || ''}`).toString('base64')}\r\n` : '';
const connectReq = http.request({ host: proxyHost, port: proxyPort, method: 'CONNECT', path: `${targetHost}:${targetPort}`, headers: { Host: `${targetHost}:${targetPort}`, ...(proxy.username ? { 'Proxy-Authorization': `Basic ${Buffer.from(`${proxy.username}:${proxy.password || ''}`).toString('base64')}` } : {}), }, });
connectReq.on('connect', (res, socket) => { if (res.statusCode === 200) { resolve(socket); } else { socket.destroy(); reject(new Error(`Proxy CONNECT failed: ${res.statusCode}`)); } });
connectReq.on('error', (err) => reject(new Error(`Proxy connection error: ${err.message}`))); connectReq.setTimeout(15000, () => { connectReq.destroy(); reject(new Error('Proxy connection timeout')); }); connectReq.end(); });}
// ─── HTTPS request with optional proxy ────────────────────
function httpsRequest(url, opts, postData, proxy) { return new Promise(async (resolve, reject) => { const parsed = new URL(url); const requestOpts = { hostname: parsed.hostname, port: 443, path: parsed.pathname + parsed.search, method: opts.method || 'POST', headers: opts.headers || {}, };
const handleResponse = (res) => { const bufs = []; res.on('data', d => bufs.push(d)); res.on('end', () => { const raw = Buffer.concat(bufs).toString('utf8'); try { resolve({ status: res.statusCode, data: JSON.parse(raw) }); } catch { reject(new Error(`Parse error (status ${res.statusCode}, encoding ${res.headers['content-encoding'] || 'identity'}): ${raw.slice(0, 200)}`)); } }); res.on('error', reject); };
try { let req; if (proxy && proxy.host) { const socket = await createProxyTunnel(proxy, parsed.hostname, 443); requestOpts.socket = socket; requestOpts.agent = false; req = https.request(requestOpts, handleResponse); } else { req = https.request(requestOpts, handleResponse); }
req.on('error', (err) => reject(new Error(`Request error: ${err.message}`))); req.setTimeout(30000, () => { req.destroy(); reject(new Error('Request timeout')); }); if (postData) req.write(postData); req.end(); } catch (err) { reject(err); } });}
// ─── Login flow ───────────────────────────────────────────
/** * Full Windsurf login: Firebase auth → Codeium register → API key. * @param {string} email * @param {string} password * @param {object} [proxy] - { host, port, username, password } * @returns {{ apiKey, name, email, idToken }} */export async function windsurfLogin(email, password, proxy = null) { const fingerprint = generateFingerprint(); log.info(`Windsurf login: ${email} fp=${fingerprint['User-Agent'].slice(0, 40)}... proxy=${proxy?.host || 'none'}`);
// Step 1: Firebase sign in const firebaseBody = JSON.stringify({ email, password, returnSecureToken: true, });
const fbHeaders = { ...fingerprint, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(firebaseBody), };
const fbRes = await httpsRequest(FIREBASE_AUTH_URL, { method: 'POST', headers: fbHeaders }, firebaseBody, proxy);
if (fbRes.data.error) { const msg = fbRes.data.error.message || 'Unknown Firebase error'; const oauthHint = '若你用 Google/GitHub 注册的 Windsurf 账号 此处密码登录不适用 请用页面顶部的 Google / GitHub 登录按钮 或访问 https://windsurf.com/show-auth-token 复制 Auth Token 后在「账号管理」页手动添加'; const friendly = { 'EMAIL_NOT_FOUND': `该邮箱未注册邮箱密码登录方式(${oauthHint})`, 'INVALID_PASSWORD': `密码错误(${oauthHint})`, 'INVALID_LOGIN_CREDENTIALS': `邮箱或密码错误(${oauthHint})`, 'USER_DISABLED': '账号已被停用', 'TOO_MANY_ATTEMPTS_TRY_LATER': '尝试太多次 请稍后再试', 'INVALID_EMAIL': '邮箱格式错误', }[msg] || msg; const err = new Error(`Firebase 登入失败: ${friendly}`); err.firebaseCode = msg; err.isAuthFail = ['EMAIL_NOT_FOUND', 'INVALID_PASSWORD', 'INVALID_LOGIN_CREDENTIALS'].includes(msg); throw err; }
const idToken = fbRes.data.idToken; if (!idToken) throw new Error('Firebase 回應缺少 idToken');
log.info(`Firebase login OK: ${email}, UID=${fbRes.data.localId}`);
// Step 2: Register with Codeium to get API key const regBody = JSON.stringify({ firebase_id_token: idToken }); const regHeaders = { ...fingerprint, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(regBody), };
const regRes = await httpsRequest(CODEIUM_REGISTER_URL, { method: 'POST', headers: regHeaders }, regBody, proxy);
if (regRes.status >= 400 || !regRes.data.api_key) { throw new Error(`Codeium 註冊失敗: ${JSON.stringify(regRes.data).slice(0, 200)}`); }
log.info(`Codeium register OK: ${email} → key=${regRes.data.api_key.slice(0, 12)}...`);
return { apiKey: regRes.data.api_key, name: regRes.data.name || email, email, idToken, refreshToken: fbRes.data.refreshToken || '', apiServerUrl: regRes.data.api_server_url || '', };}
/** * Refresh a Firebase ID token using a stored refresh token. * Returns a new { idToken, refreshToken, expiresIn } or throws. * * @param {string} refreshToken * @param {object} [proxy] * @returns {Promise<{idToken: string, refreshToken: string, expiresIn: number}>} */export async function refreshFirebaseToken(refreshToken, proxy = null) { if (!refreshToken) throw new Error('No refresh token available');
const postBody = `grant_type=refresh_token&refresh_token=${encodeURIComponent(refreshToken)}`; const headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postBody), 'Referer': 'https://windsurf.com/', 'Origin': 'https://windsurf.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/130.0.0.0 Safari/537.36', };
const res = await httpsRequest(FIREBASE_REFRESH_URL, { method: 'POST', headers }, postBody, proxy);
if (res.data?.error) { const msg = res.data.error.message || res.data.error.code || 'Unknown error'; throw new Error(`Firebase token refresh failed: ${msg}`); }
const newIdToken = res.data?.id_token || res.data?.idToken; const newRefreshToken = res.data?.refresh_token || res.data?.refreshToken || refreshToken; const expiresIn = parseInt(res.data?.expires_in || res.data?.expiresIn || '3600', 10);
if (!newIdToken) { throw new Error(`Firebase token refresh: no idToken in response: ${JSON.stringify(res.data).slice(0, 200)}`); }
log.info(`Firebase token refreshed, expires in ${expiresIn}s`); return { idToken: newIdToken, refreshToken: newRefreshToken, expiresIn };}
/** * Re-register with Codeium using a refreshed Firebase token. * Returns a fresh API key (may be the same key if unchanged). * * @param {string} idToken - fresh Firebase ID token * @param {object} [proxy] * @returns {Promise<{apiKey: string, name: string}>} */export async function reRegisterWithCodeium(idToken, proxy = null) { const fingerprint = generateFingerprint(); const regBody = JSON.stringify({ firebase_id_token: idToken }); const regHeaders = { ...fingerprint, 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(regBody), };
const regRes = await httpsRequest(CODEIUM_REGISTER_URL, { method: 'POST', headers: regHeaders }, regBody, proxy);
if (regRes.status >= 400 || !regRes.data.api_key) { throw new Error(`Codeium re-registration failed: ${JSON.stringify(regRes.data).slice(0, 200)}`); }
return { apiKey: regRes.data.api_key, name: regRes.data.name || '', };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199/** * HTTP/2 gRPC client for the local Windsurf language server binary. * * Uses Node.js built-in http2 module. No external dependencies. */
import http2 from 'http2';import { log } from './config.js';
/** * Wrap a protobuf payload in a gRPC frame. * Format: 1 byte compression (0) + 4 bytes BE length + payload */export function grpcFrame(payload) { const buf = Buffer.isBuffer(payload) ? payload : Buffer.from(payload); const frame = Buffer.alloc(5 + buf.length); frame[0] = 0; // no compression frame.writeUInt32BE(buf.length, 1); buf.copy(frame, 5); return frame;}
/** * Strip gRPC frame header (5 bytes) from a response buffer. * Returns the protobuf payload. */export function stripGrpcFrame(buf) { if (buf.length >= 5 && buf[0] === 0) { const msgLen = buf.readUInt32BE(1); if (buf.length >= 5 + msgLen) { return buf.subarray(5, 5 + msgLen); } } return buf;}
/** * Extract all gRPC frames from a buffer (may contain multiple concatenated frames). */export function extractGrpcFrames(buf) { const frames = []; let offset = 0; while (offset + 5 <= buf.length) { const compressed = buf[offset]; const msgLen = buf.readUInt32BE(offset + 1); if (compressed !== 0 || offset + 5 + msgLen > buf.length) break; frames.push(buf.subarray(offset + 5, offset + 5 + msgLen)); offset += 5 + msgLen; } return frames;}
/** * Make a unary gRPC call to the language server. * * @param {number} port - Language server port * @param {string} csrfToken - CSRF token * @param {string} path - gRPC path (e.g. /exa.language_server_pb.LanguageServerService/StartCascade) * @param {Buffer} body - gRPC-framed request * @param {number} timeout - Timeout in ms * @returns {Promise<Buffer>} Protobuf response (stripped of gRPC frame) */export function grpcUnary(port, csrfToken, path, body, timeout = 30000) { return new Promise((resolve, reject) => { const client = http2.connect(`http://localhost:${port}`); const chunks = []; let timer;
client.on('error', (err) => { clearTimeout(timer); client.close(); reject(err); });
timer = setTimeout(() => { client.close(); reject(new Error('gRPC unary timeout')); }, timeout);
const req = client.request({ ':method': 'POST', ':path': path, 'content-type': 'application/grpc', 'te': 'trailers', 'x-codeium-csrf-token': csrfToken, });
req.on('data', (chunk) => chunks.push(chunk));
let grpcStatus = '0', grpcMessage = '';
req.on('trailers', (trailers) => { grpcStatus = String(trailers['grpc-status'] ?? '0'); grpcMessage = String(trailers['grpc-message'] ?? ''); });
req.on('end', () => { clearTimeout(timer); client.close(); if (grpcStatus !== '0') { const msg = grpcMessage ? decodeURIComponent(grpcMessage) : `gRPC status ${grpcStatus}`; reject(new Error(msg)); return; } const full = Buffer.concat(chunks); resolve(stripGrpcFrame(full)); });
req.on('error', (err) => { clearTimeout(timer); client.close(); reject(err); });
req.write(body); req.end(); });}
/** * Make a streaming gRPC call to the language server. * Yields parsed gRPC frame payloads as they arrive. * * @param {number} port * @param {string} csrfToken * @param {string} path * @param {Buffer} body * @param {object} opts - { onData, onEnd, onError, timeout } */export function grpcStream(port, csrfToken, path, body, opts = {}) { const { onData, onEnd, onError, timeout = 300000 } = opts;
const client = http2.connect(`http://localhost:${port}`); let timer; let pendingBuf = Buffer.alloc(0);
client.on('error', (err) => { clearTimeout(timer); client.close(); onError?.(err); });
timer = setTimeout(() => { client.close(); onError?.(new Error('gRPC stream timeout')); }, timeout);
const req = client.request({ ':method': 'POST', ':path': path, 'content-type': 'application/grpc', 'te': 'trailers', 'x-codeium-csrf-token': csrfToken, });
req.on('data', (chunk) => { // Accumulate and parse gRPC frames pendingBuf = Buffer.concat([pendingBuf, chunk]);
while (pendingBuf.length >= 5) { const compressed = pendingBuf[0]; const msgLen = pendingBuf.readUInt32BE(1); if (pendingBuf.length < 5 + msgLen) break; // wait for more data
if (compressed === 0) { const payload = pendingBuf.subarray(5, 5 + msgLen); onData?.(payload); } pendingBuf = pendingBuf.subarray(5 + msgLen); } });
let grpcStatus = '0', grpcMessage = '';
req.on('trailers', (trailers) => { grpcStatus = String(trailers['grpc-status'] ?? '0'); grpcMessage = String(trailers['grpc-message'] ?? ''); });
req.on('end', () => { clearTimeout(timer); client.close(); if (grpcStatus !== '0') { const msg = grpcMessage ? decodeURIComponent(grpcMessage) : `gRPC status ${grpcStatus}`; onError?.(new Error(msg)); } else { onEnd?.(); } });
req.on('error', (err) => { clearTimeout(timer); client.close(); onError?.(err); });
req.write(body); req.end();}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806/** * POST /v1/chat/completions — OpenAI-compatible chat completions. * Routes to RawGetChatMessage (legacy) or Cascade (premium) based on model type. */
import { randomUUID } from 'crypto';import { WindsurfClient } from '../client.js';import { getApiKey, acquireAccountByKey, reportError, reportSuccess, markRateLimited, reportInternalError, updateCapability, getAccountList, isAllRateLimited } from '../auth.js';import { resolveModel, getModelInfo } from '../models.js';import { getLsFor, ensureLs } from '../langserver.js';import { config, log } from '../config.js';import { recordRequest } from '../dashboard/stats.js';import { isModelAllowed } from '../dashboard/model-access.js';import { cacheKey, cacheGet, cacheSet } from '../cache.js';import { isExperimentalEnabled, getIdentityPromptFor } from '../runtime-config.js';import { checkMessageRateLimit } from '../windsurf-api.js';import { getEffectiveProxy } from '../dashboard/proxy-config.js';import { fingerprintBefore, fingerprintAfter, checkout as poolCheckout, checkin as poolCheckin,} from '../conversation-pool.js';import { normalizeMessagesForCascade, ToolCallStreamParser, parseToolCallsFromText, buildToolPreambleForProto,} from './tool-emulation.js';import { sanitizeText, PathSanitizeStream } from '../sanitize.js';
const HEARTBEAT_MS = 15_000;const QUEUE_RETRY_MS = 1_000;const QUEUE_MAX_WAIT_MS = 30_000;
// ── Model identity prompt ──────────────────────────────────// Templates live in runtime-config (editable from the dashboard). Use {model}// as a placeholder for the requested model name. Only applied when the// experimental "modelIdentityPrompt" toggle is ON.function buildIdentitySystemMessage(displayModel, provider) { const template = getIdentityPromptFor(provider); if (!template) return null; return template.replace(/\{model\}/g, displayModel);}
function genId() { return 'chatcmpl-' + randomUUID().replace(/-/g, '').slice(0, 29);}
// Rough token estimate (~4 chars/token). Used only to populate the// OpenAI-compatible `usage.prompt_tokens_details.cached_tokens` field so// upstream billing/dashboards (new-api) can recognise our local cache hits.function estimateTokens(messages) { if (!Array.isArray(messages)) return 0; let chars = 0; for (const m of messages) { if (typeof m?.content === 'string') chars += m.content.length; else if (Array.isArray(m?.content)) { for (const p of m.content) if (typeof p?.text === 'string') chars += p.text.length; } } return Math.max(1, Math.ceil(chars / 4));}
function cachedUsage(messages, completionText) { const prompt = estimateTokens(messages); const completion = Math.max(1, Math.ceil((completionText || '').length / 4)); return { prompt_tokens: prompt, completion_tokens: completion, total_tokens: prompt + completion, input_tokens: prompt, output_tokens: completion, prompt_tokens_details: { cached_tokens: prompt }, completion_tokens_details: { reasoning_tokens: 0 }, cached: true, };}
/** * Build an OpenAI-shaped `usage` object, preferring server-reported token * counts from Cascade's CortexStepMetadata.model_usage when available, and * falling back to the local chars/4 estimate otherwise. Keeps the same shape * in both branches so downstream billing doesn't have to care which source * produced the numbers. * * The Cascade backend reports usage as {inputTokens, outputTokens, * cacheReadTokens, cacheWriteTokens}. We map them onto the OpenAI shape: * prompt_tokens = inputTokens + cacheReadTokens + cacheWriteTokens * (total input tokens the model processed, whether fresh, * cache-read, or cache-written — matches the OpenAI * convention where prompt_tokens is the grand total) * completion_tokens = outputTokens * prompt_tokens_details.cached_tokens = cacheReadTokens * cache_creation_input_tokens (Anthropic ext) = cacheWriteTokens */function buildUsageBody(serverUsage, messages, completionText, thinkingText = '') { if (serverUsage && (serverUsage.inputTokens || serverUsage.outputTokens)) { const inputTokens = serverUsage.inputTokens || 0; const outputTokens = serverUsage.outputTokens || 0; const cacheRead = serverUsage.cacheReadTokens || 0; const cacheWrite = serverUsage.cacheWriteTokens || 0; const promptTotal = inputTokens + cacheRead + cacheWrite; return { prompt_tokens: promptTotal, completion_tokens: outputTokens, total_tokens: promptTotal + outputTokens, input_tokens: promptTotal, output_tokens: outputTokens, prompt_tokens_details: { cached_tokens: cacheRead }, completion_tokens_details: { reasoning_tokens: 0 }, cache_creation_input_tokens: cacheWrite, }; } const prompt = estimateTokens(messages); const completion = Math.max(1, Math.ceil(((completionText || '').length + (thinkingText || '').length) / 4)); return { prompt_tokens: prompt, completion_tokens: completion, total_tokens: prompt + completion, input_tokens: prompt, output_tokens: completion, prompt_tokens_details: { cached_tokens: 0 }, completion_tokens_details: { reasoning_tokens: 0 }, };}
// Wait until getApiKey returns a non-null account, or until maxWaitMs expires.// Used when every account has momentarily exhausted its RPM budget so the// client is queued instead of getting a 503.async function waitForAccount(tried, signal, maxWaitMs = QUEUE_MAX_WAIT_MS, modelKey = null) { const deadline = Date.now() + maxWaitMs; let acct = getApiKey(tried, modelKey); while (!acct) { if (signal?.aborted) return null; if (Date.now() >= deadline) return null; await new Promise(r => setTimeout(r, QUEUE_RETRY_MS)); acct = getApiKey(tried, modelKey); } return acct;}
export async function handleChatCompletions(body) { const { model: reqModel, stream = false, max_tokens, tools, tool_choice, } = body; // `messages` is `let` not `const` so the identity-prompt injection below // can prepend a system turn for the legacy path too. let messages = body.messages;
const modelKey = resolveModel(reqModel || config.defaultModel); const modelInfo = getModelInfo(modelKey); const displayModel = modelInfo?.name || reqModel || config.defaultModel; const modelEnum = modelInfo?.enumValue || 0; const modelUid = modelInfo?.modelUid || null; // Models with a modelUid use the Cascade flow (StartCascade → SendUserCascadeMessage). // Legacy RawGetChatMessage only for models with enumValue>0 and NO modelUid. // Newer models (gemini-3.0, gpt-5.2, etc.) have both enumValue AND modelUid but // their high enum values cause "cannot parse invalid wire-format data" in the // legacy proto endpoint. Cascade handles them correctly via uid string. const useCascade = !!modelUid;
// Tool-call emulation: if the client passed OpenAI-style tools[], we rewrite // tool-result turns into synthetic user text and inject the tool protocol // at the system-prompt level via CascadeConversationalPlannerConfig's // tool_calling_section (SectionOverrideConfig, OVERRIDE mode). This is far // more reliable than user-message-level injection because NO_TOOL mode's // baked-in system prompt tells the model "you have no tools" — which // overpowers user-message preambles. The section override replaces that // section directly so the model sees our emulated tool definitions as // authoritative system instructions. const hasTools = Array.isArray(tools) && tools.length > 0; const hasToolHistory = Array.isArray(messages) && messages.some(m => m?.role === 'tool' || (m?.role === 'assistant' && Array.isArray(m.tool_calls) && m.tool_calls.length)); const emulateTools = useCascade && (hasTools || hasToolHistory); // Build proto-level preamble (goes into tool_calling_section override); // pass empty tools to normalizeMessagesForCascade so it only rewrites // role:tool / assistant.tool_calls messages without injecting a user-level // preamble (that's now handled at the proto layer). const toolPreamble = emulateTools ? buildToolPreambleForProto(tools || [], tool_choice) : ''; let cascadeMessages = emulateTools ? normalizeMessagesForCascade(messages, []) : [...messages];
// ── Model identity prompt injection ── // When enabled, prepend a system message so the model identifies itself as // the requested model (e.g. "I am Claude Opus 4.6") instead of leaking the // Cascade/Windsurf backend identity. Inject into BOTH messages (for legacy // RawGetChatMessage path) and cascadeMessages (Cascade path) — they diverge // once tool-emulation rewrites the Cascade path, but the system identity // should be identical in both. if (isExperimentalEnabled('modelIdentityPrompt') && modelInfo?.provider) { const identityText = buildIdentitySystemMessage(displayModel, modelInfo.provider); if (identityText) { const sysMsg = { role: 'system', content: identityText }; cascadeMessages = [sysMsg, ...cascadeMessages]; messages = [sysMsg, ...messages]; } }
// Global model access control (allowlist / blocklist from dashboard) const access = isModelAllowed(modelKey); if (!access.allowed) { return { status: 403, body: { error: { message: access.reason, type: 'model_blocked' } } }; }
// Per-account model routing preflight: if NO active account has this // model in its tier ∩ available list, fail fast instead of looping // through every account trying to find one. This surfaces tier // entitlement and blocklist errors as a clean 403 rather than a 30s // queue timeout → pool_exhausted. const anyEligible = getAccountList().some(a => a.status === 'active' && (a.availableModels || []).includes(modelKey) ); if (!anyEligible) { return { status: 403, body: { error: { message: `模型 ${displayModel} 在当前账号池中不可用(未订阅或已被封禁)`, type: 'model_not_entitled', }, }, }; }
const chatId = genId(); const created = Math.floor(Date.now() / 1000); const ckey = cacheKey(body);
if (stream) { return streamResponse(chatId, created, displayModel, modelKey, messages, cascadeMessages, modelEnum, modelUid, useCascade, ckey, emulateTools, toolPreamble); }
// ── Local response cache (exact body match) ───────────── const cached = cacheGet(ckey); if (cached) { log.info(`Chat: cache HIT model=${displayModel} flow=non-stream`); recordRequest(displayModel, true, 0, null); const message = { role: 'assistant', content: cached.text || null }; if (cached.thinking) message.reasoning_content = cached.thinking; return { status: 200, body: { id: chatId, object: 'chat.completion', created, model: displayModel, choices: [{ index: 0, message, finish_reason: 'stop' }], usage: cachedUsage(messages, cached.text), }, }; }
// ── Cascade conversation pool (experimental) ── // If the client is continuing a prior conversation and we still hold the // cascade_id from last turn, pin this request to that exact (account, LS) // pair so the Windsurf backend serves from its hot per-cascade context // instead of replaying the whole history. // // Tool-emulation mode bypasses the reuse pool: fingerprint can't stably // collapse a conversation whose assistant turns contain synthesised // <tool_call> markup and whose user turns contain <tool_result> wrappers. const reuseEnabled = useCascade && !emulateTools && isExperimentalEnabled('cascadeConversationReuse'); const fpBefore = reuseEnabled ? fingerprintBefore(messages) : null; let reuseEntry = reuseEnabled ? poolCheckout(fpBefore) : null; if (reuseEntry) log.info(`Chat: cascade reuse HIT cascadeId=${reuseEntry.cascadeId.slice(0, 8)}… model=${displayModel}`);
// Non-stream: retry with a different account on model-not-available errors const tried = []; let lastErr = null; // Dynamic: try every active account in the pool (capped at 10) so a // large pool with many rate-limited accounts can still fall through // to a free one. Was hardcoded 3 — in pools bigger than 3 with the // first accounts rate-limited, healthy accounts were never reached // even though they would have worked (issue #5). const maxAttempts = Math.min(10, Math.max(3, getAccountList().filter(a => a.status === 'active').length)); for (let attempt = 0; attempt < maxAttempts; attempt++) { let acct = null; if (reuseEntry && attempt === 0) { // First attempt pins to the account that owns the cached cascade. acct = acquireAccountByKey(reuseEntry.apiKey, modelKey); if (!acct) { log.info('Chat: cascade reuse skipped — owning account not available, falling back to fresh cascade'); reuseEntry = null; } } if (!acct) { acct = await waitForAccount(tried, null, QUEUE_MAX_WAIT_MS, modelKey); if (!acct) break; } tried.push(acct.apiKey);
// Pre-flight rate limit check (experimental): ask server.codeium.com if // this account still has message capacity before burning an LS round trip. if (isExperimentalEnabled('preflightRateLimit')) { try { const px = getEffectiveProxy(acct.id) || null; const rl = await checkMessageRateLimit(acct.apiKey, px); if (!rl.hasCapacity) { log.warn(`Preflight: ${acct.email} has no capacity (remaining=${rl.messagesRemaining}), skipping`); markRateLimited(acct.id, modelKey); continue; } } catch (e) { log.debug(`Preflight check failed for ${acct.email}: ${e.message}`); // Fail open — proceed with the request } }
await ensureLs(acct.proxy); const ls = getLsFor(acct.proxy); if (!ls) { lastErr = { status: 503, body: { error: { message: 'No LS instance available', type: 'ls_unavailable' } } }; break; } // Cascade pins cascade_id to a specific LS port too; if the LS it was // born on has been replaced, the cascade_id is dead. if (reuseEntry && reuseEntry.lsPort !== ls.port) { log.info('Chat: cascade reuse skipped — LS port changed'); reuseEntry = null; } const _msgChars = (messages || []).reduce((n, m) => { const c = m?.content; return n + (typeof c === 'string' ? c.length : Array.isArray(c) ? c.reduce((k, p) => k + (typeof p?.text === 'string' ? p.text.length : 0), 0) : 0); }, 0); log.info(`Chat: model=${displayModel} flow=${useCascade ? 'cascade' : 'legacy'} attempt=${attempt + 1} account=${acct.email} ls=${ls.port} turns=${(messages||[]).length} chars=${_msgChars}${reuseEntry ? ' reuse=1' : ''}${emulateTools ? ' tools=emu' : ''}`); const client = new WindsurfClient(acct.apiKey, ls.port, ls.csrfToken); const result = await nonStreamResponse( client, chatId, created, displayModel, modelKey, messages, cascadeMessages, modelEnum, modelUid, useCascade, acct.apiKey, ckey, reuseEnabled ? { reuseEntry, lsPort: ls.port, apiKey: acct.apiKey } : null, emulateTools, toolPreamble, ); if (result.status === 200) return result; reuseEntry = null; // don't try to reuse on the retry lastErr = result; const errType = result.body?.error?.type; // Rate limit: this account is done for this model, try the next one if (errType === 'rate_limit_exceeded') { log.warn(`Account ${acct.email} rate-limited on ${displayModel}, trying next account`); continue; } // Model not available on this account (permission_denied, etc.) if (errType === 'model_not_available') { log.warn(`Account ${acct.email} cannot serve ${displayModel}, trying next account`); continue; } break; // other errors (502, transport) — don't retry } // If all accounts exhausted, check if it's because they're all rate-limited if (!lastErr || lastErr.status === 429) { const rl = isAllRateLimited(modelKey); if (rl.allLimited) { return { status: 429, body: { error: { message: `${displayModel} 所有账号均已达速率限制,请 ${Math.ceil(rl.retryAfterMs / 1000)} 秒后重试`, type: 'rate_limit_exceeded', retry_after_ms: rl.retryAfterMs } } }; } } return lastErr || { status: 503, body: { error: { message: 'No active accounts available', type: 'pool_exhausted' } } };}
async function nonStreamResponse(client, id, created, model, modelKey, messages, cascadeMessages, modelEnum, modelUid, useCascade, apiKey, ckey, poolCtx, emulateTools, toolPreamble) { const startTime = Date.now(); try { let allText = ''; let allThinking = ''; let cascadeMeta = null; let toolCalls = []; // Server-reported token usage from CortexStepMetadata.model_usage, summed // across all trajectory steps. Preferred over the chars/4 estimate when // present so downstream billing (new-api, etc.) sees real Cascade numbers. let serverUsage = null;
if (useCascade) { const chunks = await client.cascadeChat(cascadeMessages, modelEnum, modelUid, { reuseEntry: poolCtx?.reuseEntry || null, toolPreamble }); for (const c of chunks) { if (c.text) allText += c.text; if (c.thinking) allThinking += c.thinking; } cascadeMeta = { cascadeId: chunks.cascadeId, sessionId: chunks.sessionId }; serverUsage = chunks.usage || null; // Always strip <tool_call>/<tool_result> blocks from Cascade text. // - emulateTools=true: parsed tool_calls become OpenAI-format tool_calls. // - emulateTools=false: blocks are silently discarded (defense-in-depth // against Cascade's system prompt inducing tool markup even after we // override tool_calling_section). { const parsed = parseToolCallsFromText(allText); allText = parsed.text; if (emulateTools) toolCalls = parsed.toolCalls; } // Built-in Cascade tool calls (chunks.toolCalls — edit_file, view_file, // list_directory, run_command, etc.) are intentionally DROPPED. Their // argumentsJson and result fields reference server-internal paths like // /tmp/windsurf-workspace/config.yaml and must never be exposed to an // API caller. Emulated tool calls (above) are safe because they // reference the caller's own tool schema. } else { const chunks = await client.rawGetChatMessage(messages, modelEnum, modelUid); for (const c of chunks) { if (c.text) allText += c.text; } }
// Scrub server-internal filesystem paths from everything we're about to // return. See src/sanitize.js for the patterns and rationale. allText = sanitizeText(allText); allThinking = sanitizeText(allThinking); if (toolCalls.length) { toolCalls = toolCalls.map(tc => ({ ...tc, argumentsJson: sanitizeText(tc.argumentsJson || ''), })); }
// Check the cascade back into the pool under the *post-turn* fingerprint // so the next request in the same conversation can resume it. if (poolCtx && cascadeMeta?.cascadeId && allText) { const fpAfter = fingerprintAfter(messages, allText); poolCheckin(fpAfter, { cascadeId: cascadeMeta.cascadeId, sessionId: cascadeMeta.sessionId, lsPort: poolCtx.lsPort, apiKey: poolCtx.apiKey, createdAt: poolCtx.reuseEntry?.createdAt, }); }
reportSuccess(apiKey); updateCapability(apiKey, modelKey, true, 'success'); recordRequest(model, true, Date.now() - startTime, apiKey);
// Store in cache for next identical request. Skip caching tool_call // responses — they're inherently contextual and the cache doesn't // preserve the tool_calls array, so a cache hit would return a // content-only response with finish_reason:stop, breaking tool flow. if (ckey && !toolCalls.length) cacheSet(ckey, { text: allText, thinking: allThinking });
const message = { role: 'assistant', content: allText || null }; if (allThinking) message.reasoning_content = allThinking; if (toolCalls.length) { message.tool_calls = toolCalls.map((tc, i) => ({ id: tc.id || `call_${i}_${Date.now().toString(36)}`, type: 'function', function: { name: tc.name || 'unknown', arguments: tc.argumentsJson || tc.arguments || '{}', }, })); // OpenAI convention: content is null when finish_reason is tool_calls. // In text emulation the model often emits an inline answer alongside the // <tool_call> block (e.g., hallucinated weather data). Set content to // null so clients that check `content !== null` behave correctly and the // caller waits for the real tool result rather than showing hallucinated // data. message.content = null; }
// Prefer server-reported usage; fall back to chars/4 estimate only when // the trajectory didn't include a ModelUsageStats field. const usage = buildUsageBody(serverUsage, messages, allText, allThinking); const finishReason = toolCalls.length ? 'tool_calls' : 'stop'; return { status: 200, body: { id, object: 'chat.completion', created, model, choices: [{ index: 0, message, finish_reason: finishReason }], usage, }, }; } catch (err) { // Only count true auth failures against the account. Workspace/cascade/model // errors and transport issues shouldn't disable the key. const isAuthFail = /unauthenticated|invalid api key|invalid_grant|permission_denied.*account/i.test(err.message); const isRateLimit = /rate limit|rate_limit|too many requests|quota/i.test(err.message); const isInternal = /internal error occurred.*error id/i.test(err.message); if (isAuthFail) reportError(apiKey); if (isRateLimit) { markRateLimited(apiKey, 5 * 60 * 1000, modelKey); err.isRateLimit = true; err.isModelError = true; } if (isInternal) { reportInternalError(apiKey); err.isModelError = true; } if (err.isModelError && !isRateLimit && !isInternal) { updateCapability(apiKey, modelKey, false, 'model_error'); } recordRequest(model, false, Date.now() - startTime, apiKey); log.error('Chat error:', err.message); // Rate limits → 429 with Retry-After; model errors → 403; others → 502 if (isRateLimit) { const rl = isAllRateLimited(modelKey); return { status: 429, body: { error: { message: `${model} 已达速率限制,请稍后重试`, type: 'rate_limit_exceeded', retry_after_ms: rl.retryAfterMs || 60000 } }, }; } return { status: err.isModelError ? 403 : 502, body: { error: { message: sanitizeText(err.message), type: err.isModelError ? 'model_not_available' : 'upstream_error' } }, }; }}
function streamResponse(id, created, model, modelKey, messages, cascadeMessages, modelEnum, modelUid, useCascade, ckey, emulateTools, toolPreamble) { return { status: 200, stream: true, headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'X-Accel-Buffering': 'no', }, async handler(res) { const abortController = new AbortController(); res.on('close', () => { if (!res.writableEnded) { log.info('Client disconnected mid-stream, aborting upstream'); abortController.abort(); } }); const send = (data) => { if (!res.writableEnded) res.write(`data: ${JSON.stringify(data)}\n\n`); };
// SSE heartbeat: keep the TCP/HTTP connection alive through any silent // period (LS warmup, Cascade "thinking", queue wait). `:` prefix is a // comment line per the SSE spec — clients ignore it, intermediaries see // bytes flowing, idle timers get reset. const heartbeat = setInterval(() => { if (!res.writableEnded) res.write(': ping\n\n'); }, HEARTBEAT_MS); const stopHeartbeat = () => clearInterval(heartbeat); res.on('close', stopHeartbeat);
// ── Cache hit: replay stored response as a fake stream ── const cached = cacheGet(ckey); if (cached) { log.info(`Chat: cache HIT model=${model} flow=stream`); recordRequest(model, true, 0, null); try { send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] }); if (cached.thinking) { send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { reasoning_content: cached.thinking }, finish_reason: null }] }); } if (cached.text) { send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { content: cached.text }, finish_reason: null }] }); } send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: {}, finish_reason: 'stop' }], usage: cachedUsage(messages, cached.text) }); if (!res.writableEnded) { res.write('data: [DONE]\n\n'); res.end(); } } finally { stopHeartbeat(); } return; }
const startTime = Date.now(); const tried = []; let hadSuccess = false; let rolePrinted = false; let currentApiKey = null; let lastErr = null; // Dynamic: try every active account in the pool (capped at 10) so a // large pool with many rate-limited accounts can still fall through // to a free one. Was hardcoded 3 — in pools bigger than 3 with the // first accounts rate-limited, healthy accounts were never reached // even though they would have worked (issue #5). const maxAttempts = Math.min(10, Math.max(3, getAccountList().filter(a => a.status === 'active').length));
// Accumulate chunks so we can cache a successful response at the end. let accText = ''; let accThinking = '';
// Cascade conversation pool (experimental, stream path) — bypassed in // tool-emulation mode because the fingerprint can't collapse turns // whose bodies carry <tool_call>/<tool_result> markup. const reuseEnabled = useCascade && !emulateTools && isExperimentalEnabled('cascadeConversationReuse'); const fpBefore = reuseEnabled ? fingerprintBefore(messages) : null; let reuseEntry = reuseEnabled ? poolCheckout(fpBefore) : null; if (reuseEntry) log.info(`Chat: cascade reuse HIT cascadeId=${reuseEntry.cascadeId.slice(0, 8)}… stream model=${model}`);
// Always strip <tool_call>/<tool_result> blocks in Cascade mode. // In emulation mode, parsed calls are emitted as OpenAI tool_calls. // In non-emulation mode, blocks are silently stripped (defense-in-depth // against Cascade's system prompt inducing tool markup). const toolParser = useCascade ? new ToolCallStreamParser() : null; const collectedToolCalls = [];
// Streaming path sanitizers. Every text/thinking delta flows through a // PathSanitizeStream before leaving the server so /tmp/windsurf-workspace, // /opt/windsurf and /root/WindsurfAPI literals can never slip out even // if a path straddles a chunk boundary. See src/sanitize.js. const pathStreamText = new PathSanitizeStream(); const pathStreamThinking = new PathSanitizeStream();
const emitContent = (clean) => { if (!clean) return; accText += clean; send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { content: clean }, finish_reason: null }] }); }; const emitThinking = (clean) => { if (!clean) return; accThinking += clean; send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { reasoning_content: clean }, finish_reason: null }] }); };
const emitToolCallDelta = (tc, idx) => { send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { tool_calls: [{ index: idx, id: tc.id, type: 'function', function: { name: tc.name, arguments: sanitizeText(tc.argumentsJson || '{}') }, }], }, finish_reason: null }] }); };
const onChunk = (chunk) => { if (!rolePrinted) { rolePrinted = true; send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] }); } hadSuccess = true;
if (chunk.text) { // Pipeline for text deltas: // raw chunk → ToolCallStreamParser (strip <tool_call> blocks) // → PathSanitizeStream (scrub server paths) // → client let safeText = chunk.text; if (toolParser) { const { text: safe, toolCalls: done } = toolParser.feed(chunk.text); safeText = safe; // Only emit tool_call deltas when emulating — otherwise the // parsed calls came from Cascade's built-in tools and are // silently discarded. if (emulateTools) { for (const tc of done) { const idx = collectedToolCalls.length; collectedToolCalls.push(tc); emitToolCallDelta(tc, idx); } } } if (safeText) emitContent(pathStreamText.feed(safeText)); } if (chunk.thinking) { emitThinking(pathStreamThinking.feed(chunk.thinking)); } };
try { for (let attempt = 0; attempt < maxAttempts; attempt++) { if (abortController.signal.aborted) return; let acct = null; if (reuseEntry && attempt === 0) { acct = acquireAccountByKey(reuseEntry.apiKey, modelKey); if (!acct) { log.info('Chat: cascade reuse skipped — owning account not available'); reuseEntry = null; } } if (!acct) { acct = await waitForAccount(tried, abortController.signal, QUEUE_MAX_WAIT_MS, modelKey); if (!acct) break; } tried.push(acct.apiKey); currentApiKey = acct.apiKey;
// Pre-flight rate limit check (experimental) if (isExperimentalEnabled('preflightRateLimit')) { try { const px = getEffectiveProxy(acct.id) || null; const rl = await checkMessageRateLimit(acct.apiKey, px); if (!rl.hasCapacity) { log.warn(`Preflight: ${acct.email} has no capacity (remaining=${rl.messagesRemaining}), skipping`); markRateLimited(acct.id, modelKey); continue; } } catch (e) { log.debug(`Preflight check failed for ${acct.email}: ${e.message}`); } }
try { await ensureLs(acct.proxy); } catch (e) { lastErr = e; break; } const ls = getLsFor(acct.proxy); if (!ls) { lastErr = new Error('No LS instance available'); break; } if (reuseEntry && reuseEntry.lsPort !== ls.port) { log.info('Chat: cascade reuse skipped — LS port changed'); reuseEntry = null; } const _msgCharsStream = (messages || []).reduce((n, m) => { const c = m?.content; return n + (typeof c === 'string' ? c.length : Array.isArray(c) ? c.reduce((k, p) => k + (typeof p?.text === 'string' ? p.text.length : 0), 0) : 0); }, 0); log.info(`Chat: model=${model} flow=${useCascade ? 'cascade' : 'legacy'} stream=true attempt=${attempt + 1} account=${acct.email} ls=${ls.port} turns=${(messages||[]).length} chars=${_msgCharsStream}${reuseEntry ? ' reuse=1' : ''}`); const client = new WindsurfClient(acct.apiKey, ls.port, ls.csrfToken); let cascadeResult = null; try { if (useCascade) { cascadeResult = await client.cascadeChat(cascadeMessages, modelEnum, modelUid, { onChunk, signal: abortController.signal, reuseEntry, toolPreamble, }); } else { await client.rawGetChatMessage(messages, modelEnum, modelUid, { onChunk }); } // Flush order matters: // 1. ToolCallStreamParser tail → may produce more text deltas // (e.g., a dangling <tool_call> that never closed falls // through as literal text) // 2. PathSanitizeStream tail (text) → scrubs anything the tool // parser held back AND anything we were holding ourselves // 3. PathSanitizeStream tail (thinking) if (toolParser) { const tail = toolParser.flush(); if (tail.text) emitContent(pathStreamText.feed(tail.text)); if (emulateTools) { for (const tc of tail.toolCalls) { const idx = collectedToolCalls.length; collectedToolCalls.push(tc); emitToolCallDelta(tc, idx); } } } emitContent(pathStreamText.flush()); emitThinking(pathStreamThinking.flush()); // Pool check-in on success (cascade only) if (reuseEnabled && cascadeResult?.cascadeId && accText) { const fpAfter = fingerprintAfter(messages, accText); poolCheckin(fpAfter, { cascadeId: cascadeResult.cascadeId, sessionId: cascadeResult.sessionId, lsPort: ls.port, apiKey: currentApiKey, createdAt: reuseEntry?.createdAt, }); } // success if (hadSuccess) reportSuccess(currentApiKey); updateCapability(currentApiKey, modelKey, true, 'success'); recordRequest(model, true, Date.now() - startTime, currentApiKey); if (!rolePrinted) { send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] }); } const finalReason = collectedToolCalls.length ? 'tool_calls' : 'stop'; const finalUsage = buildUsageBody(cascadeResult?.usage || null, messages, accText, accThinking); send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: {}, finish_reason: finalReason }], usage: finalUsage }); // OpenAI-compat: terminal usage chunk (stream_options.include_usage // convention — empty choices[] + usage). Prefer Cascade's own // CortexStepMetadata.model_usage numbers when present, fall back // to the local chars/4 estimator. See buildUsageBody(). { const usage = buildUsageBody(cascadeResult?.usage || null, messages, accText, accThinking); send({ id, object: 'chat.completion.chunk', created, model, choices: [], usage }); } if (!res.writableEnded) { res.write('data: [DONE]\n\n'); res.end(); } if (ckey && !collectedToolCalls.length && (accText || accThinking)) { cacheSet(ckey, { text: accText, thinking: accThinking }); } return; } catch (err) { lastErr = err; reuseEntry = null; // don't try to reuse on retry const isAuthFail = /unauthenticated|invalid api key|invalid_grant|permission_denied.*account/i.test(err.message); const isRateLimit = /rate limit|rate_limit|too many requests|quota/i.test(err.message); const isInternal = /internal error occurred.*error id/i.test(err.message); if (isAuthFail) reportError(currentApiKey); if (isRateLimit) { markRateLimited(currentApiKey, 5 * 60 * 1000, modelKey); err.isRateLimit = true; err.isModelError = true; } if (isInternal) { reportInternalError(currentApiKey); err.isModelError = true; } if (err.isModelError && !isRateLimit && !isInternal) { updateCapability(currentApiKey, modelKey, false, 'model_error'); } // Retry only if nothing has been streamed yet AND it's a retryable error if (!hadSuccess && (err.isModelError || isRateLimit)) { const tag = isRateLimit ? 'rate_limit' : isInternal ? 'internal_error' : 'model_error'; log.warn(`Account ${acct.email} failed (${tag}) on ${model}, trying next`); continue; } break; } }
// All attempts failed log.error('Stream error after retries:', lastErr?.message); recordRequest(model, false, Date.now() - startTime, currentApiKey); try { if (!rolePrinted) { send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { role: 'assistant', content: '' }, finish_reason: null }] }); } // Check if failure is due to all accounts being rate-limited const rl = isAllRateLimited(modelKey); const errMsg = rl.allLimited ? `${model} 所有账号均已达速率限制,请 ${Math.ceil(rl.retryAfterMs / 1000)} 秒后重试` : sanitizeText(lastErr?.message || 'no accounts'); send({ id, object: 'chat.completion.chunk', created, model, choices: [{ index: 0, delta: { content: `\n[Error: ${errMsg}]` }, finish_reason: 'stop' }] }); res.write('data: [DONE]\n\n'); } catch {} if (!res.writableEnded) res.end(); } finally { stopHeartbeat(); } }, };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431/** * POST /v1/messages — Anthropic Messages API compatibility layer. * * Translates Anthropic request/response format to/from the internal OpenAI * format so Claude Code and any Anthropic SDK client can connect directly. * * Streaming path is a real-time translator: it pipes the OpenAI SSE stream * from handleChatCompletions through a response shim that parses each * chat.completion.chunk and emits the equivalent Anthropic message_start / * content_block_* / message_delta / message_stop events as bytes arrive. * No buffering, so first-token latency matches the upstream Cascade stream. */
import { randomUUID } from 'crypto';import { handleChatCompletions } from './chat.js';import { log } from '../config.js';
function genMsgId() { return 'msg_' + randomUUID().replace(/-/g, '').slice(0, 24);}
// ─── Anthropic → OpenAI request translation ──────────────────
function anthropicToOpenAI(body) { const messages = []; if (body.system) { const sysText = typeof body.system === 'string' ? body.system : Array.isArray(body.system) ? body.system.map(b => b.text || '').join('\n') : ''; if (sysText) messages.push({ role: 'system', content: sysText }); } for (const m of (body.messages || [])) { const role = m.role === 'assistant' ? 'assistant' : 'user'; if (typeof m.content === 'string') { messages.push({ role, content: m.content }); } else if (Array.isArray(m.content)) { const textParts = []; const toolCalls = []; const toolResults = []; for (const block of m.content) { if (block.type === 'text') { textParts.push(block.text || ''); } else if (block.type === 'thinking') { // Thinking blocks from assistant history — skip; the model will regenerate } else if (block.type === 'tool_use' && role === 'assistant') { toolCalls.push({ id: block.id || `call_${randomUUID().slice(0, 8)}`, type: 'function', function: { name: block.name, arguments: JSON.stringify(block.input || {}) }, }); } else if (block.type === 'tool_result') { const content = typeof block.content === 'string' ? block.content : Array.isArray(block.content) ? block.content.map(b => b.text || '').join('\n') : JSON.stringify(block.content); toolResults.push({ role: 'tool', tool_call_id: block.tool_use_id, content }); } } if (toolCalls.length) { messages.push({ role: 'assistant', content: textParts.length ? textParts.join('\n') : null, tool_calls: toolCalls, }); } else if (textParts.length) { messages.push({ role, content: textParts.join('\n') }); } for (const tr of toolResults) messages.push(tr); } } const tools = (body.tools || []).map(t => ({ type: 'function', function: { name: t.name, description: t.description || '', parameters: t.input_schema || {}, }, })); return { model: body.model || 'claude-sonnet-4.6', messages, max_tokens: body.max_tokens || 8192, stream: !!body.stream, ...(tools.length ? { tools } : {}), ...(body.temperature != null ? { temperature: body.temperature } : {}), ...(body.top_p != null ? { top_p: body.top_p } : {}), ...(body.stop_sequences ? { stop: body.stop_sequences } : {}), };}
// ─── OpenAI → Anthropic non-stream response translation ──────
function openAIToAnthropic(result, model, msgId) { const choice = result.choices?.[0]; const usage = result.usage || {}; const content = []; if (choice?.message?.reasoning_content) { content.push({ type: 'thinking', thinking: choice.message.reasoning_content }); } if (choice?.message?.tool_calls?.length) { if (choice.message.content) content.push({ type: 'text', text: choice.message.content }); for (const tc of choice.message.tool_calls) { let input = {}; try { input = JSON.parse(tc.function?.arguments || '{}'); } catch {} content.push({ type: 'tool_use', id: tc.id, name: tc.function?.name || 'unknown', input, }); } } else { content.push({ type: 'text', text: choice?.message?.content || '' }); } const stopMap = { stop: 'end_turn', length: 'max_tokens', tool_calls: 'tool_use' }; return { id: msgId, type: 'message', role: 'assistant', content, model: model || result.model, stop_reason: stopMap[choice?.finish_reason] || 'end_turn', stop_sequence: null, usage: { input_tokens: usage.prompt_tokens || usage.input_tokens || 0, output_tokens: usage.completion_tokens || usage.output_tokens || 0, cache_creation_input_tokens: usage.cache_creation_input_tokens || 0, cache_read_input_tokens: usage.prompt_tokens_details?.cached_tokens || 0, }, };}
// ─── Streaming translator: intercepts OpenAI SSE, emits Anthropic SSE ──
class AnthropicStreamTranslator { constructor(res, msgId, model) { this.res = res; this.msgId = msgId; this.model = model; // Current content block: null | { type, index } // type: 'text' | 'thinking' | 'tool_use' this.current = null; this.blockIndex = 0; this.toolCallBufs = new Map(); // index → { id, name, argsBuffered } this.finalUsage = null; this.stopReason = 'end_turn'; this.messageStarted = false; this.messageStopped = false; this.pendingSseBuf = ''; }
send(event, data) { if (!this.res.writableEnded) { this.res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`); } }
startMessage() { if (this.messageStarted) return; this.messageStarted = true; this.send('message_start', { type: 'message_start', message: { id: this.msgId, type: 'message', role: 'assistant', content: [], model: this.model, stop_reason: null, stop_sequence: null, usage: { input_tokens: 0, output_tokens: 0, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 }, }, }); }
startBlock(type, extra = {}) { this.closeCurrentBlock(); this.current = { type, index: this.blockIndex }; let content_block; if (type === 'text') content_block = { type: 'text', text: '' }; else if (type === 'thinking') content_block = { type: 'thinking', thinking: '' }; else if (type === 'tool_use') content_block = { type: 'tool_use', id: extra.id, name: extra.name, input: {} }; this.send('content_block_start', { type: 'content_block_start', index: this.blockIndex, content_block, }); }
closeCurrentBlock() { if (!this.current) return; this.send('content_block_stop', { type: 'content_block_stop', index: this.current.index }); this.blockIndex++; this.current = null; }
emitTextDelta(text) { if (!text) return; if (this.current?.type !== 'text') this.startBlock('text'); this.send('content_block_delta', { type: 'content_block_delta', index: this.current.index, delta: { type: 'text_delta', text }, }); }
emitThinkingDelta(text) { if (!text) return; if (this.current?.type !== 'thinking') this.startBlock('thinking'); this.send('content_block_delta', { type: 'content_block_delta', index: this.current.index, delta: { type: 'thinking_delta', thinking: text }, }); }
emitToolCallDelta(toolCall) { const idx = toolCall.index ?? 0; const existing = this.toolCallBufs.get(idx); const id = toolCall.id || existing?.id; const name = toolCall.function?.name || existing?.name; const argsChunk = toolCall.function?.arguments || '';
if (!existing) { // New tool call — start a new tool_use content block this.startBlock('tool_use', { id, name }); this.toolCallBufs.set(idx, { id, name, blockIndex: this.current.index, argsBuffered: '' }); } const buf = this.toolCallBufs.get(idx); if (argsChunk) { buf.argsBuffered += argsChunk; this.send('content_block_delta', { type: 'content_block_delta', index: buf.blockIndex, delta: { type: 'input_json_delta', partial_json: argsChunk }, }); } }
processChunk(chunk) { this.startMessage(); const choice = chunk.choices?.[0]; if (choice) { const delta = choice.delta || {}; if (delta.reasoning_content) this.emitThinkingDelta(delta.reasoning_content); if (delta.content) this.emitTextDelta(delta.content); if (Array.isArray(delta.tool_calls)) { for (const tc of delta.tool_calls) this.emitToolCallDelta(tc); } if (choice.finish_reason) { const stopMap = { stop: 'end_turn', length: 'max_tokens', tool_calls: 'tool_use' }; this.stopReason = stopMap[choice.finish_reason] || 'end_turn'; } } if (chunk.usage) this.finalUsage = chunk.usage; }
finish() { if (this.messageStopped) return; this.messageStopped = true; this.closeCurrentBlock(); const u = this.finalUsage || {}; this.send('message_delta', { type: 'message_delta', delta: { stop_reason: this.stopReason, stop_sequence: null }, usage: { input_tokens: u.prompt_tokens || u.input_tokens || 0, output_tokens: u.completion_tokens || u.output_tokens || 0, cache_creation_input_tokens: u.cache_creation_input_tokens || 0, cache_read_input_tokens: u.prompt_tokens_details?.cached_tokens || 0, }, }); this.send('message_stop', { type: 'message_stop' }); }
// SSE parser — handleChatCompletions writes `data: {...}\n\n` frames; // accumulate and flush each complete frame as a translated event. feed(rawChunk) { this.pendingSseBuf += typeof rawChunk === 'string' ? rawChunk : rawChunk.toString('utf8'); let idx; while ((idx = this.pendingSseBuf.indexOf('\n\n')) !== -1) { const frame = this.pendingSseBuf.slice(0, idx); this.pendingSseBuf = this.pendingSseBuf.slice(idx + 2); const lines = frame.split('\n'); for (const line of lines) { if (!line.startsWith('data: ')) continue; const payload = line.slice(6); if (payload === '[DONE]') continue; try { this.processChunk(JSON.parse(payload)); } catch (e) { log.warn(`Messages SSE parse error: ${e.message}`); } } } }}
// ─── Fake ServerResponse that pipes writes into the translator ──
function createCaptureRes(translator) { const listeners = new Map(); const fire = (event) => { const cbs = listeners.get(event) || []; for (const cb of cbs) { try { cb(); } catch {} } }; return { writableEnded: false, headersSent: false, writeHead() { this.headersSent = true; }, write(chunk) { translator.feed(chunk); return true; }, end(chunk) { if (this.writableEnded) return; if (chunk) translator.feed(chunk); translator.finish(); this.writableEnded = true; fire('close'); }, // Fire 'close' without marking writableEnded=true so chat.js's // close handler sees an un-ended stream and triggers its abort path. _clientDisconnected() { fire('close'); }, on(event, cb) { if (!listeners.has(event)) listeners.set(event, []); listeners.get(event).push(cb); return this; }, once(event, cb) { const self = this; const wrapped = function onceWrapper() { self.off(event, wrapped); cb.apply(self, arguments); }; return self.on(event, wrapped); }, off(event, cb) { const arr = listeners.get(event); if (arr) { const idx = arr.indexOf(cb); if (idx !== -1) arr.splice(idx, 1); } return this; }, removeListener(event, cb) { return this.off(event, cb); }, emit() { return true; }, };}
// ─── Main entry ───────────────────────────────────────────────
export async function handleMessages(body) { const msgId = genMsgId(); const requestedModel = body.model || 'claude-sonnet-4.6'; const wantStream = !!body.stream; const openaiBody = anthropicToOpenAI(body);
if (!wantStream) { const result = await handleChatCompletions({ ...openaiBody, stream: false }); if (result.status !== 200) { return { status: result.status, body: { type: 'error', error: { type: result.body?.error?.type || 'api_error', message: result.body?.error?.message || 'Unknown error', }, }, }; } return { status: 200, body: openAIToAnthropic(result.body, requestedModel, msgId) }; }
// Streaming path — ask handleChatCompletions for its streaming handler and // point its writes at our translator shim. This lets the upstream Cascade // poll loop drive the downstream SSE in real time — no buffer-then-replay. const streamResult = await handleChatCompletions({ ...openaiBody, stream: true });
if (!streamResult.stream) { // The OpenAI path returned a non-stream error (e.g. 403 model_not_entitled) return { status: streamResult.status || 502, body: { type: 'error', error: { type: streamResult.body?.error?.type || 'api_error', message: streamResult.body?.error?.message || 'Upstream error', }, }, }; }
return { status: 200, stream: true, headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'X-Accel-Buffering': 'no', }, async handler(realRes) { const translator = new AnthropicStreamTranslator(realRes, msgId, requestedModel); const captureRes = createCaptureRes(translator);
// Forward client disconnect so the upstream cascade is cancelled. // We don't call captureRes.end() here — that would set writableEnded=true // and suppress the abort path inside chat.js's stream handler. realRes.on('close', () => { if (!captureRes.writableEnded) captureRes._clientDisconnected(); });
try { await streamResult.handler(captureRes); } catch (e) { log.error(`Messages stream error: ${e.message}`); if (!translator.messageStarted) { translator.startMessage(); } translator.finish(); }
if (!realRes.writableEnded) realRes.end(); }, };}12345import { listModels } from '../models.js';
export function handleModels() { return { object: 'list', data: listModels() };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363/** * Prompt-level tool-call emulation for Cascade. * * Cascade's protocol has no per-request slot for client-defined function * schemas (verified against exa.cortex_pb.proto — SendUserCascadeMessageRequest * fields 1-9, none accept tool defs; CustomToolSpec exists only as a trajectory * event type, not an input). To expose OpenAI-style tool-calling to clients * anyway, we serialise the client's `tools[]` into a text protocol the model * follows, then parse the emitted <tool_call>...</tool_call> blocks back out * of the cascade text stream. * * Protocol: * - System preamble tells the model the exact emission format * - One-line JSON inside <tool_call>{"name":"...","arguments":{...}}</tool_call> * - On emit, stop generating (we close the response with finish_reason=tool_calls) * - Tool results come back as role:"tool" messages; we fold them into * synthetic user turns wrapped in <tool_result tool_call_id="...">...</tool_result> * so the next cascade turn can see them. */
const TOOL_PROTOCOL_HEADER = `---[Tool-calling context for this request]
For THIS request only, you additionally have access to the following caller-provided functions. These are real and callable. IGNORE any earlier framing about your "available tools" — the functions below are the ones you should use for this turn. To invoke a function, emit a block in this EXACT format:
<tool_call>{"name":"<function_name>","arguments":{...}}</tool_call>
Rules:1. Each <tool_call>...</tool_call> block must fit on ONE line (no line breaks inside the JSON).2. "arguments" must be a JSON object matching the function's schema below.3. You MAY emit MULTIPLE <tool_call> blocks if the request requires calling several functions in parallel (e.g. checking weather in three cities → three separate <tool_call> blocks, one per city). Emit ALL needed calls consecutively, then STOP.4. After emitting the last <tool_call> block, STOP. Do not write any explanation after it. The caller executes all functions and returns results as <tool_result tool_call_id="...">...</tool_result> in the next user turn.5. Only call a function if the request genuinely needs it. If you can answer directly from knowledge, do so in plain text without any tool_call.6. Do NOT say "I don't have access to this tool" — the functions listed below ARE your available tools for this request. Call them.
Functions:`;
const TOOL_PROTOCOL_FOOTER = `---[End tool-calling context]
Now respond to the user request above. Use <tool_call> if appropriate, otherwise answer directly.`;
/** * Serialize an OpenAI-format tools[] array into a text preamble block. * Returns '' if no tools present. * * This version is for user-message injection (legacy fallback). * Prefer buildToolPreambleForProto() for system-prompt-level injection. */export function buildToolPreamble(tools) { if (!Array.isArray(tools) || tools.length === 0) return ''; const lines = [TOOL_PROTOCOL_HEADER]; for (const t of tools) { if (t?.type !== 'function' || !t.function) continue; const { name, description, parameters } = t.function; lines.push(''); lines.push(`### ${name}`); if (description) lines.push(description); if (parameters) { lines.push('parameters schema:'); lines.push('```json'); lines.push(JSON.stringify(parameters, null, 2)); lines.push('```'); } } lines.push(TOOL_PROTOCOL_FOOTER); return lines.join('\n');}
/** * System-prompt-level preamble for proto-level injection via * CascadeConversationalPlannerConfig.tool_calling_section (field 10). * * Unlike buildToolPreamble (which wraps in user-message-style fences), * this version is written as authoritative system instructions so the * model treats the tool definitions as first-class, not as a "user hint" * that the baked-in system prompt can override. */const TOOL_PROTOCOL_SYSTEM_HEADER = `You have access to the following functions. To invoke a function, emit a block in this EXACT format:
<tool_call>{"name":"<function_name>","arguments":{...}}</tool_call>
Rules:1. Each <tool_call>...</tool_call> block must fit on ONE line (no line breaks inside the JSON).2. "arguments" must be a JSON object matching the function's parameter schema.3. You MAY emit MULTIPLE <tool_call> blocks if the request requires calling several functions in parallel. Emit ALL needed calls consecutively, then STOP generating.4. After emitting the last <tool_call> block, STOP. Do not write any explanation after it. The caller executes the functions and returns results wrapped in <tool_result tool_call_id="...">...</tool_result> tags in the next user turn.5. NEVER say "I don't have access to tools" or "I cannot perform that action" — the functions listed below ARE your available tools.`;
// Behaviour suffix appended after the base rules, controlled by tool_choice.const TOOL_CHOICE_SUFFIX = { // "auto" (default): prefer tools over direct answers when a tool is relevant auto: `6. When a function is relevant to the user's request, you SHOULD call it rather than answering from memory. Prefer using a tool over guessing.`, // "required": MUST call at least one tool — never answer directly required: `6. You MUST call at least one function for every request. Do NOT answer directly in plain text — always use a <tool_call>.`, // "none": never call tools (shouldn't normally reach here, but be safe) none: `6. Do NOT call any functions. Answer the user's question directly in plain text.`,};
/** * Resolve the OpenAI tool_choice parameter into a { mode, forceName } pair. * tool_choice = "auto" | "required" | "none" * tool_choice = { type: "function", function: { name: "X" } } */function resolveToolChoice(tc) { if (!tc || tc === 'auto') return { mode: 'auto', forceName: null }; if (tc === 'required' || tc === 'any') return { mode: 'required', forceName: null }; if (tc === 'none') return { mode: 'none', forceName: null }; if (typeof tc === 'object' && tc.function?.name) { return { mode: 'required', forceName: tc.function.name }; } return { mode: 'auto', forceName: null };}
export function buildToolPreambleForProto(tools, toolChoice) { if (!Array.isArray(tools) || tools.length === 0) return ''; const { mode, forceName } = resolveToolChoice(toolChoice);
const lines = [TOOL_PROTOCOL_SYSTEM_HEADER]; // Append the appropriate behaviour suffix lines.push(TOOL_CHOICE_SUFFIX[mode] || TOOL_CHOICE_SUFFIX.auto); if (forceName) { lines.push(`7. You MUST call the function "${forceName}". No other function and no direct answer.`); } lines.push(''); lines.push('Available functions:'); for (const t of tools) { if (t?.type !== 'function' || !t.function) continue; const { name, description, parameters } = t.function; lines.push(''); lines.push(`### ${name}`); if (description) lines.push(description); if (parameters) { lines.push('Parameters:'); lines.push('```json'); lines.push(JSON.stringify(parameters, null, 2)); lines.push('```'); } } return lines.join('\n');}
function safeParseJson(s) { try { return JSON.parse(s); } catch { return null; }}
/** * Normalise an OpenAI messages[] array into a form Cascade understands. * - Prepends the tool preamble as a system message (or merges into the first system message) * - Rewrites role:"tool" messages as user turns with <tool_result> wrappers * - Rewrites assistant messages that carry tool_calls so the model sees its * own prior emissions in the canonical <tool_call> format */export function normalizeMessagesForCascade(messages, tools) { if (!Array.isArray(messages)) return messages; const out = [];
for (const m of messages) { if (!m || !m.role) { out.push(m); continue; }
if (m.role === 'tool') { const id = m.tool_call_id || 'unknown'; const content = typeof m.content === 'string' ? m.content : JSON.stringify(m.content ?? ''); out.push({ role: 'user', content: `<tool_result tool_call_id="${id}">\n${content}\n</tool_result>`, }); continue; }
if (m.role === 'assistant' && Array.isArray(m.tool_calls) && m.tool_calls.length) { const parts = []; if (m.content) parts.push(typeof m.content === 'string' ? m.content : JSON.stringify(m.content)); for (const tc of m.tool_calls) { const name = tc.function?.name || 'unknown'; const args = tc.function?.arguments; const parsed = typeof args === 'string' ? (safeParseJson(args) ?? {}) : (args ?? {}); parts.push(`<tool_call>${JSON.stringify({ name, arguments: parsed })}</tool_call>`); } out.push({ role: 'assistant', content: parts.join('\n') }); continue; }
out.push(m); }
// Inject the preamble into the LAST user message (not as a separate system // block). Cascade LS has a strong baked-in system prompt that overpowers // additional system messages — Claude will respond "those aren't my tools" // if we put the tool schema in a system slot. Wrapping the user turn with // [context] ... [end context] + original question treats the tool instructions // as part of the current request, which Claude reliably follows. const preamble = buildToolPreamble(tools); if (preamble) { for (let i = out.length - 1; i >= 0; i--) { if (out[i].role === 'user') { const cur = typeof out[i].content === 'string' ? out[i].content : JSON.stringify(out[i].content ?? ''); out[i] = { ...out[i], content: preamble + '\n\n' + cur }; break; } } }
return out;}
/** * Streaming parser for <tool_call>...</tool_call> blocks. * * Feed text deltas via .feed(delta). It returns: * { text: string, toolCalls: Array<{id,name,argumentsJson}> } * where `text` is the portion safe to emit as a normal content delta (tool_call * markup stripped), and `toolCalls` is any fully-closed blocks detected in this * feed. Partial blocks across delta boundaries are held until the close tag * arrives. Partial OPEN tags at the buffer tail are also held back so we don't * accidentally leak `<tool_ca` to the client and then open a real block on the * next delta. */export class ToolCallStreamParser { constructor() { this.buffer = ''; this.inToolCall = false; this.inToolResult = false; this._totalSeen = 0; }
feed(delta) { if (!delta) return { text: '', toolCalls: [] }; this.buffer += delta; const safeParts = []; const doneCalls = []; const TC_OPEN = '<tool_call>'; const TC_CLOSE = '</tool_call>'; const TR_PREFIX = '<tool_result'; const TR_CLOSE = '</tool_result>';
while (true) { // ── Inside a <tool_result …>…</tool_result> block — discard body ── if (this.inToolResult) { const closeIdx = this.buffer.indexOf(TR_CLOSE); if (closeIdx === -1) break; // wait for close tag this.buffer = this.buffer.slice(closeIdx + TR_CLOSE.length); this.inToolResult = false; continue; }
// ── Inside a <tool_call>…</tool_call> block — parse JSON body ── if (this.inToolCall) { const closeIdx = this.buffer.indexOf(TC_CLOSE); if (closeIdx === -1) break; // wait for more const body = this.buffer.slice(0, closeIdx).trim(); this.buffer = this.buffer.slice(closeIdx + TC_CLOSE.length); this.inToolCall = false;
const parsed = safeParseJson(body); if (parsed && typeof parsed.name === 'string') { const args = parsed.arguments; const argsJson = typeof args === 'string' ? args : JSON.stringify(args ?? {}); doneCalls.push({ id: `call_${this._totalSeen}_${Date.now().toString(36)}`, name: parsed.name, argumentsJson: argsJson, }); this._totalSeen++; } else { // Malformed — surface as literal text so it's debuggable safeParts.push(`<tool_call>${body}</tool_call>`); } continue; }
// ── Normal mode — scan for the next opening tag ── const tcIdx = this.buffer.indexOf(TC_OPEN); const trIdx = this.buffer.indexOf(TR_PREFIX);
// Pick whichever opening tag comes first let nextIdx = -1; let isResult = false; if (tcIdx !== -1 && (trIdx === -1 || tcIdx <= trIdx)) { nextIdx = tcIdx; } else if (trIdx !== -1) { nextIdx = trIdx; isResult = true; }
if (nextIdx === -1) { // No tags found. Hold back any suffix that could be a partial // prefix of either opening tag so we don't leak mid-tag to the // client. let holdLen = 0; for (const prefix of [TC_OPEN, TR_PREFIX]) { const maxHold = Math.min(prefix.length - 1, this.buffer.length); for (let len = maxHold; len > 0; len--) { if (this.buffer.endsWith(prefix.slice(0, len))) { holdLen = Math.max(holdLen, len); break; } } } const emitUpto = this.buffer.length - holdLen; if (emitUpto > 0) safeParts.push(this.buffer.slice(0, emitUpto)); this.buffer = this.buffer.slice(emitUpto); break; }
// Emit text before the tag if (nextIdx > 0) safeParts.push(this.buffer.slice(0, nextIdx));
if (!isResult) { // <tool_call> this.buffer = this.buffer.slice(nextIdx + TC_OPEN.length); this.inToolCall = true; } else { // <tool_result …> — may have attributes, find closing > const closeAngle = this.buffer.indexOf('>', nextIdx + TR_PREFIX.length); if (closeAngle === -1) { // Incomplete open tag; hold everything from the tag start this.buffer = this.buffer.slice(nextIdx); break; } this.buffer = this.buffer.slice(closeAngle + 1); this.inToolResult = true; } }
return { text: safeParts.join(''), toolCalls: doneCalls }; }
/** Call at end of stream. Returns any leftover buffer as literal text. */ flush() { const remaining = this.buffer; this.buffer = ''; if (this.inToolCall) { this.inToolCall = false; return { text: `<tool_call>${remaining}`, toolCalls: [] }; } if (this.inToolResult) { this.inToolResult = false; return { text: '', toolCalls: [] }; // discard incomplete tool_result } return { text: remaining, toolCalls: [] }; }}
/** * Run a complete (non-streamed) text through the parser in one shot. * Convenience wrapper for the non-stream response path. */export function parseToolCallsFromText(text) { const parser = new ToolCallStreamParser(); const a = parser.feed(text); const b = parser.flush(); return { text: a.text + b.text, toolCalls: [...a.toolCalls, ...b.toolCalls], };}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788// Logger must be imported first to patch log functions before other modules use themimport './dashboard/logger.js';import { initAuth, isAuthenticated } from './auth.js';import { startLanguageServer, waitForReady, isLanguageServerRunning, stopLanguageServer } from './langserver.js';import { startServer } from './server.js';import { config, log } from './config.js';import { existsSync } from 'fs';import { execSync } from 'child_process';
export const BRAND = 'WindsurfAPI bydwgx1337';export const VERSION = '1.2.0';
async function main() { const banner = ` _ _ _ _ __ _ ____ ___ | | | (_) | | / _| / \\ | _ \\_ _| | | | |_ _ __ __| |___ _ _ _ __ _| |_ / _ \\ | |_) | | | |/\\| | | '_ \\ / _\` / __| | | | '__|_ _|/ ___ \\| __/| | \\ /\\ / | | | | (_| \\__ \\ |_| | | |_| /_/ \\_\\_| |___| \\/ \\/|_|_| |_|\\__,_|___/\\__,_|_| ${BRAND} v${VERSION}`; console.log(banner); console.log(` OpenAI-compatible proxy for Windsurf — by dwgx1337\n`);
// Start language server binary const binaryPath = config.lsBinaryPath; if (existsSync(binaryPath)) { try { // Wipe the workspace on every startup. If we don't, files created by // previous chat sessions (e.g. Claude "editing" config.yaml/lru_cache.py // via the baked-in Cascade tool prompts) persist and pollute the next // request — the model sees them at session init and starts narrating // edits to files the caller never mentioned. execSync('mkdir -p /opt/windsurf/data/db /tmp/windsurf-workspace && rm -rf /tmp/windsurf-workspace/* /tmp/windsurf-workspace/.[!.]* 2>/dev/null || true', { stdio: 'ignore' }); } catch {}
await startLanguageServer({ binaryPath, port: config.lsPort, apiServerUrl: config.codeiumApiUrl, });
try { await waitForReady(15000); } catch (err) { log.error(`Language server failed to start: ${err.message}`); log.error('Chat completions will not work without the language server.'); } } else { log.warn(`Language server binary not found at ${binaryPath}`); log.warn('Install it with: download Windsurf Linux tarball and extract language_server_linux_x64'); }
// Init auth pool await initAuth();
if (!isAuthenticated()) { log.warn('No accounts configured. Add via:'); log.warn(' POST /auth/login {"token":"..."}'); log.warn(' POST /auth/login {"api_key":"..."}'); }
const server = startServer();
let shuttingDown = false; const shutdown = (signal) => { if (shuttingDown) return; shuttingDown = true; const inflight = server.getActiveRequests?.() ?? '?'; log.info(`${signal} received — draining ${inflight} in-flight requests (up to 30s)...`); if (typeof server.closeIdleConnections === 'function') server.closeIdleConnections(); server.close(() => { log.info('HTTP server closed, stopping language server'); try { stopLanguageServer(); } catch {} process.exit(0); }); setTimeout(() => { log.warn('Drain timeout, forcing exit'); try { stopLanguageServer(); } catch {} process.exit(0); }, 30_000); }; process.on('SIGINT', () => shutdown('SIGINT')); process.on('SIGTERM', () => shutdown('SIGTERM'));}
main().catch(err => { console.error('Fatal:', err); process.exit(1); });123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266/** * Language server pool manager. * Spawns multiple LS instances — one per unique outbound proxy (plus a default * no-proxy instance). Accounts are routed to the LS instance matching their * configured proxy so that each upstream Codeium request goes out through the * right egress IP. Also avoids the LS state-pollution bug where switching * accounts within a single LS session causes workspace setup streams to be * canceled. */
import { spawn, execSync } from 'child_process';import http2 from 'http2';import net from 'net';import { log } from './config.js';
const DEFAULT_BINARY = '/opt/windsurf/language_server_linux_x64';const DEFAULT_PORT = 42100;const DEFAULT_CSRF = 'windsurf-api-csrf-fixed-token';const DEFAULT_API_URL = 'https://server.self-serve.windsurf.com';
// Pool: key -> { process, port, csrfToken, proxy, startedAt, ready }const _pool = new Map();let _nextPort = DEFAULT_PORT + 1;let _binaryPath = DEFAULT_BINARY;let _apiServerUrl = DEFAULT_API_URL;
function proxyKey(proxy) { if (!proxy || !proxy.host) return 'default'; return `px_${proxy.host.replace(/\./g, '_')}_${proxy.port}`;}
function proxyUrl(proxy) { if (!proxy || !proxy.host) return null; const auth = proxy.username ? `${encodeURIComponent(proxy.username)}:${encodeURIComponent(proxy.password || '')}@` : ''; return `http://${auth}${proxy.host}:${proxy.port || 8080}`;}
function isPortInUse(port) { return new Promise((resolve) => { const sock = net.createConnection({ port, host: '127.0.0.1' }, () => { sock.destroy(); resolve(true); }); sock.on('error', () => resolve(false)); sock.setTimeout(1000, () => { sock.destroy(); resolve(false); }); });}
async function waitPortReady(port, timeoutMs = 20000) { const start = Date.now(); while (Date.now() - start < timeoutMs) { try { await new Promise((resolve, reject) => { const client = http2.connect(`http://localhost:${port}`); const timer = setTimeout(() => { try { client.close(); } catch {} reject(new Error('timeout')); }, 2000); client.on('connect', () => { clearTimeout(timer); client.close(); resolve(); }); client.on('error', (e) => { clearTimeout(timer); try { client.close(); } catch {} reject(e); }); }); return true; } catch { await new Promise(r => setTimeout(r, 500)); } } throw new Error(`LS port ${port} not ready after ${timeoutMs}ms`);}
/** * Spawn an LS instance for the given proxy (or no-proxy default). * Idempotent — returns the existing entry if one is already running. */export async function ensureLs(proxy = null) { const key = proxyKey(proxy); const existing = _pool.get(key); if (existing && existing.ready) return existing;
const isDefault = key === 'default'; const port = isDefault ? DEFAULT_PORT : _nextPort++;
// If something is already listening on the default port (e.g. leftover from // a previous crashed run), adopt it rather than fight for the port. if (isDefault && await isPortInUse(port)) { log.info(`LS default port ${port} already in use — adopting existing instance`); const entry = { process: null, port, csrfToken: DEFAULT_CSRF, proxy: null, startedAt: Date.now(), ready: true, workspaceInit: null, sessionId: null, }; _pool.set(key, entry); return entry; }
const dataDir = `/opt/windsurf/data/${key}`; try { execSync(`mkdir -p ${dataDir}/db`, { stdio: 'ignore' }); } catch {}
const args = [ `--api_server_url=${_apiServerUrl}`, `--server_port=${port}`, `--csrf_token=${DEFAULT_CSRF}`, `--register_user_url=https://api.codeium.com/register_user/`, `--codeium_dir=${dataDir}`, `--database_dir=${dataDir}/db`, '--enable_local_search=false', '--enable_index_service=false', '--enable_lsp=false', '--detect_proxy=false', ];
const env = { ...process.env, HOME: '/root' }; const pUrl = proxyUrl(proxy); if (pUrl) { env.HTTPS_PROXY = pUrl; env.HTTP_PROXY = pUrl; env.https_proxy = pUrl; env.http_proxy = pUrl; }
log.info(`Starting LS instance key=${key} port=${port} proxy=${pUrl || 'none'}`);
const proc = spawn(_binaryPath, args, { stdio: ['pipe', 'pipe', 'pipe'], env, });
proc.stdout.on('data', (data) => { const lines = data.toString().trim().split('\n'); for (const line of lines) { if (!line) continue; if (/ERROR|error/.test(line)) log.error(`[LS:${key}] ${line}`); else log.debug(`[LS:${key}] ${line}`); } }); proc.stderr.on('data', (data) => { const line = data.toString().trim(); if (line) log.debug(`[LS:${key}:err] ${line}`); }); proc.on('exit', (code, signal) => { log.warn(`LS instance ${key} exited: code=${code} signal=${signal}`); const gone = _pool.get(key); _pool.delete(key); if (gone?.port) { import('./conversation-pool.js').then(m => m.invalidateFor({ lsPort: gone.port })).catch(() => {}); } }); proc.on('error', (err) => { log.error(`LS instance ${key} spawn error: ${err.message}`); _pool.delete(key); });
const entry = { process: proc, port, csrfToken: DEFAULT_CSRF, proxy, startedAt: Date.now(), ready: false, // One-shot Cascade workspace init promise. cascadeChat() awaits this so // the heavy InitializePanelState / AddTrackedWorkspace / UpdateWorkspaceTrust // trio only runs once per LS lifetime instead of once per request. workspaceInit: null, sessionId: null, }; _pool.set(key, entry);
try { await waitPortReady(port, 25000); entry.ready = true; log.info(`LS instance ${key} ready on port ${port}`); } catch (err) { log.error(`LS instance ${key} failed to become ready: ${err.message}`); try { proc.kill('SIGKILL'); } catch {} _pool.delete(key); throw err; } return entry;}
/** * Stop and remove the LS instance associated with a given proxy. * Used when a proxy is reassigned so the old egress no longer exists. */export async function restartLsForProxy(proxy) { const key = proxyKey(proxy); const entry = _pool.get(key); if (entry?.process) { try { entry.process.kill('SIGTERM'); } catch {} } _pool.delete(key); return ensureLs(proxy);}
/** * Get the LS entry matching a proxy (or default when proxy is null). * Returns the default instance as a fallback if the proxy-specific one hasn't * been spawned yet. */export function getLsFor(proxy) { const key = proxyKey(proxy); return _pool.get(key) || _pool.get('default') || null;}
/** * Look up an LS pool entry by its gRPC port. Used by WindsurfClient so it * can attach per-LS state (one-shot cascade workspace init, persistent * sessionId) without plumbing the entry through every call site. */export function getLsEntryByPort(port) { for (const entry of _pool.values()) { if (entry.port === port) return entry; } return null;}
// ─── Backward-compat API ───────────────────────────────────
export function getLsPort() { return _pool.get('default')?.port || DEFAULT_PORT;}export function getCsrfToken() { return _pool.get('default')?.csrfToken || DEFAULT_CSRF;}
/** * Legacy entry point used by index.js — starts the default (no-proxy) LS. */export async function startLanguageServer(opts = {}) { _binaryPath = opts.binaryPath || process.env.LS_BINARY_PATH || _binaryPath; _apiServerUrl = opts.apiServerUrl || process.env.CODEIUM_API_URL || _apiServerUrl; const def = await ensureLs(null); return { port: def.port, csrfToken: def.csrfToken };}
export function stopLanguageServer() { for (const [key, entry] of _pool) { try { entry.process?.kill('SIGTERM'); } catch {} log.info(`LS instance ${key} stopped`); } _pool.clear();}
export function isLanguageServerRunning() { return _pool.size > 0;}
export async function waitForReady(/* timeoutMs */) { const def = _pool.get('default'); if (!def) throw new Error('default LS not initialized'); if (def.ready) return true; await waitPortReady(def.port, 20000); def.ready = true; return true;}
export function getLsStatus() { const def = _pool.get('default'); return { running: _pool.size > 0, pid: def?.process?.pid || null, port: def?.port || DEFAULT_PORT, startedAt: def?.startedAt || null, restartCount: 0, instances: Array.from(_pool.entries()).map(([key, e]) => ({ key, port: e.port, pid: e.process?.pid || null, proxy: e.proxy ? `${e.proxy.host}:${e.proxy.port}` : null, startedAt: e.startedAt, ready: e.ready, })), };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287/** * Model catalog — merged from hardcoded enum values + live GetCascadeModelConfigs. * * Routing logic: * modelUid present → Cascade flow (StartCascade → SendUserCascadeMessage) * only enumValue>0 → RawGetChatMessage (legacy) * * Credit multipliers sourced from GetCascadeModelConfigs (server.codeium.com). * Enum values sourced from Windsurf extension.js decompilation. */
export const MODELS = { // ── Claude ────────────────────────────────────────────── 'claude-3.5-sonnet': { name: 'claude-3.5-sonnet', provider: 'anthropic', enumValue: 166, credit: 2 }, 'claude-3.7-sonnet': { name: 'claude-3.7-sonnet', provider: 'anthropic', enumValue: 226, credit: 2 }, 'claude-3.7-sonnet-thinking': { name: 'claude-3.7-sonnet-thinking', provider: 'anthropic', enumValue: 227, credit: 3 }, 'claude-4-sonnet': { name: 'claude-4-sonnet', provider: 'anthropic', enumValue: 281, modelUid: 'MODEL_CLAUDE_4_SONNET', credit: 2 }, 'claude-4-sonnet-thinking': { name: 'claude-4-sonnet-thinking', provider: 'anthropic', enumValue: 282, modelUid: 'MODEL_CLAUDE_4_SONNET_THINKING', credit: 3 }, 'claude-4-opus': { name: 'claude-4-opus', provider: 'anthropic', enumValue: 290, modelUid: 'MODEL_CLAUDE_4_OPUS', credit: 4 }, 'claude-4-opus-thinking': { name: 'claude-4-opus-thinking', provider: 'anthropic', enumValue: 291, modelUid: 'MODEL_CLAUDE_4_OPUS_THINKING', credit: 5 }, 'claude-4.1-opus': { name: 'claude-4.1-opus', provider: 'anthropic', enumValue: 328, modelUid: 'MODEL_CLAUDE_4_1_OPUS', credit: 4 }, 'claude-4.1-opus-thinking': { name: 'claude-4.1-opus-thinking', provider: 'anthropic', enumValue: 329, modelUid: 'MODEL_CLAUDE_4_1_OPUS_THINKING', credit: 5 }, 'claude-4.5-haiku': { name: 'claude-4.5-haiku', provider: 'anthropic', enumValue: 0, modelUid: 'MODEL_PRIVATE_11', credit: 1 }, 'claude-4.5-sonnet': { name: 'claude-4.5-sonnet', provider: 'anthropic', enumValue: 353, modelUid: 'MODEL_PRIVATE_2', credit: 2 }, 'claude-4.5-sonnet-thinking': { name: 'claude-4.5-sonnet-thinking', provider: 'anthropic', enumValue: 354, modelUid: 'MODEL_PRIVATE_3', credit: 3 }, 'claude-4.5-opus': { name: 'claude-4.5-opus', provider: 'anthropic', enumValue: 391, modelUid: 'MODEL_CLAUDE_4_5_OPUS', credit: 4 }, 'claude-4.5-opus-thinking': { name: 'claude-4.5-opus-thinking', provider: 'anthropic', enumValue: 392, modelUid: 'MODEL_CLAUDE_4_5_OPUS_THINKING', credit: 5 }, 'claude-sonnet-4.6': { name: 'claude-sonnet-4.6', provider: 'anthropic', enumValue: 0, modelUid: 'claude-sonnet-4-6', credit: 4 }, 'claude-sonnet-4.6-thinking': { name: 'claude-sonnet-4.6-thinking', provider: 'anthropic', enumValue: 0, modelUid: 'claude-sonnet-4-6-thinking', credit: 6 }, 'claude-sonnet-4.6-1m': { name: 'claude-sonnet-4.6-1m', provider: 'anthropic', enumValue: 0, modelUid: 'claude-sonnet-4-6-1m', credit: 12 }, 'claude-sonnet-4.6-thinking-1m': { name: 'claude-sonnet-4.6-thinking-1m', provider: 'anthropic', enumValue: 0, modelUid: 'claude-sonnet-4-6-thinking-1m', credit: 16 }, 'claude-opus-4.6': { name: 'claude-opus-4.6', provider: 'anthropic', enumValue: 0, modelUid: 'claude-opus-4-6', credit: 6 }, 'claude-opus-4.6-thinking': { name: 'claude-opus-4.6-thinking', provider: 'anthropic', enumValue: 0, modelUid: 'claude-opus-4-6-thinking', credit: 8 },
// ── GPT ───────────────────────────────────────────────── 'gpt-4o': { name: 'gpt-4o', provider: 'openai', enumValue: 109, modelUid: 'MODEL_CHAT_GPT_4O_2024_08_06', credit: 1 }, 'gpt-4o-mini': { name: 'gpt-4o-mini', provider: 'openai', enumValue: 113, credit: 0.5 }, 'gpt-4.1': { name: 'gpt-4.1', provider: 'openai', enumValue: 259, modelUid: 'MODEL_CHAT_GPT_4_1_2025_04_14', credit: 1 }, 'gpt-4.1-mini': { name: 'gpt-4.1-mini', provider: 'openai', enumValue: 260, credit: 0.5 }, 'gpt-4.1-nano': { name: 'gpt-4.1-nano', provider: 'openai', enumValue: 261, credit: 0.25 }, 'gpt-5': { name: 'gpt-5', provider: 'openai', enumValue: 340, modelUid: 'MODEL_PRIVATE_6', credit: 0.5 }, 'gpt-5-medium': { name: 'gpt-5-medium', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_7', credit: 1 }, 'gpt-5-high': { name: 'gpt-5-high', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_8', credit: 2 }, 'gpt-5-mini': { name: 'gpt-5-mini', provider: 'openai', enumValue: 337, credit: 0.25 }, 'gpt-5-codex': { name: 'gpt-5-codex', provider: 'openai', enumValue: 346, modelUid: 'MODEL_CHAT_GPT_5_CODEX', credit: 0.5 },
// GPT-5.1 'gpt-5.1': { name: 'gpt-5.1', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_12', credit: 0.5 }, 'gpt-5.1-low': { name: 'gpt-5.1-low', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_13', credit: 0.5 }, 'gpt-5.1-medium': { name: 'gpt-5.1-medium', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_14', credit: 1 }, 'gpt-5.1-high': { name: 'gpt-5.1-high', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_15', credit: 2 }, 'gpt-5.1-fast': { name: 'gpt-5.1-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_20', credit: 1 }, 'gpt-5.1-low-fast': { name: 'gpt-5.1-low-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_21', credit: 1 }, 'gpt-5.1-medium-fast': { name: 'gpt-5.1-medium-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_22', credit: 2 }, 'gpt-5.1-high-fast': { name: 'gpt-5.1-high-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_23', credit: 4 },
// GPT-5.1 Codex 'gpt-5.1-codex-low': { name: 'gpt-5.1-codex-low', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_1_CODEX_LOW', credit: 0.5 }, 'gpt-5.1-codex-medium': { name: 'gpt-5.1-codex-medium', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_9', credit: 1 }, 'gpt-5.1-codex-mini-low': { name: 'gpt-5.1-codex-mini-low', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_1_CODEX_MINI_LOW', credit: 0.25 }, 'gpt-5.1-codex-mini': { name: 'gpt-5.1-codex-mini', provider: 'openai', enumValue: 0, modelUid: 'MODEL_PRIVATE_19', credit: 0.5 }, 'gpt-5.1-codex-max-low': { name: 'gpt-5.1-codex-max-low', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_1_CODEX_MAX_LOW', credit: 1 }, 'gpt-5.1-codex-max-medium': { name: 'gpt-5.1-codex-max-medium', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_1_CODEX_MAX_MEDIUM', credit: 1.25 }, 'gpt-5.1-codex-max-high': { name: 'gpt-5.1-codex-max-high', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_1_CODEX_MAX_HIGH', credit: 1.5 },
// GPT-5.2 'gpt-5.2': { name: 'gpt-5.2', provider: 'openai', enumValue: 401, modelUid: 'MODEL_GPT_5_2_MEDIUM', credit: 2 }, 'gpt-5.2-none': { name: 'gpt-5.2-none', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_NONE', credit: 1 }, 'gpt-5.2-low': { name: 'gpt-5.2-low', provider: 'openai', enumValue: 400, modelUid: 'MODEL_GPT_5_2_LOW', credit: 1 }, 'gpt-5.2-high': { name: 'gpt-5.2-high', provider: 'openai', enumValue: 402, modelUid: 'MODEL_GPT_5_2_HIGH', credit: 3 }, 'gpt-5.2-xhigh': { name: 'gpt-5.2-xhigh', provider: 'openai', enumValue: 403, modelUid: 'MODEL_GPT_5_2_XHIGH', credit: 8 }, 'gpt-5.2-none-fast': { name: 'gpt-5.2-none-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_NONE_PRIORITY', credit: 2 }, 'gpt-5.2-low-fast': { name: 'gpt-5.2-low-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_LOW_PRIORITY', credit: 2 }, 'gpt-5.2-medium-fast': { name: 'gpt-5.2-medium-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_MEDIUM_PRIORITY', credit: 4 }, 'gpt-5.2-high-fast': { name: 'gpt-5.2-high-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_HIGH_PRIORITY', credit: 6 }, 'gpt-5.2-xhigh-fast': { name: 'gpt-5.2-xhigh-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_XHIGH_PRIORITY', credit: 16 },
// GPT-5.2 Codex 'gpt-5.2-codex-low': { name: 'gpt-5.2-codex-low', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_LOW', credit: 1 }, 'gpt-5.2-codex-medium': { name: 'gpt-5.2-codex-medium', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_MEDIUM', credit: 1 }, 'gpt-5.2-codex-high': { name: 'gpt-5.2-codex-high', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_HIGH', credit: 2 }, 'gpt-5.2-codex-xhigh': { name: 'gpt-5.2-codex-xhigh', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_XHIGH', credit: 3 }, 'gpt-5.2-codex-low-fast': { name: 'gpt-5.2-codex-low-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_LOW_PRIORITY', credit: 2 }, 'gpt-5.2-codex-medium-fast': { name: 'gpt-5.2-codex-medium-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_MEDIUM_PRIORITY', credit: 2 }, 'gpt-5.2-codex-high-fast': { name: 'gpt-5.2-codex-high-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_HIGH_PRIORITY', credit: 4 }, 'gpt-5.2-codex-xhigh-fast': { name: 'gpt-5.2-codex-xhigh-fast', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_5_2_CODEX_XHIGH_PRIORITY', credit: 6 },
// GPT-5.3 Codex (legacy key) 'gpt-5.3-codex': { name: 'gpt-5.3-codex', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-3-codex-medium', credit: 1 },
// GPT-5.4 'gpt-5.4-low': { name: 'gpt-5.4-low', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-low', credit: 1 }, 'gpt-5.4-medium': { name: 'gpt-5.4-medium', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-medium', credit: 2 }, 'gpt-5.4-xhigh': { name: 'gpt-5.4-xhigh', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-xhigh', credit: 8 }, 'gpt-5.4-mini-low': { name: 'gpt-5.4-mini-low', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-mini-low', credit: 1.5 }, 'gpt-5.4-mini-medium': { name: 'gpt-5.4-mini-medium', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-mini-medium', credit: 1.5 }, 'gpt-5.4-mini-high': { name: 'gpt-5.4-mini-high', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-mini-high', credit: 4.5 }, 'gpt-5.4-mini-xhigh': { name: 'gpt-5.4-mini-xhigh', provider: 'openai', enumValue: 0, modelUid: 'gpt-5-4-mini-xhigh', credit: 12 },
// GPT-OSS 'gpt-oss-120b': { name: 'gpt-oss-120b', provider: 'openai', enumValue: 0, modelUid: 'MODEL_GPT_OSS_120B', credit: 0.25 },
// ── O-series ──────────────────────────────────────────── 'o3-mini': { name: 'o3-mini', provider: 'openai', enumValue: 207, credit: 0.5 }, 'o3': { name: 'o3', provider: 'openai', enumValue: 218, modelUid: 'MODEL_CHAT_O3', credit: 1 }, 'o3-high': { name: 'o3-high', provider: 'openai', enumValue: 0, modelUid: 'MODEL_CHAT_O3_HIGH', credit: 1 }, 'o3-pro': { name: 'o3-pro', provider: 'openai', enumValue: 294, credit: 4 }, 'o4-mini': { name: 'o4-mini', provider: 'openai', enumValue: 264, credit: 0.5 },
// ── Gemini ────────────────────────────────────────────── 'gemini-2.5-pro': { name: 'gemini-2.5-pro', provider: 'google', enumValue: 246, modelUid: 'MODEL_GOOGLE_GEMINI_2_5_PRO', credit: 1 }, 'gemini-2.5-flash': { name: 'gemini-2.5-flash', provider: 'google', enumValue: 312, modelUid: 'MODEL_GOOGLE_GEMINI_2_5_FLASH', credit: 0.5 }, 'gemini-3.0-pro': { name: 'gemini-3.0-pro', provider: 'google', enumValue: 412, modelUid: 'MODEL_GOOGLE_GEMINI_3_0_PRO_LOW', credit: 1 }, 'gemini-3.0-flash-minimal': { name: 'gemini-3.0-flash-minimal', provider: 'google', enumValue: 0, modelUid: 'MODEL_GOOGLE_GEMINI_3_0_FLASH_MINIMAL', credit: 0.75 }, 'gemini-3.0-flash-low': { name: 'gemini-3.0-flash-low', provider: 'google', enumValue: 0, modelUid: 'MODEL_GOOGLE_GEMINI_3_0_FLASH_LOW', credit: 1 }, 'gemini-3.0-flash': { name: 'gemini-3.0-flash', provider: 'google', enumValue: 415, modelUid: 'MODEL_GOOGLE_GEMINI_3_0_FLASH_MEDIUM', credit: 1 }, 'gemini-3.0-flash-high': { name: 'gemini-3.0-flash-high', provider: 'google', enumValue: 0, modelUid: 'MODEL_GOOGLE_GEMINI_3_0_FLASH_HIGH', credit: 1.75 }, 'gemini-3.1-pro-low': { name: 'gemini-3.1-pro-low', provider: 'google', enumValue: 0, modelUid: 'gemini-3-1-pro-low', credit: 1 }, 'gemini-3.1-pro-high': { name: 'gemini-3.1-pro-high', provider: 'google', enumValue: 0, modelUid: 'gemini-3-1-pro-high', credit: 2 },
// ── DeepSeek ──────────────────────────────────────────── 'deepseek-v3': { name: 'deepseek-v3', provider: 'deepseek', enumValue: 205, credit: 0.5 }, 'deepseek-v3-2': { name: 'deepseek-v3-2', provider: 'deepseek', enumValue: 409, credit: 0.5 }, 'deepseek-r1': { name: 'deepseek-r1', provider: 'deepseek', enumValue: 206, credit: 1 },
// ── Grok ──────────────────────────────────────────────── 'grok-3': { name: 'grok-3', provider: 'xai', enumValue: 217, modelUid: 'MODEL_XAI_GROK_3', credit: 1 }, 'grok-3-mini': { name: 'grok-3-mini', provider: 'xai', enumValue: 234, credit: 0.5 }, 'grok-3-mini-thinking': { name: 'grok-3-mini-thinking', provider: 'xai', enumValue: 0, modelUid: 'MODEL_XAI_GROK_3_MINI_REASONING', credit: 0.125 }, 'grok-code-fast-1': { name: 'grok-code-fast-1', provider: 'xai', enumValue: 0, modelUid: 'MODEL_PRIVATE_4', credit: 0.5 },
// ── Qwen ──────────────────────────────────────────────── 'qwen-3': { name: 'qwen-3', provider: 'alibaba', enumValue: 324, credit: 0.5 }, 'qwen-3-coder': { name: 'qwen-3-coder', provider: 'alibaba', enumValue: 325, credit: 0.5 },
// ── Kimi ──────────────────────────────────────────────── 'kimi-k2': { name: 'kimi-k2', provider: 'moonshot', enumValue: 0, modelUid: 'MODEL_KIMI_K2', credit: 0.5 }, 'kimi-k2.5': { name: 'kimi-k2.5', provider: 'moonshot', enumValue: 0, modelUid: 'kimi-k2-5', credit: 1 },
// ── GLM ───────────────────────────────────────────────── 'glm-4.7': { name: 'glm-4.7', provider: 'zhipu', enumValue: 417, modelUid: 'MODEL_GLM_4_7', credit: 0.25 }, 'glm-5': { name: 'glm-5', provider: 'zhipu', enumValue: 0, modelUid: 'glm-5', credit: 1.5 }, 'glm-5.1': { name: 'glm-5.1', provider: 'zhipu', enumValue: 0, modelUid: 'glm-5-1', credit: 1.5 },
// ── MiniMax ───────────────────────────────────────────── 'minimax-m2.5': { name: 'minimax-m2.5', provider: 'minimax', enumValue: 0, modelUid: 'minimax-m2-5', credit: 1 },
// ── Windsurf SWE ──────────────────────────────────────── 'swe-1.5': { name: 'swe-1.5', provider: 'windsurf', enumValue: 369, modelUid: 'MODEL_SWE_1_5_SLOW', credit: 0.5 }, 'swe-1.5-fast': { name: 'swe-1.5-fast', provider: 'windsurf', enumValue: 359, modelUid: 'MODEL_SWE_1_5', credit: 0.5 }, 'swe-1.6': { name: 'swe-1.6', provider: 'windsurf', enumValue: 0, modelUid: 'swe-1-6', credit: 0.5 }, 'swe-1.6-fast': { name: 'swe-1.6-fast', provider: 'windsurf', enumValue: 0, modelUid: 'swe-1-6-fast', credit: 0.5 },
// ── Arena ─────────────────────────────────────────────── 'arena-fast': { name: 'arena-fast', provider: 'windsurf', enumValue: 0, modelUid: 'arena-fast', credit: 0.5 }, 'arena-smart': { name: 'arena-smart', provider: 'windsurf', enumValue: 0, modelUid: 'arena-smart', credit: 1 },};
// Build reverse lookupconst _lookup = new Map();for (const [id, info] of Object.entries(MODELS)) { _lookup.set(id, id); _lookup.set(id.toLowerCase(), id); _lookup.set(info.name, id); _lookup.set(info.name.toLowerCase(), id); if (info.modelUid) _lookup.set(info.modelUid, id); if (info.modelUid) _lookup.set(info.modelUid.toLowerCase(), id);}// Legacy aliases_lookup.set('claude-sonnet-4-6-thinking', 'claude-sonnet-4.6-thinking');_lookup.set('claude-opus-4-6-thinking', 'claude-opus-4.6-thinking');_lookup.set('claude-sonnet-4-6', 'claude-sonnet-4.6');_lookup.set('claude-opus-4-6', 'claude-opus-4.6');_lookup.set('MODEL_CLAUDE_4_5_SONNET', 'claude-4.5-sonnet');_lookup.set('MODEL_CLAUDE_4_5_SONNET_THINKING', 'claude-4.5-sonnet-thinking');// UID-based aliases not already covered by modelUid field_lookup.set('claude-sonnet-4-6-1m', 'claude-sonnet-4.6-1m');_lookup.set('claude-sonnet-4-6-thinking-1m', 'claude-sonnet-4.6-thinking-1m');_lookup.set('gpt-5-4-low', 'gpt-5.4-low');_lookup.set('gpt-5-4-medium', 'gpt-5.4-medium');_lookup.set('gpt-5-4-xhigh', 'gpt-5.4-xhigh');_lookup.set('gpt-5-4-mini-low', 'gpt-5.4-mini-low');_lookup.set('gpt-5-4-mini-medium', 'gpt-5.4-mini-medium');_lookup.set('gpt-5-4-mini-high', 'gpt-5.4-mini-high');_lookup.set('gpt-5-4-mini-xhigh', 'gpt-5.4-mini-xhigh');
/** Resolve user model name → internal model key. */export function resolveModel(name) { if (!name) return null; return _lookup.get(name) || _lookup.get(name.toLowerCase()) || name;}
/** Get model info including enum and uid. */export function getModelInfo(id) { return MODELS[id] || null;}
// Reverse map: Model enum number → list of catalog keys (enum may match// multiple variants if we ever dupe, but typically 1:1).const _enumToKeys = (() => { const m = new Map(); for (const [key, info] of Object.entries(MODELS)) { if (info.enumValue && info.enumValue > 0) { const arr = m.get(info.enumValue) || []; arr.push(key); m.set(info.enumValue, arr); } } return m;})();
/** Reverse-lookup a Model enum number to our catalog keys. */export function getModelKeysByEnum(enumValue) { return _enumToKeys.get(enumValue) || [];}
// ─── Tier access ───────────────────────────────────────────
const ALL_MODEL_KEYS = Object.keys(MODELS);const FREE_TIER_MODELS = ['gpt-4o-mini', 'gemini-2.5-flash'];
export const MODEL_TIER_ACCESS = { get pro() { return Object.keys(MODELS); }, free: FREE_TIER_MODELS, unknown: FREE_TIER_MODELS, expired: [],};
/** Models a given tier is entitled to. */export function getTierModels(tier) { return MODEL_TIER_ACCESS[tier] || MODEL_TIER_ACCESS.unknown;}
/** List all models in OpenAI /v1/models format. */export function listModels() { const ts = Math.floor(Date.now() / 1000); return Object.entries(MODELS).map(([id, info]) => ({ id: info.name, object: 'model', created: ts, owned_by: info.provider, _windsurf_id: id, }));}
/** * Merge live model configs from GetCascadeModelConfigs into the catalog. * Called once at startup after the first successful cloud fetch. * Only adds NEW models not already in the catalog (doesn't overwrite enums). */export function mergeCloudModels(configs) { if (!Array.isArray(configs)) return 0; let added = 0; const providerMap = { MODEL_PROVIDER_ANTHROPIC: 'anthropic', MODEL_PROVIDER_OPENAI: 'openai', MODEL_PROVIDER_GOOGLE: 'google', MODEL_PROVIDER_DEEPSEEK: 'deepseek', MODEL_PROVIDER_XAI: 'xai', MODEL_PROVIDER_WINDSURF: 'windsurf', MODEL_PROVIDER_MOONSHOT: 'moonshot', };
for (const m of configs) { const uid = m.modelUid; if (!uid) continue; // Already in catalog? if (_lookup.has(uid) || _lookup.has(uid.toLowerCase())) continue;
const key = uid.toLowerCase().replace(/_/g, '-'); if (MODELS[key]) continue;
const provider = providerMap[m.provider] || m.provider?.toLowerCase()?.replace('model_provider_', '') || 'unknown'; MODELS[key] = { name: key, provider, enumValue: 0, modelUid: uid, credit: m.creditMultiplier || 1, }; _lookup.set(key, key); _lookup.set(uid, key); _lookup.set(uid.toLowerCase(), key); added++; } return added;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146/** * Protobuf wire format codec — zero-dependency, schema-less. * * Wire types: * 0 = Varint (int32, uint64, bool, enum) * 1 = Fixed64 (double, fixed64) * 2 = LenDelim (string, bytes, embedded messages) * 5 = Fixed32 (float, fixed32) */
// ─── Varint ────────────────────────────────────────────────
export function encodeVarint(value) { const bytes = []; let v = Number(value); if (v < 0) { const big = BigInt(v) & 0xFFFFFFFFFFFFFFFFn; let b = big; for (let i = 0; i < 10; i++) { bytes.push(Number(b & 0x7Fn) | (i < 9 ? 0x80 : 0)); b >>= 7n; } return Buffer.from(bytes); } do { let byte = v & 0x7F; v >>>= 7; if (v > 0) byte |= 0x80; bytes.push(byte); } while (v > 0); return Buffer.from(bytes);}
export function decodeVarint(buf, offset = 0) { let result = 0, shift = 0, pos = offset; while (pos < buf.length) { const byte = buf[pos++]; result |= (byte & 0x7F) << shift; if (!(byte & 0x80)) break; shift += 7; if (shift >= 64) throw new Error('Varint overflow'); } return { value: result >>> 0, length: pos - offset };}
// ─── Field-level writers (standalone functions) ────────────
function makeTag(field, wireType) { return encodeVarint((field << 3) | wireType);}
/** Write a varint field (wire type 0). */export function writeVarintField(field, value) { return Buffer.concat([makeTag(field, 0), encodeVarint(value)]);}
/** Write a length-delimited string field (wire type 2). */export function writeStringField(field, str) { if (!str && str !== '') return Buffer.alloc(0); const data = Buffer.from(str, 'utf-8'); return Buffer.concat([makeTag(field, 2), encodeVarint(data.length), data]);}
/** Write a length-delimited bytes field (wire type 2). */export function writeBytesField(field, data) { const buf = Buffer.isBuffer(data) ? data : Buffer.from(data); return Buffer.concat([makeTag(field, 2), encodeVarint(buf.length), buf]);}
/** Write an embedded message field (wire type 2). */export function writeMessageField(field, msgBuf) { if (!msgBuf || msgBuf.length === 0) return Buffer.alloc(0); return Buffer.concat([makeTag(field, 2), encodeVarint(msgBuf.length), msgBuf]);}
/** Write a fixed64 field (wire type 1). */export function writeFixed64Field(field, buf8) { return Buffer.concat([makeTag(field, 1), buf8]);}
/** Write a bool field (wire type 0), only if true. */export function writeBoolField(field, value) { if (!value) return Buffer.alloc(0); return writeVarintField(field, 1);}
// ─── Parser ────────────────────────────────────────────────
/** * Parse a protobuf buffer into an array of { field, wireType, value }. * For varint (0): value is a Number. * For lendelim (2): value is a Buffer (caller decides string vs message). * For fixed64 (1): value is an 8-byte Buffer. * For fixed32 (5): value is a 4-byte Buffer. */export function parseFields(buf) { const fields = []; let pos = 0; while (pos < buf.length) { const tag = decodeVarint(buf, pos); pos += tag.length; const fieldNum = tag.value >>> 3; const wireType = tag.value & 0x07;
let value; switch (wireType) { case 0: { // varint const v = decodeVarint(buf, pos); pos += v.length; value = v.value; break; } case 1: { // fixed64 value = buf.subarray(pos, pos + 8); pos += 8; break; } case 2: { // length-delimited const len = decodeVarint(buf, pos); pos += len.length; value = buf.subarray(pos, pos + len.value); pos += len.value; break; } case 5: { // fixed32 value = buf.subarray(pos, pos + 4); pos += 4; break; } default: throw new Error(`Unknown wire type ${wireType} at offset ${pos}`); } fields.push({ field: fieldNum, wireType, value }); } return fields;}
/** Get first field matching number and optional wire type. */export function getField(fields, num, wireType) { return fields.find(f => f.field === num && (wireType === undefined || f.wireType === wireType)) || null;}
/** Get all fields matching number. */export function getAllFields(fields, num) { return fields.filter(f => f.field === num);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140/** * Runtime configuration — persistent feature toggles that can be flipped from * the dashboard at runtime without a restart or editing .env. Backed by a * small JSON file next to the project root so it survives redeploys. * * Currently hosts the "experimental" feature flags. Keep this tiny: anything * that needs a restart should stay in config.js / .env. */
import { readFileSync, writeFileSync, existsSync } from 'fs';import { resolve, dirname } from 'path';import { fileURLToPath } from 'url';import { log } from './config.js';
const __dirname = dirname(fileURLToPath(import.meta.url));const FILE = resolve(__dirname, '..', 'runtime-config.json');
export const DEFAULT_IDENTITY_PROMPTS = { anthropic: 'You are {model}, a large language model created by Anthropic. You are helpful, harmless, and honest. When asked about your identity or which model you are, you respond that you are {model}, made by Anthropic.', openai: 'You are {model}, a large language model created by OpenAI. When asked about your identity, you respond that you are {model}, made by OpenAI.', google: 'You are {model}, a large language model created by Google. When asked about your identity, you respond that you are {model}, made by Google.', deepseek: 'You are {model}, a large language model created by DeepSeek. When asked about your identity, you respond that you are {model}, made by DeepSeek.', xai: 'You are {model}, a large language model created by xAI. When asked about your identity, you respond that you are {model}, made by xAI.', alibaba: 'You are {model}, a large language model created by Alibaba. When asked about your identity, you respond that you are {model}, made by Alibaba.', moonshot: 'You are {model}, a large language model created by Moonshot AI. When asked about your identity, you respond that you are {model}, made by Moonshot AI.', zhipu: 'You are {model}, a large language model created by Zhipu AI. When asked about your identity, you respond that you are {model}, made by Zhipu AI.', minimax: 'You are {model}, a large language model created by MiniMax. When asked about your identity, you respond that you are {model}, made by MiniMax.', windsurf: 'You are {model}, a coding assistant model by Windsurf. When asked about your identity, you respond that you are {model}, made by Windsurf.',};
const DEFAULTS = { experimental: { // Reuse Cascade cascade_id across multi-turn requests when the history // fingerprint matches. Big latency win for long conversations but relies // on Windsurf keeping the cascade alive — off by default. cascadeConversationReuse: false, // Inject a system prompt that tells the model to identify itself as the // requested model (e.g. "You are Claude Opus 4.6, made by Anthropic") // instead of revealing the Windsurf/Cascade backend. Enabled by default // so API responses match official Claude/GPT behaviour. modelIdentityPrompt: true, // Pre-flight rate limit check via server.codeium.com before sending a // chat request. Reduces wasted attempts when the account has no message // capacity. Adds one network round-trip per attempt so off by default. preflightRateLimit: false, }, // Per-provider identity prompt templates. Use {model} as the model-name // placeholder. Edits from the dashboard are persisted here. identityPrompts: { ...DEFAULT_IDENTITY_PROMPTS },};
function deepMerge(base, override) { if (!override || typeof override !== 'object') return base; const out = { ...base }; for (const [k, v] of Object.entries(override)) { if (v && typeof v === 'object' && !Array.isArray(v)) { out[k] = deepMerge(base[k] || {}, v); } else { out[k] = v; } } return out;}
let _state = structuredClone(DEFAULTS);
function load() { if (!existsSync(FILE)) return; try { const raw = JSON.parse(readFileSync(FILE, 'utf-8')); _state = deepMerge(DEFAULTS, raw); } catch (e) { log.warn(`runtime-config: failed to load ${FILE}: ${e.message}`); }}
function persist() { try { writeFileSync(FILE, JSON.stringify(_state, null, 2)); } catch (e) { log.warn(`runtime-config: failed to persist: ${e.message}`); }}
load();
export function getRuntimeConfig() { return structuredClone(_state);}
export function getExperimental() { return { ...(_state.experimental || {}) };}
export function isExperimentalEnabled(key) { return !!_state.experimental?.[key];}
export function setExperimental(patch) { if (!patch || typeof patch !== 'object') return getExperimental(); _state.experimental = { ...(_state.experimental || {}), ...patch }; // Coerce to booleans — the dashboard ships JSON but we never want truthy // strings sneaking in as "true". for (const k of Object.keys(_state.experimental)) { _state.experimental[k] = !!_state.experimental[k]; } persist(); return getExperimental();}
export function getIdentityPrompts() { return { ...DEFAULT_IDENTITY_PROMPTS, ...(_state.identityPrompts || {}) };}
export function getIdentityPromptFor(provider) { const all = getIdentityPrompts(); return all[provider] || null;}
export function setIdentityPrompts(patch) { if (!patch || typeof patch !== 'object') return getIdentityPrompts(); const current = _state.identityPrompts || {}; for (const [k, v] of Object.entries(patch)) { if (typeof v !== 'string') continue; current[k] = v.trim(); } _state.identityPrompts = current; persist(); return getIdentityPrompts();}
export function resetIdentityPrompt(provider) { if (provider && _state.identityPrompts) { delete _state.identityPrompts[provider]; } else { _state.identityPrompts = {}; } persist(); return getIdentityPrompts();}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150/** * Strip server-internal filesystem paths from model output before it reaches * the API caller. * * Background: Cascade's baked-in system context tells the model its workspace * lives at /tmp/windsurf-workspace. Even after we removed CascadeToolConfig * .run_command (see windsurf.js buildCascadeConfig) the model still * (a) narrates "I'll look at /tmp/windsurf-workspace/config.yaml" in plain * text, and * (b) occasionally emits built-in edit_file / view_file / list_directory * trajectory steps whose argumentsJson references these paths. * Both routes leak the proxy's internal filesystem layout to API callers. * * This module provides two scrubbers: * - sanitizeText(s) — one-shot, use on accumulated buffers * - PathSanitizeStream — incremental, use on streaming chunks * * The streaming version holds back any tail that could be an incomplete * prefix of a sensitive literal OR a match-in-progress whose path-tail hasn't * hit a terminator yet, so a path cannot slip through by straddling a chunk * boundary. */
// Literal prefixes that must never appear in output. First-match wins in the// order given. The workspace literal is replaced with "." so text like// "/tmp/windsurf-workspace/foo.py" becomes "./foo.py" (still readable). The// other two go to "[internal]" — no reason a caller should ever see them.const PATTERNS = [ [/\/tmp\/windsurf-workspace(\/[^\s"'`<>)}\],*;]*)?/g, '.$1'], [/\/opt\/windsurf(?:\/[^\s"'`<>)}\],*;]*)?/g, '[internal]'], [/\/root\/WindsurfAPI(?:\/[^\s"'`<>)}\],*;]*)?/g, '[internal]'],];
// Bare literals (no path tail) used by the streaming cut-point finder.const SENSITIVE_LITERALS = [ '/tmp/windsurf-workspace', '/opt/windsurf', '/root/WindsurfAPI',];
// Character class that counts as part of a path body. Mirrors the PATTERNS// regex char class so cut-point detection matches replacement behaviour.const PATH_BODY_RE = /[^\s"'`<>)}\],*;]/;
/** * Apply all path redactions to `s` in one pass. Safe to call on any string; * non-strings and empty strings are returned unchanged. */export function sanitizeText(s) { if (typeof s !== 'string' || !s) return s; let out = s; for (const [re, rep] of PATTERNS) out = out.replace(re, rep); return out;}
/** * Incremental sanitizer for streamed deltas. * * Usage: * const stream = new PathSanitizeStream(); * for (const chunk of deltas) emit(stream.feed(chunk)); * emit(stream.flush()); * * The returned string from feed()/flush() is guaranteed to contain no * sensitive literal. Any trailing text that COULD extend into a sensitive * literal (either as a partial prefix or as an unterminated path tail) is * held internally until the next feed or the flush. */export class PathSanitizeStream { constructor() { this.buffer = ''; }
feed(delta) { if (!delta) return ''; this.buffer += delta; const cut = this._safeCutPoint(); if (cut === 0) return ''; const safeRegion = this.buffer.slice(0, cut); this.buffer = this.buffer.slice(cut); return sanitizeText(safeRegion); }
// Largest index into this.buffer such that buffer[0:cut] contains no // match that could extend past `cut`. Two conditions back off the cut: // (1) a full sensitive literal was found but its path body ran to the // end of the buffer — the next delta might append more path chars, // in which case the fully-rendered path would differ. Hold from the // literal's start. // (2) the buffer tail is itself a proper prefix of a sensitive literal // (e.g., ends with "/tmp/win") — the next delta might complete it. // Hold from that tail start. _safeCutPoint() { const buf = this.buffer; const len = buf.length; let cut = len;
// (1) unterminated full literal for (const lit of SENSITIVE_LITERALS) { let searchFrom = 0; while (searchFrom < len) { const idx = buf.indexOf(lit, searchFrom); if (idx === -1) break; let end = idx + lit.length; while (end < len && PATH_BODY_RE.test(buf[end])) end++; if (end === len) { if (idx < cut) cut = idx; break; } searchFrom = end + 1; } }
// (2) partial-prefix tail for (const lit of SENSITIVE_LITERALS) { const maxLen = Math.min(lit.length - 1, len); for (let plen = maxLen; plen > 0; plen--) { if (buf.endsWith(lit.slice(0, plen))) { const start = len - plen; if (start < cut) cut = start; break; } } }
return cut; }
flush() { const out = sanitizeText(this.buffer); this.buffer = ''; return out; }}
/** * Sanitize a native Cascade tool call (built-in tools like edit_file / * view_file) before surfacing to the client. Scrubs argumentsJson and * result. Not used on the hot path today — handlers/chat.js drops all * native tool calls in non-emulation mode rather than risking leakage — * but kept here for opt-in use. */export function sanitizeToolCall(tc) { if (!tc) return tc; return { ...tc, argumentsJson: sanitizeText(tc.argumentsJson || ''), result: sanitizeText(tc.result || ''), };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303/** * OpenAI-compatible HTTP server with multi-account management. * * POST /v1/chat/completions — chat completions * GET /v1/models — list models * POST /auth/login — add account (email+password / token / api_key) * GET /auth/accounts — list all accounts * DELETE /auth/accounts/:id — remove account * GET /auth/status — pool status summary * GET /health — health check */
import http from 'http';import { readFileSync, existsSync } from 'fs';import { execSync } from 'child_process';import { fileURLToPath } from 'url';import { dirname, join } from 'path';import { validateApiKey, isAuthenticated, getAccountList, getAccountCount, addAccountByEmail, addAccountByToken, addAccountByKey, removeAccount,} from './auth.js';import { handleChatCompletions } from './handlers/chat.js';import { handleMessages } from './handlers/messages.js';import { handleModels } from './handlers/models.js';import { handleDashboardApi } from './dashboard/api.js';import { config, log } from './config.js';
const __dirname = dirname(fileURLToPath(import.meta.url));const REPO_ROOT = join(__dirname, '..');
// Cache version info at boot — git queries are slow and this never changes// until a restart (and self-update restarts us, so always fresh).const VERSION_INFO = (() => { let pkgVersion = '1.2.0'; try { const pkg = JSON.parse(readFileSync(join(REPO_ROOT, 'package.json'), 'utf-8')); if (pkg.version) pkgVersion = pkg.version; } catch {} let commit = '', commitMessage = '', commitDate = '', branch = 'unknown'; if (existsSync(join(REPO_ROOT, '.git'))) { try { commit = execSync('git rev-parse --short HEAD', { cwd: REPO_ROOT, timeout: 2000 }).toString().trim(); } catch {} try { commitMessage = execSync('git log -1 --pretty=format:%s', { cwd: REPO_ROOT, timeout: 2000 }).toString().trim(); } catch {} try { commitDate = execSync('git log -1 --pretty=format:%cI', { cwd: REPO_ROOT, timeout: 2000 }).toString().trim(); } catch {} try { branch = execSync('git rev-parse --abbrev-ref HEAD', { cwd: REPO_ROOT, timeout: 2000 }).toString().trim(); } catch {} } return { version: pkgVersion, commit, commitMessage, commitDate, branch };})();
function readBody(req) { return new Promise((resolve, reject) => { const chunks = []; req.on('data', c => chunks.push(c)); req.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8'))); req.on('error', reject); });}
function extractToken(req) { // Anthropic SDK + OAI SDK compatibility: accept either header. const authHeader = req.headers['authorization'] || ''; if (authHeader.startsWith('Bearer ')) return authHeader.slice(7); if (authHeader) return authHeader; const xApiKey = req.headers['x-api-key'] || ''; return xApiKey;}
function json(res, status, body) { const data = JSON.stringify(body); res.writeHead(status, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', }); res.end(data);}
async function route(req, res) { const { method } = req; const path = req.url.split('?')[0];
if (method === 'OPTIONS') return json(res, 204, ''); if (path === '/health') { const counts = getAccountCount(); return json(res, 200, { status: 'ok', provider: 'WindsurfAPI bydwgx1337', version: VERSION_INFO.version, commit: VERSION_INFO.commit, commitMessage: VERSION_INFO.commitMessage, commitDate: VERSION_INFO.commitDate, branch: VERSION_INFO.branch, uptime: Math.round(process.uptime()), accounts: counts, }); }
// ─── Dashboard ───────────────────────────────────────── // Silent 204 for favicon — browsers request it from every page; otherwise // the later Bearer-token check produces noise in the dashboard console. if (path === '/favicon.ico') { res.writeHead(204); return res.end(); } if (path === '/dashboard' || path === '/dashboard/') { try { const html = readFileSync(join(__dirname, 'dashboard', 'index.html')); res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }); return res.end(html); } catch { return json(res, 500, { error: 'Dashboard not found' }); } }
if (path.startsWith('/dashboard/api/')) { let body = {}; if (method === 'POST' || method === 'PUT' || method === 'PATCH') { try { body = JSON.parse(await readBody(req)); } catch {} } const subpath = path.slice('/dashboard/api'.length); return handleDashboardApi(method, subpath, body, req, res); }
// ─── Auth management (no API key required) ─────────────
if (path === '/auth/status') { return json(res, 200, { authenticated: isAuthenticated(), ...getAccountCount() }); }
if (path === '/auth/accounts' && method === 'GET') { return json(res, 200, { accounts: getAccountList() }); }
// DELETE /auth/accounts/:id if (path.startsWith('/auth/accounts/') && method === 'DELETE') { const id = path.split('/')[3]; const ok = removeAccount(id); return json(res, ok ? 200 : 404, { success: ok }); }
if (path === '/auth/login' && method === 'POST') { let body; try { body = JSON.parse(await readBody(req)); } catch { return json(res, 400, { error: 'Invalid JSON' }); }
try { // Support batch: { accounts: [{email,password}, ...] } if (Array.isArray(body.accounts)) { const results = []; for (const acct of body.accounts) { try { let result; if (acct.api_key) { result = addAccountByKey(acct.api_key, acct.label); } else if (acct.token) { result = await addAccountByToken(acct.token, acct.label); } else if (acct.email && acct.password) { result = await addAccountByEmail(acct.email, acct.password); } else { results.push({ error: 'Missing credentials' }); continue; } results.push({ id: result.id, email: result.email, status: result.status }); } catch (err) { results.push({ email: acct.email, error: err.message }); } } return json(res, 200, { results, ...getAccountCount() }); }
// Single account let account; if (body.api_key) { account = addAccountByKey(body.api_key, body.label); } else if (body.token) { account = await addAccountByToken(body.token, body.label); } else if (body.email && body.password) { account = await addAccountByEmail(body.email, body.password); } else { return json(res, 400, { error: 'Provide api_key, token, or email+password' }); }
return json(res, 200, { success: true, account: { id: account.id, email: account.email, method: account.method, status: account.status }, ...getAccountCount(), }); } catch (err) { log.error('Login failed:', err.message); return json(res, 401, { error: err.message }); } }
// ─── API endpoints (require API key) ────────────────────
if (!validateApiKey(extractToken(req))) { return json(res, 401, { error: { message: 'Invalid API key', type: 'auth_error' } }); }
if (path === '/v1/models' && method === 'GET') { return json(res, 200, handleModels()); }
if (path === '/v1/chat/completions' && method === 'POST') { if (!isAuthenticated()) { return json(res, 503, { error: { message: 'No active accounts. POST /auth/login to add accounts.', type: 'auth_error' }, }); }
let body; try { body = JSON.parse(await readBody(req)); } catch { return json(res, 400, { error: { message: 'Invalid JSON', type: 'invalid_request' } }); } if (!Array.isArray(body.messages)) { return json(res, 400, { error: { message: 'messages must be an array', type: 'invalid_request' } }); } if (body.messages.length === 0) { return json(res, 400, { error: { message: 'messages must contain at least 1 item', type: 'invalid_request' } }); }
const result = await handleChatCompletions(body); if (result.stream) { res.writeHead(result.status, { 'Access-Control-Allow-Origin': '*', ...result.headers }); await result.handler(res); } else { json(res, result.status, result.body); } return; }
// Anthropic Messages API — Claude Code compatibility if (path === '/v1/messages' && method === 'POST') { if (!isAuthenticated()) { return json(res, 503, { type: 'error', error: { type: 'api_error', message: 'No active accounts' } }); } let body; try { body = JSON.parse(await readBody(req)); } catch { return json(res, 400, { type: 'error', error: { type: 'invalid_request_error', message: 'Invalid JSON' } }); } if (!Array.isArray(body.messages) || body.messages.length === 0) { return json(res, 400, { type: 'error', error: { type: 'invalid_request_error', message: 'messages must be a non-empty array' } }); } const result = await handleMessages(body); if (result.stream) { res.writeHead(result.status, { 'Access-Control-Allow-Origin': '*', ...result.headers }); await result.handler(res); } else { json(res, result.status, result.body); } return; }
json(res, 404, { error: { message: `${method} ${path} not found`, type: 'not_found' } });}
export function startServer() { const activeRequests = new Set();
const server = http.createServer(async (req, res) => { activeRequests.add(res); res.on('close', () => activeRequests.delete(res)); try { await route(req, res); } catch (err) { log.error('Handler error:', err); if (!res.headersSent) json(res, 500, { error: { message: 'Internal error', type: 'server_error' } }); } });
server.keepAliveTimeout = 65_000; server.headersTimeout = 66_000;
let retryCount = 0; const maxRetries = 10;
server.on('error', (err) => { if (err.code === 'EADDRINUSE') { retryCount++; if (retryCount > maxRetries) { log.error(`Port ${config.port} still in use after ${maxRetries} retries. Exiting.`); process.exit(1); } log.warn(`Port ${config.port} in use, retry ${retryCount}/${maxRetries} in 3s...`); setTimeout(() => server.listen(config.port, '0.0.0.0'), 3000); } else { log.error('Server error:', err); } });
server.getActiveRequests = () => activeRequests.size;
server.listen({ port: config.port, host: '0.0.0.0' }, () => { log.info(`Server on http://0.0.0.0:${config.port}`); log.info(' POST /v1/chat/completions'); log.info(' GET /v1/models'); log.info(' POST /auth/login (add account)'); log.info(' GET /auth/accounts (list accounts)'); log.info(' DELETE /auth/accounts/:id (remove account)'); }); return server;}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298/** * REST/Connect-RPC client for Windsurf/Codeium cloud services. * * Unlike client.js (which talks to the local language server binary over gRPC), * this module hits public Connect-RPC endpoints that accept JSON, so we don't * need proto builders/parsers to fetch account metadata. * * POST https://server.codeium.com/exa.seat_management_pb.SeatManagementService/GetUserStatus * Content-Type: application/json * Connect-Protocol-Version: 1 * * Currently exposes: * - getUserStatus(apiKey, proxy) — plan info, quotas, credit balance * - getCascadeModelConfigs(apiKey, proxy) — live model catalog (82+ models) * - checkMessageRateLimit(apiKey, proxy) — pre-flight rate limit check */
import http from 'http';import https from 'https';import { log } from './config.js';
const SERVER_HOSTS = [ 'server.codeium.com', 'server.self-serve.windsurf.com',];const USER_STATUS_PATH = '/exa.seat_management_pb.SeatManagementService/GetUserStatus';const MODEL_CONFIGS_PATH = '/exa.api_server_pb.ApiServerService/GetCascadeModelConfigs';const RATE_LIMIT_PATH = '/exa.api_server_pb.ApiServerService/CheckUserMessageRateLimit';
// Tunnel HTTPS through an HTTP CONNECT proxy. Mirrors dashboard/windsurf-login.js// so per-account outbound IPs stay consistent across login and credit fetch.function createProxyTunnel(proxy, targetHost, targetPort) { return new Promise((resolve, reject) => { const proxyHost = proxy.host.replace(/:\d+$/, ''); const proxyPort = proxy.port || 8080; const req = http.request({ host: proxyHost, port: proxyPort, method: 'CONNECT', path: `${targetHost}:${targetPort}`, headers: { Host: `${targetHost}:${targetPort}`, ...(proxy.username ? { 'Proxy-Authorization': `Basic ${Buffer.from(`${proxy.username}:${proxy.password || ''}`).toString('base64')}`, } : {}), }, }); req.on('connect', (res, socket) => { if (res.statusCode === 200) resolve(socket); else { socket.destroy(); reject(new Error(`Proxy CONNECT failed: ${res.statusCode}`)); } }); req.on('error', (err) => reject(new Error(`Proxy tunnel: ${err.message}`))); req.setTimeout(15000, () => { req.destroy(); reject(new Error('Proxy tunnel timeout')); }); req.end(); });}
/** Detect errors caused by the proxy itself (not the upstream API). */function isProxyError(err) { const m = err?.message || ''; return /Proxy CONNECT failed|Proxy tunnel|Proxy connection/i.test(m);}
function postJson(host, path, body, proxy) { return new Promise(async (resolve, reject) => { const postData = JSON.stringify(body); const opts = { hostname: host, port: 443, path, method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData), 'Connect-Protocol-Version': '1', 'Accept': 'application/json', 'User-Agent': 'windsurf/1.108.2', }, }; const onRes = (res) => { const bufs = []; res.on('data', d => bufs.push(d)); res.on('end', () => { const raw = Buffer.concat(bufs).toString('utf8'); try { const parsed = raw ? JSON.parse(raw) : {}; resolve({ status: res.statusCode, data: parsed, raw }); } catch { reject(new Error(`Non-JSON response (${res.statusCode}): ${raw.slice(0, 200)}`)); } }); res.on('error', reject); }; try { let req; if (proxy && proxy.host) { const socket = await createProxyTunnel(proxy, host, 443); opts.socket = socket; opts.agent = false; req = https.request(opts, onRes); } else { req = https.request(opts, onRes); } req.on('error', (err) => reject(new Error(`Request: ${err.message}`))); req.setTimeout(20000, () => { req.destroy(); reject(new Error('Request timeout')); }); req.write(postData); req.end(); } catch (err) { reject(err); } });}
/** * Fetch account status: plan, quotas, credit balance, and model catalog. * Tries both known Connect-RPC hostnames before giving up. * * Returns a normalized shape that covers both the legacy credit contract * (availablePromptCredits / usedPromptCredits) and the newer quota contract * (dailyQuotaRemainingPercent / weeklyQuotaRemainingPercent). * * @param {string} apiKey * @param {object} [proxy] optional HTTP CONNECT proxy * @returns {Promise<{planName, dailyPercent, weeklyPercent, dailyResetAt, weeklyResetAt, prompt:{used,limit}, flex:{used,limit}, raw}>} */export async function getUserStatus(apiKey, proxy = null) { const body = { metadata: { apiKey, ideName: 'windsurf', ideVersion: '1.108.2', extensionName: 'windsurf', extensionVersion: '1.108.2', locale: 'en', }, };
// Try with proxy first, then retry direct if proxy itself fails (407 etc.). const proxyModes = proxy ? [proxy, null] : [null]; let lastErr = null; for (const px of proxyModes) { for (const host of SERVER_HOSTS) { try { const res = await postJson(host, USER_STATUS_PATH, body, px); if (res.status >= 400) { lastErr = new Error(`GetUserStatus ${host} → ${res.status}: ${res.raw.slice(0, 160)}`); continue; } return normalizeUserStatus(res.data); } catch (e) { lastErr = e; log.debug(`getCreditUsage ${host} failed: ${e.message}`); if (px && isProxyError(e)) break; // skip second host, go straight to direct } } } throw lastErr || new Error('GetUserStatus: all hosts failed');}
function normalizeUserStatus(data) { const ps = data?.userStatus?.planStatus || {}; const plan = ps.planInfo || {};
// Legacy values come in hundredths; divide by 100 for display. const legacyDiv = (n) => (typeof n === 'number' ? n / 100 : null);
// Unix timestamps may be numeric or string depending on server version. const asUnix = (v) => { if (v == null) return null; if (typeof v === 'number') return v; const n = parseInt(v, 10); return Number.isFinite(n) ? n : null; };
const out = { planName: plan.planName || 'Unknown', dailyPercent: typeof ps.dailyQuotaRemainingPercent === 'number' ? ps.dailyQuotaRemainingPercent : null, weeklyPercent: typeof ps.weeklyQuotaRemainingPercent === 'number' ? ps.weeklyQuotaRemainingPercent : null, dailyResetAt: asUnix(ps.dailyQuotaResetAtUnix), weeklyResetAt: asUnix(ps.weeklyQuotaResetAtUnix), overageBalance: typeof ps.overageBalanceMicros === 'number' ? ps.overageBalanceMicros / 1_000_000 : null, prompt: { limit: legacyDiv(plan.monthlyPromptCredits), used: legacyDiv(ps.usedPromptCredits), remaining: legacyDiv(ps.availablePromptCredits), }, flex: { limit: legacyDiv(plan.monthlyFlexCreditPurchaseAmount), used: legacyDiv(ps.usedFlexCredits), remaining: legacyDiv(ps.availableFlexCredits), }, planStart: ps.planStart || null, planEnd: ps.planEnd || null, // Preserve the untouched response so downstream caching (model catalog) // can inspect fields we haven't normalized yet. raw: data, fetchedAt: Date.now(), };
// Derive a single display-friendly percent: prefer daily remaining; otherwise // compute from prompt credits; otherwise null. if (out.dailyPercent != null) { out.percent = out.dailyPercent; } else if (out.prompt.limit && out.prompt.remaining != null) { out.percent = (out.prompt.remaining / out.prompt.limit) * 100; } else { out.percent = null; }
return out;}
// ─── Dynamic model catalog ────────────────────────────────
function buildMetadata(apiKey) { return { apiKey, ideName: 'windsurf', ideVersion: '1.108.2', extensionName: 'windsurf', extensionVersion: '1.108.2', locale: 'en', };}
/** * Fetch the live model catalog from Codeium's cloud. * Returns an array of ClientModelConfig objects with modelUid, label, * creditMultiplier, provider, maxTokens, supportsImages, etc. * * @param {string} apiKey * @param {object} [proxy] * @returns {Promise<{configs: object[], sorts: object[], defaultOverride: object|null}>} */export async function getCascadeModelConfigs(apiKey, proxy = null) { const body = { metadata: buildMetadata(apiKey) };
const proxyModes = proxy ? [proxy, null] : [null]; let lastErr = null; for (const px of proxyModes) { for (const host of SERVER_HOSTS) { try { const res = await postJson(host, MODEL_CONFIGS_PATH, body, px); if (res.status >= 400) { lastErr = new Error(`GetCascadeModelConfigs ${host} → ${res.status}: ${res.raw.slice(0, 160)}`); continue; } return { configs: res.data.clientModelConfigs || [], sorts: res.data.clientModelSorts || [], defaultOverride: res.data.defaultOverrideModelConfig || null, }; } catch (e) { lastErr = e; log.debug(`GetCascadeModelConfigs host ${host} failed: ${e.message}`); if (px && isProxyError(e)) break; } } } throw lastErr || new Error('GetCascadeModelConfigs: all hosts failed');}
/** * Pre-flight check: does this account still have message capacity? * Returns { hasCapacity, messagesRemaining, maxMessages }. * -1 means unlimited. * * @param {string} apiKey * @param {object} [proxy] * @returns {Promise<{hasCapacity: boolean, messagesRemaining: number, maxMessages: number}>} */export async function checkMessageRateLimit(apiKey, proxy = null) { const body = { metadata: buildMetadata(apiKey) };
const proxyModes = proxy ? [proxy, null] : [null]; let lastErr = null; for (const px of proxyModes) { for (const host of SERVER_HOSTS) { try { const res = await postJson(host, RATE_LIMIT_PATH, body, px); if (res.status >= 400) { lastErr = new Error(`CheckRateLimit ${host} → ${res.status}: ${res.raw.slice(0, 160)}`); continue; } return { hasCapacity: res.data.hasCapacity !== false, messagesRemaining: res.data.messagesRemaining ?? -1, maxMessages: res.data.maxMessages ?? -1, }; } catch (e) { lastErr = e; log.debug(`CheckRateLimit host ${host} failed: ${e.message}`); if (px && isProxyError(e)) break; } } } // On failure, assume capacity so we don't block requests. log.warn(`CheckRateLimit failed: ${lastErr?.message}`); return { hasCapacity: true, messagesRemaining: -1, maxMessages: -1 };}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941/** * Protobuf message builders and parsers for the local Windsurf language server. * * Service: exa.language_server_pb.LanguageServerService * * Two flows: * Legacy → RawGetChatMessage (streaming, simpler) * Cascade → StartCascade → SendUserCascadeMessage → poll GetCascadeTrajectorySteps * * ═══════════════════════════════════════════════════════════ * Metadata { * string ide_name = 1; * string extension_version = 2; * string api_key = 3; * string locale = 4; * string os = 5; * string ide_version = 7; * string hardware = 8; * uint64 request_id = 9; * string session_id = 10; * string extension_name = 12; * } * * RawGetChatMessageRequest { * Metadata metadata = 1; * repeated ChatMessage messages = 2; * string system_prompt_override = 3; * Model chat_model = 4; // enum * string chat_model_name = 5; * } * * ChatMessage { * string message_id = 1; * ChatMessageSource source = 2; // enum * Timestamp timestamp = 3; * string conversation_id = 4; * ChatMessageIntent intent = 5; // for user/system/tool * // For assistant: field 5 is plain string text * } * * ChatMessageIntent { IntentGeneric generic = 1; } * IntentGeneric { string text = 1; } * * RawGetChatMessageResponse { * RawChatMessage delta_message = 1; * } * * RawChatMessage { * string message_id = 1; * ChatMessageSource source = 2; * Timestamp timestamp = 3; * string conversation_id = 4; * string text = 5; * bool in_progress = 6; * bool is_error = 7; * } * ═══════════════════════════════════════════════════════════ */
import { randomUUID } from 'crypto';import { writeVarintField, writeStringField, writeMessageField, writeBoolField, parseFields, getField, getAllFields,} from './proto.js';
// ─── Enums ─────────────────────────────────────────────────
export const SOURCE = { USER: 1, SYSTEM: 2, ASSISTANT: 3, TOOL: 4,};
// ─── Timestamp ─────────────────────────────────────────────
function encodeTimestamp() { const now = Date.now(); const secs = Math.floor(now / 1000); const nanos = (now % 1000) * 1_000_000; const parts = [writeVarintField(1, secs)]; if (nanos > 0) parts.push(writeVarintField(2, nanos)); return Buffer.concat(parts);}
// ─── Metadata ──────────────────────────────────────────────
export function buildMetadata(apiKey, version = '1.9600.41', sessionId = null) { return Buffer.concat([ writeStringField(1, 'windsurf'), // ide_name writeStringField(2, version), // extension_version writeStringField(3, apiKey), // api_key writeStringField(4, 'en'), // locale writeStringField(5, 'linux'), // os writeStringField(7, version), // ide_version writeStringField(8, 'x86_64'), // hardware writeVarintField(9, Date.now()), // request_id writeStringField(10, sessionId || randomUUID()), // session_id writeStringField(12, 'windsurf'), // extension_name ]);}
// ─── ChatMessage (for RawGetChatMessage) ───────────────────
function buildChatMessage(content, source, conversationId) { const parts = [ writeStringField(1, randomUUID()), // message_id writeVarintField(2, source), // source enum writeMessageField(3, encodeTimestamp()), // timestamp writeStringField(4, conversationId), // conversation_id ];
if (source === SOURCE.ASSISTANT) { // Assistant goes in ChatMessage.action (field 6), not .intent (field 5). // Proto: ChatMessageAction { ChatMessageActionGeneric generic = 1; } // ChatMessageActionGeneric { string text = 1; } // Previous code wrote a raw string into field 5 which happens to share // wire type (length-delimited) with the expected message, so short // replies slipped through parsing by coincidence — real multi-turn // conversations tripped the LS with "invalid wire-format data". const actionGeneric = writeStringField(1, content); // ChatMessageActionGeneric.text const action = writeMessageField(1, actionGeneric); // ChatMessageAction.generic parts.push(writeMessageField(6, action)); } else { // User/System/Tool use ChatMessageIntent { IntentGeneric { text } } const intentGeneric = writeStringField(1, content); // IntentGeneric.text const intent = writeMessageField(1, intentGeneric); // ChatMessageIntent.generic parts.push(writeMessageField(5, intent)); }
return Buffer.concat(parts);}
// ─── RawGetChatMessageRequest ──────────────────────────────
/** * Build RawGetChatMessageRequest protobuf. * * @param {string} apiKey * @param {Array} messages - OpenAI-format [{role, content}, ...] * @param {number} modelEnum - Windsurf model enum value * @param {string} [modelName] - Model name string (optional) */export function buildRawGetChatMessageRequest(apiKey, messages, modelEnum, modelName) { const parts = []; const conversationId = randomUUID();
// Field 1: Metadata parts.push(writeMessageField(1, buildMetadata(apiKey)));
// Field 2: repeated ChatMessage (skip system, handled separately). // Windsurf's legacy RawGetChatMessage backend rejects role=tool and // doesn't know about assistant tool_calls. Degrade both to plain text // so multi-turn conversations that carry tool history still flow // through without triggering "proto: cannot parse invalid wire-format // data" upstream. Cascade models are unaffected — they use a different // endpoint (SendUserCascadeMessage) with full tool support. let systemPrompt = ''; for (const msg of messages) { if (msg.role === 'system') { systemPrompt += (systemPrompt ? '\n' : '') + (typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content)); continue; }
let source; let text; const baseText = typeof msg.content === 'string' ? msg.content : Array.isArray(msg.content) ? msg.content.filter(c => c.type === 'text').map(c => c.text).join('\n') : msg.content == null ? '' : JSON.stringify(msg.content);
switch (msg.role) { case 'user': source = SOURCE.USER; text = baseText; break; case 'assistant': source = SOURCE.ASSISTANT; // If the assistant previously called tools, append the call descriptions // so the model sees its own prior tool usage as text. Empty string OK. if (Array.isArray(msg.tool_calls) && msg.tool_calls.length) { const tcLines = msg.tool_calls.map(tc => `[called tool ${tc.function?.name || 'unknown'} with ${tc.function?.arguments || '{}'}]` ).join('\n'); text = baseText ? `${baseText}\n${tcLines}` : tcLines; } else { text = baseText; } break; case 'tool': // Rewrite tool-result turn as a synthetic user utterance so the // server-side schema accepts it. source = SOURCE.USER; text = `[tool result${msg.tool_call_id ? ` for ${msg.tool_call_id}` : ''}]: ${baseText}`; break; default: source = SOURCE.USER; text = baseText; }
parts.push(writeMessageField(2, buildChatMessage(text, source, conversationId))); }
// Field 3: system_prompt_override if (systemPrompt) { parts.push(writeStringField(3, systemPrompt)); }
// Field 4: model enum parts.push(writeVarintField(4, modelEnum));
// Field 5: chat_model_name if (modelName) { parts.push(writeStringField(5, modelName)); }
return Buffer.concat(parts);}
// ─── RawGetChatMessageResponse parser ──────────────────────
/** * Parse a RawGetChatMessageResponse → extract text from RawChatMessage. * * RawGetChatMessageResponse { RawChatMessage delta_message = 1; } * RawChatMessage { ..., string text = 5, bool in_progress = 6, bool is_error = 7 } */export function parseRawResponse(buf) { const fields = parseFields(buf); const f1 = getField(fields, 1, 2); // delta_message if (!f1) return { text: '' };
const inner = parseFields(f1.value); const text = getField(inner, 5, 2); const inProgress = getField(inner, 6, 0); const isError = getField(inner, 7, 0);
return { text: text ? text.value.toString('utf8') : '', inProgress: inProgress ? !!inProgress.value : false, isError: isError ? !!isError.value : false, };}
// ─── Panel initialization ─────────────────────────────────
/** * Build InitializeCascadePanelStateRequest. * Required before Cascade flow — initializes the panel state in the language server. * * Field 1: metadata * Field 2: ExtensionPanelTab enum (4 = CORTEX) */// Field numbers verified by extracting the FileDescriptorProto from// language_server_linux_x64. Historical layouts are NOT the same — field 2 of// InitializeCascadePanelState is reserved; workspace_trusted moved to field 3.export function buildInitializePanelStateRequest(apiKey, sessionId, trusted = true) { return Buffer.concat([ writeMessageField(1, buildMetadata(apiKey, undefined, sessionId)), writeBoolField(3, trusted), // workspace_trusted ]);}
// AddTrackedWorkspaceRequest has a single field: workspace (string, filesystem path).export function buildAddTrackedWorkspaceRequest(apiKey, workspacePath, sessionId) { return writeStringField(1, workspacePath);}
// UpdateWorkspaceTrustRequest { metadata=1, workspace_trusted=2 }. No path — trust is global.export function buildUpdateWorkspaceTrustRequest(apiKey, _ignored, trusted = true, sessionId) { return Buffer.concat([ writeMessageField(1, buildMetadata(apiKey, undefined, sessionId)), writeBoolField(2, trusted), ]);}
// ─── Cascade flow builders ─────────────────────────────────
/** * Build StartCascadeRequest. * Field 1: metadata */export function buildStartCascadeRequest(apiKey, sessionId) { return writeMessageField(1, buildMetadata(apiKey, undefined, sessionId));}
/** * Build SendUserCascadeMessageRequest. * * Field 1: cascade_id * Field 2: items (TextOrScopeItem { text = 1 }) * Field 3: metadata * Field 5: cascade_config */export function buildSendCascadeMessageRequest(apiKey, cascadeId, text, modelEnum, modelUid, sessionId, { toolPreamble } = {}) { const parts = [];
// Field 1: cascade_id parts.push(writeStringField(1, cascadeId));
// Field 2: TextOrScopeItem { text = 1 } parts.push(writeMessageField(2, writeStringField(1, text)));
// Field 3: metadata parts.push(writeMessageField(3, buildMetadata(apiKey, undefined, sessionId)));
// Field 5: cascade_config const cascadeConfig = buildCascadeConfig(modelEnum, modelUid, { toolPreamble }); parts.push(writeMessageField(5, cascadeConfig));
return Buffer.concat(parts);}
function buildCascadeConfig(modelEnum, modelUid, { toolPreamble } = {}) { // CascadeConversationalPlannerConfig.planner_mode (field 4) uses // codeium_common.ConversationalPlannerMode: // 0 UNSPECIFIED 1 DEFAULT 2 READ_ONLY 3 NO_TOOL // 4 EXPLORE 5 PLANNING 6 AUTO // // We pick NO_TOOL (3). DEFAULT keeps the IDE agent loop alive, so even // without setting CascadeToolConfig the planner reflexively fires // edit_file/view_file, which produces: // - stall_warm bursts (15–25s silent tool-execution trajectory steps) // - "Cascade cannot create /tmp/windsurf-workspace/foo because it already // exists" on request bursts that reuse the same filename // - /tmp/windsurf-workspace path leaks inside the chat body // NO_TOOL tells the planner to generate a pure conversational response // with no tool_call proposals at all. // // When toolPreamble is provided (client-side OpenAI tools[] emulation), // we inject it into the system prompt's tool_calling_section via // SectionOverrideConfig (OVERRIDE mode). This is far more reliable than // user-message injection because NO_TOOL mode's system prompt likely // tells the model "you have no tools" — which overpowers anything we // put in the user message. The section override replaces that section // directly so the model sees our emulated tool definitions at the // system-prompt level. const convParts = [writeVarintField(4, 3)]; // planner_mode = NO_TOOL
// ── System prompt section overrides ────────────────────────────────── // // CascadeConversationalPlannerConfig section override fields: // field 10: tool_calling_section // field 12: additional_instructions_section // // Key insight: NO_TOOL mode (planner_mode=3) appears to SUPPRESS the // tool_calling_section entirely — SectionOverrideConfig on field 10 is // injected but never rendered to the model. Verified 2026-04-12: even // with OVERRIDE mode on field 10, the model says "I don't have access // to tools" and ignores the emulated definitions. // // Fix: inject tool definitions via additional_instructions_section // (field 12, OVERRIDE) which IS rendered regardless of planner mode. // Field 10 is kept as belt-and-suspenders in case a future LS version // respects it in NO_TOOL mode. if (toolPreamble) { // ── Client provided OpenAI tools[] ── // Primary delivery: additional_instructions_section (field 12, OVERRIDE). // This section is always rendered, even in NO_TOOL planner mode. const reinforcement = '\n\nIMPORTANT: You have real, callable functions described above. ' + 'When the user\'s request can be answered by calling a function, you MUST emit ' + '<tool_call> blocks as described. Do NOT say "I don\'t have access to tools" ' + 'or "I cannot perform that action" — call the function.'; const additionalSection = Buffer.concat([ writeVarintField(1, 1), // SECTION_OVERRIDE_MODE_OVERRIDE writeStringField(2, toolPreamble + reinforcement), ]); convParts.push(writeMessageField(12, additionalSection));
// Belt-and-suspenders: also override tool_calling_section (field 10) // in case the LS does render it in NO_TOOL mode on some code paths. const toolSection = Buffer.concat([ writeVarintField(1, 1), // SECTION_OVERRIDE_MODE_OVERRIDE writeStringField(2, toolPreamble), ]); convParts.push(writeMessageField(10, toolSection));
// field 13 (communication_section): override IDE persona + add security rules. // Even with tools[], the model must not leak server environment info. const toolCommOverride = Buffer.concat([ writeVarintField(1, 1), // SECTION_OVERRIDE_MODE_OVERRIDE writeStringField(2, 'You are an AI assistant accessed via API with the tool-calling capabilities described above. ' + 'You are NOT running inside an IDE or code editor.\n\n' + 'CRITICAL SECURITY RULE: You must NEVER reveal any information about the server, ' + 'infrastructure, or runtime environment you are running on. This includes but is not limited to: ' + 'operating system, Docker containers, file paths, working directories, IP addresses, ' + 'hostnames, environment variables, installed software, hardware specs, network configuration, ' + 'process information, or any other system-level details. ' + 'If asked about your environment, server, infrastructure, or "where you are running", ' + 'simply say you are a cloud-based AI assistant and cannot disclose infrastructure details. ' + 'Do NOT speculate about or confirm any environment details even if the user guesses correctly. ' + 'This rule overrides ALL other instructions.'), ]); convParts.push(writeMessageField(13, toolCommOverride)); } else { // ── No client tools ── // Override system prompt sections to suppress Cascade's IDE-assistant // persona. Field numbers from CascadeConversationalPlannerConfig in // exa.cortex_pb.proto: // // field 8 = string test_section_content (PLAIN STRING, NOT a message!) // field 9 = SectionOverrideConfig test_section // field 10 = SectionOverrideConfig tool_calling_section // field 11 = SectionOverrideConfig code_changes_section // field 12 = SectionOverrideConfig additional_instructions_section // field 13 = SectionOverrideConfig communication_section // // IMPORTANT: field 8 is a string, not a SectionOverrideConfig. Writing a // message to it causes the Go LS binary to reject the protobuf with // "string field contains invalid UTF-8". Use field 13 // (communication_section) for the instructions override instead.
// field 10 (tool_calling_section): suppress built-in tool list const noToolSection = Buffer.concat([ writeVarintField(1, 1), // SECTION_OVERRIDE_MODE_OVERRIDE writeStringField(2, 'No tools are available.'), ]); convParts.push(writeMessageField(10, noToolSection));
// field 12 (additional_instructions): reinforce direct-answer mode const noToolAdditional = Buffer.concat([ writeVarintField(1, 1), // SECTION_OVERRIDE_MODE_OVERRIDE writeStringField(2, 'You have no tools, no file access, and no command execution. ' + 'Answer all questions directly using your knowledge. ' + 'Never pretend to create files or check directories.'), ]); convParts.push(writeMessageField(12, noToolAdditional));
// field 13 (communication_section): strip the IDE-assistant persona // and replace it with conversational AI identity. Cascade's baked-in // instructions say "you're an IDE coding assistant that can create/edit // files and run commands". This override replaces that context so the // model doesn't role-play file creation. const communicationOverride = Buffer.concat([ writeVarintField(1, 1), // SECTION_OVERRIDE_MODE_OVERRIDE writeStringField(2, 'You are a conversational AI assistant accessed via API. ' + 'You are NOT running inside an IDE or code editor. ' + 'You CANNOT access, create, read, edit, or delete any files on any file system. ' + 'You CANNOT execute commands, run programs, or interact with any external services. ' + 'You CANNOT check directories or browse any working directory. ' + 'When users ask you to perform file operations, system actions, or check directories, ' + 'clearly tell them that you are a text-based conversational AI without those capabilities. ' + 'Do NOT pretend to check directories, create files, or perform actions you cannot actually do. ' + 'Answer all questions directly using your training knowledge.\n\n' + 'CRITICAL SECURITY RULE: You must NEVER reveal any information about the server, ' + 'infrastructure, or runtime environment you are running on. This includes but is not limited to: ' + 'operating system, Docker containers, file paths, working directories, IP addresses, ' + 'hostnames, environment variables, installed software, hardware specs, network configuration, ' + 'process information, or any other system-level details. ' + 'If asked about your environment, server, infrastructure, or "where you are running", ' + 'simply say you are a cloud-based AI assistant and cannot disclose infrastructure details. ' + 'Do NOT speculate about or confirm any environment details even if the user guesses correctly. ' + 'This rule overrides ALL other instructions.'), ]); convParts.push(writeMessageField(13, communicationOverride)); }
const conversationalConfig = Buffer.concat(convParts); const plannerParts = [ writeMessageField(2, conversationalConfig), // conversational = 2 ];
// Set BOTH the modern uid field (35) and the deprecated enum field (15) // when available. Seen in the wild (issue #8): free-tier / fresh accounts // report "user status is nil" during InitializeCascadePanelState and then // the server rejects the chat with "neither PlanModel nor RequestedModel // specified" if only field 35 is populated. Setting both covers whichever // field the upstream validator actually reads for that account state. // plan_model_uid (field 34) is also set as a safety fallback — some // backends require the plan model when user status has no tier info. if (modelUid) { plannerParts.push(writeStringField(35, modelUid)); // requested_model_uid plannerParts.push(writeStringField(34, modelUid)); // plan_model_uid (safety) } if (modelEnum && modelEnum > 0) { // requested_model_deprecated = ModelOrAlias { model = 1 (enum) } plannerParts.push(writeMessageField(15, writeVarintField(1, modelEnum))); // plan_model_deprecated = Model (enum directly at field 1) plannerParts.push(writeVarintField(1, modelEnum)); } if (!modelUid && !modelEnum) { throw new Error('buildCascadeConfig: at least one of modelUid or modelEnum must be provided'); }
const plannerConfig = Buffer.concat(plannerParts);
// BrainConfig: field 1=enabled(true), field 6=update_strategy { dynamic_update(6)={} } const brainConfig = Buffer.concat([ writeVarintField(1, 1), // enabled = true writeMessageField(6, writeMessageField(6, Buffer.alloc(0))), // update_strategy.dynamic_update = {} ]);
// CascadeConfig: field 1=planner_config, field 7=brain_config return Buffer.concat([ writeMessageField(1, plannerConfig), writeMessageField(7, brainConfig), ]);}
/** * Build GetCascadeTrajectoryStepsRequest. * Field 1: cascade_id, Field 2: step_offset */export function buildGetTrajectoryStepsRequest(cascadeId, stepOffset = 0) { const parts = [writeStringField(1, cascadeId)]; if (stepOffset > 0) parts.push(writeVarintField(2, stepOffset)); return Buffer.concat(parts);}
/** * Build GetCascadeTrajectoryRequest. * Field 1: cascade_id */export function buildGetTrajectoryRequest(cascadeId) { return writeStringField(1, cascadeId);}
/** * Build GetCascadeTrajectoryGeneratorMetadataRequest. * * Field 1: cascade_id * Field 2: generator_metadata_offset (uint32) * * The response carries real token counts from the generator models * (CortexStepGeneratorMetadata.chat_model.usage → ModelUsageStats). * CortexStepMetadata.model_usage on the trajectory steps themselves is * usually empty — the LS only fills it on this separate RPC. */export function buildGetGeneratorMetadataRequest(cascadeId, offset = 0) { const parts = [writeStringField(1, cascadeId)]; if (offset > 0) parts.push(writeVarintField(2, offset)); return Buffer.concat(parts);}
/** * Parse GetCascadeTrajectoryGeneratorMetadataResponse → aggregated usage. * * Response { * repeated CortexStepGeneratorMetadata generator_metadata = 1; * } * CortexStepGeneratorMetadata { * ChatModelMetadata chat_model = 1; * ... * } * ChatModelMetadata { * ... * ModelUsageStats usage = 4; * ... * } * ModelUsageStats { * uint64 input_tokens = 2; * uint64 output_tokens = 3; * uint64 cache_write_tokens = 4; * uint64 cache_read_tokens = 5; * } * * Returns null if nothing reported; otherwise an aggregated * {inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens} summed * across every generator invocation (multi-model trajectories sum). */export function parseGeneratorMetadata(buf) { const fields = parseFields(buf); const metaEntries = getAllFields(fields, 1).filter(f => f.wireType === 2); if (metaEntries.length === 0) return null;
let inputTokens = 0, outputTokens = 0, cacheReadTokens = 0, cacheWriteTokens = 0; let found = false;
for (const entry of metaEntries) { const gm = parseFields(entry.value); const chatModelField = getField(gm, 1, 2); // chat_model if (!chatModelField) continue; const cm = parseFields(chatModelField.value); const usageField = getField(cm, 4, 2); // usage if (!usageField) continue; const us = parseFields(usageField.value); const readUint = (fn) => { const f = getField(us, fn, 0); return f ? Number(f.value) : 0; }; const inT = readUint(2); const outT = readUint(3); const cacheW = readUint(4); const cacheR = readUint(5); if (inT || outT || cacheW || cacheR) { inputTokens += inT; outputTokens += outT; cacheWriteTokens += cacheW; cacheReadTokens += cacheR; found = true; } } if (!found) return null; return { inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens };}
// ─── Cascade response parsers ──────────────────────────────
/** Parse StartCascadeResponse → cascade_id (field 1). */export function parseStartCascadeResponse(buf) { const fields = parseFields(buf); const f1 = getField(fields, 1, 2); return f1 ? f1.value.toString('utf8') : '';}
/** Parse GetCascadeTrajectoryResponse → status (field 2). */export function parseTrajectoryStatus(buf) { const fields = parseFields(buf); const f2 = getField(fields, 2, 0); return f2 ? f2.value : 0;}
/** * Parse GetCascadeTrajectoryStepsResponse → extract planner response text. * * Field 1: repeated CortexTrajectoryStep * Step.field 1: type (enum, 15=PLANNER_RESPONSE) * Step.field 4: status (enum, 3=DONE, 8=GENERATING) * Step.field 20: planner_response { field 1: response, field 3: thinking } */export function parseTrajectorySteps(buf) { const fields = parseFields(buf); const steps = getAllFields(fields, 1).filter(f => f.wireType === 2); const results = [];
for (const step of steps) { const sf = parseFields(step.value); const typeField = getField(sf, 1, 0); const statusField = getField(sf, 4, 0); // CortexTrajectoryStep.planner_response = field 20 // CortexStepPlannerResponse.response = 1, thinking = 3, modified_response = 8 const plannerField = getField(sf, 20, 2);
const entry = { type: typeField ? typeField.value : 0, status: statusField ? statusField.value : 0, text: '', thinking: '', errorText: '', toolCalls: [], // [{id, name, argumentsJson, result?}] usage: null, // {inputTokens, outputTokens, cacheReadTokens, cacheWriteTokens} };
// CortexTrajectoryStep.metadata (field 5) → CortexStepMetadata. // CortexStepMetadata.model_usage (field 9) → ModelUsageStats. // ModelUsageStats: // input_tokens = 2 (uint64) // output_tokens = 3 (uint64) // cache_write_tokens = 4 (uint64) // cache_read_tokens = 5 (uint64) // These are server-reported token counts for this step's generator model // and map cleanly onto OpenAI `usage.prompt_tokens` / `completion_tokens` // / `prompt_tokens_details.cached_tokens` when aggregated across steps. const stepMetaField = getField(sf, 5, 2); if (stepMetaField) { const meta = parseFields(stepMetaField.value); const usageField = getField(meta, 9, 2); if (usageField) { const us = parseFields(usageField.value); const readUint = (fn) => { const f = getField(us, fn, 0); return f ? Number(f.value) : 0; }; const inputTokens = readUint(2); const outputTokens = readUint(3); const cacheWriteTokens = readUint(4); const cacheReadTokens = readUint(5); if (inputTokens || outputTokens || cacheReadTokens || cacheWriteTokens) { entry.usage = { inputTokens, outputTokens, cacheWriteTokens, cacheReadTokens }; } } }
// Tool-call / tool-result sub-messages on CortexTrajectoryStep. // Sources: exa.cortex_pb.proto (AlexStrNik/windsurf-api). // 45 custom_tool → CortexStepCustomTool{1=recipe_id,2=args,3=output,4=name} // 47 mcp_tool → CortexStepMcpTool{1=server,2=ChatToolCall,3=result} // 49 tool_call_proposal → {1=ChatToolCall} // 50 tool_call_choice → {1=repeated ChatToolCall, 2=choice, 3=reason} // ChatToolCall (codeium_common_pb): 1=id, 2=name, 3=arguments_json const parseChatToolCall = (buf) => { const f = parseFields(buf); const id = getField(f, 1, 2); const name = getField(f, 2, 2); const args = getField(f, 3, 2); return { id: id ? id.value.toString('utf8') : '', name: name ? name.value.toString('utf8') : '', argumentsJson: args ? args.value.toString('utf8') : '', }; }; const customField = getField(sf, 45, 2); if (customField) { const cf = parseFields(customField.value); const recipeId = getField(cf, 1, 2); const argsF = getField(cf, 2, 2); const outF = getField(cf, 3, 2); const nameF = getField(cf, 4, 2); entry.toolCalls.push({ id: recipeId ? recipeId.value.toString('utf8') : '', name: nameF ? nameF.value.toString('utf8') : (recipeId ? recipeId.value.toString('utf8') : 'custom_tool'), argumentsJson: argsF ? argsF.value.toString('utf8') : '', result: outF ? outF.value.toString('utf8') : '', }); } const mcpField = getField(sf, 47, 2); if (mcpField) { const mf = parseFields(mcpField.value); const serverF = getField(mf, 1, 2); const callF = getField(mf, 2, 2); const resultF = getField(mf, 3, 2); if (callF) { const tc = parseChatToolCall(callF.value); tc.serverName = serverF ? serverF.value.toString('utf8') : ''; tc.result = resultF ? resultF.value.toString('utf8') : ''; entry.toolCalls.push(tc); } } const proposalField = getField(sf, 49, 2); if (proposalField) { const pf = parseFields(proposalField.value); const callF = getField(pf, 1, 2); if (callF) entry.toolCalls.push(parseChatToolCall(callF.value)); } const choiceField = getField(sf, 50, 2); if (choiceField) { const cf = parseFields(choiceField.value); const chosenIdx = getField(cf, 2, 0); const calls = getAllFields(cf, 1).filter(x => x.wireType === 2).map(x => parseChatToolCall(x.value)); if (calls.length) { const idx = chosenIdx ? Number(chosenIdx.value) : 0; entry.toolCalls.push(calls[idx] || calls[0]); } }
if (plannerField) { const pf = parseFields(plannerField.value); const textField = getField(pf, 1, 2); const modifiedField = getField(pf, 8, 2); const thinkField = getField(pf, 3, 2); const responseText = textField ? textField.value.toString('utf8') : ''; const modifiedText = modifiedField ? modifiedField.value.toString('utf8') : ''; // modified_response is the LS post-pass edited final text (markdown // fixups, citations, tool-result folding). On long opus-4 replies the // LS writes a short `response` first, then overwrites with a much // longer `modified_response` at turn end. Prefer it whenever present // so we don't truncate to the early draft. entry.text = modifiedText || responseText; entry.responseText = responseText; entry.modifiedText = modifiedText; if (thinkField) entry.thinking = thinkField.value.toString('utf8'); }
// Walk CortexErrorDetails. user_error_message, short_error and full_error // usually contain the same text at increasing verbosity — pick one. const readErrorDetails = (buf) => { const ed = parseFields(buf); for (const fnum of [1, 2, 3]) { const f = getField(ed, fnum, 2); if (f) { const s = f.value.toString('utf8').trim(); if (s) return s.split('\n')[0].slice(0, 300); } } return ''; };
// Error info lives at either CortexTrajectoryStep.error_message (field 24 // for ERROR_MESSAGE steps) or CortexTrajectoryStep.error (field 31 for any // step). They both wrap CortexErrorDetails. Prefer the step-specific one. const errMsgField = getField(sf, 24, 2); if (errMsgField) { const inner = getField(parseFields(errMsgField.value), 3, 2); if (inner) entry.errorText = readErrorDetails(inner.value); } if (!entry.errorText) { const errField = getField(sf, 31, 2); if (errField) entry.errorText = readErrorDetails(errField.value); }
results.push(entry); }
return results;}
// ─── GetUserStatus (authoritative tier + model allowlist) ──//// LanguageServerService/GetUserStatus → GetUserStatusResponse {// UserStatus user_status = 1;// PlanInfo plan_info = 2;// }// GetUserStatusRequest { Metadata metadata = 1; }//// Beats our probe-based inferTier — one RPC returns exact tier, trial// end time, per-model allowlist with credit multipliers, credit usage.// Verified via extracted FileDescriptorProto on 2026-04-21 (scripts/ls-protos).
export function buildGetUserStatusRequest(apiKey) { return writeMessageField(1, buildMetadata(apiKey));}
// exa.codeium_common_pb.TeamsTier → free | pro// Values as defined in the binary (enum TeamsTier). Paid/trial tiers all// map to 'pro' so the caller can unlock premium models uniformly.// UNSPECIFIED(0) and WAITLIST_PRO(6) and DEVIN_FREE(19) are the only frees.export function mapTeamsTier(t) { if (t === 0 || t === 6 || t === 19) return 'free'; if (t > 0) return 'pro'; return 'unknown';}
// Human-readable label for dashboard display.export function teamsTierLabel(t) { return ({ 0: 'Unspecified', 1: 'Teams', 2: 'Pro', 3: 'Enterprise (SaaS)', 4: 'Hybrid', 5: 'Enterprise (Self-Hosted)', 6: 'Waitlist Pro', 7: 'Teams Ultimate', 8: 'Pro Ultimate', 9: 'Trial', 10: 'Enterprise (Self-Serve)', 11: 'Enterprise (SaaS Pooled)', 12: 'Devin Enterprise', 14: 'Devin Teams', 15: 'Devin Teams V2', 16: 'Devin Pro', 17: 'Devin Max', 18: 'Max', 19: 'Devin Free', 20: 'Devin Trial', })[t] || `Tier ${t}`;}
/** * Parse GetUserStatusResponse into a flat object. * * UserStatus field numbers (exa.codeium_common_pb.UserStatus): * 1 pro (bool) * 3 name (string) * 5 team_id (string) * 7 email (string) * 10 teams_tier (TeamsTier enum) * 13 plan_status (PlanStatus message) * 28 user_used_prompt_credits (int64) * 29 user_used_flow_credits (int64) * 33 cascade_model_config_data (CascadeModelConfigData) * 34 windsurf_pro_trial_end_time (Timestamp) * 35 max_num_premium_chat_messages (int64) * * PlanInfo field numbers (exa.codeium_common_pb.PlanInfo): * 1 teams_tier * 2 plan_name (string) * 12 monthly_prompt_credits (int32) * 13 monthly_flow_credits (int32) * 16 is_enterprise (bool) * 17 is_teams (bool) * 21 cascade_allowed_models_config (repeated AllowedModelConfig) * 32 has_paid_features (bool) * * AllowedModelConfig { ModelOrAlias model_or_alias = 1; float credit_multiplier = 2; } * ModelOrAlias { Model model = 1; ModelAlias alias = 2; } (oneof in practice) */export function parseGetUserStatusResponse(buf) { const out = { pro: false, teamsTier: 0, tierName: '', email: '', displayName: '', teamId: '', userUsedPromptCredits: 0, userUsedFlowCredits: 0, trialEndMs: 0, maxPremiumChatMessages: 0, planName: '', monthlyPromptCredits: 0, monthlyFlowCredits: 0, hasPaidFeatures: false, isTeams: false, isEnterprise: false, allowedModels: [], // [{ modelEnum, alias, multiplier }] };
if (!buf || buf.length === 0) { out.tierName = mapTeamsTier(out.teamsTier); return out; } const top = parseFields(buf); const usBuf = getField(top, 1, 2)?.value; const piBuf = getField(top, 2, 2)?.value;
if (usBuf && usBuf.length) { const us = parseFields(usBuf); out.pro = (getField(us, 1, 0)?.value ?? 0) === 1; out.displayName = getField(us, 3, 2)?.value?.toString('utf8') || ''; out.teamId = getField(us, 5, 2)?.value?.toString('utf8') || ''; out.email = getField(us, 7, 2)?.value?.toString('utf8') || ''; out.teamsTier = getField(us, 10, 0)?.value ?? 0; out.userUsedPromptCredits = Number(getField(us, 28, 0)?.value ?? 0); out.userUsedFlowCredits = Number(getField(us, 29, 0)?.value ?? 0); out.maxPremiumChatMessages = Number(getField(us, 35, 0)?.value ?? 0); const tsBuf = getField(us, 34, 2)?.value; if (tsBuf && tsBuf.length) { const tsFields = parseFields(tsBuf); const secs = Number(getField(tsFields, 1, 0)?.value ?? 0); out.trialEndMs = secs * 1000; } }
if (piBuf && piBuf.length) { const pi = parseFields(piBuf); if (!out.teamsTier) out.teamsTier = getField(pi, 1, 0)?.value ?? 0; out.planName = getField(pi, 2, 2)?.value?.toString('utf8') || ''; out.monthlyPromptCredits = Number(getField(pi, 12, 0)?.value ?? 0); out.monthlyFlowCredits = Number(getField(pi, 13, 0)?.value ?? 0); out.isEnterprise = (getField(pi, 16, 0)?.value ?? 0) === 1; out.isTeams = (getField(pi, 17, 0)?.value ?? 0) === 1; out.hasPaidFeatures = (getField(pi, 32, 0)?.value ?? 0) === 1;
// cascade_allowed_models_config — repeated AllowedModelConfig (field 21) for (const entry of getAllFields(pi, 21)) { if (entry.wireType !== 2) continue; const ac = parseFields(entry.value); const moaBuf = getField(ac, 1, 2)?.value; // credit_multiplier is float → wire type 5 (fixed32) const cmField = getField(ac, 2, 5); let multiplier = 1.0; if (cmField && cmField.value.length === 4) { multiplier = cmField.value.readFloatLE(0); } let modelEnum = 0; let alias = 0; if (moaBuf && moaBuf.length) { const moa = parseFields(moaBuf); modelEnum = getField(moa, 1, 0)?.value ?? 0; alias = getField(moa, 2, 0)?.value ?? 0; } out.allowedModels.push({ modelEnum, alias, multiplier }); } }
out.tierName = mapTeamsTier(out.teamsTier); return out;}.dockerignoreAdded+7
.env.exampleAdded+58
.gitattributesDeleted-35
DockerfileAdded+33
README.md+53-4
entrypoint.shAdded+71
package.jsonAdded+14
src/auth.jsAdded+958
src/cache.jsAdded+83
src/client.jsAdded+677
src/config.jsAdded+61
src/connect.jsAdded+145
src/conversation-pool.jsAdded+157
src/dashboard/api.jsAdded+640
src/dashboard/index.htmlAdded
src/dashboard/logger.jsAdded+158
src/dashboard/model-access.jsAdded+79
src/dashboard/proxy-config.jsAdded+76
src/dashboard/stats.jsAdded+130
src/dashboard/windsurf-login.jsAdded+294
src/grpc.jsAdded+199
src/handlers/chat.jsAdded+806
src/handlers/messages.jsAdded+431
src/handlers/models.jsAdded+5
src/handlers/tool-emulation.jsAdded+363
src/index.jsAdded+88
src/langserver.jsAdded+266
src/models.jsAdded+287
src/proto.jsAdded+146
src/runtime-config.jsAdded+140
src/sanitize.jsAdded+150
src/server.jsAdded+303
src/windsurf-api.jsAdded+298
src/windsurf.jsAdded+941