fix formatting

This commit is contained in:
Oleg Yermolenko 2025-11-28 19:45:11 +02:00
parent d10a71c298
commit 0422b5e29d
4 changed files with 20 additions and 16 deletions

View file

@ -374,7 +374,9 @@ ENABLE_REALTIME_CHAT_SAVE = (
ENABLE_QUERIES_CACHE = os.environ.get("ENABLE_QUERIES_CACHE", "False").lower() == "true"
ENABLE_WRAP_TOOL_RESULT = os.environ.get("ENABLE_WRAP_TOOL_RESULT", "True").lower() == "true"
ENABLE_WRAP_TOOL_RESULT = (
os.environ.get("ENABLE_WRAP_TOOL_RESULT", "True").lower() == "true"
)
TOOL_RESULT_INDENT_SIZE = os.environ.get("TOOL_RESULT_INDENT_SIZE", 2)

View file

@ -286,10 +286,14 @@ def process_tool_result(
return tool_result, tool_result_files, tool_result_embeds
def dump_tool_result_to_json(model, ensure_ascii=True):
indent_size = None if TOOL_RESULT_INDENT_SIZE == 0 else TOOL_RESULT_INDENT_SIZE
separators = None if indent_size and indent_size > 0 else (',', ':')
return json.dumps(model, indent=indent_size, separators=separators, ensure_ascii=ensure_ascii)
separators = None if indent_size and indent_size > 0 else (",", ":")
return json.dumps(
model, indent=indent_size, separators=separators, ensure_ascii=ensure_ascii
)
async def chat_completion_tools_handler(
request: Request, body: dict, extra_params: dict, user: UserModel, models, tools

View file

@ -1877,6 +1877,7 @@
for (const message of _messages) {
let content = message?.merged?.content ?? message?.content;
let processedMessages = processDetailsAndExtractToolCalls(content ?? '');
let nonToolMesssage = null;
let toolCallIndex = 0;

View file

@ -862,14 +862,14 @@ const detailsAttributesRegex = /(\w+)="([^"]*)"/g;
export const processDetailsAndExtractToolCalls = (content) => {
content = removeDetails(content, ['reasoning', 'code_interpreter']);
// Split text and tool calls into messages array
let messages = [];
const matches = content.match(toolCallsDetailsRegex);
if (matches && matches.length > 0) {
let previousDetailsEndIndex = 0;
for (const match of matches) {
let detailsStartIndex = content.indexOf(match, previousDetailsEndIndex);
let assistantMessage = content.substr(previousDetailsEndIndex, detailsStartIndex - previousDetailsEndIndex);
previousDetailsEndIndex = detailsStartIndex + match.length;
@ -884,46 +884,43 @@ export const processDetailsAndExtractToolCalls = (content) => {
while ((attributeMatch = detailsAttributesRegex.exec(match)) !== null) {
attributes[attributeMatch[1]] = attributeMatch[2];
}
if (!attributes.id) {
continue;
}
let toolCall = {
id: attributes.id,
name: attributes.name,
arguments: unescapeHtml(attributes.arguments ?? ''),
result: unescapeHtml(attributes.result ?? '')
}
toolCall.arguments = parseDoubleEncodedString(toolCall.arguments);
toolCall.result = parseDoubleEncodedString(toolCall.result);
messages.push(toolCall);
}
let finalAssistantMessage = content.substr(previousDetailsEndIndex);
finalAssistantMessage = finalAssistantMessage.trim('\n');
if (finalAssistantMessage.length > 0) {
messages.push(finalAssistantMessage);
}
}
else if (content.length > 0) {
} else if (content.length > 0) {
messages.push(content);
}
return messages;
};
function parseDoubleEncodedString(value) {
try
{
try {
let parsedValue = JSON.parse(value);
if (typeof parsedValue == "string") {
return parsedValue;
}
}
catch {}
} catch {}
return value;
}