Skip to content

Commit 9d122b3

Browse files
authored
Merge pull request #456 from miurla/fix/model-config-loading
refactor: update getModels to use fetch
2 parents 8ee4854 + d17b6cc commit 9d122b3

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

app/page.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Chat } from '@/components/chat'
22
import { getModels } from '@/lib/config/models'
33
import { generateId } from 'ai'
44

5-
export default function Page() {
5+
export default async function Page() {
66
const id = generateId()
7-
const models = getModels()
7+
const models = await getModels()
88
return <Chat id={id} models={models} />
99
}

app/search/[id]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ export default async function SearchPage(props: {
3434
notFound()
3535
}
3636

37-
const models = getModels()
37+
const models = await getModels()
3838
return <Chat id={id} savedMessages={messages} models={models} />
3939
}

app/search/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export default async function SearchPage(props: {
1414
}
1515

1616
const id = generateId()
17-
const models = getModels()
17+
const models = await getModels()
1818
return <Chat id={id} query={q} models={models} />
1919
}

app/share/[id]/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default async function SharePage(props: {
2929
return notFound()
3030
}
3131

32-
const models = getModels()
32+
const models = await getModels()
3333
return (
3434
<Chat
3535
id={chat.id}

lib/config/models.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Model } from '@/lib/types/models'
2-
import { existsSync, readFileSync } from 'fs'
3-
import { join } from 'path'
2+
import { headers } from 'next/headers'
43

54
export function validateModel(model: any): model is Model {
65
return (
@@ -14,11 +13,16 @@ export function validateModel(model: any): model is Model {
1413
)
1514
}
1615

17-
export function getModels(): Model[] {
18-
const configPath = join(process.cwd(), 'public', 'config', 'models.json')
19-
16+
export async function getModels(): Promise<Model[]> {
2017
try {
21-
const config = JSON.parse(readFileSync(configPath, 'utf-8'))
18+
const headersList = await headers()
19+
const baseUrl = new URL(headersList.get('x-url') || 'http://localhost:3000')
20+
const modelUrl = new URL('/config/models.json', baseUrl.origin)
21+
22+
const response = await fetch(modelUrl, {
23+
cache: 'no-store'
24+
})
25+
const config = await response.json()
2226
if (Array.isArray(config.models) && config.models.every(validateModel)) {
2327
return config.models
2428
}

middleware.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { NextResponse } from 'next/server'
2+
import type { NextRequest } from 'next/server'
3+
4+
export function middleware(request: NextRequest) {
5+
// Create a response
6+
const response = NextResponse.next()
7+
8+
// Add request information to response headers
9+
response.headers.set('x-url', request.url)
10+
response.headers.set('x-host', request.headers.get('host') || '')
11+
response.headers.set('x-protocol', request.nextUrl.protocol)
12+
13+
return response
14+
}

0 commit comments

Comments
 (0)