+36
![github-actions[bot]](/assets/img/avatar_default.png)








Julius Marminge
maria-rcks
Bilal Bakr
shivam
Vitalii Yehorov
Jake Leventhal
Claude Opus 5
Alex Southwell
github-actions[bot]
Nicholas Wasmiller
PB
Exotic
Yash Singh
Devin
Theo Browne
Gabriel De Andrade
Dara Adedeji
scratchyone
Dominic Roy
chukfinley
Primož Ajdišek
benthecarman
NaveDanan
aaditagrawal
Aditya Garud
Nick Anisimov
MacKinley Smith
Yordis Prieto
t3-code[bot]
AKolenda
Guillermo Casanova
Otavio Salvador
Shirish Pothi
Ishaan Kothari
Bob Fowler
Anton Bezdenezhnykh
ValeraZSD
Ephraim
Ryan Ilano
Alex
maco
Cursor
Tristan Knight
PR Batch Tester
oliver
kamkm
de34391427
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Bilal Bakr <[email protected]> Co-authored-by: shivam <[email protected]> Co-authored-by: Vitalii Yehorov <[email protected]> Co-authored-by: Jake Leventhal <[email protected]> Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-authored-by: Alex Southwell <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Nicholas Wasmiller <[email protected]> Co-authored-by: PB <[email protected]> Co-authored-by: Exotic <[email protected]> Co-authored-by: Yash Singh <[email protected]> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Theo Browne <[email protected]> Co-authored-by: Gabriel De Andrade <[email protected]> Co-authored-by: Dara Adedeji <[email protected]> Co-authored-by: scratchyone <[email protected]> Co-authored-by: Dominic Roy <[email protected]> Co-authored-by: chukfinley <[email protected]> Co-authored-by: Primož Ajdišek <[email protected]> Co-authored-by: benthecarman <[email protected]> Co-authored-by: NaveDanan <[email protected]> Co-authored-by: aaditagrawal <[email protected]> Co-authored-by: Aditya Garud <[email protected]> Co-authored-by: Nick Anisimov <[email protected]> Co-authored-by: MacKinley Smith <[email protected]> Co-authored-by: Yordis Prieto <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: AKolenda <[email protected]> Co-authored-by: Guillermo Casanova <[email protected]> Co-authored-by: Otavio Salvador <[email protected]> Co-authored-by: Shirish Pothi <[email protected]> Co-authored-by: Ishaan Kothari <[email protected]> Co-authored-by: Bob Fowler <[email protected]> Co-authored-by: Anton Bezdenezhnykh <[email protected]> Co-authored-by: ValeraZSD <[email protected]> Co-authored-by: Ephraim <[email protected]> Co-authored-by: Ryan Ilano <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: maco <[email protected]> Co-authored-by: Cursor <[email protected]> Co-authored-by: Tristan Knight <[email protected]> Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: oliver <[email protected]> Co-authored-by: kamkm <[email protected]> Signed-off-by: Yordis Prieto <[email protected]>
242 lines
8.8 KiB
TypeScript
242 lines
8.8 KiB
TypeScript
import {
|
|
type CommandId,
|
|
type EventId,
|
|
MAX_SCRIPT_ID_LENGTH,
|
|
type ModelSelection,
|
|
type ProjectIconOverride,
|
|
ProjectId,
|
|
type ProjectScript,
|
|
SCRIPT_RUN_COMMAND_PATTERN,
|
|
type ThreadEnvMode,
|
|
} from "@t3tools/contracts";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Result from "effect/Result";
|
|
import * as Schema from "effect/Schema";
|
|
|
|
import type { UnsequencedProjectEvent } from "../persistence/Services/OrchestrationEventStore.ts";
|
|
import type { ProjectRow } from "./ProjectStore.ts";
|
|
|
|
export interface ProjectCreateCommand {
|
|
readonly type: "project.create";
|
|
readonly commandId: CommandId;
|
|
readonly projectId: ProjectId;
|
|
readonly title: string;
|
|
readonly workspaceRoot: string;
|
|
readonly scripts?: ReadonlyArray<ProjectScript>;
|
|
}
|
|
|
|
export interface ProjectMetaUpdateCommand {
|
|
readonly type: "project.meta.update";
|
|
readonly commandId: CommandId;
|
|
readonly projectId: ProjectId;
|
|
readonly title?: string;
|
|
readonly workspaceRoot?: string;
|
|
readonly defaultModelSelection?: ModelSelection | null;
|
|
readonly defaultThreadEnvMode?: ThreadEnvMode | null;
|
|
readonly autoPull?: boolean;
|
|
readonly faviconPath?: string | null;
|
|
readonly projectIcon?: ProjectIconOverride | null;
|
|
readonly scripts?: ReadonlyArray<ProjectScript>;
|
|
}
|
|
|
|
export interface ProjectDeleteCommand {
|
|
readonly type: "project.delete";
|
|
readonly commandId: CommandId;
|
|
readonly projectId: ProjectId;
|
|
}
|
|
|
|
export type ProjectCommand = ProjectCreateCommand | ProjectMetaUpdateCommand | ProjectDeleteCommand;
|
|
|
|
export class ProjectCommandInvariantError extends Schema.TaggedError<ProjectCommandInvariantError>()(
|
|
"ProjectCommandInvariantError",
|
|
{
|
|
commandType: Schema.String,
|
|
detail: Schema.String,
|
|
},
|
|
) {
|
|
override get message(): string {
|
|
return `Project command invariant failed (${this.commandType}): ${this.detail}`;
|
|
}
|
|
}
|
|
|
|
/** The command targets a project that does not exist or was deleted. */
|
|
export class ProjectCommandMissingProjectError extends Schema.TaggedError<ProjectCommandMissingProjectError>()(
|
|
"ProjectCommandMissingProjectError",
|
|
{
|
|
commandType: Schema.String,
|
|
projectId: ProjectId,
|
|
},
|
|
) {
|
|
override get message(): string {
|
|
return `Project '${this.projectId}' does not exist for command '${this.commandType}'.`;
|
|
}
|
|
}
|
|
|
|
export class ProjectWorkspaceConflictError extends Schema.TaggedError<ProjectWorkspaceConflictError>()(
|
|
"ProjectWorkspaceConflictError",
|
|
{
|
|
workspaceRoot: Schema.String,
|
|
conflictingProjectId: ProjectId,
|
|
},
|
|
) {
|
|
override get message(): string {
|
|
return `Active project '${this.conflictingProjectId}' already exists for workspace root '${this.workspaceRoot}'.`;
|
|
}
|
|
}
|
|
|
|
export const ProjectCommandRejection = Schema.Union([
|
|
ProjectCommandInvariantError,
|
|
ProjectCommandMissingProjectError,
|
|
ProjectWorkspaceConflictError,
|
|
]);
|
|
export type ProjectCommandRejection = typeof ProjectCommandRejection.Type;
|
|
|
|
const ProjectCommandRejectionJson = Schema.fromJsonString(ProjectCommandRejection);
|
|
/**
|
|
* A rejected receipt stores its rejection as JSON, so a retried command id
|
|
* replays the same typed error even when a fresh plan would now succeed.
|
|
*/
|
|
export const encodeProjectCommandRejection = Schema.encodeSync(ProjectCommandRejectionJson);
|
|
/** None for receipts that predate structured rejections. */
|
|
export const decodeProjectCommandRejection = Schema.decodeUnknownOption(
|
|
ProjectCommandRejectionJson,
|
|
);
|
|
|
|
export interface ProjectCommandState {
|
|
/** The target project's row, including a soft-deleted one; only create sees deleted rows as taken. */
|
|
readonly project: ProjectRow | undefined;
|
|
/** The active project that holds the command's requested workspace root, if any. */
|
|
readonly workspaceOwner: ProjectRow | undefined;
|
|
}
|
|
|
|
const monogramSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
const isScriptRunCommand = Schema.is(SCRIPT_RUN_COMMAND_PATTERN);
|
|
|
|
/**
|
|
* Decide one project command against the rows it touches. The caller reads
|
|
* `state` under the project's lock and commits the planned event.
|
|
*/
|
|
export function planProjectCommand(input: {
|
|
readonly command: ProjectCommand;
|
|
readonly state: ProjectCommandState;
|
|
readonly eventId: EventId;
|
|
readonly now: DateTime.Utc;
|
|
}): Result.Result<UnsequencedProjectEvent, ProjectCommandRejection> {
|
|
const { command, state } = input;
|
|
const invariant = (detail: string) =>
|
|
Result.fail(new ProjectCommandInvariantError({ commandType: command.type, detail }));
|
|
const missingProject = () =>
|
|
Result.fail(
|
|
new ProjectCommandMissingProjectError({
|
|
commandType: command.type,
|
|
projectId: command.projectId,
|
|
}),
|
|
);
|
|
const activeProject = state.project?.deletedAt === null ? state.project : undefined;
|
|
const requireWorkspaceAvailable = (workspaceRoot: string) =>
|
|
state.workspaceOwner === undefined || state.workspaceOwner.projectId === command.projectId
|
|
? undefined
|
|
: new ProjectWorkspaceConflictError({
|
|
workspaceRoot,
|
|
conflictingProjectId: state.workspaceOwner.projectId,
|
|
});
|
|
const occurredAt = DateTime.formatIso(input.now);
|
|
const base = {
|
|
eventId: input.eventId,
|
|
aggregateKind: "project" as const,
|
|
aggregateId: command.projectId,
|
|
occurredAt,
|
|
commandId: command.commandId,
|
|
causationEventId: null,
|
|
correlationId: command.commandId,
|
|
metadata: {},
|
|
};
|
|
|
|
switch (command.type) {
|
|
case "project.create": {
|
|
if (state.project !== undefined) {
|
|
return invariant(
|
|
`Project '${command.projectId}' already exists and cannot be created twice.`,
|
|
);
|
|
}
|
|
const conflict = requireWorkspaceAvailable(command.workspaceRoot);
|
|
if (conflict !== undefined) return Result.fail(conflict);
|
|
return Result.succeed({
|
|
...base,
|
|
type: "project.created",
|
|
payload: {
|
|
projectId: command.projectId,
|
|
title: command.title,
|
|
workspaceRoot: command.workspaceRoot,
|
|
// Project creation has no user model choice. Older clients sent an
|
|
// automatic seed, but only a metadata update records an explicit default.
|
|
defaultModelSelection: null,
|
|
faviconPath: null,
|
|
projectIcon: null,
|
|
scripts: command.scripts ?? [],
|
|
createdAt: occurredAt,
|
|
updatedAt: occurredAt,
|
|
},
|
|
});
|
|
}
|
|
|
|
case "project.meta.update": {
|
|
const project = activeProject;
|
|
if (project === undefined) return missingProject();
|
|
if (
|
|
command.projectIcon?.kind === "monogram" &&
|
|
Array.from(monogramSegmenter.segment(command.projectIcon.text)).length > 2
|
|
) {
|
|
return invariant("Project monograms must contain at most two characters.");
|
|
}
|
|
if (command.scripts !== undefined) {
|
|
// Persisted IDs predate shortcut validation. Let users edit or remove them
|
|
// without allowing another invalid ID to enter the project.
|
|
const existingIds = new Set(project.scripts.map((script) => script.id));
|
|
for (const script of command.scripts) {
|
|
if (!existingIds.has(script.id) && !isScriptRunCommand(`script.${script.id}.run`)) {
|
|
// The raw ID is unbounded user input and this detail is persisted.
|
|
return invariant(
|
|
`Script IDs must be 1-${MAX_SCRIPT_ID_LENGTH} lowercase letters, digits or hyphens, starting with a letter or digit (got ${script.id.length} characters).`,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
if (command.workspaceRoot !== undefined) {
|
|
const conflict = requireWorkspaceAvailable(command.workspaceRoot);
|
|
if (conflict !== undefined) return Result.fail(conflict);
|
|
}
|
|
return Result.succeed({
|
|
...base,
|
|
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.autoPull === undefined ? {} : { autoPull: command.autoPull }),
|
|
...(command.faviconPath === undefined ? {} : { faviconPath: command.faviconPath }),
|
|
...(command.projectIcon === undefined ? {} : { projectIcon: command.projectIcon }),
|
|
...(command.scripts === undefined ? {} : { scripts: command.scripts }),
|
|
updatedAt: occurredAt,
|
|
},
|
|
});
|
|
}
|
|
|
|
case "project.delete": {
|
|
if (activeProject === undefined) return missingProject();
|
|
// Thread children are deleted by ProjectService before this event commits.
|
|
return Result.succeed({
|
|
...base,
|
|
type: "project.deleted",
|
|
payload: { projectId: command.projectId, deletedAt: occurredAt },
|
|
});
|
|
}
|
|
}
|
|
}
|