Skip to content

Commit 73fa2e1

Browse files
committed
refactor: split gateway server methods
1 parent 4a6b33d commit 73fa2e1

File tree

8 files changed

+3065
-2860
lines changed

8 files changed

+3065
-2860
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- Agent tools: scope the Discord tool to Discord surface runs.
2525
- Agent tools: format verbose tool summaries without brackets, with unique emojis and `tool: detail` style.
2626
- Gateway: split server helpers/tests into hooks/session-utils/ws-log/net modules for better isolation; add unit coverage for hooks/session utils/ws log.
27+
- Gateway: extract WS method handling + HTTP/provider/constant helpers to shrink server wiring and improve testability.
28+
- Onboarding: fix Control UI basePath usage when showing/opening gateway URLs.
2729
- macOS Connections: move to sidebar + detail layout with structured sections and header actions.
2830
- macOS onboarding: increase window height so the permissions page fits without scrolling.
2931
- Thinking: default to low for reasoning-capable models when no /think or config default is set.

src/gateway/server-constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const MAX_PAYLOAD_BYTES = 512 * 1024; // cap incoming frame size
2+
export const MAX_BUFFERED_BYTES = 1.5 * 1024 * 1024; // per-connection send buffer limit
3+
4+
export const MAX_CHAT_HISTORY_MESSAGES_BYTES = 6 * 1024 * 1024; // keep history responses comfortably under client WS limits
5+
export const HANDSHAKE_TIMEOUT_MS = 10_000;
6+
export const TICK_INTERVAL_MS = 30_000;
7+
export const HEALTH_REFRESH_INTERVAL_MS = 60_000;
8+
export const DEDUPE_TTL_MS = 5 * 60_000;
9+
export const DEDUPE_MAX = 1000;

src/gateway/server-http.ts

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
import {
22
createServer as createHttpServer,
3-
type IncomingMessage,
43
type Server as HttpServer,
4+
type IncomingMessage,
55
type ServerResponse,
66
} from "node:http";
7-
import { type WebSocketServer } from "ws";
7+
import type { WebSocketServer } from "ws";
88
import { handleA2uiHttpRequest } from "../canvas-host/a2ui.js";
99
import type { CanvasHostHandler } from "../canvas-host/server.js";
10-
import { type HooksConfigResolved, extractHookToken, normalizeAgentPayload, normalizeHookHeaders, normalizeWakePayload, readJsonBody } from "./hooks.js";
11-
import { applyHookMappings } from "./hooks-mapping.js";
12-
import { handleControlUiHttpRequest } from "./control-ui.js";
1310
import type { createSubsystemLogger } from "../logging.js";
11+
import { handleControlUiHttpRequest } from "./control-ui.js";
12+
import {
13+
extractHookToken,
14+
type HooksConfigResolved,
15+
normalizeAgentPayload,
16+
normalizeHookHeaders,
17+
normalizeWakePayload,
18+
readJsonBody,
19+
} from "./hooks.js";
20+
import { applyHookMappings } from "./hooks-mapping.js";
1421

1522
type SubsystemLogger = ReturnType<typeof createSubsystemLogger>;
1623

1724
type HookDispatchers = {
18-
dispatchWakeHook: (value: { text: string; mode: "now" | "next-heartbeat" }) => void;
25+
dispatchWakeHook: (value: {
26+
text: string;
27+
mode: "now" | "next-heartbeat";
28+
}) => void;
1929
dispatchAgentHook: (value: {
2030
message: string;
2131
name: string;
@@ -46,13 +56,22 @@ export type HooksRequestHandler = (
4656
res: ServerResponse,
4757
) => Promise<boolean>;
4858

49-
export function createHooksRequestHandler(opts: {
50-
hooksConfig: HooksConfigResolved | null;
51-
bindHost: string;
52-
port: number;
53-
logHooks: SubsystemLogger;
54-
} & HookDispatchers): HooksRequestHandler {
55-
const { hooksConfig, bindHost, port, logHooks, dispatchAgentHook, dispatchWakeHook } = opts;
59+
export function createHooksRequestHandler(
60+
opts: {
61+
hooksConfig: HooksConfigResolved | null;
62+
bindHost: string;
63+
port: number;
64+
logHooks: SubsystemLogger;
65+
} & HookDispatchers,
66+
): HooksRequestHandler {
67+
const {
68+
hooksConfig,
69+
bindHost,
70+
port,
71+
logHooks,
72+
dispatchAgentHook,
73+
dispatchWakeHook,
74+
} = opts;
5675
return async (req, res) => {
5776
if (!hooksConfig) return false;
5877
const url = new URL(req.url ?? "/", `http://${bindHost}:${port}`);
@@ -97,7 +116,9 @@ export function createHooksRequestHandler(opts: {
97116
const headers = normalizeHookHeaders(req);
98117

99118
if (subPath === "wake") {
100-
const normalized = normalizeWakePayload(payload as Record<string, unknown>);
119+
const normalized = normalizeWakePayload(
120+
payload as Record<string, unknown>,
121+
);
101122
if (!normalized.ok) {
102123
sendJson(res, 400, { ok: false, error: normalized.error });
103124
return true;
@@ -108,7 +129,9 @@ export function createHooksRequestHandler(opts: {
108129
}
109130

110131
if (subPath === "agent") {
111-
const normalized = normalizeAgentPayload(payload as Record<string, unknown>);
132+
const normalized = normalizeAgentPayload(
133+
payload as Record<string, unknown>,
134+
);
112135
if (!normalized.ok) {
113136
sendJson(res, 400, { ok: false, error: normalized.error });
114137
return true;
@@ -178,8 +201,12 @@ export function createGatewayHttpServer(opts: {
178201
controlUiBasePath: string;
179202
handleHooksRequest: HooksRequestHandler;
180203
}): HttpServer {
181-
const { canvasHost, controlUiEnabled, controlUiBasePath, handleHooksRequest } =
182-
opts;
204+
const {
205+
canvasHost,
206+
controlUiEnabled,
207+
controlUiBasePath,
208+
handleHooksRequest,
209+
} = opts;
183210
const httpServer: HttpServer = createHttpServer((req, res) => {
184211
// Don't interfere with WebSocket upgrades; ws handles the 'upgrade' event.
185212
if (String(req.headers.upgrade ?? "").toLowerCase() === "websocket") return;

0 commit comments

Comments
 (0)