fix: two-step DynamoDB update for google_accounts; live SM lookup in list_google_accounts
This commit is contained in:
@@ -110,6 +110,28 @@ def connect_google_account(label: str = 'primary') -> str:
|
|||||||
@tool
|
@tool
|
||||||
def list_google_accounts() -> str:
|
def list_google_accounts() -> str:
|
||||||
"""List all connected Google accounts and their labels."""
|
"""List all connected Google accounts and their labels."""
|
||||||
|
actor_id = _current_actor_id
|
||||||
|
if actor_id:
|
||||||
|
try:
|
||||||
|
safe_actor_id = actor_id.replace(':', '-')
|
||||||
|
prefix = f'agent-claw/google-credentials/{safe_actor_id}/'
|
||||||
|
sm = boto3.client('secretsmanager', region_name='us-east-1')
|
||||||
|
paginator = sm.get_paginator('list_secrets')
|
||||||
|
accounts = {}
|
||||||
|
for page in paginator.paginate(Filters=[{'Key': 'name', 'Values': [prefix]}]):
|
||||||
|
for s in page['SecretList']:
|
||||||
|
label = s['Name'][len(prefix):]
|
||||||
|
try:
|
||||||
|
import json as _json
|
||||||
|
val = _json.loads(sm.get_secret_value(SecretId=s['Name'])['SecretString'])
|
||||||
|
accounts[label] = val.get('email', s['Name'])
|
||||||
|
except Exception:
|
||||||
|
accounts[label] = s['Name']
|
||||||
|
if accounts:
|
||||||
|
parts = [f'{label} ({email})' for label, email in accounts.items()]
|
||||||
|
return 'Connected Google accounts: ' + ', '.join(parts)
|
||||||
|
except Exception as e:
|
||||||
|
print(f'[list_google_accounts] SM lookup failed, falling back: {e}')
|
||||||
accounts = _gws._current_google_accounts
|
accounts = _gws._current_google_accounts
|
||||||
if not accounts:
|
if not accounts:
|
||||||
return 'No Google accounts connected. Use connect_google_account to add one.'
|
return 'No Google accounts connected. Use connect_google_account to add one.'
|
||||||
|
|||||||
@@ -205,11 +205,17 @@ def handle_callback(params: dict) -> dict:
|
|||||||
table_name = os.environ.get('USERS_TABLE_NAME', '')
|
table_name = os.environ.get('USERS_TABLE_NAME', '')
|
||||||
if table_name and actor_id:
|
if table_name and actor_id:
|
||||||
try:
|
try:
|
||||||
get_ddb().Table(table_name).update_item(
|
table = get_ddb().Table(table_name)
|
||||||
|
table.update_item(
|
||||||
Key={'actor_id': actor_id},
|
Key={'actor_id': actor_id},
|
||||||
UpdateExpression='SET google_accounts = if_not_exists(google_accounts, :empty), google_accounts.#label = :email',
|
UpdateExpression='SET google_accounts = if_not_exists(google_accounts, :empty)',
|
||||||
|
ExpressionAttributeValues={':empty': {}},
|
||||||
|
)
|
||||||
|
table.update_item(
|
||||||
|
Key={'actor_id': actor_id},
|
||||||
|
UpdateExpression='SET google_accounts.#label = :email',
|
||||||
ExpressionAttributeNames={'#label': label},
|
ExpressionAttributeNames={'#label': label},
|
||||||
ExpressionAttributeValues={':email': user_email, ':empty': {}},
|
ExpressionAttributeValues={':email': user_email},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'[oauth] DynamoDB update failed: {e}')
|
print(f'[oauth] DynamoDB update failed: {e}')
|
||||||
|
|||||||
Reference in New Issue
Block a user