Skip to content

Commit 693ac3f

Browse files
perf: Assistant error msg
1 parent 7e4e09d commit 693ac3f

File tree

4 files changed

+120
-5
lines changed

4 files changed

+120
-5
lines changed

backend/apps/system/api/assistant.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from apps.system.models.system_model import AssistantModel
77
from apps.system.schemas.auth import CacheName, CacheNamespace
88
from apps.system.schemas.system_schema import AssistantBase, AssistantDTO, AssistantValidator
9-
from common.core.deps import SessionDep
9+
from common.core.deps import SessionDep, Trans
1010
from common.core.security import create_access_token
1111
from common.core.sqlbot_cache import clear_cache
1212
from common.utils.time import get_timestamp
@@ -15,7 +15,7 @@
1515
router = APIRouter(tags=["system/assistant"], prefix="/system/assistant")
1616

1717
@router.get("/info/{id}")
18-
async def info(request: Request, response: Response, session: SessionDep, id: int) -> AssistantModel:
18+
async def info(request: Request, response: Response, session: SessionDep, trans: Trans, id: int) -> AssistantModel:
1919
if not id:
2020
raise Exception('miss assistant id')
2121
db_model = await get_assistant_info(session=session, assistant_id=id)
@@ -26,7 +26,7 @@ async def info(request: Request, response: Response, session: SessionDep, id: in
2626
origin = request.headers.get("origin") or request.headers.get("referer")
2727
origin = origin.rstrip('/')
2828
if origin != db_model.domain:
29-
raise RuntimeError("invalid domain [{origin}]")
29+
raise RuntimeError(trans('i18n_embedded.invalid_origin', origin = origin or ''))
3030
return db_model
3131

3232
@router.get("/validator", response_model=AssistantValidator)

backend/locales/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@
3232
"delete_default_error": "Cannot delete default model [{key}]!",
3333
"miss_default": "The default large language model has not been configured"
3434
},
35-
"i18n_ds_invalid": "Datasource Invalid"
35+
"i18n_ds_invalid": "Datasource Invalid",
36+
"i18n_embedded": {
37+
"invalid_origin": "Domain verification failed [{origin}]"
38+
}
3639
}

backend/locales/zh-CN.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@
3333
"delete_default_error": "无法删除默认模型[{key}]!",
3434
"miss_default": "尚未配置默认大语言模型"
3535
},
36-
"i18n_ds_invalid": "数据源连接无效"
36+
"i18n_ds_invalid": "数据源连接无效",
37+
"i18n_embedded": {
38+
"invalid_origin": "域名校验失败【{origin}】"
39+
}
3740
}

frontend/public/assistant.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@
455455
fetch(url)
456456
.then((response) => response.json())
457457
.then((res) => {
458+
if (!res.data) {
459+
throw new Error(res)
460+
}
458461
const data = res.data
459462
const config_json = data.configuration
460463
let tempData = Object.assign(defaultData, { id, domain_url })
@@ -472,6 +475,9 @@
472475
// postMessage the certificate to iframe
473476
}
474477
})
478+
.catch((e) => {
479+
showMsg('嵌入失败', e.message)
480+
})
475481
}
476482
function getDomain(src) {
477483
return src.substring(0, src.indexOf('/assistant.js'))
@@ -493,6 +499,109 @@
493499
expposeGlobalMethods(id)
494500
})
495501
}
502+
503+
function showMsg(title, content) {
504+
// 检查并创建容器(如果不存在)
505+
let container = document.getElementById('messageContainer')
506+
if (!container) {
507+
container = document.createElement('div')
508+
container.id = 'messageContainer'
509+
container.style.position = 'fixed'
510+
container.style.bottom = '20px'
511+
container.style.right = '20px'
512+
container.style.zIndex = '1000'
513+
document.body.appendChild(container)
514+
} else {
515+
// 如果容器已存在,先移除旧弹窗
516+
const oldMessage = container.querySelector('div')
517+
if (oldMessage) {
518+
oldMessage.style.transform = 'translateX(120%)'
519+
oldMessage.style.opacity = '0'
520+
setTimeout(() => {
521+
container.removeChild(oldMessage)
522+
}, 300)
523+
}
524+
}
525+
526+
// 创建弹窗元素
527+
const messageBox = document.createElement('div')
528+
messageBox.style.width = '240px'
529+
messageBox.style.minHeight = '100px'
530+
messageBox.style.background = 'linear-gradient(135deg, #ff6b6b, #ff8e8e)'
531+
messageBox.style.borderRadius = '8px'
532+
messageBox.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.15)'
533+
messageBox.style.padding = '15px'
534+
messageBox.style.color = 'white'
535+
messageBox.style.fontFamily = 'Arial, sans-serif'
536+
messageBox.style.display = 'flex'
537+
messageBox.style.flexDirection = 'column'
538+
messageBox.style.transform = 'translateX(120%)'
539+
messageBox.style.transition = 'transform 0.3s ease-out'
540+
messageBox.style.opacity = '0'
541+
messageBox.style.transition = 'opacity 0.3s ease, transform 0.3s ease'
542+
messageBox.style.overflow = 'hidden'
543+
544+
// 创建标题元素
545+
const titleElement = document.createElement('div')
546+
titleElement.style.fontSize = '18px'
547+
titleElement.style.fontWeight = 'bold'
548+
titleElement.style.marginBottom = '10px'
549+
titleElement.style.borderBottom = '1px solid rgba(255, 255, 255, 0.3)'
550+
titleElement.style.paddingBottom = '8px'
551+
titleElement.textContent = title
552+
553+
// 创建内容元素
554+
const contentElement = document.createElement('div')
555+
contentElement.style.fontSize = '14px'
556+
contentElement.style.flexGrow = '1'
557+
contentElement.style.overflow = 'auto'
558+
contentElement.textContent = content
559+
560+
// 组装元素
561+
messageBox.appendChild(titleElement)
562+
messageBox.appendChild(contentElement)
563+
564+
// 添加到容器
565+
container.appendChild(messageBox)
566+
567+
// 触发显示动画
568+
setTimeout(() => {
569+
messageBox.style.transform = 'translateX(0)'
570+
messageBox.style.opacity = '1'
571+
}, 10)
572+
573+
// 3秒后自动隐藏
574+
setTimeout(() => {
575+
messageBox.style.transform = 'translateX(120%)'
576+
messageBox.style.opacity = '0'
577+
setTimeout(() => {
578+
container.removeChild(messageBox)
579+
// 如果容器是空的,也移除容器
580+
if (container.children.length === 0) {
581+
document.body.removeChild(container)
582+
}
583+
}, 300)
584+
}, 5000)
585+
}
586+
587+
/* function hideMsg() {
588+
const container = document.getElementById('messageContainer');
589+
if (container) {
590+
const messageBox = container.querySelector('div');
591+
if (messageBox) {
592+
messageBox.style.transform = 'translateX(120%)';
593+
messageBox.style.opacity = '0';
594+
setTimeout(() => {
595+
container.removeChild(messageBox);
596+
// 如果容器是空的,也移除容器
597+
if (container.children.length === 0) {
598+
document.body.removeChild(container);
599+
}
600+
}, 300);
601+
}
602+
}
603+
} */
604+
496605
function updateParam(target_url, key, newValue) {
497606
try {
498607
const url = new URL(target_url)

0 commit comments

Comments
 (0)