import { c as stringEnum, i as jsonResult, o as readNumberParam, r as errorResult, s as readStringParam, u as Type } from "../../tool-helpers-CVAcqiCy.mjs";
import { getCredentialVault } from "../services/credential-vault.mjs";
import { checkToolConfig } from "../services/tool-config-service.mjs";
//#region extensions/crypto/src/tools/hummingbot.ts
/**
* Hummingbot Tool — market-making bot control via HummingbotClient
*
* Wraps the @clawnch/clawncher-sdk HummingbotClient (76 methods) behind a single
* OpenClaw tool with action-based dispatch. Requires a running Hummingbot instance.
*
* Env vars: HUMMINGBOT_API_URL, HUMMINGBOT_USERNAME, HUMMINGBOT_PASSWORD
*/
const HummingbotSchema = Type.Object({
	action: stringEnum([
		"status",
		"portfolio",
		"order",
		"cancel_order",
		"active_orders",
		"executor",
		"stop_executor",
		"executors",
		"executor_types",
		"market_data",
		"candles",
		"orderbook",
		"funding_rate",
		"bot_deploy",
		"bot_status",
		"bot_stop",
		"bot_logs",
		"bot_history",
		"controllers",
		"controller_configs",
		"gateway_status",
		"gateway_start",
		"gateway_stop",
		"leverage",
		"history",
		"templates",
		"backtest",
		"connectors",
		"accounts",
		"scripts",
		"pnl",
		"clmm_positions",
		"routines",
		"routine_start",
		"routine_stop",
		"dashboard"
	], { description: "status: health check. portfolio: balances/positions. order: place order. cancel_order: cancel. active_orders: list open. executor: create executor. bot_deploy/bot_status/bot_stop/bot_logs/bot_history: manage bots. market_data/candles/orderbook: prices & data. templates: strategy templates. gateway_status/start/stop: DEX gateway. leverage: set leverage. history: search trade history. backtest: run backtest. pnl: PnL tracking. clmm_positions: CLMM LP positions. routines: list auto-discoverable routines. routine_start/stop: manage routines. dashboard: Condor portfolio dashboard." }),
	connector: Type.Optional(Type.String({ description: "Connector name (e.g. \"binance\", \"uniswap\")" })),
	trading_pair: Type.Optional(Type.String({ description: "Trading pair (e.g. \"ETH-USDT\")" })),
	account: Type.Optional(Type.String({ description: "Account name" })),
	trade_type: Type.Optional(Type.String({ description: "\"BUY\" or \"SELL\"" })),
	amount: Type.Optional(Type.String({ description: "Order/trade amount" })),
	price: Type.Optional(Type.String({ description: "Limit price" })),
	order_type: Type.Optional(Type.String({ description: "\"MARKET\", \"LIMIT\", or \"LIMIT_MAKER\"" })),
	order_id: Type.Optional(Type.String({ description: "Client order ID for cancel" })),
	executor_type: Type.Optional(Type.String({ description: "\"position_executor\", \"dca_executor\", \"grid_executor\", \"order_executor\", \"arbitrage_executor\"" })),
	executor_id: Type.Optional(Type.String({ description: "Executor ID" })),
	executor_config: Type.Optional(Type.String({ description: "JSON executor config" })),
	bot_name: Type.Optional(Type.String({ description: "Bot name" })),
	controllers_config: Type.Optional(Type.String({ description: "JSON array of controller config names" })),
	template: Type.Optional(Type.String({ description: "Strategy template name" })),
	template_overrides: Type.Optional(Type.String({ description: "JSON overrides for template" })),
	interval: Type.Optional(Type.String({ description: "Candle interval: 1m, 5m, 15m, 30m, 1h, 4h, 1d" })),
	days: Type.Optional(Type.Number({ description: "Number of days for candle/history lookback" })),
	leverage: Type.Optional(Type.Number({ description: "Leverage multiplier" })),
	position_mode: Type.Optional(Type.String({ description: "\"HEDGE\" or \"ONE-WAY\"" })),
	limit: Type.Optional(Type.Number({ description: "Result limit" })),
	backtest_config: Type.Optional(Type.String({ description: "JSON backtest config or controller config name" })),
	start_time: Type.Optional(Type.Number({ description: "Start timestamp (seconds)" })),
	end_time: Type.Optional(Type.Number({ description: "End timestamp (seconds)" })),
	log_type: Type.Optional(Type.String({ description: "\"error\", \"general\", or \"all\"" })),
	passphrase: Type.Optional(Type.String({ description: "Gateway passphrase" })),
	image: Type.Optional(Type.String({ description: "Docker image for gateway/bot" }))
});
let _client = null;
async function getClient() {
	if (_client) return _client;
	const { HummingbotClient } = await import("../../dist-CXAY4yCt.mjs");
	const apiUrl = process.env.HUMMINGBOT_API_URL;
	const username = getCredentialVault().getSecret("bot.hummingbot.username", "hummingbot");
	const password = getCredentialVault().getSecret("bot.hummingbot.password", "hummingbot");
	if (!apiUrl || !username || !password) throw new Error("Hummingbot not configured. Set HUMMINGBOT_API_URL, HUMMINGBOT_USERNAME, and HUMMINGBOT_PASSWORD.");
	_client = new HummingbotClient({
		apiUrl,
		username,
		password
	});
	return _client;
}
function createHummingbotTool() {
	return {
		name: "hummingbot",
		label: "Hummingbot",
		ownerOnly: true,
		description: "Control Hummingbot/Condor market-making bots. Place orders, manage executors, deploy bots with strategies, check portfolio + PnL, CLMM LP positions, auto-routines, get market data, run backtests. Requires a running Hummingbot/Condor instance (set HUMMINGBOT_API_URL).",
		parameters: HummingbotSchema,
		execute: async (_toolCallId, args) => {
			const notReady = checkToolConfig("hummingbot");
			if (notReady) return notReady;
			const p = args;
			const action = readStringParam(p, "action", { required: true });
			try {
				const client = await getClient();
				switch (action) {
					case "status": return jsonResult(await client.checkHealth());
					case "portfolio": return jsonResult(await client.getPortfolioOverview({
						accountNames: p.account ? [p.account] : void 0,
						connectorNames: p.connector ? [p.connector] : void 0,
						includeBalances: true,
						includePerpPositions: true,
						includeActiveOrders: true
					}));
					case "order": {
						const connector = readStringParam(p, "connector", { required: true });
						const tradingPair = readStringParam(p, "trading_pair", { required: true });
						const tradeType = readStringParam(p, "trade_type", { required: true });
						const amount = readStringParam(p, "amount", { required: true });
						return jsonResult(await client.placeOrder({
							connectorName: connector,
							tradingPair,
							tradeType: tradeType.toUpperCase(),
							amount,
							orderType: (readStringParam(p, "order_type") || "LIMIT").toUpperCase(),
							price: readStringParam(p, "price"),
							accountName: readStringParam(p, "account")
						}));
					}
					case "cancel_order": {
						const connector = readStringParam(p, "connector", { required: true });
						const orderId = readStringParam(p, "order_id", { required: true });
						const account = readStringParam(p, "account") || "master_account";
						return jsonResult(await client.cancelOrder(account, connector, orderId));
					}
					case "active_orders": return jsonResult(await client.getActiveOrders({
						connectorNames: p.connector ? [p.connector] : void 0,
						tradingPairs: p.trading_pair ? [p.trading_pair] : void 0,
						limit: readNumberParam(p, "limit") ?? 50
					}));
					case "executor": {
						const configStr = readStringParam(p, "executor_config", { required: true });
						const config = JSON.parse(configStr);
						return jsonResult(await client.createExecutor(config));
					}
					case "stop_executor": {
						const eid = readStringParam(p, "executor_id", { required: true });
						return jsonResult(await client.stopExecutor(eid));
					}
					case "executors": return jsonResult(await client.searchExecutors({
						executorTypes: p.executor_type ? [p.executor_type] : void 0,
						connectorNames: p.connector ? [p.connector] : void 0,
						tradingPairs: p.trading_pair ? [p.trading_pair] : void 0,
						limit: readNumberParam(p, "limit") ?? 20
					}));
					case "executor_types": return jsonResult(await client.getExecutorTypes());
					case "market_data": {
						const connector = readStringParam(p, "connector", { required: true });
						const pairs = readStringParam(p, "trading_pair", { required: true });
						return jsonResult(await client.getPrices(connector, pairs.split(",").map((s) => s.trim())));
					}
					case "candles": {
						const connector = readStringParam(p, "connector", { required: true });
						const pair = readStringParam(p, "trading_pair", { required: true });
						return jsonResult(await client.getCandles(connector, pair, readStringParam(p, "interval") ?? "1h", readNumberParam(p, "days") ?? 1));
					}
					case "orderbook": {
						const connector = readStringParam(p, "connector", { required: true });
						const pair = readStringParam(p, "trading_pair", { required: true });
						return jsonResult(await client.getOrderBook(connector, pair));
					}
					case "funding_rate": {
						const connector = readStringParam(p, "connector", { required: true });
						const pair = readStringParam(p, "trading_pair", { required: true });
						return jsonResult(await client.getFundingRate(connector, pair));
					}
					case "bot_deploy": {
						const botName = readStringParam(p, "bot_name", { required: true });
						const configStr = readStringParam(p, "controllers_config", { required: true });
						return jsonResult(await client.deployBot({
							botName,
							controllersConfig: JSON.parse(configStr),
							accountName: readStringParam(p, "account"),
							image: readStringParam(p, "image")
						}));
					}
					case "bot_status": return jsonResult(await client.getBotsStatus());
					case "bot_stop": {
						const botName = readStringParam(p, "bot_name", { required: true });
						return jsonResult(await client.stopBot(botName));
					}
					case "bot_logs": {
						const botName = readStringParam(p, "bot_name", { required: true });
						return jsonResult(await client.getBotLogs({
							botName,
							logType: readStringParam(p, "log_type") ?? "all",
							limit: readNumberParam(p, "limit") ?? 50
						}));
					}
					case "bot_history": {
						const botName = readStringParam(p, "bot_name", { required: true });
						return jsonResult(await client.getBotHistory(botName, readNumberParam(p, "days") ?? 7));
					}
					case "controllers": return jsonResult(await client.listControllers());
					case "controller_configs": return jsonResult(await client.listControllerConfigs());
					case "gateway_status": return jsonResult(await client.getGatewayStatus());
					case "gateway_start": {
						const passphrase = readStringParam(p, "passphrase", { required: true });
						const img = readStringParam(p, "image", { required: true });
						return jsonResult(await client.startGateway({
							passphrase,
							image: img
						}));
					}
					case "gateway_stop": return jsonResult(await client.stopGateway());
					case "leverage": {
						const account = readStringParam(p, "account", { required: true });
						const connector = readStringParam(p, "connector", { required: true });
						return jsonResult(await client.setPositionModeAndLeverage({
							accountName: account,
							connectorName: connector,
							tradingPair: readStringParam(p, "trading_pair"),
							positionMode: readStringParam(p, "position_mode"),
							leverage: readNumberParam(p, "leverage")
						}));
					}
					case "history": return jsonResult(await client.searchHistory({
						dataType: "orders",
						connectorNames: p.connector ? [p.connector] : void 0,
						tradingPairs: p.trading_pair ? [p.trading_pair] : void 0,
						limit: readNumberParam(p, "limit") ?? 50,
						startTime: readNumberParam(p, "start_time"),
						endTime: readNumberParam(p, "end_time")
					}));
					case "templates": {
						const name = readStringParam(p, "template");
						if (name) {
							const overridesStr = readStringParam(p, "template_overrides");
							if (overridesStr) return jsonResult({
								template: name,
								config: client.buildFromTemplate(name, JSON.parse(overridesStr))
							});
							return jsonResult(client.getStrategyTemplate(name) ?? { error: `Template "${name}" not found` });
						}
						return jsonResult(client.getStrategyTemplates());
					}
					case "backtest": {
						const configStr = readStringParam(p, "backtest_config", { required: true });
						const startTime = readNumberParam(p, "start_time", { required: true });
						const endTime = readNumberParam(p, "end_time", { required: true });
						let config;
						try {
							config = JSON.parse(configStr);
						} catch {
							config = configStr;
						}
						return jsonResult(await client.runBacktest({
							config,
							startTime,
							endTime
						}));
					}
					case "connectors": return jsonResult(await client.listConnectors());
					case "accounts": return jsonResult(await client.listAccounts());
					case "scripts": return jsonResult(await client.listScripts());
					case "pnl":
						if (typeof client.getPnL !== "function") return errorResult("PnL tracking requires Condor backend. Upgrade from Hummingbot to Condor: https://github.com/hummingbot/condor");
						return jsonResult(await client.getPnL({
							connectorNames: p.connector ? [p.connector] : void 0,
							tradingPairs: p.trading_pair ? [p.trading_pair] : void 0,
							days: readNumberParam(p, "days") ?? 7
						}));
					case "clmm_positions":
						if (typeof client.getCLMMPositions !== "function") return errorResult("CLMM LP positions require Condor backend. Upgrade from Hummingbot to Condor: https://github.com/hummingbot/condor");
						return jsonResult(await client.getCLMMPositions({
							connectorNames: p.connector ? [p.connector] : void 0,
							accountNames: p.account ? [p.account] : void 0
						}));
					case "routines":
						if (typeof client.listRoutines !== "function") return errorResult("Auto-discoverable routines require Condor backend. Upgrade from Hummingbot to Condor: https://github.com/hummingbot/condor");
						return jsonResult(await client.listRoutines());
					case "routine_start": {
						if (typeof client.startRoutine !== "function") return errorResult("Routines require Condor backend.");
						const name = readStringParam(p, "bot_name", { required: true });
						return jsonResult(await client.startRoutine(name));
					}
					case "routine_stop": {
						if (typeof client.stopRoutine !== "function") return errorResult("Routines require Condor backend.");
						const name = readStringParam(p, "bot_name", { required: true });
						return jsonResult(await client.stopRoutine(name));
					}
					case "dashboard":
						if (typeof client.getDashboard !== "function") return errorResult("Portfolio dashboard requires Condor backend. Upgrade from Hummingbot to Condor: https://github.com/hummingbot/condor");
						return jsonResult(await client.getDashboard({
							includeBalances: true,
							includePnL: true,
							includeCLMM: true,
							includeRoutines: true
						}));
					default: return errorResult(`Unknown hummingbot action: ${action}`);
				}
			} catch (err) {
				return errorResult(`Hummingbot error: ${err instanceof Error ? err.message : String(err)}`);
			}
		}
	};
}
//#endregion
export { createHummingbotTool };

//# sourceMappingURL=hummingbot.mjs.map