Skip to content

Commit

Permalink
chore: abort inference stream implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Nov 22, 2023
1 parent b5717e0 commit 664806e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion plugins/inference-plugin/src/helpers/sse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Observable } from "rxjs";
* @param recentMessages - An array of recent messages to use as context for the inference.
* @returns An Observable that emits the generated response as a string.
*/
export function requestInference(recentMessages: any[]): Observable<string> {
export function requestInference(recentMessages: any[], controller?: AbortController): Observable<string> {
return new Observable((subscriber) => {
const requestBody = JSON.stringify({
messages: recentMessages,
Expand All @@ -20,6 +20,7 @@ export function requestInference(recentMessages: any[]): Observable<string> {
"Access-Control-Allow-Origin": "*",
},
body: requestBody,
signal: controller?.signal
})
.then(async (response) => {
const stream = response.body;
Expand Down
7 changes: 5 additions & 2 deletions plugins/inference-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { fs } from "@janhq/core";
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
*/
export default class JanInferencePlugin implements InferencePlugin {
controller = new AbortController();
/**
* Returns the type of the plugin.
* @returns {PluginType} The type of the plugin.
Expand Down Expand Up @@ -75,7 +76,7 @@ export default class JanInferencePlugin implements InferencePlugin {
* @returns {Promise<void>} A promise that resolves when the streaming is stopped.
*/
async stopInference(): Promise<void> {
// TODO: Implementation
this.controller.abort();
}

/**
Expand Down Expand Up @@ -121,7 +122,9 @@ export default class JanInferencePlugin implements InferencePlugin {
};
events.emit(EventName.OnNewMessageResponse, message);

requestInference(data.messages).subscribe({
this.controller = new AbortController();

requestInference(data.messages, this.controller).subscribe({
next: (content) => {
message.content = content;
events.emit(EventName.OnMessageResponseUpdate, message);
Expand Down

0 comments on commit 664806e

Please sign in to comment.