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 { CommandId, type OrchestrationReadModel, ProjectId } from "@akeru/contracts";
import * as Console from "effect/Console";
import * as Crypto from "effect/Crypto";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as References from "effect/References";
import { Argument, Command, Flag, GlobalFlag } from "effect/unstable/cli";
import { FetchHttpClient, HttpClient } from "effect/unstable/http";
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as ServerConfig from "../config.ts";
import * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.ts";
import * as ServerRuntimeStartup from "../serverRuntimeStartup.ts";
import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";
import { type CliAuthLocationFlags, projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
import {
type ProjectCommandExecutionMode,
type ProjectCliDispatchCommand,
ProjectAlreadyExistsError,
} from "./projectErrors.ts";
import {
projectCommandUuid,
normalizeWorkspaceRootForProjectCommand,
resolveProjectTitle,
findActiveProjectTarget,
} from "./projectTargets.ts";
import { ProjectCliRuntimeLive, getOfflineSnapshot } from "./projectOffline.ts";
import {
withProjectCliSessionToken,
fetchLiveOrchestrationSnapshot,
dispatchLiveOrchestrationCommand,
tryResolveLiveProjectExecutionMode,
} from "./projectLiveClient.ts";
const runProjectMutation = Effect.fn("runProjectMutation")(function* (
flags: CliAuthLocationFlags,
run: (input: {
readonly snapshot: OrchestrationReadModel;
readonly dispatch: (
command: ProjectCliDispatchCommand,
) => Effect.Effect<void, Error, FileSystem.FileSystem | HttpClient.HttpClient | Path.Path>;
readonly mode: ProjectCommandExecutionMode;
}) => Effect.Effect<
string,
Error,
| Crypto.Crypto
| FileSystem.FileSystem
| HttpClient.HttpClient
| Path.Path
| WorkspacePaths.WorkspacePaths
>,
) {
const logLevel = yield* GlobalFlag.LogLevel;
const config = yield* resolveCliAuthConfig(flags, logLevel);
const minimumLogLevel = config.logLevel;
return yield* Effect.gen(function* () {
const environmentAuth = yield* EnvironmentAuth.EnvironmentAuth;
const liveMode = yield* tryResolveLiveProjectExecutionMode(environmentAuth, config);
if (Option.isSome(liveMode)) {
return yield* withProjectCliSessionToken(environmentAuth, (token) =>
Effect.gen(function* () {
const snapshot = yield* fetchLiveOrchestrationSnapshot(liveMode.value.origin, token);
const output = yield* run({
snapshot,
dispatch: (command) =>
dispatchLiveOrchestrationCommand(liveMode.value.origin, token, command),
mode: "live",
});
yield* Console.log(output);
}),
);
}
const offlineRuntimeLayer = ProjectCliRuntimeLive.pipe(
Layer.provide(ServerConfig.layer(config)),
Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
);
return yield* Effect.gen(function* () {
const snapshot = yield* getOfflineSnapshot();
const orchestrationEngine = yield* OrchestrationEngine.OrchestrationEngineService;
const output = yield* run({
snapshot,
dispatch: (command) => orchestrationEngine.dispatch(command),
mode: "offline",
});
yield* Console.log(output);
}).pipe(Effect.provide(offlineRuntimeLayer));
}).pipe(
Effect.provide(
Layer.mergeAll(EnvironmentAuth.runtimeLayer, WorkspacePaths.layer).pipe(
Layer.provideMerge(FetchHttpClient.layer),
Layer.provide(ServerConfig.layer(config)),
Layer.provide(Layer.succeed(References.MinimumLogLevel, minimumLogLevel)),
),
),
);
});
const projectAddCommand = Command.make("add", {
...projectLocationFlags,
workspaceRoot: Argument.string("path").pipe(
Argument.withDescription("Workspace root to add as a project."),
),
title: Flag.string("title").pipe(Flag.withDescription("Optional project title."), Flag.optional),
}).pipe(
Command.withDescription("Add a project."),
Command.withHandler((flags) =>
runProjectMutation(
flags,
Effect.fn("projectAddMutation")(function* ({
snapshot,
dispatch,
}: {
readonly snapshot: OrchestrationReadModel;
readonly dispatch: (
command: ProjectCliDispatchCommand,
) => Effect.Effect<void, Error, FileSystem.FileSystem | HttpClient.HttpClient | Path.Path>;
}) {
const workspaceRoot = yield* normalizeWorkspaceRootForProjectCommand(flags.workspaceRoot);
const existingProject = snapshot.projects.find(
(project) => project.deletedAt === null && project.workspaceRoot === workspaceRoot,
);
if (existingProject) {
return yield* new ProjectAlreadyExistsError({
operation: "addProject",
projectId: existingProject.id,
workspaceRoot,
});
}
const title = yield* resolveProjectTitle(workspaceRoot, Option.getOrUndefined(flags.title));
const projectId = ProjectId.make(yield* projectCommandUuid);
yield* dispatch({
type: "project.create",
commandId: CommandId.make(yield* projectCommandUuid),
projectId,
title,
workspaceRoot,
defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(),
createdAt: DateTime.formatIso(yield* DateTime.now),
});
return `Added project ${projectId} (${title}) at ${workspaceRoot}.`;
}),
),
),
);
const projectRemoveCommand = Command.make("remove", {
...projectLocationFlags,
project: Argument.string("project").pipe(
Argument.withDescription("Project id or workspace root to remove."),
),
force: Flag.boolean("force").pipe(
Flag.withDescription("Delete the project and all of its threads."),
Flag.withDefault(false),
),
}).pipe(
Command.withDescription("Remove a project."),
Command.withHandler((flags) =>
runProjectMutation(
flags,
Effect.fn("projectRemoveMutation")(function* ({
snapshot,
dispatch,
}: {
readonly snapshot: OrchestrationReadModel;
readonly dispatch: (
command: ProjectCliDispatchCommand,
) => Effect.Effect<void, Error, FileSystem.FileSystem | HttpClient.HttpClient | Path.Path>;
}) {
const project = yield* findActiveProjectTarget({
snapshot,
identifier: flags.project,
});
yield* dispatch({
type: "project.delete",
commandId: CommandId.make(yield* projectCommandUuid),
projectId: project.id,
force: flags.force,
});
return `Removed project ${project.id} (${project.title}).`;
}),
),
),
);
const projectRenameCommand = Command.make("rename", {
...projectLocationFlags,
project: Argument.string("project").pipe(
Argument.withDescription("Project id or workspace root to rename."),
),
title: Argument.string("title").pipe(Argument.withDescription("New project title.")),
}).pipe(
Command.withDescription("Rename a project."),
Command.withHandler((flags) =>
runProjectMutation(
flags,
Effect.fn("projectRenameMutation")(function* ({
snapshot,
dispatch,
}: {
readonly snapshot: OrchestrationReadModel;
readonly dispatch: (
command: ProjectCliDispatchCommand,
) => Effect.Effect<void, Error, FileSystem.FileSystem | HttpClient.HttpClient | Path.Path>;
}) {
const project = yield* findActiveProjectTarget({
snapshot,
identifier: flags.project,
});
const nextTitle = yield* resolveProjectTitle(project.workspaceRoot, flags.title);
if (nextTitle === project.title) {
return `Project ${project.id} is already named ${nextTitle}.`;
}
yield* dispatch({
type: "project.meta.update",
commandId: CommandId.make(yield* projectCommandUuid),
projectId: project.id,
title: nextTitle,
});
return `Renamed project ${project.id} to ${nextTitle}.`;
}),
),
),
);
export const projectCommand = Command.make("project").pipe(
Command.withDescription("Manage projects."),
Command.withSubcommands([projectAddCommand, projectRemoveCommand, projectRenameCommand]),
);
export { ProjectCommandIdGenerationError } from "./projectErrors.ts";
export { ProjectLiveServerDeclaredResponseError } from "./projectErrors.ts";
export { ProjectLiveServerUndeclaredStatusError } from "./projectErrors.ts";
export { ProjectLiveServerRequestError } from "./projectErrors.ts";
export { ProjectTitleEmptyError } from "./projectErrors.ts";
export { ProjectIdentifierEmptyError } from "./projectErrors.ts";
export { ProjectNotFoundError } from "./projectErrors.ts";
export { ProjectAlreadyExistsError } from "./projectErrors.ts";
export { ProjectCommandError } from "./projectErrors.ts";
export { projectCommandErrorFromLiveServerRequest } from "./projectErrors.ts";