-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
226 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ | |
], | ||
"react-hooks/exhaustive-deps": "warn" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
# misc | ||
.DS_Store | ||
.vscode | ||
*.pem | ||
|
||
# debug | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ | |
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ | |
"i18n/lang", | ||
"app/api/messages" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
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, | ||
} | ||
} | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.