@robhowley/pi-openrouter 0.8.3 → 0.9.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/README.md +87 -4
- package/extensions/openrouter/__tests__/cache.test.ts +769 -0
- package/extensions/openrouter/__tests__/client.test.ts +333 -15
- package/extensions/openrouter/__tests__/commands.test.ts +816 -0
- package/extensions/openrouter/__tests__/fixtures.ts +140 -1
- package/extensions/openrouter/__tests__/format.test.ts +19 -0
- package/extensions/openrouter/__tests__/hooks.test.ts +276 -0
- package/extensions/openrouter/__tests__/index.test.ts +163 -0
- package/extensions/openrouter/__tests__/local-usage.test.ts +777 -0
- package/extensions/openrouter/__tests__/normalizers.test.ts +288 -0
- package/extensions/openrouter/__tests__/overlay.test.ts +225 -0
- package/extensions/openrouter/__tests__/session-state.test.ts +233 -0
- package/extensions/openrouter/__tests__/session.test.ts +44 -43
- package/extensions/openrouter/account-client.ts +11 -61
- package/extensions/openrouter/cache.ts +203 -91
- package/extensions/openrouter/client.ts +49 -3
- package/extensions/openrouter/commands.ts +555 -0
- package/extensions/openrouter/format.ts +7 -4
- package/extensions/openrouter/hooks.ts +229 -0
- package/extensions/openrouter/index.ts +13 -589
- package/extensions/openrouter/local-usage.ts +145 -22
- package/extensions/openrouter/models/__tests__/cache.test.ts +63 -2
- package/extensions/openrouter/models/__tests__/mapper-overrides.test.ts +102 -0
- package/extensions/openrouter/models/__tests__/mapper.test.ts +29 -0
- package/extensions/openrouter/models/__tests__/override-commands.test.ts +668 -0
- package/extensions/openrouter/models/__tests__/overrides.test.ts +237 -0
- package/extensions/openrouter/models/__tests__/sync.test.ts +156 -4
- package/extensions/openrouter/models/cache.ts +27 -2
- package/extensions/openrouter/models/mapper.ts +60 -77
- package/extensions/openrouter/models/override-commands.ts +434 -0
- package/extensions/openrouter/models/overrides.ts +174 -0
- package/extensions/openrouter/models/skip-hints.ts +19 -0
- package/extensions/openrouter/models/sync.ts +22 -10
- package/extensions/openrouter/models/types.ts +31 -1
- package/extensions/openrouter/normalizers.ts +128 -0
- package/extensions/openrouter/overlay.ts +19 -8
- package/extensions/openrouter/session-state.ts +110 -0
- package/extensions/openrouter/session.ts +16 -0
- package/extensions/openrouter/types.ts +28 -9
- package/package.json +1 -1
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
+
import { createSessionState } from '../session-state.js';
|
|
3
|
+
import type { SessionState } from '../session-state.js';
|
|
4
|
+
import { createSessionCtx, THROW_SESSION_ID } from './fixtures.js';
|
|
5
|
+
|
|
6
|
+
describe('SessionState', () => {
|
|
7
|
+
describe('createSessionState', () => {
|
|
8
|
+
it('should create a new session state instance', () => {
|
|
9
|
+
const state = createSessionState();
|
|
10
|
+
expect(state).toBeDefined();
|
|
11
|
+
expect(typeof state.getCurrentSessionId).toBe('function');
|
|
12
|
+
expect(typeof state.reset).toBe('function');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('should produce different instances with different initial IDs', () => {
|
|
16
|
+
const state1 = createSessionState();
|
|
17
|
+
const state2 = createSessionState();
|
|
18
|
+
|
|
19
|
+
const mockCtx = createSessionCtx('test-session-1');
|
|
20
|
+
const mockCtx2 = createSessionCtx('test-session-2');
|
|
21
|
+
|
|
22
|
+
const id1 = state1.getCurrentSessionId(mockCtx);
|
|
23
|
+
const id2 = state2.getCurrentSessionId(mockCtx2);
|
|
24
|
+
|
|
25
|
+
expect(id1).not.toBe(id2);
|
|
26
|
+
expect(id1).toBe('pi:test-session-1');
|
|
27
|
+
expect(id2).toBe('pi:test-session-2');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('getCurrentSessionId', () => {
|
|
32
|
+
let state: SessionState;
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
state = createSessionState();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('should return the same ID on multiple calls within same session', () => {
|
|
39
|
+
const mockCtx = createSessionCtx('stable-session');
|
|
40
|
+
|
|
41
|
+
const id1 = state.getCurrentSessionId(mockCtx);
|
|
42
|
+
const id2 = state.getCurrentSessionId(mockCtx);
|
|
43
|
+
const id3 = state.getCurrentSessionId(mockCtx);
|
|
44
|
+
|
|
45
|
+
expect(id1).toBe(id2);
|
|
46
|
+
expect(id2).toBe(id3);
|
|
47
|
+
expect(id1).toBe('pi:stable-session');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should cache the session ID even if context changes', () => {
|
|
51
|
+
const mockCtx1 = createSessionCtx('session-1');
|
|
52
|
+
const mockCtx2 = createSessionCtx('session-2');
|
|
53
|
+
|
|
54
|
+
const id1 = state.getCurrentSessionId(mockCtx1);
|
|
55
|
+
// Second call with different context should return cached ID
|
|
56
|
+
const id2 = state.getCurrentSessionId(mockCtx2);
|
|
57
|
+
|
|
58
|
+
expect(id1).toBe(id2);
|
|
59
|
+
expect(id1).toBe('pi:session-1');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it.each([
|
|
63
|
+
['empty string', ''],
|
|
64
|
+
['throwing manager', THROW_SESSION_ID],
|
|
65
|
+
])(
|
|
66
|
+
'should generate stable fallback UUID when sessionManager returns %s',
|
|
67
|
+
(_label, sessionIdOrMarker) => {
|
|
68
|
+
const mockCtx = createSessionCtx(sessionIdOrMarker);
|
|
69
|
+
|
|
70
|
+
const id1 = state.getCurrentSessionId(mockCtx);
|
|
71
|
+
const id2 = state.getCurrentSessionId(mockCtx);
|
|
72
|
+
|
|
73
|
+
expect(id1).toBe(id2);
|
|
74
|
+
expect(id1).toMatch(
|
|
75
|
+
/^pi:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
76
|
+
);
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
it('should format session ID with pi: prefix', () => {
|
|
81
|
+
const mockCtx = createSessionCtx('my-session');
|
|
82
|
+
|
|
83
|
+
const id = state.getCurrentSessionId(mockCtx);
|
|
84
|
+
expect(id).toBe('pi:my-session');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should not double-prefix if session ID already starts with pi:', () => {
|
|
88
|
+
const mockCtx = createSessionCtx('pi:already-prefixed');
|
|
89
|
+
|
|
90
|
+
const id = state.getCurrentSessionId(mockCtx);
|
|
91
|
+
expect(id).toBe('pi:already-prefixed');
|
|
92
|
+
expect(id).not.toMatch(/^pi:pi:/);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe('reset', () => {
|
|
97
|
+
let state: SessionState;
|
|
98
|
+
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
state = createSessionState();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should clear cached session ID', () => {
|
|
104
|
+
const mockCtx1 = createSessionCtx('session-1');
|
|
105
|
+
const mockCtx2 = createSessionCtx('session-2');
|
|
106
|
+
|
|
107
|
+
const id1 = state.getCurrentSessionId(mockCtx1);
|
|
108
|
+
expect(id1).toBe('pi:session-1');
|
|
109
|
+
|
|
110
|
+
state.reset();
|
|
111
|
+
|
|
112
|
+
const id2 = state.getCurrentSessionId(mockCtx2);
|
|
113
|
+
expect(id2).toBe('pi:session-2');
|
|
114
|
+
expect(id2).not.toBe(id1);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should allow new fallback UUID after reset', () => {
|
|
118
|
+
const mockCtx = createSessionCtx('');
|
|
119
|
+
|
|
120
|
+
const id1 = state.getCurrentSessionId(mockCtx);
|
|
121
|
+
state.reset();
|
|
122
|
+
const id2 = state.getCurrentSessionId(mockCtx);
|
|
123
|
+
|
|
124
|
+
expect(id1).not.toBe(id2);
|
|
125
|
+
expect(id1).toMatch(/^pi:/);
|
|
126
|
+
expect(id2).toMatch(/^pi:/);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('peek', () => {
|
|
131
|
+
let state: SessionState;
|
|
132
|
+
|
|
133
|
+
beforeEach(() => {
|
|
134
|
+
state = createSessionState();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('should return null before first call to getCurrentSessionId', () => {
|
|
138
|
+
expect(state.peek()).toBeNull();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should return cached session ID without initializing', () => {
|
|
142
|
+
const mockCtx = createSessionCtx('my-session');
|
|
143
|
+
|
|
144
|
+
state.getCurrentSessionId(mockCtx);
|
|
145
|
+
expect(state.peek()).toBe('pi:my-session');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should return null after reset', () => {
|
|
149
|
+
const mockCtx = createSessionCtx('my-session');
|
|
150
|
+
|
|
151
|
+
state.getCurrentSessionId(mockCtx);
|
|
152
|
+
expect(state.peek()).toBe('pi:my-session');
|
|
153
|
+
|
|
154
|
+
state.reset();
|
|
155
|
+
expect(state.peek()).toBeNull();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('startSession', () => {
|
|
160
|
+
let state: SessionState;
|
|
161
|
+
|
|
162
|
+
beforeEach(() => {
|
|
163
|
+
state = createSessionState();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should preserve cached ID when raw session ID is the same', () => {
|
|
167
|
+
const mockCtx = createSessionCtx('stable-session');
|
|
168
|
+
|
|
169
|
+
// Initialize with first call
|
|
170
|
+
const id1 = state.getCurrentSessionId(mockCtx);
|
|
171
|
+
expect(id1).toBe('pi:stable-session');
|
|
172
|
+
|
|
173
|
+
// Call startSession with same raw ID
|
|
174
|
+
state.startSession(mockCtx);
|
|
175
|
+
|
|
176
|
+
// Should preserve the same ID
|
|
177
|
+
const id2 = state.peek();
|
|
178
|
+
expect(id2).toBe('pi:stable-session');
|
|
179
|
+
expect(id2).toBe(id1);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should update ID when raw session ID changes', () => {
|
|
183
|
+
const mockCtx1 = createSessionCtx('session-1');
|
|
184
|
+
const mockCtx2 = createSessionCtx('session-2');
|
|
185
|
+
|
|
186
|
+
// Initialize with first session
|
|
187
|
+
const id1 = state.getCurrentSessionId(mockCtx1);
|
|
188
|
+
expect(id1).toBe('pi:session-1');
|
|
189
|
+
|
|
190
|
+
// Call startSession with different raw ID
|
|
191
|
+
state.startSession(mockCtx2);
|
|
192
|
+
|
|
193
|
+
// Should have new ID
|
|
194
|
+
const id2 = state.peek();
|
|
195
|
+
expect(id2).toBe('pi:session-2');
|
|
196
|
+
expect(id2).not.toBe(id1);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it.each([
|
|
200
|
+
['empty string', ''],
|
|
201
|
+
['throwing manager', THROW_SESSION_ID],
|
|
202
|
+
])('should clear state when session manager returns %s', (_label, sessionIdOrMarker) => {
|
|
203
|
+
const mockCtx1 = createSessionCtx('my-session');
|
|
204
|
+
const mockCtx2 = createSessionCtx(sessionIdOrMarker);
|
|
205
|
+
|
|
206
|
+
// Initialize with valid session
|
|
207
|
+
state.getCurrentSessionId(mockCtx1);
|
|
208
|
+
expect(state.peek()).toBe('pi:my-session');
|
|
209
|
+
|
|
210
|
+
// Call startSession with fallback case
|
|
211
|
+
state.startSession(mockCtx2);
|
|
212
|
+
|
|
213
|
+
// Should have cleared state
|
|
214
|
+
expect(state.peek()).toBeNull();
|
|
215
|
+
|
|
216
|
+
// Next getCurrentSessionId should generate fresh fallback
|
|
217
|
+
const newId = state.getCurrentSessionId(mockCtx2);
|
|
218
|
+
expect(newId).toMatch(
|
|
219
|
+
/^pi:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should create and cache ID when called before getCurrentSessionId', () => {
|
|
224
|
+
const mockCtx = createSessionCtx('my-session');
|
|
225
|
+
|
|
226
|
+
// Call startSession without prior getCurrentSessionId
|
|
227
|
+
state.startSession(mockCtx);
|
|
228
|
+
|
|
229
|
+
// Should have created a cached ID
|
|
230
|
+
expect(state.peek()).toBe('pi:my-session');
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
});
|
|
@@ -33,110 +33,111 @@ describe('formatSessionId', () => {
|
|
|
33
33
|
|
|
34
34
|
describe('isOpenRouterRequest', () => {
|
|
35
35
|
// =============================================================================
|
|
36
|
-
// Parameterized
|
|
36
|
+
// Parameterized tests - detection signals (single source of truth)
|
|
37
37
|
// =============================================================================
|
|
38
38
|
|
|
39
39
|
const detectionCases: DetectionTestCase[] = [
|
|
40
|
-
//
|
|
40
|
+
// Model prefix signal
|
|
41
41
|
{
|
|
42
|
-
name: '
|
|
42
|
+
name: 'model prefix: openrouter/',
|
|
43
43
|
event: { payload: { model: 'openrouter/anthropic/claude-3' } },
|
|
44
44
|
ctx: {},
|
|
45
45
|
expected: true,
|
|
46
46
|
description: 'Model with openrouter/ prefix should be detected',
|
|
47
47
|
},
|
|
48
48
|
{
|
|
49
|
-
name: '
|
|
49
|
+
name: 'model prefix: missing prefix',
|
|
50
50
|
event: { payload: { model: 'anthropic/claude-3' } },
|
|
51
51
|
ctx: {},
|
|
52
52
|
expected: false,
|
|
53
|
-
description:
|
|
53
|
+
description:
|
|
54
|
+
'Model without openrouter/ prefix should not be detected by the model-prefix signal',
|
|
54
55
|
},
|
|
55
56
|
{
|
|
56
|
-
name: '
|
|
57
|
+
name: 'model prefix: similar but not prefix',
|
|
57
58
|
event: { payload: { model: 'my-openrouter-model' } },
|
|
58
59
|
ctx: {},
|
|
59
60
|
expected: false,
|
|
60
61
|
description: 'Model containing openrouter but not as prefix should not match',
|
|
61
62
|
},
|
|
62
63
|
|
|
63
|
-
//
|
|
64
|
+
// Context baseUrl signal
|
|
64
65
|
{
|
|
65
|
-
name: '
|
|
66
|
+
name: 'context baseUrl: contains openrouter.ai',
|
|
66
67
|
event: { payload: { model: 'qwen/coder' } },
|
|
67
68
|
ctx: { model: { baseUrl: 'https://openrouter.ai/api/v1' } },
|
|
68
69
|
expected: true,
|
|
69
70
|
description: 'Context with openrouter.ai baseUrl should be detected',
|
|
70
71
|
},
|
|
71
72
|
{
|
|
72
|
-
name: '
|
|
73
|
+
name: 'context baseUrl: different baseUrl',
|
|
73
74
|
event: { payload: { model: 'claude-3' } },
|
|
74
75
|
ctx: { model: { baseUrl: 'https://api.anthropic.com' } },
|
|
75
76
|
expected: false,
|
|
76
77
|
description: 'Non-OpenRouter baseUrl should not be detected',
|
|
77
78
|
},
|
|
78
79
|
{
|
|
79
|
-
name: '
|
|
80
|
+
name: 'context baseUrl: missing baseUrl',
|
|
80
81
|
event: { payload: { model: 'claude-3' } },
|
|
81
82
|
ctx: { model: {} },
|
|
82
83
|
expected: false,
|
|
83
|
-
description: 'Missing baseUrl should not be detected by
|
|
84
|
+
description: 'Missing baseUrl should not be detected by the context baseUrl signal',
|
|
84
85
|
},
|
|
85
86
|
{
|
|
86
|
-
name: '
|
|
87
|
+
name: 'context baseUrl: no model in context',
|
|
87
88
|
event: { payload: { model: 'claude-3' } },
|
|
88
89
|
ctx: {},
|
|
89
90
|
expected: false,
|
|
90
|
-
description: 'Empty context should not crash
|
|
91
|
+
description: 'Empty context should not crash the context baseUrl signal',
|
|
91
92
|
},
|
|
92
93
|
|
|
93
|
-
//
|
|
94
|
+
// ZDR provider signal
|
|
94
95
|
{
|
|
95
|
-
name: '
|
|
96
|
+
name: 'zdr provider: flag set',
|
|
96
97
|
event: { payload: { model: 'qwen/coder' }, provider: { zdr: true } },
|
|
97
98
|
ctx: {},
|
|
98
99
|
expected: true,
|
|
99
100
|
description: 'Provider with zdr: true should be detected',
|
|
100
101
|
},
|
|
101
102
|
{
|
|
102
|
-
name: '
|
|
103
|
+
name: 'zdr provider: flag not set',
|
|
103
104
|
event: { payload: { model: 'qwen/coder' }, provider: { zdr: false } },
|
|
104
105
|
ctx: {},
|
|
105
106
|
expected: false,
|
|
106
107
|
description: 'Provider with zdr: false should not be detected',
|
|
107
108
|
},
|
|
108
109
|
{
|
|
109
|
-
name: '
|
|
110
|
+
name: 'zdr provider: no provider object',
|
|
110
111
|
event: { payload: { model: 'qwen/coder' } },
|
|
111
112
|
ctx: {},
|
|
112
113
|
expected: false,
|
|
113
|
-
description: 'Missing provider should not be detected by
|
|
114
|
+
description: 'Missing provider should not be detected by the ZDR provider signal',
|
|
114
115
|
},
|
|
115
116
|
|
|
116
|
-
//
|
|
117
|
+
// URL / endpoint signal
|
|
117
118
|
{
|
|
118
|
-
name: '
|
|
119
|
+
name: 'url: contains openrouter.ai',
|
|
119
120
|
event: { payload: { model: 'qwen/coder' }, url: 'https://openrouter.ai/api/v1/chat' },
|
|
120
121
|
ctx: {},
|
|
121
122
|
expected: true,
|
|
122
123
|
description: 'URL containing openrouter.ai should be detected',
|
|
123
124
|
},
|
|
124
125
|
{
|
|
125
|
-
name: '
|
|
126
|
+
name: 'endpoint: alternative to url',
|
|
126
127
|
event: { payload: { model: 'qwen/coder' }, endpoint: 'https://openrouter.ai/api/v1/chat' },
|
|
127
128
|
ctx: {},
|
|
128
129
|
expected: true,
|
|
129
130
|
description: 'Endpoint property should also be checked (fallback to url)',
|
|
130
131
|
},
|
|
131
132
|
{
|
|
132
|
-
name: '
|
|
133
|
+
name: 'url: non-OpenRouter url',
|
|
133
134
|
event: { payload: { model: 'qwen/coder' }, url: 'https://api.anthropic.com/v1/messages' },
|
|
134
135
|
ctx: {},
|
|
135
136
|
expected: false,
|
|
136
137
|
description: 'Non-OpenRouter URL should not be detected',
|
|
137
138
|
},
|
|
138
139
|
{
|
|
139
|
-
name: '
|
|
140
|
+
name: 'url: contains openrouter.ai in path',
|
|
140
141
|
event: {
|
|
141
142
|
payload: { model: 'qwen/coder' },
|
|
142
143
|
url: 'https://proxy.example.com/v1/openrouter.ai/endpoint',
|
|
@@ -146,58 +147,58 @@ describe('isOpenRouterRequest', () => {
|
|
|
146
147
|
description: 'URL containing openrouter.ai anywhere in string should match',
|
|
147
148
|
},
|
|
148
149
|
{
|
|
149
|
-
name: '
|
|
150
|
+
name: 'url: without openrouter.ai string',
|
|
150
151
|
event: { payload: { model: 'qwen/coder' }, url: 'https://example.com/api' },
|
|
151
152
|
ctx: {},
|
|
152
153
|
expected: false,
|
|
153
154
|
description: 'URL without openrouter.ai should not be detected',
|
|
154
155
|
},
|
|
155
156
|
|
|
156
|
-
//
|
|
157
|
+
// Provider name signal (Pi coding agent uses "openrouter" provider)
|
|
157
158
|
{
|
|
158
|
-
name: '
|
|
159
|
+
name: 'provider name: event-level string "openrouter"',
|
|
159
160
|
event: { payload: { model: 'claude-3' }, provider: 'openrouter' },
|
|
160
161
|
ctx: {},
|
|
161
162
|
expected: true,
|
|
162
163
|
description: 'Provider name "openrouter" as string should be detected',
|
|
163
164
|
},
|
|
164
165
|
{
|
|
165
|
-
name: '
|
|
166
|
+
name: 'provider name: payload string "openrouter"',
|
|
166
167
|
event: { payload: { model: 'claude-3', provider: 'openrouter' } },
|
|
167
168
|
ctx: {},
|
|
168
169
|
expected: true,
|
|
169
170
|
description: 'Provider name "openrouter" in payload should be detected',
|
|
170
171
|
},
|
|
171
172
|
{
|
|
172
|
-
name: '
|
|
173
|
+
name: 'provider name: event-level object name "openrouter"',
|
|
173
174
|
event: { payload: { model: 'claude-3' }, provider: { name: 'openrouter' } },
|
|
174
175
|
ctx: {},
|
|
175
176
|
expected: true,
|
|
176
177
|
description: 'Provider object with name "openrouter" should be detected',
|
|
177
178
|
},
|
|
178
179
|
{
|
|
179
|
-
name: '
|
|
180
|
+
name: 'provider name: payload object name "openrouter"',
|
|
180
181
|
event: { payload: { model: 'claude-3', provider: { name: 'openrouter' } } },
|
|
181
182
|
ctx: {},
|
|
182
183
|
expected: true,
|
|
183
184
|
description: 'Provider object in payload with name "openrouter" should be detected',
|
|
184
185
|
},
|
|
185
186
|
{
|
|
186
|
-
name: '
|
|
187
|
+
name: 'provider name: different provider name',
|
|
187
188
|
event: { payload: { model: 'claude-3', provider: 'anthropic' } },
|
|
188
189
|
ctx: {},
|
|
189
190
|
expected: false,
|
|
190
191
|
description: 'Different provider name should not be detected',
|
|
191
192
|
},
|
|
192
193
|
{
|
|
193
|
-
name: '
|
|
194
|
+
name: 'provider name: similar but not exact',
|
|
194
195
|
event: { payload: { model: 'claude-3', provider: 'openrouter-proxy' } },
|
|
195
196
|
ctx: {},
|
|
196
197
|
expected: false,
|
|
197
198
|
description: 'Provider name containing but not exactly "openrouter" should not match',
|
|
198
199
|
},
|
|
199
200
|
{
|
|
200
|
-
name: '
|
|
201
|
+
name: 'url: contains openrouter.ai in path (duplicate coverage)',
|
|
201
202
|
event: {
|
|
202
203
|
payload: { model: 'qwen/coder' },
|
|
203
204
|
url: 'https://proxy.example.com/v1/openrouter.ai/endpoint',
|
|
@@ -207,14 +208,14 @@ describe('isOpenRouterRequest', () => {
|
|
|
207
208
|
description: 'URL containing openrouter.ai anywhere in string should match',
|
|
208
209
|
},
|
|
209
210
|
{
|
|
210
|
-
name: '
|
|
211
|
+
name: 'url: without openrouter.ai string (duplicate coverage)',
|
|
211
212
|
event: { payload: { model: 'qwen/coder' }, url: 'https://example.com/api' },
|
|
212
213
|
ctx: {},
|
|
213
214
|
expected: false,
|
|
214
215
|
description: 'URL without openrouter.ai should not be detected',
|
|
215
216
|
},
|
|
216
217
|
|
|
217
|
-
// Edge cases - missing all detection
|
|
218
|
+
// Edge cases - missing all detection signals
|
|
218
219
|
{
|
|
219
220
|
name: 'edge: empty event',
|
|
220
221
|
event: {},
|
|
@@ -279,9 +280,9 @@ describe('isOpenRouterRequest', () => {
|
|
|
279
280
|
description: 'turn_end without URL/endpoint and without openrouter/ prefix would fail',
|
|
280
281
|
},
|
|
281
282
|
|
|
282
|
-
// Multiple
|
|
283
|
+
// Multiple signals at once
|
|
283
284
|
{
|
|
284
|
-
name: 'multi: all
|
|
285
|
+
name: 'multi: all signals satisfied',
|
|
285
286
|
event: {
|
|
286
287
|
payload: { model: 'openrouter/anthropic/claude-3' },
|
|
287
288
|
url: 'https://openrouter.ai/api/v1/chat',
|
|
@@ -289,34 +290,34 @@ describe('isOpenRouterRequest', () => {
|
|
|
289
290
|
},
|
|
290
291
|
ctx: { model: { baseUrl: 'https://openrouter.ai/api/v1' } },
|
|
291
292
|
expected: true,
|
|
292
|
-
description: 'All detection
|
|
293
|
+
description: 'All detection signals satisfied should return true',
|
|
293
294
|
},
|
|
294
295
|
{
|
|
295
|
-
name: 'multi: only
|
|
296
|
+
name: 'multi: only url signal satisfied',
|
|
296
297
|
event: {
|
|
297
298
|
payload: { model: 'any-model-name' },
|
|
298
299
|
url: 'https://openrouter.ai/api/v1',
|
|
299
300
|
},
|
|
300
301
|
ctx: {},
|
|
301
302
|
expected: true,
|
|
302
|
-
description: 'Only URL
|
|
303
|
+
description: 'Only URL signal satisfied should be sufficient',
|
|
303
304
|
},
|
|
304
305
|
{
|
|
305
|
-
name: 'multi: only
|
|
306
|
+
name: 'multi: only context baseUrl signal satisfied',
|
|
306
307
|
event: { payload: { model: 'any-model' } },
|
|
307
308
|
ctx: { model: { baseUrl: 'https://openrouter.ai/api/v1' } },
|
|
308
309
|
expected: true,
|
|
309
|
-
description: 'Only baseUrl
|
|
310
|
+
description: 'Only context baseUrl signal satisfied should be sufficient',
|
|
310
311
|
},
|
|
311
312
|
{
|
|
312
|
-
name: 'multi: only
|
|
313
|
+
name: 'multi: only ZDR provider signal satisfied',
|
|
313
314
|
event: {
|
|
314
315
|
payload: { model: 'any-model' },
|
|
315
316
|
provider: { zdr: true },
|
|
316
317
|
},
|
|
317
318
|
ctx: {},
|
|
318
319
|
expected: true,
|
|
319
|
-
description: 'Only ZDR
|
|
320
|
+
description: 'Only ZDR provider signal satisfied should be sufficient',
|
|
320
321
|
},
|
|
321
322
|
|
|
322
323
|
// Cache mismatch - model appears openrouter but URL doesn't (edge case)
|
|
@@ -3,6 +3,7 @@ import type { KeyInfo, KeyStatus } from './account-types.js';
|
|
|
3
3
|
|
|
4
4
|
// Re-export error types from client.ts
|
|
5
5
|
import { AuthError, ApiError } from './client.js';
|
|
6
|
+
import { normalizeSdkKeyMetadata } from './normalizers.js';
|
|
6
7
|
|
|
7
8
|
let client: OpenRouter | null = null;
|
|
8
9
|
|
|
@@ -33,8 +34,6 @@ export async function getAccountCredits(): Promise<number | null> {
|
|
|
33
34
|
// Key Management API
|
|
34
35
|
// =============================================================================
|
|
35
36
|
|
|
36
|
-
import type { GetCurrentKeyData, ListData } from '@openrouter/sdk/models/operations/index.js';
|
|
37
|
-
|
|
38
37
|
// Workspace ID for the default workspace (empty string) - used when workspaceId is not specified
|
|
39
38
|
const DEFAULT_WORKSPACE_ID = '';
|
|
40
39
|
|
|
@@ -61,7 +60,9 @@ export async function getAllKeys(): Promise<KeyInfo[] | null> {
|
|
|
61
60
|
const response = await client.apiKeys.list({ workspaceId, includeDisabled: true });
|
|
62
61
|
const rawKeys = response.data;
|
|
63
62
|
|
|
64
|
-
const keys = rawKeys.map((raw) =>
|
|
63
|
+
const keys = rawKeys.map((raw) =>
|
|
64
|
+
keyMetadataToKeyInfo(normalizeSdkKeyMetadata(raw), workspace.name),
|
|
65
|
+
);
|
|
65
66
|
allKeys.push(...keys);
|
|
66
67
|
}
|
|
67
68
|
|
|
@@ -81,7 +82,7 @@ export async function getCurrentKey(): Promise<KeyInfo | null> {
|
|
|
81
82
|
if (!client) return null;
|
|
82
83
|
try {
|
|
83
84
|
const response = await client.apiKeys.getCurrentKeyMetadata();
|
|
84
|
-
return
|
|
85
|
+
return keyMetadataToKeyInfo(normalizeSdkKeyMetadata(response.data), 'Current Workspace');
|
|
85
86
|
} catch (err) {
|
|
86
87
|
throw mapSdkError(err);
|
|
87
88
|
}
|
|
@@ -91,60 +92,11 @@ export async function getCurrentKey(): Promise<KeyInfo | null> {
|
|
|
91
92
|
// Helper Functions
|
|
92
93
|
// =============================================================================
|
|
93
94
|
|
|
94
|
-
function
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
// remaining is number | null in both types
|
|
101
|
-
const remainingValue = raw.limitRemaining;
|
|
102
|
-
|
|
103
|
-
// Determine BYOK status
|
|
104
|
-
let byok: 'incl' | 'excl' | '?' = '?';
|
|
105
|
-
if (raw.includeByokInLimit === true) {
|
|
106
|
-
byok = 'incl';
|
|
107
|
-
} else if (raw.includeByokInLimit === false) {
|
|
108
|
-
byok = 'excl';
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Determine reset cadence
|
|
112
|
-
let resetCadence: 'monthly' | 'daily' | 'never' | 'partial' = 'partial';
|
|
113
|
-
if (raw.limitReset) {
|
|
114
|
-
const reset = raw.limitReset.toLowerCase();
|
|
115
|
-
if (reset === 'monthly') {
|
|
116
|
-
resetCadence = 'monthly';
|
|
117
|
-
} else if (reset === 'daily') {
|
|
118
|
-
resetCadence = 'daily';
|
|
119
|
-
} else if (reset === 'never') {
|
|
120
|
-
resetCadence = 'never';
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Determine hash (ListData has hash, GetCurrentKeyData doesn't)
|
|
125
|
-
const hash = 'hash' in raw ? (raw as ListData).hash : 'unknown';
|
|
126
|
-
|
|
127
|
-
// Determine name (ListData has name, GetCurrentKeyData doesn't - use label as fallback)
|
|
128
|
-
const name = 'name' in raw ? (raw as ListData).name : raw.label;
|
|
129
|
-
|
|
130
|
-
// Get disabled status (ListData has it, GetCurrentKeyData doesn't)
|
|
131
|
-
const disabled = 'disabled' in raw ? (raw as ListData).disabled : false;
|
|
132
|
-
|
|
133
|
-
// For exactOptionalPropertyTypes, we need to handle optional properties carefully
|
|
134
|
-
// The SDK returns number | null but KeyInfo expects number | undefined (or just number)
|
|
135
|
-
// We use type assertion to tell TypeScript that limit/remaining are either number or not set
|
|
136
|
-
|
|
137
|
-
// When limitValue is null, we set limit to undefined (or omit it)
|
|
138
|
-
// When limitValue is a number, we keep it as is
|
|
139
|
-
let limit: number | undefined;
|
|
140
|
-
if (limitValue !== null) {
|
|
141
|
-
limit = limitValue;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
let remaining: number | undefined;
|
|
145
|
-
if (remainingValue !== null) {
|
|
146
|
-
remaining = remainingValue;
|
|
147
|
-
}
|
|
95
|
+
function keyMetadataToKeyInfo(
|
|
96
|
+
metadata: ReturnType<typeof normalizeSdkKeyMetadata>,
|
|
97
|
+
workspaceName: string,
|
|
98
|
+
): KeyInfo {
|
|
99
|
+
const { name, label, used, limit, remaining, resetCadence, byok, hash, disabled } = metadata;
|
|
148
100
|
|
|
149
101
|
// Calculate status based on usage percentage
|
|
150
102
|
let status: KeyStatus;
|
|
@@ -167,10 +119,9 @@ function rawToKeyInfo(raw: GetCurrentKeyData | ListData, workspaceName: string):
|
|
|
167
119
|
}
|
|
168
120
|
}
|
|
169
121
|
|
|
170
|
-
// Create the object
|
|
171
122
|
const keyInfo: KeyInfo = {
|
|
172
123
|
name,
|
|
173
|
-
label
|
|
124
|
+
label,
|
|
174
125
|
status,
|
|
175
126
|
used,
|
|
176
127
|
spend: used, // spend is the same as usage (in USD)
|
|
@@ -181,7 +132,6 @@ function rawToKeyInfo(raw: GetCurrentKeyData | ListData, workspaceName: string):
|
|
|
181
132
|
workspaceName,
|
|
182
133
|
};
|
|
183
134
|
|
|
184
|
-
// Set optional properties explicitly
|
|
185
135
|
if (limit !== undefined) {
|
|
186
136
|
keyInfo.limit = limit;
|
|
187
137
|
}
|