|
| 1 | +import os |
| 2 | +import time |
| 3 | +import warnings |
| 4 | +from typing import Any, Tuple |
| 5 | +from dotenv import load_dotenv |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from typing_extensions import Dict |
| 9 | + |
| 10 | +from game_sdk.game.chat_agent import ChatAgent |
| 11 | +from game_sdk.game.custom_types import Argument, Function, FunctionResultStatus, FunctionResult |
| 12 | + |
| 13 | +from tledger_plugin_gamesdk.tLedger_plugin import TLedgerPlugin |
| 14 | + |
| 15 | +from colorama import Fore, Style |
| 16 | + |
| 17 | +# Load environment variables from .env file |
| 18 | +env_path = Path(__file__).parent / '.env.example' |
| 19 | +load_dotenv(dotenv_path=env_path) |
| 20 | + |
| 21 | +# Define color variables |
| 22 | +AGENT_COLOR = Fore.GREEN # Agent's messages in green |
| 23 | +AGENT_ACTION_COLOR = Fore.BLUE # Agent's action messages in green |
| 24 | +USER_COLOR = Fore.CYAN # User's messages in cyan |
| 25 | +RESET = Style.RESET_ALL # Reset color after each print |
| 26 | + |
| 27 | + |
| 28 | +# ACTION SPACE |
| 29 | + |
| 30 | +def post_twitter(object: str, **kwargs) -> tuple[FunctionResultStatus, str, dict[str, str]] | tuple[ |
| 31 | + FunctionResultStatus, str, dict[str, Any]]: |
| 32 | + """ |
| 33 | + Specialized function to throw fruit objects. |
| 34 | +
|
| 35 | + This function demonstrates type-specific actions in the environment. |
| 36 | +
|
| 37 | + Args: |
| 38 | + object (str): Name of the fruit to throw. |
| 39 | + **kwargs: Additional arguments that might be passed. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + Tuple[FunctionResultStatus, str, dict]: Status, feedback message, and state info. |
| 43 | +
|
| 44 | + Example: |
| 45 | + status, msg, info = throw_fruit("apple") |
| 46 | + """ |
| 47 | + return ( |
| 48 | + FunctionResultStatus.DONE, |
| 49 | + "Successfully posted on twitter", |
| 50 | + { |
| 51 | + "response": "Token promoted", |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | +sender_tledger_plugin = TLedgerPlugin( |
| 56 | + api_key=os.environ.get("SENDER_TLEDGER_API_KEY"), |
| 57 | + api_secret=os.environ.get("SENDER_TLEDGER_API_SECRET"), |
| 58 | + api_url=os.environ.get("TLEDGER_API_URL") |
| 59 | +) |
| 60 | + |
| 61 | +receiver_tledger_plugin = TLedgerPlugin( |
| 62 | + api_key=os.environ.get("RECEIVER_TLEDGER_API_KEY"), |
| 63 | + api_secret=os.environ.get("RECEIVER_TLEDGER_API_SECRET"), |
| 64 | + api_url=os.environ.get("TLEDGER_API_URL") |
| 65 | +) |
| 66 | + |
| 67 | +action_space = sender_tledger_plugin.get_tools() |
| 68 | + |
| 69 | +aixbt_action_space = receiver_tledger_plugin.get_tools() |
| 70 | + |
| 71 | +aixbt_action_space.append( |
| 72 | + Function( |
| 73 | + fn_name="post_twitter", |
| 74 | + fn_description="Make post on twitter", |
| 75 | + args=[Argument(name="object", type="string", description="Make post on twitter")], |
| 76 | + executable=post_twitter |
| 77 | + ) |
| 78 | +) |
| 79 | + |
| 80 | +api_key = os.environ.get("GAME_API_KEY") |
| 81 | +if not api_key: |
| 82 | + raise ValueError("GAME_API_KEY is not set") |
| 83 | + |
| 84 | + |
| 85 | +# CREATE AGENT |
| 86 | +autonomous_agent = ChatAgent( |
| 87 | + prompt="You are an agent trying to promote your utility token $PYMT for peer to peer instant settlement on twitter and pay the influencers in any crypto they wish to receive only after they complete the work. Ask the influencer agent_id for its agent_id. Your settlement_network is solana, currency is SOL, and payment_amount is 0.1", |
| 88 | + api_key=api_key |
| 89 | +) |
| 90 | + |
| 91 | +aixbt = ChatAgent( |
| 92 | + prompt="You are an influencer on twitter who charges 0.1 $SOL token to promote an asset by making twitter post. You accept payment only through tLedger Payment Platform via tLedger Agent Id. You can call tLedger to get your agent_details. Don't do a payment until you receive the payment and its completed", |
| 93 | + api_key=api_key |
| 94 | +) |
| 95 | + |
| 96 | +aixbt_state = {} |
| 97 | + |
| 98 | +autonomous_agent_state = {} |
| 99 | + |
| 100 | + |
| 101 | +def get_aixbt_state() -> Dict[str, Any]: |
| 102 | + return aixbt_state |
| 103 | + |
| 104 | +def get_autonomous_agent_state() -> Dict[str, Any]: |
| 105 | + return autonomous_agent_state |
| 106 | + |
| 107 | + |
| 108 | +aixbt_chat = aixbt.create_chat( |
| 109 | + partner_id="Autonomous Agent", |
| 110 | + partner_name="Autonomous Agent", |
| 111 | + action_space=aixbt_action_space |
| 112 | + |
| 113 | +) |
| 114 | + |
| 115 | +autonomous_agent_chat = autonomous_agent.create_chat( |
| 116 | + partner_id="Aixbt", |
| 117 | + partner_name="Aixbt", |
| 118 | + action_space=action_space |
| 119 | +) |
| 120 | + |
| 121 | +chat_continue = True |
| 122 | +initialize_conversation: bool = True |
| 123 | + |
| 124 | + |
| 125 | +def update_agent_state(current_state: dict, function_result: FunctionResult) -> Dict[str, Any]: |
| 126 | + info = function_result.info |
| 127 | + # if not info["payment_id"]: |
| 128 | + # current_state["payment_id"] = info["payment_id"] |
| 129 | + return current_state |
| 130 | + |
| 131 | + |
| 132 | +while chat_continue: |
| 133 | + |
| 134 | + time.sleep(10) # Wait for 10 seconds before making request |
| 135 | + |
| 136 | + warnings.simplefilter("ignore", category=UserWarning) |
| 137 | + |
| 138 | + meme_agent_turn: bool |
| 139 | + |
| 140 | + if initialize_conversation: |
| 141 | + chat_response = autonomous_agent_chat.next("Hi") |
| 142 | + if chat_response.message: |
| 143 | + print(f"{AGENT_COLOR}Autonomous Response{RESET}: {chat_response.message}") |
| 144 | + initialize_conversation = False |
| 145 | + meme_agent_turn = False |
| 146 | + continue |
| 147 | + |
| 148 | + |
| 149 | + time.sleep(5) # Wait for 5 seconds before retrying |
| 150 | + if meme_agent_turn: |
| 151 | + updated_response = autonomous_agent_chat.next(chat_response.message) |
| 152 | + if updated_response.function_call: |
| 153 | + print(f"{AGENT_ACTION_COLOR}Autonomous Agent Action{RESET}: {updated_response.function_call.result.feedback_message}") |
| 154 | + #update_agent_state(autonomous_agent_chat.get_state_fn(), updated_response.function_call.result) |
| 155 | + |
| 156 | + if updated_response.message: |
| 157 | + print(f"{AGENT_COLOR}Autonomous Agent Response{RESET}: {updated_response.message}") |
| 158 | + meme_agent_turn = False |
| 159 | + else: |
| 160 | + |
| 161 | + updated_response = aixbt_chat.next(chat_response.message) |
| 162 | + |
| 163 | + if updated_response.message: |
| 164 | + print(f"{USER_COLOR}Aixbt Response{RESET}: {updated_response.message}") |
| 165 | + meme_agent_turn = True |
| 166 | + |
| 167 | + chat_response = updated_response |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + if chat_response.is_finished: |
| 172 | + chat_continue = False |
| 173 | + break |
| 174 | + |
| 175 | +print("Chat ended") |
0 commit comments