@@ -1,14 +1,16 @@
import { useEffect , useRef , useState } from 'react' ;
import { Activity , Check , CircleHelp , KeyRound , Layers3 , Plus , Power , RefreshCw , Route , Server , Settings2 } from 'lucide-react' ;
import { api , downloadArtifact } from './api' ;
import type { Config , ModelSelection } from './types' ;
import type { Config , ModelSelection , RuntimeFailure } from './types' ;
import './ModelCenter.css' ;
import { ModelWaitSettings , type WaitSettings } from './ModelWaitSettings' ;
import { NativeParameters } from './NativeParameters' ;
import { ModelErrorDetails } from './ModelErrorDetails' ;
type Connection = { id : string ; name : string ; brand : string ; protocol : string ; baseUrl : string ; model : string ; enabled : boolean ; revision : number ; hasKey : boolean ;
requiresKey : boolean ; streaming? : boolean ; capabilityDeclarations? : Record < string , string > ; capabilities? : Record < string , string > ; capabilityEvidence ? : { checkedAt : string } ; contextLimit : number ; maxOutputTokens : number ; concurrency : number ; status : string ; activeRequests : number ; savedCredential ? : { status : string } ; health : { state : string ; reason? : string ; cooldownUntil? : number } } ;
requiresKey : boolean ; nativeParameters? : Record < string , unknown > ; outputTokenField? : string ; streaming? : boolean ; capabilityDeclarations? : Record < string , string > ; capabilities? : Record < string , string > ; capabilityEvidence ? : { checkedAt : string } ; contextLimit : number ; maxOutputTokens : number ; concurrency : number ; status : string ; activeRequests : number ; savedCredential ? : { status : string } ; health : { state : string ; reason? : string ; cooldownUntil? : number } } ;
type Pool = { id? : string ; name : string ; members : { connectionId : string ; weight : number } [ ] ; strategy : string ; sticky : boolean ; maxAttempts : number ; revision? : number ; queueTimeoutMs : number } ;
type Attempt = { id : string ; connectionId : string ; poolId : string | null ; model : string ; protocol? : string ; streaming? : boolean ; rejections ? : { connectionName : string ; capability : string ; reason : string } [ ] ; status : string ; startedAt : string ; ordinal : number ; usageStatus : string ; reason? : string ; taskId? : string ; sessionId? : string ; runId : string ; requestArtifactId? : string ; responseArtifactId? : string ; responseSha256? : string ; responseRedacted? : boolean ; stream ? : { bytes : number ; capturedBytes? : number ; frames : number ; sha256 : string ; terminal : boolean } ; usage ? : { prompt_tokens? : number ; completion_tokens? : number } } ;
type Attempt = { diagnostic? : RuntimeFailure ; upstreamResponse ? : { requestId? : string | null } ; id : string ; connectionId : string ; poolId : string | null ; model : string ; protocol? : string ; streaming? : boolean ; rejections ? : { connectionName : string ; capability : string ; reason : string } [ ] ; status : string ; startedAt : string ; ordinal : number ; usageStatus : string ; reason? : string ; taskId? : string ; sessionId? : string ; runId : string ; requestArtifactId? : string ; responseArtifactId? : string ; responseSha256? : string ; responseRedacted? : boolean ; stream ? : { bytes : number ; capturedBytes? : number ; frames : number ; sha256 : string ; terminal : boolean } ; usage ? : { prompt_tokens? : number ; completion_tokens? : number } } ;
type ImportRecord = { id : string ; source : string ; path : string ; enabled : boolean ; lastCheckedAt? : string ; lastSyncedAt? : string ; lastError? : string ; lastResult ? : { imported? : number ; conflicts? : string [ ] } } ;
type Center = { revision : number ; selected : { type : string ; id : string } ; waitSettings : WaitSettings ; connections : Connection [ ] ; pools : Pool [ ] ; imports : ImportRecord [ ] ; attempts : Attempt [ ] ; waiting : number } ;
type Operations = { checkedAt : string ; version : string ; scope : string ; activeTasks : number ; queue : number ; checks : { id : string ; title : string ; status : string ; detail : string } [ ] ;
@@ -26,10 +28,12 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
const [ tab , setTab ] = useState ( 'connections' ) ;
const [ draft , setDraft ] = useState < typeof initial & { id ? : string ; revision ? : number } > ( initial ) ;
const [ editing , setEditing ] = useState ( false ) , [ key , setKey ] = useState ( '' ) ;
const [ nativeTarget , setNativeTarget ] = useState < Connection | null > ( null ) ;
const [ persistKey , setPersistKey ] = useState ( false ) , [ persistImports , setPersistImports ] = useState ( false ) ;
const [ credentialView , setCredentialView ] = useState < CredentialView | null > ( null ) , [ credentialKey , setCredentialKey ] = useState ( '' ) , [ credentialTarget , setCredentialTarget ] = useState ( '' ) ;
const [ pool , setPool ] = useState < Pool > ( emptyPool ) , [ poolEditing , setPoolEditing ] = useState ( false ) ;
const [ working , setWorking ] = useState ( false ) , [ notice , setNotice ] = useState ( '' ) ;
const [ noticeFailure , setNoticeFailure ] = useState < RuntimeFailure | null > ( null ) ;
const [ probeScopes , setProbeScopes ] = useState < Record < string , string > > ( { } ) ;
const [ operations , setOperations ] = useState < Operations | null > ( null ) , [ autoInspect , setAutoInspect ] = useState ( false ) ;
const [ importPath , setImportPath ] = useState ( '' ) , [ importSource , setImportSource ] = useState ( 'ccswitch' ) ;
@@ -39,9 +43,10 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
const tabsRef = useRef < HTMLElement > ( null ) ;
async function refresh() { const marker = ++ generation . current ; const result = await api < Center > ( '/model-center' ) ; if ( alive . current && marker === generation . current ) setView ( result ) ; }
async function act ( action : ( ) = > Promise < void > ) {
setWorking ( true ) ; setNotice ( '' ) ;
setWorking ( true ) ; setNotice ( '' ) ; setNoticeFailure ( null ) ;
try { await action ( ) ; if ( alive . current ) { await refresh ( ) ; if ( credentialView ) await refreshCredentials ( ) ; } }
catch ( cause ) { if ( alive . current ) setNotice ( cause instanceof Error ? cause . message : '操作未完成,请刷新核对。' ) ; }
catch ( cause ) { if ( alive . current ) { setNotice ( cause instanceof Error ? cause . message : '操作未完成,请刷新核对。' ) ;
if ( cause && typeof cause === 'object' && 'diagnostic' in cause && cause . diagnostic ) setNoticeFailure ( cause . diagnostic as RuntimeFailure ) ; } }
finally { if ( alive . current ) setWorking ( false ) ; }
}
async function inspect ( offset? : number ) { const value = await api < Operations > ( '/operations' + ( offset === undefined ? '' : '?artifactOffset=' + offset ) ) ; if ( alive . current ) setOperations ( value ) ; }
@@ -65,7 +70,7 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
< nav ref = { tabsRef } className = "mc-tabs" aria-label = "模型中心分类" > { [ [ 'connections' , '模型连接' , Server ] , [ 'pools' , '模型池' , Layers3 ] , [ 'waits' , '等待时限' , Settings2 ] , [ 'attempts' , '请求账本' , Route ] , [ 'imports' , '配置导入' , Settings2 ] , [ 'credentials' , '凭据保存' , KeyRound ] , [ 'operations' , '运行巡检' , Activity ] ] . map ( ( [ id , title , Icon ] ) = > {
const Symbol = Icon as typeof Server ; return < button key = { id as string } type = "button" aria-current = { tab === id ? 'page' : undefined } onClick = { ( ) = > { setTab ( id as string ) ; setNotice ( '' ) ; } } > < Symbol size = { 16 } / > { title as string } < / button > ;
} ) } < button className = "mc-refresh" type = "button" disabled = { working } onClick = { ( ) = > void act ( refresh ) } aria-label = "刷新模型中心" > < RefreshCw size = { 16 } / > < / button > < / nav >
{ notice && < div className = "mc-notice" role = "status" > { notice } < / div > }
{ notice && < div className = "mc-notice" role = "status" > { notice } { noticeFailure && < ModelErrorDetails failure = { noticeFailure } / > } < / div > }
{ busy && < p className = "mc-notice" > 有 任 务 正 在 执 行 ; 连 接 可 停 用 , 其 他 配 置 请 在 任 务 结 束 后 修 改 。 < / p > }
{ ! view ? < p className = "mc-empty" > 正 在 读 取 实 际 连 接 状 态 … < / p > : < div className = "mc-content" >
{ tab === 'waits' && < ModelWaitSettings key = { JSON . stringify ( view . waitSettings ) } value = { view . waitSettings } disabled = { locked } onSave = { value = > void act ( async ( ) = > {
@@ -83,9 +88,9 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
< button className = "button secondary" disabled = { locked } onClick = { ( ) = > { if ( value . id === 'legacy-default' ) { onLegacy ( ) ; return ; } setDraft ( { . . . initial , . . . value , streaming : value.streaming === true , capabilityDeclarations : { . . . initial . capabilityDeclarations , . . . value . capabilityDeclarations } } ) ; setKey ( '' ) ; setPersistKey ( false ) ; setEditing ( true ) ; } } > 编 辑 < / button >
{ value . id !== 'legacy-default' && < button className = "mc-icon-action" aria-label = { ( value . enabled ? '停用' : '启用' ) + value . name } disabled = { working } onClick = { ( ) = > void act ( async ( ) = > { await api ( '/model-center/toggle' , { id : value.id , enabled : ! value . enabled } ) ; } ) } > < Power size = { 15 } / > { value . enabled ? '停用' : '启用' } < / button > }
< label className = "mc-probe-scope" > 检 测 范 围 < select aria-label = { value . name + '检测范围' } value = { probeScopes [ value . id ] ? ? 'text' } disabled = { locked } onChange = { event = > setProbeScopes ( { . . . probeScopes , [ value . id ] : event . target . value } ) } > < option value = "text" > 文 本 响 应 < / option > < option value = "tools" > 工 具 调 用 < / option > < option value = "streaming" > 流 式 文 本 < / option > < option value = "vision" > 合 成 图 像 识 别 < / option > < / select > < / label >
< button className = "mc-probe" disabled = { locked || ! value . enabled } onClick = { ( ) = > void act ( async ( ) = > { const result = await api < { scope : string } > ( '/model-center/probe' , { id : value.id , scope : probeScopes [ value . id ] ? ? 'text' } ) ; if ( alive . current ) setNotice ( capabilityLabels [ result . scope ] + '检测通过;只验证所选能力,原请求与用量已记入账本。' ) ; } ) } > 验 证 响 应 ( 调 用 模 型 ) < / button > < / footer >
< button className = "button secondary" disabled = { locked } onClick = { ( ) = > { setNativeTarget ( value ) ; setEditing ( false ) ; } } > 原 生 参 数 < / button > < button className = " mc-probe" disabled = { locked || ! value . enabled } onClick = { ( ) = > void act ( async ( ) = > { const result = await api < { scope : string } > ( '/model-center/probe' , { id : value.id , scope : probeScopes [ value . id ] ? ? 'text' } ) ; if ( alive . current ) setNotice ( capabilityLabels [ result . scope ] + '检测通过;只验证所选能力,原请求与用量已记入账本。' ) ; } ) } > 验 证 响 应 ( 调 用 模 型 ) < / button > < / footer >
< / article > ) } < / div >
{ editing && < form className = "mc-editor" aria-label = "编辑模型连接" onSubmit = { event = > { event . preventDefault ( ) ; void act ( async ( ) = > { await api ( '/model-center/connections' , { . . . draft , . . . ( key ? { apiKey : key } : { } ) , . . . ( persistKey ? { persistKey : true } : { } ) } ) ; if ( alive . current ) { setKey ( '' ) ; setEditing ( false ) ; setNotice ( persistKey ? '连接与本机加密凭据已一起保存。' : '连接已保存。' ) ; } } ) ; } } >
{ nativeTarget && < NativeParameters key = { nativeTarget . id + ":" + nativeTarget . revision } connection = { nativeTarget } busy = { locked } onCancel = { ( ) = > setNativeTarget ( null ) } onSave = { async input = > { await act ( async ( ) = > { const result = await api < { config : Config } > ( "/model-center/native-parameters" , input ) ; if ( alive . current ) { onConfig ( result . config ) ; setNativeTarget ( null ) ; setNotice ( "原生参数已保存;新任务按所选协议发送,旧请求保持原记录。" ) ; } } ) ; } } / > } { editing && < form className = "mc-editor" aria-label = "编辑模型连接" onSubmit = { event = > { event . preventDefault ( ) ; void act ( async ( ) = > { await api ( '/model-center/connections' , { . . . draft , . . . ( key ? { apiKey : key } : { } ) , . . . ( persistKey ? { persistKey : true } : { } ) } ) ; if ( alive . current ) { setKey ( '' ) ; setEditing ( false ) ; setNotice ( persistKey ? '连接与本机加密凭据已一起保存。' : '连接已保存。' ) ; } } ) ; } } >
< h3 > { draft . id ? '编辑模型连接' : '添加模型连接' } < / h3 > < div className = "mc-form-grid" >
< label > 连 接 名 称 < input required value = { draft . name } onChange = { event = > setDraft ( { . . . draft , name : event.target.value } ) } / > < / label >
< label > 服 务 品 牌 < select value = { draft . brand } onChange = { event = > setDraft ( { . . . draft , brand : event.target.value } ) } > { [ '自定义' , 'OpenAI' , 'Claude 兼容代理' , 'DeepSeek' , 'Ollama' , 'vLLM' , '兼容服务' ] . map ( brand = > < option key = { brand } > { brand } < / option > ) } < / select > < / label >
@@ -141,7 +146,7 @@ export function ModelCenter({ onConfig, onLegacy, busy }: { onConfig: (value: Co
< fieldset > < legend > 模 型 池 成 员 与 权 重 < / legend > { view . connections . map ( connection = > { const member = pool . members . find ( value = > value . connectionId === connection . id ) ; return < label key = { connection . id } className = "mc-member-choice" > < input type = "checkbox" checked = { ! ! member } onChange = { event = > setPool ( { . . . pool , members : event.target.checked ? [ . . . pool . members , { connectionId : connection.id , weight : 1 } ] : pool . members . filter ( value = > value . connectionId !== connection . id ) } ) } / > < span > { connection . name } < small > { connection . model || '尚未选择模型' } · { states [ connection . status ] } < / small > < / span > < input type = "number" aria-label = { connection . name + '权重' } disabled = { ! member } min = "1" max = "10" value = { member ? . weight ? ? 1 } onChange = { event = > setPool ( { . . . pool , members : pool.members.map ( value = > value . connectionId === connection . id ? { . . . value , weight : Number ( event . target . value ) } : value ) } ) } / > < / label > ; } ) } < / fieldset >
< div className = "mc-actions" > < button className = "button secondary" type = "button" onClick = { ( ) = > setPoolEditing ( false ) } > 取 消 < / button > < button className = "button primary" disabled = { locked || ! pool . members . length } > 保 存 模 型 池 < / button > < / div > < / form > }
< / > }
{ tab === 'attempts' && < > < div className = "mc-section-title" > < div > < h3 > 请 求 账 本 < / h3 > < p > 最 近 100 次 物 理 请 求 ; 最 多 保 存 1000 次 。 未 知 用 量 保 持 未 知 。 < / p > < / div > < span > { view . attempts . length } 条 记 录 < / span > < / div > { ! view . attempts . length && < p className = "mc-empty" > 当 前 没 有 模 型 中 心 请 求 。 选 择 连 接 或 模 型 池 执 行 任 务 后 , 实 际 尝 试 会 保 存 在 这 里 。 < / p > } < div className = "mc-attempts" > { view . attempts . map ( value = > < article key = { value . id } > < div > < span className = { 'mc-state mc-state-' + value . status } > { states [ value . status ] } < / span > < strong > { value . model } < / strong > < time > { new Date ( value . startedAt ) . toLocaleString ( ) } < / time > < / div > < p > { view . connections . find ( connection = > connection . id === value . connectionId ) ? . name || '未派发' } · { value . protocol === 'responses' ? 'Responses' : value . protocol ? 'Chat Completions' : '能力筛选' } { value . streaming ? ' · 流式' : '' } · { value . ordinal ? '尝试 ' + value . ordinal : '未发送' } · { value . usageStatus === 'reported' ? ` ${ value . usage ? . prompt_tokens ? ? '?' } 输入 / ${ value . usage ? . completion_tokens ? ? '?' } 输出 tokens ` : value . usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知' } < / p > < code > Run { value . runId } < / code > { value . stream && < div className = "mc-stream-receipt" > < p > { value . stream . terminal ? "响应完整终结" : "响应未完整,片段保留待核对" } · 已 接 收 { value . stream . bytes . toLocaleString ( ) } bytes · { value . stream . frames } 个 事 件 { value . stream . capturedBytes !== undefined && value . stream . capturedBytes < value . stream . bytes ? " · 仅保存上限内片段" : "" } < / p > < code > 原 流 SHA256 { value . stream . sha256 } < / code > { value . responseSha256 && < code > 工 件 SHA256 { value . responseSha256 } { value . responseRedacted ? "(已脱敏)" : "" } < / code > } { value . sessionId && value . responseArtifactId && < button className = "button secondary" disabled = { working } onClick = { ( ) = > void act ( async ( ) = > { await downloadArtifact ( value . sessionId ! , value . responseArtifactId ! , "model-stream-" + value . id + ".sse" ) ; } ) } > 下 载 已 保 存 的 响 应 < / button > } < / div > } { value . reason && < p className = "mc-card-warning" > { value . reason } < / p > } { ! ! value . rejections ? . length && < p className = "mc-route-rejections" > { value . rejections . map ( item = > item . connectionName + ': ' + ( capabilityLabels [ item . capability ] ? ? item . capability ) + ' · ' + item . reason ) . join ( '; ' ) } < / p > } < / article > ) } < / div > < / > }
{ tab === 'attempts' && < > < div className = "mc-section-title" > < div > < h3 > 请 求 账 本 < / h3 > < p > 最 近 100 次 物 理 请 求 ; 最 多 保 存 1000 次 。 未 知 用 量 保 持 未 知 。 < / p > < / div > < span > { view . attempts . length } 条 记 录 < / span > < / div > { ! view . attempts . length && < p className = "mc-empty" > 当 前 没 有 模 型 中 心 请 求 。 选 择 连 接 或 模 型 池 执 行 任 务 后 , 实 际 尝 试 会 保 存 在 这 里 。 < / p > } < div className = "mc-attempts" > { view . attempts . map ( value = > < article key = { value . id } > < div > < span className = { 'mc-state mc-state-' + value . status } > { states [ value . status ] } < / span > < strong > { value . model } < / strong > < time > { new Date ( value . startedAt ) . toLocaleString ( ) } < / time > < / div > < p > { view . connections . find ( connection = > connection . id === value . connectionId ) ? . name || '未派发' } · { value . protocol === 'responses' ? 'Responses' : value . protocol ? 'Chat Completions' : '能力筛选' } { value . streaming ? ' · 流式' : '' } · { value . ordinal ? '尝试 ' + value . ordinal : '未发送' } · { value . usageStatus === 'reported' ? ` ${ value . usage ? . prompt_tokens ? ? '?' } 输入 / ${ value . usage ? . completion_tokens ? ? '?' } 输出 tokens ` : value . usageStatus === 'not_dispatched' ? '未产生请求用量' : '用量未知' } < / p > < code > Run { value . runId } < / code > { value . stream && < div className = "mc-stream-receipt" > < p > { value . stream . terminal ? "响应完整终结" : "响应未完整,片段保留待核对" } · 已 接 收 { value . stream . bytes . toLocaleString ( ) } bytes · { value . stream . frames } 个 事 件 { value . stream . capturedBytes !== undefined && value . stream . capturedBytes < value . stream . bytes ? " · 仅保存上限内片段" : "" } < / p > < code > 原 流 SHA256 { value . stream . sha256 } < / code > { value . responseSha256 && < code > 工 件 SHA256 { value . responseSha256 } { value . responseRedacted ? "(已脱敏)" : "" } < / code > } { value . sessionId && value . responseArtifactId && < button className = "button secondary" disabled = { working } onClick = { ( ) = > void act ( async ( ) = > { await downloadArtifact ( value . sessionId ! , value . responseArtifactId ! , "model-stream-" + value . id + ".sse" ) ; } ) } > 下 载 已 保 存 的 响 应 < / button > } < / div > } { value . reason && < p className = "mc-card-warning" > { value . reason } < / p > } { value . diagnostic && < ModelErrorDetails failure = { value . diagnostic } / > } { ! value . diagnostic && value . upstreamResponse ? . requestId && < p className = "mc-help" > 上 游 请 求 ID : { value . upstreamResponse . requestId } < / p > } { value . requestArtifactId && value . sessionId && < button className = "button secondary" disabled = { working } onClick = { ( ) = > void act ( async ( ) = > { await downloadArtifact ( value . sessionId ! , value . requestArtifactId ! , "model-request-" + value . id + ".json" ) ; } ) } > 下 载 实 际 请 求 < / button > } { ! ! value . rejections ? . length && < p className = "mc-route-rejections" > { value . rejections . map ( item = > item . connectionName + ': ' + ( capabilityLabels [ item . capability ] ? ? item . capability ) + ' · ' + item . reason ) . join ( '; ' ) } < / p > } < / article > ) } < / div > < / > }
{ tab === 'imports' && < > < div className = "mc-section-title" > < div > < h3 > 已 有 配 置 导 入 < / h3 > < p > 先 预 览 来 源 和 模 型 , 再 导 入 。 原 客 户 端 配 置 保 持 原 样 。 < / p > < / div > < button className = "button secondary" disabled = { working } onClick = { ( ) = > void act ( async ( ) = > { const value = await api < { sources : typeof discovery } > ( '/model-center/imports' ) ; if ( alive . current ) setDiscovery ( value . sources ) ; } ) } > 识 别 本 机 来 源 < / button > < / div >
{ view . imports . length > 0 && < > < h4 > 已 登 记 来 源 < / h4 > { view . imports . map ( value = > < article className = "mc-import-source" key = { value . id } > < strong > { value . source } < / strong > < code > { value . path } < / code > < span > { value . enabled ? '每分钟检查' : '自动同步已关闭' } < / span > { value . lastCheckedAt && < p > 最 近 检 查 { new Date ( value . lastCheckedAt ) . toLocaleString ( ) } < / p > } { value . lastError && < p className = "mc-card-warning" > { value . lastError } < / p > } { ! ! value . lastResult ? . conflicts ? . length && < p className = "mc-card-warning" > 有 { value . lastResult . conflicts . length } 项 与 手 动 修 改 冲 突 , 已 保 留 工 作 台 中 的 修 改 。 < / p > } < button className = "button secondary" disabled = { locked } onClick = { ( ) = > void act ( async ( ) = > { await api ( '/model-center/import-toggle' , { id : value.id , enabled : ! value . enabled } ) ; } ) } > { value . enabled ? '关闭自动同步' : '启用自动同步' } < / button > < / article > ) } < button className = "button secondary" disabled = { locked || ! view . imports . some ( value = > value . enabled ) } onClick = { ( ) = > void act ( async ( ) = > { const result = await api < { results : { imported? : string [ ] ; conflicts? : string [ ] ; error? : string } [ ] } > ( '/model-center/import-sync' , { } ) ; const imported = result . results . reduce ( ( sum , value ) = > sum + ( value . imported ? . length ? ? 0 ) , 0 ) ; const conflicts = result . results . reduce ( ( sum , value ) = > sum + ( value . conflicts ? . length ? ? 0 ) , 0 ) ; if ( alive . current ) setNotice ( ` 已检查启用来源:更新 ${ imported } 项,冲突 ${ conflicts } 项。 ` ) ; } ) } > 立 即 同 步 已 启 用 来 源 < / button > < / > }
{ discovery . map ( value = > < article className = "mc-import-source" key = { value . source } > < strong > { value . source } < / strong > < code > { value . path || '当前版本尚无可发现的配置路径' } < / code > < span > { value . available ? value . supported ? '可预览' : '当前不支持' : '未发现' } < / span > { value . detail && < p > { value . detail } < / p > } < button className = "button secondary" disabled = { ! value . available || ! value . supported || locked } onClick = { ( ) = > { setImportSource ( value . source ) ; setImportPath ( value . path ) ; } } > 选 择 来 源 < / button > < / article > ) }