Skip to content

Commit

Permalink
fix the way edit message works (#6969)
Browse files Browse the repository at this point in the history
  • Loading branch information
julianjelfs authored Dec 4, 2024
1 parent 4a572a3 commit 9fe4d43
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
28 changes: 14 additions & 14 deletions frontend/openchat-client/src/openchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ import type {
BotCommandInstance,
InternalBotCommandInstance,
ExternalBotCommandInstance,
CaptionedContent,
} from "openchat-shared";
import {
AuthProvider,
Expand Down Expand Up @@ -447,6 +448,8 @@ import {
deletedUser,
OPENCHAT_BOT_USER_ID,
AIRDROP_BOT_USER_ID,
isEditableContent,
isCaptionedContent,
} from "openchat-shared";
import { failedMessagesStore } from "./stores/failedMessages";
import { diamondDurationToMs } from "./stores/diamond";
Expand Down Expand Up @@ -3883,10 +3886,10 @@ export class OpenChat extends OpenChatAgentWorker {

private getMessageContent(
text: string | undefined,
attachment: AttachmentContent | undefined,
captioned: CaptionedContent | undefined,
): MessageContent {
return attachment
? { ...attachment, caption: text }
return captioned
? { ...captioned, caption: text }
: ({
kind: "text_content",
text: text ?? "",
Expand Down Expand Up @@ -4022,10 +4025,16 @@ export class OpenChat extends OpenChatAgentWorker {
textContent = disableLinksInText(textContent, disabledLinks);
}

const captioned =
attachment ??
(isCaptionedContent(editingEvent.event.content)
? editingEvent.event.content
: undefined);

const msg = {
...editingEvent.event,
edited: true,
content: this.getMessageContent(textContent ?? undefined, attachment),
content: this.getMessageContent(textContent ?? undefined, captioned),
};
localMessageUpdates.markContentEdited(msg.messageId, msg.content);
draftMessagesStore.delete(messageContext);
Expand Down Expand Up @@ -7692,17 +7701,8 @@ export class OpenChat extends OpenChatAgentWorker {
}
}

private contentTypesSupportingEdit = new Set([
"file_content",
"text_content",
"image_content",
"video_content",
"audio_content",
"giphy_content",
]);

contentTypeSupportsEdit(contentType: MessageContent["kind"]): boolean {
return this.contentTypesSupportingEdit.has(contentType);
return isEditableContent(contentType);
}

getBots(includeTestBots: boolean = false) {
Expand Down
22 changes: 22 additions & 0 deletions frontend/openchat-shared/src/domain/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,28 @@ export function isAttachmentContent(content: MessageContent): content is Attachm
}
}

export type EditableContent =
| FileContent
| TextContent
| ImageContent
| VideoContent
| AudioContent
| GiphyContent;

export function isEditableContent(kind: MessageContent["kind"]): kind is EditableContent["kind"] {
switch (kind) {
case "file_content":
case "text_content":
case "image_content":
case "video_content":
case "audio_content":
case "giphy_content":
return true;
default:
return false;
}
}

export function isCaptionedContent(content: MessageContent): content is CaptionedContent {
switch (content.kind) {
case "image_content":
Expand Down
12 changes: 12 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dotenv-cli": "^7.2.1",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier-plugin-svelte": "^3.3.2",
"rollup": "^4.22.4",
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-cleaner": "^1.0.0",
Expand Down

0 comments on commit 9fe4d43

Please sign in to comment.