mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-15 22:14:46 +00:00
feat: improved compatability layer and responses api handing
This commit is contained in:
1 parent
3e978759de
commit
0adf88abed
45 files changed
+2566
-337
No files matched your search
@@ -485,7 +485,11 @@ class TestCleanupPendingToolState:
|
||||
|
||||
# Pending but already expired — should be deleted.
|
||||
c2 = ConversationsRepository(pg_conn).create("u", "expired-pending")
|
||||
repo.save_state(c2["id"], "u", **_sample(), ttl_seconds=0)
|
||||
expired_state = _sample()
|
||||
expired_state["agent_config"] = {
|
||||
"reserved_message_id": "22222222-2222-2222-2222-222222222222"
|
||||
}
|
||||
repo.save_state(c2["id"], "u", **expired_state, ttl_seconds=0)
|
||||
|
||||
# Resuming within grace — should stay 'resuming'.
|
||||
c3 = ConversationsRepository(pg_conn).create("u", "fresh-resuming")
|
||||
@@ -518,16 +522,25 @@ class TestCleanupPendingToolState:
|
||||
with patch(
|
||||
"application.storage.db.engine.get_engine",
|
||||
return_value=fake_engine,
|
||||
):
|
||||
), patch(
|
||||
"application.api.answer.services.conversation_service."
|
||||
"ConversationService.finalize_message",
|
||||
) as finalize_expired:
|
||||
result = cleanup_pending_tool_state.run()
|
||||
|
||||
assert result["reverted"] == 1
|
||||
assert result["deleted"] == 1
|
||||
finalize_expired.assert_called_once()
|
||||
assert finalize_expired.call_args.args[0] == (
|
||||
"22222222-2222-2222-2222-222222222222"
|
||||
)
|
||||
assert finalize_expired.call_args.kwargs["status"] == "failed"
|
||||
|
||||
# Final state assertions.
|
||||
assert repo.load_state(c1["id"], "u")["status"] == "pending"
|
||||
assert repo.load_state(c2["id"], "u") is None
|
||||
assert repo.load_state(c3["id"], "u")["status"] == "resuming"
|
||||
assert repo.load_state(c3["id"], "u") is None
|
||||
assert repo.load_state_any(c3["id"], "u")["status"] == "resuming"
|
||||
c4_row = repo.load_state(c4["id"], "u")
|
||||
assert c4_row["status"] == "pending"
|
||||
assert c4_row["resumed_at"] is None
|
||||
|
||||
@@ -227,6 +227,72 @@ class TestCreate:
|
||||
resp = UserModelsCollectionResource().post()
|
||||
assert resp.status_code == 201
|
||||
|
||||
def test_create_accepts_responses_capabilities(self, app, pg_conn):
|
||||
from application.api.user.models.routes import (
|
||||
UserModelsCollectionResource,
|
||||
)
|
||||
|
||||
with patch("application.security.safe_url.socket.getaddrinfo") as gai:
|
||||
gai.return_value = [(None, None, None, None, ("104.18.0.1", 0))]
|
||||
with app.test_request_context(
|
||||
"/api/user/models",
|
||||
method="POST",
|
||||
json={
|
||||
"upstream_model_id": "gpt-custom",
|
||||
"display_name": "Responses model",
|
||||
"base_url": "https://example.openai.azure.com/openai/v1",
|
||||
"api_key": "k",
|
||||
"capabilities": {
|
||||
"api_flavor": "responses",
|
||||
"reasoning_effort": "high",
|
||||
},
|
||||
},
|
||||
):
|
||||
from flask import request
|
||||
|
||||
request.decoded_token = {"sub": "user-1"}
|
||||
with _patch_db(pg_conn):
|
||||
resp = UserModelsCollectionResource().post()
|
||||
|
||||
assert resp.status_code == 201
|
||||
assert resp.get_json()["capabilities"] == {
|
||||
"api_flavor": "responses",
|
||||
"reasoning_effort": "high",
|
||||
}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("capability", "value"),
|
||||
[("api_flavor", "grpc"), ("reasoning_effort", "extreme")],
|
||||
)
|
||||
def test_create_rejects_invalid_responses_capabilities(
|
||||
self, app, pg_conn, capability, value
|
||||
):
|
||||
from application.api.user.models.routes import (
|
||||
UserModelsCollectionResource,
|
||||
)
|
||||
|
||||
with patch("application.security.safe_url.socket.getaddrinfo") as gai:
|
||||
gai.return_value = [(None, None, None, None, ("104.18.0.1", 0))]
|
||||
with app.test_request_context(
|
||||
"/api/user/models",
|
||||
method="POST",
|
||||
json={
|
||||
"upstream_model_id": "gpt-custom",
|
||||
"display_name": "Bad model",
|
||||
"base_url": "https://example.openai.azure.com/openai/v1",
|
||||
"api_key": "k",
|
||||
"capabilities": {capability: value},
|
||||
},
|
||||
):
|
||||
from flask import request
|
||||
|
||||
request.decoded_token = {"sub": "user-1"}
|
||||
with _patch_db(pg_conn):
|
||||
resp = UserModelsCollectionResource().post()
|
||||
|
||||
assert resp.status_code == 400
|
||||
assert capability in resp.get_json()["error"]
|
||||
|
||||
def test_create_rejects_private_ip_dns(self, app, pg_conn):
|
||||
from application.api.user.models.routes import (
|
||||
UserModelsCollectionResource,
|
||||
@@ -529,6 +595,44 @@ class TestPayloadConnectionTest:
|
||||
assert call_args.kwargs["headers"]["Authorization"] == "Bearer sk-mistral-test"
|
||||
assert call_args.kwargs["json"]["model"] == "mistral-large-latest"
|
||||
|
||||
def test_payload_test_uses_responses_protocol(self, app, pg_conn):
|
||||
from application.api.user.models.routes import (
|
||||
UserModelTestPayloadResource,
|
||||
)
|
||||
|
||||
with patch("application.api.user.models.routes.pinned_post") as rp:
|
||||
rp.return_value = MagicMock(
|
||||
status_code=200,
|
||||
headers={"Content-Type": "application/json"},
|
||||
text='{"ok": true}',
|
||||
)
|
||||
with app.test_request_context(
|
||||
"/api/user/models/test",
|
||||
method="POST",
|
||||
json={
|
||||
"base_url": "https://example.openai.azure.com/openai/v1",
|
||||
"api_key": "foundry-key",
|
||||
"upstream_model_id": "gpt-custom",
|
||||
"capabilities": {"api_flavor": "responses"},
|
||||
},
|
||||
):
|
||||
from flask import request
|
||||
|
||||
request.decoded_token = {"sub": "user-1"}
|
||||
with _patch_db(pg_conn):
|
||||
resp = UserModelTestPayloadResource().post()
|
||||
|
||||
assert resp.status_code == 200
|
||||
call_args = rp.call_args
|
||||
assert call_args.args[0].endswith("/responses")
|
||||
assert call_args.kwargs["json"] == {
|
||||
"model": "gpt-custom",
|
||||
"input": "hi",
|
||||
"max_output_tokens": 16,
|
||||
"stream": False,
|
||||
"store": False,
|
||||
}
|
||||
|
||||
def test_payload_test_unauthenticated_returns_401(self, app, pg_conn):
|
||||
from application.api.user.models.routes import (
|
||||
UserModelTestPayloadResource,
|
||||
|
||||
Reference in new issue
Block a user