diff --git a/server/endpoints/api/workspace/index.js b/server/endpoints/api/workspace/index.js index 090d5c7afa..ada7f2fb73 100644 --- a/server/endpoints/api/workspace/index.js +++ b/server/endpoints/api/workspace/index.js @@ -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); diff --git a/server/swagger/openapi.json b/server/swagger/openapi.json index d478e1b264..b0272034c0 100644 --- a/server/swagger/openapi.json +++ b/server/swagger/openapi.json @@ -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": {