added websocket interface for streamed keywords extraction
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
2026-07-01 10:48:09 +02:00
parent cfac12e218
commit 1fa755769b
2 changed files with 118 additions and 4 deletions
@@ -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({
@@ -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<any>;
private socketSubscription?: Subscription;
private readonly messageSubject$ = new Subject<WsEvent>(); // created once, stays for app lifetime
private connectionPromise: Promise<void> | 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<void> {
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<void> {
await this.connect();
if (this.socket$) {
this.socket$.next(req);
} else {
throw new Error('WebSocket not available');
}
}
getMessages(): Observable<WsEvent> {
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;
}
}