Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

package com.microsoft.copilot.eclipse.core.chat;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.microsoft.copilot.eclipse.core.lsp.CopilotLanguageServerConnection;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationMode;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationModesParams;

@ExtendWith(MockitoExtension.class)
class BuiltInChatModeManagerTests {

@Mock
private CopilotLanguageServerConnection mockConnection;

@Test
void testLoadBuiltInModes_inlineAgentSkippedFromAgentModes() {
ConversationMode agentMode = createBuiltInMode("Agent", "Agent", "Agent",
"Advanced agent mode with access to tools and capabilities");
ConversationMode inlineAgentMode = createBuiltInMode("InlineAgent", "Agent", "InlineAgent",
"Agent mode with a restricted tool set for inline editing");

when(mockConnection.listConversationModes(any(ConversationModesParams.class)))
.thenReturn(CompletableFuture.completedFuture(new ConversationMode[] { agentMode, inlineAgentMode }));

List<BuiltInChatMode> builtInModes = BuiltInChatModeManager.INSTANCE.loadBuiltInModes(mockConnection).join();

assertEquals(1, builtInModes.size());

BuiltInChatMode builtInMode = builtInModes.get(0);
assertNotNull(builtInMode);
assertEquals("Agent", builtInMode.getId());
assertEquals("Agent", builtInMode.getDisplayName());
assertEquals("Agent", builtInMode.getKind());
}

@Test
void testLoadBuiltInModes_onlyAllowedBuiltInNames() {
ConversationMode ask = createBuiltInMode("ask", "aSk", "Ask", "Ask mode");
ConversationMode debugger = createBuiltInMode("debugger", "Debugger", "Debugger", "Debugger mode");
ConversationMode custom = createBuiltInMode("custom", "Plan", "Plan", "Custom mode");
custom.setBuiltIn(false);
ConversationMode unknown = createBuiltInMode("unknown", "Unknown", "Unknown", "Unknown mode");

when(mockConnection.listConversationModes(any(ConversationModesParams.class)))
.thenReturn(CompletableFuture.completedFuture(new ConversationMode[] {
null, custom, unknown, ask, debugger }));

List<BuiltInChatMode> modes = BuiltInChatModeManager.INSTANCE.loadBuiltInModes(mockConnection).join();

assertEquals(List.of("ask", "debugger"), modes.stream().map(BuiltInChatMode::getId).toList());
}

@Test
void testLoadBuiltInModes_nullConnectionReturnsEmptyList() {
assertEquals(List.of(), BuiltInChatModeManager.INSTANCE.loadBuiltInModes(null).join());
}

@Test
void testLoadBuiltInModes_conversionFailureSkipsMode() {
ConversationMode invalid = mock(ConversationMode.class);
when(invalid.isBuiltIn()).thenReturn(true);
when(invalid.getName()).thenReturn("Ask");
when(invalid.getId()).thenReturn("invalid");
when(invalid.getCustomTools()).thenThrow(new IllegalArgumentException("invalid tools"));
ConversationMode agent = createBuiltInMode("agent", "Agent", "Agent", "Agent mode");
when(mockConnection.listConversationModes(any(ConversationModesParams.class)))
.thenReturn(CompletableFuture.completedFuture(new ConversationMode[] { invalid, agent }));

List<BuiltInChatMode> modes = BuiltInChatModeManager.INSTANCE.loadBuiltInModes(mockConnection).join();

assertEquals(List.of("agent"), modes.stream().map(BuiltInChatMode::getId).toList());
}

@Test
void testLoadBuiltInModes_rpcFailurePropagates() {
IllegalStateException failure = new IllegalStateException("RPC failed");
when(mockConnection.listConversationModes(any(ConversationModesParams.class)))
.thenReturn(CompletableFuture.failedFuture(failure));

CompletionException exception = assertThrows(CompletionException.class,
() -> BuiltInChatModeManager.INSTANCE.loadBuiltInModes(mockConnection).join());

assertSame(failure, exception.getCause());
}

private ConversationMode createBuiltInMode(String id, String name, String kind, String description) {
ConversationMode mode = new ConversationMode();
mode.setId(id);
mode.setName(name);
mode.setKind(kind);
mode.setBuiltIn(true);
mode.setDescription(description);
return mode;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,70 @@
package com.microsoft.copilot.eclipse.core.chat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CompletableFuture;

import com.microsoft.copilot.eclipse.core.chat.service.BuiltInChatModeService;
import com.microsoft.copilot.eclipse.core.CopilotCore;
import com.microsoft.copilot.eclipse.core.lsp.CopilotLanguageServerConnection;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationMode;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationModesParams;

/**
* Singleton manager for built-in chat modes. Built-in modes are loaded once from the LSP API at startup.
* Shared snapshot of built-in chat modes discovered asynchronously by the chat lifecycle.
*/
public enum BuiltInChatModeManager {
INSTANCE;

private final BuiltInChatModeService service;
private List<BuiltInChatMode> builtInModes;
private static final List<String> ALLOWED_BUILTIN_NAMES = Arrays.asList(BuiltInChatMode.ASK_MODE_NAME,
BuiltInChatMode.AGENT_MODE_NAME, BuiltInChatMode.PLAN_MODE_NAME, BuiltInChatMode.DEBUGGER_MODE_NAME);

BuiltInChatModeManager() {
this.service = new BuiltInChatModeService();
this.builtInModes = new CopyOnWriteArrayList<>();
loadModesSync();
private volatile List<BuiltInChatMode> builtInModes = List.of();

/**
* Loads built-in modes using the owning chat lifecycle's language-server connection. The LSP requires workspace
* folders even though built-in modes do not depend on workspace context.
*
* @param lsConnection the connection used by the chat services
* @return the discovered built-in modes
*/
public CompletableFuture<List<BuiltInChatMode>> loadBuiltInModes(CopilotLanguageServerConnection lsConnection) {
if (lsConnection == null) {
return CompletableFuture.completedFuture(new ArrayList<>());
}
ConversationModesParams params = new ConversationModesParams(Collections.emptyList());

return lsConnection.listConversationModes(params).thenApply(conversationModes -> {
List<BuiltInChatMode> modes = new ArrayList<>();

for (ConversationMode mode : conversationModes) {
if (mode == null || !mode.isBuiltIn()) {
continue;
}
// Exclude InlineAgent kind — it is not a user-facing chat mode
if (BuiltInChatMode.INLINE_AGENT_KIND.equalsIgnoreCase(mode.getKind())) {
continue;
}
// Filter to only allowed built-in modes by name (case-insensitive)
if (ALLOWED_BUILTIN_NAMES.stream().anyMatch(name -> name.equalsIgnoreCase(mode.getName()))) {
BuiltInChatMode builtIn = convertToBuiltInChatMode(mode);
if (builtIn != null) {
modes.add(builtIn);
}
}
}

return modes;
});
}

private void loadModesSync() {
private BuiltInChatMode convertToBuiltInChatMode(ConversationMode mode) {
try {
List<BuiltInChatMode> modes = service.loadBuiltInModes().get();
this.builtInModes = new CopyOnWriteArrayList<>(modes);
return new BuiltInChatMode(mode);
} catch (Exception e) {
// Initialize with empty list on failure
this.builtInModes = new CopyOnWriteArrayList<>();
CopilotCore.LOGGER.error("Failed to convert built-in mode: " + mode.getId(), e);
return null;
}
}

Expand Down Expand Up @@ -60,10 +97,11 @@ public BuiltInChatMode getBuiltInModeById(String id) {
}

/**
* Reloads built-in chat modes from the LSP API. This should be called when the user switches
* to ensure the latest modes are available for the current user context.
* Publishes a completed discovery after its owner has validated the account and lifecycle.
*
* @param modes the modes applicable to the current account
*/
public void reloadModes() {
loadModesSync();
public void updateModes(List<BuiltInChatMode> modes) {
builtInModes = List.copyOf(modes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@


/**
* Preferences per GitHub user. All the getters and setters are synchronized due to that the ChatBaseService holds a
* shared (single) reference to the user preference. synchronized modifies makes sure the update to the instance are
* thread safe.
* Preferences per GitHub user, shared by the chat preference storage. Scalar access is synchronized and model option
* maps use immutable snapshots so readers never observe partially updated maps.
*/
public class UserPreference {

Expand Down

This file was deleted.

Loading
Loading