Skip to content

Commit c6fd15c

Browse files
committed
Refactor session state handling
Changed to generate session state on the server side.
1 parent 063b64c commit c6fd15c

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

app/backend/app.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
CONFIG_VECTOR_SEARCH_ENABLED,
8181
)
8282
from core.authentication import AuthenticationHelper
83+
from core.sessionhelper import create_session_id
8384
from decorators import authenticated, authenticated_path
8485
from error import error_dict, error_response
8586
from prepdocs import (
@@ -219,10 +220,15 @@ async def chat(auth_claims: Dict[str, Any]):
219220
else:
220221
approach = cast(Approach, current_app.config[CONFIG_CHAT_APPROACH])
221222

223+
# If session state is provided, persists the session state,
224+
# else creates a new session_id depending on the chat history options enabled.
225+
session_state = request_json.get("session_state")
226+
if session_state is None:
227+
session_state = create_session_id(current_app.config[CONFIG_CHAT_HISTORY_BROWSER_ENABLED])
222228
result = await approach.run(
223229
request_json["messages"],
224230
context=context,
225-
session_state=request_json.get("session_state"),
231+
session_state=session_state,
226232
)
227233
return jsonify(result)
228234
except Exception as error:
@@ -245,10 +251,15 @@ async def chat_stream(auth_claims: Dict[str, Any]):
245251
else:
246252
approach = cast(Approach, current_app.config[CONFIG_CHAT_APPROACH])
247253

254+
# If session state is provided, persists the session state,
255+
# else creates a new session_id depending on the chat history options enabled.
256+
session_state = request_json.get("session_state")
257+
if session_state is None:
258+
session_state = create_session_id(current_app.config[CONFIG_CHAT_HISTORY_BROWSER_ENABLED])
248259
result = await approach.run_stream(
249260
request_json["messages"],
250261
context=context,
251-
session_state=request_json.get("session_state"),
262+
session_state=session_state,
252263
)
253264
response = await make_response(format_as_ndjson(result))
254265
response.timeout = None # type: ignore

app/backend/core/sessionhelper.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import uuid
2+
3+
4+
def create_session_id(config_chat_history_browser_enabled: bool) -> str | None:
5+
if config_chat_history_browser_enabled:
6+
return str(uuid.uuid4())
7+
return None

app/frontend/src/pages/chat/Chat.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ const Chat = () => {
203203
}
204204
},
205205
// AI Chat Protocol: Client must pass on any session state received from the server
206-
// Set new UUID as session state for the first request
207-
session_state: answers.length ? answers[answers.length - 1][1].session_state : crypto.randomUUID()
206+
session_state: answers.length ? answers[answers.length - 1][1].session_state : null
208207
};
209208

210209
const response = await chatApi(request, shouldStream, token);
@@ -217,14 +216,18 @@ const Chat = () => {
217216
if (shouldStream) {
218217
const parsedResponse: ChatAppResponse = await handleAsyncRequest(question, answers, response.body);
219218
setAnswers([...answers, [question, parsedResponse]]);
220-
historyManager.addItem(parsedResponse.session_state, [...answers, [question, parsedResponse]]);
219+
if (typeof parsedResponse.session_state === "string" && parsedResponse.session_state !== "") {
220+
historyManager.addItem(parsedResponse.session_state, [...answers, [question, parsedResponse]]);
221+
}
221222
} else {
222223
const parsedResponse: ChatAppResponseOrError = await response.json();
223224
if (parsedResponse.error) {
224225
throw Error(parsedResponse.error);
225226
}
226227
setAnswers([...answers, [question, parsedResponse as ChatAppResponse]]);
227-
historyManager.addItem(parsedResponse.session_state, [...answers, [question, parsedResponse as ChatAppResponse]]);
228+
if (typeof parsedResponse.session_state === "string" && parsedResponse.session_state !== "") {
229+
historyManager.addItem(parsedResponse.session_state, [...answers, [question, parsedResponse as ChatAppResponse]]);
230+
}
228231
}
229232
setSpeechUrls([...speechUrls, null]);
230233
} catch (e) {

0 commit comments

Comments
 (0)