From 57c66fe81340913e64752e4c2685f1c5ea5490d2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 15 Jan 2026 05:12:29 +0000 Subject: [PATCH] fix: clean up onboarding + channel selection types --- src/agents/pi-embedded-helpers/google.ts | 2 +- src/commands/onboard-channels.ts | 22 ++++++++------ .../onboarding/__tests__/test-utils.ts | 4 ++- src/commands/onboarding/plugin-install.ts | 26 ++++++++-------- src/gateway/server-methods/agent.ts | 3 +- src/gateway/server/plugins-http.test.ts | 30 ++++++++++++------- src/infra/outbound/channel-selection.ts | 6 ++-- 7 files changed, 56 insertions(+), 37 deletions(-) diff --git a/src/agents/pi-embedded-helpers/google.ts b/src/agents/pi-embedded-helpers/google.ts index 8e72215a40c..c0ec87e46e9 100644 --- a/src/agents/pi-embedded-helpers/google.ts +++ b/src/agents/pi-embedded-helpers/google.ts @@ -63,7 +63,7 @@ export function downgradeGeminiThinkingBlocks(messages: AgentMessage[]): AgentMe const trimmed = thinking.trim(); hasDowngraded = true; if (!trimmed) return []; - return [{ type: "text", text: thinking }]; + return [{ type: "text" as const, text: thinking }]; }); if (!hasDowngraded) { diff --git a/src/commands/onboard-channels.ts b/src/commands/onboard-channels.ts index 1a1c9ff5768..8424b40d82f 100644 --- a/src/commands/onboard-channels.ts +++ b/src/commands/onboard-channels.ts @@ -246,15 +246,19 @@ export async function setupChannels( options?.onSelection?.(selection); - const selectionNotes = new Map( - [ - ...installedPlugins.map((plugin) => [plugin.id, plugin.meta]), - ...catalogEntries.map((entry) => [entry.id, entry.meta]), - ].map(([id, meta]) => [ - id, - formatChannelSelectionLine(meta, formatDocsLink), - ]), - ); + const selectionNotes = new Map(); + for (const plugin of installedPlugins) { + selectionNotes.set( + plugin.id, + formatChannelSelectionLine(plugin.meta, formatDocsLink), + ); + } + for (const entry of catalogEntries) { + selectionNotes.set( + entry.id, + formatChannelSelectionLine(entry.meta, formatDocsLink), + ); + } const selectedLines = selection .map((channel) => selectionNotes.get(channel)) .filter((line): line is string => Boolean(line)); diff --git a/src/commands/onboarding/__tests__/test-utils.ts b/src/commands/onboarding/__tests__/test-utils.ts index ad45563b9f3..204213e01e4 100644 --- a/src/commands/onboarding/__tests__/test-utils.ts +++ b/src/commands/onboarding/__tests__/test-utils.ts @@ -6,7 +6,9 @@ import type { WizardPrompter } from "../../../wizard/prompts.js"; export const makeRuntime = (overrides: Partial = {}): RuntimeEnv => ({ log: vi.fn(), error: vi.fn(), - exit: vi.fn(), + exit: vi.fn((code: number) => { + throw new Error(`exit:${code}`); + }) as RuntimeEnv["exit"], ...overrides, }); diff --git a/src/commands/onboarding/plugin-install.ts b/src/commands/onboarding/plugin-install.ts index bbcde25b4c2..c419ae196d4 100644 --- a/src/commands/onboarding/plugin-install.ts +++ b/src/commands/onboarding/plugin-install.ts @@ -82,24 +82,26 @@ async function promptInstallChoice(params: { prompter: WizardPrompter; }): Promise { const { entry, localPath, prompter } = params; + const localOptions: Array<{ value: InstallChoice; label: string; hint?: string }> = localPath + ? [ + { + value: "local", + label: "Use local plugin path", + hint: localPath, + }, + ] + : []; const options: Array<{ value: InstallChoice; label: string; hint?: string }> = [ { value: "npm", label: `Download from npm (${entry.install.npmSpec})` }, - ...(localPath - ? [ - { - value: "local", - label: "Use local plugin path", - hint: localPath, - }, - ] - : []), + ...localOptions, { value: "skip", label: "Skip for now" }, ]; - return (await prompter.select({ + const initialValue: InstallChoice = localPath ? "local" : "npm"; + return await prompter.select({ message: `Install ${entry.meta.label} plugin?`, options, - initialValue: localPath ? "local" : "npm", - })) as InstallChoice; + initialValue, + }); } export async function ensureOnboardingPluginInstalled(params: { diff --git a/src/gateway/server-methods/agent.ts b/src/gateway/server-methods/agent.ts index e3dc99907fd..8613b5bdf0a 100644 --- a/src/gateway/server-methods/agent.ts +++ b/src/gateway/server-methods/agent.ts @@ -111,8 +111,9 @@ export const agentHandlers: GatewayRequestHandlers = { } const rawChannel = typeof request.channel === "string" ? request.channel.trim() : ""; if (rawChannel) { + const isKnownGatewayChannel = (value: string): boolean => isGatewayMessageChannel(value); const normalized = normalizeMessageChannel(rawChannel); - if (normalized && normalized !== "last" && !isGatewayMessageChannel(normalized)) { + if (normalized && normalized !== "last" && !isKnownGatewayChannel(normalized)) { respond( false, undefined, diff --git a/src/gateway/server/plugins-http.test.ts b/src/gateway/server/plugins-http.test.ts index 63707ebfda0..9466f735495 100644 --- a/src/gateway/server/plugins-http.test.ts +++ b/src/gateway/server/plugins-http.test.ts @@ -4,13 +4,21 @@ import { describe, expect, it, vi } from "vitest"; import { createGatewayPluginRequestHandler } from "./plugins-http.js"; import { createTestRegistry } from "./__tests__/test-utils.js"; -const makeResponse = () => - ({ +const makeResponse = (): { + res: ServerResponse; + setHeader: ReturnType; + end: ReturnType; +} => { + const setHeader = vi.fn(); + const end = vi.fn(); + const res = { headersSent: false, statusCode: 200, - setHeader: vi.fn(), - end: vi.fn(), - }) as unknown as ServerResponse; + setHeader, + end, + } as unknown as ServerResponse; + return { res, setHeader, end }; +}; describe("createGatewayPluginRequestHandler", () => { it("returns false when no handlers are registered", async () => { @@ -21,7 +29,8 @@ describe("createGatewayPluginRequestHandler", () => { registry: createTestRegistry(), log, }); - const handled = await handler({} as IncomingMessage, makeResponse()); + const { res } = makeResponse(); + const handled = await handler({} as IncomingMessage, res); expect(handled).toBe(false); }); @@ -38,7 +47,8 @@ describe("createGatewayPluginRequestHandler", () => { log: { warn: vi.fn() } as unknown as Parameters[0]["log"], }); - const handled = await handler({} as IncomingMessage, makeResponse()); + const { res } = makeResponse(); + const handled = await handler({} as IncomingMessage, res); expect(handled).toBe(true); expect(first).toHaveBeenCalledTimes(1); expect(second).toHaveBeenCalledTimes(1); @@ -63,15 +73,15 @@ describe("createGatewayPluginRequestHandler", () => { log, }); - const res = makeResponse(); + const { res, setHeader, end } = makeResponse(); const handled = await handler({} as IncomingMessage, res); expect(handled).toBe(true); expect(log.warn).toHaveBeenCalledWith(expect.stringContaining("boom")); expect(res.statusCode).toBe(500); - expect(res.setHeader).toHaveBeenCalledWith( + expect(setHeader).toHaveBeenCalledWith( "Content-Type", "text/plain; charset=utf-8", ); - expect(res.end).toHaveBeenCalledWith("Internal Server Error"); + expect(end).toHaveBeenCalledWith("Internal Server Error"); }); }); diff --git a/src/infra/outbound/channel-selection.ts b/src/infra/outbound/channel-selection.ts index a619e528683..47c5e383fc3 100644 --- a/src/infra/outbound/channel-selection.ts +++ b/src/infra/outbound/channel-selection.ts @@ -11,7 +11,7 @@ export type MessageChannelId = DeliverableMessageChannel; const getMessageChannels = () => listDeliverableMessageChannels(); -function isKnownChannel(value: string): value is MessageChannelId { +function isKnownChannel(value: string): boolean { return getMessageChannels().includes(value as MessageChannelId); } @@ -46,7 +46,7 @@ export async function listConfiguredMessageChannels( for (const plugin of listChannelPlugins()) { if (!isKnownChannel(plugin.id)) continue; if (await isPluginConfigured(plugin, cfg)) { - channels.push(plugin.id); + channels.push(plugin.id as MessageChannelId); } } return channels; @@ -62,7 +62,7 @@ export async function resolveMessageChannelSelection(params: { throw new Error(`Unknown channel: ${normalized}`); } return { - channel: normalized, + channel: normalized as MessageChannelId, configured: await listConfiguredMessageChannels(params.cfg), }; }