-
Notifications
You must be signed in to change notification settings - Fork 23
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
IntelliJ reacts to attribution enable/disable from site config #318
Merged
cbart
merged 10 commits into
main
from
cb/59427/chat-attribution-ui-based-on-site-config
Feb 2, 2024
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6ffbe11
Cherry-pick commit changes to avoid merge :/
cbart d6ad6b9
Revert CodyAgentClient and CodyAgent
cbart 4cbf837
Apply changes to CodyAgentClient and CodyAgent
cbart 6f96d39
Fix WebviewMessage merge errors
cbart aec5518
Spotless
cbart e3b6917
Use cancellation token. Fix type errors
cbart 06ea442
Add CodyAgentClientTest checking setConfigFeatures
cbart ccd9389
Sync main
cbart 4ac5a35
Spotless
cbart c8b0a18
CurrentConfigFeaturesTest
cbart 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 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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sourcegraph/cody/agent/ConfigFeaturesObserver.java
This file contains 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,12 @@ | ||
package com.sourcegraph.cody.agent; | ||
|
||
/** | ||
* {@link ConfigFeaturesObserver} can be notified of changes in {@link ConfigFeatures} from the | ||
* agent. | ||
* | ||
* <p>This can be attached to {@link CurrentConfigFeatures}, which multiplexes notifications from | ||
* {@link CodyAgentClient#onConfigFeatures}. | ||
*/ | ||
public interface ConfigFeaturesObserver { | ||
void update(ConfigFeatures newConfigFeatures); | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/com/sourcegraph/cody/agent/CurrentConfigFeatures.java
This file contains 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,100 @@ | ||
package com.sourcegraph.cody.agent; | ||
|
||
import com.intellij.openapi.components.Service; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* {@link CurrentConfigFeatures} distributes the information about Cody feature configuration, like | ||
* whether attribution is turned on or off, so that UI can adapt accordingly. | ||
* | ||
* <p>These features are turned on/off on the Sourcegraph instance and are fetched periodically by | ||
* the agent. | ||
* | ||
* <p>Observers implementing {@link Consumer<ConfigFeatures>} can {@link #attach} and then will be | ||
* notified of each config feature. Note: the observers will be notified irrespective of whether | ||
* value of config feature is the same or different from the previous value, and need to | ||
* de-duplicate individually if needed. | ||
*/ | ||
@Service(Service.Level.PROJECT) | ||
public final class CurrentConfigFeatures implements ConfigFeaturesObserver { | ||
|
||
/** Most recent {@link ConfigFeatures}. */ | ||
private final AtomicReference<ConfigFeatures> features = | ||
new AtomicReference<>(new ConfigFeatures(false)); | ||
|
||
/** | ||
* Observers that are attached (see {@link #attach}) and receive updates | ||
* ({@link ConfigFeaturesObserver#update). | ||
* | ||
* <p>{@link IdentityObserver} is used here in order to provide precise | ||
* dispose semantics (removal from this set) irrespecitve of {@link #equals} | ||
* behavior on the delegate {@link ConfigFeaturesObserver}. | ||
*/ | ||
private final Set<IdentityObserver> observers = ConcurrentHashMap.newKeySet(); | ||
|
||
/** Retrieve the most recent {@link ConfigFeatures} value. */ | ||
public ConfigFeatures get() { | ||
return features.get(); | ||
} | ||
|
||
/** | ||
* New {@link ConfigFeatures} arrive from the agent. This method updates state and notifies all | ||
* observers. | ||
*/ | ||
@Override | ||
public void update(ConfigFeatures configFeatures) { | ||
this.features.set(configFeatures); | ||
observers.forEach((observer) -> observer.update(configFeatures)); | ||
} | ||
|
||
/** | ||
* Given listener will be given new {@link ConfigFeatures} whenever they arrive. Observation | ||
* relationship is ended once the returned cleanup {@link Runnable} is {@link Runnable#run()}. | ||
*/ | ||
public Runnable attach(ConfigFeaturesObserver observer) { | ||
IdentityObserver id = new IdentityObserver(observer); | ||
observers.add(id); | ||
return () -> observers.remove(id); | ||
} | ||
|
||
/** | ||
* {@link IdentityObserver} wraps {@link ConfigFeaturesObserver} reimplementing {@link #equals} | ||
* with identity for precise cleanup. This way cleanup {@link Runnable} returned from {@link | ||
* #attach} can drop precisely that given {@link ConfigFeaturesObserver} irrespective of the | ||
* {@link #equals} semantics implemented. | ||
*/ | ||
private static class IdentityObserver implements ConfigFeaturesObserver { | ||
final ConfigFeaturesObserver delegate; | ||
|
||
IdentityObserver(ConfigFeaturesObserver delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void update(ConfigFeatures newConfigFeatures) { | ||
delegate.update(newConfigFeatures); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (!(other instanceof IdentityObserver)) { | ||
return false; | ||
} | ||
IdentityObserver that = (IdentityObserver) other; | ||
return this.delegate == that.delegate; | ||
} | ||
|
||
/** | ||
* {@link #delegate#hashCode} meets the {@link #equals} / {@link #hashCode} contract since | ||
* {@link #equals} uses identity semantics on {@link IdentityObserver} which is stronger than | ||
* identity semantics on {@link #delegate}. | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return delegate.hashCode(); | ||
} | ||
} | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor thing, but you could use
CancellationToken
here.A bit more code but I think it is a bit more clear regarding the intent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!