-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5978 from pkriens/issue/refactoring
Work in progress, fixes static imports
- Loading branch information
Showing
23 changed files
with
885 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions
77
bndtools.core/src/org/bndtools/refactor/ai/AIRefactorer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.bndtools.refactor.ai; | ||
|
||
import org.bndtools.refactor.ai.api.OpenAI; | ||
import org.bndtools.refactor.util.BaseRefactorer; | ||
import org.bndtools.refactor.util.Cursor; | ||
import org.bndtools.refactor.util.ProposalBuilder; | ||
import org.bndtools.refactor.util.RefactorAssistant; | ||
import org.eclipse.core.runtime.jobs.Job; | ||
import org.eclipse.jdt.core.ICompilationUnit; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.Name; | ||
import org.eclipse.jdt.ui.text.java.IInvocationContext; | ||
import org.eclipse.jdt.ui.text.java.IQuickFixProcessor; | ||
import org.eclipse.swt.widgets.Shell; | ||
import org.eclipse.text.edits.ReplaceEdit; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
@Component | ||
public class AIRefactorer extends BaseRefactorer implements IQuickFixProcessor { | ||
|
||
final OpenAI openai; | ||
|
||
@Activate | ||
public AIRefactorer(@Reference | ||
OpenAI openai) { | ||
this.openai = openai; | ||
} | ||
|
||
@Override | ||
public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant, Cursor<?> target, | ||
IInvocationContext context) { | ||
|
||
try { | ||
int start = context.getSelectionOffset(); | ||
int end = start + context.getSelectionLength(); | ||
if (start == end) { | ||
ASTNode covered = context.getCoveredNode(); | ||
if (covered == null) { | ||
covered = context.getCoveringNode(); | ||
} | ||
while (covered instanceof Name) { | ||
covered = covered.getParent(); | ||
} | ||
if (covered == null) | ||
return; | ||
|
||
start = covered.getStartPosition(); | ||
end = start + covered.getLength(); | ||
} | ||
String completeSource = assistant.getSource(); | ||
String source = assistant.getSource() | ||
.substring(start, end); | ||
|
||
final int begin = start, l = end - start; | ||
builder.build("ai", "Open AI", "openai", -10, () -> { | ||
OpenAIDialog dialog = new OpenAIDialog(new Shell(), source, openai, s -> { | ||
Job job = Job.create("update text buffer", mon -> { | ||
ICompilationUnit compilationUnit = context.getCompilationUnit(); | ||
if (compilationUnit.getSource() | ||
.equals(completeSource)) { | ||
ReplaceEdit re = new ReplaceEdit(begin, l, s); | ||
compilationUnit.applyTextEdit(re, null); | ||
} | ||
}); | ||
job.schedule(); | ||
}); | ||
dialog.open(); | ||
}); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
// ok no proposal | ||
} | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
bndtools.core/src/org/bndtools/refactor/ai/ChatImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package org.bndtools.refactor.ai; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.bndtools.refactor.ai.api.Chat; | ||
import org.bndtools.refactor.ai.api.OpenAI.Configuration; | ||
import org.bndtools.refactor.ai.api.Reply; | ||
import org.osgi.dto.DTO; | ||
|
||
|
||
class ChatImpl implements Chat { | ||
final static String CHAT_URL = "https://api.openai.com/v1/chat/completions"; | ||
|
||
final OpenAIProvider openAIProvider; | ||
|
||
ChatRequest request = new ChatRequest(); | ||
|
||
final Configuration configuration; | ||
|
||
public static class Message extends DTO { | ||
public String role; | ||
public String content; | ||
} | ||
|
||
public static class ChatRequest extends DTO { | ||
public String model = "gpt-3.5-turbo-1106"; | ||
public List<Message> messages = new ArrayList<>(); | ||
public double temperature = 0.1; | ||
public double top_p = 1; | ||
public int n = 1; | ||
public boolean stream = false; | ||
public String stop; | ||
public Integer max_tokens = null; | ||
public double presence_penalty = 0D; | ||
public double frequency_penalty = 0D; | ||
public Map<String, Double> logit_bias = new HashMap<>(); | ||
public String user = "bndtools"; | ||
} | ||
|
||
public static class ChatUsage extends DTO { | ||
public int prompt_tokens; | ||
public int completion_tokens; | ||
public int total_tokens; | ||
} | ||
|
||
public static class ChatChoice extends DTO { | ||
public Message message; | ||
public String finish_reason; | ||
public int index; | ||
} | ||
|
||
public static class ChatResponse extends DTO { | ||
public String id; | ||
public String object; | ||
public long created; | ||
public String model; | ||
public ChatUsage usage; | ||
public List<ChatChoice> choices; | ||
|
||
} | ||
|
||
ChatImpl(OpenAIProvider openAIProvider, Configuration configuration) { | ||
this.configuration = configuration; | ||
this.openAIProvider = openAIProvider; | ||
} | ||
|
||
@Override | ||
public Reply ask(String question) { | ||
Message m = new Message(); | ||
m.role = "user"; | ||
m.content = question; | ||
if (configuration.model != null) | ||
request.model = configuration.model; | ||
request.messages.add(m); | ||
ChatResponse chatResponse = openAIProvider.get(CHAT_URL, request, ChatResponse.class); | ||
|
||
Reply reply = new Reply(); | ||
reply.reply = chatResponse.choices.get(0).message.content; | ||
return reply; | ||
} | ||
|
||
@Override | ||
public void system(String command) { | ||
Message m = new Message(); | ||
m.role = "system"; | ||
m.content = command; | ||
request.messages.add(m); | ||
} | ||
|
||
@Override | ||
public void assistant(String command) { | ||
Message m = new Message(); | ||
m.role = "assistant"; | ||
m.content = command; | ||
request.messages.add(m); | ||
} | ||
@Override | ||
public void model(String model) { | ||
request.model = model; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
request.messages.clear(); | ||
} | ||
|
||
@Override | ||
public void clear(String role) { | ||
request.messages.removeIf(m -> m.role.equals(role)); | ||
} | ||
|
||
@Override | ||
public void setProlog(String string) { | ||
request.messages.removeIf( m -> m.role.equals("system")); | ||
system(string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.bndtools.refactor.ai; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.eclipse.compare.IEditableContent; | ||
import org.eclipse.compare.IStreamContentAccessor; | ||
import org.eclipse.compare.ITypedElement; | ||
import org.eclipse.swt.graphics.Image; | ||
|
||
public class JavaNode implements ITypedElement, IStreamContentAccessor, IEditableContent { | ||
private String name; | ||
private String content; | ||
|
||
public JavaNode(String name, String content) { | ||
this.name = name; | ||
this.content = content; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public Image getImage() { | ||
return null; // You can return an appropriate image for Java files here | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return "java"; // Return the type of the node, in this case, Java | ||
} | ||
|
||
@Override | ||
public InputStream getContents() { | ||
return new ByteArrayInputStream(content.getBytes()); | ||
} | ||
|
||
@Override | ||
public boolean isEditable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setContent(byte[] newContent) { | ||
content = new String(newContent, StandardCharsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public ITypedElement replace(ITypedElement dest, ITypedElement src) { | ||
return null; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
bndtools.core/src/org/bndtools/refactor/ai/OpenAIComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.bndtools.refactor.ai; | ||
|
||
import org.bndtools.refactor.ai.api.OpenAI; | ||
import org.osgi.framework.ServiceRegistration; | ||
import org.osgi.service.component.ComponentContext; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import aQute.bnd.build.Workspace; | ||
import aQute.bnd.http.HttpClient; | ||
import aQute.lib.settings.Settings; | ||
import aQute.lib.strings.Strings; | ||
|
||
@Component | ||
public class OpenAIComponent { | ||
final static Settings settings = new Settings(); | ||
final OpenAIProvider provider; | ||
final Workspace workspace; | ||
final ServiceRegistration<OpenAI> registerService; | ||
|
||
@Activate | ||
public OpenAIComponent(@Reference | ||
Workspace workspace, ComponentContext context) { | ||
this.workspace = workspace; | ||
HttpClient client = workspace.getPlugin(HttpClient.class); | ||
if (client != null) { | ||
String apiKey = settings.getOrDefault("openai.apikey", System.getProperty("OPENAI_APIKEY")); | ||
String models = settings.getOrDefault("openai.models", null); | ||
if (apiKey != null) { | ||
provider = new OpenAIProvider(client, apiKey, models == null ? null : Strings.split(models)); | ||
registerService = context.getBundleContext() | ||
.registerService(OpenAI.class, provider, null); | ||
return; | ||
} | ||
} | ||
provider = null; | ||
registerService = null; | ||
} | ||
} |
Oops, something went wrong.