Просмотр исходного кода

Refactoring of replaceAnnotationWithPageLink()

Shun Miyazawa 1 год назад
Родитель
Сommit
6e3b9f28fa

+ 7 - 1
apps/app/src/features/openai/server/routes/message.ts

@@ -82,7 +82,13 @@ export const postMessageHandlersFactory: PostMessageHandlersFactory = (crowi) =>
       });
 
       const messageDeltaHandler = async(delta: MessageDelta) => {
-        await replaceAnnotationWithPageLink(delta, req.user.lang);
+        const content = delta.content?.[0];
+
+        // If annotation is found
+        if (content?.type === 'text' && content?.text?.annotations != null) {
+          await replaceAnnotationWithPageLink(content, req.user.lang);
+        }
+
         res.write(`data: ${JSON.stringify(delta)}\n\n`);
       };
 

+ 5 - 7
apps/app/src/features/openai/server/services/replace-annotation-with-page-link.ts

@@ -1,18 +1,16 @@
 // See: https://platform.openai.com/docs/assistants/tools/file-search#step-5-create-a-run-and-check-the-output
 
 import type { IPageHasId, Lang } from '@growi/core/dist/interfaces';
-import type { MessageDelta } from 'openai/resources/beta/threads/messages.mjs';
+import type { MessageContentDelta } from 'openai/resources/beta/threads/messages.mjs';
 
 import VectorStoreFileRelationModel, { type VectorStoreFileRelation } from '~/features/openai/server/models/vector-store-file-relation';
 import { getTranslation } from '~/server/service/i18next';
 
 type PopulatedVectorStoreFileRelation = Omit<VectorStoreFileRelation, 'pageId'> & { pageId: IPageHasId }
 
-export const replaceAnnotationWithPageLink = async(delta: MessageDelta, lang?: Lang): Promise<void> => {
-  const content = delta.content?.[0];
-
-  if (content?.type === 'text' && content?.text?.annotations != null) {
-    const annotations = content?.text?.annotations;
+export const replaceAnnotationWithPageLink = async(messageContentDelta: MessageContentDelta, lang?: Lang): Promise<void> => {
+  if (messageContentDelta?.type === 'text' && messageContentDelta?.text?.annotations != null) {
+    const annotations = messageContentDelta?.text?.annotations;
     for await (const annotation of annotations) {
       if (annotation.type === 'file_citation' && annotation.text != null) {
 
@@ -22,7 +20,7 @@ export const replaceAnnotationWithPageLink = async(delta: MessageDelta, lang?: L
 
         if (vectorStoreFileRelation != null) {
           const { t } = await getTranslation(lang);
-          content.text.value = content.text.value?.replace(
+          messageContentDelta.text.value = messageContentDelta.text.value?.replace(
             annotation.text,
             ` [${t('source')}: [${vectorStoreFileRelation.pageId.path}](/${vectorStoreFileRelation.pageId._id})]`,
           );