Skip to content

Commit

Permalink
Merge pull request #3 from xpert-ai/develop
Browse files Browse the repository at this point in the history
Fixed: api base url for chat apis
  • Loading branch information
tiven-w authored Nov 30, 2024
2 parents 2dd37f8 + ad10a9e commit 1b6cd47
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 113 deletions.
11 changes: 5 additions & 6 deletions apps/cloud/src/app/@core/interceptors/api.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/c
import { Injectable } from '@angular/core'
import { API_PREFIX } from '@metad/cloud/state'
import { Observable } from 'rxjs'
import { environment } from '../../../environments/environment'

const baseUrl = environment.API_BASE_URL
import { injectApiBaseUrl } from '../providers'

@Injectable()
export class APIInterceptor implements HttpInterceptor {
readonly baseUrl = injectApiBaseUrl()

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (baseUrl && request.url.startsWith(`${API_PREFIX}`)) {
const url = baseUrl + request.url
// console.log(`API Request: ${request.url} -> ${url}`);
if (this.baseUrl && request.url.startsWith(`${API_PREFIX}`)) {
const url = this.baseUrl + request.url
request = request.clone({
url: url
})
Expand Down
3 changes: 2 additions & 1 deletion apps/cloud/src/app/@core/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './translate'
export * from './logger'
export * from './user'
export * from './user'
export * from './url'
15 changes: 15 additions & 0 deletions apps/cloud/src/app/@core/providers/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { environment } from '../../../environments/environment'

const baseUrl = environment.API_BASE_URL

/**
* Inject the base url of api server
*
* @returns url
*/
export function injectApiBaseUrl() {
if (!baseUrl) {
return ''
}
return baseUrl?.endsWith('/') ? baseUrl.slice(0, baseUrl.length - 1) : baseUrl
}
4 changes: 3 additions & 1 deletion apps/cloud/src/app/@core/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import { EventSourceMessage, fetchEventSource } from '@microsoft/fetch-event-sou
import { Observable } from 'rxjs'
import { AuthStrategy } from '../auth'
import { API_CHAT } from '../constants/app.constants'
import { injectApiBaseUrl } from '../providers'

@Injectable({ providedIn: 'root' })
export class ChatService {
readonly #store = inject(Store)
readonly #auth = inject(AuthStrategy)
readonly baseUrl = injectApiBaseUrl()

chat(request: TChatRequest, options: TChatOptions): Observable<EventSourceMessage> {
const token = this.#store.token
const organization = this.#store.selectedOrganization ?? { id: null }

return new Observable((subscriber) => {
const ctrl = new AbortController()
fetchEventSource(API_CHAT, {
fetchEventSource(this.baseUrl + API_CHAT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
12 changes: 7 additions & 5 deletions apps/cloud/src/app/@core/services/xpert-agent.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { inject, Injectable } from '@angular/core'
import { EventSourceMessage, fetchEventSource } from '@microsoft/fetch-event-source'
import { pick } from 'lodash-es'
import { NGXLogger } from 'ngx-logger'
import { BehaviorSubject, Observable } from 'rxjs'
import { API_XPERT_AGENT } from '../constants/app.constants'
import { injectApiBaseUrl } from '../providers'
import { IXpertAgent, TChatAgentParams } from '../types'
import { XpertWorkspaceBaseCrudService } from './xpert-workspace.service'
import { Store } from './store.service'
import { pick } from 'lodash-es'
import { XpertWorkspaceBaseCrudService } from './xpert-workspace.service'

@Injectable({ providedIn: 'root' })
export class XpertAgentService extends XpertWorkspaceBaseCrudService<IXpertAgent> {
readonly #logger = inject(NGXLogger)
readonly #store = inject(Store)
readonly baseUrl = injectApiBaseUrl()

readonly #refresh = new BehaviorSubject<void>(null)

Expand All @@ -21,10 +23,10 @@ export class XpertAgentService extends XpertWorkspaceBaseCrudService<IXpertAgent

chatAgent(data: TChatAgentParams): Observable<EventSourceMessage> {
const token = this.#store.token
const organization = this.store.selectedOrganization ?? {id: null}
const organization = this.store.selectedOrganization ?? { id: null }
return new Observable((subscriber) => {
const ctrl = new AbortController()
fetchEventSource(this.apiBaseUrl + `/chat`, {
fetchEventSource(this.baseUrl + this.apiBaseUrl + `/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -44,7 +46,7 @@ export class XpertAgentService extends XpertWorkspaceBaseCrudService<IXpertAgent
},
onerror(err) {
subscriber.error(err)
throw err;
throw err
}
})

Expand Down
4 changes: 3 additions & 1 deletion apps/cloud/src/app/@core/services/xpert.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { BehaviorSubject, Observable, tap } from 'rxjs'
import { API_XPERT_ROLE } from '../constants/app.constants'
import { IUser, IXpert, IXpertAgentExecution, OrderTypeEnum, TChatRequest, TXpertTeamDraft, XpertTypeEnum } from '../types'
import { XpertWorkspaceBaseCrudService } from './xpert-workspace.service'
import { injectApiBaseUrl } from '../providers'

@Injectable({ providedIn: 'root' })
export class XpertService extends XpertWorkspaceBaseCrudService<IXpert> {
readonly #logger = inject(NGXLogger)
readonly #store = inject(Store)
readonly baseUrl = injectApiBaseUrl()

readonly #refresh = new BehaviorSubject<void>(null)

Expand Down Expand Up @@ -70,7 +72,7 @@ export class XpertService extends XpertWorkspaceBaseCrudService<IXpert> {
const organization = this.store.selectedOrganization ?? { id: null }
return new Observable((subscriber) => {
const ctrl = new AbortController()
fetchEventSource(this.apiBaseUrl + `/${id}/chat`, {
fetchEventSource(this.baseUrl + this.apiBaseUrl + `/${id}/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
Loading

0 comments on commit 1b6cd47

Please sign in to comment.