Skip to content
Open
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
17 changes: 13 additions & 4 deletions src/google/adk/integrations/firestore/firestore_session_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

import asyncio
import json
from contextlib import asynccontextmanager
from datetime import datetime
from datetime import timezone
Expand Down Expand Up @@ -203,7 +204,7 @@ async def create_session(
"id": session_id,
"appName": app_name,
"userId": user_id,
"state": session_state,
"state": json.dumps(session_state),
"createTime": now,
"updateTime": now,
"revision": 1,
Expand Down Expand Up @@ -314,7 +315,10 @@ async def get_session(
events.append(Event.model_validate(ed))

# Let's continue getting session.
session_state = data.get("state", {})
raw_state = data.get("state", {})
session_state = (
json.loads(raw_state) if isinstance(raw_state, str) else raw_state
)

# Fetch shared state
app_ref = self.client.collection(self.app_state_collection).document(
Expand Down Expand Up @@ -407,7 +411,12 @@ async def list_sessions(
data = doc.to_dict()
if data:
u_id = data["userId"]
s_state = data.get("state", {})
raw_s_state = data.get("state", {})
s_state = (
json.loads(raw_s_state)
if isinstance(raw_s_state, str)
else raw_s_state
)
u_state = user_states_map.get(u_id, {})
merged = self._merge_state(app_state, u_state, s_state)

Expand Down Expand Up @@ -555,7 +564,7 @@ async def _append_txn(transaction: firestore.AsyncTransaction) -> int:
transaction.update(
session_ref,
{
"state": session_only_state,
"state": json.dumps(session_only_state),
"updateTime": firestore.SERVER_TIMESTAMP,
"revision": new_revision,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import json
from unittest import mock

from google.adk.events.event import Event
Expand Down Expand Up @@ -117,7 +118,7 @@ async def test_create_session(mock_firestore_client):
assert args[1]["id"] == session.id
assert args[1]["appName"] == app_name
assert args[1]["userId"] == user_id
assert args[1]["state"] == {}
assert json.loads(args[1]["state"]) == {}
assert args[1]["createTime"] == firestore.SERVER_TIMESTAMP
assert args[1]["updateTime"] == firestore.SERVER_TIMESTAMP

Expand Down Expand Up @@ -324,8 +325,7 @@ async def test_append_event_with_state_delta(mock_firestore_client):

transaction.update.assert_called_once()
args, kwargs = transaction.update.call_args
# In modular Firestore configurations alignments, updating variables mock assertions core setups
assert args[1]["state"] == session.state
assert json.loads(args[1]["state"]) == session.state
assert args[1]["updateTime"] == firestore.SERVER_TIMESTAMP


Expand Down