Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import { existsSync } from 'node:fs';
import { basename, dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { _electron as electron } from '@playwright/test';
import { SPAWN_STARTUP_DEADLINE_MS } from '../../../src/shared/boot-narration.ts';
import { readinessGiveUpBoundMs } from './launch-readiness';
type ElectronLaunchOptions = NonNullable<Parameters<typeof electron.launch>[0]>;
export type SmokeLaunchEnv = NonNullable<ElectronLaunchOptions['env']>;
const HELPERS_DIR = dirname(fileURLToPath(import.meta.url));
export const DESKTOP_ROOT = resolve(HELPERS_DIR, '..', '..', '..');
export const PACKAGED_APP_ENV = 'OK_DESKTOP_PACKAGED_APP';
export const UNPACKAGED_MAIN_ENTRY = join(DESKTOP_ROOT, 'out', 'main', 'index.js');
export const DEFAULT_LOCAL_PACKAGED_APP = join(
DESKTOP_ROOT,
'dist-desktop',
'mac-arm64',
'OpenKnowledge.app',
);
export type DesktopLaunchMode = 'packaged' | 'unpackaged';
export interface DesktopTarget {
mode: DesktopLaunchMode;
appPath?: string;
targetPath: string;
readonly exists: boolean;
missingReason: string;
}
export interface ResolveDesktopTargetOptions {
env?: NodeJS.ProcessEnv;
requirePackaged?: boolean;
platform?: NodeJS.Platform;
arch?: NodeJS.Architecture;
}
export function executableForAppBundle(appPath: string): string {
return join(appPath, 'Contents', 'MacOS', basename(appPath, '.app'));
}
function probedOnRead(target: Omit<DesktopTarget, 'exists'>): DesktopTarget {
return {
...target,
get exists() {
return existsSync(target.targetPath);
},
};
}
function defaultPackagedAppPath(platform: NodeJS.Platform, arch: NodeJS.Architecture): string {
if (platform !== 'linux') return DEFAULT_LOCAL_PACKAGED_APP;
if (arch === 'x64') return join(DESKTOP_ROOT, 'dist-desktop', 'linux-unpacked');
if (arch === 'arm64') return join(DESKTOP_ROOT, 'dist-desktop', 'linux-arm64-unpacked');
throw new Error(`No local Linux packaged desktop target for architecture ${arch}`);
}
export function resolveDesktopTarget(options: ResolveDesktopTargetOptions = {}): DesktopTarget {
const env = options.env ?? process.env;
const platform = options.platform ?? process.platform;
const arch = options.arch ?? process.arch;
const override = env[PACKAGED_APP_ENV];
const appPath =
override && override.length > 0
? resolve(override)
: options.requirePackaged
? defaultPackagedAppPath(platform, arch)
: undefined;
if (appPath !== undefined) {
const targetPath =
platform === 'linux' ? join(appPath, 'openknowledge') : executableForAppBundle(appPath);
return probedOnRead({
mode: 'packaged',
appPath,
targetPath,
missingReason: `Packaged desktop build missing at ${targetPath} — build a packaged app, or point ${PACKAGED_APP_ENV} at one.`,
});
}
return probedOnRead({
mode: 'unpackaged',
targetPath: UNPACKAGED_MAIN_ENTRY,
missingReason: `Main build missing at ${UNPACKAGED_MAIN_ENTRY} — run "pnpm run build:desktop" first.`,
});
}
export interface DesktopLaunchOptionsInput {
target?: DesktopTarget;
args?: string[];
env?: SmokeLaunchEnv;
timeout?: number;
}
export interface DesktopLaunchOptions {
args: string[];
timeout: number;
executablePath?: string;
env?: SmokeLaunchEnv;
}
export const DEFAULT_LAUNCH_TIMEOUT_MS = 30_000;
export const PACKAGED_SMOKE_SERVER_IDLE_SHUTDOWN_MS = 2 * SPAWN_STARTUP_DEADLINE_MS;
export const SETUP_BEFORE_FIRST_READINESS_WAIT_MS = 0;
export const ONE_LAUNCH_AND_ITS_READINESS_VERDICT_MS =
DEFAULT_LAUNCH_TIMEOUT_MS +
readinessGiveUpBoundMs({ path: 'fork' }) +
SETUP_BEFORE_FIRST_READINESS_WAIT_MS;
export function desktopLaunchOptions(input: DesktopLaunchOptionsInput = {}): DesktopLaunchOptions {
const target = input.target ?? resolveDesktopTarget();
const extraArgs = input.args ?? [];
const timeout = input.timeout ?? DEFAULT_LAUNCH_TIMEOUT_MS;
const base: DesktopLaunchOptions =
target.mode === 'packaged'
? { args: [...extraArgs], timeout, executablePath: target.targetPath }
: { args: [target.targetPath, ...extraArgs], timeout };
return {
...base,
env: {
...process.env,
...input.env,
OK_LANG: 'en',
OK_LOG_LEVEL: 'info',
OK_DEV_PROTOCOL: '1',
...(target.mode === 'packaged'
? { OK_IDLE_SHUTDOWN: `${PACKAGED_SMOKE_SERVER_IDLE_SHUTDOWN_MS / 1000}s` }
: {}),
},
};
}