Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI UI: start on port 8092 by default #647

Merged
merged 2 commits into from
Oct 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void debug(Object message) {
}

@CommandLine.Spec protected CommandLine.Model.CommandSpec command;
private final CLILogger logger = new CLILoggerImpl(() -> getRootCmd(), () -> command);
@Getter private final CLILogger logger = new CLILoggerImpl(() -> getRootCmd(), () -> command);
private final GithubRepositoryDownloader githubRepositoryDownloader =
new GithubRepositoryDownloader(
new JGitClient(), logger, LangStreamCLI.getLangstreamCLIHomeDirectory());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package ai.langstream.cli.commands.applications;

import ai.langstream.cli.CLILogger;
import ai.langstream.cli.NamedProfile;
import ai.langstream.cli.api.model.Gateways;
import io.undertow.Handlers;
Expand Down Expand Up @@ -56,6 +57,12 @@ public class UIAppCmd extends BaseApplicationCmd {
@CommandLine.Parameters(description = "Application ID")
private String applicationId;

@CommandLine.Option(
names = {"-p", "--port"},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about "--ui-port" ? "--port" is too generic

Copy link
Member Author

@nicoloboschi nicoloboschi Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this command is literally

apps ui <appid>

I don't think it's confusing

In fact, in the docker run one, I used --ui-port

description = "Port for the local webserver and UI. If 0, a random port will be used. ",
defaultValue = "8092")
private int port = 8092;

@Override
@SneakyThrows
public void run() {
Expand Down Expand Up @@ -91,16 +98,17 @@ public void run(Consumer<String> lineConsumer) {
}
}
};
final Undertow server = startServer(() -> appModel, apiGatewayUrl, logSupplier);
final int port =
((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort();
log("Started UI at http://localhost:" + port);
startServer(port, () -> appModel, apiGatewayUrl, logSupplier, getLogger());
Thread.sleep(Long.MAX_VALUE);
}

@SneakyThrows
public static Undertow startServer(
Supplier<AppModel> appModel, String apiGatewayUrl, LogSupplier logsStream) {
int port,
Supplier<AppModel> appModel,
String apiGatewayUrl,
LogSupplier logsStream,
CLILogger logger) {
String forwardHost;
if (apiGatewayUrl.startsWith("wss://")) {
forwardHost = "https://" + apiGatewayUrl.substring("wss://".length());
Expand Down Expand Up @@ -138,15 +146,15 @@ public static Undertow startServer(
}
};

AtomicInteger port = new AtomicInteger();
AtomicInteger actualPort = new AtomicInteger();

ResourceHandler resourceHandler =
Handlers.resource(
new ClassPathResourceManager(UIAppCmd.class.getClassLoader(), "app-ui"));
HttpHandler appConfigHandler =
exchange -> {
final AppModel result = appModel.get();
result.setBaseUrl("ws://localhost:" + port.get());
result.setBaseUrl("ws://localhost:" + actualPort.get());
final String json = jsonBodyWriter.writeValueAsString(result);
exchange.getResponseHeaders()
.put(HttpString.tryFromString("Content-Type"), "application/json");
Expand All @@ -161,13 +169,15 @@ public static Undertow startServer(

Undertow server =
Undertow.builder()
.addHttpListener(0, "localhost")
.addHttpListener(port, "localhost")
.setHandler(routingHandler)
.build();
server.start();
port.set(((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort());
actualPort.set(
((InetSocketAddress) server.getListenerInfo().get(0).getAddress()).getPort());

openBrowserAtPort("open", port.get());
logger.log("Started UI at http://localhost:" + actualPort.get());
openBrowserAtPort("open", actualPort.get());

return server;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public class LocalRunApplicationCmd extends BaseDockerCmd {
description = "Start the UI")
private boolean startUI = true;

@CommandLine.Option(
names = {"-up", "--ui-port"},
description = "Port for the local webserver and UI. If 0, a random port will be used.",
defaultValue = "8092")
private int uiPort = 8092;

@CommandLine.Option(
names = {"--only-agent"},
description = "Run only one agent")
Expand Down Expand Up @@ -495,6 +501,7 @@ private void startUI(String tenant, String applicationId, Path outputLog, Proces
final String finalBody = body;
final String mermaidDefinition = MermaidAppDiagramGenerator.generate(finalBody);
UIAppCmd.startServer(
uiPort,
() -> {
final UIAppCmd.AppModel appModel = new UIAppCmd.AppModel();
appModel.setTenant(tenant);
Expand All @@ -506,7 +513,8 @@ private void startUI(String tenant, String applicationId, Path outputLog, Proces
return appModel;
},
"ws://localhost:8091",
getTailLogSupplier(outputLog));
getTailLogSupplier(outputLog),
getLogger());
}

private UIAppCmd.LogSupplier getTailLogSupplier(Path outputLog) {
Expand Down
Loading