forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddByokModelDialog.java
More file actions
252 lines (214 loc) · 8.78 KB
/
Copy pathAddByokModelDialog.java
File metadata and controls
252 lines (214 loc) · 8.78 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.preferences;
import java.util.function.Consumer;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import com.microsoft.copilot.eclipse.core.lsp.protocol.byok.ByokModel;
import com.microsoft.copilot.eclipse.core.lsp.protocol.byok.ByokModelCapabilities;
import com.microsoft.copilot.eclipse.core.lsp.protocol.byok.ByokModelProvider;
import com.microsoft.copilot.eclipse.ui.utils.UiUtils;
/**
* Dialog to add a new BYOK model that adapts based on the provider.
*/
public class AddByokModelDialog extends TrayDialog {
private static final int CONTAINER_WIDTH = 600;
private Text modelIdText;
private Text deploymentUrlText;
private Text apiKeyText;
private Text displayNameText;
private Button supportToolCallingCheck;
private Button supportVisionCheck;
private Button addButton;
private Button toggleEyeBtn;
private Image eyeOpenImg;
private Image eyeClosedImg;
private final String providerName;
private final Consumer<ByokModel> onSave;
/**
* Create the dialog.
*/
public AddByokModelDialog(Shell parentShell, String providerName, Consumer<ByokModel> onSave) {
super(parentShell);
this.providerName = providerName;
this.onSave = onSave;
setShellStyle(getShellStyle() | SWT.RESIZE);
}
@Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText(String.format(Messages.preferences_page_byok_addModel_dialog_title, providerName));
}
@Override
protected Control createDialogArea(Composite parent) {
String helpContextId = getHelpContextId();
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, helpContextId);
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = 20;
layout.marginHeight = 20;
layout.verticalSpacing = 15;
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(layout);
GridData containerGd = new GridData(SWT.FILL, SWT.FILL, true, true);
containerGd.widthHint = CONTAINER_WIDTH;
container.setLayoutData(containerGd);
// Model ID *
new Label(container, SWT.NONE).setText(Messages.preferences_page_byok_addModel_modelId);
modelIdText = new Text(container, SWT.BORDER);
modelIdText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
modelIdText.addModifyListener(this::onFieldChanged);
// Provider-specific fields
if (providerName.equals(ByokModelProvider.AZURE.getDisplayName())) {
createAzureSpecificFields(container);
}
// Display Name (optional for all providers)
new Label(container, SWT.NONE).setText(Messages.preferences_page_byok_addModel_displayName);
displayNameText = new Text(container, SWT.BORDER);
displayNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
// Capabilities
Composite caps = new Composite(container, SWT.NONE);
GridLayout capsLayout = new GridLayout(2, false);
capsLayout.marginWidth = 0;
capsLayout.marginHeight = 0;
caps.setLayout(capsLayout);
GridData capsGd = new GridData(SWT.FILL, SWT.CENTER, true, false);
capsGd.horizontalSpan = 2;
caps.setLayoutData(capsGd);
supportToolCallingCheck = new Button(caps, SWT.CHECK);
supportToolCallingCheck.setText(Messages.preferences_page_byok_addModel_supportToolCalling);
supportToolCallingCheck.setSelection(true);
supportVisionCheck = new Button(caps, SWT.CHECK);
supportVisionCheck.setText(Messages.preferences_page_byok_addModel_supportVision);
supportVisionCheck.setSelection(true);
return container;
}
private void createAzureSpecificFields(Composite container) {
// Deployment URL *
new Label(container, SWT.NONE).setText(Messages.preferences_page_byok_addModel_deploymentUrl);
deploymentUrlText = new Text(container, SWT.BORDER);
deploymentUrlText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
deploymentUrlText.addModifyListener(this::onFieldChanged);
// API Key *
new Label(container, SWT.NONE).setText(Messages.preferences_page_byok_addModel_apiKey);
Composite apiKeyRow = new Composite(container, SWT.NONE);
GridLayout rowLayout = new GridLayout(2, false);
rowLayout.marginWidth = 0;
rowLayout.marginHeight = 0;
apiKeyRow.setLayout(rowLayout);
apiKeyRow.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
apiKeyText = new Text(apiKeyRow, SWT.BORDER);
apiKeyText.setEchoChar('*');
apiKeyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
apiKeyText.addModifyListener(this::onFieldChanged);
eyeOpenImg = UiUtils
.buildImageFromPngPath(UiUtils.isDarkTheme() ? "/icons/chat/eye_dark.png" : "/icons/chat/eye.png");
eyeClosedImg = UiUtils.buildImageFromPngPath(
UiUtils.isDarkTheme() ? "/icons/chat/eye_closed_dark.png" : "/icons/chat/eye_closed.png");
toggleEyeBtn = new Button(apiKeyRow, SWT.FLAT);
toggleEyeBtn.setImage(eyeClosedImg);
toggleEyeBtn.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
toggleEyeBtn.addListener(SWT.Selection, e -> togglePasswordVisibility());
toggleEyeBtn.addDisposeListener(e -> {
if (eyeOpenImg != null && !eyeOpenImg.isDisposed()) {
eyeOpenImg.dispose();
}
if (eyeClosedImg != null && !eyeClosedImg.isDisposed()) {
eyeClosedImg.dispose();
}
});
}
/**
* Get the appropriate help context ID based on the provider.
*/
private String getHelpContextId() {
String base = "com.microsoft.copilot.eclipse.ui.add_byok_model_dialog_";
if (ByokModelProvider.isAzure(providerName)) {
return base + "azure";
}
return base + "openaiCompatible";
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
addButton = createButton(parent, IDialogConstants.OK_ID, Messages.preferences_page_byok_dialog_add, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
// Initially disable add button until required fields are filled
addButton.setEnabled(false);
}
private void onFieldChanged(ModifyEvent e) {
updateAddButtonState();
}
private void updateAddButtonState() {
if (addButton != null && !addButton.isDisposed()) {
boolean canAdd = isValidInput();
addButton.setEnabled(canAdd);
}
}
private boolean isValidInput() {
// Model ID is always required
if (StringUtils.isBlank(modelIdText.getText())) {
return false;
}
if (providerName.equals(ByokModelProvider.AZURE.getDisplayName())) {
if (deploymentUrlText == null || StringUtils.isBlank(deploymentUrlText.getText()) || apiKeyText == null
|| StringUtils.isBlank(apiKeyText.getText())) {
return false;
}
}
return true;
}
private void togglePasswordVisibility() {
if (apiKeyText == null || toggleEyeBtn == null) {
return;
}
if (apiKeyText.getEchoChar() == '*') {
apiKeyText.setEchoChar('\0');
toggleEyeBtn.setImage(eyeOpenImg);
} else {
apiKeyText.setEchoChar('*');
toggleEyeBtn.setImage(eyeClosedImg);
}
}
@Override
protected void okPressed() {
if (onSave != null) {
ByokModel model = buildModel();
onSave.accept(model);
}
super.okPressed();
}
private ByokModel buildModel() {
ByokModel model = new ByokModel();
model.setModelId(modelIdText.getText().trim());
model.setProviderName(providerName);
model.setRegistered(true);
model.setCustomModel(true);
// Set provider-specific fields
if (providerName.equals(ByokModelProvider.AZURE.getDisplayName()) && deploymentUrlText != null
&& apiKeyText != null) {
model.setDeploymentUrl(deploymentUrlText.getText().trim());
model.setApiKey(apiKeyText.getText().trim());
}
// Set capabilities
ByokModelCapabilities capabilities = new ByokModelCapabilities();
String displayedName = displayNameText.getText().trim().isEmpty() ? modelIdText.getText().trim()
: displayNameText.getText().trim();
capabilities.setName(displayedName);
capabilities.setToolCalling(supportToolCallingCheck.getSelection());
capabilities.setVision(supportVisionCheck.getSelection());
model.setModelCapabilities(capabilities);
return model;
}
}