mirror of
https://github.com/lmstudio-ai/lms.git
synced 2025-09-19 12:54:19 +08:00
Auto start for all
This commit is contained in:
@ -1,9 +1,125 @@
|
||||
import { text, type SimpleLogger } from "@lmstudio/lms-common";
|
||||
import { LMStudioClient } from "@lmstudio/sdk";
|
||||
import chalk from "chalk";
|
||||
import { checkHttpServer, getServerLastStatus } from "./subcommands/server";
|
||||
import { flag } from "cmd-ts";
|
||||
import inquirer from "inquirer";
|
||||
import { platform } from "os";
|
||||
import { getCliPref } from "./cliPref";
|
||||
import {
|
||||
checkHttpServer,
|
||||
getServerLastStatus,
|
||||
startServer,
|
||||
type StartServerOpts,
|
||||
} from "./subcommands/server";
|
||||
|
||||
export async function createClient(logger: SimpleLogger) {
|
||||
export const createClientArgs = {
|
||||
yes: flag({
|
||||
long: "yes",
|
||||
short: "y",
|
||||
description: text`
|
||||
Suppress all confirmations and warnings. Useful for scripting.
|
||||
`,
|
||||
}),
|
||||
noLaunch: flag({
|
||||
long: "no-launch",
|
||||
description: text`
|
||||
Don't launch LM Studio if it's not running. Have no effect if auto start server is disabled.
|
||||
`,
|
||||
}),
|
||||
};
|
||||
|
||||
interface CreateClientArgs {
|
||||
yes?: boolean;
|
||||
noLaunch?: boolean;
|
||||
}
|
||||
|
||||
async function maybeTryStartServer(logger: SimpleLogger, startServerOpts: StartServerOpts) {
|
||||
const { yes } = startServerOpts;
|
||||
const pref = await getCliPref(logger);
|
||||
if (pref.get().autoStartServer === undefined && !yes) {
|
||||
logger.warnWithoutPrefix(text`
|
||||
${"\n"}${chalk.greenBright.underline("Server Auto Start")}
|
||||
|
||||
LM Studio needs to be running in server mode to perform this operation.${"\n"}
|
||||
`);
|
||||
const { cont } = await inquirer.prompt([
|
||||
{
|
||||
type: "confirm",
|
||||
name: "cont",
|
||||
message: "Do you want to always start the server if it's not running? (will not ask again)",
|
||||
default: true,
|
||||
},
|
||||
]);
|
||||
if (cont) {
|
||||
logger.info("lms will automatically start the server if it's not running.");
|
||||
} else {
|
||||
logger.info("lms WILL NOT automatically start the server if it's not running.");
|
||||
}
|
||||
if (platform() === "win32") {
|
||||
logger.info(text`
|
||||
To change this, edit the config file at
|
||||
${chalk.greenBright("%USERPROFILE%\\.cache\\lm-studio\\.internal\\cli-pref.json")}
|
||||
`);
|
||||
} else {
|
||||
logger.info(text`
|
||||
To change this, edit the config file at
|
||||
${chalk.greenBright("~/.cache/lm-studio/.internal/cli-pref.json")}
|
||||
`);
|
||||
}
|
||||
pref.setWithImmer(draft => {
|
||||
draft.autoStartServer = cont;
|
||||
});
|
||||
if (!cont) {
|
||||
logger.error(text`
|
||||
To start the server manually, run the following command:
|
||||
|
||||
${chalk.yellow("lms server start ")}${"\n"}
|
||||
`);
|
||||
return false;
|
||||
}
|
||||
logger.info("Starting the server...");
|
||||
return await startServer(logger, startServerOpts);
|
||||
}
|
||||
if (pref.get().autoStartServer === true) {
|
||||
logger.info("LM Studio is not running in server mode. Starting the server...");
|
||||
return await startServer(logger, startServerOpts);
|
||||
} else if (pref.get().autoStartServer === false) {
|
||||
logger.error("LM Studio needs to be running in the server mode to perform this operation.");
|
||||
if (platform() === "win32") {
|
||||
logger.error(text`
|
||||
To automatically start the server, edit the config file at
|
||||
${chalk.yellowBright("%USERPROFILE%\\.cache\\lm-studio\\.internal\\cli-pref.json")}
|
||||
`);
|
||||
} else {
|
||||
logger.error(text`
|
||||
To automatically start the server, edit the config file at
|
||||
${chalk.yellowBright("~/.cache/lm-studio/.internal/cli-pref.json")}
|
||||
`);
|
||||
}
|
||||
logger.error(text`
|
||||
To start the server manually, run the following command:
|
||||
|
||||
${chalk.yellow("lms server start ")}${"\n"}
|
||||
`);
|
||||
return false;
|
||||
} else {
|
||||
// If not true or false, it's undefined
|
||||
// Meaning --yes is used
|
||||
logger.info(text`
|
||||
LM Studio is not running in server mode. Starting the server because
|
||||
${chalk.yellowBright("--yes")} is set
|
||||
`);
|
||||
return await startServer(logger, startServerOpts);
|
||||
}
|
||||
}
|
||||
|
||||
export interface CreateClientOpts {}
|
||||
|
||||
export async function createClient(
|
||||
logger: SimpleLogger,
|
||||
{ noLaunch, yes }: CreateClientArgs,
|
||||
_opts: CreateClientOpts = {},
|
||||
) {
|
||||
let port: number;
|
||||
try {
|
||||
const lastStatus = await getServerLastStatus(logger);
|
||||
@ -13,16 +129,9 @@ export async function createClient(logger: SimpleLogger) {
|
||||
port = 1234;
|
||||
}
|
||||
if (!(await checkHttpServer(logger, port))) {
|
||||
logger.error(
|
||||
text`
|
||||
LM Studio needs to be running in server mode to perform this operation.
|
||||
|
||||
To start the server, run the following command:
|
||||
|
||||
${chalk.yellow("lms server start ")}${"\n"}
|
||||
`,
|
||||
);
|
||||
process.exit(1);
|
||||
if (!(await maybeTryStartServer(logger, { port, noLaunch, yes }))) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
const baseUrl = `ws://127.0.0.1:${port}`;
|
||||
logger.debug(`Connecting to server with baseUrl ${port}`);
|
||||
|
Reference in New Issue
Block a user