@namzu/openai 1.0.3 → 1.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 +167 -0
- package/dist/client.d.ts +28 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +77 -7
- package/dist/client.js.map +1 -1
- package/dist/strict-schema.d.ts +2 -0
- package/dist/strict-schema.d.ts.map +1 -0
- package/dist/strict-schema.js +2 -0
- package/dist/strict-schema.js.map +1 -0
- package/package.json +13 -3
- package/src/client.ts +86 -7
- package/src/strict-schema.ts +0 -0
- package/dist/__tests__/error-taxonomy.test.d.ts +0 -14
- package/dist/__tests__/error-taxonomy.test.d.ts.map +0 -1
- package/dist/__tests__/error-taxonomy.test.js +0 -203
- package/dist/__tests__/error-taxonomy.test.js.map +0 -1
- package/dist/__tests__/openai.test.d.ts +0 -2
- package/dist/__tests__/openai.test.d.ts.map +0 -1
- package/dist/__tests__/openai.test.js +0 -111
- package/dist/__tests__/openai.test.js.map +0 -1
- package/src/__tests__/error-taxonomy.test.ts +0 -234
- package/src/__tests__/openai.test.ts +0 -127
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider error taxonomy — OpenAI driver.
|
|
3
|
-
*
|
|
4
|
-
* Like the Anthropic driver, this one lets the vendor SDK's own error type
|
|
5
|
-
* escape verbatim (`RateLimitError`, `BadRequestError`, …). Those carry a
|
|
6
|
-
* status but are vendor classes, so no caller can classify them without
|
|
7
|
-
* importing `openai` — and their `message` interpolates the response body.
|
|
8
|
-
*
|
|
9
|
-
* Transport seam: `OpenAIConfig.baseURL` — a loopback server answering
|
|
10
|
-
* with a scripted status/headers/body, so the REAL vendor client runs
|
|
11
|
-
* (including its internal retries, which this group must not disturb).
|
|
12
|
-
*/
|
|
13
|
-
import { createServer } from 'node:http';
|
|
14
|
-
import { afterEach, describe, expect, it } from 'vitest';
|
|
15
|
-
import { OpenAIProvider } from '../client.js';
|
|
16
|
-
/** Obviously fake, non-functional token — present only as a leak probe. */
|
|
17
|
-
const FAKE_CREDENTIAL = 'sk-ant-FAKE-DO-NOT-USE-0000';
|
|
18
|
-
let server;
|
|
19
|
-
afterEach(async () => {
|
|
20
|
-
if (server) {
|
|
21
|
-
await new Promise((resolve) => server?.close(() => resolve()));
|
|
22
|
-
server = undefined;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
async function startEndpoint(reply) {
|
|
26
|
-
server = createServer((_req, res) => {
|
|
27
|
-
res.writeHead(reply.status, {
|
|
28
|
-
'Content-Type': 'application/json',
|
|
29
|
-
...(reply.headers ?? {}),
|
|
30
|
-
});
|
|
31
|
-
res.end(reply.body);
|
|
32
|
-
});
|
|
33
|
-
await new Promise((resolve) => server?.listen(0, '127.0.0.1', () => resolve()));
|
|
34
|
-
const { port } = server.address();
|
|
35
|
-
return `http://127.0.0.1:${port}/v1`;
|
|
36
|
-
}
|
|
37
|
-
async function captureChatStreamError(reply) {
|
|
38
|
-
const baseURL = await startEndpoint(reply);
|
|
39
|
-
// maxRetries is deliberately NOT set: this group classifies errors and
|
|
40
|
-
// must leave the vendor SDK's own retry behaviour exactly as it is.
|
|
41
|
-
const provider = new OpenAIProvider({
|
|
42
|
-
apiKey: 'test-key',
|
|
43
|
-
model: 'gpt-4o-mini',
|
|
44
|
-
baseURL,
|
|
45
|
-
});
|
|
46
|
-
try {
|
|
47
|
-
for await (const _chunk of provider.chatStream({
|
|
48
|
-
model: 'gpt-4o-mini',
|
|
49
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
50
|
-
})) {
|
|
51
|
-
// drain
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
catch (err) {
|
|
55
|
-
return err;
|
|
56
|
-
}
|
|
57
|
-
throw new Error('expected chatStream to throw');
|
|
58
|
-
}
|
|
59
|
-
async function captureProviderError(provider, signal) {
|
|
60
|
-
try {
|
|
61
|
-
for await (const _chunk of provider.chatStream({
|
|
62
|
-
model: 'gpt-4o-mini',
|
|
63
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
64
|
-
signal,
|
|
65
|
-
})) {
|
|
66
|
-
// drain
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
catch (err) {
|
|
70
|
-
return err;
|
|
71
|
-
}
|
|
72
|
-
throw new Error('expected chatStream to throw');
|
|
73
|
-
}
|
|
74
|
-
describe('@namzu/openai — provider error taxonomy', () => {
|
|
75
|
-
it('(a) 429 with `retry-after: 2` throws a throttle-classified error carrying retryAfterMs', async () => {
|
|
76
|
-
const err = await captureChatStreamError({
|
|
77
|
-
status: 429,
|
|
78
|
-
headers: { 'retry-after': '2' },
|
|
79
|
-
body: JSON.stringify({
|
|
80
|
-
error: { message: 'Rate limit reached', type: 'rate_limit_error' },
|
|
81
|
-
}),
|
|
82
|
-
});
|
|
83
|
-
expect(err).toMatchObject({
|
|
84
|
-
name: 'ProviderRequestError',
|
|
85
|
-
kind: 'throttle',
|
|
86
|
-
status: 429,
|
|
87
|
-
retryAfterMs: 2000,
|
|
88
|
-
providerId: 'openai',
|
|
89
|
-
});
|
|
90
|
-
}, 60_000);
|
|
91
|
-
it("(c) 400 whose body reports a context-length overflow classifies as 'context_overflow'", async () => {
|
|
92
|
-
const err = await captureChatStreamError({
|
|
93
|
-
status: 400,
|
|
94
|
-
body: JSON.stringify({
|
|
95
|
-
error: {
|
|
96
|
-
message: "This model's maximum context length is 128000 tokens. However, your messages resulted in 190210 tokens.",
|
|
97
|
-
type: 'invalid_request_error',
|
|
98
|
-
code: 'context_length_exceeded',
|
|
99
|
-
},
|
|
100
|
-
}),
|
|
101
|
-
});
|
|
102
|
-
expect(err).toMatchObject({
|
|
103
|
-
name: 'ProviderRequestError',
|
|
104
|
-
kind: 'context_overflow',
|
|
105
|
-
status: 400,
|
|
106
|
-
providerId: 'openai',
|
|
107
|
-
});
|
|
108
|
-
}, 30_000);
|
|
109
|
-
it("(c) 400 that is not an overflow classifies as 'bad_request'", async () => {
|
|
110
|
-
const err = await captureChatStreamError({
|
|
111
|
-
status: 400,
|
|
112
|
-
body: JSON.stringify({
|
|
113
|
-
error: {
|
|
114
|
-
message: "Unrecognized request argument: 'temperture'",
|
|
115
|
-
type: 'invalid_request_error',
|
|
116
|
-
},
|
|
117
|
-
}),
|
|
118
|
-
});
|
|
119
|
-
expect(err).toMatchObject({
|
|
120
|
-
name: 'ProviderRequestError',
|
|
121
|
-
kind: 'bad_request',
|
|
122
|
-
status: 400,
|
|
123
|
-
providerId: 'openai',
|
|
124
|
-
});
|
|
125
|
-
}, 30_000);
|
|
126
|
-
it("(auth) 401 classifies as 'auth'", async () => {
|
|
127
|
-
const err = await captureChatStreamError({
|
|
128
|
-
status: 401,
|
|
129
|
-
body: JSON.stringify({
|
|
130
|
-
error: {
|
|
131
|
-
message: 'Incorrect API key provided',
|
|
132
|
-
type: 'invalid_request_error',
|
|
133
|
-
},
|
|
134
|
-
}),
|
|
135
|
-
});
|
|
136
|
-
expect(err).toMatchObject({
|
|
137
|
-
name: 'ProviderRequestError',
|
|
138
|
-
kind: 'auth',
|
|
139
|
-
status: 401,
|
|
140
|
-
providerId: 'openai',
|
|
141
|
-
});
|
|
142
|
-
}, 30_000);
|
|
143
|
-
it('(d) SECURITY: a credential echoed back in a 401 body never reaches the thrown message', async () => {
|
|
144
|
-
const err = await captureChatStreamError({
|
|
145
|
-
status: 401,
|
|
146
|
-
body: JSON.stringify({
|
|
147
|
-
error: { message: `Incorrect API key provided: ${FAKE_CREDENTIAL}` },
|
|
148
|
-
}),
|
|
149
|
-
});
|
|
150
|
-
expect(err.message).not.toContain(FAKE_CREDENTIAL);
|
|
151
|
-
expect(JSON.stringify(err)).not.toContain(FAKE_CREDENTIAL);
|
|
152
|
-
expect('cause' in err).toBe(false);
|
|
153
|
-
}, 30_000);
|
|
154
|
-
it('classifies and sanitizes an SDK failure thrown after streaming began', async () => {
|
|
155
|
-
const vendor = Object.assign(new Error(`overloaded_error ${FAKE_CREDENTIAL}`), {
|
|
156
|
-
name: 'APIError',
|
|
157
|
-
});
|
|
158
|
-
const provider = new OpenAIProvider({
|
|
159
|
-
apiKey: 'test-key',
|
|
160
|
-
model: 'gpt-4o-mini',
|
|
161
|
-
});
|
|
162
|
-
const stream = {
|
|
163
|
-
async *[Symbol.asyncIterator]() {
|
|
164
|
-
yield {
|
|
165
|
-
id: 'chatcmpl-1',
|
|
166
|
-
choices: [{ delta: { content: 'partial' }, finish_reason: null }],
|
|
167
|
-
usage: null,
|
|
168
|
-
};
|
|
169
|
-
throw vendor;
|
|
170
|
-
},
|
|
171
|
-
};
|
|
172
|
-
provider.client = {
|
|
173
|
-
chat: { completions: { create: async () => stream } },
|
|
174
|
-
};
|
|
175
|
-
const err = await captureProviderError(provider);
|
|
176
|
-
expect(err).toMatchObject({
|
|
177
|
-
name: 'ProviderRequestError',
|
|
178
|
-
kind: 'server',
|
|
179
|
-
providerId: 'openai',
|
|
180
|
-
});
|
|
181
|
-
expect(err.message).not.toContain(FAKE_CREDENTIAL);
|
|
182
|
-
expect('cause' in err).toBe(false);
|
|
183
|
-
});
|
|
184
|
-
it('preserves the caller abort reason when the SDK replaces the error object', async () => {
|
|
185
|
-
const controller = new AbortController();
|
|
186
|
-
const reason = new Error('user stopped');
|
|
187
|
-
const sdkAbort = Object.assign(new Error('request aborted'), {
|
|
188
|
-
name: 'APIUserAbortError',
|
|
189
|
-
});
|
|
190
|
-
controller.abort(reason);
|
|
191
|
-
const provider = new OpenAIProvider({
|
|
192
|
-
apiKey: 'test-key',
|
|
193
|
-
model: 'gpt-4o-mini',
|
|
194
|
-
});
|
|
195
|
-
provider.client = {
|
|
196
|
-
chat: { completions: { create: async () => Promise.reject(sdkAbort) } },
|
|
197
|
-
};
|
|
198
|
-
const err = await captureProviderError(provider, controller.signal);
|
|
199
|
-
expect(sdkAbort).not.toBe(reason);
|
|
200
|
-
expect(err).toBe(reason);
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
//# sourceMappingURL=error-taxonomy.test.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;QACzF,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;QACxC,MAAM,MAAM,GAAG,IAAI,KAAK,CAAC,cAAc,CAAC,CAAA;QACxC,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,iBAAiB,CAAC,EAAE;YAC5D,IAAI,EAAE,mBAAmB;SACzB,CAAC,CAAA;QACF,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;QACxB,MAAM,QAAQ,GAAG,IAAI,cAAc,CAAC;YACnC,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,aAAa;SACpB,CAAC,CACD;QAAC,QAA2C,CAAC,MAAM,GAAG;YACtD,IAAI,EAAE,EAAE,WAAW,EAAE,EAAE,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,EAAE;SACvE,CAAA;QAED,MAAM,GAAG,GAAG,MAAM,oBAAoB,CAAC,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,CAAA;QAEnE,MAAM,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACjC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IACzB,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openai.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/openai.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { DuplicateProviderError, ProviderRegistry } from '@namzu/sdk';
|
|
2
|
-
import { beforeEach, describe, expect, it } from 'vitest';
|
|
3
|
-
import { toOpenAIMessages } from '../client.js';
|
|
4
|
-
import { OPENAI_CAPABILITIES, OpenAIProvider, registerOpenAI } from '../index.js';
|
|
5
|
-
// Ensure a clean slate between tests. The sdk pre-registers 'mock' on import
|
|
6
|
-
// via its sideEffects whitelist; we only need to clear 'openai' to make
|
|
7
|
-
// re-registration deterministic across tests.
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
if (ProviderRegistry.isSupported('openai')) {
|
|
10
|
-
ProviderRegistry.unregister('openai');
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
describe('@namzu/openai', () => {
|
|
14
|
-
describe('registerOpenAI()', () => {
|
|
15
|
-
it("adds 'openai' to the ProviderRegistry", () => {
|
|
16
|
-
expect(ProviderRegistry.isSupported('openai')).toBe(false);
|
|
17
|
-
registerOpenAI();
|
|
18
|
-
expect(ProviderRegistry.isSupported('openai')).toBe(true);
|
|
19
|
-
expect(ProviderRegistry.listTypes()).toContain('openai');
|
|
20
|
-
});
|
|
21
|
-
it('throws DuplicateProviderError when called twice without options', () => {
|
|
22
|
-
registerOpenAI();
|
|
23
|
-
expect(() => registerOpenAI()).toThrowError(DuplicateProviderError);
|
|
24
|
-
});
|
|
25
|
-
it('allows re-registration when { replace: true } is passed', () => {
|
|
26
|
-
registerOpenAI();
|
|
27
|
-
expect(() => registerOpenAI({ replace: true })).not.toThrow();
|
|
28
|
-
expect(ProviderRegistry.isSupported('openai')).toBe(true);
|
|
29
|
-
});
|
|
30
|
-
it('exposes capabilities through the registry after registration', () => {
|
|
31
|
-
registerOpenAI();
|
|
32
|
-
const caps = ProviderRegistry.getCapabilities('openai');
|
|
33
|
-
expect(caps).toEqual(OPENAI_CAPABILITIES);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
describe('OPENAI_CAPABILITIES', () => {
|
|
37
|
-
it('declares the expected capability flags', () => {
|
|
38
|
-
expect(OPENAI_CAPABILITIES).toEqual({
|
|
39
|
-
supportsTools: true,
|
|
40
|
-
supportsStreaming: true,
|
|
41
|
-
supportsFunctionCalling: true,
|
|
42
|
-
supportsVision: true,
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
it('is exposed on the provider instance for runtime negotiation', () => {
|
|
46
|
-
const provider = new OpenAIProvider({ apiKey: 'test-key' });
|
|
47
|
-
expect(provider.capabilities).toEqual(OPENAI_CAPABILITIES);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
describe('ProviderRegistry.create({ type: "openai", ... })', () => {
|
|
51
|
-
it('narrows the config type via module augmentation and instantiates OpenAIProvider', () => {
|
|
52
|
-
registerOpenAI();
|
|
53
|
-
const { provider, capabilities } = ProviderRegistry.create({
|
|
54
|
-
type: 'openai',
|
|
55
|
-
apiKey: 'test-key',
|
|
56
|
-
model: 'gpt-4o-mini',
|
|
57
|
-
});
|
|
58
|
-
expect(provider).toBeInstanceOf(OpenAIProvider);
|
|
59
|
-
expect(capabilities).toEqual(OPENAI_CAPABILITIES);
|
|
60
|
-
});
|
|
61
|
-
it('throws when apiKey is missing', () => {
|
|
62
|
-
registerOpenAI();
|
|
63
|
-
expect(() => ProviderRegistry.create({
|
|
64
|
-
type: 'openai',
|
|
65
|
-
apiKey: '',
|
|
66
|
-
})).toThrowError(/API key is required/);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
describe('toOpenAIMessages image attachments', () => {
|
|
70
|
-
it('maps user-message attachments to image_url content parts with base64 data URIs', () => {
|
|
71
|
-
const messages = [
|
|
72
|
-
{
|
|
73
|
-
role: 'user',
|
|
74
|
-
content: 'what is in this image?',
|
|
75
|
-
attachments: [
|
|
76
|
-
{ data: 'aGVsbG8=', mediaType: 'image/png' },
|
|
77
|
-
{ data: 'd29ybGQ=', mediaType: 'image/jpeg' },
|
|
78
|
-
],
|
|
79
|
-
},
|
|
80
|
-
];
|
|
81
|
-
const [mapped] = toOpenAIMessages(messages);
|
|
82
|
-
expect(mapped).toEqual({
|
|
83
|
-
role: 'user',
|
|
84
|
-
content: [
|
|
85
|
-
{ type: 'text', text: 'what is in this image?' },
|
|
86
|
-
{ type: 'image_url', image_url: { url: 'data:image/png;base64,aGVsbG8=' } },
|
|
87
|
-
{ type: 'image_url', image_url: { url: 'data:image/jpeg;base64,d29ybGQ=' } },
|
|
88
|
-
],
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
it('omits the text part when the user message is empty but keeps the images', () => {
|
|
92
|
-
const messages = [
|
|
93
|
-
{
|
|
94
|
-
role: 'user',
|
|
95
|
-
content: '',
|
|
96
|
-
attachments: [{ data: 'aGVsbG8=', mediaType: 'image/webp' }],
|
|
97
|
-
},
|
|
98
|
-
];
|
|
99
|
-
const [mapped] = toOpenAIMessages(messages);
|
|
100
|
-
expect(mapped).toEqual({
|
|
101
|
-
role: 'user',
|
|
102
|
-
content: [{ type: 'image_url', image_url: { url: 'data:image/webp;base64,aGVsbG8=' } }],
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
it('keeps plain text-only user messages in string form (no content parts)', () => {
|
|
106
|
-
const messages = [{ role: 'user', content: 'hello' }];
|
|
107
|
-
expect(toOpenAIMessages(messages)).toEqual([{ role: 'user', content: 'hello' }]);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
//# sourceMappingURL=openai.test.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"openai.test.js","sourceRoot":"","sources":["../../src/__tests__/openai.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAErE,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjF,6EAA6E;AAC7E,wEAAwE;AACxE,8CAA8C;AAC9C,UAAU,CAAC,GAAG,EAAE;IACf,IAAI,gBAAgB,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5C,gBAAgB,CAAC,UAAU,CAAC,QAAQ,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CAAC;gBACtB,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,SAAS,EAAE,EAAE,GAAG,EAAE,iCAAiC,EAAE,EAAE,CAAC;aACvF,CAAC,CAAA;QACH,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,uEAAuE,EAAE,GAAG,EAAE;YAChF,MAAM,QAAQ,GAAqC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAA;YACvF,MAAM,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,CAAA;QACjF,CAAC,CAAC,CAAA;IACH,CAAC,CAAC,CAAA;AACH,CAAC,CAAC,CAAA"}
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider error taxonomy — OpenAI driver.
|
|
3
|
-
*
|
|
4
|
-
* Like the Anthropic driver, this one lets the vendor SDK's own error type
|
|
5
|
-
* escape verbatim (`RateLimitError`, `BadRequestError`, …). Those carry a
|
|
6
|
-
* status but are vendor classes, so no caller can classify them without
|
|
7
|
-
* importing `openai` — and their `message` interpolates the response body.
|
|
8
|
-
*
|
|
9
|
-
* Transport seam: `OpenAIConfig.baseURL` — a loopback server answering
|
|
10
|
-
* with a scripted status/headers/body, so the REAL vendor client runs
|
|
11
|
-
* (including its internal retries, which this group must not disturb).
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { type Server, createServer } from 'node:http'
|
|
15
|
-
import type { AddressInfo } from 'node:net'
|
|
16
|
-
import { afterEach, describe, expect, it } from 'vitest'
|
|
17
|
-
import { OpenAIProvider } from '../client.js'
|
|
18
|
-
|
|
19
|
-
/** Obviously fake, non-functional token — present only as a leak probe. */
|
|
20
|
-
const FAKE_CREDENTIAL = 'sk-ant-FAKE-DO-NOT-USE-0000'
|
|
21
|
-
|
|
22
|
-
interface ScriptedReply {
|
|
23
|
-
status: number
|
|
24
|
-
body: string
|
|
25
|
-
headers?: Record<string, string>
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let server: Server | undefined
|
|
29
|
-
|
|
30
|
-
afterEach(async () => {
|
|
31
|
-
if (server) {
|
|
32
|
-
await new Promise<void>((resolve) => server?.close(() => resolve()))
|
|
33
|
-
server = undefined
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
async function startEndpoint(reply: ScriptedReply): Promise<string> {
|
|
38
|
-
server = createServer((_req, res) => {
|
|
39
|
-
res.writeHead(reply.status, {
|
|
40
|
-
'Content-Type': 'application/json',
|
|
41
|
-
...(reply.headers ?? {}),
|
|
42
|
-
})
|
|
43
|
-
res.end(reply.body)
|
|
44
|
-
})
|
|
45
|
-
await new Promise<void>((resolve) => server?.listen(0, '127.0.0.1', () => resolve()))
|
|
46
|
-
const { port } = server.address() as AddressInfo
|
|
47
|
-
return `http://127.0.0.1:${port}/v1`
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function captureChatStreamError(reply: ScriptedReply): Promise<unknown> {
|
|
51
|
-
const baseURL = await startEndpoint(reply)
|
|
52
|
-
// maxRetries is deliberately NOT set: this group classifies errors and
|
|
53
|
-
// must leave the vendor SDK's own retry behaviour exactly as it is.
|
|
54
|
-
const provider = new OpenAIProvider({
|
|
55
|
-
apiKey: 'test-key',
|
|
56
|
-
model: 'gpt-4o-mini',
|
|
57
|
-
baseURL,
|
|
58
|
-
})
|
|
59
|
-
try {
|
|
60
|
-
for await (const _chunk of provider.chatStream({
|
|
61
|
-
model: 'gpt-4o-mini',
|
|
62
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
63
|
-
})) {
|
|
64
|
-
// drain
|
|
65
|
-
}
|
|
66
|
-
} catch (err) {
|
|
67
|
-
return err
|
|
68
|
-
}
|
|
69
|
-
throw new Error('expected chatStream to throw')
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function captureProviderError(
|
|
73
|
-
provider: OpenAIProvider,
|
|
74
|
-
signal?: AbortSignal,
|
|
75
|
-
): Promise<unknown> {
|
|
76
|
-
try {
|
|
77
|
-
for await (const _chunk of provider.chatStream({
|
|
78
|
-
model: 'gpt-4o-mini',
|
|
79
|
-
messages: [{ role: 'user', content: 'hi' }],
|
|
80
|
-
signal,
|
|
81
|
-
})) {
|
|
82
|
-
// drain
|
|
83
|
-
}
|
|
84
|
-
} catch (err) {
|
|
85
|
-
return err
|
|
86
|
-
}
|
|
87
|
-
throw new Error('expected chatStream to throw')
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
describe('@namzu/openai — provider error taxonomy', () => {
|
|
91
|
-
it('(a) 429 with `retry-after: 2` throws a throttle-classified error carrying retryAfterMs', async () => {
|
|
92
|
-
const err = await captureChatStreamError({
|
|
93
|
-
status: 429,
|
|
94
|
-
headers: { 'retry-after': '2' },
|
|
95
|
-
body: JSON.stringify({
|
|
96
|
-
error: { message: 'Rate limit reached', type: 'rate_limit_error' },
|
|
97
|
-
}),
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
expect(err).toMatchObject({
|
|
101
|
-
name: 'ProviderRequestError',
|
|
102
|
-
kind: 'throttle',
|
|
103
|
-
status: 429,
|
|
104
|
-
retryAfterMs: 2000,
|
|
105
|
-
providerId: 'openai',
|
|
106
|
-
})
|
|
107
|
-
}, 60_000)
|
|
108
|
-
|
|
109
|
-
it("(c) 400 whose body reports a context-length overflow classifies as 'context_overflow'", async () => {
|
|
110
|
-
const err = await captureChatStreamError({
|
|
111
|
-
status: 400,
|
|
112
|
-
body: JSON.stringify({
|
|
113
|
-
error: {
|
|
114
|
-
message:
|
|
115
|
-
"This model's maximum context length is 128000 tokens. However, your messages resulted in 190210 tokens.",
|
|
116
|
-
type: 'invalid_request_error',
|
|
117
|
-
code: 'context_length_exceeded',
|
|
118
|
-
},
|
|
119
|
-
}),
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
expect(err).toMatchObject({
|
|
123
|
-
name: 'ProviderRequestError',
|
|
124
|
-
kind: 'context_overflow',
|
|
125
|
-
status: 400,
|
|
126
|
-
providerId: 'openai',
|
|
127
|
-
})
|
|
128
|
-
}, 30_000)
|
|
129
|
-
|
|
130
|
-
it("(c) 400 that is not an overflow classifies as 'bad_request'", async () => {
|
|
131
|
-
const err = await captureChatStreamError({
|
|
132
|
-
status: 400,
|
|
133
|
-
body: JSON.stringify({
|
|
134
|
-
error: {
|
|
135
|
-
message: "Unrecognized request argument: 'temperture'",
|
|
136
|
-
type: 'invalid_request_error',
|
|
137
|
-
},
|
|
138
|
-
}),
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
expect(err).toMatchObject({
|
|
142
|
-
name: 'ProviderRequestError',
|
|
143
|
-
kind: 'bad_request',
|
|
144
|
-
status: 400,
|
|
145
|
-
providerId: 'openai',
|
|
146
|
-
})
|
|
147
|
-
}, 30_000)
|
|
148
|
-
|
|
149
|
-
it("(auth) 401 classifies as 'auth'", async () => {
|
|
150
|
-
const err = await captureChatStreamError({
|
|
151
|
-
status: 401,
|
|
152
|
-
body: JSON.stringify({
|
|
153
|
-
error: {
|
|
154
|
-
message: 'Incorrect API key provided',
|
|
155
|
-
type: 'invalid_request_error',
|
|
156
|
-
},
|
|
157
|
-
}),
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
expect(err).toMatchObject({
|
|
161
|
-
name: 'ProviderRequestError',
|
|
162
|
-
kind: 'auth',
|
|
163
|
-
status: 401,
|
|
164
|
-
providerId: 'openai',
|
|
165
|
-
})
|
|
166
|
-
}, 30_000)
|
|
167
|
-
|
|
168
|
-
it('(d) SECURITY: a credential echoed back in a 401 body never reaches the thrown message', async () => {
|
|
169
|
-
const err = await captureChatStreamError({
|
|
170
|
-
status: 401,
|
|
171
|
-
body: JSON.stringify({
|
|
172
|
-
error: { message: `Incorrect API key provided: ${FAKE_CREDENTIAL}` },
|
|
173
|
-
}),
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
expect((err as Error).message).not.toContain(FAKE_CREDENTIAL)
|
|
177
|
-
expect(JSON.stringify(err)).not.toContain(FAKE_CREDENTIAL)
|
|
178
|
-
expect('cause' in (err as object)).toBe(false)
|
|
179
|
-
}, 30_000)
|
|
180
|
-
|
|
181
|
-
it('classifies and sanitizes an SDK failure thrown after streaming began', async () => {
|
|
182
|
-
const vendor = Object.assign(new Error(`overloaded_error ${FAKE_CREDENTIAL}`), {
|
|
183
|
-
name: 'APIError',
|
|
184
|
-
})
|
|
185
|
-
const provider = new OpenAIProvider({
|
|
186
|
-
apiKey: 'test-key',
|
|
187
|
-
model: 'gpt-4o-mini',
|
|
188
|
-
})
|
|
189
|
-
const stream = {
|
|
190
|
-
async *[Symbol.asyncIterator]() {
|
|
191
|
-
yield {
|
|
192
|
-
id: 'chatcmpl-1',
|
|
193
|
-
choices: [{ delta: { content: 'partial' }, finish_reason: null }],
|
|
194
|
-
usage: null,
|
|
195
|
-
}
|
|
196
|
-
throw vendor
|
|
197
|
-
},
|
|
198
|
-
}
|
|
199
|
-
;(provider as unknown as { client: unknown }).client = {
|
|
200
|
-
chat: { completions: { create: async () => stream } },
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const err = await captureProviderError(provider)
|
|
204
|
-
|
|
205
|
-
expect(err).toMatchObject({
|
|
206
|
-
name: 'ProviderRequestError',
|
|
207
|
-
kind: 'server',
|
|
208
|
-
providerId: 'openai',
|
|
209
|
-
})
|
|
210
|
-
expect((err as Error).message).not.toContain(FAKE_CREDENTIAL)
|
|
211
|
-
expect('cause' in (err as object)).toBe(false)
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
it('preserves the caller abort reason when the SDK replaces the error object', async () => {
|
|
215
|
-
const controller = new AbortController()
|
|
216
|
-
const reason = new Error('user stopped')
|
|
217
|
-
const sdkAbort = Object.assign(new Error('request aborted'), {
|
|
218
|
-
name: 'APIUserAbortError',
|
|
219
|
-
})
|
|
220
|
-
controller.abort(reason)
|
|
221
|
-
const provider = new OpenAIProvider({
|
|
222
|
-
apiKey: 'test-key',
|
|
223
|
-
model: 'gpt-4o-mini',
|
|
224
|
-
})
|
|
225
|
-
;(provider as unknown as { client: unknown }).client = {
|
|
226
|
-
chat: { completions: { create: async () => Promise.reject(sdkAbort) } },
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const err = await captureProviderError(provider, controller.signal)
|
|
230
|
-
|
|
231
|
-
expect(sdkAbort).not.toBe(reason)
|
|
232
|
-
expect(err).toBe(reason)
|
|
233
|
-
})
|
|
234
|
-
})
|