ai-assistant.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { GroupType, type IGrantedGroup } from '@growi/core';
  2. import { type Document, type Model, Schema } from 'mongoose';
  3. import { getOrCreateModel } from '~/server/util/mongoose-utils';
  4. import {
  5. type AiAssistant,
  6. AiAssistantAccessScope,
  7. AiAssistantShareScope,
  8. } from '../../interfaces/ai-assistant';
  9. export interface AiAssistantDocument extends AiAssistant, Document {}
  10. interface AiAssistantModel extends Model<AiAssistantDocument> {
  11. setDefault(id: string, isDefault: boolean): Promise<AiAssistantDocument>;
  12. }
  13. /*
  14. * Schema Definition
  15. */
  16. const schema = new Schema<AiAssistantDocument>(
  17. {
  18. name: {
  19. type: String,
  20. required: true,
  21. },
  22. description: {
  23. type: String,
  24. required: true,
  25. default: '',
  26. },
  27. additionalInstruction: {
  28. type: String,
  29. required: true,
  30. default: '',
  31. },
  32. pagePathPatterns: [
  33. {
  34. type: String,
  35. required: true,
  36. },
  37. ],
  38. vectorStore: {
  39. type: Schema.Types.ObjectId,
  40. ref: 'VectorStore',
  41. required: true,
  42. },
  43. owner: {
  44. type: Schema.Types.ObjectId,
  45. ref: 'User',
  46. required: true,
  47. },
  48. grantedGroupsForShareScope: {
  49. type: [
  50. {
  51. type: {
  52. type: String,
  53. enum: Object.values(GroupType),
  54. required: true,
  55. default: 'UserGroup',
  56. },
  57. item: {
  58. type: Schema.Types.ObjectId,
  59. refPath: 'grantedGroupsForShareScope.type',
  60. required: true,
  61. index: true,
  62. },
  63. },
  64. ],
  65. validate: [
  66. (arr: IGrantedGroup[]): boolean => {
  67. if (arr == null) return true;
  68. const uniqueItemValues = new Set(arr.map((e) => e.item));
  69. return arr.length === uniqueItemValues.size;
  70. },
  71. 'grantedGroups contains non unique item',
  72. ],
  73. default: [],
  74. },
  75. grantedGroupsForAccessScope: {
  76. type: [
  77. {
  78. type: {
  79. type: String,
  80. enum: Object.values(GroupType),
  81. required: true,
  82. default: 'UserGroup',
  83. },
  84. item: {
  85. type: Schema.Types.ObjectId,
  86. refPath: 'grantedGroupsForAccessScope.type',
  87. required: true,
  88. index: true,
  89. },
  90. },
  91. ],
  92. validate: [
  93. (arr: IGrantedGroup[]): boolean => {
  94. if (arr == null) return true;
  95. const uniqueItemValues = new Set(arr.map((e) => e.item));
  96. return arr.length === uniqueItemValues.size;
  97. },
  98. 'grantedGroups contains non unique item',
  99. ],
  100. default: [],
  101. },
  102. shareScope: {
  103. type: String,
  104. enum: Object.values(AiAssistantShareScope),
  105. required: true,
  106. },
  107. accessScope: {
  108. type: String,
  109. enum: Object.values(AiAssistantAccessScope),
  110. required: true,
  111. },
  112. isDefault: {
  113. type: Boolean,
  114. required: true,
  115. default: false,
  116. },
  117. },
  118. {
  119. timestamps: true,
  120. },
  121. );
  122. schema.statics.setDefault = async function (
  123. id: string,
  124. isDefault: boolean,
  125. ): Promise<AiAssistantDocument> {
  126. if (isDefault) {
  127. await this.bulkWrite([
  128. {
  129. updateOne: {
  130. filter: {
  131. _id: id,
  132. shareScope: AiAssistantShareScope.PUBLIC_ONLY,
  133. },
  134. update: { $set: { isDefault: true } },
  135. },
  136. },
  137. {
  138. updateMany: {
  139. filter: {
  140. _id: { $ne: id },
  141. isDefault: true,
  142. },
  143. update: { $set: { isDefault: false } },
  144. },
  145. },
  146. ]);
  147. } else {
  148. await this.findByIdAndUpdate(id, { isDefault: false });
  149. }
  150. const updatedAiAssistant = await this.findById(id);
  151. return updatedAiAssistant;
  152. };
  153. export default getOrCreateModel<AiAssistantDocument, AiAssistantModel>(
  154. 'AiAssistant',
  155. schema,
  156. );