Skip to content

Commit

Permalink
Merge branch 'development' into feat/issue-571
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay authored Nov 15, 2024
2 parents b8d4911 + 8d2eb24 commit a12767a
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.mutable.MutableObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -148,7 +149,7 @@ public Publication createPublication(ProxyContext context, Publication publicati
prepareAndValidatePublicationRequest(context, publication, bucket, bucketLocation, isAdmin);

List<Publication.Resource> resourcesToAdd = publication.getResources().stream()
.filter(resource -> resource.getAction() == Publication.ResourceAction.ADD)
.filter(resource -> resource.getAction() == Publication.ResourceAction.ADD || resource.getAction() == Publication.ResourceAction.ADD_IF_ABSENT)
.toList();

copySourceToReviewResources(resourcesToAdd);
Expand Down Expand Up @@ -204,7 +205,7 @@ public Publication deletePublication(ResourceDescriptor resource) {

if (publication.getStatus() == Publication.Status.PENDING) {
List<Publication.Resource> resourcesToAdd = publication.getResources().stream()
.filter(i -> i.getAction() == Publication.ResourceAction.ADD)
.filter(i -> i.getAction() == Publication.ResourceAction.ADD || i.getAction() == Publication.ResourceAction.ADD_IF_ABSENT)
.toList();
deleteReviewResources(resourcesToAdd);
}
Expand All @@ -220,7 +221,7 @@ public Publication approvePublication(ResourceDescriptor resource) {
}

List<Publication.Resource> resourcesToAdd = publication.getResources().stream()
.filter(i -> i.getAction() == Publication.ResourceAction.ADD)
.filter(i -> i.getAction() == Publication.ResourceAction.ADD || i.getAction() == Publication.ResourceAction.ADD_IF_ABSENT)
.toList();

List<Publication.Resource> resourcesToDelete = publication.getResources().stream()
Expand Down Expand Up @@ -294,7 +295,7 @@ public Publication rejectPublication(ResourceDescriptor resource, RejectPublicat

Publication publication = reference.getValue();
List<Publication.Resource> resourcesToAdd = publication.getResources().stream()
.filter(i -> i.getAction() == Publication.ResourceAction.ADD)
.filter(i -> i.getAction() == Publication.ResourceAction.ADD || i.getAction() == Publication.ResourceAction.ADD_IF_ABSENT)
.toList();
deleteReviewResources(resourcesToAdd);

Expand Down Expand Up @@ -504,7 +505,7 @@ private void checkTargetResources(List<Publication.Resource> resources, boolean
ResourceDescriptor descriptor = ResourceDescriptorFactory.fromPublicUrl(url);
verifyResourceType(descriptor);

if (resourceService.hasResource(descriptor) != exists) {
if (resource.getAction() != Publication.ResourceAction.ADD_IF_ABSENT && resourceService.hasResource(descriptor) != exists) {
String errorMessage = exists ? "Target resource does not exists: " + url : "Target resource exists: " + url;
throw new IllegalArgumentException(errorMessage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.epam.aidial.core.server;

import com.epam.aidial.core.server.util.ProxyUtil;
import com.fasterxml.jackson.databind.JsonNode;
import io.vertx.core.http.HttpMethod;
import org.junit.jupiter.api.Test;

Expand All @@ -26,6 +28,25 @@ class PublicationApiTest extends ResourceBaseTest {
}
""";

private static final String PUBLICATION_REQUEST_WITH_FILE = """
{
"name": "Publication name",
"targetFolder": "public/folder/",
"resources": [
{
"action": "ADD",
"sourceUrl": "conversations/%s/my/folder/conversation%s",
"targetUrl": "conversations/public/folder/conversation%s"
},
{
"action": "%s",
"sourceUrl": "files/%s/file",
"targetUrl": "files/public/folder/file"
}
]
}
""";

private static final String PUBLICATION_URL = """
{
"url": "publications/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/0123"
Expand Down Expand Up @@ -546,6 +567,113 @@ void testRejectUserDeletionRequestWorkflow() {
verify(response, 200);
}

@Test
void testPublicationWithAddIfAbsent() throws Exception {
Response response = upload(HttpMethod.PUT, "/v1/files/" + bucket + "/file", null, "text data");
verify(response, 200);
String conversationTemplate = """
{
"id": "%s",
"name": "display_name",
"model": {"id": "model_id"},
"prompt": "system prompt",
"temperature": 1,
"folderId": "%s",
"messages": [{
"role": "user",
"content": "what's the file?",
"custom_content": {
"attachments": [
{
"type": "text/markdown",
"title": "title",
"url": "%s"
}
]
}
}],
"selectedAddons": ["R", "T", "G"],
"assistantModelId": "assistantId",
"lastActivityDate": 4848683153
}
""";
JsonNode fileResponse = ProxyUtil.MAPPER.readTree(response.body());
String conversation = conversationTemplate.formatted("conversation_id", "folder1", fileResponse.get("url").asText());

response = resourceRequest(HttpMethod.PUT, "/my/folder/conversation1", conversation);
verify(response, 200);

response = operationRequest("/v1/ops/publication/create", PUBLICATION_REQUEST_WITH_FILE.formatted(bucket, "1", "1", "ADD_IF_ABSENT", bucket));
verify(response, 200);

response = operationRequest("/v1/ops/publication/approve", PUBLICATION_URL, "authorization", "admin");
verifyJsonNotExact(response, 200, """
{
"url" : "publications/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/0123",
"name" : "Publication name",
"targetFolder" : "public/folder/",
"status" : "APPROVED",
"createdAt" : 0,
"resources" : [ {
"action": "ADD",
"sourceUrl" : "conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/my/folder/conversation1",
"targetUrl" : "conversations/public/folder/conversation1",
"reviewUrl" : "conversations/2CZ9i2bcBACFts8JbBu3MdTHfU5imDZBmDVomBuDCkbhEstv1KXNzCiw693js8BLmo/conversation1"
},
{
"action": "ADD_IF_ABSENT",
"sourceUrl" : "files/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/file",
"targetUrl" : "files/public/folder/file",
"reviewUrl" : "files/2CZ9i2bcBACFts8JbBu3MdTHfU5imDZBmDVomBuDCkbhEstv1KXNzCiw693js8BLmo/file"
}
],
"resourceTypes" : [ "@ignore", "@ignore" ]
}
""");


response = resourceRequest(HttpMethod.PUT, "/my/folder/conversation2", conversation);
verify(response, 200);

response = operationRequest("/v1/ops/publication/create", PUBLICATION_REQUEST_WITH_FILE.formatted(bucket, "2", "2", "ADD_IF_ABSENT", bucket));
verify(response, 200);

String publication = """
{
"url": "publications/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/0124"
}
""";
response = operationRequest("/v1/ops/publication/approve", publication, "authorization", "admin");
verifyJsonNotExact(response, 200, """
{
"url" : "publications/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/0124",
"name" : "Publication name",
"targetFolder" : "public/folder/",
"status" : "APPROVED",
"createdAt" : 0,
"resources" : [ {
"action": "ADD",
"sourceUrl" : "conversations/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/my/folder/conversation2",
"targetUrl" : "conversations/public/folder/conversation2",
"reviewUrl" : "conversations/2CZ9i2bcBACFts8JbBu3MdTHfU5imDZBmDVomBuDCkbhQHmtD7fN295EFSG4HiW8Zi/conversation2"
},
{
"action": "ADD_IF_ABSENT",
"sourceUrl" : "files/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/file",
"targetUrl" : "files/public/folder/file",
"reviewUrl" : "files/2CZ9i2bcBACFts8JbBu3MdTHfU5imDZBmDVomBuDCkbhQHmtD7fN295EFSG4HiW8Zi/file"
}
],
"resourceTypes" : [ "@ignore", "@ignore" ]
}
""");

response = send(HttpMethod.GET, "/v1/conversations/public/folder/conversation2");
verifyJsonNotExact(response, 200, conversationTemplate.formatted("conversations/public/folder/conversation2",
"conversations/public/folder", "files/public/folder/file"));

}

@Test
void testPublicationApprove() {
Response response = resourceRequest(HttpMethod.PUT, "/my/folder/conversation", CONVERSATION_BODY_1);
Expand Down

0 comments on commit a12767a

Please sign in to comment.