Skip to content

Commit

Permalink
feat: back-end refinements
Browse files Browse the repository at this point in the history
- log support
- return nodeoutput json representation
- update front-end distribution

work on #9
  • Loading branch information
bsorrentino committed Jul 16, 2024
1 parent f48618c commit bdec3a3
Show file tree
Hide file tree
Showing 26 changed files with 237 additions and 177 deletions.
18 changes: 18 additions & 0 deletions jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<version>${jetty.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -71,6 +76,19 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -27,6 +30,8 @@

public interface LangGraphStreamingServer {

Logger log = LoggerFactory.getLogger(LangGraphStreamingServer.class);

CompletableFuture<Void> start() throws Exception;

static Builder builder() {
Expand All @@ -51,8 +56,11 @@ public Builder addInputStringArg(String name) {
return this;
}

public <State extends AgentState> LangGraphStreamingServer build(CompiledGraph<State> compiledGraph) {
public <State extends AgentState> LangGraphStreamingServer build(CompiledGraph<State> compiledGraph) throws Exception {

Server server = new Server();


ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);
Expand All @@ -61,7 +69,8 @@ public <State extends AgentState> LangGraphStreamingServer build(CompiledGraph<S

// Path publicResourcesPath = Paths.get("jetty", "src", "main", "webapp");
// Resource baseResource = ResourceFactory.of(resourceHandler).newResource(publicResourcesPath));
Resource baseResource = ResourceFactory.of(resourceHandler).newClassLoaderResource("webapp");
// Resource baseResource = ResourceFactory.of(resourceHandler).newClassLoaderResource(".");
Resource baseResource = ResourceFactory.of(resourceHandler).newResource(".");
resourceHandler.setBaseResource(baseResource);

resourceHandler.setDirAllowed(true);
Expand Down Expand Up @@ -121,17 +130,27 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
try {
compiledGraph.stream(dataMap)
.forEachAsync(s -> {
writer.println(s.node());
writer.flush();

try {

writer.print("{");
writer.printf( "\"node\": \"%s\"", s.node() );
try {
var stateAsString = objectMapper.writeValueAsString(s.state().data());
writer.printf( ",\"state\": %s" , stateAsString );
}
catch( IOException e ) {
LangGraphStreamingServer.log.info("error serializing state", e);
writer.printf( ",\"state\": {}" );
}
writer.print("}");
writer.flush();
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}).thenAccept(v -> {
writer.close();
});

})
.thenAccept(v -> writer.close() );

} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -144,13 +163,20 @@ record ArgumentMetadata (
boolean required
) {}


/**
* return the graph representation in mermaid format
*/
class GraphInitServlet<State extends AgentState> extends HttpServlet {

final CompiledGraph<State> compiledGraph;
final Map<String, ArgumentMetadata> inputArgs;
final ObjectMapper objectMapper = new ObjectMapper();

record Result (
String graph,
Map<String, ArgumentMetadata> args
) {}

public GraphInitServlet(CompiledGraph<State> compiledGraph, Map<String, ArgumentMetadata> inputArgs) {
Objects.requireNonNull(compiledGraph, "compiledGraph cannot be null");
Expand All @@ -160,15 +186,16 @@ public GraphInitServlet(CompiledGraph<State> compiledGraph, Map<String, Argument

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");

GraphRepresentation result = compiledGraph.getGraph(GraphRepresentation.Type.MERMAID);
GraphRepresentation graph = compiledGraph.getGraph(GraphRepresentation.Type.MERMAID);

final Result result = new Result(graph.getContent(), inputArgs);
String resultJson = objectMapper.writeValueAsString(result);
// Start asynchronous processing
request.startAsync();
final PrintWriter writer = response.getWriter();
writer.println(result.getContent());
writer.println(resultJson);
writer.close();
}
}

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions jetty/src/main/webapp/index.371372f8.css.map

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions jetty/src/main/webapp/index.44c76a85.js

This file was deleted.

1 change: 0 additions & 1 deletion jetty/src/main/webapp/index.44c76a85.js.map

This file was deleted.

16 changes: 16 additions & 0 deletions jetty/src/main/webapp/index.4c99d4d4.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bdec3a3

Please sign in to comment.