From 0bb526509da5ad220e508b265c54685e081d1b3d Mon Sep 17 00:00:00 2001 From: iven Date: Thu, 23 Apr 2026 14:01:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(identity):=20=E5=90=8D=E5=AD=97=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E4=BB=8E=20memory=20extraction=20=E8=A7=A3=E8=80=A6?= =?UTF-8?q?=20=E2=80=94=20502=20=E4=B8=8D=E5=86=8D=E9=98=BB=E6=96=AD?= =?UTF-8?q?=E9=9D=A2=E6=9D=BF=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit agent/user name 检测之前放在 extractFromConversation().then() 回调里, memory extractor 502 时整个 .then() 跳过,名字永远不会更新。 现在名字检测独立执行,memory extraction 失败不影响面板刷新。 --- desktop/src/store/chat/streamStore.ts | 65 ++++++++++++++------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/desktop/src/store/chat/streamStore.ts b/desktop/src/store/chat/streamStore.ts index 065a814..725e4ac 100644 --- a/desktop/src/store/chat/streamStore.ts +++ b/desktop/src/store/chat/streamStore.ts @@ -366,44 +366,45 @@ function createCompleteHandler( }); } - // Async memory extraction + // Detect name changes from last user message (independent of memory extraction) const msgs = chat.getMessages() || []; + const lastUserMsg = [...msgs].reverse().find(m => m.role === 'user'); + const lastContent = typeof lastUserMsg?.content === 'string' ? lastUserMsg.content : ''; + + if (lastContent && agentId) { + // User name detection (e.g. "叫我小王") + const detectedName = detectNameSuggestion(lastContent); + if (detectedName) { + import('../agentStore').then(({ useAgentStore }) => + useAgentStore.getState().updateClone(agentId, { userName: detectedName }) + .then(() => log.info(`Updated userName to "${detectedName}" from conversation`)) + .catch(e => log.warn('Failed to persist detected userName:', e)) + ); + } + // Agent name detection (e.g. "叫你小马", "名称改为小芳") + const detectedAgentName = detectAgentNameSuggestion(lastContent); + if (detectedAgentName) { + import('../agentStore').then(({ useAgentStore }) => + useAgentStore.getState().updateClone(agentId, { name: detectedAgentName }) + .then(() => { + log.info(`Updated agent name to "${detectedAgentName}" from conversation`); + if (typeof window !== 'undefined') { + window.dispatchEvent(new CustomEvent('zclaw:agent-profile-updated', { + detail: { agentId } + })); + } + }) + .catch(e => log.warn('Failed to persist detected agent name:', e)) + ); + } + } + + // Async memory extraction (independent — failures don't block name detection) const filtered = msgs .filter(m => m.role === 'user' || m.role === 'assistant') .map(m => ({ role: m.role, content: m.content })); const convId = useConversationStore.getState().currentConversationId; getMemoryExtractor().extractFromConversation(filtered, agentId, convId ?? undefined) - .then(async () => { - // Detect name preference from last user message (e.g. "叫我小马") - const lastUserMsg = [...msgs].reverse().find(m => m.role === 'user'); - const detectedName = lastUserMsg ? detectNameSuggestion(lastUserMsg.content) : undefined; - if (detectedName && agentId) { - try { - const { useAgentStore } = await import('../agentStore'); - await useAgentStore.getState().updateClone(agentId, { userName: detectedName }); - log.info(`Updated userName to "${detectedName}" from conversation`); - } catch (e) { - log.warn('Failed to persist detected userName:', e); - } - } - // Detect agent name change (e.g. "叫你小马", "以后叫你小马", "你就叫小马吧") - const lastContent = typeof lastUserMsg?.content === 'string' ? lastUserMsg.content : ''; - const detectedAgentName = lastContent ? detectAgentNameSuggestion(lastContent) : undefined; - if (detectedAgentName && agentId) { - try { - const { useAgentStore } = await import('../agentStore'); - await useAgentStore.getState().updateClone(agentId, { name: detectedAgentName }); - log.info(`Updated agent name to "${detectedAgentName}" from conversation`); - } catch (e) { - log.warn('Failed to persist detected agent name:', e); - } - } - if (typeof window !== 'undefined') { - window.dispatchEvent(new CustomEvent('zclaw:agent-profile-updated', { - detail: { agentId } - })); - } - }) .catch(err => log.warn('Memory extraction failed:', err)); intelligenceClient.reflection.recordConversation().catch(err => {