Webchat: auto-start server and simplify config
This commit is contained in:
@@ -7,7 +7,8 @@ const monitorWebProvider = vi.fn();
|
||||
const logWebSelfId = vi.fn();
|
||||
const waitForever = vi.fn();
|
||||
const monitorTelegramProvider = vi.fn();
|
||||
const startWebChatServer = vi.fn(async () => ({ port: 18788, token: null }));
|
||||
const startWebChatServer = vi.fn(async () => ({ port: 18788 }));
|
||||
const ensureWebChatServerFromConfig = vi.fn(async () => ({ port: 18788 }));
|
||||
|
||||
const runtime = {
|
||||
log: vi.fn(),
|
||||
@@ -29,6 +30,7 @@ vi.mock("../telegram/monitor.js", () => ({
|
||||
}));
|
||||
vi.mock("../webchat/server.js", () => ({
|
||||
startWebChatServer,
|
||||
ensureWebChatServerFromConfig,
|
||||
getWebChatServer: () => null,
|
||||
}));
|
||||
vi.mock("./deps.js", () => ({
|
||||
@@ -104,7 +106,7 @@ describe("cli program", () => {
|
||||
await program.parseAsync(["webchat", "--json"], { from: "user" });
|
||||
expect(startWebChatServer).toHaveBeenCalled();
|
||||
expect(runtime.log).toHaveBeenCalledWith(
|
||||
JSON.stringify({ port: 18788, token: null, basePath: "/webchat/", host: "127.0.0.1" }),
|
||||
JSON.stringify({ port: 18788, basePath: "/webchat/", host: "127.0.0.1" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,7 +17,11 @@ import {
|
||||
setHeartbeatsEnabled,
|
||||
type WebMonitorTuning,
|
||||
} from "../provider-web.js";
|
||||
import { startWebChatServer, getWebChatServer } from "../webchat/server.js";
|
||||
import {
|
||||
startWebChatServer,
|
||||
getWebChatServer,
|
||||
ensureWebChatServerFromConfig,
|
||||
} from "../webchat/server.js";
|
||||
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
||||
import { VERSION } from "../version.js";
|
||||
import {
|
||||
@@ -509,6 +513,16 @@ Examples:
|
||||
),
|
||||
);
|
||||
try {
|
||||
// Start loopback web chat server unless disabled.
|
||||
const webchatServer = await ensureWebChatServerFromConfig();
|
||||
if (webchatServer) {
|
||||
defaultRuntime.log(
|
||||
info(
|
||||
`webchat listening on http://127.0.0.1:${webchatServer.port}/webchat/`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await monitorWebProvider(
|
||||
Boolean(opts.verbose),
|
||||
undefined,
|
||||
@@ -748,7 +762,6 @@ Shows token usage per session when the agent reports it; set inbound.reply.agent
|
||||
const server = await startWebChatServer(port);
|
||||
const payload = {
|
||||
port: server.port,
|
||||
token: server.token ?? null,
|
||||
basePath: "/webchat/",
|
||||
host: "127.0.0.1",
|
||||
};
|
||||
|
||||
@@ -44,6 +44,11 @@ export type WebConfig = {
|
||||
reconnect?: WebReconnectConfig;
|
||||
};
|
||||
|
||||
export type WebChatConfig = {
|
||||
enabled?: boolean;
|
||||
port?: number;
|
||||
};
|
||||
|
||||
export type TelegramConfig = {
|
||||
botToken?: string;
|
||||
requireMention?: boolean;
|
||||
@@ -101,6 +106,7 @@ export type WarelayConfig = {
|
||||
};
|
||||
web?: WebConfig;
|
||||
telegram?: TelegramConfig;
|
||||
webchat?: WebChatConfig;
|
||||
};
|
||||
|
||||
// New branding path (preferred)
|
||||
@@ -226,6 +232,12 @@ const WarelaySchema = z.object({
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
webchat: z
|
||||
.object({
|
||||
enabled: z.boolean().optional(),
|
||||
port: z.number().int().positive().optional(),
|
||||
})
|
||||
.optional(),
|
||||
telegram: z
|
||||
.object({
|
||||
botToken: z.string().optional(),
|
||||
|
||||
@@ -20,7 +20,6 @@ const WEBCHAT_DEFAULT_PORT = 18788;
|
||||
type WebChatServerState = {
|
||||
server: http.Server;
|
||||
port: number;
|
||||
token?: string;
|
||||
};
|
||||
|
||||
let state: WebChatServerState | null = null;
|
||||
@@ -119,7 +118,7 @@ function notFound(res: http.ServerResponse) {
|
||||
res.end("Not Found");
|
||||
}
|
||||
|
||||
export async function startWebChatServer(port = WEBCHAT_DEFAULT_PORT, token?: string) {
|
||||
export async function startWebChatServer(port = WEBCHAT_DEFAULT_PORT) {
|
||||
if (state) return state;
|
||||
|
||||
const root = resolveWebRoot();
|
||||
@@ -147,7 +146,6 @@ export async function startWebChatServer(port = WEBCHAT_DEFAULT_PORT, token?: st
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
port,
|
||||
token: token ?? null,
|
||||
sessionKey,
|
||||
storePath,
|
||||
sessionId,
|
||||
@@ -202,11 +200,23 @@ export async function startWebChatServer(port = WEBCHAT_DEFAULT_PORT, token?: st
|
||||
server.listen(port, "127.0.0.1", () => resolve());
|
||||
});
|
||||
|
||||
state = { server, port, token };
|
||||
state = { server, port };
|
||||
logDebug(info(`webchat server listening on 127.0.0.1:${port}`));
|
||||
return state;
|
||||
}
|
||||
|
||||
export async function ensureWebChatServerFromConfig() {
|
||||
const cfg = loadConfig();
|
||||
if (cfg.webchat?.enabled === false) return null;
|
||||
const port = cfg.webchat?.port ?? WEBCHAT_DEFAULT_PORT;
|
||||
try {
|
||||
return await startWebChatServer(port);
|
||||
} catch (err) {
|
||||
logDebug(`webchat server failed to start: ${String(err)}`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export function getWebChatServer(): WebChatServerState | null {
|
||||
return state;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user