chore: provider masked api-key

This commit is contained in:
jialin
2026-02-06 17:33:06 +08:00
parent 1bd31d2936
commit 4132494a49
7 changed files with 128 additions and 14 deletions
+49
View File
@@ -42,6 +42,7 @@ export async function queryProviderModels(
params: { params: {
data: { data: {
api_token: string; api_token: string;
proxy_url: string;
config: { config: {
type: string; type: string;
}; };
@@ -59,11 +60,35 @@ export async function queryProviderModels(
); );
} }
export async function queryProviderModelsInEditing(
params: {
id?: number;
data: {
api_token: string;
proxy_url: string;
config: {
type: string;
};
};
},
options?: any
) {
return request<{ data: any[] }>(
`${MAAS_PROVIDERS_API}/${params.id}/get-models`,
{
method: 'POST',
data: params.data,
cancelToken: options?.token
}
);
}
export async function testProviderModel( export async function testProviderModel(
params: { params: {
data: { data: {
model_name: string; model_name: string;
api_token: string; api_token: string;
proxy_url: string;
config: { config: {
type: string; type: string;
}; };
@@ -77,3 +102,27 @@ export async function testProviderModel(
cancelToken: options?.token cancelToken: options?.token
}); });
} }
export async function testProviderModelInEditing(
params: {
id?: number;
data: {
model_name: string;
api_token: string;
proxy_url: string;
config: {
type: string;
};
};
},
options?: any
) {
return request<any>(
`${MAAS_PROVIDERS_API}/${params.id}${TEST_PROVIDER_MODEL_API}`,
{
method: 'post',
data: params.data,
cancelToken: options?.token
}
);
}
@@ -5,6 +5,8 @@ import { maasProviderType } from '.';
interface FormContextProps { interface FormContextProps {
providerType?: maasProviderType; providerType?: maasProviderType;
action?: PageActionType; action?: PageActionType;
currentData?: any;
id?: number;
} }
const FormContext = createContext<FormContextProps>({}); const FormContext = createContext<FormContextProps>({});
+2 -2
View File
@@ -10,6 +10,8 @@ export interface FormData {
api_tokens: string[]; api_tokens: string[];
models: ProviderModel[]; models: ProviderModel[];
api_key: string; api_key: string;
proxy_url: string;
proxy_timeout: number;
config: { config: {
type: maasProviderType; type: maasProviderType;
openaiCustomUrl?: string; openaiCustomUrl?: string;
@@ -23,8 +25,6 @@ export interface MaasProviderItem extends FormData {
deleted_at: string; deleted_at: string;
timeout: number; timeout: number;
models: ProviderModel[]; models: ProviderModel[];
proxy_url: string;
proxy_timeout: number;
builtin: boolean; builtin: boolean;
provider_model_count: number; provider_model_count: number;
api_token_count: number; api_token_count: number;
+11 -4
View File
@@ -71,7 +71,9 @@ const ProviderForm: React.FC<ProviderFormProps> = forwardRef((props, ref) => {
); );
const data = { const data = {
..._.omit(values, ['api_key']), ..._.omit(values, ['api_key']),
api_tokens: _.concat([], values.api_key, apiTokens || []), api_tokens: _.concat([], values.api_key, apiTokens || []).map(
(item: string) => ({ input: item })
),
config: { config: {
type: values.config.type, type: values.config.type,
...yaml2Json(advanceRef.current?.getYamlValue() || '') ...yaml2Json(advanceRef.current?.getYamlValue() || '')
@@ -102,10 +104,13 @@ const ProviderForm: React.FC<ProviderFormProps> = forwardRef((props, ref) => {
(action === PageAction.EDIT || action === PageAction.COPY) && (action === PageAction.EDIT || action === PageAction.COPY) &&
currentData currentData
) { ) {
const apiTokensList = _.get(currentData, 'api_tokens', []).map(
(item: any) => item.hash || ''
);
form.setFieldsValue({ form.setFieldsValue({
...currentData, ...currentData,
api_key: currentData.api_tokens?.[0] || '', api_key: apiTokensList?.[0] || '',
api_tokens: currentData.api_tokens?.slice(1) || [], api_tokens: apiTokensList?.slice(1) || [],
proxy_enabled: !!currentData.proxy_url, proxy_enabled: !!currentData.proxy_url,
custom_config: json2Yaml( custom_config: json2Yaml(
_.omit(currentData.config, ['type', 'openaiCustomUrl']) || {} _.omit(currentData.config, ['type', 'openaiCustomUrl']) || {}
@@ -127,7 +132,9 @@ const ProviderForm: React.FC<ProviderFormProps> = forwardRef((props, ref) => {
}} }}
getScrollElementScrollableHeight={getScrollElementScrollableHeight} getScrollElementScrollableHeight={getScrollElementScrollableHeight}
> >
<FormContext.Provider value={{ action }}> <FormContext.Provider
value={{ action, id: currentData?.id, currentData }}
>
<Form <Form
form={form} form={form}
onFinish={handleOnFinish} onFinish={handleOnFinish}
+19 -2
View File
@@ -1,5 +1,6 @@
import AutoComplete from '@/components/seal-form/auto-complete'; import AutoComplete from '@/components/seal-form/auto-complete';
import SealSelect from '@/components/seal-form/seal-select'; import SealSelect from '@/components/seal-form/seal-select';
import { PageAction } from '@/config';
import { categoryOptions } from '@/pages/llmodels/config'; import { categoryOptions } from '@/pages/llmodels/config';
import { import {
CheckCircleFilled, CheckCircleFilled,
@@ -10,6 +11,7 @@ import { useIntl } from '@umijs/max';
import { Button, Form, Tooltip } from 'antd'; import { Button, Form, Tooltip } from 'antd';
import React from 'react'; import React from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { useFormContext } from '../config/form-context';
import { FormData, ProviderModel } from '../config/types'; import { FormData, ProviderModel } from '../config/types';
import { useTestProviderModel } from '../hooks/use-query-provider-models'; import { useTestProviderModel } from '../hooks/use-query-provider-models';
@@ -52,12 +54,27 @@ const ModelItem: React.FC<ModelItemProps> = ({
const intl = useIntl(); const intl = useIntl();
const form = Form.useFormInstance<FormData>(); const form = Form.useFormInstance<FormData>();
const { runTestModel, loading: testLoading } = useTestProviderModel(); const { runTestModel, loading: testLoading } = useTestProviderModel();
const { id, action, currentData } = useFormContext();
const generateCurrentAPIKey = (currentAPIKey: string) => {
if (
action === PageAction.EDIT &&
currentAPIKey === currentData?.api_tokens?.[0]?.hash
) {
return undefined;
}
return currentAPIKey;
};
const handleTestModel = async () => { const handleTestModel = async () => {
const res = await runTestModel({ const res = await runTestModel({
id: action === PageAction.EDIT ? id! : 0,
data: { data: {
model_name: item.name, model_name: item.name,
api_token: form.getFieldValue('api_key') || '', api_token: generateCurrentAPIKey(
form.getFieldValue('api_key')
) as string,
proxy_url: form.getFieldValue('proxy_url') || undefined,
config: { config: {
type: form.getFieldValue(['config', 'type']) || '' type: form.getFieldValue(['config', 'type']) || ''
} }
@@ -103,7 +120,7 @@ const ModelItem: React.FC<ModelItemProps> = ({
if (item.accessible === false) { if (item.accessible === false) {
return ( return (
<WarningFilled <WarningFilled
style={{ color: 'var(--ant-color-error)', fontSize: 14 }} style={{ color: 'var(--ant-color-warning)', fontSize: 14 }}
/> />
); );
} }
@@ -1,8 +1,10 @@
import MetadataList from '@/components/metadata-list'; import MetadataList from '@/components/metadata-list';
import { PageAction } from '@/config';
import useAppUtils from '@/hooks/use-app-utils'; import useAppUtils from '@/hooks/use-app-utils';
import { useIntl } from '@umijs/max'; import { useIntl } from '@umijs/max';
import { Form } from 'antd'; import { Form } from 'antd';
import { useRef } from 'react'; import { useRef } from 'react';
import { useFormContext } from '../config/form-context';
import { FormData, ProviderModel } from '../config/types'; import { FormData, ProviderModel } from '../config/types';
import { useQueryProviderModels } from '../hooks/use-query-provider-models'; import { useQueryProviderModels } from '../hooks/use-query-provider-models';
import ModelItem from './model-item'; import ModelItem from './model-item';
@@ -15,6 +17,17 @@ const SupportedModels = () => {
const modelList = Form.useWatch('models', form) || []; const modelList = Form.useWatch('models', form) || [];
const prevAPIKeyRef = useRef<string>(''); const prevAPIKeyRef = useRef<string>('');
const { getRuleMessage } = useAppUtils(); const { getRuleMessage } = useAppUtils();
const { id, action, currentData } = useFormContext();
const generateCurrentAPIKey = (currentAPIKey: string) => {
if (
action === PageAction.EDIT &&
currentAPIKey === currentData?.api_tokens?.[0]?.hash
) {
return undefined;
}
return currentAPIKey;
};
const handleOpenChange = async (open: boolean) => { const handleOpenChange = async (open: boolean) => {
try { try {
@@ -31,8 +44,10 @@ const SupportedModels = () => {
) { ) {
prevAPIKeyRef.current = currentAPIKey; prevAPIKeyRef.current = currentAPIKey;
fetchProviderModels({ fetchProviderModels({
id: action === PageAction.EDIT ? id! : 0,
data: { data: {
api_token: currentAPIKey, api_token: generateCurrentAPIKey(currentAPIKey) as string,
proxy_url: form.getFieldValue('proxy_url') || undefined,
config: { config: {
type: form.getFieldValue(['config', 'type']) || '' type: form.getFieldValue(['config', 'type']) || ''
} }
@@ -3,7 +3,12 @@ import { useRequest } from 'ahooks';
import { message } from 'antd'; import { message } from 'antd';
import { CancelTokenSource } from 'axios'; import { CancelTokenSource } from 'axios';
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
import { queryProviderModels, testProviderModel } from '../apis'; import {
queryProviderModels,
queryProviderModelsInEditing,
testProviderModel,
testProviderModelInEditing
} from '../apis';
/** /**
* *
@@ -19,10 +24,16 @@ export const useQueryProviderModels = () => {
cancel cancel
} = useRequest( } = useRequest(
async (params: { async (params: {
data: { api_token: string; config: { type: string } }; id: number;
data: { api_token: string; config: { type: string }; proxy_url: string };
}) => { }) => {
axiosTokenRef.current?.cancel(); axiosTokenRef.current?.cancel();
axiosTokenRef.current = createAxiosToken(); axiosTokenRef.current = createAxiosToken();
if (params.id) {
return await queryProviderModelsInEditing(params, {
token: axiosTokenRef.current.token
});
}
return await queryProviderModels(params, { return await queryProviderModels(params, {
token: axiosTokenRef.current.token token: axiosTokenRef.current.token
}); });
@@ -68,14 +79,27 @@ export const useTestProviderModel = () => {
cancel cancel
} = useRequest( } = useRequest(
async (params: { async (params: {
data: { api_token: string; config: { type: string }; model_name: string }; id: number;
data: {
api_token: string;
config: { type: string };
model_name: string;
proxy_url: string;
};
}) => { }) => {
axiosTokenRef.current?.cancel(); axiosTokenRef.current?.cancel();
axiosTokenRef.current = createAxiosToken(); axiosTokenRef.current = createAxiosToken();
const response = await testProviderModel(params, {
// for edit page
if (params.id) {
return await testProviderModelInEditing(params, {
token: axiosTokenRef.current.token
});
}
// for create page
return await testProviderModel(params, {
token: axiosTokenRef.current.token token: axiosTokenRef.current.token
}); });
return response;
}, },
{ {
manual: true, manual: true,