fix: sqlalchemy query error in DatabaseSessionService when using custom GetSessionConfig with PostgreSQL

Copybara import of the project:
--
cec6f5044307e3ecdd20a513e0d8202b0854ff4c by ZhiNing <574775237@qq.com>:

COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/705 from czn574775237:fix/issue-655 c15f116d4bc0328e8a90a437958042ef1b087c14
PiperOrigin-RevId: 759381068
This commit is contained in:
Zhining 2025-05-15 18:07:18 -07:00 committed by Copybara-Service
parent a786f75968
commit e62e15e720

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import copy import copy
from datetime import datetime from datetime import datetime, timezone
import json import json
import logging import logging
from typing import Any, Optional from typing import Any, Optional
@ -374,17 +374,19 @@ class DatabaseSessionService(BaseSessionService):
) )
if storage_session is None: if storage_session is None:
return None return None
if config and config.after_timestamp:
after_dt = datetime.fromtimestamp(config.after_timestamp, tz=timezone.utc)
timestamp_filter = StorageEvent.timestamp > after_dt
else:
timestamp_filter = True
storage_events = ( storage_events = (
sessionFactory.query(StorageEvent) sessionFactory.query(StorageEvent)
.filter(StorageEvent.session_id == storage_session.id) .filter(StorageEvent.session_id == storage_session.id)
.filter( .filter(timestamp_filter)
StorageEvent.timestamp < config.after_timestamp
if config
else True
)
.limit(config.num_recent_events if config else None)
.order_by(StorageEvent.timestamp.asc()) .order_by(StorageEvent.timestamp.asc())
.limit(config.num_recent_events if config and config.num_recent_events else None)
.all() .all()
) )