@mmmbuto/zai-codex-bridge 0.2.0 → 0.2.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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.js +32 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmmbuto/zai-codex-bridge",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Local proxy that translates OpenAI Responses API format to Z.AI Chat Completions format for Codex",
5
5
  "main": "src/server.js",
6
6
  "bin": {
package/src/server.js CHANGED
@@ -247,6 +247,30 @@ function translateChatToResponses(chatResponse) {
247
247
  return response;
248
248
  }
249
249
 
250
+ /**
251
+ * Extract and normalize Bearer token
252
+ */
253
+ function getBearer(raw) {
254
+ if (!raw) return '';
255
+ let t = String(raw).trim();
256
+ if (!t) return '';
257
+ // If already "Bearer xxx" keep it, otherwise add it
258
+ if (!t.toLowerCase().startsWith('bearer ')) t = `Bearer ${t}`;
259
+ return t;
260
+ }
261
+
262
+ /**
263
+ * Pick auth token from env ZAI_API_KEY (priority) or incoming headers
264
+ */
265
+ function pickAuth(incomingHeaders) {
266
+ // PRIORITY: env ZAI_API_KEY (force correct key) -> incoming header
267
+ const envTok = (process.env.ZAI_API_KEY || '').trim();
268
+ if (envTok) return getBearer(envTok);
269
+
270
+ const h = (incomingHeaders['authorization'] || incomingHeaders['Authorization'] || '').trim();
271
+ return getBearer(h);
272
+ }
273
+
250
274
  /**
251
275
  * Make upstream request to Z.AI
252
276
  */
@@ -257,9 +281,10 @@ async function makeUpstreamRequest(path, body, headers) {
257
281
  const cleanPath = path.startsWith('/') ? path.slice(1) : path;
258
282
  const url = new URL(cleanPath, baseUrl);
259
283
 
284
+ const auth = pickAuth(headers);
260
285
  const upstreamHeaders = {
261
286
  'Content-Type': 'application/json',
262
- 'Authorization': headers['authorization'] || headers['Authorization'] || ''
287
+ 'Authorization': auth
263
288
  };
264
289
 
265
290
  log('info', 'Upstream request:', {
@@ -267,7 +292,8 @@ async function makeUpstreamRequest(path, body, headers) {
267
292
  path: path,
268
293
  cleanPath: cleanPath,
269
294
  base: ZAI_BASE_URL,
270
- hasAuth: !!upstreamHeaders.Authorization,
295
+ auth_len: auth.length,
296
+ auth_prefix: auth.slice(0, 14), // "Bearer xxxxxx"
271
297
  bodyKeys: Object.keys(body),
272
298
  bodyPreview: JSON.stringify(body).substring(0, 800),
273
299
  messagesCount: body.messages?.length || 0,
@@ -466,15 +492,16 @@ async function handlePostRequest(req, res) {
466
492
 
467
493
  if (!upstreamResponse.ok) {
468
494
  const errorBody = await upstreamResponse.text();
495
+ const status = upstreamResponse.status;
469
496
  log('error', 'Upstream error:', {
470
- status: upstreamResponse.status,
497
+ status: status,
471
498
  body: errorBody.substring(0, 200)
472
499
  });
473
500
 
474
- res.writeHead(502, { 'Content-Type': 'application/json' });
501
+ res.writeHead(status, { 'Content-Type': 'application/json' });
475
502
  res.end(JSON.stringify({
476
503
  error: 'Upstream request failed',
477
- upstream_status: upstreamResponse.status,
504
+ upstream_status: status,
478
505
  upstream_body: errorBody
479
506
  }));
480
507
  return;