@yalieny/pi-better-cost-display-footer

pi extension: scriptable peak/off-peak tier rules per provider with an enhanced footer (tier label, cache-hit-rate precision, custom currency symbol)

Packages

Package details

extension

Install @yalieny/pi-better-cost-display-footer from npm and Pi will load the resources declared by the package manifest.

$ pi install npm:@yalieny/pi-better-cost-display-footer
Package
@yalieny/pi-better-cost-display-footer
Version
2.1.1
Published
Aug 28, 2026
Downloads
1,390/mo · 737/wk
Author
yalieny
License
MIT
Types
extension
Size
37.3 KB
Dependencies
0 dependencies · 2 peers
Pi manifest JSON
{
  "extensions": [
    "./extensions"
  ]
}

Security note

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

README

@yalieny/pi-better-cost-display-footer

pi 扩展:脚本化多段计价动态列表 + footer 增强。

  • 多段动态计价:每个 provider 一个 tierFn 脚本函数(JS 表达式字符串)判定当前档位,档位是任意字符串 id(peak / flat / valley / sharp…),不绑定峰谷语义;在 session_start 和每次发送消息时重注册 provider 的 cost(仅影响 pi 本地用量统计)。
  • 档位标签:价格后紧跟当前档位标签(如 ¥0.047(梁文峰)),按档位 id 配置文案与颜色;未配置标签的档位不显示。
  • CH 精度:footer 缓存命中率小数位可配(默认 1 位)。
  • 货币符号:按 provider 配置计费金额符号(默认 $)。
  • 配置升级:旧版顶层计价配置与 peakWindows 首次启动自动迁移为 provider 分层 + tierFn 格式(语义不变)。

安装

pi install npm:@yalieny/pi-better-cost-display-footer

或本地加载:pi -e npm:@yalieny/pi-better-cost-display-footer

配置

扩展读取 <configDir>/pi-better-cost-display-footer.json(configDir 默认 ~/.pi/agent,可用 PI_CODING_AGENT_DIR 覆盖)。配置文件不存在时自动生成默认配置并落盘(DeepSeek 官方价目,开箱即用,生成后可直接编辑);配置文件存在时按字段覆盖默认值(provider/model 三级深合并)。

{
  "cacheHitRatePrecision": 3,
  "providers": {
    "deepseek": {
      "timezone": "Asia/Shanghai",
      "effectiveFrom": "2026-08-23T00:00:00+08:00",
      "currencySymbol": "¥",
      "tierFn": "(ctx) => ctx.isWeekend() ? 'offPeak' : (ctx.inWindow('09:00','12:00') || ctx.inWindow('14:00','18:00')) ? 'peak' : 'offPeak'",
      "defaultTier": "offPeak",
      "labels": {
        "peak": { "text": "(梁文峰)", "color": "error" },
        "offPeak": { "text": "(梁文谷)", "color": "success" }
      },
      "models": {
        "deepseek-v4-flash": {
          "offPeak": { "input": 1.5, "output": 4.5, "cacheRead": 0.05, "cacheWrite": 0 },
          "peak":    { "input": 3.0, "output": 9.0, "cacheRead": 0.1, "cacheWrite": 0 }
        }
      }
    }
  }
}

价格单位:每百万 tokens(元),与 pi 的 calculateCost 约定一致。effectiveFrom(含)之前不做任何事。

档位判定(tierFn)

tierFn 是 JS 函数表达式字符串(编译执行),返回任意档位 id(字符串)。上下文 ctx 均为配置时区本地值:

字段/方法 说明
ctx.now 当前时刻 Date
ctx.timezone 计费时区
ctx.weekday 0=周日 … 6=周六
ctx.hour / ctx.minute 时区本地时钟
ctx.dateStr YYYY-MM-DD(如 '2026-10-01',可做日期区间比较)
ctx.isWeekend() 周六/周日
ctx.inWindow(start, end) HH:mm 窗口命中,含起点不含终点
ctx.afterISO(iso) / ctx.beforeISO(iso) ISO 日期比较(节假日窗口、调价日等)

任一声场(语法错误、执行异常、返回非字符串、未配置 tierFn)→ 回退 defaultTier(默认 "offPeak"),绝不崩溃。旧字段 peakWindows 首次加载自动迁移为等价 tierFn 并写回配置文件(语义不变)。

示例(档位数不限):

  • 工作日峰谷 + 周末全天低谷(DeepSeek 2026-08-23 起官方新规): "(ctx) => ctx.isWeekend() ? 'offPeak' : (ctx.inWindow('09:00','12:00') || ctx.inWindow('14:00','18:00')) ? 'peak' : 'offPeak'"
  • 三段计价(尖峰/高峰/平段): "(ctx) => ctx.inWindow('11:00','12:00') ? 'sharp' : ctx.inWindow('09:00','12:00') ? 'peak' : 'valley'" 对应每个模型三方价格 "sharp": {...}, "peak": {...}, "valley": {...},labels 同样按档位 id 添加。
  • 单一平价(官方取消峰谷):"(ctx) => 'offPeak'"
  • 国庆节假日全天低谷: "(ctx) => ctx.dateStr >= '2026-10-01' && ctx.dateStr <= '2026-10-07' ? 'offPeak' : (ctx.inWindow('09:00','12:00') ? 'peak' : 'offPeak')"

tierFn 为本地执行脚本(new Function 编译),仅应来自受信任的配置文件。

一键更新官方定价

/deepseek-pricing-update

该命令把抓取官方价目页、解析价格、更新配置文件的完整任务交给 agent 执行(自然语言指令,无需手动维护价格表)。命令会告知 agent 配置文件位置与 JSON 格式;agent 抓取 DeepSeek 官方价目页 后更新 providers.deepseek.models 下各模型的分档价格,其余字段不动。

测试

npm install && npm test

说明

  • 顶层仅配置全局 cacheHitRatePrecision;timezone/effectiveFrom/currencySymbol/tierFn/defaultTier/labels/models 均配置在对应 provider 下。
  • provider 必须在 models.json 中有定义;未列出的模型保持原价。
  • 模型缺少当前档位价格时保持原价并打日志。
  • cost 仅影响 pi 本地用量统计,不影响 API 实际账单。