mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 10:44:52 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - People review agent artifacts (plans, documents, routine descriptions, and case documents) with inline annotation comments > - Today a person must type a full reply to say "done", "checked", "looks good", or "I have a question" > - This slows down quick review feedback, and a question inside a reply does not automatically bring the assignee back > - This pull request adds four one-tap reactions to each annotation comment: Done, Check, Thumbs up, and Question > - A person's first Question reaction on an open task saves a durable wake receipt and wakes the task's assignee in a dedicated follow-up run to reply in that comment's thread (respecting chat ownership and wake rate limits) > - The benefit is fast review feedback without extra typing, while agents can read and set the same reactions through the API ## Linked Issues or Issue Description Refs #21637 (supersedes on current `master` with renumbered Drizzle migration, OpenAPI/experimental route contracts, durable wake receipts, chat ownership and rate-limit hardening, and current UI test suite) Refs #9336 (agent wake notifications for document annotation threads) Refs #9674 (agents cannot read inline document review comments) **What problem does this solve?** A reviewer cannot quickly acknowledge or question an annotation comment on an artifact. A short answer requires a typed reply, and nothing tells the agent that a person has a question about a comment. **Proposed solution** Add four fixed reactions per annotation comment: `done`, `check`, `thumbs_up`, and `question`. One tap adds a reaction and a second tap removes it. Each person or agent can add each reaction once per comment. A person's first `question` reaction on an open task commits a durable wake receipt in the same transaction and wakes the assignee once in a dedicated follow-up turn, with the comment included in the wake context. **Alternatives considered** Free-form emoji reactions. Fixed named reactions keep the API contract stable for agents and avoid storing arbitrary emoji strings on the server. ## What Changed - `packages/db`: Added `document_annotation_comment_reactions` table (`company_id`, `comment_id`, `reaction`, `actor_type`, `actor_id`, `created_at`, `removed_at`) with check constraints, cascade delete on comment deletion, and unique `(comment_id, reaction, actor_type, actor_id)` index. Generated online-compatible migration `0510_clear_speedball.sql`, refreshed Drizzle snapshot `0510_snapshot.json`, and pruned `0505_snapshot.json`. - `packages/shared`: Added `DOCUMENT_ANNOTATION_COMMENT_REACTIONS`, `DocumentAnnotationCommentReactionName`, `DocumentAnnotationCommentReactionActor`, `DocumentAnnotationCommentReactionSummary`, optional `reactions` on `DocumentAnnotationComment`, and `documentAnnotationCommentReactionSchema`. - `server`: Added `setCommentReaction` in `document-annotations.ts` using soft-delete (`removed_at`) and upsert revival (`(xmax = 0)` for `firstAdd`), and attached reaction summaries (`count`, `actors`, `reactedByMe`) to thread/comment reads. - `server` routes & contracts: Added `PUT` and `DELETE .../annotations/:threadId/comments/:commentId/reactions/:reaction` for issues, routines, and cases, registered them in `openapi.ts`, `experimental-api-metadata.ts`, and `experimental-api-paths.ts`, and logged activity (`*.document_annotation_comment_reaction_added` / `_removed`) only when a row changes. - `server` durable wake, chat ownership, rate limits & `packages/adapter-utils`: - A person's first `question` reaction on an open assigned issue inserts a recoverable `deferred_issue_execution` receipt in `agent_wakeup_requests` in the same DB transaction (`annotation-question-reaction:<id>`), passes `executionWaitRequestId` to `heartbeat.wakeup` (`reason: "issue_commented"`, `allowRunCoalescing: false`), resumes via `resumeDeferredExecutionReceipts` if immediate wake fails, and defers behind busy turns without duplicating receipts. - Recognized human annotation question reactions (`isHumanAnnotationQuestionWake`) as an independent wake cause for idle waiting agent chats while strictly enforcing chat ownership (`!conversationAgentId || conversationUserId === actorId`) across service, route, wake preflight, locked admission, and recovery queries. - Bounded distinct-comment question wakes under `issues ... FOR UPDATE` to at most 1 pending/in-flight wake per `(companyId, issueId, agentId)` and at most 3 original question receipts (`idempotency_key LIKE 'annotation-question-reaction:%'`) per `(companyId, issueId, user)` in a rolling 10-minute window. - `renderPaperclipWakePrompt` instructs the agent to answer the question in the annotation thread. - `ui`: Added four one-tap reaction buttons (`✅`, `✔️`, `👍`, `❓`) under each non-optimistic annotation comment in `DocumentAnnotationPanel` / `DocumentAnnotationPopover`, with counts, `aria-pressed`, hover tooltips naming actors, optimistic updates, client-scoped write serialization across panel/popover remounts (`WeakMap<QueryClient, Map<string, TargetReactionState>>`), live-update refetch overlay preservation, deferred invalidation while writes are pending, and updated Storybook fixture. - `docs/api` & `tests/runner-e2e`: Documented the reaction endpoints and wake bounds in `docs/api/issues.md` and `docs/api/routines.md`, and refreshed `tests/runner-e2e/source-sdk-public-inputs.json`. ## Verification - `pnpm --filter @paperclipai/db run check:migrations` and `migration-compatibility.ts`: `0510_clear_speedball.sql` classified `online_compatible`. - `pnpm --filter @paperclipai/db exec vitest run src/migration-snapshot-drift.test.ts src/migration-compatibility.test.ts`: passed (117/117). - `pnpm --filter @paperclipai/adapter-utils exec vitest run src/server-utils.test.ts -t "question reaction"`: passed. - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/document-annotations-service.test.ts` (20/20, embedded Postgres — covering durable wake recovery, idle waiting-chat wake, busy-turn deferral, non-owner chat wake denial, and rolling-window + pending-backlog wake bounds across full queue admission), `src/__tests__/document-annotation-routes.test.ts` (20/20), `src/__tests__/routine-document-annotation-routes.test.ts`, `src/modules/wake-queue/application/issue-wake-admission.test.ts` (15/15), and `src/modules/wake-queue/domain/policy.test.ts`: all passed. - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/openapi-routes.test.ts`: passed (59/59). - `pnpm --filter @paperclipai/ui exec vitest run src/hooks/useDocumentAnnotationMutations.test.tsx`: passed (9/9, covering optimistic updates, serialized writes across remounts, live-update refetch preservation, deferred invalidation across comments, and rendered `ThreadCard` DOM buttons/toggles). - Package typechecks (`@paperclipai/shared`, `@paperclipai/db`, `@paperclipai/adapter-utils`, `@paperclipai/server` all 7 layers, `@paperclipai/ui` + Storybook catalog): all passed. - `pnpm check:token-gates`, `node scripts/check-module-boundaries.mjs`, and `node scripts/check-test-budget.mjs`: all passed. - `node tests/runner-e2e/source-sdk-public-inputs.mjs $(git rev-parse HEAD) --check`: in sync (`inputs: 579, changed: false`). - Full-stack Playwright verification against a throwaway local server + embedded PostgreSQL: created a task, `plan` document, and annotation thread; verified invalid reaction returns `400`; clicked `thumbs_up` and `question` in the real document viewer; reloaded to prove PostgreSQL persistence; toggled `thumbs_up` off and reloaded to verify removal persisted. - PR CI on `4d892c8067`: 83/83 executed checks passing (including `ci / verify`, `ci / e2e` + all 16 E2E shards, all General/Serialized server shards, `Build`, `Typecheck + Release Registry`, `Verify Paperclip Runner`, `Superagent Security Scan`, and `Greptile Review` 5/5 with 0 unresolved threads). ## Risks - Low risk. Migration `0510_clear_speedball.sql` only creates a new table and its indexes (`online_compatible`), so running releases are unaffected. - `reactions` is optional on `DocumentAnnotationComment`, preserving backward compatibility for existing clients. - Question wakes are bounded by soft-delete revival (`firstAdd`), chat ownership enforcement, single pending/in-flight wake per `(companyId, issueId, agentId)`, and at most 3 original question wakes per `(companyId, issueId, user)` per 10-minute window. > 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 - Google Gemini (`gemini-3-pro` / Antigravity agentic coding environment with shell execution, file editing, embedded PostgreSQL, and Playwright verification), rebasing and extending prior work originally authored with Claude Opus 5.5 in #21637. ## 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: lucktastic <13757243+lucktastic@users.noreply.github.com> Co-authored-by: Paperclip <noreply@paperclip.ing>
1473 lines
56 KiB
TypeScript
1473 lines
56 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { and, asc, desc, eq, gte, inArray, isNull, sql } from "drizzle-orm";
|
|
import { assertAgentInteraction } from "./agent-interaction-policy.js";
|
|
import type { Db } from "@paperclipai/db";
|
|
import {
|
|
agentWakeupRequests,
|
|
documentAnnotationAnchorSnapshots,
|
|
documentAnnotationCommentReactions,
|
|
documentAnnotationComments,
|
|
documentAnnotationThreads,
|
|
documents,
|
|
caseDocuments,
|
|
issueComments,
|
|
issueDocuments,
|
|
issues,
|
|
routineDocuments,
|
|
} from "@paperclipai/db";
|
|
import { notifyDeferredExecutionWork } from "./deferred-execution-notifications.js";
|
|
import { deferredWorkIntentWait } from "./work-intent-outbox.js";
|
|
import {
|
|
anchorSnapshotToSelector,
|
|
remapDocumentAnchor,
|
|
selectorToAnchorSnapshot,
|
|
verifyDocumentAnchorSelector,
|
|
type DocumentAnnotationAnchorSnapshot,
|
|
DOCUMENT_ANNOTATION_COMMENT_REACTIONS,
|
|
type DocumentAnnotationComment,
|
|
type DocumentAnnotationCommentReactionName,
|
|
type DocumentAnnotationCommentReactionSummary,
|
|
type DocumentAnnotationThread,
|
|
CreateDocumentAnnotationComment,
|
|
CreateDocumentAnnotationThread,
|
|
UpdateDocumentAnnotationThread,
|
|
} from "@paperclipai/shared";
|
|
import { conflict, notFound, unprocessable } from "../errors.js";
|
|
|
|
const ANNOTATION_QUESTION_WAKE_WINDOW_MS = 10 * 60 * 1000;
|
|
const ANNOTATION_QUESTION_WAKE_MAX_PER_WINDOW = 3;
|
|
const ANNOTATION_QUESTION_REACTION_KEY_PREFIX = "annotation-question-reaction:";
|
|
|
|
type ActorInput = {
|
|
actorType: "agent" | "user";
|
|
actorId: string;
|
|
agentId?: string | null;
|
|
userId?: string | null;
|
|
runId?: string | null;
|
|
};
|
|
|
|
type ReactionViewer = { actorType: "agent" | "user"; actorId: string } | null | undefined;
|
|
|
|
type ListThreadOptions = {
|
|
status?: "open" | "resolved" | "all";
|
|
includeComments?: boolean;
|
|
viewer?: ReactionViewer;
|
|
};
|
|
|
|
export type DocumentAnnotationReactionTarget =
|
|
| { kind: "issue"; issueId: string; key: string }
|
|
| { kind: "routine"; routineId: string; key: string }
|
|
| { kind: "case"; caseId: string; key: string };
|
|
|
|
type IssueDocumentRow = {
|
|
issueId: string;
|
|
companyId: string;
|
|
documentId: string;
|
|
documentKey: string;
|
|
latestBody: string;
|
|
latestRevisionId: string | null;
|
|
latestRevisionNumber: number;
|
|
};
|
|
|
|
type RoutineDocumentRow = {
|
|
routineId: string;
|
|
companyId: string;
|
|
documentId: string;
|
|
documentKey: string;
|
|
latestBody: string;
|
|
latestRevisionId: string | null;
|
|
latestRevisionNumber: number;
|
|
};
|
|
|
|
type CaseDocumentRow = {
|
|
caseId: string;
|
|
companyId: string;
|
|
documentId: string;
|
|
documentKey: string;
|
|
latestBody: string;
|
|
latestRevisionId: string | null;
|
|
latestRevisionNumber: number;
|
|
};
|
|
|
|
const threadSelect = {
|
|
id: documentAnnotationThreads.id,
|
|
companyId: documentAnnotationThreads.companyId,
|
|
issueId: documentAnnotationThreads.issueId,
|
|
routineId: documentAnnotationThreads.routineId,
|
|
caseId: documentAnnotationThreads.caseId,
|
|
documentId: documentAnnotationThreads.documentId,
|
|
documentKey: documentAnnotationThreads.documentKey,
|
|
status: documentAnnotationThreads.status,
|
|
anchorState: documentAnnotationThreads.anchorState,
|
|
anchorConfidence: documentAnnotationThreads.anchorConfidence,
|
|
originalRevisionId: documentAnnotationThreads.originalRevisionId,
|
|
originalRevisionNumber: documentAnnotationThreads.originalRevisionNumber,
|
|
currentRevisionId: documentAnnotationThreads.currentRevisionId,
|
|
currentRevisionNumber: documentAnnotationThreads.currentRevisionNumber,
|
|
selectedText: documentAnnotationThreads.selectedText,
|
|
prefixText: documentAnnotationThreads.prefixText,
|
|
suffixText: documentAnnotationThreads.suffixText,
|
|
normalizedStart: documentAnnotationThreads.normalizedStart,
|
|
normalizedEnd: documentAnnotationThreads.normalizedEnd,
|
|
markdownStart: documentAnnotationThreads.markdownStart,
|
|
markdownEnd: documentAnnotationThreads.markdownEnd,
|
|
anchorSelector: documentAnnotationThreads.anchorSelector,
|
|
createdByAgentId: documentAnnotationThreads.createdByAgentId,
|
|
createdByUserId: documentAnnotationThreads.createdByUserId,
|
|
resolvedByAgentId: documentAnnotationThreads.resolvedByAgentId,
|
|
resolvedByUserId: documentAnnotationThreads.resolvedByUserId,
|
|
resolvedAt: documentAnnotationThreads.resolvedAt,
|
|
createdAt: documentAnnotationThreads.createdAt,
|
|
updatedAt: documentAnnotationThreads.updatedAt,
|
|
};
|
|
|
|
const commentSelect = {
|
|
id: documentAnnotationComments.id,
|
|
companyId: documentAnnotationComments.companyId,
|
|
threadId: documentAnnotationComments.threadId,
|
|
issueId: documentAnnotationComments.issueId,
|
|
routineId: documentAnnotationComments.routineId,
|
|
caseId: documentAnnotationComments.caseId,
|
|
documentId: documentAnnotationComments.documentId,
|
|
body: documentAnnotationComments.body,
|
|
authorType: documentAnnotationComments.authorType,
|
|
authorAgentId: documentAnnotationComments.authorAgentId,
|
|
authorUserId: documentAnnotationComments.authorUserId,
|
|
createdByRunId: documentAnnotationComments.createdByRunId,
|
|
issueCommentId: documentAnnotationComments.issueCommentId,
|
|
createdAt: documentAnnotationComments.createdAt,
|
|
updatedAt: documentAnnotationComments.updatedAt,
|
|
};
|
|
|
|
function snapshotFromThread(thread: Pick<DocumentAnnotationThread, "selectedText" | "prefixText" | "suffixText" | "normalizedStart" | "normalizedEnd" | "markdownStart" | "markdownEnd">): DocumentAnnotationAnchorSnapshot {
|
|
return {
|
|
selectedText: thread.selectedText,
|
|
prefixText: thread.prefixText,
|
|
suffixText: thread.suffixText,
|
|
normalizedStart: thread.normalizedStart,
|
|
normalizedEnd: thread.normalizedEnd,
|
|
markdownStart: thread.markdownStart,
|
|
markdownEnd: thread.markdownEnd,
|
|
};
|
|
}
|
|
|
|
export function documentAnnotationService(db: Db) {
|
|
async function getIssueDocument(issueId: string, key: string, dbOrTx: any = db): Promise<IssueDocumentRow | null> {
|
|
return dbOrTx
|
|
.select({
|
|
issueId: issueDocuments.issueId,
|
|
companyId: documents.companyId,
|
|
documentId: documents.id,
|
|
documentKey: issueDocuments.key,
|
|
latestBody: documents.latestBody,
|
|
latestRevisionId: documents.latestRevisionId,
|
|
latestRevisionNumber: documents.latestRevisionNumber,
|
|
})
|
|
.from(issueDocuments)
|
|
.innerJoin(documents, eq(issueDocuments.documentId, documents.id))
|
|
.where(and(eq(issueDocuments.issueId, issueId), eq(issueDocuments.key, key)))
|
|
.then((rows: IssueDocumentRow[]) => rows[0] ?? null);
|
|
}
|
|
|
|
async function getRoutineDocument(
|
|
routineId: string,
|
|
key: string,
|
|
dbOrTx: any = db,
|
|
): Promise<RoutineDocumentRow | null> {
|
|
return dbOrTx
|
|
.select({
|
|
routineId: routineDocuments.routineId,
|
|
companyId: documents.companyId,
|
|
documentId: documents.id,
|
|
documentKey: routineDocuments.key,
|
|
latestBody: documents.latestBody,
|
|
latestRevisionId: documents.latestRevisionId,
|
|
latestRevisionNumber: documents.latestRevisionNumber,
|
|
})
|
|
.from(routineDocuments)
|
|
.innerJoin(documents, eq(routineDocuments.documentId, documents.id))
|
|
.where(and(
|
|
eq(routineDocuments.routineId, routineId),
|
|
eq(routineDocuments.key, key),
|
|
eq(routineDocuments.companyId, documents.companyId),
|
|
))
|
|
.then((rows: RoutineDocumentRow[]) => rows[0] ?? null);
|
|
}
|
|
|
|
async function getCaseDocument(
|
|
caseId: string,
|
|
key: string,
|
|
dbOrTx: any = db,
|
|
): Promise<CaseDocumentRow | null> {
|
|
return dbOrTx
|
|
.select({
|
|
caseId: caseDocuments.caseId,
|
|
companyId: documents.companyId,
|
|
documentId: documents.id,
|
|
documentKey: caseDocuments.key,
|
|
latestBody: documents.latestBody,
|
|
latestRevisionId: documents.latestRevisionId,
|
|
latestRevisionNumber: documents.latestRevisionNumber,
|
|
})
|
|
.from(caseDocuments)
|
|
.innerJoin(documents, eq(caseDocuments.documentId, documents.id))
|
|
.where(and(
|
|
eq(caseDocuments.caseId, caseId),
|
|
eq(caseDocuments.key, key),
|
|
eq(caseDocuments.companyId, documents.companyId),
|
|
))
|
|
.then((rows: CaseDocumentRow[]) => rows[0] ?? null);
|
|
}
|
|
|
|
async function getThreadForIssue(
|
|
issueId: string,
|
|
documentKey: string,
|
|
threadId: string,
|
|
dbOrTx: any = db,
|
|
): Promise<DocumentAnnotationThread | null> {
|
|
return dbOrTx
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(
|
|
eq(documentAnnotationThreads.id, threadId),
|
|
eq(documentAnnotationThreads.issueId, issueId),
|
|
eq(documentAnnotationThreads.documentKey, documentKey),
|
|
))
|
|
.then((rows: DocumentAnnotationThread[]) => rows[0] ?? null);
|
|
}
|
|
|
|
async function getThreadForRoutine(
|
|
routineId: string,
|
|
documentKey: string,
|
|
threadId: string,
|
|
companyId: string,
|
|
documentId: string,
|
|
dbOrTx: any = db,
|
|
): Promise<DocumentAnnotationThread | null> {
|
|
return dbOrTx
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(
|
|
eq(documentAnnotationThreads.id, threadId),
|
|
eq(documentAnnotationThreads.companyId, companyId),
|
|
eq(documentAnnotationThreads.routineId, routineId),
|
|
eq(documentAnnotationThreads.documentId, documentId),
|
|
eq(documentAnnotationThreads.documentKey, documentKey),
|
|
))
|
|
.then((rows: DocumentAnnotationThread[]) => rows[0] ?? null);
|
|
}
|
|
|
|
async function getThreadForCase(
|
|
caseId: string,
|
|
documentKey: string,
|
|
threadId: string,
|
|
companyId: string,
|
|
documentId: string,
|
|
dbOrTx: any = db,
|
|
): Promise<DocumentAnnotationThread | null> {
|
|
return dbOrTx
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(
|
|
eq(documentAnnotationThreads.id, threadId),
|
|
eq(documentAnnotationThreads.companyId, companyId),
|
|
eq(documentAnnotationThreads.caseId, caseId),
|
|
eq(documentAnnotationThreads.documentId, documentId),
|
|
eq(documentAnnotationThreads.documentKey, documentKey),
|
|
))
|
|
.then((rows: DocumentAnnotationThread[]) => rows[0] ?? null);
|
|
}
|
|
|
|
async function reactionsForComments(
|
|
commentIds: string[],
|
|
viewer: ReactionViewer,
|
|
dbOrTx: any = db,
|
|
): Promise<Map<string, DocumentAnnotationCommentReactionSummary[]>> {
|
|
const byComment = new Map<string, DocumentAnnotationCommentReactionSummary[]>();
|
|
if (commentIds.length === 0) return byComment;
|
|
const rows: Array<{
|
|
commentId: string;
|
|
reaction: DocumentAnnotationCommentReactionName;
|
|
actorType: "agent" | "user";
|
|
actorId: string;
|
|
}> = await dbOrTx
|
|
.select({
|
|
commentId: documentAnnotationCommentReactions.commentId,
|
|
reaction: documentAnnotationCommentReactions.reaction,
|
|
actorType: documentAnnotationCommentReactions.actorType,
|
|
actorId: documentAnnotationCommentReactions.actorId,
|
|
})
|
|
.from(documentAnnotationCommentReactions)
|
|
.where(and(
|
|
inArray(documentAnnotationCommentReactions.commentId, commentIds),
|
|
isNull(documentAnnotationCommentReactions.removedAt),
|
|
))
|
|
.orderBy(asc(documentAnnotationCommentReactions.createdAt), asc(documentAnnotationCommentReactions.id));
|
|
const grouped = new Map<string, Map<DocumentAnnotationCommentReactionName, DocumentAnnotationCommentReactionSummary>>();
|
|
for (const row of rows) {
|
|
const perComment = grouped.get(row.commentId) ?? new Map();
|
|
grouped.set(row.commentId, perComment);
|
|
const summary = perComment.get(row.reaction) ?? { reaction: row.reaction, count: 0, reactedByMe: false, actors: [] };
|
|
perComment.set(row.reaction, summary);
|
|
summary.count += 1;
|
|
summary.actors.push({
|
|
actorType: row.actorType,
|
|
agentId: row.actorType === "agent" ? row.actorId : null,
|
|
userId: row.actorType === "user" ? row.actorId : null,
|
|
});
|
|
if (viewer && viewer.actorType === row.actorType && viewer.actorId === row.actorId) summary.reactedByMe = true;
|
|
}
|
|
for (const [commentId, perComment] of grouped) {
|
|
byComment.set(
|
|
commentId,
|
|
DOCUMENT_ANNOTATION_COMMENT_REACTIONS.flatMap((reaction) => perComment.get(reaction) ?? []),
|
|
);
|
|
}
|
|
return byComment;
|
|
}
|
|
|
|
async function withReactions(
|
|
comments: DocumentAnnotationComment[],
|
|
viewer: ReactionViewer,
|
|
dbOrTx: any = db,
|
|
): Promise<DocumentAnnotationComment[]> {
|
|
const reactions = await reactionsForComments(comments.map((comment) => comment.id), viewer, dbOrTx);
|
|
return comments.map((comment) => ({ ...comment, reactions: reactions.get(comment.id) ?? [] }));
|
|
}
|
|
|
|
async function commentsForThreads(
|
|
threadIds: string[],
|
|
dbOrTx: any = db,
|
|
viewer?: ReactionViewer,
|
|
): Promise<DocumentAnnotationComment[]> {
|
|
if (threadIds.length === 0) return [];
|
|
const comments: DocumentAnnotationComment[] = await dbOrTx
|
|
.select(commentSelect)
|
|
.from(documentAnnotationComments)
|
|
.where(inArray(documentAnnotationComments.threadId, threadIds))
|
|
.orderBy(asc(documentAnnotationComments.createdAt), asc(documentAnnotationComments.id));
|
|
return withReactions(comments, viewer, dbOrTx);
|
|
}
|
|
|
|
async function getThreadForReactionTarget(
|
|
target: DocumentAnnotationReactionTarget,
|
|
threadId: string,
|
|
dbOrTx: any,
|
|
): Promise<DocumentAnnotationThread | null> {
|
|
if (target.kind === "issue") return getThreadForIssue(target.issueId, target.key, threadId, dbOrTx);
|
|
if (target.kind === "routine") {
|
|
const doc = await getRoutineDocument(target.routineId, target.key, dbOrTx);
|
|
if (!doc) throw notFound("Document not found");
|
|
return getThreadForRoutine(target.routineId, target.key, threadId, doc.companyId, doc.documentId, dbOrTx);
|
|
}
|
|
const doc = await getCaseDocument(target.caseId, target.key, dbOrTx);
|
|
if (!doc) throw notFound("Document not found");
|
|
return getThreadForCase(target.caseId, target.key, threadId, doc.companyId, doc.documentId, dbOrTx);
|
|
}
|
|
|
|
async function assertLinkedIssueComment(
|
|
issueId: string,
|
|
commentId: string | null | undefined,
|
|
dbOrTx: any = db,
|
|
) {
|
|
if (!commentId) return null;
|
|
const comment = await dbOrTx
|
|
.select({
|
|
id: issueComments.id,
|
|
companyId: issueComments.companyId,
|
|
issueId: issueComments.issueId,
|
|
})
|
|
.from(issueComments)
|
|
.where(and(eq(issueComments.id, commentId), isNull(issueComments.deletedAt)))
|
|
.then((rows: Array<{ id: string; companyId: string; issueId: string }>) => rows[0] ?? null);
|
|
if (!comment || comment.issueId !== issueId) {
|
|
throw unprocessable("Linked issue comment must belong to this issue");
|
|
}
|
|
return comment;
|
|
}
|
|
|
|
return {
|
|
listThreadsForIssueDocument: async (
|
|
issueId: string,
|
|
key: string,
|
|
options: ListThreadOptions = {},
|
|
) => {
|
|
const doc = await getIssueDocument(issueId, key);
|
|
if (!doc) throw notFound("Document not found");
|
|
const conditions = [
|
|
eq(documentAnnotationThreads.issueId, issueId),
|
|
eq(documentAnnotationThreads.documentId, doc.documentId),
|
|
];
|
|
if (options.status && options.status !== "all") {
|
|
conditions.push(eq(documentAnnotationThreads.status, options.status));
|
|
}
|
|
const threads: DocumentAnnotationThread[] = await db
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(...conditions))
|
|
.orderBy(desc(documentAnnotationThreads.updatedAt), desc(documentAnnotationThreads.id));
|
|
if (!options.includeComments) return threads;
|
|
const comments = await commentsForThreads(threads.map((thread) => thread.id), db, options.viewer);
|
|
const commentsByThread = new Map<string, DocumentAnnotationComment[]>();
|
|
for (const comment of comments) {
|
|
const existing = commentsByThread.get(comment.threadId) ?? [];
|
|
existing.push(comment);
|
|
commentsByThread.set(comment.threadId, existing);
|
|
}
|
|
return threads.map((thread) => ({
|
|
...thread,
|
|
comments: commentsByThread.get(thread.id) ?? [],
|
|
}));
|
|
},
|
|
|
|
listThreadsForRoutineDocument: async (
|
|
routineId: string,
|
|
key: string,
|
|
options: ListThreadOptions = {},
|
|
) => {
|
|
const doc = await getRoutineDocument(routineId, key);
|
|
if (!doc) throw notFound("Document not found");
|
|
const conditions = [
|
|
eq(documentAnnotationThreads.companyId, doc.companyId),
|
|
eq(documentAnnotationThreads.routineId, routineId),
|
|
eq(documentAnnotationThreads.documentId, doc.documentId),
|
|
];
|
|
if (options.status && options.status !== "all") {
|
|
conditions.push(eq(documentAnnotationThreads.status, options.status));
|
|
}
|
|
const threads: DocumentAnnotationThread[] = await db
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(...conditions))
|
|
.orderBy(desc(documentAnnotationThreads.updatedAt), desc(documentAnnotationThreads.id));
|
|
if (!options.includeComments) return threads;
|
|
const comments = await commentsForThreads(threads.map((thread) => thread.id), db, options.viewer);
|
|
const commentsByThread = new Map<string, DocumentAnnotationComment[]>();
|
|
for (const comment of comments) {
|
|
const existing = commentsByThread.get(comment.threadId) ?? [];
|
|
existing.push(comment);
|
|
commentsByThread.set(comment.threadId, existing);
|
|
}
|
|
return threads.map((thread) => ({
|
|
...thread,
|
|
comments: commentsByThread.get(thread.id) ?? [],
|
|
}));
|
|
},
|
|
|
|
listThreadsForCaseDocument: async (
|
|
caseId: string,
|
|
key: string,
|
|
options: ListThreadOptions = {},
|
|
) => {
|
|
const doc = await getCaseDocument(caseId, key);
|
|
if (!doc) throw notFound("Document not found");
|
|
const conditions = [
|
|
eq(documentAnnotationThreads.companyId, doc.companyId),
|
|
eq(documentAnnotationThreads.caseId, caseId),
|
|
eq(documentAnnotationThreads.documentId, doc.documentId),
|
|
];
|
|
if (options.status && options.status !== "all") {
|
|
conditions.push(eq(documentAnnotationThreads.status, options.status));
|
|
}
|
|
const threads: DocumentAnnotationThread[] = await db
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(...conditions))
|
|
.orderBy(desc(documentAnnotationThreads.updatedAt), desc(documentAnnotationThreads.id));
|
|
if (!options.includeComments) return threads;
|
|
const comments = await commentsForThreads(threads.map((thread) => thread.id), db, options.viewer);
|
|
const commentsByThread = new Map<string, DocumentAnnotationComment[]>();
|
|
for (const comment of comments) {
|
|
const existing = commentsByThread.get(comment.threadId) ?? [];
|
|
existing.push(comment);
|
|
commentsByThread.set(comment.threadId, existing);
|
|
}
|
|
return threads.map((thread) => ({
|
|
...thread,
|
|
comments: commentsByThread.get(thread.id) ?? [],
|
|
}));
|
|
},
|
|
|
|
getThreadForIssueDocument: async (issueId: string, key: string, threadId: string, viewer?: ReactionViewer) => {
|
|
const thread = await getThreadForIssue(issueId, key, threadId);
|
|
if (!thread) return null;
|
|
const comments = await commentsForThreads([thread.id], db, viewer);
|
|
return { ...thread, comments };
|
|
},
|
|
|
|
getThreadForRoutineDocument: async (routineId: string, key: string, threadId: string, viewer?: ReactionViewer) => {
|
|
const doc = await getRoutineDocument(routineId, key);
|
|
if (!doc) return null;
|
|
const thread = await getThreadForRoutine(routineId, key, threadId, doc.companyId, doc.documentId);
|
|
if (!thread) return null;
|
|
const comments = await commentsForThreads([thread.id], db, viewer);
|
|
return { ...thread, comments };
|
|
},
|
|
|
|
getThreadForCaseDocument: async (caseId: string, key: string, threadId: string, viewer?: ReactionViewer) => {
|
|
const doc = await getCaseDocument(caseId, key);
|
|
if (!doc) return null;
|
|
const thread = await getThreadForCase(caseId, key, threadId, doc.companyId, doc.documentId);
|
|
if (!thread) return null;
|
|
const comments = await commentsForThreads([thread.id], db, viewer);
|
|
return { ...thread, comments };
|
|
},
|
|
|
|
createThread: async (
|
|
issueId: string,
|
|
key: string,
|
|
input: CreateDocumentAnnotationThread,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
await tx.execute(sql`
|
|
select ${documents.id}
|
|
from ${issueDocuments}
|
|
inner join ${documents} on ${issueDocuments.documentId} = ${documents.id}
|
|
where ${and(eq(issueDocuments.issueId, issueId), eq(issueDocuments.key, key))}
|
|
for update of ${documents}
|
|
`);
|
|
const doc = await getIssueDocument(issueId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
await assertAgentInteraction(tx, { companyId: doc.companyId, actorAgentId: actor.agentId,
|
|
operation: "comment", issueId, texts: [input.body], lock: true });
|
|
if (
|
|
input.baseRevisionId !== doc.latestRevisionId
|
|
|| input.baseRevisionNumber !== doc.latestRevisionNumber
|
|
) {
|
|
throw conflict("Annotation anchor requires the current document revision", {
|
|
currentRevisionId: doc.latestRevisionId,
|
|
currentRevisionNumber: doc.latestRevisionNumber,
|
|
});
|
|
}
|
|
|
|
const verification = verifyDocumentAnchorSelector({
|
|
markdown: doc.latestBody,
|
|
selector: input.selector,
|
|
});
|
|
if (!verification.ok || !verification.anchor) {
|
|
throw unprocessable("Annotation anchor does not match the current document revision", {
|
|
reason: verification.reason,
|
|
});
|
|
}
|
|
|
|
const now = new Date();
|
|
const linkedIssueComment = await assertLinkedIssueComment(issueId, input.issueCommentId, tx);
|
|
const [thread] = await tx
|
|
.insert(documentAnnotationThreads)
|
|
.values({
|
|
companyId: doc.companyId,
|
|
issueId,
|
|
documentId: doc.documentId,
|
|
documentKey: doc.documentKey,
|
|
status: "open",
|
|
anchorState: "active",
|
|
anchorConfidence: "exact",
|
|
originalRevisionId: doc.latestRevisionId,
|
|
originalRevisionNumber: doc.latestRevisionNumber,
|
|
currentRevisionId: doc.latestRevisionId,
|
|
currentRevisionNumber: doc.latestRevisionNumber,
|
|
selectedText: verification.anchor.selectedText,
|
|
prefixText: verification.anchor.prefixText,
|
|
suffixText: verification.anchor.suffixText,
|
|
normalizedStart: verification.anchor.normalizedStart,
|
|
normalizedEnd: verification.anchor.normalizedEnd,
|
|
markdownStart: verification.anchor.markdownStart,
|
|
markdownEnd: verification.anchor.markdownEnd,
|
|
anchorSelector: input.selector,
|
|
createdByAgentId: actor.agentId ?? null,
|
|
createdByUserId: actor.userId ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(threadSelect);
|
|
|
|
const [comment] = await tx
|
|
.insert(documentAnnotationComments)
|
|
.values({
|
|
companyId: doc.companyId,
|
|
threadId: thread.id,
|
|
issueId,
|
|
documentId: doc.documentId,
|
|
body: input.body,
|
|
authorType: actor.actorType,
|
|
authorAgentId: actor.agentId ?? null,
|
|
authorUserId: actor.userId ?? null,
|
|
createdByRunId: actor.runId ?? null,
|
|
issueCommentId: linkedIssueComment?.id ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(commentSelect);
|
|
|
|
return { ...thread, comments: [comment] };
|
|
}),
|
|
|
|
createRoutineThread: async (
|
|
routineId: string,
|
|
key: string,
|
|
input: CreateDocumentAnnotationThread,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
await tx.execute(sql`
|
|
select ${documents.id}
|
|
from ${routineDocuments}
|
|
inner join ${documents} on ${routineDocuments.documentId} = ${documents.id}
|
|
where ${and(eq(routineDocuments.routineId, routineId), eq(routineDocuments.key, key))}
|
|
for update of ${documents}
|
|
`);
|
|
const doc = await getRoutineDocument(routineId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
if (
|
|
input.baseRevisionId !== doc.latestRevisionId
|
|
|| input.baseRevisionNumber !== doc.latestRevisionNumber
|
|
) {
|
|
throw conflict("Annotation anchor requires the current document revision", {
|
|
currentRevisionId: doc.latestRevisionId,
|
|
currentRevisionNumber: doc.latestRevisionNumber,
|
|
});
|
|
}
|
|
|
|
const verification = verifyDocumentAnchorSelector({
|
|
markdown: doc.latestBody,
|
|
selector: input.selector,
|
|
});
|
|
if (!verification.ok || !verification.anchor) {
|
|
throw unprocessable("Annotation anchor does not match the current document revision", {
|
|
reason: verification.reason,
|
|
});
|
|
}
|
|
|
|
const now = new Date();
|
|
const [thread] = await tx
|
|
.insert(documentAnnotationThreads)
|
|
.values({
|
|
companyId: doc.companyId,
|
|
issueId: null,
|
|
routineId,
|
|
documentId: doc.documentId,
|
|
documentKey: doc.documentKey,
|
|
status: "open",
|
|
anchorState: "active",
|
|
anchorConfidence: "exact",
|
|
originalRevisionId: doc.latestRevisionId,
|
|
originalRevisionNumber: doc.latestRevisionNumber,
|
|
currentRevisionId: doc.latestRevisionId,
|
|
currentRevisionNumber: doc.latestRevisionNumber,
|
|
selectedText: verification.anchor.selectedText,
|
|
prefixText: verification.anchor.prefixText,
|
|
suffixText: verification.anchor.suffixText,
|
|
normalizedStart: verification.anchor.normalizedStart,
|
|
normalizedEnd: verification.anchor.normalizedEnd,
|
|
markdownStart: verification.anchor.markdownStart,
|
|
markdownEnd: verification.anchor.markdownEnd,
|
|
anchorSelector: input.selector,
|
|
createdByAgentId: actor.agentId ?? null,
|
|
createdByUserId: actor.userId ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(threadSelect);
|
|
|
|
const [comment] = await tx
|
|
.insert(documentAnnotationComments)
|
|
.values({
|
|
companyId: doc.companyId,
|
|
threadId: thread.id,
|
|
issueId: null,
|
|
routineId,
|
|
documentId: doc.documentId,
|
|
body: input.body,
|
|
authorType: actor.actorType,
|
|
authorAgentId: actor.agentId ?? null,
|
|
authorUserId: actor.userId ?? null,
|
|
createdByRunId: actor.runId ?? null,
|
|
issueCommentId: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(commentSelect);
|
|
|
|
return { ...thread, comments: [comment] };
|
|
}),
|
|
|
|
createCaseThread: async (
|
|
caseId: string,
|
|
key: string,
|
|
input: CreateDocumentAnnotationThread,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
await tx.execute(sql`
|
|
select ${documents.id}
|
|
from ${caseDocuments}
|
|
inner join ${documents} on ${caseDocuments.documentId} = ${documents.id}
|
|
where ${and(eq(caseDocuments.caseId, caseId), eq(caseDocuments.key, key))}
|
|
for update of ${documents}
|
|
`);
|
|
const doc = await getCaseDocument(caseId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
if (
|
|
input.baseRevisionId !== doc.latestRevisionId
|
|
|| input.baseRevisionNumber !== doc.latestRevisionNumber
|
|
) {
|
|
throw conflict("Annotation anchor requires the current document revision", {
|
|
currentRevisionId: doc.latestRevisionId,
|
|
currentRevisionNumber: doc.latestRevisionNumber,
|
|
});
|
|
}
|
|
|
|
const verification = verifyDocumentAnchorSelector({
|
|
markdown: doc.latestBody,
|
|
selector: input.selector,
|
|
});
|
|
if (!verification.ok || !verification.anchor) {
|
|
throw unprocessable("Annotation anchor does not match the current document revision", {
|
|
reason: verification.reason,
|
|
});
|
|
}
|
|
|
|
const now = new Date();
|
|
const [thread] = await tx
|
|
.insert(documentAnnotationThreads)
|
|
.values({
|
|
companyId: doc.companyId,
|
|
issueId: null,
|
|
routineId: null,
|
|
caseId,
|
|
documentId: doc.documentId,
|
|
documentKey: doc.documentKey,
|
|
status: "open",
|
|
anchorState: "active",
|
|
anchorConfidence: "exact",
|
|
originalRevisionId: doc.latestRevisionId,
|
|
originalRevisionNumber: doc.latestRevisionNumber,
|
|
currentRevisionId: doc.latestRevisionId,
|
|
currentRevisionNumber: doc.latestRevisionNumber,
|
|
selectedText: verification.anchor.selectedText,
|
|
prefixText: verification.anchor.prefixText,
|
|
suffixText: verification.anchor.suffixText,
|
|
normalizedStart: verification.anchor.normalizedStart,
|
|
normalizedEnd: verification.anchor.normalizedEnd,
|
|
markdownStart: verification.anchor.markdownStart,
|
|
markdownEnd: verification.anchor.markdownEnd,
|
|
anchorSelector: input.selector,
|
|
createdByAgentId: actor.agentId ?? null,
|
|
createdByUserId: actor.userId ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(threadSelect);
|
|
|
|
const [comment] = await tx
|
|
.insert(documentAnnotationComments)
|
|
.values({
|
|
companyId: doc.companyId,
|
|
threadId: thread.id,
|
|
issueId: null,
|
|
routineId: null,
|
|
caseId,
|
|
documentId: doc.documentId,
|
|
body: input.body,
|
|
authorType: actor.actorType,
|
|
authorAgentId: actor.agentId ?? null,
|
|
authorUserId: actor.userId ?? null,
|
|
createdByRunId: actor.runId ?? null,
|
|
issueCommentId: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(commentSelect);
|
|
|
|
return { ...thread, comments: [comment] };
|
|
}),
|
|
|
|
addComment: async (
|
|
issueId: string,
|
|
key: string,
|
|
threadId: string,
|
|
input: CreateDocumentAnnotationComment,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
const thread = await getThreadForIssue(issueId, key, threadId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
await assertAgentInteraction(tx, { companyId: thread.companyId, actorAgentId: actor.agentId,
|
|
operation: "comment", issueId, texts: [input.body], lock: true });
|
|
const now = new Date();
|
|
const linkedIssueComment = await assertLinkedIssueComment(issueId, input.issueCommentId, tx);
|
|
const [comment] = await tx
|
|
.insert(documentAnnotationComments)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
threadId: thread.id,
|
|
issueId: thread.issueId,
|
|
documentId: thread.documentId,
|
|
body: input.body,
|
|
authorType: actor.actorType,
|
|
authorAgentId: actor.agentId ?? null,
|
|
authorUserId: actor.userId ?? null,
|
|
createdByRunId: actor.runId ?? null,
|
|
issueCommentId: linkedIssueComment?.id ?? null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(commentSelect);
|
|
await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({ updatedAt: now })
|
|
.where(eq(documentAnnotationThreads.id, thread.id));
|
|
return comment;
|
|
}),
|
|
|
|
addRoutineComment: async (
|
|
routineId: string,
|
|
key: string,
|
|
threadId: string,
|
|
input: CreateDocumentAnnotationComment,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
const doc = await getRoutineDocument(routineId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
const thread = await getThreadForRoutine(routineId, key, threadId, doc.companyId, doc.documentId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
const now = new Date();
|
|
const [comment] = await tx
|
|
.insert(documentAnnotationComments)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
threadId: thread.id,
|
|
issueId: null,
|
|
routineId: thread.routineId,
|
|
documentId: thread.documentId,
|
|
body: input.body,
|
|
authorType: actor.actorType,
|
|
authorAgentId: actor.agentId ?? null,
|
|
authorUserId: actor.userId ?? null,
|
|
createdByRunId: actor.runId ?? null,
|
|
issueCommentId: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(commentSelect);
|
|
await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({ updatedAt: now })
|
|
.where(eq(documentAnnotationThreads.id, thread.id));
|
|
return comment;
|
|
}),
|
|
|
|
addCaseComment: async (
|
|
caseId: string,
|
|
key: string,
|
|
threadId: string,
|
|
input: CreateDocumentAnnotationComment,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
const doc = await getCaseDocument(caseId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
const thread = await getThreadForCase(caseId, key, threadId, doc.companyId, doc.documentId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
const now = new Date();
|
|
const [comment] = await tx
|
|
.insert(documentAnnotationComments)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
threadId: thread.id,
|
|
issueId: null,
|
|
routineId: null,
|
|
caseId: thread.caseId,
|
|
documentId: thread.documentId,
|
|
body: input.body,
|
|
authorType: actor.actorType,
|
|
authorAgentId: actor.agentId ?? null,
|
|
authorUserId: actor.userId ?? null,
|
|
createdByRunId: actor.runId ?? null,
|
|
issueCommentId: null,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.returning(commentSelect);
|
|
await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({ updatedAt: now })
|
|
.where(eq(documentAnnotationThreads.id, thread.id));
|
|
return comment;
|
|
}),
|
|
|
|
setCommentReaction: async (input: {
|
|
target: DocumentAnnotationReactionTarget;
|
|
threadId: string;
|
|
commentId: string;
|
|
reaction: DocumentAnnotationCommentReactionName;
|
|
present: boolean;
|
|
actor: ActorInput;
|
|
}) => db.transaction(async (tx) => {
|
|
const thread = await getThreadForReactionTarget(input.target, input.threadId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
if (input.target.kind === "issue") {
|
|
await assertAgentInteraction(tx, { companyId: thread.companyId, actorAgentId: input.actor.agentId,
|
|
operation: "comment", issueId: input.target.issueId, texts: [], lock: true });
|
|
}
|
|
const [comment]: DocumentAnnotationComment[] = await tx
|
|
.select(commentSelect)
|
|
.from(documentAnnotationComments)
|
|
.where(and(
|
|
eq(documentAnnotationComments.id, input.commentId),
|
|
eq(documentAnnotationComments.threadId, thread.id),
|
|
eq(documentAnnotationComments.companyId, thread.companyId),
|
|
));
|
|
if (!comment) throw notFound("Annotation comment not found");
|
|
|
|
const changedRows: Array<{ id: string; firstAdd: boolean }> = input.present
|
|
? await tx
|
|
.insert(documentAnnotationCommentReactions)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
commentId: comment.id,
|
|
reaction: input.reaction,
|
|
actorType: input.actor.actorType,
|
|
actorId: input.actor.actorId,
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: [
|
|
documentAnnotationCommentReactions.commentId,
|
|
documentAnnotationCommentReactions.reaction,
|
|
documentAnnotationCommentReactions.actorType,
|
|
documentAnnotationCommentReactions.actorId,
|
|
],
|
|
set: { removedAt: null },
|
|
setWhere: sql`${documentAnnotationCommentReactions.removedAt} is not null`,
|
|
})
|
|
.returning({ id: documentAnnotationCommentReactions.id, firstAdd: sql<boolean>`(xmax = 0)` })
|
|
: await tx
|
|
.update(documentAnnotationCommentReactions)
|
|
.set({ removedAt: new Date() })
|
|
.where(and(
|
|
eq(documentAnnotationCommentReactions.commentId, comment.id),
|
|
eq(documentAnnotationCommentReactions.reaction, input.reaction),
|
|
eq(documentAnnotationCommentReactions.actorType, input.actor.actorType),
|
|
eq(documentAnnotationCommentReactions.actorId, input.actor.actorId),
|
|
isNull(documentAnnotationCommentReactions.removedAt),
|
|
))
|
|
.returning({ id: documentAnnotationCommentReactions.id, firstAdd: sql<boolean>`false` });
|
|
const changed = changedRows.length > 0;
|
|
const firstAdd = changedRows[0]?.firstAdd === true;
|
|
let questionWakeRequestId: string | null = null;
|
|
let questionWakeAgentId: string | null = null;
|
|
let questionWakeSuppressed = false;
|
|
if (
|
|
input.present &&
|
|
firstAdd &&
|
|
input.reaction === "question" &&
|
|
input.actor.actorType === "user" &&
|
|
input.target.kind === "issue"
|
|
) {
|
|
const [issue] = await tx
|
|
.select({
|
|
id: issues.id,
|
|
status: issues.status,
|
|
assigneeAgentId: issues.assigneeAgentId,
|
|
conversationAgentId: issues.conversationAgentId,
|
|
conversationUserId: issues.conversationUserId,
|
|
})
|
|
.from(issues)
|
|
.where(and(eq(issues.companyId, thread.companyId), eq(issues.id, input.target.issueId)))
|
|
.for("update");
|
|
const chatOwnerAllowed =
|
|
!issue?.conversationAgentId ||
|
|
(issue.assigneeAgentId === issue.conversationAgentId &&
|
|
issue.conversationUserId === input.actor.actorId);
|
|
if (
|
|
issue &&
|
|
issue.assigneeAgentId &&
|
|
issue.status !== "done" &&
|
|
issue.status !== "cancelled" &&
|
|
chatOwnerAllowed
|
|
) {
|
|
const [existingPending] = await tx
|
|
.select({ id: agentWakeupRequests.id })
|
|
.from(agentWakeupRequests)
|
|
.where(and(
|
|
eq(agentWakeupRequests.companyId, thread.companyId),
|
|
eq(agentWakeupRequests.agentId, issue.assigneeAgentId),
|
|
inArray(agentWakeupRequests.status, ["deferred_issue_execution", "queued", "claimed"]),
|
|
sql`${agentWakeupRequests.payload}->>'issueId' = ${issue.id}`,
|
|
sql`${agentWakeupRequests.payload}->>'mutation' = 'annotation_reaction'`,
|
|
))
|
|
.limit(1);
|
|
const windowStart = new Date(Date.now() - ANNOTATION_QUESTION_WAKE_WINDOW_MS);
|
|
const recentActorWakes = existingPending
|
|
? []
|
|
: await tx
|
|
.select({ id: agentWakeupRequests.id })
|
|
.from(agentWakeupRequests)
|
|
.where(and(
|
|
eq(agentWakeupRequests.companyId, thread.companyId),
|
|
eq(agentWakeupRequests.requestedByActorType, "user"),
|
|
eq(agentWakeupRequests.requestedByActorId, input.actor.actorId),
|
|
gte(agentWakeupRequests.requestedAt, windowStart),
|
|
sql`${agentWakeupRequests.idempotencyKey} like ${ANNOTATION_QUESTION_REACTION_KEY_PREFIX + "%"}`,
|
|
sql`${agentWakeupRequests.payload}->>'issueId' = ${issue.id}`,
|
|
sql`${agentWakeupRequests.payload}->>'mutation' = 'annotation_reaction'`,
|
|
))
|
|
.limit(ANNOTATION_QUESTION_WAKE_MAX_PER_WINDOW);
|
|
if (existingPending || recentActorWakes.length >= ANNOTATION_QUESTION_WAKE_MAX_PER_WINDOW) {
|
|
questionWakeSuppressed = true;
|
|
} else {
|
|
const wakeRequestId = randomUUID();
|
|
await notifyDeferredExecutionWork(tx, {
|
|
companyId: thread.companyId,
|
|
requestId: wakeRequestId,
|
|
});
|
|
await tx.insert(agentWakeupRequests).values({
|
|
id: wakeRequestId,
|
|
companyId: thread.companyId,
|
|
agentId: issue.assigneeAgentId,
|
|
source: "automation",
|
|
triggerDetail: "system",
|
|
reason: "issue_commented",
|
|
status: "deferred_issue_execution",
|
|
requestedByActorType: "user",
|
|
requestedByActorId: input.actor.actorId,
|
|
idempotencyKey: `${ANNOTATION_QUESTION_REACTION_KEY_PREFIX}${changedRows[0]!.id}`,
|
|
payload: {
|
|
issueId: issue.id,
|
|
annotationCommentId: comment.id,
|
|
annotationThreadId: thread.id,
|
|
annotationReaction: "question",
|
|
mutation: "annotation_reaction",
|
|
executionWait: deferredWorkIntentWait(
|
|
"wake_lost",
|
|
"Waiting to deliver annotation question reaction.",
|
|
),
|
|
_paperclipWakeContext: {
|
|
issueId: issue.id,
|
|
taskId: issue.id,
|
|
annotationCommentId: comment.id,
|
|
annotationThreadId: thread.id,
|
|
annotationReaction: "question",
|
|
annotationReactionUserId: input.actor.actorId,
|
|
source: "issue.document_annotation_reaction",
|
|
wakeReason: "issue_commented",
|
|
},
|
|
},
|
|
});
|
|
questionWakeRequestId = wakeRequestId;
|
|
questionWakeAgentId = issue.assigneeAgentId;
|
|
}
|
|
} else {
|
|
questionWakeSuppressed = true;
|
|
}
|
|
}
|
|
const [withSummary] = await withReactions([comment], input.actor, tx);
|
|
return {
|
|
thread,
|
|
comment: withSummary!,
|
|
changed,
|
|
firstAdd,
|
|
questionWakeRequestId,
|
|
questionWakeAgentId,
|
|
questionWakeSuppressed,
|
|
};
|
|
}),
|
|
|
|
cleanupForIssueCommentDeletion: async (
|
|
issueId: string,
|
|
issueCommentId: string,
|
|
actor: ActorInput,
|
|
dbOrTx: any = db,
|
|
) => {
|
|
const runCleanup = async (tx: any) => {
|
|
const linkedComments: Array<Pick<DocumentAnnotationComment, "id" | "threadId">> = await tx
|
|
.select({
|
|
id: documentAnnotationComments.id,
|
|
threadId: documentAnnotationComments.threadId,
|
|
})
|
|
.from(documentAnnotationComments)
|
|
.where(and(
|
|
eq(documentAnnotationComments.issueId, issueId),
|
|
eq(documentAnnotationComments.issueCommentId, issueCommentId),
|
|
));
|
|
if (linkedComments.length === 0) {
|
|
return { deletedCommentIds: [], resolvedThreadIds: [] };
|
|
}
|
|
|
|
const deletedCommentIds = linkedComments.map((comment) => comment.id);
|
|
const threadIds = [...new Set(linkedComments.map((comment) => comment.threadId))];
|
|
const now = new Date();
|
|
|
|
await tx
|
|
.delete(documentAnnotationComments)
|
|
.where(inArray(documentAnnotationComments.id, deletedCommentIds));
|
|
|
|
const remainingRows: Array<{ threadId: string; count: number }> = await tx
|
|
.select({
|
|
threadId: documentAnnotationComments.threadId,
|
|
count: sql<number>`count(*)::int`,
|
|
})
|
|
.from(documentAnnotationComments)
|
|
.where(inArray(documentAnnotationComments.threadId, threadIds))
|
|
.groupBy(documentAnnotationComments.threadId);
|
|
const remainingByThreadId = new Map(remainingRows.map((row) => [row.threadId, Number(row.count)]));
|
|
const emptyThreadIds = threadIds.filter((threadId) => (remainingByThreadId.get(threadId) ?? 0) === 0);
|
|
|
|
if (emptyThreadIds.length > 0) {
|
|
const resolvedRows: Array<{ id: string }> = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({
|
|
status: "resolved",
|
|
resolvedByAgentId: actor.actorType === "agent" ? actor.agentId ?? null : null,
|
|
resolvedByUserId: actor.actorType === "user" ? actor.userId ?? null : null,
|
|
resolvedAt: now,
|
|
updatedAt: now,
|
|
})
|
|
.where(and(
|
|
inArray(documentAnnotationThreads.id, emptyThreadIds),
|
|
eq(documentAnnotationThreads.status, "open"),
|
|
))
|
|
.returning({ id: documentAnnotationThreads.id });
|
|
|
|
return { deletedCommentIds, resolvedThreadIds: resolvedRows.map((row) => row.id) };
|
|
}
|
|
|
|
return { deletedCommentIds, resolvedThreadIds: [] };
|
|
};
|
|
|
|
return dbOrTx === db ? db.transaction(runCleanup) : runCleanup(dbOrTx);
|
|
},
|
|
|
|
updateThread: async (
|
|
issueId: string,
|
|
key: string,
|
|
threadId: string,
|
|
input: UpdateDocumentAnnotationThread,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
const thread = await getThreadForIssue(issueId, key, threadId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
await assertAgentInteraction(tx, { companyId: thread.companyId, actorAgentId: actor.agentId,
|
|
operation: "edit", issueId, lock: true });
|
|
if (!input.status || input.status === thread.status) return thread;
|
|
|
|
const now = new Date();
|
|
const [updated] = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set(input.status === "resolved"
|
|
? {
|
|
status: "resolved",
|
|
resolvedByAgentId: actor.agentId ?? null,
|
|
resolvedByUserId: actor.userId ?? null,
|
|
resolvedAt: now,
|
|
updatedAt: now,
|
|
}
|
|
: {
|
|
status: "open",
|
|
resolvedByAgentId: null,
|
|
resolvedByUserId: null,
|
|
resolvedAt: null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documentAnnotationThreads.id, thread.id))
|
|
.returning(threadSelect);
|
|
return updated;
|
|
}),
|
|
|
|
updateRoutineThread: async (
|
|
routineId: string,
|
|
key: string,
|
|
threadId: string,
|
|
input: UpdateDocumentAnnotationThread,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
const doc = await getRoutineDocument(routineId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
const thread = await getThreadForRoutine(routineId, key, threadId, doc.companyId, doc.documentId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
if (!input.status || input.status === thread.status) return thread;
|
|
|
|
const now = new Date();
|
|
const [updated] = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set(input.status === "resolved"
|
|
? {
|
|
status: "resolved",
|
|
resolvedByAgentId: actor.agentId ?? null,
|
|
resolvedByUserId: actor.userId ?? null,
|
|
resolvedAt: now,
|
|
updatedAt: now,
|
|
}
|
|
: {
|
|
status: "open",
|
|
resolvedByAgentId: null,
|
|
resolvedByUserId: null,
|
|
resolvedAt: null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documentAnnotationThreads.id, thread.id))
|
|
.returning(threadSelect);
|
|
return updated;
|
|
}),
|
|
|
|
updateCaseThread: async (
|
|
caseId: string,
|
|
key: string,
|
|
threadId: string,
|
|
input: UpdateDocumentAnnotationThread,
|
|
actor: ActorInput,
|
|
) => db.transaction(async (tx) => {
|
|
const doc = await getCaseDocument(caseId, key, tx);
|
|
if (!doc) throw notFound("Document not found");
|
|
const thread = await getThreadForCase(caseId, key, threadId, doc.companyId, doc.documentId, tx);
|
|
if (!thread) throw notFound("Annotation thread not found");
|
|
if (!input.status || input.status === thread.status) return thread;
|
|
|
|
const now = new Date();
|
|
const [updated] = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set(input.status === "resolved"
|
|
? {
|
|
status: "resolved",
|
|
resolvedByAgentId: actor.agentId ?? null,
|
|
resolvedByUserId: actor.userId ?? null,
|
|
resolvedAt: now,
|
|
updatedAt: now,
|
|
}
|
|
: {
|
|
status: "open",
|
|
resolvedByAgentId: null,
|
|
resolvedByUserId: null,
|
|
resolvedAt: null,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documentAnnotationThreads.id, thread.id))
|
|
.returning(threadSelect);
|
|
return updated;
|
|
}),
|
|
|
|
remapOpenThreadsForDocument: async (input: {
|
|
issueId: string;
|
|
key: string;
|
|
documentId: string;
|
|
nextRevisionId: string | null;
|
|
nextRevisionNumber: number;
|
|
nextBody: string;
|
|
}) => db.transaction(async (tx) => {
|
|
const threads: DocumentAnnotationThread[] = await tx
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(
|
|
eq(documentAnnotationThreads.issueId, input.issueId),
|
|
eq(documentAnnotationThreads.documentId, input.documentId),
|
|
eq(documentAnnotationThreads.status, "open"),
|
|
));
|
|
const changed = [];
|
|
const now = new Date();
|
|
|
|
for (const thread of threads) {
|
|
if (thread.currentRevisionId === input.nextRevisionId) continue;
|
|
const previousAnchor = snapshotFromThread(thread);
|
|
const remap = remapDocumentAnchor({
|
|
previousAnchor,
|
|
nextMarkdown: input.nextBody,
|
|
});
|
|
const nextAnchor = remap.anchor;
|
|
const nextSelector = nextAnchor ? anchorSnapshotToSelector(nextAnchor) : thread.anchorSelector;
|
|
const [updated] = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({
|
|
currentRevisionId: input.nextRevisionId,
|
|
currentRevisionNumber: input.nextRevisionNumber,
|
|
anchorState: remap.anchorState,
|
|
anchorConfidence: remap.confidence,
|
|
...(nextAnchor
|
|
? {
|
|
selectedText: nextAnchor.selectedText,
|
|
prefixText: nextAnchor.prefixText,
|
|
suffixText: nextAnchor.suffixText,
|
|
normalizedStart: nextAnchor.normalizedStart,
|
|
normalizedEnd: nextAnchor.normalizedEnd,
|
|
markdownStart: nextAnchor.markdownStart,
|
|
markdownEnd: nextAnchor.markdownEnd,
|
|
}
|
|
: {}),
|
|
anchorSelector: nextSelector,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documentAnnotationThreads.id, thread.id))
|
|
.returning(threadSelect);
|
|
const [snapshot] = await tx
|
|
.insert(documentAnnotationAnchorSnapshots)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
threadId: thread.id,
|
|
documentId: thread.documentId,
|
|
fromRevisionId: thread.currentRevisionId,
|
|
fromRevisionNumber: thread.currentRevisionNumber,
|
|
toRevisionId: input.nextRevisionId,
|
|
toRevisionNumber: input.nextRevisionNumber,
|
|
previousAnchor,
|
|
nextAnchor,
|
|
anchorState: remap.anchorState,
|
|
anchorConfidence: remap.confidence,
|
|
failureReason: remap.anchor ? null : remap.reason,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
changed.push({ thread: updated, snapshot });
|
|
}
|
|
|
|
return changed;
|
|
}),
|
|
|
|
remapOpenThreadsForRoutineDocument: async (input: {
|
|
routineId: string;
|
|
key: string;
|
|
documentId: string;
|
|
nextRevisionId: string | null;
|
|
nextRevisionNumber: number;
|
|
nextBody: string;
|
|
}) => db.transaction(async (tx) => {
|
|
const threads: DocumentAnnotationThread[] = await tx
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(
|
|
eq(documentAnnotationThreads.routineId, input.routineId),
|
|
eq(documentAnnotationThreads.documentId, input.documentId),
|
|
eq(documentAnnotationThreads.status, "open"),
|
|
));
|
|
const changed = [];
|
|
const now = new Date();
|
|
|
|
for (const thread of threads) {
|
|
if (thread.currentRevisionId === input.nextRevisionId) continue;
|
|
const previousAnchor = snapshotFromThread(thread);
|
|
const remap = remapDocumentAnchor({
|
|
previousAnchor,
|
|
nextMarkdown: input.nextBody,
|
|
});
|
|
const nextAnchor = remap.anchor;
|
|
const nextSelector = nextAnchor ? anchorSnapshotToSelector(nextAnchor) : thread.anchorSelector;
|
|
const [updated] = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({
|
|
currentRevisionId: input.nextRevisionId,
|
|
currentRevisionNumber: input.nextRevisionNumber,
|
|
anchorState: remap.anchorState,
|
|
anchorConfidence: remap.confidence,
|
|
...(nextAnchor
|
|
? {
|
|
selectedText: nextAnchor.selectedText,
|
|
prefixText: nextAnchor.prefixText,
|
|
suffixText: nextAnchor.suffixText,
|
|
normalizedStart: nextAnchor.normalizedStart,
|
|
normalizedEnd: nextAnchor.normalizedEnd,
|
|
markdownStart: nextAnchor.markdownStart,
|
|
markdownEnd: nextAnchor.markdownEnd,
|
|
}
|
|
: {}),
|
|
anchorSelector: nextSelector,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documentAnnotationThreads.id, thread.id))
|
|
.returning(threadSelect);
|
|
const [snapshot] = await tx
|
|
.insert(documentAnnotationAnchorSnapshots)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
threadId: thread.id,
|
|
documentId: thread.documentId,
|
|
fromRevisionId: thread.currentRevisionId,
|
|
fromRevisionNumber: thread.currentRevisionNumber,
|
|
toRevisionId: input.nextRevisionId,
|
|
toRevisionNumber: input.nextRevisionNumber,
|
|
previousAnchor,
|
|
nextAnchor,
|
|
anchorState: remap.anchorState,
|
|
anchorConfidence: remap.confidence,
|
|
failureReason: remap.anchor ? null : remap.reason,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
changed.push({ thread: updated, snapshot });
|
|
}
|
|
|
|
return changed;
|
|
}),
|
|
|
|
remapOpenThreadsForCaseDocument: async (input: {
|
|
caseId: string;
|
|
key: string;
|
|
documentId: string;
|
|
nextRevisionId: string | null;
|
|
nextRevisionNumber: number;
|
|
nextBody: string;
|
|
}) => db.transaction(async (tx) => {
|
|
const threads: DocumentAnnotationThread[] = await tx
|
|
.select(threadSelect)
|
|
.from(documentAnnotationThreads)
|
|
.where(and(
|
|
eq(documentAnnotationThreads.caseId, input.caseId),
|
|
eq(documentAnnotationThreads.documentId, input.documentId),
|
|
eq(documentAnnotationThreads.status, "open"),
|
|
));
|
|
const changed = [];
|
|
const now = new Date();
|
|
|
|
for (const thread of threads) {
|
|
if (thread.currentRevisionId === input.nextRevisionId) continue;
|
|
const previousAnchor = snapshotFromThread(thread);
|
|
const remap = remapDocumentAnchor({
|
|
previousAnchor,
|
|
nextMarkdown: input.nextBody,
|
|
});
|
|
const nextAnchor = remap.anchor;
|
|
const nextSelector = nextAnchor ? anchorSnapshotToSelector(nextAnchor) : thread.anchorSelector;
|
|
const [updated] = await tx
|
|
.update(documentAnnotationThreads)
|
|
.set({
|
|
currentRevisionId: input.nextRevisionId,
|
|
currentRevisionNumber: input.nextRevisionNumber,
|
|
anchorState: remap.anchorState,
|
|
anchorConfidence: remap.confidence,
|
|
...(nextAnchor
|
|
? {
|
|
selectedText: nextAnchor.selectedText,
|
|
prefixText: nextAnchor.prefixText,
|
|
suffixText: nextAnchor.suffixText,
|
|
normalizedStart: nextAnchor.normalizedStart,
|
|
normalizedEnd: nextAnchor.normalizedEnd,
|
|
markdownStart: nextAnchor.markdownStart,
|
|
markdownEnd: nextAnchor.markdownEnd,
|
|
}
|
|
: {}),
|
|
anchorSelector: nextSelector,
|
|
updatedAt: now,
|
|
})
|
|
.where(eq(documentAnnotationThreads.id, thread.id))
|
|
.returning(threadSelect);
|
|
const [snapshot] = await tx
|
|
.insert(documentAnnotationAnchorSnapshots)
|
|
.values({
|
|
companyId: thread.companyId,
|
|
threadId: thread.id,
|
|
documentId: thread.documentId,
|
|
fromRevisionId: thread.currentRevisionId,
|
|
fromRevisionNumber: thread.currentRevisionNumber,
|
|
toRevisionId: input.nextRevisionId,
|
|
toRevisionNumber: input.nextRevisionNumber,
|
|
previousAnchor,
|
|
nextAnchor,
|
|
anchorState: remap.anchorState,
|
|
anchorConfidence: remap.confidence,
|
|
failureReason: remap.anchor ? null : remap.reason,
|
|
createdAt: now,
|
|
})
|
|
.returning();
|
|
changed.push({ thread: updated, snapshot });
|
|
}
|
|
|
|
return changed;
|
|
}),
|
|
|
|
selectorToAnchorSnapshot,
|
|
};
|
|
}
|