openai.ts 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. import fs from 'fs';
  2. import assert from 'node:assert';
  3. import { Readable, Transform, Writable } from 'stream';
  4. import { pipeline } from 'stream/promises';
  5. import type {
  6. IUser, Ref, Lang, IPage, Nullable,
  7. } from '@growi/core';
  8. import {
  9. PageGrant, getIdForRef, getIdStringForRef, isPopulated, type IUserHasId,
  10. } from '@growi/core';
  11. import { deepEquals } from '@growi/core/dist/utils';
  12. import { isGlobPatternPath } from '@growi/core/dist/utils/page-path-utils';
  13. import escapeStringRegexp from 'escape-string-regexp';
  14. import createError from 'http-errors';
  15. import mongoose, { type HydratedDocument, type Types } from 'mongoose';
  16. import { type OpenAI, toFile } from 'openai';
  17. import { type ChatCompletionChunk } from 'openai/resources/chat/completions';
  18. import ExternalUserGroupRelation from '~/features/external-user-group/server/models/external-user-group-relation';
  19. import ThreadRelationModel, { type ThreadRelationDocument } from '~/features/openai/server/models/thread-relation';
  20. import VectorStoreModel, { type VectorStoreDocument } from '~/features/openai/server/models/vector-store';
  21. import VectorStoreFileRelationModel, {
  22. type VectorStoreFileRelation,
  23. prepareVectorStoreFileRelations,
  24. } from '~/features/openai/server/models/vector-store-file-relation';
  25. import type Crowi from '~/server/crowi';
  26. import type { IAttachmentDocument, IAttachmentModel } from '~/server/models/attachment';
  27. import type { PageDocument, PageModel } from '~/server/models/page';
  28. import UserGroupRelation from '~/server/models/user-group-relation';
  29. import { configManager } from '~/server/service/config-manager';
  30. import { createBatchStream } from '~/server/util/batch-stream';
  31. import loggerFactory from '~/utils/logger';
  32. import { OpenaiServiceTypes } from '../../interfaces/ai';
  33. import type { UpsertAiAssistantData } from '../../interfaces/ai-assistant';
  34. import {
  35. type AccessibleAiAssistants, type AiAssistant, AiAssistantAccessScope, AiAssistantShareScope,
  36. } from '../../interfaces/ai-assistant';
  37. import type { MessageListParams } from '../../interfaces/message';
  38. import { ThreadType } from '../../interfaces/thread-relation';
  39. import type { IVectorStore } from '../../interfaces/vector-store';
  40. import { removeGlobPath } from '../../utils/remove-glob-path';
  41. import AiAssistantModel, { type AiAssistantDocument } from '../models/ai-assistant';
  42. import { convertMarkdownToHtml } from '../utils/convert-markdown-to-html';
  43. import { generateGlobPatterns } from '../utils/generate-glob-patterns';
  44. import { isVectorStoreCompatible } from '../utils/is-vector-store-compatible';
  45. import { getClient, isStreamResponse } from './client-delegator';
  46. import { openaiApiErrorHandler } from './openai-api-error-handler';
  47. import { replaceAnnotationWithPageLink } from './replace-annotation-with-page-link';
  48. const { isDeepEquals } = deepEquals;
  49. const BATCH_SIZE = 100;
  50. const logger = loggerFactory('growi:service:openai');
  51. type VectorStoreFileRelationsMap = Map<string, VectorStoreFileRelation>
  52. const convertPathPatternsToRegExp = (pagePathPatterns: string[]): Array<string | RegExp> => {
  53. return pagePathPatterns.map((pagePathPattern) => {
  54. if (isGlobPatternPath(pagePathPattern)) {
  55. const trimedPagePathPattern = pagePathPattern.replace('/*', '');
  56. const escapedPagePathPattern = escapeStringRegexp(trimedPagePathPattern);
  57. // https://regex101.com/r/x5KIZL/1
  58. return new RegExp(`^${escapedPagePathPattern}($|/)`);
  59. }
  60. return pagePathPattern;
  61. });
  62. };
  63. export interface IOpenaiService {
  64. generateAndProcessPreMessage(message: string, chunkProcessor: (chunk: ChatCompletionChunk) => void): Promise<void>
  65. createThread(userId: string, type: ThreadType, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument>;
  66. getThreadsByAiAssistantId(aiAssistantId: string): Promise<ThreadRelationDocument[]>
  67. deleteThread(threadRelationId: string): Promise<ThreadRelationDocument>;
  68. deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
  69. deleteObsoletedVectorStoreRelations(): Promise<void> // for CronJob
  70. deleteVectorStore(vectorStoreRelationId: string): Promise<void>;
  71. getMessageData(threadId: string, lang?: Lang, options?: MessageListParams): Promise<OpenAI.Beta.Threads.Messages.MessagesPage>;
  72. createVectorStoreFileOnPageCreate(pages: PageDocument[]): Promise<void>;
  73. updateVectorStoreFileOnPageUpdate(page: HydratedDocument<PageDocument>): Promise<void>;
  74. deleteVectorStoreFilesByPageIds(pageIds: Types.ObjectId[]): Promise<void>;
  75. deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void>; // for CronJob
  76. isAiAssistantUsable(aiAssistantId: string, user: IUserHasId): Promise<boolean>;
  77. createAiAssistant(data: UpsertAiAssistantData, user: IUserHasId): Promise<AiAssistantDocument>;
  78. updateAiAssistant(aiAssistantId: string, data: UpsertAiAssistantData, user: IUserHasId): Promise<AiAssistantDocument>;
  79. getAccessibleAiAssistants(user: IUserHasId): Promise<AccessibleAiAssistants>
  80. isLearnablePageLimitExceeded(user: IUserHasId, pagePathPatterns: string[]): Promise<boolean>;
  81. }
  82. class OpenaiService implements IOpenaiService {
  83. private crowi: Crowi;
  84. constructor(crowi: Crowi) {
  85. this.crowi = crowi;
  86. this.createVectorStoreFileOnUploadAttachment = this.createVectorStoreFileOnUploadAttachment.bind(this);
  87. crowi.attachmentService.addAttachHandler(this.createVectorStoreFileOnUploadAttachment);
  88. this.deleteVectorStoreFileOnDeleteAttachment = this.deleteVectorStoreFileOnDeleteAttachment.bind(this);
  89. crowi.attachmentService.addDetachHandler(this.deleteVectorStoreFileOnDeleteAttachment);
  90. }
  91. private get client() {
  92. const openaiServiceType = configManager.getConfig('openai:serviceType');
  93. return getClient({ openaiServiceType });
  94. }
  95. async generateAndProcessPreMessage(message: string, chunkProcessor: (delta: ChatCompletionChunk) => void): Promise<void> {
  96. const systemMessage = [
  97. "Generate a message briefly confirming the user's question.",
  98. 'Please generate up to 20 characters',
  99. ].join('');
  100. const preMessageCompletion = await this.client.chatCompletion({
  101. stream: true,
  102. model: 'gpt-4.1-nano',
  103. messages: [
  104. {
  105. role: 'system',
  106. content: systemMessage,
  107. },
  108. {
  109. role: 'user',
  110. content: message,
  111. },
  112. ],
  113. });
  114. if (!isStreamResponse(preMessageCompletion)) {
  115. return;
  116. }
  117. for await (const chunk of preMessageCompletion) {
  118. chunkProcessor(chunk);
  119. }
  120. }
  121. private async generateThreadTitle(message: string): Promise<Nullable<string>> {
  122. const systemMessage = [
  123. 'Create a brief title (max 5 words) from your message.',
  124. 'Respond in the same language the user uses in their input.',
  125. 'Response should only contain the title.',
  126. ].join('');
  127. const threadTitleCompletion = await this.client.chatCompletion({
  128. model: 'gpt-4.1-nano',
  129. messages: [
  130. {
  131. role: 'system',
  132. content: systemMessage,
  133. },
  134. {
  135. role: 'user',
  136. content: message,
  137. },
  138. ],
  139. });
  140. if (!isStreamResponse(threadTitleCompletion)) {
  141. const threadTitle = threadTitleCompletion.choices[0].message.content;
  142. return threadTitle;
  143. }
  144. }
  145. async createThread(userId: string, type: ThreadType, aiAssistantId?: string, initialUserMessage?: string): Promise<ThreadRelationDocument> {
  146. try {
  147. const aiAssistant = aiAssistantId != null
  148. ? await AiAssistantModel.findOne({ _id: { $eq: aiAssistantId } }).populate<{ vectorStore: IVectorStore }>('vectorStore')
  149. : null;
  150. const thread = await this.client.createThread(aiAssistant?.vectorStore?.vectorStoreId);
  151. const threadRelation = await ThreadRelationModel.create({
  152. userId,
  153. type,
  154. aiAssistant: aiAssistantId,
  155. threadId: thread.id,
  156. title: null, // Initialize title as null
  157. });
  158. if (initialUserMessage != null) {
  159. // Do not await, run in background
  160. this.generateThreadTitle(initialUserMessage)
  161. .then(async(generatedTitle) => {
  162. if (generatedTitle != null) {
  163. threadRelation.title = generatedTitle;
  164. await threadRelation.save();
  165. }
  166. })
  167. .catch((err) => {
  168. logger.error(`Failed to generate thread title for threadId ${thread.id}:`, err);
  169. });
  170. }
  171. return threadRelation;
  172. }
  173. catch (err) {
  174. throw err;
  175. }
  176. }
  177. private async updateThreads(aiAssistantId: string, vectorStoreId: string): Promise<void> {
  178. const threadRelations = await this.getThreadsByAiAssistantId(aiAssistantId);
  179. for await (const threadRelation of threadRelations) {
  180. try {
  181. const updatedThreadResponse = await this.client.updateThread(threadRelation.threadId, vectorStoreId);
  182. logger.debug('Update thread', updatedThreadResponse);
  183. }
  184. catch (err) {
  185. logger.error(err);
  186. }
  187. }
  188. }
  189. async getThreadsByAiAssistantId(aiAssistantId: string, type: ThreadType = ThreadType.KNOWLEDGE): Promise<ThreadRelationDocument[]> {
  190. const threadRelations = await ThreadRelationModel
  191. .find({ aiAssistant: aiAssistantId, type })
  192. .sort({ updatedAt: -1 });
  193. return threadRelations;
  194. }
  195. async deleteThread(threadRelationId: string): Promise<ThreadRelationDocument> {
  196. const threadRelation = await ThreadRelationModel.findById(threadRelationId);
  197. if (threadRelation == null) {
  198. throw createError(404, 'ThreadRelation document does not exist');
  199. }
  200. try {
  201. const deletedThreadResponse = await this.client.deleteThread(threadRelation.threadId);
  202. logger.debug('Delete thread', deletedThreadResponse);
  203. await threadRelation.remove();
  204. }
  205. catch (err) {
  206. await openaiApiErrorHandler(err, { notFoundError: async() => { await threadRelation.remove() } });
  207. throw err;
  208. }
  209. return threadRelation;
  210. }
  211. public async deleteExpiredThreads(limit: number, apiCallInterval: number): Promise<void> {
  212. const expiredThreadRelations = await ThreadRelationModel.getExpiredThreadRelations(limit);
  213. if (expiredThreadRelations == null) {
  214. return;
  215. }
  216. const deletedThreadIds: string[] = [];
  217. for await (const expiredThreadRelation of expiredThreadRelations) {
  218. try {
  219. const deleteThreadResponse = await this.client.deleteThread(expiredThreadRelation.threadId);
  220. logger.debug('Delete thread', deleteThreadResponse);
  221. deletedThreadIds.push(expiredThreadRelation.threadId);
  222. // sleep
  223. await new Promise(resolve => setTimeout(resolve, apiCallInterval));
  224. }
  225. catch (err) {
  226. logger.error(err);
  227. }
  228. }
  229. await ThreadRelationModel.deleteMany({ threadId: { $in: deletedThreadIds } });
  230. }
  231. async getMessageData(threadId: string, lang?: Lang, options?: MessageListParams): Promise<OpenAI.Beta.Threads.Messages.MessagesPage> {
  232. const messages = await this.client.getMessages(threadId, options);
  233. for await (const message of messages.data) {
  234. for await (const content of message.content) {
  235. if (content.type === 'text') {
  236. await replaceAnnotationWithPageLink(content, lang);
  237. }
  238. }
  239. }
  240. return messages;
  241. }
  242. private async getVectorStoreRelationsByPageIds(pageIds: Types.ObjectId[]): Promise<VectorStoreDocument[]> {
  243. const pipeline = [
  244. // Stage 1: Match documents with the given pageId
  245. {
  246. $match: {
  247. page: {
  248. $in: pageIds,
  249. },
  250. },
  251. },
  252. // Stage 2: Lookup VectorStore documents
  253. {
  254. $lookup: {
  255. from: 'vectorstores',
  256. localField: 'vectorStoreRelationId',
  257. foreignField: '_id',
  258. as: 'vectorStore',
  259. },
  260. },
  261. // Stage 3: Unwind the vectorStore array
  262. {
  263. $unwind: '$vectorStore',
  264. },
  265. // Stage 4: Match non-deleted vector stores
  266. {
  267. $match: {
  268. 'vectorStore.isDeleted': false,
  269. },
  270. },
  271. // Stage 5: Replace the root with vectorStore document
  272. {
  273. $replaceRoot: {
  274. newRoot: '$vectorStore',
  275. },
  276. },
  277. // Stage 6: Group by _id to remove duplicates
  278. {
  279. $group: {
  280. _id: '$_id',
  281. doc: { $first: '$$ROOT' },
  282. },
  283. },
  284. // Stage 7: Restore the document structure
  285. {
  286. $replaceRoot: {
  287. newRoot: '$doc',
  288. },
  289. },
  290. ];
  291. const vectorStoreRelations = await VectorStoreFileRelationModel.aggregate<VectorStoreDocument>(pipeline);
  292. return vectorStoreRelations;
  293. }
  294. private async createVectorStore(name: string): Promise<VectorStoreDocument> {
  295. try {
  296. const newVectorStore = await this.client.createVectorStore(name);
  297. const newVectorStoreDocument = await VectorStoreModel.create({
  298. vectorStoreId: newVectorStore.id,
  299. }) as VectorStoreDocument;
  300. return newVectorStoreDocument;
  301. }
  302. catch (err) {
  303. throw new Error(err);
  304. }
  305. }
  306. private async uploadFile(revisionBody: string, page: HydratedDocument<PageDocument>): Promise<OpenAI.Files.FileObject> {
  307. const siteUrl = configManager.getConfig('app:siteUrl');
  308. const convertedHtml = await convertMarkdownToHtml(revisionBody, { page, siteUrl });
  309. const file = await toFile(Readable.from(convertedHtml), `${page._id}.html`);
  310. const uploadedFile = await this.client.uploadFile(file);
  311. return uploadedFile;
  312. }
  313. private async uploadFileForAttachment(fileName: string, readStream?: NodeJS.ReadableStream, filePath?: string): Promise<OpenAI.Files.FileObject> {
  314. const streamSource: NodeJS.ReadableStream = (() => {
  315. if (readStream != null) {
  316. return readStream;
  317. }
  318. if (filePath != null) {
  319. return fs.createReadStream(filePath);
  320. }
  321. throw new Error('readStream and filePath are both null');
  322. })();
  323. const uploadableFile = await toFile(
  324. streamSource,
  325. fileName,
  326. );
  327. const uploadedFile = await this.client.uploadFile(uploadableFile);
  328. return uploadedFile;
  329. }
  330. async deleteVectorStore(vectorStoreRelationId: string): Promise<void> {
  331. const vectorStoreDocument: VectorStoreDocument | null = await VectorStoreModel.findOne({ _id: vectorStoreRelationId, isDeleted: false });
  332. if (vectorStoreDocument == null) {
  333. return;
  334. }
  335. try {
  336. const deleteVectorStoreResponse = await this.client.deleteVectorStore(vectorStoreDocument.vectorStoreId);
  337. logger.debug('Delete vector store', deleteVectorStoreResponse);
  338. await vectorStoreDocument.markAsDeleted();
  339. }
  340. catch (err) {
  341. await openaiApiErrorHandler(err, { notFoundError: vectorStoreDocument.markAsDeleted });
  342. throw new Error(err);
  343. }
  344. }
  345. private async createVectorStoreFileWithStreamForAttachment(
  346. pageId: Types.ObjectId, vectorStoreRelationId: Types.ObjectId, vectorStoreFileRelationsMap: VectorStoreFileRelationsMap,
  347. ): Promise<void> {
  348. const Attachment = mongoose.model<HydratedDocument<IAttachmentDocument>, IAttachmentModel>('Attachment');
  349. const attachmentsCursor = Attachment.find({ page: pageId }).cursor();
  350. const batchStream = createBatchStream(BATCH_SIZE);
  351. const uploadFileStreamForAttachment = new Writable({
  352. objectMode: true,
  353. write: async(attachments: HydratedDocument<IAttachmentDocument>[], _encoding, callback) => {
  354. for await (const attachment of attachments) {
  355. try {
  356. if (!isVectorStoreCompatible(attachment.originalName, attachment.fileFormat)) {
  357. continue;
  358. }
  359. const readStream = await this.crowi.fileUploadService.findDeliveryFile(attachment);
  360. const uploadedFileForAttachment = await this.uploadFileForAttachment(attachment.originalName, readStream);
  361. prepareVectorStoreFileRelations(
  362. vectorStoreRelationId, pageId, uploadedFileForAttachment.id, vectorStoreFileRelationsMap, attachment._id,
  363. );
  364. }
  365. catch (err) {
  366. logger.error(err);
  367. }
  368. }
  369. callback();
  370. },
  371. final: (callback) => {
  372. logger.debug('Finished uploading attachments');
  373. callback();
  374. },
  375. });
  376. await pipeline(attachmentsCursor, batchStream, uploadFileStreamForAttachment);
  377. }
  378. private async createVectorStoreFile(
  379. vectorStoreRelation: VectorStoreDocument, pages: Array<HydratedDocument<PageDocument>>, ignoreAttachments = false,
  380. ): Promise<void> {
  381. const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
  382. const processUploadFile = async(page: HydratedDocument<PageDocument>) => {
  383. if (page._id != null && page.revision != null) {
  384. if (isPopulated(page.revision) && page.revision.body.length > 0) {
  385. const uploadedFile = await this.uploadFile(page.revision.body, page);
  386. prepareVectorStoreFileRelations(vectorStoreRelation._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
  387. if (!ignoreAttachments) {
  388. await this.createVectorStoreFileWithStreamForAttachment(page._id, vectorStoreRelation._id, vectorStoreFileRelationsMap);
  389. }
  390. return;
  391. }
  392. const pagePopulatedToShowRevision = await page.populateDataToShowRevision();
  393. if (pagePopulatedToShowRevision.revision != null && pagePopulatedToShowRevision.revision.body.length > 0) {
  394. const uploadedFile = await this.uploadFile(pagePopulatedToShowRevision.revision.body, page);
  395. prepareVectorStoreFileRelations(vectorStoreRelation._id, page._id, uploadedFile.id, vectorStoreFileRelationsMap);
  396. if (!ignoreAttachments) {
  397. await this.createVectorStoreFileWithStreamForAttachment(page._id, vectorStoreRelation._id, vectorStoreFileRelationsMap);
  398. }
  399. }
  400. }
  401. };
  402. // Start workers to process results
  403. const workers = pages.map(processUploadFile);
  404. // Wait for all processing to complete.
  405. assert(workers.length <= BATCH_SIZE, 'workers.length must be less than or equal to BATCH_SIZE');
  406. const fileUploadResult = await Promise.allSettled(workers);
  407. fileUploadResult.forEach((result) => {
  408. if (result.status === 'rejected') {
  409. logger.error(result.reason);
  410. }
  411. });
  412. const vectorStoreFileRelations = Array.from(vectorStoreFileRelationsMap.values());
  413. const uploadedFileIds = vectorStoreFileRelations.map(data => data.fileIds).flat();
  414. if (uploadedFileIds.length === 0) {
  415. return;
  416. }
  417. const pageIds = pages.map(page => page._id);
  418. try {
  419. // Save vector store file relation
  420. await VectorStoreFileRelationModel.upsertVectorStoreFileRelations(vectorStoreFileRelations);
  421. // Create vector store file
  422. const createVectorStoreFileBatchResponse = await this.client.createVectorStoreFileBatch(vectorStoreRelation.vectorStoreId, uploadedFileIds);
  423. logger.debug('Create vector store file', createVectorStoreFileBatchResponse);
  424. // Set isAttachedToVectorStore: true when the uploaded file is attached to VectorStore
  425. await VectorStoreFileRelationModel.markAsAttachedToVectorStore(pageIds);
  426. }
  427. catch (err) {
  428. logger.error(err);
  429. // Delete all uploaded files if createVectorStoreFileBatch fails
  430. for await (const pageId of pageIds) {
  431. await this.deleteVectorStoreFile(vectorStoreRelation._id, pageId);
  432. }
  433. }
  434. }
  435. // Deletes all VectorStore documents that are marked as deleted (isDeleted: true) and have no associated VectorStoreFileRelation documents
  436. async deleteObsoletedVectorStoreRelations(): Promise<void> {
  437. const deletedVectorStoreRelations = await VectorStoreModel.find({ isDeleted: true });
  438. if (deletedVectorStoreRelations.length === 0) {
  439. return;
  440. }
  441. const currentVectorStoreRelationIds: Types.ObjectId[] = await VectorStoreFileRelationModel.aggregate([
  442. {
  443. $group: {
  444. _id: '$vectorStoreRelationId',
  445. relationCount: { $sum: 1 },
  446. },
  447. },
  448. { $match: { relationCount: { $gt: 0 } } },
  449. { $project: { _id: 1 } },
  450. ]);
  451. if (currentVectorStoreRelationIds.length === 0) {
  452. return;
  453. }
  454. await VectorStoreModel.deleteMany({ _id: { $nin: currentVectorStoreRelationIds }, isDeleted: true });
  455. }
  456. private async deleteVectorStoreFileForAttachment(vectorStoreFileRelation: VectorStoreFileRelation): Promise<void> {
  457. if (vectorStoreFileRelation.attachment == null) {
  458. return;
  459. }
  460. const deleteAllAttachmentVectorStoreFileRelations = async() => {
  461. await VectorStoreFileRelationModel.deleteMany({ attachment: vectorStoreFileRelation.attachment });
  462. };
  463. try {
  464. // Delete entities in VectorStoreFile
  465. const fileId = vectorStoreFileRelation.fileIds[0];
  466. const deleteFileResponse = await this.client.deleteFile(fileId);
  467. logger.debug('Delete vector store file (attachment) ', deleteFileResponse);
  468. // Delete related VectorStoreFileRelation document
  469. const attachmentId = vectorStoreFileRelation.attachment;
  470. if (attachmentId != null) {
  471. await deleteAllAttachmentVectorStoreFileRelations();
  472. }
  473. }
  474. catch (err) {
  475. logger.error(err);
  476. await openaiApiErrorHandler(err, {
  477. notFoundError: () => deleteAllAttachmentVectorStoreFileRelations(),
  478. });
  479. }
  480. }
  481. private async deleteVectorStoreFile(
  482. vectorStoreRelationId: Types.ObjectId, pageId: Types.ObjectId, ignoreAttachments = false, apiCallInterval?: number,
  483. ): Promise<void> {
  484. if (!ignoreAttachments) {
  485. // Get all VectorStoreFIleDocument (attachments) associated with the page
  486. const vectorStoreFileRelationsForAttachment = await VectorStoreFileRelationModel.find({
  487. vectorStoreRelationId, page: pageId, attachment: { $exists: true },
  488. });
  489. if (vectorStoreFileRelationsForAttachment.length !== 0) {
  490. for await (const vectorStoreFileRelation of vectorStoreFileRelationsForAttachment) {
  491. try {
  492. await this.deleteVectorStoreFileForAttachment(vectorStoreFileRelation);
  493. }
  494. catch (err) {
  495. logger.error(err);
  496. }
  497. }
  498. }
  499. }
  500. // Delete vector store file and delete vector store file relation
  501. const vectorStoreFileRelation = await VectorStoreFileRelationModel.findOne({ vectorStoreRelationId, page: pageId });
  502. if (vectorStoreFileRelation == null) {
  503. return;
  504. }
  505. const deletedFileIds: string[] = [];
  506. for await (const fileId of vectorStoreFileRelation.fileIds) {
  507. try {
  508. const deleteFileResponse = await this.client.deleteFile(fileId);
  509. logger.debug('Delete vector store file', deleteFileResponse);
  510. deletedFileIds.push(fileId);
  511. if (apiCallInterval != null) {
  512. // sleep
  513. await new Promise(resolve => setTimeout(resolve, apiCallInterval));
  514. }
  515. }
  516. catch (err) {
  517. await openaiApiErrorHandler(err, { notFoundError: async() => { deletedFileIds.push(fileId) } });
  518. logger.error(err);
  519. }
  520. }
  521. const undeletedFileIds = vectorStoreFileRelation.fileIds.filter(fileId => !deletedFileIds.includes(fileId));
  522. if (undeletedFileIds.length === 0) {
  523. await vectorStoreFileRelation.remove();
  524. return;
  525. }
  526. vectorStoreFileRelation.fileIds = undeletedFileIds;
  527. await vectorStoreFileRelation.save();
  528. }
  529. async deleteVectorStoreFilesByPageIds(pageIds: Types.ObjectId[]): Promise<void> {
  530. const vectorStoreRelations = await this.getVectorStoreRelationsByPageIds(pageIds);
  531. if (vectorStoreRelations != null && vectorStoreRelations.length !== 0) {
  532. for await (const pageId of pageIds) {
  533. const deleteVectorStoreFilePromises = vectorStoreRelations.map(vectorStoreRelation => this.deleteVectorStoreFile(vectorStoreRelation._id, pageId));
  534. await Promise.allSettled(deleteVectorStoreFilePromises);
  535. }
  536. }
  537. }
  538. async deleteObsoleteVectorStoreFile(limit: number, apiCallInterval: number): Promise<void> {
  539. // Retrieves all VectorStore documents that are marked as deleted
  540. const deletedVectorStoreRelations = await VectorStoreModel.find({ isDeleted: true });
  541. if (deletedVectorStoreRelations.length === 0) {
  542. return;
  543. }
  544. // Retrieves VectorStoreFileRelation documents associated with deleted VectorStore documents
  545. const obsoleteVectorStoreFileRelations = await VectorStoreFileRelationModel.find(
  546. { vectorStoreRelationId: { $in: deletedVectorStoreRelations.map(deletedVectorStoreRelation => deletedVectorStoreRelation._id) } },
  547. ).limit(limit);
  548. if (obsoleteVectorStoreFileRelations.length === 0) {
  549. return;
  550. }
  551. // Delete obsolete VectorStoreFile
  552. for await (const vectorStoreFileRelation of obsoleteVectorStoreFileRelations) {
  553. try {
  554. await this.deleteVectorStoreFile(vectorStoreFileRelation.vectorStoreRelationId, vectorStoreFileRelation.page, false, apiCallInterval);
  555. }
  556. catch (err) {
  557. logger.error(err);
  558. }
  559. }
  560. }
  561. private async deleteVectorStoreFileOnDeleteAttachment(attachmentId: string) {
  562. const vectorStoreFileRelation = await VectorStoreFileRelationModel.findOne({ attachment: attachmentId });
  563. if (vectorStoreFileRelation == null) {
  564. return;
  565. }
  566. try {
  567. await this.deleteVectorStoreFileForAttachment(vectorStoreFileRelation);
  568. }
  569. catch (err) {
  570. logger.error(err);
  571. }
  572. }
  573. private async filterPagesByAccessScope(aiAssistant: AiAssistantDocument, pages: HydratedDocument<PageDocument>[]) {
  574. const isPublicPage = (page: HydratedDocument<PageDocument>) => page.grant === PageGrant.GRANT_PUBLIC;
  575. const isUserGroupAccessible = (page: HydratedDocument<PageDocument>, ownerUserGroupIds: string[]) => {
  576. if (page.grant !== PageGrant.GRANT_USER_GROUP) return false;
  577. return page.grantedGroups.some(group => ownerUserGroupIds.includes(getIdStringForRef(group.item)));
  578. };
  579. const isOwnerAccessible = (page: HydratedDocument<PageDocument>, ownerId: Ref<IUser>) => {
  580. if (page.grant !== PageGrant.GRANT_OWNER) return false;
  581. return page.grantedUsers.some(user => getIdStringForRef(user) === getIdStringForRef(ownerId));
  582. };
  583. const getOwnerUserGroupIds = async(owner: Ref<IUser>) => {
  584. const userGroups = await UserGroupRelation.findAllUserGroupIdsRelatedToUser(owner);
  585. const externalGroups = await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(owner);
  586. return [...userGroups, ...externalGroups].map(group => getIdStringForRef(group));
  587. };
  588. switch (aiAssistant.accessScope) {
  589. case AiAssistantAccessScope.PUBLIC_ONLY:
  590. return pages.filter(isPublicPage);
  591. case AiAssistantAccessScope.GROUPS: {
  592. const ownerUserGroupIds = await getOwnerUserGroupIds(aiAssistant.owner);
  593. return pages.filter(page => isPublicPage(page) || isUserGroupAccessible(page, ownerUserGroupIds));
  594. }
  595. case AiAssistantAccessScope.OWNER: {
  596. const ownerUserGroupIds = await getOwnerUserGroupIds(aiAssistant.owner);
  597. return pages.filter(page => isPublicPage(page) || isOwnerAccessible(page, aiAssistant.owner) || isUserGroupAccessible(page, ownerUserGroupIds));
  598. }
  599. default:
  600. return [];
  601. }
  602. }
  603. async createVectorStoreFileOnPageCreate(pages: HydratedDocument<PageDocument>[]): Promise<void> {
  604. const pagePaths = pages.map(page => page.path);
  605. const aiAssistants = await this.findAiAssistantByPagePath(pagePaths, { shouldPopulateOwner: true, shouldPopulateVectorStore: true });
  606. if (aiAssistants.length === 0) {
  607. return;
  608. }
  609. for await (const aiAssistant of aiAssistants) {
  610. if (!isPopulated(aiAssistant.owner)) {
  611. continue;
  612. }
  613. const isLearnablePageLimitExceeded = await this.isLearnablePageLimitExceeded(aiAssistant.owner, aiAssistant.pagePathPatterns);
  614. if (isLearnablePageLimitExceeded) {
  615. continue;
  616. }
  617. const pagesToVectorize = await this.filterPagesByAccessScope(aiAssistant, pages);
  618. const vectorStoreRelation = aiAssistant.vectorStore;
  619. if (vectorStoreRelation == null || !isPopulated(vectorStoreRelation)) {
  620. continue;
  621. }
  622. logger.debug('--------- createVectorStoreFileOnPageCreate ---------');
  623. logger.debug('AccessScopeType of aiAssistant: ', aiAssistant.accessScope);
  624. logger.debug('VectorStoreFile pagePath to be created: ', pagesToVectorize.map(page => page.path));
  625. logger.debug('-----------------------------------------------------');
  626. await this.createVectorStoreFile(vectorStoreRelation as VectorStoreDocument, pagesToVectorize);
  627. }
  628. }
  629. async updateVectorStoreFileOnPageUpdate(page: HydratedDocument<PageDocument>) {
  630. const aiAssistants = await this.findAiAssistantByPagePath([page.path], { shouldPopulateVectorStore: true });
  631. if (aiAssistants.length === 0) {
  632. return;
  633. }
  634. for await (const aiAssistant of aiAssistants) {
  635. const pagesToVectorize = await this.filterPagesByAccessScope(aiAssistant, [page]);
  636. const vectorStoreRelation = aiAssistant.vectorStore;
  637. if (vectorStoreRelation == null || !isPopulated(vectorStoreRelation)) {
  638. continue;
  639. }
  640. logger.debug('---------- updateVectorStoreOnPageUpdate ------------');
  641. logger.debug('AccessScopeType of aiAssistant: ', aiAssistant.accessScope);
  642. logger.debug('PagePath of VectorStoreFile to be deleted: ', page.path);
  643. logger.debug('pagePath of VectorStoreFile to be created: ', pagesToVectorize.map(page => page.path));
  644. logger.debug('-----------------------------------------------------');
  645. // Do not create a new VectorStoreFile if page is changed to a permission that AiAssistant does not have access to
  646. await this.deleteVectorStoreFile(
  647. (vectorStoreRelation as VectorStoreDocument)._id,
  648. page._id,
  649. true, // ignoreAttachments = true
  650. );
  651. await this.createVectorStoreFile(
  652. vectorStoreRelation as VectorStoreDocument,
  653. pagesToVectorize,
  654. true, // ignoreAttachments = true
  655. );
  656. }
  657. }
  658. private async createVectorStoreFileOnUploadAttachment(
  659. pageId: string, attachment: HydratedDocument<IAttachmentDocument>, file: Express.Multer.File,
  660. ): Promise<void> {
  661. if (!isVectorStoreCompatible(file.originalname, file.mimetype)) {
  662. return;
  663. }
  664. const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
  665. const page = await Page.findById(pageId);
  666. if (page == null) {
  667. return;
  668. }
  669. const aiAssistants = await this.findAiAssistantByPagePath([page.path], { shouldPopulateVectorStore: true });
  670. if (aiAssistants.length === 0) {
  671. return;
  672. }
  673. const uploadedFile = await this.uploadFileForAttachment(file.originalname, undefined, file.path);
  674. logger.debug('Uploaded file', uploadedFile);
  675. for await (const aiAssistant of aiAssistants) {
  676. const pagesToVectorize = await this.filterPagesByAccessScope(aiAssistant, [page]);
  677. if (pagesToVectorize.length === 0) {
  678. continue;
  679. }
  680. const vectorStoreRelation = aiAssistant.vectorStore;
  681. if (vectorStoreRelation == null || !isPopulated(vectorStoreRelation)) {
  682. continue;
  683. }
  684. await this.client.createVectorStoreFile(vectorStoreRelation.vectorStoreId, uploadedFile.id);
  685. const vectorStoreFileRelationsMap: VectorStoreFileRelationsMap = new Map();
  686. prepareVectorStoreFileRelations(vectorStoreRelation._id as Types.ObjectId, page._id, uploadedFile.id, vectorStoreFileRelationsMap, attachment._id);
  687. const vectorStoreFileRelations = Array.from(vectorStoreFileRelationsMap.values());
  688. await VectorStoreFileRelationModel.upsertVectorStoreFileRelations(vectorStoreFileRelations);
  689. }
  690. }
  691. private async createVectorStoreFileWithStream(vectorStoreRelation: VectorStoreDocument, conditions: mongoose.FilterQuery<PageDocument>): Promise<void> {
  692. const Page = mongoose.model<HydratedDocument<PageDocument>, PageModel>('Page');
  693. const pagesStream = Page.find({ ...conditions })
  694. .populate('revision')
  695. .cursor({ batchSize: BATCH_SIZE });
  696. const batchStream = createBatchStream(BATCH_SIZE);
  697. const createVectorStoreFile = this.createVectorStoreFile.bind(this);
  698. const createVectorStoreFileStream = new Transform({
  699. objectMode: true,
  700. async transform(chunk: HydratedDocument<PageDocument>[], encoding, callback) {
  701. try {
  702. logger.debug('Target page path for VectorStoreFile generation: ', chunk.map(page => page.path));
  703. await createVectorStoreFile(vectorStoreRelation, chunk);
  704. this.push(chunk);
  705. callback();
  706. }
  707. catch (error) {
  708. callback(error);
  709. }
  710. },
  711. });
  712. await pipeline(pagesStream, batchStream, createVectorStoreFileStream);
  713. }
  714. private async createConditionForCreateVectorStoreFile(
  715. owner: AiAssistant['owner'],
  716. accessScope: AiAssistant['accessScope'],
  717. grantedGroupsForAccessScope: AiAssistant['grantedGroupsForAccessScope'],
  718. pagePathPatterns: AiAssistant['pagePathPatterns'],
  719. ): Promise<mongoose.FilterQuery<PageDocument>> {
  720. const convertedPagePathPatterns = convertPathPatternsToRegExp(pagePathPatterns);
  721. // Include pages in search targets when their paths with 'Anyone with the link' permission are directly specified instead of using glob pattern
  722. const nonGrabPagePathPatterns = pagePathPatterns.filter(pagePathPattern => !isGlobPatternPath(pagePathPattern));
  723. const baseCondition: mongoose.FilterQuery<PageDocument> = {
  724. grant: PageGrant.GRANT_RESTRICTED,
  725. path: { $in: nonGrabPagePathPatterns },
  726. };
  727. if (accessScope === AiAssistantAccessScope.PUBLIC_ONLY) {
  728. return {
  729. $or: [
  730. baseCondition,
  731. {
  732. grant: PageGrant.GRANT_PUBLIC,
  733. path: { $in: convertedPagePathPatterns },
  734. },
  735. ],
  736. };
  737. }
  738. if (accessScope === AiAssistantAccessScope.GROUPS) {
  739. if (grantedGroupsForAccessScope == null || grantedGroupsForAccessScope.length === 0) {
  740. throw new Error('grantedGroups is required when accessScope is GROUPS');
  741. }
  742. const extractedGrantedGroupIdsForAccessScope = grantedGroupsForAccessScope.map(group => getIdForRef(group.item).toString());
  743. return {
  744. $or: [
  745. baseCondition,
  746. {
  747. grant: { $in: [PageGrant.GRANT_PUBLIC, PageGrant.GRANT_USER_GROUP] },
  748. path: { $in: convertedPagePathPatterns },
  749. $or: [
  750. { 'grantedGroups.item': { $in: extractedGrantedGroupIdsForAccessScope } },
  751. { grant: PageGrant.GRANT_PUBLIC },
  752. ],
  753. },
  754. ],
  755. };
  756. }
  757. if (accessScope === AiAssistantAccessScope.OWNER) {
  758. const ownerUserGroups = [
  759. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  760. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  761. ].map(group => group.toString());
  762. return {
  763. $or: [
  764. baseCondition,
  765. {
  766. grant: { $in: [PageGrant.GRANT_PUBLIC, PageGrant.GRANT_USER_GROUP, PageGrant.GRANT_OWNER] },
  767. path: { $in: convertedPagePathPatterns },
  768. $or: [
  769. { 'grantedGroups.item': { $in: ownerUserGroups } },
  770. { grantedUsers: { $in: [getIdForRef(owner)] } },
  771. { grant: PageGrant.GRANT_PUBLIC },
  772. ],
  773. },
  774. ],
  775. };
  776. }
  777. throw new Error('Invalid accessScope value');
  778. }
  779. private async validateGrantedUserGroupsForAiAssistant(
  780. owner: AiAssistant['owner'],
  781. shareScope: AiAssistant['shareScope'],
  782. accessScope: AiAssistant['accessScope'],
  783. grantedGroupsForShareScope: AiAssistant['grantedGroupsForShareScope'],
  784. grantedGroupsForAccessScope: AiAssistant['grantedGroupsForAccessScope'],
  785. ) {
  786. // Check if grantedGroupsForShareScope is not specified when shareScope is not a “group”
  787. if (shareScope !== AiAssistantShareScope.GROUPS && grantedGroupsForShareScope != null) {
  788. throw new Error('grantedGroupsForShareScope is specified when shareScope is not “groups”.');
  789. }
  790. // Check if grantedGroupsForAccessScope is not specified when accessScope is not a “group”
  791. if (accessScope !== AiAssistantAccessScope.GROUPS && grantedGroupsForAccessScope != null) {
  792. throw new Error('grantedGroupsForAccessScope is specified when accsessScope is not “groups”.');
  793. }
  794. const ownerUserGroupIds = [
  795. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  796. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(owner)),
  797. ].map(group => group.toString());
  798. // Check if the owner belongs to the group specified in grantedGroupsForShareScope
  799. if (grantedGroupsForShareScope != null && grantedGroupsForShareScope.length > 0) {
  800. const extractedGrantedGroupIdsForShareScope = grantedGroupsForShareScope.map(group => getIdForRef(group.item).toString());
  801. const isValid = extractedGrantedGroupIdsForShareScope.every(groupId => ownerUserGroupIds.includes(groupId));
  802. if (!isValid) {
  803. throw new Error('A userGroup to which the owner does not belong is specified in grantedGroupsForShareScope');
  804. }
  805. }
  806. // Check if the owner belongs to the group specified in grantedGroupsForAccessScope
  807. if (grantedGroupsForAccessScope != null && grantedGroupsForAccessScope.length > 0) {
  808. const extractedGrantedGroupIdsForAccessScope = grantedGroupsForAccessScope.map(group => getIdForRef(group.item).toString());
  809. const isValid = extractedGrantedGroupIdsForAccessScope.every(groupId => ownerUserGroupIds.includes(groupId));
  810. if (!isValid) {
  811. throw new Error('A userGroup to which the owner does not belong is specified in grantedGroupsForAccessScope');
  812. }
  813. }
  814. }
  815. async isAiAssistantUsable(aiAssistantId: string, user: IUserHasId): Promise<boolean> {
  816. const aiAssistant = await AiAssistantModel.findOne({ _id: { $eq: aiAssistantId } });
  817. if (aiAssistant == null) {
  818. throw createError(404, 'AiAssistant document does not exist');
  819. }
  820. const isOwner = getIdStringForRef(aiAssistant.owner) === getIdStringForRef(user._id);
  821. if (aiAssistant.shareScope === AiAssistantShareScope.PUBLIC_ONLY) {
  822. return true;
  823. }
  824. if ((aiAssistant.shareScope === AiAssistantShareScope.OWNER) && isOwner) {
  825. return true;
  826. }
  827. if ((aiAssistant.shareScope === AiAssistantShareScope.SAME_AS_ACCESS_SCOPE) && (aiAssistant.accessScope === AiAssistantAccessScope.OWNER) && isOwner) {
  828. return true;
  829. }
  830. if ((aiAssistant.shareScope === AiAssistantShareScope.GROUPS)
  831. || ((aiAssistant.shareScope === AiAssistantShareScope.SAME_AS_ACCESS_SCOPE) && (aiAssistant.accessScope === AiAssistantAccessScope.GROUPS))) {
  832. const userGroupIds = [
  833. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
  834. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
  835. ].map(group => group.toString());
  836. const grantedGroupIdsForShareScope = aiAssistant.grantedGroupsForShareScope?.map(group => getIdStringForRef(group.item)) ?? [];
  837. const isShared = userGroupIds.some(userGroupId => grantedGroupIdsForShareScope.includes(userGroupId));
  838. return isShared;
  839. }
  840. return false;
  841. }
  842. async createAiAssistant(data: UpsertAiAssistantData, user: IUserHasId): Promise<AiAssistantDocument> {
  843. await this.validateGrantedUserGroupsForAiAssistant(
  844. user,
  845. data.shareScope,
  846. data.accessScope,
  847. data.grantedGroupsForShareScope,
  848. data.grantedGroupsForAccessScope,
  849. );
  850. const conditions = await this.createConditionForCreateVectorStoreFile(
  851. user,
  852. data.accessScope,
  853. data.grantedGroupsForAccessScope,
  854. data.pagePathPatterns,
  855. );
  856. const vectorStoreRelation = await this.createVectorStore(data.name);
  857. const aiAssistant = await AiAssistantModel.create({
  858. ...data, owner: user, vectorStore: vectorStoreRelation,
  859. });
  860. // VectorStore creation process does not await
  861. this.createVectorStoreFileWithStream(vectorStoreRelation, conditions);
  862. return aiAssistant;
  863. }
  864. async updateAiAssistant(aiAssistantId: string, data: UpsertAiAssistantData, user: IUserHasId): Promise<AiAssistantDocument> {
  865. const aiAssistant = await AiAssistantModel.findOne({ owner: user, _id: aiAssistantId });
  866. if (aiAssistant == null) {
  867. throw createError(404, 'AiAssistant document does not exist');
  868. }
  869. await this.validateGrantedUserGroupsForAiAssistant(
  870. user,
  871. data.shareScope,
  872. data.accessScope,
  873. data.grantedGroupsForShareScope,
  874. data.grantedGroupsForAccessScope,
  875. );
  876. const grantedGroupIdsForAccessScopeFromReq = data.grantedGroupsForAccessScope?.map(group => getIdStringForRef(group.item)) ?? []; // ObjectId[] -> string[]
  877. const grantedGroupIdsForAccessScopeFromDb = aiAssistant.grantedGroupsForAccessScope?.map(group => getIdStringForRef(group.item)) ?? []; // ObjectId[] -> string[]
  878. // If accessScope, pagePathPatterns, grantedGroupsForAccessScope have not changed, do not build VectorStore
  879. const shouldRebuildVectorStore = data.accessScope !== aiAssistant.accessScope
  880. || !isDeepEquals(data.pagePathPatterns, aiAssistant.pagePathPatterns)
  881. || !isDeepEquals(grantedGroupIdsForAccessScopeFromReq, grantedGroupIdsForAccessScopeFromDb);
  882. let newVectorStoreRelation: VectorStoreDocument | undefined;
  883. if (shouldRebuildVectorStore) {
  884. const conditions = await this.createConditionForCreateVectorStoreFile(
  885. user,
  886. data.accessScope,
  887. data.grantedGroupsForAccessScope,
  888. data.pagePathPatterns,
  889. );
  890. // Delete obsoleted VectorStore
  891. const obsoletedVectorStoreRelationId = getIdStringForRef(aiAssistant.vectorStore);
  892. await this.deleteVectorStore(obsoletedVectorStoreRelationId);
  893. newVectorStoreRelation = await this.createVectorStore(data.name);
  894. this.updateThreads(aiAssistantId, newVectorStoreRelation.vectorStoreId);
  895. // VectorStore creation process does not await
  896. this.createVectorStoreFileWithStream(newVectorStoreRelation, conditions);
  897. }
  898. const newData = {
  899. ...data,
  900. vectorStore: newVectorStoreRelation ?? aiAssistant.vectorStore,
  901. };
  902. aiAssistant.set({ ...newData });
  903. let updatedAiAssistant: AiAssistantDocument = await aiAssistant.save();
  904. if (data.shareScope !== AiAssistantShareScope.PUBLIC_ONLY && aiAssistant.isDefault) {
  905. updatedAiAssistant = await AiAssistantModel.setDefault(aiAssistant._id, false);
  906. }
  907. return updatedAiAssistant;
  908. }
  909. async getAccessibleAiAssistants(user: IUserHasId): Promise<AccessibleAiAssistants> {
  910. const userGroupIds = [
  911. ...(await UserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
  912. ...(await ExternalUserGroupRelation.findAllUserGroupIdsRelatedToUser(user)),
  913. ];
  914. const assistants = await AiAssistantModel.find({
  915. $or: [
  916. // Case 1: Assistants owned by the user
  917. { owner: user },
  918. // Case 2: Public assistants owned by others
  919. {
  920. $and: [
  921. { owner: { $ne: user } },
  922. { shareScope: AiAssistantShareScope.PUBLIC_ONLY },
  923. ],
  924. },
  925. // Case 3: Group-restricted assistants where user is in granted groups
  926. {
  927. $and: [
  928. { owner: { $ne: user } },
  929. { shareScope: AiAssistantShareScope.GROUPS },
  930. { 'grantedGroupsForShareScope.item': { $in: userGroupIds } },
  931. ],
  932. },
  933. ],
  934. })
  935. .populate('grantedGroupsForShareScope.item')
  936. .populate('grantedGroupsForAccessScope.item');
  937. return {
  938. myAiAssistants: assistants.filter(assistant => assistant.owner.toString() === user._id.toString()) ?? [],
  939. teamAiAssistants: assistants.filter(assistant => assistant.owner.toString() !== user._id.toString()) ?? [],
  940. };
  941. }
  942. async isLearnablePageLimitExceeded(user: IUserHasId, pagePathPatterns: string[]): Promise<boolean> {
  943. const normalizedPagePathPatterns = removeGlobPath(pagePathPatterns);
  944. const PageModel = mongoose.model<IPage, PageModel>('Page');
  945. const pagePathsWithDescendantCount = await PageModel.descendantCountByPaths(normalizedPagePathPatterns, user, null, true, true);
  946. const totalPageCount = pagePathsWithDescendantCount.reduce((total, pagePathWithDescendantCount) => {
  947. const descendantCount = pagePathPatterns.includes(pagePathWithDescendantCount.path)
  948. ? 0 // Treat as single page when included in "pagePathPatterns"
  949. : pagePathWithDescendantCount.descendantCount;
  950. const pageCount = descendantCount + 1;
  951. return total + pageCount;
  952. }, 0);
  953. logger.debug('TotalPageCount: ', totalPageCount);
  954. const limitLearnablePageCountPerAssistant = configManager.getConfig('openai:limitLearnablePageCountPerAssistant');
  955. return totalPageCount > limitLearnablePageCountPerAssistant;
  956. }
  957. private async findAiAssistantByPagePath(
  958. pagePaths: string[], options?: { shouldPopulateOwner?: boolean, shouldPopulateVectorStore?: boolean },
  959. ): Promise<AiAssistantDocument[]> {
  960. const pagePathsWithGlobPattern = pagePaths.map(pagePath => generateGlobPatterns(pagePath)).flat();
  961. const query = AiAssistantModel.find({
  962. $or: [
  963. // Case 1: Exact match
  964. { pagePathPatterns: { $in: pagePaths } },
  965. // Case 2: Glob pattern match
  966. { pagePathPatterns: { $in: pagePathsWithGlobPattern } },
  967. ],
  968. });
  969. if (options?.shouldPopulateOwner) {
  970. query.populate('owner');
  971. }
  972. if (options?.shouldPopulateVectorStore) {
  973. query.populate('vectorStore');
  974. }
  975. const aiAssistants = await query.exec();
  976. return aiAssistants;
  977. }
  978. }
  979. let instance: OpenaiService;
  980. export const initializeOpenaiService = (crowi: Crowi): void => {
  981. const aiEnabled = configManager.getConfig('app:aiEnabled');
  982. const openaiServiceType = configManager.getConfig('openai:serviceType');
  983. if (aiEnabled && openaiServiceType != null && OpenaiServiceTypes.includes(openaiServiceType)) {
  984. instance = new OpenaiService(crowi);
  985. }
  986. };
  987. export const getOpenaiService = (): IOpenaiService | undefined => {
  988. if (instance != null) {
  989. return instance;
  990. }
  991. return;
  992. };