Skip to content

Commit

Permalink
Add optional limit and orderBy to dev api chat endpoint (#2559)
Browse files Browse the repository at this point in the history
add optional limit and orderBy to dev api chat endpoint
  • Loading branch information
shatfield4 authored Oct 30, 2024
1 parent ccde891 commit e719d05
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
31 changes: 28 additions & 3 deletions server/endpoints/api/workspace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -378,20 +390,33 @@ 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) {
response.sendStatus(400).end();
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);
Expand Down
18 changes: 18 additions & 0 deletions server/swagger/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit e719d05

Please sign in to comment.