feat: lint code

This commit is contained in:
Joel
2023-06-10 14:04:40 +08:00
parent 2e46f795a4
commit cfd0c9532f
36 changed files with 226 additions and 210 deletions

View File

@ -1,5 +1,5 @@
import { type NextRequest } from 'next/server'
import { getInfo, client } from '@/app/api/utils/common'
import { client, getInfo } from '@/app/api/utils/common'
import { OpenAIStream } from '@/app/api/utils/stream'
export async function POST(request: NextRequest) {
@ -8,10 +8,10 @@ export async function POST(request: NextRequest) {
inputs,
query,
conversation_id: conversationId,
response_mode: responseMode
response_mode: responseMode,
} = body
const { user } = getInfo(request);
const { user } = getInfo(request)
const res = await client.createChatMessage(inputs, query, user, responseMode, conversationId)
const stream = await OpenAIStream(res as any)
return new Response(stream as any)
}
}

View File

@ -1,11 +1,11 @@
import { type NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { getInfo, setSession, client } from '@/app/api/utils/common'
import { client, getInfo, setSession } from '@/app/api/utils/common'
export async function GET(request: NextRequest) {
const { sessionId, user } = getInfo(request);
const { data }: any = await client.getConversations(user);
const { sessionId, user } = getInfo(request)
const { data }: any = await client.getConversations(user)
return NextResponse.json(data, {
headers: setSession(sessionId)
headers: setSession(sessionId),
})
}
}

View File

@ -1,16 +1,16 @@
import { type NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { getInfo, client } from '@/app/api/utils/common'
import { client, getInfo } from '@/app/api/utils/common'
export async function POST(request: NextRequest, { params }: {
params: { messageId: string }
}) {
const body = await request.json()
const {
rating
rating,
} = body
const { messageId } = params
const { user } = getInfo(request);
const { user } = getInfo(request)
const { data } = await client.messageFeedback(messageId, rating, user)
return NextResponse.json(data)
}

View File

@ -1,13 +1,13 @@
import { type NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { getInfo, setSession, client } from '@/app/api/utils/common'
import { client, getInfo, setSession } from '@/app/api/utils/common'
export async function GET(request: NextRequest) {
const { sessionId, user } = getInfo(request);
const { searchParams } = new URL(request.url);
const { sessionId, user } = getInfo(request)
const { searchParams } = new URL(request.url)
const conversationId = searchParams.get('conversation_id')
const { data }: any = await client.getConversationMessages(user, conversationId as string);
const { data }: any = await client.getConversationMessages(user, conversationId as string)
return NextResponse.json(data, {
headers: setSession(sessionId)
headers: setSession(sessionId),
})
}
}

View File

@ -1,11 +1,11 @@
import { type NextRequest } from 'next/server'
import { NextResponse } from 'next/server'
import { getInfo, setSession, client } from '@/app/api/utils/common'
import { client, getInfo, setSession } from '@/app/api/utils/common'
export async function GET(request: NextRequest) {
const { sessionId, user } = getInfo(request);
const { data } = await client.getApplicationParameters(user);
const { sessionId, user } = getInfo(request)
const { data } = await client.getApplicationParameters(user)
return NextResponse.json(data as object, {
headers: setSession(sessionId)
headers: setSession(sessionId),
})
}
}

View File

@ -1,16 +1,16 @@
import { type NextRequest } from 'next/server'
import { APP_ID, API_KEY, API_URL } from '@/config'
import { ChatClient } from 'dify-client'
import { v4 } from 'uuid'
import { API_KEY, API_URL, APP_ID } from '@/config'
const userPrefix = `user_${APP_ID}:`;
const userPrefix = `user_${APP_ID}:`
export const getInfo = (request: NextRequest) => {
const sessionId = request.cookies.get('session_id')?.value || v4();
const user = userPrefix + sessionId;
const sessionId = request.cookies.get('session_id')?.value || v4()
const user = userPrefix + sessionId
return {
sessionId,
user
user,
}
}
@ -18,4 +18,4 @@ export const setSession = (sessionId: string) => {
return { 'Set-Cookie': `session_id=${sessionId}` }
}
export const client = new ChatClient(API_KEY, API_URL ? API_URL : undefined)
export const client = new ChatClient(API_KEY, API_URL || undefined)

View File

@ -1,25 +1,25 @@
export async function OpenAIStream(res: { body: any }) {
const reader = res.body.getReader();
const reader = res.body.getReader()
const stream = new ReadableStream({
// https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
// https://github.com/whichlight/chatgpt-api-streaming/blob/master/pages/api/OpenAIStream.ts
start(controller) {
return pump();
return pump()
function pump() {
return reader.read().then(({ done, value }: any) => {
// When no more data needs to be consumed, close the stream
if (done) {
controller.close();
return;
controller.close()
return
}
// Enqueue the next data chunk into our target stream
controller.enqueue(value);
return pump();
});
controller.enqueue(value)
return pump()
})
}
},
});
})
return stream;
}
return stream
}