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 * as Predicate from "effect/Predicate";
import { ProviderDriverKind } from "@akeru/contracts";
import { resolveCommandPath } from "@akeru/shared/shell";
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
export function nonEmptyString(value: string | null | undefined): string | null {
return Predicate.isString(value) && value.trim().length > 0 ? value.trim() : null;
}
export const compactEnv = (input: Record<string, Option.Option<string>>): NodeJS.ProcessEnv =>
Object.fromEntries(
Object.entries(input).flatMap(([key, value]) =>
Option.match(value, {
onNone: () => [],
onSome: (resolved) => [[key, resolved]],
}),
),
);
export const CommandLookupEnvConfig = Config.all({
PATH: Config.string("PATH").pipe(Config.option),
Path: Config.string("Path").pipe(Config.option),
path: Config.string("path").pipe(Config.option),
PATHEXT: Config.string("PATHEXT").pipe(Config.option),
}).pipe(Config.map(compactEnv));
export const readCommandLookupEnv = CommandLookupEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
export interface ProviderMaintenanceCapabilities {
readonly provider: ProviderDriverKind;
readonly packageName: string | null;
readonly update: ProviderMaintenanceCommandAction | null;
}
export interface ProviderMaintenanceCommandAction {
readonly command: string;
readonly executable: string;
readonly args: ReadonlyArray<string>;
readonly lockKey: string;
}
export interface ProviderMaintenanceCapabilityResolutionOptions {
readonly binaryPath?: string | null;
readonly env?: NodeJS.ProcessEnv;
readonly resolvedCommandPath?: string | null;
readonly realCommandPath?: string | null;
}
export interface ProviderMaintenanceCapabilitiesResolver {
readonly resolve: (
options?: ProviderMaintenanceCapabilityResolutionOptions,
) => ProviderMaintenanceCapabilities;
}
export interface PackageManagedProviderMaintenanceDefinition {
readonly provider: ProviderDriverKind;
readonly npmPackageName: string;
readonly homebrewFormula: string | null;
readonly nativeUpdate: {
readonly executable: string;
readonly args: ReadonlyArray<string>;
readonly lockKey: string;
readonly isCommandPath: (commandPath: string) => boolean;
} | null;
}
export function providerMaintenanceCapabilities(input: {
readonly provider: ProviderDriverKind;
readonly packageName: string | null;
readonly updateExecutable: string | null;
readonly updateArgs: ReadonlyArray<string>;
readonly updateLockKey: string | null;
}): ProviderMaintenanceCapabilities {
const update =
input.updateExecutable === null || input.updateLockKey === null
? null
: {
command: [input.updateExecutable, ...input.updateArgs].join(" "),
executable: input.updateExecutable,
args: input.updateArgs,
lockKey: input.updateLockKey,
};
return {
provider: input.provider,
packageName: input.packageName,
update,
};
}
export function manualOnlyProviderMaintenanceCapabilities(input: {
readonly provider: ProviderDriverKind;
readonly packageName: string | null;
}): ProviderMaintenanceCapabilities {
return providerMaintenanceCapabilities({
provider: input.provider,
packageName: input.packageName,
updateExecutable: null,
updateArgs: [],
updateLockKey: null,
});
}
export function makeNpmGlobalProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilities {
return providerMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
updateExecutable: "npm",
// npm 12 blocks install scripts by default (empty allow-scripts allowlist)
// and still exits 0, so a package whose postinstall finishes the install
// (claude copies its native binary over a placeholder stub) is left broken
// while the update reports success. Allow this one package's scripts.
// Older npm warns about the unknown config and continues.
updateArgs: [
"install",
"-g",
`--allow-scripts=${definition.npmPackageName}`,
`${definition.npmPackageName}@latest`,
],
updateLockKey: "npm-global",
});
}
export function makeBunGlobalProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilities {
return providerMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
updateExecutable: "bun",
updateArgs: ["i", "-g", `${definition.npmPackageName}@latest`],
updateLockKey: "bun-global",
});
}
export function makePnpmGlobalProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilities {
return providerMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
updateExecutable: "pnpm",
updateArgs: ["add", "-g", `${definition.npmPackageName}@latest`],
updateLockKey: "pnpm-global",
});
}
export function makeVitePlusGlobalProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilities {
return providerMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
updateExecutable: "vp",
updateArgs: ["i", "-g", definition.npmPackageName],
updateLockKey: "vite-plus-global",
});
}
export function makeHomebrewProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilities {
if (!definition.homebrewFormula) {
return manualOnlyProviderMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
});
}
return providerMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
updateExecutable: "brew",
updateArgs: ["upgrade", definition.homebrewFormula],
updateLockKey: "homebrew",
});
}
export function makeNativeProviderMaintenanceCapabilities(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilities | null {
if (!definition.nativeUpdate) {
return null;
}
return providerMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
updateExecutable: definition.nativeUpdate.executable,
updateArgs: definition.nativeUpdate.args,
updateLockKey: definition.nativeUpdate.lockKey,
});
}
export function hasPathSeparator(value: string): boolean {
return value.includes("/") || value.includes("\\");
}
export function normalizeCommandPath(commandPath: string): string {
return commandPath.replaceAll("\\", "/").toLowerCase();
}
export function isBunGlobalCommandPath(commandPath: string): boolean {
return normalizeCommandPath(commandPath).includes("/.bun/bin/");
}
export function isVitePlusGlobalCommandPath(commandPath: string): boolean {
return normalizeCommandPath(commandPath).includes("/.vite-plus/bin/");
}
export function isPnpmGlobalCommandPath(commandPath: string): boolean {
const normalized = normalizeCommandPath(commandPath);
return (
normalized.includes("/.local/share/pnpm/") ||
normalized.includes("/library/pnpm/") ||
normalized.includes("/local/share/pnpm/") ||
normalized.includes("/appdata/local/pnpm/") ||
normalized.includes("/pnpm/global/")
);
}
export function isNpmGlobalCommandPath(commandPath: string): boolean {
const normalized = normalizeCommandPath(commandPath);
return (
normalized.includes("/node_modules/.bin/") ||
normalized.includes("/lib/node_modules/") ||
normalized.includes("/npm/node_modules/")
);
}
export function isHomebrewCommandPath(commandPath: string): boolean {
const normalized = normalizeCommandPath(commandPath);
return (
normalized.includes("/opt/homebrew/cellar/") ||
normalized.includes("/usr/local/cellar/") ||
normalized.includes("/homebrew/cellar/") ||
normalized.includes("/opt/homebrew/caskroom/") ||
normalized.includes("/usr/local/caskroom/") ||
normalized.includes("/homebrew/caskroom/") ||
normalized.startsWith("/opt/homebrew/bin/") ||
normalized.startsWith("/usr/local/bin/")
);
}
export function resolvePackageManagedProviderMaintenance(
definition: PackageManagedProviderMaintenanceDefinition,
options?: ProviderMaintenanceCapabilityResolutionOptions,
): ProviderMaintenanceCapabilities {
const binaryPath = nonEmptyString(options?.binaryPath);
if (!binaryPath) {
return makeNpmGlobalProviderMaintenanceCapabilities(definition);
}
const resolvedCommandPath =
options?.resolvedCommandPath ?? (hasPathSeparator(binaryPath) ? binaryPath : null);
if (resolvedCommandPath) {
const commandPaths = [
resolvedCommandPath,
...(options?.realCommandPath ? [options.realCommandPath] : []),
];
const nativeUpdate = definition.nativeUpdate;
if (
nativeUpdate &&
commandPaths.some((commandPath) => nativeUpdate.isCommandPath(commandPath))
) {
return (
makeNativeProviderMaintenanceCapabilities(definition) ??
makeNpmGlobalProviderMaintenanceCapabilities(definition)
);
}
if (commandPaths.some(isVitePlusGlobalCommandPath)) {
return makeVitePlusGlobalProviderMaintenanceCapabilities(definition);
}
if (commandPaths.some(isBunGlobalCommandPath)) {
return makeBunGlobalProviderMaintenanceCapabilities(definition);
}
if (commandPaths.some(isPnpmGlobalCommandPath)) {
return makePnpmGlobalProviderMaintenanceCapabilities(definition);
}
if (commandPaths.some(isNpmGlobalCommandPath)) {
return makeNpmGlobalProviderMaintenanceCapabilities(definition);
}
if (commandPaths.some(isHomebrewCommandPath)) {
return makeHomebrewProviderMaintenanceCapabilities(definition);
}
}
if (!hasPathSeparator(binaryPath)) {
return makeNpmGlobalProviderMaintenanceCapabilities(definition);
}
return manualOnlyProviderMaintenanceCapabilities({
provider: definition.provider,
packageName: definition.npmPackageName,
});
}
export function packageManagedProviderMaintenanceResolver(
definition: PackageManagedProviderMaintenanceDefinition,
): ProviderMaintenanceCapabilitiesResolver {
return {
resolve: (options) => resolvePackageManagedProviderMaintenance(definition, options),
};
}
export function staticProviderMaintenanceResolver(
capabilities: ProviderMaintenanceCapabilities,
): ProviderMaintenanceCapabilitiesResolver {
return {
resolve: () => capabilities,
};
}
export function manualProviderMaintenanceCapabilities(
provider: ProviderDriverKind,
): ProviderMaintenanceCapabilities {
return manualOnlyProviderMaintenanceCapabilities({
provider,
packageName: null,
});
}
export const resolveProviderMaintenanceCapabilitiesEffect = Effect.fn(
"resolveProviderMaintenanceCapabilitiesEffect",
)(function* (
resolver: ProviderMaintenanceCapabilitiesResolver,
options?: Omit<ProviderMaintenanceCapabilityResolutionOptions, "realCommandPath">,
) {
const binaryPath = nonEmptyString(options?.binaryPath);
if (!binaryPath) {
return resolver.resolve(options);
}
const env = options?.env ?? (yield* readCommandLookupEnv);
const resolvedCommandPath =
(yield* resolveCommandPath(binaryPath, { env }).pipe(
Effect.catchTag("CommandResolutionError", () => Effect.succeed(null)),
)) ?? (hasPathSeparator(binaryPath) ? binaryPath : null);
if (!resolvedCommandPath) {
return resolver.resolve(options);
}
const fileSystem = yield* FileSystem.FileSystem;
const realCommandPath = yield* fileSystem
.realPath(resolvedCommandPath)
.pipe(Effect.orElseSucceed(() => resolvedCommandPath));
return resolver.resolve({
...options,
env,
resolvedCommandPath,
realCommandPath,
});
});