Skip to content

Commit

Permalink
chore(chat-sdk): dedupe relevant context so when users push Cmd-L twi… (
Browse files Browse the repository at this point in the history
TabbyML#2961)

* chore(chat-sdk): dedupe relevant context so when users push Cmd-L twice we won't append the same context twice

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wsxiaoys and autofix-ci[bot] authored Aug 23, 2024
1 parent ea9745f commit e452790
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function ChatRenderer(
}

const handleAddRelevantContext = useLatest((context: Context) => {
setRelevantContext(relevantContext.concat([context]))
setRelevantContext(oldValue => appendContextAndDedupe(oldValue, context))
})

const addRelevantContext = (context: Context) => {
Expand Down Expand Up @@ -504,4 +504,25 @@ function ChatRenderer(
)
}

function appendContextAndDedupe(
ctxList: Context[],
newCtx: Context
): Context[] {
if (!ctxList.some(ctx => isContextEqual(ctx, newCtx))) {
ctxList.push(newCtx)
}
return ctxList
}

function isContextEqual(lhs: Context, rhs: Context): boolean {
return (
lhs.kind === rhs.kind &&
lhs.range.start === rhs.range.start &&
lhs.range.end === rhs.range.end &&
lhs.filepath === rhs.filepath &&
lhs.content === rhs.content &&
lhs.git_url === rhs.git_url
)
}

export const Chat = React.forwardRef<ChatRef, ChatProps>(ChatRenderer)

0 comments on commit e452790

Please sign in to comment.