mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-06 00:14:35 +00:00
Give a member the account they just connected
Member-mode resolution ranked accounts by last_used_at first, so a connection added by Connect to continue (never used) lost to any older used account. Accounts now rank by the later of last use and creation.
This commit is contained in:
1 parent
259da38f17
commit
23e7145cee
2 files changed
+21
-3
No files matched your search
@@ -122,8 +122,9 @@ def _member_connection(repo: ConnectorSessionsRepository, owned: Optional[dict],
|
||||
"""The invoker's own connection to the service the owner's connection is for.
|
||||
|
||||
A member with several connected accounts of that service gets the one
|
||||
they used most recently (then the most recently connected): the account
|
||||
they are working in, and the one a "Connect to continue" just added.
|
||||
most recently used or connected, whichever is later: the account they
|
||||
are working in, or the one a "Connect to continue" just added (never
|
||||
used yet). Ties go to the last used, then the last updated.
|
||||
"""
|
||||
if owned is None:
|
||||
return None
|
||||
@@ -144,7 +145,8 @@ def _member_connection(repo: ConnectorSessionsRepository, owned: Optional[dict],
|
||||
return value if value.tzinfo else value.replace(tzinfo=timezone.utc)
|
||||
|
||||
def recency(row: dict) -> tuple:
|
||||
return tuple(when(row.get(field)) for field in ("last_used_at", "updated_at", "created_at"))
|
||||
used, updated, created = (when(row.get(field)) for field in ("last_used_at", "updated_at", "created_at"))
|
||||
return max(used, created), used, updated, created
|
||||
|
||||
return max(candidates, key=recency)
|
||||
|
||||
|
||||
@@ -104,6 +104,22 @@ class TestResolution:
|
||||
resolved = resolve_connection(_tool(owner, mode="member"), "bob")
|
||||
assert resolved.available and resolved.connection_id == newer
|
||||
|
||||
def test_member_gets_the_account_they_just_connected(self, pg_conn):
|
||||
from docsgpt.connectors.resolve import resolve_connection
|
||||
|
||||
owner = _connection(pg_conn)
|
||||
used = _connection(pg_conn, user="bob")
|
||||
pg_conn.execute(text(
|
||||
"UPDATE connector_sessions SET account_label = 'bob-work', created_at = now() - interval '30 days', "
|
||||
"updated_at = now() - interval '30 days', last_used_at = now() - interval '1 hour' "
|
||||
"WHERE id = CAST(:i AS uuid)"
|
||||
), {"i": used})
|
||||
# Added by "Connect to continue": never used yet.
|
||||
added = _connection(pg_conn, user="bob")
|
||||
with _service_db(pg_conn):
|
||||
resolved = resolve_connection(_tool(owner, mode="member"), "bob")
|
||||
assert resolved.available and resolved.connection_id == added
|
||||
|
||||
def test_member_mode_without_own_connection_is_unavailable(self, pg_conn):
|
||||
from docsgpt.connectors.resolve import resolve_connection
|
||||
|
||||
|
||||
Reference in new issue
Block a user