Support Qoder CN (#4)
* feat(qoder-cn): add region endpoints and model catalog * feat(qoder-cn): add separate provider auth and streaming * docs: document Qoder CN provider
This commit is contained in:
+30
-14
@@ -2,7 +2,7 @@ import { existsSync, readFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
||||
import { getMachineId } from "./cosy.js";
|
||||
import { getMachineId, getQoderCNPat, getQoderMode, getQoderRefreshURL, isQoderCNMode } from "./cosy.js";
|
||||
import { interactiveLogin } from "./login.js";
|
||||
import { updateQoderModelsCache } from "./models.js";
|
||||
import { credentialsFromPat, decodePatRefresh, isPatRefresh } from "./pat.js";
|
||||
@@ -24,11 +24,11 @@ const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json");
|
||||
* Note: the auth.json path/shape is a pi internal convention, not a public API.
|
||||
* This is best-effort and falls back to null so callers can use placeholders.
|
||||
*/
|
||||
export function getCachedCredentials(_accessToken: string): QoderCredentials | null {
|
||||
export function getCachedCredentials(_accessToken: string, providerID = "qoder"): QoderCredentials | null {
|
||||
if (existsSync(AUTH_FILE)) {
|
||||
try {
|
||||
const auth = JSON.parse(readFileSync(AUTH_FILE, "utf-8"));
|
||||
const creds = auth?.qoder;
|
||||
const creds = auth?.[providerID] || (providerID === "qoder" ? auth?.qoder : null);
|
||||
if (creds?.userID) {
|
||||
return creds as QoderCredentials;
|
||||
}
|
||||
@@ -37,45 +37,61 @@ export function getCachedCredentials(_accessToken: string): QoderCredentials | n
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
||||
async function loginQoderForMode(callbacks: OAuthLoginCallbacks, mode: string): Promise<OAuthCredentials> {
|
||||
// 1. Try environment variables first (PAT). A PAT (pt-...) must be exchanged
|
||||
// for a short-lived job token before it can be used — credentialsFromPat
|
||||
// handles the exchange + identity resolution.
|
||||
const pat = process.env.QODER_PERSONAL_ACCESS_TOKEN || process.env.QODER_PAT;
|
||||
const pat = isQoderCNMode(mode) ? getQoderCNPat() : process.env.QODER_PERSONAL_ACCESS_TOKEN || process.env.QODER_PAT;
|
||||
if (pat) {
|
||||
try {
|
||||
const creds = await credentialsFromPat(pat);
|
||||
const creds = await credentialsFromPat(pat, mode);
|
||||
const qCreds = creds as QoderCredentials;
|
||||
// pi persists these credentials in auth.json itself; no separate cache needed.
|
||||
// Cache models in background
|
||||
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email).catch(() => {});
|
||||
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email, mode).catch(() => {});
|
||||
return creds;
|
||||
} catch {
|
||||
// Fall through to interactive login if PAT exchange fails.
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Interactive login
|
||||
const creds = await interactiveLogin(callbacks);
|
||||
// 2. Interactive login (CN only supports PAT prompt here; global supports device flow fallback)
|
||||
const creds = await interactiveLogin(callbacks, mode);
|
||||
|
||||
// Cache models in background. pi persists the credentials in auth.json itself.
|
||||
try {
|
||||
const qCreds = creds as QoderCredentials;
|
||||
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email).catch(() => {});
|
||||
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email, mode).catch(() => {});
|
||||
} catch {}
|
||||
|
||||
return creds;
|
||||
}
|
||||
|
||||
export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
||||
return loginQoderForMode(callbacks, getQoderMode());
|
||||
}
|
||||
|
||||
export async function loginQoderCN(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
||||
return loginQoderForMode(callbacks, "cn");
|
||||
}
|
||||
|
||||
export async function refreshQoderToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
||||
return refreshQoderTokenForMode(credentials, getQoderMode());
|
||||
}
|
||||
|
||||
export async function refreshQoderTokenCN(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
||||
return refreshQoderTokenForMode(credentials, "cn");
|
||||
}
|
||||
|
||||
async function refreshQoderTokenForMode(credentials: OAuthCredentials, mode: string): Promise<OAuthCredentials> {
|
||||
// PAT-based credentials: re-exchange the stored PAT for a fresh job token.
|
||||
if (isPatRefresh(credentials.refresh)) {
|
||||
const { pat } = decodePatRefresh(credentials.refresh);
|
||||
if (pat) {
|
||||
try {
|
||||
const refreshed = await credentialsFromPat(pat);
|
||||
const refreshed = await credentialsFromPat(pat, mode);
|
||||
const qCreds = refreshed as QoderCredentials;
|
||||
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email).catch(() => {});
|
||||
updateQoderModelsCache(qCreds.access, qCreds.userID, qCreds.name, qCreds.email, mode).catch(() => {});
|
||||
return refreshed;
|
||||
} catch {
|
||||
// Fall through to validity extension below.
|
||||
@@ -95,7 +111,7 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise<
|
||||
const prevName = prev.name || "";
|
||||
const prevEmail = prev.email || "";
|
||||
|
||||
const refreshURL = "https://center.qoder.sh/algo/api/v3/user/refresh_token";
|
||||
const refreshURL = getQoderRefreshURL(mode);
|
||||
try {
|
||||
const response = await fetch(refreshURL, {
|
||||
method: "POST",
|
||||
@@ -140,7 +156,7 @@ export async function refreshQoderToken(credentials: OAuthCredentials): Promise<
|
||||
|
||||
// pi persists the refreshed credentials in auth.json itself.
|
||||
// Cache models in background
|
||||
updateQoderModelsCache(newAccess, userID, prevName, prevEmail).catch(() => {});
|
||||
updateQoderModelsCache(newAccess, userID, prevName, prevEmail, mode).catch(() => {});
|
||||
|
||||
return refreshed;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user