mirror of
https://github.com/basketikun/infinite-canvas.git
synced 2026-08-05 17:04:27 +08:00
feat(agent): add local file reveal functionality in system file manager
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import express, { type NextFunction, type Request, type Response } from "express";
|
||||
|
||||
import { runClaudeTurn } from "../agent/claude.js";
|
||||
@@ -72,6 +75,13 @@ export function startHttpServer() {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.type(attachment.type).send(Buffer.from(data, "base64"));
|
||||
}));
|
||||
app.post("/agent/local-file/reveal", route(async (req, res) => {
|
||||
const filePath = String(req.body?.path || "");
|
||||
if (!path.isAbsolute(filePath)) return res.status(400).json({ ok: false, error: "文件路径必须是绝对路径" });
|
||||
const file = await stat(filePath);
|
||||
await revealLocalFile(filePath, file.isDirectory());
|
||||
res.json({ ok: true });
|
||||
}));
|
||||
app.post("/api/tools", route(async (req, res) => res.json({ ok: true, result: await session.callTool(req.body?.name, req.body?.input || {}) })));
|
||||
app.get("/agent/codex/workspace", (_req, res) => {
|
||||
const workspace = ensureSiteWorkspace(config);
|
||||
@@ -225,6 +235,24 @@ function permissionMode(value: unknown): AgentPermissionMode {
|
||||
return value === "automatic" || value === "full" ? value : "request";
|
||||
}
|
||||
|
||||
/** 使用当前操作系统的文件管理器定位本地文件。 */
|
||||
function revealLocalFile(filePath: string, isDirectory: boolean) {
|
||||
const command = process.platform === "darwin" ? "open" : process.platform === "win32" ? "explorer.exe" : "xdg-open";
|
||||
const args = process.platform === "darwin"
|
||||
? ["-R", filePath]
|
||||
: process.platform === "win32"
|
||||
? [isDirectory ? filePath : `/select,${filePath}`]
|
||||
: [isDirectory ? filePath : path.dirname(filePath)];
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const child = spawn(command, args, { detached: true, stdio: "ignore" });
|
||||
child.once("spawn", () => {
|
||||
child.unref();
|
||||
resolve();
|
||||
});
|
||||
child.once("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
/** 结合服务配置解析当前请求 URL。 */
|
||||
function requestUrl(req: Request, config: CanvasAgentConfig) {
|
||||
return new URL(req.originalUrl || req.url || "/", config.url);
|
||||
|
||||
Reference in New Issue
Block a user