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 all commits
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
25 changes: 17 additions & 8 deletions assets/javascripts/discourse/components/modal/diff-modal.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export default class ModalDiffModal extends Component {

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

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

get isStreaming() {
return this.diffStreamer.isStreaming || this.smoothStreamer.isStreaming;
// diffStreamer stops "streaming" when it is finished with a chunk
return (
this.diffStreamer.isStreaming ||
!this.diffStreamer.isDone ||
Comment on lines +54 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like that when you are done you should not be streaming anymore. So setting isDone to true should also set isStreaming to false, otherwise we will plague the code with this double check.

this.smoothStreamer.isStreaming
);
}

get primaryBtnLabel() {
Expand All @@ -71,7 +76,7 @@ export default class ModalDiffModal extends Component {
@bind
unsubscribe() {
const channel = "/discourse-ai/ai-helper/stream_composer_suggestion";
this.messageBus.subscribe(channel, this.updateResult);
this.messageBus.unsubscribe(channel, this.updateResult);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow, nice catch

}

@action
Expand Down Expand Up @@ -105,7 +110,7 @@ export default class ModalDiffModal extends Component {
data: {
location: "composer",
mode: this.args.model.mode,
text: this.selectedText,
text: this.args.model.selectedText,
custom_prompt: this.args.model.customPromptValue,
force_default_locale: true,
client_id: this.messageBus.clientId,
Expand All @@ -122,7 +127,7 @@ export default class ModalDiffModal extends Component {

if (this.suggestion) {
this.args.model.toolbarEvent.replaceText(
this.selectedText,
this.args.model.selectedText,
this.suggestion
);
}
Expand All @@ -131,8 +136,12 @@ export default class ModalDiffModal extends Component {
this.finalResult?.length > 0
? this.finalResult
: this.diffStreamer.suggestion;

if (this.args.model.showResultAsDiff && finalResult) {
this.args.model.toolbarEvent.replaceText(this.selectedText, finalResult);
this.args.model.toolbarEvent.replaceText(
this.args.model.selectedText,
finalResult
);
}
}

Expand Down
14 changes: 11 additions & 3 deletions assets/javascripts/discourse/lib/diff-streamer.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { tracked } from "@glimmer/tracking";
import { cancel, later } from "@ember/runloop";
import loadJSDiff from "discourse/lib/load-js-diff";
import { parseAsync } from "discourse/lib/text";
import { escapeExpression } from "discourse/lib/utilities";

const DEFAULT_CHAR_TYPING_DELAY = 30;
const DEFAULT_CHAR_TYPING_DELAY = 10;
const STREAMING_DIFF_TRUNCATE_THRESHOLD = 0.1;
const STREAMING_DIFF_TRUNCATE_BUFFER = 10;

Expand Down Expand Up @@ -51,7 +52,7 @@ export default class DiffStreamer {

const originalDiff = this.jsDiff.diffWordsWithSpace(
this.selectedText,
this.suggestion
newText
);
this.diff = this.#formatDiffWithTags(originalDiff, false);
return;
Expand Down Expand Up @@ -172,6 +173,10 @@ export default class DiffStreamer {
}

async #streamNextChar() {
if (!this.isStreaming || this.isDone) {
return;
}

if (this.currentWordIndex < this.words.length) {
const currentToken = this.words[this.currentWordIndex];

Expand Down Expand Up @@ -252,6 +257,7 @@ export default class DiffStreamer {
return `<span>${text}</span>`;
}

// returns an HTML safe diff (escaping all internals)
#formatDiffWithTags(diffArray, highlightLastWord = true) {
const wordsWithType = [];
const output = [];
Expand Down Expand Up @@ -280,7 +286,8 @@ export default class DiffStreamer {
}

for (let i = 0; i <= lastWordIndex; i++) {
const { text, type } = wordsWithType[i];
let { text, type } = wordsWithType[i];
text = escapeExpression(text);

if (/^\s+$/.test(text)) {
output.push(text);
Expand Down Expand Up @@ -310,6 +317,7 @@ export default class DiffStreamer {
i++;
}

chunkText = escapeExpression(chunkText);
output.push(this.#wrapChunk(chunkText, chunkType));
}
}
Expand Down
2 changes: 1 addition & 1 deletion assets/stylesheets/modules/ai-helper/common/ai-helper.scss
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@

.desktop-view & {
// a little extra space for extra narrow desktop view
@media screen and (width <= 675px) {
@media screen and (max-width: 675px) {
span {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably update this to use our viewport.until(md) mixin, but this is probably fine for now.

display: none;
}
Expand Down
Loading