pi-opencode-go-quota

Show OpenCode Go subscription usage (5h / weekly / monthly) in the Pi footer

Packages

Package details

extension

Install pi-opencode-go-quota from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:pi-opencode-go-quota
Package
pi-opencode-go-quota
Version
0.1.0
Published
Nov 16, 2026
Downloads
344/mo · 344/wk
Author
bpldbt
License
MIT
Types
extension
Size
13.6 KB
Dependencies
0 dependencies · 1 peer
Pi manifest JSON
{
  "extensions": [
    "./index.ts"
  ]
}

Security note

Pi packages can execute code and influence agent behavior. Review the source before installing third-party packages.

README

pi-opencode-go-quota

在 Pi 底部状态栏显示 OpenCode Go 订阅的剩余用量(单账号,不做切换)。

opencode-go 5h 100% · 7d 100% · 30d 77%(23d)
  • 5h / 7d / 30d:滚动 5 小时窗口、每周窗口、每月窗口的剩余百分比。
  • (23d):月窗口额度的重置倒计时。

括号前故意不加空格。@narumitw/pi-statusline 渲染扩展状态前会执行 .replace(/\s+\([^)]*\)\s*$/, ""),带空格的 (23d) 会被整段删掉; 不带空格就不匹配,内置 footer 和 statusline 两种情况下都能显示。 tests/usage.test.ts 里有对应的回归用例。

数据来源

GET https://opencode.ai/zen/go/v1/usage
Authorization: Bearer <Go API key>

返回三个窗口的 status / percent(已用百分比)/ resetsAt:

{"usage":{
  "rolling":{"status":"ok","percent":0,"resetsAt":"2026-11-16T06:44:15.000Z"},
  "weekly": {"status":"ok","percent":0,"resetsAt":"2026-11-23T00:00:00.000Z"},
  "monthly":{"status":"ok","percent":23,"resetsAt":"2026-12-08T18:42:15.000Z"}}}

需要注意的限制:

  • 这个端点没有出现在官方文档里(Console API 只文档化了 inference 和 providers),行为可能变动;官方建议是在 https://opencode.ai/console 里看用量。
  • 只返回百分比,没有美元金额,也没有分模型明细。Go 的额度是「每个模型各自的月度美元额度 + 5h/周/月三层窗口」,所以这里的百分比是账户级的汇总。

安装

pi install npm:pi-opencode-go-quota

本地开发时可以直接装目录:

pi install C:\Dev\pi-extensions\pi-opencode-go-quota

安装后重启 Pi 或执行 /reload。已写进 ~/.pi/agent/settings.json 的 packages 里则无需重复安装。

API key 解析顺序

  1. ctx.modelRegistry.getProviderAuth("opencode-go").auth.apiKey(Pi 自己解析的凭据);
  2. $PI_CODING_AGENT_DIR/auth.json,默认 ~/.pi/agent/auth.json 中的 opencode-go.key。

只读取,不回写,也不打印明文。

刷新时机

时机 行为
session_start 立即刷新
agent_start 立即刷新,并启动 60s 定时器
运行期间 每 60s 刷新一次,进入空闲后停表
agent_settled 停止定时器后刷新(60s 内不重复)
after_provider_response 当前模型是 opencode-go 且返回 429 时强制刷新

状态显示

情况 底部文本
正常 opencode-go 5h 100% · 7d 100% · 30d 77%(23d)
找不到 key opencode-go no key
key 无效(401/403) opencode-go key invalid
网络错误 / 5xx / 响应异常 opencode-go offline

状态键为 opencode-go-usage。装了 @narumitw/pi-statusline 时,该状态渲染在状态栏下面单独一行。

测试

npm test

覆盖解析(含 rate-limited、越界百分比)、剩余百分比换算、倒计时格式、真实响应快照的格式化,以及 fetch 的 200 / 401 / 5xx / 网络异常 / 非 JSON 分支。测试用注入的 fetchImpl,不访问网络。

文件

  • index.ts — 扩展本体:凭据解析、fetchGoUsage、formatUsageStatus、事件注册。
  • tests/usage.test.ts — 纯逻辑与 fetch 分支测试。