import { z } from "zod";
import { defineContract } from "@withboundary/contract";
export const emailSchema = z.object({
sender: z.string().email(),
subject: z.string(),
intent: z.enum([
"general_inquiry",
"billing",
"urgent_support",
"feature_request",
"cancellation",
]),
priority: z.enum(["low", "medium", "high", "critical"]),
department: z.enum(["sales", "support", "engineering", "billing", "product"]),
summary: z.string(),
});
export const emailContract = defineContract({
name: "email-parsing",
schema: emailSchema,
rules: [
{
name: "urgent_requires_high_priority",
description: "Urgent intents must be high or critical priority",
fields: ["intent", "priority"],
check: (email) =>
!email.intent.includes("urgent") || ["high", "critical"].includes(email.priority)
|| `intent is "${email.intent}" but priority is "${email.priority}"; use high or critical`,
},
{
name: "support_routed_to_support",
description: "Support intents route to support or engineering",
fields: ["intent", "department"],
check: (email) =>
!email.intent.includes("support") || ["support", "engineering"].includes(email.department)
|| `support intent routed to "${email.department}"; use support or engineering`,
},
{
name: "cancellation_routed_correctly",
description: "Cancellation requests route to billing or support",
fields: ["intent", "department"],
check: (email) =>
email.intent !== "cancellation" || ["billing", "support"].includes(email.department)
|| `cancellation routed to "${email.department}"; use billing or support`,
},
{
name: "summary_substantive",
description: "Summary has enough context for the receiving team",
fields: ["summary"],
check: (email) =>
email.summary.trim().length >= 20
|| "summary must include enough context for the receiving team",
},
],
});