feat: 新增agent

This commit is contained in:
2026-09-18 17:34:38 +08:00
parent 0d9b5a21c2
commit bc73668737
64 changed files with 3293 additions and 18 deletions
+66
View File
@@ -0,0 +1,66 @@
"""用户记忆用例。"""
from __future__ import annotations
import hashlib
import re
from domain.models import Actor, MemoryKind, UserMemory
from domain.ports import MemoryRepository
from config.settings import Settings
_REMEMBER_RE = re.compile(
r"^(?:请?记住|记住一下)[::\s]*(.+)$",
re.DOTALL,
)
class MemoryService:
def __init__(self, settings: Settings, repo: MemoryRepository) -> None:
self._settings = settings
self._repo = repo
async def list_public(self, actor: Actor, limit: int = 50) -> list[dict]:
rows = await self._repo.list_active(actor.company_id, actor.user_id, limit)
return [
{
"id": m.id,
"kind": m.kind.value,
"key": m.key,
"text": m.text,
"updatedAt": m.updated_at.isoformat() + "Z",
}
for m in rows
]
async def delete(self, actor: Actor, memory_id: str) -> bool:
return await self._repo.soft_delete(actor.company_id, actor.user_id, memory_id)
async def lines_for_prompt(self, actor: Actor) -> list[str]:
rows = await self._repo.list_active(
actor.company_id, actor.user_id, self._settings.memory_inject_limit
)
return [m.text.strip() for m in rows if m.text and m.text.strip()]
async def maybe_capture_from_user(
self, actor: Actor, *, thread_id: str, text: str
) -> None:
"""显式「记住…」指令写入记忆;自动抽取留给 P2。"""
m = _REMEMBER_RE.match((text or "").strip())
if not m:
return
body = m.group(1).strip()
if not body:
return
key = "remember:" + hashlib.sha1(body.encode("utf-8")).hexdigest()[:16]
await self._repo.upsert(
UserMemory(
company_id=actor.company_id,
user_id=actor.user_id,
kind=MemoryKind.PREFERENCE,
key=key,
text=body[:1000],
source_thread_id=thread_id,
)
)