ai-assistant.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { type IGrantedGroup, GroupType } from '@growi/core';
  2. import { type Model, type Document, Schema } from 'mongoose';
  3. import { getOrCreateModel } from '~/server/util/mongoose-utils';
  4. import { type AiAssistant, AiAssistantShareScope, AiAssistantAccessScope } from '../../interfaces/ai-assistant';
  5. export interface AiAssistantDocument extends AiAssistant, Document {}
  6. type AiAssistantModel = Model<AiAssistantDocument>
  7. /*
  8. * Schema Definition
  9. */
  10. const schema = new Schema<AiAssistantDocument>(
  11. {
  12. name: {
  13. type: String,
  14. required: true,
  15. },
  16. description: {
  17. type: String,
  18. required: true,
  19. default: '',
  20. },
  21. additionalInstruction: {
  22. type: String,
  23. required: true,
  24. default: '',
  25. },
  26. pagePathPatterns: [{
  27. type: String,
  28. required: true,
  29. }],
  30. vectorStore: {
  31. type: Schema.Types.ObjectId,
  32. ref: 'VectorStore',
  33. required: true,
  34. },
  35. owner: {
  36. type: Schema.Types.ObjectId,
  37. ref: 'User',
  38. required: true,
  39. },
  40. grantedGroupsForShareScope: {
  41. type: [{
  42. type: {
  43. type: String,
  44. enum: Object.values(GroupType),
  45. required: true,
  46. default: 'UserGroup',
  47. },
  48. item: {
  49. type: Schema.Types.ObjectId,
  50. refPath: 'grantedGroups.type',
  51. required: true,
  52. index: true,
  53. },
  54. }],
  55. validate: [function(arr: IGrantedGroup[]): boolean {
  56. if (arr == null) return true;
  57. const uniqueItemValues = new Set(arr.map(e => e.item));
  58. return arr.length === uniqueItemValues.size;
  59. }, 'grantedGroups contains non unique item'],
  60. default: [],
  61. },
  62. grantedGroupsForAccessScope: {
  63. type: [{
  64. type: {
  65. type: String,
  66. enum: Object.values(GroupType),
  67. required: true,
  68. default: 'UserGroup',
  69. },
  70. item: {
  71. type: Schema.Types.ObjectId,
  72. refPath: 'grantedGroups.type',
  73. required: true,
  74. index: true,
  75. },
  76. }],
  77. validate: [function(arr: IGrantedGroup[]): boolean {
  78. if (arr == null) return true;
  79. const uniqueItemValues = new Set(arr.map(e => e.item));
  80. return arr.length === uniqueItemValues.size;
  81. }, 'grantedGroups contains non unique item'],
  82. default: [],
  83. },
  84. shareScope: {
  85. type: String,
  86. enum: Object.values(AiAssistantShareScope),
  87. required: true,
  88. },
  89. accessScope: {
  90. type: String,
  91. enum: Object.values(AiAssistantAccessScope),
  92. required: true,
  93. },
  94. },
  95. {
  96. timestamps: true,
  97. },
  98. );
  99. export default getOrCreateModel<AiAssistantDocument, AiAssistantModel>('AiAssistant', schema);