@dsnbyte/pi-cheaper-inference 0.0.0-stage → 0.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/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,79 @@
1
- # Temporary Holding Version
1
+ # pi-cheaper-inference
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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-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-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-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 / Enter** to toggle whether a model is included.
52
+ - **Ctrl+R** (or **F5**) to manually refresh the model list from the provider.
53
+ - **Esc** to save your selection and update `/model`.
54
+
55
+ You can also run `/cheaper-models refresh` to force an immediate catalog refresh when opening the menu.
56
+
57
+ After selecting models, switch to any of them with `/model`.
58
+
59
+ ## Configuration
60
+
61
+ Selected model snapshots are stored at `~/.pi/agent/cheaper-inference.json`:
62
+
63
+ ```json
64
+ {
65
+ "models": [
66
+ {
67
+ "type": "chat",
68
+ "id": "gemini-4-neon",
69
+ "name": "gemini-4-neon"
70
+ }
71
+ ]
72
+ }
73
+ ```
74
+
75
+ For full provider documentation, see [cheaperinference.com/docs](https://www.cheaperinference.com/docs).
76
+
77
+ ## License
78
+
79
+ MIT
@@ -0,0 +1,667 @@
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
+ class ModelPickerComponent implements Component {
259
+ private catalog: CatalogModel[];
260
+ private selected: Set<string>;
261
+ private tui: TUI;
262
+ private theme: Theme;
263
+ private onDone: (result: Set<string> | undefined) => void;
264
+ private onRefresh?: (signal: AbortSignal) => Promise<CatalogModel[]>;
265
+ private cursorIndex = 0;
266
+ private scrollTop = 0;
267
+ private filterText = "";
268
+ private isRefreshing = false;
269
+ private statusMessage?: string;
270
+ private refreshController?: AbortController;
271
+ private closed = false;
272
+ private cachedWidth?: number;
273
+ private cachedLines?: string[];
274
+
275
+ constructor(
276
+ catalog: CatalogModel[],
277
+ selectedIds: Set<string>,
278
+ tui: TUI,
279
+ theme: Theme,
280
+ onDone: (result: Set<string> | undefined) => void,
281
+ onRefresh?: (signal: AbortSignal) => Promise<CatalogModel[]>,
282
+ ) {
283
+ this.catalog = catalog;
284
+ this.selected = new Set(selectedIds);
285
+ this.tui = tui;
286
+ this.theme = theme;
287
+ this.onDone = onDone;
288
+ this.onRefresh = onRefresh;
289
+ }
290
+
291
+ private getFiltered(): CatalogModel[] {
292
+ if (!this.filterText) return this.catalog;
293
+ const kw = this.filterText.toLowerCase();
294
+ return this.catalog.filter(
295
+ (m) =>
296
+ m.id.toLowerCase().includes(kw) ||
297
+ (m.owned_by && m.owned_by.toLowerCase().includes(kw)) ||
298
+ (m.provider && m.provider.toLowerCase().includes(kw)),
299
+ );
300
+ }
301
+
302
+ private close(result: Set<string> | undefined): void {
303
+ this.closed = true;
304
+ this.refreshController?.abort();
305
+ this.onDone(result);
306
+ }
307
+
308
+ private triggerRefresh(): void {
309
+ if (this.isRefreshing || !this.onRefresh) {
310
+ return;
311
+ }
312
+
313
+ this.isRefreshing = true;
314
+ this.statusMessage = undefined;
315
+ this.invalidate();
316
+ this.tui.requestRender();
317
+
318
+ const controller = new AbortController();
319
+ this.refreshController = controller;
320
+
321
+ void this.onRefresh(controller.signal)
322
+ .then((models) => {
323
+ if (this.closed || controller.signal.aborted) return;
324
+ this.catalog = models;
325
+ this.statusMessage = `Refreshed (${models.length} models)`;
326
+ })
327
+ .catch((err: unknown) => {
328
+ if (this.closed || controller.signal.aborted) return;
329
+ const message = err instanceof Error ? err.message : String(err);
330
+ this.statusMessage = `Refresh failed: ${message}`;
331
+ })
332
+ .finally(() => {
333
+ if (this.refreshController === controller) {
334
+ this.refreshController = undefined;
335
+ }
336
+ if (this.closed) return;
337
+ this.isRefreshing = false;
338
+ this.invalidate();
339
+ this.tui.requestRender();
340
+ });
341
+ }
342
+
343
+ dispose(): void {
344
+ this.closed = true;
345
+ this.refreshController?.abort();
346
+ }
347
+
348
+ invalidate(): void {
349
+ this.cachedWidth = undefined;
350
+ this.cachedLines = undefined;
351
+ }
352
+
353
+ render(width: number): string[] {
354
+ if (this.cachedLines && this.cachedWidth === width) {
355
+ return this.cachedLines;
356
+ }
357
+
358
+ const th = this.theme;
359
+ const termRows = this.tui.terminal?.rows || process.stdout.rows || 24;
360
+ const filtered = this.getFiltered();
361
+ const lines: string[] = [];
362
+
363
+ lines.push(th.fg("borderMuted", "─".repeat(width)));
364
+ const filterInfo = this.filterText ? ` [Filter: "${this.filterText}"]` : "";
365
+ const statusInfo = this.isRefreshing
366
+ ? th.fg("accent", " · Refreshing...")
367
+ : this.statusMessage
368
+ ? th.fg(this.statusMessage.startsWith("Refresh failed:") ? "warning" : "dim", ` · ${this.statusMessage}`)
369
+ : "";
370
+ const header =
371
+ th.fg("muted", ` Cheaper Inference Models (`) +
372
+ th.fg("accent", `${this.selected.size}`) +
373
+ th.fg("muted", `/${this.catalog.length} selected)${filterInfo}`) +
374
+ statusInfo;
375
+ lines.push(truncateToWidth(header, width - 2, ""));
376
+ lines.push(th.fg("borderMuted", "─".repeat(width)));
377
+
378
+ const maxVisibleRows = Math.max(6, Math.min(20, termRows - 8));
379
+
380
+ if (filtered.length === 0) {
381
+ const emptyMsg = this.filterText
382
+ ? ` (No models matching "${this.filterText}")`
383
+ : " (No compatible models available)";
384
+ lines.push(th.fg("dim", emptyMsg));
385
+ for (let i = 1; i < maxVisibleRows; i++) {
386
+ lines.push("");
387
+ }
388
+ } else {
389
+ this.cursorIndex = Math.max(0, Math.min(filtered.length - 1, this.cursorIndex));
390
+ if (this.cursorIndex < this.scrollTop) {
391
+ this.scrollTop = this.cursorIndex;
392
+ } else if (this.cursorIndex >= this.scrollTop + maxVisibleRows) {
393
+ this.scrollTop = this.cursorIndex - maxVisibleRows + 1;
394
+ }
395
+ const maxScrollTop = Math.max(0, filtered.length - maxVisibleRows);
396
+ if (this.scrollTop > maxScrollTop) {
397
+ this.scrollTop = maxScrollTop;
398
+ }
399
+
400
+ const start = this.scrollTop;
401
+ const end = Math.min(filtered.length, start + maxVisibleRows);
402
+
403
+ for (let i = start; i < end; i++) {
404
+ const model = filtered[i];
405
+ const isCursor = i === this.cursorIndex;
406
+ const isChecked = this.selected.has(model.id);
407
+
408
+ const pointer = isCursor ? th.fg("accent", "▸ ") : " ";
409
+ const marker = isChecked ? th.fg("accent", "[x]") : th.fg("dim", "[ ]");
410
+ const idText = isCursor ? th.bold(th.fg("accent", model.id)) : th.fg("muted", model.id);
411
+ const metaText = th.fg("dim", ` ${formatModelMeta(model)}`);
412
+
413
+ lines.push(truncateToWidth(`${pointer}${marker} ${idText}${metaText}`, width - 2));
414
+ }
415
+
416
+ for (let i = end - start; i < maxVisibleRows; i++) {
417
+ lines.push("");
418
+ }
419
+ }
420
+
421
+ lines.push(th.fg("borderMuted", "─".repeat(width)));
422
+ const scrollPos = filtered.length > 0 ? ` (${this.cursorIndex + 1}/${filtered.length})` : "";
423
+ const hints = [
424
+ formatKeyHint(th, "↑↓", "move"),
425
+ formatKeyHint(th, "space", "toggle"),
426
+ formatKeyHint(th, "type", "to search"),
427
+ formatKeyHint(th, "ctrl+r", this.isRefreshing ? "refreshing" : "refresh"),
428
+ formatKeyHint(th, "esc", "save & close"),
429
+ ].join(th.fg("dim", " · "));
430
+ lines.push(truncateToWidth(` ${hints}${th.fg("dim", scrollPos)}`, width - 2, ""));
431
+ lines.push(th.fg("borderMuted", "─".repeat(width)));
432
+
433
+ this.cachedWidth = width;
434
+ this.cachedLines = lines;
435
+ return lines;
436
+ }
437
+
438
+ handleInput(data: string): void {
439
+ const filtered = this.getFiltered();
440
+
441
+ if (matchesKey(data, "escape")) {
442
+ this.close(new Set(this.selected));
443
+ return;
444
+ }
445
+
446
+ if (matchesKey(data, "ctrl+c")) {
447
+ this.close(undefined);
448
+ return;
449
+ }
450
+
451
+ if (matchesKey(data, "ctrl+r") || matchesKey(data, "f5")) {
452
+ this.triggerRefresh();
453
+ return;
454
+ }
455
+
456
+ this.statusMessage = undefined;
457
+
458
+ if (
459
+ matchesKey(data, "space") ||
460
+ data === " " ||
461
+ matchesKey(data, "enter") ||
462
+ data === "\r" ||
463
+ data === "\n"
464
+ ) {
465
+ const model = filtered[this.cursorIndex];
466
+ if (model) {
467
+ if (this.selected.has(model.id)) {
468
+ this.selected.delete(model.id);
469
+ } else {
470
+ this.selected.add(model.id);
471
+ }
472
+ this.invalidate();
473
+ this.tui.requestRender();
474
+ }
475
+ return;
476
+ }
477
+
478
+ if (matchesKey(data, "up")) {
479
+ if (filtered.length > 0) {
480
+ this.cursorIndex = Math.max(0, this.cursorIndex - 1);
481
+ this.invalidate();
482
+ this.tui.requestRender();
483
+ }
484
+ return;
485
+ }
486
+
487
+ if (matchesKey(data, "down")) {
488
+ if (filtered.length > 0) {
489
+ this.cursorIndex = Math.min(filtered.length - 1, this.cursorIndex + 1);
490
+ this.invalidate();
491
+ this.tui.requestRender();
492
+ }
493
+ return;
494
+ }
495
+
496
+ if (matchesKey(data, "pageUp")) {
497
+ if (filtered.length > 0) {
498
+ this.cursorIndex = Math.max(0, this.cursorIndex - 8);
499
+ this.invalidate();
500
+ this.tui.requestRender();
501
+ }
502
+ return;
503
+ }
504
+
505
+ if (matchesKey(data, "pageDown")) {
506
+ if (filtered.length > 0) {
507
+ this.cursorIndex = Math.min(filtered.length - 1, this.cursorIndex + 8);
508
+ this.invalidate();
509
+ this.tui.requestRender();
510
+ }
511
+ return;
512
+ }
513
+
514
+ if (matchesKey(data, "home")) {
515
+ this.cursorIndex = 0;
516
+ this.invalidate();
517
+ this.tui.requestRender();
518
+ return;
519
+ }
520
+
521
+ if (matchesKey(data, "end")) {
522
+ this.cursorIndex = Math.max(0, filtered.length - 1);
523
+ this.invalidate();
524
+ this.tui.requestRender();
525
+ return;
526
+ }
527
+
528
+ if (matchesKey(data, "backspace") || data === "\x7f" || data === "\b") {
529
+ if (this.filterText.length > 0) {
530
+ this.filterText = this.filterText.slice(0, -1);
531
+ this.cursorIndex = 0;
532
+ this.scrollTop = 0;
533
+ this.invalidate();
534
+ this.tui.requestRender();
535
+ }
536
+ return;
537
+ }
538
+
539
+ if (data.length === 1 && data > " " && data <= "~") {
540
+ this.filterText += data;
541
+ this.cursorIndex = 0;
542
+ this.scrollTop = 0;
543
+ this.invalidate();
544
+ this.tui.requestRender();
545
+ }
546
+ }
547
+ }
548
+
549
+ export default function cheaperInferenceExtension(pi: ExtensionAPI, options?: CheaperInferenceOptions) {
550
+ const configFile = resolveConfigFile(options);
551
+ const cacheFile = resolveCacheFile(configFile);
552
+ let savedConfig = loadConfig(configFile);
553
+ let catalogCache = loadCatalogCache(cacheFile);
554
+
555
+ function registerProvider(models: ProviderModelConfig[]): void {
556
+ pi.registerProvider(PROVIDER_ID, {
557
+ name: PROVIDER_NAME,
558
+ baseUrl: BASE_URL,
559
+ api: "openai-completions",
560
+ apiKey: API_KEY_ENV,
561
+ models,
562
+ });
563
+ }
564
+
565
+ async function fetchAndCacheCatalog(apiKey: string, signal?: AbortSignal): Promise<CatalogModel[]> {
566
+ const models = await fetchCatalog(apiKey, signal);
567
+ catalogCache = { fetchedAt: Date.now(), models };
568
+ saveCatalogCache(cacheFile, catalogCache);
569
+ return models;
570
+ }
571
+
572
+ registerProvider(savedConfig.models);
573
+
574
+ pi.registerCommand("cheaper-models", {
575
+ description: "Choose which Cheaper Inference models appear in the model list",
576
+ getArgumentCompletions: (prefix: string) => {
577
+ const normalized = prefix.trim().toLowerCase();
578
+ if ("refresh".startsWith(normalized)) {
579
+ return [{ value: "refresh", label: "refresh", description: "Fetch the latest model catalog from Cheaper Inference" }];
580
+ }
581
+ return null;
582
+ },
583
+ handler: async (args, ctx: ExtensionCommandContext) => {
584
+ const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_ID);
585
+ if (!apiKey) {
586
+ ctx.ui.notify(
587
+ "No Cheaper Inference API key found. Run /login and choose Cheaper Inference, or set CHEAPER_INFERENCE_API_KEY.",
588
+ "warning",
589
+ );
590
+ return;
591
+ }
592
+
593
+ const forceRefresh = args.trim().toLowerCase() === "refresh";
594
+ if (!isCatalogCacheFresh(catalogCache)) {
595
+ catalogCache = loadCatalogCache(cacheFile);
596
+ }
597
+
598
+ let catalog: CatalogModel[];
599
+ if (!forceRefresh && isCatalogCacheFresh(catalogCache)) {
600
+ catalog = catalogCache.models;
601
+ } else {
602
+ try {
603
+ catalog = await fetchAndCacheCatalog(apiKey);
604
+ } catch (err) {
605
+ const message = err instanceof Error ? err.message : String(err);
606
+ if (!forceRefresh && catalogCache) {
607
+ catalog = catalogCache.models;
608
+ ctx.ui.notify(
609
+ `Failed to refresh Cheaper Inference models (${message}); using cached list.`,
610
+ "warning",
611
+ );
612
+ } else {
613
+ ctx.ui.notify(`Failed to load Cheaper Inference models: ${message}`, "error");
614
+ return;
615
+ }
616
+ }
617
+ }
618
+
619
+ const selectedIds = new Set(savedConfig.models.map((m) => m.id));
620
+
621
+ if (ctx.mode !== "tui" || !ctx.hasUI) {
622
+ const selectedList =
623
+ selectedIds.size > 0
624
+ ? Array.from(selectedIds).sort().join(", ")
625
+ : "(none)";
626
+ ctx.ui.notify(
627
+ `Cheaper Inference selected models (${selectedIds.size}): ${selectedList}\nAvailable in catalog: ${catalog.length}`,
628
+ "info",
629
+ );
630
+ return;
631
+ }
632
+
633
+ const nextSelected = await ctx.ui.custom<Set<string> | undefined>((tui, theme, _kb, done) => {
634
+ return new ModelPickerComponent(catalog, selectedIds, tui, theme, done, async (signal) => {
635
+ const refreshed = await fetchAndCacheCatalog(apiKey, signal);
636
+ catalog = refreshed;
637
+ return refreshed;
638
+ });
639
+ });
640
+
641
+ if (!nextSelected) {
642
+ return;
643
+ }
644
+
645
+ const catalogById = new Map(catalog.map((m) => [m.id, m]));
646
+ const previousById = new Map(savedConfig.models.map((m) => [m.id, m]));
647
+ const nextModels: ProviderModelConfig[] = [];
648
+
649
+ for (const id of Array.from(nextSelected).sort()) {
650
+ const catalogModel = catalogById.get(id);
651
+ if (catalogModel) {
652
+ nextModels.push(toPiModel(catalogModel));
653
+ } else {
654
+ const existing = previousById.get(id);
655
+ if (existing) {
656
+ nextModels.push(existing);
657
+ }
658
+ }
659
+ }
660
+
661
+ savedConfig = { models: nextModels };
662
+ saveConfig(configFile, savedConfig);
663
+ registerProvider(nextModels);
664
+ ctx.ui.notify(`${nextModels.length} Cheaper Inference model(s) available in /model.`, "info");
665
+ },
666
+ });
667
+ }
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "@dsnbyte/pi-cheaper-inference",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
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
+ }