Only show 3 recent files as context in obsidian file read, write mode

Related #1209
This commit is contained in:
Debanjum
2025-08-20 15:09:01 -07:00
parent eb2f0ec6bc
commit d8b2df4107

View File

@@ -1,4 +1,4 @@
import { App, TFile } from 'obsidian'; import { App, MarkdownView, TFile } from 'obsidian';
import { diffWords } from 'diff'; import { diffWords } from 'diff';
/** /**
@@ -55,6 +55,7 @@ export class FileInteractions {
private app: App; private app: App;
private readonly EDIT_BLOCK_START = '<khoj_edit>'; private readonly EDIT_BLOCK_START = '<khoj_edit>';
private readonly EDIT_BLOCK_END = '</khoj_edit>'; private readonly EDIT_BLOCK_END = '</khoj_edit>';
private readonly CONTEXT_FILES_LIMIT = 3;
/** /**
* Constructor for FileInteractions * Constructor for FileInteractions
@@ -65,6 +66,26 @@ export class FileInteractions {
this.app = app; this.app = app;
} }
/**
* Get N open, recently viewed markdown files.
*/
private getRecentActiveMarkdownFiles(N: number): TFile[] {
const seen = new Set<string>();
const recentActiveFiles = this.app.workspace.getLeavesOfType('markdown')
.sort((a, b) => (b as any).activeTime - (a as any).activeTime) // Sort by leaf activeTime (note: undocumented prop)
.map(leaf => (leaf.view as MarkdownView)?.file)
// Dedupe by file path
.filter((file): file is TFile => {
if (!file || seen.has(file.path)) return false;
seen.add(file.path);
return true;
})
.slice(0, N);
console.log(`Using ${recentActiveFiles.length} recently viewed md files for context: ${recentActiveFiles.map(file => file.path).join(', ')}`);
return recentActiveFiles;
}
/** /**
* Gets the content of all open files * Gets the content of all open files
* *
@@ -75,9 +96,9 @@ export class FileInteractions {
// Only proceed if we have read or write access // Only proceed if we have read or write access
if (fileAccessMode === 'none') return ''; if (fileAccessMode === 'none') return '';
// Get all open markdown leaves // Get recently viewed markdown files
const leaves = this.app.workspace.getLeavesOfType('markdown'); const recentFiles = this.getRecentActiveMarkdownFiles(this.CONTEXT_FILES_LIMIT);
if (leaves.length === 0) return ''; if (recentFiles.length === 0) return '';
// Instructions in write access mode // Instructions in write access mode
let editInstructions: string = ''; let editInstructions: string = '';
@@ -274,11 +295,7 @@ For context, the user is currently working on the following files:
`; `;
for (const leaf of leaves) { for (const file of recentFiles) {
const view = leaf.view as any;
const file = view?.file;
if (!file || file.extension !== 'md') continue;
// Read file content // Read file content
let fileContent: string; let fileContent: string;
try { try {
@@ -684,10 +701,8 @@ For context, the user is currently working on the following files:
// Track current content for each file as we apply edits // Track current content for each file as we apply edits
const currentFileContents = new Map<string, string>(); const currentFileContents = new Map<string, string>();
// Get all open markdown files // Get recently viewed markdown file(s) to edit
const files = this.app.workspace.getLeavesOfType('markdown') const files = this.getRecentActiveMarkdownFiles(this.CONTEXT_FILES_LIMIT);
.map(leaf => (leaf.view as any)?.file)
.filter(file => file && file.extension === 'md');
// Track success/failure for each edit // Track success/failure for each edit
const editResults: { block: EditBlock, success: boolean, error?: string }[] = []; const editResults: { block: EditBlock, success: boolean, error?: string }[] = [];