update api context

This commit is contained in:
duanfuxiang 2025-06-06 17:20:05 +08:00
parent 8732ebf30e
commit 5bdfc91042
6 changed files with 16 additions and 6 deletions

View File

@ -2,6 +2,7 @@ import * as Tooltip from '@radix-ui/react-tooltip'
import { Check, CopyIcon } from 'lucide-react' import { Check, CopyIcon } from 'lucide-react'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { useSettings } from '../../contexts/SettingsContext'
import { t } from '../../lang/helpers' import { t } from '../../lang/helpers'
import { ChatAssistantMessage } from '../../types/chat' import { ChatAssistantMessage } from '../../types/chat'
import { calculateLLMCost } from '../../utils/price-calculator' import { calculateLLMCost } from '../../utils/price-calculator'
@ -46,6 +47,7 @@ function CopyButton({ message }: { message: ChatAssistantMessage }) {
function LLMResponesInfoButton({ message }: { message: ChatAssistantMessage }) { function LLMResponesInfoButton({ message }: { message: ChatAssistantMessage }) {
const [cost, setCost] = useState<number | null>(0); const [cost, setCost] = useState<number | null>(0);
const { settings } = useSettings();
useEffect(() => { useEffect(() => {
async function calculateCost() { async function calculateCost() {
@ -56,12 +58,13 @@ function LLMResponesInfoButton({ message }: { message: ChatAssistantMessage }) {
const calculatedCost = await calculateLLMCost({ const calculatedCost = await calculateLLMCost({
model: message.metadata.model, model: message.metadata.model,
usage: message.metadata.usage, usage: message.metadata.usage,
settings: settings,
}); });
setCost(calculatedCost); setCost(calculatedCost);
} }
calculateCost(); calculateCost();
}, [message]); }, [message, settings]);
return ( return (
<Tooltip.Provider delayDuration={0}> <Tooltip.Provider delayDuration={0}>

View File

@ -151,7 +151,7 @@ export function ModelSelect() {
const fetchModels = async () => { const fetchModels = async () => {
setIsLoading(true) setIsLoading(true)
try { try {
const models = await GetProviderModelIds(modelProvider) const models = await GetProviderModelIds(modelProvider, settings)
setModelIds(models) setModelIds(models)
} catch (error) { } catch (error) {
console.error('Failed to fetch provider models:', error) console.error('Failed to fetch provider models:', error)
@ -162,7 +162,7 @@ export function ModelSelect() {
} }
fetchModels() fetchModels()
}, [modelProvider]) }, [modelProvider, settings])
// Sync chat model id & chat model provider // Sync chat model id & chat model provider
useEffect(() => { useEffect(() => {

View File

@ -83,7 +83,7 @@ const ControlArea: React.FC<ControlAreaProps> = ({
useEffect(() => { useEffect(() => {
const fetchModels = async () => { const fetchModels = async () => {
try { try {
const models = await GetProviderModelIds(settings.chatModelProvider); const models = await GetProviderModelIds(settings.chatModelProvider, settings);
setProviderModels(models); setProviderModels(models);
} catch (err) { } catch (err) {
const error = err as Error; const error = err as Error;

View File

@ -185,7 +185,7 @@ export const ComboBoxComponent: React.FC<ComboBoxComponentProps> = ({
}; };
fetchModelIds(); fetchModelIds();
}, [modelProvider, isEmbedding]); }, [modelProvider, isEmbedding, settings]);
const searchableItems = useMemo(() => { const searchableItems = useMemo(() => {
return modelIds.map((id) => ({ return modelIds.map((id) => ({

View File

@ -143,6 +143,7 @@ export const infioDefaultModelInfo: ModelInfo = {
let infioModelsCache: Record<string, ModelInfo> | null = null; let infioModelsCache: Record<string, ModelInfo> | null = null;
async function fetchInfioModels(apiKey?: string): Promise<Record<string, ModelInfo>> { async function fetchInfioModels(apiKey?: string): Promise<Record<string, ModelInfo>> {
console.log("fetchInfioModels apiKey", apiKey)
if (infioModelsCache) { if (infioModelsCache) {
return infioModelsCache; return infioModelsCache;
} }
@ -1606,6 +1607,7 @@ export const GetProviderModels = async (provider: ApiProvider, settings?: InfioS
switch (provider) { switch (provider) {
case ApiProvider.Infio: { case ApiProvider.Infio: {
const apiKey = settings?.infioProvider?.apiKey const apiKey = settings?.infioProvider?.apiKey
console.log("apiKey", apiKey)
return await fetchInfioModels(apiKey) return await fetchInfioModels(apiKey)
} }
case ApiProvider.OpenRouter: case ApiProvider.OpenRouter:
@ -1640,6 +1642,8 @@ export const GetProviderModelsWithSettings = async (provider: ApiProvider, setti
switch (provider) { switch (provider) {
case ApiProvider.Infio: { case ApiProvider.Infio: {
const apiKey = settings?.infioProvider?.apiKey const apiKey = settings?.infioProvider?.apiKey
console.log("apiKey", apiKey)
console.log("settings", settings)
return await fetchInfioModels(apiKey) return await fetchInfioModels(apiKey)
} }
case ApiProvider.OpenRouter: case ApiProvider.OpenRouter:

View File

@ -1,5 +1,6 @@
import { LLMModel } from '../types/llm/model' import { LLMModel } from '../types/llm/model'
import { ResponseUsage } from '../types/llm/response' import { ResponseUsage } from '../types/llm/response'
import { InfioSettings } from '../types/settings'
import { GetProviderModels } from './api' import { GetProviderModels } from './api'
@ -7,11 +8,13 @@ import { GetProviderModels } from './api'
export const calculateLLMCost = async ({ export const calculateLLMCost = async ({
model, model,
usage, usage,
settings,
}: { }: {
model: LLMModel model: LLMModel
usage: ResponseUsage usage: ResponseUsage
settings?: InfioSettings
}): Promise<number | null> => { }): Promise<number | null> => {
const providerModels = await GetProviderModels(model.provider) const providerModels = await GetProviderModels(model.provider, settings)
if (!providerModels) { if (!providerModels) {
return null return null
} }