






















@@ -41,6 +41,7 @@ import {
4141OLLAMA_CLOUD_DEFAULT_MODELS,
4242OLLAMA_CLOUD_PROVIDER_ID,
4343OLLAMA_DEFAULT_BASE_URL,
44+OLLAMA_GLM52_CLOUD_MODEL_ID,
4445} from "./src/defaults.js";
4546import {
4647OLLAMA_DEFAULT_API_KEY,
@@ -79,8 +80,6 @@ const dynamicModelCache = new Map<string, ProviderRuntimeModel[]>();
7980const OLLAMA_CLOUD_DEFAULT_MODEL_REF = `${OLLAMA_CLOUD_PROVIDER_ID}/${OLLAMA_CLOUD_DEFAULT_MODELS[0]}`;
8081const OLLAMA_CONFIGURED_SHOW_CONCURRENCY = 4;
8182const OLLAMA_CONFIGURED_SHOW_MAX_MODELS = 8;
82-const OLLAMA_API_KEY_ENV_REF_RE = /^[A-Z_][A-Z0-9_]*$/u;
83-8483function buildDynamicCacheKey(provider: string, baseUrl: string | undefined): string {
8584return `${provider}\0${baseUrl ?? ""}`;
8685}
@@ -182,10 +181,7 @@ function readEnvBackedOllamaApiKey(value: unknown, env: NodeJS.ProcessEnv): stri
182181if (ref?.source === "env") {
183182return readConcreteOllamaApiKey(env[ref.id.trim()]);
184183}
185-const apiKey = readConfiguredOllamaApiKey(value);
186-return apiKey && OLLAMA_API_KEY_ENV_REF_RE.test(apiKey)
187- ? readConcreteOllamaApiKey(env[apiKey])
188- : undefined;
184+return undefined;
189185}
190186191187function isAmbientOllamaApiKeyMarker(value: string | undefined): boolean {
@@ -195,7 +191,7 @@ function isAmbientOllamaApiKeyMarker(value: string | undefined): boolean {
195191function readUsableOllamaShowApiKey(params: {
196192env: NodeJS.ProcessEnv;
197193allowAmbientEnvFallback: boolean;
198-explicitApiKey?: string;
194+explicitApiKey?: unknown;
199195resolved?: { apiKey?: unknown; discoveryApiKey?: unknown };
200196}): string | undefined {
201197const explicitEnvApiKey = readEnvBackedOllamaApiKey(params.explicitApiKey, params.env);
@@ -218,7 +214,7 @@ function readUsableOllamaShowApiKey(params: {
218214return resolvedEnvApiKey;
219215}
220216const apiKey = readConcreteOllamaApiKey(params.resolved?.apiKey);
221-if (apiKey && !OLLAMA_API_KEY_ENV_REF_RE.test(apiKey)) {
217+if (apiKey) {
222218return apiKey;
223219}
224220return params.allowAmbientEnvFallback
@@ -306,9 +302,36 @@ function buildStaticOllamaCloudProvider(): ModelProviderConfig {
306302};
307303}
308304309-async function buildOllamaCloudProvider(): Promise<ModelProviderConfig> {
310-const discovered = await buildOllamaProvider(OLLAMA_CLOUD_BASE_URL, { quiet: true });
311-return discovered.models?.length ? discovered : buildStaticOllamaCloudProvider();
305+async function buildOllamaCloudProvider(apiKey?: string): Promise<ModelProviderConfig> {
306+const discovered = await buildOllamaProvider(OLLAMA_CLOUD_BASE_URL, {
307+ ...(apiKey ? { apiKey } : {}),
308+quiet: true,
309+});
310+if (!discovered.models?.length) {
311+return buildStaticOllamaCloudProvider();
312+}
313+if (!apiKey || discovered.models.some((model) => model.id === OLLAMA_GLM52_CLOUD_MODEL_ID)) {
314+return discovered;
315+}
316+const showInfo = await queryOllamaModelShowInfo(
317+OLLAMA_CLOUD_BASE_URL,
318+OLLAMA_GLM52_CLOUD_MODEL_ID,
319+{ apiKey },
320+);
321+if (typeof showInfo.contextWindow !== "number" && (showInfo.capabilities?.length ?? 0) === 0) {
322+return discovered;
323+}
324+return {
325+ ...discovered,
326+models: [
327+ ...discovered.models,
328+buildOllamaModelDefinition(
329+OLLAMA_GLM52_CLOUD_MODEL_ID,
330+showInfo.contextWindow,
331+showInfo.capabilities,
332+),
333+],
334+};
312335}
313336314337async function resolveRequestedDynamicOllamaModel(params: {
@@ -360,7 +383,7 @@ async function augmentConfiguredOllamaCatalogModels(params: {
360383const showApiKey = readUsableOllamaShowApiKey({
361384env: params.env,
362385allowAmbientEnvFallback: !isLocalBaseUrl,
363-explicitApiKey: readConfiguredOllamaApiKey(configuredProvider?.apiKey),
386+explicitApiKey: configuredProvider?.apiKey,
364387resolved: params.resolveProviderApiKey?.(params.provider),
365388});
366389if (!isLocalBaseUrl && !showApiKey) {
@@ -457,13 +480,19 @@ export default definePluginEntry({
457480catalog: {
458481order: "simple",
459482run: async (ctx: ProviderCatalogContext) => {
460-const apiKey = ctx.resolveProviderApiKey(OLLAMA_CLOUD_PROVIDER_ID).apiKey;
483+const resolvedAuth = ctx.resolveProviderApiKey(OLLAMA_CLOUD_PROVIDER_ID);
484+const apiKey = resolvedAuth.apiKey ?? resolvedAuth.discoveryApiKey;
461485if (!apiKey) {
462486return null;
463487}
488+const discoveryApiKey = readUsableOllamaShowApiKey({
489+env: ctx.env,
490+allowAmbientEnvFallback: true,
491+resolved: resolvedAuth,
492+});
464493return {
465494provider: {
466- ...(await buildOllamaCloudProvider()),
495+ ...(await buildOllamaCloudProvider(discoveryApiKey)),
467496 apiKey,
468497},
469498};
@@ -495,6 +524,13 @@ export default definePluginEntry({
495524resolveReasoningOutputMode: () => "native",
496525resolveThinkingProfile: resolveOllamaThinkingProfile,
497526wrapStreamFn: createConfiguredOllamaCompatStreamWrapper,
527+resolveDynamicModel: ({ provider, modelId }) => {
528+const cloudProvider = buildStaticOllamaCloudProvider();
529+const model = cloudProvider.models?.find((entry) => entry.id === modelId);
530+return model
531+ ? toDynamicOllamaModel({ provider, providerConfig: cloudProvider, model })
532+ : undefined;
533+},
498534augmentModelCatalog: async (ctx) =>
499535await augmentConfiguredOllamaCatalogModels({
500536config: ctx.config,
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。