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),