Skip to content

Commit

Permalink
Complete type defined. (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunIce authored Sep 20, 2023
1 parent 1367f34 commit e65f9cb
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
8 changes: 5 additions & 3 deletions web/app/components/app/chat/copy-btn/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use client'
import React from 'react'
import { useRef, useState } from 'react'
import { t } from 'i18next'
import copy from 'copy-to-clipboard'
import s from './style.module.css'
import Tooltip from '@/app/components/base/tooltip'
import { randomString } from '@/utils'

type ICopyBtnProps = {
value: string
Expand All @@ -16,12 +17,13 @@ const CopyBtn = ({
className,
isPlain,
}: ICopyBtnProps) => {
const [isCopied, setIsCopied] = React.useState(false)
const [isCopied, setIsCopied] = useState(false)
const selector = useRef(`copy-tooltip-${randomString(4)}`)

return (
<div className={`${className}`}>
<Tooltip
selector={`copy-btn-tooltip-${value}`}
selector={selector.current}
content={(isCopied ? t('appApi.copied') : t('appApi.copy')) as string}
className='z-10'
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,15 @@ import OpeningStatement from '@/app/components/app/configuration/features/chat-g
import GroupName from '@/app/components/app/configuration/base/group-name'
import Loading from '@/app/components/base/loading'
import Confirm from '@/app/components/base/confirm'
// type
import type { AutomaticRes } from '@/service/debug'

const noDataIcon = (
<svg width="56" height="56" viewBox="0 0 56 56" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M10.4998 51.3333V39.6666M10.4998 16.3333V4.66663M4.6665 10.5H16.3332M4.6665 45.5H16.3332M30.3332 6.99996L26.2868 17.5206C25.6287 19.2315 25.2997 20.0869 24.7881 20.8065C24.3346 21.4442 23.7774 22.0014 23.1397 22.4549C22.4202 22.9665 21.5647 23.2955 19.8538 23.9535L9.33317 28L19.8539 32.0464C21.5647 32.7044 22.4202 33.0334 23.1397 33.5451C23.7774 33.9985 24.3346 34.5557 24.7881 35.1934C25.2997 35.913 25.6287 36.7684 26.2868 38.4793L30.3332 49L34.3796 38.4793C35.0376 36.7684 35.3666 35.913 35.8783 35.1934C36.3317 34.5557 36.8889 33.9985 37.5266 33.5451C38.2462 33.0334 39.1016 32.7044 40.8125 32.0464L51.3332 28L40.8125 23.9535C39.1016 23.2955 38.2462 22.9665 37.5266 22.4549C36.8889 22.0014 36.3317 21.4442 35.8783 20.8065C35.3666 20.0869 35.0376 19.2315 34.3796 17.5206L30.3332 6.99996Z" stroke="#EAECF0" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round"/>
</svg>
)

export type AutomaticRes = {
prompt: string
variables: string[]
opening_statement: string
}

export type IGetAutomaticResProps = {
mode: AppType
isShow: boolean
Expand Down Expand Up @@ -98,7 +94,7 @@ const GetAutomaticRes: FC<IGetAutomaticResProps> = ({
audiences,
hoping_to_solve: hopingToSolve,
})
setRes(res as AutomaticRes)
setRes(res)
}
finally {
setLoadingFalse()
Expand Down Expand Up @@ -193,7 +189,7 @@ const GetAutomaticRes: FC<IGetAutomaticResProps> = ({
onClose={() => setShowConfirmOverwrite(false)}
onConfirm={() => {
setShowConfirmOverwrite(false)
onFinished(res as AutomaticRes)
onFinished(res!)
}}
onCancel={() => setShowConfirmOverwrite(false)}
/>
Expand Down
22 changes: 11 additions & 11 deletions web/service/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ export function format(text: string) {
return res.replaceAll('\n', '<br/>').replaceAll('```', '')
}

const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd) => {
const handleStream = (response: Response, onData: IOnData, onCompleted?: IOnCompleted, onThought?: IOnThought, onMessageEnd?: IOnMessageEnd) => {
if (!response.ok)
throw new Error('Network response was not ok')

const reader = response.body.getReader()
const reader = response.body?.getReader()
const decoder = new TextDecoder('utf-8')
let buffer = ''
let bufferObj: any
let bufferObj: Record<string, any>
let isFirstMessage = true
function read() {
let hasError = false
reader.read().then((result: any) => {
reader?.read().then((result: any) => {
if (result.done) {
onCompleted && onCompleted()
return
Expand All @@ -99,7 +99,7 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
lines.forEach((message) => {
if (message.startsWith('data: ')) { // check if it starts with data:
try {
bufferObj = JSON.parse(message.substring(6)) // remove data: and parse as json
bufferObj = JSON.parse(message.substring(6)) as Record<string, any>// remove data: and parse as json
}
catch (e) {
// mute handle message cut off
Expand All @@ -113,11 +113,11 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
onData('', false, {
conversationId: undefined,
messageId: '',
errorMessage: bufferObj.message,
errorCode: bufferObj.code,
errorMessage: bufferObj?.message,
errorCode: bufferObj?.code,
})
hasError = true
onCompleted && onCompleted(true)
onCompleted?.(true)
return
}
if (bufferObj.event === 'message') {
Expand All @@ -130,10 +130,10 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
isFirstMessage = false
}
else if (bufferObj.event === 'agent_thought') {
onThought?.(bufferObj as any)
onThought?.(bufferObj as ThoughtItem)
}
else if (bufferObj.event === 'message_end') {
onMessageEnd?.(bufferObj as any)
onMessageEnd?.(bufferObj as MessageEnd)
}
}
})
Expand All @@ -146,7 +146,7 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
errorMessage: `${e}`,
})
hasError = true
onCompleted && onCompleted(true)
onCompleted?.(true)
return
}
if (!hasError)
Expand Down
8 changes: 7 additions & 1 deletion web/service/debug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { IOnCompleted, IOnData, IOnError, IOnMessageEnd } from './base'
import { get, post, ssePost } from './base'

export type AutomaticRes = {
prompt: string
variables: string[]
opening_statement: string
}

export const sendChatMessage = async (appId: string, body: Record<string, any>, { onData, onCompleted, onError, getAbortController, onMessageEnd }: {
onData: IOnData
onCompleted: IOnCompleted
Expand Down Expand Up @@ -46,7 +52,7 @@ export const fetchConvesationMessages = (appId: string, conversation_id: string)
}

export const generateRule = (body: Record<string, any>) => {
return post('/rule-generate', {
return post<AutomaticRes>('/rule-generate', {
body,
})
}
Expand Down

0 comments on commit e65f9cb

Please sign in to comment.