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.

Merge branch 'main' into update-workspaceId

Kiran K5b48f140a8b1
diff --cc apps/web/app/api/ai/completion/route.ts
index 859ae16aa0,45eef2d21a..4ddcfa7b18
--- a/apps/web/app/api/ai/completion/route.ts
+++ b/apps/web/app/api/ai/completion/route.ts
@@@ -1,6 -1,6 +1,7 @@@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+ import { throwIfAIUsageExceeded } from "@/lib/api/links/usage-checks";
+import { normalizeWorkspaceId } from "@/lib/api/workspace-id";
- import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
+ import { withWorkspace } from "@/lib/auth";
import z from "@/lib/zod";
import { anthropic } from "@ai-sdk/anthropic";
import { prismaEdge } from "@dub/prisma/edge";
@@@ -18,45 -16,42 +17,43 @@@ const completionSchema = z.object(
});
// POST /api/ai/completion – Generate AI completion
- export const POST = withWorkspaceEdge(
- async ({ req, workspace }) => {
- try {
- const {
- // comment for better diff
- prompt,
- model,
- } = completionSchema.parse(await req.json());
+ export const POST = withWorkspace(async ({ req, workspace }) => {
+ try {
+ const {
+ // comment for better diff
+ prompt,
+ model,
+ } = completionSchema.parse(await req.json());
- const result = streamText({
- model: anthropic(model),
- messages: [
- {
- role: "user",
- content: prompt,
- },
- ],
- maxTokens: 300,
- });
- // only count usage for the sonnet model
- if (model === "claude-3-5-sonnet-latest") {
- waitUntil(
- prismaEdge.project.update({
- where: {
- id: normalizeWorkspaceId(workspace.id),
- },
- data: {
- aiUsage: {
- increment: 1,
- },
- },
- }),
- );
- }
+ throwIfAIUsageExceeded(workspace);
+
+ const result = streamText({
+ model: anthropic(model),
+ messages: [
+ {
+ role: "user",
+ content: prompt,
+ },
+ ],
+ maxTokens: 300,
+ });
+
- return result.toDataStreamResponse();
- } catch (error) {
- return handleAndReturnErrorResponse(error);
+ // only count usage for the sonnet model
+ if (model === "claude-3-5-sonnet-latest") {
+ waitUntil(
+ prismaEdge.project.update({
- where: { id: workspace.id.replace("ws_", "") },
++ where: { id: normalizeWorkspaceId(workspace.id) },
+ data: {
+ aiUsage: {
+ increment: 1,
+ },
+ },
+ }),
+ );
}
- },
- { needNotExceededAI: true },
- );
+
+ return result.toDataStreamResponse();
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+ });