mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-15 10:14:32 +00:00
feat: history overwrite
This commit is contained in:
1 parent
cdd6ff6557
commit
0c15af90b1
21 files changed
+635
-70
No files matched your search
@@ -330,6 +330,170 @@ class TestStreamProcessorDocPrefetch:
|
||||
assert docs_together is not None
|
||||
assert "Agent doc content" in docs_together
|
||||
|
||||
def test_configure_source_treats_default_string_as_no_docs(self, mock_mongo_db):
|
||||
from application.api.answer.services.stream_processor import StreamProcessor
|
||||
from application.core.settings import settings
|
||||
|
||||
db = mock_mongo_db[settings.MONGO_DB_NAME]
|
||||
agents_collection = db["agents"]
|
||||
|
||||
agent_id = ObjectId()
|
||||
agents_collection.insert_one(
|
||||
{
|
||||
"_id": agent_id,
|
||||
"key": "agent_default_source_key",
|
||||
"user": "user_123",
|
||||
"prompt_id": "default",
|
||||
"agent_type": "classic",
|
||||
"source": "default",
|
||||
}
|
||||
)
|
||||
|
||||
processor = StreamProcessor(
|
||||
{"question": "Hi", "api_key": "agent_default_source_key"},
|
||||
None,
|
||||
)
|
||||
processor._configure_agent()
|
||||
processor._configure_source()
|
||||
|
||||
assert processor.source == {}
|
||||
assert processor.all_sources == []
|
||||
|
||||
def test_prefetch_skipped_when_no_active_docs(self, mock_mongo_db):
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from application.api.answer.services.stream_processor import StreamProcessor
|
||||
|
||||
processor = StreamProcessor(
|
||||
{"question": "Hi there"},
|
||||
{"sub": "user_123"},
|
||||
)
|
||||
processor.initialize()
|
||||
processor.create_retriever = MagicMock()
|
||||
|
||||
docs_together, docs = processor.pre_fetch_docs("Hi there")
|
||||
|
||||
processor.create_retriever.assert_not_called()
|
||||
assert docs_together is None
|
||||
assert docs is None
|
||||
|
||||
def test_prefetch_skipped_when_active_docs_is_default(self, mock_mongo_db):
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from application.api.answer.services.stream_processor import StreamProcessor
|
||||
|
||||
processor = StreamProcessor(
|
||||
{"question": "Hi", "active_docs": "default"},
|
||||
{"sub": "user_123"},
|
||||
)
|
||||
processor.initialize()
|
||||
processor.create_retriever = MagicMock()
|
||||
|
||||
docs_together, docs = processor.pre_fetch_docs("Hi")
|
||||
|
||||
processor.create_retriever.assert_not_called()
|
||||
assert docs_together is None
|
||||
assert docs is None
|
||||
|
||||
def test_agent_retriever_and_chunks_propagate_to_retriever_config(self, mock_mongo_db):
|
||||
from application.api.answer.services.stream_processor import StreamProcessor
|
||||
from application.core.settings import settings
|
||||
|
||||
db = mock_mongo_db[settings.MONGO_DB_NAME]
|
||||
agents_collection = db["agents"]
|
||||
source_id = ObjectId()
|
||||
db["sources"].insert_one(
|
||||
{"_id": source_id, "name": "src", "retriever": "hybrid", "chunks": 5}
|
||||
)
|
||||
|
||||
agent_id = ObjectId()
|
||||
agents_collection.insert_one(
|
||||
{
|
||||
"_id": agent_id,
|
||||
"key": "agent_ret_key",
|
||||
"user": "user_123",
|
||||
"prompt_id": "default",
|
||||
"agent_type": "classic",
|
||||
"retriever": "hybrid",
|
||||
"chunks": 5,
|
||||
"source": DBRef("sources", source_id),
|
||||
}
|
||||
)
|
||||
|
||||
processor = StreamProcessor(
|
||||
{"question": "Test", "api_key": "agent_ret_key"},
|
||||
None,
|
||||
)
|
||||
processor.initialize()
|
||||
|
||||
assert processor.retriever_config["retriever_name"] == "hybrid"
|
||||
assert processor.retriever_config["chunks"] == 5
|
||||
|
||||
def test_request_retriever_and_chunks_override_agent_config(self, mock_mongo_db):
|
||||
from application.api.answer.services.stream_processor import StreamProcessor
|
||||
from application.core.settings import settings
|
||||
|
||||
db = mock_mongo_db[settings.MONGO_DB_NAME]
|
||||
agents_collection = db["agents"]
|
||||
|
||||
agent_id = ObjectId()
|
||||
agents_collection.insert_one(
|
||||
{
|
||||
"_id": agent_id,
|
||||
"key": "agent_override_key",
|
||||
"user": "user_123",
|
||||
"prompt_id": "default",
|
||||
"agent_type": "classic",
|
||||
"retriever": "hybrid",
|
||||
"chunks": 5,
|
||||
}
|
||||
)
|
||||
|
||||
processor = StreamProcessor(
|
||||
{
|
||||
"question": "Test",
|
||||
"api_key": "agent_override_key",
|
||||
"retriever": "classic",
|
||||
"chunks": 7,
|
||||
},
|
||||
None,
|
||||
)
|
||||
processor.initialize()
|
||||
|
||||
assert processor.retriever_config["retriever_name"] == "classic"
|
||||
assert processor.retriever_config["chunks"] == 7
|
||||
|
||||
def test_agent_data_fetched_once_per_request(self, mock_mongo_db):
|
||||
from unittest.mock import patch
|
||||
|
||||
from application.api.answer.services.stream_processor import StreamProcessor
|
||||
from application.core.settings import settings
|
||||
|
||||
db = mock_mongo_db[settings.MONGO_DB_NAME]
|
||||
agents_collection = db["agents"]
|
||||
|
||||
agent_id = ObjectId()
|
||||
agents_collection.insert_one(
|
||||
{
|
||||
"_id": agent_id,
|
||||
"key": "agent_once_key",
|
||||
"user": "user_123",
|
||||
"prompt_id": "default",
|
||||
"agent_type": "classic",
|
||||
}
|
||||
)
|
||||
|
||||
processor = StreamProcessor(
|
||||
{"question": "Test", "api_key": "agent_once_key"},
|
||||
None,
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
processor, "_get_data_from_api_key", wraps=processor._get_data_from_api_key
|
||||
) as spy:
|
||||
processor.initialize()
|
||||
assert spy.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestStreamProcessorAttachments:
|
||||
|
||||
@@ -566,14 +566,13 @@ class TestConfigureSource:
|
||||
decoded_token={"sub": "u"},
|
||||
)
|
||||
sp.agent_key = None
|
||||
agent_data = {
|
||||
sp._agent_data = {
|
||||
"sources": [
|
||||
{"id": "src1", "retriever": "classic"},
|
||||
{"id": "src2", "retriever": "hybrid"},
|
||||
],
|
||||
"source": None,
|
||||
}
|
||||
sp._get_data_from_api_key = MagicMock(return_value=agent_data)
|
||||
sp._configure_source()
|
||||
assert sp.source == {"active_docs": ["src1", "src2"]}
|
||||
assert len(sp.all_sources) == 2
|
||||
@@ -593,12 +592,11 @@ class TestConfigureSource:
|
||||
decoded_token={"sub": "u"},
|
||||
)
|
||||
sp.agent_key = None
|
||||
agent_data = {
|
||||
sp._agent_data = {
|
||||
"sources": [],
|
||||
"source": "single_src",
|
||||
"retriever": "classic",
|
||||
}
|
||||
sp._get_data_from_api_key = MagicMock(return_value=agent_data)
|
||||
sp._configure_source()
|
||||
assert sp.source == {"active_docs": "single_src"}
|
||||
assert len(sp.all_sources) == 1
|
||||
@@ -618,8 +616,7 @@ class TestConfigureSource:
|
||||
decoded_token={"sub": "u"},
|
||||
)
|
||||
sp.agent_key = None
|
||||
agent_data = {"sources": [], "source": None}
|
||||
sp._get_data_from_api_key = MagicMock(return_value=agent_data)
|
||||
sp._agent_data = {"sources": [], "source": None}
|
||||
sp._configure_source()
|
||||
assert sp.source == {}
|
||||
assert sp.all_sources == []
|
||||
@@ -639,11 +636,10 @@ class TestConfigureSource:
|
||||
decoded_token={"sub": "u"},
|
||||
)
|
||||
sp.agent_key = "agent_key_123"
|
||||
agent_data = {
|
||||
sp._agent_data = {
|
||||
"sources": [{"id": "s1", "retriever": "classic"}],
|
||||
"source": None,
|
||||
}
|
||||
sp._get_data_from_api_key = MagicMock(return_value=agent_data)
|
||||
sp._configure_source()
|
||||
assert sp.source == {"active_docs": ["s1"]}
|
||||
|
||||
@@ -662,11 +658,10 @@ class TestConfigureSource:
|
||||
decoded_token={"sub": "u"},
|
||||
)
|
||||
sp.agent_key = None
|
||||
agent_data = {
|
||||
sp._agent_data = {
|
||||
"sources": [{"id": None}, {"retriever": "classic"}],
|
||||
"source": None,
|
||||
}
|
||||
sp._get_data_from_api_key = MagicMock(return_value=agent_data)
|
||||
sp._configure_source()
|
||||
assert sp.source == {}
|
||||
|
||||
@@ -1189,6 +1184,8 @@ class TestConfigureAgent:
|
||||
"chunks": "5",
|
||||
})
|
||||
sp._configure_agent()
|
||||
sp.model_id = "test-model"
|
||||
sp._configure_retriever()
|
||||
assert sp.agent_config["workflow"] == "wf_123"
|
||||
assert sp.agent_config["workflow_owner"] == "user1"
|
||||
assert sp.retriever_config["retriever_name"] == "hybrid"
|
||||
@@ -1211,6 +1208,8 @@ class TestConfigureAgent:
|
||||
"chunks": "not_a_number",
|
||||
})
|
||||
sp._configure_agent()
|
||||
sp.model_id = "test-model"
|
||||
sp._configure_retriever()
|
||||
assert sp.retriever_config["chunks"] == 2
|
||||
|
||||
|
||||
@@ -1763,8 +1762,8 @@ class TestConfigureAgentAdditionalPaths:
|
||||
assert sp.decoded_token == {"sub": "owner_user"}
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_configure_agent_with_source_in_data_key(self):
|
||||
"""Cover line 463-464: data_key has 'source' set."""
|
||||
def test_configure_source_picks_up_cached_agent_data(self):
|
||||
"""After _configure_agent caches _agent_data, _configure_source uses it."""
|
||||
sp = self._make_sp()
|
||||
sp._resolve_agent_id = MagicMock(return_value="agent_id_1")
|
||||
sp._get_agent_key = MagicMock(return_value=("agent_key", False, None))
|
||||
@@ -1780,6 +1779,7 @@ class TestConfigureAgentAdditionalPaths:
|
||||
"source": "my_source",
|
||||
})
|
||||
sp._configure_agent()
|
||||
sp._configure_source()
|
||||
assert sp.source == {"active_docs": "my_source"}
|
||||
|
||||
@pytest.mark.unit
|
||||
@@ -2067,7 +2067,7 @@ class TestPreFetchDocsFullPaths:
|
||||
"chunks": 2,
|
||||
"doc_token_limit": 50000,
|
||||
}
|
||||
sp.source = {}
|
||||
sp.source = {"active_docs": ["src1"]}
|
||||
sp.model_id = "test-model"
|
||||
sp.agent_id = None
|
||||
return sp
|
||||
|
||||
Reference in new issue
Block a user