Skip to content

Commit

Permalink
py4j.py a wip but other stuff worky
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Sep 7, 2023
1 parent 419402d commit 5ef82fa
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 34 deletions.
22 changes: 22 additions & 0 deletions src/main/java/org/myrobotlab/framework/interfaces/JsonInvoker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.myrobotlab.framework.interfaces;

import org.myrobotlab.framework.Message;

public interface JsonInvoker {

/**
* No parameter method
* @param method
* @return
*/
public Object invoke(String method);

/**
* Encoded parameters as a JSON String (encoded once!)
* @param method
* @param encodedParameters
* @return
*/
public Object invoke(String method, String encodedParameters);

}
12 changes: 12 additions & 0 deletions src/main/java/org/myrobotlab/framework/interfaces/JsonSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.myrobotlab.framework.interfaces;

public interface JsonSender {

/**
* Send interface which takes a json encoded Message.
* For schema look at org.myrobotlab.framework.Message
* @param jsonEncodedMessage
*/
public void send(String jsonEncodedMessage);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.myrobotlab.framework.Message;
import org.myrobotlab.framework.TimeoutException;

public interface MessageSender extends NameProvider {
public interface MessageSender extends NameProvider, SimpleMessageSender {

/**
* Send invoking messages to remote location to invoke {name} instance's
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.myrobotlab.framework.interfaces;

import org.myrobotlab.framework.Message;

public interface SimpleMessageSender {

public void send(Message msg);

}
6 changes: 2 additions & 4 deletions src/main/java/org/myrobotlab/service/Gpt3.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ public Response getResponse(String text) {
@SuppressWarnings({ "unchecked", "rawtypes" })
Map<String, Object> textObject = (Map) choices.get(0);
responseText = (String) textObject.get("text");
if (responseText != null) {
// /completions
invoke("publishText", responseText);
} else {
if (responseText == null) {
// /chat/completions
@SuppressWarnings({ "unchecked", "rawtypes" })
Map<String, Object> content = (Map)textObject.get("message");
Expand Down Expand Up @@ -156,6 +153,7 @@ public Response getResponse(String text) {
if (responseText != null && responseText.length() > 0) {
invoke("publishUtterance", utterance);
invoke("publishResponse", response);
invoke("publishText", responseText);
}

return response;
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/myrobotlab/service/InMoov2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,15 @@ public FiniteStateMachine.StateChange onStateChange(FiniteStateMachine.StateChan
log.error("onStateChange {}", stateChange);

String state = stateChange.current;
systemEvent("ON STATE %s", state);

// getPeer("py4j") ?
// Py4j py4j
// String code = getName()".onStateChange("
// invoke("publishPython", "onStateChange", stateChange );

if (config.systemEventStateChange) {
systemEvent("ON STATE %s", state);
}

if (config.customSounds && customSoundMap.containsKey(state)) {
invoke("publishPlayAudioFile", customSoundMap.get(state));
Expand Down Expand Up @@ -1167,6 +1175,10 @@ public FiniteStateMachine.StateChange onStateChange(FiniteStateMachine.StateChan
}
return stateChange;
}

// public Message publishPython(String method, Object...data) {
// return Message.createMessage(getName(), getName(), method, data);
// }

public OpenCVData onOpenCVData(OpenCVData data) {
// FIXME - publish event with or without data ? String file reference
Expand Down
62 changes: 48 additions & 14 deletions src/main/java/org/myrobotlab/service/Py4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bytedeco.javacpp.Loader;
import org.myrobotlab.codec.CodecUtils;
Expand All @@ -21,9 +20,11 @@
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.myrobotlab.net.Connection;
import org.myrobotlab.service.config.Py4jConfig;
import org.myrobotlab.service.data.Script;
import org.myrobotlab.service.interfaces.Executor;
import org.myrobotlab.service.interfaces.Gateway;
import org.slf4j.Logger;

import py4j.GatewayServer;
Expand Down Expand Up @@ -53,7 +54,7 @@
*
* @author GroG
*/
public class Py4j extends Service<Py4jConfig> implements GatewayServerListener {
public class Py4j extends Service<Py4jConfig> implements GatewayServerListener, Gateway {

/**
* POJO class to tie all the data elements of a external python process
Expand Down Expand Up @@ -234,15 +235,6 @@ private String getClientKey(Py4JServerConnection gatewayConnection) {
return String.format("%s:%d", gatewayConnection.getSocket().getInetAddress(), gatewayConnection.getSocket().getPort());
}

/**
* return a set of client connections - probably could be deprecated to a
* single client, but was not sure
*
* @return
*/
public Set<String> getClients() {
return clients.keySet();
}

/**
* get listing of filesystem files location will be data/Py4j/{serviceName}
Expand Down Expand Up @@ -336,7 +328,19 @@ public boolean preProcessHook(Message msg) {
// TODO - determine clients are connected .. how many clients etc..
try {
if (handler != null) {
handler.invoke(msg.method, msg.data);
// afaik - Py4j does some kind of magical encoding to get a JavaObject
// back to the Python process, but:
// 1. its useless for users - no way to access the content ?
// 2. you can't do anything with it
// So, I've chosen to json encode it here, and the Py4j.py MessageHandler will
// decode it into a Python dictionary \o/
// we do single encoding including the parameter array - there is no header needed
// with method and other details, as the invoke here is invoking directly in the
// Py4j.py script

String json = CodecUtils.toJson(msg);
// handler.invoke(msg.method, json);
handler.send(json);
} else {
error("preProcessHook handler is null");
}
Expand Down Expand Up @@ -586,7 +590,37 @@ public static void main(String[] args) {
log.error("main threw", e);
}
}



@Override
public void connect(String uri) throws Exception {
// host:port of python process running py4j ???

}

/**
* Remote in this context is the remote python process
*/
@Override
public void sendRemote(Message msg) throws Exception {
log.info("sendRemote");
String jsonMsg = CodecUtils.toJson(msg);
handler.send(jsonMsg);
}

@Override
public boolean isLocal(Message msg) {
return Runtime.getInstance().isLocal(msg);
}

@Override
public List<String> getClientIds() {
return Runtime.getInstance().getConnectionUuids(getName());
}

@Override
public Map<String, Connection> getClients() {
return Runtime.getInstance().getConnections(getName());
}

}

13 changes: 12 additions & 1 deletion src/main/java/org/myrobotlab/service/config/InMoov2Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class InMoov2Config extends ServiceConfig {
/**
* enable custom sound map for state changes
*/
public boolean customSound = false;
public boolean customSounds = false;


public boolean forceMicroOnIfSleeping = true;
Expand Down Expand Up @@ -121,6 +121,11 @@ public class InMoov2Config extends ServiceConfig {
*/
public boolean systemEventsOnBoot = false;

/**
* Publish system event when state changes
*/
public boolean systemEventStateChange = true;

/**
*
*/
Expand Down Expand Up @@ -225,6 +230,12 @@ public Plan getDefault(Plan plan, String name) {
chatBot.listeners = new ArrayList<>();
}
chatBot.listeners.add(new Listener("publishText", name + ".htmlFilter", "onText"));


ProgramABConfig gpt3 = (ProgramABConfig) plan.get(getPeerName("gpt3"));
gpt3.listeners = new ArrayList<>();
gpt3.listeners.add(new Listener("publishText", name + ".htmlFilter", "onText"));


HtmlFilterConfig htmlFilter = (HtmlFilterConfig) plan.get(getPeerName("htmlFilter"));
// htmlFilter.textListeners = new String[] { name + ".mouth" };
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/myrobotlab/service/interfaces/Executor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.myrobotlab.service.interfaces;

import org.myrobotlab.framework.interfaces.Invoker;
import org.myrobotlab.framework.interfaces.JsonInvoker;
import org.myrobotlab.framework.interfaces.JsonSender;

/**
* Interface to a Executor - currently only utilized by Py4j to
Expand All @@ -10,7 +11,7 @@
* @author GroG
*
*/
public interface Executor extends Invoker {
public interface Executor extends JsonInvoker, JsonSender {

/**
* exec in Python - executes arbitrary code
Expand Down
Loading

0 comments on commit 5ef82fa

Please sign in to comment.