refactor(sessions): discover target handles through search (#6315)
Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
This commit is contained in:
1 parent
acc3750c28
commit
b6d9009bb4
6 files changed
+93
-116
No files matched your search
@@ -5,7 +5,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
from collections import OrderedDict, deque
|
||||
from collections.abc import Callable
|
||||
@@ -59,46 +58,6 @@ class _PendingReply:
|
||||
timer: _CancelHandle | None = None
|
||||
|
||||
|
||||
@tool_parameters(tool_parameters_schema())
|
||||
class ListSessionsTool(Tool):
|
||||
"""List the handles of other persisted sessions."""
|
||||
|
||||
def __init__(self, sessions: SessionManager) -> None:
|
||||
self._handles = SessionHandleResolver(sessions)
|
||||
|
||||
@classmethod
|
||||
def create(cls, ctx: ToolContext) -> Tool:
|
||||
if ctx.sessions is None:
|
||||
raise RuntimeError("list_sessions requires a session manager")
|
||||
return cls(ctx.sessions)
|
||||
|
||||
@classmethod
|
||||
def enabled(cls, ctx: ToolContext) -> bool:
|
||||
return ctx.sessions is not None
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "list_sessions"
|
||||
|
||||
@property
|
||||
def description(self) -> str:
|
||||
return "List other persisted sessions by @handle."
|
||||
|
||||
async def execute(self, **kwargs: Any) -> str:
|
||||
request = current_request_context()
|
||||
if request is None or not request.session_key:
|
||||
return ToolResult.error("Error: session context is unavailable")
|
||||
handles = await self._handles.alist_all()
|
||||
return json.dumps(
|
||||
[
|
||||
f"@{handle.name}"
|
||||
for handle in handles
|
||||
if handle.session_key != request.session_key
|
||||
],
|
||||
ensure_ascii=True,
|
||||
)
|
||||
|
||||
|
||||
@tool_parameters(
|
||||
tool_parameters_schema(
|
||||
to=StringSchema("Target @handle."),
|
||||
|
||||
@@ -54,6 +54,7 @@ def _session_ref(session_key: str) -> str:
|
||||
class _SessionTool(Tool):
|
||||
def __init__(self, sessions: SessionManager) -> None:
|
||||
self._access = SessionHistoryReader(sessions)
|
||||
self._handles = SessionHandleResolver(sessions)
|
||||
|
||||
@classmethod
|
||||
def create(cls, ctx: ToolContext) -> Tool:
|
||||
@@ -81,7 +82,7 @@ class _SessionTool(Tool):
|
||||
)
|
||||
)
|
||||
class SearchSessionsTool(_SessionTool):
|
||||
"""Find persisted sessions without changing them."""
|
||||
"""Find persisted conversations and their public handles."""
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
@@ -90,10 +91,10 @@ class SearchSessionsTool(_SessionTool):
|
||||
@property
|
||||
def description(self) -> str:
|
||||
return (
|
||||
"Search other persisted conversation sessions by title or recent visible message "
|
||||
"text. Use this only when the user asks about a past conversation or when prior "
|
||||
"discussion is needed to answer. Results contain bounded excerpts; use "
|
||||
"read_session for more context. When citing a result, link its title to the exact "
|
||||
"Search other persisted conversations by title or visible message text. Use when "
|
||||
"past discussion is relevant or to find the target of a user-requested session "
|
||||
"message. Results include @handles for read_session and send_session_message "
|
||||
"when available, plus bounded excerpts. When citing a result, link its title to the exact "
|
||||
"session_ref using Markdown. The current session is excluded."
|
||||
)
|
||||
|
||||
@@ -111,6 +112,7 @@ class SearchSessionsTool(_SessionTool):
|
||||
_SEARCH_LIMIT,
|
||||
exclude_session_key=current_request_session_key(),
|
||||
)
|
||||
handles = await self._handles.alist_all_by_key() if matches else {}
|
||||
needle = query.casefold()
|
||||
result = {
|
||||
"notice": _UNTRUSTED_NOTICE,
|
||||
@@ -119,6 +121,10 @@ class SearchSessionsTool(_SessionTool):
|
||||
{
|
||||
"session_key": match["session_key"],
|
||||
"session_ref": _session_ref(match["session_key"]),
|
||||
"handle": (
|
||||
f"@{handles[match['session_key']].name}"
|
||||
if match["session_key"] in handles else None
|
||||
),
|
||||
"title": match["title"],
|
||||
"updated_at": match["updated_at"],
|
||||
"excerpts": [
|
||||
@@ -156,10 +162,6 @@ class SearchSessionsTool(_SessionTool):
|
||||
class ReadSessionTool(_SessionTool):
|
||||
"""Read bounded visible history from one persisted session."""
|
||||
|
||||
def __init__(self, sessions: SessionManager) -> None:
|
||||
super().__init__(sessions)
|
||||
self._handles = SessionHandleResolver(sessions)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "read_session"
|
||||
|
||||
Reference in new issue
Block a user