Skip to content

Commit cef1a07

Browse files
authored
Merge branch 'master' into delFS
2 parents 31d43e1 + 34a92fb commit cef1a07

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

libs/remix-ui/remix-ai/src/lib/components/Default.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
1-
import React from 'react'
1+
import React, { useState, useEffect } from 'react'
22
import '../remix-ai.css'
3-
import { DefaultModels, GenerationParams, ChatHistory, HandleStreamResponse, HandleSimpleResponse } from '@remix/remix-ai-core';
3+
import { DefaultModels, GenerationParams, ChatHistory, HandleStreamResponse } from '@remix/remix-ai-core';
44
import { ConversationStarter, StreamSend, StreamingAdapterObserver, useAiChatApi } from '@nlux/react';
55
import { AiChat, useAsStreamAdapter, ChatItem } from '@nlux/react';
6-
import { JsonStreamParser } from '@remix/remix-ai-core';
76
import { user, assistantAvatar } from './personas';
87
import { highlighter } from '@nlux/highlighter'
98
import './color.css'
109
import '@nlux/themes/unstyled.css';
10+
import copy from 'copy-to-clipboard'
1111

1212
export let ChatApi = null
1313

1414
export const Default = (props) => {
15+
const [is_streaming, setIS_streaming] = useState<boolean>(false)
16+
17+
const HandleCopyToClipboard = () => {
18+
const markdown = document.getElementsByClassName('nlux-chatSegments-container')
19+
if (markdown.length < 1) return
20+
21+
const codeBlocks = markdown[0].getElementsByClassName('code-block')
22+
Array.from(codeBlocks).forEach((block) => {
23+
const copyButtons = block.getElementsByClassName('nlux-comp-copyButton')
24+
Array.from(copyButtons).forEach((cp_btn) => {
25+
const hdlr = async () => {
26+
copy(block.textContent)
27+
}
28+
cp_btn.removeEventListener('click', async() => { hdlr() })
29+
cp_btn.addEventListener('click', async () => { hdlr() })
30+
})
31+
})
32+
}
33+
34+
useEffect(() => {
35+
HandleCopyToClipboard();
36+
}, [is_streaming]);
37+
1538
const send: StreamSend = async (
1639
prompt: string,
1740
observer: StreamingAdapterObserver,
1841
) => {
1942
GenerationParams.stream_result = true
43+
setIS_streaming(true)
2044
GenerationParams.return_stream_response = GenerationParams.stream_result
2145

2246
let response = null
@@ -32,13 +56,15 @@ export const Default = (props) => {
3256
observer.next(' ') // Add a space to flush the last message
3357
ChatHistory.pushHistory(prompt, result)
3458
observer.complete()
59+
setTimeout(() => { setIS_streaming(false) }, 1000)
3560
}
3661
)
3762
else {
3863
observer.next(response)
3964
observer.complete()
40-
}
4165

66+
setTimeout(() => { setIS_streaming(false) }, 1000)
67+
}
4268
};
4369
ChatApi = useAiChatApi();
4470
const conversationStarters: ConversationStarter[] = [
@@ -73,9 +99,9 @@ export const Default = (props) => {
7399
submitShortcut: 'Enter',
74100
hideStopButton: false,
75101
}}
76-
messageOptions={{ showCodeBlockCopyButton: false,
102+
messageOptions={{ showCodeBlockCopyButton: true,
77103
editableUserMessages: true,
78-
streamingAnimationSpeed: 2,
104+
streamingAnimationSpeed: 1,
79105
waitTimeBeforeStreamCompletion: 1000,
80106
syntaxHighlighter: highlighter
81107
}}

0 commit comments

Comments
 (0)