@@ -417,7 +417,11 @@ void Widget::ensureLocalServer(bool lazyWake)
417417 ensureTimer.start ();
418418
419419 cancelLazyUnload (QStringLiteral (" ensureLocalServer entry" ));
420- if (lazyWake) lazyWakeInFlight_ = true ;
420+ if (lazyWake)
421+ {
422+ lazyWakeInFlight_ = true ;
423+ applyWakeUiLock (true );
424+ }
421425
422426 if (!firstAutoNglEvaluated_ && !serverManager->isRunning ())
423427 {
@@ -552,6 +556,7 @@ void Widget::ensureLocalServer(bool lazyWake)
552556 initiatePortFallback ();
553557 }
554558 lazyWakeInFlight_ = false ;
559+ applyWakeUiLock (false );
555560 return ;
556561 }
557562
@@ -610,7 +615,11 @@ void Widget::ensureLocalServer(bool lazyWake)
610615 if (proxyServer_) proxyServer_->setBackendAvailable (backendOnline_);
611616 if (!lazyWake && backendOnline_) markBackendActivity ();
612617
613- if (!lastServerRestart_ && backendRunning) lazyWakeInFlight_ = false ;
618+ if (!lastServerRestart_ && backendRunning)
619+ {
620+ lazyWakeInFlight_ = false ;
621+ applyWakeUiLock (false );
622+ }
614623
615624 apis.api_endpoint = formatLocalEndpoint (activeServerHost_, activeServerPort_);
616625 apis.api_key = " " ;
@@ -824,9 +833,17 @@ void Widget::performLazyUnloadInternal(bool forced)
824833 emit ui2tool_cancelActive ();
825834 toolInvocationActive_ = false ;
826835 }
836+ const bool hasUi = ui && ui->output ;
837+ const bool hasDocument = hasUi && ui->output ->document ();
838+ const bool hasRenderedContent = hasDocument && !ui->output ->document ()->isEmpty ();
839+ const bool hasConversationHistory = !ui_messagesArray.isEmpty ();
840+ const bool preserveAfterWake = (ui_state == CHAT_STATE) && (hasConversationHistory || hasRenderedContent);
827841 lazyUnloaded_ = true ;
828- if (!forced) lazyUnloadPreserveState_ = true ;
842+ if (preserveAfterWake)
843+ lazyUnloadPreserveState_ = true ;
844+ preserveConversationOnNextReady_ = preserveAfterWake; // Resume chat log after lazy wake
829845 lazyWakeInFlight_ = false ;
846+ applyWakeUiLock (false );
830847 backendOnline_ = false ;
831848 if (proxyServer_) proxyServer_->setBackendAvailable (false );
832849 reflash_state (" ui:" + jtr (" auto eject stop backend" ), SIGNAL_SIGNAL);
@@ -928,6 +945,7 @@ void Widget::onServerReady(const QString &endpoint)
928945 backendOnline_ = true ;
929946 lazyUnloaded_ = false ;
930947 lazyWakeInFlight_ = false ;
948+ applyWakeUiLock (false );
931949 cancelLazyUnload (QStringLiteral (" backend ready" ));
932950 markBackendActivity ();
933951 updateProxyBackend (backendListenHost_, activeBackendPort_);
@@ -961,31 +979,38 @@ void Widget::onServerReady(const QString &endpoint)
961979 load_time = load_timer.isValid () ? (load_timer.nsecsElapsed () / 1e9 ) : 0.0 ;
962980 ui_mode = LOCAL_MODE;
963981
982+ const bool preserveConversation = preserveConversationOnNextReady_;
983+ preserveConversationOnNextReady_ = false ;
984+ skipUnlockLoadIntro_ = preserveConversation;
985+
964986 flushPendingStream ();
965- ui->output ->clear ();
966- ui_messagesArray = QJsonArray ();
967- {
968- QJsonObject systemMessage;
969- systemMessage.insert (" role" , DEFAULT_SYSTEM_NAME);
970- systemMessage.insert (" content" , ui_DATES.date_prompt );
971- ui_messagesArray.append (systemMessage);
972- if (history_ && ui_state == CHAT_STATE)
987+ if (!preserveConversation)
988+ {
989+ ui->output ->clear ();
990+ ui_messagesArray = QJsonArray ();
973991 {
974- SessionMeta meta;
975- meta.id = QString::number (QDateTime::currentMSecsSinceEpoch ());
976- meta.title = " " ;
977- meta.endpoint = frontendEndpoint;
978- meta.model = ui_SETTINGS.modelpath ;
979- meta.system = ui_DATES.date_prompt ;
980- meta.n_ctx = ui_SETTINGS.nctx ;
981- meta.slot_id = -1 ;
982- meta.startedAt = QDateTime::currentDateTime ();
983- history_->begin (meta);
984- history_->appendMessage (systemMessage);
985- currentSlotId_ = -1 ;
992+ QJsonObject systemMessage;
993+ systemMessage.insert (" role" , DEFAULT_SYSTEM_NAME);
994+ systemMessage.insert (" content" , ui_DATES.date_prompt );
995+ ui_messagesArray.append (systemMessage);
996+ if (history_ && ui_state == CHAT_STATE)
997+ {
998+ SessionMeta meta;
999+ meta.id = QString::number (QDateTime::currentMSecsSinceEpoch ());
1000+ meta.title = " " ;
1001+ meta.endpoint = frontendEndpoint;
1002+ meta.model = ui_SETTINGS.modelpath ;
1003+ meta.system = ui_DATES.date_prompt ;
1004+ meta.n_ctx = ui_SETTINGS.nctx ;
1005+ meta.slot_id = -1 ;
1006+ meta.startedAt = QDateTime::currentDateTime ();
1007+ history_->begin (meta);
1008+ history_->appendMessage (systemMessage);
1009+ currentSlotId_ = -1 ;
1010+ }
9861011 }
1012+ bot_predecode_content = ui_DATES.date_prompt ; // 使用系统指令作为“预解码内容”展示
9871013 }
988- bot_predecode_content = ui_DATES.date_prompt ; // 使用系统指令作为“预解码内容”展示
9891014 is_load = true ;
9901015 // After fresh load, the first "all slots are idle" is an idle baseline -> ignore once
9911016 lastServerRestart_ = false ; // 一次重启流程结束
@@ -1585,6 +1610,7 @@ void Widget::onServerStartFailed(const QString &reason)
15851610{
15861611 backendOnline_ = false ;
15871612 lazyWakeInFlight_ = false ;
1613+ applyWakeUiLock (false );
15881614 if (proxyServer_) proxyServer_->setBackendAvailable (false );
15891615 cancelLazyUnload (QStringLiteral (" backend start failed" ));
15901616 pendingSendAfterWake_ = false ;
@@ -1658,6 +1684,11 @@ void Widget::unlockLoad()
16581684 ui->cpu_bar ->setToolTip (jtr (" nthread/maxthread" ) + " " + QString::number (ui_SETTINGS.nthread ) + " /" + QString::number (max_thread));
16591685 auto_save_user ();
16601686 ui_state_normal ();
1687+ if (skipUnlockLoadIntro_)
1688+ {
1689+ skipUnlockLoadIntro_ = false ; // Already showing prior chat, do not inject system prompt
1690+ return ;
1691+ }
16611692 // Record a system header and show system prompt as pre-decode content
16621693 int __idx = recordCreate (RecordRole::System);
16631694 appendRoleHeader (QStringLiteral (" system" ));
0 commit comments