Skip to content

Commit

Permalink
Add filtering to sessionID for workspace chats (#2531)
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycarambat authored Oct 24, 2024
1 parent c3a7a35 commit 72ba9f7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
14 changes: 13 additions & 1 deletion server/endpoints/api/workspace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ function apiWorkspaceEndpoints(app) {
required: true,
type: 'string'
}
#swagger.parameters['apiSessionId'] = {
in: 'query',
description: 'Optional apiSessionId to filter by',
required: false,
type: 'string'
}
#swagger.responses[200] = {
content: {
"application/json": {
Expand Down Expand Up @@ -370,14 +376,20 @@ function apiWorkspaceEndpoints(app) {
*/
try {
const { slug } = request.params;
const { apiSessionId = null } = request.query;
const workspace = await Workspace.get({ slug });

if (!workspace) {
response.sendStatus(400).end();
return;
}

const history = await WorkspaceChats.forWorkspace(workspace.id);
const history = apiSessionId
? await WorkspaceChats.forWorkspaceByApiSessionId(
workspace.id,
apiSessionId
)
: await WorkspaceChats.forWorkspace(workspace.id);
response.status(200).json({ history: convertToChatHistory(history) });
} catch (e) {
console.error(e.message, e);
Expand Down
25 changes: 25 additions & 0 deletions server/models/workspaceChats.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ const WorkspaceChats = {
}
},

forWorkspaceByApiSessionId: async function (
workspaceId = null,
apiSessionId = null,
limit = null,
orderBy = null
) {
if (!workspaceId || !apiSessionId) return [];
try {
const chats = await prisma.workspace_chats.findMany({
where: {
workspaceId,
user_id: null,
api_session_id: String(apiSessionId),
thread_id: null,
},
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
});
return chats;
} catch (error) {
console.error(error.message);
return [];
}
},

forWorkspace: async function (
workspaceId = null,
limit = null,
Expand Down
9 changes: 9 additions & 0 deletions server/swagger/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,15 @@
"type": "string"
},
"description": "Unique slug of workspace to find"
},
{
"name": "apiSessionId",
"in": "query",
"description": "Optional apiSessionId to filter by",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down

0 comments on commit 72ba9f7

Please sign in to comment.