diff --git a/src/cli.ts b/src/cli.ts index 3ee9945..e9ebb2e 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -734,6 +734,34 @@ clientsCmd } }); +clientsCmd + .command("delete ") + .description("Delete a client by its ID") + .action(async (id: string) => { + await ensureDaemonRunning(); + + const result = await callDaemon("/clients/delete", { + method: "POST", + body: { id }, + }); + + if (result.error) { + console.log(result.error); + process.exit(1); + } + + const output = result.output as + | { + message: string; + id: string; + } + | undefined; + + if (output) { + console.log(output.message); + } + }); + clientsCmd .command("add") .description("Add a new client or set up client integrations") diff --git a/src/daemon/http/index.ts b/src/daemon/http/index.ts index 0c3307e..4ecbbb4 100644 --- a/src/daemon/http/index.ts +++ b/src/daemon/http/index.ts @@ -831,6 +831,65 @@ export function createDaemonRequestHandler(deps: { return; } + if (req.method === "POST" && url.pathname === "/clients/delete") { + try { + const bodyText = await readBody(req); + const body = bodyText ? JSON.parse(bodyText) : {}; + const id = body.id as string | undefined; + + if (!id || typeof id !== "string" || id.trim() === "") { + res.writeHead(400, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + error: + "Missing required 'id' field (must be a non-empty string).", + }), + ); + return; + } + + const state = deps.store.getState(); + const existingClients = state.clientIds || []; + const index = existingClients.findIndex( + (c: { clientId: string }) => c.clientId === id, + ); + + if (index === -1) { + res.writeHead(404, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + error: `Client with id '${id}' not found.`, + }), + ); + return; + } + + const removedClient = existingClients[index]; + const updatedClients = existingClients.filter( + (_c: unknown, i: number) => i !== index, + ); + + deps.store.getState().setClientIds(updatedClients); + + logger.log( + `Deleted client '${removedClient.name}' with id '${id}'`, + ); + + res.writeHead(200, { "Content-Type": "application/json" }); + res.end( + JSON.stringify({ + output: { + message: `Client '${removedClient.name}' deleted successfully`, + id, + }, + }), + ); + } catch (error) { + respondWithError(res, error); + } + return; + } + if (req.method === "GET" && url.pathname === "/providers") { try { const state = deps.store.getState();