Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 61ef193

Browse files
authored
DEV: Follow-up small fixes to AI composer helper (#1361)
**This update includes a variety of small fixes to the AI composer helper:** - ensures there is no flash of no text when diff modal is triggered - escapes selected text prior to diff streaming - uses monospace font in diff modal since text rendered is raw markdown
1 parent 11e90f5 commit 61ef193

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

assets/javascripts/discourse/components/modal/diff-modal.gjs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import concatClass from "discourse/helpers/concat-class";
1212
import { ajax } from "discourse/lib/ajax";
1313
import { popupAjaxError } from "discourse/lib/ajax-error";
1414
import { bind } from "discourse/lib/decorators";
15+
import { escapeExpression } from "discourse/lib/utilities";
1516
import { i18n } from "discourse-i18n";
1617
import DiffStreamer from "../../lib/diff-streamer";
1718
import SmoothStreamer from "../../lib/smooth-streamer";
@@ -23,7 +24,8 @@ export default class ModalDiffModal extends Component {
2324

2425
@tracked loading = false;
2526
@tracked finalResult = "";
26-
@tracked diffStreamer = new DiffStreamer(this.args.model.selectedText);
27+
@tracked selectedText = escapeExpression(this.args.model.selectedText);
28+
@tracked diffStreamer = new DiffStreamer(this.selectedText);
2729
@tracked suggestion = "";
2830
@tracked
2931
smoothStreamer = new SmoothStreamer(
@@ -36,6 +38,16 @@ export default class ModalDiffModal extends Component {
3638
this.suggestChanges();
3739
}
3840

41+
get diffResult() {
42+
if (this.diffStreamer.diff?.length > 0) {
43+
return this.diffStreamer.diff;
44+
}
45+
46+
// Prevents flash by showing the
47+
// original text when the diff is empty
48+
return this.selectedText;
49+
}
50+
3951
get isStreaming() {
4052
return this.diffStreamer.isStreaming || this.smoothStreamer.isStreaming;
4153
}
@@ -93,7 +105,7 @@ export default class ModalDiffModal extends Component {
93105
data: {
94106
location: "composer",
95107
mode: this.args.model.mode,
96-
text: this.args.model.selectedText,
108+
text: this.selectedText,
97109
custom_prompt: this.args.model.customPromptValue,
98110
force_default_locale: true,
99111
},
@@ -109,7 +121,7 @@ export default class ModalDiffModal extends Component {
109121

110122
if (this.suggestion) {
111123
this.args.model.toolbarEvent.replaceText(
112-
this.args.model.selectedText,
124+
this.selectedText,
113125
this.suggestion
114126
);
115127
}
@@ -119,10 +131,7 @@ export default class ModalDiffModal extends Component {
119131
? this.finalResult
120132
: this.diffStreamer.suggestion;
121133
if (this.args.model.showResultAsDiff && finalResult) {
122-
this.args.model.toolbarEvent.replaceText(
123-
this.args.model.selectedText,
124-
finalResult
125-
);
134+
this.args.model.toolbarEvent.replaceText(this.selectedText, finalResult);
126135
}
127136
}
128137

@@ -133,7 +142,11 @@ export default class ModalDiffModal extends Component {
133142
@closeModal={{@closeModal}}
134143
>
135144
<:body>
136-
<div {{didInsert this.subscribe}} {{willDestroy this.unsubscribe}}>
145+
<div
146+
{{didInsert this.subscribe}}
147+
{{willDestroy this.unsubscribe}}
148+
class="text-preview"
149+
>
137150
{{#if this.loading}}
138151
<div class="composer-ai-helper-modal__loading">
139152
{{~@model.selectedText~}}
@@ -149,9 +162,7 @@ export default class ModalDiffModal extends Component {
149162
}}
150163
>
151164
{{~#if @model.showResultAsDiff~}}
152-
<span class="diff-inner">{{htmlSafe
153-
this.diffStreamer.diff
154-
}}</span>
165+
<span class="diff-inner">{{htmlSafe this.diffResult}}</span>
155166
{{else}}
156167
{{#if this.smoothStreamer.isStreaming}}
157168
<CookText

assets/javascripts/discourse/lib/diff-streamer.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class DiffStreamer {
1111
@tracked isStreaming = false;
1212
@tracked words = [];
1313
@tracked lastResultText = "";
14-
@tracked diff = "";
14+
@tracked diff = this.selectedText;
1515
@tracked suggestion = "";
1616
@tracked isDone = false;
1717
@tracked isThinking = false;

assets/stylesheets/modules/ai-helper/common/ai-helper.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
.composer-ai-helper-modal {
22
.text-preview,
33
.inline-diff {
4+
font-family: var(--d-font-family--monospace);
5+
font-variant-ligatures: none;
6+
47
ins {
58
background-color: var(--success-low);
69
text-decoration: none;

0 commit comments

Comments
 (0)