@sleep2agi/commhub-server 0.4.2 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +19 -4
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -34,6 +34,7 @@ const TaskSchema = z.object({
|
|
|
34
34
|
alias: z.string().min(1).max(200),
|
|
35
35
|
task: z.string().min(1).max(10000),
|
|
36
36
|
priority: z.enum(["high", "normal", "low"]).default("normal"),
|
|
37
|
+
from: z.string().max(200).optional(),
|
|
37
38
|
});
|
|
38
39
|
|
|
39
40
|
const BroadcastSchema = z.object({
|
|
@@ -49,7 +50,10 @@ const CORS_ORIGINS = process.env.COMMHUB_CORS_ORIGINS
|
|
|
49
50
|
|
|
50
51
|
function corsHeaders(req: Request): Record<string, string> {
|
|
51
52
|
const origin = req.headers.get("Origin") || "";
|
|
52
|
-
const allowed = CORS_ORIGINS.includes(origin)
|
|
53
|
+
const allowed = CORS_ORIGINS.includes(origin)
|
|
54
|
+
|| origin === "https://agent-network.vansin.me"
|
|
55
|
+
|| origin === "https://agent-network-dashboard.vercel.app"
|
|
56
|
+
? origin : "";
|
|
53
57
|
return {
|
|
54
58
|
"Access-Control-Allow-Origin": allowed,
|
|
55
59
|
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
@@ -156,16 +160,17 @@ Bun.serve({
|
|
|
156
160
|
}
|
|
157
161
|
const body = parsed.data;
|
|
158
162
|
const id = crypto.randomUUID();
|
|
163
|
+
const fromSession = body.from || "api";
|
|
159
164
|
db.run(
|
|
160
165
|
`INSERT INTO inbox (id, session_name, type, priority, content, from_session)
|
|
161
|
-
VALUES (?1, ?2, 'task', ?3, ?4,
|
|
162
|
-
[id, body.alias, body.priority, body.task]
|
|
166
|
+
VALUES (?1, ?2, 'task', ?3, ?4, ?5)`,
|
|
167
|
+
[id, body.alias, body.priority, body.task, fromSession]
|
|
163
168
|
);
|
|
164
169
|
// SSE push: 秒达
|
|
165
170
|
const pending = db.query<{ cnt: number }, [string]>(
|
|
166
171
|
"SELECT COUNT(*) as cnt FROM inbox WHERE session_name = ?1 AND acked = 0"
|
|
167
172
|
).get(body.alias);
|
|
168
|
-
pushEvent(body.alias, { type: "new_task", inbox_count: pending?.cnt ?? 1, priority: body.priority });
|
|
173
|
+
pushEvent(body.alias, { type: "new_task", inbox_count: pending?.cnt ?? 1, priority: body.priority, from: fromSession });
|
|
169
174
|
return withCors(req, Response.json({ ok: true, message_id: id }));
|
|
170
175
|
}
|
|
171
176
|
|
|
@@ -249,6 +254,16 @@ Bun.serve({
|
|
|
249
254
|
}
|
|
250
255
|
}
|
|
251
256
|
|
|
257
|
+
// ── REST: recent messages (for Dashboard communication graph) ──
|
|
258
|
+
if (url.pathname === "/api/messages") {
|
|
259
|
+
const limit = Number(url.searchParams.get("limit")) || 100;
|
|
260
|
+
const since = url.searchParams.get("since") ?? new Date(Date.now() - 3600000).toISOString().replace("T", " ").slice(0, 19);
|
|
261
|
+
const rows = db.query(
|
|
262
|
+
"SELECT id, session_name as to_alias, from_session as from_alias, type, priority, content, created_at FROM inbox WHERE created_at >= ?1 ORDER BY created_at DESC LIMIT ?2"
|
|
263
|
+
).all(since, limit);
|
|
264
|
+
return withCors(req, Response.json({ ok: true, messages: rows }));
|
|
265
|
+
}
|
|
266
|
+
|
|
252
267
|
// ── REST: recent completions ──
|
|
253
268
|
if (url.pathname === "/api/completions") {
|
|
254
269
|
const since = url.searchParams.get("since") ?? new Date(Date.now() - 86400000).toISOString();
|