npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dsh-trae-api

v1.0.2

Published

DSH plugin: expose Trae Work CN credits as a local OpenAI/Anthropic compatible API for Claude Code, Cursor, Cline and other agents

Downloads

458

Readme

dsh-trae-api (Trae Local API)

将 Trae Work CN 的积分/额度转化为本地 OpenAI/Anthropic 兼容 API 服务,让 Claude Code、Cursor、Cline、Windsurf 等第三方 AI 编程工具直接调用 Trae 底层模型(GLM、DeepSeek、Qwen、Kimi、MiniMax 等)。

本项目是一个 DeepSeek Harness (DSH) 插件(npm 包名 dsh-trae-api),安装后随 dsh 启动自动运行代理服务;也可以独立运行。

原理

  1. 本机已安装并登录 Trae Work CN(或 TRAE SOLO CN / Trae SG / TRAE SOLO)
  2. 脚本自动从本地 storage.json 中解密认证 Token(支持 tc 加密和明文两种格式)
  3. 启动一个本地 Express 服务器,提供 OpenAI/Anthropic 兼容的 API 端点
  4. 第三方 Agent 将请求发送到本地服务器,本地服务器转发到 Trae 上游 API
  5. 消耗的是 Trae 的积分,无需额外付费

支持的 Trae 版本

| 版本 | IDE 名称 | 加密格式 | 上游端点 | |------|----------|----------|----------| | cn | Trae CN 国内版 | tc 加密 | trae-api-cn.mchost.guru | | solo | TRAE SOLO CN 独立部署版 | tc 加密 | trae-api-cn.mchost.guru | | sg | Trae 国际版 | 明文 JSON | a0ai-api-sg.byteintlapi.com | | solo-sg | TRAE SOLO 国际版 | tc 加密 | a0ai-api-sg.byteintlapi.com |

功能

  • 自动解密四版本认证数据,无需手动配置 Token
  • 提供 OpenAI (/v1/chat/completions) 和 Anthropic (/v1/messages) 兼容接口
  • 支持 OpenAI Responses API (/v1/responses),兼容新版 OpenAI SDK 和 Agents SDK
  • Token 过期自动刷新,自动保存到 .env
  • 支持流式输出 (SSE)
  • 完整支持 Claude Code 工具调用
  • 3 级 API 端点回退机制
  • 自适应 CN/SG 两种 SSE 事件格式
  • 上下文窗口自动截断,避免超出 Token 限制

前置条件

  • Node.js >= 18(内置 fetch 支持)
  • 已安装并登录以下任一 Trae IDE:
    • Trae CN(国内版)
    • TRAE SOLO CN(独立部署版)
    • Trae(国际版)
    • TRAE SOLO(国际版)
  • 对应 IDE 的 %APPDATA% 目录下存在 globalStorage/storage.json

快速开始

方式 A:作为 DSH 插件安装(推荐)

前提:已安装 DeepSeek Harnessdsh CLI)。

# 安装到 web profile
dsh plugin --profile web add dsh-trae-api

# 重启 dsh 使插件生效
dsh web

插件启动后,代理服务自动运行在 http://localhost:9220。可通过插件配置自定义端口和 API Key:

# 编辑 profile 的 cordis.patch.yml
- insert:
    - id: trae-api
      name: 'dsh-trae-api'
      config:
        port: 9220
        apiKey: my-secret-key

卸载:

dsh plugin --profile web remove dsh-trae-api

方式 B:独立运行

1. 安装依赖

npm install

2. 一键启动

# Windows 双击 start.bat 即可
start.bat

# 或命令行
npm start

首次运行会自动从本机 Trae IDE 的 storage.json 解密认证数据并保存到 .env,之后直接读取 .env 启动。

3. 连接第三方 Agent

Claude Code

$env:ANTHROPIC_BASE_URL = "http://localhost:9220"
$env:ANTHROPIC_API_KEY = "trae-local-api"
claude

