Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

refactor: migrate Ollama Cloud to OpenAI-compatible Chat API and update provider configuration logic

Salman Qureshi069fd93e7a84
diff --git a/.env.example b/.env.example
index e0a09a32..1062d769 100644
--- a/.env.example
+++ b/.env.example
@@ -28,9 +28,9 @@ GROK_API_KEY=
GROK_BASE_URL=https://api.x.ai/v1
GROK_MODEL=grok-4
-# Ollama Cloud (API key required for ollama.com/api)
+# Ollama Cloud (API key required for ollama.com/v1)
OLLAMA_CLOUD_API_KEY=
-OLLAMA_CLOUD_BASE_URL=https://ollama.com/api
+OLLAMA_CLOUD_BASE_URL=https://ollama.com/v1
OLLAMA_CLOUD_MODEL=gpt-oss:120b
# Ollama Local (no API key required)
diff --git a/src/providers/ollama.ts b/src/providers/ollama.ts
index 0b024dff..fcae23bc 100644
--- a/src/providers/ollama.ts
+++ b/src/providers/ollama.ts
@@ -37,7 +37,7 @@ export class OllamaProvider extends BaseProvider {
if (this.name === 'ollamaLocal') {
return this.config.baseUrl.length > 0 && this.config.model.length > 0;
}
- return this.config.apiKey.length > 0;
+ return this.config.apiKey.length > 0 && this.config.baseUrl.length > 0;
}
getModelInstance() {
diff --git a/src/providers/openai-compat.ts b/src/providers/openai-compat.ts
index cb54ae67..eb87e886 100644
--- a/src/providers/openai-compat.ts
+++ b/src/providers/openai-compat.ts
@@ -11,7 +11,7 @@ export class OpenAICompatProvider extends BaseProvider {
private client: ReturnType<typeof createOpenAI>;
private modelInstance: ReturnType<ReturnType<typeof createOpenAI>['languageModel']>;
- constructor(config: ProviderConfig) {
+ constructor(config: ProviderConfig, { useChatApi }: { useChatApi?: boolean } = {}) {
super(config);
this.name = config.name;
this.model = config.model;
@@ -20,7 +20,9 @@ export class OpenAICompatProvider extends BaseProvider {
apiKey: config.apiKey,
baseURL: config.baseUrl,
});
- this.modelInstance = this.client(config.model);
+ this.modelInstance = useChatApi
+ ? this.client.chat(config.model)
+ : this.client(config.model);
}
async generateText(prompt: string, systemPrompt: string): Promise<LLMResponse> {
diff --git a/src/providers/registry.ts b/src/providers/registry.ts
index e41a8c54..47d077b7 100644
--- a/src/providers/registry.ts
+++ b/src/providers/registry.ts
@@ -35,8 +35,10 @@ export class ProviderRegistry {
provider = new AnthropicProvider(pc);
} else if (pc.name === 'deepseek') {
provider = new DeepSeekProvider(pc);
- } else if (pc.name === 'ollamaCloud' || pc.name === 'ollamaLocal') {
+ } else if (pc.name === 'ollamaLocal') {
provider = new OllamaProvider(pc);
+ } else if (pc.name === 'ollamaCloud') {
+ provider = new OpenAICompatProvider(pc, { useChatApi: true });
} else if (pc.name === 'mimo' || pc.name === 'mimoTokenPlan') {
provider = new MiMoProvider(pc);
} else {
diff --git a/src/utils/config.ts b/src/utils/config.ts
index 2034a95f..5cbb1015 100644
--- a/src/utils/config.ts
+++ b/src/utils/config.ts
@@ -166,7 +166,7 @@ export function getDefaultConfig(): MercuryConfig {
ollamaCloud: {
name: 'ollamaCloud',
apiKey: getEnv('OLLAMA_CLOUD_API_KEY', ''),
- baseUrl: getEnv('OLLAMA_CLOUD_BASE_URL', 'https://ollama.com/api'),
+ baseUrl: getEnv('OLLAMA_CLOUD_BASE_URL', 'https://ollama.com/v1'),
model: getEnv('OLLAMA_CLOUD_MODEL', 'gpt-oss:120b'),
enabled: getEnvBool('OLLAMA_CLOUD_ENABLED', true),
},
@@ -236,7 +236,9 @@ export function loadConfig(): MercuryConfig {
const raw = readFileSync(CONFIG_PATH, 'utf-8');
const fileConfig = parseYaml(raw) as Partial<MercuryConfig>;
const defaults = getDefaultConfig();
- return migrateLegacyTelegramAccess(deepMerge(defaults, fileConfig));
+ return migrateLegacyOllamaCloudBaseUrl(
+ migrateLegacyTelegramAccess(deepMerge(defaults, fileConfig)),
+ );
}
return migrateLegacyTelegramAccess(getDefaultConfig());
}
@@ -295,6 +297,9 @@ export function isProviderConfigured(provider: ProviderConfig): boolean {
if (provider.name === 'ollamaLocal') {
return provider.baseUrl.length > 0 && provider.model.length > 0;
}
+ if (provider.name === 'ollamaCloud') {
+ return provider.apiKey.length > 0 && provider.baseUrl.length > 0;
+ }
return provider.apiKey.length > 0;
}
@@ -498,3 +503,11 @@ export function migrateLegacyTelegramAccess(config: MercuryConfig): MercuryConfi
return config;
}
+
+export function migrateLegacyOllamaCloudBaseUrl(config: MercuryConfig): MercuryConfig {
+ if (config.providers.ollamaCloud.baseUrl === 'https://ollama.com/api') {
+ config.providers.ollamaCloud.baseUrl = 'https://ollama.com/v1';
+ saveConfig(config);
+ }
+ return config;
+}
diff --git a/src/utils/provider-models.test.ts b/src/utils/provider-models.test.ts
index adbdc0db..50212362 100644
--- a/src/utils/provider-models.test.ts
+++ b/src/utils/provider-models.test.ts
@@ -74,4 +74,26 @@ describe('buildModelCatalog', () => {
expect(catalog.models).toContain('mimo-v2.5');
expect(catalog.models).toContain('mimo-v2-flash');
});
+
+ it('prefers gpt-oss:120b for Ollama Cloud when available', () => {
+ const catalog = buildModelCatalog('ollamaCloud', [
+ 'gpt-oss:120b',
+ 'gpt-oss:20b',
+ 'gpt-oss:120b-cloud',
+ ]);
+
+ expect(catalog.recommendedModel).toBe('gpt-oss:120b');
+ expect(catalog.models).toContain('gpt-oss:120b-cloud');
+ expect(catalog.models).toContain('gpt-oss:20b');
+ });
+
+ it('falls back to the current Ollama Cloud model when no preferred default is available', () => {
+ const catalog = buildModelCatalog(
+ 'ollamaCloud',
+ ['other-model:8b'],
+ 'other-model:8b',
+ );
+
+ expect(catalog.recommendedModel).toBe('other-model:8b');
+ });
});
diff --git a/src/utils/provider-models.ts b/src/utils/provider-models.ts
index 956f6af2..213dc598 100644
--- a/src/utils/provider-models.ts
+++ b/src/utils/provider-models.ts
@@ -273,26 +273,36 @@ async function fetchGrokModels(config: ProviderConfig): Promise<ProviderModelCat
return buildModelCatalog('grok', ids, config.model);
}
-async function fetchOllamaModels(provider: ProviderName, config: ProviderConfig): Promise<ProviderModelCatalog> {
- const headers = config.apiKey
- ? { Authorization: `Bearer ${config.apiKey}` }
- : undefined;
+async function fetchOllamaCloudModels(config: ProviderConfig): Promise<ProviderModelCatalog> {
+ const data = await fetchJson<OpenAIModelResponse>(
+ `${trimTrailingSlash(config.baseUrl)}/models`,
+ {
+ headers: {
+ Authorization: `Bearer ${config.apiKey}`,
+ },
+ },
+ 'Mercury could not fetch models for this Ollama Cloud key. Please re-enter it.',
+ );
+ const ids = (data.data ?? [])
+ .map((model) => model.id?.trim() ?? '')
+ .filter(Boolean);
+
+ return buildModelCatalog('ollamaCloud', ids, config.model);
+}
+
+async function fetchOllamaLocalModels(config: ProviderConfig): Promise<ProviderModelCatalog> {
const data = await fetchJson<OllamaTagsResponse>(
`${trimTrailingSlash(config.baseUrl)}/tags`,
- {
- headers,
- },
- provider === 'ollamaCloud'
- ? 'Mercury could not fetch models for this Ollama Cloud key. Please re-enter it.'
- : 'Mercury could not fetch models from this Ollama Local server. Please check the base URL and try again.',
+ {},
+ 'Mercury could not fetch models from this Ollama Local server. Please check the base URL and try again.',
);
const ids = (data.models ?? [])
.map((model) => model.model?.trim() || model.name?.trim() || '')
.filter(Boolean);
- return buildModelCatalog(provider, ids, config.model);
+ return buildModelCatalog('ollamaLocal', ids, config.model);
}
async function fetchMiMoModels(config: ProviderConfig): Promise<ProviderModelCatalog> {
@@ -349,8 +359,12 @@ export async function fetchProviderModelCatalog(
return fetchGrokModels(config);
}
- if (provider === 'ollamaCloud' || provider === 'ollamaLocal') {
- return fetchOllamaModels(provider, config);
+ if (provider === 'ollamaCloud') {
+ return fetchOllamaCloudModels(config);
+ }
+
+ if (provider === 'ollamaLocal') {
+ return fetchOllamaLocalModels(config);
}
if (provider === 'mimo') {