@dsnbyte/pi-cheaper-inference 0.0.0-stage → 0.1.1
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/CHANGELOG.md +12 -0
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/extensions/index.ts +701 -0
- package/package.json +62 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.1.0] - 2026-11-22
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- Cheaper Inference provider registration (`cheaper-inference`) using OpenAI-compatible Chat Completions (`https://api.cheaperinference.com/v1`).
|
|
9
|
+
- API key authentication via Pi's `/login` flow or the `CHEAPER_INFERENCE_API_KEY` environment variable.
|
|
10
|
+
- Interactive `/cheaper-models` TUI picker with live search, pricing per million tokens, context window, and capability indicators (`reasoning`, `vision`).
|
|
11
|
+
- Persistent model selection stored in `~/.pi/agent/cheaper-inference.json` so only user-selected models appear in `/model`.
|
|
12
|
+
- Non-TUI fallback output displaying selected models and catalog availability.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dsnbyte
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-cheaper-inference
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Pi extension that adds the Cheaper Inference provider with API key login and a curated model picker.
|
|
4
|
+
|
|
5
|
+
[Cheaper Inference](https://www.cheaperinference.com/) is an inference API provider offering unified access to models from multiple upstream providers. `pi-cheaper-inference` integrates it into Pi so you can authenticate with your API key, choose the models you want with `/cheaper-inference-models`, and use them in Pi via `/model`.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **API Key Authentication**: Sign in via `/login` or the `CHEAPER_INFERENCE_API_KEY` environment variable.
|
|
10
|
+
- **Interactive Model Picker**: Search and filter available models, inspect pricing and capabilities, and toggle the ones you want.
|
|
11
|
+
- **Persistent Selection**: Saves your chosen models across Pi sessions.
|
|
12
|
+
- **Live Registration**: Updates Pi's `/model` list as soon as you close `/cheaper-inference-models`.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pi install npm:@dsnbyte/pi-cheaper-inference
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Try Without Installing
|
|
21
|
+
|
|
22
|
+
You can test the extension in a single Pi session without modifying your settings:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pi -e npm:@dsnbyte/pi-cheaper-inference
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Setup
|
|
29
|
+
|
|
30
|
+
1. Create an account and API key in the [Cheaper Inference dashboard](https://www.cheaperinference.com/docs) (requires a funded wallet).
|
|
31
|
+
2. Authenticate in Pi using either method:
|
|
32
|
+
- Run `/login`, select **Cheaper Inference**, and paste your API key.
|
|
33
|
+
- Or export the environment variable before launching Pi:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
export CHEAPER_INFERENCE_API_KEY=ci_live_your_key
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
Open the model selector inside an active Pi session:
|
|
42
|
+
|
|
43
|
+
```text
|
|
44
|
+
/cheaper-inference-models
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
In the interactive picker:
|
|
48
|
+
|
|
49
|
+
- **Type** to filter models by ID, owner, or provider.
|
|
50
|
+
- **Up / Down / PgUp / PgDn / Home / End** to move through the list.
|
|
51
|
+
- **Space** to toggle whether a model is included.
|
|
52
|
+
- **Enter** to activate the highlighted model, set it as the active model, save, and close.
|
|
53
|
+
- **Ctrl+R** (or **F5**) to manually refresh the model list from the provider.
|
|
54
|
+
- **Esc** to save your selection and update `/model`.
|
|
55
|
+
|
|
56
|
+
You can also run `/cheaper-inference-models refresh` to force an immediate catalog refresh when opening the menu.
|
|
57
|
+
|
|
58
|
+
After selecting models, switch to any of them with `/model`.
|
|
59
|
+
|
|
60
|
+
## Configuration
|
|
61
|
+
|
|
62
|
+
Selected model snapshots are stored at `~/.pi/agent/cheaper-inference.json`:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"models": [
|
|
67
|
+
{
|
|
68
|
+
"type": "chat",
|
|
69
|
+
"id": "gemini-4-neon",
|
|
70
|
+
"name": "gemini-4-neon"
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
For full provider documentation, see [cheaperinference.com/docs](https://www.cheaperinference.com/docs).
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getAgentDir,
|
|
3
|
+
type ExtensionAPI,
|
|
4
|
+
type ExtensionCommandContext,
|
|
5
|
+
type ProviderModelConfig,
|
|
6
|
+
type Theme,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { matchesKey, truncateToWidth, type Component, type TUI } from "@earendil-works/pi-tui";
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
|
|
12
|
+
const PROVIDER_ID = "cheaper-inference";
|
|
13
|
+
const PROVIDER_NAME = "Cheaper Inference";
|
|
14
|
+
const BASE_URL = "https://api.cheaperinference.com/v1";
|
|
15
|
+
const API_KEY_ENV = "$CHEAPER_INFERENCE_API_KEY";
|
|
16
|
+
const DEFAULT_CONTEXT = 128_000;
|
|
17
|
+
const DEFAULT_MAX_TOKENS = 16_384;
|
|
18
|
+
const CATALOG_CACHE_TTL_MS = 60 * 60 * 1000;
|
|
19
|
+
|
|
20
|
+
export interface CheaperInferenceOptions {
|
|
21
|
+
configPath?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface CheaperInferenceConfig {
|
|
25
|
+
models: ProviderModelConfig[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface CatalogCache {
|
|
29
|
+
fetchedAt: number;
|
|
30
|
+
models: CatalogModel[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CatalogModel {
|
|
34
|
+
id: string;
|
|
35
|
+
context_length?: number | null;
|
|
36
|
+
max_output_tokens?: number | null;
|
|
37
|
+
owned_by?: string;
|
|
38
|
+
provider?: string | null;
|
|
39
|
+
type?: string;
|
|
40
|
+
endpoint?: string;
|
|
41
|
+
supported_endpoints?: string[];
|
|
42
|
+
capabilities?: {
|
|
43
|
+
vision?: boolean;
|
|
44
|
+
video?: boolean;
|
|
45
|
+
reasoning?: boolean;
|
|
46
|
+
streaming?: boolean;
|
|
47
|
+
image_generation?: boolean;
|
|
48
|
+
image_edit?: boolean;
|
|
49
|
+
};
|
|
50
|
+
is_free?: boolean;
|
|
51
|
+
pricing?: {
|
|
52
|
+
currency?: string;
|
|
53
|
+
input_per_million?: string;
|
|
54
|
+
output_per_million?: string;
|
|
55
|
+
cache_read_input_per_million?: string;
|
|
56
|
+
cache_write_input_per_million?: string;
|
|
57
|
+
cache_read_per_million?: string;
|
|
58
|
+
cache_write_per_million?: string;
|
|
59
|
+
discount_percent?: string;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function resolveConfigFile(options?: CheaperInferenceOptions): string {
|
|
64
|
+
if (options?.configPath) {
|
|
65
|
+
return options.configPath;
|
|
66
|
+
}
|
|
67
|
+
return join(getAgentDir(), "cheaper-inference.json");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function resolveCacheFile(configFile: string): string {
|
|
71
|
+
return configFile.endsWith(".json")
|
|
72
|
+
? `${configFile.slice(0, -5)}-cache.json`
|
|
73
|
+
: `${configFile}-cache.json`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function readJsonObject(filePath: string): Record<string, unknown> | undefined {
|
|
77
|
+
if (!existsSync(filePath)) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const raw = JSON.parse(readFileSync(filePath, "utf-8")) as unknown;
|
|
81
|
+
return raw && typeof raw === "object" && !Array.isArray(raw) ? (raw as Record<string, unknown>) : undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function writeJsonFile(filePath: string, data: unknown): void {
|
|
85
|
+
try {
|
|
86
|
+
mkdirSync(dirname(filePath), { recursive: true });
|
|
87
|
+
writeFileSync(filePath, JSON.stringify(data, null, 2) + "\n", "utf-8");
|
|
88
|
+
} catch {
|
|
89
|
+
// Gracefully ignore write failures (e.g. read-only filesystem)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function loadConfig(configFile: string): CheaperInferenceConfig {
|
|
94
|
+
try {
|
|
95
|
+
const cfg = readJsonObject(configFile);
|
|
96
|
+
if (!cfg) {
|
|
97
|
+
return { models: [] };
|
|
98
|
+
}
|
|
99
|
+
const models = Array.isArray(cfg.models)
|
|
100
|
+
? cfg.models.filter(
|
|
101
|
+
(m): m is ProviderModelConfig =>
|
|
102
|
+
Boolean(m && typeof m === "object" && typeof (m as ProviderModelConfig).id === "string"),
|
|
103
|
+
)
|
|
104
|
+
: [];
|
|
105
|
+
return { models };
|
|
106
|
+
} catch {
|
|
107
|
+
return { models: [] };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function saveConfig(configFile: string, cfg: CheaperInferenceConfig): void {
|
|
112
|
+
writeJsonFile(configFile, cfg);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function loadCatalogCache(cacheFile: string): CatalogCache | undefined {
|
|
116
|
+
try {
|
|
117
|
+
const obj = readJsonObject(cacheFile);
|
|
118
|
+
if (!obj) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const fetchedAt = obj.fetchedAt;
|
|
122
|
+
if (typeof fetchedAt !== "number" || !Number.isFinite(fetchedAt) || fetchedAt <= 0) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
if (!Array.isArray(obj.models)) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
const models = obj.models.filter(
|
|
129
|
+
(m): m is CatalogModel =>
|
|
130
|
+
Boolean(m && typeof m === "object" && typeof (m as CatalogModel).id === "string"),
|
|
131
|
+
);
|
|
132
|
+
return { fetchedAt, models };
|
|
133
|
+
} catch {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function saveCatalogCache(cacheFile: string, cache: CatalogCache): void {
|
|
139
|
+
writeJsonFile(cacheFile, cache);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function isCatalogCacheFresh(cache: CatalogCache | undefined, now = Date.now()): cache is CatalogCache {
|
|
143
|
+
if (!cache) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const age = now - cache.fetchedAt;
|
|
147
|
+
return age >= 0 && age < CATALOG_CACHE_TTL_MS;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function parsePrice(value: string | undefined | null): number {
|
|
151
|
+
if (!value) return 0;
|
|
152
|
+
const parsed = Number.parseFloat(value);
|
|
153
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function toPiModel(m: CatalogModel): ProviderModelConfig {
|
|
157
|
+
const reasoning = Boolean(m.capabilities?.reasoning);
|
|
158
|
+
const vision = Boolean(m.capabilities?.vision);
|
|
159
|
+
const contextWindow =
|
|
160
|
+
typeof m.context_length === "number" && m.context_length > 0 ? m.context_length : DEFAULT_CONTEXT;
|
|
161
|
+
const rawMaxTokens =
|
|
162
|
+
typeof m.max_output_tokens === "number" && m.max_output_tokens > 0
|
|
163
|
+
? m.max_output_tokens
|
|
164
|
+
: DEFAULT_MAX_TOKENS;
|
|
165
|
+
const maxTokens = Math.min(rawMaxTokens, contextWindow);
|
|
166
|
+
|
|
167
|
+
const compat: Record<string, unknown> = {};
|
|
168
|
+
if (reasoning) {
|
|
169
|
+
compat.supportsReasoningEffort = true;
|
|
170
|
+
compat.thinkingFormat = "openai";
|
|
171
|
+
}
|
|
172
|
+
if (m.id.startsWith("claude")) {
|
|
173
|
+
compat.cacheControlFormat = "anthropic";
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
type: "chat",
|
|
178
|
+
id: m.id,
|
|
179
|
+
name: m.id,
|
|
180
|
+
reasoning,
|
|
181
|
+
input: vision ? ["text", "image"] : ["text"],
|
|
182
|
+
cost: {
|
|
183
|
+
input: parsePrice(m.pricing?.input_per_million),
|
|
184
|
+
output: parsePrice(m.pricing?.output_per_million),
|
|
185
|
+
cacheRead: parsePrice(m.pricing?.cache_read_input_per_million ?? m.pricing?.cache_read_per_million),
|
|
186
|
+
cacheWrite: parsePrice(m.pricing?.cache_write_input_per_million ?? m.pricing?.cache_write_per_million),
|
|
187
|
+
},
|
|
188
|
+
contextWindow,
|
|
189
|
+
maxTokens,
|
|
190
|
+
...(Object.keys(compat).length > 0 ? { compat } : {}),
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function fetchCatalog(apiKey: string, signal?: AbortSignal): Promise<CatalogModel[]> {
|
|
195
|
+
const response = await fetch(`${BASE_URL}/models?type=text`, {
|
|
196
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
197
|
+
signal,
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
if (!response.ok) {
|
|
201
|
+
let message = `HTTP ${response.status}`;
|
|
202
|
+
try {
|
|
203
|
+
const body = (await response.json()) as { error?: { message?: string } };
|
|
204
|
+
if (body?.error?.message) {
|
|
205
|
+
message = body.error.message;
|
|
206
|
+
}
|
|
207
|
+
} catch {
|
|
208
|
+
// Fall back to HTTP status message
|
|
209
|
+
}
|
|
210
|
+
throw new Error(message);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const payload = (await response.json()) as { data?: CatalogModel[] };
|
|
214
|
+
const items = Array.isArray(payload?.data) ? payload.data : [];
|
|
215
|
+
|
|
216
|
+
return items
|
|
217
|
+
.filter(
|
|
218
|
+
(m) =>
|
|
219
|
+
Boolean(m?.id) &&
|
|
220
|
+
(m.endpoint === "/v1/chat/completions" ||
|
|
221
|
+
(Array.isArray(m.supported_endpoints) && m.supported_endpoints.includes("/v1/chat/completions"))),
|
|
222
|
+
)
|
|
223
|
+
.sort((a, b) => a.id.localeCompare(b.id));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function formatContext(tokens?: number | null): string {
|
|
227
|
+
const ctx = typeof tokens === "number" && tokens > 0 ? tokens : DEFAULT_CONTEXT;
|
|
228
|
+
if (ctx >= 1_000_000) {
|
|
229
|
+
const m = ctx / 1_000_000;
|
|
230
|
+
return `${Number.isInteger(m) ? m : m.toFixed(1)}M`;
|
|
231
|
+
}
|
|
232
|
+
return `${Math.round(ctx / 1000)}K`;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function formatPriceDollar(value: string | undefined): string {
|
|
236
|
+
const n = parsePrice(value);
|
|
237
|
+
if (n === 0) return "$0";
|
|
238
|
+
if (n < 0.01) return `$${n.toFixed(4).replace(/0+$/, "").replace(/\.$/, "")}`;
|
|
239
|
+
return `$${n.toFixed(2)}`;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function formatModelMeta(m: CatalogModel): string {
|
|
243
|
+
const ctx = formatContext(m.context_length);
|
|
244
|
+
const price = m.is_free
|
|
245
|
+
? "free"
|
|
246
|
+
: `${formatPriceDollar(m.pricing?.input_per_million)}/${formatPriceDollar(m.pricing?.output_per_million)}`;
|
|
247
|
+
const flags: string[] = [];
|
|
248
|
+
if (m.capabilities?.reasoning) flags.push("reasoning");
|
|
249
|
+
if (m.capabilities?.vision) flags.push("vision");
|
|
250
|
+
const flagStr = flags.length > 0 ? ` · ${flags.join(", ")}` : "";
|
|
251
|
+
return `${ctx} · ${price}${flagStr}`;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function formatKeyHint(th: Theme, key: string, desc: string): string {
|
|
255
|
+
return th.fg("accent", key) + th.fg("dim", ` ${desc}`);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
interface PickerResult {
|
|
259
|
+
selected: Set<string>;
|
|
260
|
+
activateModelId?: string;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
class ModelPickerComponent implements Component {
|
|
264
|
+
private catalog: CatalogModel[];
|
|
265
|
+
private selected: Set<string>;
|
|
266
|
+
private tui: TUI;
|
|
267
|
+
private theme: Theme;
|
|
268
|
+
private onDone: (result: PickerResult | undefined) => void;
|
|
269
|
+
private onRefresh?: (signal: AbortSignal) => Promise<CatalogModel[]>;
|
|
270
|
+
private cursorIndex = 0;
|
|
271
|
+
private scrollTop = 0;
|
|
272
|
+
private filterText = "";
|
|
273
|
+
private isRefreshing = false;
|
|
274
|
+
private statusMessage?: string;
|
|
275
|
+
private refreshController?: AbortController;
|
|
276
|
+
private closed = false;
|
|
277
|
+
private cachedWidth?: number;
|
|
278
|
+
private cachedLines?: string[];
|
|
279
|
+
|
|
280
|
+
constructor(
|
|
281
|
+
catalog: CatalogModel[],
|
|
282
|
+
selectedIds: Set<string>,
|
|
283
|
+
tui: TUI,
|
|
284
|
+
theme: Theme,
|
|
285
|
+
onDone: (result: PickerResult | undefined) => void,
|
|
286
|
+
onRefresh?: (signal: AbortSignal) => Promise<CatalogModel[]>,
|
|
287
|
+
) {
|
|
288
|
+
this.catalog = catalog;
|
|
289
|
+
this.selected = new Set(selectedIds);
|
|
290
|
+
this.tui = tui;
|
|
291
|
+
this.theme = theme;
|
|
292
|
+
this.onDone = onDone;
|
|
293
|
+
this.onRefresh = onRefresh;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private getFiltered(): CatalogModel[] {
|
|
297
|
+
if (!this.filterText) return this.catalog;
|
|
298
|
+
const kw = this.filterText.toLowerCase();
|
|
299
|
+
return this.catalog.filter(
|
|
300
|
+
(m) =>
|
|
301
|
+
m.id.toLowerCase().includes(kw) ||
|
|
302
|
+
(m.owned_by && m.owned_by.toLowerCase().includes(kw)) ||
|
|
303
|
+
(m.provider && m.provider.toLowerCase().includes(kw)),
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private close(result: PickerResult | undefined): void {
|
|
308
|
+
this.closed = true;
|
|
309
|
+
this.refreshController?.abort();
|
|
310
|
+
this.onDone(result);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
private triggerRefresh(): void {
|
|
314
|
+
if (this.isRefreshing || !this.onRefresh) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
this.isRefreshing = true;
|
|
319
|
+
this.statusMessage = undefined;
|
|
320
|
+
this.invalidate();
|
|
321
|
+
this.tui.requestRender();
|
|
322
|
+
|
|
323
|
+
const controller = new AbortController();
|
|
324
|
+
this.refreshController = controller;
|
|
325
|
+
|
|
326
|
+
void this.onRefresh(controller.signal)
|
|
327
|
+
.then((models) => {
|
|
328
|
+
if (this.closed || controller.signal.aborted) return;
|
|
329
|
+
this.catalog = models;
|
|
330
|
+
this.statusMessage = `Refreshed (${models.length} models)`;
|
|
331
|
+
})
|
|
332
|
+
.catch((err: unknown) => {
|
|
333
|
+
if (this.closed || controller.signal.aborted) return;
|
|
334
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
335
|
+
this.statusMessage = `Refresh failed: ${message}`;
|
|
336
|
+
})
|
|
337
|
+
.finally(() => {
|
|
338
|
+
if (this.refreshController === controller) {
|
|
339
|
+
this.refreshController = undefined;
|
|
340
|
+
}
|
|
341
|
+
if (this.closed) return;
|
|
342
|
+
this.isRefreshing = false;
|
|
343
|
+
this.invalidate();
|
|
344
|
+
this.tui.requestRender();
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
dispose(): void {
|
|
349
|
+
this.closed = true;
|
|
350
|
+
this.refreshController?.abort();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
invalidate(): void {
|
|
354
|
+
this.cachedWidth = undefined;
|
|
355
|
+
this.cachedLines = undefined;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
render(width: number): string[] {
|
|
359
|
+
if (this.cachedLines && this.cachedWidth === width) {
|
|
360
|
+
return this.cachedLines;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const th = this.theme;
|
|
364
|
+
const termRows = this.tui.terminal?.rows || process.stdout.rows || 24;
|
|
365
|
+
const filtered = this.getFiltered();
|
|
366
|
+
const lines: string[] = [];
|
|
367
|
+
|
|
368
|
+
lines.push(th.fg("borderMuted", "─".repeat(width)));
|
|
369
|
+
const filterInfo = this.filterText ? ` [Filter: "${this.filterText}"]` : "";
|
|
370
|
+
const statusInfo = this.isRefreshing
|
|
371
|
+
? th.fg("accent", " · Refreshing...")
|
|
372
|
+
: this.statusMessage
|
|
373
|
+
? th.fg(this.statusMessage.startsWith("Refresh failed:") ? "warning" : "dim", ` · ${this.statusMessage}`)
|
|
374
|
+
: "";
|
|
375
|
+
const header =
|
|
376
|
+
th.fg("muted", ` Cheaper Inference Models (`) +
|
|
377
|
+
th.fg("accent", `${this.selected.size}`) +
|
|
378
|
+
th.fg("muted", `/${this.catalog.length} selected)${filterInfo}`) +
|
|
379
|
+
statusInfo;
|
|
380
|
+
lines.push(truncateToWidth(header, width - 2, ""));
|
|
381
|
+
lines.push(th.fg("borderMuted", "─".repeat(width)));
|
|
382
|
+
|
|
383
|
+
const maxVisibleRows = Math.max(6, Math.min(20, termRows - 8));
|
|
384
|
+
|
|
385
|
+
if (filtered.length === 0) {
|
|
386
|
+
const emptyMsg = this.filterText
|
|
387
|
+
? ` (No models matching "${this.filterText}")`
|
|
388
|
+
: " (No compatible models available)";
|
|
389
|
+
lines.push(th.fg("dim", emptyMsg));
|
|
390
|
+
for (let i = 1; i < maxVisibleRows; i++) {
|
|
391
|
+
lines.push("");
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
this.cursorIndex = Math.max(0, Math.min(filtered.length - 1, this.cursorIndex));
|
|
395
|
+
if (this.cursorIndex < this.scrollTop) {
|
|
396
|
+
this.scrollTop = this.cursorIndex;
|
|
397
|
+
} else if (this.cursorIndex >= this.scrollTop + maxVisibleRows) {
|
|
398
|
+
this.scrollTop = this.cursorIndex - maxVisibleRows + 1;
|
|
399
|
+
}
|
|
400
|
+
const maxScrollTop = Math.max(0, filtered.length - maxVisibleRows);
|
|
401
|
+
if (this.scrollTop > maxScrollTop) {
|
|
402
|
+
this.scrollTop = maxScrollTop;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const start = this.scrollTop;
|
|
406
|
+
const end = Math.min(filtered.length, start + maxVisibleRows);
|
|
407
|
+
|
|
408
|
+
for (let i = start; i < end; i++) {
|
|
409
|
+
const model = filtered[i];
|
|
410
|
+
const isCursor = i === this.cursorIndex;
|
|
411
|
+
const isChecked = this.selected.has(model.id);
|
|
412
|
+
|
|
413
|
+
const pointer = isCursor ? th.fg("accent", "▸ ") : " ";
|
|
414
|
+
const marker = isChecked ? th.fg("accent", "[x]") : th.fg("dim", "[ ]");
|
|
415
|
+
const idText = isCursor ? th.bold(th.fg("accent", model.id)) : th.fg("muted", model.id);
|
|
416
|
+
const metaText = th.fg("dim", ` ${formatModelMeta(model)}`);
|
|
417
|
+
|
|
418
|
+
lines.push(truncateToWidth(`${pointer}${marker} ${idText}${metaText}`, width - 2));
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
for (let i = end - start; i < maxVisibleRows; i++) {
|
|
422
|
+
lines.push("");
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
lines.push(th.fg("borderMuted", "─".repeat(width)));
|
|
427
|
+
const scrollPos = filtered.length > 0 ? ` (${this.cursorIndex + 1}/${filtered.length})` : "";
|
|
428
|
+
const hints = [
|
|
429
|
+
formatKeyHint(th, "↑↓", "move"),
|
|
430
|
+
formatKeyHint(th, "space", "toggle"),
|
|
431
|
+
formatKeyHint(th, "enter", "use & close"),
|
|
432
|
+
formatKeyHint(th, "type", "to search"),
|
|
433
|
+
formatKeyHint(th, "ctrl+r", this.isRefreshing ? "refreshing" : "refresh"),
|
|
434
|
+
formatKeyHint(th, "esc", "save & close"),
|
|
435
|
+
].join(th.fg("dim", " · "));
|
|
436
|
+
lines.push(truncateToWidth(` ${hints}${th.fg("dim", scrollPos)}`, width - 2, ""));
|
|
437
|
+
lines.push(th.fg("borderMuted", "─".repeat(width)));
|
|
438
|
+
|
|
439
|
+
this.cachedWidth = width;
|
|
440
|
+
this.cachedLines = lines;
|
|
441
|
+
return lines;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
handleInput(data: string): void {
|
|
445
|
+
const filtered = this.getFiltered();
|
|
446
|
+
|
|
447
|
+
if (matchesKey(data, "escape")) {
|
|
448
|
+
this.close({ selected: new Set(this.selected) });
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (matchesKey(data, "ctrl+c")) {
|
|
453
|
+
this.close(undefined);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (matchesKey(data, "ctrl+r") || matchesKey(data, "f5")) {
|
|
458
|
+
this.triggerRefresh();
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
this.statusMessage = undefined;
|
|
463
|
+
|
|
464
|
+
if (matchesKey(data, "enter") || data === "\r" || data === "\n") {
|
|
465
|
+
const model = filtered[this.cursorIndex];
|
|
466
|
+
if (model) {
|
|
467
|
+
this.selected.add(model.id);
|
|
468
|
+
this.close({ selected: new Set(this.selected), activateModelId: model.id });
|
|
469
|
+
}
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (matchesKey(data, "space") || data === " ") {
|
|
474
|
+
const model = filtered[this.cursorIndex];
|
|
475
|
+
if (model) {
|
|
476
|
+
if (this.selected.has(model.id)) {
|
|
477
|
+
this.selected.delete(model.id);
|
|
478
|
+
} else {
|
|
479
|
+
this.selected.add(model.id);
|
|
480
|
+
}
|
|
481
|
+
this.invalidate();
|
|
482
|
+
this.tui.requestRender();
|
|
483
|
+
}
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
if (matchesKey(data, "up")) {
|
|
488
|
+
if (filtered.length > 0) {
|
|
489
|
+
this.cursorIndex = Math.max(0, this.cursorIndex - 1);
|
|
490
|
+
this.invalidate();
|
|
491
|
+
this.tui.requestRender();
|
|
492
|
+
}
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
if (matchesKey(data, "down")) {
|
|
497
|
+
if (filtered.length > 0) {
|
|
498
|
+
this.cursorIndex = Math.min(filtered.length - 1, this.cursorIndex + 1);
|
|
499
|
+
this.invalidate();
|
|
500
|
+
this.tui.requestRender();
|
|
501
|
+
}
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (matchesKey(data, "pageUp")) {
|
|
506
|
+
if (filtered.length > 0) {
|
|
507
|
+
this.cursorIndex = Math.max(0, this.cursorIndex - 8);
|
|
508
|
+
this.invalidate();
|
|
509
|
+
this.tui.requestRender();
|
|
510
|
+
}
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
if (matchesKey(data, "pageDown")) {
|
|
515
|
+
if (filtered.length > 0) {
|
|
516
|
+
this.cursorIndex = Math.min(filtered.length - 1, this.cursorIndex + 8);
|
|
517
|
+
this.invalidate();
|
|
518
|
+
this.tui.requestRender();
|
|
519
|
+
}
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (matchesKey(data, "home")) {
|
|
524
|
+
this.cursorIndex = 0;
|
|
525
|
+
this.invalidate();
|
|
526
|
+
this.tui.requestRender();
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (matchesKey(data, "end")) {
|
|
531
|
+
this.cursorIndex = Math.max(0, filtered.length - 1);
|
|
532
|
+
this.invalidate();
|
|
533
|
+
this.tui.requestRender();
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (matchesKey(data, "backspace") || data === "\x7f" || data === "\b") {
|
|
538
|
+
if (this.filterText.length > 0) {
|
|
539
|
+
this.filterText = this.filterText.slice(0, -1);
|
|
540
|
+
this.cursorIndex = 0;
|
|
541
|
+
this.scrollTop = 0;
|
|
542
|
+
this.invalidate();
|
|
543
|
+
this.tui.requestRender();
|
|
544
|
+
}
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (data.length === 1 && data > " " && data <= "~") {
|
|
549
|
+
this.filterText += data;
|
|
550
|
+
this.cursorIndex = 0;
|
|
551
|
+
this.scrollTop = 0;
|
|
552
|
+
this.invalidate();
|
|
553
|
+
this.tui.requestRender();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
export default function cheaperInferenceExtension(pi: ExtensionAPI, options?: CheaperInferenceOptions) {
|
|
559
|
+
const configFile = resolveConfigFile(options);
|
|
560
|
+
const cacheFile = resolveCacheFile(configFile);
|
|
561
|
+
let savedConfig = loadConfig(configFile);
|
|
562
|
+
let catalogCache = loadCatalogCache(cacheFile);
|
|
563
|
+
|
|
564
|
+
function registerProvider(models: ProviderModelConfig[]): void {
|
|
565
|
+
pi.registerProvider(PROVIDER_ID, {
|
|
566
|
+
name: PROVIDER_NAME,
|
|
567
|
+
baseUrl: BASE_URL,
|
|
568
|
+
api: "openai-completions",
|
|
569
|
+
apiKey: API_KEY_ENV,
|
|
570
|
+
models,
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
async function fetchAndCacheCatalog(apiKey: string, signal?: AbortSignal): Promise<CatalogModel[]> {
|
|
575
|
+
const models = await fetchCatalog(apiKey, signal);
|
|
576
|
+
catalogCache = { fetchedAt: Date.now(), models };
|
|
577
|
+
saveCatalogCache(cacheFile, catalogCache);
|
|
578
|
+
return models;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
registerProvider(savedConfig.models);
|
|
582
|
+
|
|
583
|
+
pi.registerCommand("cheaper-inference-models", {
|
|
584
|
+
description: "Choose which Cheaper Inference models appear in the model list",
|
|
585
|
+
getArgumentCompletions: (prefix: string) => {
|
|
586
|
+
const normalized = prefix.trim().toLowerCase();
|
|
587
|
+
if ("refresh".startsWith(normalized)) {
|
|
588
|
+
return [{ value: "refresh", label: "refresh", description: "Fetch the latest model catalog from Cheaper Inference" }];
|
|
589
|
+
}
|
|
590
|
+
return null;
|
|
591
|
+
},
|
|
592
|
+
handler: async (args, ctx: ExtensionCommandContext) => {
|
|
593
|
+
const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_ID);
|
|
594
|
+
if (!apiKey) {
|
|
595
|
+
ctx.ui.notify(
|
|
596
|
+
"No Cheaper Inference API key found. Run /login and choose Cheaper Inference, or set CHEAPER_INFERENCE_API_KEY.",
|
|
597
|
+
"warning",
|
|
598
|
+
);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const forceRefresh = args.trim().toLowerCase() === "refresh";
|
|
603
|
+
if (!isCatalogCacheFresh(catalogCache)) {
|
|
604
|
+
catalogCache = loadCatalogCache(cacheFile);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
let catalog: CatalogModel[];
|
|
608
|
+
if (!forceRefresh && isCatalogCacheFresh(catalogCache)) {
|
|
609
|
+
catalog = catalogCache.models;
|
|
610
|
+
} else {
|
|
611
|
+
try {
|
|
612
|
+
catalog = await fetchAndCacheCatalog(apiKey);
|
|
613
|
+
} catch (err) {
|
|
614
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
615
|
+
if (!forceRefresh && catalogCache) {
|
|
616
|
+
catalog = catalogCache.models;
|
|
617
|
+
ctx.ui.notify(
|
|
618
|
+
`Failed to refresh Cheaper Inference models (${message}); using cached list.`,
|
|
619
|
+
"warning",
|
|
620
|
+
);
|
|
621
|
+
} else {
|
|
622
|
+
ctx.ui.notify(`Failed to load Cheaper Inference models: ${message}`, "error");
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
const selectedIds = new Set(savedConfig.models.map((m) => m.id));
|
|
629
|
+
|
|
630
|
+
if (ctx.mode !== "tui" || !ctx.hasUI) {
|
|
631
|
+
const selectedList =
|
|
632
|
+
selectedIds.size > 0
|
|
633
|
+
? Array.from(selectedIds).sort().join(", ")
|
|
634
|
+
: "(none)";
|
|
635
|
+
ctx.ui.notify(
|
|
636
|
+
`Cheaper Inference selected models (${selectedIds.size}): ${selectedList}\nAvailable in catalog: ${catalog.length}`,
|
|
637
|
+
"info",
|
|
638
|
+
);
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const pickerResult = await ctx.ui.custom<PickerResult | undefined>((tui, theme, _kb, done) => {
|
|
643
|
+
return new ModelPickerComponent(catalog, selectedIds, tui, theme, done, async (signal) => {
|
|
644
|
+
const refreshed = await fetchAndCacheCatalog(apiKey, signal);
|
|
645
|
+
catalog = refreshed;
|
|
646
|
+
return refreshed;
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
|
|
650
|
+
if (!pickerResult) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const catalogById = new Map(catalog.map((m) => [m.id, m]));
|
|
655
|
+
const previousById = new Map(savedConfig.models.map((m) => [m.id, m]));
|
|
656
|
+
const nextModels: ProviderModelConfig[] = [];
|
|
657
|
+
|
|
658
|
+
for (const id of Array.from(pickerResult.selected).sort()) {
|
|
659
|
+
const catalogModel = catalogById.get(id);
|
|
660
|
+
if (catalogModel) {
|
|
661
|
+
nextModels.push(toPiModel(catalogModel));
|
|
662
|
+
} else {
|
|
663
|
+
const existing = previousById.get(id);
|
|
664
|
+
if (existing) {
|
|
665
|
+
nextModels.push(existing);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
savedConfig = { models: nextModels };
|
|
671
|
+
saveConfig(configFile, savedConfig);
|
|
672
|
+
registerProvider(nextModels);
|
|
673
|
+
|
|
674
|
+
if (pickerResult.activateModelId) {
|
|
675
|
+
const model = ctx.modelRegistry.find(PROVIDER_ID, pickerResult.activateModelId);
|
|
676
|
+
if (model) {
|
|
677
|
+
const success = await pi.setModel(model);
|
|
678
|
+
if (success) {
|
|
679
|
+
ctx.ui.notify(
|
|
680
|
+
`Active model set to ${pickerResult.activateModelId} (${nextModels.length} Cheaper Inference model(s) in /model).`,
|
|
681
|
+
"info",
|
|
682
|
+
);
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
ctx.ui.notify(
|
|
686
|
+
`Saved ${nextModels.length} Cheaper Inference model(s), but could not activate ${pickerResult.activateModelId}.`,
|
|
687
|
+
"warning",
|
|
688
|
+
);
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
ctx.ui.notify(
|
|
692
|
+
`Saved ${nextModels.length} Cheaper Inference model(s), but ${pickerResult.activateModelId} was not found in model registry.`,
|
|
693
|
+
"warning",
|
|
694
|
+
);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
ctx.ui.notify(`${nextModels.length} Cheaper Inference model(s) available in /model.`, "info");
|
|
699
|
+
},
|
|
700
|
+
});
|
|
701
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsnbyte/pi-cheaper-inference",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Pi extension that adds the Cheaper Inference provider with API key login and a curated model picker",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "dsnbyte",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/dsnbyte/pi-cheaper-inference.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/dsnbyte/pi-cheaper-inference/issues"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/dsnbyte/pi-cheaper-inference#readme",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"pi-package",
|
|
21
|
+
"pi-extension",
|
|
22
|
+
"provider",
|
|
23
|
+
"cheaper-inference",
|
|
24
|
+
"llm",
|
|
25
|
+
"models"
|
|
26
|
+
],
|
|
27
|
+
"files": [
|
|
28
|
+
"extensions",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE",
|
|
31
|
+
"CHANGELOG.md"
|
|
32
|
+
],
|
|
33
|
+
"pi": {
|
|
34
|
+
"extensions": [
|
|
35
|
+
"./extensions/index.ts"
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@earendil-works/pi-agent-core": "*",
|
|
40
|
+
"@earendil-works/pi-ai": "*",
|
|
41
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
42
|
+
"@earendil-works/pi-tui": "*"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"@earendil-works/pi-agent-core": {
|
|
46
|
+
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"@earendil-works/pi-ai": {
|
|
49
|
+
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"@earendil-works/pi-coding-agent": {
|
|
52
|
+
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"@earendil-works/pi-tui": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@earendil-works/pi-coding-agent": "^1.5.0",
|
|
60
|
+
"@earendil-works/pi-tui": "^1.5.0",
|
|
61
|
+
"@types/node": "^26.6.6",
|
|
62
|
+
"typescript": "^7.0.2"
|
|
63
|
+
}
|
|
64
|
+
}
|