Skip to content

Commit a4f3c7f

Browse files
authored
Merge pull request #27 from The-Nefarious-Developer/fix-context
Fix editor context retrieval due to async chat operation
2 parents aba7a8a + f16e22b commit a4f3c7f

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

com.developer.nefarious.zjoule.plugin/src/com/developer/nefarious/zjoule/plugin/chat/ChatOrchestrator.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7-
import com.developer.nefarious.zjoule.plugin.chat.utils.EditorContentReader;
87
import com.developer.nefarious.zjoule.plugin.models.Role;
98

109
/**
@@ -18,8 +17,7 @@ public class ChatOrchestrator implements IChatOrchestrator {
1817
* {@inheritDoc}
1918
*/
2019
@Override
21-
public String getAnswer(final String userPrompt) {
22-
20+
public String getAnswer(final String userPrompt, final String editorContent) {
2321
List<IChatMessage> messages = new ArrayList<>();
2422

2523
// 0. Define which AI Client should be used
@@ -32,10 +30,8 @@ public String getAnswer(final String userPrompt) {
3230
List<IChatMessage> messageHistory = aiClient.getMessageHistory();
3331
messages.addAll(messageHistory);
3432

35-
// 2. Get content of the active window
33+
// 2. Get context
3634
String baseInstructions = Instruction.getMessage();
37-
String editorContent = EditorContentReader.readActiveEditorContent();
38-
3935
String systemInstructions = editorContent != null
4036
? baseInstructions + " Consider the following code as context: " + editorContent
4137
: baseInstructions;

com.developer.nefarious.zjoule.plugin/src/com/developer/nefarious/zjoule/plugin/chat/IChatOrchestrator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ public interface IChatOrchestrator {
1919
* <li>Returning the response message as a string.</li>
2020
* </ol>
2121
*
22-
* @param userPrompt the prompt or query provided by the user.
22+
* @param userPrompt the prompt or query provided by the user
23+
* @param editorContent the editor content to be provided as context
2324
* @return the AI-generated response as a {@link String}.
2425
*/
25-
String getAnswer(final String userPrompt);
26+
String getAnswer(final String userPrompt, final String editorContent);
2627

2728
}

com.developer.nefarious.zjoule.plugin/src/com/developer/nefarious/zjoule/plugin/core/functions/PromptHandler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.eclipse.swt.widgets.Display;
88

99
import com.developer.nefarious.zjoule.plugin.chat.ChatOrchestrator;
10+
import com.developer.nefarious.zjoule.plugin.chat.utils.EditorContentReader;
1011

1112
/**
1213
* Handles user prompts from the browser and communicates with the chat orchestrator to generate responses.
@@ -49,9 +50,9 @@ public PromptHandler(final Browser browser, final String name) {
4950
* Handles a user prompt from the browser and initiates asynchronous processing to generate an AI response.
5051
*
5152
* <p>This method is invoked via the {@link BrowserFunction} interface when a corresponding JavaScript
52-
* function is called in the browser widget. It processes the first argument as a user prompt, retrieves
53-
* an AI-generated response asynchronously using the {@link ChatOrchestrator}, and delivers the response
54-
* back to the browser environment through the JavaScript function {@code receiveMessage}.</p>
53+
* function is called in the browser widget. It processes the first argument as a user prompt, read the editor
54+
* content, retrieves an AI-generated response asynchronously using the {@link ChatOrchestrator}, and delivers
55+
* the response back to the browser environment through the JavaScript function {@code receiveMessage}.</p>
5556
*
5657
* <p>The response is escaped to ensure it is safe for inclusion in JavaScript code, avoiding issues
5758
* caused by special characters.</p>
@@ -65,8 +66,10 @@ public PromptHandler(final Browser browser, final String name) {
6566
@Override
6667
public Object function(final Object[] arguments) {
6768
String userPrompt = arguments[0].toString();
69+
String editorContent = EditorContentReader.readActiveEditorContent();
6870

69-
CompletableFuture<String> futureResponse = CompletableFuture.supplyAsync(() -> chatOrchestrator.getAnswer(userPrompt));
71+
CompletableFuture<String> futureResponse = CompletableFuture.supplyAsync(
72+
() -> chatOrchestrator.getAnswer(userPrompt, editorContent));
7073

7174
futureResponse.thenAccept(response -> {
7275
Display.getDefault().asyncExec(new Runnable() {

com.developer.nefarious.zjoule.test/src/com/developer/nefarious/zjoule/test/chat/ChatOrchestratorTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ public void shouldNotStopIfThereIsNoMessageHistory() throws IOException, Interru
6868

6969
List<IChatMessage> mockMessageHistory = new ArrayList<>();
7070
when(mockAIClient.getMessageHistory()).thenReturn(mockMessageHistory);
71-
72-
mockedStaticEditorContentReader.when(EditorContentReader::readActiveEditorContent).thenReturn(null);
71+
72+
String mockEditorContent = null;
73+
mockedStaticEditorContentReader.when(EditorContentReader::readActiveEditorContent).thenReturn(mockEditorContent);
7374

7475
IChatMessage mockSystemMessage = mock(IChatMessage.class);
7576
when(mockAIClient.createMessage(Role.SYSTEM, mockBaseInstructions)).thenReturn(mockSystemMessage);
@@ -84,7 +85,7 @@ public void shouldNotStopIfThereIsNoMessageHistory() throws IOException, Interru
8485
when(mockAnswer.getMessage()).thenReturn(expectedValue);
8586

8687
// Act
87-
String returnValue = cut.getAnswer(mockUserPrompt);
88+
String returnValue = cut.getAnswer(mockUserPrompt, mockEditorContent);
8889

8990
// Assert
9091
verify(mockAIClient).setMessageHistory(mockAllMessages);
@@ -125,7 +126,7 @@ public void shouldPlumbAnswer() throws IOException, InterruptedException {
125126
when(mockAnswer.getMessage()).thenReturn(expectedValue);
126127

127128
// Act
128-
String returnValue = cut.getAnswer(mockUserPrompt);
129+
String returnValue = cut.getAnswer(mockUserPrompt, mockEditorContent);
129130

130131
// Assert
131132
verify(mockAIClient).setMessageHistory(mockAllMessages);
@@ -145,7 +146,8 @@ public void shouldPlumbErrorMessageIfChatCompletionDoesntWork() throws IOExcepti
145146
List<IChatMessage> mockMessageHistory = Arrays.asList(mockOldMessage);
146147
when(mockAIClient.getMessageHistory()).thenReturn(mockMessageHistory);
147148

148-
mockedStaticEditorContentReader.when(EditorContentReader::readActiveEditorContent).thenReturn(null);
149+
String mockEditorContent = null;
150+
mockedStaticEditorContentReader.when(EditorContentReader::readActiveEditorContent).thenReturn(mockEditorContent);
149151

150152
IChatMessage mockSystemMessage = mock(IChatMessage.class);
151153
when(mockAIClient.createMessage(Role.SYSTEM, mockBaseInstructions)).thenReturn(mockSystemMessage);
@@ -156,7 +158,7 @@ public void shouldPlumbErrorMessageIfChatCompletionDoesntWork() throws IOExcepti
156158
String expectedValue = "Error during the AI request execution";
157159

158160
// Act
159-
String returnValue = cut.getAnswer(mockUserPrompt);
161+
String returnValue = cut.getAnswer(mockUserPrompt, mockEditorContent);
160162

161163
// Assert
162164
verify(mockAIClient, never()).setMessageHistory(any());
@@ -170,11 +172,14 @@ public void shouldReturnErrorIfModelIsIncompatible() {
170172
String expectedValue = "The model you have selected is incompatible with the current "
171173
+ "operation. Please verify the model's configuration or choose a "
172174
+ "compatible alternative.";
175+
176+
String mockEditorContent = null;
177+
mockedStaticEditorContentReader.when(EditorContentReader::readActiveEditorContent).thenReturn(mockEditorContent);
173178

174179
mockedStaticAIClientFactory.when(AIClientFactory::getClient).thenReturn(null);
175180

176181
// Act
177-
String returnValue = cut.getAnswer(mockUserPrompt);
182+
String returnValue = cut.getAnswer(mockUserPrompt, mockEditorContent);
178183

179184
// Assert
180185
assertEquals(expectedValue, returnValue);

0 commit comments

Comments
 (0)