@stage-labs/metro 0.1.0-beta.160 → 0.1.0-beta.161

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stage-labs/metro",
3
- "version": "0.1.0-beta.160",
3
+ "version": "0.1.0-beta.161",
4
4
  "description": "The metro command line. Sign in once per machine, then hand your MCP connector list to Claude Code without the credentials touching disk, argv or shell history.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro",
3
- "version": "0.1.0-beta.160",
3
+ "version": "0.1.0-beta.161",
4
4
  "description": "Metro for Claude Code, installed by the metro daemon on this machine: every connector the agent holds as its own MCP server relayed through the daemon, the orchestrator-only guard on the main thread, and the standing rules loaded at each session start. No vendor credential ever sits in a config file.",
5
5
  "author": {
6
6
  "name": "Bonustrack",
@@ -1,6 +1,6 @@
1
1
  import type { IncomingMessage, ServerResponse } from 'node:http';
2
2
  import { isRecord } from '@metro-labs/core/is-record';
3
- import { errMsg } from '@metro-labs/core/log';
3
+ import { errMsg, log } from '@metro-labs/core/log';
4
4
  import { refreshTokens, tokensStale, type GeminiTokens } from './gemini-auth.js';
5
5
  import { API, clientHeaders, generateBases, machineSessionId, type Bases } from './gemini-client.js';
6
6
  import { GeminiStreamTranslator } from './gemini-stream.js';
@@ -109,12 +109,37 @@ async function send(call: Call, tokens: GeminiTokens, stream: boolean): Promise<
109
109
  if (base === bases.at(-1) || call.watch.signal.aborted) throw err;
110
110
  continue;
111
111
  }
112
- if (last.status < 500) return last;
112
+ if (last.status < 500 && last.status !== 429) return last;
113
113
  }
114
114
  if (last === null) throw new GatewayError(502, 'api_error', 'no Code Assist endpoint to call');
115
115
  return last;
116
116
  }
117
117
 
118
+ const REFUSAL_LOG_MAX = 2000;
119
+
120
+ const detailText = (detail: Record<string, unknown>): string[] => {
121
+ const out: string[] = [];
122
+ if (typeof detail.reason === 'string') out.push(detail.reason);
123
+ if (isRecord(detail.metadata)) out.push(...Object.entries(detail.metadata).map(([k, v]) => `${k}=${String(v)}`));
124
+ if (typeof detail.retryDelay === 'string') out.push(`retry after ${detail.retryDelay}`);
125
+ if (Array.isArray(detail.violations))
126
+ out.push(...detail.violations.filter(isRecord).map((v) => [v.subject, v.description].filter((x) => typeof x === 'string').join(': ')));
127
+ return out.filter((x) => x !== '');
128
+ };
129
+
130
+ export function refusalMessage(text: string, status: number): string {
131
+ const message = upstreamMessage(text, `Gemini answered ${String(status)}`);
132
+ let details: unknown = null;
133
+ try {
134
+ const parsed: unknown = JSON.parse(text);
135
+ details = isRecord(parsed) && isRecord(parsed.error) ? parsed.error.details : null;
136
+ } catch {
137
+ return message;
138
+ }
139
+ const notes = Array.isArray(details) ? details.filter(isRecord).flatMap(detailText) : [];
140
+ return notes.length === 0 ? message : `${message} (${notes.join('; ')})`;
141
+ }
142
+
118
143
  async function reach(call: Call, cfg: ModelConfig, state: GeminiState, stream: boolean): Promise<Response> {
119
144
  let tokens = await currentGeminiTokens(cfg, call.deps, state);
120
145
  let upstream = await send(call, tokens, stream);
@@ -244,7 +269,8 @@ export async function geminiMessages(
244
269
  const upstream = await reach(call, cfg, state, stream);
245
270
  if (!upstream.ok) {
246
271
  const text = await upstream.text();
247
- sendError(res, providerStatus(upstream.status), errorKind(upstream.status), upstreamMessage(text, `Gemini answered ${String(upstream.status)}`));
272
+ log.warn({ provider: 'gemini', model, status: upstream.status, body: text.slice(0, REFUSAL_LOG_MAX) }, 'gateway: the provider refused the request');
273
+ sendError(res, providerStatus(upstream.status), errorKind(upstream.status), refusalMessage(text, upstream.status));
248
274
  return;
249
275
  }
250
276
  if (stream) await relayStream(upstream, res, call);
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.160"
2
+ "version": "0.1.0-beta.161"
3
3
  }