Many-in-one AI client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.9 KiB

  1. import { DoFetch } from '../../../packages/request';
  2. import { PlatformApiError } from '../../../common';
  3. import { ResponseObjectType } from '../common';
  4. export enum DataEventObjectType {
  5. MODEL = 'model',
  6. }
  7. export type ModelId = string;
  8. export interface ModelData {
  9. id: ModelId;
  10. object: DataEventObjectType,
  11. owned_by: string;
  12. permission: string[];
  13. }
  14. export interface ListModelsResponse {
  15. object: ResponseObjectType;
  16. data: ModelData[];
  17. }
  18. export function listModels(
  19. this: NodeJS.EventEmitter,
  20. doFetch: DoFetch,
  21. ) {
  22. doFetch('GET', '/models')
  23. .then(async (response) => {
  24. if (!response.ok) {
  25. this.emit('error', new PlatformApiError(
  26. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  27. `Request from platform returned with status: ${response.status}`,
  28. response,
  29. ));
  30. this.emit('end');
  31. return;
  32. }
  33. const responseData = await response.json() as ListModelsResponse;
  34. this.emit('data', responseData);
  35. this.emit('end');
  36. })
  37. .catch((err) => {
  38. this.emit('error', err as Error);
  39. this.emit('end');
  40. });
  41. return this;
  42. }
  43. export function retrieveModel(
  44. this: NodeJS.EventEmitter,
  45. doFetch: DoFetch,
  46. modelId: ModelId,
  47. ) {
  48. doFetch('GET', `/models/${modelId}`)
  49. .then(async (response) => {
  50. if (!response.ok) {
  51. this.emit('error', new PlatformApiError(
  52. // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
  53. `Request from platform returned with status: ${response.status}`,
  54. response,
  55. ));
  56. this.emit('end');
  57. return;
  58. }
  59. const responseData = await response.json() as ModelData;
  60. this.emit('data', responseData);
  61. this.emit('end');
  62. })
  63. .catch((err) => {
  64. this.emit('error', err as Error);
  65. this.emit('end');
  66. });
  67. return this;
  68. }