fix: select capable models and align effective permission evidence
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
export type ApprovalMode = 'ask' | 'auto' | 'full';
|
||||
export interface PermissionConfig { approvalMode?: ApprovalMode; executionMode?: string }
|
||||
export interface McpPermissionContext { schemaVersion: 1; approvalMode: ApprovalMode; executionMode: string; action: 'execute' | 'request-approval'; source: string }
|
||||
export const approvalModes: ApprovalMode[];
|
||||
export const approvalLabels: Record<ApprovalMode, string>;
|
||||
export function mcpPermissionContext(config: PermissionConfig): McpPermissionContext;
|
||||
export function mcpPermissionDescription(context: McpPermissionContext): string;
|
||||
export function automaticApproval(config: PermissionConfig, action?: Record<string, unknown>): { approve: boolean; mode: ApprovalMode; source: string };
|
||||
export function unrestricted(value: unknown): boolean;
|
||||
export function ceiling(value: unknown, restrictedMaximum: number): number;
|
||||
export function openToolDefinitions<T>(tools: T[], config: PermissionConfig): T[];
|
||||
export function executionMode(value?: string): string;
|
||||
export const openDefaults: Readonly<Record<string, string | number>>;
|
||||
@@ -5,6 +5,15 @@ export const ceiling = (value, restrictedMaximum) => unrestricted(value) ? Infin
|
||||
|
||||
export const approvalModes = ['ask', 'auto', 'full'];
|
||||
export const approvalLabels = { ask: '请求批准', auto: '帮我批准', full: '完全访问权限' };
|
||||
export function mcpPermissionContext(config) {
|
||||
const decision = automaticApproval(config, { command: 'MCP程序' });
|
||||
return { schemaVersion: 1, approvalMode: decision.mode, executionMode: config.executionMode ?? 'restricted',
|
||||
action: decision.approve ? 'execute' : 'request-approval', source: decision.source };
|
||||
}
|
||||
export function mcpPermissionDescription(context) {
|
||||
return context.action === 'execute' ? '当前完全访问权限:任务中的已启用MCP调用直接执行,无需额外询问。'
|
||||
: '当前' + (approvalLabels[context.approvalMode] ?? approvalLabels.ask) + ':外部MCP调用由工作台请求批准;模型直接提交工具调用,不要先用文字重复询问。';
|
||||
}
|
||||
export function automaticApproval(config, action = {}) {
|
||||
const mode = config.approvalMode ?? 'ask';
|
||||
if (mode === 'full') return { approve: true, mode, source: 'full-access-policy' };
|
||||
@@ -17,6 +26,8 @@ export function automaticApproval(config, action = {}) {
|
||||
}
|
||||
|
||||
export function openToolDefinitions(tools, config) {
|
||||
tools = tools.map(tool => tool.function.name === 'call_registered_mcp' ? { ...tool, function: { ...tool.function,
|
||||
description: '调用本次目录的已启用MCP工具。' + mcpPermissionDescription(mcpPermissionContext(config)) + '先核对固定程序/实际握手/schema,不自动重试;完整结果按工件读取。' } } : tool);
|
||||
if (!unrestricted(config)) return tools.filter(tool => !['rollback_change', 'open_browser_tab'].includes(tool.function.name));
|
||||
const open = value => Array.isArray(value) ? value.map(open) : value && typeof value === 'object'
|
||||
? Object.fromEntries(Object.entries(value).filter(([key]) => !['maxLength', 'maxItems', 'maximum'].includes(key)).map(([key, item]) => [key, open(item)])) : value;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export interface ModelIdentity {
|
||||
schemaVersion: number; requestedModel: string; reportedModels: string[]; status: string; complete: boolean; invalidIdentifiers: boolean; policy: string;
|
||||
effectivePolicy?: string; effectivePolicySource?: string;
|
||||
mapping?: { models: string[]; source: string; kind: string }; responseSource?: { artifactId: string; sha256: string; sessionId: string; scope: string }; scope: string;
|
||||
}
|
||||
export function modelIdentityLabel(identity?: ModelIdentity | null): string;
|
||||
|
||||
@@ -5,7 +5,7 @@ export function modelIdentitySettings(input = {}, previous = {}) {
|
||||
const fields = ['modelIdentityPolicy', 'modelIdentityAliases', 'modelIdentityAliasSource'];
|
||||
if (!fields.some(key => Object.hasOwn(input, key) || Object.hasOwn(previous, key))) return {};
|
||||
const modelIdentityPolicy = input.modelIdentityPolicy ?? previous.modelIdentityPolicy ?? 'record';
|
||||
if (!['record', 'require-match'].includes(modelIdentityPolicy)) throw new Error('请选择记录型号或型号不一致时暂停。');
|
||||
if (!['record', 'require-match'].includes(modelIdentityPolicy)) throw new Error('请选择记录型号或严格核对型号。');
|
||||
const modelIdentityAliases = input.modelIdentityAliases ?? previous.modelIdentityAliases ?? [];
|
||||
const modelIdentityAliasSource = input.modelIdentityAliasSource ?? previous.modelIdentityAliasSource ?? '';
|
||||
if (!Array.isArray(modelIdentityAliases) || modelIdentityAliases.length > 8 || !modelIdentityAliases.every(validModel)
|
||||
@@ -15,6 +15,12 @@ export function modelIdentitySettings(input = {}, previous = {}) {
|
||||
return { modelIdentityPolicy, modelIdentityAliases: [...modelIdentityAliases], modelIdentityAliasSource: modelIdentityAliasSource.trim() };
|
||||
}
|
||||
|
||||
export function modelIdentityPolicyContext(config = {}) {
|
||||
const policy = config.modelIdentityPolicy ?? 'record';
|
||||
return { policy, effectivePolicy: config.executionMode === 'unrestricted' ? 'record' : policy,
|
||||
effectivePolicySource: config.executionMode === 'unrestricted' ? 'unrestricted-execution' : 'connection-policy' };
|
||||
}
|
||||
|
||||
export function modelIdentityObserver(requestedModel, config = {}) {
|
||||
const settings = modelIdentitySettings(config), models = new Set(); let invalid = false;
|
||||
return {
|
||||
@@ -29,8 +35,8 @@ export function modelIdentityObserver(requestedModel, config = {}) {
|
||||
const reportedModels = [...models];
|
||||
const status = !complete ? 'incomplete' : invalid ? 'invalid' : !reportedModels.length ? 'missing' : reportedModels.length > 1 ? 'mixed'
|
||||
: reportedModels[0] === requestedModel ? 'matched' : settings.modelIdentityAliases?.includes(reportedModels[0]) ? 'mapped' : 'mismatch';
|
||||
return { schemaVersion: 1, requestedModel, reportedModels, status, complete: complete === true, invalidIdentifiers: invalid,
|
||||
policy: settings.modelIdentityPolicy ?? 'record',
|
||||
return { schemaVersion: 2, requestedModel, reportedModels, status, complete: complete === true, invalidIdentifiers: invalid,
|
||||
...modelIdentityPolicyContext(config),
|
||||
...(settings.modelIdentityAliases?.length ? { mapping: { models: settings.modelIdentityAliases, source: settings.modelIdentityAliasSource, kind: 'user-declared' } } : {}),
|
||||
scope: 'Identifiers in the outgoing request and upstream response; neither equal identifiers nor a declared mapping authenticate model weights.' };
|
||||
},
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { capabilityView } from './model-capabilities.mjs';
|
||||
|
||||
// This is an automatic-selection policy, not a gate on an explicitly selected
|
||||
// connection. Keep the observations bound to the frozen member/revisions.
|
||||
export function modelRoutingDecision(routing, requirements, { open = false, probe = false, availability = () => null } = {}) {
|
||||
const automatic = routing.type === 'pool' && open && !probe;
|
||||
const passthrough = open && !automatic || probe;
|
||||
const candidates = routing.members.map(member => {
|
||||
const required = probe ? [] : [...new Set([...requirements, ...(member.streaming ? ['streaming'] : [])])];
|
||||
const view = capabilityView(member, member.credentialRevision);
|
||||
const upstream = member.upstreamModel;
|
||||
const catalog = upstream?.id === member.model && (!upstream.endpoint || upstream.endpoint === member.baseUrl) ? upstream : null;
|
||||
const capabilities = Object.fromEntries(required.map(capability => {
|
||||
let state = view[capability] ?? 'unknown';
|
||||
let source = state === 'verified' ? 'revision-bound-observation' : state === 'unknown' ? 'unknown' : 'connection-declaration';
|
||||
if (automatic && catalog?.capabilityDeclarations?.[capability] === 'unsupported') {
|
||||
state = 'unsupported'; source = 'upstream-model-list';
|
||||
} else if (automatic && state === 'unknown' && catalog?.capabilityDeclarations?.[capability] === 'supported') {
|
||||
state = 'upstream_declared'; source = 'upstream-model-list';
|
||||
}
|
||||
return [capability, { state, source }];
|
||||
}));
|
||||
const reasons = required.filter(capability => capabilities[capability].state === 'unsupported' || !passthrough && !automatic && capabilities[capability].state === 'unknown')
|
||||
.map(capability => ({ capability, ...capabilities[capability], reason: capabilities[capability].state === 'unsupported' ? '明确声明不支持所需能力' : '尚未声明或验证所需能力' }));
|
||||
if (automatic && catalog?.lifecycle === 'retired') reasons.push({ capability: 'lifecycle', state: 'retired', source: 'upstream-model-list', reason: '相符上游目录已标记型号退役' });
|
||||
const unavailable = availability(member);
|
||||
return { connectionId: member.id, connectionName: member.name, model: member.model, protocol: member.protocol ?? 'chat-completions',
|
||||
connectionRevision: member.revision, credentialRevision: member.credentialRevision, requirements: required, capabilities,
|
||||
...(catalog ? { catalog: { id: catalog.id, fetchedAt: catalog.fetchedAt ?? null, lifecycle: catalog.lifecycle ?? null, source: 'upstream-model-list' } } : {}),
|
||||
tier: passthrough ? 'passthrough' : reasons.length ? 'unsupported' : Object.values(capabilities).some(value => value.state === 'unknown') ? 'unverified' : 'known',
|
||||
reasons, unavailable, disposition: unavailable ? 'unavailable' : !passthrough && reasons.length ? 'excluded' : 'eligible' };
|
||||
});
|
||||
const eligible = candidates.filter(value => value.disposition === 'eligible');
|
||||
const tier = automatic ? eligible.some(value => value.tier === 'known') ? 'known' : 'unverified' : passthrough ? 'passthrough' : 'known';
|
||||
if (automatic) for (const candidate of eligible) if (candidate.tier !== tier) candidate.disposition = 'lower_priority';
|
||||
return { schemaVersion: 1, mode: probe ? 'probe' : automatic ? 'automatic' : open ? 'manual-passthrough' : 'restricted',
|
||||
reason: automatic ? eligible.length ? tier === 'known' ? 'known-capabilities' : 'unverified-fallback' : 'no-eligible-candidate' : passthrough ? 'explicit-selection' : 'restricted-capabilities',
|
||||
requirements: [...requirements], selectedConnectionId: null, candidates };
|
||||
}
|
||||
Reference in New Issue
Block a user