-
-
Notifications
You must be signed in to change notification settings - Fork 8
Add clipboard relevance filter and line-level distillation #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f7d39b9
Add clipboard relevance filter to drop stale/irrelevant context
FuJacob 9afea0c
Add line-level clipboard distillation before prompt injection
FuJacob e49c60c
Fix distiller test using camelCase name with no token overlap
FuJacob de6620e
Address Greptile review of clipboard filter
FuJacob 74a40a2
Merge remote-tracking branch 'origin/main' into clipboard-relevance-f…
FuJacob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import Foundation | ||
|
|
||
| /// Extracts only the clipboard lines that share meaningful tokens with the user's current | ||
| /// prefix text. Short clipboard content passes through unchanged; longer content is filtered | ||
| /// to the lines most likely to help the autocomplete model. | ||
| enum ClipboardContentDistiller { | ||
| private static let compactLineThreshold = 3 | ||
| private static let headFallbackCharacters = 300 | ||
|
|
||
| /// Returns a distilled version of `clipboard` containing only lines relevant to `prefixText`. | ||
| /// | ||
| /// - Clipboard with ≤3 lines or empty `prefixText` is returned as-is. | ||
| /// - Longer clipboard keeps only lines whose tokens overlap with `prefixText`. | ||
| /// - If no individual line overlaps, the first 300 characters are returned as a head fallback. | ||
| static func distill(clipboard: String, prefixText: String) -> String { | ||
| let lines = clipboard.components(separatedBy: "\n") | ||
| guard lines.count > compactLineThreshold else { return clipboard } | ||
|
|
||
| let prefixTokens = PromptContextSanitizer.significantTokens(from: prefixText) | ||
| guard !prefixTokens.isEmpty else { return clipboard } | ||
|
|
||
| let relevantLines = lines.filter { line in | ||
| let lineTokens = PromptContextSanitizer.significantTokens(from: line) | ||
| return !lineTokens.isDisjoint(with: prefixTokens) | ||
| } | ||
|
|
||
| if relevantLines.isEmpty { | ||
| return String(clipboard.prefix(headFallbackCharacters)) | ||
| } | ||
|
|
||
| return relevantLines.joined(separator: "\n") | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import Foundation | ||
|
|
||
| /// Decides whether the current clipboard content is relevant enough to inject into the | ||
| /// autocomplete prompt. Tracks clipboard identity via an external change count, records when | ||
| /// the clipboard last changed during this Cotabby session, and applies two heuristics: | ||
| /// staleness and token overlap. | ||
| /// | ||
| /// Why no source-app affinity: we never observe the actual copier — only the app that is | ||
| /// frontmost when autocomplete fires, which is always the typing app. Recording the typing | ||
| /// app as the "source" granted same-app shortcuts in apps where the user merely typed, | ||
| /// bypassing the overlap guard for unrelated clipboard content. | ||
| /// | ||
| /// Why a sentinel baseline: `NSPasteboard.changeCount` is a non-zero cumulative counter, so | ||
| /// initializing to `0` made every first observation look like a fresh copy event and reset | ||
| /// the staleness clock to "now" — granting up to five minutes of injection for content | ||
| /// copied hours before Cotabby launched. The first observation now records the baseline | ||
| /// without stamping a date, gating injection until an actual change is detected. | ||
| /// | ||
| /// The filter never reads `NSPasteboard` directly — the caller passes in a plain `Int` change | ||
| /// count and the raw clipboard string, keeping this type fully testable without AppKit. | ||
| @MainActor | ||
| final class ClipboardRelevanceFilter: ClipboardRelevanceFiltering { | ||
| static let staleThresholdSeconds: TimeInterval = 300 | ||
| private static let minimumTokenLength = 3 | ||
|
|
||
| private var lastKnownChangeCount: Int? | ||
| private var lastChangeDate: Date? | ||
| private let dateProvider: () -> Date | ||
|
|
||
| init(dateProvider: @escaping () -> Date = { Date() }) { | ||
| self.dateProvider = dateProvider | ||
| } | ||
|
|
||
| /// Returns `clipboard` unchanged when it looks relevant, or `nil` when it should be dropped. | ||
| func filter( | ||
| clipboard: String?, | ||
| pasteboardChangeCount: Int, | ||
| precedingText: String | ||
| ) -> String? { | ||
| guard let clipboard else { return nil } | ||
|
|
||
| guard let baselineChangeCount = lastKnownChangeCount else { | ||
| // First observation: record the baseline so we can detect *new* copies, but leave | ||
| // the staleness clock unset. Pre-existing clipboard content is not injected until | ||
| // the user actually copies again while Cotabby is running. | ||
| lastKnownChangeCount = pasteboardChangeCount | ||
| return nil | ||
| } | ||
|
|
||
| if pasteboardChangeCount != baselineChangeCount { | ||
| lastKnownChangeCount = pasteboardChangeCount | ||
| lastChangeDate = dateProvider() | ||
| } | ||
|
|
||
| guard let lastChangeDate, | ||
| dateProvider().timeIntervalSince(lastChangeDate) < Self.staleThresholdSeconds | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| let clipboardTokens = Self.tokens(from: clipboard) | ||
| let prefixTokens = Self.tokens(from: precedingText) | ||
| guard !clipboardTokens.isDisjoint(with: prefixTokens) else { | ||
| return nil | ||
| } | ||
|
|
||
| return clipboard | ||
| } | ||
|
|
||
| private static func tokens(from text: String) -> Set<String> { | ||
| PromptContextSanitizer.significantTokens(from: text, minimumLength: minimumTokenLength) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.