anthropics / anthropics/claude-code

Function hooks: an event when session usage or a rate-limit window changes, so trackers do not have to poll

未关闭
#94,424 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
area:hooks enhancement
主要语言
Python
星标
145k
派生
23.1k
PR 合并指标
PR 指标待抓取

描述

## What we are building

Three mods that keep session figures on screen (https://github.com/Arunjay4213/claude-mods): the context window fill, the 5-hour and 7-day plan windows with a reset countdown and burn rate, and a per-turn cost ledger. All three read `$.session.usage()`.

## The gap

`$.session.usage()` is a pull. Nothing tells a mod that the figures moved, so a live meter has to sample on `turn.complete` and on a `$.clock.every(60_000)` tick. Two things follow:

1. The countdown to a reset and a change in `percentUsed` reach the screen up to a minute late.
2. Every idle session runs a timer that mostly finds nothing changed, which is exactly the kind of busy work the hooks worker's budgets exist to discourage.

In #91870 the same ask was raised as "an event when a limit changes state (`{kind, percent, resetsAt}`)" and, after our mods landed, narrowed to "What remains of item 4 is an event when a threshold is crossed; today a quota guard has to poll" (https://github.com/anthropics/claude-code/issues/91870#issuecomment-5670691408). It has not been answered.

## Proposal

A `session.usage` event dispatched by core whenever the figures `$.session.usage()` returns would change: after each API response that carries `context` or `rateLimits`, and when a window resets. Input as the same `SessionUsage` shape, so a hook can write:

```ts
on('session.usage', ($, e, next) => {
const five = e.rateLimits.find(r => r.kind === 'five_hour')
if (five && five.percentUsed >= 90) $.ui.toast('5-hour window at 90%')
return next(e)
})
```

With that event a tracker needs no timer at all. The 60-second tick would still be wanted for a countdown that moves while nothing else happens, but it could then do no I/O.

## Two smaller things seen on 2.1.270 while building this

- `rateLimits[].percentUsed` arrives as floats such as `7.000000000000001`; whole numbers would match what the API means.
- After a window resets, `rateLimits` drops the window until the next API response. That is workable, but a note in the declarations would save the next person a surprise.

Claude Code 2.1.270, `CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1`, Linux.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。