trellis
Version:
Agentic State Engine — event-sourced causal graph with branching, decision traces, and realtime sync for AI-native applications
152 lines (145 loc) • 3.84 kB
JavaScript
import {
__esm,
__export
} from "./chunk-2ESYSVXG.js";
// src/cli/open-browser.ts
var open_browser_exports = {};
__export(open_browser_exports, {
openBrowser: () => openBrowser
});
import { exec, execFile } from "child_process";
function shellQuote(s) {
return `'${s.replace(/'/g, `'\\''`)}'`;
}
function localOriginKeys(url) {
let parsed;
try {
parsed = new URL(url);
} catch {
return null;
}
const host = parsed.hostname.toLowerCase();
if (host !== "localhost" && host !== "127.0.0.1" && host !== "[::1]" && host !== "::1") {
return null;
}
const port = parsed.port || (parsed.protocol === "https:" ? "443" : parsed.protocol === "http:" ? "80" : "");
if (!port) return null;
return { port };
}
function reuseTabScript(chromiumApps) {
const appList = chromiumApps.map((a) => `"${a}"`).join(", ");
return `
on run argv
set targetURL to item 1 of argv
set portStr to item 2 of argv
set chromiumApps to {${appList}}
tell application "System Events"
set procs to name of every process
end tell
repeat with appName in chromiumApps
if procs contains (appName as text) then
try
tell application appName
if not (exists window 1) then
-- skip
else
repeat with w in windows
set i to 0
repeat with t in tabs of w
set i to i + 1
set tabURL to URL of t
if tabURL contains (":" & portStr) then
if tabURL contains "localhost" or tabURL contains "127.0.0.1" then
set URL of t to targetURL
set active tab index of w to i
set index of w to 1
activate
return "hit"
end if
end if
end repeat
end repeat
end if
end tell
end try
end if
end repeat
if procs contains "Safari" then
try
tell application "Safari"
if (exists window 1) then
repeat with w in windows
repeat with t in tabs of w
set tabURL to URL of t
if tabURL contains (":" & portStr) then
if tabURL contains "localhost" or tabURL contains "127.0.0.1" then
set URL of t to targetURL
set current tab of w to t
set index of w to 1
activate
return "hit"
end if
end if
end repeat
end repeat
end if
end tell
end try
end if
return "miss"
end run
`;
}
function tryReuseTab(url, port) {
return new Promise((resolve) => {
execFile(
"osascript",
["-e", reuseTabScript(CHROMIUM_APPS), url, port],
{ timeout: 5e3 },
(err, stdout) => {
if (err) {
resolve(false);
return;
}
resolve(String(stdout).trim() === "hit");
}
);
});
}
function fallbackOpen(url) {
const cmd = process.platform === "darwin" ? "open" : process.platform === "win32" ? "start" : "xdg-open";
if (process.platform === "win32") {
exec(`start "" ${shellQuote(url)}`);
return;
}
exec(`${cmd} ${shellQuote(url)}`);
}
function openBrowser(url) {
const keys = process.platform === "darwin" ? localOriginKeys(url) : null;
if (!keys) {
fallbackOpen(url);
return;
}
void tryReuseTab(url, keys.port).then((hit) => {
if (!hit) fallbackOpen(url);
});
}
var CHROMIUM_APPS;
var init_open_browser = __esm({
"src/cli/open-browser.ts"() {
CHROMIUM_APPS = [
"Google Chrome",
"Chromium",
"Brave Browser",
"Arc",
"Microsoft Edge",
"Dia",
"Vivaldi"
];
}
});
export {
openBrowser,
open_browser_exports,
init_open_browser
};