UNPKG

@agentek/tools

Version:

Blockchain tools for AI agents

61 lines 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.twitterTools = twitterTools; const tools_js_1 = require("./tools.js"); const client_js_1 = require("../client.js"); const twitter_api_v2_1 = require("twitter-api-v2"); async function fetchBearerToken(apiKey, apiKeySecret) { const credentials = btoa(`${apiKey}:${apiKeySecret}`); const response = await fetch("https://api.x.com/oauth2/token", { method: "POST", headers: { Authorization: `Basic ${credentials}`, "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", }, body: "grant_type=client_credentials", }); if (!response.ok) { const body = await response.text(); throw new Error(`Failed to obtain X Bearer Token (${response.status}): ${body}`); } const data = await response.json(); return data.access_token; } async function twitterTools(config) { let bearerToken; if ("xBearerToken" in config && config.xBearerToken) { bearerToken = config.xBearerToken; } else if ("xApiKey" in config && config.xApiKey && "xApiKeySecret" in config && config.xApiKeySecret) { bearerToken = await fetchBearerToken(config.xApiKey, config.xApiKeySecret); } else { throw new Error("X (Twitter) credentials required: provide either xBearerToken, or xApiKey + xApiKeySecret."); } const tools = [ (0, tools_js_1.createSearchRecentTweetsTool)(bearerToken), (0, tools_js_1.createGetTweetByIdTool)(bearerToken), (0, tools_js_1.createGetUserByUsernameTool)(bearerToken), (0, tools_js_1.createGetUserTweetsTool)(bearerToken), ]; // Home timeline requires user OAuth 1.0a credentials if ("xApiKey" in config && config.xApiKey && "xApiKeySecret" in config && config.xApiKeySecret && config.xAccessToken && config.xAccessTokenSecret) { const userClient = new twitter_api_v2_1.TwitterApi({ appKey: config.xApiKey, appSecret: config.xApiKeySecret, accessToken: config.xAccessToken, accessSecret: config.xAccessTokenSecret, }); tools.push((0, tools_js_1.createGetHomeTimelineTool)(userClient)); } return (0, client_js_1.createToolCollection)(tools); } //# sourceMappingURL=index.js.map