Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { HostProcessPlatform } from "@akeru/shared/hostProcess";
import { getDefaultBuildArch } from "../build-target-arch.ts";
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { BuildPlatform, BuildArch, RepoRoot } from "./model.ts";
import {
UnsupportedHostBuildPlatformError,
UnsupportedDesktopBuildArchitectureError,
InvalidMockUpdateServerPortError,
} from "./errors.ts";
import { PLATFORM_CONFIG } from "./config.ts";
interface BuildCliInput {
readonly platform: Option.Option<typeof BuildPlatform.Type>;
readonly target: Option.Option<string>;
readonly arch: Option.Option<typeof BuildArch.Type>;
readonly buildVersion: Option.Option<string>;
readonly outputDir: Option.Option<string>;
readonly skipBuild: Option.Option<boolean>;
readonly keepStage: Option.Option<boolean>;
readonly signed: Option.Option<boolean>;
readonly verbose: Option.Option<boolean>;
readonly mockUpdates: Option.Option<boolean>;
readonly mockUpdateServerPort: Option.Option<number>;
readonly wslPrebuild: Option.Option<string>;
}
function detectHostBuildPlatform(hostPlatform: string): typeof BuildPlatform.Type | undefined {
if (hostPlatform === "darwin") return "mac";
if (hostPlatform === "linux") return "linux";
if (hostPlatform === "win32") return "win";
return undefined;
}
export interface ResolvedBuildOptions {
readonly platform: typeof BuildPlatform.Type;
readonly target: string;
readonly arch: typeof BuildArch.Type;
readonly version: string | undefined;
readonly outputDir: string;
readonly skipBuild: boolean;
readonly keepStage: boolean;
readonly signed: boolean;
readonly verbose: boolean;
readonly mockUpdates: boolean;
readonly mockUpdateServerPort: number | undefined;
readonly wslPrebuild: string | undefined;
}
const BuildEnvConfig = Config.all({
platform: Config.schema(BuildPlatform, "T3CODE_DESKTOP_PLATFORM").pipe(Config.option),
target: Config.string("T3CODE_DESKTOP_TARGET").pipe(Config.option),
arch: Config.schema(BuildArch, "T3CODE_DESKTOP_ARCH").pipe(Config.option),
version: Config.string("T3CODE_DESKTOP_VERSION").pipe(Config.option),
outputDir: Config.string("T3CODE_DESKTOP_OUTPUT_DIR").pipe(Config.option),
skipBuild: Config.boolean("T3CODE_DESKTOP_SKIP_BUILD").pipe(Config.withDefault(false)),
keepStage: Config.boolean("T3CODE_DESKTOP_KEEP_STAGE").pipe(Config.withDefault(false)),
signed: Config.boolean("T3CODE_DESKTOP_SIGNED").pipe(Config.withDefault(false)),
verbose: Config.boolean("T3CODE_DESKTOP_VERBOSE").pipe(Config.withDefault(false)),
mockUpdates: Config.boolean("T3CODE_DESKTOP_MOCK_UPDATES").pipe(Config.withDefault(false)),
mockUpdateServerPort: Config.string("T3CODE_DESKTOP_MOCK_UPDATE_SERVER_PORT").pipe(Config.option),
// Path to a prebuilt Linux node-pty binary (pty.node) for the target arch,
// produced by the Linux CI job and handed to the Windows packaging job. Placed
// into the staged node-pty so the WSL backend ships a ready binary and never
// compiles on the user's machine.
wslPrebuild: Config.string("T3CODE_DESKTOP_WSL_PREBUILD").pipe(Config.option),
});
const MockUpdateServerPortSchema = Schema.NumberFromString.check(
Schema.isInt(),
Schema.isBetween({ minimum: 1, maximum: 65535 }),
);
const decodeMockUpdateServerPort = Schema.decodeUnknownEffect(MockUpdateServerPortSchema);
const resolveBooleanFlag = (flag: Option.Option<boolean>, envValue: boolean) =>
Option.getOrElse(flag, () => envValue);
const mergeOptions = <A>(a: Option.Option<A>, b: Option.Option<A>, defaultValue: A) =>
Option.getOrElse(a, () => Option.getOrElse(b, () => defaultValue));
export const resolveMockUpdateServerPort = Effect.fn("resolveMockUpdateServerPort")(function* (
mockUpdateServerPort: string | undefined,
) {
const port = mockUpdateServerPort?.trim();
if (!port) {
return undefined;
}
return yield* decodeMockUpdateServerPort(port);
});
export const resolveBuildOptions = Effect.fn("resolveBuildOptions")(function* (
input: BuildCliInput,
) {
const path = yield* Path.Path;
const repoRoot = yield* RepoRoot;
const env = yield* BuildEnvConfig;
const hostPlatform = yield* HostProcessPlatform;
const platform = mergeOptions(
input.platform,
env.platform,
detectHostBuildPlatform(hostPlatform),
);
if (!platform) {
return yield* new UnsupportedHostBuildPlatformError({ hostPlatform });
}
const target = mergeOptions(input.target, env.target, PLATFORM_CONFIG[platform].defaultTarget);
const defaultArch = yield* getDefaultBuildArch(PLATFORM_CONFIG[platform].archChoices);
const arch = mergeOptions(input.arch, env.arch, defaultArch);
const supportedArchitectures = PLATFORM_CONFIG[platform].archChoices;
if (!supportedArchitectures.includes(arch)) {
return yield* new UnsupportedDesktopBuildArchitectureError({
platform,
arch,
supportedArchitectures: [...supportedArchitectures],
});
}
const version = mergeOptions(input.buildVersion, env.version, undefined);
const releaseDir = resolveBooleanFlag(input.mockUpdates, env.mockUpdates)
? "release-mock"
: "release";
const outputDir = path.resolve(
repoRoot,
mergeOptions(input.outputDir, env.outputDir, releaseDir),
);
const skipBuild = resolveBooleanFlag(input.skipBuild, env.skipBuild);
const keepStage = resolveBooleanFlag(input.keepStage, env.keepStage);
const signed = resolveBooleanFlag(input.signed, env.signed);
const verbose = resolveBooleanFlag(input.verbose, env.verbose);
const mockUpdates = resolveBooleanFlag(input.mockUpdates, env.mockUpdates);
const configuredMockUpdateServerPort = Option.getOrUndefined(env.mockUpdateServerPort);
const mockUpdateServerPort =
Option.getOrUndefined(input.mockUpdateServerPort) ??
(configuredMockUpdateServerPort === undefined
? undefined
: yield* resolveMockUpdateServerPort(configuredMockUpdateServerPort).pipe(
Effect.mapError((cause) =>
InvalidMockUpdateServerPortError.fromConfigValue(configuredMockUpdateServerPort, cause),
),
));
const wslPrebuild =
Option.getOrUndefined(input.wslPrebuild) ?? Option.getOrUndefined(env.wslPrebuild);
return {
platform,
target,
arch,
version,
outputDir,
skipBuild,
keepStage,
signed,
verbose,
mockUpdates,
mockUpdateServerPort,
wslPrebuild,
} satisfies ResolvedBuildOptions;
});