Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/endpoints/conversations_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,6 @@ def transform_chat_message(entry: CacheEntry) -> dict[str, Any]:
{"content": entry.query, "type": "user"},
{"content": entry.response, "type": "assistant"},
],
"started_at": entry.started_at,
"completed_at": entry.completed_at,
}
4 changes: 4 additions & 0 deletions src/app/endpoints/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ async def query_endpoint_handler( # pylint: disable=R0914

user_id, _, _skip_userid_check, token = auth

started_at = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
user_conversation: UserConversation | None = None
if query_request.conversation_id:
logger.debug(
Expand Down Expand Up @@ -330,6 +331,7 @@ async def query_endpoint_handler( # pylint: disable=R0914
topic_summary=topic_summary,
)

completed_at = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
store_conversation_into_cache(
configuration,
user_id,
Expand All @@ -338,6 +340,8 @@ async def query_endpoint_handler( # pylint: disable=R0914
model_id,
query_request.query,
summary.llm_response,
started_at,
completed_at,
_skip_userid_check,
topic_summary,
)
Expand Down
5 changes: 5 additions & 0 deletions src/app/endpoints/streaming_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import logging
import re
from datetime import UTC, datetime
from typing import Annotated, Any, AsyncIterator, Iterator, cast

from fastapi import APIRouter, Depends, HTTPException, Request, status
Expand Down Expand Up @@ -596,6 +597,7 @@ async def streaming_query_endpoint_handler( # pylint: disable=R0915,R0914
_ = request

check_configuration_loaded(configuration)
started_at = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")

# Enforce RBAC: optionally disallow overriding model/provider in requests
validate_model_provider_override(query_request, request.state.authorized_actions)
Expand Down Expand Up @@ -719,6 +721,7 @@ async def response_generator(
query_request.query, client, model_id
)

completed_at = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%SZ")
store_conversation_into_cache(
configuration,
user_id,
Expand All @@ -727,6 +730,8 @@ async def response_generator(
model_id,
query_request.query,
summary.llm_response,
started_at,
completed_at,
_skip_userid_check,
topic_summary,
)
Expand Down
15 changes: 12 additions & 3 deletions src/cache/postgres_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class PostgresCache(Cache):
user_id | text | not null |
conversation_id | text | not null |
created_at | timestamp without time zone | not null |
started_at | text | |
completed_at | text | |
query | text | |
response | text | |
provider | text | |
Expand All @@ -38,6 +40,8 @@ class PostgresCache(Cache):
user_id text NOT NULL,
conversation_id text NOT NULL,
created_at timestamp NOT NULL,
started_at text,
completed_at text,
query text,
response text,
provider text,
Expand All @@ -62,15 +66,16 @@ class PostgresCache(Cache):
"""

SELECT_CONVERSATION_HISTORY_STATEMENT = """
SELECT query, response, provider, model
SELECT query, response, provider, model, started_at, completed_at
FROM cache
WHERE user_id=%s AND conversation_id=%s
ORDER BY created_at
"""

INSERT_CONVERSATION_HISTORY_STATEMENT = """
INSERT INTO cache(user_id, conversation_id, created_at, query, response, provider, model)
VALUES (%s, %s, CURRENT_TIMESTAMP, %s, %s, %s, %s)
INSERT INTO cache(user_id, conversation_id, created_at, started_at, completed_at,
query, response, provider, model)
VALUES (%s, %s, CURRENT_TIMESTAMP, %s, %s, %s, %s, %s, %s)
"""

QUERY_CACHE_SIZE = """
Expand Down Expand Up @@ -211,6 +216,8 @@ def get(
response=conversation_entry[1],
provider=conversation_entry[2],
model=conversation_entry[3],
started_at=conversation_entry[4],
completed_at=conversation_entry[5],
)
result.append(cache_entry)

Expand Down Expand Up @@ -245,6 +252,8 @@ def insert_or_append(
(
user_id,
conversation_id,
cache_entry.started_at,
cache_entry.completed_at,
cache_entry.query,
cache_entry.response,
cache_entry.provider,
Expand Down
15 changes: 12 additions & 3 deletions src/cache/sqlite_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class SQLiteCache(Cache):
user_id | text | not null |
conversation_id | text | not null |
created_at | int | not null |
started_at | text | |
completed_at | text | |
query | text | |
response | text | |
provider | text | |
Expand All @@ -42,6 +44,8 @@ class SQLiteCache(Cache):
user_id text NOT NULL,
conversation_id text NOT NULL,
created_at int NOT NULL,
started_at text,
completed_at text,
query text,
response text,
provider text,
Expand All @@ -66,15 +70,16 @@ class SQLiteCache(Cache):
"""

SELECT_CONVERSATION_HISTORY_STATEMENT = """
SELECT query, response, provider, model
SELECT query, response, provider, model, started_at, completed_at
FROM cache
WHERE user_id=? AND conversation_id=?
ORDER BY created_at
"""

INSERT_CONVERSATION_HISTORY_STATEMENT = """
INSERT INTO cache(user_id, conversation_id, created_at, query, response, provider, model)
VALUES (?, ?, ?, ?, ?, ?, ?)
INSERT INTO cache(user_id, conversation_id, created_at, started_at, completed_at,
query, response, provider, model)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
"""

QUERY_CACHE_SIZE = """
Expand Down Expand Up @@ -209,6 +214,8 @@ def get(
response=conversation_entry[1],
provider=conversation_entry[2],
model=conversation_entry[3],
started_at=conversation_entry[4],
completed_at=conversation_entry[5],
)
result.append(cache_entry)

Expand Down Expand Up @@ -243,6 +250,8 @@ def insert_or_append(
user_id,
conversation_id,
current_time,
cache_entry.started_at,
cache_entry.completed_at,
cache_entry.query,
cache_entry.response,
cache_entry.provider,
Expand Down
2 changes: 2 additions & 0 deletions src/models/cache_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class CacheEntry(BaseModel):
response: str
provider: str
model: str
started_at: str
completed_at: str


class ConversationData(BaseModel):
Expand Down
4 changes: 4 additions & 0 deletions src/utils/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ def store_conversation_into_cache(
model_id: str,
query: str,
response: str,
started_at: str,
completed_at: str,
_skip_userid_check: bool,
topic_summary: str | None,
) -> None:
Expand All @@ -203,6 +205,8 @@ def store_conversation_into_cache(
response=response,
provider=provider_id,
model=model_id,
started_at=started_at,
completed_at=completed_at,
)
cache.insert_or_append(
user_id, conversation_id, cache_entry, _skip_userid_check
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/app/endpoints/test_conversations_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
def test_transform_message() -> None:
"""Test the transform_chat_message transformation function."""
entry = CacheEntry(
query="query", response="response", provider="provider", model="model"
query="query",
response="response",
provider="provider",
model="model",
started_at="2024-01-01T00:00:00Z",
completed_at="2024-01-01T00:00:05Z",
)
transformed = transform_chat_message(entry)
assert transformed is not None
Expand All @@ -21,6 +26,12 @@ def test_transform_message() -> None:
assert "model" in transformed
assert transformed["model"] == "model"

assert "started_at" in transformed
assert transformed["started_at"] == "2024-01-01T00:00:00Z"

assert "completed_at" in transformed
assert transformed["completed_at"] == "2024-01-01T00:00:05Z"

assert "messages" in transformed
assert len(transformed["messages"]) == 2

Expand Down
14 changes: 12 additions & 2 deletions tests/unit/cache/test_noop_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
CONVERSATION_ID = suid.get_suid()
USER_PROVIDED_USER_ID = "test-user1"
cache_entry_1 = CacheEntry(
query="user message1", response="AI message1", provider="foo", model="bar"
query="user message1",
response="AI message1",
provider="foo",
model="bar",
started_at="2025-10-03T09:31:25Z",
completed_at="2025-10-03T09:31:29Z",
)
cache_entry_2 = CacheEntry(
query="user message2", response="AI message2", provider="foo", model="bar"
query="user message2",
response="AI message2",
provider="foo",
model="bar",
started_at="2025-10-03T09:31:25Z",
completed_at="2025-10-03T09:31:29Z",
)


Expand Down
14 changes: 12 additions & 2 deletions tests/unit/cache/test_postgres_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@
CONVERSATION_ID_1 = suid.get_suid()
CONVERSATION_ID_2 = suid.get_suid()
cache_entry_1 = CacheEntry(
query="user message1", response="AI message1", provider="foo", model="bar"
query="user message1",
response="AI message1",
provider="foo",
model="bar",
started_at="2025-10-03T09:31:25Z",
completed_at="2025-10-03T09:31:29Z",
)
cache_entry_2 = CacheEntry(
query="user message2", response="AI message2", provider="foo", model="bar"
query="user message2",
response="AI message2",
provider="foo",
model="bar",
started_at="2025-10-03T09:31:25Z",
completed_at="2025-10-03T09:31:29Z",
)

# pylint: disable=fixme
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/cache/test_sqlite_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@
CONVERSATION_ID_1 = suid.get_suid()
CONVERSATION_ID_2 = suid.get_suid()
cache_entry_1 = CacheEntry(
query="user message1", response="AI message1", provider="foo", model="bar"
query="user message1",
response="AI message1",
provider="foo",
model="bar",
started_at="2025-10-03T09:31:25Z",
completed_at="2025-10-03T09:31:29Z",
)
cache_entry_2 = CacheEntry(
query="user message2", response="AI message2", provider="foo", model="bar"
query="user message2",
response="AI message2",
provider="foo",
model="bar",
started_at="2025-10-03T09:31:25Z",
completed_at="2025-10-03T09:31:29Z",
)


Expand Down
Loading