openpencil/src/components/chat/ProviderSettings/context.ts
Danila Poyarkov c3f5189f8f style: tighten import grouping
- Configure oxfmt custom import groups for workspace, app, package, and test aliases
- Keep type imports grouped with their matching source category instead of one global tail group
- Expand the format script to cover formatter config, Vite files, and scripts
2026-05-06 02:22:08 +03:00

129 lines
3.3 KiB
TypeScript

import { computed, inject, provide, proxyRefs, ref, watch } from 'vue'
import type { InjectionKey, ShallowUnwrapRef } from 'vue'
import { useAIChat } from '@/app/ai/chat/use'
function createProviderSettingsContext() {
const {
providerID,
providerDef,
apiKey,
setAPIKey,
customBaseURL,
customModelID,
customAPIType,
maxOutputTokens,
pexelsApiKey,
unsplashAccessKey
} = useAIChat()
const isACP = computed(() => providerID.value.startsWith('acp:'))
const keyInput = ref('')
const pexelsKeyInput = ref('')
const unsplashKeyInput = ref('')
const baseURLInput = ref(customBaseURL.value)
const customModelInput = ref(customModelID.value)
const hasExistingKey = ref(!!apiKey.value)
const hasExistingPexelsKey = ref(!!pexelsApiKey.value)
const hasExistingUnsplashKey = ref(!!unsplashAccessKey.value)
watch(providerID, () => {
keyInput.value = ''
hasExistingKey.value = !!apiKey.value
baseURLInput.value = customBaseURL.value
customModelInput.value = customModelID.value
})
function save() {
if (keyInput.value.trim()) {
setAPIKey(keyInput.value.trim())
hasExistingKey.value = true
keyInput.value = ''
}
if (pexelsKeyInput.value.trim()) {
pexelsApiKey.value = pexelsKeyInput.value.trim()
hasExistingPexelsKey.value = true
pexelsKeyInput.value = ''
}
if (unsplashKeyInput.value.trim()) {
unsplashAccessKey.value = unsplashKeyInput.value.trim()
hasExistingUnsplashKey.value = true
unsplashKeyInput.value = ''
}
if (providerDef.value.supportsCustomBaseURL) {
customBaseURL.value = baseURLInput.value.trim()
}
if (providerDef.value.supportsCustomModel) {
customModelID.value = customModelInput.value.trim()
}
}
function clearKey() {
setAPIKey('')
keyInput.value = ''
hasExistingKey.value = false
}
function clearPexelsKey() {
pexelsApiKey.value = ''
pexelsKeyInput.value = ''
hasExistingPexelsKey.value = false
}
function clearUnsplashKey() {
unsplashAccessKey.value = ''
unsplashKeyInput.value = ''
hasExistingUnsplashKey.value = false
}
function setCustomAPIType(value: string) {
customAPIType.value = value as 'completions' | 'responses'
save()
}
return {
providerID,
providerDef,
apiKey,
customAPIType,
customBaseURL,
customModelID,
maxOutputTokens,
pexelsApiKey,
unsplashAccessKey,
isACP,
keyInput,
pexelsKeyInput,
unsplashKeyInput,
baseURLInput,
customModelInput,
hasExistingKey,
hasExistingPexelsKey,
hasExistingUnsplashKey,
save,
clearKey,
clearPexelsKey,
clearUnsplashKey,
setCustomAPIType
}
}
export type ProviderSettingsContext = ShallowUnwrapRef<
ReturnType<typeof createProviderSettingsContext>
>
const PROVIDER_SETTINGS_KEY: InjectionKey<ProviderSettingsContext> =
Symbol('ProviderSettingsContext')
export function provideProviderSettings() {
const ctx = proxyRefs(createProviderSettingsContext())
provide(PROVIDER_SETTINGS_KEY, ctx)
return ctx
}
export function useProviderSettingsContext(): ProviderSettingsContext {
const ctx = inject(PROVIDER_SETTINGS_KEY)
if (!ctx) throw new Error('Provider settings controls must be used within ProviderSettings')
return ctx
}