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(pi): handle catalog temp directory failures

Chris2d8621a9c4cc
diff --git a/apps/desktop/src/main/maker-host/__tests__/piNativeProviders.test.ts b/apps/desktop/src/main/maker-host/__tests__/piNativeProviders.test.ts
index c7d4e048e1..05e2a503f9 100644
--- a/apps/desktop/src/main/maker-host/__tests__/piNativeProviders.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/piNativeProviders.test.ts
@@ -4,6 +4,7 @@
*/
import { describe, expect, it, vi } from 'vitest';
import { existsSync } from 'node:fs';
+import fsp from 'node:fs/promises';
import path from 'node:path';
import { BUNDLED_CATALOG, type Catalog } from '@cindy/model-providers';
@@ -65,6 +66,20 @@ describe('buildPiNativeProvidersFromConfigs', () => {
});
const bundledPiPath = path.join(process.cwd(), 'apps/pi-bin/darwin-arm64/pi');
+ it('caches a null fallback when the PI probe temp directory cannot be created', async () => {
+ const binaryPath = path.join(process.cwd(), 'pi-temp-dir-probe-failure');
+ const mkdtempSpy = vi.spyOn(fsp, 'mkdtemp')
+ .mockRejectedValueOnce(new Error('temporary directory unavailable'));
+
+ try {
+ await expect(readPiBundledModels(binaryPath)).resolves.toBeNull();
+ await expect(readPiBundledModels(binaryPath)).resolves.toBeNull();
+ expect(mkdtempSpy).toHaveBeenCalledTimes(1);
+ } finally {
+ mkdtempSpy.mockRestore();
+ }
+ });
+
it.skipIf(process.platform !== 'darwin' || process.arch !== 'arm64' || !existsSync(bundledPiPath))(
'reads full APIs from the exact bundled PI binary without network access',
async () => {
diff --git a/apps/desktop/src/main/maker-host/pi-host.ts b/apps/desktop/src/main/maker-host/pi-host.ts
index 1d04c28544..22ce9e617f 100644
--- a/apps/desktop/src/main/maker-host/pi-host.ts
+++ b/apps/desktop/src/main/maker-host/pi-host.ts
@@ -297,8 +297,9 @@ export async function readPiBundledModels(
const cached = piBundledModelsByBinary.get(binaryPath);
if (cached) return cached;
const pending = (async () => {
- const configDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'cindy-pi-catalog-'));
+ let configDir: string | undefined;
try {
+ configDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'cindy-pi-catalog-'));
// Placeholder keys only make bundled providers visible to the offline
// catalog commands; no credential or network request is involved.
await fsp.writeFile(path.join(configDir, 'models.json'), JSON.stringify({
@@ -358,7 +359,9 @@ export async function readPiBundledModels(
});
return null;
} finally {
- await fsp.rm(configDir, { recursive: true, force: true }).catch(() => undefined);
+ if (configDir) {
+ await fsp.rm(configDir, { recursive: true, force: true }).catch(() => undefined);
+ }
}
})();
piBundledModelsByBinary.set(binaryPath, pending);