The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
feat: fix ai key generation
flooorianflo351a36e6b2bf
diff --git a/apps/web/app/api/ai/shortlink/route.ts b/apps/web/app/api/ai/shortlink/route.tsindex ffdbf6ce2e..4257ae1fa6 100644--- a/apps/web/app/api/ai/shortlink/route.ts+++ b/apps/web/app/api/ai/shortlink/route.ts@@ -14,7 +14,7 @@ export async function POST(req: NextRequest) {const searchParams = getSearchParams(req.url);const { workspaceId } = searchParams;- const { url, domain } = await req.json();+ const { metaTitle, metaDescription, doNotUseKey } = await req.json();const workspace = await getWorkspaceViaEdge(workspaceId);@@ -32,11 +32,11 @@ export async function POST(req: NextRequest) {{role: "system",content:- "You are a helpful assistant and only answer in short link keys, e.g. you receive a question like 'What is the shortlink for https://dub.dev/docs on dub.dev?' and you respond with 'docs'. If there are paths in the URL, you should ignore them and try to combine them in a shortlink that makes sense and is easy to share on social media.",+ "You are a helpful assistant and only answer in short link keys, e.g. you receive a question like 'What is the shortlink for meta-title Notion and meta-description The all in one workspace?' and you respond with e.g. 'notion-workspace'. Try to combine them in a shortlink that makes sense and is easy to share on social media. Don't use any special characters or spaces.",},{role: "user",- content: `What is the shortlink for ${url} on ${domain}?`,+ content: `What is the shortlink for meta-title ${metaTitle} and meta-description ${metaDescription}?${doNotUseKey ? ` Don't use ${doNotUseKey}` : ""}`,},],});diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsxindex 3ea96eb98a..05720794ce 100644--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx@@ -121,29 +121,35 @@ function AddEditLinkModal({setKeyError(null);setGeneratingAIKey(true);- const res = await fetch(`/api/ai/shortlink?workspaceId=${workspaceId}`, {- method: "POST",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({- url,- domain,- }),- }).then(async (res) => {- if (res.status !== 200) {- const error = await res.text();- setKeyError(error);- setGeneratingAIKey(false);- throw new Error(error);- }- return res.text();- });+ const returnKey = async (doNotUseKey?) =>+ await fetch(`/api/ai/shortlink?workspaceId=${workspaceId}`, {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ metaTitle: data.title,+ metaDescription: data.description,+ doNotUseKey,+ }),+ }).then(async (res) => {+ if (res.status !== 200) {+ const error = await res.text();+ setKeyError(error);+ setGeneratingAIKey(false);+ throw new Error(error);+ }+ return res.text();+ });- setData((prev) => ({ ...prev, key: res }));+ let key = await returnKey();++ if (key === data.key) key = await returnKey(key);++ setData((prev) => ({ ...prev, key }));setGeneratingAIKey(false);- }, [url, domain, slug]);+ }, [data, workspaceId, slug]);useEffect(() => {// when someone pastes a URL