From 0dc40884a5ca2cb53bca76d88c176b4457c5baea Mon Sep 17 00:00:00 2001 From: redshift <213178690+1ftredsh@users.noreply.github.com> Date: Thu, 30 Apr 2026 09:21:01 +0530 Subject: [PATCH] Simplify client creation: fetch list first instead of catch-and-fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace ensureDaemonClient (try-create → catch 'already exists' → fetch list) with a single addDaemonClient that fetches the list first, checks if client exists, then creates only if needed. Returns { client, created } in all cases. --- src/integrations/index.ts | 4 +-- src/utils/clients.ts | 68 +++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/integrations/index.ts b/src/integrations/index.ts index 78e2fdb..993b237 100644 --- a/src/integrations/index.ts +++ b/src/integrations/index.ts @@ -1,7 +1,7 @@ import type { RoutstrdConfig } from "../utils/config"; import { logger } from "../utils/logger"; import { - ensureDaemonClient, + addDaemonClient, type DaemonClient, } from "../utils/clients"; import { installOpencodeIntegration } from "./opencode"; @@ -69,7 +69,7 @@ export async function setupIntegration( } const integrationConfig = CLIENT_CONFIGS[key]!; - const { client, created } = await ensureDaemonClient( + const { client, created } = await addDaemonClient( integrationConfig.name, integrationConfig.clientId, ); diff --git a/src/utils/clients.ts b/src/utils/clients.ts index 409c7ad..95c0372 100644 --- a/src/utils/clients.ts +++ b/src/utils/clients.ts @@ -83,12 +83,30 @@ export async function getClientsList(): Promise { export async function addDaemonClient( name: string, -): Promise<{ message?: string; client: DaemonClient }> { + clientId?: string, +): Promise<{ message?: string; client: DaemonClient; created: boolean }> { + const existingClients = await getClientsList(); + const existing = clientId + ? existingClients.find((c) => c.clientId === clientId) + : existingClients.find((c) => c.name === name); + + if (existing) { + const client: DaemonClient = { + id: existing.clientId, + name: existing.name, + apiKey: existing.apiKey, + createdAt: existing.createdAt, + lastUsed: existing.lastUsed, + }; + return { client, created: false }; + } + const result = await callDaemon("/clients/add", { method: "POST", body: { name }, }); + const output = result.output as | { message?: string; client?: DaemonClient } | undefined; @@ -97,41 +115,7 @@ export async function addDaemonClient( throw new Error(`Daemon did not return an API key for ${name}.`); } - return { message: output.message, client: output.client }; -} - -export async function ensureDaemonClient( - name: string, - clientId: string, -): Promise<{ client: DaemonClient; created: boolean }> { - try { - const { client } = await addDaemonClient(name); - return { client, created: true }; - } catch (error) { - const message = (error as Error).message || ""; - if (!message.includes("already exists")) { - throw error; - } - - const clients = await getClientsList(); - const entry = clients.find((c) => c.clientId === clientId); - - if (!entry?.apiKey) { - throw new Error( - `Client '${clientId}' already exists but could not be fetched from the daemon.`, - ); - } - - const client: DaemonClient = { - id: entry.clientId, - name: entry.name, - apiKey: entry.apiKey, - createdAt: entry.createdAt, - lastUsed: entry.lastUsed, - }; - - return { client, created: false }; - } + return { message: output.message, client: output.client, created: true }; } export async function listClientsAction(): Promise { @@ -243,7 +227,7 @@ export async function addClientAction(options: AddClientOptions): Promise if (!integrationFn || !integrationConfig) continue; try { - const { client, created } = await ensureDaemonClient( + const { client, created } = await addDaemonClient( integrationConfig.name, integrationConfig.clientId, ); @@ -281,7 +265,15 @@ export async function addClientAction(options: AddClientOptions): Promise const resolvedName = suffix ? `${options.name} ${suffix}` : options.name; try { - const { message, client } = await addDaemonClient(resolvedName); + const { message, client, created } = await addDaemonClient(resolvedName); + + if (!created) { + console.log(`Client '${resolvedName}' already exists.`); + console.log(`\n ID: ${client.id}`); + console.log(` Name: ${client.name}`); + console.log(` API Key: ${client.apiKey}`); + return; + } if (message) { console.log(message);