mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-12 04:15:25 +00:00
refac
This commit is contained in:
parent
08f8713ee1
commit
53de48d2b3
2 changed files with 54 additions and 31 deletions
|
|
@ -344,7 +344,7 @@ async def model_response_handler(request, channel, message, user):
|
||||||
"role": "system",
|
"role": "system",
|
||||||
"content": f"You are {model.get('name', model_id)}, participating in a threaded conversation. Be concise and conversational."
|
"content": f"You are {model.get('name', model_id)}, participating in a threaded conversation. Be concise and conversational."
|
||||||
+ (
|
+ (
|
||||||
f"Here's the thread history:\n\n{''.join([f'{msg}' for msg in thread_history])}\n\nContinue the conversation naturally as {model.get('name', model_id)}, addressing the most recent message while being aware of the full context."
|
f"Here's the thread history:\n\n\n{''.join([f'{msg}\n\n' for msg in thread_history])}\n\n\nContinue the conversation naturally as {model.get('name', model_id)}, addressing the most recent message while being aware of the full context."
|
||||||
if thread_history
|
if thread_history
|
||||||
else ""
|
else ""
|
||||||
),
|
),
|
||||||
|
|
@ -384,19 +384,34 @@ async def model_response_handler(request, channel, message, user):
|
||||||
)
|
)
|
||||||
|
|
||||||
if res:
|
if res:
|
||||||
await update_message_by_id(
|
if res.get("choices", []) and len(res["choices"]) > 0:
|
||||||
channel.id,
|
await update_message_by_id(
|
||||||
response_message.id,
|
channel.id,
|
||||||
MessageForm(
|
response_message.id,
|
||||||
**{
|
MessageForm(
|
||||||
"content": res["choices"][0]["message"]["content"],
|
**{
|
||||||
"meta": {
|
"content": res["choices"][0]["message"]["content"],
|
||||||
"done": True,
|
"meta": {
|
||||||
},
|
"done": True,
|
||||||
}
|
},
|
||||||
),
|
}
|
||||||
user,
|
),
|
||||||
)
|
user,
|
||||||
|
)
|
||||||
|
elif res.get("error", None):
|
||||||
|
await update_message_by_id(
|
||||||
|
channel.id,
|
||||||
|
response_message.id,
|
||||||
|
MessageForm(
|
||||||
|
**{
|
||||||
|
"content": f"Error: {res['error']}",
|
||||||
|
"meta": {
|
||||||
|
"done": True,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
),
|
||||||
|
user,
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.info(e)
|
log.info(e)
|
||||||
pass
|
pass
|
||||||
|
|
|
||||||
|
|
@ -775,28 +775,36 @@
|
||||||
|
|
||||||
htmlValue = editor.getHTML();
|
htmlValue = editor.getHTML();
|
||||||
jsonValue = editor.getJSON();
|
jsonValue = editor.getJSON();
|
||||||
mdValue = turndownService
|
|
||||||
.turndown(
|
|
||||||
htmlValue
|
|
||||||
.replace(/<p><\/p>/g, '<br/>')
|
|
||||||
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
|
|
||||||
)
|
|
||||||
.replace(/\u00a0/g, ' ');
|
|
||||||
|
|
||||||
if (richText) {
|
if (richText) {
|
||||||
onChange({
|
mdValue = turndownService
|
||||||
html: htmlValue,
|
.turndown(
|
||||||
json: jsonValue,
|
htmlValue
|
||||||
md: mdValue
|
.replace(/<p><\/p>/g, '<br/>')
|
||||||
});
|
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
|
||||||
|
)
|
||||||
|
.replace(/\u00a0/g, ' ');
|
||||||
} else {
|
} else {
|
||||||
// Plain text path: preserve \t and \n exactly
|
mdValue = turndownService
|
||||||
const doc = editor.view.state.doc;
|
.turndown(
|
||||||
const plain = doc.textBetween(0, doc.content.size, '\n\n', '\n'); // keeps \t intact
|
htmlValue
|
||||||
value = plain;
|
// Replace empty paragraphs with line breaks
|
||||||
onChange({ html: null, json: null, md: plain });
|
.replace(/<p><\/p>/g, '<br/>')
|
||||||
|
// Replace multiple spaces with non-breaking spaces
|
||||||
|
.replace(/ {2,}/g, (m) => m.replace(/ /g, '\u00a0'))
|
||||||
|
// Replace tabs with non-breaking spaces (preserve indentation)
|
||||||
|
.replace(/\t/g, '\u00a0\u00a0\u00a0\u00a0') // 1 tab = 4 spaces
|
||||||
|
)
|
||||||
|
// Convert non-breaking spaces back to regular spaces for markdown
|
||||||
|
.replace(/\u00a0/g, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onChange({
|
||||||
|
html: htmlValue,
|
||||||
|
json: jsonValue,
|
||||||
|
md: mdValue
|
||||||
|
});
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
value = jsonValue;
|
value = jsonValue;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue