Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Anything Llm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
MachineLearning
Mintplex Labs
Anything Llm
Commits
548da9ad
Commit
548da9ad
authored
6 months ago
by
timothycarambat
Browse files
Options
Downloads
Patches
Plain Diff
Exception handler on embed chat middleware
parent
47a5c712
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/utils/middleware/embedMiddleware.js
+94
-79
94 additions, 79 deletions
server/utils/middleware/embedMiddleware.js
with
94 additions
and
79 deletions
server/utils/middleware/embedMiddleware.js
+
94
−
79
View file @
548da9ad
...
...
@@ -41,106 +41,121 @@ async function validEmbedConfigId(request, response, next) {
}
async
function
canRespond
(
request
,
response
,
next
)
{
const
embed
=
response
.
locals
.
embedConfig
;
if
(
!
embed
)
{
response
.
sendStatus
(
404
).
end
();
return
;
}
// Block if disabled by admin.
if
(
!
embed
.
enabled
)
{
response
.
status
(
503
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
This chat has been disabled by the administrator - try again later.
"
,
});
return
;
}
// Check if requester hostname is in the valid allowlist of domains.
const
host
=
request
.
headers
.
origin
??
""
;
const
allowedHosts
=
EmbedConfig
.
parseAllowedHosts
(
embed
);
if
(
allowedHosts
!==
null
&&
!
allowedHosts
.
includes
(
host
))
{
response
.
status
(
401
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
Invalid request.
"
,
});
return
;
}
const
{
sessionId
,
message
}
=
reqBody
(
request
);
if
(
!
message
?.
length
||
!
VALID_CHAT_MODE
.
includes
(
embed
.
chat_mode
))
{
response
.
status
(
400
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
!
message
?.
length
?
"
Message is empty.
"
:
`
${
embed
.
chat_mode
}
is not a valid mode.`
,
});
return
;
}
if
(
!
isNaN
(
embed
.
max_chats_per_day
)
&&
Number
(
embed
.
max_chats_per_day
)
>
0
)
{
const
dailyChatCount
=
await
EmbedChats
.
count
({
embed_id
:
embed
.
id
,
createdAt
:
{
gte
:
new
Date
(
new
Date
()
-
24
*
60
*
60
*
1000
),
},
});
try
{
const
embed
=
response
.
locals
.
embedConfig
;
if
(
!
embed
)
{
response
.
sendStatus
(
404
).
end
();
return
;
}
if
(
dailyChatCount
>=
Number
(
embed
.
max_chats_per_day
))
{
response
.
status
(
429
).
json
({
// Block if disabled by admin.
if
(
!
embed
.
enabled
)
{
response
.
status
(
503
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
Th
e quota for th
is chat has been
reached. Try again later or contact the site own
er.
"
,
"
This chat has been
disabled by the administrator - try again lat
er.
"
,
});
return
;
}
}
if
(
!
isNaN
(
embed
.
max_chats_per_session
)
&&
Number
(
embed
.
max_chats_per_session
)
>
0
)
{
const
dailySessionCount
=
await
EmbedChats
.
count
({
embed_id
:
embed
.
id
,
session_id
:
sessionId
,
createdAt
:
{
gte
:
new
Date
(
new
Date
()
-
24
*
60
*
60
*
1000
),
},
});
// Check if requester hostname is in the valid allowlist of domains.
const
host
=
request
.
headers
.
origin
??
""
;
const
allowedHosts
=
EmbedConfig
.
parseAllowedHosts
(
embed
);
if
(
allowedHosts
!==
null
&&
!
allowedHosts
.
includes
(
host
))
{
response
.
status
(
401
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
Invalid request.
"
,
});
return
;
}
if
(
dailySessionCount
>=
Number
(
embed
.
max_chats_per_session
))
{
response
.
status
(
429
).
json
({
const
{
sessionId
,
message
}
=
reqBody
(
request
);
if
(
!
message
?.
length
||
!
VALID_CHAT_MODE
.
includes
(
embed
.
chat_mode
))
{
response
.
status
(
400
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
Your quota for this chat has been reached. Try again later or contact the site owner.
"
,
error
:
!
message
?.
length
?
"
Message is empty.
"
:
`
${
embed
.
chat_mode
}
is not a valid mode.`
,
});
return
;
}
}
next
();
if
(
!
isNaN
(
embed
.
max_chats_per_day
)
&&
Number
(
embed
.
max_chats_per_day
)
>
0
)
{
const
dailyChatCount
=
await
EmbedChats
.
count
({
embed_id
:
embed
.
id
,
createdAt
:
{
gte
:
new
Date
(
new
Date
()
-
24
*
60
*
60
*
1000
),
},
});
if
(
dailyChatCount
>=
Number
(
embed
.
max_chats_per_day
))
{
response
.
status
(
429
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
The quota for this chat has been reached. Try again later or contact the site owner.
"
,
});
return
;
}
}
if
(
!
isNaN
(
embed
.
max_chats_per_session
)
&&
Number
(
embed
.
max_chats_per_session
)
>
0
)
{
const
dailySessionCount
=
await
EmbedChats
.
count
({
embed_id
:
embed
.
id
,
session_id
:
sessionId
,
createdAt
:
{
gte
:
new
Date
(
new
Date
()
-
24
*
60
*
60
*
1000
),
},
});
if
(
dailySessionCount
>=
Number
(
embed
.
max_chats_per_session
))
{
response
.
status
(
429
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
Your quota for this chat has been reached. Try again later or contact the site owner.
"
,
});
return
;
}
}
next
();
}
catch
(
e
)
{
response
.
status
(
500
).
json
({
id
:
uuidv4
(),
type
:
"
abort
"
,
textResponse
:
null
,
sources
:
[],
close
:
true
,
error
:
"
Invalid request.
"
,
});
return
;
}
}
module
.
exports
=
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment