File size: 1,477 Bytes
846ea5b
 
58379d0
 
 
99c03ec
903bee0
846ea5b
3e3029e
5443f48
58379d0
 
 
93ad82e
846ea5b
 
99c03ec
63ac902
846ea5b
 
99c03ec
63ac902
846ea5b
58379d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
846ea5b
5443f48
58132fb
99c03ec
 
58132fb
63ac902
 
846ea5b
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use server"


import { Ratelimit } from "@upstash/ratelimit"
import { Redis } from "@upstash/redis"
import { ClapProject, ClapImageRatio } from "@aitube/clap"
import { createClap as apiCreateClap } from "@aitube/client"

import { getToken } from "./getToken"
import { RESOLUTION_LONG, RESOLUTION_SHORT, MAX_PROMPT_LENGTH_IN_CHARS } from "../config"
import { getRateLimit } from "../redis/getRateLimit"

const rateLimit = getRateLimit()

export async function createClap({
  prompt = "",
  imageRatio = ClapImageRatio.PORTRAIT,
  turbo = false,
}: {
  prompt: string
  imageRatio?: ClapImageRatio
  turbo?: boolean
}): Promise<ClapProject> {

  // TODO: use 

  /*
  if (process.env.ENABLE_RATE_LIMIT) {
    const user = "anon"
    const userRateLimitResult = await rateLimit.limit(user)

    // result.limit
    if (!userRateLimitResult.success) {
      console.log(`blocking user ${user} who requested "${prompt}${prompt.length > 60 ? "..." : ""}"`)
      throw new Error(`Rate Limit Reached`)
    } else {
      console.log(`allowing user ${user}: who requested "${prompt}${prompt.length >630 ? "..." : ""}"`)
    }
  }
  */

  const clap: ClapProject = await apiCreateClap({
    prompt: prompt.slice(0, MAX_PROMPT_LENGTH_IN_CHARS),

    height: imageRatio === ClapImageRatio.PORTRAIT ? RESOLUTION_LONG : RESOLUTION_SHORT,
    width: imageRatio === ClapImageRatio.PORTRAIT ? RESOLUTION_SHORT : RESOLUTION_LONG,

    turbo,

    token: await getToken()
  })

  return clap
}