Skip to content

Commit

Permalink
feat(chat): add filtering functionality for mention items based on qu…
Browse files Browse the repository at this point in the history
…ery input
  • Loading branch information
Sma1lboy committed Feb 2, 2025
1 parent 369fb1a commit 09e5f26
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ee/tabby-ui/components/chat/form-editor/mention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import { IconChevronLeft } from '@/components/ui/icons'

import { emitter } from '../event-emitter'
import type { CategoryItem, CategoryMenu, FileItem, SourceItem } from './types'
import { fileItemToSourceItem, symbolItemToSourceItem } from './utils'
import {
fileItemToSourceItem,
filterItemsByQuery,
symbolItemToSourceItem
} from './utils'

/**
* A React component to render a mention node in the editor.
Expand Down Expand Up @@ -238,9 +242,8 @@ export const MentionList = forwardRef<MentionListActions, MentionListProps>(
setItems(files.map(fileItemToSourceItem))
} else {
const symbols = await listActiveSymbols?.()
// eslint-disable-next-line no-console
console.log('symbols', symbols)
setItems(uniqBy(symbols?.map(symbolItemToSourceItem), 'id'))
const symbolItems = uniqBy(symbols?.map(symbolItemToSourceItem), 'id')
setItems(filterItemsByQuery(symbolItems, query))
}
}

Expand Down
36 changes: 36 additions & 0 deletions ee/tabby-ui/components/chat/form-editor/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,39 @@ export const isSameFileContext = (a: FileContext, b: FileContext) => {
a.range?.end === b.range?.end
)
}

// Remove - and _ and convert to lowercase
const normalizeString = (str: string): string => {
return str.toLowerCase().replace(/[-_]/g, '')
}

export const filterItemsByQuery = (
items: SourceItem[],
query: string
): SourceItem[] => {
if (!query) return items

const normalizedQuery = normalizeString(query)

return items.filter(item => {
if (item.isRootCategoryItem) return true

const normalizedName = normalizeString(item.name)
const nameStartsWith = normalizedName.startsWith(normalizedQuery)

if (item.category === 'file') {
const pathParts = item.filepath.split('/')
const normalizedParts = pathParts.map(part => normalizeString(part))
const pathStartsWith = normalizedParts.some(part =>
part.startsWith(normalizedQuery)
)
return nameStartsWith || pathStartsWith
}

if (item.category === 'symbol') {
return nameStartsWith
}

return nameStartsWith
})
}

0 comments on commit 09e5f26

Please sign in to comment.