From 1fa755769bc274a73602f2aa7732ed426468b3cb Mon Sep 17 00:00:00 2001 From: genja Date: Wed, 1 Jul 2026 10:48:09 +0200 Subject: [PATCH] added websocket interface for streamed keywords extraction --- .../keywords-search.component.ts | 5 +- .../keywords-websocket.service.ts | 117 ++++++++++++++++++ 2 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 webapp/src/app/modules/keywords-search/keywords-websocket.service.ts diff --git a/webapp/src/app/modules/keywords-search/keywords-search.component.ts b/webapp/src/app/modules/keywords-search/keywords-search.component.ts index 0af2ae1d..1b7d6979 100644 --- a/webapp/src/app/modules/keywords-search/keywords-search.component.ts +++ b/webapp/src/app/modules/keywords-search/keywords-search.component.ts @@ -17,10 +17,7 @@ import { ExtractedKeywords } from '../../api/model/extractedKeywords'; import { ExtractedKeywordsResponse } from '../../api/model/extractedKeywordsResponse'; import { DestroyRef } from '@angular/core'; import { MatProgressBarModule } from '@angular/material/progress-bar'; -import { - KeywordWebSocketService, - WsEvent, -} from '../../shared/services/websocket.service'; +import { KeywordWebSocketService, WsEvent } from './keywords-websocket.service'; type InputMode = 'url' | 'text' | 'keywords'; @Component({ diff --git a/webapp/src/app/modules/keywords-search/keywords-websocket.service.ts b/webapp/src/app/modules/keywords-search/keywords-websocket.service.ts new file mode 100644 index 00000000..b1989c5c --- /dev/null +++ b/webapp/src/app/modules/keywords-search/keywords-websocket.service.ts @@ -0,0 +1,117 @@ +// keyword-websocket.service.ts + +import { Injectable, Inject, Optional } from '@angular/core'; +import { webSocket, WebSocketSubject } from 'rxjs/webSocket'; +import { Observable, Subject, Subscription } from 'rxjs'; +import { KeywordsRequest } from '../../api/model/keywordsRequest'; +import { Configuration } from '../../api/configuration'; +import { BaseService } from '../../api/api.base.service'; +import { BASE_PATH } from '../../api/variables'; +import { ExtractedKeywords } from '../../api/model/extractedKeywords'; +import { ExtractedKeywordsResponse } from '../../api/model/extractedKeywordsResponse'; + +export interface WsEvent { + type: string; + step?: string | null; + message?: string | null; + result?: ExtractedKeywordsResponse; + content?: string | null; + level?: string | null; + keywords?: ExtractedKeywords; + duration?: number | null; +} + +@Injectable({ providedIn: 'root' }) +export class KeywordWebSocketService extends BaseService { + private socket$?: WebSocketSubject; + private socketSubscription?: Subscription; + private readonly messageSubject$ = new Subject(); // created once, stays for app lifetime + private connectionPromise: Promise | null = null; + + constructor( + @Optional() @Inject(BASE_PATH) basePath: string | string[], + @Optional() configuration?: Configuration, + ) { + super(basePath, configuration); + this.url = + `${this.configuration?.basePath ?? 'http://localhost:8000'}/api/inse-service/keywords/extract/ws`.replace( + /^http/, + 'ws', + ); + } + private readonly url: string; + + connect(): Promise { + if (this.socket$ && this.socket$.closed === false) { + return Promise.resolve(); + } + if (this.connectionPromise) { + return this.connectionPromise; + } + + this.closeSocket(); + + // no longer recreate messageSubject$ here + + this.connectionPromise = new Promise((resolve, reject) => { + this.socket$ = webSocket({ + url: this.url, + deserializer: (msg) => JSON.parse(msg.data), + serializer: (value) => JSON.stringify(value), + openObserver: { + next: () => resolve(), + }, + closeObserver: { + next: () => { + this.connectionPromise = null; + }, + }, + }); + + this.socketSubscription = this.socket$.subscribe({ + next: (event: WsEvent) => this.messageSubject$.next(event), + error: (err) => { + // don't error/complete the shared subject — just reject this connection attempt + console.error('WebSocket error', err); + reject(err); + this.connectionPromise = null; + }, + complete: () => { + this.connectionPromise = null; + }, + }); + }); + + return this.connectionPromise; + } + + async sendRequest(req: KeywordsRequest): Promise { + await this.connect(); + if (this.socket$) { + this.socket$.next(req); + } else { + throw new Error('WebSocket not available'); + } + } + + getMessages(): Observable { + return this.messageSubject$.asObservable(); + } + + close(): void { + this.closeSocket(); + // don't complete messageSubject$ — it must survive across connections + } + + private closeSocket(): void { + if (this.socketSubscription) { + this.socketSubscription.unsubscribe(); + this.socketSubscription = undefined; + } + if (this.socket$) { + this.socket$.complete(); + this.socket$ = undefined; + } + this.connectionPromise = null; + } +}