-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathgame.py
More file actions
311 lines (266 loc) · 10.3 KB
/
game.py
File metadata and controls
311 lines (266 loc) · 10.3 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from typing import List, Any, Dict, Optional, Union, Set
from dataclasses import dataclass, asdict
from string import Template
import json
import uuid
import requests
from virtuals_sdk import sdk
@dataclass
class FunctionArgument:
name: str
description: str
type: str
id: str = None
def __post_init__(self):
self.id = self.id or str(uuid.uuid4())
@dataclass
class FunctionConfig:
method: str = "get"
url: str = ""
headers: Dict = None
payload: Dict = None
success_feedback: str = ""
error_feedback: str = ""
isMainLoop: bool = False
isReaction: bool = False
headersString: str = "{}" # Added field
payloadString: str = "{}" # Added field
platform: str = None
def __post_init__(self):
self.headers = self.headers or {}
self.payload = self.payload or {}
self.headersString = json.dumps(self.headers, indent=4)
self.payloadString = json.dumps(self.payload, indent=4)
@dataclass
class Function:
fn_name: str
fn_description: str
args: List[FunctionArgument]
config: FunctionConfig
hint: str = ""
id: str = None
def __post_init__(self):
self.id = self.id or str(uuid.uuid4())
def toJson(self):
return {
"id": self.id,
"fn_name": self.fn_name,
"fn_description": self.fn_description,
"args": [asdict(arg) for arg in self.args],
"hint": self.hint,
"config": asdict(self.config)
}
def _validate_args(self, *args) -> Dict[str, Any]:
"""Validate and convert positional arguments to named arguments"""
if len(args) != len(self.args):
raise ValueError(f"Expected {len(self.args)} arguments, got {len(args)}")
# Create dictionary of argument name to value
arg_dict = {}
for provided_value, arg_def in zip(args, self.args):
arg_dict[arg_def.name] = provided_value
# Type validation (basic)
if arg_def.type == "string" and not isinstance(provided_value, str):
raise TypeError(f"Argument {arg_def.name} must be a string")
elif arg_def.type == "array" and not isinstance(provided_value, (list, tuple)):
raise TypeError(f"Argument {arg_def.name} must be an array")
# elif arg_def.type == "boolean" and not isinstance(provided_value, bool):
# raise TypeError(f"Argument {arg_def.name} must be a boolean")
return arg_dict
def _interpolate_template(self, template_str: str, values: Dict[str, Any]) -> str:
"""Interpolate a template string with given values"""
# Convert Template-style placeholders ({{var}}) to Python style ($var)
python_style = template_str.replace('{{', '$').replace('}}', '')
return Template(python_style).safe_substitute(values)
def _prepare_request(self, arg_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Prepare the request configuration with interpolated values"""
config = self.config
# Interpolate URL
url = self._interpolate_template(config.url, arg_dict)
# Interpolate payload
payload = {}
for key, value in config.payload.items():
if isinstance(value, str):
# Handle template values
template_key = self._interpolate_template(key, arg_dict)
if value.strip('{}') in arg_dict:
# For array and other non-string types, use direct value
payload[template_key] = arg_dict[value.strip('{}')]
else:
# For string interpolation
payload[template_key] = self._interpolate_template(value, arg_dict)
else:
payload[key] = value
return {
"method": config.method,
"url": url,
"headers": config.headers,
"data": json.dumps(payload)
}
def __call__(self, *args):
"""Allow the function to be called directly with arguments"""
# Validate and convert args to dictionary
arg_dict = self._validate_args(*args)
# Prepare request
request_config = self._prepare_request(arg_dict)
# Make the request
response = requests.request(**request_config)
# Handle response
if response.ok:
try:
result = response.json()
except requests.exceptions.JSONDecodeError:
result = response.text or None
# Interpolate success feedback if provided
if hasattr(self.config, 'success_feedback'):
print(self._interpolate_template(self.config.success_feedback,
{"response": result, **arg_dict}))
return result
else:
# Handle error
try:
error_msg = response.json()
except requests.exceptions.JSONDecodeError:
error_msg = {"description": response.text or response.reason}
if hasattr(self.config, "error_feedback"):
print(
self._interpolate_template(
self.config.error_feedback, {"response": error_msg, **arg_dict}
)
)
raise requests.exceptions.HTTPError(f"Request failed: {error_msg}")
class Agent:
def __init__(
self,
api_key: str,
goal: str = "",
description: str = "",
world_info: str = "",
main_heartbeat: int = 15,
reaction_heartbeat: int = 5
):
self.game_sdk = sdk.GameSDK(api_key)
self.goal = goal
self.description = description
self.world_info = world_info
self.enabled_functions: List[str] = []
self.custom_functions: List[Function] = []
self.main_heartbeat = main_heartbeat
self.reaction_heartbeat = reaction_heartbeat
def set_goal(self, goal: str):
self.goal = goal
return True
def set_description(self, description: str):
self.description = description
return True
def set_world_info(self, world_info: str):
self.world_info = world_info
return True
def set_main_heartbeat(self, main_heartbeat: int):
self.main_heartbeat = main_heartbeat
return True
def set_reaction_heartbeat(self, reaction_heartbeat: int):
self.reaction_heartbeat = reaction_heartbeat
return True
def get_goal(self) -> str:
return self.goal
def get_description(self) -> str:
return self.description
def get_world_info(self) -> str:
return self.world_info
def list_available_default_twitter_functions(self) -> Dict[str, str]:
"""
List all of the default functions (currently default functions are only available for Twitter/X platform)
TODO: will be moved to another layer of abstraction later
"""
# Combine built-in and custom function descriptions
return self.game_sdk.functions()
def use_default_twitter_functions(self, functions: List[str]):
"""
Enable built-in functions by default
"""
self.enabled_functions = functions
return True
def add_custom_function(self, custom_function: Function) -> bool:
"""
Add a custom function to the agent
Custom functions are automatically added and enabled
"""
# Add to custom functions list
self.custom_functions.append(custom_function)
return True
def simulate_twitter(self, session_id: str):
"""
Simulate the agent configuration for Twitter
"""
return self.game_sdk.simulate(
session_id,
self.goal,
self.description,
self.world_info,
self.enabled_functions,
self.custom_functions
)
def react(self, session_id: str, platform: str, tweet_id: str = None, event: str = None, task: str = None):
"""
React to a tweet
"""
return self.game_sdk.react(
session_id=session_id,
platform=platform,
event=event,
task=task,
tweet_id=tweet_id,
goal=self.goal,
description=self.description,
world_info=self.world_info,
functions=self.enabled_functions,
custom_functions=self.custom_functions
)
def eval_react(self, response: List[Dict[str, Any]]):
"""
Evaluate the agent reply with EvaEngine
Checkout your eval dashboard here: https://evaengine.ai/virtuals (import your api key to view)
"""
try:
original_tweet = response[0]["EVENT-REQUEST"]["event"].split("New tweet: ")[1]
replied_tweet = response[-1]["TWEET-CONTENT"]["content"]
except (KeyError, IndexError) as e:
raise ValueError("Invalid response format - missing tweet content. Please ensure the agent's goal includes replying to tweets.")
return self.game_sdk.eval_react(original_tweet, replied_tweet)
def deploy_twitter(self):
"""
Deploy the agent configuration
"""
return self.game_sdk.deploy(
self.goal,
self.description,
self.world_info,
self.enabled_functions,
self.custom_functions,
self.main_heartbeat,
self.reaction_heartbeat
)
def export(self) -> str:
"""Export the agent configuration as JSON string"""
export_dict = {
"goal": self.goal,
"description": self.description,
"worldInfo": self.world_info,
"functions": self.enabled_functions,
"customFunctions": [
{
"id": func.id,
"fn_name": func.fn_name,
"fn_description": func.fn_description,
"args": [asdict(arg) for arg in func.args],
"hint": func.hint,
"config": asdict(func.config)
}
for func in self.custom_functions
]
}
agent_json = json.dumps(export_dict, indent=4)
# save to file
with open('agent.json', 'w') as f:
f.write(agent_json)
return agent_json