@robhowley/pi-openrouter 0.8.0 → 0.8.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.
@@ -27,11 +27,15 @@ import {
27
27
  groupSkipReasons,
28
28
  } from './models/sync.js';
29
29
  import { loadCache, getCacheAgeMs, formatDuration } from './models/cache.js';
30
+ import { mapOpenRouterModels } from './models/mapper.js';
30
31
 
31
32
  // Store the current session state for use in command handlers
32
33
  let currentSessionState: OpenRouterSessionState | null = null;
33
34
  let sessionTrackingInstalled = false;
34
35
 
36
+ // Store startup cache state for notifications
37
+ let startupCacheInfo: { count: number; age: string } | undefined;
38
+
35
39
  // =============================================================================
36
40
  // Utility Functions
37
41
  // =============================================================================
@@ -87,7 +91,36 @@ function getCurrentSessionId(ctx: { sessionManager: { getSessionId(): string } }
87
91
  }
88
92
  }
89
93
 
90
- export default function (pi: ExtensionAPI) {
94
+ export default async function (pi: ExtensionAPI) {
95
+ // Eager cache load on extension startup (before any sessions)
96
+ startupCacheInfo = undefined;
97
+ let startupCacheWarning: string | undefined;
98
+ if (isSyncEnabled()) {
99
+ const cache = await loadCache().catch(() => null);
100
+
101
+ if (cache?.models.length) {
102
+ try {
103
+ const { configs } = mapOpenRouterModels(cache.models);
104
+
105
+ // Register models directly with Pi's OpenRouter provider
106
+ pi.registerProvider('openrouter', {
107
+ baseUrl: 'https://openrouter.ai/api/v1',
108
+ apiKey: 'OPENROUTER_API_KEY',
109
+ api: 'openai-completions',
110
+ models: configs,
111
+ authHeader: true,
112
+ });
113
+
114
+ // Store for session_start notification
115
+ const age = formatDuration(getCacheAgeMs(cache));
116
+ startupCacheInfo = { count: configs.length, age };
117
+ } catch (error) {
118
+ startupCacheInfo = undefined;
119
+ startupCacheWarning = `OpenRouter: cached models found but failed to register: ${error instanceof Error ? error.message : String(error)}`;
120
+ }
121
+ }
122
+ }
123
+
91
124
  // Install before_provider_request hook once
92
125
  if (!sessionTrackingInstalled) {
93
126
  sessionTrackingInstalled = true;
@@ -198,6 +231,27 @@ export default function (pi: ExtensionAPI) {
198
231
  stopBackgroundRefresh();
199
232
  });
200
233
 
234
+ // Notify on first session start after extension load
235
+ pi.on('session_start', (event, ctx) => {
236
+ if (!ctx.hasUI) return;
237
+
238
+ // Show a persistent status indicator
239
+ if (startupCacheInfo) {
240
+ const statusText = `OpenRouter ${startupCacheInfo.count} models`;
241
+ ctx.ui.setStatus('openrouter', ctx.ui.theme.fg('dim', statusText));
242
+ }
243
+
244
+ // Show a one-time notification on startup
245
+ if (event.reason === 'startup' && startupCacheInfo) {
246
+ const notice = `OpenRouter: ${startupCacheInfo.count} models loaded from cache (${startupCacheInfo.age} old). Run /openrouter models-sync to refresh.`;
247
+ ctx.ui.notify(notice, 'info');
248
+ }
249
+
250
+ if (event.reason === 'startup' && startupCacheWarning) {
251
+ ctx.ui.notify(startupCacheWarning, 'warning');
252
+ }
253
+ });
254
+
201
255
  pi.registerCommand('openrouter-usage', {
202
256
  description: 'Show OpenRouter usage: caps, spend, burn rate, and model breakdowns',
203
257
  getArgumentCompletions: () => null,
@@ -66,7 +66,7 @@ export function getSyncState(): SyncResult | null {
66
66
  * Uses modelRegistry.registerProvider() to add models to the built-in openrouter provider.
67
67
  * The models array replaces all existing models for the provider.
68
68
  */
69
- async function registerModelsWithProvider(
69
+ export async function registerModelsWithProvider(
70
70
  ctx: ExtensionContext,
71
71
  configs: PiModelConfig[],
72
72
  ): Promise<void> {
@@ -79,8 +79,6 @@ async function registerModelsWithProvider(
79
79
  models: configs,
80
80
  authHeader: true,
81
81
  });
82
-
83
- console.log(`[pi-openrouter] Registered ${configs.length} models with OpenRouter provider`);
84
82
  }
85
83
 
86
84
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robhowley/pi-openrouter",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "type": "module",
5
5
  "description": "Live OpenRouter spend/account TUI overlays, user-scoped model sync, and session tagging for Pi.",
6
6
  "license": "MIT",