token_usage · diff

git:20260618.25b228f to git:20261014.4631425

54 added, 199 removed. Audit C to C.

---
{
"name": "token_usage",
"description": "Install, show, open, or close the token usage dashboard on the board display. Shows Cursor/Codex activity, Deepseek balance, standing reminders, and weather. Requires a display and a PC host running the token usage HTTP server on the same network.",
"metadata":
{
- "cap_groups": ["cap_lua"],
- "manage_mode": "web",
+ "cap_groups": ["cap_lua", "cap_http_request", "cap_router_mgr", "cap_scheduler", "claw_memory", "cap_im_wechat", "cap_im_feishu", "cap_im_qq", "cap_im_tg", "cap_im_local"],
"category": ["utility", "ai"],
"peripherals": ["display"],
"tags": ["cursor", "deepseek", "dashboard", "token", "install", "standing"]
}
}
---
# Token Usage
- Use this skill when the user asks to install, set up, show, open, refresh, or close the token usage dashboard, token monitor, or standing reminder screen.
-
- The installer switches the agent persona, registers boot auto-start for the dashboard, saves hourly telemetry into long-term memory, and sends on-the-hour greeting letters to known IM chats. Standing reminders are managed locally on the device.
-
- Match the user's language when you reply; do not switch language on your own.
-
- ## When to use
-
- - The user asks to install, set up, or adopt the token usage dashboard, token monitor, or standing reminder screen, in any language.
- - The user asks to show, open, refresh, or close the dashboard without reinstalling.
-
- ## Prerequisites
-
- - The board must declare a `display_lcd` device in board hardware info.
- - Live Cursor, Deepseek, standing, and weather data require a PC IPv4 address on the same network running the token usage HTTP server.
- - On-the-hour greeting letters require SNTP time sync after installation.
-
- ## Install
-
- Use this flow when the user wants installation, boot auto-start, hourly memory snapshots, or scheduled greeting letters.
-
- Run exactly one bundled Lua script with `lua_run_script`.
-
- If script execution returns an error, report that error directly to the user.
- Do not retry with changed arguments in the same turn unless the user explicitly asks.
+ 在用户要求查看 Cursor/Codex 工作状态、DeepSeek 用量、天气或久坐提醒时使用。打开面板与安装自动化是两个独立操作;仅打开界面不运行安装器。
- 1. Ask for the PC IPv4 address that runs the token usage HTTP server. Do not run the installer without it.
- 2. Run `{CUR_SKILL_DIR}/scripts/install_token.lua` with the inputs shown below.
- 3. Wait for the log line `[install_token] install complete` before telling the user that setup is ready.
- 4. If the script prints `[install_token] ERROR: ...`, report that line to the user and stop.
- 5. After installation completes successfully, tell the user to restart the device (for example press the reset button or use `device_reset`) for the skill to take effect.
+ ## 前提
- Re-running the installer updates the existing router rules and schedules (host, port, or letter language). On memory-constrained boards, the installer skips relaunching the dashboard when it is already running with the same host/port, and stops display jobs before a relaunch when host/port changes or `restart_dashboard=true`.
+ - 设备具有显示屏;触摸由 LVGL 自动接入。
+ - 实时工作状态、余额和天气需要同一网络内运行 Token Usage HTTP 服务的 PC,提供 `host` 或 `pc_ip` 与 `port`(默认 8080)。访问目标必须在 HTTP allowlist 中。
+ - 环境传感器和 LED 灯带为可选硬件,缺失时记录错误并保持其他功能。
+ - 定时问候依赖已同步的本地时间、PC 提供的模型配置及已建立的 IM 会话。
- The hourly greeting schedule uses cron and requires SNTP time sync. The telemetry snapshot uses a relative interval and does not require wall-clock time.
+ ## 打开面板
- ### Install Tool Call Inputs
+ 调用 `lua_run_script_async`:
```json
{
- "path": "{CUR_SKILL_DIR}/scripts/install_token.lua",
- "args": {
- "host": "192.168.1.20",
- "port": 8080,
- "language": "Chinese"
- },
- "timeout_ms": 30000
+ "path": "{CUR_SKILL_DIR}/scripts/token_usage.lua",
+ "name": "token_usage",
+ "exclusive": "display",
+ "replace": false,
+ "timeout_ms": 0,
+ "args": { "host": "192.168.1.20", "port": 8080 }
}
```
- - `host` or `pc_ip` is required.
- - `port` defaults to `8080`.
- - `language` defaults to `Chinese` and controls scheduled greeting letters.
- - `restart_dashboard` optional boolean. When true, stop the running dashboard and relaunch it after install even if host/port are unchanged.
- - `skip_dashboard_start` optional boolean. When true, update soul/rules/schedules only and do not queue a dashboard start event.
-
- ### Install Example
-
- User asks to install the token dashboard and gives a PC IP.
- -> `lua_run_script` with the inputs above
- -> wait for `[install_token] install complete`
- -> Reply in the user's language, confirming boot auto-start, hourly memory snapshots, and on-the-hour greeting letters.
-
- ## Show Dashboard
-
- Use this flow when the user only wants to open or refresh the dashboard UI.
-
- Run exactly one bundled Lua script asynchronously with `lua_run_script_async`.
-
- If script execution returns an error, report that error directly to the user.
- Do not retry with changed arguments or run another script in the same turn unless the user explicitly asks.
-
- ### Script Args Schema
+ 不知道 PC 地址而只想查看本地界面时可用空 `args`。同名任务已运行时先查看状态;只有用户明确要求重启或替换占用显示的任务时才使用 `replace: true`。
- ```json
- {
- "type": "object",
- "properties": {
- "host": {
- "type": "string",
- "description": "PC IPv4 address that runs the token usage HTTP server. Also accepts pc_ip."
- },
- "port": {
- "type": "integer",
- "default": 8080,
- "minimum": 1,
- "maximum": 65535,
- "description": "HTTP port on the PC host."
- },
- "heartbeat_ms": {
- "type": "integer",
- "default": 1000,
- "minimum": 1000,
- "maximum": 60000,
- "description": "Legacy alias for status_poll_ms."
- },
- "status_poll_ms": {
- "type": "integer",
- "default": 1000,
- "minimum": 1000,
- "maximum": 60000,
- "description": "Interval for GET /device-status polling from the PC host."
- },
- "token_poll_ms": {
- "type": "integer",
- "default": 30000,
- "minimum": 1000,
- "maximum": 600000
- },
- "aux_poll_ms": {
- "type": "integer",
- "default": 30000,
- "minimum": 1000,
- "maximum": 600000
- },
- "weather_poll_ms": {
- "type": "integer",
- "default": 600000,
- "minimum": 1000,
- "maximum": 3600000
- },
- "weather_retry_ms": {
- "type": "integer",
- "default": 10000,
- "minimum": 1000,
- "maximum": 600000
- },
- "http_timeout_ms": {
- "type": "integer",
- "default": 5000,
- "minimum": 1000,
- "maximum": 60000
- },
- "instance_id": {
- "type": "string",
- "description": "Optional stable id used for worker queue and async job names."
- }
- }
- }
- ```
+ - 查询状态:`lua_get_async_job`,参数 `{"name":"token_usage"}`。
+ - 查看日志:`lua_tail_async_job`,使用返回的游标增量读取。
+ - 关闭面板:`lua_stop_async_job`,参数 `{"name":"token_usage","wait_ms":3000}`。
- ### Dashboard Tool Call Inputs
+ 可选脚本参数:
- Show the dashboard with PC polling enabled:
+ | 参数 | 默认值 | 用途 |
+ | --- | --- | --- |
+ | `status_poll_ms` / `cursor_poll_ms` | 1000 | 工作状态轮询,500–60000 ms |
+ | `token_poll_ms` | 30000 | 用量轮询,1000–600000 ms |
+ | `aux_poll_ms` | 30000 | 余额等数据轮询,1000–600000 ms |
+ | `weather_poll_ms` | 600000 | 天气轮询,1000–3600000 ms |
+ | `weather_retry_ms` | 10000 | 天气重试间隔,1000–600000 ms |
+ | `http_timeout_ms` | 5000 | 单次 HTTP 超时,1000–60000 ms |
+ | `boot_delay_ms` | 3000 | 启动延迟 |
+ | `led_gpio` / `led_count` | 27 / 5 | 可选提醒灯带,先确认硬件连接 |
- ```json
- {
- "path": "{CUR_SKILL_DIR}/scripts/token_usage.lua",
- "args": {
- "host": "192.168.1.20",
- "port": 8080
- },
- "timeout_ms": 0
- }
- ```
+ 当前界面使用 `lvgl.init(options)`,不再获取板级 LCD/触摸裸句柄。点击右上角 `>` 切换用量页和环境页,点击用量页右下角 `OK` 确认本次起身;旧版原始触摸轮询滑动改为 LVGL 控件事件。
- Show the dashboard UI without PC polling when the host is unknown:
+ ## 安装自动化
- ```json
- {
- "path": "{CUR_SKILL_DIR}/scripts/token_usage.lua",
- "args": {},
- "timeout_ms": 0
- }
- ```
+ 仅当用户要求安装以下自动化时运行安装器:替换 agent persona、开机启动面板、按小时采集遥测、向已有 IM 会话发送每小时问候。提前说明这四项行为;仅要求查看用量时不要安装。
- Use a custom heartbeat interval when push is unavailable:
+ 通过 `lua_run_script` 执行:
```json
{
- "path": "{CUR_SKILL_DIR}/scripts/token_usage.lua",
- "args": {
- "host": "192.168.1.20",
- "heartbeat_ms": 15000
- },
- "timeout_ms": 0
+ "path": "{CUR_SKILL_DIR}/scripts/install_token.lua",
+ "args": { "host": "192.168.1.20", "port": 8080, "language": "Chinese" },
+ "timeout_ms": 30000
}
```
- ### Dashboard Recommended Flow
-
- Run the bundled Lua script asynchronously so the LVGL dashboard stays alive:
-
- - Script: `{CUR_SKILL_DIR}/scripts/token_usage.lua`
- - Capability: `lua_run_script_async`
- - Timeout: `0`
- - Name: `token_usage`
- - Exclusive: `display`
- - Replace: `true`
- - Args: optional object
-
- 1. Activate the `board_hardware_info` skill and confirm that a `display_lcd` device is listed.
- 2. If no display is listed, tell the user that the board does not declare a display and stop.
- 3. Ask for the PC host IP when the user wants live Cursor, Deepseek, standing, or weather data. Pass it as `host` or `pc_ip`.
- 4. If the host is unknown, run the script with empty `args` to show the local UI without Windows polling.
- 5. Run `{CUR_SKILL_DIR}/scripts/token_usage.lua` with the resolved `args`.
- 6. Report the start result or error directly to the user.
- 7. Use `lua_get_async_job` or `lua_tail_async_job` when the user asks for runtime status or logs.
- 8. Stop the async Lua job named `token_usage` when the user asks to close the dashboard.
-
- ## Configuration
-
- Install and schedule tunables live in `{CUR_SKILL_DIR}/scripts/token_usage_config.lua` — snapshot interval, letter cron expression, default letter language, letter persona name, and default port. After editing the config, run `{CUR_SKILL_DIR}/scripts/install_token.lua` again to update the scheduler entries and router rules.
-
- ## Persona
-
- Installation copies `{CUR_SKILL_DIR}/soul_token.md` to `memory/soul.md` under the device DATA root. ESP-Claw speaks as a warm desk-side colleague with gentle, good-natured teasing — not a status dashboard. Scheduled hourly letters follow the same voice: human, caring, and concise.
-
- ## Behavior
-
- The dashboard script initializes `display_lcd` and optional `lcd_touch`, builds a single-page LVGL dashboard, and keeps running until the async job is stopped.
+ - `host` 或 `pc_ip` 必填;已有上下文提供地址时直接使用。
+ - `language` 默认 `Chinese`,用于问候信。
+ - `restart_dashboard: true` 仅在用户要求重启当前面板时传入;只停止名为 `token_usage` 的任务。
+ - `skip_dashboard_start: true` 只更新 persona、路由和定时配置,跳过本次启动与重启。
+ - 重装按固定 id 更新本 Skill 的路由和定时任务,不直接重写共享配置文件。
+ - 等到 `[install_token] install complete` 再报告安装步骤完成。面板异步启动的结果需查看对应任务,不能把事件入队当成显示成功。
+ - 失败后报告实际错误,不自行修改参数反复重试。
- When `host` is set, the script polls the PC over HTTP in the main loop (no extra Lua worker task):
+ 安装器读取 `{CUR_SKILL_DIR}/soul_token.md`,写入 DATA 根目录的 `memory/soul.md`。开机路由匹配 `app_claw` 的 `boot_completed` 事件;定时参数从 `event.payload.user_payload` 读取。已有面板在运行时默认保留,更新的 PC 地址在下一次启动生效。
- 1. `GET /cursor-status` every `cursor_poll_ms` / `status_poll_ms` (default 1s)
- 2. `GET /deepseek-balance`, `/deepseek-account-balance`, `/weather` on their own intervals
+ ## 数据与包资源
- Polling runs in the same async job as the LVGL UI to avoid spawning a second 32KB Lua task on memory-constrained boards.
+ `{CUR_SKILL_DIR}/scripts/token_usage_config.lua` 保存默认配置;在源码包中修改后重新部署并运行安装器更新自动化。SYSTEM 中的包文件按只读处理。
- The UI merges `cursor_status` and `codex_status` locally for the four-state IDE indicator.
+ 脚本通过 `storage.get_root_dir()` 和 `storage.join_path(...)` 将可写数据统一放在 DATA 根目录的 `token_usage/`:
- The standing icon is drawn directly on an LVGL canvas in the bottom-left area of the dashboard.
+ - `standing_state.json`:起身提醒完成情况。
+ - `telemetry_history/`:每日遥测 JSON。
+ - `last_sent.txt`:每小时问候去重记录。
- Touching the screen during an active standing reminder window confirms that hour's pill.
+ 旧版写在包目录下的历史数据不再作为默认数据源;如需保留,先将对应数据搬到上述 DATA 目录。不要覆盖已有 DATA 历史文件。
- After installation, `{CUR_SKILL_DIR}/scripts/token_usage_snapshot.lua` runs every hour to sample local environmental sensors (temperature, humidity, pressure, estimated CO2) plus PC data (DeepSeek token balance, account balance, outdoor weather, Cursor/Codex activity) and local standing count from the dashboard state file, append `{CUR_SKILL_DIR}/telemetry_history/`, and call `memory_store` with tags `token_usage,environment,standing,weather,cursor`.
+ `{CUR_SKILL_DIR}/scripts/token_usage_snapshot.lua` 按配置间隔采集环境、天气、用量和工作状态,追加历史并调用 `memory_store`。CO2 数值由气体电阻估算,不是直接测量。
- `{CUR_SKILL_DIR}/scripts/token_usage_letter.lua` runs synchronously (no async task spawned) every minute via scheduler, but only composes and sends a letter at the top of each clock hour. It reads telemetry history and recent long-term memory (`memory_recall` on the `token_usage` label), then broadcasts the greeting to known IM chats. Letters weave desk air, outdoor weather, standing progress, token trends, and agent workload into everyday language with one caring suggestion — not a technical report. Synchronous execution avoids a 32KB Lua task allocation that would fail when internal DRAM is fragmented.
+ `{CUR_SKILL_DIR}/scripts/token_usage_letter.lua` 每分钟由 scheduler 同步调用,在配置的发送分钟生成每小时问候。读取历史和 `memory_recall`,通过已有会话对应的 IM 能力发送;没有会话时跳过。该流程不会新增长驻任务。修改发送分钟使用配置项 `letter_send_minute`,默认 0。