Files
plugins/packages/gcp-ai/lib/fetch_credits.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

90 lines
5.3 KiB
JavaScript

#!/usr/bin/env node
import { readFile } from "node:fs/promises"
import { pathToFileURL } from "node:url"
import { closeKeptBrowsers, defaultProfile, openBrowser, routeAll } from "./browser.mjs"
import { accountOf, resolveIdentity, startLogin, tokenProvider } from "./credentials.mjs"
import { fetchCredits, parseCreditTables, readTables } from "./credits.mjs"
import { testEndpoint, summarize } from "./endpoint.mjs"
import { availableModels } from "./models.mjs"
export async function main(args = process.argv.slice(2)) {
const command = args[0] ?? "credits"
if (command === "help" || args.includes("--help")) {
console.log("Usage: node lib/fetch_credits.mjs credits|credentials|login|models|endpoint|parse-html [--project ID] [--billing-account ID] [--profile PATH] [--cloudsdk-config DIR] [--force] [--headed] [--account EMAIL] [--auth-mode auto|gcloud|adc] [--location REGION] [--model ID] [--json] [--file HTML]")
return
}
if (!["credits", "credentials", "login", "models", "endpoint", "parse-html"].includes(command)) throw new Error(`Unknown command: ${command}`)
const valued = new Set(["--project", "--billing-account", "--profile", "--cloudsdk-config", "--account", "--auth-mode", "--file", "--location", "--model"])
for (let i = 1; i < args.length; i++) {
const option = args[i]
if (valued.has(option)) {
if (!args[i + 1] || args[i + 1].startsWith("--")) throw new Error(`${option} requires a value`)
i++
} else if (!["--force", "--headed", "--json"].includes(option)) throw new Error(`Unknown option: ${option}`)
}
const value = (name) => {
const i = args.indexOf(name)
if (i < 0) return undefined
if (!args[i + 1] || args[i + 1].startsWith("--")) throw new Error(`${name} requires a value`)
return args[i + 1]
}
const options = {
project: value("--project") ?? process.env.GOOGLE_VERTEX_PROJECT ?? process.env.GOOGLE_CLOUD_PROJECT,
billingAccount: value("--billing-account") ?? process.env.VERTEX_BILLING_ACCOUNT,
profile: value("--profile") ?? process.env.VERTEX_CHROME_PROFILE ?? defaultProfile(),
account: value("--account") ?? process.env.VERTEX_GCLOUD_ACCOUNT,
authMode: value("--auth-mode") ?? process.env.VERTEX_GCLOUD_AUTH_MODE,
// Which ADC a command reads: a directory named here, else the plugin's own
// for the account, else gcloud's default. The machine-wide CLOUDSDK_CONFIG
// is deliberately not read: one variable would point every account at one
// credential, and this plugin writes only its own directories.
cloudsdkConfig: value("--cloudsdk-config"),
location: value("--location") ?? process.env.GOOGLE_VERTEX_LOCATION ?? "global",
force: args.includes("--force"), headless: !args.includes("--headed"),
}
if (command === "credits") {
try {
console.log(JSON.stringify(await fetchCredits(options), null, 2))
} finally { await closeKeptBrowsers() }
} else if (command === "credentials") {
const identity = await resolveIdentity(options)
const token = await tokenProvider(identity)()
const account = identity.account ?? (identity.authMode === "adc" ? await accountOf(token) : undefined)
console.log(JSON.stringify({ ...identity, ...(account ? { account } : {}), inferenceAuth: "token-verified" }, null, 2))
} else if (command === "login") {
console.error("Opening Chrome. If no Application Default Credentials exist yet, approving Google there also has gcloud write them.")
const login = await startLogin(options)
try {
console.log(JSON.stringify({ ...await login.result, inferenceAuth: "token-verified" }, null, 2))
} finally { await login.close() }
} else if (command === "models") {
console.log(JSON.stringify(await availableModels({ ...options, force: options.force }), null, 2))
} else if (command === "endpoint") {
// The models the row offers are what a real endpoint test should ask
// about; --model narrows it to one when a single answer is wanted.
const chosen = value("--model")
const models = chosen ? [chosen] : (await availableModels(options).catch(() => ({ ids: [] }))).ids
const report = await testEndpoint(options, { models })
console.log(args.includes("--json") ? JSON.stringify(report, null, 2) : summarize(report))
if (!report.reachable) process.exitCode = 1
} else if (command === "parse-html") {
const file = value("--file")
if (!file) throw new Error("parse-html requires --file PATH")
const browser = await openBrowser({ profile: options.profile, headless: true, javaScriptEnabled: false })
try {
// The file is data, never code: scripts are stripped before it is parsed,
// and every request it would make is refused.
await routeAll(browser.page, () => undefined)
const html = (await readFile(file, "utf8")).replace(/<script\b[^>]*>[\s\S]*?<\/script\s*>/gi, "")
const encoded = Buffer.from(html).toString("base64")
await browser.page.send("Page.navigate", { url: `data:text/html;base64,${encoded}` })
await browser.page.waitForFunction(() => document.readyState !== "loading")
console.log(JSON.stringify(parseCreditTables(await readTables(browser.page)), null, 2))
} finally { await browser.close() }
}
}
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
main().catch((error) => { console.error(JSON.stringify({ error: error.message })); process.exitCode = 1 })
}