refac
This commit is contained in:
@@ -54,10 +54,7 @@ async def compact_messages_for_request(
|
||||
|
||||
messages, previous_summary = _apply_latest_summary_checkpoint(messages)
|
||||
token_threshold = _resolve_token_threshold(config['token_threshold'], metadata)
|
||||
if (
|
||||
not _exceeds_token_threshold(messages, system_prompt, previous_summary, token_threshold)
|
||||
or len(messages) <= 3
|
||||
):
|
||||
if not _exceeds_token_threshold(messages, system_prompt, previous_summary, token_threshold) or len(messages) <= 3:
|
||||
return messages, previous_summary, False
|
||||
|
||||
boundary = _find_compaction_boundary(messages)
|
||||
|
||||
@@ -355,9 +355,9 @@ async def add_memory_context(request, form_data: dict, user, model: dict | None
|
||||
seen_ids.add(memory_id)
|
||||
|
||||
content = str(doc)
|
||||
if metadata.get('path') and content.startswith(f"{metadata.get('path')}\n"):
|
||||
if metadata.get('path') and content.startswith(f'{metadata.get("path")}\n'):
|
||||
content = content[len(metadata.get('path')) + 1 :]
|
||||
label = f"{metadata.get('path')}: {content}" if metadata.get('path') else content
|
||||
label = f'{metadata.get("path")}: {content}' if metadata.get('path') else content
|
||||
sections[Memories.normalize_memory_type(metadata.get('type'))].append(label)
|
||||
|
||||
parts = []
|
||||
|
||||
@@ -1476,6 +1476,7 @@ async def chat_completion_tools_handler(
|
||||
|
||||
return body, {'sources': sources}
|
||||
|
||||
|
||||
async def chat_web_search_handler(request: Request, form_data: dict, extra_params: dict, user):
|
||||
event_emitter = extra_params['__event_emitter__']
|
||||
await event_emitter(
|
||||
@@ -3636,9 +3637,7 @@ async def non_streaming_chat_response_handler(response, ctx):
|
||||
'end_tag': '</think>',
|
||||
'attributes': {'type': 'reasoning_content'},
|
||||
'content': (
|
||||
[{'type': 'output_text', 'text': reasoning_content}]
|
||||
if reasoning_content
|
||||
else []
|
||||
[{'type': 'output_text', 'text': reasoning_content}] if reasoning_content else []
|
||||
),
|
||||
'summary': None,
|
||||
}
|
||||
@@ -4148,19 +4147,19 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
# error events so frontend and DB paths still receive them.
|
||||
try:
|
||||
raw_obj = json.loads(data)
|
||||
raw_error = raw_obj.get("error") if isinstance(raw_obj, dict) else None
|
||||
raw_error = raw_obj.get('error') if isinstance(raw_obj, dict) else None
|
||||
if raw_error:
|
||||
try:
|
||||
Chats.upsert_message_to_chat_by_id_and_message_id(
|
||||
metadata["chat_id"],
|
||||
metadata["message_id"],
|
||||
metadata['chat_id'],
|
||||
metadata['message_id'],
|
||||
{
|
||||
"error": {"content": raw_error},
|
||||
'error': {'content': raw_error},
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
await event_emitter({ "type": "chat:completion", "data": { "error": raw_error } })
|
||||
await event_emitter({'type': 'chat:completion', 'data': {'error': raw_error}})
|
||||
except Exception:
|
||||
pass
|
||||
continue
|
||||
@@ -4390,9 +4389,9 @@ async def streaming_chat_response_handler(response, ctx):
|
||||
current_response_tool_call['function']['name'] = delta_name
|
||||
|
||||
if delta_arguments:
|
||||
current_response_tool_call['function'][
|
||||
'arguments'
|
||||
] += delta_arguments
|
||||
current_response_tool_call['function']['arguments'] += (
|
||||
delta_arguments
|
||||
)
|
||||
|
||||
# Emit pending tool calls in real-time
|
||||
if response_tool_calls:
|
||||
|
||||
@@ -254,12 +254,7 @@ def convert_output_to_messages(
|
||||
|
||||
def flush_pending():
|
||||
nonlocal pending_content, pending_tool_calls, pending_reasoning, pending_reasoning_details
|
||||
if (
|
||||
not pending_content
|
||||
and not pending_tool_calls
|
||||
and not pending_reasoning
|
||||
and not pending_reasoning_details
|
||||
):
|
||||
if not pending_content and not pending_tool_calls and not pending_reasoning and not pending_reasoning_details:
|
||||
return
|
||||
|
||||
message = {
|
||||
|
||||
@@ -54,16 +54,8 @@ async def fetch_openai_models(request: Request, user: UserModel = None):
|
||||
|
||||
async def get_all_base_models(request: Request, user: UserModel = None):
|
||||
config = await Config.get_many('openai.enable', 'ollama.enable')
|
||||
openai_task = (
|
||||
fetch_openai_models(request, user)
|
||||
if config.get('openai.enable')
|
||||
else asyncio.sleep(0, result=[])
|
||||
)
|
||||
ollama_task = (
|
||||
fetch_ollama_models(request, user)
|
||||
if config.get('ollama.enable')
|
||||
else asyncio.sleep(0, result=[])
|
||||
)
|
||||
openai_task = fetch_openai_models(request, user) if config.get('openai.enable') else asyncio.sleep(0, result=[])
|
||||
ollama_task = fetch_ollama_models(request, user) if config.get('ollama.enable') else asyncio.sleep(0, result=[])
|
||||
function_task = get_function_models(request)
|
||||
|
||||
openai_models, ollama_models, function_models = await asyncio.gather(openai_task, ollama_task, function_task)
|
||||
|
||||
@@ -178,10 +178,7 @@ def _default_value(value):
|
||||
async def get_oauth_runtime_config() -> SimpleNamespace:
|
||||
keys = [key for key, _default in OAUTH_RUNTIME_CONFIG.values()]
|
||||
stored = await Config.get_many(*keys)
|
||||
values = {
|
||||
name: stored.get(key, _default_value(default))
|
||||
for name, (key, default) in OAUTH_RUNTIME_CONFIG.items()
|
||||
}
|
||||
values = {name: stored.get(key, _default_value(default)) for name, (key, default) in OAUTH_RUNTIME_CONFIG.items()}
|
||||
return SimpleNamespace(**values)
|
||||
|
||||
|
||||
@@ -715,10 +712,7 @@ def apply_connection_oauth_options(connection: dict, oauth_client_info: dict) ->
|
||||
def scope_has_resource_indicator(scope: str | None) -> bool:
|
||||
if not scope:
|
||||
return False
|
||||
return any(
|
||||
scope_value.startswith(('https://', 'http://', 'api://'))
|
||||
for scope_value in scope.split()
|
||||
)
|
||||
return any(scope_value.startswith(('https://', 'http://', 'api://')) for scope_value in scope.split())
|
||||
|
||||
|
||||
def should_send_oauth_resource(client_info: OAuthClientInformationFull | None) -> bool:
|
||||
@@ -852,9 +846,7 @@ class OAuthClientManager:
|
||||
|
||||
try:
|
||||
oauth_client_info = resolve_oauth_client_info(connection)
|
||||
oauth_client_info = await recover_static_oauth_client_metadata(
|
||||
connection, oauth_client_info
|
||||
)
|
||||
oauth_client_info = await recover_static_oauth_client_metadata(connection, oauth_client_info)
|
||||
oauth_client_info = apply_connection_oauth_options(connection, oauth_client_info)
|
||||
return self.add_client(expected_client_id, OAuthClientInformationFull(**oauth_client_info))['client']
|
||||
except Exception as e:
|
||||
|
||||
@@ -217,9 +217,7 @@ async def load_tool_module_by_id(tool_id, content=None):
|
||||
# Install required packages found within the frontmatter.
|
||||
# Runs `pip install` via subprocess, which can take a long time;
|
||||
# offload to a thread so it doesn't block the event loop.
|
||||
await asyncio.to_thread(
|
||||
install_frontmatter_requirements, frontmatter.get('requirements', '')
|
||||
)
|
||||
await asyncio.to_thread(install_frontmatter_requirements, frontmatter.get('requirements', ''))
|
||||
|
||||
module_name = f'tool_{tool_id}'
|
||||
module = types.ModuleType(module_name)
|
||||
@@ -264,9 +262,7 @@ async def load_function_module_by_id(function_id: str, content: str | None = Non
|
||||
else:
|
||||
frontmatter = extract_frontmatter(content)
|
||||
# `pip install` via subprocess can block for a long time; offload it.
|
||||
await asyncio.to_thread(
|
||||
install_frontmatter_requirements, frontmatter.get('requirements', '')
|
||||
)
|
||||
await asyncio.to_thread(install_frontmatter_requirements, frontmatter.get('requirements', ''))
|
||||
|
||||
module_name = f'function_{function_id}'
|
||||
module = types.ModuleType(module_name)
|
||||
@@ -468,8 +464,6 @@ async def install_tool_and_function_dependencies():
|
||||
all_dependencies += f'{dependencies}, '
|
||||
|
||||
# `pip install` via subprocess can block for a long time; offload it.
|
||||
await asyncio.to_thread(
|
||||
install_frontmatter_requirements, all_dependencies.strip(', ')
|
||||
)
|
||||
await asyncio.to_thread(install_frontmatter_requirements, all_dependencies.strip(', '))
|
||||
except Exception as e:
|
||||
log.error(f'Error installing requirements: {e}')
|
||||
|
||||
@@ -618,19 +618,11 @@ async def get_builtin_tools(
|
||||
builtin_functions.append(execute_code)
|
||||
|
||||
# Notes tools - search, view, create, and update user's notes
|
||||
if (
|
||||
is_builtin_tool_enabled('notes')
|
||||
and config.get('notes.enable')
|
||||
and await has_user_permission('notes')
|
||||
):
|
||||
if is_builtin_tool_enabled('notes') and config.get('notes.enable') and await has_user_permission('notes'):
|
||||
builtin_functions.extend([search_notes, view_note, write_note, replace_note_content])
|
||||
|
||||
# Channels tools - search channels and messages
|
||||
if (
|
||||
is_builtin_tool_enabled('channels')
|
||||
and config.get('channels.enable')
|
||||
and await has_user_permission('channels')
|
||||
):
|
||||
if is_builtin_tool_enabled('channels') and config.get('channels.enable') and await has_user_permission('channels'):
|
||||
builtin_functions.extend(
|
||||
[
|
||||
search_channels,
|
||||
@@ -659,11 +651,7 @@ async def get_builtin_tools(
|
||||
)
|
||||
|
||||
# Calendar tools - search/create/update/delete events
|
||||
if (
|
||||
is_builtin_tool_enabled('calendar')
|
||||
and config.get('calendar.enable')
|
||||
and await has_user_permission('calendar')
|
||||
):
|
||||
if is_builtin_tool_enabled('calendar') and config.get('calendar.enable') and await has_user_permission('calendar'):
|
||||
builtin_functions.extend(
|
||||
[search_calendar_events, create_calendar_event, update_calendar_event, delete_calendar_event]
|
||||
)
|
||||
|
||||
@@ -27,9 +27,7 @@ def _event_text(message: str, description: str | None = None, event_data: dict |
|
||||
return '\n'.join(lines)
|
||||
|
||||
|
||||
async def post_webhook(
|
||||
name: str, url: str, message: str, event_data: dict, description: str | None = None
|
||||
) -> bool:
|
||||
async def post_webhook(name: str, url: str, message: str, event_data: dict, description: str | None = None) -> bool:
|
||||
try:
|
||||
log.debug(f'post_webhook: {url}, {message}, {event_data}')
|
||||
# Block private-IP / loopback / cloud-metadata targets — the URL is
|
||||
|
||||
Reference in New Issue
Block a user