44 lines
5.5 KiB
TypeScript
44 lines
5.5 KiB
TypeScript
import { messagesOutputSetting } from '../shared/contracts/model-capacity.mjs';
|
||
import { useEffect, useRef, useState } from 'react';
|
||
import { nativeParameterSettings } from '../shared/contracts/native-parameters.mjs';
|
||
import { changeReasoningSetting, modelParameterProfile, reasoningSetting } from '../shared/contracts/model-parameter-profiles.mjs';
|
||
import { ReasoningSettings } from './ReasoningSettings';
|
||
import type { CatalogModel } from '../shared/contracts/model-catalog.mjs';
|
||
|
||
export type NativeConnection = { id: string; revision: number; name: string; protocol: string; model?: string; brand?: string; baseUrl?: string; parameterProfile?: string; capabilityDeclarations?: Record<string, string>; capabilities?: Record<string, string>; nativeParameters?: Record<string, unknown>; outputTokenField?: string; upstreamModel?: CatalogModel; maxOutputTokens?: number };
|
||
export function NativeParameters({ connection, busy, onCancel, onSave, executionMode }: { connection: NativeConnection; busy: boolean; executionMode?: string; onCancel: () => void;
|
||
onSave: (input: { id: string; revision: number; nativeParameters: Record<string, unknown>; outputTokenField: string; parameterProfile?: string }) => Promise<void> }) {
|
||
const [text, setText] = useState(JSON.stringify(connection.nativeParameters ?? {}, null, 2));
|
||
const [field, setField] = useState(connection.outputTokenField ?? (connection.protocol === 'responses' ? 'max_output_tokens' : 'max_tokens'));
|
||
const [error, setError] = useState('');
|
||
const [profile, setProfile] = useState(connection.parameterProfile ?? 'auto');
|
||
let parameters: Record<string, unknown> | null = null;
|
||
try { const parsed = JSON.parse(text); if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) parameters = parsed; } catch { /* The original editor content remains editable. */ }
|
||
const updateParameters = (value: Record<string, unknown>) => { setText(JSON.stringify(value, null, 2)); setError(''); };
|
||
const form = useRef<HTMLFormElement>(null);
|
||
useEffect(() => { form.current?.scrollIntoView({ block: 'nearest' }); }, []);
|
||
return <form ref={form} className="mc-editor mc-native" aria-label="原生请求参数" onSubmit={async event => {
|
||
event.preventDefault(); setError('');
|
||
try { const settings = nativeParameterSettings(connection.protocol, JSON.parse(text), field, profile, { executionMode }); await onSave({ id: connection.id, revision: connection.revision, ...settings }); }
|
||
catch (cause) { setError(cause instanceof Error ? cause.message : '参数格式无效。'); }
|
||
}}>
|
||
<h3>{connection.name} · 推理与原生参数</h3>
|
||
<p className="mc-help">参数按所选协议原字段发送,并封存在实际请求中。具体模型是否接受这些选项,以服务响应为准;未填写的选项采用上游默认值。</p>
|
||
<ReasoningSettings connection={connection} parameters={parameters} profile={profile} busy={busy} onChange={updateParameters} onProfile={value => {
|
||
if (parameters) {
|
||
const before = { ...connection, parameterProfile: profile }, after = { ...connection, parameterProfile: value };
|
||
if (modelParameterProfile(before).thinking !== modelParameterProfile(after).thinking) {
|
||
updateParameters(changeReasoningSetting(after, parameters, 'mode', reasoningSetting(before, parameters).mode));
|
||
}
|
||
}
|
||
setProfile(value);
|
||
}} />
|
||
<label>输出上限字段<select aria-label="输出上限字段" value={field} disabled={busy} onChange={event => setField(event.target.value)}>{(connection.protocol === 'responses' ? ['max_output_tokens'] : connection.protocol === 'anthropic-messages' ? ['max_tokens'] : ['max_tokens', 'max_completion_tokens']).map(value => <option key={value}>{value}</option>)}</select></label>
|
||
<p className="mc-help">{connection.protocol === 'anthropic-messages' ? 'Messages 协议必须带 max_tokens;当前 ' + String(messagesOutputSetting(connection).value) + '(' + ({ explicit_native: '显式原生参数', upstream_catalog: '上游目录最大输出', official_model_spec: 'Anthropic 官方同步输出规格', connection_declaration: '连接声明,非已验证最大值' }[messagesOutputSetting(connection).source] ?? '') + ')。可在 JSON 中显式设置;本地不裁剪输出。' : executionMode === 'unrestricted' ? '默认不附加输出上限;可在 JSON 中显式填写上游参数,服务是否接受以原始响应为准。' : '数值沿用当前任务和连接的输出预算。'}消息、工具、接口和凭据由各自设置管理。</p>
|
||
<label>原生参数 JSON<textarea aria-label="原生参数 JSON" rows={7} maxLength={executionMode === 'unrestricted' ? undefined : 8192} spellCheck={false} value={text} disabled={busy} onChange={event => setText(event.target.value)} /></label>
|
||
<p className="mc-help">{connection.protocol === 'anthropic-messages' ? '已接入 temperature(0–1)、top_p、top_k、stop_sequences、thinking、output_config.effort。' : connection.protocol === 'responses' ? '已接入 temperature、top_p、parallel_tool_calls、reasoning.effort、text.verbosity。' : '已接入 temperature、top_p、seed、parallel_tool_calls、reasoning_effort、verbosity、DeepSeek thinking。'}推理控件与 JSON 编辑同一份参数,其他字段保存时会给出具体提示。</p>
|
||
{error && <p className="mc-card-warning" role="alert">{error}</p>}
|
||
<div className="mc-actions"><button className="button secondary" type="button" disabled={busy} onClick={onCancel}>取消</button><button className="button primary" disabled={busy} type="submit">保存原生参数</button></div>
|
||
</form>;
|
||
}
|