-
Notifications
You must be signed in to change notification settings - Fork 48
feat: Auto-approve reads of Copilot customization files #331
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
4 commits
Select commit
Hold shift + click to select a range
0209dac
feat: Auto-approve reads of Copilot customization files
xinyi-gong 073d3af
rename refresh functions and merge workspaceFoldersParams
xinyi-gong 90c46a9
refactor: Decouple customization file service via event bus
xinyi-gong 5970215
rename refreshAll to refreshAllAsync
xinyi-gong 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
144 changes: 144 additions & 0 deletions
144
...se.core/src/com/microsoft/copilot/eclipse/core/chat/service/CustomizationFileService.java
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,144 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| package com.microsoft.copilot.eclipse.core.chat.service; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.eclipse.e4.core.contexts.EclipseContextFactory; | ||
| import org.eclipse.e4.core.services.events.IEventBroker; | ||
| import org.eclipse.lsp4j.WorkspaceFolder; | ||
| import org.osgi.framework.FrameworkUtil; | ||
| import org.osgi.service.event.EventHandler; | ||
|
|
||
| import com.microsoft.copilot.eclipse.core.CopilotCore; | ||
| import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants; | ||
| import com.microsoft.copilot.eclipse.core.lsp.CopilotLanguageServerConnection; | ||
| import com.microsoft.copilot.eclipse.core.lsp.protocol.CustomizationFileInfo; | ||
| import com.microsoft.copilot.eclipse.core.utils.FileUtils; | ||
| import com.microsoft.copilot.eclipse.core.utils.WorkspaceUtils; | ||
|
|
||
| /** | ||
| * Default {@link ICustomizationFileService} backed by the language server's {@code copilot/custom*} | ||
| * list requests. Each customization type is fetched and snapshotted independently. | ||
| */ | ||
| public class CustomizationFileService implements ICustomizationFileService { | ||
|
|
||
| private final CopilotLanguageServerConnection lsConnection; | ||
| private final IEventBroker eventBroker; | ||
| private final EventHandler customizationFilesChangedHandler; | ||
|
|
||
| private volatile Set<Path> promptFiles = Set.of(); | ||
| private volatile Set<Path> instructionFiles = Set.of(); | ||
| private volatile Set<Path> agentFiles = Set.of(); | ||
| private volatile Set<Path> skillFolders = Set.of(); | ||
|
|
||
| private volatile Set<Path> customizationFiles = Set.of(); | ||
|
|
||
| /** | ||
| * Creates the service and subscribes to customization-file change events. | ||
| * | ||
| * @param lsConnection the language server connection used to issue the list requests | ||
| */ | ||
| public CustomizationFileService(CopilotLanguageServerConnection lsConnection) { | ||
| this.lsConnection = lsConnection; | ||
| this.eventBroker = EclipseContextFactory | ||
| .getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext()).get(IEventBroker.class); | ||
| this.customizationFilesChangedHandler = event -> { | ||
| if (event.getProperty(IEventBroker.DATA) instanceof CustomizationType type) { | ||
| CompletableFuture.runAsync(() -> refreshType(type, WorkspaceUtils.listWorkspaceFolders())); | ||
| } | ||
| }; | ||
| if (eventBroker != null) { | ||
| eventBroker.subscribe(CopilotEventConstants.TOPIC_CHAT_DID_CHANGE_CUSTOMIZATION_FILES, | ||
| customizationFilesChangedHandler); | ||
| } | ||
| } | ||
|
|
||
| /** Unsubscribes from customization-file change events. */ | ||
| public void dispose() { | ||
| if (eventBroker != null) { | ||
| eventBroker.unsubscribe(customizationFilesChangedHandler); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Path> getCustomizationFiles() { | ||
| return customizationFiles; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Path> getSkillFolders() { | ||
| return skillFolders; | ||
| } | ||
|
|
||
| @Override | ||
| public void refreshAllAsync() { | ||
| CompletableFuture.runAsync(() -> { | ||
| List<WorkspaceFolder> workspaceFolders = WorkspaceUtils.listWorkspaceFolders(); | ||
| for (CustomizationType type : CustomizationType.values()) { | ||
| refreshType(type, workspaceFolders); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void refreshType(CustomizationType type, List<WorkspaceFolder> workspaceFolders) { | ||
| switch (type) { | ||
| case SKILL -> toPaths(lsConnection.listCustomSkills(workspaceFolders)).thenAccept(paths -> { | ||
| Set<Path> folders = new HashSet<>(); | ||
| for (Path skillFile : paths) { | ||
| Path parent = skillFile.getParent(); | ||
| if (parent != null) { | ||
| folders.add(parent); | ||
| } | ||
| } | ||
| this.skillFolders = Set.copyOf(folders); | ||
| }); | ||
| case PROMPT -> toPaths(lsConnection.listCustomPrompts(workspaceFolders)).thenAccept(paths -> { | ||
| this.promptFiles = Set.copyOf(paths); | ||
| rebuildCustomizationFiles(); | ||
| }); | ||
| case INSTRUCTION -> toPaths(lsConnection.listCustomInstructions(workspaceFolders)).thenAccept(paths -> { | ||
| this.instructionFiles = Set.copyOf(paths); | ||
| rebuildCustomizationFiles(); | ||
| }); | ||
| case AGENT -> toPaths(lsConnection.listCustomAgents(workspaceFolders)).thenAccept(paths -> { | ||
| this.agentFiles = Set.copyOf(paths); | ||
| rebuildCustomizationFiles(); | ||
| }); | ||
| default -> { | ||
| // No other customization types. | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private synchronized void rebuildCustomizationFiles() { | ||
| Set<Path> all = new HashSet<>(promptFiles); | ||
| all.addAll(instructionFiles); | ||
| all.addAll(agentFiles); | ||
| this.customizationFiles = Set.copyOf(all); | ||
| } | ||
|
|
||
| private CompletableFuture<List<Path>> toPaths(CompletableFuture<CustomizationFileInfo[]> future) { | ||
| return future.thenApply(infos -> { | ||
| List<Path> paths = new ArrayList<>(); | ||
| if (infos != null) { | ||
| for (CustomizationFileInfo info : infos) { | ||
| Path path = FileUtils.getLocalFilePath(info.uri()); | ||
| if (path != null) { | ||
| paths.add(path.toAbsolutePath().normalize()); | ||
| } | ||
| } | ||
| } | ||
| return paths; | ||
| }).exceptionally(ex -> { | ||
| CopilotCore.LOGGER.error("Failed to list customization files", ex); | ||
| return List.of(); | ||
| }); | ||
| } | ||
| } | ||
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
39 changes: 39 additions & 0 deletions
39
...e.core/src/com/microsoft/copilot/eclipse/core/chat/service/ICustomizationFileService.java
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,39 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| package com.microsoft.copilot.eclipse.core.chat.service; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * Maintains an up-to-date view of the Copilot customization files (skills, prompts, instructions, | ||
| * and agents) reported by the language server, keyed by their on-disk location. | ||
| * | ||
| */ | ||
| public interface ICustomizationFileService { | ||
|
|
||
| /** The customization kinds, each backed by a distinct language-server list request. */ | ||
| enum CustomizationType { | ||
| SKILL, PROMPT, INSTRUCTION, AGENT | ||
| } | ||
|
|
||
| /** | ||
| * Returns the absolute paths of single-file customization files (prompts, instructions, agents). | ||
| * The returned set is an immutable snapshot. | ||
| */ | ||
| Set<Path> getCustomizationFiles(); | ||
|
|
||
| /** | ||
| * Returns the absolute paths of skill folders (the directory containing each {@code SKILL.md}). | ||
| * A read of any file within one of these folders is a skill read. The returned set is an immutable | ||
| * snapshot. | ||
| */ | ||
| Set<Path> getSkillFolders(); | ||
|
|
||
| /** | ||
| * Refreshes every customization type from the language server. Used for the initial load. | ||
| * Fire-and-forget; the work completes asynchronously off the caller's thread. | ||
| */ | ||
| void refreshAllAsync(); | ||
| } |
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
Oops, something went wrong.
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.