mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 17:01:37 +00:00
refactor(cli): dedupe browser start/stop
This commit is contained in:
@@ -13,6 +13,45 @@ import { shortenHomePath } from "../utils.js";
|
|||||||
import { callBrowserRequest, type BrowserParentOpts } from "./browser-cli-shared.js";
|
import { callBrowserRequest, type BrowserParentOpts } from "./browser-cli-shared.js";
|
||||||
import { runCommandWithRuntime } from "./cli-utils.js";
|
import { runCommandWithRuntime } from "./cli-utils.js";
|
||||||
|
|
||||||
|
async function fetchBrowserStatus(
|
||||||
|
parent: BrowserParentOpts,
|
||||||
|
profile?: string,
|
||||||
|
): Promise<BrowserStatus> {
|
||||||
|
return await callBrowserRequest<BrowserStatus>(
|
||||||
|
parent,
|
||||||
|
{
|
||||||
|
method: "GET",
|
||||||
|
path: "/",
|
||||||
|
query: profile ? { profile } : undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeoutMs: 1500,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runBrowserToggle(
|
||||||
|
parent: BrowserParentOpts,
|
||||||
|
params: { profile?: string; path: string },
|
||||||
|
) {
|
||||||
|
await callBrowserRequest(
|
||||||
|
parent,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
path: params.path,
|
||||||
|
query: params.profile ? { profile: params.profile } : undefined,
|
||||||
|
},
|
||||||
|
{ timeoutMs: 15000 },
|
||||||
|
);
|
||||||
|
const status = await fetchBrowserStatus(parent, params.profile);
|
||||||
|
if (parent?.json) {
|
||||||
|
defaultRuntime.log(JSON.stringify(status, null, 2));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const name = status.profile ?? "openclaw";
|
||||||
|
defaultRuntime.log(info(`🦞 browser [${name}] running: ${status.running}`));
|
||||||
|
}
|
||||||
|
|
||||||
function runBrowserCommand(action: () => Promise<void>) {
|
function runBrowserCommand(action: () => Promise<void>) {
|
||||||
return runCommandWithRuntime(defaultRuntime, action, (err) => {
|
return runCommandWithRuntime(defaultRuntime, action, (err) => {
|
||||||
defaultRuntime.error(danger(String(err)));
|
defaultRuntime.error(danger(String(err)));
|
||||||
@@ -30,17 +69,7 @@ export function registerBrowserManageCommands(
|
|||||||
.action(async (_opts, cmd) => {
|
.action(async (_opts, cmd) => {
|
||||||
const parent = parentOpts(cmd);
|
const parent = parentOpts(cmd);
|
||||||
await runBrowserCommand(async () => {
|
await runBrowserCommand(async () => {
|
||||||
const status = await callBrowserRequest<BrowserStatus>(
|
const status = await fetchBrowserStatus(parent, parent?.browserProfile);
|
||||||
parent,
|
|
||||||
{
|
|
||||||
method: "GET",
|
|
||||||
path: "/",
|
|
||||||
query: parent?.browserProfile ? { profile: parent.browserProfile } : undefined,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
timeoutMs: 1500,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (parent?.json) {
|
if (parent?.json) {
|
||||||
defaultRuntime.log(JSON.stringify(status, null, 2));
|
defaultRuntime.log(JSON.stringify(status, null, 2));
|
||||||
return;
|
return;
|
||||||
@@ -71,30 +100,7 @@ export function registerBrowserManageCommands(
|
|||||||
const parent = parentOpts(cmd);
|
const parent = parentOpts(cmd);
|
||||||
const profile = parent?.browserProfile;
|
const profile = parent?.browserProfile;
|
||||||
await runBrowserCommand(async () => {
|
await runBrowserCommand(async () => {
|
||||||
await callBrowserRequest(
|
await runBrowserToggle(parent, { profile, path: "/start" });
|
||||||
parent,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
path: "/start",
|
|
||||||
query: profile ? { profile } : undefined,
|
|
||||||
},
|
|
||||||
{ timeoutMs: 15000 },
|
|
||||||
);
|
|
||||||
const status = await callBrowserRequest<BrowserStatus>(
|
|
||||||
parent,
|
|
||||||
{
|
|
||||||
method: "GET",
|
|
||||||
path: "/",
|
|
||||||
query: profile ? { profile } : undefined,
|
|
||||||
},
|
|
||||||
{ timeoutMs: 1500 },
|
|
||||||
);
|
|
||||||
if (parent?.json) {
|
|
||||||
defaultRuntime.log(JSON.stringify(status, null, 2));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const name = status.profile ?? "openclaw";
|
|
||||||
defaultRuntime.log(info(`🦞 browser [${name}] running: ${status.running}`));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -105,30 +111,7 @@ export function registerBrowserManageCommands(
|
|||||||
const parent = parentOpts(cmd);
|
const parent = parentOpts(cmd);
|
||||||
const profile = parent?.browserProfile;
|
const profile = parent?.browserProfile;
|
||||||
await runBrowserCommand(async () => {
|
await runBrowserCommand(async () => {
|
||||||
await callBrowserRequest(
|
await runBrowserToggle(parent, { profile, path: "/stop" });
|
||||||
parent,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
path: "/stop",
|
|
||||||
query: profile ? { profile } : undefined,
|
|
||||||
},
|
|
||||||
{ timeoutMs: 15000 },
|
|
||||||
);
|
|
||||||
const status = await callBrowserRequest<BrowserStatus>(
|
|
||||||
parent,
|
|
||||||
{
|
|
||||||
method: "GET",
|
|
||||||
path: "/",
|
|
||||||
query: profile ? { profile } : undefined,
|
|
||||||
},
|
|
||||||
{ timeoutMs: 1500 },
|
|
||||||
);
|
|
||||||
if (parent?.json) {
|
|
||||||
defaultRuntime.log(JSON.stringify(status, null, 2));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const name = status.profile ?? "openclaw";
|
|
||||||
defaultRuntime.log(info(`🦞 browser [${name}] running: ${status.running}`));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user