Bind Qoder providers to fixed regions
Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
This commit is contained in:
co-authored by
Simon Shi
parent
b1d66d6eb4
commit
66f950f9c5
@@ -0,0 +1,80 @@
|
|||||||
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const originalRegion = process.env.QODER_REGION;
|
||||||
|
const patEnvNames = [
|
||||||
|
"QODER_API_KEY",
|
||||||
|
"QODER_PERSONAL_ACCESS_TOKEN",
|
||||||
|
"QODER_PAT",
|
||||||
|
"QODERCN_API_KEY",
|
||||||
|
"QODERCN_PERSONAL_ACCESS_TOKEN",
|
||||||
|
"QODERCN_PAT",
|
||||||
|
] as const;
|
||||||
|
const originalPats = Object.fromEntries(patEnvNames.map((name) => [name, process.env[name]]));
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
if (originalRegion === undefined) delete process.env.QODER_REGION;
|
||||||
|
else process.env.QODER_REGION = originalRegion;
|
||||||
|
for (const name of patEnvNames) {
|
||||||
|
const value = originalPats[name];
|
||||||
|
if (value === undefined) delete process.env[name];
|
||||||
|
else process.env[name] = value;
|
||||||
|
}
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
vi.resetModules();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("provider region binding", () => {
|
||||||
|
it("keeps qoder global and qoder-cn in CN when QODER_REGION=cn", async () => {
|
||||||
|
process.env.QODER_REGION = "cn";
|
||||||
|
for (const name of patEnvNames) delete process.env[name];
|
||||||
|
const providers = new Map<string, Record<string, unknown>>();
|
||||||
|
const pi = {
|
||||||
|
registerProvider(providerID: string, config: Record<string, unknown>) {
|
||||||
|
providers.set(providerID, config);
|
||||||
|
},
|
||||||
|
on: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const { default: registerProviders } = await import("../index.js");
|
||||||
|
await registerProviders(pi as never);
|
||||||
|
|
||||||
|
expect(providers.get("qoder")?.baseUrl).toBe("https://api3.qoder.sh/");
|
||||||
|
expect(providers.get("qoder-cn")?.baseUrl).toBe("https://gateway.qoder.com.cn/");
|
||||||
|
|
||||||
|
const fetchMock = vi
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue(
|
||||||
|
new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
userQuota: { total: 100, used: 1, remaining: 99, percentage: 1, unit: "requests" },
|
||||||
|
orgResourcePackage: { total: 0, used: 0, remaining: 0, percentage: 0, unit: "requests" },
|
||||||
|
totalUsagePercentage: 1,
|
||||||
|
isQuotaExceeded: false,
|
||||||
|
expiresAt: Date.now() + 3600_000,
|
||||||
|
}),
|
||||||
|
{ status: 200 },
|
||||||
|
),
|
||||||
|
);
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
const credentials = { access: "test-token", refresh: "", expires: Date.now() + 3600_000 };
|
||||||
|
const globalOAuth = providers.get("qoder")?.oauth as {
|
||||||
|
fetchUsage: (credentials: typeof credentials) => Promise<unknown>;
|
||||||
|
};
|
||||||
|
const cnOAuth = providers.get("qoder-cn")?.oauth as {
|
||||||
|
fetchUsage: (credentials: typeof credentials) => Promise<unknown>;
|
||||||
|
};
|
||||||
|
|
||||||
|
await globalOAuth.fetchUsage(credentials);
|
||||||
|
expect(fetchMock).toHaveBeenLastCalledWith(
|
||||||
|
"https://openapi.qoder.sh/api/v2/quota/usage",
|
||||||
|
expect.any(Object),
|
||||||
|
);
|
||||||
|
|
||||||
|
await cnOAuth.fetchUsage(credentials);
|
||||||
|
expect(fetchMock).toHaveBeenLastCalledWith(
|
||||||
|
"https://openapi.qoder.com.cn/api/v2/quota/usage",
|
||||||
|
expect.any(Object),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -93,8 +93,8 @@ function mockFetch(body: string): typeof fetch {
|
|||||||
return vi.fn(async () => response) as unknown as typeof fetch;
|
return vi.fn(async () => response) as unknown as typeof fetch;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeModel(): Model<Api> {
|
function makeModel(provider = "qoder"): Model<Api> {
|
||||||
return { id: "ultimate", api: "qoder-api" as Api, provider: "qoder" } as Model<Api>;
|
return { id: "ultimate", api: "qoder-api" as Api, provider } as Model<Api>;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeContext(): Context {
|
function makeContext(): Context {
|
||||||
@@ -116,8 +116,11 @@ async function consume(stream: AssistantMessageEventStream): Promise<AssistantMe
|
|||||||
|
|
||||||
describe("streamQoder", () => {
|
describe("streamQoder", () => {
|
||||||
const originalFetch = globalThis.fetch;
|
const originalFetch = globalThis.fetch;
|
||||||
|
const originalCnPat = process.env.QODERCN_PERSONAL_ACCESS_TOKEN;
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
globalThis.fetch = originalFetch;
|
globalThis.fetch = originalFetch;
|
||||||
|
if (originalCnPat === undefined) delete process.env.QODERCN_PERSONAL_ACCESS_TOKEN;
|
||||||
|
else process.env.QODERCN_PERSONAL_ACCESS_TOKEN = originalCnPat;
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -134,6 +137,24 @@ describe("streamQoder", () => {
|
|||||||
expect(text && "text" in text ? text.text : "").toBe("OK");
|
expect(text && "text" in text ? text.text : "").toBe("OK");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("binds chat hosts to provider ids even when only a CN PAT is set", async () => {
|
||||||
|
process.env.QODERCN_PERSONAL_ACCESS_TOKEN = "pt-cn-only";
|
||||||
|
|
||||||
|
globalThis.fetch = mockFetch(SUCCESS_SSE);
|
||||||
|
await consume(streamQoder(makeModel("qoder"), makeContext(), { apiKey: "fake" }));
|
||||||
|
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||||
|
expect.stringMatching(/^https:\/\/api3\.qoder\.sh\//),
|
||||||
|
expect.any(Object),
|
||||||
|
);
|
||||||
|
|
||||||
|
globalThis.fetch = mockFetch(SUCCESS_SSE);
|
||||||
|
await consume(streamQoder(makeModel("qoder-cn"), makeContext(), { apiKey: "fake" }));
|
||||||
|
expect(globalThis.fetch).toHaveBeenCalledWith(
|
||||||
|
expect.stringMatching(/^https:\/\/gateway\.qoder\.com\.cn\//),
|
||||||
|
expect.any(Object),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("surfaces an upstream 406 'Session blocked' as an error event, not a silent stop", async () => {
|
it("surfaces an upstream 406 'Session blocked' as an error event, not a silent stop", async () => {
|
||||||
globalThis.fetch = mockFetch(BLOCKED_SSE);
|
globalThis.fetch = mockFetch(BLOCKED_SSE);
|
||||||
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
|
||||||
|
|||||||
+12
-14
@@ -1,17 +1,15 @@
|
|||||||
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
||||||
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
||||||
import { getQoderBaseUrl, getQoderMode, getQoderRegionConfig, isQoderCNMode } from "./cosy.js";
|
import { getQoderBaseUrl, getQoderRegionConfig, isQoderCNMode } from "./cosy.js";
|
||||||
import { getCachedModels, isCacheStale, staticCnModels, staticModels, updateQoderModelsCache } from "./models.js";
|
import { getCachedModels, isCacheStale, staticCnModels, staticModels, updateQoderModelsCache } from "./models.js";
|
||||||
import {
|
import {
|
||||||
autoLoginQoderFromEnvironment,
|
autoLoginQoderFromEnvironment,
|
||||||
getCachedCredentials,
|
getCachedCredentials,
|
||||||
loginQoder,
|
loginQoderForMode,
|
||||||
loginQoderCN,
|
refreshQoderTokenForMode,
|
||||||
refreshQoderToken,
|
|
||||||
refreshQoderTokenCN,
|
|
||||||
} from "./oauth.js";
|
} from "./oauth.js";
|
||||||
import { streamQoder } from "./stream.js";
|
import { streamQoder } from "./stream.js";
|
||||||
import { fetchQoderUsage, fetchQoderUsageCN } from "./usage.js";
|
import { fetchQoderUsageForMode } from "./usage.js";
|
||||||
|
|
||||||
// pi supports a `fetchUsage` hook on the oauth config at runtime, but it is not
|
// pi supports a `fetchUsage` hook on the oauth config at runtime, but it is not
|
||||||
// part of the published ProviderConfig type. Declare the extension locally.
|
// part of the published ProviderConfig type. Declare the extension locally.
|
||||||
@@ -30,24 +28,24 @@ function modelsForProvider(mode: string, providerID: string): Model<Api>[] {
|
|||||||
})) as unknown as Model<Api>[];
|
})) as unknown as Model<Api>[];
|
||||||
}
|
}
|
||||||
|
|
||||||
function createQoderOAuth(_providerID: string, mode: string): OAuthConfigWithUsage {
|
function createQoderOAuth(mode: string): OAuthConfigWithUsage {
|
||||||
const region = getQoderRegionConfig(mode);
|
const region = getQoderRegionConfig(mode);
|
||||||
return {
|
return {
|
||||||
name: region.loginName,
|
name: region.loginName,
|
||||||
login: isQoderCNMode(mode) ? loginQoderCN : loginQoder,
|
login: (callbacks) => loginQoderForMode(callbacks, mode),
|
||||||
refreshToken: isQoderCNMode(mode) ? refreshQoderTokenCN : refreshQoderToken,
|
refreshToken: (credentials) => refreshQoderTokenForMode(credentials, mode),
|
||||||
getApiKey: (cred: OAuthCredentials) => cred.access,
|
getApiKey: (cred: OAuthCredentials) => cred.access,
|
||||||
// NOTE: no `modifyModels` hook on purpose. OMP (Bun) does a whole-catalog
|
// NOTE: no `modifyModels` hook on purpose. OMP (Bun) does a whole-catalog
|
||||||
// structuredClone before invoking it, and its bundled catalog contains a
|
// structuredClone before invoking it, and its bundled catalog contains a
|
||||||
// model with a non-cloneable property -> "The object can not be cloned."
|
// model with a non-cloneable property -> "The object can not be cloned."
|
||||||
// removes qoder from `omp models`. Models are supplied at registration
|
// removes qoder from `omp models`. Models are supplied at registration
|
||||||
// via `modelsForProvider` and refreshed by the startup/session cache hooks.
|
// via `modelsForProvider` and refreshed by the startup/session cache hooks.
|
||||||
fetchUsage: isQoderCNMode(mode) ? fetchQoderUsageCN : fetchQoderUsage,
|
fetchUsage: (credentials) => fetchQoderUsageForMode(credentials, mode),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerQoderProvider(pi: ExtensionAPI, providerID: string, mode: string): void {
|
function registerQoderProvider(pi: ExtensionAPI, providerID: string, mode: string): void {
|
||||||
const oauth = createQoderOAuth(providerID, mode);
|
const oauth = createQoderOAuth(mode);
|
||||||
pi.registerProvider(providerID, {
|
pi.registerProvider(providerID, {
|
||||||
baseUrl: getQoderBaseUrl(mode),
|
baseUrl: getQoderBaseUrl(mode),
|
||||||
api: "qoder-api" as Api,
|
api: "qoder-api" as Api,
|
||||||
@@ -77,7 +75,7 @@ async function refreshModelsAtStartup(providerID: string, mode: string): Promise
|
|||||||
|
|
||||||
export default async function (pi: ExtensionAPI) {
|
export default async function (pi: ExtensionAPI) {
|
||||||
for (const [providerID, mode] of [
|
for (const [providerID, mode] of [
|
||||||
["qoder", getQoderMode()],
|
["qoder", "global"],
|
||||||
["qoder-cn", "cn"],
|
["qoder-cn", "cn"],
|
||||||
] as const) {
|
] as const) {
|
||||||
try {
|
try {
|
||||||
@@ -95,7 +93,7 @@ export default async function (pi: ExtensionAPI) {
|
|||||||
// the cache was deleted while the token is still valid.
|
// the cache was deleted while the token is still valid.
|
||||||
pi.on("session_start", async (_event, ctx) => {
|
pi.on("session_start", async (_event, ctx) => {
|
||||||
for (const [providerID, mode] of [
|
for (const [providerID, mode] of [
|
||||||
["qoder", getQoderMode()],
|
["qoder", "global"],
|
||||||
["qoder-cn", "cn"],
|
["qoder-cn", "cn"],
|
||||||
] as const) {
|
] as const) {
|
||||||
try {
|
try {
|
||||||
@@ -113,6 +111,6 @@ export default async function (pi: ExtensionAPI) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
registerQoderProvider(pi, "qoder", getQoderMode());
|
registerQoderProvider(pi, "qoder", "global");
|
||||||
registerQoderProvider(pi, "qoder-cn", "cn");
|
registerQoderProvider(pi, "qoder-cn", "cn");
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-19
@@ -3,7 +3,7 @@ import { homedir } from "node:os";
|
|||||||
import { dirname, join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
import type { OAuthCredentials, OAuthLoginCallbacks } from "@earendil-works/pi-ai";
|
||||||
import * as PiCodingAgent from "@earendil-works/pi-coding-agent";
|
import * as PiCodingAgent from "@earendil-works/pi-coding-agent";
|
||||||
import { getMachineId, getQoderMode, getQoderRefreshURL, getQoderRegionConfig } from "./cosy.js";
|
import { getMachineId, getQoderRefreshURL, getQoderRegionConfig } from "./cosy.js";
|
||||||
import { interactiveLogin } from "./login.js";
|
import { interactiveLogin } from "./login.js";
|
||||||
import { updateQoderModelsCache } from "./models.js";
|
import { updateQoderModelsCache } from "./models.js";
|
||||||
import { credentialsFromPat, decodePatRefresh, fetchUserInfo, isPatRefresh } from "./pat.js";
|
import { credentialsFromPat, decodePatRefresh, fetchUserInfo, isPatRefresh } from "./pat.js";
|
||||||
@@ -144,7 +144,7 @@ export async function resolveQoderIdentity(
|
|||||||
return creds;
|
return creds;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loginQoderForMode(callbacks: OAuthLoginCallbacks, mode: string): Promise<OAuthCredentials> {
|
export async function loginQoderForMode(callbacks: OAuthLoginCallbacks, mode: string): Promise<OAuthCredentials> {
|
||||||
const providerID = getQoderRegionConfig(mode).providerID;
|
const providerID = getQoderRegionConfig(mode).providerID;
|
||||||
// 1. Try environment variables first (PAT). A PAT (pt-...) must be exchanged
|
// 1. Try environment variables first (PAT). A PAT (pt-...) must be exchanged
|
||||||
// for a short-lived job token before it can be used — credentialsFromPat
|
// for a short-lived job token before it can be used — credentialsFromPat
|
||||||
@@ -182,23 +182,10 @@ async function loginQoderForMode(callbacks: OAuthLoginCallbacks, mode: string):
|
|||||||
return creds;
|
return creds;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loginQoder(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
export async function refreshQoderTokenForMode(
|
||||||
return loginQoderForMode(callbacks, getQoderMode());
|
credentials: OAuthCredentials,
|
||||||
}
|
mode: string,
|
||||||
|
): Promise<OAuthCredentials> {
|
||||||
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.
|
// PAT-based credentials: re-exchange the stored PAT for a fresh job token.
|
||||||
if (isPatRefresh(credentials.refresh)) {
|
if (isPatRefresh(credentials.refresh)) {
|
||||||
const { pat } = decodePatRefresh(credentials.refresh);
|
const { pat } = decodePatRefresh(credentials.refresh);
|
||||||
|
|||||||
+1
-2
@@ -16,7 +16,6 @@ import {
|
|||||||
buildAuthHeaders,
|
buildAuthHeaders,
|
||||||
getMachineId,
|
getMachineId,
|
||||||
getQoderChatURL,
|
getQoderChatURL,
|
||||||
getQoderMode,
|
|
||||||
getQoderRegionConfig,
|
getQoderRegionConfig,
|
||||||
isQoderCNMode,
|
isQoderCNMode,
|
||||||
} from "./cosy.js";
|
} from "./cosy.js";
|
||||||
@@ -117,7 +116,7 @@ export function streamQoder(
|
|||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const providerMode = model.provider === "qoder-cn" ? "cn" : getQoderMode();
|
const providerMode = model.provider === "qoder-cn" ? "cn" : "global";
|
||||||
const region = getQoderRegionConfig(providerMode);
|
const region = getQoderRegionConfig(providerMode);
|
||||||
const accessToken = options?.apiKey;
|
const accessToken = options?.apiKey;
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
|
|||||||
+5
-10
@@ -1,5 +1,5 @@
|
|||||||
import type { OAuthCredentials } from "@earendil-works/pi-ai";
|
import type { OAuthCredentials } from "@earendil-works/pi-ai";
|
||||||
import { getQoderMode, getQoderRegionConfig, getQoderUsageURL } from "./cosy.js";
|
import { getQoderRegionConfig, getQoderUsageURL } from "./cosy.js";
|
||||||
|
|
||||||
interface QoderQuota {
|
interface QoderQuota {
|
||||||
total: number;
|
total: number;
|
||||||
@@ -33,7 +33,10 @@ export interface QoderProviderUsage {
|
|||||||
raw?: Record<string, unknown>;
|
raw?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchQoderUsageForMode(credentials: OAuthCredentials, mode: string): Promise<QoderProviderUsage> {
|
export async function fetchQoderUsageForMode(
|
||||||
|
credentials: OAuthCredentials,
|
||||||
|
mode: string,
|
||||||
|
): Promise<QoderProviderUsage> {
|
||||||
const region = getQoderRegionConfig(mode);
|
const region = getQoderRegionConfig(mode);
|
||||||
const response = await fetch(getQoderUsageURL(mode), {
|
const response = await fetch(getQoderUsageURL(mode), {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
@@ -84,11 +87,3 @@ async function fetchQoderUsageForMode(credentials: OAuthCredentials, mode: strin
|
|||||||
raw: raw as unknown as Record<string, unknown>,
|
raw: raw as unknown as Record<string, unknown>,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchQoderUsage(credentials: OAuthCredentials): Promise<QoderProviderUsage> {
|
|
||||||
return fetchQoderUsageForMode(credentials, getQoderMode());
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function fetchQoderUsageCN(credentials: OAuthCredentials): Promise<QoderProviderUsage> {
|
|
||||||
return fetchQoderUsageForMode(credentials, "cn");
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user