lint: format and stabilize gateway health

This commit is contained in:
Peter Steinberger
2025-12-10 18:00:33 +00:00
parent 27ad3b917f
commit 47a1f757a9
11 changed files with 43 additions and 36 deletions

View File

@@ -805,13 +805,13 @@ export async function runCommandReply(
} }
}, },
}); });
let stdoutUsed = stdout; const stdoutUsed = stdout;
let stderrUsed = stderr; const stderrUsed = stderr;
let codeUsed = code; const codeUsed = code;
let signalUsed = signal; const signalUsed = signal;
let killedUsed = killed; const killedUsed = killed;
let rpcAssistantText = extractRpcAssistantText(stdoutUsed); const rpcAssistantText = extractRpcAssistantText(stdoutUsed);
let rawStdout = stdoutUsed.trim(); const rawStdout = stdoutUsed.trim();
let mediaFromCommand: string[] | undefined; let mediaFromCommand: string[] | undefined;
const trimmed = stripRpcNoise(rawStdout); const trimmed = stripRpcNoise(rawStdout);
if (stderrUsed?.trim()) { if (stderrUsed?.trim()) {

View File

@@ -1,5 +1,5 @@
import { join } from "node:path";
import { tmpdir } from "node:os"; import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest"; import { afterEach, describe, expect, it, vi } from "vitest";
import * as tauRpc from "../process/tau-rpc.js"; import * as tauRpc from "../process/tau-rpc.js";

View File

@@ -769,7 +769,11 @@ export async function getReplyFromConfig(
const returnedSessionId = meta.agentMeta?.sessionId; const returnedSessionId = meta.agentMeta?.sessionId;
// TODO: remove once pi-mono persists stable session ids for custom --session paths. // TODO: remove once pi-mono persists stable session ids for custom --session paths.
const allowMetaSessionId = false; const allowMetaSessionId = false;
if (allowMetaSessionId && returnedSessionId && returnedSessionId !== sessionId) { if (
allowMetaSessionId &&
returnedSessionId &&
returnedSessionId !== sessionId
) {
const entry = sessionEntry ?? const entry = sessionEntry ??
sessionStore[sessionKey] ?? { sessionStore[sessionKey] ?? {
sessionId: returnedSessionId, sessionId: returnedSessionId,

View File

@@ -238,7 +238,10 @@ Examples:
const webchatPort = opts.webchatPort const webchatPort = opts.webchatPort
? Number.parseInt(String(opts.webchatPort), 10) ? Number.parseInt(String(opts.webchatPort), 10)
: undefined; : undefined;
if (webchatPort !== undefined && (Number.isNaN(webchatPort) || webchatPort <= 0)) { if (
webchatPort !== undefined &&
(Number.isNaN(webchatPort) || webchatPort <= 0)
) {
defaultRuntime.error("Invalid webchat port"); defaultRuntime.error("Invalid webchat port");
defaultRuntime.exit(1); defaultRuntime.exit(1);
} }
@@ -290,7 +293,12 @@ Examples:
const callGatewayCli = async ( const callGatewayCli = async (
method: string, method: string,
opts: { url?: string; token?: string; timeout?: string; expectFinal?: boolean }, opts: {
url?: string;
token?: string;
timeout?: string;
expectFinal?: boolean;
},
params?: unknown, params?: unknown,
) => ) =>
callGateway({ callGateway({

View File

@@ -1,11 +1,15 @@
import { loadConfig } from "../config/config.js"; import { loadConfig } from "../config/config.js";
import { loadSessionStore, resolveStorePath } from "../config/sessions.js"; import { loadSessionStore, resolveStorePath } from "../config/sessions.js";
import { callGateway } from "../gateway/call.js";
import { info } from "../globals.js"; import { info } from "../globals.js";
import type { RuntimeEnv } from "../runtime.js"; import type { RuntimeEnv } from "../runtime.js";
import { makeProxyFetch } from "../telegram/proxy.js"; import { makeProxyFetch } from "../telegram/proxy.js";
import { resolveHeartbeatSeconds } from "../web/reconnect.js"; import { resolveHeartbeatSeconds } from "../web/reconnect.js";
import { getWebAuthAgeMs, logWebSelfId, webAuthExists } from "../web/session.js"; import {
import { callGateway } from "../gateway/call.js"; getWebAuthAgeMs,
logWebSelfId,
webAuthExists,
} from "../web/session.js";
type TelegramProbe = { type TelegramProbe = {
ok: boolean; ok: boolean;

View File

@@ -6,11 +6,11 @@ import {
resolveStorePath, resolveStorePath,
type SessionEntry, type SessionEntry,
} from "../config/sessions.js"; } from "../config/sessions.js";
import { callGateway } from "../gateway/call.js";
import { info } from "../globals.js"; import { info } from "../globals.js";
import { buildProviderSummary } from "../infra/provider-summary.js"; import { buildProviderSummary } from "../infra/provider-summary.js";
import { peekSystemEvents } from "../infra/system-events.js"; import { peekSystemEvents } from "../infra/system-events.js";
import type { RuntimeEnv } from "../runtime.js"; import type { RuntimeEnv } from "../runtime.js";
import { callGateway } from "../gateway/call.js";
import { resolveHeartbeatSeconds } from "../web/reconnect.js"; import { resolveHeartbeatSeconds } from "../web/reconnect.js";
import { import {
getWebAuthAgeMs, getWebAuthAgeMs,

View File

@@ -233,7 +233,7 @@ function formatError(err: unknown): string {
return JSON.stringify(err, null, 2); return JSON.stringify(err, null, 2);
} }
async function refreshHealthSnapshot(opts?: { probe?: boolean }) { async function refreshHealthSnapshot(_opts?: { probe?: boolean }) {
if (!healthRefresh) { if (!healthRefresh) {
healthRefresh = (async () => { healthRefresh = (async () => {
const snap = await getHealthSnapshot(undefined); const snap = await getHealthSnapshot(undefined);

View File

@@ -15,7 +15,9 @@ describe("sendMessageTelegram", () => {
message_id: 42, message_id: 42,
chat: { id: chatId }, chat: { id: chatId },
}); });
const api = { sendMessage } as unknown as { sendMessage: typeof sendMessage }; const api = { sendMessage } as unknown as {
sendMessage: typeof sendMessage;
};
const res = await sendMessageTelegram(chatId, "_oops_", { const res = await sendMessageTelegram(chatId, "_oops_", {
token: "tok", token: "tok",

View File

@@ -327,10 +327,10 @@ export async function runWebHeartbeatOnce(opts: {
{ to, reason: "heartbeat-token", rawLength: replyPayload.text?.length }, { to, reason: "heartbeat-token", rawLength: replyPayload.text?.length },
"heartbeat skipped", "heartbeat skipped",
); );
console.log(success("heartbeat: ok (HEARTBEAT_OK)")); console.log(success("heartbeat: ok (HEARTBEAT_OK)"));
emitHeartbeatEvent({ status: "ok-token", to }); emitHeartbeatEvent({ status: "ok-token", to });
return; return;
} }
if (hasMedia) { if (hasMedia) {
heartbeatLogger.warn( heartbeatLogger.warn(

View File

@@ -38,7 +38,7 @@ export async function sendMessageWhatsApp(
mediaType = mediaType =
media.contentType === "audio/ogg" media.contentType === "audio/ogg"
? "audio/ogg; codecs=opus" ? "audio/ogg; codecs=opus"
: media.contentType ?? "application/octet-stream"; : (media.contentType ?? "application/octet-stream");
} else if (media.kind === "video") { } else if (media.kind === "video") {
text = caption ?? ""; text = caption ?? "";
} else if (media.kind === "image") { } else if (media.kind === "image") {
@@ -54,18 +54,9 @@ export async function sendMessageWhatsApp(
{ jid, hasMedia: Boolean(options.mediaUrl) }, { jid, hasMedia: Boolean(options.mediaUrl) },
"sending message", "sending message",
); );
const result = await (async () => { if (!active) throw new Error("Active web listener missing");
if (!active) throw new Error("Active web listener missing"); await active.sendComposingTo(to);
let mediaBuffer: Buffer | undefined; const result = await active.sendMessage(to, text, mediaBuffer, mediaType);
let mediaType: string | undefined;
if (options.mediaUrl) {
const media = await loadWebMedia(options.mediaUrl);
mediaBuffer = media.buffer;
mediaType = media.contentType;
}
await active.sendComposingTo(to);
return active.sendMessage(to, text, mediaBuffer, mediaType);
})();
const messageId = const messageId =
(result as { messageId?: string })?.messageId ?? "unknown"; (result as { messageId?: string })?.messageId ?? "unknown";
logInfo( logInfo(

View File

@@ -160,9 +160,7 @@ export async function __broadcastGatewayEventForTests() {
// no-op // no-op
} }
export async function ensureWebChatServerFromConfig( export async function ensureWebChatServerFromConfig(overridePort?: number) {
overridePort?: number,
) {
const cfg = loadConfig(); const cfg = loadConfig();
if (cfg.webchat?.enabled === false) return null; if (cfg.webchat?.enabled === false) return null;
const port = overridePort ?? cfg.webchat?.port ?? WEBCHAT_DEFAULT_PORT; const port = overridePort ?? cfg.webchat?.port ?? WEBCHAT_DEFAULT_PORT;