mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-06-25 17:12:16 +00:00
DEV: cleanup diff streaming (#1370)
This simplifies some of the internal logic and ensures it is a bit more robust
This commit is contained in:
parent
e264572597
commit
6781ecd02e
@ -18,6 +18,8 @@ import DiffStreamer from "../../lib/diff-streamer";
|
|||||||
import SmoothStreamer from "../../lib/smooth-streamer";
|
import SmoothStreamer from "../../lib/smooth-streamer";
|
||||||
import AiIndicatorWave from "../ai-indicator-wave";
|
import AiIndicatorWave from "../ai-indicator-wave";
|
||||||
|
|
||||||
|
const CHANNEL = "/discourse-ai/ai-helper/stream_composer_suggestion";
|
||||||
|
|
||||||
export default class ModalDiffModal extends Component {
|
export default class ModalDiffModal extends Component {
|
||||||
@service currentUser;
|
@service currentUser;
|
||||||
@service messageBus;
|
@service messageBus;
|
||||||
@ -49,12 +51,9 @@ export default class ModalDiffModal extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get isStreaming() {
|
get isStreaming() {
|
||||||
// diffStreamer stops "streaming" when it is finished with a chunk
|
// diffStreamer stops Streaming when it is finished with a chunk, looking at isDone is safe
|
||||||
return (
|
// it starts off not done
|
||||||
this.diffStreamer.isStreaming ||
|
return !this.diffStreamer.isDone || this.smoothStreamer.isStreaming;
|
||||||
!this.diffStreamer.isDone ||
|
|
||||||
this.smoothStreamer.isStreaming
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get primaryBtnLabel() {
|
get primaryBtnLabel() {
|
||||||
@ -69,25 +68,23 @@ export default class ModalDiffModal extends Component {
|
|||||||
|
|
||||||
@bind
|
@bind
|
||||||
subscribe() {
|
subscribe() {
|
||||||
const channel = "/discourse-ai/ai-helper/stream_composer_suggestion";
|
this.messageBus.subscribe(CHANNEL, this.updateResult);
|
||||||
this.messageBus.subscribe(channel, this.updateResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
unsubscribe() {
|
cleanup() {
|
||||||
const channel = "/discourse-ai/ai-helper/stream_composer_suggestion";
|
// stop all callbacks so it does not end up streaming pointlessly
|
||||||
this.messageBus.unsubscribe(channel, this.updateResult);
|
this.smoothStreamer.resetStreaming();
|
||||||
|
this.diffStreamer.reset();
|
||||||
|
this.messageBus.unsubscribe(CHANNEL, this.updateResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async updateResult(result) {
|
updateResult(result) {
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
|
|
||||||
if (result.done) {
|
if (result.done) {
|
||||||
this.finalResult = result.result;
|
this.finalResult = result.result;
|
||||||
}
|
|
||||||
|
|
||||||
if (result.done) {
|
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +151,7 @@ export default class ModalDiffModal extends Component {
|
|||||||
<:body>
|
<:body>
|
||||||
<div
|
<div
|
||||||
{{didInsert this.subscribe}}
|
{{didInsert this.subscribe}}
|
||||||
{{willDestroy this.unsubscribe}}
|
{{willDestroy this.cleanup}}
|
||||||
class="text-preview"
|
class="text-preview"
|
||||||
>
|
>
|
||||||
{{#if this.loading}}
|
{{#if this.loading}}
|
||||||
|
@ -100,6 +100,7 @@ export default class DiffStreamer {
|
|||||||
this.currentWordIndex = 0;
|
this.currentWordIndex = 0;
|
||||||
this.currentCharIndex = 0;
|
this.currentCharIndex = 0;
|
||||||
this.isStreaming = false;
|
this.isStreaming = false;
|
||||||
|
this.isDone = false;
|
||||||
if (this.typingTimer) {
|
if (this.typingTimer) {
|
||||||
cancel(this.typingTimer);
|
cancel(this.typingTimer);
|
||||||
this.typingTimer = null;
|
this.typingTimer = null;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user