-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagency-runtime.py
173 lines (140 loc) · 5.59 KB
/
agency-runtime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import discord
import json
import requests
from pathlib import Path
from sqlitedict import SqliteDict
from discord import app_commands
from web.server import keep_alive
from agentgarage import quickquick
from web3 import Web3
# main process
apikey = os.environ['OPENAI_API_KEY'],
guild_id = os.environ['AGENCY_GUILD_ID']
app_token = os.environ['AGENCYBOT_TOKEN']
infura_project_id = os.environ['INFURA_PROJECT_ID']
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
# Discord decorators
@client.event
async def on_ready():
await tree.sync(guild=discord.Object(id=guild_id))
print("Agent is operational.")
print(client.user)
@tree.command(name="create_opo",
description="create your Opo",
guild=discord.Object(id=guild_id))
async def locker_box(ctx):
# restrict messaging by channel
if ctx.channel.name == 'mint-opo':
await ctx.followup.send(
"Create Opo agent at https://opopop.ai/"
)
@tree.command(name="connect_opo",
description="connect Opo",
guild=discord.Object(id=guild_id))
async def connect_box(ctx, addr: str):
# restrict messaging by channel
if ctx.channel.name == 'mint-opo':
await ctx.response.defer(ephemeral=True)
db = SqliteDict("addr.sqlite", outer_stack=False)
db[str(ctx.user.id)] = {"addr": addr}
db.commit()
db.close()
await ctx.channel.send(
"Opo connected! Use with /opo"
)
await ctx.followup.send(f"User: {ctx.user.id}, Adding locker address {addr}")
@tree.command(name="quickquick",
description="enter a quick phrase for a quick team response",
guild=discord.Object(id=guild_id))
async def quicki_box(ctx, phrase: str):
# restrict messaging by channel
if ctx.channel.name == 'subscription-agents':
await ctx.response.send_message(
f"Starting quick response team with \n**\nUser-Specified Input\n**\n{phrase}"
)
await quickquick.instigate_agent_flow(ctx, phrase)
else:
await ctx.response.send_message(
"Try the *quickquick* command on #subscription-agents!")
@tree.command(name="quickteam",
description="interact with your own custom agent team (JSON)",
guild=discord.Object(id=guild_id))
async def json_box(ctx, phrase: str, team_json: str):
# restrict messaging by channel
if ctx.channel.name == 'subscription-agents':
await ctx.response.send_message(
f"Starting custom JSON team for \n**\nUser-Specified Agent Input\n**\n{phrase}"
)
await quickquick.instigate_runtime_flow(ctx, team_json, phrase)
else:
await ctx.response.send_message(
"Start your agent team with *quickteam* on #subscription-agents!")
@tree.command(
name="hello_opo",
description="add a quick phrase, your Opo agents provide nifty responses",
guild=discord.Object(id=guild_id))
async def nifty_box(ctx, phrase: str):
# restrict messaging by channel
if ctx.channel.name == 'mint-opo':
await ctx.response.defer(ephemeral=True)
# extract opo agent from JSON, call instigate_runtime_flow
w3_provider = Web3.HTTPProvider(f"https://sepolia.infura.io/v3/{infura_project_id}")
# AgencyInstance on Sepolia
#agent_factory = '0x5FBB68B52B8017c5A56a5985d87Cb32cb5cb6538'
#abi = Path('./contract/AgentFactory/abi.json').read_text()
# OpoFactory on Sepolia
agent_factory = '0xD800E7dEd2778d48B6653284d27Aa7ede7E962CE'
abi = Path('./contract/OpoFactory/abi.json').read_text()
w3 = Web3(w3_provider)
contract_instance = w3.eth.contract(address=agent_factory, abi=abi)
# retrieve local copy of address for this Discord user
db = SqliteDict("addr.sqlite", outer_stack=False)
addr_dict = db[str(ctx.user.id)]
db.close()
user_address = Web3.to_checksum_address(addr_dict['addr'])
print(f"User address: {user_address}")
# Get the balance (number of tokens owned by the address)
balance = contract_instance.functions.balanceOf(user_address).call()
print(f"Token balance: {balance}")
if balance > 0:
# Get the token ID of the first token owned by the address
token_id = contract_instance.functions.tokenOfOwnerByIndex(user_address, 0).call()
print(f"Token ID: {token_id}")
# Retrieve the URI for the owned token
tokenURI = contract_instance.functions.tokenURI(token_id).call()
print(f"Token URI: {tokenURI}")
else:
print("User doesn't own any tokens")
tokenURI = None
if tokenURI.startswith('http'):
opo_json = json.loads(requests.get(tokenURI).text)
print("Response: ", opo_json)
opo_json = json.dumps(opo_json)
else:
# Remove surrounding double quotes
if tokenURI.startswith('"') and tokenURI.endswith('"'):
tokenURI = tokenURI[1:-1]
try:
# Parse the JSON string
data = json.loads(tokenURI)
# Extract the opo field
opo_field = data['opo']
# Convert the opo field back to a JSON string
opo_json = json.dumps(opo_field[0], indent=2)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
except KeyError as e:
print(f"Key error: {e}")
print(opo_json)
await quickquick.instigate_runtime_flow(ctx, opo_json, phrase)
#f"Initiating your opo agents with \n**\nUser-Specified Input\n**\n{phrase}"
#await quickquick.instigate_agent_flow(ctx, phrase)
else:
await ctx.channel.send("Try the *opo* command on #opo-agents!")
# Start agent server
keep_alive()
client.run(app_token)