Add URL-based change review and selective change-set rollback

This commit is contained in:
Ruslan Bakiev
2026-02-20 19:04:49 +07:00
parent b9ba5778f5
commit 129daa31d7
5 changed files with 593 additions and 88 deletions

View File

@@ -5,7 +5,7 @@ import { clearAuthSession, setSession } from "../utils/auth";
import { prisma } from "../utils/prisma";
import { normalizePhone, verifyPassword } from "../utils/password";
import { persistChatMessage, runCrmAgentFor } from "../agent/crmAgent";
import { buildChangeSet, captureSnapshot, rollbackChangeSet } from "../utils/changeSet";
import { buildChangeSet, captureSnapshot, rollbackChangeSet, rollbackChangeSetItems } from "../utils/changeSet";
import type { ChangeSet } from "../utils/changeSet";
type GraphQLContext = {
@@ -275,12 +275,17 @@ async function getChatMessages(auth: AuthContext | null) {
changeStatus: cs?.status ?? null,
changeSummary: cs?.summary ?? null,
changeItems: Array.isArray(cs?.items)
? cs.items.map((item) => ({
? cs.items.map((item, idx) => ({
id: String((item as any)?.id ?? `legacy-${idx}`),
entity: String(item.entity ?? ""),
entityId: (item as any)?.entityId ? String((item as any).entityId) : null,
action: String(item.action ?? ""),
title: String(item.title ?? ""),
before: String(item.before ?? ""),
after: String(item.after ?? ""),
rolledBack: Array.isArray((cs as any)?.rolledBackItemIds)
? (cs as any).rolledBackItemIds.includes((item as any)?.id)
: false,
}))
: [],
createdAt: m.createdAt.toISOString(),
@@ -685,6 +690,31 @@ async function findLatestChangeCarrierMessage(auth: AuthContext | null) {
return null;
}
async function findChangeCarrierMessageByChangeSetId(auth: AuthContext | null, changeSetId: string) {
const ctx = requireAuth(auth);
const targetId = String(changeSetId ?? "").trim();
if (!targetId) return null;
const items = await prisma.chatMessage.findMany({
where: {
teamId: ctx.teamId,
conversationId: ctx.conversationId,
role: "ASSISTANT",
},
orderBy: { createdAt: "desc" },
take: 200,
});
for (const item of items) {
const changeSet = getChangeSetFromPlanJson(item.planJson);
if (!changeSet) continue;
if (changeSet.id !== targetId) continue;
return { item, changeSet };
}
return null;
}
async function confirmLatestChangeSet(auth: AuthContext | null) {
const found = await findLatestChangeCarrierMessage(auth);
if (!found) return { ok: true };
@@ -725,6 +755,54 @@ async function rollbackLatestChangeSet(auth: AuthContext | null) {
...changeSet,
status: "rolled_back",
rolledBackAt: new Date().toISOString(),
rolledBackItemIds: Array.isArray(changeSet.items)
? changeSet.items
.map((changeItem: any, idx: number) => String(changeItem?.id ?? `legacy-${idx}`))
.filter(Boolean)
: [],
},
};
await prisma.chatMessage.update({
where: { id: item.id },
data: { planJson: next as any },
});
return { ok: true };
}
async function rollbackChangeSetItemsMutation(auth: AuthContext | null, changeSetId: string, itemIds: string[]) {
const ctx = requireAuth(auth);
const found = await findChangeCarrierMessageByChangeSetId(ctx, changeSetId);
if (!found) return { ok: true };
const { item, changeSet } = found;
if (changeSet.status === "rolled_back") return { ok: true };
const selectedIds = [...new Set((itemIds ?? []).map((id) => String(id ?? "").trim()).filter(Boolean))];
if (!selectedIds.length) return { ok: true };
await rollbackChangeSetItems(prisma, ctx.teamId, changeSet, selectedIds);
const allIds = Array.isArray(changeSet.items)
? changeSet.items
.map((changeItem: any, idx: number) => String(changeItem?.id ?? `legacy-${idx}`))
.filter(Boolean)
: [];
const prevRolledBack = Array.isArray((changeSet as any)?.rolledBackItemIds)
? ((changeSet as any).rolledBackItemIds as string[]).map((id) => String(id))
: [];
const nextRolledBackSet = new Set([...prevRolledBack, ...selectedIds]);
const nextRolledBack = [...nextRolledBackSet];
const allRolledBack = allIds.length > 0 && allIds.every((id) => nextRolledBackSet.has(id));
const next = {
...((item.planJson as any) ?? {}),
changeSet: {
...changeSet,
status: allRolledBack ? "rolled_back" : "pending",
rolledBackAt: allRolledBack ? new Date().toISOString() : null,
rolledBackItemIds: nextRolledBack,
},
};
@@ -867,6 +945,7 @@ export const crmGraphqlSchema = buildSchema(`
sendPilotMessage(text: String!): MutationResult!
confirmLatestChangeSet: MutationResult!
rollbackLatestChangeSet: MutationResult!
rollbackChangeSetItems(changeSetId: ID!, itemIds: [ID!]!): MutationResult!
logPilotNote(text: String!): MutationResult!
toggleContactPin(contact: String!, text: String!): PinToggleResult!
createCalendarEvent(input: CreateCalendarEventInput!): CalendarEvent!
@@ -963,11 +1042,14 @@ export const crmGraphqlSchema = buildSchema(`
}
type PilotChangeItem {
id: ID!
entity: String!
entityId: String
action: String!
title: String!
before: String!
after: String!
rolledBack: Boolean!
}
type PilotToolRun {
@@ -1116,6 +1198,11 @@ export const crmGraphqlRoot = {
rollbackLatestChangeSet: async (_args: unknown, context: GraphQLContext) =>
rollbackLatestChangeSet(context.auth),
rollbackChangeSetItems: async (
args: { changeSetId: string; itemIds: string[] },
context: GraphQLContext,
) => rollbackChangeSetItemsMutation(context.auth, args.changeSetId, args.itemIds),
logPilotNote: async (args: { text: string }, context: GraphQLContext) =>
logPilotNote(context.auth, args.text),

View File

@@ -49,11 +49,14 @@ export type SnapshotState = {
};
export type ChangeItem = {
id: string;
entity: "calendar_event" | "contact_note" | "message" | "deal";
entityId: string | null;
action: "created" | "updated" | "deleted";
title: string;
before: string;
after: string;
undo: UndoOp[];
};
type UndoOp =
@@ -71,6 +74,7 @@ export type ChangeSet = {
summary: string;
items: ChangeItem[];
undo: UndoOp[];
rolledBackItemIds: string[];
};
function fmt(val: string | null | undefined) {
@@ -190,18 +194,24 @@ export async function captureSnapshot(prisma: PrismaClient, teamId: string): Pro
export function buildChangeSet(before: SnapshotState, after: SnapshotState): ChangeSet | null {
const items: ChangeItem[] = [];
const undo: UndoOp[] = [];
const pushItem = (item: Omit<ChangeItem, "id">) => {
const next: ChangeItem = { ...item, id: randomUUID() };
items.push(next);
undo.push(...next.undo);
};
for (const [id, row] of after.calendarById) {
const prev = before.calendarById.get(id);
if (!prev) {
items.push({
pushItem({
entity: "calendar_event",
entityId: row.id,
action: "created",
title: `Event created: ${row.title}`,
before: "",
after: toCalendarText(row),
undo: [{ kind: "delete_calendar_event", id }],
});
undo.push({ kind: "delete_calendar_event", id });
continue;
}
if (
@@ -214,95 +224,104 @@ export function buildChangeSet(before: SnapshotState, after: SnapshotState): Cha
fmt(prev.archivedAt) !== fmt(row.archivedAt) ||
prev.contactId !== row.contactId
) {
items.push({
pushItem({
entity: "calendar_event",
entityId: row.id,
action: "updated",
title: `Event updated: ${row.title}`,
before: toCalendarText(prev),
after: toCalendarText(row),
undo: [{ kind: "restore_calendar_event", data: prev }],
});
undo.push({ kind: "restore_calendar_event", data: prev });
}
}
for (const [id, row] of before.calendarById) {
if (after.calendarById.has(id)) continue;
items.push({
pushItem({
entity: "calendar_event",
entityId: row.id,
action: "deleted",
title: `Event archived: ${row.title}`,
before: toCalendarText(row),
after: "",
undo: [{ kind: "restore_calendar_event", data: row }],
});
undo.push({ kind: "restore_calendar_event", data: row });
}
for (const [contactId, row] of after.noteByContactId) {
const prev = before.noteByContactId.get(contactId);
if (!prev) {
items.push({
pushItem({
entity: "contact_note",
entityId: contactId,
action: "created",
title: `Summary added: ${row.contactName}`,
before: "",
after: row.content,
undo: [{ kind: "restore_contact_note", contactId, content: null }],
});
undo.push({ kind: "restore_contact_note", contactId, content: null });
continue;
}
if (prev.content !== row.content) {
items.push({
pushItem({
entity: "contact_note",
entityId: contactId,
action: "updated",
title: `Summary updated: ${row.contactName}`,
before: prev.content,
after: row.content,
undo: [{ kind: "restore_contact_note", contactId, content: prev.content }],
});
undo.push({ kind: "restore_contact_note", contactId, content: prev.content });
}
}
for (const [contactId, row] of before.noteByContactId) {
if (after.noteByContactId.has(contactId)) continue;
items.push({
pushItem({
entity: "contact_note",
entityId: contactId,
action: "deleted",
title: `Summary cleared: ${row.contactName}`,
before: row.content,
after: "",
undo: [{ kind: "restore_contact_note", contactId, content: row.content }],
});
undo.push({ kind: "restore_contact_note", contactId, content: row.content });
}
for (const [id, row] of after.messageById) {
if (before.messageById.has(id)) continue;
items.push({
pushItem({
entity: "message",
entityId: row.id,
action: "created",
title: `Message created: ${row.contactName}`,
before: "",
after: toMessageText(row),
undo: [{ kind: "delete_contact_message", id }],
});
undo.push({ kind: "delete_contact_message", id });
}
for (const [id, row] of after.dealById) {
const prev = before.dealById.get(id);
if (!prev) continue;
if (prev.stage !== row.stage || fmt(prev.nextStep) !== fmt(row.nextStep) || fmt(prev.summary) !== fmt(row.summary)) {
items.push({
pushItem({
entity: "deal",
entityId: row.id,
action: "updated",
title: `Deal updated: ${row.title}`,
before: toDealText(prev),
after: toDealText(row),
});
undo.push({
kind: "restore_deal",
id,
stage: prev.stage,
nextStep: prev.nextStep,
summary: prev.summary,
undo: [
{
kind: "restore_deal",
id,
stage: prev.stage,
nextStep: prev.nextStep,
summary: prev.summary,
},
],
});
}
}
@@ -320,11 +339,12 @@ export function buildChangeSet(before: SnapshotState, after: SnapshotState): Cha
summary: `Created: ${created}, Updated: ${updated}, Archived: ${deleted}`,
items,
undo,
rolledBackItemIds: [],
};
}
export async function rollbackChangeSet(prisma: PrismaClient, teamId: string, changeSet: ChangeSet) {
const ops = [...changeSet.undo].reverse();
async function applyUndoOps(prisma: PrismaClient, teamId: string, undoOps: UndoOp[]) {
const ops = [...undoOps].reverse();
await prisma.$transaction(async (tx) => {
for (const op of ops) {
@@ -424,3 +444,31 @@ export async function rollbackChangeSet(prisma: PrismaClient, teamId: string, ch
}
});
}
export async function rollbackChangeSet(prisma: PrismaClient, teamId: string, changeSet: ChangeSet) {
await applyUndoOps(prisma, teamId, changeSet.undo);
}
export async function rollbackChangeSetItems(
prisma: PrismaClient,
teamId: string,
changeSet: ChangeSet,
itemIds: string[],
) {
const wanted = new Set(itemIds.filter(Boolean));
if (!wanted.size) return;
const itemUndoOps = changeSet.items
.filter((item) => wanted.has(item.id))
.flatMap((item) => (Array.isArray(item.undo) ? item.undo : []));
if (itemUndoOps.length > 0) {
await applyUndoOps(prisma, teamId, itemUndoOps);
return;
}
// Legacy fallback for old change sets without per-item undo.
if (wanted.size >= changeSet.items.length && Array.isArray(changeSet.undo) && changeSet.undo.length > 0) {
await applyUndoOps(prisma, teamId, changeSet.undo);
}
}