Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ repo
.claude/

# Fabric version backup
FabricVersion/
FabricVersion/

# External libs
libs/

# Logs
*.log
46 changes: 44 additions & 2 deletions src/main/java/com/opencode/minecraft/client/OpenCodeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class OpenCodeClient {

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private volatile boolean initialized = false;
private volatile java.util.function.Consumer<String> guiMessageListener = null;
private volatile Runnable guiResponseCompleteListener = null;

public OpenCodeClient(ModConfig config, PauseController pauseController) {
this.config = config;
Expand Down Expand Up @@ -99,6 +101,10 @@ private void handleEvent(SseEvent event) {
if ("idle".equals(statusType)) {
sessionManager.onSessionIdle();
messageRenderer.sendSystemMessage("Ready for input");
// Notify GUI that response is complete
if (guiResponseCompleteListener != null) {
guiResponseCompleteListener.run();
}
} else if ("busy".equals(statusType)) {
sessionManager.onSessionBusy();
messageRenderer.sendSystemMessage("Processing...");
Expand All @@ -108,7 +114,8 @@ private void handleEvent(SseEvent event) {
handlePartUpdated(event);
}
case "message.created" -> {
messageRenderer.startNewMessage();
// Don't clutter chat with message creation events
// messageRenderer.startNewMessage();
}
case "session.error" -> {
messageRenderer.sendErrorMessage("Session error occurred");
Expand Down Expand Up @@ -138,7 +145,13 @@ private void handlePartUpdated(SseEvent event) {
pauseController.onDeltaReceived();
String delta = event.getDelta();
if (delta != null && !delta.isEmpty()) {
messageRenderer.appendDelta(delta);
// Don't send AI text to chat - only show in GUI
// messageRenderer.appendDelta(delta);

// Notify GUI listener if present
if (guiMessageListener != null) {
guiMessageListener.accept(delta);
}
}
}
}
Expand Down Expand Up @@ -267,6 +280,35 @@ public SessionStatus getStatus() {
return sessionManager.getStatus();
}

/**
* Gets the message history for a session
*/
public CompletableFuture<com.google.gson.JsonArray> getSessionMessages(String sessionId) {
return httpClient.getSessionMessages(sessionId);
}

/**
* Sets a listener for real-time message updates (for GUI)
*/
public void setGuiMessageListener(java.util.function.Consumer<String> listener) {
this.guiMessageListener = listener;
}

/**
* Sets a listener for when a response completes (for GUI)
*/
public void setGuiResponseCompleteListener(Runnable listener) {
this.guiResponseCompleteListener = listener;
}

/**
* Removes the GUI message listeners
*/
public void clearGuiMessageListener() {
this.guiMessageListener = null;
this.guiResponseCompleteListener = null;
}

/**
* Returns true if connected and initialized
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ public CompletableFuture<Boolean> checkHealth() {
public CompletableFuture<SessionInfo> createSession() {
JsonObject body = new JsonObject();

// Don't send x-opencode-directory header - let OpenCode use its default directory
// (The mod's config directory contains a config file that OpenCode doesn't understand)
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/session"))
.header("Content-Type", "application/json")
.header("x-opencode-directory", directory)
.timeout(Duration.ofSeconds(10))
.POST(HttpRequest.BodyPublishers.ofString(body.toString()))
.build();
Expand All @@ -99,7 +100,6 @@ public CompletableFuture<SessionInfo> createSession() {
public CompletableFuture<List<SessionInfo>> listSessions() {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/session"))
.header("x-opencode-directory", directory)
.timeout(Duration.ofSeconds(10))
.GET()
.build();
Expand All @@ -124,7 +124,6 @@ public CompletableFuture<List<SessionInfo>> listSessions() {
public CompletableFuture<SessionInfo> getSession(String sessionId) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/session/" + sessionId))
.header("x-opencode-directory", directory)
.timeout(Duration.ofSeconds(10))
.GET()
.build();
Expand Down Expand Up @@ -188,7 +187,6 @@ public CompletableFuture<String> sendPrompt(String sessionId, String text) {
public CompletableFuture<Void> abortSession(String sessionId) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/session/" + sessionId + "/abort"))
.header("x-opencode-directory", directory)
.timeout(Duration.ofSeconds(10))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
Expand All @@ -201,6 +199,30 @@ public CompletableFuture<Void> abortSession(String sessionId) {
});
}

/**
* Gets the message history for a session
*/
public CompletableFuture<JsonArray> getSessionMessages(String sessionId) {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/session/" + sessionId + "/message"))
.timeout(Duration.ofSeconds(10))
.GET()
.build();

return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(response -> {
if (response.statusCode() != 200) {
OpenCodeMod.LOGGER.warn("Failed to get messages: {}", response.statusCode());
return new JsonArray();
}
return JsonParser.parseString(response.body()).getAsJsonArray();
})
.exceptionally(e -> {
OpenCodeMod.LOGGER.warn("Failed to get messages: {}", e.getMessage());
return new JsonArray();
});
}

/**
* Subscribes to the global event stream (SSE)
*/
Expand All @@ -222,7 +244,6 @@ private void runSseLoop() {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl + "/global/event"))
.header("Accept", "text/event-stream")
.header("x-opencode-directory", directory)
.GET()
.build();

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/opencode/minecraft/command/OpenCodeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.opencode.minecraft.OpenCodeMod;
import com.opencode.minecraft.client.OpenCodeClient;
import com.opencode.minecraft.client.session.SessionInfo;
import com.opencode.minecraft.gui.OpenCodeGuiScreen;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
Expand All @@ -20,6 +22,7 @@
* Commands:
* - /oc - Show help
* - /oc help - Show help
* - /oc gui - Open ModernUI GUI interface
* - /oc <prompt> - Send a prompt to OpenCode
* - /oc status - Show connection and session status
* - /oc session new - Create a new session
Expand All @@ -42,6 +45,10 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(Commands.literal("help")
.executes(OpenCodeCommand::executeHelp))

// /oc gui
.then(Commands.literal("gui")
.executes(OpenCodeCommand::executeGui))

// /oc status
.then(Commands.literal("status")
.executes(OpenCodeCommand::executeStatus))
Expand Down Expand Up @@ -93,6 +100,8 @@ private static int executeHelp(CommandContext<CommandSourceStack> context) {
source.sendSystemMessage(Component.literal("=== OpenCode Commands ===").withStyle(ChatFormatting.AQUA, ChatFormatting.BOLD));
source.sendSystemMessage(Component.literal("/oc <prompt>").withStyle(ChatFormatting.GREEN)
.append(Component.literal(" - Send a prompt").withStyle(ChatFormatting.GRAY)));
source.sendSystemMessage(Component.literal("/oc gui").withStyle(ChatFormatting.GREEN)
.append(Component.literal(" - Open GUI interface").withStyle(ChatFormatting.GRAY)));
source.sendSystemMessage(Component.literal("/oc status").withStyle(ChatFormatting.GREEN)
.append(Component.literal(" - Show status").withStyle(ChatFormatting.GRAY)));
source.sendSystemMessage(Component.literal("/oc session new").withStyle(ChatFormatting.GREEN)
Expand All @@ -111,6 +120,14 @@ private static int executeHelp(CommandContext<CommandSourceStack> context) {
return 1;
}

private static int executeGui(CommandContext<CommandSourceStack> context) {
// Open terminal GUI on client thread
Minecraft.getInstance().execute(() -> {
Minecraft.getInstance().setScreen(new OpenCodeGuiScreen());
});
return 1;
}

private static int executeStatus(CommandContext<CommandSourceStack> context) {
CommandSourceStack source = context.getSource();
OpenCodeClient client = OpenCodeMod.getClient();
Expand Down
Loading