Files
paperclip/server/src/services/projects.ts
T
scotttong eaef47f4c7 Information Architecture + project/agent visual refresh (experimental) (#7543)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - The board UI is the control surface for issues, projects, agents,
goals, workspaces, and operator settings.
> - The existing navigation and list surfaces make several
high-frequency workflows feel harder to scan than they should,
especially around projects and agents.
> - The product direction is to improve those surfaces without breaking
the existing route model or forcing a new IA on every operator at once.
> - This pull request now keeps the dependent IA, project identity, and
agent-list visual refresh work together while the Issue-to-Task copy
migration is split into #7651.
> - The benefit is a clearer left nav, better project identity, denser
agent/project list rows, and brand-aligned status treatment while
preserving the classic default experience behind a flag.

## Linked Issues or Issue Description

Refs #7645
Refs #7651

Internal planning/work references: PAP-53, PAP-56, PAP-58, PAP-59,
PAP-60, PAP-61, PAP-68, PAP-69, PAP-70, PAP-71, PAP-72, PAP-75, PAP-76,
PAP-80, PAP-85, PAP-86, PAP-87, PAP-88, PAP-89.

## What Changed

- Adds `enableStreamlinedLeftNavigation`, defaulting off, and gates
sidebar presentation so classic navigation remains the default.
- Adds project icon persistence, validation, portability, picker UI, and
`ProjectTile` rendering while defaulting new projects to neutral gray.
- Adds projects-list task-count and budget summary data with focused
server/shared/UI coverage.
- Refreshes agent list rows, row actions, active/recent sidebar
behavior, and status capsule/chip styling for the approved brand state
system.
- Removes the placeholder Conference room and Artifacts nav/routes from
the finalized experimental nav direction.
- Removes `pnpm-lock.yaml` and the Issue-to-Task copy migration from
this PR diff; the copy migration now lives in #7651.

## Verification

- Existing branch verification from the authored commits: UI typecheck,
targeted unit tests, and light/dark visual checks for `/agents`, agent
detail, and design-guide status states.
- Maintainer cleanup verification on `75e34e5`: `git diff --check
origin/master...HEAD` passed, the `design/` diff is empty, and the PR
diff is 61 files, below Greptile's 100-file review limit.
- `pnpm --filter @paperclipai/ui build` passed.
- `NODE_ENV=test pnpm exec vitest run
ui/src/components/Sidebar.test.tsx` passed: 1 file, 8 tests.
- CI and Greptile should rerun on the latest push.

## Risks

- Broad UI surface area: the experimental flag keeps the classic nav
default, but changed shared components such as `EntityRow`,
`ProjectTile`, and agent status badges could affect multiple pages.
- Database migration: `projects.icon` is additive and nullable, but
migration ordering and portability import/export must stay aligned.
- The Issue-to-Task copy migration is now separated into #7651, so
reviewers should evaluate this PR as IA/project/agent presentation work
only.
- Visual regressions are possible across smaller widths because the PR
intentionally changes dense list-row layouts.

## Model Used

Claude Opus 4.8 assisted the original feature commits.
Paperclip-Paperclip agents assisted some planning/design commits. Codex
/ GPT-5-class coding agent with shell, GitHub CLI, and repository access
performed this PR-readiness cleanup and split.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] If this change affects the UI, I have included before/after
screenshots
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Dotta <bippadotta@protonmail.com>
2026-06-06 09:17:27 -05:00

1216 lines
42 KiB
TypeScript

