Skip to content

Commit

Permalink
fix: worker un exit (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored Dec 18, 2023
1 parent 703583f commit b14a1db
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 130 deletions.
2 changes: 1 addition & 1 deletion packages/global/common/string/textSplitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export const splitText2Chunks = (props: {
step: 0,
lastText: '',
mdTitle: ''
}).map((chunk) => chunk.replaceAll(codeBlockMarker, '\n')); // restore code block
}).map((chunk) => chunk?.replaceAll(codeBlockMarker, '\n') || ''); // restore code block

const tokens = countTokens
? chunks.reduce((sum, chunk) => sum + countPromptTokens(chunk, 'system'), 0)
Expand Down
3 changes: 3 additions & 0 deletions packages/service/common/string/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export const htmlToMarkdown = (html?: string | null) =>
const worker = new Worker(getWorkerPath('html2md'));

worker.on('message', (md: string) => {
worker.terminate();

resolve(simpleMarkdownText(md));
});
worker.on('error', (err) => {
worker.terminate();
reject(err);
});

Expand Down
35 changes: 0 additions & 35 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion projects/app/src/components/Markdown/chat/Guide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function MyLink(e: any) {
<Box as={'li'} mb={1}>
<Box
as={'span'}
color={'blue.500'}
color={'blue.700'}
textDecoration={'underline'}
cursor={'pointer'}
onClick={() => {
Expand Down
2 changes: 1 addition & 1 deletion projects/app/src/components/SideTabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const SideTabs = ({ list, size = 'md', activeId, onChange, ...props }: Props) =>
alignItems={'center'}
{...(activeId === item.id
? {
bg: ' blue.200 !important',
bg: ' blue.100 !important',
fontWeight: 'bold',
color: 'blue.600 ',
cursor: 'default'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import React from 'react';
import {
SmoothStepEdge,
EdgeLabelRenderer,
EdgeProps,
getSmoothStepPath,
MarkerType
} from 'reactflow';
import { SmoothStepEdge, EdgeLabelRenderer, EdgeProps, getSmoothStepPath } from 'reactflow';
import { Flex } from '@chakra-ui/react';
import MyIcon from '@/components/Icon';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,28 +289,30 @@ export const PreviewFileOrChunk = () => {
overflow={'overlay'}
px={[4, 8]}
my={4}
contentEditable
dangerouslySetInnerHTML={{ __html: previewFile.rawText }}
// contentEditable
// dangerouslySetInnerHTML={{ __html: previewFile.rawText }}
fontSize={'sm'}
whiteSpace={'pre-wrap'}
wordBreak={'break-all'}
onBlur={(e) => {
// @ts-ignore
const val = e.target.innerText;
setReShowRePreview(true);
// onBlur={(e) => {
// // @ts-ignore
// const val = e.target.innerText;
// setReShowRePreview(true);

setFiles((state) =>
state.map((file) =>
file.id === previewFile.id
? {
...file,
text: val
}
: file
)
);
}}
/>
// setFiles((state) =>
// state.map((file) =>
// file.id === previewFile.id
// ? {
// ...file,
// text: val
// }
// : file
// )
// );
// }}
>
{previewFile.rawText}
</Box>
</Box>
) : (
<Box pt={[3, 6]}>
Expand Down Expand Up @@ -365,49 +367,9 @@ export const PreviewFileOrChunk = () => {
}}
/>
</Flex>
<Box
px={4}
fontSize={'sm'}
whiteSpace={'pre-wrap'}
wordBreak={'break-all'}
contentEditable={!chunk.a}
dangerouslySetInnerHTML={{
__html: chunk.a ? `q:${chunk.q}\na:${chunk.a}` : chunk.q
}}
onBlur={(e) => {
// @ts-ignore
const val = e.target.innerText;

/* delete file */
if (val === '') {
setFiles((state) =>
state.map((stateFile) =>
stateFile.id === file.id
? {
...file,
chunks: [...file.chunks.slice(0, i), ...file.chunks.slice(i + 1)]
}
: stateFile
)
);
} else {
// update chunk
setFiles((stateFiles) =>
stateFiles.map((stateFile) =>
file.id === stateFile.id
? {
...stateFile,
chunks: stateFile.chunks.map((chunk, index) => ({
...chunk,
index: i === index ? val : chunk.q
}))
}
: stateFile
)
);
}
}}
/>
<Box px={4} fontSize={'sm'} whiteSpace={'pre-wrap'} wordBreak={'break-all'}>
{chunk.a ? `q:${chunk.q}\na:${chunk.a}` : chunk.q}
</Box>
</Box>
))
)}
Expand Down
20 changes: 10 additions & 10 deletions projects/app/src/web/styles/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,16 @@ export const theme = extendTheme({
1000: '#121416'
},
blue: {
50: '#f0f7ff',
100: '#EBF7FD',
200: '#d6e8ff',
300: '#adceff',
400: '#85b1ff',
500: '#4e83fd',
600: '#3370ff',
700: '#2152d9',
800: '#1237b3',
900: '#07228c'
50: '#F0F4FF',
100: '#E1EAFF',
200: '#C5D7FF',
300: '#94B5FF',
400: '#5E8FFF',
500: '#487FFF',
600: '#3370FF',
700: '#2B5FD9',
800: '#2450B5',
900: '#1D4091'
},
myRead: {
600: '#ff4d4f'
Expand Down
32 changes: 18 additions & 14 deletions worker/html2md.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ parentPort?.on('message', (html) => {
});

const html2md = (html) => {
const window = domino.createWindow(html);
const document = window.document;
try {
const window = domino.createWindow(html);
const document = window.document;

turndownService.remove(['i', 'script', 'iframe']);
turndownService.addRule('codeBlock', {
filter: 'pre',
replacement(_, node) {
const content = node.textContent?.trim() || '';
// @ts-ignore
const codeName = node?._attrsByQName?.class?.data?.trim() || '';
turndownService.remove(['i', 'script', 'iframe']);
turndownService.addRule('codeBlock', {
filter: 'pre',
replacement(_, node) {
const content = node.textContent?.trim() || '';
// @ts-ignore
const codeName = node?._attrsByQName?.class?.data?.trim() || '';

return `\n\`\`\`${codeName}\n${content}\n\`\`\`\n`;
}
});
return `\n\`\`\`${codeName}\n${content}\n\`\`\`\n`;
}
});

turndownService.use(turndownPluginGfm.gfm);
turndownService.use(turndownPluginGfm.gfm);

return turndownService.turndown(document);
return turndownService.turndown(document);
} catch (error) {
return '';
}
};

0 comments on commit b14a1db

Please sign in to comment.