Skip to content
Merged
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,16 +4,73 @@
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.CompletableFuture;

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;

/**
* Shared snapshot of built-in chat modes discovered asynchronously by the chat lifecycle.
*/
public enum BuiltInChatModeManager {
INSTANCE;

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);

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 BuiltInChatMode convertToBuiltInChatMode(ConversationMode mode) {
try {
return new BuiltInChatMode(mode);
} catch (Exception e) {
CopilotCore.LOGGER.error("Failed to convert built-in mode: " + mode.getId(), e);
return null;
}
}

public List<BuiltInChatMode> getBuiltInModes() {
return new ArrayList<>(builtInModes);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.microsoft.copilot.eclipse.core.chat.CustomChatModeManager;
import com.microsoft.copilot.eclipse.core.chat.InputNavigation;
import com.microsoft.copilot.eclipse.core.chat.UserPreference;
import com.microsoft.copilot.eclipse.core.chat.service.BuiltInChatModeService;
import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants;
import com.microsoft.copilot.eclipse.core.lsp.CopilotLanguageServerConnection;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ChatMode;
Expand Down Expand Up @@ -145,7 +144,7 @@ private void reloadBuiltInModes() {
String account = authStatusManager.getUserName();
modeDiscoveryState.setValue(ModeDiscoveryState.LOADING);
CompletableFuture<List<BuiltInChatMode>> discovery = CompletableFuture
.supplyAsync(() -> new BuiltInChatModeService().loadBuiltInModes(lsConnection))
.supplyAsync(() -> BuiltInChatModeManager.INSTANCE.loadBuiltInModes(lsConnection))
.thenCompose(result -> result);
modeDiscovery = discovery;
discovery.thenAccept(modes -> ensureRealm(() -> {
Expand Down