import { and, asc, desc, eq, inArray, sql } from "drizzle-orm";
import type { Db } from "@paperclipai/db";
import {
projects,
projectGoals,
goals,
issues,
budgetPolicies,
pluginManagedResources,
plugins,
projectWorkspaces,
workspaceRuntimeServices,
} from "@paperclipai/db";
import {
deriveProjectUrlKey,
hasNonAsciiContent,
isUuidLike,
normalizeProjectUrlKey,
type BudgetWindowKind,
type ProjectBudgetSummary,
type ProjectCodebase,
type ProjectExecutionWorkspacePolicy,
type ProjectGoalRef,
type ProjectManagedByPlugin,
type ProjectWorkspaceRuntimeConfig,
type ProjectWorkspace,
type WorkspaceRuntimeService,
type PluginManagedProjectDeclaration,
type PluginManagedProjectResolution,
} from "@paperclipai/shared";
import { listCurrentRuntimeServicesForProjectWorkspaces } from "./workspace-runtime-read-model.js";
import { parseProjectExecutionWorkspacePolicy } from "./execution-workspace-policy.js";
import { mergeProjectWorkspaceRuntimeConfig, readProjectWorkspaceRuntimeConfig } from "./project-workspace-runtime-config.js";
import { resolveManagedProjectWorkspaceDir } from "../home-paths.js";
type ProjectRow = typeof projects.$inferSelect;
type ProjectWorkspaceRow = typeof projectWorkspaces.$inferSelect;
type WorkspaceRuntimeServiceRow = typeof workspaceRuntimeServices.$inferSelect;
const REPO_ONLY_CWD_SENTINEL = "/__paperclip_repo_only__";
type CreateWorkspaceInput = {
name?: string | null;
sourceType?: string | null;
cwd?: string | null;
repoUrl?: string | null;
repoRef?: string | null;
defaultRef?: string | null;
visibility?: string | null;
setupCommand?: string | null;
cleanupCommand?: string | null;
remoteProvider?: string | null;
remoteWorkspaceRef?: string | null;
sharedWorkspaceKey?: string | null;
metadata?: Record<string, unknown> | null;
runtimeConfig?: Partial<ProjectWorkspaceRuntimeConfig> | null;
isPrimary?: boolean;
};
type UpdateWorkspaceInput = Partial<CreateWorkspaceInput>;
interface ProjectWithGoals extends Omit<ProjectRow, "executionWorkspacePolicy"> {
urlKey: string;
goalIds: string[];
goals: ProjectGoalRef[];
executionWorkspacePolicy: ProjectExecutionWorkspacePolicy | null;
codebase: ProjectCodebase;
workspaces: ProjectWorkspace[];
primaryWorkspace: ProjectWorkspace | null;
managedByPlugin: ProjectManagedByPlugin | null;
taskCount?: number;
budget?: ProjectBudgetSummary | null;
}
interface ProjectShortnameRow {
id: string;
name: string;
}
interface ResolveProjectNameOptions {
excludeProjectId?: string | null;
}
/** Batch-load goal refs for a set of projects. */
async function attachGoals(db: Db, rows: ProjectRow[]): Promise<ProjectWithGoals[]> {
if (rows.length === 0) return [];
const projectIds = rows.map((r) => r.id);
// Fetch join rows + goal titles in one query
const links = await db
.select({
projectId: projectGoals.projectId,
goalId: projectGoals.goalId,
goalTitle: goals.title,
})
.from(projectGoals)
.innerJoin(goals, eq(projectGoals.goalId, goals.id))
.where(inArray(projectGoals.projectId, projectIds));
const map = new Map<string, ProjectGoalRef[]>();
for (const link of links) {
let arr = map.get(link.projectId);
if (!arr) {
arr = [];
map.set(link.projectId, arr);
}
arr.push({ id: link.goalId, title: link.goalTitle });
}
return rows.map((r) => {
const g = map.get(r.id) ?? [];
return {
...r,
urlKey: deriveProjectUrlKey(r.name, r.id),
goalIds: g.map((x) => x.id),
goals: g,
executionWorkspacePolicy: parseProjectExecutionWorkspacePolicy(r.executionWorkspacePolicy),
} as ProjectWithGoals;
});
}
function toRuntimeService(row: WorkspaceRuntimeServiceRow): WorkspaceRuntimeService {
return {
id: row.id,
companyId: row.companyId,
projectId: row.projectId ?? null,
projectWorkspaceId: row.projectWorkspaceId ?? null,
executionWorkspaceId: row.executionWorkspaceId ?? null,
issueId: row.issueId ?? null,
scopeType: row.scopeType as WorkspaceRuntimeService["scopeType"],
scopeId: row.scopeId ?? null,
serviceName: row.serviceName,
status: row.status as WorkspaceRuntimeService["status"],
lifecycle: row.lifecycle as WorkspaceRuntimeService["lifecycle"],
reuseKey: row.reuseKey ?? null,
command: row.command ?? null,
cwd: row.cwd ?? null,
port: row.port ?? null,
url: row.url ?? null,
provider: row.provider as WorkspaceRuntimeService["provider"],
providerRef: row.providerRef ?? null,
ownerAgentId: row.ownerAgentId ?? null,
startedByRunId: row.startedByRunId ?? null,
lastUsedAt: row.lastUsedAt,
startedAt: row.startedAt,
stoppedAt: row.stoppedAt ?? null,
stopPolicy: (row.stopPolicy as Record<string, unknown> | null) ?? null,
healthStatus: row.healthStatus as WorkspaceRuntimeService["healthStatus"],
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
function toWorkspace(
row: ProjectWorkspaceRow,
runtimeServices: WorkspaceRuntimeService[] = [],
): ProjectWorkspace {
return {
id: row.id,
companyId: row.companyId,
projectId: row.projectId,
name: row.name,
sourceType: row.sourceType as ProjectWorkspace["sourceType"],
cwd: normalizeWorkspaceCwd(row.cwd),
repoUrl: row.repoUrl ?? null,
repoRef: row.repoRef ?? null,
defaultRef: row.defaultRef ?? row.repoRef ?? null,
visibility: row.visibility as ProjectWorkspace["visibility"],
setupCommand: row.setupCommand ?? null,
cleanupCommand: row.cleanupCommand ?? null,
remoteProvider: row.remoteProvider ?? null,
remoteWorkspaceRef: row.remoteWorkspaceRef ?? null,
sharedWorkspaceKey: row.sharedWorkspaceKey ?? null,
metadata: (row.metadata as Record<string, unknown> | null) ?? null,
runtimeConfig: readProjectWorkspaceRuntimeConfig((row.metadata as Record<string, unknown> | null) ?? null),
isPrimary: row.isPrimary,
runtimeServices,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
function deriveRepoNameFromRepoUrl(repoUrl: string | null): string | null {
const raw = readNonEmptyString(repoUrl);
if (!raw) return null;
try {
const parsed = new URL(raw);
const cleanedPath = parsed.pathname.replace(/\/+$/, "");
const repoName = cleanedPath.split("/").filter(Boolean).pop()?.replace(/\.git$/i, "") ?? "";
return repoName || null;
} catch {
return null;
}
}
function deriveProjectCodebase(input: {
companyId: string;
projectId: string;
primaryWorkspace: ProjectWorkspace | null;
fallbackWorkspaces: ProjectWorkspace[];
}): ProjectCodebase {
const primaryWorkspace = input.primaryWorkspace ?? input.fallbackWorkspaces[0] ?? null;
const repoUrl = primaryWorkspace?.repoUrl ?? null;
const repoName = deriveRepoNameFromRepoUrl(repoUrl);
const localFolder = primaryWorkspace?.cwd ?? null;
const managedFolder = resolveManagedProjectWorkspaceDir({
companyId: input.companyId,
projectId: input.projectId,
repoName,
});
return {
workspaceId: primaryWorkspace?.id ?? null,
repoUrl,
repoRef: primaryWorkspace?.repoRef ?? null,
defaultRef: primaryWorkspace?.defaultRef ?? null,
repoName,
localFolder,
managedFolder,
effectiveLocalFolder: localFolder ?? managedFolder,
origin: localFolder ? "local_folder" : "managed_checkout",
};
}
function pickPrimaryWorkspace(
rows: ProjectWorkspaceRow[],
runtimeServicesByWorkspaceId?: Map<string, WorkspaceRuntimeService[]>,
): ProjectWorkspace | null {
if (rows.length === 0) return null;
const explicitPrimary = rows.find((row) => row.isPrimary);
const primary = explicitPrimary ?? rows[0];
return toWorkspace(primary, runtimeServicesByWorkspaceId?.get(primary.id) ?? []);
}
/** Batch-load workspace refs for a set of projects. */
async function attachWorkspaces(db: Db, rows: ProjectWithGoals[]): Promise<ProjectWithGoals[]> {
if (rows.length === 0) return [];
const projectIds = rows.map((r) => r.id);
const workspaceRows = await db
.select()
.from(projectWorkspaces)
.where(inArray(projectWorkspaces.projectId, projectIds))
.orderBy(desc(projectWorkspaces.isPrimary), asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id));
const runtimeServicesByWorkspaceId = await listCurrentRuntimeServicesForProjectWorkspaces(
db,
rows[0]!.companyId,
workspaceRows.map((workspace) => workspace.id),
);
const sharedRuntimeServicesByWorkspaceId = new Map(
Array.from(runtimeServicesByWorkspaceId.entries()).map(([workspaceId, services]) => [
workspaceId,
services.map(toRuntimeService),
]),
);
const map = new Map<string, ProjectWorkspaceRow[]>();
for (const row of workspaceRows) {
let arr = map.get(row.projectId);
if (!arr) {
arr = [];
map.set(row.projectId, arr);
}
arr.push(row);
}
const managedRows = await db
.select({
id: pluginManagedResources.id,
pluginId: pluginManagedResources.pluginId,
pluginKey: pluginManagedResources.pluginKey,
manifestJson: plugins.manifestJson,
resourceKind: pluginManagedResources.resourceKind,
resourceKey: pluginManagedResources.resourceKey,
resourceId: pluginManagedResources.resourceId,
defaultsJson: pluginManagedResources.defaultsJson,
createdAt: pluginManagedResources.createdAt,
updatedAt: pluginManagedResources.updatedAt,
})
.from(pluginManagedResources)
.innerJoin(plugins, eq(pluginManagedResources.pluginId, plugins.id))
.where(and(
eq(pluginManagedResources.resourceKind, "project"),
inArray(pluginManagedResources.resourceId, projectIds),
));
const managedByProjectId = new Map<string, ProjectManagedByPlugin>();
for (const row of managedRows) {
managedByProjectId.set(row.resourceId, {
id: row.id,
pluginId: row.pluginId,
pluginKey: row.pluginKey,
pluginDisplayName: row.manifestJson.displayName ?? row.pluginKey,
resourceKind: "project",
resourceKey: row.resourceKey,
defaultsJson: row.defaultsJson,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
});
}
return rows.map((row) => {
const projectWorkspaceRows = map.get(row.id) ?? [];
const workspaces = projectWorkspaceRows.map((workspace) =>
toWorkspace(
workspace,
sharedRuntimeServicesByWorkspaceId.get(workspace.id) ?? [],
),
);
const primaryWorkspace = pickPrimaryWorkspace(projectWorkspaceRows, sharedRuntimeServicesByWorkspaceId);
return {
...row,
codebase: deriveProjectCodebase({
companyId: row.companyId,
projectId: row.id,
primaryWorkspace,
fallbackWorkspaces: workspaces,
}),
workspaces,
primaryWorkspace,
managedByPlugin: managedByProjectId.get(row.id) ?? null,
};
});
}
type TaskCountRow = { projectId: string | null; count: number };
type ProjectBudgetRow = { scopeId: string; amount: number; windowKind: string };
/**
* Build the per-project task-count and budget lookups from the aggregate query
* rows. Pure (no DB) so the merge logic can be unit-tested in isolation.
* Only active policies with a positive amount surface as a budget.
*/
export function buildProjectListMetricMaps(taskCountRows: TaskCountRow[], budgetRows: ProjectBudgetRow[]) {
const taskCountByProjectId = new Map<string, number>();
for (const row of taskCountRows) {
if (row.projectId) taskCountByProjectId.set(row.projectId, Number(row.count) || 0);
}
const budgetByProjectId = new Map<string, ProjectBudgetSummary>();
for (const row of budgetRows) {
if (row.amount > 0) {
budgetByProjectId.set(row.scopeId, {
amountCents: row.amount,
windowKind: row.windowKind as BudgetWindowKind,
});
}
}
return { taskCountByProjectId, budgetByProjectId };
}
/**
* Attach lightweight list-only metrics (task count + budget) to a set of
* projects using two aggregate queries (no N+1). Used by the projects list
* view (IA Phase 4 — PAP-60).
*/
async function attachListMetrics(
db: Db,
companyId: string,
rows: ProjectWithGoals[],
): Promise<ProjectWithGoals[]> {
if (rows.length === 0) return rows;
const projectIds = rows.map((r) => r.id);
const [taskCountRows, budgetRows] = await Promise.all([
db
.select({
projectId: issues.projectId,
count: sql<number>`count(*)::int`,
})
.from(issues)
.where(and(eq(issues.companyId, companyId), inArray(issues.projectId, projectIds)))
.groupBy(issues.projectId),
db
.select({
scopeId: budgetPolicies.scopeId,
amount: budgetPolicies.amount,
windowKind: budgetPolicies.windowKind,
})
.from(budgetPolicies)
.where(
and(
eq(budgetPolicies.companyId, companyId),
eq(budgetPolicies.scopeType, "project"),
eq(budgetPolicies.metric, "billed_cents"),
eq(budgetPolicies.isActive, true),
inArray(budgetPolicies.scopeId, projectIds),
),
),
]);
const { taskCountByProjectId, budgetByProjectId } = buildProjectListMetricMaps(
taskCountRows,
budgetRows,
);
return rows.map((row) => ({
...row,
taskCount: taskCountByProjectId.get(row.id) ?? 0,
budget: budgetByProjectId.get(row.id) ?? null,
}));
}
/** Sync the project_goals join table for a single project. */
async function syncGoalLinks(db: Db, projectId: string, companyId: string, goalIds: string[]) {
// Delete existing links
await db.delete(projectGoals).where(eq(projectGoals.projectId, projectId));
// Insert new links
if (goalIds.length > 0) {
await db.insert(projectGoals).values(
goalIds.map((goalId) => ({ projectId, goalId, companyId })),
);
}
}
/** Resolve goalIds from input, handling the legacy goalId field. */
function resolveGoalIds(data: { goalIds?: string[]; goalId?: string | null }): string[] | undefined {
if (data.goalIds !== undefined) return data.goalIds;
if (data.goalId !== undefined) {
return data.goalId ? [data.goalId] : [];
}
return undefined;
}
function readNonEmptyString(value: unknown): string | null {
if (typeof value !== "string") return null;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : null;
}
function normalizeWorkspaceCwd(value: unknown): string | null {
const cwd = readNonEmptyString(value);
if (!cwd) return null;
return cwd === REPO_ONLY_CWD_SENTINEL ? null : cwd;
}
function deriveNameFromCwd(cwd: string): string {
const normalized = cwd.replace(/[\\/]+$/, "");
const segments = normalized.split(/[\\/]/).filter(Boolean);
return segments[segments.length - 1] ?? "Local folder";
}
function deriveNameFromRepoUrl(repoUrl: string): string {
try {
const url = new URL(repoUrl);
const cleanedPath = url.pathname.replace(/\/+$/, "");
const lastSegment = cleanedPath.split("/").filter(Boolean).pop() ?? "";
const noGitSuffix = lastSegment.replace(/\.git$/i, "");
return noGitSuffix || repoUrl;
} catch {
return repoUrl;
}
}
function deriveWorkspaceName(input: {
name?: string | null;
cwd?: string | null;
repoUrl?: string | null;
}) {
const explicit = readNonEmptyString(input.name);
if (explicit) return explicit;
const cwd = readNonEmptyString(input.cwd);
if (cwd) return deriveNameFromCwd(cwd);
const repoUrl = readNonEmptyString(input.repoUrl);
if (repoUrl) return deriveNameFromRepoUrl(repoUrl);
return "Workspace";
}
function buildManagedProjectDefaults(declaration: PluginManagedProjectDeclaration) {
return {
projectKey: declaration.projectKey,
displayName: declaration.displayName,
description: declaration.description ?? null,
status: declaration.status ?? "in_progress",
color: declaration.color ?? null,
settings: declaration.settings ?? {},
};
}
export function resolveProjectNameForUniqueShortname(
requestedName: string,
existingProjects: ProjectShortnameRow[],
options?: ResolveProjectNameOptions,
): string {
const requestedShortname = normalizeProjectUrlKey(requestedName);
if (!requestedShortname) return requestedName;
// Non-ASCII names get a UUID suffix in deriveProjectUrlKey, making slugs inherently unique.
if (hasNonAsciiContent(requestedName)) return requestedName;
const usedShortnames = new Set(
existingProjects
.filter((project) => !(options?.excludeProjectId && project.id === options.excludeProjectId))
.map((project) => normalizeProjectUrlKey(project.name))
.filter((value): value is string => value !== null),
);
if (!usedShortnames.has(requestedShortname)) return requestedName;
for (let suffix = 2; suffix < 10_000; suffix += 1) {
const candidateName = `${requestedName} ${suffix}`;
const candidateShortname = normalizeProjectUrlKey(candidateName);
if (candidateShortname && !usedShortnames.has(candidateShortname)) {
return candidateName;
}
}
// Fallback guard for pathological naming collisions.
return `${requestedName} ${Date.now()}`;
}
async function ensureSinglePrimaryWorkspace(
dbOrTx: any,
input: {
companyId: string;
projectId: string;
keepWorkspaceId: string;
},
) {
await dbOrTx
.update(projectWorkspaces)
.set({ isPrimary: false, updatedAt: new Date() })
.where(
and(
eq(projectWorkspaces.companyId, input.companyId),
eq(projectWorkspaces.projectId, input.projectId),
),
);
await dbOrTx
.update(projectWorkspaces)
.set({ isPrimary: true, updatedAt: new Date() })
.where(
and(
eq(projectWorkspaces.companyId, input.companyId),
eq(projectWorkspaces.projectId, input.projectId),
eq(projectWorkspaces.id, input.keepWorkspaceId),
),
);
}
export function projectService(db: Db) {
const createProject = async (
companyId: string,
data: Omit<typeof projects.$inferInsert, "companyId"> & { goalIds?: string[] },
): Promise<ProjectWithGoals> => {
const { goalIds: inputGoalIds, ...projectData } = data;
const ids = resolveGoalIds({ goalIds: inputGoalIds, goalId: projectData.goalId });
// Note: color is intentionally NOT auto-assigned. New projects default to
// `color = null` (neutral gray) unless an explicit color is supplied. See PAP-68.
const existingProjects = await db
.select({ id: projects.id, name: projects.name })
.from(projects)
.where(eq(projects.companyId, companyId));
projectData.name = resolveProjectNameForUniqueShortname(projectData.name, existingProjects);
// Also write goalId to the legacy column (first goal or null)
const legacyGoalId = ids && ids.length > 0 ? ids[0] : projectData.goalId ?? null;
const row = await db
.insert(projects)
.values({ ...projectData, goalId: legacyGoalId, companyId })
.returning()
.then((rows) => rows[0]);
if (ids && ids.length > 0) {
await syncGoalLinks(db, row.id, companyId, ids);
}
const [withGoals] = await attachGoals(db, [row]);
const [enriched] = withGoals ? await attachWorkspaces(db, [withGoals]) : [];
return enriched!;
};
const getProjectById = async (id: string): Promise<ProjectWithGoals | null> => {
const row = await db
.select()
.from(projects)
.where(eq(projects.id, id))
.then((rows) => rows[0] ?? null);
if (!row) return null;
const [withGoals] = await attachGoals(db, [row]);
if (!withGoals) return null;
const [enriched] = await attachWorkspaces(db, [withGoals]);
return enriched ?? null;
};
return {
list: async (companyId: string): Promise<ProjectWithGoals[]> => {
const rows = await db.select().from(projects).where(eq(projects.companyId, companyId));
const withGoals = await attachGoals(db, rows);
const withWorkspaces = await attachWorkspaces(db, withGoals);
return attachListMetrics(db, companyId, withWorkspaces);
},
listByIds: async (companyId: string, ids: string[]): Promise<ProjectWithGoals[]> => {
const dedupedIds = [...new Set(ids)];
if (dedupedIds.length === 0) return [];
const rows = await db
.select()
.from(projects)
.where(and(eq(projects.companyId, companyId), inArray(projects.id, dedupedIds)));
const withGoals = await attachGoals(db, rows);
const withWorkspaces = await attachWorkspaces(db, withGoals);
const byId = new Map(withWorkspaces.map((project) => [project.id, project]));
return dedupedIds.map((id) => byId.get(id)).filter((project): project is ProjectWithGoals => Boolean(project));
},
getById: getProjectById,
resolveManagedProject: async (input: {
companyId: string;
pluginId: string;
pluginKey: string;
projectKey: string;
reset?: boolean;
createIfMissing?: boolean;
}): Promise<PluginManagedProjectResolution> => {
const plugin = await db
.select({ id: plugins.id, pluginKey: plugins.pluginKey, manifestJson: plugins.manifestJson })
.from(plugins)
.where(eq(plugins.id, input.pluginId))
.then((rows) => rows[0] ?? null);
if (!plugin || plugin.pluginKey !== input.pluginKey) {
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: null,
project: null,
status: "missing",
};
}
const declaration = plugin.manifestJson.projects?.find((project) => project.projectKey === input.projectKey);
if (!declaration) {
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: null,
project: null,
status: "missing",
};
}
const defaults = buildManagedProjectDefaults(declaration);
const existingBinding = await db
.select()
.from(pluginManagedResources)
.where(and(
eq(pluginManagedResources.companyId, input.companyId),
eq(pluginManagedResources.pluginId, input.pluginId),
eq(pluginManagedResources.resourceKind, "project"),
eq(pluginManagedResources.resourceKey, input.projectKey),
))
.then((rows) => rows[0] ?? null);
if (existingBinding) {
const existingProject = await db
.select({ id: projects.id })
.from(projects)
.where(and(eq(projects.companyId, input.companyId), eq(projects.id, existingBinding.resourceId)))
.then((rows) => rows[0] ?? null);
if (existingProject) {
if (input.reset) {
await db
.update(projects)
.set({
name: declaration.displayName,
description: declaration.description ?? null,
status: declaration.status ?? "in_progress",
color: declaration.color ?? null,
updatedAt: new Date(),
})
.where(and(eq(projects.companyId, input.companyId), eq(projects.id, existingBinding.resourceId)));
}
if (input.createIfMissing !== false) {
await db
.update(pluginManagedResources)
.set({ defaultsJson: defaults, updatedAt: new Date() })
.where(eq(pluginManagedResources.id, existingBinding.id));
}
const project = await getProjectById(existingBinding.resourceId);
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: project?.id ?? existingBinding.resourceId,
project: project as import("@paperclipai/shared").Project | null,
status: input.reset ? "reset" : "resolved",
};
}
if (input.createIfMissing === false) {
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: null,
project: null,
status: "missing",
};
}
const project = await createProject(input.companyId, {
name: declaration.displayName,
description: declaration.description ?? null,
status: declaration.status ?? "in_progress",
color: declaration.color ?? undefined,
});
await db
.update(pluginManagedResources)
.set({ resourceId: project.id, defaultsJson: defaults, updatedAt: new Date() })
.where(eq(pluginManagedResources.id, existingBinding.id));
const hydrated = await getProjectById(project.id);
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: hydrated?.id ?? project.id,
project: hydrated as import("@paperclipai/shared").Project | null,
status: "relinked",
};
}
if (input.createIfMissing === false) {
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: null,
project: null,
status: "missing",
};
}
const project = await createProject(input.companyId, {
name: declaration.displayName,
description: declaration.description ?? null,
status: declaration.status ?? "in_progress",
color: declaration.color ?? undefined,
});
await db.insert(pluginManagedResources).values({
companyId: input.companyId,
pluginId: input.pluginId,
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
resourceId: project.id,
defaultsJson: defaults,
});
const hydrated = await getProjectById(project.id);
return {
pluginKey: input.pluginKey,
resourceKind: "project",
resourceKey: input.projectKey,
companyId: input.companyId,
projectId: hydrated?.id ?? project.id,
project: hydrated as import("@paperclipai/shared").Project | null,
status: "created",
};
},
create: createProject,
update: async (
id: string,
data: Partial<typeof projects.$inferInsert> & { goalIds?: string[] },
): Promise<ProjectWithGoals | null> => {
const { goalIds: inputGoalIds, ...projectData } = data;
const ids = resolveGoalIds({ goalIds: inputGoalIds, goalId: projectData.goalId });
const existingProject = await db
.select({ id: projects.id, companyId: projects.companyId, name: projects.name })
.from(projects)
.where(eq(projects.id, id))
.then((rows) => rows[0] ?? null);
if (!existingProject) return null;
if (projectData.name !== undefined) {
const existingShortname = normalizeProjectUrlKey(existingProject.name);
const nextShortname = normalizeProjectUrlKey(projectData.name);
if (existingShortname !== nextShortname) {
const existingProjects = await db
.select({ id: projects.id, name: projects.name })
.from(projects)
.where(eq(projects.companyId, existingProject.companyId));
projectData.name = resolveProjectNameForUniqueShortname(projectData.name, existingProjects, {
excludeProjectId: id,
});
}
}
// Keep legacy goalId column in sync
const updates: Partial<typeof projects.$inferInsert> = {
...projectData,
updatedAt: new Date(),
};
if (ids !== undefined) {
updates.goalId = ids.length > 0 ? ids[0] : null;
}
const row = await db
.update(projects)
.set(updates)
.where(eq(projects.id, id))
.returning()
.then((rows) => rows[0] ?? null);
if (!row) return null;
if (ids !== undefined) {
await syncGoalLinks(db, id, row.companyId, ids);
}
const [withGoals] = await attachGoals(db, [row]);
const [enriched] = withGoals ? await attachWorkspaces(db, [withGoals]) : [];
return enriched ?? null;
},
clearExecutionWorkspaceEnvironmentSelection: async (companyId: string, environmentId: string) => {
const rows = await db
.select({
id: projects.id,
executionWorkspacePolicy: projects.executionWorkspacePolicy,
})
.from(projects)
.where(eq(projects.companyId, companyId));
let cleared = 0;
for (const row of rows) {
const policy = parseProjectExecutionWorkspacePolicy(row.executionWorkspacePolicy);
if (policy?.environmentId !== environmentId) continue;
await db
.update(projects)
.set({
executionWorkspacePolicy: {
...policy,
environmentId: null,
},
updatedAt: new Date(),
})
.where(eq(projects.id, row.id));
cleared += 1;
}
return cleared;
},
remove: (id: string) =>
db
.delete(projects)
.where(eq(projects.id, id))
.returning()
.then((rows) => {
const row = rows[0] ?? null;
if (!row) return null;
return { ...row, urlKey: deriveProjectUrlKey(row.name, row.id) };
}),
listWorkspaces: async (projectId: string): Promise<ProjectWorkspace[]> => {
const rows = await db
.select()
.from(projectWorkspaces)
.where(eq(projectWorkspaces.projectId, projectId))
.orderBy(desc(projectWorkspaces.isPrimary), asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id));
if (rows.length === 0) return [];
const runtimeServicesByWorkspaceId = await listCurrentRuntimeServicesForProjectWorkspaces(
db,
rows[0]!.companyId,
rows.map((workspace) => workspace.id),
);
return rows.map((row) =>
toWorkspace(
row,
(runtimeServicesByWorkspaceId.get(row.id) ?? []).map(toRuntimeService),
),
);
},
createWorkspace: async (
projectId: string,
data: CreateWorkspaceInput,
): Promise<ProjectWorkspace | null> => {
const project = await db
.select()
.from(projects)
.where(eq(projects.id, projectId))
.then((rows) => rows[0] ?? null);
if (!project) return null;
const cwd = normalizeWorkspaceCwd(data.cwd);
const repoUrl = readNonEmptyString(data.repoUrl);
const sourceType = readNonEmptyString(data.sourceType) ?? (repoUrl ? "git_repo" : cwd ? "local_path" : "remote_managed");
const remoteWorkspaceRef = readNonEmptyString(data.remoteWorkspaceRef);
if (sourceType === "remote_managed") {
if (!remoteWorkspaceRef && !repoUrl) return null;
} else if (!cwd && !repoUrl) {
return null;
}
const name = deriveWorkspaceName({
name: data.name,
cwd,
repoUrl,
});
const existing = await db
.select()
.from(projectWorkspaces)
.where(eq(projectWorkspaces.projectId, projectId))
.orderBy(asc(projectWorkspaces.createdAt))
.then((rows) => rows);
const shouldBePrimary = data.isPrimary === true || existing.length === 0;
const created = await db.transaction(async (tx) => {
if (shouldBePrimary) {
await tx
.update(projectWorkspaces)
.set({ isPrimary: false, updatedAt: new Date() })
.where(
and(
eq(projectWorkspaces.companyId, project.companyId),
eq(projectWorkspaces.projectId, projectId),
),
);
}
const row = await tx
.insert(projectWorkspaces)
.values({
companyId: project.companyId,
projectId,
name,
sourceType,
cwd: cwd ?? null,
repoUrl: repoUrl ?? null,
repoRef: readNonEmptyString(data.repoRef),
defaultRef: readNonEmptyString(data.defaultRef) ?? readNonEmptyString(data.repoRef),
visibility: readNonEmptyString(data.visibility) ?? "default",
setupCommand: readNonEmptyString(data.setupCommand),
cleanupCommand: readNonEmptyString(data.cleanupCommand),
remoteProvider: readNonEmptyString(data.remoteProvider),
remoteWorkspaceRef,
sharedWorkspaceKey: readNonEmptyString(data.sharedWorkspaceKey),
metadata:
data.runtimeConfig !== undefined
? mergeProjectWorkspaceRuntimeConfig(
(data.metadata as Record<string, unknown> | null | undefined) ?? null,
data.runtimeConfig ?? null,
)
: (data.metadata as Record<string, unknown> | null | undefined) ?? null,
isPrimary: shouldBePrimary,
})
.returning()
.then((rows) => rows[0] ?? null);
return row;
});
return created ? toWorkspace(created) : null;
},
updateWorkspace: async (
projectId: string,
workspaceId: string,
data: UpdateWorkspaceInput,
): Promise<ProjectWorkspace | null> => {
const existing = await db
.select()
.from(projectWorkspaces)
.where(
and(
eq(projectWorkspaces.id, workspaceId),
eq(projectWorkspaces.projectId, projectId),
),
)
.then((rows) => rows[0] ?? null);
if (!existing) return null;
const nextCwd =
data.cwd !== undefined
? normalizeWorkspaceCwd(data.cwd)
: normalizeWorkspaceCwd(existing.cwd);
const nextRepoUrl =
data.repoUrl !== undefined
? readNonEmptyString(data.repoUrl)
: readNonEmptyString(existing.repoUrl);
const nextSourceType =
data.sourceType !== undefined
? readNonEmptyString(data.sourceType)
: readNonEmptyString(existing.sourceType);
const nextRemoteWorkspaceRef =
data.remoteWorkspaceRef !== undefined
? readNonEmptyString(data.remoteWorkspaceRef)
: readNonEmptyString(existing.remoteWorkspaceRef);
if (nextSourceType === "remote_managed") {
if (!nextRemoteWorkspaceRef && !nextRepoUrl) return null;
} else if (!nextCwd && !nextRepoUrl) {
return null;
}
const patch: Partial<typeof projectWorkspaces.$inferInsert> = {
updatedAt: new Date(),
};
if (data.name !== undefined) patch.name = deriveWorkspaceName({ name: data.name, cwd: nextCwd, repoUrl: nextRepoUrl });
if (data.name === undefined && (data.cwd !== undefined || data.repoUrl !== undefined)) {
patch.name = deriveWorkspaceName({ cwd: nextCwd, repoUrl: nextRepoUrl });
}
if (data.cwd !== undefined) patch.cwd = nextCwd ?? null;
if (data.repoUrl !== undefined) patch.repoUrl = nextRepoUrl ?? null;
if (data.repoRef !== undefined) patch.repoRef = readNonEmptyString(data.repoRef);
if (data.sourceType !== undefined && nextSourceType) patch.sourceType = nextSourceType;
if (data.defaultRef !== undefined) patch.defaultRef = readNonEmptyString(data.defaultRef);
if (data.visibility !== undefined && readNonEmptyString(data.visibility)) {
patch.visibility = readNonEmptyString(data.visibility)!;
}
if (data.setupCommand !== undefined) patch.setupCommand = readNonEmptyString(data.setupCommand);
if (data.cleanupCommand !== undefined) patch.cleanupCommand = readNonEmptyString(data.cleanupCommand);
if (data.remoteProvider !== undefined) patch.remoteProvider = readNonEmptyString(data.remoteProvider);
if (data.remoteWorkspaceRef !== undefined) patch.remoteWorkspaceRef = nextRemoteWorkspaceRef;
if (data.sharedWorkspaceKey !== undefined) patch.sharedWorkspaceKey = readNonEmptyString(data.sharedWorkspaceKey);
if (data.metadata !== undefined || data.runtimeConfig !== undefined) {
patch.metadata =
data.runtimeConfig !== undefined
? mergeProjectWorkspaceRuntimeConfig(
data.metadata !== undefined
? (data.metadata as Record<string, unknown> | null | undefined)
: ((existing.metadata as Record<string, unknown> | null | undefined) ?? null),
data.runtimeConfig ?? null,
)
: data.metadata;
}
const updated = await db.transaction(async (tx) => {
if (data.isPrimary === true) {
await tx
.update(projectWorkspaces)
.set({ isPrimary: false, updatedAt: new Date() })
.where(
and(
eq(projectWorkspaces.companyId, existing.companyId),
eq(projectWorkspaces.projectId, projectId),
),
);
patch.isPrimary = true;
} else if (data.isPrimary === false) {
patch.isPrimary = false;
}
const row = await tx
.update(projectWorkspaces)
.set(patch)
.where(eq(projectWorkspaces.id, workspaceId))
.returning()
.then((rows) => rows[0] ?? null);
if (!row) return null;
if (row.isPrimary) return row;
const hasPrimary = await tx
.select({ id: projectWorkspaces.id })
.from(projectWorkspaces)
.where(
and(
eq(projectWorkspaces.companyId, row.companyId),
eq(projectWorkspaces.projectId, row.projectId),
eq(projectWorkspaces.isPrimary, true),
),
)
.then((rows) => rows[0] ?? null);
if (!hasPrimary) {
const nextPrimaryCandidate = await tx
.select({ id: projectWorkspaces.id })
.from(projectWorkspaces)
.where(
and(
eq(projectWorkspaces.companyId, row.companyId),
eq(projectWorkspaces.projectId, row.projectId),
eq(projectWorkspaces.id, row.id),
),
)
.then((rows) => rows[0] ?? null);
const alternateCandidate = await tx
.select({ id: projectWorkspaces.id })
.from(projectWorkspaces)
.where(
and(
eq(projectWorkspaces.companyId, row.companyId),
eq(projectWorkspaces.projectId, row.projectId),
),
)
.orderBy(asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id))
.then((rows) => rows.find((candidate) => candidate.id !== row.id) ?? null);
await ensureSinglePrimaryWorkspace(tx, {
companyId: row.companyId,
projectId: row.projectId,
keepWorkspaceId: alternateCandidate?.id ?? nextPrimaryCandidate?.id ?? row.id,
});
const refreshed = await tx
.select()
.from(projectWorkspaces)
.where(eq(projectWorkspaces.id, row.id))
.then((rows) => rows[0] ?? row);
return refreshed;
}
return row;
});
return updated ? toWorkspace(updated) : null;
},
removeWorkspace: async (projectId: string, workspaceId: string): Promise<ProjectWorkspace | null> => {
const existing = await db
.select()
.from(projectWorkspaces)
.where(
and(
eq(projectWorkspaces.id, workspaceId),
eq(projectWorkspaces.projectId, projectId),
),
)
.then((rows) => rows[0] ?? null);
if (!existing) return null;
const removed = await db.transaction(async (tx) => {
const row = await tx
.delete(projectWorkspaces)
.where(eq(projectWorkspaces.id, workspaceId))
.returning()
.then((rows) => rows[0] ?? null);
if (!row) return null;
if (!row.isPrimary) return row;
const next = await tx
.select()
.from(projectWorkspaces)
.where(
and(
eq(projectWorkspaces.companyId, row.companyId),
eq(projectWorkspaces.projectId, row.projectId),
),
)
.orderBy(asc(projectWorkspaces.createdAt), asc(projectWorkspaces.id))
.limit(1)
.then((rows) => rows[0] ?? null);
if (next) {
await ensureSinglePrimaryWorkspace(tx, {
companyId: row.companyId,
projectId: row.projectId,
keepWorkspaceId: next.id,
});
}
return row;
});
return removed ? toWorkspace(removed) : null;
},
resolveByReference: async (companyId: string, reference: string) => {
const raw = reference.trim();
if (raw.length === 0) {
return { project: null, ambiguous: false } as const;
}
if (isUuidLike(raw)) {
const row = await db
.select({ id: projects.id, companyId: projects.companyId, name: projects.name })
.from(projects)
.where(and(eq(projects.id, raw), eq(projects.companyId, companyId)))
.then((rows) => rows[0] ?? null);
if (!row) return { project: null, ambiguous: false } as const;
return {
project: { id: row.id, companyId: row.companyId, urlKey: deriveProjectUrlKey(row.name, row.id) },
ambiguous: false,
} as const;
}
const urlKey = normalizeProjectUrlKey(raw);
if (!urlKey) {
return { project: null, ambiguous: false } as const;
}
const rows = await db
.select({ id: projects.id, companyId: projects.companyId, name: projects.name })
.from(projects)
.where(eq(projects.companyId, companyId));
const matches = rows.filter((row) => deriveProjectUrlKey(row.name, row.id) === urlKey);
if (matches.length === 1) {
const match = matches[0]!;
return {
project: { id: match.id, companyId: match.companyId, urlKey: deriveProjectUrlKey(match.name, match.id) },
ambiguous: false,
} as const;
}
if (matches.length > 1) {
return { project: null, ambiguous: true } as const;
}
return { project: null, ambiguous: false } as const;
},
};
}