forked from microsoft/copilot-for-eclipse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationUtils.java
More file actions
154 lines (133 loc) · 4.7 KB
/
Copy pathConversationUtils.java
File metadata and controls
154 lines (133 loc) · 4.7 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package com.microsoft.copilot.eclipse.ui.chat;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.ui.PlatformUI;
import com.microsoft.copilot.eclipse.ui.CopilotUi;
import com.microsoft.copilot.eclipse.ui.chat.tools.FileToolService;
/**
* Utility class for conversation-related operations.
*/
public class ConversationUtils {
/**
* Dialog type for end chat confirmation.
*/
private enum DialogType {
END_CHAT(Messages.endChat_confirmationTitle, Messages.endChat_confirmationMessage);
private final String title;
private final String message;
DialogType(String title, String message) {
this.title = title;
this.message = message;
}
public String getTitle() {
return title;
}
public String getMessage() {
return message;
}
}
/**
* Dialog result constants for better readability.
*/
private static final int KEEP_CHANGES = 0;
private static final int UNDO_CHANGES = 1;
/**
* Confirm ending the current chat session when there are unhandled file changes. If there are
* no unhandled changes, returns true immediately. If there are unhandled changes, prompts the
* user with options to keep or undo them. Returning false means the user cancelled the
* operation.
*
* @return true if it's ok to proceed with ending the chat session; false if cancelled.
*/
public static boolean confirmEndChat() {
return confirmChatAction(DialogType.END_CHAT);
}
/**
* Common logic for confirming chat actions when there are unhandled file changes.
*
* @param dialogType the type of dialog to show
* @return true if it's ok to proceed; false if cancelled.
*/
private static boolean confirmChatAction(DialogType dialogType) {
FileToolService fileToolService = getFileToolService();
if (fileToolService == null) {
return true; // Fail open if services are not ready or no file service available
}
if (!hasUnhandledChanges(fileToolService)) {
return true;
}
return showConfirmationDialog(dialogType, fileToolService);
}
/**
* Gets the FileToolService instance.
*
* @return the FileToolService, or null if not available
*/
private static FileToolService getFileToolService() {
if (CopilotUi.getPlugin() == null || CopilotUi.getPlugin().getChatServiceManager() == null) {
return null;
}
return CopilotUi.getPlugin().getChatServiceManager().getFileToolService();
}
/**
* Checks if there are any unhandled file changes.
*
* @param fileToolService the file tool service
* @return true if there are unhandled changes
*/
private static boolean hasUnhandledChanges(FileToolService fileToolService) {
return fileToolService.getChangedFiles().size() > 0;
}
/**
* Shows the confirmation dialog and handles the user's response.
*
* @param dialogType the type of dialog to show
* @param fileToolService the file tool service
* @return true if the user chose to proceed; false if cancelled
*/
private static boolean showConfirmationDialog(DialogType dialogType, FileToolService fileToolService) {
int result = MessageDialog.open(
MessageDialog.QUESTION,
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
dialogType.getTitle(),
dialogType.getMessage(),
SWT.NONE,
Messages.confirmDialog_keepChangesButton,
Messages.confirmDialog_undoChangesButton);
return handleDialogResult(result, fileToolService);
}
/**
* Handles the result of the confirmation dialog.
*
* @param result the dialog result
* @param fileToolService the file tool service
* @return true if the user chose a valid option; false if cancelled
*/
private static boolean handleDialogResult(int result, FileToolService fileToolService) {
switch (result) {
case KEEP_CHANGES:
fileToolService.onKeepAllChanges();
return true;
case UNDO_CHANGES:
fileToolService.onUndoAllChanges();
return true;
default:
return false; // Close / Cancel
}
}
/**
* Checks if the given throwable is an instance of cancellation exception, either directly or wrapped in a
* CompletionException.
*
* @param th the exception to check
* @return true if it's a cancellation exception; false otherwise
*/
public static boolean isConversationCancellationThrowable(Throwable th) {
return th instanceof CancellationException
|| (th instanceof CompletionException && th.getCause() instanceof CancellationException);
}
}