openai.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. import assert from 'node:assert';
  2. import { Readable, Transform } from 'stream';
  3. import { pipeline } from 'stream/promises';
  4. import {
  5. PageGrant, getIdForRef, getIdStringForRef, isPopulated, type IUserHasId,
  6. } from '@growi/core';
  7. import { deepEquals } from '@growi/core/dist/utils';
  8. import { isGrobPatternPath } from '@growi/core/dist/utils/page-path-utils';
  9. import escapeStringRegexp from 'escape-string-regexp';
  10. import createError from 'http-errors';
  11. import mongoose, { type HydratedDocument, type Types } from 'mongoose';
  12. import { type OpenAI, toFile } from 'openai';
  13. import ExternalUserGroupRelation from '~/features/external-user-group/server/models/external-user-group-relation';
  14. import ThreadRelationModel from '~/features/openai/server/models/thread-relation';
  15. import VectorStoreModel, { type VectorStoreDocument } from '~/features/openai/server/models/vector-store';
  16. import VectorStoreFileRelationModel, {
  17. type VectorStoreFileRelation,
  18. prepareVectorStoreFileRelations,
  19. } from '~/features/openai/server/models/vector-store-file-relation';
  20. import type { PageDocument, PageModel } from '~/server/models/page';
  21. import UserGroupRelation from '~/server/models/user-group-relation';
  22. import { configManager } from '~/server/service/config-manager';
  23. import { createBatchStream } from '~/server/util/batch-stream';
  24. import loggerFactory from '~/utils/logger';
  25. import { OpenaiServiceTypes } from '../../interfaces/ai';
  26. import {
  27. type AccessibleAiAssistants, type AiAssistant, AiAssistantAccessScope, AiAssistantShareScope,
  28. } from '../../interfaces/ai-assistant';
  29. import AiAssistantModel, { type AiAssistantDocument } from '../models/ai-assistant';
  30. import { convertMarkdownToHtml } from '../utils/convert-markdown-to-html';
  31. import { getClient } from './client-delegator';
  32. // import { splitMarkdownIntoChunks } from './markdown-splitter/markdown-token-splitter';
  33. import { openaiApiErrorHandler } from './openai-api-error-handler';
  34. const { isDeepEquals } = deepEquals;
  35. const BATCH_SIZE = 100;
  36. const logger = loggerFactory('growi:service:openai');
  37. // const isVectorStoreForPublicScopeExist = false;
  38. type VectorStoreFileRelationsMap = Map<string, VectorStoreFileRelation>
  39. const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string | RegExp> => {
  40. return pagePathPatterns.map((pagePathPattern) => {
  41. if (isGrobPatternPath(pagePathPattern)) {
  42. const trimedPagePathPattern = pagePathPattern.replace('/*', '');
  43. const escapedPagePathPattern = escapeStringRegexp(trimedPagePathPattern);
  44. return new RegExp(`^${escapedPagePathPattern}`);
  45. }
  46. return pagePathPattern;
  47. });
  48. };
  49. export interface IOpenaiService {
  50. getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread | undefined>;
  51. // getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument>;
  52. deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
  53. deleteObsolatedVectorStoreRelations(): Promise<void> // for CronJob
  54. createVectorStoreFile(vectorStoreRelation: VectorStoreDocument, pages: PageDocument[]): Promise<void>;
  55. deleteVectorStoreFile(vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId): Promise<void>;
  56. deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
  57. // rebuildVectorStoreAll(): Promise<void>;
  58. // rebuildVectorStore(page: HydratedDocument<PageDocument>): Promise<void>;
  59. createAiAssistant(data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument>;
  60. updateAiAssistant(aiAssistantId: string, data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument>;
  61. getAccessibleAiAssistants(user: IUserHasId): Promise<AccessibleAiAssistants>
  62. deleteAiAssistant(ownerId: string, aiAssistantId: string): Promise<AiAssistantDocument>
  63. }
  64. class OpenaiService implements IOpenaiService {
  65. private get client() {
  66. const openaiServiceType = configManager.getConfig('openai:serviceType');
  67. return getClient({ openaiServiceType });
  68. }
  69. public async getOrCreateThread(userId: string, vectorStoreId?: string, threadId?: string): Promise<OpenAI.Beta.Threads.Thread> {
  70. if (vectorStoreId != null && threadId == null) {
  71. try {
  72. const thread = await this.client.createThread(vectorStoreId);
  73. await ThreadRelationModel.create({ userId, threadId: thread.id });
  74. return thread;
  75. }
  76. catch (err) {
  77. throw new Error(err);
  78. }
  79. }
  80. const threadRelation = await ThreadRelationModel.findOne({ threadId });
  81. if (threadRelation == null) {
  82. throw new Error('ThreadRelation document is not exists');
  83. }
  84. // Check if a thread entity exists
  85. // If the thread entity does not exist, the thread-relation document is deleted
  86. try {
  87. const thread = await this.client.retrieveThread(threadRelation.threadId);
  88. // Update expiration date if thread entity exists
  89. await threadRelation.updateThreadExpiration();
  90. return thread;
  91. }
  92. catch (err) {
  93. await openaiApiErrorHandler(err, { notFoundError: async() => { await threadRelation.remove() } });
  94. throw new Error(err);
  95. }
  96. }
  97. public async deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void> {
  98. const expiredThreadRelations = await ThreadRelationModel.getExpiredThreadRelations(limit);
  99. if (expiredThreadRelations == null) {
  100. return;
  101. }
  102. const deletedThreadIds: string[] = [];
  103. for await (const expiredThreadRelation of expiredThreadRelations) {
  104. try {
  105. const deleteThreadResponse = await this.client.deleteThread(expiredThreadRelation.threadId);
  106. logger.debug('Delete thread', deleteThreadResponse);
  107. deletedThreadIds.push(expiredThreadRelation.threadId);
  108. // sleep
  109. await new Promise(resolve => setTimeout(resolve, apiCallInterval));
  110. }
  111. catch (err) {
  112. logger.error(err);
  113. }
  114. }
  115. await ThreadRelationModel.deleteMany({ threadId: { $in: deletedThreadIds } });
  116. }
  117. // TODO: https://redmine.weseek.co.jp/issues/160332
  118. // public async getOrCreateVectorStoreForPublicScope(): Promise<VectorStoreDocument> {
  119. // const vectorStoreDocument: VectorStoreDocument | null = await VectorStoreModel.findOne({ scopeType: VectorStoreScopeType.PUBLIC, isDeleted: false });
  120. // if (vectorStoreDocument != null && isVectorStoreForPublicScopeExist) {
  121. // return vectorStoreDocument;
  122. // }
  123. // if (vectorStoreDocument != null && !isVectorStoreForPublicScopeExist) {
  124. // try {
  125. // // Check if vector store entity exists
  126. // // If the vector store entity does not exist, the vector store document is deleted
  127. // await this.client.retrieveVectorStore(vectorStoreDocument.vectorStoreId);
  128. // isVectorStoreForPublicScopeExist = true;
  129. // return vectorStoreDocument;
  130. // }
  131. // catch (err) {
  132. // await oepnaiApiErrorHandler(err, { notFoundError: vectorStoreDocument.markAsDeleted });
  133. // throw new Error(err);
  134. // }
  135. // }
  136. // const newVectorStore = await this.client.createVectorStore(VectorStoreScopeType.PUBLIC);
  137. // const newVectorStoreDocument = await VectorStoreModel.create({
  138. // vectorStoreId: newVectorStore.id,
  139. // scopeType: VectorStoreScopeType.PUBLIC,
  140. // }) as VectorStoreDocument;
  141. // isVectorStoreForPublicScopeExist = true;
  142. // return newVectorStoreDocument;
  143. // }
  144. private async createVectorStore(name: string): Promise<VectorStoreDocument> {
  145. try {
  146. const newVectorStore = await this.client.createVectorStore(name);
  147. const newVectorStoreDocument = await VectorStoreModel.create({
  148. vectorStoreId: newVectorStore.id,
  149. }) as VectorStoreDocument;
  150. return newVectorStoreDocument;
  151. }
  152. catch (err) {
  153. throw new Error(err);
  154. }
  155. }
  156. // TODO: https://redmine.weseek.co.jp/issues/160332
  157. // TODO: https://redmine.weseek.co.jp/issues/156643
  158. // private async uploadFileByChunks(pageId: Types.ObjectId, body: string, vectorStoreFileRelationsMap: VectorStoreFileRelationsMap) {
  159. // const chunks = await splitMarkdownIntoChunks(body, 'gpt-4o');
  160. // for await (const [index, chunk] of chunks.entries()) {
  161. // try {
  162. // const file = await toFile(Readable.from(chunk), `${pageId}-chunk-${index}.md`);
  163. // const uploadedFile = await this.client.uploadFile(file);
  164. // prepareVectorStoreFileRelations(pageId, uploadedFile.id, vectorStoreFileRelationsMap);
  165. // }
  166. // catch (err) {
  167. // logger.error(err);
  168. // }
  169. // }
  170. // }
  171. private async uploadFile(pageId: Types.ObjectId, pagePath: string, revisionBody: string): Promise<OpenAI.Files.FileObject> {
  172. const convertedHtml = await convertMarkdownToHtml({ pagePath, revisionBody });
  173. const file = await toFile(Readable.from(convertedHtml), `${pageId}.html`);
  174. const uploadedFile = await this.client.uploadFile(file);
  175. return uploadedFile;
  176. }
  177. private async deleteVectorStore(vectorStoreRelationId: string): Promise<void> {
  178. const vectorStoreDocument: VectorStoreDocument | null = await VectorStoreModel.findOne({ _id: vectorStoreRelationId, isDeleted: false });
  179. if (vectorStoreDocument == null) {
  180. return;
  181. }
  182. try {
  183. await this.client.deleteVectorStore(vectorStoreDocument.vectorStoreId);
  184. await vectorStoreDocument.markAsDeleted();
  185. }
  186. catch (err) {
  187. await openaiApiErrorHandler(err, { notFoundError: vectorStoreDocument.markAsDeleted });
  188. throw new Error(err);
  189. }
  190. }
  191. async createVectorStoreFile(vectorStoreRelation: VectorStoreDocument, pages: Array<HydratedDocument<PageDocument>>): Promise<void> {
  192. // const vectorStore = await this.getOrCreateVectorStoreForPublicScope();
  193. const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
  194. const processUploadFile = async(page: HydratedDocument<PageDocument>) => {
  195. if (page._id != null && page.grant === PageGrant.GRANT_PUBLIC && page.revision != null) {
  196. if (isPopulated(page.revision) && page.revision.body.length > 0) {
  197. const uploadedFile = await this.uploadFile(page._id, page.path, page.revision.body);
  198. prepareVectorStoreFileRelations(vectorStoreRelation._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
  199. return;
  200. }
  201. const pagePopulatedToShowRevision = await page.populateDataToShowRevision();
  202. if (pagePopulatedToShowRevision.revision != null && pagePopulatedToShowRevision.revision.body.length > 0) {
  203. const uploadedFile = await this.uploadFile(page._id, page.path, pagePopulatedToShowRevision.revision.body);
  204. prepareVectorStoreFileRelations(vectorStoreRelation._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
  205. }
  206. }
  207. };
  208. // Start workers to process results
  209. const workers = pages.map(processUploadFile);
  210. // Wait for all processing to complete.
  211. assert(workers.length <= BATCH_SIZE, 'workers.length must be less than or equal to BATCH_SIZE');
  212. const fileUploadResult = await Promise.allSettled(workers);
  213. fileUploadResult.forEach((result) => {
  214. if (result.status === 'rejected') {
  215. logger.error(result.reason);
  216. }
  217. });
  218. const vectorStoreFileRelations = Array.from(vectorStoreFileRelationsMap.values());
  219. const uploadedFileIds = vectorStoreFileRelations.map(data => data.fileIds).flat();
  220. if (uploadedFileIds.length === 0) {
  221. return;
  222. }
  223. const pageIds = pages.map(page => page._id);
  224. try {
  225. // Save vector store file relation
  226. await VectorStoreFileRelationModel.upsertVectorStoreFileRelations(vectorStoreFileRelations);
  227. // Create vector store file
  228. const createVectorStoreFileBatchResponse = await this.client.createVectorStoreFileBatch(vectorStoreRelation.vectorStoreId, uploadedFileIds);
  229. logger.debug('Create vector store file', createVectorStoreFileBatchResponse);
  230. // Set isAttachedToVectorStore: true when the uploaded file is attached to VectorStore
  231. await VectorStoreFileRelationModel.markAsAttachedToVectorStore(pageIds);
  232. }
  233. catch (err) {
  234. logger.error(err);
  235. // Delete all uploaded files if createVectorStoreFileBatch fails
  236. for await (const pageId of pageIds) {
  237. await this.deleteVectorStoreFile(vectorStoreRelation._id, pageId);
  238. }
  239. }
  240. }
  241. // Deletes all VectorStore documents that are marked as deleted (isDeleted: true) and have no associated VectorStoreFileRelation documents
  242. async deleteObsolatedVectorStoreRelations(): Promise<void> {
  243. const deletedVectorStoreRelations = await VectorStoreModel.find({ isDeleted: true });
  244. if (deletedVectorStoreRelations.length === 0) {
  245. return;
  246. }
  247. const currentVectorStoreRelationIds: Types.ObjectId[] = await VectorStoreFileRelationModel.aggregate([
  248. {
  249. $group: {
  250. _id: '$vectorStoreRelationId',
  251. relationCount: { $sum: 1 },
  252. },
  253. },
  254. { $match: { relationCount: { $gt: 0 } } },
  255. { $project: { _id: 1 } },
  256. ]);
  257. if (currentVectorStoreRelationIds.length === 0) {
  258. return;
  259. }
  260. await VectorStoreModel.deleteMany({ _id: { $nin: currentVectorStoreRelationIds }, isDeleted: true });
  261. }
  262. async deleteVectorStoreFile(vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId, apiCallInterval?: number): Promise<void> {
  263. // Delete vector store file and delete vector store file relation
  264. const vectorStoreFileRelation = await VectorStoreFileRelationModel.findOne({ vectorStoreRelationId, page: pageId });
  265. if (vectorStoreFileRelation == null) {
  266. return;
  267. }
  268. const deletedFileIds: string[] = [];
  269. for await (const fileId of vectorStoreFileRelation.fileIds) {
  270. try {
  271. const deleteFileResponse = await this.client.deleteFile(fileId);
  272. logger.debug('Delete vector store file', deleteFileResponse);
  273. deletedFileIds.push(fileId);
  274. if (apiCallInterval != null) {
  275. // sleep
  276. await new Promise(resolve => setTimeout(resolve, apiCallInterval));
  277. }
  278. }
  279. catch (err) {
  280. await openaiApiErrorHandler(err, { notFoundError: async() => { deletedFileIds.push(fileId) } });
  281. logger.error(err);
  282. }
  283. }
  284. const undeletedFileIds = vectorStoreFileRelation.fileIds.filter(fileId => !deletedFileIds.includes(fileId));
  285. if (undeletedFileIds.length === 0) {
  286. await vectorStoreFileRelation.remove();
  287. return;
  288. }
  289. vectorStoreFileRelation.fileIds = undeletedFileIds;
  290. await vectorStoreFileRelation.save();
  291. }
  292. async deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void> {
  293. // Retrieves all VectorStore documents that are marked as deleted
  294. const deletedVectorStoreRelations = await VectorStoreModel.find({ isDeleted: true });
  295. if (deletedVectorStoreRelations.length === 0) {
  296. return;
  297. }
  298. // Retrieves VectorStoreFileRelation documents associated with deleted VectorStore documents
  299. const obsoleteVectorStoreFileRelations = await VectorStoreFileRelationModel.find(
  300. { vectorStoreRelationId: { $in: deletedVectorStoreRelations.map(deletedVectorStoreRelation => deletedVectorStoreRelation._id) } },
  301. ).limit(limit);
  302. if (obsoleteVectorStoreFileRelations.length === 0) {
  303. return;
  304. }
  305. // Delete obsolete VectorStoreFile
  306. for await (const vectorStoreFileRelation of obsoleteVectorStoreFileRelations) {
  307. try {
  308. await this.deleteVectorStoreFile(vectorStoreFileRelation.vectorStoreRelationId, vectorStoreFileRelation.page, apiCallInterval);
  309. }
  310. catch (err) {
  311. logger.error(err);
  312. }
  313. }
  314. }
  315. // TODO: https://redmine.weseek.co.jp/issues/160332
  316. // async rebuildVectorStoreAll() {
  317. // await this.deleteVectorStore(VectorStoreScopeType.PUBLIC);
  318. // // Create all public pages VectorStoreFile
  319. // const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
  320. // const pagesStream = Page.find({ grant: PageGrant.GRANT_PUBLIC }).populate('revision').cursor({ batch_size: BATCH_SIZE });
  321. // const batchStrem = createBatchStream(BATCH_SIZE);
  322. // const createVectorStoreFile = this.createVectorStoreFile.bind(this);
  323. // const createVectorStoreFileStream = new Transform({
  324. // objectMode: true,
  325. // async transform(chunk: HydratedDocument<PageDocument>[], encoding, callback) {
  326. // await createVectorStoreFile(chunk);
  327. // this.push(chunk);
  328. // callback();
  329. // },
  330. // });
  331. // await pipeline(pagesStream, batchStrem, createVectorStoreFileStream);
  332. // }
  333. // async rebuildVectorStore(page: HydratedDocument<PageDocument>) {
  334. // const vectorStore = await this.getOrCreateVectorStoreForPublicScope();
  335. // await this.deleteVectorStoreFile(vectorStore._id, page._id);
  336. // await this.createVectorStoreFile([page]);
  337. // }
  338. private async createVectorStoreFileWithStream(vectorStoreRelation: VectorStoreDocument, conditions: mongoose.FilterQuery<PageDocument>): Promise<void> {
  339. const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
  340. const pagesStream = Page.find({ ...conditions })
  341. .populate('revision')
  342. .cursor({ batchSize: BATCH_SIZE });
  343. const batchStream = createBatchStream(BATCH_SIZE);
  344. const createVectorStoreFile = this.createVectorStoreFile.bind(this);
  345. const createVectorStoreFileStream = new Transform({
  346. objectMode: true,
  347. async transform(chunk: HydratedDocument<PageDocument>[], encoding, callback) {
  348. try {
  349. logger.debug('Search results of page paths', chunk.map(page => page.path));
  350. await createVectorStoreFile(vectorStoreRelation, chunk);
  351. this.push(chunk);
  352. callback();
  353. }
  354. catch (error) {
  355. callback(error);
  356. }
  357. },
  358. });
  359. await pipeline(pagesStream, batchStream, createVectorStoreFileStream);
  360. }
  361. private async createConditionForCreateVectorStoreFile(
  362. owner: AiAssistant['owner'],
  363. accessScope: AiAssistant['accessScope'],
  364. grantedGroupsForAccessScope: AiAssistant['grantedGroupsForAccessScope'],
  365. pagePathPatterns: AiAssistant['pagePathPatterns'],
  366. ): Promise<mongoose.FilterQuery<PageDocument>> {
  367. const converterdPagePathPatterns = convertPathPatternsToRegExp(pagePathPatterns);
  368. // Include pages in search targets when their paths with 'Anyone with the link' permission are directly specified instead of using glob pattern
  369. const nonGrabPagePathPatterns = pagePathPatterns.filter(pagePathPattern => !isGrobPatternPath(pagePathPattern));
  370. const baseCondition: mongoose.FilterQuery<PageDocument> = {
  371. grant: PageGrant.GRANT_RESTRICTED,
  372. path: { $in: nonGrabPagePathPatterns },
  373. };
  374. if (accessScope === AiAssistantAccessScope.PUBLIC_ONLY) {
  375. return {
  376. $or: [
  377. baseCondition,
  378. {
  379. grant: PageGrant.GRANT_PUBLIC,
  380. path: { $in: converterdPagePathPatterns },
  381. },
  382. ],
  383. };
  384. }
  385. if (accessScope === AiAssistantAccessScope.GROUPS) {
  386. if (grantedGroupsForAccessScope == null || grantedGroupsForAccessScope.length === 0) {
  387. throw new Error('grantedGroups is required when accessScope is GROUPS');
  388. }
  389. const extractedGrantedGroupIdsForAccessScope = grantedGroupsForAccessScope.map(group => getIdForRef(group.item).toString());
  390. return {
  391. $or: [
  392. baseCondition,
  393. {
  394. grant: { $in: [PageGrant.GRANT_PUBLIC, PageGrant.GRANT_USER_GROUP] },
  395. path: { $in: converterdPagePathPatterns },
  396. $or: [
  397. { 'grantedGroups.item': { $in: extractedGrantedGroupIdsForAccessScope } },
  398. { grant: PageGrant.GRANT_PUBLIC },
  399. ],
  400. },
  401. ],
  402. };
  403. }
  404. if (accessScope === AiAssistantAccessScope.OWNER) {
  405. const ownerUserGroups = [
  406. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  407. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  408. ].map(group => group.toString());
  409. return {
  410. $or: [
  411. baseCondition,
  412. {
  413. grant: { $in: [PageGrant.GRANT_PUBLIC, PageGrant.GRANT_USER_GROUP, PageGrant.GRANT_OWNER] },
  414. path: { $in: converterdPagePathPatterns },
  415. $or: [
  416. { 'grantedGroups.item': { $in: ownerUserGroups } },
  417. { grantedUsers: { $in: [getIdForRef(owner)] } },
  418. { grant: PageGrant.GRANT_PUBLIC },
  419. ],
  420. },
  421. ],
  422. };
  423. }
  424. throw new Error('Invalid accessScope value');
  425. }
  426. private async validateGrantedUserGroupsForAiAssistant(
  427. owner: AiAssistant['owner'],
  428. shareScope: AiAssistant['shareScope'],
  429. accessScope: AiAssistant['accessScope'],
  430. grantedGroupsForShareScope: AiAssistant['grantedGroupsForShareScope'],
  431. grantedGroupsForAccessScope: AiAssistant['grantedGroupsForAccessScope'],
  432. ) {
  433. // Check if grantedGroupsForShareScope is not specified when shareScope is not a “group”
  434. if (shareScope !== AiAssistantShareScope.GROUPS && grantedGroupsForShareScope != null) {
  435. throw new Error('grantedGroupsForShareScope is specified when shareScope is not “groups”.');
  436. }
  437. // Check if grantedGroupsForAccessScope is not specified when accessScope is not a “group”
  438. if (accessScope !== AiAssistantAccessScope.GROUPS && grantedGroupsForAccessScope != null) {
  439. throw new Error('grantedGroupsForAccessScope is specified when accsessScope is not “groups”.');
  440. }
  441. const ownerUserGroupIds = [
  442. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  443. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  444. ].map(group => group.toString());
  445. // Check if the owner belongs to the group specified in grantedGroupsForShareScope
  446. if (grantedGroupsForShareScope != null && grantedGroupsForShareScope.length > 0) {
  447. const extractedGrantedGroupIdsForShareScope = grantedGroupsForShareScope.map(group => getIdForRef(group.item).toString());
  448. const isValid = extractedGrantedGroupIdsForShareScope.every(groupId => ownerUserGroupIds.includes(groupId));
  449. if (!isValid) {
  450. throw new Error('A userGroup to which the owner does not belong is specified in grantedGroupsForShareScope');
  451. }
  452. }
  453. // Check if the owner belongs to the group specified in grantedGroupsForAccessScope
  454. if (grantedGroupsForAccessScope != null && grantedGroupsForAccessScope.length > 0) {
  455. const extractedGrantedGroupIdsForAccessScope = grantedGroupsForAccessScope.map(group => getIdForRef(group.item).toString());
  456. const isValid = extractedGrantedGroupIdsForAccessScope.every(groupId => ownerUserGroupIds.includes(groupId));
  457. if (!isValid) {
  458. throw new Error('A userGroup to which the owner does not belong is specified in grantedGroupsForAccessScope');
  459. }
  460. }
  461. }
  462. async createAiAssistant(data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument> {
  463. await this.validateGrantedUserGroupsForAiAssistant(
  464. data.owner,
  465. data.shareScope,
  466. data.accessScope,
  467. data.grantedGroupsForShareScope,
  468. data.grantedGroupsForAccessScope,
  469. );
  470. const conditions = await this.createConditionForCreateVectorStoreFile(
  471. data.owner,
  472. data.accessScope,
  473. data.grantedGroupsForAccessScope,
  474. data.pagePathPatterns,
  475. );
  476. const vectorStoreRelation = await this.createVectorStore(data.name);
  477. const aiAssistant = await AiAssistantModel.create({
  478. ...data, vectorStore: vectorStoreRelation,
  479. });
  480. // VectorStore creation process does not await
  481. this.createVectorStoreFileWithStream(vectorStoreRelation, conditions);
  482. return aiAssistant;
  483. }
  484. async updateAiAssistant(aiAssistantId: string, data: Omit<AiAssistant, 'vectorStore'>): Promise<AiAssistantDocument> {
  485. const aiAssistant = await AiAssistantModel.findOne({ owner: data.owner, _id: aiAssistantId });
  486. if (aiAssistant == null) {
  487. throw createError(404, 'AiAssistant document does not exist');
  488. }
  489. await this.validateGrantedUserGroupsForAiAssistant(
  490. data.owner,
  491. data.shareScope,
  492. data.accessScope,
  493. data.grantedGroupsForShareScope,
  494. data.grantedGroupsForAccessScope,
  495. );
  496. const grantedGroupIdsForAccessScopeFromReq = data.grantedGroupsForAccessScope?.map(group => getIdStringForRef(group.item)) ?? []; // ObjectId[] -> string[]
  497. const grantedGroupIdsForAccessScopeFromDb = aiAssistant.grantedGroupsForAccessScope?.map(group => getIdStringForRef(group.item)) ?? []; // ObjectId[] -> string[]
  498. // If accessScope, pagePathPatterns, grantedGroupsForAccessScope have not changed, do not build VectorStore
  499. const shouldRebuildVectorStore = data.accessScope !== aiAssistant.accessScope
  500. || !isDeepEquals(data.pagePathPatterns, aiAssistant.pagePathPatterns)
  501. || !isDeepEquals(grantedGroupIdsForAccessScopeFromReq, grantedGroupIdsForAccessScopeFromDb);
  502. let newVectorStoreRelation: VectorStoreDocument | undefined;
  503. if (shouldRebuildVectorStore) {
  504. const conditions = await this.createConditionForCreateVectorStoreFile(
  505. data.owner,
  506. data.accessScope,
  507. data.grantedGroupsForAccessScope,
  508. data.pagePathPatterns,
  509. );
  510. // Delete obsoleted VectorStore
  511. const obsoletedVectorStoreRelationId = getIdStringForRef(aiAssistant.vectorStore);
  512. await this.deleteVectorStore(obsoletedVectorStoreRelationId);
  513. newVectorStoreRelation = await this.createVectorStore(data.name);
  514. // VectorStore creation process does not await
  515. this.createVectorStoreFileWithStream(newVectorStoreRelation, conditions);
  516. }
  517. const newData = {
  518. ...data,
  519. vectorStore: newVectorStoreRelation ?? aiAssistant.vectorStore,
  520. };
  521. aiAssistant.set({ ...newData });
  522. const updatedAiAssistant = await aiAssistant.save();
  523. return updatedAiAssistant;
  524. }
  525. async getAccessibleAiAssistants(user: IUserHasId): Promise<AccessibleAiAssistants> {
  526. const userGroupIds = [
  527. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
  528. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
  529. ];
  530. const assistants = await AiAssistantModel.find({
  531. $or: [
  532. // Case 1: Assistants owned by the user
  533. { owner: user },
  534. // Case 2: Public assistants owned by others
  535. {
  536. $and: [
  537. { owner: { $ne: user } },
  538. { shareScope: AiAssistantShareScope.PUBLIC_ONLY },
  539. ],
  540. },
  541. // Case 3: Group-restricted assistants where user is in granted groups
  542. {
  543. $and: [
  544. { owner: { $ne: user } },
  545. { shareScope: AiAssistantShareScope.GROUPS },
  546. { 'grantedGroupsForShareScope.item': { $in: userGroupIds } },
  547. ],
  548. },
  549. ],
  550. });
  551. return {
  552. myAiAssistants: assistants.filter(assistant => assistant.owner.toString() === user._id.toString()) ?? [],
  553. teamAiAssistants: assistants.filter(assistant => assistant.owner.toString() !== user._id.toString()) ?? [],
  554. };
  555. }
  556. async deleteAiAssistant(ownerId: string, aiAssistantId: string): Promise<AiAssistantDocument> {
  557. const aiAssistant = await AiAssistantModel.findOne({ owner: ownerId, _id: aiAssistantId });
  558. if (aiAssistant == null) {
  559. throw createError(404, 'AiAssistant document does not exist');
  560. }
  561. const vectorStoreRelationId = getIdStringForRef(aiAssistant.vectorStore);
  562. await this.deleteVectorStore(vectorStoreRelationId);
  563. const deletedAiAssistant = await aiAssistant.remove();
  564. return deletedAiAssistant;
  565. }
  566. }
  567. let instance: OpenaiService;
  568. export const getOpenaiService = (): IOpenaiService | undefined => {
  569. if (instance != null) {
  570. return instance;
  571. }
  572. const aiEnabled = configManager.getConfig('app:aiEnabled');
  573. const openaiServiceType = configManager.getConfig('openai:serviceType');
  574. if (aiEnabled && openaiServiceType != null && OpenaiServiceTypes.includes(openaiServiceType)) {
  575. instance = new OpenaiService();
  576. return instance;
  577. }
  578. return;
  579. };