Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(ollama): 离线加载时补齐旧模型显示名称 (#4587)

Dashd3b662f91979
diff --git a/apps/desktop/src/main/local-model-runtime/__tests__/managedOllamaProvider.test.ts b/apps/desktop/src/main/local-model-runtime/__tests__/managedOllamaProvider.test.ts
index 9948a2f30b..ec484e4ec2 100644
--- a/apps/desktop/src/main/local-model-runtime/__tests__/managedOllamaProvider.test.ts
+++ b/apps/desktop/src/main/local-model-runtime/__tests__/managedOllamaProvider.test.ts
@@ -17,6 +17,7 @@ import {
emptyCodexRuntime,
emptyPiRuntime,
ensureManagedOllamaProvider,
+ migrateManagedOllamaProvider,
removeManagedOllamaModel,
syncManagedOllamaAgentProjections,
upsertManagedOllamaModel,
@@ -107,6 +108,21 @@ describe('managed Ollama model identity', () => {
expect(updateCustomProvider).not.toHaveBeenCalled();
});
+ it('keeps an explicitly chosen raw ID name through import and later catalog loads', async () => {
+ const id = 'hf.co/team/Example-GGUF:Q8_0';
+ let existing = buildEmptyManagedOllamaProvider();
+ vi.mocked(getCustomProvider).mockImplementation(async () => existing);
+ vi.mocked(updateCustomProvider).mockImplementation(async (_id, next) => {
+ existing = next;
+ return next;
+ });
+ await upsertManagedOllamaModel({ id, name: id, nameExplicit: true });
+ for (const runtime of Object.values(existing.runtimes)) {
+ expect(runtime.models[0]).toMatchObject({ id, name: id, nameExplicit: true });
+ }
+ expect(migrateManagedOllamaProvider(existing)).toBe(existing);
+ });
+
it('does not create a managed provider after the captured owner is gone', async () => {
let checks = 0;
vi.mocked(getCustomProvider).mockResolvedValue(null);
@@ -163,6 +179,64 @@ describe('managed Ollama model identity', () => {
});
describe('local model display names', () => {
+ const hfId = 'hf.co/ornith-ai/Ornith-1.5-35B-A3B-GGUF:Q4_K_M';
+
+ it('repairs a cached ID name without changing other fields', () => {
+ const existing = providerWith(hfId);
+ for (const runtime of Object.values(existing.runtimes)) {
+ runtime.models = [
+ {
+ id: hfId,
+ name: hfId,
+ contextWindow: 65_536,
+ defaultEnabled: false,
+ reasoning: true,
+ supportsImageInput: false,
+ },
+ ] as typeof runtime.models;
+ }
+ const before = structuredClone(existing);
+ const migrated = migrateManagedOllamaProvider(existing)!;
+ for (const agent of ['pi', 'claude-code', 'codex'] as const) {
+ expect(migrated.runtimes[agent]).toEqual({
+ ...before.runtimes[agent],
+ models: [{ ...before.runtimes[agent].models[0], name: 'Ornith 1.5 35B A3B (Q4_K_M)' }],
+ });
+ }
+ expect(existing).toEqual(before);
+ expect(migrateManagedOllamaProvider(migrated)).toBe(migrated);
+ });
+
+ it.each([{ name: 'My local model' }, { name: hfId, nameExplicit: true }, { name: '' }, {}])(
+ 'leaves names other than implicit ID fallbacks untouched: %j',
+ (saved) => {
+ const existing = providerWith(hfId);
+ for (const runtime of Object.values(existing.runtimes)) {
+ runtime.models = [{ id: hfId, ...saved }] as typeof runtime.models;
+ }
+ expect(migrateManagedOllamaProvider(existing)).toBe(existing);
+ },
+ );
+
+ it('does not normalize unrelated custom providers', () => {
+ const existing = { ...providerWith(hfId), id: 'my-ollama' };
+ expect(migrateManagedOllamaProvider(existing)).toBeNull();
+ expect(existing.runtimes.pi.models[0].name).toBe(hfId);
+ });
+
+ it('repairs legacy Pi-only names without inventing coding-agent capabilities', () => {
+ const existing = providerWith(hfId);
+ const migrated = migrateManagedOllamaProvider({
+ ...existing,
+ runtimes: { pi: existing.runtimes.pi },
+ })!;
+ expect(migrated.runtimes.pi?.models).toEqual([
+ { id: hfId, name: 'Ornith 1.5 35B A3B (Q4_K_M)' },
+ ]);
+ expect(migrated.runtimes['claude-code']?.models).toEqual([]);
+ expect(migrated.runtimes.codex?.models).toEqual([]);
+ });
+
it.each([
['hf.co/ornith-ai/Ornith-1.5-35B-A3B-GGUF:Q4_K_M', 'Ornith 1.5 35B A3B (Q4_K_M)'],
['hf.co/team/Example-GGUF:Q8_0', 'Example (Q8_0)'],
diff --git a/apps/desktop/src/main/local-model-runtime/__tests__/service.list.test.ts b/apps/desktop/src/main/local-model-runtime/__tests__/service.list.test.ts
index 534d3745ec..b0c107c38d 100644
--- a/apps/desktop/src/main/local-model-runtime/__tests__/service.list.test.ts
+++ b/apps/desktop/src/main/local-model-runtime/__tests__/service.list.test.ts
@@ -167,4 +167,27 @@ describe('Ollama list refresh convergence', () => {
expect(await upsertManagedOllamaModels([])).toMatchObject({ ok: true, changed: true });
expect(await upsertManagedOllamaModels([])).toMatchObject({ ok: true, changed: false });
});
+
+ it.each(['offline', 'tags failure'])(
+ 'repairs old names once during %s and retains the saved model across service restarts',
+ async (failure) => {
+ const id = 'hf.co/ornith-ai/Ornith-1.5-35B-A3B-GGUF:Q4_K_M';
+ const saved = { id, name: id, contextWindow: 65_536, defaultEnabled: false };
+ provider.runtimes.pi!.models = [saved];
+ provider.runtimes.codex!.models = [{ ...saved, name: 'My coding model' }];
+ offline = failure === 'offline';
+ tagsFail = failure === 'tags failure';
+
+ expect((await makeService().list()).catalogDirty).toBe(true);
+ expect(provider.runtimes.pi!.models).toEqual([
+ { ...saved, name: 'Ornith 1.5 35B A3B (Q4_K_M)' },
+ ]);
+ expect(provider.runtimes.codex!.models).toEqual([{ ...saved, name: 'My coding model' }]);
+ expect(provider.runtimes['claude-code']!.models).toEqual([]);
+ const persisted = structuredClone(provider);
+ expect((await makeService().list()).catalogDirty).toBe(false);
+ expect(provider).toEqual(persisted);
+ expect(updateCustomProvider).toHaveBeenCalledTimes(1);
+ },
+ );
});
diff --git a/apps/desktop/src/main/local-model-runtime/managedOllamaProvider.ts b/apps/desktop/src/main/local-model-runtime/managedOllamaProvider.ts
index dd8532efa3..a230151017 100644
--- a/apps/desktop/src/main/local-model-runtime/managedOllamaProvider.ts
+++ b/apps/desktop/src/main/local-model-runtime/managedOllamaProvider.ts
@@ -98,7 +98,10 @@ export function buildEmptyManagedOllamaProvider(): CustomProviderConfig {
}
/** Keep the repository identity and quantization visible without the HF transport prefix. */
-function localModelDisplayName(name: string): string {
+function localModelDisplayName(name: string, savedName?: string, nameExplicit?: boolean): string {
+ if (savedName !== undefined && (nameExplicit || (savedName && savedName !== name))) {
+ return savedName;
+ }
const curated = curatedOllamaDisplayName(name);
if (curated) return curated;
const hf = /^hf\.co\/[^/]+\/([^/:]+)(?::([^/]+))?$/.exec(name);
@@ -140,12 +143,13 @@ function toAgentModel(
): ProviderRuntimeModelConfig {
const named = {
...model,
- name: model.name || curatedOllamaDisplayName(model.id) || model.id,
+ name: localModelDisplayName(model.id, model.name, model.nameExplicit),
};
if (agent === 'pi') return named;
return {
id: named.id,
name: named.name,
+ ...(named.nameExplicit !== undefined ? { nameExplicit: named.nameExplicit } : {}),
...(named.contextWindow ? { contextWindow: named.contextWindow } : {}),
...(named.supportsImageInput ? { supportsImageInput: true } : {}),
...(named.reasoning
@@ -160,6 +164,24 @@ function toAgentModel(
};
}
+/** Old imports saved the execution ID as the name. Repair only that fallback,
+ * without probing Ollama or changing saved capabilities and agent membership. */
+function restoreManagedModelNames(provider: CustomProviderConfig): CustomProviderConfig {
+ let next = provider;
+ for (const agent of ['pi', 'claude-code', 'codex'] as const) {
+ const runtime = provider.runtimes[agent];
+ if (!runtime) continue;
+ const models = runtime.models.map((model) => {
+ if (!model.name || model.name !== model.id) return model;
+ const name = localModelDisplayName(model.id, model.name, model.nameExplicit);
+ return name === model.name ? model : { ...model, name };
+ });
+ if (models.every((model, index) => model === runtime.models[index])) continue;
+ next = { ...next, runtimes: { ...next.runtimes, [agent]: { ...runtime, models } } };
+ }
+ return next;
+}
+
export function migrateManagedOllamaProvider(
existing: CustomProviderConfig,
): CustomProviderConfig | null {
@@ -169,6 +191,7 @@ export function migrateManagedOllamaProvider(
runtimes: existing.runtimes,
};
if (matchesManagedOllamaV2Fingerprint(input)) {
+ existing = restoreManagedModelNames(existing);
const codex = existing.runtimes.codex;
if (codex?.wireProtocol === 'openai-responses') {
return {
@@ -182,6 +205,7 @@ export function migrateManagedOllamaProvider(
return existing;
}
if (!matchesLegacyPiOnlyOllamaFingerprint(input)) return null;
+ existing = restoreManagedModelNames(existing);
const piModels = existing.runtimes.pi?.models ?? [];
return {
...existing,
@@ -195,7 +219,7 @@ export function migrateManagedOllamaProvider(
};
}
-/** Catalog 加载前调用:不依赖打开设置页,把旧 Responses 行迁成 Chat 桥。 */
+/** Catalog 加载前调用:离线也补齐旧名称,并把旧 Responses 行迁成 Chat 桥。 */
export async function migrateManagedOllamaOnCatalogLoad(
stillCurrent: () => boolean = () => true,
): Promise<boolean> {