Skip to content

Commit

Permalink
fixed types
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Nov 26, 2023
1 parent c34ff19 commit 35e941f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/components/Sidebar/chat/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import ChatHistory from './ChatHistory'
import { useChatHistory } from '../../../hooks/useChatHistory'
import WebPageContentToggle from './WebPageContentToggle'
import ImageCaptureButton from './ImageCaptureButton'
import { useMessageDraft } from '../../../hooks/useMessageDraft'
import { MessageDraft, useMessageDraft } from '../../../hooks/useMessageDraft'
import FilePreviewBar from './FilePreviewBar'
import MessageDraftLengthCounter from './MessageDraftLengthCounter'

interface SidebarInputProps {
loading: boolean
submitMessage: (message: string, context?: string) => void
submitMessage: (message: MessageDraft, context?: string) => void
clearMessages: () => void
chatIsEmpty: boolean
cancelRequest: () => void
Expand Down Expand Up @@ -66,7 +66,7 @@ export function SidebarInput({
})
context = (await pageContent) as string
}
submitMessage(messageDraft.text, isWebpageContextOn ? context : undefined)
submitMessage(messageDraft, isWebpageContextOn ? context : undefined)
resetMessageDraft()
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/Sidebar/chat/MessageDraftLengthCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react'

interface MessageDraftlengthCounterProps {
interface MessageDraftLengthCounterProps {
length: number
MAX_LENGTH: number
}
const MessageDraftLengthCounter = ({
length,
MAX_LENGTH,
}: MessageDraftlengthCounterProps) => {
}: MessageDraftLengthCounterProps) => {
return (
<div className="cdx-text-neutral-500">
<span>{length.toLocaleString()}</span>/
Expand Down
2 changes: 1 addition & 1 deletion src/components/Sidebar/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const Chat = ({ settings }: ChatProps) => {
prompt: string
}
if (action === 'generate') {
submitQuery(prompt)
submitQuery({ text: prompt, files: [] })
}
}
window.addEventListener('message', handleWindowMessage)
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/useChatCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useMemo, useState } from 'react'
import { AvailableModels, Mode } from '../config/settings'
import { ChatRole, useCurrentChat } from './useCurrentChat'
import { getMatchedContent } from '../lib/getMatchedContent'
import { MessageDraft } from './useMessageDraft'

interface UseChatCompletionProps {
model: AvailableModels
Expand Down Expand Up @@ -62,8 +63,8 @@ export const useChatCompletion = ({

const controller = new AbortController()

const submitQuery = async (query: string, context?: string) => {
await addNewMessage(ChatRole.USER, query)
const submitQuery = async (message: MessageDraft, context?: string) => {
await addNewMessage(ChatRole.USER, message)
const options = {
signal: controller.signal,
callbacks: [{ handleLLMNewToken: updateAssistantMessage }],
Expand All @@ -78,17 +79,17 @@ export const useChatCompletion = ({
*/
let matchedContext
if (context) {
matchedContext = await getMatchedContent(query, context, apiKey)
matchedContext = await getMatchedContent(message.text, context, apiKey)
}

const expandedQuery = matchedContext
? endent`
### Context
${matchedContext}
### Question:
${query}
${message}
`
: query
: message.text

const messages = [
new SystemMessage(systemPrompt),
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/useCurrentChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from 'react'
import { getCurrentSiteHostName } from '../lib/getCurrentSiteHostName'
import { useChatHistory } from './useChatHistory'
import { readStorage, setStorage } from './useStorage'
import { MessageDraft, MessageFile } from './useMessageDraft'

export enum ChatRole {
USER = 'USER',
Expand All @@ -12,6 +13,7 @@ export enum ChatRole {
export type ChatMessage = {
role: ChatRole
content: string
files?: MessageFile[]
timestamp: number
}

Expand Down Expand Up @@ -104,7 +106,7 @@ export const useCurrentChat = () => {
})
}

const addNewMessage = async (role: ChatRole, message: string) => {
const addNewMessage = async (role: ChatRole, message: MessageDraft) => {
if (!currentChatIdRef.current || !historyRef.current.length) {
console.log({
currentChatId: currentChatIdRef.current,
Expand Down Expand Up @@ -132,7 +134,8 @@ export const useCurrentChat = () => {
}
const newMessage: ChatMessage = {
role,
content: message,
content: message.text,
files: message.files,
timestamp: Date.now(),
}
setMessages((m) => [...m, newMessage])
Expand Down

0 comments on commit 35e941f

Please sign in to comment.