Skip to content

An AI-powered search engine with a generative UI

feat: Add support for local Redis

casistack876a472f9f46
diff --git a/.env.local.example b/.env.local.example
index ed99772c..81653a0a 100644
--- a/.env.local.example
+++ b/.env.local.example
@@ -7,6 +7,10 @@ OPENAI_API_KEY=[YOUR_OPENAI_API_KEY]
# Tavily API Key retrieved here: https://app.tavily.com/home
TAVILY_API_KEY=[YOUR_TAVILY_API_KEY]
+# Redis Configuration
+USE_LOCAL_REDIS=true
+LOCAL_REDIS_URL=redis://localhost:6379 # or redis://redis:6379 if you're using docker compose
+
# Upstash Redis URL and Token retrieved here: https://console.upstash.com/redis
UPSTASH_REDIS_REST_URL=[YOUR_UPSTASH_REDIS_REST_URL]
UPSTASH_REDIS_REST_TOKEN=[YOUR_UPSTASH_REDIS_REST_TOKEN]
@@ -47,4 +51,4 @@ UPSTASH_REDIS_REST_TOKEN=[YOUR_UPSTASH_REDIS_REST_TOKEN]
# enable the video search tool
# Serper API Key retrieved here: https://serper.dev/api-key
-# SERPER_API_KEY=[YOUR_SERPER_API_KEY]
+# SERPER_API_KEY=[YOUR_SERPER_API_KEY]
\ No newline at end of file
diff --git a/Dockerfile.test b/Dockerfile.test
new file mode 100644
index 00000000..4e7248af
--- /dev/null
+++ b/Dockerfile.test
@@ -0,0 +1,14 @@
+FROM oven/bun:1.1.3-alpine
+
+RUN apk add --no-cache nodejs npm git
+
+WORKDIR /app
+
+COPY package.json bun.lockb ./
+RUN bun install
+
+COPY . .
+
+RUN bun next telemetry disable
+
+CMD ["bun", "dev", "-H", "0.0.0.0"]
\ No newline at end of file
diff --git a/README.md b/README.md
index e3a6e782..ed7ca4c6 100644
--- a/README.md
+++ b/README.md
@@ -83,6 +83,18 @@ TAVILY_API_KEY=
# Upstash Redis URL and Token retrieved here: https://console.upstash.com/redis
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
+
+## Redis Configuration
+
+This application supports both Upstash Redis and local Redis. To use local Redis:
+
+1. Set `USE_LOCAL_REDIS=true` in your `.env.local` file.
+2. Optionally, set `LOCAL_REDIS_URL` if your local Redis is not running on the default `localhost:6379` or `redis://redis:6379` if you're using docker compose.
+
+To use Upstash Redis:
+
+1. Set `USE_LOCAL_REDIS=false` or leave it unset in your `.env.local` file.
+2. Set `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` with your Upstash credentials.
```
_Note: This project focuses on Generative UI and requires complex output from LLMs. Currently, it's assumed that the official OpenAI models will be used. Although it's possible to set up other models, if you use an OpenAI-compatible model, but we don't guarantee that it'll work._
@@ -157,3 +169,4 @@ This will allow you to use Morphic as your default search engine in the browser.
- LLaMA3.1 70B
- LLaMA3 8b
- LLaMA3 70b
+
diff --git a/app/actions.tsx b/app/actions.tsx
index 748724ae..d1cccddd 100644
--- a/app/actions.tsx
+++ b/app/actions.tsx
@@ -358,7 +358,13 @@ export const AI = createAI<AIState, UIState>({
export const getUIStateFromAIState = (aiState: Chat) => {
const chatId = aiState.chatId
const isSharePage = aiState.isSharePage
- return aiState.messages
+
+ // Ensure messages is an array of plain objects
+ const messages = Array.isArray(aiState.messages)
+ ? aiState.messages.map(msg => ({...msg}))
+ : [];
+
+ return messages
.map((message, index) => {
const { role, content, id, type, name } = message
diff --git a/docker-compose.yaml b/docker-compose.yaml
index a8c83389..c09263ac 100644
--- a/docker-compose.yaml
+++ b/docker-compose.yaml
@@ -5,8 +5,21 @@ services:
morphic:
build:
context: . # The build context is the current directory
- dockerfile: Dockerfile
+ dockerfile: Dockerfile.test
command: bun dev # Use `bun dev -H 0.0.0.0` to listen on all interfaces
env_file: .env.local # Load environment variables
ports:
- "3000:3000" # Maps port 3000 on the host to port 3000 in the container.
+ depends_on:
+ - redis
+
+ redis:
+ image: redis:alpine
+ ports:
+ - "6379:6379"
+ volumes:
+ - redis_data:/data
+ command: redis-server --appendonly yes
+
+volumes:
+ redis_data:
\ No newline at end of file
diff --git a/lib/actions/chat.ts b/lib/actions/chat.ts
index 9651a89b..61dde6f6 100644
--- a/lib/actions/chat.ts
+++ b/lib/actions/chat.ts
@@ -3,12 +3,11 @@
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'
import { type Chat } from '@/lib/types'
-import { Redis } from '@upstash/redis'
+import { getRedisClient, RedisWrapper } from '@/lib/redis/config'
-const redis = new Redis({
- url: process.env.UPSTASH_REDIS_REST_URL || '',
- token: process.env.UPSTASH_REDIS_REST_TOKEN || ''
-})
+async function getRedis(): Promise<RedisWrapper> {
+ return await getRedisClient()
+}
export async function getChats(userId?: string | null) {
if (!userId) {
@@ -16,37 +15,78 @@ export async function getChats(userId?: string | null) {
}
try {
- const pipeline = redis.pipeline()
- const chats: string[] = await redis.zrange(`user:chat:${userId}`, 0, -1, {
+ const redis = await getRedis()
+ const chats = await redis.zrange(`user:chat:${userId}`, 0, -1, {
rev: true
})
- for (const chat of chats) {
- pipeline.hgetall(chat)
+ if (chats.length === 0) {
+ return []
}
- const results = await pipeline.exec()
-
- return results as Chat[]
+ const results = await Promise.all(
+ chats.map(async chatKey => {
+ const chat = await redis.hgetall(chatKey)
+ return chat
+ })
+ )
+
+ return results
+ .filter((result): result is Record<string, any> => {
+ if (result === null || Object.keys(result).length === 0) {
+ return false
+ }
+ return true
+ })
+ .map(chat => {
+ const plainChat = { ...chat }
+ if (typeof plainChat.messages === 'string') {
+ try {
+ plainChat.messages = JSON.parse(plainChat.messages)
+ } catch (error) {
+ plainChat.messages = []
+ }
+ }
+ if (plainChat.createdAt && !(plainChat.createdAt instanceof Date)) {
+ plainChat.createdAt = new Date(plainChat.createdAt)
+ }
+ return plainChat as Chat
+ })
} catch (error) {
return []
}
}
export async function getChat(id: string, userId: string = 'anonymous') {
+ const redis = await getRedis()
const chat = await redis.hgetall<Chat>(`chat:${id}`)
if (!chat) {
return null
}
+ // Parse the messages if they're stored as a string
+ if (typeof chat.messages === 'string') {
+ try {
+ chat.messages = JSON.parse(chat.messages)
+ } catch (error) {
+ chat.messages = []
+ }
+ }
+
+ // Ensure messages is always an array
+ if (!Array.isArray(chat.messages)) {
+ chat.messages = []
+ }
+
return chat
}
export async function clearChats(
userId: string = 'anonymous'
): Promise<{ error?: string }> {
- const chats: string[] = await redis.zrange(`user:chat:${userId}`, 0, -1)
+ const redis = await getRedis()
+ const chats = await redis.zrange(`user:chat:${userId}`, 0, -1)
if (!chats.length) {
return { error: 'No chats to clear' }
}
@@ -64,16 +104,28 @@ export async function clearChats(
}
export async function saveChat(chat: Chat, userId: string = 'anonymous') {
- const pipeline = redis.pipeline()
- pipeline.hmset(`chat:${chat.id}`, chat)
- pipeline.zadd(`user:chat:${chat.userId}`, {
- score: Date.now(),
- member: `chat:${chat.id}`
- })
- await pipeline.exec()
+ try {
+ const redis = await getRedis()
+ const pipeline = redis.pipeline()
+
+ const chatToSave = {
+ ...chat,
+ messages: JSON.stringify(chat.messages)
+ }
+
+ pipeline.hmset(`chat:${chat.id}`, chatToSave)
+ pipeline.zadd(`user:chat:${userId}`, Date.now(), `chat:${chat.id}`)
+
+ const results = await pipeline.exec()
+
+ return results
+ } catch (error) {
+ throw error
+ }
}
export async function getSharedChat(id: string) {
+ const redis = await getRedis()
const chat = await redis.hgetall<Chat>(`chat:${id}`)
if (!chat || !chat.sharePath) {
@@ -84,6 +136,7 @@ export async function getSharedChat(id: string) {
}
export async function shareChat(id: string, userId: string = 'anonymous') {
+ const redis = await getRedis()
const chat = await redis.hgetall<Chat>(`chat:${id}`)
if (!chat || chat.userId !== userId) {
diff --git a/lib/redis/config.ts b/lib/redis/config.ts
new file mode 100644
index 00000000..02d352ad
--- /dev/null
+++ b/lib/redis/config.ts
@@ -0,0 +1,302 @@
+import { Redis } from '@upstash/redis'
+import { createClient, RedisClientType } from 'redis'
+
+export type RedisConfig = {
+ useLocalRedis: boolean
+ upstashRedisRestUrl?: string
+ upstashRedisRestToken?: string
+ localRedisUrl?: string
+}
+
+export const redisConfig: RedisConfig = {
+ useLocalRedis: process.env.USE_LOCAL_REDIS === 'true',
+ upstashRedisRestUrl: process.env.UPSTASH_REDIS_REST_URL,
+ upstashRedisRestToken: process.env.UPSTASH_REDIS_REST_TOKEN,
+ localRedisUrl: process.env.LOCAL_REDIS_URL || 'redis://localhost:6379'
+}
+
+let localRedisClient: RedisClientType | null = null
+let redisWrapper: RedisWrapper | null = null
+
+// Wrapper class for Redis client
+export class RedisWrapper {
+ private client: Redis | RedisClientType
+
+ constructor(client: Redis | RedisClientType) {
+ this.client = client
+ }
+
+ async zrange(
+ key: string,
+ start: number,
+ stop: number,
+ options?: { rev: boolean }
+ ): Promise<string[]> {
+ let result: string[]
+ if (this.client instanceof Redis) {
+ result = await this.client.zrange(key, start, stop, options)
+ } else {
+ const redisClient = this.client as RedisClientType
+ if (options?.rev) {
+ result = await redisClient.zRange(key, start, stop, { REV: true })
+ } else {
+ result = await redisClient.zRange(key, start, stop)
+ }
+ }
+ return result
+ }
+
+ async hgetall<T extends Record<string, unknown>>(
+ key: string
+ ): Promise<T | null> {
+ if (this.client instanceof Redis) {
+ return this.client.hgetall(key) as Promise<T | null>
+ } else {
+ const result = await (this.client as RedisClientType).hGetAll(key)
+ return Object.keys(result).length > 0 ? (result as T) : null
+ }
+ }
+
+ pipeline() {
+ return this.client instanceof Redis
+ ? new UpstashPipelineWrapper(this.client.pipeline())
+ : new LocalPipelineWrapper((this.client as RedisClientType).multi())
+ }
+
+ async hmset(key: string, value: Record<string, any>): Promise<'OK' | number> {
+ if (this.client instanceof Redis) {
+ return this.client.hmset(key, value)
+ } else {
+ return (this.client as RedisClientType).hSet(key, value)
+ }
+ }
+
+ async zadd(
+ key: string,
+ score: number,
+ member: string
+ ): Promise<number | null> {
+ if (this.client instanceof Redis) {
+ return this.client.zadd(key, { score, member })
+ } else {
+ return (this.client as RedisClientType).zAdd(key, {
+ score,
+ value: member
+ })
+ }
+ }
+
+ async del(key: string): Promise<number> {
+ if (this.client instanceof Redis) {
+ return this.client.del(key)
+ } else {
+ return (this.client as RedisClientType).del(key)
+ }
+ }
+
+ async zrem(key: string, member: string): Promise<number> {
+ if (this.client instanceof Redis) {
+ return this.client.zrem(key, member)
+ } else {
+ return (this.client as RedisClientType).zRem(key, member)
+ }
+ }
+
+ async close(): Promise<void> {
+ if (this.client instanceof Redis) {
+ // Upstash Redis doesn't require explicit closing
+ return
+ } else {
+ await (this.client as RedisClientType).quit()
+ }
+ }
+}
+
+// Wrapper class for Upstash Redis pipeline
+class UpstashPipelineWrapper {
+ private pipeline: ReturnType<Redis['pipeline']>
+
+ constructor(pipeline: ReturnType<Redis['pipeline']>) {
+ this.pipeline = pipeline
+ }
+
+ hgetall(key: string) {
+ this.pipeline.hgetall(key)
+ return this
+ }
+
+ del(key: string) {
+ this.pipeline.del(key)
+ return this
+ }
+
+ zrem(key: string, member: string) {
+ this.pipeline.zrem(key, member)
+ return this
+ }
+
+ hmset(key: string, value: Record<string, any>) {
+ this.pipeline.hmset(key, value)
+ return this
+ }
+
+ zadd(key: string, score: number, member: string) {
+ this.pipeline.zadd(key, { score, member })
+ return this
+ }
+
+ async exec() {
+ try {
+ return await this.pipeline.exec()
+ } catch (error) {
+ throw error
+ }
+ }
+}
+
+// Wrapper class for local Redis pipeline
+class LocalPipelineWrapper {
+ private pipeline: ReturnType<RedisClientType['multi']>
+
+ constructor(pipeline: ReturnType<RedisClientType['multi']>) {
+ this.pipeline = pipeline
+ }
+
+ hgetall(key: string) {
+ this.pipeline.hGetAll(key)
+ return this
+ }
+
+ del(key: string) {
+ this.pipeline.del(key)
+ return this
+ }
+
+ zrem(key: string, member: string) {
+ this.pipeline.zRem(key, member)
+ return this
+ }
+
+ hmset(key: string, value: Record<string, any>) {
+ // Convert all values to strings
+ const stringValue = Object.fromEntries(
+ Object.entries(value).map(([k, v]) => [k, String(v)])
+ )
+ this.pipeline.hSet(key, stringValue)
+ return this
+ }
+
+ zadd(key: string, score: number, member: string) {
+ this.pipeline.zAdd(key, { score, value: member })
+ return this
+ }
+
+ async exec() {
+ try {
+ return await this.pipeline.exec()
+ } catch (error) {
+ throw error
+ }
+ }
+}
+
+// Function to get a Redis client
+export async function getRedisClient(): Promise<RedisWrapper> {
+ if (redisWrapper) {
+ return redisWrapper
+ }
+
+ if (redisConfig.useLocalRedis) {
+ if (!localRedisClient) {
+ const localRedisUrl =
+ redisConfig.localRedisUrl || 'redis://localhost:6379'
+ try {
+ localRedisClient = createClient({ url: localRedisUrl })
+ await localRedisClient.connect()
+ } catch (error) {
+ if (error instanceof Error) {
+ if (error.message.includes('ECONNREFUSED')) {
+ console.error(
+ `Failed to connect to local Redis at ${localRedisUrl}: Connection refused. Is Redis running?`
+ )
+ } else if (error.message.includes('ETIMEDOUT')) {
+ console.error(
+ `Failed to connect to local Redis at ${localRedisUrl}: Connection timed out. Check your network or Redis server.`
+ )
+ } else if (error.message.includes('ENOTFOUND')) {
+ console.error(
+ `Failed to connect to local Redis at ${localRedisUrl}: Host not found. Check your Redis URL.`
+ )
+ } else {
+ console.error(
+ `Failed to connect to local Redis at ${localRedisUrl}:`,
+ error.message
+ )
+ }
+ } else {
+ console.error(
+ `An unexpected error occurred while connecting to local Redis at ${localRedisUrl}:`,
+ error
+ )
+ }
+ throw new Error(
+ 'Failed to connect to local Redis. Check your configuration and ensure Redis is running.'
+ )
+ }
+ }
+ redisWrapper = new RedisWrapper(localRedisClient)
+ } else {
+ if (
+ !redisConfig.upstashRedisRestUrl ||
+ !redisConfig.upstashRedisRestToken
+ ) {
+ throw new Error(
+ 'Upstash Redis configuration is missing. Please check your environment variables.'
+ )
+ }
+ try {
+ redisWrapper = new RedisWrapper(
+ new Redis({
+ url: redisConfig.upstashRedisRestUrl,
+ token: redisConfig.upstashRedisRestToken
+ })
+ )
+ } catch (error) {
+ if (error instanceof Error) {
+ if (error.message.includes('unauthorized')) {
+ console.error(
+ 'Failed to connect to Upstash Redis: Unauthorized. Check your Upstash Redis token.'
+ )
+ } else if (error.message.includes('not found')) {
+ console.error(
+ 'Failed to connect to Upstash Redis: URL not found. Check your Upstash Redis URL.'
+ )
+ } else {
+ console.error('Failed to connect to Upstash Redis:', error.message)
+ }
+ } else {
+ console.error(
+ 'An unexpected error occurred while connecting to Upstash Redis:',
+ error
+ )
+ }
+ throw new Error(
+ 'Failed to connect to Upstash Redis. Check your configuration and credentials.'
+ )
+ }
+ }
+
+ return redisWrapper
+}
+
+// Function to close the Redis connection
+export async function closeRedisConnection(): Promise<void> {
+ if (redisWrapper) {
+ await redisWrapper.close()
+ redisWrapper = null
+ }
+ if (localRedisClient) {
+ await localRedisClient.quit()
+ localRedisClient = null
+ }
+}