-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
96 lines (86 loc) · 3.1 KB
/
Copy pathserver.ts
File metadata and controls
96 lines (86 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import { createServer as createViteServer } from "vite";
import { decomposeTask, estimateEffort, getNudgeResponse } from "./src/server/gemini.js";
// Setup __dirname equivalent in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function startServer() {
const app = express();
const PORT = 3000;
// Middleware
app.use(express.json());
// API Routes
app.get("/api/health", (req, res) => {
res.json({ status: "ok", time: new Date().toISOString() });
});
// 1. AI Task Decomposition
app.post("/api/decompose", async (req, res) => {
try {
const { title, description, estimatedHours } = req.body;
if (!title) {
return res.status(400).json({ error: "Task title is required" });
}
const subtasks = await decomposeTask(title, description || "", estimatedHours);
res.json({ subtasks });
} catch (error: any) {
console.error("Decompose API error:", error);
res.status(500).json({ error: error.message || "Failed to decompose task" });
}
});
// 2. Intelligent Effort Estimation
app.post("/api/estimate", async (req, res) => {
try {
const { title, description } = req.body;
if (!title) {
return res.status(400).json({ error: "Task title is required" });
}
const estimate = await estimateEffort(title, description || "");
res.json(estimate);
} catch (error: any) {
console.error("Estimate API error:", error);
res.status(500).json({ error: error.message || "Failed to estimate effort" });
}
});
// 3. Nudge AI Assistant Chat
app.post("/api/nudge", async (req, res) => {
try {
const { message, context, history } = req.body;
if (!message) {
return res.status(400).json({ error: "Message is required" });
}
const chatHistory = history || [];
const chatContext = context || { tasks: [] };
const responseText = await getNudgeResponse(message, chatContext, chatHistory);
res.json({ response: responseText });
} catch (error: any) {
console.error("Nudge API error:", error);
res.status(500).json({ error: error.message || "Failed to get response from Nudge" });
}
});
// Vite Integration
if (process.env.NODE_ENV !== "production") {
console.log("Starting server in development mode with Vite HMR middleware...");
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
console.log("Starting server in production mode...");
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`[DoNext Server] Running at http://localhost:${PORT}`);
console.log(`Press Ctrl+C to stop`);
});
}
startServer().catch((err) => {
console.error("Failed to start DoNext server:", err);
process.exit(1);
});