chore: format

This commit is contained in:
Timothy Jaeryang Baek
2026-04-14 17:27:31 -05:00
parent f1be85d997
commit 5dae600ce7
81 changed files with 247 additions and 140 deletions
+15 -49
View File
@@ -205,9 +205,7 @@ def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
elif content_type == 'image':
source = content_block.get('source', {})
if source.get('type') == 'base64':
media_type = source.get(
'media_type', 'image/png'
)
media_type = source.get('media_type', 'image/png')
data = source.get('data', '')
converted_parts.append(
{
@@ -229,68 +227,36 @@ def convert_anthropic_to_openai_payload(anthropic_payload: dict) -> dict:
elif content_type == 'document':
# Documents have no direct OpenAI equivalent;
# convert to a text representation.
document_source = content_block.get(
'source', {}
)
document_title = content_block.get(
'title', 'Document'
)
document_context = content_block.get(
'context', ''
)
document_text = (
f'[Document: {document_title}]'
)
document_source = content_block.get('source', {})
document_title = content_block.get('title', 'Document')
document_context = content_block.get('context', '')
document_text = f'[Document: {document_title}]'
if document_context:
document_text += f'\n{document_context}'
if (
document_source.get('type') == 'text'
and document_source.get('data')
):
document_text += (
f'\n{document_source["data"]}'
)
converted_parts.append(
{'type': 'text', 'text': document_text}
)
if document_source.get('type') == 'text' and document_source.get('data'):
document_text += f'\n{document_source["data"]}'
converted_parts.append({'type': 'text', 'text': document_text})
elif content_type == 'search_result':
# Convert search results to a text
# representation with source attribution.
search_title = content_block.get('title', '')
search_url = content_block.get('source', '')
search_content_blocks = content_block.get(
'content', []
)
search_content_blocks = content_block.get('content', [])
search_texts = []
for search_block in search_content_blocks:
if (
isinstance(search_block, dict)
and search_block.get('type') == 'text'
):
search_texts.append(
search_block.get('text', '')
)
if isinstance(search_block, dict) and search_block.get('type') == 'text':
search_texts.append(search_block.get('text', ''))
search_body = '\n'.join(search_texts)
search_text = (
f'[Search Result: {search_title}]'
)
search_text = f'[Search Result: {search_title}]'
if search_url:
search_text += f'\nSource: {search_url}'
if search_body:
search_text += f'\n{search_body}'
converted_parts.append(
{'type': 'text', 'text': search_text}
)
converted_parts.append({'type': 'text', 'text': search_text})
# Flatten to string when only text parts are present
if all(
part.get('type') == 'text'
for part in converted_parts
):
tool_content = '\n'.join(
part.get('text', '')
for part in converted_parts
)
if all(part.get('type') == 'text' for part in converted_parts):
tool_content = '\n'.join(part.get('text', '') for part in converted_parts)
elif converted_parts:
tool_content = converted_parts
else:
+2 -7
View File
@@ -105,10 +105,7 @@ class CommitSessionMiddleware:
try:
ScopedSession.commit()
except Exception:
log.exception(
'CommitSessionMiddleware: post-request commit failed; '
'response was already sent to client'
)
log.exception('CommitSessionMiddleware: post-request commit failed; response was already sent to client')
try:
ScopedSession.rollback()
except Exception:
@@ -190,9 +187,7 @@ class WebsocketUpgradeGuardMiddleware:
if query_params.get('transport', [''])[0] == 'websocket':
headers = _scope_headers(scope)
upgrade = headers.get('upgrade', '').lower()
connection_tokens = [
token.strip() for token in headers.get('connection', '').lower().split(',')
]
connection_tokens = [token.strip() for token in headers.get('connection', '').lower().split(',')]
if upgrade != 'websocket' or 'upgrade' not in connection_tokens:
response = JSONResponse(
status_code=400,
+8 -6
View File
@@ -3101,8 +3101,8 @@ async def outlet_filter_handler(ctx):
'content': m.get('content', ''),
'info': m.get('info'),
'timestamp': m.get('timestamp'),
**(({'usage': m['usage']} if m.get('usage') else {})),
**(({'sources': m['sources']} if m.get('sources') else {})),
**({'usage': m['usage']} if m.get('usage') else {}),
**({'sources': m['sources']} if m.get('sources') else {}),
}
for m in message_list
],
@@ -3157,10 +3157,12 @@ async def outlet_filter_handler(ctx):
)
if event_emitter:
await event_emitter({
'type': 'chat:outlet',
'data': {'messages': outlet_result['messages']},
})
await event_emitter(
{
'type': 'chat:outlet',
'data': {'messages': outlet_result['messages']},
}
)
except Exception as e:
log.debug(f'Error running outlet filters: {e}')
+11 -4
View File
@@ -169,7 +169,7 @@ def _normalize_token_expiry(token: dict) -> dict:
# Neither field present — conservative fallback
log.warning(
"OAuth token response missing both 'expires_in' and 'expires_at'; "
f"defaulting to {DEFAULT_TOKEN_EXPIRY_SECONDS}s from now"
f'defaulting to {DEFAULT_TOKEN_EXPIRY_SECONDS}s from now'
)
token['expires_at'] = int(datetime.now().timestamp() + DEFAULT_TOKEN_EXPIRY_SECONDS)
return token
@@ -548,7 +548,6 @@ async def get_oauth_client_info_with_static_credentials(
raise e
def resolve_oauth_client_info(connection: dict) -> dict:
"""
Decrypt OAuth client info from a tool server connection config.
@@ -766,7 +765,11 @@ class OAuthClientManager:
log.warning(f'No OAuth session found for user {user_id}, client_id {client_id}')
return None
if force_refresh or session.expires_at is None or datetime.now() + timedelta(minutes=5) >= datetime.fromtimestamp(session.expires_at):
if (
force_refresh
or session.expires_at is None
or datetime.now() + timedelta(minutes=5) >= datetime.fromtimestamp(session.expires_at)
):
log.debug(f'Token refresh needed for user {user_id}, client_id {session.provider}')
refreshed_token = await self._refresh_token(session)
if refreshed_token:
@@ -1017,7 +1020,11 @@ class OAuthManager:
log.warning(f'No OAuth session found for user {user_id}, session {session_id}')
return None
if force_refresh or session.expires_at is None or datetime.now() + timedelta(minutes=5) >= datetime.fromtimestamp(session.expires_at):
if (
force_refresh
or session.expires_at is None
or datetime.now() + timedelta(minutes=5) >= datetime.fromtimestamp(session.expires_at)
):
log.debug(f'Token refresh needed for user {user_id}, provider {session.provider}')
refreshed_token = await self._refresh_token(session)
if refreshed_token: