update memory gateway
This commit is contained in:
@ -60,7 +60,7 @@ def install_test_stubs() -> None:
|
||||
install_test_stubs()
|
||||
|
||||
from memory_gateway.server import app
|
||||
from memory_gateway.types import Config, SearchResult, ServerConfig
|
||||
from memory_gateway.types import Config, ObsidianConfig, SearchResult, ServerConfig
|
||||
|
||||
|
||||
class FakeOVClient:
|
||||
@ -71,7 +71,7 @@ class FakeOVClient:
|
||||
return SearchResult(
|
||||
results=[
|
||||
{
|
||||
"uri": "viking://soc/test",
|
||||
"uri": "viking://memory-gateway/test",
|
||||
"abstract": query,
|
||||
"score": 1.0,
|
||||
"context_type": "memory",
|
||||
@ -107,6 +107,16 @@ async def fake_get_openviking_client():
|
||||
return FakeOVClient()
|
||||
|
||||
|
||||
async def fake_summarize_with_llm(content, **kwargs):
|
||||
return {
|
||||
"title": kwargs.get("title") or "Fake LLM title",
|
||||
"summary": f"LLM summary: {content[:80]}",
|
||||
"key_points": ["LLM key point", "Preserve IP 198.51.100.20"],
|
||||
"tags": kwargs.get("tags") or ["fake"],
|
||||
"llm": {"provider": "fake", "model": "fake-model"},
|
||||
}
|
||||
|
||||
|
||||
def build_headers(api_key: str | None):
|
||||
return {"x-api-key": api_key} if api_key is not None else {}
|
||||
|
||||
@ -120,6 +130,7 @@ def test_health_requires_api_key(monkeypatch):
|
||||
"memory_gateway.server.get_openviking_client",
|
||||
fake_get_openviking_client,
|
||||
)
|
||||
monkeypatch.setattr("memory_gateway.server.summarize_with_llm", fake_summarize_with_llm)
|
||||
|
||||
with TestClient(app) as client:
|
||||
response = client.get("/health")
|
||||
@ -149,7 +160,8 @@ def test_mcp_rpc_lists_tools_with_api_key(monkeypatch):
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["jsonrpc"] == "2.0"
|
||||
assert len(payload["result"]["tools"]) == 6
|
||||
assert len(payload["result"]["tools"]) == 7
|
||||
assert any(tool["name"] == "commit_summary" for tool in payload["result"]["tools"])
|
||||
|
||||
|
||||
def test_search_passes_through_gateway(monkeypatch):
|
||||
@ -168,3 +180,73 @@ def test_search_passes_through_gateway(monkeypatch):
|
||||
payload = response.json()
|
||||
assert payload["total"] == 1
|
||||
assert payload["results"][0]["abstract"] == "phishing"
|
||||
|
||||
|
||||
def test_summary_endpoint_builds_generic_artifact(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
"memory_gateway.server.get_config",
|
||||
lambda: Config(server=ServerConfig(api_key="")),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"memory_gateway.server.get_openviking_client",
|
||||
fake_get_openviking_client,
|
||||
)
|
||||
monkeypatch.setattr("memory_gateway.server.summarize_with_llm", fake_summarize_with_llm)
|
||||
|
||||
with TestClient(app) as client:
|
||||
response = client.post(
|
||||
"/api/summary",
|
||||
json={
|
||||
"title": "Demo investigation summary",
|
||||
"content": "结论:这是一次高价值沉淀。\n- 证据:命中历史 case。\n- 建议:后续复用该处置路径。",
|
||||
"namespace": "demo",
|
||||
"memory_type": "knowledge",
|
||||
"tags": ["demo", "summary"],
|
||||
"persist_as": "none",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["status"] == "ok"
|
||||
assert payload["artifact"]["title"] == "Demo investigation summary"
|
||||
assert payload["artifact"]["namespace"] == "demo"
|
||||
assert payload["artifact"]["memory_type"] == "knowledge"
|
||||
assert payload["artifact"]["summary"].startswith("LLM summary:")
|
||||
assert payload["artifact"]["llm"]["provider"] == "fake"
|
||||
assert payload["memory_result"] is None
|
||||
assert payload["resource_result"] is None
|
||||
|
||||
|
||||
def test_knowledge_upload_converts_saves_and_commits(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(
|
||||
"memory_gateway.server.get_config",
|
||||
lambda: Config(
|
||||
server=ServerConfig(api_key=""),
|
||||
obsidian=ObsidianConfig(vault_path=str(tmp_path / "vault"), knowledge_dir="01_Knowledge/Uploaded"),
|
||||
),
|
||||
)
|
||||
monkeypatch.setattr("memory_gateway.server.get_openviking_client", fake_get_openviking_client)
|
||||
monkeypatch.setattr("memory_gateway.server.summarize_with_llm", fake_summarize_with_llm)
|
||||
monkeypatch.setattr("memory_gateway.server.convert_file_to_markdown", lambda path: "# Uploaded Doc\n\nImportant uploaded knowledge.")
|
||||
|
||||
with TestClient(app) as client:
|
||||
response = client.post(
|
||||
"/api/knowledge/upload",
|
||||
data={
|
||||
"title": "Uploaded Knowledge",
|
||||
"namespace": "demo",
|
||||
"knowledge_type": "playbook",
|
||||
"tags": "demo,upload",
|
||||
"persist_as": "resource",
|
||||
},
|
||||
files={"file": ("sample.txt", b"hello", "text/plain")},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
payload = response.json()
|
||||
assert payload["status"] == "ok"
|
||||
assert payload["artifact"]["schema_version"] == "memory-gateway.knowledge_upload.v1"
|
||||
assert payload["artifact"]["knowledge_type"] == "playbook"
|
||||
assert payload["artifact"]["markdown_content"].startswith("# Uploaded Doc")
|
||||
assert payload["resource_result"]["status"] == "ok"
|
||||
assert (tmp_path / "vault" / payload["artifact"]["obsidian_relative_path"]).exists()
|
||||
|
||||
Reference in New Issue
Block a user