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.

pass the user's role into the prompt

Pedro Ladeira47d5070c4c37
diff --git a/apps/web/app/api/ai/support-chat/route.ts b/apps/web/app/api/ai/support-chat/route.ts
index 69c36c8c4d..e043f39dca 100644
--- a/apps/web/app/api/ai/support-chat/route.ts
+++ b/apps/web/app/api/ai/support-chat/route.ts
@@ -13,6 +13,7 @@ import { prisma } from "@/lib/prisma";
import { getSlackClient } from "@/lib/slack/client";
import { ratelimit } from "@/lib/upstash/ratelimit";
import { anthropic } from "@ai-sdk/anthropic";
+import { PartnerRole } from "@prisma/client";
import {
convertToModelMessages,
isFileUIPart,
@@ -191,6 +192,7 @@ export const POST = withSession(async ({ req, session }) => {
}
let partnerCountry: string | null | undefined;
+ let partnerRole: PartnerRole | null | undefined;
if (
globalContext?.accountType === "partner" &&
@@ -202,10 +204,20 @@ export const POST = withSession(async ({ req, session }) => {
},
select: {
country: true,
+ users: {
+ where: {
+ userId: session.user.id,
+ },
+ select: {
+ role: true,
+ },
+ take: 1,
+ },
},
});
partnerCountry = partner?.country;
+ partnerRole = partner?.users[0]?.role;
}
const result = streamText({
@@ -213,6 +225,7 @@ export const POST = withSession(async ({ req, session }) => {
system: buildSystemPrompt({
...globalContext,
partnerCountry,
+ partnerRole,
}),
messages: await convertToModelMessages(modelMessages),
stopWhen: stepCountIs(5),
diff --git a/apps/web/lib/ai/build-system-prompt.ts b/apps/web/lib/ai/build-system-prompt.ts
index 154d9f4c43..024e2f60d2 100644
--- a/apps/web/lib/ai/build-system-prompt.ts
+++ b/apps/web/lib/ai/build-system-prompt.ts
@@ -1,6 +1,7 @@
+import { hasPermission } from "@/lib/auth/partner-users/partner-user-permissions";
import { SupportChatContext } from "@/ui/support/types";
import { COUNTRIES } from "@dub/utils";
-import { Program, Project } from "@prisma/client";
+import { PartnerRole, Program, Project } from "@prisma/client";
export type GlobalChatContext = {
chatLocation?: SupportChatContext;
@@ -8,6 +9,7 @@ export type GlobalChatContext = {
selectedWorkspace?: Pick<Project, "id" | "name" | "slug">;
selectedProgram?: Pick<Program, "id" | "name" | "slug">;
partnerCountry?: string | null;
+ partnerRole?: PartnerRole | null;
};
const CONTEXT_SYSTEM_PROMPTS: Record<SupportChatContext, string> = {
@@ -56,7 +58,26 @@ const PARTNERS_PAYOUT_PROMPT = `
- Offer to create a Dub support ticket (call requestSupportTicket, then createSupportTicket once the user confirms) so Dub can investigate further.
`.trim();
-function buildPartnerProfileCountryPrompt(partnerCountry?: string | null) {
+function buildPartnerProfileCountryPrompt({
+ partnerCountry,
+ partnerRole,
+}: Pick<GlobalChatContext, "partnerCountry" | "partnerRole">) {
+ const canUpdateCountry =
+ partnerCountry === "US" &&
+ !!partnerRole &&
+ hasPermission(partnerRole, "partner_profile.update");
+
+ let countryChangeInstruction =
+ "They cannot change it themselves. Tell them their current country and that support has to update it. In that same response, call requestSupportTicket so the upload form appears. Do not only ask whether they want a ticket. After they confirm, call createSupportTicket.";
+
+ if (canUpdateCountry) {
+ countryChangeInstruction =
+ "They can update it themselves in partner profile settings at https://partners.dub.co/profile. Tell them to change it there. Do not create a support ticket.";
+ } else if (partnerCountry === "US") {
+ countryChangeInstruction =
+ "They do not have permission to update the partner profile. An owner of this partner account can change the country in partner profile settings at https://partners.dub.co/profile. Tell them to ask an owner. Do not create a support ticket.";
+ }
+
return `
Partner profile country:
The partner's profile country is based on their current location at signup.
@@ -65,11 +86,8 @@ function buildPartnerProfileCountryPrompt(partnerCountry?: string | null) {
? `${COUNTRIES[partnerCountry] ?? partnerCountry} (${partnerCountry})`
: "unknown"
}.
- ${
- partnerCountry === "US"
- ? "They can update it themselves in partner profile settings at https://partners.dub.co/profile. Tell them to change it there. Do not create a support ticket."
- : "They cannot change it themselves. Tell them their current country and that support has to update it. In that same response, call requestSupportTicket so the upload form appears. Do not only ask whether they want a ticket. After they confirm, call createSupportTicket."
- }
+ The current user's role on this partner account is ${partnerRole ?? "unknown"}.
+ ${countryChangeInstruction}
Do not confuse this with which countries support payouts — that is a different question and can still use docs.
`.trim();
}
@@ -120,7 +138,7 @@ export function buildSystemPrompt(globalContext?: GlobalChatContext): string {
NONPROFIT_DISCOUNT_PROMPT,
globalContext?.accountType === "partner" ? PARTNERS_PAYOUT_PROMPT : null,
globalContext?.accountType === "partner"
- ? buildPartnerProfileCountryPrompt(globalContext.partnerCountry)
+ ? buildPartnerProfileCountryPrompt(globalContext)
: null,
...accountSpecificPrompts,
].filter((section): section is string => Boolean(section));