feat: 接入精确回滚与确认后执行的运维操作

- 生成绑定操作者与 SHA 的签名计划,确认后复用发布、取消和重试服务,限制过期、并发与重复提交。

- 从真实历史构建核实回滚目标,升级纳管 Jenkins 任务以锁定提交并保留原参数选择器。

- Agent 只准备操作卡片,不直接执行;补充权限、回放、历史令牌与实际部署参数回归。
This commit is contained in:
湛兮
2026-10-28 10:18:01 +08:00
parent 3cd644158d
commit a29066f343
26 changed files with 1486 additions and 32 deletions
+68 -3
View File
@@ -1,4 +1,9 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Optional } from '@nestjs/common';
import { OperationsService } from '../operations/operations.service';
import {
OperationPlan,
prepareOperationSchema,
} from '../operations/operation.dto';
import { z } from 'zod';
import { AuthService } from '../auth/auth.service';
import { AuthenticatedUser } from '../auth/auth.types';
@@ -55,6 +60,27 @@ const object = (
) => ({ type: 'object', properties, required, additionalProperties: false });
const string = { type: 'string' };
export const agentToolDefinitions: AgentToolDefinition[] = [
{
type: 'function',
function: {
name: 'prepare_operation',
description:
'用户明确要求发布、回滚、取消、重试时,生成待确认的操作计划。仅准备,不执行。用户必须在界面点确认执行。部署/默认回滚必须明确项目与环境,不得猜测环境;指定历史发布可用 runId。生产操作也只生成计划。没有返回计划就不能声称已准备或已执行。',
parameters: object(
{
kind: {
type: 'string',
enum: ['deploy', 'rollback', 'cancel', 'retry'],
},
projectKey: string,
environment: { type: 'string', enum: ['test', 'production'] },
ref: string,
runId: string,
},
['kind'],
),
},
},
{
type: 'function',
function: {
@@ -134,6 +160,7 @@ export type AgentToolSession = {
sources: Map<string, AgentSource>;
traces: AgentToolTrace[];
signal: AbortSignal;
plans: OperationPlan[];
};
@Injectable()
@@ -143,6 +170,7 @@ export class AgentToolsService {
private readonly repository: DeployRunRepository,
private readonly auth: AuthService,
private readonly jenkins: JenkinsClient,
@Optional() private readonly operations?: OperationsService,
) {}
dataSource() {
@@ -178,6 +206,7 @@ export class AgentToolsService {
signal,
sources: new Map(),
traces: [],
plans: [],
};
if (runId) await this.requireRun(session, runId);
return session;
@@ -192,7 +221,42 @@ export class AgentToolsService {
session.signal.throwIfAborted();
const args: unknown = JSON.parse(argumentsText);
let result: unknown;
if (name === 'list_projects') {
if (name === 'prepare_operation') {
if (!this.operations)
throw new AppError('CONFIG_MISSING', '操作服务未启用', 503);
if (session.plans.length >= 2)
throw new AppError(
'VALIDATION_FAILED',
'一次提问最多准备两个操作计划',
400,
);
const input = prepareOperationSchema.parse(args);
if (
'projectKey' in input &&
input.projectKey &&
!session.projects.some((project) => project.key === input.projectKey)
)
throw new AppError('FORBIDDEN', '项目不在当前查询范围', 403);
if ('runId' in input && input.runId)
await this.requireRun(session, input.runId);
const plan = await this.operations.prepare(input, session.operator);
if (
!session.projects.some((project) => project.key === plan.projectKey)
)
throw new AppError('FORBIDDEN', '项目不在当前查询范围', 403);
session.plans.push(plan);
result = {
planId: plan.id,
kind: plan.kind,
projectKey: plan.projectKey,
environment: plan.environment,
ref: plan.ref,
commit: plan.commit,
expiresAt: plan.expiresAt,
status: 'awaiting-user-confirmation',
message: '计划已准备,尚未执行;请用户点击操作卡片的确认执行按钮。',
};
} else if (name === 'list_projects') {
const input = projectSchema.parse(args);
const filtered = session.projects.filter(
(project) =>
@@ -376,7 +440,7 @@ export class AgentToolsService {
} else
throw new AppError(
'VALIDATION_FAILED',
'未知工具;仅提供只读项目、部署及日志查询',
'未知工具;仅提供查询和准备待确认的操作计划',
400,
);
const text = redactSensitive({ text: JSON.stringify(result) }).text;
@@ -392,6 +456,7 @@ export class AgentToolsService {
summary:
(
{
prepare_operation: '准备待确认的操作计划(尚未执行)',
list_projects: '查询项目清单',
list_deployments: '查询部署记录',
get_deployment: '读取部署详情',