-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9411 from weseek/feat/157516-remove-unnecessary-s…
…trings-from-markdown-and-save-in-vector-store imprv(ai): Remove unnecessary strings from markdown when creating VectorStoreFIie
- Loading branch information
Showing
2 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
apps/app/src/features/openai/server/utils/sanitize-markdown.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { dynamicImport } from '@cspell/dynamic-import'; | ||
import type { Root, Code } from 'mdast'; | ||
import type * as RemarkParse from 'remark-parse'; | ||
import type * as RemarkStringify from 'remark-stringify'; | ||
import type * as Unified from 'unified'; | ||
import type * as UnistUtilVisit from 'unist-util-visit'; | ||
|
||
interface ModuleCache { | ||
remarkParse?: typeof RemarkParse.default; | ||
remarkStringify?: typeof RemarkStringify.default; | ||
unified?: typeof Unified.unified; | ||
visit?: typeof UnistUtilVisit.visit; | ||
} | ||
|
||
let moduleCache: ModuleCache = {}; | ||
|
||
const initializeModules = async(): Promise<void> => { | ||
if (moduleCache.remarkParse != null && moduleCache.remarkStringify != null && moduleCache.unified != null && moduleCache.visit != null) { | ||
return; | ||
} | ||
|
||
const [{ default: remarkParse }, { default: remarkStringify }, { unified }, { visit }] = await Promise.all([ | ||
dynamicImport<typeof RemarkParse>('remark-parse', __dirname), | ||
dynamicImport<typeof RemarkStringify>('remark-stringify', __dirname), | ||
dynamicImport<typeof Unified>('unified', __dirname), | ||
dynamicImport<typeof UnistUtilVisit>('unist-util-visit', __dirname), | ||
]); | ||
|
||
moduleCache = { | ||
remarkParse, | ||
remarkStringify, | ||
unified, | ||
visit, | ||
}; | ||
}; | ||
|
||
export const sanitizeMarkdown = async(markdown: string): Promise<string> => { | ||
await initializeModules(); | ||
|
||
const { | ||
remarkParse, remarkStringify, unified, visit, | ||
} = moduleCache; | ||
|
||
|
||
if (remarkParse == null || remarkStringify == null || unified == null || visit == null) { | ||
throw new Error('Failed to initialize required modules'); | ||
} | ||
|
||
const sanitize = () => { | ||
return (tree: Root) => { | ||
visit(tree, 'code', (node: Code) => { | ||
if (node.lang === 'drawio') { | ||
node.value = '<!-- drawio content replaced -->'; | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
const processor = unified() | ||
.use(remarkParse) | ||
.use(sanitize) | ||
.use(remarkStringify); | ||
|
||
return processor.processSync(markdown).toString(); | ||
}; |