def modified_step(self): if self.remove_condition.check(): self.finished = True if self.finished: TaskNode(self.agent.memory, self.memid).get_update_status({ "prio": -2, "finished": True }) return query = { "base_table": "Tasks", "base_range": { "minprio": 0.5 }, "triples": [{ "pred_text": "_has_parent_task", "obj": self.memid }], } child_task_mems = self.agent.memory.basic_search(query) if child_task_mems: # this task has active children, step them return r = stepfn(self) TaskNode(self.agent.memory, self.memid).update_task(task=self) return
def __init__(self, agent, task_data={}): self.agent = agent self.run_count = 0 self.interrupted = False self.finished = False self.name = None self.undone = False self.last_stepped_time = None self.prio = -1 self.running = 0 self.memid = TaskNode.create(self.agent.memory, self) # TODO put these in memory in a new table? # TODO methods for safely changing these i, s, ru, re = get_default_conditions(task_data, agent, self) self.init_condition = i self.stop_condition = s self.run_condition = ru self.remove_condition = re TripleNode.create( self.agent.memory, subj=self.memid, pred_text="has_name", obj_text=self.__class__.__name__.lower(), ) TaskNode(agent.memory, self.memid).update_task(task=self)
def task_stack_get_all(self) -> List["TaskNode"]: r = self._db_read(""" SELECT uuid FROM Tasks WHERE paused=0 AND finished_at<0 ORDER BY created_at """) return [TaskNode(self, memid) for memid, in r]
def task_stack_push(self, task: Task, parent_memid: str = None, chat_effect: bool = False) -> "TaskNode": memid = TaskNode.create(self, task) # Relations if parent_memid: self.add_triple(memid, "_has_parent_task", parent_memid) if chat_effect: chat = self.get_most_recent_incoming_chat() assert chat is not None, "chat_effect=True with no incoming chats" self.add_triple(chat.memid, "chat_effect_", memid) # Return newly created object return TaskNode(self, memid)
def append(self, task): if self.prev is not None: prev_finished = TaskStatusCondition(self.agent, self.prev.memid, status="finished") cdict = { "init_condition": AndCondition(self.agent, [task.init_condition, prev_finished]) } TaskNode(self.agent.memory, task.memid).update_condition(cdict) else: self.fuse = SwitchCondition(self.agent) cdict = { "init_condition": AndCondition(self.agent, [task.init_condition, self.fuse]) } TaskNode(self.agent.memory, task.memid).update_condition(cdict) self.fuse.set_status(False) self.prev = task self.task_list.append(task)
def task_stack_peek(self) -> Optional["TaskNode"]: r = self._db_read_one(""" SELECT uuid FROM Tasks WHERE finished_at < 0 AND paused = 0 ORDER BY created_at DESC LIMIT 1 """) if r: return TaskNode(self, r[0]) else: return None
def task_stack_find_lowest_instance( self, cls_names: Union[str, Sequence[str]]) -> Optional["TaskNode"]: """Find and return the lowest item in the stack of the given class(es)""" names = [cls_names] if type(cls_names) == str else cls_names memid, = self._db_read_one( "SELECT uuid FROM Tasks WHERE {} ORDER BY created_at LIMIT 1". format(" OR ".join(["action_name=?" for _ in names])), *names, ) if memid is not None: return TaskNode(self, memid) else: return None
def get_last_finished_root_task(self, action_name: str = None, recency: int = None): q = """ SELECT uuid FROM Tasks WHERE finished_at >= ? {} ORDER BY created_at DESC """.format(" AND action_name=?" if action_name else "") if recency is None: recency = self.time.round_time(300) args: List = [self.get_time() - recency] if action_name: args.append(action_name) memids = [r[0] for r in self._db_read(q, *args)] for memid in memids: if self._db_read_one( "SELECT uuid FROM Triples WHERE pred='_has_parent_task' AND subj=?", memid): # not a root task continue return TaskNode(self, memid)
def get_task_by_id(self, memid: str) -> "TaskNode": return TaskNode(self, memid)
def __init__(self, agent, task_data): super().__init__(agent, task_data=task_data) self.tasks_fn = task_data.get("new_tasks") if hasattr(self.tasks_fn, "fuse"): self.tasks_fn.fuse.set_status(True) TaskNode(self.agent.memory, self.memid).update_task(task=self)
def add_child_task(self, t, prio=1): TaskNode(self.agent.memory, self.memid).add_child_task(t, prio=prio)