@ 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.port | number | 4096 | OpenCode API 服务器的端口 |
server.username | string | "opencode" | 基本认证用户名( 仅在密码设定时使用) |
server.password | string | - 说吧 | API 服务器的基本认证密码 |
web.port | number | 4097 | OpenCode 网络用户界面端口 |
web.username | string | "opencode" | 基本认证用户名( 仅在密码设定时使用) |
web.password | string | - 说吧 | Web UI 的基本认证密码 |
env | Record<string, string> | {} | 环境变量传递到 OpenCode 进程 |
环境变量
OpenCode要求API密钥与AI提供者互动. 在 Freestyle VM 中运行时,使用 env 选项.
说明: 环境变量作为纯文本被存储在VM的启动脚本中. 直接或间接访问VM的用户可以查看这些值. 使用适当范围化的API密钥,并在VM共享或被曝光时旋转.
| 变量 | 说明 |
|---|---|
ANTHROPIC_API_KEY | Claude 模型的 API 密钥 |
OPENAI_API_KEY | OpenAI 模型的 API 密钥 |
OPENROUTER_API_KEY | OpenRouter 的 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:
- 通过正式安装脚本安装 OpenCode
- 启动 OpenCode API 服务器( )
opencode serve(中文(简体) ) - 启动 OpenCode 网络 UI ()
opencode web(中文(简体) )
这两个服务都被配置为自动重启,并在所有接口上收听(0.0.0.0).