-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgateway-main.ts
More file actions
169 lines (147 loc) · 6 KB
/
Copy pathgateway-main.ts
File metadata and controls
169 lines (147 loc) · 6 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
import type http from "node:http";
import os from "node:os";
import { loadGatewayConfig } from "./gateway/config.js";
import { startGateway } from "./gateway/server.js";
import { AgentBoxManager, LocalSpawner, K8sSpawner, ProcessSpawner } from "./gateway/agentbox/index.js";
import { createChannelBridge } from "./gateway/plugins/channel-bridge.js";
import { ChannelStore } from "./gateway/channels/channel-store.js";
import { ChannelManager } from "./gateway/channels/channel-manager.js";
import { createChannelRpcMethods } from "./gateway/channels/channel-rpc.js";
import { ConfigRepository } from "./gateway/db/repositories/config-repo.js";
import { WorkspaceRepository } from "./gateway/db/repositories/workspace-repo.js";
// Parse arguments
const args = process.argv.slice(2);
const useK8s = args.includes("--k8s");
const useProcess = args.includes("--process");
// Load config
const config = loadGatewayConfig();
console.log(`[gateway] Config: port=${config.port}, host=${config.host}`);
// Create Spawner (selects K8s / Process / Local based on arguments)
const spawner = useK8s
? new K8sSpawner({
namespace: process.env.SICLAW_K8S_NAMESPACE || "default",
image: process.env.SICLAW_AGENTBOX_IMAGE || "siclaw-agentbox:latest",
persistence: process.env.SICLAW_PERSISTENCE_ENABLED === "true"
? {
enabled: true,
claimName: process.env.SICLAW_PERSISTENCE_CLAIM_NAME || "siclaw-data",
}
: undefined,
})
: useProcess
? new ProcessSpawner()
: new LocalSpawner(4000);
console.log(`[gateway] Using spawner: ${spawner.name}`);
// Create AgentBox Manager
const agentBoxManager = new AgentBoxManager(spawner, {
namespace: process.env.SICLAW_K8S_NAMESPACE || "default",
});
agentBoxManager.startHealthCheck();
// Channel RPC + Manager are initialized after gateway starts (needs broadcast)
// Plugin HTTP handlers (dynamically loaded from overlay builds)
const extraHttpHandlers = new Map<string, (req: http.IncomingMessage, res: http.ServerResponse) => void>();
try {
const pluginPath = "./gateway/plugins/internal/index.js";
const { createHandlers } = await import(pluginPath);
for (const [k, v] of createHandlers()) extraHttpHandlers.set(k, v);
console.log("[gateway] Internal plugin loaded");
} catch {
// Internal plugin not available — running in open-source mode
}
// Instance ID for multi-replica cron coordination (K8s mode only)
const instanceId = useK8s
? (process.env.SICLAW_POD_NAME || os.hostname())
: undefined;
// Start HTTP + WebSocket server
const gateway = await startGateway({
config,
agentBoxManager,
spawner,
extraHttpHandlers,
instanceId,
});
// --- Channel subsystem ---
// ChannelStore: persists channel configs to DB
const channelStore = new ChannelStore(gateway.db);
await channelStore.init();
// Repositories for channel bridge
const configRepo = gateway.db ? new ConfigRepository(gateway.db) : undefined;
const wsRepo = gateway.db ? new WorkspaceRepository(gateway.db) : undefined;
// Create channel bridge (routes through AgentBox pods, not in-process sessions)
const channelBridge = createChannelBridge(agentBoxManager, gateway.broadcast, gateway.userStore, configRepo, gateway.buildCredentialPayload, wsRepo, gateway.agentBoxTlsOptions, gateway.cronService);
// Auto-remember chatId from inbound messages for notifications
channelBridge.onInbound = (channelId, chatId) => {
channelStore.setDefaultChatId(channelId, chatId);
};
// Create channel manager (with deps for bind command interception)
const channelManager = new ChannelManager(channelBridge, channelStore, {
userStore: gateway.userStore,
bindCodeStore: gateway.bindCodeStore,
});
// Register channel RPC methods
const channelRpc = createChannelRpcMethods(channelStore, channelManager);
for (const [name, handler] of channelRpc) {
gateway.rpcMethods.set(name, handler);
}
// --- Start in-process cron service ---
if (gateway.cronService) {
gateway.cronService.onNotify = (data) => {
const { userId, jobName, result, resultText, error } = data;
const text = result === "success"
? `**Scheduled task "${jobName}" completed**\n\n${resultText || "(no output)"}`
: `**Scheduled task "${jobName}" failed**\n\n${error || "Unknown error"}`;
channelManager.sendUserNotification(userId, text);
};
gateway.cronService.start().then(() => {
console.log("[gateway] Cron service started");
}).catch((err) => {
console.error("[gateway] Cron service start error:", err);
});
}
// --- Trigger webhook subsystem ---
// Triggers execute via the same internal agent-prompt API as cron
gateway.onWebhook = async (trigger, payload) => {
const sessionId = `trigger-${trigger.id}`;
const description = (trigger.configJson as any)?.description || trigger.name;
const prompt = `You received an event from trigger "${trigger.name}".
Trigger description: ${description}
Event payload:
\`\`\`json
${JSON.stringify(payload, null, 2)}
\`\`\`
Analyze this event and take appropriate action.`;
try {
const resp = await fetch(`http://localhost:${config.port}/api/internal/agent-prompt`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
userId: trigger.userId,
sessionId,
text: prompt,
timeoutMs: 300_000,
caller: "trigger",
}),
});
const data = await resp.json() as { status: string; error?: string };
if (data.status !== "success") {
console.error(`[trigger] ${trigger.id} failed: ${data.error}`);
}
} catch (err) {
console.error(`[trigger] Error for ${trigger.id}:`, err instanceof Error ? err.message : err);
}
};
// Boot all enabled channels
channelManager.bootFromStore().then(() => {
console.log("[gateway] Channel boot complete");
}).catch((err) => {
console.error("[gateway] Channel boot error:", err);
});
// Graceful shutdown
async function shutdown() {
console.log("\n[gateway] Shutting down...");
await channelManager.stopAll();
await gateway.close();
process.exit(0);
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);