-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
73 lines (62 loc) · 2.1 KB
/
memory.py
File metadata and controls
73 lines (62 loc) · 2.1 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
"""
memory.py — Session memory for OpenMobile agent.
Keeps a rolling history of steps so the LLMs can reason about
what has already been tried without hallucinating.
"""
from dataclasses import dataclass, field
from typing import Optional, List
from config import MEMORY_WINDOW
@dataclass
class Step:
step_num: int
thought: str
action_name: str
action_params: dict
result: str # "OK" | "FAIL" | "FINISH"
screen_desc: str = ""
class SessionMemory:
"""Stores the step history for a single goal execution."""
def __init__(self):
self.goal: str = ""
self.steps: List[Step] = []
self.total_steps: int = 0
self.last_thought: str = ""
self.last_action: str = ""
self.status: str = "idle" # idle | running | done | failed
def reset(self, goal: str):
self.goal = goal
self.steps = []
self.total_steps = 0
self.last_thought = ""
self.last_action = ""
self.status = "running"
def add_step(self, thought: str, action_name: str, action_params: dict,
result: str, screen_desc: str = ""):
self.total_steps += 1
s = Step(
step_num=self.total_steps,
thought=thought,
action_name=action_name,
action_params=action_params,
result=result,
screen_desc=screen_desc,
)
self.steps.append(s)
self.last_thought = thought
self.last_action = f"{action_name}({action_params})"
def get_context_window(self) -> str:
"""Returns the last MEMORY_WINDOW steps as a plain-text summary for prompts."""
recent = self.steps[-MEMORY_WINDOW:]
if not recent:
return "No steps taken yet."
lines = []
for s in recent:
lines.append(
f"Step {s.step_num}: thought='{s.thought}' | "
f"action={s.action_name}({s.action_params}) | result={s.result}"
)
return "\n".join(lines)
def mark_done(self):
self.status = "done"
def mark_failed(self):
self.status = "failed"