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 => {