mirror of
https://github.com/block/goose.git
synced 2026-07-17 12:56:20 +02:00
fix: Properly detect and handle Goose messages to prevent LLM chain reactions
- Added Goose message detection in real-time Matrix listener
- Messages starting with '🦆 Goose:' are now treated as assistant messages, not user messages
- Added role correction for Goose messages in Matrix history loading
- Prevents Goose responses from triggering new LLM responses in other users' instances
- Fixes the issue where Goose messages were being processed as user input
- Includes detection based on content patterns, sender names, and display names
This commit is contained in:
@@ -342,13 +342,31 @@ export default function Pair({
|
||||
if (roomHistory.length > 0) {
|
||||
// Convert Matrix messages to Goose message format
|
||||
const gooseMessages: Message[] = roomHistory.map((msg, index) => {
|
||||
// Detect if this is a Goose message and correct the role if needed
|
||||
let messageRole = msg.role as 'user' | 'assistant';
|
||||
|
||||
// Check if the message content indicates it's from Goose
|
||||
const isGooseMessage = msg.content && (
|
||||
msg.content.startsWith('🦆 Goose:') ||
|
||||
msg.content.includes('🦆 Goose:') ||
|
||||
msg.content.startsWith('🤖') ||
|
||||
(msg.metadata?.senderInfo?.displayName && msg.metadata.senderInfo.displayName.toLowerCase().includes('goose')) ||
|
||||
(msg.sender && msg.sender.toLowerCase().includes('goose'))
|
||||
);
|
||||
|
||||
// Override role if it's detected as a Goose message
|
||||
if (isGooseMessage && messageRole === 'user') {
|
||||
messageRole = 'assistant';
|
||||
console.log('📜 Corrected role from user to assistant for Goose message in history:', msg.content.substring(0, 50) + '...');
|
||||
}
|
||||
|
||||
// Create more stable ID based on content and timestamp to help with deduplication
|
||||
const contentHash = msg.content.substring(0, 50).replace(/[^a-zA-Z0-9]/g, '');
|
||||
const stableId = `matrix_${msg.timestamp.getTime()}_${msg.role}_${contentHash}`;
|
||||
const stableId = `matrix_${msg.timestamp.getTime()}_${messageRole}_${contentHash}`;
|
||||
|
||||
return {
|
||||
id: stableId,
|
||||
role: msg.role as 'user' | 'assistant',
|
||||
role: messageRole,
|
||||
created: Math.floor(msg.timestamp.getTime() / 1000),
|
||||
content: [
|
||||
{
|
||||
@@ -421,10 +439,26 @@ export default function Pair({
|
||||
roomId
|
||||
});
|
||||
|
||||
// Detect if this is a Goose message (assistant response)
|
||||
const isGooseMessage = content && (
|
||||
content.startsWith('🦆 Goose:') ||
|
||||
content.includes('🦆 Goose:') ||
|
||||
content.startsWith('🤖') ||
|
||||
sender.toLowerCase().includes('goose') ||
|
||||
(senderInfo?.displayName && senderInfo.displayName.toLowerCase().includes('goose'))
|
||||
);
|
||||
|
||||
// Determine the role based on message content and sender
|
||||
let messageRole: 'user' | 'assistant' = 'user';
|
||||
if (isGooseMessage) {
|
||||
messageRole = 'assistant';
|
||||
console.log('🦆 Detected Goose message from Matrix - treating as assistant message');
|
||||
}
|
||||
|
||||
// Convert to Goose message format
|
||||
const newMessage: Message = {
|
||||
id: `matrix_${Date.now()}_user_${Math.random().toString(36).substr(2, 9)}`,
|
||||
role: 'user',
|
||||
id: `matrix_${Date.now()}_${messageRole}_${Math.random().toString(36).substr(2, 9)}`,
|
||||
role: messageRole,
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
content: [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user