import { getRealtimeRecoveryRequestConfig } from '@baserow/modules/core/plugins/realtimeProtocol' // Fill tall conversation lists in one request with lightweight list metadata. const CONVERSATION_PAGE_SIZE = 100 const MESSAGE_PAGE_SIZE = 50 export default (client) => ({ get(agentId) { return client.get(`/agent/${agentId}/`) }, recordView(agentId) { return client.post(`/agent/${agentId}/`) }, getConfiguration(agentId, realtimeRecovery = false) { return client.get(`/agent/${agentId}/configuration/`, { ...(realtimeRecovery ? getRealtimeRecoveryRequestConfig() : {}), }) }, getWorkspaceTools(workspaceId) { return client.get(`/agent/tools/workspace/${workspaceId}/`) }, getRunAsIdentities(agentId, page = 1, search = '') { return client.get(`/agent/${agentId}/run-as-identities/`, { params: { page, search, size: 100 }, // Opening the picker revalidates choices, including changes missed while // disconnected. A lagging replica must not reintroduce a deleted choice. ...getRealtimeRecoveryRequestConfig(), }) }, updateConfiguration(agentId, { run_as: runAs, ...values }) { if (runAs !== undefined) { values.run_as_subject_type = runAs?.type ?? null values.run_as_subject_id = runAs?.id ?? null } return client.patch(`/agent/${agentId}/configuration/`, values) }, getConversations( agentId, cursor = null, { realtimeRecovery = false, ...filters } = {} ) { return client.get(`/agent/${agentId}/conversations/`, { params: { cursor, limit: CONVERSATION_PAGE_SIZE, ...filters, }, ...(realtimeRecovery ? getRealtimeRecoveryRequestConfig() : {}), }) }, createConversation(agentId, values) { return client.post(`/agent/${agentId}/conversations/`, values) }, updateConversation(agentId, conversationUuid, values) { return client.patch( `/agent/${agentId}/conversations/${conversationUuid}/`, values ) }, shareConversation(agentId, conversationUuid) { return client.post( `/agent/${agentId}/conversations/${conversationUuid}/share/` ) }, leaveConversation(agentId, conversationUuid) { return client.post( `/agent/${agentId}/conversations/${conversationUuid}/leave/` ) }, getApproverCandidates(agentId, conversationUuid, page, search) { return client.get( `/agent/${agentId}/conversations/${conversationUuid}/approvers/`, { params: { page, search } } ) }, requestApproval(agentId, conversationUuid, userIds) { return client.post( `/agent/${agentId}/conversations/${conversationUuid}/approvers/`, { user_ids: userIds } ) }, deleteConversation(agentId, conversationUuid) { return client.delete(`/agent/${agentId}/conversations/${conversationUuid}/`) }, getMessages(agentId, conversationUuid, page = 1, realtimeRecovery = false) { return client.get( `/agent/${agentId}/conversations/${conversationUuid}/messages/`, { params: { page, size: MESSAGE_PAGE_SIZE }, ...(realtimeRecovery ? getRealtimeRecoveryRequestConfig() : {}), } ) }, createMessage(agentId, conversationUuid, values) { return client.post( `/agent/${agentId}/conversations/${conversationUuid}/messages/`, values ) }, getPendingApprovals(agentId, page = 1) { return client.get(`/agent/${agentId}/pending-approvals/`, { params: { page, size: 20 }, ...getRealtimeRecoveryRequestConfig(), }) }, decideApprovals(agentId, conversationUuid, decisions, { alwaysAllow } = {}) { return client.post( `/agent/${agentId}/conversations/${conversationUuid}/approvals/`, { decisions, ...(alwaysAllow ? { always_allow: true } : {}) } ) }, })