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 * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as Stream from "effect/Stream";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
import { BuildCommandFailedError } from "./errors.ts";
const collectStreamAsString = <E>(stream: Stream.Stream<Uint8Array, E>): Effect.Effect<string, E> =>
stream.pipe(
Stream.decodeText(),
Stream.runFold(
() => "",
(acc, chunk) => acc + chunk,
),
);
const COMMAND_OUTPUT_TAIL_LENGTH = 20_000;
function appendOutputTail(acc: string, chunk: string): string {
const next = acc + chunk;
return next.length > COMMAND_OUTPUT_TAIL_LENGTH ? next.slice(-COMMAND_OUTPUT_TAIL_LENGTH) : next;
}
const collectCommandStream = <E>(
stream: Stream.Stream<Uint8Array, E>,
output: NodeJS.WriteStream,
verbose: boolean,
): Effect.Effect<string, E> =>
stream.pipe(
Stream.decodeText(),
Stream.runFoldEffect(
() => "",
(acc, chunk) =>
Effect.as(
verbose ? Effect.sync(() => output.write(chunk)) : Effect.void,
appendOutputTail(acc, chunk),
),
),
);
const spawnAndCollectOutput = Effect.fn("spawnAndCollectOutput")(function* (
command: ChildProcess.Command,
) {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const child = yield* spawner.spawn(command);
const [stdout, stderr, exitCode] = yield* Effect.all(
[
collectStreamAsString(child.stdout),
collectStreamAsString(child.stderr),
child.exitCode.pipe(Effect.map(Number)),
],
{ concurrency: "unbounded" },
);
return { stdout, stderr, exitCode } as const;
});
export const resolveGitCommitHash = Effect.fn("resolveGitCommitHash")(function* (repoRoot: string) {
const result = yield* spawnAndCollectOutput(
ChildProcess.make("git", ["rev-parse", "--short=12", "HEAD"], {
cwd: repoRoot,
}),
).pipe(
Effect.orElseSucceed(() => ({
stdout: "",
stderr: "",
exitCode: 1,
})),
);
if (result.exitCode !== 0) {
return "unknown";
}
const hash = result.stdout.trim();
if (!/^[0-9a-f]{7,40}$/i.test(hash)) {
return "unknown";
}
return hash.toLowerCase();
});
export const resolvePythonForNodeGyp = Effect.fn("resolvePythonForNodeGyp")(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const hostPlatform = yield* HostProcessPlatform;
const env = yield* Config.all({
configuredPython: Config.string("npm_config_python").pipe(
Config.orElse(() => Config.string("PYTHON")),
Config.option,
),
localAppData: Config.string("LOCALAPPDATA").pipe(Config.option),
});
const configured = Option.getOrUndefined(env.configuredPython);
if (configured && (yield* fs.exists(configured))) {
return configured;
}
if (hostPlatform === "win32") {
const localAppData = Option.getOrUndefined(env.localAppData);
if (localAppData) {
for (const version of ["Python313", "Python312", "Python311", "Python310"]) {
const candidate = path.join(localAppData, "Programs", "Python", version, "python.exe");
if (yield* fs.exists(candidate)) {
return candidate;
}
}
}
}
const probe = yield* spawnAndCollectOutput(
ChildProcess.make("python", ["-c", "import sys;print(sys.executable)"]),
).pipe(
Effect.orElseSucceed(() => ({
stdout: "",
stderr: "",
exitCode: 1,
})),
);
if (probe.exitCode !== 0) {
return undefined;
}
const executable = probe.stdout.trim();
if (!executable || !(yield* fs.exists(executable))) {
return undefined;
}
return executable;
});
export const runCommand = Effect.fn("runCommand")(function* (
command: ChildProcess.Command,
options: {
readonly label: string;
readonly verbose: boolean;
},
) {
const commandSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const child = yield* commandSpawner.spawn(command);
const [stdout, stderr, exitCode] = yield* Effect.all(
[
collectCommandStream(child.stdout, process.stdout, options.verbose),
collectCommandStream(child.stderr, process.stderr, options.verbose),
child.exitCode.pipe(Effect.map(Number)),
],
{ concurrency: "unbounded" },
);
if (exitCode !== 0) {
return yield* new BuildCommandFailedError({
command: options.label,
exitCode,
...(stdout.trim() ? { stdoutTail: stdout } : {}),
...(stderr.trim() ? { stderrTail: stderr } : {}),
});
}
});