diff --git a/app/utils/cloud/redisClient.ts b/app/utils/cloud/redisClient.ts deleted file mode 100644 index dfdcd4931..000000000 --- a/app/utils/cloud/redisClient.ts +++ /dev/null @@ -1,34 +0,0 @@ -// redisClient.ts -import Redis from 'ioredis'; - -const redis = new Redis(process.env.UPSTASH_REDIS_URL); - -export const incrementSignInCount = async (email: string | undefined, dateKey: string) => { - if (!email) { - console.error('Email is undefined, cannot increment sign-in count.'); - return; - } - await redis.hincrby(`sign_ins:${email}`, dateKey, 1); -}; - -export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => { - if (!email) { - console.error('Email is undefined, cannot increment session refresh count.'); - return; - } - await redis.hincrby(`session_refreshes:${email}`, dateKey, 1); -}; - -export const incrementTokenCounts = async ( - email: string | undefined, - dateKey: string, - completionTokens: number, - promptTokens: number -) => { - if (!email) { - console.error('Email is undefined, cannot increment token counts.'); - return; - } - await redis.hincrby(`tokens:${email}`, `${dateKey}:completion_tokens`, completionTokens); - await redis.hincrby(`tokens:${email}`, `${dateKey}:prompt_tokens`, promptTokens); -}; diff --git a/app/utils/cloud/redisRestClient.ts b/app/utils/cloud/redisRestClient.ts new file mode 100644 index 000000000..b82544267 --- /dev/null +++ b/app/utils/cloud/redisRestClient.ts @@ -0,0 +1,76 @@ +// redisRestClient.ts +import fetch from 'node-fetch'; // or any other fetch-compatible API + +const redisRestUrl = process.env.UPSTASH_REDIS_URL; +const redisRestToken = process.env.UPSTASH_REDIS_REST_TOKEN; + +if (!redisRestUrl || !redisRestToken) { + console.error('The Upstash Redis URL and token must be provided.'); + // Handle the lack of Redis client here, e.g., by disabling certain features +} + +const headers = { + Authorization: `Bearer ${redisRestToken}`, + 'Content-Type': 'application/json', +}; + +export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => { + if (!email) { + console.error('Email is undefined, cannot increment session refresh count.'); + return; + } + + const response = await fetch(`${redisRestUrl}/hincrby`, { + method: 'POST', + headers: headers, + body: JSON.stringify({ + key: `session_refreshes:${email}`, + field: dateKey, + increment: 1 + }), + }); + + if (!response.ok) { + console.error('Failed to increment session refresh count in Redis via REST API'); + } +}; + +export const incrementTokenCounts = async ( + email: string | undefined, + dateKey: string, + completionTokens: number, + promptTokens: number +) => { + if (!email) { + console.error('Email is undefined, cannot increment token counts.'); + return; + } + + const incrementCompletionTokensResponse = await fetch(`${redisRestUrl}/hincrby`, { + method: 'POST', + headers: headers, + body: JSON.stringify({ + key: `tokens:${email}`, + field: `${dateKey}:completion_tokens`, + increment: completionTokens + }), + }); + + const incrementPromptTokensResponse = await fetch(`${redisRestUrl}/hincrby`, { + method: 'POST', + headers: headers, + body: JSON.stringify({ + key: `tokens:${email}`, + field: `${dateKey}:prompt_tokens`, + increment: promptTokens + }), + }); + + if (!incrementCompletionTokensResponse.ok) { + console.error('Failed to increment completion token count in Redis via REST API'); + } + + if (!incrementPromptTokensResponse.ok) { + console.error('Failed to increment prompt token count in Redis via REST API'); + } +}; diff --git a/package.json b/package.json index 88955f73f..eed58ae95 100644 --- a/package.json +++ b/package.json @@ -40,8 +40,7 @@ "sass": "^1.59.2", "spark-md5": "^3.0.2", "use-debounce": "^9.0.4", - "zustand": "^4.3.8", - "ioredis": "^5.3.2" + "zustand": "^4.3.8" }, "devDependencies": { "@tauri-apps/cli": "^1.4.0",