@oxyhq/core 21.0.0 → 21.0.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.
Files changed (57) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/HttpService.js +47 -8
  3. package/dist/cjs/i18n/locales/en-US.json +7 -2
  4. package/dist/cjs/i18n/locales/es-ES.json +7 -2
  5. package/dist/cjs/i18n/locales/locales/en-US.json +7 -2
  6. package/dist/cjs/i18n/locales/locales/es-ES.json +7 -2
  7. package/dist/cjs/index.js +8 -1
  8. package/dist/cjs/inference/OxyInferenceClient.js +330 -0
  9. package/dist/cjs/mixins/OxyServices.accounts.js +5 -72
  10. package/dist/cjs/mixins/OxyServices.inference.js +59 -0
  11. package/dist/cjs/mixins/OxyServices.utility.js +18 -6
  12. package/dist/cjs/mixins/index.js +6 -0
  13. package/dist/cjs/server/auth.js +76 -0
  14. package/dist/cjs/server/index.js +5 -1
  15. package/dist/esm/.tsbuildinfo +1 -1
  16. package/dist/esm/HttpService.js +47 -8
  17. package/dist/esm/i18n/locales/en-US.json +7 -2
  18. package/dist/esm/i18n/locales/es-ES.json +7 -2
  19. package/dist/esm/i18n/locales/locales/en-US.json +7 -2
  20. package/dist/esm/i18n/locales/locales/es-ES.json +7 -2
  21. package/dist/esm/index.js +4 -0
  22. package/dist/esm/inference/OxyInferenceClient.js +325 -0
  23. package/dist/esm/mixins/OxyServices.accounts.js +5 -72
  24. package/dist/esm/mixins/OxyServices.inference.js +56 -0
  25. package/dist/esm/mixins/OxyServices.utility.js +18 -6
  26. package/dist/esm/mixins/index.js +6 -0
  27. package/dist/esm/server/auth.js +72 -0
  28. package/dist/esm/server/index.js +1 -1
  29. package/dist/types/.tsbuildinfo +1 -1
  30. package/dist/types/HttpService.d.ts +39 -1
  31. package/dist/types/index.d.ts +3 -1
  32. package/dist/types/inference/OxyInferenceClient.d.ts +324 -0
  33. package/dist/types/mixins/OxyServices.accounts.d.ts +73 -95
  34. package/dist/types/mixins/OxyServices.inference.d.ts +95 -0
  35. package/dist/types/mixins/OxyServices.utility.d.ts +44 -13
  36. package/dist/types/mixins/index.d.ts +2 -1
  37. package/dist/types/server/auth.d.ts +80 -0
  38. package/dist/types/server/index.d.ts +2 -2
  39. package/package.json +2 -2
  40. package/src/HttpService.ts +50 -10
  41. package/src/__tests__/httpServiceUnwrapEnvelope.test.ts +115 -0
  42. package/src/i18n/locales/en-US.json +7 -2
  43. package/src/i18n/locales/es-ES.json +7 -2
  44. package/src/index.ts +19 -7
  45. package/src/inference/OxyInferenceClient.ts +590 -0
  46. package/src/inference/__tests__/OxyInferenceClient.test.ts +383 -0
  47. package/src/mixins/OxyServices.accounts.ts +75 -176
  48. package/src/mixins/OxyServices.inference.ts +57 -0
  49. package/src/mixins/OxyServices.utility.ts +58 -14
  50. package/src/mixins/__tests__/accounts.test.ts +57 -102
  51. package/src/mixins/__tests__/inferenceFactory.test.ts +58 -0
  52. package/src/mixins/__tests__/serviceAuth.test.ts +2 -0
  53. package/src/mixins/index.ts +8 -0
  54. package/src/server/__tests__/serviceTokenAttribution.test.ts +396 -0
  55. package/src/server/auth.ts +118 -0
  56. package/src/server/index.ts +6 -0
  57. package/src/session/__tests__/accountDialogShape.test.ts +118 -0
