Skip to content

Commit

Permalink
feat: support different cachedId
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 15, 2023
1 parent 6f6a94c commit da20fd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
10 changes: 3 additions & 7 deletions lib/openai/fetchOpenAIResult.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Redis } from "@upstash/redis";
import {
createParser,
ParsedEvent,
ReconnectInterval,
} from "eventsource-parser";
import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser";
import { trimOpenAiResult } from "~/lib/openai/trimOpenAiResult";
import { VideoConfig } from "~/lib/types";
import { isDev } from "~/utils/env";
import { getCacheId } from "~/utils/getCacheId";

export enum ChatGPTAgent {
user = "user",
Expand Down Expand Up @@ -54,9 +51,8 @@ export async function fetchOpenAIResult(
throw new Error(`OpenAI API Error [${res.statusText}]: ${errorJson.error?.message}`);
}

const { showTimestamp, videoId } = videoConfig;
const redis = Redis.fromEnv();
const cacheId = showTimestamp ? `timestamp-${videoId}` : videoId;
const cacheId = getCacheId(videoConfig);

if (!payload.stream) {
const result = await res.json();
Expand Down
12 changes: 4 additions & 8 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { Redis } from "@upstash/redis";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { SummarizeParams } from "~/lib/types";
import { getCacheId } from "~/utils/getCacheId";
import { validateLicenseKey } from "./lib/lemon";
import { checkOpenaiApiKeys } from "./lib/openai/checkOpenaiApiKey";
import {
ratelimitForApiKeyIps,
ratelimitForFreeAccounts,
ratelimitForIps,
} from "./lib/upstash";
import { ratelimitForApiKeyIps, ratelimitForFreeAccounts, ratelimitForIps } from "./lib/upstash";
import { isDev } from "./utils/env";

const redis = Redis.fromEnv();
Expand All @@ -32,9 +29,8 @@ export async function middleware(req: NextRequest, context: NextFetchEvent) {
try {
const { userConfig, videoConfig } = (await req.json()) as SummarizeParams;
// TODO: update shouldShowTimestamp to use videoConfig
const { userKey, shouldShowTimestamp } = userConfig || {};
const { videoId } = videoConfig || {};
const cacheId = shouldShowTimestamp ? `timestamp-${videoId}` : videoId;
const { userKey } = userConfig || {};
const cacheId = getCacheId(videoConfig);
const ipIdentifier = req.ip ?? "127.0.0.11";

// licenseKeys
Expand Down
11 changes: 11 additions & 0 deletions utils/getCacheId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { VideoConfig } from "~/lib/types";

export function getCacheId({
showTimestamp,
videoId,
detailLevel,
}: VideoConfig) {
return showTimestamp
? `timestamp-${videoId}-${detailLevel}`
: `${videoId}-${detailLevel}`;
}

1 comment on commit da20fd4

@vercel
Copy link

@vercel vercel bot commented on da20fd4 Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.