@freestyle-sh/with-opencode

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

OpenCode integration for Freestyle VMs. Provides a fully configured OpenCode AI coding assistant server in your Freestyle VM.

原始使用文档
文档目录15 节
  1. Installation
  2. Usage
  3. Basic Setup
  4. Using the API Client
  5. With Custom Domain
  6. With Authentication
  7. Options
  8. Environment Variables
  9. API
  10. vm.opencode.routeWeb(options?)
  11. vm.opencode.client(options?)
  12. vm.opencode.serverPort()
  13. vm.opencode.webPort()
  14. How It Works
  15. Documentation

@freestyle-sh/with-opencode

OpenCode integration for Freestyle VMs. Provides a fully configured OpenCode AI coding assistant server in your Freestyle VM.

Installation

bun add @freestyle-sh/with-opencode freestyle

Usage

Basic Setup

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}`);

Using the API Client

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

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

With Custom Domain

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

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

With Authentication

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();

Options

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
  },
});
OptionTypeDefaultDescription
server.portnumber4096Port for the OpenCode API server
server.usernamestring"opencode"Basic auth username (only used if password set)
server.passwordstring-Basic auth password for the API server
web.portnumber4097Port for the OpenCode web UI
web.usernamestring"opencode"Basic auth username (only used if password set)
web.passwordstring-Basic auth password for the web UI
envRecord<string, string>{}Environment variables passed to OpenCode processes

Environment Variables

OpenCode requires API keys to interact with AI providers. When running in a Freestyle VM, pass the required keys using the env option.

Note: Environment variables are stored as plain text in the VM's startup scripts. Users with direct or indirect access to the VM may be able to view these values. Use appropriately scoped API keys and rotate them if the VM is shared or exposed.

VariableDescription
ANTHROPIC_API_KEYAPI key for Claude models
OPENAI_API_KEYAPI key for OpenAI models
OPENROUTER_API_KEYAPI key for OpenRouter

See OpenCode Providers for all supported providers and their environment variables.

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?)

Exposes the OpenCode web UI on a public domain.

Options:

  • domain?: string - Custom domain to use. If not specified, generates a random subdomain.

Returns: Promise<{ url: string }> - URL includes credentials if authentication is configured.

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

vm.opencode.client(options?)

Creates an OpenCode SDK client connected to the server. The client is automatically configured with credentials if authentication is enabled.

Options:

  • domain?: string - Custom domain for the API endpoint. If not specified, generates a random subdomain.

Returns: 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()

Returns the configured API server port.

Returns: number

vm.opencode.webPort()

Returns the configured web UI port.

Returns: number

How It Works

The package uses systemd services to install and run OpenCode during VM creation:

  1. Installs OpenCode via the official install script
  2. Starts the OpenCode API server (opencode serve)
  3. Starts the OpenCode web UI (opencode web)

Both services are configured to restart automatically and listen on all interfaces (0.0.0.0).

Documentation

版本与发布记录

@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 是什么?

OpenCode integration for Freestyle VMs. Provides a fully configured OpenCode AI coding assistant server in your Freestyle VM.

如何安装 @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 和提交记录判断。