Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
赵月辉
/
fastgpt-migrated
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
984baf60
authored
Mar 21, 2023
by
archer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: share窗口
parent
d0655397
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
33 additions
and
13 deletions
+33
-13
src/api/chat.ts
+2
-1
src/api/fetch.ts
+3
-1
src/pages/api/chat/chatGpt.ts
+2
-1
src/pages/api/chat/generate.ts
+5
-1
src/pages/api/chat/gpt3.ts
+2
-1
src/pages/chat/components/SlideBar.tsx
+2
-2
src/service/models/bill.ts
+2
-2
src/service/models/chat.ts
+4
-0
src/service/utils/chat.ts
+8
-2
src/types/mongoSchema.d.ts
+2
-1
src/utils/adapt.ts
+1
-1
No files found.
src/api/chat.ts
View file @
984baf60
...
@@ -5,7 +5,8 @@ import type { InitChatResponse } from './response/chat';
...
@@ -5,7 +5,8 @@ import type { InitChatResponse } from './response/chat';
/**
/**
* 获取一个聊天框的ID
* 获取一个聊天框的ID
*/
*/
export
const
getChatSiteId
=
(
modelId
:
string
)
=>
GET
<
string
>
(
`/chat/generate?modelId=
${
modelId
}
`
);
export
const
getChatSiteId
=
(
modelId
:
string
,
isShare
=
false
)
=>
GET
<
string
>
(
`/chat/generate?modelId=
${
modelId
}
&isShare=
${
isShare
?
'true'
:
'false'
}
`
);
/**
/**
* 获取初始化聊天内容
* 获取初始化聊天内容
...
...
src/api/fetch.ts
View file @
984baf60
import
{
getToken
}
from
'../utils/user'
;
interface
StreamFetchProps
{
interface
StreamFetchProps
{
url
:
string
;
url
:
string
;
data
:
any
;
data
:
any
;
...
@@ -9,7 +10,8 @@ export const streamFetch = ({ url, data, onMessage }: StreamFetchProps) =>
...
@@ -9,7 +10,8 @@ export const streamFetch = ({ url, data, onMessage }: StreamFetchProps) =>
const
res
=
await
fetch
(
url
,
{
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
method
:
'POST'
,
headers
:
{
headers
:
{
'Content-Type'
:
'application/json'
'Content-Type'
:
'application/json'
,
Authorization
:
getToken
()
||
''
},
},
body
:
JSON
.
stringify
(
data
)
body
:
JSON
.
stringify
(
data
)
});
});
...
...
src/pages/api/chat/chatGpt.ts
View file @
984baf60
...
@@ -17,6 +17,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -17,6 +17,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
prompt
:
ChatItemType
;
prompt
:
ChatItemType
;
chatId
:
string
;
chatId
:
string
;
};
};
const
{
authorization
}
=
req
.
headers
;
try
{
try
{
if
(
!
chatId
||
!
prompt
)
{
if
(
!
chatId
||
!
prompt
)
{
...
@@ -25,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -25,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await
connectToDatabase
();
await
connectToDatabase
();
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
);
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
,
authorization
);
const
model
:
ModelSchema
=
chat
.
modelId
;
const
model
:
ModelSchema
=
chat
.
modelId
;
...
...
src/pages/api/chat/generate.ts
View file @
984baf60
...
@@ -7,7 +7,10 @@ import type { ModelSchema } from '@/types/mongoSchema';
...
@@ -7,7 +7,10 @@ import type { ModelSchema } from '@/types/mongoSchema';
/* 获取我的模型 */
/* 获取我的模型 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
try
{
const
{
modelId
}
=
req
.
query
;
const
{
modelId
,
isShare
=
'false'
}
=
req
.
query
as
{
modelId
:
string
;
isShare
?:
'true'
|
'false'
;
};
const
{
authorization
}
=
req
.
headers
;
const
{
authorization
}
=
req
.
headers
;
if
(
!
authorization
)
{
if
(
!
authorization
)
{
...
@@ -40,6 +43,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
...
@@ -40,6 +43,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
expiredTime
:
Date
.
now
()
+
model
.
security
.
expiredTime
,
expiredTime
:
Date
.
now
()
+
model
.
security
.
expiredTime
,
loadAmount
:
model
.
security
.
maxLoadAmount
,
loadAmount
:
model
.
security
.
maxLoadAmount
,
updateTime
:
Date
.
now
(),
updateTime
:
Date
.
now
(),
isShare
:
isShare
===
'true'
,
content
:
[]
content
:
[]
});
});
...
...
src/pages/api/chat/gpt3.ts
View file @
984baf60
...
@@ -12,6 +12,7 @@ import { pushBill } from '@/service/events/bill';
...
@@ -12,6 +12,7 @@ import { pushBill } from '@/service/events/bill';
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
try
{
const
{
prompt
,
chatId
}
=
req
.
body
as
{
prompt
:
ChatItemType
[];
chatId
:
string
};
const
{
prompt
,
chatId
}
=
req
.
body
as
{
prompt
:
ChatItemType
[];
chatId
:
string
};
const
{
authorization
}
=
req
.
headers
;
if
(
!
prompt
||
!
chatId
)
{
if
(
!
prompt
||
!
chatId
)
{
throw
new
Error
(
'缺少参数'
);
throw
new
Error
(
'缺少参数'
);
...
@@ -19,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
...
@@ -19,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await
connectToDatabase
();
await
connectToDatabase
();
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
);
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
,
authorization
);
const
model
=
chat
.
modelId
;
const
model
=
chat
.
modelId
;
...
...
src/pages/chat/components/SlideBar.tsx
View file @
984baf60
...
@@ -281,7 +281,7 @@ const SlideBar = ({
...
@@ -281,7 +281,7 @@ const SlideBar = ({
mr=
{
3
}
mr=
{
3
}
onClick=
{
async
()
=>
{
onClick=
{
async
()
=>
{
copyData
(
copyData
(
`${location.origin}/chat?chatId=${await getChatSiteId(modelId)}`
,
`${location.origin}/chat?chatId=${await getChatSiteId(modelId
, true
)}`
,
'已复制分享链接'
'已复制分享链接'
);
);
onCloseShare
();
onCloseShare
();
...
@@ -299,7 +299,7 @@ const SlideBar = ({
...
@@ -299,7 +299,7 @@ const SlideBar = ({
onClose
();
onClose
();
}
}
}
}
>
>
分享
当前对话
分享
聊天记录
</
Button
>
</
Button
>
</
ModalFooter
>
</
ModalFooter
>
</
ModalContent
>
</
ModalContent
>
...
...
src/service/models/bill.ts
View file @
984baf60
...
@@ -12,8 +12,8 @@ const BillSchema = new Schema({
...
@@ -12,8 +12,8 @@ const BillSchema = new Schema({
required
:
true
required
:
true
},
},
time
:
{
time
:
{
type
:
Number
,
type
:
Date
,
default
:
()
=>
Date
.
now
()
default
:
()
=>
new
Date
()
},
},
textLen
:
{
textLen
:
{
// 提示词+响应的总字数
// 提示词+响应的总字数
...
...
src/service/models/chat.ts
View file @
984baf60
...
@@ -25,6 +25,10 @@ const ChatSchema = new Schema({
...
@@ -25,6 +25,10 @@ const ChatSchema = new Schema({
type
:
Number
,
type
:
Number
,
required
:
true
required
:
true
},
},
isShare
:
{
type
:
Boolean
,
default
:
false
},
content
:
{
content
:
{
type
:
[
type
:
[
{
{
...
...
src/service/utils/chat.ts
View file @
984baf60
...
@@ -2,6 +2,7 @@ import { Configuration, OpenAIApi } from 'openai';
...
@@ -2,6 +2,7 @@ import { Configuration, OpenAIApi } from 'openai';
import
{
Chat
}
from
'../mongo'
;
import
{
Chat
}
from
'../mongo'
;
import
type
{
ChatPopulate
}
from
'@/types/mongoSchema'
;
import
type
{
ChatPopulate
}
from
'@/types/mongoSchema'
;
import
{
formatPrice
}
from
'@/utils/user'
;
import
{
formatPrice
}
from
'@/utils/user'
;
import
{
authToken
}
from
'./tools'
;
export
const
getOpenAIApi
=
(
apiKey
:
string
)
=>
{
export
const
getOpenAIApi
=
(
apiKey
:
string
)
=>
{
const
configuration
=
new
Configuration
({
const
configuration
=
new
Configuration
({
...
@@ -11,7 +12,7 @@ export const getOpenAIApi = (apiKey: string) => {
...
@@ -11,7 +12,7 @@ export const getOpenAIApi = (apiKey: string) => {
return
new
OpenAIApi
(
configuration
,
undefined
);
return
new
OpenAIApi
(
configuration
,
undefined
);
};
};
export
const
authChat
=
async
(
chatId
:
string
)
=>
{
export
const
authChat
=
async
(
chatId
:
string
,
authorization
?:
string
)
=>
{
// 获取 chat 数据
// 获取 chat 数据
const
chat
=
await
Chat
.
findById
<
ChatPopulate
>
(
chatId
)
const
chat
=
await
Chat
.
findById
<
ChatPopulate
>
(
chatId
)
.
populate
({
.
populate
({
...
@@ -36,12 +37,17 @@ export const authChat = async (chatId: string) => {
...
@@ -36,12 +37,17 @@ export const authChat = async (chatId: string) => {
return
Promise
.
reject
(
'聊天框已过期'
);
return
Promise
.
reject
(
'聊天框已过期'
);
}
}
// 分享校验
if
(
!
chat
.
isShare
)
{
await
authToken
(
authorization
);
}
// 获取 user 的 apiKey
// 获取 user 的 apiKey
const
user
=
chat
.
userId
;
const
user
=
chat
.
userId
;
const
userApiKey
=
user
.
accounts
?.
find
((
item
:
any
)
=>
item
.
type
===
'openai'
)?.
value
;
const
userApiKey
=
user
.
accounts
?.
find
((
item
:
any
)
=>
item
.
type
===
'openai'
)?.
value
;
if
(
!
userApiKey
&&
formatPrice
(
user
.
balance
)
<=
-
1
)
{
if
(
!
userApiKey
&&
formatPrice
(
user
.
balance
)
<=
0
)
{
return
Promise
.
reject
(
'该账号余额不足'
);
return
Promise
.
reject
(
'该账号余额不足'
);
}
}
...
...
src/types/mongoSchema.d.ts
View file @
984baf60
...
@@ -69,6 +69,7 @@ export interface ChatSchema {
...
@@ -69,6 +69,7 @@ export interface ChatSchema {
expiredTime
:
number
;
expiredTime
:
number
;
loadAmount
:
number
;
loadAmount
:
number
;
updateTime
:
number
;
updateTime
:
number
;
isShare
:
boolean
;
content
:
ChatItemType
[];
content
:
ChatItemType
[];
}
}
export
interface
ChatPopulate
extends
ChatSchema
{
export
interface
ChatPopulate
extends
ChatSchema
{
...
@@ -80,7 +81,7 @@ export interface BillSchema {
...
@@ -80,7 +81,7 @@ export interface BillSchema {
_id
:
string
;
_id
:
string
;
userId
:
string
;
userId
:
string
;
chatId
:
string
;
chatId
:
string
;
time
:
number
;
time
:
Date
;
textLen
:
number
;
textLen
:
number
;
price
:
number
;
price
:
number
;
}
}
src/utils/adapt.ts
View file @
984baf60
...
@@ -8,7 +8,7 @@ export const adaptBill = (bill: BillSchema): UserBillType => {
...
@@ -8,7 +8,7 @@ export const adaptBill = (bill: BillSchema): UserBillType => {
id
:
bill
.
_id
,
id
:
bill
.
_id
,
userId
:
bill
.
userId
,
userId
:
bill
.
userId
,
chatId
:
bill
.
chatId
,
chatId
:
bill
.
chatId
,
time
:
dayjs
(
bill
.
time
).
format
(
'YYYY/MM/DD
hh
:mm:ss'
),
time
:
dayjs
(
bill
.
time
).
format
(
'YYYY/MM/DD
HH
:mm:ss'
),
textLen
:
bill
.
textLen
,
textLen
:
bill
.
textLen
,
price
:
formatPrice
(
bill
.
price
)
price
:
formatPrice
(
bill
.
price
)
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment