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';
/**
* 获取一个聊天框的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
{
url
:
string
;
data
:
any
;
...
...
@@ -9,7 +10,8 @@ export const streamFetch = ({ url, data, onMessage }: StreamFetchProps) =>
const
res
=
await
fetch
(
url
,
{
method
:
'POST'
,
headers
:
{
'Content-Type'
:
'application/json'
'Content-Type'
:
'application/json'
,
Authorization
:
getToken
()
||
''
},
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)
prompt
:
ChatItemType
;
chatId
:
string
;
};
const
{
authorization
}
=
req
.
headers
;
try
{
if
(
!
chatId
||
!
prompt
)
{
...
...
@@ -25,7 +26,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await
connectToDatabase
();
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
);
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
,
authorization
);
const
model
:
ModelSchema
=
chat
.
modelId
;
...
...
src/pages/api/chat/generate.ts
View file @
984baf60
...
...
@@ -7,7 +7,10 @@ import type { ModelSchema } from '@/types/mongoSchema';
/* 获取我的模型 */
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
<
any
>
)
{
try
{
const
{
modelId
}
=
req
.
query
;
const
{
modelId
,
isShare
=
'false'
}
=
req
.
query
as
{
modelId
:
string
;
isShare
?:
'true'
|
'false'
;
};
const
{
authorization
}
=
req
.
headers
;
if
(
!
authorization
)
{
...
...
@@ -40,6 +43,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
expiredTime
:
Date
.
now
()
+
model
.
security
.
expiredTime
,
loadAmount
:
model
.
security
.
maxLoadAmount
,
updateTime
:
Date
.
now
(),
isShare
:
isShare
===
'true'
,
content
:
[]
});
...
...
src/pages/api/chat/gpt3.ts
View file @
984baf60
...
...
@@ -12,6 +12,7 @@ import { pushBill } from '@/service/events/bill';
export
default
async
function
handler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
try
{
const
{
prompt
,
chatId
}
=
req
.
body
as
{
prompt
:
ChatItemType
[];
chatId
:
string
};
const
{
authorization
}
=
req
.
headers
;
if
(
!
prompt
||
!
chatId
)
{
throw
new
Error
(
'缺少参数'
);
...
...
@@ -19,7 +20,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
await
connectToDatabase
();
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
);
const
{
chat
,
userApiKey
,
systemKey
,
userId
}
=
await
authChat
(
chatId
,
authorization
);
const
model
=
chat
.
modelId
;
...
...
src/pages/chat/components/SlideBar.tsx
View file @
984baf60
...
...
@@ -281,7 +281,7 @@ const SlideBar = ({
mr=
{
3
}
onClick=
{
async
()
=>
{
copyData
(
`${location.origin}/chat?chatId=${await getChatSiteId(modelId)}`
,
`${location.origin}/chat?chatId=${await getChatSiteId(modelId
, true
)}`
,
'已复制分享链接'
);
onCloseShare
();
...
...
@@ -299,7 +299,7 @@ const SlideBar = ({
onClose
();
}
}
>
分享
当前对话
分享
聊天记录
</
Button
>
</
ModalFooter
>
</
ModalContent
>
...
...
src/service/models/bill.ts
View file @
984baf60
...
...
@@ -12,8 +12,8 @@ const BillSchema = new Schema({
required
:
true
},
time
:
{
type
:
Number
,
default
:
()
=>
Date
.
now
()
type
:
Date
,
default
:
()
=>
new
Date
()
},
textLen
:
{
// 提示词+响应的总字数
...
...
src/service/models/chat.ts
View file @
984baf60
...
...
@@ -25,6 +25,10 @@ const ChatSchema = new Schema({
type
:
Number
,
required
:
true
},
isShare
:
{
type
:
Boolean
,
default
:
false
},
content
:
{
type
:
[
{
...
...
src/service/utils/chat.ts
View file @
984baf60
...
...
@@ -2,6 +2,7 @@ import { Configuration, OpenAIApi } from 'openai';
import
{
Chat
}
from
'../mongo'
;
import
type
{
ChatPopulate
}
from
'@/types/mongoSchema'
;
import
{
formatPrice
}
from
'@/utils/user'
;
import
{
authToken
}
from
'./tools'
;
export
const
getOpenAIApi
=
(
apiKey
:
string
)
=>
{
const
configuration
=
new
Configuration
({
...
...
@@ -11,7 +12,7 @@ export const getOpenAIApi = (apiKey: string) => {
return
new
OpenAIApi
(
configuration
,
undefined
);
};
export
const
authChat
=
async
(
chatId
:
string
)
=>
{
export
const
authChat
=
async
(
chatId
:
string
,
authorization
?:
string
)
=>
{
// 获取 chat 数据
const
chat
=
await
Chat
.
findById
<
ChatPopulate
>
(
chatId
)
.
populate
({
...
...
@@ -36,12 +37,17 @@ export const authChat = async (chatId: string) => {
return
Promise
.
reject
(
'聊天框已过期'
);
}
// 分享校验
if
(
!
chat
.
isShare
)
{
await
authToken
(
authorization
);
}
// 获取 user 的 apiKey
const
user
=
chat
.
userId
;
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
(
'该账号余额不足'
);
}
...
...
src/types/mongoSchema.d.ts
View file @
984baf60
...
...
@@ -69,6 +69,7 @@ export interface ChatSchema {
expiredTime
:
number
;
loadAmount
:
number
;
updateTime
:
number
;
isShare
:
boolean
;
content
:
ChatItemType
[];
}
export
interface
ChatPopulate
extends
ChatSchema
{
...
...
@@ -80,7 +81,7 @@ export interface BillSchema {
_id
:
string
;
userId
:
string
;
chatId
:
string
;
time
:
number
;
time
:
Date
;
textLen
:
number
;
price
:
number
;
}
src/utils/adapt.ts
View file @
984baf60
...
...
@@ -8,7 +8,7 @@ export const adaptBill = (bill: BillSchema): UserBillType => {
id
:
bill
.
_id
,
userId
:
bill
.
userId
,
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
,
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