Fix arguments containing spaces
This commit is contained in:
BIN
lm-launcher.jar
BIN
lm-launcher.jar
Binary file not shown.
@@ -6,7 +6,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@@ -30,11 +29,11 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
"MIN_P", Model::minPDisplay,
|
"MIN_P", Model::minPDisplay,
|
||||||
"REASONING_STOP", m -> "... thinking budget exceeded, let's answer now.\n");
|
"REASONING_STOP", m -> "... thinking budget exceeded, let's answer now.\n");
|
||||||
|
|
||||||
String argumentsToString(boolean showAll) {
|
String argumentsToString(final boolean showAll) {
|
||||||
return this.argumentsToStringByArgs(this.appArguments.stream(), showAll);
|
return this.argumentsToStringByArgs(this.appArguments.stream(), showAll);
|
||||||
}
|
}
|
||||||
|
|
||||||
String argumentsToString(final List<AppArgument> argumentOverrides, boolean showAll) {
|
String argumentsToString(final List<AppArgument> argumentOverrides, final boolean showAll) {
|
||||||
return this.argumentsToStringByArgs(this.appArguments
|
return this.argumentsToStringByArgs(this.appArguments
|
||||||
.stream()
|
.stream()
|
||||||
.map(arg -> argumentOverrides.stream().filter(a -> a.argument().equals(arg.argument())).findFirst().orElse(arg)),
|
.map(arg -> argumentOverrides.stream().filter(a -> a.argument().equals(arg.argument())).findFirst().orElse(arg)),
|
||||||
@@ -45,7 +44,7 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
if (modelPath == null || modelPath.isBlank()) {
|
if (modelPath == null || modelPath.isBlank()) {
|
||||||
return copyArguments(this.appArguments);
|
return copyArguments(this.appArguments);
|
||||||
}
|
}
|
||||||
final AppModelSettings savedSettings = this.findModelSettings(modelPath);
|
final var savedSettings = this.findModelSettings(modelPath);
|
||||||
if (savedSettings == null) {
|
if (savedSettings == null) {
|
||||||
return copyArguments(this.appArguments);
|
return copyArguments(this.appArguments);
|
||||||
}
|
}
|
||||||
@@ -56,8 +55,8 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
if (modelPath == null || modelPath.isBlank()) {
|
if (modelPath == null || modelPath.isBlank()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final AppModelSettings updatedSettings = new AppModelSettings(modelPath, copyArguments(selectedArguments));
|
final var updatedSettings = new AppModelSettings(modelPath, copyArguments(selectedArguments));
|
||||||
for (int i = 0; i < this.modelSettings.size(); i++) {
|
for (var i = 0; i < this.modelSettings.size(); i++) {
|
||||||
if (this.modelSettings.get(i).modelPath().equals(modelPath)) {
|
if (this.modelSettings.get(i).modelPath().equals(modelPath)) {
|
||||||
this.modelSettings.set(i, updatedSettings);
|
this.modelSettings.set(i, updatedSettings);
|
||||||
return;
|
return;
|
||||||
@@ -66,7 +65,7 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
this.modelSettings.add(updatedSettings);
|
this.modelSettings.add(updatedSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String argumentsToStringByArgs(final Stream<AppArgument> args, boolean showAll) {
|
private String argumentsToStringByArgs(final Stream<AppArgument> args, final boolean showAll) {
|
||||||
return args
|
return args
|
||||||
.filter(arg -> showAll || (arg.optional() ? arg.default_() : true))
|
.filter(arg -> showAll || (arg.optional() ? arg.default_() : true))
|
||||||
.map(AppArgument::argument)
|
.map(AppArgument::argument)
|
||||||
@@ -82,7 +81,7 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
for (final AppArgument baseArgument : this.appArguments) {
|
for (final AppArgument baseArgument : this.appArguments) {
|
||||||
final int occurrenceIndex = occurrences.getOrDefault(baseArgument.argument(), 0);
|
final int occurrenceIndex = occurrences.getOrDefault(baseArgument.argument(), 0);
|
||||||
occurrences.put(baseArgument.argument(), occurrenceIndex + 1);
|
occurrences.put(baseArgument.argument(), occurrenceIndex + 1);
|
||||||
final AppArgument savedArgument = nthArgument(savedArguments, baseArgument.argument(), occurrenceIndex);
|
final var savedArgument = nthArgument(savedArguments, baseArgument.argument(), occurrenceIndex);
|
||||||
mergedArguments.add(savedArgument == null
|
mergedArguments.add(savedArgument == null
|
||||||
? new AppArgument(baseArgument.argument(), baseArgument.optional(), baseArgument.default_())
|
? new AppArgument(baseArgument.argument(), baseArgument.optional(), baseArgument.default_())
|
||||||
: new AppArgument(baseArgument.argument(), baseArgument.optional(), baseArgument.optional() ? savedArgument.default_() : baseArgument.default_()));
|
: new AppArgument(baseArgument.argument(), baseArgument.optional(), baseArgument.optional() ? savedArgument.default_() : baseArgument.default_()));
|
||||||
@@ -98,7 +97,7 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static AppArgument nthArgument(final List<AppArgument> arguments, final String argument, final int occurrenceIndex) {
|
private static AppArgument nthArgument(final List<AppArgument> arguments, final String argument, final int occurrenceIndex) {
|
||||||
int matchIndex = 0;
|
var matchIndex = 0;
|
||||||
for (final AppArgument candidate : arguments) {
|
for (final AppArgument candidate : arguments) {
|
||||||
if (!candidate.argument().equals(argument)) {
|
if (!candidate.argument().equals(argument)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -118,25 +117,41 @@ record App(Path path, Path interpreter, @Deprecated List<String> arguments, List
|
|||||||
}
|
}
|
||||||
|
|
||||||
void run(final Model model, final List<AppArgument> argumentOverrides) throws IOException, IllegalArgumentException {
|
void run(final Model model, final List<AppArgument> argumentOverrides) throws IOException, IllegalArgumentException {
|
||||||
String interpreterStr = "";
|
final List<String> cmdParts = new ArrayList<>();
|
||||||
|
|
||||||
if (this.interpreter() != null) {
|
if (this.interpreter() != null) {
|
||||||
interpreterStr = this.interpreter().toString().concat(" ");
|
cmdParts.add(this.interpreter().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
String consoleCmdStr = Data.props.getProperty("console")
|
cmdParts.add(this.path().toString());
|
||||||
.replace("CMD", interpreterStr + this.path().toString() + " " + this.argumentsToString(argumentOverrides, false));
|
|
||||||
|
|
||||||
if (PLACEHOLDERS.keySet().stream().anyMatch(consoleCmdStr::contains)) {
|
for (final AppArgument arg_ : this.appArguments) {
|
||||||
|
final var arg = argumentOverrides.stream().filter(a -> a.argument().equals(arg_.argument())).findFirst().orElseGet(() -> arg_);
|
||||||
|
if (!arg.optional() || arg.default_()) {
|
||||||
|
var argReplaced = arg.argument();
|
||||||
|
if (PLACEHOLDERS.keySet().contains(arg.argument())) {
|
||||||
if (model == null) {
|
if (model == null) {
|
||||||
throw new IllegalArgumentException("Select a model first");
|
throw new IllegalArgumentException("Select a model first");
|
||||||
}
|
}
|
||||||
for (final Entry<String, Function<Model, String>> placeholder : PLACEHOLDERS.entrySet()) {
|
argReplaced = PLACEHOLDERS.get(arg.argument()).apply(model);
|
||||||
consoleCmdStr = consoleCmdStr.replace(placeholder.getKey(), placeholder.getValue().apply(model));
|
}
|
||||||
|
|
||||||
|
cmdParts.add(argReplaced);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<String> arguments = new ArrayList<>();
|
||||||
|
for (final var arg : Data.props.getProperty("console").split("\\s+")) {
|
||||||
|
if ("CMD".equals(arg)) {
|
||||||
|
arguments.addAll(cmdParts);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
arguments.add(arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Runtime.getRuntime().exec(
|
Runtime.getRuntime().exec(
|
||||||
consoleCmdStr.split("\\s+"),
|
arguments.toArray(new String[0]),
|
||||||
null,
|
null,
|
||||||
this.path().toAbsolutePath().getParent().toFile());
|
this.path().toAbsolutePath().getParent().toFile());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user