AQIT
0.1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
sync.py
Go to the documentation of this file.
1
# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2
# This file is part of the Aquin Engine. Unauthorized copying, modification,
3
# distribution, or use of this file, via any medium, is strictly prohibited.
4
# Proprietary and confidential. See LICENSE for terms.
5
6
from
__future__
import
annotations
7
8
from
typing
import
Any
9
10
import
httpx
11
12
from
aquin.engine.session_memory_store
import
memory_from_sync_events
13
14
15
def
_client
() -> httpx.Client:
16
return
httpx.Client(verify=
False
)
17
18
19
def
push_events
(
20
session_id: str,
21
events: list[dict[str, Any]],
22
api_key: str,
23
base_url: str,
24
timeout: float = 15.0,
25
) -> int:
26
"""No-op — web sync event push removed."""
27
_ = session_id, api_key, base_url, timeout
28
return
len(events)
29
30
31
def
fetch_session_state
(
32
session_id: str,
33
api_key: str,
34
base_url: str,
35
timeout: float = 10.0,
36
) -> dict[str, Any]:
37
"""Load session state from cloud; merge memory from snapshot + event replay."""
38
if
not
(session_id
and
api_key):
39
return
{}
40
try
:
41
with
_client
()
as
client:
42
resp = client.get(
43
f
"{base_url.rstrip('/')}/api/sync/sessions/{session_id}"
,
44
headers={
"Authorization"
: f
"Bearer {api_key}"
},
45
timeout=timeout,
46
)
47
if
resp.status_code != 200:
48
return
{}
49
body = resp.json()
50
state = dict(body.get(
"state"
)
or
{})
51
snapshot_mem = dict(state.get(
"memory"
)
or
{})
52
replay_mem = memory_from_sync_events(body.get(
"events"
)
or
[])
53
merged_mem = {**replay_mem, **snapshot_mem}
54
if
merged_mem:
55
state[
"memory"
] = merged_mem
56
return
state
57
except
Exception:
58
pass
59
return
{}
60
61
62
def
load_session_memory
(
63
session_id: str,
64
api_key: str,
65
base_url: str,
66
*,
67
local: dict[str, Any] |
None
=
None
,
68
) -> dict[str, Any]:
69
"""Merged memory: cloud snapshot/events, then local ctx, then disk cache."""
70
from
aquin.engine.session_memory_store
import
load_local_memory
71
72
mem: dict[str, Any] = {}
73
if
session_id
and
api_key
and
base_url:
74
cloud =
fetch_session_state
(session_id, api_key, base_url)
75
mem.update(cloud.get(
"memory"
)
or
{})
76
mem.update(load_local_memory(session_id))
77
if
local:
78
mem.update(local)
79
return
mem
aquin.engine.session_memory_store
Definition
session_memory_store.py:1
aquin.engine.sync.load_session_memory
dict[str, Any] load_session_memory(str session_id, str api_key, str base_url, *, dict[str, Any]|None local=None)
Definition
sync.py:72
aquin.engine.sync.push_events
int push_events(str session_id, list[dict[str, Any]] events, str api_key, str base_url, float timeout=15.0)
Definition
sync.py:29
aquin.engine.sync._client
httpx.Client _client()
Definition
sync.py:19
aquin.engine.sync.fetch_session_state
dict[str, Any] fetch_session_state(str session_id, str api_key, str base_url, float timeout=10.0)
Definition
sync.py:40
aquin
engine
sync.py
AQIT · Aquin Labs Private Limited · Apache 2.0 · Generated by
1.18.0