Skip to content

Commit

Permalink
feat: add test patterns button for RAG exclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-on committed Nov 20, 2024
1 parent 8351df5 commit 9360fc4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/settings/SettingTab.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { App, PluginSettingTab, Setting } from 'obsidian'
import { App, Modal, PluginSettingTab, Setting, TFile } from 'obsidian'

import {
APPLY_MODEL_OPTIONS,
CHAT_MODEL_OPTIONS,
EMBEDDING_MODEL_OPTIONS,
} from '../constants'
import SmartCopilotPlugin from '../main'
import { findFilesMatchingPatterns } from '../utils/globUtils'

export class SmartCopilotSettingTab extends PluginSettingTab {
plugin: SmartCopilotPlugin
Expand Down Expand Up @@ -178,7 +179,17 @@ export class SmartCopilotSettingTab extends PluginSettingTab {
new Setting(containerEl)
.setName('Exclude patterns')
.setDesc(
'Files matching these patterns will be excluded from indexing. One pattern per line. Uses glob patterns (e.g., "private/*", "*.tmp").',
'Files matching these patterns will be excluded from indexing. One pattern per line. Uses glob patterns (e.g., "private/*", "*.tmp"). After changing this, use the command "Rebuild entire vault index" to apply changes.',
)
.addButton((button) =>
button.setButtonText('Test patterns').onClick(async () => {
const patterns = this.plugin.settings.ragOptions.excludePatterns
const excludedFiles = await findFilesMatchingPatterns(
patterns,
this.plugin.app.vault,
)
new ExcludedFilesModal(this.app, excludedFiles).open()
}),
)

new Setting(containerEl)
Expand Down Expand Up @@ -294,3 +305,34 @@ export class SmartCopilotSettingTab extends PluginSettingTab {
)
}
}

class ExcludedFilesModal extends Modal {
private files: TFile[]

constructor(app: App, files: TFile[]) {
super(app)
this.files = files
}

onOpen() {
const { contentEl } = this
contentEl.empty()

this.titleEl.setText(`Excluded Files (${this.files.length})`)

if (this.files.length === 0) {
contentEl.createEl('p', { text: 'No files match the exclusion patterns' })
return
}

const list = contentEl.createEl('ul')
this.files.forEach((file) => {
list.createEl('li', { text: file.path })
})
}

onClose() {
const { contentEl } = this
contentEl.empty()
}
}
12 changes: 12 additions & 0 deletions src/utils/globUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { minimatch } from 'minimatch'
import { Vault } from 'obsidian'

export const findFilesMatchingPatterns = async (
patterns: string[],
vault: Vault,
) => {
const files = vault.getMarkdownFiles()
return files.filter((file) => {
return patterns.some((pattern) => minimatch(file.path, pattern))
})
}

0 comments on commit 9360fc4

Please sign in to comment.