-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCopilotAppMapEnvProvider.java
More file actions
72 lines (62 loc) · 2.96 KB
/
Copy pathCopilotAppMapEnvProvider.java
File metadata and controls
72 lines (62 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package appland.copilotChat;
import appland.cli.AppLandCliEnvProvider;
import appland.copilotChat.copilot.GitHubCopilotService;
import appland.rpcService.AppLandJsonRpcService;
import appland.settings.AppMapApplicationSettingsService;
import appland.settings.AppMapSecureApplicationSettingsService;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.util.text.StringUtil;
import java.util.HashMap;
import java.util.Map;
/**
* Extends the environment setup of AppMap CLI commands with the values to use this plugin's GitHub Copilot integration.
* <p>
* We're not using an optional plugin dependency to avoid complicating our build setup.
*/
public class CopilotAppMapEnvProvider implements AppLandCliEnvProvider {
@Override
public Map<String, String> getEnvironment() {
if (isDisabled()) {
return Map.of();
}
if (!GitHubCopilotService.getInstance().isCopilotAuthenticated()) {
return Map.of();
}
var userCopilotModel = AppMapApplicationSettingsService.getInstance().getCopilotModelId();
var env = new HashMap<String, String>();
env.put("OPENAI_BASE_URL", NavieCopilotChatRequestHandler.getBaseUrl());
env.put("APPMAP_NAVIE_COMPLETION_BACKEND", "openai");
env.put("OPENAI_API_KEY", GitHubCopilotService.RandomIdeSessionId);
if (StringUtil.isNotEmpty(userCopilotModel)) {
env.put("APPMAP_NAVIE_MODEL", userCopilotModel);
}
return Map.copyOf(env);
}
/**
* @return {@code true} if the integration with GitHub Copilot is unavailable,
* either because the user chose a different LLM in Navie or because the GitHub Copilot plugin is not installed.
* This method must not evaluate the state of Copilot authentication.
*/
public static boolean isDisabled() {
return hasCustomAppMapModelSettings() || isGitHubCopilotDisabled();
}
/**
* @return {@code true} if the user has set custom model settings for Navie for an OpenAI or Azure OpenAI API key.
* Existing settings for a custom key override the Copilot integration.
*/
static boolean hasCustomAppMapModelSettings() {
var environment = AppMapApplicationSettingsService.getInstance().getCliEnvironment();
return AppMapSecureApplicationSettingsService.getInstance().hasOpenAIKey()
|| environment.containsKey(AppLandJsonRpcService.OPENAI_API_KEY)
|| environment.containsKey(AppLandJsonRpcService.OPENAI_BASE_URL)
|| environment.containsKey(AppLandJsonRpcService.AZURE_OPENAI_API_KEY);
}
private static boolean isGitHubCopilotDisabled() {
if (AppMapApplicationSettingsService.getInstance().isCopilotIntegrationDisabled()) {
return true;
}
return PluginManager.getLoadedPlugins()
.stream()
.noneMatch(plugin -> plugin.isEnabled() && plugin.getPluginId().equals(GitHubCopilotService.CopilotPluginId));
}
}