@router-for-me/pi-cliproxyapi-provider 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -5
- package/extensions/tps.ts +161 -0
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
# pi-cliproxyapi-provider
|
|
2
2
|
|
|
3
|
-
Pi provider extension that discovers models from [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI) and registers them for use
|
|
3
|
+
Pi provider extension that discovers models from [CLIProxyAPI](https://github.com/router-for-me/CLIProxyAPI) and registers them for use in pi. It also ships a small TUI helper that shows elapsed runtime and a TPS summary after each agent turn.
|
|
4
4
|
|
|
5
5
|
## What it does
|
|
6
6
|
|
|
7
7
|
1. Registers a provider that always appears in `/login` (subscription path) and via `/cliproxyapi` / `/cpa`.
|
|
8
8
|
2. Interactive setup collects `baseUrl` + `apiKey`.
|
|
9
9
|
3. Fetches `{root}/v1/models?client_version=pi`.
|
|
10
|
-
4. Maps the
|
|
11
|
-
5. Registers inference against `{root}/backend-api
|
|
10
|
+
4. Maps the CLIProxyAPI catalog into pi models.
|
|
11
|
+
5. Registers inference against `{root}/backend-api/`.
|
|
12
|
+
6. In interactive TUI sessions, shows footer elapsed time during runs and a TPS / token usage toast when the agent settles.
|
|
12
13
|
|
|
13
14
|
## Install
|
|
14
15
|
|
|
@@ -137,11 +138,11 @@ Preferred form is **host:port only**:
|
|
|
137
138
|
| `http://127.0.0.1:8317/v1` | `http://127.0.0.1:8317/backend-api/` | same models URL |
|
|
138
139
|
| `127.0.0.1:8317` | `http://127.0.0.1:8317/backend-api/` | same models URL |
|
|
139
140
|
|
|
140
|
-
pi then
|
|
141
|
+
pi then sends inference traffic to `{inference}/codex/responses`.
|
|
141
142
|
|
|
142
143
|
## Model mapping
|
|
143
144
|
|
|
144
|
-
From CPA
|
|
145
|
+
From CPA catalog entry → pi model:
|
|
145
146
|
|
|
146
147
|
| CPA field | Pi field |
|
|
147
148
|
|-----------|----------|
|
|
@@ -162,6 +163,16 @@ If you previously maintained a static provider such as `cpa-responses` in `~/.pi
|
|
|
162
163
|
2. Point `defaultProvider` / `enabledModels` at `cliproxyapi/<model-id>` (or set `providerId` to `cpa-responses` for a drop-in id).
|
|
163
164
|
3. Remove the hand-maintained models array once the dynamic list looks correct.
|
|
164
165
|
|
|
166
|
+
## Elapsed time and TPS (TUI)
|
|
167
|
+
|
|
168
|
+
The package also registers `extensions/tps.ts`, which only activates for the primary interactive TUI session (`ctx.hasUI && ctx.mode === "tui"`):
|
|
169
|
+
|
|
170
|
+
- While the agent is running, the footer shows `Elapsed …` (updates every second).
|
|
171
|
+
- When the agent settles, the footer keeps the final elapsed time and a notification reports approximate TPS plus token usage (`out` / `in` / cache r/w / total).
|
|
172
|
+
- Subagent and print-mode sessions do not own the timer, clear the parent footer, or emit TPS toasts.
|
|
173
|
+
|
|
174
|
+
Disable just this helper via `pi config` if you only want the CLIProxyAPI provider.
|
|
175
|
+
|
|
165
176
|
## Failure behavior
|
|
166
177
|
|
|
167
178
|
- Before setup / without credentials: provider still appears in `/login`; `/cliproxyapi` and `/cpa` are shown; no models are listed yet.
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
|
|
4
|
+
const STATUS_KEY = "tps";
|
|
5
|
+
const REFRESH_INTERVAL_MS = 1000;
|
|
6
|
+
|
|
7
|
+
function isAssistantMessage(message: unknown): message is AssistantMessage {
|
|
8
|
+
if (!message || typeof message !== "object") return false;
|
|
9
|
+
const role = (message as { role?: unknown }).role;
|
|
10
|
+
return role === "assistant";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Only the interactive parent TUI session owns the footer timer / TPS summary.
|
|
15
|
+
* Subagent sessions load global extensions too, but run in print mode with a
|
|
16
|
+
* no-op UI. Guard on both hasUI and mode so a shared UI binding can never let
|
|
17
|
+
* a child clear the parent footer or emit a TPS toast.
|
|
18
|
+
*/
|
|
19
|
+
function isPrimaryUiSession(ctx: ExtensionContext): boolean {
|
|
20
|
+
return ctx.hasUI && ctx.mode === "tui";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function (pi: ExtensionAPI) {
|
|
24
|
+
let requestStartMs: number | null = null;
|
|
25
|
+
let refreshTimer: ReturnType<typeof setInterval> | undefined;
|
|
26
|
+
let statusCtx: ExtensionContext | null = null;
|
|
27
|
+
let input = 0;
|
|
28
|
+
let output = 0;
|
|
29
|
+
let cacheRead = 0;
|
|
30
|
+
let cacheWrite = 0;
|
|
31
|
+
let totalTokens = 0;
|
|
32
|
+
|
|
33
|
+
function clearRefreshTimer(): void {
|
|
34
|
+
if (refreshTimer === undefined) return;
|
|
35
|
+
clearInterval(refreshTimer);
|
|
36
|
+
refreshTimer = undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function getElapsedSeconds(): number {
|
|
40
|
+
if (requestStartMs === null) return 0;
|
|
41
|
+
return Math.floor((Date.now() - requestStartMs) / 1000);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function formatElapsed(totalSeconds: number): string {
|
|
45
|
+
const safeSeconds = Math.max(0, Math.floor(totalSeconds));
|
|
46
|
+
const days = Math.floor(safeSeconds / 86400);
|
|
47
|
+
const hours = Math.floor((safeSeconds % 86400) / 3600);
|
|
48
|
+
const minutes = Math.floor((safeSeconds % 3600) / 60);
|
|
49
|
+
const seconds = safeSeconds % 60;
|
|
50
|
+
|
|
51
|
+
const units: Array<{ value: number; suffix: string }> = [
|
|
52
|
+
{ value: days, suffix: "d" },
|
|
53
|
+
{ value: hours, suffix: "h" },
|
|
54
|
+
{ value: minutes, suffix: "m" },
|
|
55
|
+
{ value: seconds, suffix: "s" },
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
// Skip leading zero units; always keep at least seconds.
|
|
59
|
+
const parts: string[] = [];
|
|
60
|
+
let started = false;
|
|
61
|
+
for (let i = 0; i < units.length; i++) {
|
|
62
|
+
const unit = units[i]!;
|
|
63
|
+
if (!started) {
|
|
64
|
+
if (unit.value === 0 && i < units.length - 1) continue;
|
|
65
|
+
started = true;
|
|
66
|
+
}
|
|
67
|
+
parts.push(`${unit.value}${unit.suffix}`);
|
|
68
|
+
}
|
|
69
|
+
return parts.join(" ");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function setElapsedStatus(ctx: ExtensionContext, totalSeconds: number): void {
|
|
73
|
+
if (!isPrimaryUiSession(ctx)) return;
|
|
74
|
+
ctx.ui.setStatus(STATUS_KEY, ctx.ui.theme.fg("dim", `Elapsed ${formatElapsed(totalSeconds)}`));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function refreshStatus(): void {
|
|
78
|
+
if (requestStartMs === null || !statusCtx) return;
|
|
79
|
+
setElapsedStatus(statusCtx, getElapsedSeconds());
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function clearStatus(ctx?: ExtensionContext): void {
|
|
83
|
+
const target = ctx ?? statusCtx;
|
|
84
|
+
if (!target || !isPrimaryUiSession(target)) return;
|
|
85
|
+
target.ui.setStatus(STATUS_KEY, undefined);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
pi.on("before_agent_start", (_event, ctx) => {
|
|
89
|
+
// Subagents / print sessions must not own the footer timer.
|
|
90
|
+
if (!isPrimaryUiSession(ctx)) return;
|
|
91
|
+
|
|
92
|
+
// Keep the same timer across retries / tool continuations within one run.
|
|
93
|
+
if (requestStartMs !== null) {
|
|
94
|
+
statusCtx = ctx;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
requestStartMs = Date.now();
|
|
99
|
+
statusCtx = ctx;
|
|
100
|
+
input = 0;
|
|
101
|
+
output = 0;
|
|
102
|
+
cacheRead = 0;
|
|
103
|
+
cacheWrite = 0;
|
|
104
|
+
totalTokens = 0;
|
|
105
|
+
refreshStatus();
|
|
106
|
+
|
|
107
|
+
clearRefreshTimer();
|
|
108
|
+
refreshTimer = setInterval(() => refreshStatus(), REFRESH_INTERVAL_MS);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
pi.on("agent_end", (event, ctx) => {
|
|
112
|
+
// Ignore usage from subagent / non-TUI sessions.
|
|
113
|
+
if (!isPrimaryUiSession(ctx)) return;
|
|
114
|
+
if (requestStartMs === null) return;
|
|
115
|
+
|
|
116
|
+
for (const message of event.messages) {
|
|
117
|
+
if (!isAssistantMessage(message)) continue;
|
|
118
|
+
input += message.usage.input || 0;
|
|
119
|
+
output += message.usage.output || 0;
|
|
120
|
+
cacheRead += message.usage.cacheRead || 0;
|
|
121
|
+
cacheWrite += message.usage.cacheWrite || 0;
|
|
122
|
+
totalTokens += message.usage.totalTokens || 0;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
127
|
+
// Subagents must never clear the parent elapsed status or notify TPS.
|
|
128
|
+
if (!isPrimaryUiSession(ctx)) return;
|
|
129
|
+
if (requestStartMs === null) return;
|
|
130
|
+
|
|
131
|
+
const elapsedMs = Date.now() - requestStartMs;
|
|
132
|
+
const elapsedSecondsExact = elapsedMs / 1000;
|
|
133
|
+
const elapsedSecondsFloor = Math.floor(elapsedSecondsExact);
|
|
134
|
+
|
|
135
|
+
requestStartMs = null;
|
|
136
|
+
clearRefreshTimer();
|
|
137
|
+
|
|
138
|
+
// Keep the final total time in the footer after the run settles.
|
|
139
|
+
setElapsedStatus(ctx, elapsedSecondsFloor);
|
|
140
|
+
statusCtx = ctx;
|
|
141
|
+
|
|
142
|
+
if (elapsedMs <= 0) return;
|
|
143
|
+
|
|
144
|
+
const tps = output > 0 ? (output / elapsedSecondsExact).toFixed(1) : "--";
|
|
145
|
+
const message = `TPS ${tps} tok/s. out ${output.toLocaleString()}, in ${input.toLocaleString()}, cache r/w ${cacheRead.toLocaleString()}/${cacheWrite.toLocaleString()}, total ${totalTokens.toLocaleString()}, ${elapsedSecondsExact.toFixed(1)}s`;
|
|
146
|
+
ctx.ui.notify(message, "info");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
150
|
+
if (statusCtx !== ctx && !isPrimaryUiSession(ctx)) return;
|
|
151
|
+
|
|
152
|
+
clearRefreshTimer();
|
|
153
|
+
if (isPrimaryUiSession(ctx)) {
|
|
154
|
+
clearStatus(ctx);
|
|
155
|
+
}
|
|
156
|
+
if (statusCtx === ctx) {
|
|
157
|
+
requestStartMs = null;
|
|
158
|
+
statusCtx = null;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@router-for-me/pi-cliproxyapi-provider",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "Pi provider extension that auto-registers CLIProxyAPI models",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Pi provider extension that auto-registers CLIProxyAPI models, plus TUI elapsed/TPS status",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"pi-package",
|
|
8
8
|
"pi-extension",
|
|
9
9
|
"cliproxyapi",
|
|
10
|
-
"
|
|
10
|
+
"tps"
|
|
11
11
|
],
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"repository": {
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"pi": {
|
|
35
35
|
"extensions": [
|
|
36
|
-
"./extensions/index.ts"
|
|
36
|
+
"./extensions/index.ts",
|
|
37
|
+
"./extensions/tps.ts"
|
|
37
38
|
]
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|