Skip to content

Commit 11c4d9f

Browse files
committed
Refactor CosmosDB integration for chat history feature
Use the timestamp property instead of _ts
1 parent 44bfc40 commit 11c4d9f

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

app/backend/chat_history/cosmosdb.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import time
23
from typing import Any, Dict, Union, cast
34

45
from azure.cosmos.aio import ContainerProxy, CosmosClient
@@ -36,8 +37,11 @@ async def post_chat_history(auth_claims: Dict[str, Any]):
3637
id = request_json.get("id")
3738
answers = request_json.get("answers")
3839
title = answers[0][0][:50] + "..." if len(answers[0][0]) > 50 else answers[0][0]
40+
timestamp = int(time.time() * 1000)
3941

40-
await container.upsert_item({"id": id, "entra_oid": entra_oid, "title": title, "answers": answers})
42+
await container.upsert_item(
43+
{"id": id, "entra_oid": entra_oid, "title": title, "answers": answers, "timestamp": timestamp}
44+
)
4145

4246
return jsonify({}), 201
4347
except Exception as error:
@@ -64,7 +68,7 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
6468
continuation_token = request_json.get("continuation_token")
6569

6670
res = container.query_items(
67-
query="SELECT c.id, c.entra_oid, c.title, c._ts FROM c WHERE c.entra_oid = @entra_oid ORDER BY c._ts DESC",
71+
query="SELECT c.id, c.entra_oid, c.title, c.timestamp FROM c WHERE c.entra_oid = @entra_oid ORDER BY c.timestamp DESC",
6872
parameters=[dict(name="@entra_oid", value=entra_oid)],
6973
max_item_count=count,
7074
)
@@ -79,7 +83,14 @@ async def get_chat_history(auth_claims: Dict[str, Any]):
7983

8084
items = []
8185
async for item in page:
82-
items.append(item)
86+
items.append(
87+
{
88+
"id": item.get("id"),
89+
"entra_oid": item.get("entra_oid"),
90+
"title": item.get("title", "untitled"),
91+
"timestamp": item.get("timestamp"),
92+
}
93+
)
8394

8495
# If there are no page, StopAsyncIteration is raised
8596
except StopAsyncIteration:
@@ -111,7 +122,18 @@ async def get_chat_history_session(path: str, auth_claims: Dict[str, Any]):
111122

112123
try:
113124
res = await container.read_item(item=path, partition_key=entra_oid)
114-
return jsonify(res), 200
125+
return (
126+
jsonify(
127+
{
128+
"id": res.get("id"),
129+
"entra_oid": res.get("entra_oid"),
130+
"title": res.get("title", "untitled"),
131+
"timestamp": res.get("timestamp"),
132+
"answers": res.get("answers", []),
133+
}
134+
),
135+
200,
136+
)
115137
except Exception as error:
116138
return error_response(error, f"/chat_history/items/{path}")
117139

app/frontend/src/api/models.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ export type HistoryListApiResponse = {
109109
items: {
110110
id: string;
111111
entra_oid: string;
112-
title?: string;
113-
_ts: number;
112+
title: string;
113+
timestamp: number;
114114
}[];
115115
continuation_token?: string;
116116
};
117117

118118
export type HistroyApiResponse = {
119119
id: string;
120120
entra_oid: string;
121-
title?: string;
121+
title: string;
122122
answers: any;
123-
_ts: number;
123+
timestamp: number;
124124
};

app/frontend/src/components/HistoryProviders/CosmosDB.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class CosmosDBProvider implements IHistoryProvider {
2525
}
2626
return response.items.map(item => ({
2727
id: item.id,
28-
title: item.title || "untitled",
29-
timestamp: item._ts * 1000
28+
title: item.title,
29+
timestamp: item.timestamp
3030
}));
3131
} catch (e) {
3232
console.error(e);

0 commit comments

Comments
 (0)