Skip to content

Commit 6a00420

Browse files
committed
enh: inline html result context
1 parent ee9099c commit 6a00420

1 file changed

Lines changed: 43 additions & 16 deletions

File tree

backend/open_webui/utils/middleware.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -938,18 +938,28 @@ def process_tool_result(
938938
tool_result_embeds = []
939939
EXTERNAL_TOOL_TYPES = ('external', 'action', 'terminal')
940940

941+
# Support (HTMLResponse, result_context) tuples: the optional second
942+
# element lets tool authors provide the LLM with actionable context
943+
# about the generated embed instead of the generic fallback message.
944+
result_context = None
945+
if isinstance(tool_result, tuple) and len(tool_result) == 2 and isinstance(tool_result[0], HTMLResponse):
946+
tool_result, result_context = tool_result
947+
941948
if isinstance(tool_result, HTMLResponse):
942949
content_disposition = tool_result.headers.get('Content-Disposition', '')
943950
if 'inline' in content_disposition:
944951
content = tool_result.body.decode('utf-8', 'replace')
945952
tool_result_embeds.append(content)
946953

947954
if 200 <= tool_result.status_code < 300:
948-
tool_result = {
949-
'status': 'success',
950-
'code': 'ui_component',
951-
'message': f'{tool_function_name}: Embedded UI result is active and visible to the user.',
952-
}
955+
if result_context is not None and isinstance(result_context, (str, dict, list)):
956+
tool_result = result_context
957+
else:
958+
tool_result = {
959+
'status': 'success',
960+
'code': 'ui_component',
961+
'message': f'{tool_function_name}: Embedded UI result is active and visible to the user.',
962+
}
953963
elif 400 <= tool_result.status_code < 500:
954964
tool_result = {
955965
'status': 'error',
@@ -1000,20 +1010,37 @@ def process_tool_result(
10001010
)
10011011

10021012
if 'text/html' in content_type:
1013+
# Support (html_content, result_context) nested tuple
1014+
result_context = None
1015+
html_content = tool_result
1016+
if isinstance(tool_result, (tuple, list)) and len(tool_result) == 2:
1017+
html_content, result_context = tool_result
1018+
10031019
# Display as iframe embed
1004-
tool_result_embeds.append(tool_result)
1005-
tool_result = {
1006-
'status': 'success',
1007-
'code': 'ui_component',
1008-
'message': f'{tool_function_name}: Embedded UI result is active and visible to the user.',
1009-
}
1020+
tool_result_embeds.append(html_content)
1021+
if result_context is not None and isinstance(result_context, (str, dict, list)):
1022+
tool_result = result_context
1023+
else:
1024+
tool_result = {
1025+
'status': 'success',
1026+
'code': 'ui_component',
1027+
'message': f'{tool_function_name}: Embedded UI result is active and visible to the user.',
1028+
}
10101029
elif location:
1030+
# Support (html_content, result_context) nested tuple for location embeds
1031+
result_context = None
1032+
if isinstance(tool_result, (tuple, list)) and len(tool_result) == 2:
1033+
_, result_context = tool_result
1034+
10111035
tool_result_embeds.append(location)
1012-
tool_result = {
1013-
'status': 'success',
1014-
'code': 'ui_component',
1015-
'message': f'{tool_function_name}: Embedded UI result is active and visible to the user.',
1016-
}
1036+
if result_context is not None and isinstance(result_context, (str, dict, list)):
1037+
tool_result = result_context
1038+
else:
1039+
tool_result = {
1040+
'status': 'success',
1041+
'code': 'ui_component',
1042+
'message': f'{tool_function_name}: Embedded UI result is active and visible to the user.',
1043+
}
10171044

10181045
tool_result_files = []
10191046

0 commit comments

Comments
 (0)