79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
"""Example custom extension: Quick Scratchpad & Local Notes.
|
|
|
|
Shows how easy it is to write and drop a new extension into HomeAgent.
|
|
Any file with @tool decorated functions in extensions/ is automatically discovered!
|
|
"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from homeagent.tools.base import tool
|
|
|
|
NOTES_FILE = Path("data") / "scratchpad_notes.json"
|
|
|
|
|
|
def _read_notes() -> dict[str, str]:
|
|
if not NOTES_FILE.exists():
|
|
return {}
|
|
try:
|
|
with open(NOTES_FILE, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def _save_notes(notes: dict[str, str]) -> None:
|
|
NOTES_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
with open(NOTES_FILE, "w", encoding="utf-8") as f:
|
|
json.dump(notes, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
@tool(
|
|
name="save_note",
|
|
description="Сохранить локальную текстовую заметку с уникальным заголовком.",
|
|
)
|
|
def save_note(title: str, content: str) -> str:
|
|
"""Сохранение заметки в блокнот.
|
|
|
|
Args:
|
|
title: Заголовок заметки
|
|
content: Содержимое заметки
|
|
"""
|
|
notes = _read_notes()
|
|
notes[title.strip()] = content.strip()
|
|
_save_notes(notes)
|
|
return f"Заметка '{title}' успешно сохранена."
|
|
|
|
|
|
@tool(
|
|
name="get_note",
|
|
description="Прочитать сохраненную локальную заметку по ее заголовку.",
|
|
)
|
|
def get_note(title: str) -> str:
|
|
"""Чтение заметки.
|
|
|
|
Args:
|
|
title: Заголовок заметки
|
|
"""
|
|
notes = _read_notes()
|
|
content = notes.get(title.strip())
|
|
if content is None:
|
|
return f"Заметка с заголовком '{title}' не найдена."
|
|
return f"**{title}**:\n{content}"
|
|
|
|
|
|
@tool(
|
|
name="list_notes",
|
|
description="Показать список всех сохраненных локальных заметок.",
|
|
)
|
|
def list_notes() -> str:
|
|
"""Список всех заметок."""
|
|
notes = _read_notes()
|
|
if not notes:
|
|
return "Список заметок пуст."
|
|
lines = ["Сохраненные заметки:"]
|
|
for title in notes.keys():
|
|
lines.append(f"- {title}")
|
|
return "\n".join(lines)
|