Files
openclaw/extensions/github-copilot/dynamic-models.ts
T
53c0ca9754 fix(github-copilot): honor configured identity across model requests (#127965)
* fix(github-copilot): honor configured request identity

Honor existing provider request headers consistently across model discovery, setup, inference, and embeddings. Preserve the default identity and partition the live catalog by the configured identity.

Co-authored-by: Finn763 <165816600+Finn763@users.noreply.github.com>

* test(github-copilot): real-behavior wire-trace proof for #127965

* fix(github-copilot proof): print on-disk wire-trace.json sha256

* chore(github-copilot proof): drop pr-body.md from PR diff

* fix(github-copilot): finish identity checks and consolidate proof

Complete the typed provider fixture and keep the identity constant private. Retain canonical owner tests and real provider evidence; remove parallel copied mock implementations and generated PR-only traces while preserving the diagnostic commits in ancestry.

Co-authored-by: Finn763 <165816600+Finn763@users.noreply.github.com>

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
Co-authored-by: Finn763 <Finn763@users.noreply.github.com>
2026-08-27 01:29:28 -07:00

165 lines
5.3 KiB
TypeScript

import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
import type {
ProviderCatalogContext,
ProviderCatalogResult,
ProviderPrepareDynamicModelContext,
ProviderResolveDynamicModelContext,
ProviderRuntimeModel,
} from "openclaw/plugin-sdk/plugin-entry";
import { getCachedLiveCatalogValue } from "openclaw/plugin-sdk/provider-catalog-shared";
import { resolveFirstGithubToken } from "./auth.js";
import { resolveGithubCopilotDomain } from "./domain.js";
import {
PROVIDER_ID,
fetchCopilotModelCatalog,
isCopilotCatalogModelVisible,
resolveCopilotForwardCompatModel,
} from "./models.js";
import { buildCopilotRuntimeHeaders } from "./runtime-identity.js";
type GithubCopilotCatalogContext = {
agentDir?: string;
config?: OpenClawConfig;
env: NodeJS.ProcessEnv;
profileId?: string;
authProfileMode?: ProviderPrepareDynamicModelContext["authProfileMode"];
};
function dynamicModelScope(
profileId?: string,
authProfileMode?: ProviderPrepareDynamicModelContext["authProfileMode"],
): string {
const normalizedProfileId = profileId?.trim();
return normalizedProfileId
? `profile:${normalizedProfileId}`
: authProfileMode
? `direct:${authProfileMode}`
: "unscoped";
}
async function loadGithubCopilotRuntime() {
return await import("./register.runtime.js");
}
export function createGithubCopilotDynamicModelHooks(params: {
discoveryEnabled(config?: OpenClawConfig): boolean;
}) {
const preparedDynamicModels = new WeakMap<
object,
Map<string, ReadonlyMap<string, ProviderRuntimeModel>>
>();
async function resolveCatalog(ctx: GithubCopilotCatalogContext) {
if (!params.discoveryEnabled(ctx.config)) {
return null;
}
const { DEFAULT_COPILOT_API_BASE_URL, resolveCopilotRuntimeAuth } =
await loadGithubCopilotRuntime();
const { githubToken, githubDomain, hasProfile } = await resolveFirstGithubToken({
agentDir: ctx.agentDir,
env: ctx.env,
...(ctx.config ? { config: ctx.config } : {}),
...(ctx.profileId ? { profileId: ctx.profileId } : {}),
...(ctx.authProfileMode ? { authProfileMode: ctx.authProfileMode } : {}),
});
if (!hasProfile && !githubToken) {
return null;
}
let baseUrl = DEFAULT_COPILOT_API_BASE_URL;
let copilotApiToken: string | undefined;
if (githubToken) {
try {
const auth = await resolveCopilotRuntimeAuth({
githubToken,
env: ctx.env,
githubDomain: resolveGithubCopilotDomain({
env: ctx.env,
explicit: githubDomain,
config: ctx.config,
}),
});
baseUrl = auth.baseUrl;
copilotApiToken = auth.apiKey;
} catch {
baseUrl = DEFAULT_COPILOT_API_BASE_URL;
}
}
// Live metadata follows account entitlements and context limits. Static
// manifest models remain the visible fallback when exchange or discovery fails.
let discoveredModels: Awaited<ReturnType<typeof fetchCopilotModelCatalog>> = [];
if (copilotApiToken) {
const headers = buildCopilotRuntimeHeaders({ config: ctx.config });
try {
discoveredModels = await getCachedLiveCatalogValue({
keyParts: [
PROVIDER_ID,
"models",
baseUrl,
copilotApiToken,
headers["Copilot-Integration-Id"],
],
load: async () => await fetchCopilotModelCatalog({ copilotApiToken, baseUrl, headers }),
});
} catch {
discoveredModels = [];
}
}
return { baseUrl, models: discoveredModels };
}
async function runCatalog(ctx: ProviderCatalogContext): Promise<ProviderCatalogResult> {
const catalog = await resolveCatalog(ctx);
return catalog
? {
provider: {
...catalog,
models: catalog.models.filter(isCopilotCatalogModelVisible),
},
}
: null;
}
async function prepareDynamicModel(ctx: ProviderPrepareDynamicModelContext): Promise<void> {
const catalog = await resolveCatalog({
agentDir: ctx.agentDir,
env: process.env,
...(ctx.config ? { config: ctx.config } : {}),
...(ctx.authProfileId ? { profileId: ctx.authProfileId } : {}),
...(ctx.authProfileMode ? { authProfileMode: ctx.authProfileMode } : {}),
});
const models = new Map<string, ProviderRuntimeModel>();
if (catalog) {
for (const model of catalog.models) {
models.set(model.id, {
...model,
provider: PROVIDER_ID,
baseUrl: catalog.baseUrl,
});
}
}
let scopedModels = preparedDynamicModels.get(ctx.modelRegistry);
if (!scopedModels) {
scopedModels = new Map();
preparedDynamicModels.set(ctx.modelRegistry, scopedModels);
}
scopedModels.set(dynamicModelScope(ctx.authProfileId, ctx.authProfileMode), models);
}
function resolveDynamicModel(ctx: ProviderResolveDynamicModelContext) {
return (
preparedDynamicModels
.get(ctx.modelRegistry)
?.get(dynamicModelScope(ctx.authProfileId, ctx.authProfileMode))
?.get(ctx.modelId) ?? resolveCopilotForwardCompatModel(ctx)
);
}
return {
prepareDynamicModel,
resolveDynamicModel,
runCatalog,
preferRuntimeResolvedModel: ({ config }: { config?: OpenClawConfig }) =>
params.discoveryEnabled(config),
};
}