文档 / 开始
五步跑通第一条链路
目标不是把所有功能都装起来,而是让一次「安装 → 授权 → 调用」完整发生。走完这一步,宿主的权限模型就不再是抽象描述。
1. 构建宿主与 CLI
两个仓库分别构建:宿主产出沙箱执行体 oext-sandbox,CLI 产出用户命令bootidea。宿主本身是库,没有独立可执行入口。
bashshell
# 宿主:产生 oext-sandbox
cd originlang.extension.host
cargo build --release
# CLI:产生 bootidea
cd ../bootidea.cli
cargo build --release2. 让两侧互相找到
bootidea run 与 bootidea serve 默认使用进程外沙箱, 要求 oext-sandbox 在 PATH 上,或用环境变量指定完整路径。 数据目录默认走平台路径,可用 OEXT_HOME 覆盖。
bashshell
# bash / Git Bash
export PATH="$PWD/target/release:$PATH"
export OEXT_SANDBOX_BIN="/abs/path/to/oext-sandbox" # 可选
export OEXT_HOME="$HOME/.local/share/bootidea" # 可选
# PowerShell
$env:PATH = "$PWD\target\release;$env:PATH"3. 写一个扩展
扩展是一个目录:extension.json 声明身份与能力,main.js 通过 Origin.define 注册方法。 生成的模板可以直接改,也可以手写下面两个文件。
extension.json
extension.jsonjson
{
"id": "demo",
"name": "Demo",
"version": "0.1.0",
"apiVersion": "1",
"main": "main.js",
"capabilities": ["storage.read", "storage.write", "log.write"],
"limits": {
"memoryBytes": 33554432,
"stackBytes": 524288,
"timeoutMs": 30000
}
}main.js
main.jsjavascript
Origin.define({
id: "demo",
version: "0.1.0",
capabilities: ["storage.read", "storage.write", "log.write"],
activate() {
Origin.host.log("demo ready", "debug");
},
methods: {
hello(params) {
const name = String(params?.name ?? "world");
Origin.host.storage.set("last-name", name);
return { message: "hello " + name };
},
},
});4. 安装并授权
安装是显式的:清单只声明「我需要什么」,--grant 才是「我批准什么」。 可以只授予其中一部分,扩展调用未授予能力时会失败。
bashshell
bootidea ext init demo # 或用模板起步
bootidea ext install ./demo --grant storage.read,storage.write,log.write
bootidea ext list
bootidea ext info demo5. 调用一次
run 使用一次性安装目录,适合调试,不污染正式数据目录。--with 可同时带上配套包。
bashshell
bootidea run ./demo --method hello --params '{"name":"OriginLang"}'
# 不想启动独立进程时,切回同进程 QuickJS
bootidea --backend quickjs run ./demo --method hello6. 接入 agent(可选)
要先构建并安装 harness 插件,它提供 agent 循环能力。密钥只从环境变量读取, CLI 不接受把密钥写在参数里——那会留在进程列表和 shell 历史中。
bashshell
cd originlang.extension.harness
cargo build --release -p originlang-ext-harness-plugin
cp target/release/harness.dll examples/harness/
bootidea ext install ./examples/harness --grant extension.call,log.write
bootidea ext install ./examples/toolbox --grant storage.read,storage.write,log.write
BOOTIDEA_API_KEY=sk-... bootidea agent "what is 2+3?" \
--model gpt-4o-mini --base-url https://api.openai.com/v1预期结果:status: completed,模型被调用两次、工具被调用一次。 工具调用来自 toolbox 通过 contributes.tools 贡献的add 方法。
常见失败
| 现象 | 原因 | 处理 |
|---|---|---|
| 找不到 oext-sandbox | 沙箱不在 PATH,且未设 OEXT_SANDBOX_BIN | 按第 2 步配置,或加 --backend quickjs 临时绕过 |
| 首次构建卡在拉取依赖 | 无法访问 GitHub,git 依赖取不到 | 配置代理,或从有网络的机器拷贝 ~/.cargo/git 与 Cargo.lock |
| 调用返回权限错误 | 能力未授予,或清单与代码声明不一致 | 用 ext grant 补授权,并核对两处 capabilities |
| agent 报缺少扩展 | harness 未安装或 id 不是默认值 | bootidea ext list 确认,必要时显式指定扩展 id |
- 数据目录被
--home/BOOTIDEA_HOME/ 配置文件覆盖时, 排查请先确认当前生效的 home,别在错误目录里找日志。 serve的 TCP 与 HTTP 传输都需要令牌;HTTP 每次请求都要携带Authorization: Bearer。