@freestyle-sh/with-opencode

中文文档
0.0.14发布于 2026/04/22TypeScriptES Module近 30 天下载 1.1万

打开代码 合并 自由风格 页:1 在您的 Freestyle VM 中提供一个完全配置的 OpenCode AI 编码助理服务器 .

中文使用文档
文档目录15 节
  1. 安装
  2. 使用量
  3. 基本设置
  4. 使用 API 客户端
  5. 带有自定义域
  6. 有认证
  7. 选项
  8. 环境变量
  9. API 密码
  10. vm.opencode.routeWeb(options?)
  11. vm.opencode.client(options?)
  12. vm.opencode.serverPort()
  13. vm.opencode.webPort()
  14. 如何运作
  15. 文档

@ freestyle-sh/ 有打开代码

打开代码 合并 自由风格 页:1 在您的 Freestyle VM 中提供一个完全配置的 OpenCode AI 编码助理服务器 .

安装

bun add @freestyle-sh/with-opencode freestyle

使用量

基本设置

import { freestyle, VmSpec } from "freestyle";
import { VmOpenCode } from "@freestyle-sh/with-opencode";

const { vm } = await freestyle.vms.create({
  spec: new VmSpec({
    with: {
      opencode: new VmOpenCode(),
    },
  }),
});

// Expose the web UI
const { url } = await vm.opencode.routeWeb();
console.log(`OpenCode UI: ${url}`);

使用 API 客户端

const { client } = await vm.opencode.client();

// Use the OpenCode SDK client to interact with the server
const sessions = await client.session.list();

带有自定义域

const { url } = await vm.opencode.routeWeb({
  domain: "my-opencode.example.com",
});

const { client } = await vm.opencode.client({
  domain: "my-opencode-api.example.com",
});

有认证

import { freestyle, VmSpec } from "freestyle";
import { VmOpenCode } from "@freestyle-sh/with-opencode";

const { vm } = await freestyle.vms.create({
  spec: new VmSpec({
    with: {
      opencode: new VmOpenCode({
        server: {
          username: "serveradmin",
          password: "secret-server-pass",
        },
        web: {
          username: "webadmin",
          password: "secret-web-pass",
        },
      }),
    },
  }),
});

// URLs will include credentials automatically
const { url } = await vm.opencode.routeWeb();
const { client } = await vm.opencode.client();

选项

new VmOpenCode({
  server: {
    port: 4096, // Optional: API server port (default: 4096)
    username: "admin", // Optional: Basic auth username (default: "opencode" when password is set)
    password: "secret", // Optional: Basic auth password
  },
  web: {
    port: 4097, // Optional: Web UI port (default: 4097)
    username: "admin", // Optional: Basic auth username (default: "opencode" when password is set)
    password: "secret", // Optional: Basic auth password
  },
  env: {
    ANTHROPIC_API_KEY: "sk-ant-...", // Optional: Environment variables for OpenCode
  },
});
选项类型默认说明
server.portnumber4096OpenCode API 服务器的端口
server.usernamestring"opencode"基本认证用户名( 仅在密码设定时使用)
server.passwordstring- 说吧API 服务器的基本认证密码
web.portnumber4097OpenCode 网络用户界面端口
web.usernamestring"opencode"基本认证用户名( 仅在密码设定时使用)
web.passwordstring- 说吧Web UI 的基本认证密码
envRecord<string, string>{}环境变量传递到 OpenCode 进程

环境变量

OpenCode要求API密钥与AI提供者互动. 在 Freestyle VM 中运行时,使用 env 选项.

说明: 环境变量作为纯文本被存储在VM的启动脚本中. 直接或间接访问VM的用户可以查看这些值. 使用适当范围化的API密钥,并在VM共享或被曝光时旋转.

变量说明
ANTHROPIC_API_KEYClaude 模型的 API 密钥
OPENAI_API_KEYOpenAI 模型的 API 密钥
OPENROUTER_API_KEYOpenRouter 的 API 密钥

见 打开代码提供者 对所有支助提供者及其环境变量.

const { vm } = await freestyle.vms.create({
  spec: new VmSpec({
    with: {
      opencode: new VmOpenCode({
        env: {
          ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY!,
        },
      }),
    },
  }),
});

API 密码

vm.opencode.routeWeb(options?)

在公有领域曝光 OpenCode Web UI.

选项 :

  • domain?: string - 需要使用的自定义域。 如果未指定,则生成一个随机子域.

回返情况: Promise<{ url: string }> - 如果配置了认证, URL 包括证书 .

const { url } = await vm.opencode.routeWeb();
console.log(`OpenCode UI available at: ${url}`);

vm.opencode.client(options?)

创建连接到服务器的 OpenCode SDK 客户端. 如果启用认证, 客户端会自动配置证书 .

选项 :

  • domain?: string - 自定义 API 端点域。 如果未指定,则生成一个随机子域.

回返情况: Promise<{ client: OpencodeClient }>

const { client } = await vm.opencode.client();

// List sessions
const sessions = await client.session.list();

// Create a new session
const session = await client.session.create({ path: "/workspace" });

vm.opencode.serverPort()

返回已配置的 API 服务器端口 .

回返情况: number

vm.opencode.webPort()

返回配置的 Web UI 端口 .

回返情况: number

如何运作

软件包使用系统化服务在VM创建期间安装并运行OpenCode:

  1. 通过正式安装脚本安装 OpenCode
  2. 启动 OpenCode API 服务器( )opencode serve(中文(简体) )
  3. 启动 OpenCode 网络 UI ()opencode web(中文(简体) )

这两个服务都被配置为自动重启,并在所有接口上收听(0.0.0.0).

文档

版本与发布记录

@freestyle-sh/with-opencode 共发布 14 个版本,首次发布于 2026/01/30;过去 12 个月发布了 14 个版本。

版本发布日期距今
0.0.14latest2026/04/226 个月前
0.0.132026/04/226 个月前
0.0.122026/04/127 个月前
0.0.112026/02/218 个月前
0.0.102026/02/218 个月前
0.0.92026/02/208 个月前

关于 @freestyle-sh/with-opencode 的常见问题

@freestyle-sh/with-opencode 是什么?

打开代码 合并 自由风格 页:1 在您的 Freestyle VM 中提供一个完全配置的 OpenCode AI 编码助理服务器 .

如何安装 @freestyle-sh/with-opencode?

在项目目录运行 npm install @freestyle-sh/with-opencode,或使用 pnpm add / yarn add / bun add。

@freestyle-sh/with-opencode 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@freestyle-sh/with-opencode 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@freestyle-sh/with-opencode 可以商用吗?

@freestyle-sh/with-opencode 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@freestyle-sh/with-opencode 的下载量有多少?

根据 npm 官方下载统计,@freestyle-sh/with-opencode 在 2026/10/16 – 2026/11/14 这 30 天内被下载 11,281 次,日均约 376 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freestyle-sh/with-opencode 还在维护吗?

最新版本 0.0.14 发布于 2026/04/22(6 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 14 个;首次发布于 2026/01/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。