perf: 优化翻译消息

This commit is contained in:
kuaifan 2025-02-07 04:23:34 +09:00
parent b467dc55e5
commit 34cfd1e344
6 changed files with 61 additions and 6 deletions

View File

@ -1792,6 +1792,8 @@ class DialogController extends AbstractController
* @apiName msg__translation * @apiName msg__translation
* *
* @apiParam {Number} msg_id 消息ID * @apiParam {Number} msg_id 消息ID
* @apiParam {Number} [force] 强制翻译1是、0否
* - 默认不强制翻译,已翻译过的消息不再翻译
* @apiParam {String} [language] 目标语言,默认当前语言 * @apiParam {String} [language] 目标语言,默认当前语言
* *
* @apiSuccess {Number} ret 返回状态码1正确、0错误 * @apiSuccess {Number} ret 返回状态码1正确、0错误
@ -1803,6 +1805,7 @@ class DialogController extends AbstractController
User::auth(); User::auth();
// //
$msg_id = intval(Request::input("msg_id")); $msg_id = intval(Request::input("msg_id"));
$force = intval(Request::input("force"));
$language = Base::inputOrHeader('language'); $language = Base::inputOrHeader('language');
$targetLanguage = Doo::getLanguages($language); $targetLanguage = Doo::getLanguages($language);
// //
@ -1820,13 +1823,20 @@ class DialogController extends AbstractController
// //
$row = WebSocketDialogMsgTranslate::whereMsgId($msg_id)->whereLanguage($language)->first(); $row = WebSocketDialogMsgTranslate::whereMsgId($msg_id)->whereLanguage($language)->first();
if ($row) { if ($row) {
return Base::retSuccess("success", $row->only(['msg_id', 'language', 'content'])); if ($force) {
$row->delete();
} else {
return Base::retSuccess("success", $row->only(['msg_id', 'language', 'content']));
}
} }
// //
$msgData = Base::json2array($msg->getRawOriginal('msg')); $msgData = Base::json2array($msg->getRawOriginal('msg'));
if (empty($msgData['text'])) { if (empty($msgData['text'])) {
return Base::retError("消息内容为空"); return Base::retError("消息内容为空");
} }
if ($msg->type === 'text' && $msgData['type'] === 'md') {
$msgData['text'] = preg_replace('/:::\s*reasoning.*?:::/s', '', $msgData['text']);
}
$res = Extranet::openAItranslations($msgData['text'], $targetLanguage); $res = Extranet::openAItranslations($msgData['text'], $targetLanguage);
if (Base::isError($res)) { if (Base::isError($res)) {
return $res; return $res;

View File

@ -1924,3 +1924,4 @@ API请求的基础URL路径如果没有请留空
欢迎词 欢迎词
仪表盘欢迎词,(*)代表用户昵称 仪表盘欢迎词,(*)代表用户昵称
思考过程

View File

@ -10,7 +10,7 @@
<div class="divider-label translation-label" @click="viewText">{{ translation.label }}</div> <div class="divider-label translation-label" @click="viewText">{{ translation.label }}</div>
<span></span> <span></span>
</div> </div>
<DialogMarkdown v-if="msg.type === 'md'" :text="translation.content"/> <DialogMarkdown v-if="msg.type === 'md'" :text="translation.content" class="content-translation"/>
<pre v-else v-html="$A.formatTextMsg(translation.content, userId)"></pre> <pre v-else v-html="$A.formatTextMsg(translation.content, userId)"></pre>
</template> </template>
</div> </div>

View File

@ -3197,7 +3197,7 @@ export default {
}); });
}, },
onTranslation() { onTranslation(language = undefined) {
if (!this.actionPermission(this.operateItem, 'translation')) { if (!this.actionPermission(this.operateItem, 'translation')) {
return; return;
} }
@ -3206,12 +3206,22 @@ export default {
if (this.isLoad(key)) { if (this.isLoad(key)) {
return; return;
} }
let force = 0;
if (language === 'hidden') {
this.$store.dispatch("removeTranslation", key);
return;
} else if (language === 'retranslation') {
this.$store.dispatch("removeTranslation", key);
language = undefined;
force = 1;
}
this.$store.dispatch("setLoad", key) this.$store.dispatch("setLoad", key)
this.$store.dispatch("call", { this.$store.dispatch("call", {
url: 'dialog/msg/translation', url: 'dialog/msg/translation',
data: { data: {
msg_id, msg_id,
language: this.cacheTranslationLanguage force,
language: language || this.cacheTranslationLanguage
}, },
}).then(({data}) => { }).then(({data}) => {
this.$store.dispatch("saveTranslation", Object.assign(data, {key})); this.$store.dispatch("saveTranslation", Object.assign(data, {key}));
@ -3356,14 +3366,20 @@ export default {
label: languageList[item], label: languageList[item],
value: item value: item
})) }))
list.push(...[
{label: '重新翻译', value: 'retranslation', divided: true},
{label: '隐藏翻译', value: 'hidden'},
])
this.$store.state.menuOperation = { this.$store.state.menuOperation = {
event, event,
list, list,
active: this.cacheTranslationLanguage, active: this.cacheTranslationLanguage,
scrollHide: true, scrollHide: true,
onUpdate: async (language) => { onUpdate: async (language) => {
await this.$store.dispatch("setTranslationLanguage", language); if (languageList[language]) {
this.onTranslation(); await this.$store.dispatch("setTranslationLanguage", language);
}
this.onTranslation(language);
} }
} }
}, },

View File

@ -3594,6 +3594,16 @@ export default {
$A.IDBSave("cacheTranslations", state.cacheTranslations.slice(-200)) $A.IDBSave("cacheTranslations", state.cacheTranslations.slice(-200))
}, },
/**
* 删除翻译
* @param state
* @param key
*/
removeTranslation({state}, key) {
state.cacheTranslations = state.cacheTranslations.filter(item => item.key != key)
$A.IDBSave("cacheTranslations", state.cacheTranslations.slice(-200))
},
/** /**
* 设置翻译语言 * 设置翻译语言
* @param state * @param state

View File

@ -2171,6 +2171,24 @@
} }
} }
} }
.content-translation {
.apply-reasoning {
display: none;
}
.apply-create-task {
ul {
li:last-child {
margin-bottom: 0;
}
}
.apply-button {
display: none;
}
}
}
} }
body:not(.window-touch) { body:not(.window-touch) {