Untitled
unknown
plain_text
a year ago
7.3 kB
5
Indexable
// Customizable Area Start import { IBlock } from '../../../framework/src/IBlock'; import { Message } from '../../../framework/src/Message'; import { BlockComponent } from '../../../framework/src/BlockComponent'; import MessageEnum, { getName, } from '../../../framework/src/Messages/MessageEnum'; import { runEngine } from '../../../framework/src/RunEngine'; import { GiftedChat } from 'react-native-gifted-chat'; import { v4 as uuidv4 } from 'uuid'; import { ActionCable, Cable } from '@kesha-antonov/react-native-action-cable'; import { getStorageData } from '../../../framework/src/Utilities'; // Customizable Area Start interface MessageArray { id: number; message: string; isUser: boolean; } interface RespData { warning: string; id: string; object: string; created: number; model: string; choices: [ { text: ' Hello there! How can I help you?'; index: 0; logprobs: null; finish_reason: 'stop'; } ]; usage: { prompt_tokens: 7; completion_tokens: 9; total_tokens: 16 }; } interface IMessage { _id: string | number; text: string; createdAt: Date | number; user: User; image?: string; video?: string; audio?: string; system?: boolean; sent?: boolean; received?: boolean; pending?: boolean; quickReplies?: QuickReplies; } interface User { _id: string | number; name: string; avatar: string; } interface QuickReplies { type: 'radio' | 'checkbox'; values: Reply[]; keepIt?: boolean; } interface Reply { title: string; value: string; messageId?: any; } // Customizable Area End export const configJSON = require('./config'); export interface Props { navigation: any; id: string; // Customizable Area Start // Customizable Area End } interface S { txtInputValue: string; txtSavedValue: string; enableField: boolean; // Customizable Area Start messages: IMessage[]; input: string; isLoading: boolean; data: string; ws: any; // Customizable Area End } interface SS { id: any; // Customizable Area Start // Customizable Area End } export default class ChatgptController extends BlockComponent<Props, S, SS> { // Customizable Area Start openApiID: string = ''; actionCable: any; // Customizable Area End constructor(props: Props) { super(props); this.receive = this.receive.bind(this); this.subScribedMessages = [ getName(MessageEnum.AccoutLoginSuccess), // Customizable Area Start getName(MessageEnum.RestAPIResponceMessage), getName(MessageEnum.RestAPIRequestMessage), getName(MessageEnum.RestAPIResponceEndPointMessage), getName(MessageEnum.RestAPIRequestBodyMessage), getName(MessageEnum.RestAPIRequestHeaderMessage), getName(MessageEnum.RestAPIRequestMethodMessage), // Customizable Area End ]; this.state = { txtInputValue: '', txtSavedValue: 'A', enableField: false, // Customizable Area Start messages: [], input: '', isLoading: false, data: '', ws: null, // Customizable Area End }; runEngine.attachBuildingBlock(this as IBlock, this.subScribedMessages); // Customizable Area Start // Customizable Area End } async receive(from: string, message: Message) { runEngine.debugLog('Message Recived', message); // Customizable Area Start // Customizable Area End } // Customizable Area Start onSend = (messages: any) => { const { ws } = this.state; if (ws && ws.readyState === WebSocket.OPEN) { // Send the message content through WebSocket const messageContent = messages.text; // Assuming you send one message at a time ws.send(JSON.stringify({ text: messageContent })); console.log('Sent message:', messageContent); } // GiftedChat.append(prevState.messages, messages) this.setState((prevState) => ({ messages: GiftedChat.append(prevState.messages, messages), })); }; async componentDidMount(): Promise<void> { // Create a WebSocket connection this.chatDetailsActioncable(); // const ws = new WebSocket('ws://localhost:9000'); // Replace with your WebSocket server URL // ws.onopen = () => { // console.log('WebSocket connected'); // }; // ws.onmessage = (event: MessageEvent) => { // const response = JSON.parse(event.data); // console.log('Message from server:', response.id); // if (response.id != 1) { // const message = response.choices.map((choice: any) => ({ // _id: uuidv4(), // Ensure unique ID // text: response.choices[0].message.content, // createdAt: new Date(), // Use current date as message timestamp // user: { // _id: 2, // Replace with the assistant's user ID // name: 'Assistant', // Replace with assistant's name if needed // }, // })); // this.setState((prevState) => ({ // messages: GiftedChat.append(prevState.messages, message), // })); // } // // Transform the server response to GiftedChat messages // // const message: IMessage[] = response.choices.map((choice: any) => ({ // // _id: uuidv4(), // Ensure unique ID // // text: choice.message.content, // // createdAt: new Date(), // Use current date as message timestamp // // user: { // // _id: 2, // Replace with the assistant's user ID // // name: 'Assistant', // Replace with assistant's name if needed // // }, // // })); // // console.log(messages, 'AssistantAssistant'); // // this.setState({ messages: [message, ...this.state.messages] }); // // Append the messages to the chat // // this.setState((prevState) => ({ // // messages: GiftedChat.append(prevState.messages, messages), // // })); // }; // ws.onerror = (error: Event) => { // console.error('WebSocket error:', error); // }; // ws.onclose = () => { // console.log('WebSocket closed'); // }; // this.setState({ ws }); } chatDetailsActioncable = async () => { const configFrameWork = require('../../../framework/src/config.js'); const hostUrl = configFrameWork.baseURL.substring(8); let value = await getStorageData('usertoken'); let token = JSON.parse(value); this.actionCable = ActionCable.createConsumer(`wss://${hostUrl}/cable`, { headers: { token, }, }); console.log('chatDetailsActioncable', this.actionCable); const cable = new Cable({ channel: `ChatGptChannel`, }); const channel = cable.setChannel( `ChatGptChannel`, this.actionCable.subscriptions.create({ channel: `ChatGptChannel`, }) ); channel .on('received', (event: any) => { this._onRecieved(event); }) .on('connected', () => { console.log('connectedconnectedconnected'); }) .on('disconnected', () => { this.actionCable.disconnect(); }); }; async componentWillUnmount() { // Clean up the WebSocket connection when the component unmounts // if (this.state.ws) { // this.state.ws.close(); // } } // Customizable Area End } // Customizable Area End
Editor is loading...
Leave a Comment