ai-assistant.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import type mongoose from 'mongoose';
  2. import { type Model, type Document, Schema } from 'mongoose';
  3. import { getOrCreateModel } from '~/server/util/mongoose-utils';
  4. /*
  5. * Objects
  6. */
  7. const AiAssistantType = {
  8. KNOWLEDGE: 'knowledge',
  9. // EDITOR: 'editor',
  10. // LEARNING: 'learning',
  11. } as const;
  12. const AiAssistantSharingScope = {
  13. PUBLIC: 'public',
  14. ONLY_ME: 'onlyMe',
  15. USER_GROUP: 'userGroup',
  16. } as const;
  17. const AiAssistantLearningScope = {
  18. PUBLIC: 'public',
  19. ONLY_ME: 'onlyMe',
  20. USER_GROUP: 'userGroup',
  21. } as const;
  22. /*
  23. * Interfaces
  24. */
  25. type AiAssistantType = typeof AiAssistantType[keyof typeof AiAssistantType];
  26. type AiAssistantSharingScope = typeof AiAssistantSharingScope[keyof typeof AiAssistantSharingScope];
  27. type AiAssistantLearningScope = typeof AiAssistantLearningScope[keyof typeof AiAssistantLearningScope];
  28. interface AiAssistant {
  29. name: string;
  30. description?: string
  31. instruction?: string
  32. vectorStoreId: string // VectorStoreId of OpenAI Specify (https://platform.openai.com/docs/api-reference/vector-stores/object)
  33. types: AiAssistantType[]
  34. pages: mongoose.Types.ObjectId[]
  35. sharingScope: AiAssistantSharingScope
  36. learningScope: AiAssistantLearningScope
  37. }
  38. interface AiAssistantDocument extends AiAssistant, Document {}
  39. type AiAssistantModel = Model<AiAssistantDocument>
  40. /*
  41. * Schema Definition
  42. */
  43. const schema = new Schema<AiAssistantDocument>(
  44. {
  45. name: {
  46. type: String,
  47. required: true,
  48. },
  49. description: {
  50. type: String,
  51. },
  52. instruction: {
  53. type: String,
  54. },
  55. vectorStoreId: {
  56. type: String,
  57. required: true,
  58. },
  59. types: [{
  60. type: String,
  61. enum: Object.values(AiAssistantType),
  62. required: true,
  63. }],
  64. pages: [{
  65. type: Schema.Types.ObjectId,
  66. ref: 'Page',
  67. required: true,
  68. }],
  69. sharingScope: {
  70. type: String,
  71. enum: Object.values(AiAssistantSharingScope),
  72. required: true,
  73. },
  74. learningScope: {
  75. type: String,
  76. enum: Object.values(AiAssistantLearningScope),
  77. required: true,
  78. },
  79. },
  80. {
  81. timestamps: true,
  82. },
  83. );
  84. export default getOrCreateModel<AiAssistantDocument, AiAssistantModel>('AiAssistant', schema);