Skip to content

Commit d5489d1

Browse files
committed
fix(gemini): 统一图片附件路径与项目临时目录解析
从技术上,这次改动把 Gemini 图片附件链路补齐到了完整闭环: - 主进程新增 Gemini 项目 shortId / temp / images 目录解析,兼容 Windows 与 WSL UNC 路径 - 图片保存 IPC 透传 provider、runtime、distro 与项目路径上下文,Gemini 可写入专用临时目录 - 前端发送阶段将 Gemini 图片 Chip 序列化为 CLI 需要的 @path 语法 - 统一图片文件识别逻辑,拖拽、粘贴、手输路径与 @ 选中的图片都按图片附件发送 - 修复 UNC 路径规范化错误压缩前导反斜杠导致的混合分隔符问题 从产品上,Gemini 现在能稳定识别项目级图片附件,不再把图片当普通文件路径发给 CLI, 同时不会影响 Codex / Claude 既有的路径插入与图片保存行为。 测试: - npm run test Signed-off-by: Lulu <58587930+lulu-sk@users.noreply.github.com>
1 parent b2734d5 commit d5489d1

File tree

12 files changed

+815
-84
lines changed

12 files changed

+815
-84
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import fs from "node:fs";
2+
import fsp from "node:fs/promises";
3+
import os from "node:os";
4+
import path from "node:path";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import {
7+
resolveGeminiImageDirWinPath,
8+
resolveGeminiProjectIdentifier,
9+
resolveGeminiProjectTempRootWinPath,
10+
} from "./projectTemp";
11+
import { getDistroHomeSubPathUNCAsync } from "../wsl";
12+
13+
vi.mock("../wsl", () => ({
14+
getDistroHomeSubPathUNCAsync: vi.fn(),
15+
}));
16+
17+
/**
18+
* 中文说明:创建临时测试目录,并确保用例结束后清理。
19+
*/
20+
function createTempDir(prefix: string): string {
21+
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
22+
}
23+
24+
describe("Gemini 项目临时目录解析", () => {
25+
const tempDirs: string[] = [];
26+
27+
afterEach(async () => {
28+
try { vi.restoreAllMocks(); } catch {}
29+
try { vi.unstubAllEnvs(); } catch {}
30+
delete process.env.GEMINI_CLI_HOME;
31+
while (tempDirs.length > 0) {
32+
const dir = tempDirs.pop();
33+
if (!dir) continue;
34+
try { await fsp.rm(dir, { recursive: true, force: true }); } catch {}
35+
}
36+
});
37+
38+
it("优先读取 projects.json 中已存在的 shortId 映射", async () => {
39+
const geminiHome = createTempDir("gemini-home-");
40+
tempDirs.push(geminiHome);
41+
process.env.GEMINI_CLI_HOME = geminiHome;
42+
43+
const normalizedProjectRoot = "g:\\projects\\demo";
44+
await fsp.mkdir(path.join(geminiHome, "tmp"), { recursive: true });
45+
await fsp.writeFile(
46+
path.join(geminiHome, "projects.json"),
47+
JSON.stringify({ projects: { [normalizedProjectRoot]: "demo-app" } }),
48+
"utf8",
49+
);
50+
51+
const projectId = await resolveGeminiProjectIdentifier({
52+
projectWinRoot: "G:\\Projects\\demo",
53+
runtimeEnv: "windows",
54+
});
55+
56+
expect(projectId).toBe("demo-app");
57+
});
58+
59+
it("registry 缺失时会根据 marker 反查 shortId", async () => {
60+
const geminiHome = createTempDir("gemini-home-");
61+
tempDirs.push(geminiHome);
62+
process.env.GEMINI_CLI_HOME = geminiHome;
63+
64+
const markerDir = path.join(geminiHome, "tmp", "demo-worktree");
65+
await fsp.mkdir(markerDir, { recursive: true });
66+
await fsp.writeFile(path.join(markerDir, ".project_root"), "/home/lulu/demo", "utf8");
67+
68+
const projectId = await resolveGeminiProjectIdentifier({
69+
projectWslRoot: "/home/lulu/demo",
70+
runtimeEnv: "wsl",
71+
});
72+
73+
expect(projectId).toBe("demo-worktree");
74+
});
75+
76+
it("无 registry/marker 时会按 Gemini slug 规则声明新的 shortId", async () => {
77+
const geminiHome = createTempDir("gemini-home-");
78+
tempDirs.push(geminiHome);
79+
process.env.GEMINI_CLI_HOME = geminiHome;
80+
81+
const projectId = await resolveGeminiProjectIdentifier({
82+
projectWinRoot: "G:\\Projects\\My Demo",
83+
runtimeEnv: "windows",
84+
});
85+
86+
expect(projectId).toBe("my-demo");
87+
expect(fs.existsSync(path.join(geminiHome, "tmp", "my-demo", ".project_root"))).toBe(true);
88+
});
89+
90+
it("WSL 运行时将 Gemini temp 目录解析为 UNC 路径", async () => {
91+
vi.spyOn(os, "platform").mockReturnValue("win32" as any);
92+
vi.mocked(getDistroHomeSubPathUNCAsync).mockResolvedValue("\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini");
93+
const originalReadFile = fsp.readFile.bind(fsp);
94+
vi.spyOn(fsp, "readFile").mockImplementation(async (targetPath: any, ...args: any[]) => {
95+
const normalizedPath = String(targetPath || "");
96+
if (normalizedPath === "\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini\\projects.json")
97+
return JSON.stringify({ projects: { "/home/lulu/demo": "demo" } }) as any;
98+
return await (originalReadFile as any)(targetPath, ...args);
99+
});
100+
101+
const tempRoot = await resolveGeminiProjectTempRootWinPath({
102+
projectWslRoot: "/home/lulu/demo",
103+
runtimeEnv: "wsl",
104+
distro: "Ubuntu-24.04",
105+
});
106+
const imageRoot = await resolveGeminiImageDirWinPath({
107+
projectWslRoot: "/home/lulu/demo",
108+
runtimeEnv: "wsl",
109+
distro: "Ubuntu-24.04",
110+
});
111+
112+
expect(tempRoot).toBe("\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini\\tmp\\demo");
113+
expect(imageRoot).toBe("\\\\wsl.localhost\\Ubuntu-24.04\\home\\lulu\\.gemini\\tmp\\demo\\images");
114+
});
115+
});

0 commit comments

Comments
 (0)