@wengine-ai/llms 2.3.23 → 2.3.231
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/server.cjs +1 -1
- package/dist/cjs/server.cjs.map +2 -2
- package/dist/esm/server.mjs +1 -1
- package/dist/esm/server.mjs.map +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -93,6 +93,7 @@ npm install -g @wengine-ai/claude-code-router-next@latest && ccr restart
|
|
|
93
93
|
|
|
94
94
|
| 版本 | 发布内容 |
|
|
95
95
|
| --- | --- |
|
|
96
|
+
| **v2.3.231** | <ul><li>**项目级接管默认仅接管 Claude Code**: 此前打开项目「CCR 接管」默认接管全部客户端(Claude Code、pi、qwen-code、opencode),会把 `.pi/settings.json`、`.qwen/settings.json`、`opencode.json` 等配置写进项目目录(opencode 首次运行还会自行生成 `AGENTS.md`),污染项目根目录;现在所有默认路径(主开关、多选清空、legacy `enabled: true`)都只接管 Claude Code,其余客户端在多选中显式勾选才接管。</li><li>**修复「检查更新」永远提示已是最新(双重 bug)**: 后端 `checkForUpdates`/`performUpdate` 硬编码了已从 npm unpublish 的旧包名 `claude-code-router-next`,`npm view` 404 被 catch 静默吞掉、恒返回「无更新」,「立即更新」也因同样的错包名安装失败,现改为从 `package.json` 动态读取包名(`@wengine-ai/claude-code-router-next`),今后改包名不会再失效;另外前端更新弹窗条件要求 `changelog` 非空而后端该字段恒为空串,即使有新版本也不弹窗,现去掉该强制判断(弹窗内已有「暂无更新日志」兜底文案)。</li><li>**发布确认点闸门(release gate)**: `scripts/release.sh` 新增 `validate_release_docs`,发布前(含 dry-run)强制校验:6 个 `package.json` 版本一致、`CHANGELOG.md` 有该版本的非空段落、两份 README 表格有该版本行、版本号严格大于 npm 已发布 latest(逐段数字比较,registry 不可达时警告跳过);任一不满足即中止发布。</li><li>**版本号策略:多位 patch 日常小迭代**: 自本版本起日常小迭代在 patch 段追加一位数字(`2.3.23` → `2.3.231` → `2.3.232`),避免每日发布推高主版本数字;patch 按数字比较,用过 `2.3.23x` 后下一个功能版本是 `2.3.240`(`2.3.24` 会被闸门当降级拦截)或直接升 `2.4.0`。</li></ul> |
|
|
96
97
|
| **v2.3.23** | <ul><li>**修复状态栏 token 速率虚高(撞到 999 上限)**: `ccr statusline` 的 token 速率常显示几百、极端撞到 999 上限,与「用量统计」的几十 t/s 对不上。根因是 token-speed 插件流式过程中每秒上报的是「最近 1 秒到达的 token 数」滑动窗口值,而 SSE delta 常成批到达(同一时间戳)使瞬时计数虚高;现改为与用量统计同一套解码平均公式(`输出 token ÷ (总耗时 − TTFT)`)上报,速率随流式收敛到最终值、与用量统计一致,并移除失效的滑动窗口记账。</li><li>**状态栏默认改为无图标表格风格**: 默认主题不再带装饰图标,模块之间改用细竖线 `│` 分隔,呈简洁表格样式,默认模块顺序为「模型 │ 工作目录 │ git 分支 │ 上下文进度条 │ token 速率 │ 会话总 token」。动机是歧义宽度的 emoji 图标(如闪电 `⚡` U+26A1)会让 Claude Code 误算状态栏显示宽度、在双击等重绘时产生数字重影/位移;改用定宽字符或不带图标可避免。图标仍支持在 UI 中自定义。</li><li>**修复 UI 状态栏图标无法删除 + 旧圆圈模块迁移**: 切换所选模块时图标搜索框(`IconSearchInput`)不随 `value` 同步、与模块真实图标脱节导致看似删不掉,现已在 `value` 变化时同步;同时加载配置时把旧 `contextCircle` 模块迁移为 `contextBar`,配置弹窗显示长条进度条而非旧圆圈图标。</li><li>**`build:ui` 构建后同步产物到 CLI/根 dist**: `pnpm build:ui` 现会把 `index.html` 同步到 `packages/cli/dist` 与根 `dist`,使单独运行 `build:ui` 也能更新本地运行中的 ccr 实际读取的包。</li></ul> |
|
|
97
98
|
| **v2.3.22** | <ul><li>**状态栏按用户配置的压缩阈值显示上下文上限**: `ccr statusline` 优先用 `CLAUDE_CODE_AUTO_COMPACT_WINDOW`(CCR 从顶层 `ContextWindow` 写入)作为上下文上限与百分比分母,未设才回退到 Claude Code 的窗口值,使状态栏与实际 auto-compact 触发点对齐,不再固定显示 1M/200k。</li><li>**接管保留用户手写的 auto-compact 自定义值**: CCR 接管 Claude Code 时不再无条件用 `ContextWindow` 覆盖 `CLAUDE_CODE_AUTO_COMPACT_WINDOW`,改用状态文件(`~/.claude-code-router/client-state.json` 全局、`<project-id>/ccr-state.json` 项目级)区分 CCR 写入值(可随 `ContextWindow` 更新)与用户手写值(保留);卸载只清自己写的、保留用户的;状态缺失时保守保留,绝不误覆盖。</li><li>**UI 上下文窗口与扩展上下文配合提示**: 顶层 `ContextWindow` > 200000 但未启用对应模型家族「扩展上下文 (1M)」(模型名带 `[1m]`)时显示红色警告,并在两处配置补充配合说明——否则 Claude Code 会将其封顶至 200000。</li><li>**修复状态栏百分比按 1M 计算与偶发闪 0%**: statusline 子进程不一定继承项目级 settings 的环境变量,导致项目里设的 `CLAUDE_CODE_AUTO_COMPACT_WINDOW` 被漏读而按 1M 计算,现在依次从 `process.env`→项目 `settings.local.json`→全局 `settings.json` 读取;另 `current_usage` 在请求进行中/刚 auto-compact 后会暂态为空使百分比偶发闪 0%,现回退到 transcript 最近一条 assistant 消息的上下文用量保持稳定。</li><li>**修复项目接管 disable→enable 循环冻结窗口与 disable 残留**: 项目级 `ccr-state.json` 在 disable 后丢失、再 enable 时不重建,导致 CCR 无法识别自己写入的窗口——既会让 `ContextWindow` 变更无法刷新生效,也会在关闭接管时把 `CLAUDE_CODE_AUTO_COMPACT_WINDOW` 当用户值残留。现在状态缺失时按「值等于当前 `ContextWindow`」兜底识别 CCR-managed,enable 重建状态、disable 清残留,真正的用户手写值仍保留。</li></ul> |
|
|
98
99
|
| **v2.3.21** | <ul><li>**新增 pi (earendil-works) 客户端接管**: 新增 pi 作为第三个接管目标(与 Claude Code、Codex 并列),pi 使用 Anthropic `/v1/messages` 协议直连无需 transformer;接管会在 `~/.pi/agent` 写入 `models.json`(注册指向 ccr 代理的 `ccr` provider,暴露 `ccr-opus`/`ccr-sonnet`/`ccr-haiku` 族别名)与 `settings.json`(默认 provider/model 指向 ccr),并备份原文件,仅做配置注入不含账号管理;用量统计也会把 pi 识别为独立客户端(pi 与 Claude Code 共用 `ccr-*` 别名,靠 pi system prompt 特征 + Anthropic SDK 请求头区分)。</li><li>**项目级接管支持多客户端多选**: 「项目级配置」页「CCR 接管」从只接管 Claude Code 改为客户端多选下拉(Claude Code + pi + qwen-code + opencode),不选默认接管全部;pi 项目级接管在项目目录写 `.pi/settings.json` 指向全局 ccr provider 并在 `trust.json` 信任该目录,接管状态从各客户端项目级配置文件实时推导、自动兼容既有项目(Codex 配置全局-only 不纳入)。</li><li>**新增 qwen-code (Alibaba) 客户端接管**: 新增 qwen-code(`@qwen-code/qwen-code`)作为第四个接管目标,走 Anthropic `/v1/messages` 直连;接管在 `~/.qwen/settings.json`(用户级)/ `<项目>/.qwen/settings.json`(项目级)写入指向 ccr 的 `modelProviders.anthropic`(暴露 ccr-opus/sonnet/haiku)、备份原文件,项目级还在 `trustedFolders.json` 信任该目录。用量统计区分 qwen-code:qwen 经代理时会伪装成 Claude Code 的 `claude-cli` UA,故改用 qwen system prompt 特征(`You are Qwen Code`)正向识别、Claude Code 用 `cc_version`/`metadata.user_id` 强信号识别。</li><li>**新增 opencode (opencode.ai) 客户端接管**: 新增 opencode 作为第五个接管目标,走 Anthropic `/v1/messages` 直连;接管在 `~/.config/opencode/opencode.json`(全局)/ `<项目>/opencode.json`(项目级)注入一个 `@ai-sdk/anthropic` 的 `provider.ccr`(baseURL=`…/v1`、apiKey 内联、暴露 ccr-opus/sonnet/haiku)并设 `model: ccr/ccr-opus`,备份原文件,无需 trust。用量统计区分 opencode:其请求带 `opencode/…` UA 且不伪装,直接按 UA 识别。</li></ul> |
|
|
@@ -102,7 +103,6 @@ npm install -g @wengine-ai/claude-code-router-next@latest && ccr restart
|
|
|
102
103
|
| **v2.3.17** | <ul><li>**修复 fallback 触发时所有备用模型报 Invalid URL**: 主模型限流(如智谱套餐)触发 fallback 后,此前所有备用模型都报 `Invalid URL`;根因是 fallback 用了原始 `ConfigProvider[]`(无 `baseUrl`)而非已注册的 `LLMProvider[]`,改为 `providerService.getProviders()`。</li><li>**修复 fireworks 托管上游用量统计全 0**: fireworks 流式把真实 usage 放在 `finish_reason` 之后的 `choices:[]` 空 chunk 里,此前 `finish_reason` 块用 null usage 覆盖了已 merge 的真实值、且 `break` 丢弃了后续 usage chunk,导致 `input` 有值但 `output`/缓存命中为 0;现改为 finish 后继续读取、只设 stop_reason 不碰 usage、`break` 改 `hasFinished=true`,并修复下游三层用量捕获与 `??` 对 0 不 fallback 的问题。</li><li>**忽略已删除模型的残留路由,防止健康池污染**: 从 provider 配置删除模型后,路由与 fallback 中残留的 `provider,model` 仍被当作有效候选反复失败污染健康池;现在对无法匹配的候选直接返回 `null` 并在请求前校验,`ProviderHealthStore` 统一拦截空 provider/model 防畸形 key 污染,同时抽取 `findProviderModel` 共用函数。</li></ul> |
|
|
103
104
|
| **v2.3.16** | <ul><li>**修复 system 消息顺序兼容 DeepSeek/vLLM**: OpenAI 兼容提供商(DeepSeek V4、GLM、vLLM)要求 `[system, user, assistant]` 顺序,此前 system 排在 user/assistant 之后会导致输出乱码;现在将 system 消息前置并去重。</li><li>**接管时去除 Attribution 动态头以提升缓存命中**: CCR 接管 Claude Code 时默认写入 `CLAUDE_CODE_ATTRIBUTION_HEADER=0`,去掉系统提示词开头随请求变化的 attribution 头(客户端版本 + prompt fingerprint),稳定上游 prompt 缓存前缀;可在设置页或 `disableAttributionHeader: false` 关闭。</li><li>**设置页布局紧凑化**: 「日志级别」下拉从独占整行移入两列网格,与「API 密钥」并排显示,减少纵向留白。</li></ul> |
|
|
104
105
|
| **v2.3.15** | <ul><li>**Fallback 前同模型重试一次**: 模型调用出现一次异常不再立即切换到备用模型,而是先对同一模型自动重试一次(Double-Check),避免因瞬时错误(网络抖动、偶发限流、空 SSE 响应等)导致不必要的模型切换;重试仍失败才走原有 fallback 流程。</li><li>**用量统计显示上游真实模型**: 部分上游网关会偷偷将请求路由/降级到另一个后端模型(如请求 glm-5 实际返回 MiniMax-M2.5)。用量统计的模型映射现在追加上游返回的真实模型,格式为 `originalModel → routedModel → upstreamModel`(如 `ccr-opus → glm-5 → minimax-m2.5`),上游未偷换时自动省略。</li></ul> |
|
|
105
|
-
| **v2.3.14** | <ul><li>**修复状态栏显示 `<synthetic>` 模型名**:Claude Code 在 auto-compact 自动压缩、中断恢复时写入的 `<synthetic>` 合成消息(非真实 LLM 响应)此前被状态栏当作模型名显示(从 Claude 账号会话切换到 ccr 接管、或发生自动压缩后尤其常见);现在过滤该合成标识,正确显示实际路由模型,同时不再把合成消息的 usage 计入 token 统计。</li></ul> |
|
|
106
106
|
|
|
107
107
|
> 仅保留最近 10 个版本,更早版本的发布摘要见 [CHANGELOG-archive.md](./CHANGELOG-archive.md),完整详细变更记录见 [CHANGELOG.md](./CHANGELOG.md)。
|
|
108
108
|
|
package/dist/cjs/server.cjs
CHANGED
|
@@ -71,7 +71,7 @@ ${this.toMarkdown(o,n+1)}`:`${r}- ${o}`).join(`
|
|
|
71
71
|
${this.toMarkdown(s,n+1)}`:`${r}${o}: ${s}`).join(`
|
|
72
72
|
`):`${r}${e}`}async output(e,n={}){try{let r=this.formatData(e,n);switch(this.config.level||"log"){case"info":console.info(r);break;case"warn":console.warn(r);break;case"error":console.error(r);break;case"debug":console.debug(r);break;case"log":default:console.log(r);break}return!0}catch(r){return console.error("[ConsoleOutputHandler] Output failed:",r),!1}}}});var Ns,If=Le(()=>{"use strict";Ns=class{type="webhook";config;defaultTimeout=3e4;constructor(e){if(!e.url)throw new Error("Webhook URL is required");this.config={method:"POST",retry:{maxAttempts:3,backoffMs:1e3},silent:!1,...e}}buildHeaders(){let e={"Content-Type":"application/json",...this.config.headers||{}};if(this.config.auth)switch(this.config.auth.type){case"bearer":this.config.auth.token&&(e.Authorization=`Bearer ${this.config.auth.token}`);break;case"basic":if(this.config.auth.username&&this.config.auth.password){let n=Buffer.from(`${this.config.auth.username}:${this.config.auth.password}`).toString("base64");e.Authorization=`Basic ${n}`}break;case"custom":this.config.auth.custom&&(e[this.config.auth.custom.header]=this.config.auth.custom.value);break}return e}buildBody(e,n){let{format:r="json",timestamp:o=!0,prefix:s,metadata:a}=n||{},u={data:e};return o&&(u.timestamp=new Date().toISOString()),s&&(u.prefix=s),a&&Object.keys(a).length>0&&(u.metadata=a),u}async sendRequest(e,n,r,o,s){let a=new AbortController,u=setTimeout(()=>a.abort(),s);try{let l=await fetch(e,{method:n,headers:r,body:JSON.stringify(o),signal:a.signal});if(clearTimeout(u),!l.ok)throw new Error(`HTTP ${l.status}: ${l.statusText}`);return l}catch(l){throw clearTimeout(u),l}}delay(e){return new Promise(n=>setTimeout(n,e))}async sendWithRetry(e,n,r,o,s,a){let u=null;for(let l=1;l<=a.maxAttempts;l++)try{return await this.sendRequest(e,n,r,o,s)}catch(f){if(u=f,l===a.maxAttempts)break;let d=a.backoffMs*Math.pow(2,l-1);console.warn(`[WebhookOutputHandler] Request failed (attempt ${l}/${a.maxAttempts}), retrying in ${d}ms...`,f.message),await this.delay(d)}throw u}async output(e,n={}){let r=n.timeout||this.defaultTimeout;try{let o=this.buildHeaders(),s=this.buildBody(e,n),a=await this.sendWithRetry(this.config.url,this.config.method,o,s,r,this.config.retry);return!0}catch(o){let s=o instanceof Error?o.message:String(o);if(this.config.silent)return console.error(`[WebhookOutputHandler] Failed to send data: ${s}`),!1;throw new Error(`Webhook output failed: ${s}`)}}}});var Oo,Nf,M_,Bs,Bf=Le(()=>{"use strict";Oo=require("fs"),Nf=require("path"),M_=require("os"),Bs=class{type="temp-file";config;baseDir;constructor(e={}){this.config={subdirectory:"claude-code-router",extension:"json",includeTimestamp:!1,prefix:"session",...e};let n=(0,M_.tmpdir)();this.baseDir=(0,Nf.join)(n,this.config.subdirectory),this.ensureDir()}ensureDir(){try{(0,Oo.existsSync)(this.baseDir)||(0,Oo.mkdirSync)(this.baseDir,{recursive:!0})}catch{}}getFilePath(e){let n=this.config.prefix||"session",r=this.config.extension?`.${this.config.extension}`:"",o;if(this.config.includeTimestamp){let s=Date.now();o=`${n}-${e}-${s}${r}`}else o=`${n}-${e}${r}`;return(0,Nf.join)(this.baseDir,o)}async output(e,n={}){try{let r=n.metadata?.sessionId;if(!r)return!1;let o={...e,timestamp:Date.now(),sessionId:r},s=this.getFilePath(r);return(0,Oo.writeFileSync)(s,JSON.stringify(o,null,2),"utf-8"),!0}catch{return!1}}getBaseDir(){return this.baseDir}}});var Mf,Po,L_=Le(()=>{"use strict";Ff();If();Bf();Mf=class{handlers=new Map;defaultOptions={};registerHandler(e,n){this.handlers.set(e,n)}registerHandlers(e){for(let n of e)if(n.enabled!==!1)try{let r=this.createHandler(n),o=n.type+"_"+Date.now();this.registerHandler(o,r)}catch(r){console.error(`[OutputManager] Failed to register ${n.type} handler:`,r)}}createHandler(e){switch(e.type){case"console":return new Is(e.config);case"webhook":return new Ns(e.config);case"temp-file":return new Bs(e.config);default:throw new Error(`Unknown output handler type: ${e.type}`)}}unregisterHandler(e){return this.handlers.delete(e)}getHandler(e){return this.handlers.get(e)}getAllHandlers(){return new Map(this.handlers)}clearHandlers(){this.handlers.clear()}setDefaultOptions(e){this.defaultOptions={...this.defaultOptions,...e}}getDefaultOptions(){return{...this.defaultOptions}}async output(e,n){let r={...this.defaultOptions,...n},o={success:[],failed:[]},s=Array.from(this.handlers.entries()).map(async([a,u])=>{try{await u.output(e,r)?o.success.push(a):o.failed.push(a)}catch(l){console.error(`[OutputManager] Handler ${a} failed:`,l),o.failed.push(a)}});return await Promise.all(s),o}async outputTo(e,n,r){let o={...this.defaultOptions,...r},s={success:[],failed:[]},a=e.map(async u=>{let l=this.handlers.get(u);if(!l){console.warn(`[OutputManager] Handler ${u} not found`),s.failed.push(u);return}try{await l.output(n,o)?s.success.push(u):s.failed.push(u)}catch(f){console.error(`[OutputManager] Handler ${u} failed:`,f),s.failed.push(u)}});return await Promise.all(a),s}async outputToType(e,n,r){let o=Array.from(this.handlers.entries()).filter(([s,a])=>a.type===e).map(([s])=>s);return this.outputTo(o,n,r)}},Po=new Mf});var FR={};hi(FR,{ActiveProbeService:()=>Ps,ConfigService:()=>hr,ProviderHealthStore:()=>Ho,ProviderService:()=>yr,SSEParserTransform:()=>Or,SSESerializerTransform:()=>Fs,TokenizerService:()=>xr,TransformerService:()=>Rr,calculateTokenCount:()=>xc,default:()=>PR,extractSessionIdFromUserId:()=>Qr,getActiveProbeService:()=>Tf,getAllQuotaResults:()=>Dp,getAllRateLimitInfo:()=>Ap,getHealthStore:()=>Je,getQuotaAdapter:()=>Du,getQuotaResult:()=>vi,getRateLimitInfo:()=>kp,getRuntimeDebugLog:()=>cp,initProviderHealthPersistence:()=>Ei,initQuotaStorePersistence:()=>Ti,initRateLimitPersistence:()=>ki,normalizeSessionId:()=>mr,pluginManager:()=>Rf,resetActiveProbeService:()=>O_,rewriteStream:()=>Pf,router:()=>eo,searchProjectBySession:()=>Oc,sessionUsageCache:()=>Ai,setRuntimeDebugLog:()=>up,startActiveProbe:()=>Ru,stopActiveProbe:()=>xu,storeQuotaResult:()=>Si,tokenSpeedPlugin:()=>jf});module.exports=Hh(FR);var q_=xe(require("fastify"),1),z_=xe(require("@fastify/cors"),1);var zo=require("fs"),pc=require("path"),tp=require("dotenv"),np=xe(hc(),1),hr=class{config={};options;constructor(e={jsonPath:"./config.json"}){this.options={envPath:e.envPath||".env",jsonPath:e.jsonPath,useEnvFile:!1,useJsonFile:e.useJsonFile!==!1,useEnvironmentVariables:e.useEnvironmentVariables!==!1,...e},this.loadConfig()}loadConfig(){this.options.useJsonFile&&this.options.jsonPath&&this.loadJsonConfig(),this.options.initialConfig&&(this.config={...this.config,...this.options.initialConfig}),this.options.useEnvFile&&this.loadEnvConfig(),this.config.LOG_FILE&&(process.env.LOG_FILE=this.config.LOG_FILE),this.config.LOG&&(process.env.LOG=this.config.LOG)}loadJsonConfig(){if(!this.options.jsonPath)return;let e=this.isAbsolutePath(this.options.jsonPath)?this.options.jsonPath:(0,pc.join)(process.cwd(),this.options.jsonPath);if((0,zo.existsSync)(e))try{let n=(0,zo.readFileSync)(e,"utf-8"),r=np.default.parse(n);this.config={...this.config,...r},console.log(`Loaded JSON config from: ${e}`)}catch(n){console.warn(`Failed to load JSON config from ${e}:`,n)}else console.warn(`JSON config file not found: ${e}`)}loadEnvConfig(){let e=this.isAbsolutePath(this.options.envPath)?this.options.envPath:(0,pc.join)(process.cwd(),this.options.envPath);if((0,zo.existsSync)(e))try{let n=(0,tp.config)({path:e});n.parsed&&(this.config={...this.config,...this.parseEnvConfig(n.parsed)})}catch(n){console.warn(`Failed to load .env config from ${e}:`,n)}}loadEnvironmentVariables(){let e=this.parseEnvConfig(process.env);this.config={...this.config,...e}}parseEnvConfig(e){let n={};return Object.assign(n,e),n}isAbsolutePath(e){return e.startsWith("/")||e.includes(":")}get(e,n){let r=this.config[e];return r!==void 0?r:n}getAll(){return{...this.config}}getHttpsProxy(){return this.get("HTTPS_PROXY")||this.get("https_proxy")||this.get("httpsProxy")||this.get("PROXY_URL")}has(e){return this.config[e]!==void 0}set(e,n){this.config[e]=n}reload(){this.config={},this.loadConfig()}getConfigSummary(){let e=[];return this.options.initialConfig&&e.push("Initial Config"),this.options.useJsonFile&&this.options.jsonPath&&e.push(`JSON: ${this.options.jsonPath}`),this.options.useEnvFile&&e.push(`ENV: ${this.options.envPath}`),this.options.useEnvironmentVariables&&e.push("Environment Variables"),`Config sources: ${e.join(", ")}`}};function He(t,e=500,n="internal_error",r="api_error"){let o=new Error(t);return o.statusCode=e,o.code=n,o.type=r,o}async function rp(t,e,n){e.log.error(t);let r=t.statusCode||500;if(t.rawBody)try{let s=JSON.parse(t.rawBody);return n.code(r).send(s)}catch{return n.code(r).send(t.rawBody)}let o={error:{message:t.message+(t.stack||"Internal Server Error"),type:t.type||"api_error",code:t.code||"internal_error"}};return n.code(r).send(o)}var op=require("undici");function sp(t,e,n,r,o){let s=new Headers({"Content-Type":"application/json"});n.headers&&Object.entries(n.headers).forEach(([f,d])=>{d&&s.set(f,d)});let a,u=AbortSignal.timeout(n.TIMEOUT??60*1e3*60);if(n.signal){let f=new AbortController,d=()=>f.abort();n.signal.addEventListener("abort",d),u.addEventListener("abort",d),a=f.signal}else a=u;let l={method:"POST",headers:s,body:JSON.stringify(e),signal:a};return n.httpsProxy&&(l.dispatcher=new op.ProxyAgent(new URL(n.httpsProxy).toString())),o?.debug({reqId:r.req.id,request:l,headers:Object.fromEntries(s.entries()),requestUrl:typeof t=="string"?t:t.toString(),useProxy:n.httpsProxy},"final request"),fetch(typeof t=="string"?t:t.toString(),l)}var LC=new Set(["authorization","x-api-key","x-goog-api-key","cookie","set-cookie"]),Gr={maxBodyLength:4096,maxStreamChunks:100},mc=!1;function up(t){mc=t}function cp(){return mc}function _i(t){return mc||t.get("DEBUG_LOG")===!0}function lp(t){let e=t.get("DEBUG_LOG_OPTIONS");return!e||typeof e!="object"?Gr:{maxBodyLength:e.maxBodyLength??Gr.maxBodyLength,maxStreamChunks:e.maxStreamChunks??Gr.maxStreamChunks}}function dp(t){let e={};for(let[n,r]of Object.entries(t))LC.has(n.toLowerCase())?e[n]="***MASKED***":e[n]=r;return e}function bi(t,e){return t.length<=e?t:t.slice(0,e)+`...[truncated, total ${t.length} bytes]`}function fp(t,e,n){let r=typeof n.body=="string"?n.body:JSON.stringify(n.body);t.info({debug_log:!0,reqId:e,phase:"provider_request",url:n.url,headers:dp(n.headers),body:bi(r,Gr.maxBodyLength)})}function hp(t,e,n){t.info({debug_log:!0,reqId:e,phase:"provider_response",status:n.status,headers:dp(n.headers),...n.body!==void 0?{body:bi(n.body,Gr.maxBodyLength)}:{}})}function ip(t,e,n,r){let o=jC(r);t.info({debug_log:!0,reqId:e,phase:"provider_stream_chunk",chunkIndex:n,...o?{usage_monitor:o}:{},data:bi(r,Gr.maxBodyLength)})}function jC(t){let e=[];for(let n of t.split(/\r?\n/)){if(!n.startsWith("data:"))continue;let r=n.slice(5).trim();if(!(!r||r==="[DONE]"))try{let o=JSON.parse(r),s=o?.usage||o?.response?.usage;s&&e.push({path:o?.usage?"usage":"response.usage",usage:s,choicesLength:Array.isArray(o?.choices)?o.choices.length:void 0,finishReason:o?.choices?.[0]?.finish_reason})}catch{e.push({parseError:!0,data:bi(r,512)})}}if(e.length)return e.length===1?e[0]:e}function ap(t,e,n){t.info({debug_log:!0,reqId:e,phase:"provider_stream_end",totalChunks:n})}function pp(t,e,n,r){(async()=>{let o=t.pipeThrough(new TextDecoderStream).getReader(),s=0;try{let a="";for(;;){let{done:u,value:l}=await o.read();if(u)break;a+=l;let f=a.split(`
|
|
73
73
|
|
|
74
|
-
`);a=f.pop()||"";for(let d of f)d.trim()&&(s<r.maxStreamChunks?ip(e,n,s,d):s===r.maxStreamChunks&&e.info({debug_log:!0,reqId:n,phase:"provider_stream_chunk_omitted",message:`[chunks after #${s} omitted, maxStreamChunks=${r.maxStreamChunks}]`}),s++)}a.trim()&&(s<r.maxStreamChunks&&ip(e,n,s,a.trim()),s++),ap(e,n,s)}catch(a){console.error(`[debug_log] Error reading debug stream for reqId=${n}:`,a),ap(e,n,s)}finally{o.releaseLock()}})()}var Ci="2.3.
|
|
74
|
+
`);a=f.pop()||"";for(let d of f)d.trim()&&(s<r.maxStreamChunks?ip(e,n,s,d):s===r.maxStreamChunks&&e.info({debug_log:!0,reqId:n,phase:"provider_stream_chunk_omitted",message:`[chunks after #${s} omitted, maxStreamChunks=${r.maxStreamChunks}]`}),s++)}a.trim()&&(s<r.maxStreamChunks&&ip(e,n,s,a.trim()),s++),ap(e,n,s)}catch(a){console.error(`[debug_log] Error reading debug stream for reqId=${n}:`,a),ap(e,n,s)}finally{o.releaseLock()}})()}var Ci="2.3.231";Wn();var Sn=require("fs"),Ac=require("path"),Ep=require("@wengine-ai/claude-code-router-shared"),Ec=(0,Ac.join)(Ep.HOME_DIR,"runtime"),kc=(0,Ac.join)(Ec,"rate-limit.json"),Vr=new Map,bp=!1,JC=null,VC=["x-ratelimit-remaining-tokens","x-ratelimit-remaining-requests","x-ratelimit-remaining"],KC=["x-ratelimit-limit-tokens","x-ratelimit-limit-requests","x-ratelimit-limit"],YC=["x-ratelimit-reset-tokens","x-ratelimit-reset-requests","x-ratelimit-reset"];function Wo(t,e,n){let r=u=>{if(n instanceof Headers)return n.get(u);let l=Object.keys(n).find(f=>f.toLowerCase()===u);return l?n[l]:null},o=wc(r,VC),s=wc(r,KC),a=wc(r,YC);(o||s||a)&&Vr.set(t,{provider:t,remaining:Cp(o),limit:Cp(s),reset:QC(a),capturedAt:Date.now()})}function kp(t){return Vr.get(t)}function Ap(){return Array.from(Vr.values())}function wc(t,e){for(let n of e){let r=t(n);if(r)return r}return null}function Cp(t){if(!t)return null;let e=Number(t.trim().replace(/,/g,""));return Number.isFinite(e)?e:null}function QC(t){if(!t)return null;let e=t.trim();if(!e)return null;let n=Number(e);if(Number.isFinite(n))return n>1e12?Math.floor(n/1e3):n>1e9?Math.floor(n):Math.floor(Date.now()/1e3+n);let r=Date.parse(e);if(Number.isFinite(r))return Math.floor(r/1e3);let o=XC(e);return o===null?null:Math.floor((Date.now()+o)/1e3)}function XC(t){let e=/(\d+(?:\.\d+)?)(ms|s|m|h|d)/gi,n=0,r=!1;for(let o of t.matchAll(e)){r=!0;let s=Number(o[1]),a=o[2].toLowerCase();if(Number.isFinite(s))switch(a){case"ms":n+=s;break;case"s":n+=s*1e3;break;case"m":n+=s*60*1e3;break;case"h":n+=s*60*60*1e3;break;case"d":n+=s*24*60*60*1e3;break}}return r?n:null}function ZC(){try{if(!(0,Sn.existsSync)(kc))return;let t=JSON.parse((0,Sn.readFileSync)(kc,"utf-8"));if(!Array.isArray(t))return;let e=Date.now();for(let n of t)if(n&&n.provider){if(n.capturedAt&&e-n.capturedAt>3600*1e3)continue;Vr.set(n.provider,n)}}catch{}}function wp(){try{if(Vr.size===0)return;(0,Sn.existsSync)(Ec)||(0,Sn.mkdirSync)(Ec,{recursive:!0}),(0,Sn.writeFileSync)(kc,JSON.stringify(Array.from(Vr.values()),null,2),"utf-8")}catch{}}function ki(){bp||(bp=!0,ZC(),JC=setInterval(wp,6e4),process.on("exit",wp))}wi();var Kr=class{name="openai-responses";endPoint="/v1/responses";getCacheReadInputTokens(e){return e?.cache_read_input_tokens??e?.input_tokens_details?.cached_tokens??e?.prompt_tokens_details?.cached_tokens??0}getCacheCreationInputTokens(e){return e?.cache_creation_input_tokens??e?.input_tokens_details?.cache_creation_tokens??e?.input_tokens_details?.cache_write_tokens??e?.prompt_tokens_details?.cache_creation_tokens??e?.prompt_tokens_details?.cache_write_tokens??0}buildResponsesUsage(e){let n=e?.input_tokens??e?.prompt_tokens??0,r=e?.output_tokens??e?.completion_tokens??0,o=this.getCacheReadInputTokens(e),s=this.getCacheCreationInputTokens(e);return{input_tokens:n,output_tokens:r,total_tokens:e?.total_tokens??n+r,input_tokens_details:{cached_tokens:o,cache_creation_tokens:s},output_tokens_details:{reasoning_tokens:e?.output_tokens_details?.reasoning_tokens??0},cache_read_input_tokens:o,cache_creation_input_tokens:s}}async transformRequestIn(e){delete e.temperature,delete e.max_tokens,e.reasoning&&(e.reasoning={effort:e.reasoning.effort,summary:e.reasoning.summary||"auto"});let n=[],r=e.messages.filter(o=>o.role==="system");if(r.length>0){let o=r[0];Array.isArray(o.content)?o.content.forEach(s=>{let a="";typeof s=="string"?a=s:s&&typeof s=="object"&&"text"in s&&(a=s.text),n.push({role:"system",content:a})}):e.instructions=o.content}if(e.messages.forEach(o=>{if(o.role!=="system"){if(Array.isArray(o.content)){let s=o.content.map(a=>this.normalizeRequestContent(a,o.role)).filter(a=>a!==null);s.length>0?o.content=s:delete o.content}if(o.role==="tool"){let s={...o};s.type="function_call_output",s.call_id=o.tool_call_id,s.output=o.content,delete s.cache_control,delete s.role,delete s.tool_call_id,delete s.content,n.push(s);return}if(o.role==="assistant"&&Array.isArray(o.tool_calls)){o.tool_calls.forEach(s=>{n.push({type:"function_call",arguments:s.function.arguments,name:s.function.name,call_id:s.id})});return}n.push(o)}}),e.input=n,delete e.messages,Array.isArray(e.tools)){let o=e.tools.find(s=>s.function.name==="web_search");e.tools=e.tools.filter(s=>s.function.name!=="web_search").map(s=>(s.function.name==="WebSearch"&&delete s.function.parameters.properties.allowed_domains,s.function.name==="Edit"?{type:s.type,name:s.function.name,description:s.function.description,parameters:{...s.function.parameters,required:["file_path","old_string","new_string","replace_all"]},strict:!0}:{type:s.type,name:s.function.name,description:s.function.description,parameters:s.function.parameters})),o&&e.tools.push({type:"web_search"})}return e.parallel_tool_calls=!1,e}async transformResponseOut(e){let n=e.headers.get("Content-Type")||"";if(n.includes("application/json")){let r=await e.json();if(r.object==="response"&&r.output){let o=this.convertResponseToChat(r);return new Response(JSON.stringify(o),{status:e.status,statusText:e.statusText,headers:e.headers})}return r.type==="message"&&Array.isArray(r.content)?new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers}):new Response(JSON.stringify(r),{status:e.status,statusText:e.statusText,headers:e.headers})}else if(n.includes("text/event-stream")){if(!e.body)return e;let[r,o]=e.body.tee(),s=r.getReader(),a="";try{let{value:g,done:_}=await s.read();!_&&g&&(a=new TextDecoder().decode(g))}finally{s.releaseLock()}if(r.cancel().catch(()=>{}),a.includes("message_start")||a.includes("content_block_start")){let g=this.prependToStream(a,o);return new Response(g,{status:e.status,statusText:e.statusText,headers:e.headers})}if(a.includes("response.created")||a.includes("response.output_text.delta")||a.includes("response.in_progress")||a.includes("response.output_item.added")){let g=this.prependToStream(a,o);return new Response(g,{status:e.status,statusText:e.statusText,headers:e.headers})}let f=this.prependToStream(a,o),d=new TextDecoder,h=new TextEncoder,m="",C=!1,w=this,y=new ReadableStream({async start(g){let _=f.getReader(),E=-1,b="",S=T=>(T!==b&&(E++,b=T),E);try{for(;;){let{done:T,value:R}=await _.read();if(T){C||g.enqueue(h.encode(`data: [DONE]
|
|
75
75
|
|
|
76
76
|
`));break}let $=d.decode(R,{stream:!0});m+=$;let H=m.split(/\r?\n/);m=H.pop()||"";for(let P of H)if(P.trim())try{if(P.startsWith("event: "))continue;if(P.startsWith("data: ")){let K=P.slice(5).trim();if(K==="[DONE]"){C=!0,g.enqueue(h.encode(`data: [DONE]
|
|
77
77
|
|