-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
185 lines (164 loc) · 5.89 KB
/
server.ts
File metadata and controls
185 lines (164 loc) · 5.89 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import express from "express";
import { createServer as createViteServer } from "vite";
import multer from "multer";
import fs from "fs";
import http from "http";
import { WebSocketServer } from "ws";
const app = express();
const PORT = 3000;
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
const DEV_VERSION = "dev-fallback";
const DEV_COMPILED_CHANNELS = ["telegram", "weixin", "feishu"];
const DEV_WORKSPACE_DIR = ".dev-workspace";
const DEV_MEMORY_DIR = `${DEV_WORKSPACE_DIR}/memory`;
const devState = {
skills: [] as any[],
cronJobs: [] as any[],
channelDrafts: {} as Record<string, any>,
};
const uploadDir = "uploads";
if (!fs.existsSync(uploadDir)) fs.mkdirSync(uploadDir);
const storage = multer.diskStorage({
destination: (_, __, cb) => cb(null, uploadDir),
filename: (_, file, cb) => cb(null, Date.now() + "-" + file.originalname),
});
app.use(express.json());
app.use("/uploads", express.static(uploadDir));
if (!fs.existsSync(DEV_WORKSPACE_DIR)) fs.mkdirSync(DEV_WORKSPACE_DIR, { recursive: true });
if (!fs.existsSync(DEV_MEMORY_DIR)) fs.mkdirSync(DEV_MEMORY_DIR, { recursive: true });
const rawConfig = {
gateway: { host: "0.0.0.0", port: 18790, token: "" },
agents: {
defaults: { max_tool_iterations: 10, max_tokens: 4000 },
agents: {
main: { enabled: true, kind: "agent", role: "world-mind", prompt_file: "agents/main/AGENT.md" },
innkeeper: { enabled: true, kind: "npc", role: "host", persona: "Keeps the inn running and tracks rumors.", home_location: "inn", default_goals: ["keep guests calm"], prompt_file: "agents/innkeeper/AGENT.md" },
},
},
models: {
providers: {
openai: { enabled: true, api_key: "sk-...", api_base: "https://api.openai.com/v1" },
claude: { enabled: true, api_key: "sk-ant...", api_base: "https://api.anthropic.com" },
deepseek: { enabled: false },
groq: { enabled: false },
google: { enabled: false },
custom: { enabled: false }
}
},
channels: {
weixin: { enabled: true, base_url: "https://ilinkai.weixin.qq.com" },
telegram: { enabled: false },
feishu: { enabled: false }
},
system: { logging: { level: "info" } },
};
function buildNormalizedConfig() {
const providers: any = {};
Object.keys((rawConfig.models as any).providers).forEach(p => {
providers[p] = { enabled: (rawConfig.models as any).providers[p].enabled };
});
return {
core: {
default_provider: "openai",
default_model: "gpt-4o",
main_agent: "main",
agents: {
main: { enabled: true, role: "world-mind", prompt: "agents/main/AGENT.md", runtime_class: "default" },
innkeeper: { enabled: true, role: "host", prompt: "agents/innkeeper/AGENT.md", runtime_class: "default" },
},
tools: {},
gateway: { host: rawConfig.gateway.host, port: rawConfig.gateway.port },
},
runtime: {
providers: providers,
router: {},
},
};
}
app.get("/api/config", (req, res) => {
if (String(req.query.mode || "").trim() === "normalized") {
return res.json({
ok: true,
config: buildNormalizedConfig(),
raw_config: rawConfig,
hot_reload_fields: [],
hot_reload_field_details: [],
});
}
res.json(rawConfig);
});
app.post("/api/config", (_req, res) => {
const next = _req.body && typeof _req.body === "object" ? _req.body : {};
for (const key of Object.keys(rawConfig)) {
delete (rawConfig as any)[key];
}
Object.assign(rawConfig, next);
res.json({ ok: true });
});
// Mock Channel Draft APIs
app.post("/api/channels/draft", (req, res) => {
const { name, config } = req.body || {};
if (!name) return res.status(400).json({ error: "name required" });
devState.channelDrafts[name] = config;
console.log(`[DEV] Saved draft for channel: ${name}`, config);
res.json({ ok: true });
});
app.post("/api/channels/draft/commit", (req, res) => {
const { name } = req.body || {};
if (!name) return res.status(400).json({ error: "name required" });
const draft = devState.channelDrafts[name];
if (draft) {
if (!rawConfig.channels) rawConfig.channels = {};
(rawConfig.channels as any)[name] = draft;
console.log(`[DEV] Committed draft for channel: ${name}`);
delete devState.channelDrafts[name];
}
res.json({ ok: true });
});
app.post("/api/rpc/provider", (req, res) => {
const method = String(req.body?.method || "").trim();
const params = req.body?.params || {};
if (method === "provider.list_models") {
return res.json({
ok: true,
result: {
provider: String(params?.provider || "openai"),
models: ["gpt-4o", "gpt-4o-mini", "claude-3-5-sonnet", "deepseek-chat"],
default_model: "gpt-4o-mini",
},
request_id: req.body?.request_id || "",
});
}
return res.status(400).json({ ok: false, error: { code: "invalid_argument", message: "unknown method" }, request_id: req.body?.request_id || "" });
});
app.get("/api/version", (_req, res) => {
res.json({
gateway_version: DEV_VERSION,
webui_version: DEV_VERSION,
compiled_channels: DEV_COMPILED_CHANNELS,
});
});
app.get("/api/cron", (req, res) => res.json({ jobs: devState.cronJobs }));
app.get("/api/skills", (_req, res) => res.json({ skills: devState.skills, clawhub_installed: false, clawhub_path: "" }));
app.get("/api/provider/runtime", (_req, res) => {
const items = Object.keys((rawConfig.models as any).providers).map(p => ({
name: p,
api_state: "healthy",
auth: "bearer"
}));
res.json({ items });
});
wss.on("connection", () => {
// ... basic ws handling ...
});
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({ server: { middlewareMode: true }, appType: "spa" });
app.use(vite.middlewares);
} else {
app.use(express.static("dist"));
app.get("*", (_req, res) => res.sendFile("dist/index.html", { root: "." }));
}
server.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});