Cursor

  • Base URL: http://localhost:9220/v1
  • API Key: trae-local-api
  • Model: 选择任意模型名(如 claude-sonnet-4-6gpt-4oauto

Cline / Roo Code

在扩展设置中配置:

  • OpenAI Compatible Base URL: http://localhost:9220/v1
  • API Key: trae-local-api
  • Model ID: claude-sonnet-4-6

Windsurf

在配置中添加自定义模型:

  • Provider: OpenAI Compatible
  • Base URL: http://localhost:9220/v1
  • API Key: trae-local-api

Python (OpenAI SDK)

from openai import OpenAI

client = OpenAI(base_url="http://localhost:9220/v1", api_key="trae-local-api")

response = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Hello!"}],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

API 接口

| 方法 | 路径 | 说明 | |------|------|------| | GET | /v1/status | 服务状态 | | GET | /v1/models | 模型列表 | | POST | /v1/chat/completions | OpenAI 格式对话 | | POST | /v1/messages | Anthropic 格式对话 | | POST | /v1/messages/count_tokens | 估算 Token 数 | | POST | /v1/responses | OpenAI Responses API |

模型映射

请求时传入 Claude/GPT 等模型名,会自动映射到 Trae 内部模型:

| 请求模型 | 映射到 | 档位 | |----------|--------|------| | claude-opus-4-7 / 4-6 / 4-5 | glm-5.2 | T1 | | claude-sonnet-4-6 / 4-5 / 4 | glm-5.2 | T1 | | claude-3.5-sonnet / 3.7-sonnet | glm-5.2 | T1 | | claude-haiku-4-5 | glm-5.1 | T2 | | gpt-4o / gpt-4.1 | DeepSeek-V4-Pro | T2 | | gpt-4o-mini | DeepSeek-V4-Flash | T3 | | auto | glm-5.2 | T1 |

环境变量

| 变量 | 说明 | 默认值 | |------|------|--------| | TRAE_EDITION | Trae 版本 (cn/solo/sg/solo-sg) | cn | | TRAE_TOKEN | 解密后的 JWT Token | (自动生成) | | TRAE_REFRESH_TOKEN | 刷新用 Token | (自动生成) | | TRAE_USER_ID | 用户 ID | (自动生成) | | TRAE_API_HOST | Token 刷新服务地址 | (自动设置) | | API_KEY | 本服务的 API Key(设为 none 可禁用鉴权,不推荐) | trae-local-api | | PORT | 监听端口 | 9220 | | HOST | 监听地址(0.0.0.0 开放局域网,需自担风险) | 127.0.0.1 | | BASE_URL | 上游 API 地址 | (按版本自动设置) | | MAX_CONTEXT_TOKENS | 最大上下文 Token 数 | 200000 | | TRAE_MANUAL_TOKEN | 手动指定 Token (备用) | (空) |

手动解密

如果自动解密失败,可以单独运行解密流程:

npm run setup

项目结构

dsh-trae-api/
├── lib/
│   └── index.js           # DSH 插件入口 (ESM, export name + apply)
├── cordis.patch.yml       # DSH bundle patch (挂载到 loader)
├── start.bat              # Windows 一键启动
├── setup.js               # 自动解密配置
├── package.json           # 项目配置 (含 dsh.bundle 声明)
├── .env.example           # 环境变量模板
├── .gitignore
├── LICENSE
├── README.md
└── src/
    ├── server.js          # 独立运行入口
    ├── server-core.js     # 可复用服务器核心 (startServer)
    ├── auth.js            # 认证管理 (Token 刷新)
    ├── trae-decrypt.js    # tc 加密解密核心
    ├── trae-client.js     # Trae API 客户端
    ├── openai-format.js   # OpenAI 格式转换
    └── anthropic-format.js # Anthropic 格式转换

注意事项

  • 本工具仅在你已经拥有 Trae 积分的情况下有效,本质上是将 Trae 的 API 额度通过本地代理暴露为标准接口
  • 默认仅监听 127.0.0.1,局域网与公网无法访问;如需开放给局域网设备,设置 HOST=0.0.0.0(请同时设置强 API_KEY)
  • 默认启用 API Key 鉴权(默认值 trae-local-api,建议修改);设置 API_KEY=none 可禁用鉴权,不推荐
  • CORS 仅允许 localhost 来源的浏览器跨域请求,第三方网页无法盗刷积分
  • Token 过期会自动刷新并回写 .envBASE_URLMAX_CONTEXT_TOKENS 等自定义配置会被保留
  • 请勿将 .env 文件提交到版本控制(已在 .gitignore 中忽略)

免责声明

本工具仅供学习和研究使用。使用本工具调用 Trae API 所产生的费用和合规问题由用户自行承担。请遵守 Trae 平台的使用条款。