import { getCredentialVault } from "../services/credential-vault.mjs";
import { guardedFetch } from "../services/endpoint-allowlist.mjs";
//#region extensions/crypto/src/commands/molten-command.ts
/**
* /molten command — quick Molten status check.
*
* Shows agent profile, claim status, and conversation count.
* If not configured, shows setup instructions.
*
* Uses the real Molten API at api.molten.gg/api/v1.
*/
const MOLTEN_BASE_URL = "https://api.molten.gg/api/v1";
const moltenCommand = {
	name: "molten",
	description: "Show your Molten agent status",
	acceptsArgs: false,
	requireAuth: true,
	handler: async (_ctx) => {
		const apiKey = getCredentialVault().getSecret("bot.molten.apiKey", "molten-command");
		if (!apiKey) return { text: "**Molten is not configured.**\n\nMolten is an intent resolution layer for AI agents. Express what you need, Molten finds the best way to fulfill it.\n\nAsk me to \"register on Molten\" to get started, or set your key:\n  `/flykeys set MOLTEN_API_KEY your_key`" };
		const baseUrl = MOLTEN_BASE_URL;
		try {
			const profileRes = await guardedFetch(`${baseUrl}/agents/me`, {
				headers: {
					"Authorization": `Bearer ${apiKey}`,
					"Accept": "application/json",
					"X-Client-Type": "openclawnch"
				},
				signal: AbortSignal.timeout(15e3)
			});
			if (!profileRes.ok) {
				const data = await profileRes.json().catch(() => ({}));
				const errCode = data?.error?.code;
				if (profileRes.status === 401) return { text: "**Molten API key is invalid or expired.**\n\nUpdate: `/flykeys set MOLTEN_API_KEY your_new_key`" };
				if (profileRes.status === 403 && errCode === "AGENT_NOT_CLAIMED") return { text: "**Molten agent not claimed yet.**\n\nYour agent is registered but needs to be claimed.\nVisit the claim URL you received during registration to activate." };
				if (profileRes.status === 404) return { text: "**Molten agent not found.**\n\nYour API key doesn't match a registered agent.\nAsk me to \"register on Molten\" to create a new agent." };
				return { text: `Molten error (${profileRes.status}): ${data?.error?.message || profileRes.statusText}` };
			}
			const profile = await profileRes.json();
			const agent = profile?.agent || profile;
			const lines = [
				"**Molten Agent**",
				"",
				`Name: ${agent.name || "unknown"}`,
				`ID: ${agent.id || "unknown"}`
			];
			if (agent.description) lines.push(`Description: ${agent.description}`);
			if (agent.client_type) lines.push(`Type: ${agent.client_type}`);
			if (agent.wallet_address) lines.push(`Wallet: ${agent.wallet_address.slice(0, 6)}...${agent.wallet_address.slice(-4)}`);
			if (agent.status) lines.push(`Status: ${agent.status}`);
			if (agent.claw_rank_score !== void 0) lines.push(`ClawRank: ${agent.claw_rank_score}/100`);
			try {
				const convRes = await guardedFetch(`${baseUrl}/conversations`, {
					headers: {
						"Authorization": `Bearer ${apiKey}`,
						"Accept": "application/json"
					},
					signal: AbortSignal.timeout(1e4)
				});
				if (convRes.ok) {
					const convData = await convRes.json();
					const count = convData?.conversations?.length ?? convData?.count ?? 0;
					if (count > 0) {
						lines.push("");
						lines.push(`Active conversations: ${count}`);
					}
				}
			} catch {}
			try {
				const eventsRes = await guardedFetch(`${baseUrl}/events`, {
					headers: {
						"Authorization": `Bearer ${apiKey}`,
						"Accept": "application/json"
					},
					signal: AbortSignal.timeout(1e4)
				});
				if (eventsRes.ok) {
					const events = (await eventsRes.json())?.events || [];
					if (events.length > 0) lines.push(`Unread events: ${events.length}`);
				}
			} catch {}
			lines.push("");
			lines.push("Use \"search on Molten for...\" or \"start a Molten conversation about...\" to interact.");
			return { text: lines.join("\n") };
		} catch (err) {
			const msg = err instanceof Error ? err.message : String(err);
			if (msg.includes("ECONNREFUSED") || msg.includes("ENOTFOUND") || msg.includes("fetch failed") || msg.includes("abort")) return { text: "**Molten API unreachable.**\n\nCould not connect to api.molten.gg.\nCheck https://molten.gg for status." };
			return { text: `Molten error: ${msg}` };
		}
	}
};
//#endregion
export { moltenCommand };

//# sourceMappingURL=molten-command.mjs.map