agent-server-http.service.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { HttpService } from '@nestjs/axios'
  2. import { Injectable } from '@nestjs/common'
  3. import { lastValueFrom } from 'rxjs'
  4. import { ServiceResponse } from '@/response/response.interface'
  5. import { HttpStatusCode } from '@/response/status-code.enum'
  6. @Injectable()
  7. export class AgentServerHttpService {
  8. private readonly baseUrl = 'http://192.168.206.189:8083/api'
  9. constructor(private readonly httpService: HttpService) {}
  10. private async makeRequest<T>(
  11. endpoint: string,
  12. params?: Record<string, any>
  13. ): Promise<ServiceResponse<T>> {
  14. try {
  15. const url = `${this.baseUrl}/${endpoint}`
  16. const responseObservable = await this.httpService.get<ServiceResponse<T>>(
  17. url,
  18. { params }
  19. )
  20. const { data } = await lastValueFrom(responseObservable)
  21. return data
  22. } catch (error) {
  23. return {
  24. code: HttpStatusCode.INTERNAL_SERVER_ERROR,
  25. msg: error.message,
  26. data: null as T
  27. }
  28. }
  29. }
  30. async makeRequestPost<T>(
  31. endpoint: string,
  32. params?: Record<string, any>
  33. ): Promise<ServiceResponse<T>> {
  34. try {
  35. const url = `${this.baseUrl}/${endpoint}`
  36. const responseObservable = await this.httpService.post<
  37. ServiceResponse<T>
  38. >(url, params, {
  39. headers: {
  40. 'Content-Type': 'application/json'
  41. }
  42. })
  43. const { data } = await lastValueFrom(responseObservable)
  44. return data
  45. } catch (error) {
  46. return {
  47. code: HttpStatusCode.INTERNAL_SERVER_ERROR,
  48. msg: error.message,
  49. data: null as T
  50. }
  51. }
  52. }
  53. async listStaffs(): Promise<ServiceResponse<StaffsType[]>> {
  54. return this.makeRequest<StaffsType[]>('listStaffs')
  55. }
  56. async getStaffProfile(
  57. staff_id: string
  58. ): Promise<ServiceResponse<StaffProfile>> {
  59. return this.makeRequest<StaffProfile>('getStaffProfile', { staff_id })
  60. }
  61. async getUserProfile(user_id: string): Promise<ServiceResponse<UserProfile>> {
  62. return this.makeRequest<UserProfile>('getUserProfile', { user_id })
  63. }
  64. async listUsers(
  65. user_name: string,
  66. user_union_id: string
  67. ): Promise<ServiceResponse<UserProfile[]>> {
  68. return this.makeRequest<UserProfile[]>('listUsers', {
  69. user_name,
  70. user_union_id
  71. })
  72. }
  73. async getDialogueHistory(
  74. staff_id: string,
  75. user_id: string,
  76. recent_minutes: string
  77. ): Promise<ServiceResponse<DialogueHistory[]>> {
  78. return this.makeRequest<DialogueHistory[]>('getDialogueHistory', {
  79. staff_id,
  80. user_id,
  81. recent_minutes
  82. })
  83. }
  84. async listModels(): Promise<ServiceResponse<Model[]>> {
  85. return this.makeRequest<Model[]>('listModels')
  86. }
  87. async listScenes(): Promise<ServiceResponse<any[]>> {
  88. return this.makeRequest<any[]>('listScenes')
  89. }
  90. async getBasePrompt(scene: string): Promise<ServiceResponse<string>> {
  91. return this.makeRequest<string>('getBasePrompt', { scene })
  92. }
  93. async runPrompt(params: any): Promise<ServiceResponse<any>> {
  94. return this.makeRequestPost<any>('runPrompt', params)
  95. }
  96. async formatForPrompt(params: any): Promise<ServiceResponse<any>> {
  97. return this.makeRequestPost<any>('formatForPrompt', params)
  98. }
  99. }