Skip to content

Commit

Permalink
feat: enhance event tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
CNSeniorious000 committed Nov 10, 2024
1 parent fe26e9c commit 1b0c394
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/components/Generator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export default () => {

const moderationInterval = Number(import.meta.env.PUBLIC_MODERATION_INTERVAL ?? '2000')

createEffect(() => currentError() && trackEvent('error', { code: currentError()!.code }))

const isHigher = () => {
const distanceToBottom = footer.offsetTop - window.innerHeight
const currentScrollHeight = window.scrollY
Expand Down Expand Up @@ -201,7 +203,7 @@ export default () => {
}

const handleSubmit = async() => {
trackEvent(inputValue() ? 'send' : `${recording() ? 'end' : 'start'}Record`, recording() ? undefined : { length: messageList().length })
!inputValue() && trackEvent(`${recording() ? 'end' : 'start'}-record`)

if (recording()) {
stopRecording()
Expand Down Expand Up @@ -254,6 +256,8 @@ export default () => {
const smoothToBottom = () => toBottom('smooth')
const instantToBottom = () => toBottom('instant')

const formatTokenCount = (messages: ChatMessage[]) => encoder() && `<=${Math.ceil(countTokens(encoder()!, messages)!.total / 1000)}k`

const requestWithLatestMessage = async() => {
setStreaming(true)
setCurrentAssistantMessage('')
Expand Down Expand Up @@ -298,6 +302,13 @@ export default () => {
setCurrentError({ code: `${res.status} ${res.statusText}`, message })
throw new Error('Request failed')
}

trackEvent('send', {
model: payload.model,
temperature: payload.temperature,
originalTokenCont: formatTokenCount((systemMsg ? [systemMsg, ...messageList()] : messageList())),
tokenCount: formatTokenCount(requestMessageList),
})
const data = res.body
if (!data)
throw new Error('No data')
Expand Down Expand Up @@ -391,8 +402,8 @@ export default () => {
document.dispatchEvent(new MessagesEvent('clearMessages', messageList().length + Number(Boolean(currentSystemRoleSettings()))))
inputRef.value = ''
inputRef.style.height = 'auto'
trackEvent('clear', { totalTokenCount: formatTokenCount(messageList()) })
tokenCountCache.clear()
trackEvent('clear', { length: messageList().length })
batch(() => {
setInputValue('')
setMessageList([])
Expand All @@ -415,7 +426,7 @@ export default () => {

const retryLastFetch = () => {
if (messageList().length > 0) {
trackEvent('retry', { length: messageList().length, lastMessage: messageList().at(-1)!.role })
trackEvent('retry', { lastMessage: messageList().at(-1)!.role })
const lastMessage = messageList()[messageList().length - 1]
if (lastMessage.role === 'assistant')
setMessageList(messageList().slice(0, -1))
Expand Down

1 comment on commit 1b0c394

@vercel
Copy link

@vercel vercel bot commented on 1b0c394 Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.