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 { type OrchestrationCommand, type OrchestrationReadModel } from "@akeru/contracts";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
import type * as PlatformError from "effect/PlatformError";
import { OrchestrationCommandInvariantError } from "../Errors.ts";
import type { OrchestrationDispatchActor } from "../Services/OrchestrationEngine.ts";
import {
listThreadsByProjectId,
requireProjectWorkspaceRootAbsent,
requireProject,
requireProjectAbsent,
} from "../commandInvariants.ts";
import {
withEventBase,
nowIso,
type DecideOrchestrationCommandResult,
type DecideCommandSequence,
} from "./eventBase.ts";
export const decideProjects = Effect.fn("decideProjects")(function* ({
command,
readModel,
actor,
decideCommandSequence,
}: {
readonly command: Extract<
OrchestrationCommand,
{ type: "project.create" | "project.meta.update" | "project.delete" }
>;
readonly readModel: OrchestrationReadModel;
readonly actor?: OrchestrationDispatchActor;
readonly decideCommandSequence: DecideCommandSequence;
}): Effect.fn.Return<
DecideOrchestrationCommandResult,
OrchestrationCommandInvariantError | PlatformError.PlatformError,
Crypto.Crypto
> {
switch (command.type) {
case "project.create": {
yield* requireProjectAbsent({
readModel,
command,
projectId: command.projectId,
});
yield* requireProjectWorkspaceRootAbsent({
readModel,
command,
workspaceRoot: command.workspaceRoot,
exceptProjectId: command.projectId,
});
return {
...(yield* withEventBase({
aggregateKind: "project",
aggregateId: command.projectId,
occurredAt: command.createdAt,
commandId: command.commandId,
})),
type: "project.created",
payload: {
projectId: command.projectId,
title: command.title,
workspaceRoot: command.workspaceRoot,
defaultModelSelection: command.defaultModelSelection ?? null,
faviconPath: null,
scripts: [],
createdAt: command.createdAt,
updatedAt: command.createdAt,
},
};
}
case "project.meta.update": {
yield* requireProject({
readModel,
command,
projectId: command.projectId,
});
if (command.workspaceRoot !== undefined) {
yield* requireProjectWorkspaceRootAbsent({
readModel,
command,
workspaceRoot: command.workspaceRoot,
exceptProjectId: command.projectId,
});
}
const occurredAt = yield* nowIso;
return {
...(yield* withEventBase({
aggregateKind: "project",
aggregateId: command.projectId,
occurredAt,
commandId: command.commandId,
})),
type: "project.meta-updated",
payload: {
projectId: command.projectId,
...(command.title !== undefined ? { title: command.title } : {}),
...(command.workspaceRoot !== undefined ? { workspaceRoot: command.workspaceRoot } : {}),
...(command.defaultModelSelection !== undefined
? { defaultModelSelection: command.defaultModelSelection }
: {}),
...(command.defaultThreadEnvMode !== undefined
? { defaultThreadEnvMode: command.defaultThreadEnvMode }
: {}),
...(command.faviconPath !== undefined ? { faviconPath: command.faviconPath } : {}),
...(command.scripts !== undefined ? { scripts: command.scripts } : {}),
updatedAt: occurredAt,
},
};
}
case "project.delete": {
yield* requireProject({
readModel,
command,
projectId: command.projectId,
});
const activeThreads = listThreadsByProjectId(readModel, command.projectId).filter(
(thread) => thread.deletedAt === null,
);
if (activeThreads.length > 0 && command.force !== true) {
return yield* new OrchestrationCommandInvariantError({
commandType: command.type,
detail: `Project '${command.projectId}' is not empty and cannot be deleted without force=true.`,
});
}
if (activeThreads.length > 0) {
return yield* decideCommandSequence({
readModel,
...(actor !== undefined ? { actor } : {}),
commands: [
...activeThreads.map(
(thread): Extract<OrchestrationCommand, { type: "thread.delete" }> => ({
type: "thread.delete",
commandId: command.commandId,
threadId: thread.id,
}),
),
{
type: "project.delete",
commandId: command.commandId,
projectId: command.projectId,
},
],
});
}
const occurredAt = yield* nowIso;
return {
...(yield* withEventBase({
aggregateKind: "project",
aggregateId: command.projectId,
occurredAt,
commandId: command.commandId,
})),
type: "project.deleted" as const,
payload: {
projectId: command.projectId,
deletedAt: occurredAt,
},
};
}
}
});