Skip to content

Commit

Permalink
fix: replace attachment links for replay and playback (#364)(#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Gadalov authored Jun 12, 2024
1 parent 8cd5af2 commit 1a5830a
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 8 deletions.
26 changes: 21 additions & 5 deletions src/main/java/com/epam/aidial/core/service/PublicationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class PublicationUtil {
* Replacing `id` and `folderId` - chat specific and may not be suitable for generic use-case.
* Typical use-case: replace attachment links in conversation after publishing.
*
* @param conversationBody - source conversation body
* @param targetResource - target resource link
* @param conversationBody - source conversation body
* @param targetResource - target resource link
* @param attachmentsMapping - attachments map (sourceUrl -> targetUrl) to replace
* @return conversation body after replacement
*/
Expand All @@ -31,8 +31,26 @@ public String replaceLinks(String conversationBody, ResourceDescription targetRe
}

JsonArray messages = conversation.getJsonArray("messages");
replaceAttachments(messages, attachmentsMapping);

JsonObject playback = conversation.getJsonObject("playback");
if (playback != null) {
JsonArray messagesStack = playback.getJsonArray("messagesStack");
replaceAttachments(messagesStack, attachmentsMapping);
}

JsonObject replay = conversation.getJsonObject("replay");
if (replay != null) {
JsonArray messagesStack = replay.getJsonArray("replayUserMessagesStack");
replaceAttachments(messagesStack, attachmentsMapping);
}

return conversation.toString();
}

private void replaceAttachments(JsonArray messages, Map<String, String> attachmentsMapping) {
if (messages == null || messages.isEmpty()) {
return conversation.toString();
return;
}

for (int i = 0; i < messages.size(); i++) {
Expand Down Expand Up @@ -62,8 +80,6 @@ public String replaceLinks(String conversationBody, ResourceDescription targetRe
}
}
}

return conversation.toString();
}

private JsonObject replaceConversationIdentity(String conversationBody, ResourceDescription targetResource) {
Expand Down
133 changes: 130 additions & 3 deletions src/test/java/com/epam/aidial/core/service/PublicationUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,57 @@ void testAttachmentLinksReplacement() {
],
"selectedAddons": ["R", "T", "G"],
"assistantModelId": "assistantId",
"lastActivityDate": 4848683153
"lastActivityDate": 4848683153,
"playback": {
"messagesStack": [
{
"custom_content": {
"attachments": [
{
"type": "application/octet-stream",
"title": "LICENSE",
"url": "files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/LICENSE"
},
{
"type": "binary/octet-stream",
"title": "Dockerfile",
"url": "files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/Dockerfile"
},
{
"type": "application/vnd.dial.metadata+json",
"title": ".dockerignore",
"url": "metadata/files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/"
}
]
}
}
]
},
"replay": {
"replayUserMessagesStack": [
{
"custom_content": {
"attachments": [
{
"type": "application/octet-stream",
"title": "LICENSE",
"url": "files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/LICENSE"
},
{
"type": "binary/octet-stream",
"title": "Dockerfile",
"url": "files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/Dockerfile"
},
{
"type": "application/vnd.dial.metadata+json",
"title": ".dockerignore",
"url": "metadata/files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/"
}
]
}
}
]
}
}
""";

Expand Down Expand Up @@ -125,7 +175,45 @@ void testAttachmentLinksReplacement() {
],
"selectedAddons": ["R", "T", "G"],
"assistantModelId": "assistantId",
"lastActivityDate": 4848683153
"lastActivityDate": 4848683153,
"playback" : {
"messagesStack" : [ {
"custom_content" : {
"attachments" : [ {
"type" : "application/octet-stream",
"title" : "LICENSE",
"url" : "files/public/License"
}, {
"type" : "binary/octet-stream",
"title" : "Dockerfile",
"url" : "files/public/Dockerfile"
}, {
"type" : "application/vnd.dial.metadata+json",
"title" : ".dockerignore",
"url" : "metadata/files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/"
} ]
}
} ]
},
"replay" : {
"replayUserMessagesStack" : [ {
"custom_content" : {
"attachments" : [ {
"type" : "application/octet-stream",
"title" : "LICENSE",
"url" : "files/public/License"
}, {
"type" : "binary/octet-stream",
"title" : "Dockerfile",
"url" : "files/public/Dockerfile"
}, {
"type" : "application/vnd.dial.metadata+json",
"title" : ".dockerignore",
"url" : "metadata/files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/"
} ]
}
} ]
}
}
""", PublicationUtil.replaceLinks(conversationBody, targetResource, Map.of(
"files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/LICENSE", "files/public/License",
Expand Down Expand Up @@ -166,7 +254,46 @@ void testAttachmentLinksReplacement() {
],
"selectedAddons": ["R", "T", "G"],
"assistantModelId": "assistantId",
"lastActivityDate": 4848683153
"lastActivityDate": 4848683153,
"playback" : {
"messagesStack" : [ {
"custom_content" : {
"attachments" : [ {
"type" : "application/octet-stream",
"title" : "LICENSE",
"url" : "files/public/License"
}, {
"type" : "binary/octet-stream",
"title" : "Dockerfile",
"url" : "files/public/Dockerfile"
}, {
"type" : "application/vnd.dial.metadata+json",
"title" : ".dockerignore",
"url" : "metadata/files/public/attachments/"
} ]
}
} ]
},
"replay" : {
"replayUserMessagesStack" : [ {
"custom_content" : {
"attachments" : [ {
"type" : "application/octet-stream",
"title" : "LICENSE",
"url" : "files/public/License"
}, {
"type" : "binary/octet-stream",
"title" : "Dockerfile",
"url" : "files/public/Dockerfile"
}, {
"type" : "application/vnd.dial.metadata+json",
"title" : ".dockerignore",
"url" : "metadata/files/public/attachments/"
} ]
}
} ]
}
}
}
""", PublicationUtil.replaceLinks(conversationBody, targetResource, Map.of(
"files/7G9WZNcoY26Vy9D7bEgbv6zqbJGfyDp9KZyEbJR4XMZt/b1/LICENSE", "files/public/License",
Expand Down

0 comments on commit 1a5830a

Please sign in to comment.