Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
2.9 kB · 64 lines
JavaScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#!/usr/bin/env node// Turn a browser HAR export of the Alibaba Model Studio console into the session file the// `alibaba` usage feed reads.//// Token-plan API keys (sk-sp-*) are inference-only: every quota path rejects them. Quota// lives behind the console gateway, which wants the console session — cookies plus the// per-page `sec_token` — so the session comes from a logged-in browser instead of a key.// The session expires the way any console session does; re-run this after logging in again.//// node scripts/capture-alibaba-session.mjs ~/.alibaba.har// # writes ~/.alibaba-session.json (0600)//// Nothing is printed but the shape: cookies, the CSRF token, and the request envelope stay// in the file.import { readFileSync, writeFileSync } from "node:fs";import { homedir } from "node:os";import { join } from "node:path";
const harPath = process.argv[2] ?? join(homedir(), ".alibaba.har");const outPath = process.argv[3] ?? join(homedir(), ".alibaba-session.json");const API = "zeldaHttp.apikeyMgr./tokenplan/personal/api/v2/usage";
const har = JSON.parse(readFileSync(harPath, "utf8"));const entry = har.log.entries.find( (e) => typeof e.request?.url === "string" && e.request.url.includes(API.replace("usage", "")) && e.request.method === "POST",);if (!entry) { console.error(`no token-plan console call found in ${harPath}; open the console's token plan page with devtools recording, then export the HAR`); process.exit(1);}
const headers = Object.fromEntries( entry.request.headers .filter((h) => !h.name.startsWith(":")) .filter((h) => ["cookie", "origin", "referer", "user-agent", "content-type", "accept", "accept-language"].includes(h.name.toLowerCase())) .map((h) => [h.name.toLowerCase(), h.value]),);const form = new URLSearchParams(entry.request.postData?.text ?? "");const secToken = form.get("sec_token");const region = form.get("region") ?? new URL(entry.request.url).searchParams.get("region") ?? "ap-southeast-1";if (!headers.cookie) { console.error("this HAR has no cookies: re-export with sensitive data (Chrome: 'Copy all as HAR (with sensitive data)'; Safari's inspector strips them)"); process.exit(1);}if (!secToken) { console.error("no sec_token in the captured request body; capture a fresh token-plan page load"); process.exit(1);}
// The gateway is per region: bailian-<region>-cs for international, bailian-cs for the// mainland console.const host = new URL(entry.request.url).host;const url = `https://${host}/data/api.json`;
writeFileSync( outPath, `${JSON.stringify({ url, region, secToken, headers, capturedAt: new Date().toISOString(), source: harPath }, null, 2)}\n`, { mode: 0o600 },);console.log(`wrote ${outPath}`);console.log(` host: ${host}`);console.log(` region: ${region}`);console.log(` cookies: ${headers.cookie.split(";").length}`);console.log(` sec_token: present (${secToken.length} chars)`);