feat(api): 优化模型选择逻辑,支持动态更新模型
This commit is contained in:
1 parent
45d717a028
commit
d8f3fa9606
2 files changed
+14
-27
No files matched your search
@@ -13,6 +13,7 @@ type AutoCompleteProps = Omit<React.InputHTMLAttributes<HTMLInputElement>, 'valu
|
||||
value: string
|
||||
onChange: (value: string) => void
|
||||
options: AutoCompleteOption[]
|
||||
maxOptions?: number
|
||||
emptyText?: string
|
||||
dropdownClassName?: string
|
||||
inputClassName?: string
|
||||
@@ -41,6 +42,7 @@ export const AutoCompleteInput = React.forwardRef<HTMLInputElement, AutoComplete
|
||||
value,
|
||||
onChange,
|
||||
options,
|
||||
maxOptions,
|
||||
emptyText = '无匹配结果',
|
||||
className,
|
||||
dropdownClassName,
|
||||
@@ -78,7 +80,8 @@ export const AutoCompleteInput = React.forwardRef<HTMLInputElement, AutoComplete
|
||||
const dedupedOptions = options.filter((option, index) => {
|
||||
return options.findIndex((candidate) => candidate.value === option.value) === index
|
||||
})
|
||||
const filteredOptions = dedupedOptions.filter((option) => matchesOption(option, query)).slice(0, 12)
|
||||
const matchedOptions = dedupedOptions.filter((option) => matchesOption(option, query))
|
||||
const filteredOptions = typeof maxOptions === 'number' ? matchedOptions.slice(0, maxOptions) : matchedOptions
|
||||
|
||||
const handleSelect = (selectedValue: string) => {
|
||||
onChange(selectedValue)
|
||||
|
||||
@@ -114,33 +114,10 @@ export function ApiTest() {
|
||||
[apiToken, tokens, channels]
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (availableModels.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
if (modelValue) {
|
||||
return
|
||||
}
|
||||
|
||||
const nextModel = availableModels[0]
|
||||
setModelValue(nextModel)
|
||||
setRequestBody((currentBody) => {
|
||||
try {
|
||||
const parsedBody = JSON.parse(currentBody)
|
||||
parsedBody.model = nextModel
|
||||
return JSON.stringify(parsedBody, null, 2)
|
||||
} catch {
|
||||
return buildRequestBody(endpoint, nextModel)
|
||||
}
|
||||
})
|
||||
}, [availableModels])
|
||||
|
||||
const handleEndpointChange = (newEndpoint: string) => {
|
||||
setEndpoint(newEndpoint)
|
||||
const nextModel = availableModels[0] || ''
|
||||
setModelValue(nextModel)
|
||||
setRequestBody(buildRequestBody(newEndpoint, nextModel))
|
||||
setModelValue('')
|
||||
setRequestBody(buildRequestBody(newEndpoint))
|
||||
}
|
||||
|
||||
const handleCopyResponse = async () => {
|
||||
@@ -167,7 +144,11 @@ export function ApiTest() {
|
||||
|
||||
try {
|
||||
const parsedBody = JSON.parse(requestBody)
|
||||
parsedBody.model = nextModel
|
||||
if (nextModel) {
|
||||
parsedBody.model = nextModel
|
||||
} else {
|
||||
delete parsedBody.model
|
||||
}
|
||||
setRequestBody(JSON.stringify(parsedBody, null, 2))
|
||||
} catch {
|
||||
setRequestBody(buildRequestBody(endpoint, nextModel))
|
||||
@@ -181,6 +162,8 @@ export function ApiTest() {
|
||||
const parsedBody = JSON.parse(value)
|
||||
if (typeof parsedBody?.model === 'string') {
|
||||
setModelValue(parsedBody.model)
|
||||
} else {
|
||||
setModelValue('')
|
||||
}
|
||||
} catch {
|
||||
// Keep the current model selection while the JSON is temporarily invalid.
|
||||
@@ -289,6 +272,7 @@ export function ApiTest() {
|
||||
placeholder="选择或输入模型名称"
|
||||
inputClassName="font-mono"
|
||||
options={modelOptions}
|
||||
maxOptions={availableModels.length}
|
||||
emptyText="没有匹配的模型"
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user