WIP memory editor

This commit is contained in:
2024-11-23 21:36:50 +01:00
parent dbc0068a82
commit 2c820f6ead
15 changed files with 467 additions and 37 deletions

View File

@@ -15,14 +15,9 @@ class Entry(ABC):
Args:
id: Unique identifier for this entry
"""
self._id = id
self.id = id
self._change_handlers: List[Callable[['Entry'], None]] = []
@property
def id(self) -> str:
"""Get entry's unique identifier."""
return self._id
def add_change_handler(self, handler: Callable[['Entry'], None]) -> None:
"""Add a callback for entry changes."""
if handler not in self._change_handlers:

View File

@@ -135,7 +135,7 @@ class BackgroundEntry(Entry):
Returns:
ET.Element: XML element containing the entry's data
"""
element = ET.Element("background", {"id": self._id})
element = ET.Element("background", {"id": self.id})
if self._process is not None:
element.set("pid", str(self._process.pid))
elif self.exit_code is not None:
@@ -154,7 +154,7 @@ class BackgroundEntry(Entry):
"""
return {
"type": "background",
"id": self._id,
"id": self.id,
"script": self.script,
"stdout": self.stdout,
"stderr": self.stderr,

View File

@@ -116,5 +116,4 @@ class EntryFactory:
if "content" in data:
entry.content = data["content"]
if "written" in data:
entry.written = bool(data["written"])
entry.notify_change()
entry.written = bool(data["written"])

View File

@@ -35,7 +35,7 @@ class ParseErrorEntry(Entry):
Returns:
ET.Element: XML element containing the entry's data
"""
element = ET.Element("parse_error", {"id": self._id})
element = ET.Element("parse_error", {"id": self.id})
error_elem = ET.SubElement(element, "error")
error_elem.text = self.error
content_elem = ET.SubElement(element, "content")
@@ -49,7 +49,7 @@ class ParseErrorEntry(Entry):
"""
return {
"type": "parse_error",
"id": self._id,
"id": self.id,
"content": self.content,
"error": self.error,
}

View File

@@ -21,7 +21,7 @@ class ReadEntry(Entry):
io_buffer: Buffer to use for IO operations
"""
super().__init__(id)
self.content: str = "",
self.content: str = ""
self.read = False
self._io_buffer = io_buffer
@@ -42,7 +42,7 @@ class ReadEntry(Entry):
Returns:
ET.Element: XML element containing the entry's data
"""
element = ET.Element("read_stdin", {"id": self._id})
element = ET.Element("read_stdin", {"id": self.id})
if self.read:
element.text = self.content
@@ -54,7 +54,7 @@ class ReadEntry(Entry):
"""
return {
"type": "read_stdin",
"id": self._id,
"id": self.id,
"content": self.content,
"read": self.read,
}

View File

@@ -34,7 +34,7 @@ class ReasoningEntry(Entry):
Returns:
ET.Element: XML element containing the entry's data
"""
element = ET.Element("reasoning", {"id": self._id})
element = ET.Element("reasoning", {"id": self.id})
element.text = self.content
return element
@@ -45,6 +45,6 @@ class ReasoningEntry(Entry):
"""
return {
"type": "reasoning",
"id": self._id,
"id": self.id,
"content": self.content,
}

View File

@@ -32,9 +32,9 @@ class RepeatEntry(Entry):
self.script = script
self.timeout = timeout
self.limit = limit
self.stdout: str = "",
self.stderr: str = "",
self.exit_code: Optional[int] = None,
self.stdout: str = ""
self.stderr: str = ""
self.exit_code: Optional[int] = None
self.timed_out: bool = False
def update(self) -> None:

View File

@@ -32,10 +32,10 @@ class SingleEntry(Entry):
self.script = script
self.timeout = timeout
self.limit = limit
self.stdout: str = "",
self.stderr: str = "",
self.stdout: str = ""
self.stderr: str = ""
self.exit_code: Optional[int] = None
self.executed: bool = False,
self.executed: bool = False
self.timed_out: bool = False
def reset(self) -> None:
@@ -111,4 +111,6 @@ class SingleEntry(Entry):
"stdout": self.stdout,
"stderr": self.stderr,
"exit_code": self.exit_code,
"executed": self.executed,
"timed_out": self.timed_out
}

View File

@@ -54,6 +54,6 @@ class WriteEntry(Entry):
"""
return {
"type": "write",
"id": self._id,
"id": self.id,
"content": self.content,
}

View File

@@ -43,9 +43,10 @@ class Api:
self._app.router.add_post("/api/auto_approver/llm", self._set_llm_name)
self._app.router.add_get("/api/memory", self._get_memory)
self._app.router.add_post("/api/memory/entry", self._create_entry)
self._app.router.add_put("/api/memory/entry/{id}", self._update_entry)
self._app.router.add_put("/api/memory/entry/{id}", self._save_entry)
self._app.router.add_delete("/api/memory/entry/{id}", self._delete_entry)
self._app.router.add_post("/api/memory/entry/{id}/reset", self._reset_entry)
self._app.router.add_post("/api/memory/entry/{id}/update", self._update_entry)
async def _run_inference(self, request: web.Request) -> web.Response:
"""Start inference on specified LLM."""
@@ -212,7 +213,7 @@ class Api:
except (ValueError, TypeError) as e:
return web.Response(status=400, text=str(e))
async def _update_entry(self, request: web.Request) -> web.Response:
async def _save_entry(self, request: web.Request) -> web.Response:
"""Update properties of an existing entry."""
entry_id = request.match_info["id"]
data = await request.json()
@@ -221,6 +222,7 @@ class Api:
return web.Response(status=404, text="Entry not found")
try:
EntryFactory.update_entry(entry, data)
entry.notify_change()
return web.Response(status=200)
except ValueError as e:
return web.Response(status=400, text=str(e))
@@ -238,4 +240,18 @@ class Api:
if not entry:
return web.Response(status=404, text="Entry not found")
entry.reset()
return web.Response(status=200)
entry.notify_change()
return web.Response(status=200)
async def _update_entry(self, request: web.Request) -> web.Response:
"""Update an entry's state."""
entry_id = request.match_info["id"]
entry = self._working_memory.get_entry(entry_id)
if not entry:
return web.Response(status=404, text="Entry not found")
try:
entry.update()
entry.notify_change()
return web.Response(status=200)
except ValueError as e:
return web.Response(status=400, text=str(e))