witty-diagnosis-agent 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Huawei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # witty-diagnosis-agent
2
+
3
+ witty-diagnosis-agent 是一个自动化诊断系统,旨在为复杂的系统问题提供标准化的分析和故障排除。当前版本兼容支持 **OpenCode** 运行环境。
4
+
5
+ ## 核心设计理念 (Core Philosophy)
6
+
7
+ 本方案严格遵循 **"发现→收集→定位→根因→方案→实施→验证"** 的标准化运维作业流程,结合**多模块协作 (Multi-Module)** 架构,实现全生命周期的自动化管理。
8
+
9
+ ### 核心流程图解
10
+
11
+ `1.问题输入/采集` → `2.收集信息` → `3.定位故障` → `4.分析根因` → `5.制定方案` → `6.实施修复` → `7.验证效果`
12
+
13
+ ## 核心组件功能 (Core Components & Functions)
14
+
15
+ | 组件名称 | 功能描述 (Function) |
16
+ | :--- | :--- |
17
+ | **Commander** | **1. 发现问题 & 全局统筹**。作为系统入口接收告警或用户输入,初始化故障上下文,**路由请求**,**调度**各功能模块。 |
18
+ | **Investigator** | **2. 收集信息 & 3. 定位故障**。**并发执行**多个采集任务收集数据,并按“应用→系统→网络→硬件”逻辑进行分层定位。 |
19
+ | **Analyst** | **4. 分析根因**。基于收集的信息进行**逻辑分析**,确定 Root Cause。 |
20
+ | **Strategist** | **5. 制定方案**。生成修复计划,包含临时止血措施和永久修复方案,并评估风险。 |
21
+ | **Guardian** | **风险控制**。在 Step 5/6 阶段审查修复方案的风险,执行审批策略,防止二次故障。 |
22
+ | **Operator** | **6. 实施修复**。执行 Strategist 制定的修复脚本(需通过 Guardian 审批)。 |
23
+ | **Auditor** | **7. 验证效果**。执行冒烟测试 (Smoke Test) 和指标回归检查。 |
24
+
25
+ ## 功能扩展机制 (Functional Extensibility)
26
+
27
+ 系统支持高度的扩展性,允许在标准流程中灵活插入或替换**功能插件**。
28
+
29
+ ### 1. 阶段扩展 (Phase Extensions)
30
+ 支持在任意标准阶段(如 `Investigator` 之后,`Analyst` 之前)挂载**自定义插件 (Hook Plugins)**。
31
+ * **用途**: 针对特定技术栈(如 Redis, Kafka)增加专用的信息收集或分析步骤。
32
+ * **机制**: **执行引擎**会自动检测并执行注册的 `pre-step` 或 `post-step` **扩展插件**。
33
+
34
+ ### 2. 端到端接管 (End-to-End Override)
35
+ 支持跳过标准的 7 步流程,直接调用专用的**端到端流程 (Dedicated Workflows)**。
36
+ * **用途**: 针对已知的高频特定故障模式(如 "K8s Pod OOM" 或 "MySQL 死锁"),直接执行固化的诊断与修复剧本。
37
+ * **机制**: `Commander` 在第一步识别场景后,可直接路由至**专用流程**,绕过通用的分层排查流程。
38
+
39
+ ## 安装与开发 (Installation & Development)
40
+
41
+ ### 前置要求 (Prerequisites)
42
+
43
+ * [Bun](https://bun.sh) (v1.0.0+)
44
+ * [OpenCode](https://github.com/anomalyco/opencode)
45
+
46
+ ### 源码构建与安装 (Build from Source)
47
+
48
+ 如果你想从源码构建并安装本插件,请遵循以下步骤:
49
+
50
+ 1. **安装依赖**:
51
+ ```bash
52
+ bun install
53
+ ```
54
+
55
+ 2. **编译项目**:
56
+ ```bash
57
+ bun run build
58
+ ```
59
+ 这将编译 TypeScript 源码到 `dist/` 目录。
60
+
61
+ 3. **构建二进制文件 (可选)**:
62
+ ```bash
63
+ bun run build:binaries
64
+ ```
65
+ 这将为不同平台(macOS, Linux, Windows)生成独立的可执行文件,位于 `packages/` 目录下。
66
+
67
+ 4. **安装插件**:
68
+ ```bash
69
+ # 使用开发环境的脚本进行安装
70
+ ./bin/witty-diagnosis-agent.js install
71
+ ```
72
+ 该命令会自动检测你的 OpenCode 配置文件位置,并将 `witty-diagnosis-agent` 添加到插件列表中。
73
+
74
+ ### 快速开始 (Quick Start)
75
+
76
+ 通过 CLI 交互,体验基于核心组件协作的全流程诊断:
77
+
78
+ 1. **启动诊断 (Commander)**
79
+ 初始化故障上下文,自动触发信息收集:
80
+ ```bash
81
+ witty-diagnosis start --incident-id <ID> --desc "API响应慢"
82
+ ```
83
+ *系统将调度 `Investigator` 并行采集数据,并由 `Analyst` 产出根因分析报告。*
84
+
85
+ 2. **审批修复方案 (Guardian)**
86
+ 当 `Strategist` 生成修复方案后,系统会暂停并等待确认:
87
+ ```bash
88
+ witty-diagnosis approve --plan-id <PLAN_ID>
89
+ ```
90
+ *`Guardian` 组件将拦截所有高危操作,确保人工确认后才由 `Operator` 执行修复。*
91
+
92
+ 3. **验证结果 (Auditor)**
93
+ 修复完成后,自动运行回归测试:
94
+ ```bash
95
+ witty-diagnosis verify --incident-id <ID>
96
+ ```
97
+ *由 `Auditor` 确认核心指标恢复基线。*
98
+
99
+ ## 功能扩展 (Extending Functionality)
100
+ 参考前文“功能扩展机制”,编写自定义插件配置。
@@ -0,0 +1,13 @@
1
+ // bin/platform.js
2
+ // Shared platform detection module
3
+
4
+ export function getPlatformPackage({ platform, arch }) {
5
+ // Map platform names: win32 -> windows (for package name)
6
+ const os = platform === "win32" ? "windows" : platform;
7
+ return `witty-diagnosis-agent-${os}-${arch}`;
8
+ }
9
+
10
+ export function getBinaryPath(pkg, platform) {
11
+ const ext = platform === "win32" ? ".exe" : "";
12
+ return `${pkg}/bin/witty-diagnosis-agent${ext}`;
13
+ }
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env node
2
+ // bin/witty-diagnosis-agent.js
3
+ // Wrapper script that detects platform and spawns the correct binary
4
+
5
+ import { spawnSync } from "node:child_process";
6
+ import { existsSync } from "node:fs";
7
+ import { fileURLToPath } from "node:url";
8
+ import { dirname, resolve, join } from "node:path";
9
+
10
+ const __dirname = dirname(fileURLToPath(import.meta.url));
11
+
12
+ function main() {
13
+ // Check if we are running from source (development mode)
14
+ const distEntry = resolve(__dirname, "../dist/cli/index.js");
15
+ const srcEntry = resolve(__dirname, "../src/cli/index.ts");
16
+
17
+ // If bun is available, we prefer running the source or dist with bun
18
+ const hasBun = spawnSync("bun", ["--version"]).status === 0;
19
+
20
+ if (hasBun && (existsSync(srcEntry) || existsSync(distEntry))) {
21
+ // Development mode: use Bun to run source or dist
22
+ const entry = existsSync(srcEntry) ? srcEntry : distEntry;
23
+ // console.log(`⚡ Running in development mode with Bun: ${entry}`);
24
+
25
+ const result = spawnSync("bun", [entry, ...process.argv.slice(2)], {
26
+ stdio: "inherit",
27
+ });
28
+
29
+ process.exit(result.status ?? 0);
30
+ return;
31
+ }
32
+
33
+ // Production mode: try to find the binary
34
+ // In a real scenario, we would look into node_modules for platform-specific packages
35
+ // For this local setup, we check the locally built packages folder
36
+ const { platform, arch } = process;
37
+ const os = platform === "win32" ? "windows" : platform;
38
+
39
+ // Construct path to locally built binary
40
+ const binaryPath = resolve(__dirname, `../packages/${os}-${arch}/bin/witty-diagnosis-agent${platform === "win32" ? ".exe" : ""}`);
41
+
42
+ if (existsSync(binaryPath)) {
43
+ // console.log(`⚡ Running binary: ${binaryPath}`);
44
+ const result = spawnSync(binaryPath, process.argv.slice(2), {
45
+ stdio: "inherit",
46
+ });
47
+ process.exit(result.status ?? 0);
48
+ return;
49
+ }
50
+
51
+ // Fallback to node if dist exists
52
+ if (existsSync(distEntry)) {
53
+ // console.log(`⚡ Fallback to Node.js: ${distEntry}`);
54
+ const result = spawnSync("node", [distEntry, ...process.argv.slice(2)], {
55
+ stdio: "inherit",
56
+ });
57
+ process.exit(result.status ?? 0);
58
+ return;
59
+ }
60
+
61
+ console.error("❌ Could not find a way to run witty-diagnosis-agent.");
62
+ console.error(" Please ensure you have built the project with 'bun run build'.");
63
+ process.exit(1);
64
+ }
65
+
66
+ main();