Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overrided the nlux clipboard button #5607

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions libs/remix-ui/remix-ai/src/lib/components/Default.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
import React from 'react'
import React, { useState, useEffect } from 'react'
import '../remix-ai.css'
import { DefaultModels, GenerationParams, ChatHistory, HandleStreamResponse, HandleSimpleResponse } from '@remix/remix-ai-core';
import { DefaultModels, GenerationParams, ChatHistory, HandleStreamResponse } from '@remix/remix-ai-core';
import { ConversationStarter, StreamSend, StreamingAdapterObserver, useAiChatApi } from '@nlux/react';
import { AiChat, useAsStreamAdapter, ChatItem } from '@nlux/react';
import { JsonStreamParser } from '@remix/remix-ai-core';
import { user, assistantAvatar } from './personas';
import { highlighter } from '@nlux/highlighter'
import './color.css'
import '@nlux/themes/unstyled.css';
import copy from 'copy-to-clipboard'

export let ChatApi = null

export const Default = (props) => {
const [is_streaming, setIS_streaming] = useState<boolean>(false)

const HandleCopyToClipboard = () => {
const markdown = document.getElementsByClassName('nlux-chatSegments-container')
if (markdown.length < 1) return

const codeBlocks = markdown[0].getElementsByClassName('code-block')
Array.from(codeBlocks).forEach((block) => {
const copyButtons = block.getElementsByClassName('nlux-comp-copyButton')
Array.from(copyButtons).forEach((cp_btn) => {
const hdlr = async () => {
copy(block.textContent)
}
cp_btn.removeEventListener('click', async() => { hdlr() })
cp_btn.addEventListener('click', async () => { hdlr() })
})
})
}

useEffect(() => {
HandleCopyToClipboard();
}, [is_streaming]);

const send: StreamSend = async (
prompt: string,
observer: StreamingAdapterObserver,
) => {
GenerationParams.stream_result = true
setIS_streaming(true)
GenerationParams.return_stream_response = GenerationParams.stream_result

let response = null
Expand All @@ -32,13 +56,15 @@ export const Default = (props) => {
observer.next(' ') // Add a space to flush the last message
ChatHistory.pushHistory(prompt, result)
observer.complete()
setTimeout(() => { setIS_streaming(false) }, 1000)
}
)
else {
observer.next(response)
observer.complete()
}

setTimeout(() => { setIS_streaming(false) }, 1000)
}
};
ChatApi = useAiChatApi();
const conversationStarters: ConversationStarter[] = [
Expand Down Expand Up @@ -73,9 +99,9 @@ export const Default = (props) => {
submitShortcut: 'Enter',
hideStopButton: false,
}}
messageOptions={{ showCodeBlockCopyButton: false,
messageOptions={{ showCodeBlockCopyButton: true,
editableUserMessages: true,
streamingAnimationSpeed: 2,
streamingAnimationSpeed: 1,
waitTimeBeforeStreamCompletion: 1000,
syntaxHighlighter: highlighter
}}
Expand Down
Loading