Files
plugins/packages/gcp-ai/lib/endpoint.mjs
T
Yaka e801e434f3 gcp-ai: Vertex models and Google Cloud credits with your own gcloud ADC (#307)
New package gcp-ai (Yaka88): Vertex models and Google Cloud credits through the user's own gcloud ADC, with multiple accounts, a signed-in model list, and a pipe-only Chrome for the credits read.

Reviewed at 8989b58 against the 10-24 review at 4586c99. I read the whole incremental diff (index.mjs, lib/browser, lib/credentials, lib/lock, test/*). Earlier rounds had already reviewed the rest.
- Tests reaching the network: fixed. test/network.mjs replaces fetch and blocks non-local net/tls/http/dns. `bun test` (40 pass, 1 skip [Linux-only lock test], 0 fail, 106s) ran in a sandbox HOME/TMPDIR behind a dead HTTPS_PROXY, with a preload that logged non-local fetch and connect calls: 0 lines logged. Broken by hand: with the fetch stub removed from "already filed", the guard prints "Unexpected network fetch … userinfo" and bun exits 1. Restored, it exits 0.
- Chrome background traffic: lsof on the test Chrome processes (56 samples during browser.test.mjs, 5 pass) found no inet sockets at all.
- Leftover work after a test: close() now awaits settled, and every sign-in test closes in finally before restoring fetch.
- Smaller points: the lock's age ceiling now comes after the Linux /proc check; settings() is auth > options > env for every field; resolveAdcDirectory and authorizeAdc are removed.
- `bun scripts/check.mjs gcp-ai`: ok. The merge onto main 98985a2 is clean. Loaded into magpie's plugin host (origin/main 4dfd88206) with scripts/try.sh in a sandbox HOME: gcp-ai, 12 models, not signed in.

Not tried: a real Google account or gcloud sign-in, Windows, Linux (the lock test is skipped on macOS). The headless Console credits scrape still runs for every signed-in account, cached on failure, as noted in the earlier review.
2026-10-25 14:33:15 +08:00

92 lines
4.6 KiB
JavaScript

import { resolveIdentity, tokenProvider } from "./credentials.mjs"
// This is the check the official Vertex provider runs: the project's location
// resource. It answers whether the endpoint, the project, the location and the
// caller's permission are all right, without spending a model request.
const API = "https://aiplatform.googleapis.com/v1"
export function locationUrl(project, location) {
return `${API}/projects/${project}/locations/${location}`
}
export async function checkLocation(token, project, location, timeout = 15000) {
const started = Date.now()
const response = await fetch(locationUrl(project, location), {
headers: { authorization: `Bearer ${token}`, "x-goog-user-project": project },
signal: AbortSignal.timeout(timeout),
})
const body = await response.json().catch(() => undefined)
return {
ok: response.ok, status: response.status, ms: Date.now() - started,
...(body?.locationId ? { locationId: body.locationId } : {}),
...(body?.error?.message ? { error: String(body.error.message).split("\n")[0] } : {}),
}
}
// A model probe answers what the location test cannot: which models this
// project may actually call. countTokens is free and is the cheapest call
// Vertex offers.
export async function checkModel(token, project, location, model, timeout = 20000) {
const started = Date.now()
const response = await fetch(
`${API}/projects/${project}/locations/${location}/publishers/google/models/${encodeURIComponent(model)}:countTokens`,
{
method: "POST",
headers: { authorization: `Bearer ${token}`, "x-goog-user-project": project, "content-type": "application/json" },
body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "hi" }] }] }),
signal: AbortSignal.timeout(timeout),
},
)
const body = await response.json().catch(() => undefined)
return {
model, ok: response.ok, status: response.status, ms: Date.now() - started,
...(body?.totalTokens !== undefined ? { tokens: body.totalTokens } : {}),
...(body?.error?.message ? { error: String(body.error.message).split("\n")[0] } : {}),
}
}
// A location that answers, or refuses for a reason about the project or the
// model, proves the endpoint is reachable. Only a rejected credential, a quota
// block or a server failure means it cannot be used at all.
const UNUSABLE = new Set([401, 429, 500, 502, 503, 504])
export async function testEndpoint(settings = {}, { models = [], timeout = 20000 } = {}) {
const identity = await resolveIdentity(settings)
const location = settings.location || "global"
const token = await tokenProvider(identity)()
const where = { project: identity.project, location, ...(identity.account ? { account: identity.account } : {}) }
let locationTest
try { locationTest = await checkLocation(token, identity.project, location) }
catch (error) { locationTest = { ok: false, ms: 0, error: error.name === "TimeoutError" ? "no answer in time" : error.message } }
if (!locationTest.ok) {
return { ...where, authMode: identity.authMode, reachable: !UNUSABLE.has(locationTest.status), locationTest, models: [] }
}
const candidates = models.length ? models : ["gemini-2.5-flash"]
const probes = []
for (const model of candidates) {
try { probes.push(await checkModel(token, identity.project, location, model.replace(/^google\//, ""), timeout)) }
catch (error) { probes.push({ model, ok: false, ms: 0, error: error.name === "TimeoutError" ? "no answer in time" : error.message }) }
}
return {
...where, authMode: identity.authMode, reachable: true, locationTest, models: probes,
served: probes.filter((p) => p.ok).map((p) => p.model),
}
}
export function summarize(report) {
const line = report.locationTest
? `Location ${report.locationTest.ok ? "ok" : `failed (${report.locationTest.status ?? "no answer"})`} · ${report.locationTest.ms}ms${report.locationTest.error ? `\n ${report.locationTest.error}` : ""}`
: "Location not tested"
const lines = [
`Endpoint ${report.reachable ? "reachable" : "UNREACHABLE"}: ${locationUrl(report.project, report.location)}`,
`Project ${report.project} · ${report.location}${report.account ? ` · ${report.account}` : ""}`,
line,
]
for (const probe of report.models) {
const verdict = probe.ok ? "ok" : probe.status ? `failed (${probe.status})` : "failed"
lines.push(` ${probe.model}: ${verdict} · ${probe.ms}ms${probe.tokens !== undefined ? ` · ${probe.tokens} tokens` : ""}${probe.error ? `\n ${probe.error}` : ""}`)
}
if (report.models.length) lines.push(report.served.length ? `${report.served.length} of ${report.models.length} models answered` : "No model answered")
return lines.join("\n")
}