179 lines
6.4 KiB
TypeScript
179 lines
6.4 KiB
TypeScript
/**
|
||
* Agent Runbook 知识源只维护可公开给模型的运维规则和当前项目上下文摘要。
|
||
* 这里不读取密钥、不调用外部系统,也不生成伪造结论。
|
||
*/
|
||
import { Injectable } from '@nestjs/common';
|
||
import { DeployRunSummary } from '../deploy-runs/deploy-run.types';
|
||
import { ProjectSummary } from '../projects/project.types';
|
||
import { AgentPurpose } from './agent.types';
|
||
|
||
export type AgentRunbookEntry = {
|
||
title: string;
|
||
lines: string[];
|
||
};
|
||
|
||
export type AgentRunbookContext = {
|
||
purpose: AgentPurpose;
|
||
project?: ProjectSummary;
|
||
run?: DeployRunSummary;
|
||
};
|
||
|
||
@Injectable()
|
||
export class AgentRunbookService {
|
||
/**
|
||
* 返回当前 Agent 调用可使用的白名单 Runbook 条目。
|
||
*/
|
||
buildEntries(context: AgentRunbookContext): AgentRunbookEntry[] {
|
||
const entries: AgentRunbookEntry[] = [
|
||
this.releaseFlowEntry(),
|
||
this.jenkinsFailureEntry(),
|
||
this.notificationEntry(),
|
||
this.agentSafetyEntry(),
|
||
];
|
||
|
||
if (context.project) {
|
||
entries.unshift(this.projectEntry(context.project));
|
||
|
||
if (this.isSelfManagedProject(context.project)) {
|
||
entries.push(this.selfManagedProjectEntry(context.project));
|
||
}
|
||
}
|
||
|
||
if (context.run) {
|
||
entries.unshift(this.runEntry(context.run));
|
||
}
|
||
|
||
if (context.purpose === 'runbook-qa') {
|
||
entries.push(this.runbookQaEntry());
|
||
}
|
||
|
||
return entries;
|
||
}
|
||
|
||
private projectEntry(project: ProjectSummary): AgentRunbookEntry {
|
||
return {
|
||
title: '当前项目发布边界',
|
||
lines: [
|
||
`项目:${project.key} / ${project.name}`,
|
||
`仓库:${project.repositoryUrl}`,
|
||
`默认分支:${project.defaultBranch}`,
|
||
`项目状态:${project.status}`,
|
||
...project.environments.map(
|
||
(environment) =>
|
||
`环境 ${environment.displayName}(${environment.name}):releasePolicy=${environment.releasePolicy},refPattern=${environment.refPattern},Jenkins=${environment.jenkinsJobPath}`,
|
||
),
|
||
],
|
||
};
|
||
}
|
||
|
||
private runEntry(run: DeployRunSummary): AgentRunbookEntry {
|
||
return {
|
||
title: '当前发布单处理建议',
|
||
lines: [
|
||
`Run:${run.id}`,
|
||
`状态:${run.status}`,
|
||
`目标:${run.projectKey}/${run.environment}/${run.ref}`,
|
||
`Jenkins:queue=${run.jenkinsQueueId ?? '无'},build=${run.jenkinsBuildNumber ?? '无'}`,
|
||
`下一步:${this.nextActionForRun(run)}`,
|
||
],
|
||
};
|
||
}
|
||
|
||
private releaseFlowEntry(): AgentRunbookEntry {
|
||
return {
|
||
title: '标准发布流程',
|
||
lines: [
|
||
'发布单必须先校验项目、环境和 ref,再触发 Jenkins。',
|
||
'生产环境必须带 productionConfirmation.confirmed=true。',
|
||
'BPMN 节点顺序为 start-release、gitea-check、jenkins-build、health-check、notify-wecom、finish-release。',
|
||
'Jenkins 成功后推进 health-check 和 finish-release;通知未配置时 notify 节点 skipped,不阻断发布成功。',
|
||
],
|
||
};
|
||
}
|
||
|
||
private jenkinsFailureEntry(): AgentRunbookEntry {
|
||
return {
|
||
title: 'Jenkins 失败排查',
|
||
lines: [
|
||
'先查看 run 的 currentStep、failureSummary、step.errorSummary 和 step.logExcerpt。',
|
||
'需要完整日志时通过平台读取 /deploy-runs/:id/jenkins-log,前端不直接访问 Jenkins。',
|
||
'queued/running 状态先执行 sync-jenkins,失败或取消终态才允许 retry。',
|
||
'并发同步可能返回 409/CONFLICT,前端应刷新最新 run 后再操作。',
|
||
],
|
||
};
|
||
}
|
||
|
||
private notificationEntry(): AgentRunbookEntry {
|
||
return {
|
||
title: '通知与 outbox',
|
||
lines: [
|
||
'通知 provider 支持 wecom、feishu、generic,webhook 只允许保存在后端配置。',
|
||
'未配置 webhook 时发布流程继续执行,outbox 记录 failed/dead 摘要用于后续排查。',
|
||
'超级管理员可在 Settings 查看 notification-outbox,并对 failed/dead 消息手动重发。',
|
||
'审计和响应不得返回 webhook URL、token、cookie、authorization header 或连接串。',
|
||
],
|
||
};
|
||
}
|
||
|
||
private agentSafetyEntry(): AgentRunbookEntry {
|
||
return {
|
||
title: 'Agent 安全边界',
|
||
lines: [
|
||
'Agent 只能围绕发布风险、失败诊断、Runbook、发布说明和事故复盘回答。',
|
||
'前端不保存 LLM key、baseURL 或 model,真实调用统一由后端代理。',
|
||
'未配置 LLM 时必须返回失败状态和配置提示,不能返回 mock 成功结果。',
|
||
'Agent 配置变更必须写 AGENT_CONFIG_UPDATED 审计,只记录状态和变化字段,不记录 key/baseURL 原文。',
|
||
],
|
||
};
|
||
}
|
||
|
||
private selfManagedProjectEntry(project: ProjectSummary): AgentRunbookEntry {
|
||
return {
|
||
title: 'DevOps 平台自管理发布',
|
||
lines: [
|
||
`${project.key} 只配置 production 环境,master 合并后发布生产。`,
|
||
'该项目测试和生产视为同一套个人运维环境,不额外拆测试域名。',
|
||
'平台页面应能看到自身仓库、Jenkins job、最近构建和发布记录状态。',
|
||
],
|
||
};
|
||
}
|
||
|
||
private runbookQaEntry(): AgentRunbookEntry {
|
||
return {
|
||
title: 'Runbook 问答规则',
|
||
lines: [
|
||
'回答必须优先引用当前项目、发布单、Jenkins/Gitea 诊断和通知 outbox 状态。',
|
||
'无法从当前上下文确认的外部事实要明确说明需要进一步查看真实系统。',
|
||
'不得建议在前端直接配置或调用 Jenkins、Gitea、通知平台或 LLM 供应商。',
|
||
],
|
||
};
|
||
}
|
||
|
||
private nextActionForRun(run: DeployRunSummary): string {
|
||
if (run.status === 'queued' || run.status === 'running') {
|
||
return '同步 Jenkins 状态,等待 queue/build 结果后再判断是否需要取消。';
|
||
}
|
||
|
||
if (run.status === 'failed') {
|
||
return '查看 Jenkins 日志和失败节点,确认失败原因后决定重试或回滚。';
|
||
}
|
||
|
||
if (run.status === 'canceled') {
|
||
return '确认取消原因和 Jenkins 停止结果,必要时基于原参数重试。';
|
||
}
|
||
|
||
if (run.status === 'success') {
|
||
return '核对 health-check、通知 outbox、审计记录和线上入口。';
|
||
}
|
||
|
||
return '等待发布单进入 Jenkins queue 或执行取消。';
|
||
}
|
||
|
||
private isSelfManagedProject(project: ProjectSummary): boolean {
|
||
return (
|
||
project.key === 'devops-platform-api' ||
|
||
project.key === 'devops-platform-web'
|
||
);
|
||
}
|
||
}
|