-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from xpert-ai/develop
Version 3.0.1
- Loading branch information
Showing
180 changed files
with
8,180 additions
and
807 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Injectable } from '@angular/core' | ||
import { API_PREFIX, OrganizationBaseCrudService } from '@metad/cloud/state' | ||
import { IApiKey } from '../types' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ApiKeyService extends OrganizationBaseCrudService<IApiKey> { | ||
constructor() { | ||
super(API_PREFIX + '/api-key') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,21 @@ | ||
import { Injectable, inject } from '@angular/core' | ||
import { Store } from '@metad/cloud/state' | ||
import { TChatOptions, TChatRequest } from '../types' | ||
import { EventSourceMessage, fetchEventSource } from '@microsoft/fetch-event-source' | ||
import { Observable } from 'rxjs' | ||
import { AuthStrategy } from '../auth' | ||
import { Injectable } from '@angular/core' | ||
import { API_CHAT } from '../constants/app.constants' | ||
import { injectApiBaseUrl } from '../providers' | ||
import { TChatOptions, TChatRequest } from '../types' | ||
import { injectFetchEventSource } from './fetch-event-source' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class ChatService { | ||
readonly #store = inject(Store) | ||
readonly #auth = inject(AuthStrategy) | ||
readonly baseUrl = injectApiBaseUrl() | ||
readonly fetchEventSource = injectFetchEventSource() | ||
|
||
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(this.baseUrl + API_CHAT, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
'Organization-Id': `${organization.id}` | ||
}, | ||
body: JSON.stringify({ | ||
request, | ||
options | ||
}), | ||
signal: ctrl.signal, | ||
onmessage(msg) { | ||
subscriber.next(msg) | ||
}, | ||
onclose() { | ||
subscriber.complete() | ||
}, | ||
onerror(err) { | ||
subscriber.error(err) | ||
throw err | ||
} | ||
chat(request: TChatRequest, options: TChatOptions) { | ||
return this.fetchEventSource( | ||
this.baseUrl + API_CHAT, | ||
JSON.stringify({ | ||
request, | ||
options | ||
}) | ||
|
||
return () => ctrl.abort() | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { inject } from '@angular/core' | ||
import { EventSourceMessage, EventStreamContentType, fetchEventSource } from '@microsoft/fetch-event-source' | ||
import { firstValueFrom, Observable } from 'rxjs' | ||
import { AuthStrategy } from '../auth' | ||
import { Store } from './store.service' | ||
|
||
export function injectFetchEventSource<T extends BodyInit | null>() { | ||
const store = inject(Store) | ||
const auth = inject(AuthStrategy) | ||
|
||
return (url: string, data: T) => { | ||
const token = store.token | ||
const organization = store.selectedOrganization ?? { id: null } | ||
|
||
return new Observable<EventSourceMessage>((subscriber) => { | ||
const ctrl = new AbortController() | ||
|
||
let unauthorized = false | ||
function req() { | ||
fetchEventSource(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
'Organization-Id': `${organization.id}` | ||
}, | ||
body: data, | ||
signal: ctrl.signal, | ||
onopen: async (response) => { | ||
if (!unauthorized && response.status === 401) { | ||
unauthorized = true | ||
await firstValueFrom(auth.refreshToken()) | ||
return req() | ||
} | ||
|
||
const contentType = response.headers.get('content-type') | ||
if ( | ||
!(contentType === null || contentType === void 0 | ||
? void 0 | ||
: contentType.startsWith(EventStreamContentType)) | ||
) { | ||
throw new Error(`Expected content-type to be ${EventStreamContentType}, Actual: ${contentType}`) | ||
} | ||
}, | ||
onmessage(msg) { | ||
subscriber.next(msg) | ||
}, | ||
onclose() { | ||
if (!unauthorized) { | ||
subscriber.complete() | ||
} | ||
}, | ||
onerror(err) { | ||
subscriber.error(err) | ||
throw err | ||
} | ||
}) | ||
} | ||
|
||
req() | ||
|
||
return () => ctrl.abort() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,29 @@ | ||
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 { BehaviorSubject } from 'rxjs' | ||
import { API_XPERT_AGENT } from '../constants/app.constants' | ||
import { injectApiBaseUrl } from '../providers' | ||
import { IXpertAgent, TChatAgentParams } from '../types' | ||
import { Store } from './store.service' | ||
import { XpertWorkspaceBaseCrudService } from './xpert-workspace.service' | ||
import { injectFetchEventSource } from './fetch-event-source' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class XpertAgentService extends XpertWorkspaceBaseCrudService<IXpertAgent> { | ||
readonly #logger = inject(NGXLogger) | ||
readonly #store = inject(Store) | ||
readonly baseUrl = injectApiBaseUrl() | ||
readonly fetchEventSource = injectFetchEventSource() | ||
|
||
readonly #refresh = new BehaviorSubject<void>(null) | ||
|
||
constructor() { | ||
super(API_XPERT_AGENT) | ||
} | ||
|
||
chatAgent(data: TChatAgentParams): Observable<EventSourceMessage> { | ||
const token = this.#store.token | ||
const organization = this.store.selectedOrganization ?? { id: null } | ||
return new Observable((subscriber) => { | ||
const ctrl = new AbortController() | ||
fetchEventSource(this.baseUrl + this.apiBaseUrl + `/chat`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
'Organization-Id': `${organization.id}` | ||
}, | ||
body: JSON.stringify({ | ||
...data, | ||
xpert: pick(data.xpert, 'id', 'name', 'copilotId', 'copilotModel') | ||
}), | ||
signal: ctrl.signal, | ||
onmessage(msg) { | ||
subscriber.next(msg) | ||
}, | ||
onclose() { | ||
subscriber.complete() | ||
}, | ||
onerror(err) { | ||
subscriber.error(err) | ||
throw err | ||
} | ||
}) | ||
|
||
return () => ctrl.abort() | ||
}) | ||
chatAgent(data: TChatAgentParams) { | ||
return this.fetchEventSource(this.baseUrl + this.apiBaseUrl + `/chat`, JSON.stringify({ | ||
...data, | ||
xpert: pick(data.xpert, 'id', 'name', 'copilotId', 'copilotModel') | ||
})) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.