2026-09-07 20:36:39 +00:00
import { z } from "zod" ;
import { KomodoClient } from "../komodo-client.js" ;
import { ExecuteOperation , EXECUTE_REQUEST_MAP } from "../types.js" ;
2026-09-08 11:17:46 +00:00
// Operations that need {server: <value>} instead of {id: <value>}
const SERVER_SCOPED_OPS = new Set ([
"run_server_prune_images" ,
"run_server_prune_containers" ,
"run_server_prune_networks" ,
"rotate_all_server_keys" ,
"prune_buildx" ,
"prune_docker_builders" ,
"prune_system" ,
"prune_volumes" ,
"delete_image" ,
"delete_network" ,
"delete_volume" ,
]);
2026-09-07 20:36:39 +00:00
export const executeInputSchema = {
operation : ExecuteOperation.describe (
2026-09-08 11:17:46 +00:00
"Execute operation. Server-scoped ops (prune_*, delete_*) require the server name in the id field. RunSync requires the sync name/id in the id field." ,
2026-09-07 20:36:39 +00:00
),
2026-09-08 11:17:46 +00:00
id : z.string (). optional (). describe ( "Resource ID, name, or server name (depends on operation)" ),
2026-09-07 20:36:39 +00:00
params : z
. record ( z . string (), z . unknown ())
. optional ()
. describe ( "Additional parameters for the operation" ),
};
export async function handleExecute (
args : {
operation : z.infer < typeof ExecuteOperation >;
id? : string ;
params? : Record < string , unknown >;
},
client : KomodoClient ,
) : Promise < { content : { type : "text" ; text : string }[] } > {
const { operation , id , params } = args ;
const mapping = EXECUTE_REQUEST_MAP [ operation ];
if ( ! mapping ) {
throw new Error ( `Unknown execute operation: ${ operation } ` );
}
const requestParams : Record < string , unknown > = { ... params };
2026-09-08 11:17:46 +00:00
if ( id ) {
if ( SERVER_SCOPED_OPS . has ( operation )) {
requestParams . server = id ;
} else if ( operation === "run_sync" ) {
requestParams . sync = id ;
} else {
requestParams . id = id ;
}
}
2026-09-07 20:36:39 +00:00
const result = await client . rpc ( mapping . route , mapping . name , requestParams );
return {
content : [
{
type : "text" ,
text : JSON.stringify ( result , null , 2 ),
},
],
};
}