Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions assets/javascripts/discourse/components/modal/diff-modal.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import concatClass from "discourse/helpers/concat-class";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { bind } from "discourse/lib/decorators";
import { escapeExpression } from "discourse/lib/utilities";
import { i18n } from "discourse-i18n";
import DiffStreamer from "../../lib/diff-streamer";
import SmoothStreamer from "../../lib/smooth-streamer";
import AiIndicatorWave from "../ai-indicator-wave";

export default class ModalDiffModal extends Component {
@service currentUser;
@service siteSettings;
@service messageBus;

@tracked loading = false;
@tracked finalResult = "";
@tracked diffStreamer = new DiffStreamer(this.args.model.selectedText);
@tracked selectedText = escapeExpression(this.args.model.selectedText);
@tracked diffStreamer = new DiffStreamer(this.selectedText);
@tracked suggestion = "";
@tracked
smoothStreamer = new SmoothStreamer(
Expand All @@ -36,6 +39,16 @@ export default class ModalDiffModal extends Component {
this.suggestChanges();
}

get diffResult() {
if (this.diffStreamer.diff?.length > 0) {
return this.diffStreamer.diff;
}

// Prevents flash by showing the
// original text when the diff is empty
return this.selectedText;
}

get isStreaming() {
return this.diffStreamer.isStreaming || this.smoothStreamer.isStreaming;
}
Expand Down Expand Up @@ -93,7 +106,7 @@ export default class ModalDiffModal extends Component {
data: {
location: "composer",
mode: this.args.model.mode,
text: this.args.model.selectedText,
text: this.selectedText,
custom_prompt: this.args.model.customPromptValue,
force_default_locale: true,
},
Expand All @@ -109,7 +122,7 @@ export default class ModalDiffModal extends Component {

if (this.suggestion) {
this.args.model.toolbarEvent.replaceText(
this.args.model.selectedText,
this.selectedText,
this.suggestion
);
}
Expand All @@ -119,21 +132,23 @@ export default class ModalDiffModal extends Component {
? this.finalResult
: this.diffStreamer.suggestion;
if (this.args.model.showResultAsDiff && finalResult) {
this.args.model.toolbarEvent.replaceText(
this.args.model.selectedText,
finalResult
);
this.args.model.toolbarEvent.replaceText(this.selectedText, finalResult);
}
}

<template>
{{log this.prefersRichEditor}}
<DModal
class="composer-ai-helper-modal"
@title={{i18n "discourse_ai.ai_helper.context_menu.changes"}}
@closeModal={{@closeModal}}
>
<:body>
<div {{didInsert this.subscribe}} {{willDestroy this.unsubscribe}}>
<div
{{didInsert this.subscribe}}
{{willDestroy this.unsubscribe}}
class="text-preview"
>
{{#if this.loading}}
<div class="composer-ai-helper-modal__loading">
{{[email protected]~}}
Expand All @@ -149,9 +164,7 @@ export default class ModalDiffModal extends Component {
}}
>
{{~#if @model.showResultAsDiff~}}
<span class="diff-inner">{{htmlSafe
this.diffStreamer.diff
}}</span>
<span class="diff-inner">{{htmlSafe this.diffResult}}</span>
{{else}}
{{#if this.smoothStreamer.isStreaming}}
<CookText
Expand Down
2 changes: 1 addition & 1 deletion assets/javascripts/discourse/lib/diff-streamer.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class DiffStreamer {
@tracked isStreaming = false;
@tracked words = [];
@tracked lastResultText = "";
@tracked diff = "";
@tracked diff = this.selectedText;
@tracked suggestion = "";
@tracked isDone = false;
@tracked isThinking = false;
Expand Down
3 changes: 3 additions & 0 deletions assets/stylesheets/modules/ai-helper/common/ai-helper.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.composer-ai-helper-modal {
.text-preview,
.inline-diff {
font-family: var(--d-font-family--monospace);
font-variant-ligatures: none;

ins {
background-color: var(--success-low);
text-decoration: none;
Expand Down
Loading