@@ -0,0 +1,383 @@
1
+ /**
2
+ * What the inference client puts on the wire, and what it makes of an answer.
3
+ *
4
+ * Three things here can actually be wrong, and they are what these cases pin:
5
+ *
6
+ * 1. **The URL.** A canonical model id contains a slash, so it becomes TWO path
7
+ * segments; a single encoded one matches no route.
8
+ * 2. **The credential lane.** A static key is sent verbatim; a function is
9
+ * called on EVERY request, because an Oxy bearer rotates and a captured one
10
+ * goes stale.
11
+ * 3. **The refusal.** Two routers answer under `/v1` with two different error
12
+ * shapes, and both must become one `OxyInferenceError` carrying the server's
13
+ * own `code`, `retryable` and `requestId` — never a retryability this client
14
+ * inferred from a status.
15
+ *
16
+ * Every "sends nothing" assertion is paired with a positive control proving a
17
+ * valid input DOES reach the transport; without one, a method that threw
18
+ * unconditionally would satisfy them all.
19
+ */
20
+
21
+ import { OxyInferenceClient, OxyInferenceError } from '../OxyInferenceClient';
22
+
23
+ /** A `fetch` double that records its calls and replays queued answers. */
24
+ function stubFetch(
25
+ answers: Array<{ status: number; body: unknown; headers?: Record<string, string> }>,
26
+ ) {
27
+ const calls: Array<{ url: string; init: RequestInit }> = [];
28
+ const impl = jest.fn(async (url: string | URL | Request, init?: RequestInit) => {
29
+ calls.push({ url: String(url), init: init ?? {} });
30
+ const answer = answers.shift() ?? { status: 200, body: {} };
31
+ return new Response(JSON.stringify(answer.body), {
32
+ status: answer.status,
33
+ headers: { 'Content-Type': 'application/json', ...(answer.headers ?? {}) },
34
+ });
35
+ });
36
+ return { impl: impl as unknown as typeof fetch, calls };
37
+ }
38
+
39
+ function headerOf(init: RequestInit, name: string): string | undefined {
40
+ const headers = init.headers as Record<string, string> | undefined;
41
+ return headers?.[name];
42
+ }
43
+
44
+ describe('OxyInferenceClient', () => {
45
+ describe('the two credential lanes', () => {
46
+ it('sends a static machine key verbatim, exactly as a stock SDK would', async () => {
47
+ const { impl, calls } = stubFetch([{ status: 200, body: { data: [], count: 0 } }]);
48
+ const client = new OxyInferenceClient({
49
+ credential: 'oxy_sk_0123456789abcdef_deadbeef',
50
+ baseURL: 'http://test.invalid',
51
+ fetch: impl,
52
+ });
53
+
54
+ await client.listModels();
55
+
56
+ expect(headerOf(calls[0].init, 'Authorization')).toBe(
57
+ 'Bearer oxy_sk_0123456789abcdef_deadbeef',
58
+ );
59
+ });
60
+
61
+ it('calls a credential FUNCTION on every request, so a rotated bearer is used', async () => {
62
+ const { impl, calls } = stubFetch([
63
+ { status: 200, body: { data: [], count: 0 } },
64
+ { status: 200, body: { data: [], count: 0 } },
65
+ ]);
66
+ const bearers = ['first', 'second'];
67
+ const client = new OxyInferenceClient({
68
+ credential: () => bearers.shift() ?? null,
69
+ baseURL: 'http://test.invalid',
70
+ fetch: impl,
71
+ });
72
+
73
+ await client.listModels();
74
+ await client.listModels();
75
+
76
+ // A client that captured the bearer at construction would send
77
+ // `first` twice — which is what an expired session looks like an
78
+ // hour into a process's life.
79
+ expect(headerOf(calls[0].init, 'Authorization')).toBe('Bearer first');
80
+ expect(headerOf(calls[1].init, 'Authorization')).toBe('Bearer second');
81
+ });
82
+
83
+ it('refuses before fetching when the credential resolves to nothing', async () => {
84
+ const { impl, calls } = stubFetch([]);
85
+ const client = new OxyInferenceClient({
86
+ credential: () => null,
87
+ baseURL: 'http://test.invalid',
88
+ fetch: impl,
89
+ });
90
+
91
+ await expect(client.listModels()).rejects.toThrow('no bearer');
92
+ expect(calls).toHaveLength(0);
93
+ });
94
+ });
95
+
96
+ describe('catalogue reads', () => {
97
+ it('GETs /v1/models and unwraps the data envelope', async () => {
98
+ const entry = { schemaVersion: 1, modelId: 'acme/some-model' };
99
+ const { impl, calls } = stubFetch([
100
+ { status: 200, body: { data: [entry], count: 1 } },
101
+ ]);
102
+ const client = new OxyInferenceClient({
103
+ credential: 'k',
104
+ baseURL: 'http://test.invalid',
105
+ fetch: impl,
106
+ });
107
+
108
+ await expect(client.listModels()).resolves.toEqual([entry]);
109
+ expect(calls[0].url).toBe('http://test.invalid/v1/models');
110
+ });
111
+
112
+ it('returns [] for an empty catalogue rather than throwing', async () => {
113
+ // The catalogue IS empty today. A consumer treating `[]` as a failure
114
+ // would be broken on the only answer the endpoint currently gives.
115
+ const { impl } = stubFetch([{ status: 200, body: { data: [], count: 0 } }]);
116
+ const client = new OxyInferenceClient({
117
+ credential: 'k',
118
+ baseURL: 'http://test.invalid',
119
+ fetch: impl,
120
+ });
121
+
122
+ await expect(client.listModels()).resolves.toEqual([]);
123
+ });
124
+
125
+ it('splits a canonical model id into two path segments', async () => {
126
+ const entry = { schemaVersion: 1, modelId: 'acme/some-model' };
127
+ const { impl, calls } = stubFetch([{ status: 200, body: { data: entry } }]);
128
+ const client = new OxyInferenceClient({
129
+ credential: 'k',
130
+ baseURL: 'http://test.invalid',
131
+ fetch: impl,
132
+ });
133
+
134
+ await expect(client.getModel('acme/some-model')).resolves.toEqual(entry);
135
+ // NOT `/v1/models/acme%2Fsome-model` — one encoded segment matches no route.
136
+ expect(calls[0].url).toBe('http://test.invalid/v1/models/acme/some-model');
137
+ });
138
+
139
+ it.each([
140
+ ['a revision pin, which names a reference and not a model', 'acme/some-model@2026-05-01'],
141
+ ['no publisher segment', 'some-model'],
142
+ ['a third segment', 'acme/some-model/turbo'],
143
+ ['an empty publisher', '/some-model'],
144
+ ['an empty model', 'acme/'],
145
+ ['uppercase, which the id grammar forbids', 'Acme/Some-Model'],
146
+ ['the empty string', ''],
147
+ ])('getModel refuses %s, and sends nothing', async (_label, modelId) => {
148
+ const { impl, calls } = stubFetch([]);
149
+ const client = new OxyInferenceClient({
150
+ credential: 'k',
151
+ baseURL: 'http://test.invalid',
152
+ fetch: impl,
153
+ });
154
+
155
+ await expect(client.getModel(modelId)).rejects.toThrow('Not a canonical model id');
156
+ expect(calls).toHaveLength(0);
157
+ });
158
+
159
+ it('positive control: a well-formed id DOES reach the transport', async () => {
160
+ const { impl, calls } = stubFetch([
161
+ { status: 200, body: { data: { schemaVersion: 1, modelId: 'acme/other' } } },
162
+ ]);
163
+ const client = new OxyInferenceClient({
164
+ credential: 'k',
165
+ baseURL: 'http://test.invalid',
166
+ fetch: impl,
167
+ });
168
+
169
+ await client.getModel('acme/other');
170
+ expect(calls).toHaveLength(1);
171
+ });
172
+
173
+ it('GETs the routing-profiles collection as ONE segment', async () => {
174
+ // Were it built as two, `/v1/models/:publisher/:model` would capture
175
+ // it and read it as a model lookup.
176
+ const { impl, calls } = stubFetch([
177
+ { status: 200, body: { data: [{ schemaVersion: 1, slug: 'auto' }], count: 1 } },
178
+ ]);
179
+ const client = new OxyInferenceClient({
180
+ credential: 'k',
181
+ baseURL: 'http://test.invalid',
182
+ fetch: impl,
183
+ });
184
+
185
+ await client.listRoutingProfiles();
186
+ expect(calls[0].url).toBe('http://test.invalid/v1/models/routing-profiles');
187
+ });
188
+ });
189
+
190
+ describe('respond', () => {
191
+ it('POSTs the request body to /v1/responses', async () => {
192
+ const { impl, calls } = stubFetch([
193
+ {
194
+ status: 200,
195
+ body: {
196
+ schemaVersion: 1,
197
+ requestId: 'req-1',
198
+ model: 'acme/some-model@1',
199
+ servingProvider: 'acme-cloud',
200
+ finishReason: 'stop',
201
+ output: [],
202
+ usage: [],
203
+ routingPolicy: { routingPolicyId: 'platform-default', policyVersion: 1 },
204
+ },
205
+ },
206
+ ]);
207
+ const client = new OxyInferenceClient({
208
+ credential: 'k',
209
+ baseURL: 'http://test.invalid',
210
+ fetch: impl,
211
+ });
212
+
213
+ const answer = await client.respond({ model: 'acme/some-model', input: 'hello' });
214
+
215
+ expect(calls[0].url).toBe('http://test.invalid/v1/responses');
216
+ expect(calls[0].init.method).toBe('POST');
217
+ expect(JSON.parse(String(calls[0].init.body))).toEqual({
218
+ model: 'acme/some-model',
219
+ input: 'hello',
220
+ });
221
+ expect(answer.requestId).toBe('req-1');
222
+ });
223
+
224
+ it('carries the idempotency key and the delegated user in headers, not the body', async () => {
225
+ const { impl, calls } = stubFetch([{ status: 200, body: {} }]);
226
+ const client = new OxyInferenceClient({
227
+ credential: 'k',
228
+ baseURL: 'http://test.invalid',
229
+ fetch: impl,
230
+ });
231
+
232
+ await client.respond(
233
+ { model: 'acme/some-model', input: 'hello' },
234
+ { idempotencyKey: 'key-1', delegatedUserId: 'user-1' },
235
+ );
236
+
237
+ expect(headerOf(calls[0].init, 'Idempotency-Key')).toBe('key-1');
238
+ expect(headerOf(calls[0].init, 'X-Oxy-User-Id')).toBe('user-1');
239
+ // Neither belongs in the body: the delegated user is attribution and
240
+ // never a billing identity, and the key is a transport concern.
241
+ expect(JSON.parse(String(calls[0].init.body))).toEqual({
242
+ model: 'acme/some-model',
243
+ input: 'hello',
244
+ });
245
+ });
246
+
247
+ it('passes the abort signal through, so a disconnect can cancel', async () => {
248
+ const { impl, calls } = stubFetch([{ status: 200, body: {} }]);
249
+ const client = new OxyInferenceClient({
250
+ credential: 'k',
251
+ baseURL: 'http://test.invalid',
252
+ fetch: impl,
253
+ });
254
+ const controller = new AbortController();
255
+
256
+ await client.respond({ input: 'hello' }, { signal: controller.signal });
257
+
258
+ expect(calls[0].init.signal).toBe(controller.signal);
259
+ });
260
+ });
261
+
262
+ describe('refusals', () => {
263
+ it('maps the edge error body onto OxyInferenceError, keeping the server verdict', async () => {
264
+ // This IS what a developer observes today: the edge authenticates,
265
+ // reserves, finds no data plane, releases the hold and refuses.
266
+ const { impl } = stubFetch([
267
+ {
268
+ status: 503,
269
+ body: {
270
+ schemaVersion: 1,
271
+ code: 'service_unavailable',
272
+ message: 'No inference data plane is configured for this deployment.',
273
+ retryable: false,
274
+ requestId: 'req-503',
275
+ },
276
+ },
277
+ ]);
278
+ const client = new OxyInferenceClient({
279
+ credential: 'k',
280
+ baseURL: 'http://test.invalid',
281
+ fetch: impl,
282
+ });
283
+
284
+ const error: unknown = await client
285
+ .respond({ model: 'acme/some-model', input: 'hi' })
286
+ .catch((thrown: unknown) => thrown);
287
+
288
+ expect(error).toBeInstanceOf(OxyInferenceError);
289
+ const inferenceError = error as OxyInferenceError;
290
+ expect(inferenceError.code).toBe('service_unavailable');
291
+ expect(inferenceError.requestId).toBe('req-503');
292
+ expect(inferenceError.status).toBe(503);
293
+ // NOT derived from the 503: an unconfigured deployment is an
294
+ // operator's to fix, and a retrying client would make it a storm.
295
+ expect(inferenceError.retryable).toBe(false);
296
+ });
297
+
298
+ it('keeps retryAfterMs only when the server said the retry could succeed', async () => {
299
+ const { impl } = stubFetch([
300
+ {
301
+ status: 429,
302
+ body: {
303
+ schemaVersion: 1,
304
+ code: 'rate_limited',
305
+ message: 'Slow down.',
306
+ retryable: true,
307
+ retryAfterMs: 2000,
308
+ requestId: 'req-429',
309
+ },
310
+ },
311
+ ]);
312
+ const client = new OxyInferenceClient({
313
+ credential: 'k',
314
+ baseURL: 'http://test.invalid',
315
+ fetch: impl,
316
+ });
317
+
318
+ const error = (await client
319
+ .respond({ model: 'acme/some-model', input: 'hi' })
320
+ .catch((thrown: unknown) => thrown)) as OxyInferenceError;
321
+
322
+ expect(error.retryable).toBe(true);
323
+ expect(error.retryAfterMs).toBe(2000);
324
+ });
325
+
326
+ it('treats a body that asserts nothing as NOT retryable', async () => {
327
+ // The safe direction. Inventing a retryable code for an unreadable
328
+ // failure is how one outage becomes a retry storm.
329
+ const { impl } = stubFetch([{ status: 500, body: {} }]);
330
+ const client = new OxyInferenceClient({
331
+ credential: 'k',
332
+ baseURL: 'http://test.invalid',
333
+ fetch: impl,
334
+ });
335
+
336
+ const error = (await client
337
+ .respond({ model: 'acme/some-model', input: 'hi' })
338
+ .catch((thrown: unknown) => thrown)) as OxyInferenceError;
339
+
340
+ expect(error.retryable).toBe(false);
341
+ expect(error.code).toBe('internal_error');
342
+ });
343
+
344
+ it('reads the platform envelope the catalogue router answers with', async () => {
345
+ // Two routers serve `/v1`, and only one of them speaks the contract
346
+ // error shape. A caller's `catch` must not have to know which.
347
+ const { impl } = stubFetch([
348
+ {
349
+ status: 404,
350
+ body: { error: 'Not Found', message: 'No model acme/nope is available to you' },
351
+ headers: { 'X-Oxy-Request-Id': 'req-404' },
352
+ },
353
+ ]);
354
+ const client = new OxyInferenceClient({
355
+ credential: 'k',
356
+ baseURL: 'http://test.invalid',
357
+ fetch: impl,
358
+ });
359
+
360
+ const error = (await client
361
+ .getModel('acme/nope')
362
+ .catch((thrown: unknown) => thrown)) as OxyInferenceError;
363
+
364
+ expect(error).toBeInstanceOf(OxyInferenceError);
365
+ expect(error.code).toBe('model_not_found');
366
+ expect(error.message).toBe('No model acme/nope is available to you');
367
+ // The envelope carries no requestId, so the header is the source.
368
+ expect(error.requestId).toBe('req-404');
369
+ });
370
+ });
371
+
372
+ it('trims a trailing slash from the base URL rather than doubling it', async () => {
373
+ const { impl, calls } = stubFetch([{ status: 200, body: { data: [], count: 0 } }]);
374
+ const client = new OxyInferenceClient({
375
+ credential: 'k',
376
+ baseURL: 'http://test.invalid/',
377
+ fetch: impl,
378
+ });
379
+
380
+ await client.listModels();
381
+ expect(calls[0].url).toBe('http://test.invalid/v1/models');
382
+ });
383
+ });