Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions agent/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
from agent.agent.agent import Agent
import datetime


class Actor:
"""
agent:
init -> QA -> plan
plan -> building -> moving
moving -> act -> use / chat
use -> critic ? plan : act
moving -> act -> use(trade) / chat
use(trade) -> critic ? plan : act
chat -> set maxInteraction to 5 -> storeMemory -> plan
critic true -> pack act to experience
timetick -> storeMemory -> memory_store -> Memory
"""
def __init__(self, name: str, bio: str, goal: str, model: str, memorySystem: str, planSystem: str, buildings: List[str], cash: int, start_time: float) -> None:
self.using = False
self.agent = Agent(name, bio, goal, model, memorySystem, planSystem, buildings, cash, start_time)

# def _init(self):
# self.agent.plan()
def from_json(self, obj: Dict[str, Any]):
self.agent.from_json(obj)
return self

def to_json(self) -> Dict[str, Any]:
return self.agent.to_json()

async def react(self, observation: Dict[str, Any]) -> Dict[str, Any]:
"""_summary_

Expand All @@ -40,14 +41,17 @@ async def react(self, observation: Dict[str, Any]) -> Dict[str, Any]:
"""
ret_dict = {"status": 500, "message": "", "data": dict()}
if self.using:
ret_dict["message"] = "still using" # to avoid message overwhelming
if observation['source'] != 'chatted':
return ret_dict
ret_dict["message"] = "still using"
return ret_dict
self.using = True
self.agent.state.equipments = observation.get("data", dict()).get("equipments", list())
self.agent.state.people = observation.get("data", dict()).get("people", list())
self.agent.state.cash = observation.get("data", dict()).get("cash", list())
self.agent.state.game_time = datetime.datetime.fromtimestamp(observation.get("data", dict()).get("game_time", datetime.datetime.now().timestamp() * 1000) / 1000)
# self.agent.state.equipments_items = observation.get("data", dict()).get("equipments_items", list())
self.agent.state.npc_items = observation.get("data", dict()).get("npc_items", list())
self.agent.state.all_trade_items = observation.get("data", dict()).get("all_trade_items", list())
self.agent.state.game_time = datetime.datetime.fromtimestamp(
observation.get("data", dict()).get("game_time", datetime.datetime.now().timestamp() * 1000) / 1000)
if observation['source'] == 'timetick-finishMoving':
ret_dict["data"] = await self._act()
elif observation['source'] == 'timetick-finishUse':
Expand Down Expand Up @@ -78,7 +82,7 @@ async def react(self, observation: Dict[str, Any]) -> Dict[str, Any]:

async def _critic(self) -> Dict[str, Any]:
# ret_dict = dict()
if self.agent.cache.experience_cache:
if self.agent.cache.experience_cache: # if previous act not finish yet, finish first
acts = self.agent.cache.experience_cache
act = dict()
if acts:
Expand All @@ -104,7 +108,7 @@ async def _critic(self) -> Dict[str, Any]:
# ret_dict["newPlan": self.agent.state.plan]
else:
return await self._act()

async def _plan(self) -> Dict[str, Any]:
ret_dict = dict()
await self.agent.plan()
Expand All @@ -119,19 +123,34 @@ async def _act(self) -> Dict[str, Any]:
# target = self.agent.state.act.get("target", "")
if action == "use":
equipment = self.agent.state.act.get("equipment", "")
# equipmentId = self.agent.state.get("equipmentId", "")
operation = self.agent.state.act.get("operation", "")
description = ""
menu = dict()
# TODO: find nearest
equipmentId = False
for e in self.agent.state.equipments:
if equipment in e["name"]:
description = e["description"]
menu = e["menu"]
equipmentId = e["id"]
break
await self.agent.use(equipment, operation, description, menu)
if self.agent.state.use["bought_thing"] in menu and isinstance(self.agent.state.use["amount"], int):
self.agent.state.use["cost"] = self.agent.state.use["amount"] * menu[self.agent.state.use["bought_thing"]]
return {"use": self.agent.state.use, "equipment": equipment, "operation": operation}

# 设置当前equipment的所有物品
if equipmentId:
self.agent.state.equipments_items = [x for x in self.agent.state.all_trade_items if
x["belongUid"] == equipmentId]


await self.agent.use(equipment, operation, description) # trade combined in this step
# if self.agent.state.use["bought_thing"] in menu and isinstance(self.agent.state.use["amount"], int):
# self.agent.state.use["cost"] = self.agent.state.use["amount"] * menu[
# self.agent.state.use["bought_thing"]]
return {
"use": self.agent.state.use,
"equipment": equipment,
"operation": operation,
"trade": self.agent.state.trade
}

elif action == "chat":
person = self.agent.state.act.get("person", "")
topic = self.agent.state.act.get("topic", "")
Expand All @@ -149,21 +168,21 @@ async def _act(self) -> Dict[str, Any]:
if acts:
act = acts.pop(0)
print(act)
self.agent.cache.experience_cache = acts
self.agent.cache.experience_cache = acts # inhibit critic, trigger a sew of acts
# if acts:
if not act:
return {"use": {"continue_time": 0, "result": "fail", "cost": 0, "earn": 0}, "equipment": "", "operation": "nothing to do"}
self.agent.state.execute_experience = True
print(act)
return {"use": {"continue_time": act["continue_time"], "result": act["result"], "cost": act.get("cost", 0), "earn": act.get("earn", 0)}, "equipment": act['equipment'], "operation": act['operation']}

async def _chat(self, person: str, topic: str) -> Dict[str, Any]:
await self.agent.chat(person, topic)
return {"chat": self.agent.state.chat, "person": person, "topic": topic}

def _addBuilding(self, building_name: str):
self.agent.state.buildings.append(building_name)

async def _store_memory(self):
await self.agent.memory_store()
if self.agent.state.memory:
Expand Down
Loading