feat: add OpenRouter audit generation pipeline

This commit is contained in:
2026-06-05 11:06:01 +02:00
parent 370aeec2a0
commit 03cb65fde4
29 changed files with 5462 additions and 74 deletions

View File

@@ -1,7 +1,7 @@
import { v } from "convex/values";
import { normalizeListLimit } from "./domain";
import { mutation, query } from "./_generated/server";
import { internalMutation, mutation, query } from "./_generated/server";
const strategy = v.union(
v.literal("call_first"),
@@ -35,6 +35,59 @@ export const create = mutation({
},
});
export const upsertFromAuditGeneration = internalMutation({
args: {
leadId: v.id("leads"),
auditId: v.optional(v.id("audits")),
strategy: strategy,
phoneScript: v.optional(v.string()),
emailSubject: v.optional(v.string()),
emailBody: v.optional(v.string()),
followUpDraft: v.optional(v.string()),
},
handler: async (ctx, args) => {
const now = Date.now();
const existing = await ctx.db
.query("outreachRecords")
.withIndex("by_leadId", (q) => q.eq("leadId", args.leadId))
.order("desc")
.take(1);
if (existing.length > 0) {
const current = existing[0]!;
if (args.auditId) {
await ctx.db.patch(current._id, { auditId: args.auditId });
}
await ctx.db.patch(current._id, {
strategy: args.strategy,
...(args.phoneScript !== undefined ? { phoneScript: args.phoneScript } : {}),
...(args.emailSubject !== undefined
? { emailSubject: args.emailSubject }
: {}),
...(args.emailBody !== undefined ? { emailBody: args.emailBody } : {}),
...(args.followUpDraft !== undefined
? { followUpDraft: args.followUpDraft }
: {}),
updatedAt: now,
});
return current._id;
}
return await ctx.db.insert("outreachRecords", {
...args,
approvalStatus: "draft",
sendStatus: "not_sent",
responseStatus: "none",
salesStatus: "follow_up_planned",
createdAt: now,
updatedAt: now,
});
},
});
export const list = query({
args: {
leadId: v.optional(v.id("leads")),