150 lines
6.2 KiB
TypeScript
150 lines
6.2 KiB
TypeScript
import { z } from "zod";
|
||
import type { KyInstance } from "ky";
|
||
import * as api from "../api.js";
|
||
import {
|
||
ThinkingPolicySchema,
|
||
type ThinkingPolicy,
|
||
type DecisionPreferences
|
||
} from "../types.js";
|
||
import type {
|
||
CapabilityContext
|
||
} from "./helpers.js";
|
||
import * as helpers from "./helpers.js";
|
||
|
||
const {
|
||
mutationPreview
|
||
} = helpers;
|
||
|
||
export const preferenceCapabilities = [
|
||
{
|
||
name: "sealPreferenceGet",
|
||
description: "获取当前决策偏好、思考深度偏好与绑定的规则版本",
|
||
parameters: z.object({}),
|
||
handler: async (client: KyInstance) => {
|
||
const config = (await api.getApprovalStylePreferences(client)) as any;
|
||
return {
|
||
decisionPreferences: config.decisionPreferences,
|
||
thinkingDepthPreference: config.thinkingDepthPreference,
|
||
ruleSetVersionId: config.ruleSetVersionId
|
||
};
|
||
}
|
||
},
|
||
{
|
||
name: "sealPreferenceDecisionUpdate",
|
||
description: "修改审核决策偏好(立即影响当前运行中的审核智能体及后续决策)",
|
||
parameters: z.object({
|
||
explicitApprove: z.string().optional(),
|
||
explicitReject: z.string().optional(),
|
||
exemptApprove: z.string().optional(),
|
||
review: z.string().optional(),
|
||
dryRun: z.boolean().optional().describe("只返回前后配置 Diff,不执行写入")
|
||
}),
|
||
handler: async (client: KyInstance, params: Partial<DecisionPreferences> & { dryRun?: boolean }, context: CapabilityContext) => {
|
||
const config = (await api.getApprovalStylePreferences(client, context.corp.seal.endpoints.approvalStylePreferences)) as any;
|
||
const { dryRun, ...changes } = params;
|
||
const updated = {
|
||
...config,
|
||
decisionPreferences: {
|
||
...config.decisionPreferences,
|
||
...changes
|
||
}
|
||
};
|
||
if (dryRun) return mutationPreview(context, context.corp.seal.endpoints.approvalStylePreferences, config, updated, 1);
|
||
await api.updateApprovalStylePreferences(client, updated, context.corp.seal.endpoints.approvalStylePreferences);
|
||
return {
|
||
decisionPreferences: updated.decisionPreferences
|
||
};
|
||
}
|
||
},
|
||
{
|
||
name: "sealPreferenceThinkingDepthSet",
|
||
description: "修改默认思考深度(medium 或 high)",
|
||
parameters: z.object({
|
||
defaultLevel: z.enum(["medium", "high"]).describe("默认思考深度"),
|
||
dryRun: z.boolean().optional().describe("只返回前后配置 Diff,不执行写入")
|
||
}),
|
||
handler: async (client: KyInstance, params: { defaultLevel: "medium" | "high"; dryRun?: boolean }, context: CapabilityContext) => {
|
||
const config = (await api.getApprovalStylePreferences(client, context.corp.seal.endpoints.approvalStylePreferences)) as any;
|
||
const updated = {
|
||
...config,
|
||
thinkingDepthPreference: {
|
||
defaultLevel: params.defaultLevel,
|
||
routingPolicies: config.thinkingDepthPreference?.routingPolicies ?? []
|
||
}
|
||
};
|
||
if (params.dryRun) return mutationPreview(context, context.corp.seal.endpoints.approvalStylePreferences, config, updated, 1);
|
||
await api.updateApprovalStylePreferences(client, updated, context.corp.seal.endpoints.approvalStylePreferences);
|
||
return {
|
||
thinkingDepthPreference: updated.thinkingDepthPreference
|
||
};
|
||
}
|
||
},
|
||
{
|
||
name: "sealPreferenceThinkingDepthPoliciesList",
|
||
description: "列出思考深度路由策略列表",
|
||
parameters: z.object({}),
|
||
handler: async (client: KyInstance) => {
|
||
const config = (await api.getApprovalStylePreferences(client)) as any;
|
||
return {
|
||
policies: config.thinkingDepthPreference?.routingPolicies ?? []
|
||
};
|
||
}
|
||
},
|
||
{
|
||
name: "sealPreferenceThinkingDepthPoliciesUpsert",
|
||
description: "新增或修改思考深度路由策略",
|
||
parameters: ThinkingPolicySchema.extend({
|
||
dryRun: z.boolean().optional().describe("只返回前后配置 Diff,不执行写入")
|
||
}),
|
||
handler: async (client: KyInstance, policy: ThinkingPolicy & { dryRun?: boolean }, context: CapabilityContext) => {
|
||
const config = (await api.getApprovalStylePreferences(client, context.corp.seal.endpoints.approvalStylePreferences)) as any;
|
||
const { dryRun, ...cleanPolicy } = policy;
|
||
const existing: ThinkingPolicy[] = config.thinkingDepthPreference?.routingPolicies ?? [];
|
||
const next = existing.filter((item) => item.id !== cleanPolicy.id);
|
||
next.push(cleanPolicy);
|
||
const updated = {
|
||
...config,
|
||
thinkingDepthPreference: {
|
||
defaultLevel: config.thinkingDepthPreference?.defaultLevel ?? "medium",
|
||
routingPolicies: next
|
||
}
|
||
};
|
||
if (dryRun) return mutationPreview(context, context.corp.seal.endpoints.approvalStylePreferences, config, updated, 1);
|
||
await api.updateApprovalStylePreferences(client, updated, context.corp.seal.endpoints.approvalStylePreferences);
|
||
return {
|
||
policy: cleanPolicy,
|
||
policies: next
|
||
};
|
||
}
|
||
},
|
||
{
|
||
name: "sealPreferenceThinkingDepthPoliciesRemove",
|
||
description: "删除一条思考深度路由策略",
|
||
parameters: z.object({
|
||
policyId: z.string().describe("路由策略 ID"),
|
||
dryRun: z.boolean().optional().describe("只返回前后配置 Diff,不执行写入")
|
||
}),
|
||
handler: async (client: KyInstance, params: { policyId: string; dryRun?: boolean }, context: CapabilityContext) => {
|
||
const config = (await api.getApprovalStylePreferences(client, context.corp.seal.endpoints.approvalStylePreferences)) as any;
|
||
const existing: ThinkingPolicy[] = config.thinkingDepthPreference?.routingPolicies ?? [];
|
||
const next = existing.filter((item) => item.id !== params.policyId);
|
||
if (next.length === existing.length) {
|
||
throw new Error(`未找到路由策略:${params.policyId}`);
|
||
}
|
||
const updated = {
|
||
...config,
|
||
thinkingDepthPreference: {
|
||
defaultLevel: config.thinkingDepthPreference?.defaultLevel ?? "medium",
|
||
routingPolicies: next
|
||
}
|
||
};
|
||
if (params.dryRun) return mutationPreview(context, context.corp.seal.endpoints.approvalStylePreferences, config, updated, 1);
|
||
await api.updateApprovalStylePreferences(client, updated, context.corp.seal.endpoints.approvalStylePreferences);
|
||
return {
|
||
removedId: params.policyId,
|
||
policies: next
|
||
};
|
||
}
|
||
}
|
||
];
|