Skip to content

fix(ai): eliminate duplicate note insertion in RAG prompt context (#1273) - #1290

Open
Aniketchavan7 wants to merge 1 commit into
blinkospace:mainfrom
Aniketchavan7:fix/rag-context-duplicate-notes
Open

Aniketchavan7 wants to merge 1 commit into
blinkospace:mainfrom
Aniketchavan7:fix/rag-context-duplicate-notes

Conversation

@Aniketchavan7

@Aniketchavan7 Aniketchavan7 commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #1273

What happens

In server/aiServer/index.ts, when withRAG is enabled, retrieved notes are injected into ragNoteString twice:

let { notes, aiContext } = await AiModelFactory.queryVector(question, Number(ctx.id));
ragNote = notes;
ragNoteString = `This is the note content ${ragNote.map((i) => i.content).join('\n')} ${aiContext}`;

Because AiModelFactory.queryVector already maps the notes into aiContext, concatenating both ${ragNote.map(...)} and ${aiContext} inserts the text of every note twice into the AI prompt. Furthermore, when aiContext was an array of strings, template literal interpolation implicitly converted it with commas between items.

Additionally, AiModelFactory.queryVector did not exclude recycled (isRecycle: true) or archived (isArchived: true) notes when fetching matching note records by ID.

Changes

  1. server/aiServer/index.ts:
    • Only populate ragNoteString when matching notes are found (ragNote.length > 0).
    • Use aiContext directly instead of concatenating the mapped note content twice.
  2. server/aiServer/aiModelFactory.ts:
    • Filter out isRecycle: false and isArchived: false in prisma.notes.findMany within queryVector.
    • Format aiContext as a clean joined newline string.

Fixes #1273

What happens

In server/aiServer/index.ts, when withRAG is enabled, retrieved notes are injected into ragNoteString twice:

let { notes, aiContext } = await AiModelFactory.queryVector(question, Number(ctx.id));
ragNote = notes;
ragNoteString = `This is the note content ${ragNote.map((i) => i.content).join('\n')} ${aiContext}`;

Because AiModelFactory.queryVector already maps the notes into aiContext, concatenating both ${ragNote.map(...)} and ${aiContext} inserts the text of every note twice into the AI prompt. Furthermore, when aiContext was an array of strings, template literal interpolation implicitly converted it with commas between items.

Additionally, AiModelFactory.queryVector did not exclude recycled (isRecycle: true) or archived (isArchived: true) notes when fetching matching note records by ID.

Changes

  1. server/aiServer/index.ts:
  • Only populate ragNoteString when matching notes are found (ragNote.length > 0).
  • Use aiContext directly instead of concatenating the mapped note content twice.
  1. server/aiServer/aiModelFactory.ts:
  • Filter out isRecycle: false and isArchived: false in prisma.notes.findMany within queryVector.
  • Format aiContext as a clean joined newline string.

…inkospace#1273)

Co-authored-by: yalongwastaken <yalongwastaken@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RAG context injects each retrieved note twice into the AI prompt

1 participant