Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

code improvements

Pedro Ladeira3e517734dc33
diff --git a/apps/web/lib/ai/review-reward-tooltip.ts b/apps/web/lib/ai/review-reward-tooltip.ts
index 2dd205bb25..a1572406f3 100644
--- a/apps/web/lib/ai/review-reward-tooltip.ts
+++ b/apps/web/lib/ai/review-reward-tooltip.ts
@@ -13,6 +13,7 @@ import { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import {
CONDITION_OPERATOR_LABELS,
+ isOneOffRewardEvent,
REWARD_CONDITIONS,
} from "@/lib/zod/schemas/rewards";
import { anthropic } from "@ai-sdk/anthropic";
@@ -35,6 +36,9 @@ export async function screenRewardTooltipContradiction(
): Promise<{ flagged: boolean | null }> {
try {
const authorized = await authorizeRewardTooltipReview(input);
+ if (authorized === "ai_usage_exceeded") {
+ return { flagged: null };
+ }
if (!authorized) {
return { flagged: false };
}
@@ -117,7 +121,7 @@ export async function reviewRewardTooltipConsistency(input: unknown): Promise<{
try {
const authorized = await authorizeRewardTooltipReview(input);
- if (!authorized) {
+ if (!authorized || authorized === "ai_usage_exceeded") {
return empty;
}
@@ -162,6 +166,7 @@ Use modifierIndex and conditionIndex from conditionGroups when a condition shoul
suggestions: parsedOutput.data.suggestions,
});
const payoutFixes = filterPayoutFixes({
+ event,
basePayout,
modifiers,
fixes: parsedOutput.data.payoutFixes,
@@ -213,7 +218,7 @@ async function authorizeRewardTooltipReview(input: unknown) {
});
if (workspace.aiUsage >= workspace.aiLimit) {
- return null;
+ return "ai_usage_exceeded";
}
return {
@@ -224,11 +229,13 @@ async function authorizeRewardTooltipReview(input: unknown) {
}
function buildSystemPrompt(event: string) {
+ const oneOff = event === "click" || event === "lead";
+
return `You review whether partner-facing reward copy contradicts the structured reward.
Event type: ${event}
-The reward has partner copy (shown-as text and tooltip), a default payout, and condition groups. Each group pays its own amount and duration when its conditions match. The default payout applies when no group matches. Amounts are dollars for flat payouts and percents for percentage payouts. Duration is "one time", "for N months", or "for the customer's lifetime".
+The reward has partner copy (shown-as text and tooltip), a default payout, and condition groups. Each group pays its own amount${oneOff ? "" : " and duration"} when its conditions match. The default payout applies when no group matches. Amounts are dollars for flat payouts and percents for percentage payouts. ${oneOff ? "Click and lead rewards have no duration. Do not return maxDuration. Ignore duration phrases in the copy." : `Duration is "one time", "for N months", or "for the customer's lifetime".`}
Rules:
- Compare meaning, not wording.
@@ -236,10 +243,9 @@ Rules:
- Do not flag extra conditions the copy never mentioned.
- Do not invent new condition rows. Do not change entity or attribute.
- A suggestion may change operator and/or value only.
-- When the copy states an amount or duration that a payout does not match, return a payoutFix for each payout that disagrees. Set amount and/or maxDuration to what the copy says. Omit a field that already matches.
+- When the copy states an amount${oneOff ? "" : " or duration"} that a payout does not match, return a payoutFix for each payout that disagrees. Set amount${oneOff ? "" : " and/or maxDuration"} to what the copy says. Omit a field that already matches.
- "no money", "$0", "free", or "earn nothing" means amount 0.
-- "no month", "one time", or "not monthly" means maxDuration 0.
-- "every month" or "monthly" means maxDuration 1. "every N months" means maxDuration N. "lifetime" means maxDuration null.
+${oneOff ? "" : `- "no month", "one time", or "not monthly" means maxDuration 0.\n- "every month" or "monthly" means maxDuration 1. "every N months" means maxDuration N. "lifetime" means maxDuration null.`}
- If you return a condition suggestion or a payoutFix, set note to null.
- Use note only when the copy disagrees and you cannot name the replacement amount, duration, operator, or threshold.
- If you are not sure, return suggestions: [], payoutFixes: [], and note: null. Never guess.
@@ -257,9 +263,13 @@ Condition contradictions:
- Copy says "more than $20" / "over $20" but the condition is "is greater than or equal to" $20.
Payout fixes, not a note:
-- Copy says "earn no money" and "no month", but the default payout is $10 one time and a condition group pays $30 one time. Return two payoutFixes with amount 0. Omit maxDuration because it is already one time.
+${
+ oneOff
+ ? `- Copy says "earn $300" but the payout is a flat $10. Return amount 300.`
+ : `- Copy says "earn no money" and "no month", but the default payout is $10 one time and a condition group pays $30 one time. Return two payoutFixes with amount 0. Omit maxDuration because it is already one time.
- Copy says "every 3 months" but the payout duration is "one time". Return maxDuration 3 and omit amount if it already matches.
-- Copy says "earn $300" but the payout is a flat $10. Return amount 300 and omit maxDuration if it already matches.
+- Copy says "earn $300" but the payout is a flat $10. Return amount 300 and omit maxDuration if it already matches.`
+}
Not contradictions:
- Copy is shorter than the config or omits extra filters (country, product, metadata).
@@ -288,7 +298,7 @@ function describeReward({
tooltip,
},
defaultPayout: {
- pays: formatPayout(basePayout),
+ pays: formatPayout(basePayout, event),
appliesWhen: "no condition group matches",
},
conditionGroups: modifiers.map((modifier, modifierIndex) => ({
@@ -301,16 +311,19 @@ function describeReward({
conditionIndex,
rule: formatCondition(event, condition),
})),
- thenPays: formatPayout(modifier.payout),
+ thenPays: formatPayout(modifier.payout, event),
})),
};
}
-function formatPayout(payout: RewardPayout) {
+function formatPayout(payout: RewardPayout, event: EventType) {
const amount =
payout.type === "percentage"
? `${payout.amount ?? 0}% of the sale`
: `$${payout.amount ?? 0} flat`;
+
+ if (isOneOffRewardEvent(event)) return amount;
+
const duration =
payout.maxDuration == null
? "for the customer's lifetime"
diff --git a/apps/web/lib/rewards/validate-tooltip-suggestion.ts b/apps/web/lib/rewards/validate-tooltip-suggestion.ts
index 793115ae9a..ad1e730f17 100644
--- a/apps/web/lib/rewards/validate-tooltip-suggestion.ts
+++ b/apps/web/lib/rewards/validate-tooltip-suggestion.ts
@@ -10,6 +10,7 @@ import {
CONDITION_OPERATORS,
DATE_CONDITION_OPERATORS,
ENUM_CONDITION_OPERATORS,
+ isOneOffRewardEvent,
METADATA_CONDITION_OPERATORS,
METADATA_NUMBER_CONDITION_OPERATORS,
NUMBER_CONDITION_OPERATORS,
@@ -180,10 +181,12 @@ export function filterValidatedTooltipSuggestions({
}
export function filterPayoutFixes({
+ event,
basePayout,
modifiers,
fixes,
}: {
+ event?: EventType;
basePayout: RewardPayout;
modifiers: ReviewRewardTooltipModifier[];
fixes: PayoutFix[];
@@ -213,12 +216,14 @@ export function filterPayoutFixes({
const maxAmount = current.type === "percentage" ? 100 : 999_999.99;
if (typeof fix.amount === "number" && fix.amount > maxAmount) continue;
+ const fixDuration =
+ event && isOneOffRewardEvent(event) ? undefined : fix.maxDuration;
const nextDuration =
- fix.maxDuration === undefined ? current.maxDuration : fix.maxDuration;
+ fixDuration === undefined ? current.maxDuration : fixDuration;
const amountChanges =
typeof fix.amount === "number" && fix.amount !== current.amount;
const durationChanges =
- fix.maxDuration !== undefined && nextDuration !== current.maxDuration;
+ fixDuration !== undefined && nextDuration !== current.maxDuration;
if (!amountChanges && !durationChanges) continue;
diff --git a/apps/web/tests/rewards/validate-tooltip-suggestion.test.ts b/apps/web/tests/rewards/validate-tooltip-suggestion.test.ts
index 98c15eff06..2a1be99174 100644
--- a/apps/web/tests/rewards/validate-tooltip-suggestion.test.ts
+++ b/apps/web/tests/rewards/validate-tooltip-suggestion.test.ts
@@ -189,6 +189,17 @@ describe("filterPayoutFixes", () => {
).toHaveLength(1);
});
+ test("drops a duration-only fix for a click reward", () => {
+ expect(
+ filterPayoutFixes({
+ event: "click",
+ basePayout: flatBase,
+ modifiers: [],
+ fixes: [{ ...fix, amount: undefined, maxDuration: 3 }],
+ }),
+ ).toEqual([]);
+ });
+
test.each<[string, PayoutFix]>([
["a no-op", { ...fix, amount: 10 }],
["a flat amount over the save limit", { ...fix, amount: 1_000_000 }],
diff --git a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
index 370b207157..9a48fe6025 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -419,7 +419,7 @@ function RewardSheetContent({
);
}
- if (fix.maxDuration !== undefined) {
+ if (selectedEvent === "sale" && fix.maxDuration !== undefined) {
setValue(
"maxDuration",
fix.maxDuration === null ? Infinity : fix.maxDuration,
@@ -446,7 +446,7 @@ function RewardSheetContent({
);
}
- if (fix.maxDuration !== undefined) {
+ if (selectedEvent === "sale" && fix.maxDuration !== undefined) {
setValue(
`${modifierKey}.maxDuration`,
fix.maxDuration === null ? Infinity : fix.maxDuration,
diff --git a/apps/web/ui/partners/rewards/suggested-fix-popover.tsx b/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
index 117fef87be..68e0efbfd1 100644
--- a/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
+++ b/apps/web/ui/partners/rewards/suggested-fix-popover.tsx
@@ -435,10 +435,15 @@ function PayoutFixPreview({ fixes }: { fixes: PayoutFix[] }) {
<PreviewChip changed={typeof fix.amount === "number"}>
{amountLabel}
</PreviewChip>{" "}
- per {event}{" "}
- <PreviewChip changed={fix.maxDuration !== undefined}>
- {durationLabel}
- </PreviewChip>
+ per {event}
+ {event === "sale" && (
+ <>
+ {" "}
+ <PreviewChip changed={fix.maxDuration !== undefined}>
+ {durationLabel}
+ </PreviewChip>
+ </>
+ )}
</p>
);
})}