mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-27 02:44:50 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The Skills Manager gives operators a reusable skill layer, but iteration still required manual edits, ad hoc prompts, and indirect run inspection. > - Skill authors need a focused workflow for editing skill files, saving representative test inputs, and running those inputs through an agent without exposing harness tasks as normal company work. > - The backend therefore needs durable test inputs, reusable run templates, hidden harness issues, scoped run execution, retention metadata, and read-containment rules around hidden work. > - The frontend needs a three-pane Studio that keeps skill files, saved inputs/templates, and run output/history visible together while preserving the existing design system and token rules. > - This pull request ships that Skill Studio surface end to end: database migrations, shared contracts, server APIs/services, hidden harness execution behavior, UI routes/components, and focused tests. > - The benefit is faster and safer skill iteration, with inspectable outputs and fewer ways for internal harness work to leak into normal task lists, costs, or adjacent read APIs. ## Linked Issues or Issue Description No public GitHub issue exists for this feature. Feature request summary: - Problem: Skill authors need to edit and test company skills in one place instead of switching between the skill detail page, task creation, run output, and manual prompt history. - Proposed solution: Add a Skill Studio workbench with saved inputs, reusable templates, hidden sandboxed test runs, live run status, output inspection, run history, rerun/delete controls, and frontmatter-aware editing. - Expected users: Paperclip operators and agent-company maintainers who create, fork, import, and tune skills. - Related public PRs: Supersedes #9205, which was replaced so the public PR branch name follows contributor policy. - Duplicate search: searched public GitHub issues and PRs for "Skill Studio"; no other active public issue or PR directly covers this feature. ## What Changed - Added database migrations for Skill Studio test inputs, test runs, test run retention, and reusable run templates. - Added shared Skill Studio types, validators, route helpers, frontmatter utilities, and status handling. - Added server services and routes for saved inputs, test runs, templates, reruns, terminal-run deletion, hidden harness issue execution, and run-detail hydration. - Strengthened hidden-issue read containment across issue-adjacent routes and cost rollups used by skill test harness work. - Added the Skill Studio UI with skill file editing, frontmatter editing, saved inputs, templates, run creation/cancel/rerun/delete flows, output rendering, history, route support, and responsive pane behavior. - Added focused backend, shared, and UI tests for the new APIs, routing logic, editor/run behavior, hidden-issue containment, and migration safety. - Rebased onto current `master`, removed the generated lockfile diff from the PR, and verified no workflow files are changed. ## Verification - [x] `pnpm --filter @paperclipai/db check:migrations` - [x] `pnpm check:token-gates` - [x] `pnpm exec vitest run server/src/__tests__/company-skills-service.test.ts server/src/__tests__/company-skills-routes.test.ts server/src/__tests__/company-skill-test-runs-service.test.ts ui/src/lib/skill-studio.test.ts ui/src/pages/SkillStudio.test.tsx` — 5 files, 132 tests passed - [x] Greptile review on the latest PR head - [x] GitHub PR checks on the latest PR head ## Risks - Medium risk because this is a broad feature touching database schema, server orchestration, issue visibility, and a large UI surface. - Hidden harness issue containment is security-sensitive; this PR includes regression coverage for adjacent read paths and cost rollups. - The new migrations are additive and use idempotent guards where applicable, but deployed databases that previously tested draft migration numbers should still be checked carefully. - The UI depends on a new resizable panels package in `ui/package.json`; the lockfile is intentionally left to repository automation. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used OpenAI Codex, GPT-5 coding agent with shell, git, and GitHub CLI tool use. Earlier feature commits include assistance from other Paperclip coding agents; this PR preparation, rebase, cleanup commit, and PR body were completed by OpenAI Codex in a Paperclip worktree. ## 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 not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [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>
746 lines
27 KiB
TypeScript
746 lines
27 KiB
TypeScript
import { and, asc, desc, eq } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { documentRevisions, documents, issueDocuments, issues } from "@paperclipai/db";
|
|
import { isSystemIssueDocumentKey, issueDocumentKeySchema } from "@paperclipai/shared";
|
|
import { conflict, notFound, unprocessable } from "../errors.js";
|
|
|
|
function normalizeDocumentKey(key: string) {
|
|
const normalized = key.trim().toLowerCase();
|
|
const parsed = issueDocumentKeySchema.safeParse(normalized);
|
|
if (!parsed.success) {
|
|
throw unprocessable("Invalid document key", parsed.error.issues);
|
|
}
|
|
return parsed.data;
|
|
}
|
|
|
|
function isUniqueViolation(error: unknown): boolean {
|
|
return !!error && typeof error === "object" && "code" in error && (error as { code?: string }).code === "23505";
|
|
}
|
|
|
|
function nextAvailableDocumentKey(sourceKey: string, existingKeys: string[]) {
|
|
const usedKeys = new Set(existingKeys);
|
|
for (let index = 2; index < 1000; index += 1) {
|
|
const suffix = `-${index}`;
|
|
const baseMaxLength = 64 - suffix.length;
|
|
const base = sourceKey.slice(0, baseMaxLength).replace(/[-_]+$/g, "") || "document";
|
|
const candidate = `${base}${suffix}`;
|
|
if (!usedKeys.has(candidate) && issueDocumentKeySchema.safeParse(candidate).success) {
|
|
return candidate;
|
|
}
|
|
}
|
|
throw conflict("Unable to choose a new document key for locked document", { key: sourceKey });
|
|
}
|
|
|
|
export function extractLegacyPlanBody(description: string | null | undefined) {
|
|
if (!description) return null;
|
|
const match = /<plan>\s*([\s\S]*?)\s*<\/plan>/i.exec(description);
|
|
if (!match) return null;
|
|
const body = match[1]?.trim();
|
|
return body ? body : null;
|
|
}
|
|
|
|
export function mapIssueDocumentRow(
|
|
row: {
|
|
id: string;
|
|
companyId: string;
|
|
issueId: string;
|
|
key: string;
|
|
title: string | null;
|
|
format: string;
|
|
latestBody: string;
|
|
latestRevisionId: string | null;
|
|
latestRevisionNumber: number;
|
|
createdByAgentId: string | null;
|
|
createdByUserId: string | null;
|
|
updatedByAgentId: string | null;
|
|
updatedByUserId: string | null;
|
|
lockedAt: Date | null;
|
|
lockedByAgentId: string | null;
|
|
lockedByUserId: string | null;
|
|
sourceTrust: typeof documents.$inferSelect.sourceTrust;
|
|
createdAt: Date;
|
|
updatedAt: Date;
|
|
},
|
|
includeBody: boolean,
|
|
) {
|
|
return {
|
|
id: row.id,
|
|
companyId: row.companyId,
|
|
issueId: row.issueId,
|
|
key: row.key,
|
|
title: row.title,
|
|
format: row.format,
|
|
...(includeBody ? { body: row.latestBody } : {}),
|
|
latestRevisionId: row.latestRevisionId ?? null,
|
|
latestRevisionNumber: row.latestRevisionNumber,
|
|
createdByAgentId: row.createdByAgentId,
|
|
createdByUserId: row.createdByUserId,
|
|
updatedByAgentId: row.updatedByAgentId,
|
|
updatedByUserId: row.updatedByUserId,
|
|
lockedAt: row.lockedAt,
|
|
lockedByAgentId: row.lockedByAgentId,
|
|
lockedByUserId: row.lockedByUserId,
|
|
sourceTrust: row.sourceTrust ?? null,
|
|
createdAt: row.createdAt,
|
|
updatedAt: row.updatedAt,
|
|
};
|
|
}
|
|
|
|
export const issueDocumentSelect = {
|
|
id: documents.id,
|
|
companyId: documents.companyId,
|
|
issueId: issueDocuments.issueId,
|
|
key: issueDocuments.key,
|
|
title: documents.title,
|
|
format: documents.format,
|
|
latestBody: documents.latestBody,
|
|
latestRevisionId: documents.latestRevisionId,
|
|
latestRevisionNumber: documents.latestRevisionNumber,
|
|
createdByAgentId: documents.createdByAgentId,
|
|
createdByUserId: documents.createdByUserId,
|
|
updatedByAgentId: documents.updatedByAgentId,
|
|
updatedByUserId: documents.updatedByUserId,
|
|
lockedAt: documents.lockedAt,
|
|
lockedByAgentId: documents.lockedByAgentId,
|
|
lockedByUserId: documents.lockedByUserId,
|
|
sourceTrust: documents.sourceTrust,
|
|
createdAt: documents.createdAt,
|
|
updatedAt: documents.updatedAt,
|
|
};
|
|
|
|
export function documentService(db: Db) {
|
|
const filterSystemDocuments = <T extends { key: string }>(rows: T[], includeSystem: boolean) =>
|
|
includeSystem ? rows : rows.filter((row) => !isSystemIssueDocumentKey(row.key));
|
|
|
|
return {
|
|
getIssueDocumentPayload: async (
|
|
issue: { id: string; description: string | null },
|
|
options: { includeSystem?: boolean } = {},
|
|
) => {
|
|
const [planDocument, documentSummaries] = await Promise.all([
|
|
db
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issue.id), eq(issueDocuments.key, "plan")))
|
|
.then((rows) => rows[0] ?? null),
|
|
db
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(eq(issueDocuments.issueId, issue.id))
|
|
.orderBy(asc(issueDocuments.key), desc(documents.updatedAt)),
|
|
]);
|
|
|
|
const legacyPlanBody = planDocument ? null : extractLegacyPlanBody(issue.description);
|
|
|
|
return {
|
|
planDocument: planDocument ? mapIssueDocumentRow(planDocument, true) : null,
|
|
documentSummaries: filterSystemDocuments(documentSummaries, options.includeSystem ?? false)
|
|
.map((row) => mapIssueDocumentRow(row, false)),
|
|
legacyPlanDocument: legacyPlanBody
|
|
? {
|
|
key: "plan" as const,
|
|
body: legacyPlanBody,
|
|
source: "issue_description" as const,
|
|
}
|
|
: null,
|
|
};
|
|
},
|
|
|
|
listIssueDocuments: async (issueId: string, options: { includeSystem?: boolean } = {}) => {
|
|
const rows = await db
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(eq(issueDocuments.issueId, issueId))
|
|
.orderBy(asc(issueDocuments.key), desc(documents.updatedAt));
|
|
return filterSystemDocuments(rows, options.includeSystem ?? false).map((row) => mapIssueDocumentRow(row, true));
|
|
},
|
|
|
|
getIssueDocumentByKey: async (issueId: string, rawKey: string) => {
|
|
const key = normalizeDocumentKey(rawKey);
|
|
const row = await db
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issueId), eq(issueDocuments.key, key)))
|
|
.then((rows) => rows[0] ?? null);
|
|
return row ? mapIssueDocumentRow(row, true) : null;
|
|
},
|
|
|
|
listIssueDocumentRevisions: async (issueId: string, rawKey: string) => {
|
|
const key = normalizeDocumentKey(rawKey);
|
|
return db
|
|
.select({
|
|
id: documentRevisions.id,
|
|
companyId: documentRevisions.companyId,
|
|
documentId: documentRevisions.documentId,
|
|
issueId: issueDocuments.issueId,
|
|
key: issueDocuments.key,
|
|
revisionNumber: documentRevisions.revisionNumber,
|
|
title: documentRevisions.title,
|
|
format: documentRevisions.format,
|
|
body: documentRevisions.body,
|
|
changeSummary: documentRevisions.changeSummary,
|
|
createdByAgentId: documentRevisions.createdByAgentId,
|
|
createdByUserId: documentRevisions.createdByUserId,
|
|
createdAt: documentRevisions.createdAt,
|
|
})
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.innerJoin(documentRevisions, eq(documentRevisions.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issueId), eq(issueDocuments.key, key)))
|
|
.orderBy(desc(documentRevisions.revisionNumber));
|
|
},
|
|
|
|
upsertIssueDocument: async (input: {
|
|
issueId: string;
|
|
key: string;
|
|
title?: string | null;
|
|
format: string;
|
|
body: string;
|
|
changeSummary?: string | null;
|
|
baseRevisionId?: string | null;
|
|
createdByAgentId?: string | null;
|
|
createdByUserId?: string | null;
|
|
createdByRunId?: string | null;
|
|
sourceTrust?: typeof documents.$inferInsert.sourceTrust;
|
|
lockedDocumentStrategy?: "conflict" | "create_new_document";
|
|
}) => {
|
|
const key = normalizeDocumentKey(input.key);
|
|
const issue = await db
|
|
.select({ id: issues.id, companyId: issues.companyId })
|
|
.from(issues)
|
|
.where(eq(issues.id, input.issueId))
|
|
.then((rows) => rows[0] ?? null);
|
|
if (!issue) throw notFound("Issue not found");
|
|
|
|
const maxAttempts = input.lockedDocumentStrategy === "create_new_document" ? 3 : 1;
|
|
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
|
|
try {
|
|
return await db.transaction(async (tx) => {
|
|
const now = new Date();
|
|
const existing = await tx
|
|
.select({
|
|
id: documents.id,
|
|
companyId: documents.companyId,
|
|
issueId: issueDocuments.issueId,
|
|
key: issueDocuments.key,
|
|
title: documents.title,
|
|
format: documents.format,
|
|
latestBody: documents.latestBody,
|
|
latestRevisionId: documents.latestRevisionId,
|
|
latestRevisionNumber: documents.latestRevisionNumber,
|
|
createdByAgentId: documents.createdByAgentId,
|
|
createdByUserId: documents.createdByUserId,
|
|
updatedByAgentId: documents.updatedByAgentId,
|
|
updatedByUserId: documents.updatedByUserId,
|
|
lockedAt: documents.lockedAt,
|
|
lockedByAgentId: documents.lockedByAgentId,
|
|
lockedByUserId: documents.lockedByUserId,
|
|
sourceTrust: documents.sourceTrust,
|
|
createdAt: documents.createdAt,
|
|
updatedAt: documents.updatedAt,
|
|
})
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issue.id), eq(issueDocuments.key, key)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (existing) {
|
|
if (existing.lockedAt) {
|
|
if (input.lockedDocumentStrategy === "create_new_document") {
|
|
const issueDocumentKeys = await tx
|
|
.select({ key: issueDocuments.key })
|
|
.from(issueDocuments)
|
|
.where(eq(issueDocuments.issueId, issue.id));
|
|
const fallbackKey = nextAvailableDocumentKey(key, issueDocumentKeys.map((row) => row.key));
|
|
|
|
const [document] = await tx
|
|
.insert(documents)
|
|
.values({
|
|
companyId: issue.companyId,
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
latestBody: input.body,
|
|
latestRevisionId: null,
|
|
latestRevisionNumber: 1,
|
|
createdByAgentId: input.createdByAgentId ?? null,
|
|
createdByUserId: input.createdByUserId ?? null,
|
|
updatedByAgentId: input.createdByAgentId ?? null,
|
|
updatedByUserId: input.createdByUserId ?? null,
|
|
lockedAt: null,
|
|
lockedByAgentId: null,
|
|
lockedByUserId: null,
|
|
sourceTrust: input.sourceTrust ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning();
|
|
|
|
const [revision] = await tx
|
|
.insert(documentRevisions)
|
|
.values({
|
|
companyId: issue.companyId,
|
|
documentId: document.id,
|
|
revisionNumber: 1,
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
body: input.body,
|
|
changeSummary: input.changeSummary ?? null,
|
|
createdByAgentId: input.createdByAgentId ?? null,
|
|
createdByUserId: input.createdByUserId ?? null,
|
|
createdByRunId: input.createdByRunId ?? null,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
|
|
await tx
|
|
.update(documents)
|
|
.set({ latestRevisionId: revision.id })
|
|
.where(eq(documents.id, document.id));
|
|
|
|
await tx.insert(issueDocuments).values({
|
|
companyId: issue.companyId,
|
|
issueId: issue.id,
|
|
documentId: document.id,
|
|
key: fallbackKey,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
return {
|
|
created: true as const,
|
|
redirectedFromLockedDocument: {
|
|
id: existing.id,
|
|
key: existing.key,
|
|
},
|
|
document: {
|
|
id: document.id,
|
|
companyId: issue.companyId,
|
|
issueId: issue.id,
|
|
key: fallbackKey,
|
|
title: document.title,
|
|
format: document.format,
|
|
body: document.latestBody,
|
|
latestRevisionId: revision.id,
|
|
latestRevisionNumber: 1,
|
|
createdByAgentId: document.createdByAgentId,
|
|
createdByUserId: document.createdByUserId,
|
|
updatedByAgentId: document.updatedByAgentId,
|
|
updatedByUserId: document.updatedByUserId,
|
|
lockedAt: null,
|
|
lockedByAgentId: null,
|
|
lockedByUserId: null,
|
|
sourceTrust: document.sourceTrust ?? null,
|
|
createdAt: document.createdAt,
|
|
updatedAt: document.updatedAt,
|
|
},
|
|
};
|
|
}
|
|
|
|
throw conflict("Document is locked", {
|
|
key: existing.key,
|
|
documentId: existing.id,
|
|
lockedAt: existing.lockedAt,
|
|
});
|
|
}
|
|
|
|
if (!input.baseRevisionId) {
|
|
throw conflict("Document update requires baseRevisionId", {
|
|
currentRevisionId: existing.latestRevisionId,
|
|
});
|
|
}
|
|
if (input.baseRevisionId !== existing.latestRevisionId) {
|
|
throw conflict("Document was updated by someone else", {
|
|
currentRevisionId: existing.latestRevisionId,
|
|
});
|
|
}
|
|
|
|
const nextRevisionNumber = existing.latestRevisionNumber + 1;
|
|
const [revision] = await tx
|
|
.insert(documentRevisions)
|
|
.values({
|
|
companyId: issue.companyId,
|
|
documentId: existing.id,
|
|
revisionNumber: nextRevisionNumber,
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
body: input.body,
|
|
changeSummary: input.changeSummary ?? null,
|
|
createdByAgentId: input.createdByAgentId ?? null,
|
|
createdByUserId: input.createdByUserId ?? null,
|
|
createdByRunId: input.createdByRunId ?? null,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
|
|
await tx
|
|
.update(documents)
|
|
.set({
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
latestBody: input.body,
|
|
latestRevisionId: revision.id,
|
|
latestRevisionNumber: nextRevisionNumber,
|
|
updatedByAgentId: input.createdByAgentId ?? null,
|
|
updatedByUserId: input.createdByUserId ?? null,
|
|
sourceTrust: input.sourceTrust ?? null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documents.id, existing.id));
|
|
|
|
await tx
|
|
.update(issueDocuments)
|
|
.set({ updatedAt: now })
|
|
.where(eq(issueDocuments.documentId, existing.id));
|
|
|
|
return {
|
|
created: false as const,
|
|
document: {
|
|
...existing,
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
body: input.body,
|
|
latestRevisionId: revision.id,
|
|
latestRevisionNumber: nextRevisionNumber,
|
|
updatedByAgentId: input.createdByAgentId ?? null,
|
|
updatedByUserId: input.createdByUserId ?? null,
|
|
lockedAt: existing.lockedAt,
|
|
lockedByAgentId: existing.lockedByAgentId,
|
|
lockedByUserId: existing.lockedByUserId,
|
|
sourceTrust: input.sourceTrust ?? null,
|
|
updatedAt: now,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (input.baseRevisionId) {
|
|
throw conflict("Document does not exist yet", { key });
|
|
}
|
|
|
|
const [document] = await tx
|
|
.insert(documents)
|
|
.values({
|
|
companyId: issue.companyId,
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
latestBody: input.body,
|
|
latestRevisionId: null,
|
|
latestRevisionNumber: 1,
|
|
createdByAgentId: input.createdByAgentId ?? null,
|
|
createdByUserId: input.createdByUserId ?? null,
|
|
updatedByAgentId: input.createdByAgentId ?? null,
|
|
updatedByUserId: input.createdByUserId ?? null,
|
|
lockedAt: null,
|
|
lockedByAgentId: null,
|
|
lockedByUserId: null,
|
|
sourceTrust: input.sourceTrust ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning();
|
|
|
|
const [revision] = await tx
|
|
.insert(documentRevisions)
|
|
.values({
|
|
companyId: issue.companyId,
|
|
documentId: document.id,
|
|
revisionNumber: 1,
|
|
title: input.title ?? null,
|
|
format: input.format,
|
|
body: input.body,
|
|
changeSummary: input.changeSummary ?? null,
|
|
createdByAgentId: input.createdByAgentId ?? null,
|
|
createdByUserId: input.createdByUserId ?? null,
|
|
createdByRunId: input.createdByRunId ?? null,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
|
|
await tx
|
|
.update(documents)
|
|
.set({ latestRevisionId: revision.id })
|
|
.where(eq(documents.id, document.id));
|
|
|
|
await tx.insert(issueDocuments).values({
|
|
companyId: issue.companyId,
|
|
issueId: issue.id,
|
|
documentId: document.id,
|
|
key,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
});
|
|
|
|
return {
|
|
created: true as const,
|
|
document: {
|
|
id: document.id,
|
|
companyId: issue.companyId,
|
|
issueId: issue.id,
|
|
key,
|
|
title: document.title,
|
|
format: document.format,
|
|
body: document.latestBody,
|
|
latestRevisionId: revision.id,
|
|
latestRevisionNumber: 1,
|
|
createdByAgentId: document.createdByAgentId,
|
|
createdByUserId: document.createdByUserId,
|
|
updatedByAgentId: document.updatedByAgentId,
|
|
updatedByUserId: document.updatedByUserId,
|
|
lockedAt: document.lockedAt,
|
|
lockedByAgentId: document.lockedByAgentId,
|
|
lockedByUserId: document.lockedByUserId,
|
|
sourceTrust: document.sourceTrust ?? null,
|
|
createdAt: document.createdAt,
|
|
updatedAt: document.updatedAt,
|
|
},
|
|
};
|
|
});
|
|
} catch (error) {
|
|
if (isUniqueViolation(error)) {
|
|
if (input.lockedDocumentStrategy === "create_new_document" && attempt < maxAttempts - 1) {
|
|
continue;
|
|
}
|
|
throw conflict("Document key already exists on this issue", { key });
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
throw conflict("Unable to choose a new document key for locked document", { key });
|
|
},
|
|
|
|
restoreIssueDocumentRevision: async (input: {
|
|
issueId: string;
|
|
key: string;
|
|
revisionId: string;
|
|
createdByAgentId?: string | null;
|
|
createdByUserId?: string | null;
|
|
}) => {
|
|
const key = normalizeDocumentKey(input.key);
|
|
return db.transaction(async (tx) => {
|
|
const existing = await tx
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, input.issueId), eq(issueDocuments.key, key)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!existing) throw notFound("Document not found");
|
|
if (existing.lockedAt) {
|
|
throw conflict("Document is locked", {
|
|
key: existing.key,
|
|
documentId: existing.id,
|
|
lockedAt: existing.lockedAt,
|
|
});
|
|
}
|
|
|
|
const revision = await tx
|
|
.select({
|
|
id: documentRevisions.id,
|
|
companyId: documentRevisions.companyId,
|
|
documentId: documentRevisions.documentId,
|
|
revisionNumber: documentRevisions.revisionNumber,
|
|
title: documentRevisions.title,
|
|
format: documentRevisions.format,
|
|
body: documentRevisions.body,
|
|
})
|
|
.from(documentRevisions)
|
|
.where(and(eq(documentRevisions.id, input.revisionId), eq(documentRevisions.documentId, existing.id)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!revision) throw notFound("Document revision not found");
|
|
if (existing.latestRevisionId === revision.id) {
|
|
throw conflict("Selected revision is already the latest revision", {
|
|
currentRevisionId: existing.latestRevisionId,
|
|
});
|
|
}
|
|
|
|
const now = new Date();
|
|
const nextRevisionNumber = existing.latestRevisionNumber + 1;
|
|
const [restoredRevision] = await tx
|
|
.insert(documentRevisions)
|
|
.values({
|
|
companyId: existing.companyId,
|
|
documentId: existing.id,
|
|
revisionNumber: nextRevisionNumber,
|
|
title: revision.title ?? null,
|
|
format: revision.format,
|
|
body: revision.body,
|
|
changeSummary: `Restored from revision ${revision.revisionNumber}`,
|
|
createdByAgentId: input.createdByAgentId ?? null,
|
|
createdByUserId: input.createdByUserId ?? null,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
|
|
await tx
|
|
.update(documents)
|
|
.set({
|
|
title: revision.title ?? null,
|
|
format: revision.format,
|
|
latestBody: revision.body,
|
|
latestRevisionId: restoredRevision.id,
|
|
latestRevisionNumber: nextRevisionNumber,
|
|
updatedByAgentId: input.createdByAgentId ?? null,
|
|
updatedByUserId: input.createdByUserId ?? null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documents.id, existing.id));
|
|
|
|
await tx
|
|
.update(issueDocuments)
|
|
.set({ updatedAt: now })
|
|
.where(eq(issueDocuments.documentId, existing.id));
|
|
|
|
return {
|
|
restoredFromRevisionId: revision.id,
|
|
restoredFromRevisionNumber: revision.revisionNumber,
|
|
document: {
|
|
...existing,
|
|
title: revision.title ?? null,
|
|
format: revision.format,
|
|
body: revision.body,
|
|
latestRevisionId: restoredRevision.id,
|
|
latestRevisionNumber: nextRevisionNumber,
|
|
updatedByAgentId: input.createdByAgentId ?? null,
|
|
updatedByUserId: input.createdByUserId ?? null,
|
|
updatedAt: now,
|
|
},
|
|
};
|
|
});
|
|
},
|
|
|
|
lockIssueDocument: async (input: {
|
|
issueId: string;
|
|
key: string;
|
|
lockedByAgentId?: string | null;
|
|
lockedByUserId?: string | null;
|
|
}) => {
|
|
const key = normalizeDocumentKey(input.key);
|
|
return db.transaction(async (tx) => {
|
|
const existing = await tx
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, input.issueId), eq(issueDocuments.key, key)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!existing) throw notFound("Document not found");
|
|
if (existing.lockedAt) {
|
|
return {
|
|
changed: false as const,
|
|
document: mapIssueDocumentRow(existing, true),
|
|
};
|
|
}
|
|
|
|
const now = new Date();
|
|
await tx
|
|
.update(documents)
|
|
.set({
|
|
lockedAt: now,
|
|
lockedByAgentId: input.lockedByAgentId ?? null,
|
|
lockedByUserId: input.lockedByUserId ?? null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documents.id, existing.id));
|
|
|
|
await tx
|
|
.update(issueDocuments)
|
|
.set({ updatedAt: now })
|
|
.where(eq(issueDocuments.documentId, existing.id));
|
|
|
|
return {
|
|
changed: true as const,
|
|
document: {
|
|
...mapIssueDocumentRow(existing, true),
|
|
lockedAt: now,
|
|
lockedByAgentId: input.lockedByAgentId ?? null,
|
|
lockedByUserId: input.lockedByUserId ?? null,
|
|
updatedAt: now,
|
|
},
|
|
};
|
|
});
|
|
},
|
|
|
|
unlockIssueDocument: async (issueId: string, rawKey: string) => {
|
|
const key = normalizeDocumentKey(rawKey);
|
|
return db.transaction(async (tx) => {
|
|
const existing = await tx
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issueId), eq(issueDocuments.key, key)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!existing) throw notFound("Document not found");
|
|
if (!existing.lockedAt) {
|
|
return {
|
|
changed: false as const,
|
|
document: mapIssueDocumentRow(existing, true),
|
|
};
|
|
}
|
|
|
|
const now = new Date();
|
|
await tx
|
|
.update(documents)
|
|
.set({
|
|
lockedAt: null,
|
|
lockedByAgentId: null,
|
|
lockedByUserId: null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documents.id, existing.id));
|
|
|
|
await tx
|
|
.update(issueDocuments)
|
|
.set({ updatedAt: now })
|
|
.where(eq(issueDocuments.documentId, existing.id));
|
|
|
|
return {
|
|
changed: true as const,
|
|
document: {
|
|
...mapIssueDocumentRow(existing, true),
|
|
lockedAt: null,
|
|
lockedByAgentId: null,
|
|
lockedByUserId: null,
|
|
updatedAt: now,
|
|
},
|
|
};
|
|
});
|
|
},
|
|
|
|
deleteIssueDocument: async (issueId: string, rawKey: string) => {
|
|
const key = normalizeDocumentKey(rawKey);
|
|
return db.transaction(async (tx) => {
|
|
const existing = await tx
|
|
.select(issueDocumentSelect)
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issueId), eq(issueDocuments.key, key)))
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (!existing) return null;
|
|
if (existing.lockedAt) {
|
|
throw conflict("Document is locked", {
|
|
key: existing.key,
|
|
documentId: existing.id,
|
|
lockedAt: existing.lockedAt,
|
|
});
|
|
}
|
|
|
|
await tx.delete(issueDocuments).where(eq(issueDocuments.documentId, existing.id));
|
|
await tx.delete(documents).where(eq(documents.id, existing.id));
|
|
|
|
return {
|
|
...existing,
|
|
body: existing.latestBody,
|
|
latestRevisionId: existing.latestRevisionId ?? null,
|
|
};
|
|
});
|
|
},
|
|
};
|
|
}
|