@stage-labs/metro 0.1.0-beta.149 → 0.1.0-beta.150

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.149",
3
+ "version": "0.1.0-beta.150",
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.149",
3
+ "version": "0.1.0-beta.150",
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",
@@ -5,7 +5,9 @@ import { GeminiAuthError, type GeminiTokens } from './gemini-auth.js';
5
5
  export const CODE_ASSIST_BASE = 'https://cloudcode-pa.googleapis.com';
6
6
  const API = 'v1internal';
7
7
  const METADATA = { ideType: 'IDE_UNSPECIFIED', platform: 'PLATFORM_UNSPECIFIED', pluginType: 'GEMINI' };
8
+ const CLI_VERSION = '0.60.0';
8
9
  const FREE_TIER = 'free-tier';
10
+ const PROJECT_RE = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/;
9
11
  const POLL_MS = 5000;
10
12
  const POLL_MAX = 24;
11
13
 
@@ -16,12 +18,23 @@ export interface Onboarded {
16
18
 
17
19
  const text = (value: unknown): string | null => (typeof value === 'string' && value !== '' ? value : null);
18
20
 
21
+ export const userAgent = (model: string): string => `GeminiCLI/${CLI_VERSION}/${model} (${process.platform}; ${process.arch}; terminal)`;
22
+
23
+ export function parseGeminiProject(value: unknown): string | null {
24
+ const project = typeof value === 'string' ? value.trim() : '';
25
+ if (project === '') return null;
26
+ if (!PROJECT_RE.test(project)) throw new GeminiAuthError('that is not a Google Cloud project id: 6 to 30 lowercase letters, digits and dashes, starting with a letter');
27
+ return project;
28
+ }
29
+
30
+ const metadataFor = (project: string | null): Record<string, string> => (project === null ? METADATA : { ...METADATA, duetProject: project });
31
+
19
32
  async function call(base: string, method: string, token: string, body: unknown, fetchImpl: typeof fetch): Promise<Record<string, unknown>> {
20
33
  let res: Response;
21
34
  try {
22
35
  res = await fetchImpl(`${base}/${API}:${method}`, {
23
36
  method: 'POST',
24
- headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
37
+ headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json', 'user-agent': userAgent('auto') },
25
38
  body: JSON.stringify(body),
26
39
  signal: AbortSignal.timeout(30_000),
27
40
  });
@@ -62,6 +75,7 @@ const REASONS: Record<string, string> = {
62
75
  UNKNOWN_LOCATION: 'Google could not tell where this account is located',
63
76
  RESTRICTED_NETWORK: 'the network this box is on is restricted',
64
77
  INELIGIBLE_ACCOUNT: 'Google marks this account as not eligible',
78
+ UNSUPPORTED_CLIENT: 'Google closed Gemini CLI sign-in for personal accounts on 2026-06-18; a Gemini Code Assist Standard or Enterprise licence on a Google Cloud project is what still works, enter that project id',
65
79
  };
66
80
 
67
81
  function refuseIneligible(load: Record<string, unknown>): void {
@@ -74,8 +88,7 @@ function refuseIneligible(load: Record<string, unknown>): void {
74
88
  throw new GeminiAuthError(said.join('; '));
75
89
  }
76
90
 
77
- const MANAGED =
78
- 'Google treats this account as a managed (Workspace) one: it has a Code Assist tier but no project of its own, and the individual tier is for personal Google accounts (gmail.com). Sign in with the personal account that holds your Google AI plan';
91
+ const NO_PROJECT = 'this account holds a Code Assist licence but Google named no project for it: enter the id of the Google Cloud project that carries the licence and connect again';
79
92
 
80
93
  function tierToOnboard(load: Record<string, unknown>): { id: string; name: string | null } {
81
94
  const allowed = Array.isArray(load.allowedTiers) ? load.allowedTiers.filter(isRecord) : [];
@@ -91,18 +104,36 @@ const summary = (load: Record<string, unknown>): Record<string, unknown> => ({
91
104
  project: text(load.cloudaicompanionProject),
92
105
  });
93
106
 
94
- export async function onboard(tokens: GeminiTokens, base = CODE_ASSIST_BASE, fetchImpl: typeof fetch = fetch): Promise<Onboarded> {
95
- const load = await call(base, 'loadCodeAssist', tokens.accessToken, { metadata: METADATA }, fetchImpl);
107
+ function onboardBody(load: Record<string, unknown>, project: string | null): { body: Record<string, unknown>; name: string | null } {
108
+ const wanted = tierToOnboard(load);
109
+ const paid = wanted.id !== FREE_TIER && project !== null;
110
+ return { body: { tierId: wanted.id, metadata: metadataFor(paid ? project : null), ...withProject(paid ? project : null) }, name: wanted.name ?? wanted.id };
111
+ }
112
+
113
+ const NO_ASSIGNMENT = 'Google did not assign a Code Assist project to this account; enter the id of the Google Cloud project that carries your licence and connect again';
114
+
115
+ const withProject = (project: string | null): Record<string, unknown> => (project === null ? {} : { cloudaicompanionProject: project });
116
+
117
+ const assignedProject = (op: Record<string, unknown>): string | null => {
118
+ const response = isRecord(op.response) ? op.response : {};
119
+ return isRecord(response.cloudaicompanionProject) ? text(response.cloudaicompanionProject.id) : null;
120
+ };
121
+
122
+ async function onboardNew(load: Record<string, unknown>, tokens: GeminiTokens, project: string | null, base: string, fetchImpl: typeof fetch): Promise<Onboarded> {
123
+ const { body, name } = onboardBody(load, project);
124
+ const op = await waitOperation(base, tokens.accessToken, await call(base, 'onboardUser', tokens.accessToken, body, fetchImpl), fetchImpl);
125
+ const assigned = assignedProject(op) ?? project;
126
+ if (assigned === null) throw new GeminiAuthError(NO_ASSIGNMENT);
127
+ return { project: assigned, tier: name };
128
+ }
129
+
130
+ export async function onboard(tokens: GeminiTokens, project: string | null = null, base = CODE_ASSIST_BASE, fetchImpl: typeof fetch = fetch): Promise<Onboarded> {
131
+ const load = await call(base, 'loadCodeAssist', tokens.accessToken, { metadata: metadataFor(project), ...withProject(project) }, fetchImpl);
96
132
  log.info(summary(load), 'gemini: Code Assist answered loadCodeAssist');
97
133
  refuseIneligible(load);
134
+ if (!isRecord(load.currentTier)) return onboardNew(load, tokens, project, base, fetchImpl);
135
+ const known = text(load.cloudaicompanionProject) ?? project;
136
+ if (known === null) throw new GeminiAuthError(NO_PROJECT);
98
137
  const tier = tierOf(load);
99
- const known = text(load.cloudaicompanionProject);
100
- if (known !== null) return { project: known, tier: tier.name ?? tier.id };
101
- if (isRecord(load.currentTier)) throw new GeminiAuthError(MANAGED);
102
- const wanted = tierToOnboard(load);
103
- const op = await waitOperation(base, tokens.accessToken, await call(base, 'onboardUser', tokens.accessToken, { tierId: wanted.id, metadata: METADATA }, fetchImpl), fetchImpl);
104
- const response = isRecord(op.response) ? op.response : {};
105
- const project = isRecord(response.cloudaicompanionProject) ? text(response.cloudaicompanionProject.id) : null;
106
- if (project === null) throw new GeminiAuthError('Google did not assign a Code Assist project to this account; sign in to https://geminicli.com once with the Gemini CLI, then try again');
107
- return { project, tier: wanted.name ?? wanted.id };
138
+ return { project: known, tier: tier.name ?? tier.id };
108
139
  }
@@ -2,7 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
2
2
  import { isRecord } from '@metro-labs/core/is-record';
3
3
  import { errMsg } from '@metro-labs/core/log';
4
4
  import { refreshTokens, tokensStale, type GeminiTokens } from './gemini-auth.js';
5
- import { CODE_ASSIST_BASE } from './gemini-setup.js';
5
+ import { CODE_ASSIST_BASE, userAgent } from './gemini-setup.js';
6
6
  import { GeminiStreamTranslator } from './gemini-stream.js';
7
7
  import { toGeminiRequest } from './gemini-translate.js';
8
8
  import { ToolNames } from './codex-translate.js';
@@ -12,7 +12,6 @@ import type { ModelConfig } from './model-config.js';
12
12
  import { UsageScanner } from './usage.js';
13
13
 
14
14
  const PING_MS = 25_000;
15
- const CLI_VERSION = '0.62.0';
16
15
  const STATUS_OF: Record<string, number> = { rate_limit_error: 429, invalid_request_error: 400, permission_error: 403, overloaded_error: 529 };
17
16
 
18
17
  export const KNOWN_GEMINI_MODELS = ['gemini-3.1-pro-preview', 'gemini-3-pro-preview', 'gemini-3-flash-preview', 'gemini-3.1-flash-lite', 'gemini-2.5-pro', 'gemini-2.5-flash'];
@@ -31,8 +30,6 @@ export interface GeminiState {
31
30
 
32
31
  export const freshGeminiState = (): GeminiState => ({ refreshing: null, latest: null });
33
32
 
34
- export const userAgent = (model: string): string => `GeminiCLI/${CLI_VERSION}/${model} (${process.platform}; ${process.arch}; metro)`;
35
-
36
33
  const newerThan = (a: GeminiTokens, b: GeminiTokens): boolean => Date.parse(a.savedAt) > Date.parse(b.savedAt);
37
34
 
38
35
  function refreshed(tokens: GeminiTokens, deps: GeminiDeps, state: GeminiState): Promise<GeminiTokens> {
@@ -7,7 +7,7 @@ import { beginLogin, CodexAuthError, finishLogin, readCodexCliAuth } from './cod
7
7
  import { beginDeviceLogin, pollDeviceLogin } from './codex-device.js';
8
8
  import { codexModels, currentTokens, freshCodexState } from './codex.js';
9
9
  import { beginLogin as beginGeminiLogin, exchangeCode as exchangeGeminiCode, GeminiAuthError, userEmail } from './gemini-auth.js';
10
- import { onboard } from './gemini-setup.js';
10
+ import { onboard, parseGeminiProject } from './gemini-setup.js';
11
11
  import { KNOWN_GEMINI_MODELS } from './gemini.js';
12
12
  import { openrouterCredits, openrouterModels, openrouterZdrModels } from './openrouter.js';
13
13
  import { anthropicModels, bedrockModels } from './provider-models.js';
@@ -181,13 +181,22 @@ const CODEX_ROUTES: Record<string, Route> = {
181
181
  },
182
182
  };
183
183
 
184
+ function projectOf(body: unknown): string | null {
185
+ try {
186
+ return parseGeminiProject(isRecord(body) ? body.project : null);
187
+ } catch (err) {
188
+ return asApiError(err);
189
+ }
190
+ }
191
+
184
192
  async function connectGemini(req: IncomingMessage, deps: ModelApiDeps, store: Store): Promise<unknown> {
185
193
  const body = await readJsonBody(req, BODY_MAX);
186
194
  const code = isRecord(body) && typeof body.code === 'string' ? body.code : '';
187
195
  const state = isRecord(body) && typeof body.state === 'string' ? body.state : '';
196
+ const project = projectOf(body);
188
197
  const tokens = await exchangeGeminiCode(code, state, deps.geminiTokenBase, deps.fetchImpl).catch(asApiError);
189
198
  const email = await userEmail(tokens, deps.geminiUserBase, deps.fetchImpl);
190
- const onboarded = await onboard(tokens, deps.geminiBase, deps.fetchImpl).catch(asApiError);
199
+ const onboarded = await onboard(tokens, project, deps.geminiBase, deps.fetchImpl).catch(asApiError);
191
200
  const cfg = setGeminiAuth(store.read(), { ...tokens, email, project: onboarded.project, tier: onboarded.tier });
192
201
  store.write(cfg);
193
202
  log.info({ tier: onboarded.tier }, 'model-api: Gemini connected');
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.149"
2
+ "version": "0.1.0-beta.150"
3
3
  }