126 lines
3.4 KiB
JavaScript
126 lines
3.4 KiB
JavaScript
import {
|
|
InternalError,
|
|
NotFoundError,
|
|
ValidationError,
|
|
resolveNotebookPath
|
|
} from "./chunk-ER4KPD22.js";
|
|
import {
|
|
asyncHandler,
|
|
createApiModule,
|
|
defineApiModule,
|
|
successResponse
|
|
} from "./chunk-74TMTGBG.js";
|
|
|
|
// shared/modules/ai/index.ts
|
|
var AI_MODULE = defineApiModule({
|
|
id: "ai",
|
|
name: "AI",
|
|
basePath: "/ai",
|
|
order: 70,
|
|
version: "1.0.0",
|
|
frontend: {
|
|
enabled: false
|
|
},
|
|
backend: {
|
|
enabled: true
|
|
}
|
|
});
|
|
|
|
// api/modules/ai/routes.ts
|
|
import express from "express";
|
|
import { spawn } from "child_process";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import fs from "fs/promises";
|
|
import fsSync from "fs";
|
|
var __filename = fileURLToPath(import.meta.url);
|
|
var __dirname = path.dirname(__filename);
|
|
var router = express.Router();
|
|
var PYTHON_TIMEOUT_MS = 3e4;
|
|
var spawnPythonWithTimeout = (scriptPath, args, stdinContent, timeoutMs = PYTHON_TIMEOUT_MS) => {
|
|
return new Promise((resolve, reject) => {
|
|
const pythonProcess = spawn("python", args, {
|
|
env: { ...process.env }
|
|
});
|
|
let stdout = "";
|
|
let stderr = "";
|
|
let timeoutId = null;
|
|
const cleanup = () => {
|
|
if (timeoutId) {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = null;
|
|
}
|
|
};
|
|
timeoutId = setTimeout(() => {
|
|
cleanup();
|
|
pythonProcess.kill();
|
|
reject(new Error(`Python script timed out after ${timeoutMs}ms`));
|
|
}, timeoutMs);
|
|
pythonProcess.stdout.on("data", (data) => {
|
|
stdout += data.toString();
|
|
});
|
|
pythonProcess.stderr.on("data", (data) => {
|
|
stderr += data.toString();
|
|
});
|
|
pythonProcess.on("close", (code) => {
|
|
cleanup();
|
|
if (code !== 0) {
|
|
reject(new Error(`Python script exited with code ${code}. Stderr: ${stderr}`));
|
|
} else {
|
|
resolve(stdout);
|
|
}
|
|
});
|
|
pythonProcess.on("error", (err) => {
|
|
cleanup();
|
|
reject(new Error(`Failed to start python process: ${err.message}`));
|
|
});
|
|
pythonProcess.stdin.write(stdinContent);
|
|
pythonProcess.stdin.end();
|
|
});
|
|
};
|
|
router.post(
|
|
"/doubao",
|
|
asyncHandler(async (req, res) => {
|
|
const { task, path: relPath } = req.body;
|
|
if (!task) throw new ValidationError("Task is required");
|
|
if (!relPath) throw new ValidationError("Path is required");
|
|
const { fullPath } = resolveNotebookPath(relPath);
|
|
try {
|
|
await fs.access(fullPath);
|
|
} catch {
|
|
throw new NotFoundError("File not found");
|
|
}
|
|
const content = await fs.readFile(fullPath, "utf-8");
|
|
const projectRoot = path.resolve(__dirname, "..", "..", "..");
|
|
const scriptPath = path.join(projectRoot, "tools", "doubao", "main.py");
|
|
if (!fsSync.existsSync(scriptPath)) {
|
|
throw new InternalError(`Python script not found: ${scriptPath}`);
|
|
}
|
|
try {
|
|
const result = await spawnPythonWithTimeout(scriptPath, ["--task", task], content);
|
|
await fs.writeFile(fullPath, result, "utf-8");
|
|
successResponse(res, { message: "Task completed successfully" });
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : "Unknown error";
|
|
throw new InternalError(`AI task failed: ${message}`);
|
|
}
|
|
})
|
|
);
|
|
var createAiRoutes = () => router;
|
|
|
|
// api/modules/ai/index.ts
|
|
var createAiModule = () => {
|
|
return createApiModule(AI_MODULE, {
|
|
routes: (_container) => {
|
|
return createAiRoutes();
|
|
}
|
|
});
|
|
};
|
|
var ai_default = createAiModule;
|
|
|
|
export {
|
|
createAiRoutes,
|
|
createAiModule,
|
|
ai_default
|
|
};
|