Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Dec 8, 2023
1 parent 13cf8d5 commit 9453966
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
6 changes: 3 additions & 3 deletions ee/tabby-ui/components/prompt-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { debounce, has, isEqual } from 'lodash-es'
import useSWR from 'swr'

import { useEnterSubmit } from '@/lib/hooks/use-enter-submit'
import { useSession } from '@/lib/tabby/auth'
import fetcher from '@/lib/tabby/fetcher'
import type { ISearchHit, SearchReponse } from '@/lib/types'
import { cn } from '@/lib/utils'
Expand All @@ -26,7 +27,6 @@ import {
TooltipContent,
TooltipTrigger
} from '@/components/ui/tooltip'
import { useSession } from '@/lib/tabby/auth'

export interface PromptProps
extends Pick<UseChatHelpers, 'input' | 'setInput'> {
Expand Down Expand Up @@ -56,11 +56,11 @@ function PromptFormRenderer(
Record<string, ISearchHit>
>({})

const { data } = useSession();
const { data } = useSession()
useSWR<SearchReponse>([queryCompletionUrl, data?.accessToken], fetcher, {
revalidateOnFocus: false,
dedupingInterval: 0,
onSuccess: (data) => {
onSuccess: data => {
setOptions(data?.hits ?? [])
}
})
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-ui/lib/hooks/use-health.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import useSWR, { SWRResponse } from 'swr'

import fetcher from '@/lib/tabby/fetcher'

import { useSession } from '../tabby/auth'

export interface HealthInfo {
Expand Down
11 changes: 6 additions & 5 deletions ee/tabby-ui/lib/hooks/use-patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StreamingTextResponse,
type AIStreamCallbacksAndOptions
} from 'ai'

import { useSession } from '../tabby/auth'

const serverUrl = process.env.NEXT_PUBLIC_TABBY_SERVER_URL || ''
Expand All @@ -14,28 +15,28 @@ export function usePatchFetch() {

useEffect(() => {
if (!(window as any)._originFetch) {
(window as any)._originFetch = window.fetch;
;(window as any)._originFetch = window.fetch
}

const fetch = (window as any)._originFetch as (typeof window.fetch);
const fetch = (window as any)._originFetch as typeof window.fetch

window.fetch = async function (url, options) {
if (url !== '/api/chat') {
return fetch(url, options)
}

const headers: HeadersInit = {
'Content-Type': 'application/json',
'Content-Type': 'application/json'
}

if (data?.accessToken) {
headers["Authorization"] = `Bearer ${data?.accessToken}`;
headers['Authorization'] = `Bearer ${data?.accessToken}`
}

const res = await fetch(`${serverUrl}/v1beta/chat/completions`, {
...options,
method: 'POST',
headers,
headers
})

const stream = StreamAdapter(res, undefined)
Expand Down
12 changes: 7 additions & 5 deletions ee/tabby-ui/lib/tabby/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export default function tokenFetcher([url, token]: Array<string | undefined>): Promise<any> {
const headers = new Headers();
export default function tokenFetcher([url, token]: Array<
string | undefined
>): Promise<any> {
const headers = new Headers()
if (token) {
headers.append("authorization", `Bearer ${token}`)
headers.append('authorization', `Bearer ${token}`)
}

if (process.env.NODE_ENV !== 'production') {
url = `${process.env.NEXT_PUBLIC_TABBY_SERVER_URL}${url}`;
url = `${process.env.NEXT_PUBLIC_TABBY_SERVER_URL}${url}`
}

return fetch(url!, { headers }).then(x => x.json())
}
}
32 changes: 19 additions & 13 deletions ee/tabby-ui/lib/tabby/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TypedDocumentNode } from '@graphql-typed-document-node/core'
import { GraphQLClient, Variables } from 'graphql-request'
import { GraphQLResponse } from 'graphql-request/build/esm/types'
import useSWR, { SWRConfiguration, SWRResponse } from 'swr'

import { useSession } from './auth'

export const gqlClient = new GraphQLClient(
Expand All @@ -27,17 +28,19 @@ export function useGraphQLForm<
onError?: (path: string, message: string) => void
}
) {
const { data } = useSession();
const accessToken = data?.accessToken;
const { data } = useSession()
const accessToken = data?.accessToken
const onSubmit = async (variables: TVariables) => {
let res
try {
res = await gqlClient.request({
document,
variables,
requestHeaders: accessToken ? {
"authorization": `Bearer ${accessToken}`
} : undefined
requestHeaders: accessToken
? {
authorization: `Bearer ${accessToken}`
}
: undefined
})
} catch (err) {
const { errors = [] } = (err as any).response as GraphQLResponse
Expand Down Expand Up @@ -70,16 +73,19 @@ export function useGraphQLQuery<
variables?: TVariables,
swrConfiguration?: SWRConfiguration<TResult>
): SWRResponse<TResult> {
const { data } = useSession();
const { data } = useSession()
return useSWR(
[document, variables, data?.accessToken],
([document, variables, accessToken]) => gqlClient.request({
document,
variables,
requestHeaders: accessToken ? {
"authorization": `Bearer ${accessToken}`
} : undefined
}),
([document, variables, accessToken]) =>
gqlClient.request({
document,
variables,
requestHeaders: accessToken
? {
authorization: `Bearer ${accessToken}`
}
: undefined
}),
swrConfiguration
)
}

0 comments on commit 9453966

Please sign in to comment.