Skip to content

Commit

Permalink
feat: add font (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Mar 21, 2023
1 parent 391839d commit 3a9f75b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 28 deletions.
35 changes: 24 additions & 11 deletions src/api/openAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@microsoft/fetch-event-source'
import { useSessionStore, useSettingsStore, useRoleStore } from '@/stores'
import { executeSQL } from '@/sqls'
import { dialogErrorMessage } from '@/utils'
import type { MessageData, SessionData } from '@/types'

/**
Expand Down Expand Up @@ -92,15 +93,27 @@ export const getOpenAIResultStreamApi = async (messages: MessageData[]) => {
* 获取账号余额信息
*/
export const getOpenAICreditApi = async () => {
const apiKey = getOpenAIKey()
if (!apiKey) return
try {
const apiKey = getOpenAIKey()
if (!apiKey) return

return await request(OPENAI_CREDIT_URL, {
method: 'GET',
headers: {
Authorization: `Bearer ${apiKey}`
const result = await request(OPENAI_CREDIT_URL, {
method: 'GET',
headers: {
Authorization: `Bearer ${apiKey}`
}
})
console.log('result', result)
return result
} catch ({ message }: any) {
const { isThinking } = useSessionStore()

if (isThinking) {
return '请求的 API KEY 无效'
} else {
dialogErrorMessage(message)
}
})
}
}

/**
Expand All @@ -116,10 +129,6 @@ export const getAiMessage = async (value?: string) => {

if (!currentRole) return

// 检测是否有余额
const credit = await getOpenAICreditApi()
if (!credit) return

const messages: MessageData[] = []

const { currentSession, sessionDataList } = useSessionStore()
Expand Down Expand Up @@ -189,6 +198,10 @@ export const getAiMessage = async (value?: string) => {
}
})

// 检测是否有余额
const credit = await getOpenAICreditApi()
if (!credit) return

await getOpenAIResultStreamApi(messages)

isThinking.value = false
Expand Down
5 changes: 3 additions & 2 deletions src/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export const request = async (url: string, options?: FetchOptions) => {
})

const { error } = data

if (error) throw new Error(error.message)

return data
} catch (error: any) {
dialogErrorMessage('请求出错:' + error.message)
} catch ({ message }: any) {
throw new Error(`请求出错:${message}`)
}
}
9 changes: 8 additions & 1 deletion src/assets/css/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
}
}

@font-face {
font-family: JetBrainsMono;

src: url('../font/JetBrainsMono.woff2') format('woff2');
font-display: auto;
}

html {
background-color: transparent !important;

Expand All @@ -29,7 +36,7 @@ body {

color: var(--color-text-1);

font: 1rem/1;
font: 1rem/1 JetBrainsMono;
}

.bordered {
Expand Down
Binary file added src/assets/font/JetBrainsMono.woff2
Binary file not shown.
19 changes: 11 additions & 8 deletions src/components/Function/components/SettingsModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ const usedCredit = ref(0)
const getCredit = async () => {
const credit = await getOpenAICreditApi()
if (!credit) return
totalCredit.value = parseFloat(credit.total_granted.toFixed(2))
usedCredit.value = parseFloat(credit.total_available.toFixed(2))
totalCredit.value = parseFloat(credit.total_granted?.toFixed(2))
usedCredit.value = parseFloat(credit.total_available?.toFixed(2))
}
// TODO 优化,这里可能需要防抖
watch(apiKey, getCredit)
watch(
() => props.visible,
(val) => {
val && getCredit()
[apiKey, props],
([newValue]) => {
if (!props.visible || newValue.length !== 51) return
getCredit()
},
{
immediate: true
}
)
</script>
Expand Down
11 changes: 6 additions & 5 deletions src/components/Session/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ const { uuid } = storeToRefs(useSettingsStore())
const { sessionDataList } = storeToRefs(useSessionStore())
const { currentRole } = storeToRefs(useRoleStore())
/** 自动滚动到底部 */
const sessionElement = ref<HTMLDivElement | null>(null)
const scrollHeight = ref<number | undefined>(0)
const autoscrollBottom = () => {
/**
* 自动滚动到底部
*/
const autoScrollBottom = () => {
if (scrollHeight.value !== sessionElement.value?.scrollHeight) {
sessionElement.value?.scroll({
top: sessionElement.value.scrollHeight,
behavior: 'smooth'
top: sessionElement.value.scrollHeight
})
scrollHeight.value = sessionElement.value?.scrollHeight
}
Expand All @@ -37,7 +38,7 @@ const localTime = (time: string) =>
dayjs.utc(time).local().format('YYYY-MM-DD HH:mm:ss')
onUpdated(() => {
autoscrollBottom()
autoScrollBottom()
})
</script>

Expand Down
6 changes: 5 additions & 1 deletion src/utils/copyCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ const renderCode = (originRule: (...args: RulesArgs) => string) => {

const content = tokens[idx].content

const copyContent = content
.replaceAll('"', '&quot;')
.replaceAll("'", '&apos;')

const originRendered = originRule(...args)

if (!content) return originRendered

return `
<div class='relative'>
${originRendered}
<div class="${PLUGIN_CLASS}" data-clipboard-text="${content}" data-uuid="${crypto.randomUUID()}" title="复制代码"></div>
<div class="${PLUGIN_CLASS}" data-clipboard-text="${copyContent}" data-uuid="${crypto.randomUUID()}" title="复制代码"></div>
</div>
`
}
Expand Down

0 comments on commit 3a9f75b

Please sign in to comment.