Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

修复sessionId含特殊字符会报错 (#2682)

DuTao342152f0ac35
diff --git a/openviking_cli/client/http.py b/openviking_cli/client/http.py
index e6331de6e..90c65b53e 100644
--- a/openviking_cli/client/http.py
+++ b/openviking_cli/client/http.py
@@ -10,6 +10,7 @@ import uuid
import zipfile
from pathlib import Path
from typing import Any, Dict, List, Optional, Union
+from urllib.parse import quote
import httpx
@@ -281,6 +282,11 @@ class AsyncHTTPClient(BaseClient):
normalize_telemetry_request(telemetry)
return telemetry
+ @staticmethod
+ def _path_segment(value: str) -> str:
+ """Encode a value for safe use as one URL path segment."""
+ return quote(value, safe="")
+
@staticmethod
def _normalize_target_uri(
target_uri: Union[str, List[str]],
@@ -510,8 +516,9 @@ class AsyncHTTPClient(BaseClient):
if telemetry is not False:
payload["telemetry"] = telemetry
+ session_path = self._path_segment(session_id)
response = await self._http.post(
- f"/api/v1/sessions/{session_id}/messages/batch",
+ f"/api/v1/sessions/{session_path}/messages/batch",
json=payload,
)
response_data = self._handle_response_data(response)
@@ -1128,29 +1135,34 @@ class AsyncHTTPClient(BaseClient):
params = {}
if auto_create:
params["auto_create"] = "true"
- response = await self._http.get(f"/api/v1/sessions/{session_id}", params=params)
+ session_path = self._path_segment(session_id)
+ response = await self._http.get(f"/api/v1/sessions/{session_path}", params=params)
return self._handle_response(response)
async def get_session_context(
self, session_id: str, token_budget: int = 128_000
) -> Dict[str, Any]:
"""Get assembled session context."""
+ session_path = self._path_segment(session_id)
response = await self._http.get(
- f"/api/v1/sessions/{session_id}/context",
+ f"/api/v1/sessions/{session_path}/context",
params={"token_budget": token_budget},
)
return self._handle_response(response)
async def get_session_archive(self, session_id: str, archive_id: str) -> Dict[str, Any]:
"""Get one completed archive for a session."""
+ session_path = self._path_segment(session_id)
+ archive_path = self._path_segment(archive_id)
response = await self._http.get(
- f"/api/v1/sessions/{session_id}/archives/{archive_id}",
+ f"/api/v1/sessions/{session_path}/archives/{archive_path}",
)
return self._handle_response(response)
async def delete_session(self, session_id: str) -> None:
"""Delete a session."""
- response = await self._http.delete(f"/api/v1/sessions/{session_id}")
+ session_path = self._path_segment(session_id)
+ response = await self._http.delete(f"/api/v1/sessions/{session_path}")
self._handle_response(response)
async def get_task(self, task_id: str) -> Optional[Dict[str, Any]]:
@@ -1198,8 +1210,9 @@ class AsyncHTTPClient(BaseClient):
"keep_recent_count": keep_recent_count,
"telemetry": telemetry,
}
+ session_path = self._path_segment(session_id)
response = await self._http.post(
- f"/api/v1/sessions/{session_id}/commit",
+ f"/api/v1/sessions/{session_path}/commit",
json=payload,
)
response_data = self._handle_response_data(response)
@@ -1244,8 +1257,9 @@ class AsyncHTTPClient(BaseClient):
payload["telemetry"] = telemetry
payload = self._attach_legacy_message_scope(payload)
+ session_path = self._path_segment(session_id)
response = await self._http.post(
- f"/api/v1/sessions/{session_id}/messages",
+ f"/api/v1/sessions/{session_path}/messages",
json=payload,
)
response_data = self._handle_response_data(response)
diff --git a/tests/client/test_rebuild_clients.py b/tests/client/test_rebuild_clients.py
index 3d7f153ba..d72b0762e 100644
--- a/tests/client/test_rebuild_clients.py
+++ b/tests/client/test_rebuild_clients.py
@@ -156,6 +156,7 @@ async def test_local_client_batch_add_messages_forwards_to_session():
client = LocalClient.__new__(LocalClient)
client._service = SimpleNamespace(sessions=FakeSessions())
client._ctx = SimpleNamespace(user=SimpleNamespace(user_id="user-1"))
+ client._legacy_agent_id = None
result = await LocalClient.batch_add_messages(
client,
@@ -208,6 +209,7 @@ async def test_local_client_add_message_accepts_image_parts():
client = LocalClient.__new__(LocalClient)
client._service = SimpleNamespace(sessions=FakeSessions())
client._ctx = SimpleNamespace(user=SimpleNamespace(user_id="user-1"))
+ client._legacy_agent_id = None
result = await LocalClient.add_message(
client,
@@ -252,6 +254,30 @@ async def test_async_http_client_batch_add_messages_posts_batch_payload():
)
+async def test_async_http_client_batch_add_messages_url_encodes_session_id():
+ client = AsyncHTTPClient(url="http://localhost:1933")
+ fake_http = SimpleNamespace(post=AsyncMock(return_value=object()))
+ client._http = fake_http
+ client._handle_response_data = lambda _response: {
+ "result": {"session_id": "encoded-session", "message_count": 1, "added": 1}
+ }
+
+ session_id = (
+ "feishu__cli_a938e530eb7c9bd9__"
+ "oc_aa9e08fddf5727f9c53400a07ff505cd#om_x100b6ff6c3df48ace10030ac68d3eb4"
+ )
+
+ await client.batch_add_messages(session_id, [{"role": "user", "content": "hello"}])
+
+ fake_http.post.assert_awaited_once_with(
+ "/api/v1/sessions/"
+ "feishu__cli_a938e530eb7c9bd9__"
+ "oc_aa9e08fddf5727f9c53400a07ff505cd%23om_x100b6ff6c3df48ace10030ac68d3eb4"
+ "/messages/batch",
+ json={"messages": [{"role": "user", "content": "hello"}]},
+ )
+
+
async def test_async_http_client_reindex_posts_content_reindex():
client = AsyncHTTPClient(url="http://localhost:1933")
fake_http = SimpleNamespace(post=AsyncMock(return_value=object()))