mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-31 10:37:12 -04:00 
			
		
		
		
	Try rewriting with httpclient
This commit is contained in:
		
							parent
							
								
									c809a65571
								
							
						
					
					
						commit
						0052f21cea
					
				| @ -5,18 +5,24 @@ import { | |||||||
|   HttpRequest, |   HttpRequest, | ||||||
| } from '@angular/common/http' | } from '@angular/common/http' | ||||||
| import { inject, Injectable } from '@angular/core' | import { inject, Injectable } from '@angular/core' | ||||||
|  | import { Meta } from '@angular/platform-browser' | ||||||
|  | import { CookieService } from 'ngx-cookie-service' | ||||||
| import { Observable } from 'rxjs' | import { Observable } from 'rxjs' | ||||||
| import { CsrfService } from '../services/csrf.service' |  | ||||||
| 
 | 
 | ||||||
| @Injectable() | @Injectable() | ||||||
| export class CsrfInterceptor implements HttpInterceptor { | export class CsrfInterceptor implements HttpInterceptor { | ||||||
|   private csrfService = inject(CsrfService) |   private cookieService: CookieService = inject(CookieService) | ||||||
|  |   private meta: Meta = inject(Meta) | ||||||
| 
 | 
 | ||||||
|   intercept( |   intercept( | ||||||
|     request: HttpRequest<unknown>, |     request: HttpRequest<unknown>, | ||||||
|     next: HttpHandler |     next: HttpHandler | ||||||
|   ): Observable<HttpEvent<unknown>> { |   ): Observable<HttpEvent<unknown>> { | ||||||
|     const csrfToken = this.csrfService.getToken() |     let prefix = '' | ||||||
|  |     if (this.meta.getTag('name=cookie_prefix')) { | ||||||
|  |       prefix = this.meta.getTag('name=cookie_prefix').content | ||||||
|  |     } | ||||||
|  |     let csrfToken = this.cookieService.get(`${prefix}csrftoken`) | ||||||
|     if (csrfToken) { |     if (csrfToken) { | ||||||
|       request = request.clone({ |       request = request.clone({ | ||||||
|         setHeaders: { |         setHeaders: { | ||||||
|  | |||||||
| @ -1,7 +1,11 @@ | |||||||
|  | import { | ||||||
|  |   HttpClient, | ||||||
|  |   HttpDownloadProgressEvent, | ||||||
|  |   HttpEventType, | ||||||
|  | } from '@angular/common/http' | ||||||
| import { Injectable } from '@angular/core' | import { Injectable } from '@angular/core' | ||||||
| import { Observable } from 'rxjs' | import { filter, map, Observable } from 'rxjs' | ||||||
| import { environment } from 'src/environments/environment' | import { environment } from 'src/environments/environment' | ||||||
| import { CsrfService } from './csrf.service' |  | ||||||
| 
 | 
 | ||||||
| export interface ChatMessage { | export interface ChatMessage { | ||||||
|   role: 'user' | 'assistant' |   role: 'user' | 'assistant' | ||||||
| @ -13,48 +17,31 @@ export interface ChatMessage { | |||||||
|   providedIn: 'root', |   providedIn: 'root', | ||||||
| }) | }) | ||||||
| export class ChatService { | export class ChatService { | ||||||
|   constructor(private csrfService: CsrfService) {} |   constructor(private http: HttpClient) {} | ||||||
| 
 | 
 | ||||||
|   streamChat(documentId: number, prompt: string): Observable<string> { |   streamChat(documentId: number, prompt: string): Observable<string> { | ||||||
|     return new Observable<string>((observer) => { |     // use httpclient as we have withFetch
 | ||||||
|       const url = `${environment.apiBaseUrl}documents/chat/` |     return this.http | ||||||
|       const xhr = new XMLHttpRequest() |       .post( | ||||||
|       let lastLength = 0 |         `${environment.apiBaseUrl}documents/chat/`, | ||||||
| 
 |         { | ||||||
|       xhr.open('POST', url) |           document_id: documentId, | ||||||
|       xhr.setRequestHeader('Content-Type', 'application/json') |           q: prompt, | ||||||
| 
 |         }, | ||||||
|       xhr.withCredentials = true |         { | ||||||
|       let csrfToken = this.csrfService.getToken() |           observe: 'events', | ||||||
|       if (csrfToken) { |           reportProgress: true, | ||||||
|         xhr.setRequestHeader('X-CSRFToken', csrfToken) |           responseType: 'text', | ||||||
|       } |           withCredentials: true, | ||||||
| 
 |         } | ||||||
|       xhr.onreadystatechange = () => { |       ) | ||||||
|         if (xhr.readyState === 3 || xhr.readyState === 4) { |       .pipe( | ||||||
|           const partial = xhr.responseText.slice(lastLength) |         map((event) => { | ||||||
|           lastLength = xhr.responseText.length |           if (event.type === HttpEventType.DownloadProgress) { | ||||||
| 
 |             return (event as HttpDownloadProgressEvent).partialText! | ||||||
|           if (partial) { |  | ||||||
|             observer.next(partial) |  | ||||||
|           } |           } | ||||||
|         } |         }), | ||||||
| 
 |         filter((chunk) => !!chunk) | ||||||
|         if (xhr.readyState === 4) { |       ) | ||||||
|           observer.complete() |  | ||||||
|         } |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       xhr.onerror = () => { |  | ||||||
|         observer.error(new Error('Streaming request failed.')) |  | ||||||
|       } |  | ||||||
| 
 |  | ||||||
|       const body = JSON.stringify({ |  | ||||||
|         document_id: documentId, |  | ||||||
|         q: prompt, |  | ||||||
|       }) |  | ||||||
| 
 |  | ||||||
|       xhr.send(body) |  | ||||||
|     }) |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,23 +0,0 @@ | |||||||
| import { Injectable } from '@angular/core' |  | ||||||
| import { Meta } from '@angular/platform-browser' |  | ||||||
| import { CookieService } from 'ngx-cookie-service' // Assuming you're using this
 |  | ||||||
| 
 |  | ||||||
| @Injectable({ providedIn: 'root' }) |  | ||||||
| export class CsrfService { |  | ||||||
|   constructor( |  | ||||||
|     private cookieService: CookieService, |  | ||||||
|     private meta: Meta |  | ||||||
|   ) {} |  | ||||||
| 
 |  | ||||||
|   public getCookiePrefix(): string { |  | ||||||
|     let prefix = '' |  | ||||||
|     if (this.meta.getTag('name=cookie_prefix')) { |  | ||||||
|       prefix = this.meta.getTag('name=cookie_prefix').content |  | ||||||
|     } |  | ||||||
|     return prefix |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   public getToken(): string { |  | ||||||
|     return this.cookieService.get(`${this.getCookiePrefix()}csrftoken`) |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -9,6 +9,7 @@ import { DatePipe, registerLocaleData } from '@angular/common' | |||||||
| import { | import { | ||||||
|   HTTP_INTERCEPTORS, |   HTTP_INTERCEPTORS, | ||||||
|   provideHttpClient, |   provideHttpClient, | ||||||
|  |   withFetch, | ||||||
|   withInterceptorsFromDi, |   withInterceptorsFromDi, | ||||||
| } from '@angular/common/http' | } from '@angular/common/http' | ||||||
| import { FormsModule, ReactiveFormsModule } from '@angular/forms' | import { FormsModule, ReactiveFormsModule } from '@angular/forms' | ||||||
| @ -393,6 +394,6 @@ bootstrapApplication(AppComponent, { | |||||||
|     CorrespondentNamePipe, |     CorrespondentNamePipe, | ||||||
|     DocumentTypeNamePipe, |     DocumentTypeNamePipe, | ||||||
|     StoragePathNamePipe, |     StoragePathNamePipe, | ||||||
|     provideHttpClient(withInterceptorsFromDi()), |     provideHttpClient(withInterceptorsFromDi(), withFetch()), | ||||||
|   ], |   ], | ||||||
| }).catch((err) => console.error(err)) | }).catch((err) => console.error(err)) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user