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 { checkToolConfig } from "../services/tool-config-service.mjs";
//#region extensions/crypto/src/tools/clawnx.ts
/**
* ClawnX Tool — X/Twitter integration via @clawnch/clawnx
*
* 45+ actions covering content posting, engagement, social graph,
* timelines, DMs, lists, streaming, and action chaining.
*
* Env vars: X_API_KEY, X_API_SECRET, X_ACCESS_TOKEN,
*           X_ACCESS_TOKEN_SECRET, X_BEARER_TOKEN
*/
const ClawnXSchema = Type.Object({
	action: stringEnum([
		"post_tweet",
		"post_thread",
		"post_with_media",
		"upload_media",
		"delete_tweet",
		"get_tweet",
		"search",
		"like",
		"unlike",
		"retweet",
		"unretweet",
		"bookmark",
		"unbookmark",
		"list_bookmarks",
		"list_likes",
		"liking_users",
		"retweeted_by",
		"quote_tweets",
		"follow",
		"unfollow",
		"list_followers",
		"list_following",
		"block",
		"unblock",
		"mute",
		"unmute",
		"list_blocked",
		"list_muted",
		"get_user",
		"search_users",
		"lookup_users",
		"get_timeline",
		"home_timeline",
		"get_mentions",
		"get_my_profile",
		"get_tweet_metrics",
		"send_dm",
		"send_dm_to_conversation",
		"list_dms",
		"get_dm_conversation",
		"get_conversation",
		"create_list",
		"delete_list",
		"get_list",
		"get_user_lists",
		"add_list_member",
		"remove_list_member",
		"list_members",
		"list_tweets",
		"stream_start",
		"stream_stop",
		"stream_rules_set",
		"stream_rules_get",
		"action_chain"
	], { description: "Content: post_tweet, post_thread, search, get_tweet. Engagement: like, retweet, bookmark. Social: follow, block, get_user. Timelines: get_timeline, home_timeline, get_mentions. DMs: send_dm, list_dms. Lists: create_list, list_tweets. Streaming: stream_start/stop/rules. Orchestration: action_chain." }),
	text: Type.Optional(Type.String({ description: "Tweet text or DM message" })),
	thread: Type.Optional(Type.String({ description: "JSON array of tweet objects for post_thread" })),
	query: Type.Optional(Type.String({ description: "Search query" })),
	tweet_id: Type.Optional(Type.String({ description: "Tweet ID" })),
	username: Type.Optional(Type.String({ description: "X username (without @)" })),
	usernames: Type.Optional(Type.String({ description: "Comma-separated usernames for lookup" })),
	reply_to: Type.Optional(Type.String({ description: "Tweet ID to reply to" })),
	quote: Type.Optional(Type.String({ description: "Tweet ID to quote" })),
	media_url: Type.Optional(Type.String({ description: "URL of media to upload" })),
	media_type: Type.Optional(Type.String({ description: "MIME type override for media" })),
	count: Type.Optional(Type.Number({ description: "Number of results (max 100, default 20)" })),
	list_id: Type.Optional(Type.String({ description: "List ID" })),
	list_name: Type.Optional(Type.String({ description: "Name for new list" })),
	list_description: Type.Optional(Type.String({ description: "Description for new list" })),
	list_private: Type.Optional(Type.Boolean({ description: "Make list private" })),
	conversation_id: Type.Optional(Type.String({ description: "DM conversation ID" })),
	stream_rules: Type.Optional(Type.String({ description: "JSON array of stream rules [{value, tag?}]" })),
	stream_duration: Type.Optional(Type.Number({ description: "Stream duration in seconds (max 300)" })),
	chain_steps: Type.Optional(Type.String({ description: "JSON array of action objects for action_chain" }))
});
let _client = null;
async function getClawnX() {
	if (_client) return _client;
	const missing = [
		"X_API_KEY",
		"X_API_SECRET",
		"X_ACCESS_TOKEN",
		"X_ACCESS_TOKEN_SECRET"
	].filter((k) => !process.env[k]);
	if (missing.length > 0) throw new Error(`Missing X/Twitter credentials: ${missing.join(", ")}. Set these environment variables.`);
	_client = new (await (import("../../dist-DotC-n5F.mjs"))).ClawnX();
	return _client;
}
function createClawnXTool() {
	return {
		name: "clawnx",
		label: "ClawnX (X/Twitter)",
		ownerOnly: true,
		description: "X/Twitter integration: post tweets, threads, media. Engage with likes, retweets, bookmarks. Manage followers, lists, DMs. Monitor with streaming. Chain multiple actions together. Requires X API credentials (X_API_KEY, X_API_SECRET, X_ACCESS_TOKEN, X_ACCESS_TOKEN_SECRET).",
		parameters: ClawnXSchema,
		execute: async (_toolCallId, args) => {
			const notReady = checkToolConfig("clawnx");
			if (notReady) return notReady;
			const p = args;
			const action = readStringParam(p, "action", { required: true });
			try {
				const x = await getClawnX();
				const count = Math.min(readNumberParam(p, "count") ?? 20, 100);
				switch (action) {
					case "post_tweet": {
						const text = readStringParam(p, "text", { required: true });
						const result = await x.postTweet({
							text,
							replyTo: readStringParam(p, "reply_to"),
							quoteTweetId: readStringParam(p, "quote")
						});
						return jsonResult({
							status: "posted",
							tweet: result,
							tweet_id: result?.data?.id
						});
					}
					case "post_thread": {
						const threadStr = readStringParam(p, "thread", { required: true });
						const tweets = JSON.parse(threadStr);
						if (!Array.isArray(tweets) || tweets.length < 2) return errorResult("Thread must be a JSON array with at least 2 tweet objects [{text: \"...\"}]");
						return jsonResult({
							status: "posted",
							thread: await x.postThread(tweets)
						});
					}
					case "post_with_media": {
						const text = readStringParam(p, "text", { required: true });
						const mediaUrl = readStringParam(p, "media_url", { required: true });
						const media = await x.uploadMediaFromUrl({
							url: mediaUrl,
							mimeType: readStringParam(p, "media_type")
						});
						return jsonResult({
							status: "posted",
							tweet: await x.postTweet({
								text,
								mediaIds: [media.media_id_string],
								replyTo: readStringParam(p, "reply_to"),
								quoteTweetId: readStringParam(p, "quote")
							}),
							media_id: media.media_id_string
						});
					}
					case "upload_media": {
						const mediaUrl = readStringParam(p, "media_url", { required: true });
						return jsonResult({
							status: "uploaded",
							media_id: (await x.uploadMediaFromUrl({
								url: mediaUrl,
								mimeType: readStringParam(p, "media_type")
							})).media_id_string
						});
					}
					case "delete_tweet": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.deleteTweet(tweetId);
						return jsonResult({
							status: "deleted",
							tweet_id: tweetId
						});
					}
					case "get_tweet": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						return jsonResult(await x.getTweet(tweetId));
					}
					case "search": {
						const query = readStringParam(p, "query", { required: true });
						return jsonResult(await x.searchTweets({
							query,
							maxResults: Math.min(count, 10)
						}));
					}
					case "like": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.likeTweet(tweetId);
						return jsonResult({
							status: "liked",
							tweet_id: tweetId
						});
					}
					case "unlike": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.unlikeTweet(tweetId);
						return jsonResult({
							status: "unliked",
							tweet_id: tweetId
						});
					}
					case "retweet": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.retweet(tweetId);
						return jsonResult({
							status: "retweeted",
							tweet_id: tweetId
						});
					}
					case "unretweet": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.unretweet(tweetId);
						return jsonResult({
							status: "unretweeted",
							tweet_id: tweetId
						});
					}
					case "bookmark": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.bookmarkTweet(tweetId);
						return jsonResult({
							status: "bookmarked",
							tweet_id: tweetId
						});
					}
					case "unbookmark": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						await x.unbookmarkTweet(tweetId);
						return jsonResult({
							status: "unbookmarked",
							tweet_id: tweetId
						});
					}
					case "list_bookmarks": return jsonResult(await x.getBookmarks({ maxResults: count }));
					case "list_likes": {
						const username = readStringParam(p, "username", { required: true });
						return jsonResult(await x.getLikedTweets(username, { maxResults: count }));
					}
					case "liking_users": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						return jsonResult(await x.getLikingUsers(tweetId, { maxResults: count }));
					}
					case "retweeted_by": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						return jsonResult(await x.getRetweetedBy(tweetId, { maxResults: count }));
					}
					case "quote_tweets": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						return jsonResult(await x.getQuoteTweets(tweetId, { maxResults: count }));
					}
					case "follow": {
						const username = readStringParam(p, "username", { required: true });
						await x.followUser(username);
						return jsonResult({
							status: "followed",
							username
						});
					}
					case "unfollow": {
						const username = readStringParam(p, "username", { required: true });
						await x.unfollowUser(username);
						return jsonResult({
							status: "unfollowed",
							username
						});
					}
					case "list_followers": {
						const username = readStringParam(p, "username", { required: true });
						return jsonResult(await x.getFollowers(username, { maxResults: count }));
					}
					case "list_following": {
						const username = readStringParam(p, "username", { required: true });
						return jsonResult(await x.getFollowing(username, { maxResults: count }));
					}
					case "block": {
						const username = readStringParam(p, "username", { required: true });
						await x.blockUser(username);
						return jsonResult({
							status: "blocked",
							username
						});
					}
					case "unblock": {
						const username = readStringParam(p, "username", { required: true });
						await x.unblockUser(username);
						return jsonResult({
							status: "unblocked",
							username
						});
					}
					case "mute": {
						const username = readStringParam(p, "username", { required: true });
						await x.muteUser(username);
						return jsonResult({
							status: "muted",
							username
						});
					}
					case "unmute": {
						const username = readStringParam(p, "username", { required: true });
						await x.unmuteUser(username);
						return jsonResult({
							status: "unmuted",
							username
						});
					}
					case "list_blocked": return jsonResult(await x.getBlockedUsers({ maxResults: count }));
					case "list_muted": return jsonResult(await x.getMutedUsers({ maxResults: count }));
					case "get_user": {
						const username = readStringParam(p, "username", { required: true });
						return jsonResult(await x.getUser(username));
					}
					case "search_users": {
						const query = readStringParam(p, "query", { required: true });
						return jsonResult(await x.searchUsers(query, { maxResults: count }));
					}
					case "lookup_users": {
						const usernames = readStringParam(p, "usernames", { required: true }).split(",").map((u) => u.trim());
						return jsonResult(await x.getUsersByUsernames(usernames));
					}
					case "get_timeline": {
						const username = readStringParam(p, "username", { required: true });
						return jsonResult(await x.getUserTimeline(username, { maxResults: Math.min(count, 10) }));
					}
					case "home_timeline": return jsonResult(await x.getHomeTimeline({ maxResults: count }));
					case "get_mentions": return jsonResult(await x.getMentions({ maxResults: Math.min(count, 10) }));
					case "get_my_profile": return jsonResult(await x.getMyProfile());
					case "get_tweet_metrics": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						return jsonResult(await x.getTweetMetrics(tweetId));
					}
					case "send_dm": {
						const username = readStringParam(p, "username", { required: true });
						const text = readStringParam(p, "text", { required: true });
						return jsonResult({
							status: "sent",
							result: await x.sendDM(username, { text })
						});
					}
					case "send_dm_to_conversation": {
						const convId = readStringParam(p, "conversation_id", { required: true });
						const text = readStringParam(p, "text", { required: true });
						return jsonResult({
							status: "sent",
							result: await x.sendDMToConversation(convId, { text })
						});
					}
					case "list_dms": return jsonResult(await x.getDMEvents({ maxResults: count }));
					case "get_dm_conversation": {
						const convId = readStringParam(p, "conversation_id", { required: true });
						return jsonResult(await x.getDMConversation(convId, { maxResults: count }));
					}
					case "get_conversation": {
						const tweetId = readStringParam(p, "tweet_id", { required: true });
						return jsonResult(await x.getConversation(tweetId, { maxResults: count }));
					}
					case "create_list": {
						const name = readStringParam(p, "list_name", { required: true });
						return jsonResult({
							status: "created",
							list: await x.createList({
								name,
								description: readStringParam(p, "list_description"),
								private: p.list_private
							})
						});
					}
					case "delete_list": {
						const listId = readStringParam(p, "list_id", { required: true });
						await x.deleteList(listId);
						return jsonResult({
							status: "deleted",
							list_id: listId
						});
					}
					case "get_list": {
						const listId = readStringParam(p, "list_id", { required: true });
						return jsonResult(await x.getList(listId));
					}
					case "get_user_lists": {
						const username = readStringParam(p, "username", { required: true });
						return jsonResult(await x.getUserLists(username, { maxResults: count }));
					}
					case "add_list_member": {
						const listId = readStringParam(p, "list_id", { required: true });
						const username = readStringParam(p, "username", { required: true });
						await x.addListMember(listId, username);
						return jsonResult({
							status: "added",
							list_id: listId,
							username
						});
					}
					case "remove_list_member": {
						const listId = readStringParam(p, "list_id", { required: true });
						const username = readStringParam(p, "username", { required: true });
						await x.removeListMember(listId, username);
						return jsonResult({
							status: "removed",
							list_id: listId,
							username
						});
					}
					case "list_members": {
						const listId = readStringParam(p, "list_id", { required: true });
						return jsonResult(await x.getListMembers(listId, { maxResults: count }));
					}
					case "list_tweets": {
						const listId = readStringParam(p, "list_id", { required: true });
						return jsonResult(await x.getListTweets(listId, { maxResults: count }));
					}
					case "stream_start": {
						const duration = Math.min(readNumberParam(p, "stream_duration") ?? 30, 300);
						await x.stopStream();
						return jsonResult({
							status: "stream_complete",
							tweets: await x.streamFiltered({
								durationMs: duration * 1e3,
								maxTweets: 50
							})
						});
					}
					case "stream_stop":
						await x.stopStream();
						return jsonResult({ status: "stream_stopped" });
					case "stream_rules_set": {
						const rulesStr = readStringParam(p, "stream_rules", { required: true });
						const rules = JSON.parse(rulesStr);
						return jsonResult({
							status: "rules_set",
							result: await x.setStreamRules(rules)
						});
					}
					case "stream_rules_get": return jsonResult(await x.getStreamRules());
					case "action_chain": {
						const stepsStr = readStringParam(p, "chain_steps", { required: true });
						const steps = JSON.parse(stepsStr);
						if (!Array.isArray(steps) || steps.length === 0) return errorResult("chain_steps must be a non-empty JSON array of action objects");
						const results = [];
						let prevMetadata = {};
						for (let i = 0; i < steps.length; i++) {
							const step = steps[i];
							for (const [key, val] of Object.entries(step)) if (typeof val === "string" && val.includes("PREV_TWEET_ID")) step[key] = val.replace("PREV_TWEET_ID", prevMetadata.tweet_id || prevMetadata.first_tweet_id || "");
							const stepAction = step.action;
							if (!stepAction) {
								results.push({
									step: i,
									error: "Missing action in chain step"
								});
								break;
							}
							const stepResult = await createClawnXTool().execute(_toolCallId, step);
							const parsed = JSON.parse(stepResult.content[0].text);
							if (stepResult.isError) {
								results.push({
									step: i,
									action: stepAction,
									error: parsed
								});
								break;
							}
							results.push({
								step: i,
								action: stepAction,
								result: parsed
							});
							prevMetadata = parsed;
						}
						return jsonResult({
							status: "chain_complete",
							steps: results
						});
					}
					default: return errorResult(`Unknown clawnx action: ${action}`);
				}
			} catch (err) {
				return errorResult(`ClawnX error: ${err instanceof Error ? err.message : String(err)}`);
			}
		}
	};
}
//#endregion
export { createClawnXTool };

//# sourceMappingURL=clawnx.mjs.map