|
| 1 | +import { HttpClient } from '@angular/common/http' |
| 2 | +import { inject, Injectable } from '@angular/core' |
| 3 | +import { API_PREFIX, SystemPrivacyFields } from '@metad/cloud/state' |
| 4 | +import { CopilotChatMessage } from '@metad/copilot' |
| 5 | +import { Indicator } from '@metad/ocap-core' |
| 6 | +import { omit } from 'lodash-es' |
| 7 | +import { NGXLogger } from 'ngx-logger' |
| 8 | +import { BehaviorSubject, map } from 'rxjs' |
| 9 | +import { IChatBIConversation } from '../types' |
| 10 | + |
| 11 | +const API_CHATBI_CONVERSATION = API_PREFIX + '/chatbi-conversation' |
| 12 | + |
| 13 | +export interface ChatbiConverstion { |
| 14 | + id?: string |
| 15 | + key: string |
| 16 | + name: string |
| 17 | + modelId: string |
| 18 | + dataSource: string |
| 19 | + entity: string |
| 20 | + command: string |
| 21 | + messages: CopilotChatMessage[] |
| 22 | + indicators: Indicator[] |
| 23 | +} |
| 24 | + |
| 25 | +@Injectable({ providedIn: 'root' }) |
| 26 | +export class ChatBIConversationService { |
| 27 | + readonly #logger = inject(NGXLogger) |
| 28 | + readonly httpClient = inject(HttpClient) |
| 29 | + |
| 30 | + readonly #refresh = new BehaviorSubject<void>(null) |
| 31 | + |
| 32 | + getMy() { |
| 33 | + return this.httpClient |
| 34 | + .get<{ items: IChatBIConversation[] }>(API_CHATBI_CONVERSATION + '/my') |
| 35 | + .pipe(map(({ items }) => items.map(convertChatBIConversationResult))) |
| 36 | + } |
| 37 | + |
| 38 | + getById(id: string) { |
| 39 | + return this.httpClient |
| 40 | + .get<IChatBIConversation>(API_CHATBI_CONVERSATION + '/' + id) |
| 41 | + .pipe(map(convertChatBIConversationResult)) |
| 42 | + } |
| 43 | + |
| 44 | + upsert(entity: Partial<ChatbiConverstion>) { |
| 45 | + return entity.id |
| 46 | + ? this.httpClient |
| 47 | + .put<ChatbiConverstion>(`${API_CHATBI_CONVERSATION}/${entity.id}`, convertChatBIConversation(entity)) |
| 48 | + .pipe(map(() => entity as ChatbiConverstion)) |
| 49 | + : this.httpClient |
| 50 | + .post<ChatbiConverstion>(API_CHATBI_CONVERSATION, convertChatBIConversation(entity)) |
| 51 | + .pipe(map((result) => convertChatBIConversationResult(result))) |
| 52 | + } |
| 53 | + |
| 54 | + delete(id: string) { |
| 55 | + return this.httpClient.delete(`${API_CHATBI_CONVERSATION}/${id}`) |
| 56 | + } |
| 57 | + |
| 58 | + refresh() { |
| 59 | + this.#refresh.next() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export function convertChatBIConversation(input: Partial<ChatbiConverstion>) { |
| 64 | + return { |
| 65 | + ...omit(input, 'messages', 'indicators'), |
| 66 | + options: { |
| 67 | + messages: input.messages, |
| 68 | + indicators: input.indicators, |
| 69 | + } |
| 70 | + } as IChatBIConversation |
| 71 | +} |
| 72 | + |
| 73 | +export function convertChatBIConversationResult(result: IChatBIConversation) { |
| 74 | + return { |
| 75 | + ...omit(result, 'options', ...SystemPrivacyFields), |
| 76 | + messages: result.options?.messages || [], |
| 77 | + indicators: result.options?.indicators |
| 78 | + } as ChatbiConverstion |
| 79 | +} |
0 commit comments