Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

2.8 KB
Raw Blame
import type { Context } from 'hono';

import { BaseController } from '../common/base.controller';
import { ModelService } from '../services/model.service';
import type { CreateModelRequest, ModelsListQuery, UpdateModelRequest } from '../types/model.type';

export class ModelController extends BaseController {
  /**
   * Retrieves the model list endpoint
   * GET /api/v1/models
   * Query: { page?, pageSize?, keyword? }
   */
  async handleGetModels(c: Context) {
    try {
      const query = this.getQuery<ModelsListQuery>(c);

      const db = await this.getDatabase();
      const modelService = new ModelService(db, this.getUserId(c), this.getWorkspaceId(c));

      const result = await modelService.getModels(query);

      return this.list(c, 'models', result, {
        message: 'Model list retrieved successfully',
        pagination: query,
      });
    } catch (error) {
      return this.handleError(c, error);
    }
  }

  /**
   * Retrieves model details
   * GET /api/v1/models/:providerId/:modelId
   */
  async handleGetModel(c: Context) {
    try {
      const { providerId, modelId } = this.getParams<{ modelId: string; providerId: string }>(c);

      const db = await this.getDatabase();
      const modelService = new ModelService(db, this.getUserId(c), this.getWorkspaceId(c));
      const result = await modelService.getModelDetail(providerId, modelId);

      return this.success(c, result, 'Model details retrieved successfully');
    } catch (error) {
      return this.handleError(c, error);
    }
  }

  /**
   * Creates a model
   * POST /api/v1/models
   */
  async handleCreateModel(c: Context) {
    try {
      const body = await this.getBody<CreateModelRequest>(c);

      if (!body) {
        return this.error(c, 'Request body cannot be empty', 400);
      }

      const db = await this.getDatabase();
      const modelService = new ModelService(db, this.getUserId(c), this.getWorkspaceId(c));
      const result = await modelService.createModel(body);

      return this.success(c, result, 'Model created successfully');
    } catch (error) {
      return this.handleError(c, error);
    }
  }

  /**
   * Updates a model
   * PATCH /api/v1/models/:providerId/:modelId
   */
  async handleUpdateModel(c: Context) {
    try {
      const { providerId, modelId } = this.getParams<{ modelId: string; providerId: string }>(c);
      const body = await this.getBody<UpdateModelRequest>(c);

      if (!body) {
        return this.error(c, 'Request body cannot be empty', 400);
      }

      const db = await this.getDatabase();
      const modelService = new ModelService(db, this.getUserId(c), this.getWorkspaceId(c));
      const result = await modelService.updateModel(providerId, modelId, body);

      return this.success(c, result, 'Model updated successfully');
    } catch (error) {
      return this.handleError(c, error);
    }
  }
}