Add optional limit and orderBy to dev api chat endpoint ()

add optional limit and orderBy to dev api chat endpoint
This commit is contained in:
Sean Hatfield 2024-10-30 14:47:24 -07:00 committed by GitHub
parent ccde891aa2
commit e719d05027
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 3 deletions
server
endpoints/api/workspace
swagger

View file

@ -347,6 +347,18 @@ function apiWorkspaceEndpoints(app) {
required: false,
type: 'string'
}
#swagger.parameters['limit'] = {
in: 'query',
description: 'Optional number of chat messages to return (default: 100)',
required: false,
type: 'integer'
}
#swagger.parameters['orderBy'] = {
in: 'query',
description: 'Optional order of chat messages (asc or desc)',
required: false,
type: 'string'
}
#swagger.responses[200] = {
content: {
"application/json": {
@ -378,7 +390,11 @@ function apiWorkspaceEndpoints(app) {
*/
try {
const { slug } = request.params;
const { apiSessionId = null } = request.query;
const {
apiSessionId = null,
limit = 100,
orderBy = "desc",
} = request.query;
const workspace = await Workspace.get({ slug });
if (!workspace) {
@ -386,12 +402,21 @@ function apiWorkspaceEndpoints(app) {
return;
}
const validLimit = Math.max(1, parseInt(limit));
const validOrderBy = ["asc", "desc"].includes(orderBy)
? orderBy
: "desc";
const history = apiSessionId
? await WorkspaceChats.forWorkspaceByApiSessionId(
workspace.id,
apiSessionId
apiSessionId,
validLimit,
{ createdAt: validOrderBy }
)
: await WorkspaceChats.forWorkspace(workspace.id);
: await WorkspaceChats.forWorkspace(workspace.id, validLimit, {
createdAt: validOrderBy,
});
response.status(200).json({ history: convertToChatHistory(history) });
} catch (e) {
console.error(e.message, e);

View file

@ -1660,6 +1660,24 @@
"schema": {
"type": "string"
}
},
{
"name": "limit",
"in": "query",
"description": "Optional number of chat messages to return (default: 100)",
"required": false,
"schema": {
"type": "integer"
}
},
{
"name": "orderBy",
"in": "query",
"description": "Optional order of chat messages (asc or desc)",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {