Install
Install with PiG
pig install npm:@wbline/pi-dynamic-modelsInstall with Pi
pi install npm:@wbline/pi-dynamic-modelsPackages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.
Pi manifest JSON
The pi field of package.json.
{
"extensions": [
"./index.ts"
]
}README
Written by the package's publisher, shown as published in version 0.4.0 (README.md). PiG does not review it.
pi-dynamic-models
pi install npm:@wbline/pi-dynamic-models
Pi 会把包记录到 ~/.pi/agent/settings.json,并自动启用其中声明的扩展。仅 npm install -g 不会激活 Pi 扩展。
为 pi 提供动态模型发现能力。启动时从配置的 OpenAI 兼容服务器拉取模型列表(含元数据),自动注册为可选 provider——无需手动维护模型清单。
配置
1. 安装包
在 Pi agent 设置(~/.pi/agent/settings.json)中添加:
{
"packages": [
"local:/path/to/pi-dynamic-models"
]
}
2. 创建配置文件
创建 ~/.pi/agent/pi-dynamic-models.json:
[
{
"provider": "my-provider",
"baseUrl": "https://your-server.com/v1",
"apiKey": "$MY_PROVIDER_KEY",
"api": "openai-completions"
}
]
字段说明
| 字段 | 必填 | 说明 |
|---|---|---|
provider | ✓ | 在模型选择器中显示的名称。配置内重复的名称只保留第一条并告警。 |
baseUrl | ✓ | 服务器地址,如需要请包含 /v1 |
apiKey | 字面量密钥、环境变量名或 $ENV_VAR。开放服务器可省略或设为 "none"。 | |
api | Pi API 类型(默认:openai-completions),必须是下表中的合法值,拼错会在启动时告警并跳过该条目。 | |
defaults | 元数据补丁,应用到该服务器的所有模型 | |
overrides | 按模型 ID 精确覆盖:{ "模型ID": { ...字段 } },叠加在 defaults 之后 |
元数据覆盖(defaults / overrides):当 /models 返回的元数据缺失或不准确时,无需改服务器即可修正。优先级:overrides > defaults > /models 原值。字段名与 /models 响应同名(snake_case),可直接从响应里复制:
[
{
"provider": "my-gateway",
"baseUrl": "https://your-server.com/v1",
"apiKey": "none",
"defaults": { "context_window": 200000, "max_tokens": 65536 },
"overrides": {
"vision-x": { "input": ["text", "image"] },
"claude-x": { "max_tokens": 32000, "reasoning": true }
}
}
]
可覆盖字段:name、context_window、max_tokens、input、reasoning、thinking_level_map、cost(含 cache_read/cache_write)。context_window/max_tokens 仍会被钳制为正整数,cost 负值按 0 处理。未知字段会告警并忽略;id 在补丁中无意义(每行覆盖的键名就是模型 ID),写了它也会被剥离。覆盖在注册时实时应用,修改后重启即生效,无需清除缓存。注意 cost 与 thinking_level_map 是整体替换:覆盖只写部分子字段时,未写的子字段按缺省值处理(如 cost 只写 input,则 output 等按 0 计)。
安全建议:优先使用环境变量方式引用密钥($MY_KEY / ${MY_KEY} / 裸变量名),避免把明文密钥写入配置文件。另请注意:若 baseUrl 使用非 localhost 的 http://,密钥将以明文传输,建议改用 https://。
apiKey 解析规则:
| 写法 | 行为 |
|---|---|
"none" 或省略 | 不发送认证 |
"$MY_KEY" / "${MY_KEY}" | 从环境变量读取;未设置时告警并按 "none" 处理 |
"MY_KEY"(裸名) | 优先从环境变量读取;不存在时作为字面量密钥使用(若形似环境变量名会告警提示) |
| 其他字符串 | 直接作为字面量密钥 |
支持的 API 类型
| 值 | 适用场景 |
|---|---|
openai-completions | Ollama、vLLM、LM Studio、llama.cpp 及大多数本地服务器 |
openai-responses | 兼容 OpenAI Responses API 的服务器 |
azure-openai-responses | Azure OpenAI |
openai-codex-responses | OpenAI Codex Responses |
anthropic-messages | 兼容 Anthropic 的代理 |
google-generative-ai | 兼容 Google AI 的服务器 |
google-vertex | Google Vertex AI |
mistral-conversations | Mistral Conversations |
bedrock-converse-stream | AWS Bedrock |
pi-messages | Pi 原生协议 |
工作原理
启动时对每个 provider 并发请求一次:
GET {baseUrl}/models— 模型列表(每个条目自带元数据)
容错
/models不可达或返回空列表时,该 provider 不会注册,启动 widget 会显示具体原因。- 注册的 provider 带
refreshModels钩子,宿主刷新模型目录时会重新拉取/models,拉取失败则保留现有列表。
错误隔离
配置条目在解析时逐条校验(字段类型、api 白名单、provider 重名),坏条目只产生一条告警并跳过,不会影响其他条目,也不会导致扩展加载失败。
/models 响应格式
{
"object": "list",
"data": [
{
"id": "example-model",
"object": "meta",
"owned_by": "relay",
"context_window": 524288,
"max_tokens": 65536,
"input": ["text", "image"]
}
]
}
字段映射(context_window/max_tokens 会被钳制为正整数,cost 负值按 0 处理):
| models 字段 | 模型字段 | 缺省 |
|---|---|---|
id | id | — |
name | name | 回退为 id |
context_window | contextWindow | 128000 |
max_tokens | maxTokens | 16384 |
input | input | ["text"] |
reasoning | reasoning | false |
thinking_level_map | thinkingLevelMap | 不传 |
cost | cost(cache_read→cacheRead 等) | 全 0 |
/models 是唯一数据源:仅当其成功且返回至少一个模型时才注册该 provider。
故障排查
模型列表为空:确认配置文件位于 ~/.pi/agent/pi-dynamic-models.json,且 {baseUrl}/models 可访问。启动 widget 会显示每个 provider 的状态与具体原因。
API 行为异常:为服务器类型设置正确的 api 字段;拼错的值会在启动时被拒绝并给出支持列表。
API key 未解析:支持裸环境变量名(如 CLINE_API_KEY)与 $CLINE_API_KEY / ${CLINE_API_KEY} 写法;环境变量缺失时会在 widget 中告警,不会把 $VAR 字符串当作真实密钥发出。
contextWindow 始终为 128000:确认 /models 中对应模型带有非空的 context_window。
服务器离线时还能用吗:不能。/models 不可达时该 provider 不会注册;恢复联网后重启(或在会话内触发模型目录刷新)即可。