Skip to content

Commit

Permalink
Merge pull request #5978 from pkriens/issue/refactoring
Browse files Browse the repository at this point in the history
Work in progress, fixes static imports
  • Loading branch information
pkriens authored Jan 20, 2024
2 parents 4a74d2b + 5cf9121 commit bc9ecf9
Show file tree
Hide file tree
Showing 23 changed files with 885 additions and 48 deletions.
6 changes: 6 additions & 0 deletions bndtools.core/_plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@
icon="icons/database.png" id="bndtools.repositoriesView"
name="Repositories" restorable="true">
</view>
<view category="bndtools.viewCategory"
name="OpenAI"
icon="icons/openai.png"
class="org.bndtools.refactor.ai.OpenAIView"
id="bndtools.openAIView">
</view>
</extension>
<extension point="org.eclipse.ui.commands">
<command id="bndtools.core.removeRepoResource"
Expand Down
6 changes: 4 additions & 2 deletions bndtools.core/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Export-Package: org.osgi.service.metatype.annotations
osgi.annotation,\
org.osgi.service.metatype.annotations,\
osgi.core,\
org.osgi.service.component;version='1.3.0',\
org.osgi.service.component.annotations;version='1.3.0',\
org.osgi.service.component;version='1.4.0',\
org.osgi.service.component.annotations;version='1.4.0',\
org.osgi.service.coordinator;version=latest,\
org.osgi.service.metatype;version='1.3.0',\
org.osgi.service.prefs;version=latest,\
Expand All @@ -63,6 +63,8 @@ Export-Package: org.osgi.service.metatype.annotations
org.eclipse.core.runtime,\
org.eclipse.jface,\
org.eclipse.jface.text,\
org.eclipse.compare;version=latest,\
org.eclipse.compare.core;version=latest,\
org.eclipse.core.resources,\
org.eclipse.swt,\
org.eclipse.swt.cocoa.macosx.x86_64,\
Expand Down
7 changes: 7 additions & 0 deletions bndtools.core/icons.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ icons.bundle.new=icons/bundle-new.png
icons.component=icons/component.png
icons.service=icons/service.png
icons.capreq=icons/connections.png

# Refactorings
icons.version-plain=icons/version-plain.png
icons.version-major=icons/version-major.png
icons.version-minor=icons/version-minor.png
icons.aA=icons/lower-to-upper.png
icons.Aa=icons/upper-to-lower.png
icons.block-to-string=icons/block-to-string.png
icons.string-to-block=icons/string-to-block.png
icons.final-field=icons/final-field.png
icons.set-selection=icons/set-selection.png

# Gogo things

Expand Down Expand Up @@ -94,3 +100,4 @@ icons.pin.disabled = icons/pin-disabled.png
icons.remote.install = icons/remote-install.png


icons.openai= icons/openai.png
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 bndtools.core/src/org/bndtools/refactor/ai/AIRefactorer.java
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 bndtools.core/src/org/bndtools/refactor/ai/ChatImpl.java
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);
}
}
54 changes: 54 additions & 0 deletions bndtools.core/src/org/bndtools/refactor/ai/JavaNode.java
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 bndtools.core/src/org/bndtools/refactor/ai/OpenAIComponent.java
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;
}
}
Loading

0 comments on commit bc9ecf9

Please sign in to comment.