# pi-thinking-realtime-tps

A Pi extension that keeps Pi's original animated working loader, but adds elapsed time, a cumulative current-run token count, and **real-time tokens-per-second (TPS)** to the active working message:

```text
Working… (7m 7s · ↓ 30.0k tokens ⚡ 25 tok/s)
```

The token number is always the cumulative current-run total (`input + output`) so it keeps increasing during a run. The token arrow shows the current movement direction: `↑` while request/input tokens are being sent upstream, and `↓` while assistant output tokens are coming back from the model. The `⚡ N tok/s` segment is a live, sliding-window throughput reading that updates as tokens stream in.

When Pi is streaming hidden thinking content, the same active loader line switches to:

```text
Thinking… (7m 7s · ↓ 30.0k tokens ⚡ 25 tok/s)
```

## Behavior

- Keeps Pi's original animated working indicator.
- Suppresses the separate collapsed hidden-thinking label and its empty placeholder line so `Thinking...` only appears in the controlled loader.
- On each `agent_start`, starts a per-run timer and token counter.
- Switches the controlled working message between `Thinking…` and `Working…` based on the current assistant stream phase.
- Updates the working message every second with elapsed time from `agent_start`.
- Shows a cumulative `input + output` token total that increases through the run; `↑`/`↓` only indicates the current token movement direction.
- **Real-time TPS** — measures live tokens-per-second over a sliding window (default `1s`) as text/thinking deltas stream in. The segment disappears when no tokens arrive within the window, so it only appears while actively streaming.
- For input, estimates only the current user talk/prompt text, not the full serialized provider payload, system prompt, tool schemas, or cached context overhead.
- For output, estimates the current assistant message while streaming, then reconciles the output estimate with provider-reported output usage when each assistant message ends.
- Appends staged idle notices when no request or response token activity is seen:
  - after more than 60 seconds: yellow `- agent is taking a while...`
  - after more than 180 seconds: yellow `- agent is probably idle...`
  - after more than 300 seconds: red `- agent is idle`
- Does not intercept terminal input, send messages, steer/follow up, or await work in event handlers, so Esc/abort stays owned by Pi.
- Clears intervals and restores Pi's working-loader defaults on `agent_end`.

No commands or settings are registered. To change the labels, update interval, or TPS window, edit the constants at the top of `src/index.ts`:

- `UPDATE_INTERVAL_MS` — how often the loader line refreshes (default `1000`)
- `TPS_WINDOW_MS` — the sliding window used for TPS (default `1000`)

## Install

Install once for your computer:

```bash
pi install git:github.com/Lxxyx/pi-thinking-messaging
```

Or from npm:

```bash
pi install npm:pi-thinking-realtime-tps
```

Then reload your active Pi session:

```text
/reload
```

## Local development

Load the local checkout for one run:

```bash
pi -e /path/to/pi-thinking-realtime-tps
```

Run the focused unit tests:

```bash
npm test
```

Type-check:

```bash
npm run typecheck
```

## Token count limitation

During streaming, provider-reported exact usage may not be finalized until the response completes. This extension intentionally treats input tokens as current user talk/prompt size rather than exact provider input usage, because provider `usage.input` includes system prompt, tool schemas, cached context, and other overhead. Assistant output is estimated while streaming and reconciled with provider `usage.output` when Pi receives it at `message_end`. The displayed number is the cumulative sum of the current-run input estimate plus assistant output tokens; the arrow only shows which side is currently moving.

## License

MIT. This is a fork of [`dantetekanem/pi-thinking-messaging`](https://github.com/dantetekanem/pi-thinking-messaging) (MIT), with the addition of real-time TPS measurement.
