-
Notifications
You must be signed in to change notification settings - Fork 0
Fix duplicate replies to home feed posts #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5845,42 +5845,52 @@ Response (YES/NO):`; | |
| const convId = this._getConversationIdFromEvent(evt); | ||
| const { roomId } = await this._ensureNostrContext(evt.pubkey, undefined, convId); | ||
|
|
||
| // Decide whether to engage based on thread context | ||
| const shouldEngage = this._shouldEngageWithThread(evt, threadContext); | ||
| if (!shouldEngage) { | ||
| logger.debug(`[NOSTR] Home feed skipping reply to ${evt.id.slice(0, 8)} after thread analysis - not suitable for engagement`); | ||
| success = false; | ||
| break; | ||
| } | ||
|
|
||
| // Process images in home feed post content (if enabled) | ||
| let imageContext = { imageDescriptions: [], imageUrls: [] }; | ||
| if (this.imageProcessingEnabled) { | ||
| try { | ||
| logger.info(`[NOSTR] Processing images in home feed post: "${evt.content?.slice(0, 200)}..."`); | ||
| const { processImageContent } = require('./image-vision'); | ||
| const fullImageContext = await processImageContent(evt.content || '', this.runtime); | ||
| imageContext = { | ||
| imageDescriptions: fullImageContext.imageDescriptions.slice(0, this.maxImagesPerMessage), | ||
| imageUrls: fullImageContext.imageUrls.slice(0, this.maxImagesPerMessage) | ||
| }; | ||
| logger.info(`[NOSTR] Processed ${imageContext.imageDescriptions.length} images from home feed post`); | ||
| } catch (error) { | ||
| logger.error(`[NOSTR] Error in home feed image processing: ${error.message || error}`); | ||
| imageContext = { imageDescriptions: [], imageUrls: [] }; | ||
| // Decide whether to engage based on thread context | ||
| const shouldEngage = this._shouldEngageWithThread(evt, threadContext); | ||
| if (!shouldEngage) { | ||
| logger.debug(`[NOSTR] Home feed skipping reply to ${evt.id.slice(0, 8)} after thread analysis - not suitable for engagement`); | ||
| success = false; | ||
| break; | ||
| } | ||
|
|
||
| // Check if we've already replied to this event (early exit to avoid unnecessary LLM calls) | ||
| const eventMemoryId = this.createUniqueUuid(this.runtime, evt.id); | ||
| const recent = await this.runtime.getMemories({ tableName: 'messages', roomId, count: 100 }); | ||
anabelle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const hasReply = recent.some((m) => m.content?.inReplyTo === eventMemoryId || m.content?.inReplyTo === evt.id); | ||
| if (hasReply) { | ||
| logger.info(`[NOSTR] Skipping home feed reply to ${evt.id.slice(0, 8)} (found existing reply)`); | ||
| success = false; | ||
| break; | ||
| } | ||
|
|
||
| // Process images in home feed post content (if enabled) | ||
| let imageContext = { imageDescriptions: [], imageUrls: [] }; | ||
| if (this.imageProcessingEnabled) { | ||
| try { | ||
| logger.info(`[NOSTR] Processing images in home feed post: "${evt.content?.slice(0, 200)}..."`); | ||
| const { processImageContent } = require('./image-vision'); | ||
| const fullImageContext = await processImageContent(evt.content || '', this.runtime); | ||
| imageContext = { | ||
| imageDescriptions: fullImageContext.imageDescriptions.slice(0, this.maxImagesPerMessage), | ||
| imageUrls: fullImageContext.imageUrls.slice(0, this.maxImagesPerMessage) | ||
| }; | ||
| logger.info(`[NOSTR] Processed ${imageContext.imageDescriptions.length} images from home feed post`); | ||
| } catch (error) { | ||
| logger.error(`[NOSTR] Error in home feed image processing: ${error.message || error}`); | ||
| imageContext = { imageDescriptions: [], imageUrls: [] }; | ||
| } | ||
| } | ||
|
|
||
| const text = await this.generateReplyTextLLM(evt, roomId, threadContext, imageContext); | ||
|
|
||
| // Check if LLM generation failed (returned null) | ||
| if (!text || !text.trim()) { | ||
| logger.warn(`[NOSTR] Skipping home feed reply to ${evt.id.slice(0, 8)} - LLM generation failed`); | ||
| success = false; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const text = await this.generateReplyTextLLM(evt, roomId, threadContext, imageContext); | ||
|
|
||
| // Check if LLM generation failed (returned null) | ||
| if (!text || !text.trim()) { | ||
| logger.warn(`[NOSTR] Skipping home feed reply to ${evt.id.slice(0, 8)} - LLM generation failed`); | ||
| success = false; | ||
| break; | ||
| } | ||
|
|
||
| success = await this.postReply(evt, text); | ||
|
|
||
| success = await this.postReply(evt, text); | ||
| break; | ||
|
Comment on lines
+5849
to
5894
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing reply memory makes dedupe ineffective We now check - const { roomId } = await this._ensureNostrContext(evt.pubkey, undefined, convId);
+ const { roomId, entityId } = await this._ensureNostrContext(evt.pubkey, undefined, convId);
@@
- success = await this.postReply(evt, text);
+ success = await this.postReply(evt, text);
+ if (success) {
+ try {
+ const replyMemoryId = this.createUniqueUuid(this.runtime, `${evt.id}:reply:${Date.now()}`);
+ await this._createMemorySafe({
+ id: replyMemoryId,
+ entityId,
+ agentId: this.runtime.agentId,
+ roomId,
+ content: {
+ text,
+ source: 'nostr',
+ inReplyTo: eventMemoryId,
+ },
+ createdAt: Date.now(),
+ }, 'messages');
+ } catch (err) {
+ this.logger?.debug?.('[NOSTR] Failed to persist home feed reply memory:', err?.message || err);
+ }
+ }
|
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,154 @@ | ||||||
| import { NostrService } from '../lib/service.js'; | ||||||
| import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||||||
|
|
||||||
| describe('NostrService Home Feed Reply Deduplication', () => { | ||||||
| let service; | ||||||
| let mockRuntime; | ||||||
| let mockPool; | ||||||
|
|
||||||
| beforeEach(() => { | ||||||
| // Mock runtime with minimal required interface | ||||||
| mockRuntime = { | ||||||
| character: { name: 'Test', postExamples: ['test'] }, | ||||||
| getSetting: vi.fn((key) => { | ||||||
| const settings = { | ||||||
| 'NOSTR_RELAYS': 'wss://test.relay', | ||||||
| 'NOSTR_PRIVATE_KEY': '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', // Test fixture - not a real secret | ||||||
| 'NOSTR_LISTEN_ENABLE': 'false', // Disable listening to prevent subscriptions | ||||||
| 'NOSTR_POST_ENABLE': 'false', // Disable posting to prevent scheduled posts | ||||||
| 'NOSTR_REPLY_ENABLE': 'true', | ||||||
| 'NOSTR_DM_ENABLE': 'false', | ||||||
| 'NOSTR_DM_REPLY_ENABLE': 'false', | ||||||
| 'NOSTR_CONTEXT_ACCUMULATOR_ENABLED': 'false', | ||||||
| 'NOSTR_CONTEXT_LLM_ANALYSIS': 'false', | ||||||
| 'NOSTR_HOME_FEED_ENABLE': 'true', | ||||||
| 'NOSTR_DISCOVERY_ENABLE': 'false', | ||||||
| 'NOSTR_ENABLE_PING': 'false', | ||||||
| 'NOSTR_POST_DAILY_DIGEST_ENABLE': 'false', | ||||||
| 'NOSTR_CONNECTION_MONITOR_ENABLE': 'false', | ||||||
| 'NOSTR_UNFOLLOW_ENABLE': 'false', | ||||||
| 'NOSTR_DM_THROTTLE_SEC': '60', | ||||||
| 'NOSTR_REPLY_THROTTLE_SEC': '60', | ||||||
| 'NOSTR_REPLY_INITIAL_DELAY_MIN_MS': '0', | ||||||
| 'NOSTR_REPLY_INITIAL_DELAY_MAX_MS': '0', | ||||||
| 'NOSTR_DISCOVERY_INTERVAL_MIN': '900', | ||||||
| 'NOSTR_DISCOVERY_INTERVAL_MAX': '1800', | ||||||
| 'NOSTR_HOME_FEED_INTERVAL_MIN': '300', | ||||||
| 'NOSTR_HOME_FEED_INTERVAL_MAX': '900', | ||||||
| 'NOSTR_HOME_FEED_REACTION_CHANCE': '0', | ||||||
| 'NOSTR_HOME_FEED_REPOST_CHANCE': '0', | ||||||
| 'NOSTR_HOME_FEED_QUOTE_CHANCE': '0', | ||||||
| 'NOSTR_HOME_FEED_REPLY_CHANCE': '1.0', // Always choose reply for testing | ||||||
| 'NOSTR_HOME_FEED_MAX_INTERACTIONS': '10', | ||||||
| 'NOSTR_MIN_DELAY_BETWEEN_POSTS_MS': '0', | ||||||
| 'NOSTR_MAX_DELAY_BETWEEN_POSTS_MS': '0', | ||||||
| 'NOSTR_MENTION_PRIORITY_BOOST_MS': '5000', | ||||||
| 'NOSTR_MAX_EVENT_AGE_DAYS': '2', | ||||||
| 'NOSTR_ZAP_THANKS_ENABLE': 'false', | ||||||
| 'NOSTR_IMAGE_PROCESSING_ENABLED': 'false' | ||||||
| }; | ||||||
| return settings[key] || ''; | ||||||
| }), | ||||||
| useModel: vi.fn(() => Promise.resolve({ text: 'Test reply' })), | ||||||
| createMemory: vi.fn(), | ||||||
| getMemoryById: vi.fn(), | ||||||
| getMemories: vi.fn(() => []), | ||||||
|
||||||
| getMemories: vi.fn(() => []), | |
| getMemories: vi.fn(() => Promise.resolve([])), |
Uh oh!
There was an error while loading. Please reload this page.