Compare commits

...

43 Commits

Author SHA1 Message Date
公明 b9e5527131 Update config.yaml 2026-07-03 20:58:41 +08:00
公明 3d5e2bc4c7 Add files via upload 2026-07-03 20:31:49 +08:00
公明 d58c4642f7 Add files via upload 2026-07-03 20:30:47 +08:00
公明 9df6de088b Add files via upload 2026-07-03 20:29:09 +08:00
公明 aae71a0c3e Add files via upload 2026-07-03 20:27:51 +08:00
公明 059a33029e Add files via upload 2026-07-03 20:26:21 +08:00
公明 15daad97d4 Add files via upload 2026-07-03 20:25:31 +08:00
公明 f02c0d175b Add files via upload 2026-07-03 20:23:46 +08:00
公明 a8da115d28 Add files via upload 2026-07-03 19:41:53 +08:00
公明 e4a01089e7 Add files via upload 2026-07-03 19:41:05 +08:00
公明 bbf8c416fc Add files via upload 2026-07-03 19:39:17 +08:00
公明 d41decd707 Add files via upload 2026-07-03 19:38:23 +08:00
公明 93a600d60e Add files via upload 2026-07-03 19:36:40 +08:00
公明 c86825d365 Remove Stargazers over time section
Removed the 'Stargazers over time' section from the README.
2026-07-03 19:35:46 +08:00
公明 4af5e2691e Update README.md 2026-07-03 19:35:28 +08:00
公明 85400cd3f8 Add files via upload 2026-07-03 19:34:42 +08:00
公明 a66b8fc821 Add files via upload 2026-07-03 19:33:10 +08:00
公明 58be62fa24 Add files via upload 2026-07-03 17:55:08 +08:00
公明 a3739210e4 Add files via upload 2026-07-03 17:10:03 +08:00
公明 e936c63754 Add files via upload 2026-07-03 17:08:41 +08:00
公明 1f46d4a930 Add files via upload 2026-07-03 17:06:18 +08:00
公明 3a995183a6 Add files via upload 2026-07-03 17:03:37 +08:00
公明 3ed7499a0b Add files via upload 2026-07-03 17:01:43 +08:00
公明 f26354d483 Add files via upload 2026-07-03 16:59:39 +08:00
公明 ebd872b373 Add files via upload 2026-07-03 16:57:09 +08:00
公明 07439bce6e Add files via upload 2026-07-03 16:54:18 +08:00
公明 625ac4358f Update config.yaml 2026-07-03 14:29:16 +08:00
公明 eb6b9d6f45 Add files via upload 2026-07-03 14:28:37 +08:00
公明 ad97544bbe Add files via upload 2026-07-03 14:20:06 +08:00
公明 12a1ebe9cd Add files via upload 2026-07-03 14:17:47 +08:00
公明 b97e726237 Add files via upload 2026-07-03 14:15:51 +08:00
公明 2eb923e5fa Add files via upload 2026-07-03 14:13:35 +08:00
公明 745a69f93b Add files via upload 2026-07-03 14:12:20 +08:00
公明 011a242acc Add files via upload 2026-07-03 14:10:14 +08:00
公明 6a52ef96f4 Add files via upload 2026-07-03 10:56:22 +08:00
公明 52f8c377b6 Add files via upload 2026-07-03 10:55:07 +08:00
公明 8d04b0c266 Add files via upload 2026-07-03 10:52:21 +08:00
公明 bcdff06702 Add files via upload 2026-07-03 10:49:53 +08:00
公明 3210bc727f Add files via upload 2026-07-03 10:48:38 +08:00
公明 5254ca52fb Add files via upload 2026-07-03 10:46:04 +08:00
公明 1ff2df68ac Add files via upload 2026-07-02 23:32:48 +08:00
公明 fe60497863 Add files via upload 2026-07-02 19:21:29 +08:00
公明 7acd21bc98 Add files via upload 2026-07-02 19:14:30 +08:00
52 changed files with 7799 additions and 320 deletions
+3 -7
View File
@@ -127,6 +127,7 @@ CyberStrikeAI is an **AI-native security testing platform** built in Go. It inte
- 🛡️ Vulnerability management with CRUD operations, severity tracking, status workflow, and statistics
- 📋 Batch task management: create task queues, add multiple tasks, and execute them sequentially
- 🎭 Role-based testing: predefined security testing roles (Penetration Testing, CTF, Web App Scanning, etc.) with custom prompts and tool restrictions
- 🔀 **Graph orchestration**: visual workflow editor (Start / Agent / Tool / Condition / HITL / Output) with `{{previous.output}}` and `{{outputs.variable_name}}` for inter-node data passing; bind a graph to a role for automatic execution on chat. See [Graph orchestration guide](docs/workflow-graph_en.md)
- 🧩 **Agent orchestration (CloudWeGo Eino)**: **single-agent** via **`/api/eino-agent/stream`** (Eino ADK `ChatModelAgent`); **multi-agent** via **`/api/multi-agent/stream`** with **`deep`** (coordinator + `task` sub-agents), **`plan_execute`**, or **`supervisor`** (`orchestration` in the request body). ADK **summarization** compresses long contexts; pre-compaction **transcripts** land at `data/conversation_artifacts/<conversation-id>/summarization/transcript.txt` (full user/assistant/tool turns; static system omitted). Markdown under `agents/`: `orchestrator.md`, `orchestrator-plan-execute.md`, `orchestrator-supervisor.md`, plus sub-agent `*.md` (see [Multi-agent doc](docs/MULTI_AGENT_EINO.md))
- 🖼️ **Vision analysis (`analyze_image`)**: separate VL model (e.g. `qwen-vl-max`) via MCP for local screenshots, captchas, and UI; image bytes stay out of agent history (text summaries only). Configure `vision` in `config.yaml`; see [docs/VISION.md](docs/VISION.md)
- 🎯 **Skills (refactored for Eino)**: packs under `skills_dir` follow **Agent Skills** layout (`SKILL.md` + optional files); **multi-agent** sessions use the official Eino ADK **`skill`** tool for **progressive disclosure** (load by name), with optional **host filesystem / shell** via `multi_agent.eino_skills`; optional **`eino_middleware`** adds patchtoolcalls, tool_search, **plantask** (`TaskCreate` / `TaskList` boards under `skills_dir/.eino/plantask/`), reduction, file **checkpoints** (`checkpoint_dir`), ChatModel **retries**, session **output key**, and Deep tuning—20+ sample domains (SQLi, XSS, API security, …) ship under `skills/`
@@ -255,6 +256,7 @@ Requirements / tips:
- **Conversation testing** Natural-language prompts trigger toolchains with streaming SSE output.
- **Single vs multi-agent** Chat UI switches between **Eino single-agent** (`/api/eino-agent/stream`) and **multi-agent** (`/api/multi-agent/stream` with `orchestration`: `deep` | `plan_execute` | `supervisor`). Multi mode requires `multi_agent.enabled: true`. MCP tools are bridged the same way for both paths.
- **Role-based testing** Select from predefined security testing roles (Penetration Testing, CTF, Web App Scanning, API Security Testing, etc.) to customize AI behavior and tool availability. Each role applies custom system prompts and can restrict available tools for focused testing scenarios.
- **Graph orchestration** Design flows on the **Graph Orchestration** page (drag nodes, connect edges, save); bind `workflow_id` on a role to run the graph on chat (Agent, MCP tools, condition branches). Use `{{outputs.variable_name}}` to pass data across non-adjacent nodes. See [Graph orchestration guide](docs/workflow-graph_en.md).
- **Tool monitor** Inspect running jobs, execution logs, and large-result attachments.
- **History & audit** Every conversation and tool invocation is stored in SQLite with replay.
- **Conversation groups** Organize conversations into groups, pin important groups, rename or delete groups via context menu.
@@ -472,11 +474,6 @@ A test SSE MCP server is available at `cmd/test-sse-mcp-server/` for validation
- **Web management** create, update, delete knowledge items through the web UI, with category-based organization; settings page exposes MultiQuery / rerank / prefetch options.
- **Retrieval logs** tracks all knowledge retrieval operations for audit and debugging.
**Quick Start (Using Pre-built Knowledge Base):**
1. **Download the knowledge database** Download the pre-built knowledge database file from [GitHub Releases](https://github.com/Ed1s0nZ/CyberStrikeAI/releases).
2. **Extract and place** Extract the downloaded knowledge database file (`knowledge.db`) and place it in the project's `data/` directory.
3. **Restart the service** Restart the CyberStrikeAI service, and the knowledge base will be ready to use immediately without rebuilding the index.
**Setting up the knowledge base:**
1. **Enable in config** set `knowledge.enabled: true` in `config.yaml`:
```yaml
@@ -635,6 +632,7 @@ enabled: true
## Related documentation
- [Multi-agent mode (Eino)](docs/MULTI_AGENT_EINO.md): **Deep**, **Plan-Execute**, **Supervisor**, `agents/*.md`, `eino_skills` / `eino_middleware`, APIs, and chat/stream behavior.
- [Graph orchestration guide](docs/workflow-graph_en.md): visual workflow design, node configuration, `previous` / `outputs` variable passing, and role binding.
- [Robot / Chatbot guide (DingTalk & Lark)](docs/robot_en.md): Full setup, commands, and troubleshooting for using CyberStrikeAI from DingTalk or Lark on your phone. **Follow this doc to avoid common pitfalls.**
## Project Layout
@@ -687,8 +685,6 @@ CyberStrikeAI has joined [404Starlink](https://github.com/knownsec/404StarLink)
</a>
</div>
## Stargazers over time
![Stargazers over time](https://starchart.cc/Ed1s0nZ/CyberStrikeAI.svg)
---
+3 -7
View File
@@ -126,6 +126,7 @@ CyberStrikeAI 是一款 **AI 原生安全测试平台**,基于 Go 构建,集
- 🛡️ 漏洞管理功能:完整的漏洞 CRUD 操作,支持严重程度分级、状态流转、按对话/严重程度/状态过滤,以及统计看板
- 📋 批量任务管理:创建任务队列,批量添加任务,依次顺序执行,支持任务编辑与状态跟踪
- 🎭 角色化测试:预设安全测试角色(渗透测试、CTF、Web 应用扫描等),支持自定义提示词和工具限制
- 🔀 **图编排**:可视化流程编排(开始 / Agent / 工具 / 条件 / 审批 / 输出),节点间用 `{{previous.output}}``{{outputs.变量名}}` 传参;绑定角色后对话自动按图执行。详见 [图编排使用说明](docs/workflow-graph.md)
- 🧩 **Agent 编排(CloudWeGo Eino****单代理** `POST /api/eino-agent/stream`Eino ADK);**多代理** `POST /api/multi-agent/stream``orchestration`**`deep`** / **`plan_execute`** / **`supervisor`**。ADK **Summarization** 在上下文过长时压缩历史;压缩前将可恢复 **转录** 写入 `data/conversation_artifacts/<会话ID>/summarization/transcript.txt`(保留完整 user/assistant/tool 轮次,省略静态 system)。`agents/` 下主代理与子代理 Markdown 见 [多代理说明](docs/MULTI_AGENT_EINO.md)
- 🖼️ **视觉分析(`analyze_image`**:独立 Vision 模型(如 `qwen-vl-max`),MCP 工具分析本地截图/验证码/UI;图片仅在单次 VL 调用中出现,对话上下文只保留文字摘要。配置见 `config.yaml``vision` 与 [视觉分析说明](docs/VISION.md)
- 🎯 **Skills(面向 Eino 重构)**:技能包放在 **`skills_dir`**,遵循 **Agent Skills** 目录规范(`SKILL.md` + 可选文件);**多代理** 下通过 Eino 官方 **`skill`** 工具 **渐进式披露**(按 name 加载)。**`multi_agent.eino_skills`** 控制是否启用、本机文件/Shell 工具、工具名覆盖;**`eino_middleware`** 可选 patch、tool_search、**plantask**`TaskCreate` / `TaskList` 任务板,落在 `skills_dir/.eino/plantask/`)、reduction、文件型 **checkpoint**`checkpoint_dir`)、ChatModel **重试**、会话 **输出键** 及 Deep 调参。20+ 领域示例仍可绑定角色
@@ -253,6 +254,7 @@ go build -o cyberstrike-ai cmd/server/main.go
- **对话测试**:自然语言触发多步工具编排,SSE 实时输出。
- **单代理 / 多代理**:聊天可选 **Eino 单代理**`/api/eino-agent/stream`)与 **多代理**`/api/multi-agent/stream` + `orchestration`)。多代理需 `multi_agent.enabled: true`。MCP 工具桥接一致。
- **角色化测试**:从预设的安全测试角色(渗透测试、CTF、Web 应用扫描、API 安全测试等)中选择,自定义 AI 行为和可用工具。每个角色可应用自定义系统提示词,并可限制可用工具列表,实现聚焦的测试场景。
- **图编排**:在 **图编排** 页拖拽节点、连线并保存流程;在角色中绑定 `workflow_id` 后,该角色对话将按图执行(Agent、MCP 工具、条件分支等)。跨节点传参优先用 `{{outputs.变量名}}`。详见 [图编排使用说明](docs/workflow-graph.md)。
- **工具监控**:查看任务队列、执行日志、大文件附件。
- **会话历史**:所有对话与工具调用保存在 SQLite,可随时重放。
- **对话分组**:将对话按项目或主题组织到不同分组,支持置顶、重命名、删除等操作,所有数据持久化存储。
@@ -470,11 +472,6 @@ CyberStrikeAI 支持通过三种传输模式连接外部 MCP 服务器:
- **Web 管理**:通过 Web 界面创建、更新、删除知识项,支持分类管理;设置页可配置 MultiQuery / 精排 / 预取候选数。
- **检索日志**:记录所有知识检索操作,便于审计与调试。
**快速开始(使用预构建知识库):**
1. **下载知识数据库**:从 [GitHub Releases](https://github.com/Ed1s0nZ/CyberStrikeAI/releases) 下载预构建的知识数据库文件。
2. **解压并放置**:将下载的知识数据库文件(`knowledge.db`)解压后放到项目的 `data/` 目录下。
3. **重启服务**:重启 CyberStrikeAI 服务,知识库即可直接使用,无需重新构建索引。
**知识库配置步骤:**
1. **启用功能**:在 `config.yaml` 中设置 `knowledge.enabled: true`
```yaml
@@ -633,6 +630,7 @@ enabled: true
## 相关文档
- [多代理模式(Eino](docs/MULTI_AGENT_EINO.md)**Deep**、**Plan-Execute**、**Supervisor**、`agents/*.md`、`eino_skills` / `eino_middleware`、接口与流式说明。
- [图编排使用说明](docs/workflow-graph.md):可视化流程搭建、节点配置、`previous` / `outputs` 变量传参与角色绑定。
- [机器人使用说明(钉钉 / 飞书)](docs/robot.md):在手机端通过钉钉、飞书与 CyberStrikeAI 对话的完整配置步骤、命令与排查说明,**建议按该文档操作以避免走弯路**。
## 项目结构
@@ -684,8 +682,6 @@ CyberStrikeAI 现已加入 [404星链计划](https://github.com/knownsec/404Star
</a>
</div>
## Stargazers over time
![Stargazers over time](https://starchart.cc/Ed1s0nZ/CyberStrikeAI.svg)
---
+3 -1
View File
@@ -10,7 +10,7 @@
# ============================================
# 前端显示的版本号(可选,不填则显示默认版本)
version: "v1.6.49"
version: "v1.6.50"
# 服务器配置
server:
host: 0.0.0.0 # 监听地址,0.0.0.0 表示监听所有网络接口
@@ -106,6 +106,8 @@ agent:
# approval → audit_agent_prompt
# review_edit → audit_agent_prompt_review_edit(可改参后放行)
hitl:
# 全局默认审批方:human=人工审批,audit_agent=审计 Agent;未选会话时切换会写入本项,重启后仍生效
default_reviewer: human
# 已决策审计日志保留天数(与 MCP 监控一致;省略默认 90;0 表示不自动清理)
retention_days: 90
# 按你环境里的真实工具名增删(与侧栏一致、小写不敏感);不需要全局免审批可改为 []
+403
View File
@@ -0,0 +1,403 @@
# CyberStrikeAI 图编排使用说明
[English](workflow-graph_en.md)
本文档说明 **图编排(Graph Orchestration** 的完整使用方式:如何在画布上搭建流程、配置各类型节点、在节点之间传递数据,以及如何将流程绑定到角色并自动运行。
---
## 一、在哪里使用图编排
1. 登录 CyberStrikeAI Web 端
2. 左侧导航进入 **图编排**
3. 在左侧列表选择已有流程,或新建流程
4. 在中央画布拖拽、连线、配置节点
5. 填写流程 **ID**、**名称**、**描述** 后点击 **保存**
保存后的流程可在 **角色管理** 中绑定到某个角色。绑定后,用户与该角色对话时会按流程图自动执行(`workflow_policy: auto`)。
---
## 二、画布基本操作
| 操作 | 说明 |
|------|------|
| 添加节点 | 点击画布上方节点类型按钮(开始、工具、Agent、条件、审批、输出、结束) |
| 连线 | 点击 **连线**,依次点击源节点和目标节点;再次点击 **连线** 退出连线模式 |
| 选中元素 | 单击节点或连线,右侧显示 **节点属性** |
| 删除选中 | 点击 **删除选中** 删除当前节点或连线 |
| 自动布局 | 点击 **自动布局** 整理节点位置 |
| 删除流程 | 点击 **删除** 删除整个流程定义 |
**建议:** 每个流程至少包含 **1 个开始节点****1 个输出节点**;开始节点不应有入边,输出节点不应有出边。
---
## 三、执行模型(先理解再配置)
图编排按 **有向图** 执行,引擎从 **开始** 节点出发,沿连线依次运行下游节点。
每次运行会维护一份内部状态,模板变量 `{{...}}` 从这里取值:
| 内部状态 | 模板前缀 | 含义 |
|----------|----------|------|
| `inputs` | `{{inputs.xxx}}` | 流程启动时的输入(用户消息、会话 ID 等) |
| `lastOutput` | `{{previous.xxx}}` | **上一个刚执行完** 的节点的输出 |
| `outputs` | `{{outputs.xxx}}` | 全局 **命名变量池**(由节点的「输出变量名」写入) |
| `nodeOutputs` | `{{节点ID.xxx}}` | 指定节点 ID 的完整输出对象 |
### 3.1 `previous` 是什么?
`{{previous.output}}` 表示 **紧邻的上一个执行节点**`output` 字段。
- 每执行完一个节点,引擎都会更新 `lastOutput`
- **不是**「画布上画线的上游」,而是 **实际执行顺序上的上一步**
示例:
```text
开始 → Agent A → Agent B
```
Agent B 的 `{{previous.output}}` = Agent A 的输出。
但若中间有条件节点:
```text
开始 → Agent A → 条件 → Agent B
```
Agent B 的 `{{previous.output}}` = **条件节点** 的输出(`true` / `false`),**不是** Agent A 的结果。
### 3.2 `outputs` 是什么?
`outputs` 是引擎在运行过程中维护的 **命名变量注册表**
当 Agent、工具、输出 等节点配置了 **输出变量名**(字段 `output_key`)后,节点执行成功会把结果写入:
```text
outputs["你填的变量名"] = 节点输出内容
```
之后 **任意下游节点** 都可以通过 `{{outputs.变量名}}` 引用,不要求两个节点直接相连。
示例:
- Agent A 的 **输出变量名**`agent_result1`
- Agent B 的 **输入来源**`{{outputs.agent_result1}}`
即使 A 和 B 之间隔着条件节点,B 仍能拿到 A 的输出。
### 3.3 什么时候用 `previous`,什么时候用 `outputs`
| 场景 | 推荐写法 |
|------|----------|
| 两个节点 **直连**,只取上一步结果 | `{{previous.output}}` |
| 中间有其他节点(条件、工具、审批等) | `{{outputs.变量名}}` |
| 需要引用 **更早** 的某个节点结果 | `{{outputs.变量名}}``{{节点ID.output}}` |
| 条件判断要基于某 Agent 的输出 | `{{outputs.变量名}} != ""` |
| 读取用户最初输入 | `{{inputs.message}}` |
**记忆口诀:**
- `previous` = 上一步(链式、紧邻)
- `outputs` = 按名字取(跨节点、可回溯)
---
## 四、模板语法
### 4.1 基本格式
```text
{{变量路径}}
```
支持字母、数字、下划线、点、连字符,例如:
```text
{{previous.output}}
{{outputs.agent_result1}}
{{inputs.message}}
{{inputs.conversationId}}
{{previous.matched}}
{{node-abc123.output}}
```
### 4.2 可用路径一览
| 路径 | 说明 |
|------|------|
| `{{inputs.message}}` | 用户消息(开始节点输入) |
| `{{inputs.conversationId}}` | 会话 ID |
| `{{inputs.projectId}}` | 项目 ID |
| `{{previous.output}}` | 上一节点主输出 |
| `{{previous.matched}}` | 上一条件节点的匹配结果(`true` / `false` |
| `{{outputs.变量名}}` | 某节点注册过的命名输出 |
| `{{节点ID.output}}` | 指定节点 ID 的 `output` 字段 |
### 4.3 条件表达式
条件节点和连线条件支持简单比较:
```text
{{outputs.agent_result1}} != ""
{{previous.output}} == "ok"
{{outputs.count}} == "100"
```
规则:
- 使用 `==``!=` 做字符串比较(两侧会自动去掉首尾空格和引号)
- 无比较符时,非空且不为 `false` / `0` / `null` 视为真
---
## 五、节点类型与配置
### 5.1 开始(start
流程入口,将用户输入注入 `inputs`
| 字段 | 说明 | 默认值 |
|------|------|--------|
| 输入变量 | 逗号分隔的输入键名 | `message, conversationId, projectId` |
开始节点输出包含:`output``message``conversationId``projectId`
### 5.2 Agentagent
调用大模型 Agent 处理任务,支持多种运行模式。
| 字段 | 说明 | 默认值 |
|------|------|--------|
| Agent 模式 | `eino_single` / `deep` / `plan_execute` / `supervisor` | `eino_single` |
| 输入来源 | 上游数据的模板表达式 | `{{previous.output}}` |
| 节点指令 | 本节点要完成的任务描述 | 空 |
| 输出变量名 | 写入 `outputs` 的键名 | `agent_result` |
**消息拼装规则:**
- 仅填 **节点指令**:直接把指令发给 Agent
- 仅填 **输入来源**:生成「请基于上游节点输出继续处理:…」
- 两者都填:合并为「上游输入 + 节点指令」
Agent 节点执行后:
- `previous.output` 更新为本节点响应文本
- 若配置了 **输出变量名**,同时写入 `outputs[输出变量名]`
### 5.3 工具(tool
调用已启用的 MCP 工具。
| 字段 | 说明 | 默认值 |
|------|------|--------|
| MCP 工具 | 工具名称(必填) | — |
| 参数模板 | JSON,支持 `{{...}}` 模板 | `{}` |
| 超时秒数 | 可选 | 空 |
示例参数模板:
```json
{"target": "{{inputs.message}}", "port": "443"}
```
若配置了 **输出变量名**,工具返回结果会写入 `outputs`
### 5.4 条件(condition
根据表达式计算分支,输出 `matched``true` / `false`)。
| 字段 | 说明 | 默认值 |
|------|------|--------|
| 条件表达式 | 支持 `{{...}}``==` / `!=` | `{{previous.output}} != ""` |
**分支规则:**
- 从条件节点连出的 **第一条线** 默认为 **「是」** 分支(`matched == true`
- **第二条线** 默认为 **「否」** 分支(`matched == false`
- 连线标签可写 `是` / `否`(或 `yes` / `no``true` / `false`)辅助识别
- 第三条及以后的出边需在 **连线条件** 中自定义表达式
连线条件示例(选中连线后在右侧配置):
```text
{{previous.matched}} == "true"
{{previous.matched}} == "false"
```
### 5.5 审批(hitl
人工确认检查点(当前为记录模式,自动标记 `approved: true` 并继续)。
| 字段 | 说明 | 默认值 |
|------|------|--------|
| 审批提示 | 支持模板 | `请审批该步骤是否继续执行` |
| 审批方 | `human` / `audit_agent` | `human` |
### 5.6 输出(output
将流程最终结果写入 `outputs`,供结束摘要和对话展示使用。
| 字段 | 说明 | 默认值 |
|------|------|--------|
| 输出变量名 | 必填,最终结果的键名 | `result` |
| 变量来源 | 模板表达式,决定写入的值 | `{{previous.output}}` |
**注意:** 输出节点是流程的「出口」,不应再有出边。
### 5.7 结束(end
可选节点,用于生成结束摘要模板(角色绑定流程中较少单独使用)。
| 字段 | 说明 | 默认值 |
|------|------|--------|
| 结束摘要模板 | 支持 `{{outputs.xxx}}` | `{{outputs.result}}` |
---
## 六、连线配置
选中 **连线** 后,右侧可配置 **连线条件**
| 场景 | 示例 |
|------|------|
| 普通节点后的过滤 | `{{previous.output}} == "ok"` |
| 条件节点「是」分支 | `{{previous.matched}} == "true"` |
| 条件节点「否」分支 | `{{previous.matched}} == "false"` |
若不填连线条件:
- 非条件节点:连线始终放行
- 条件节点:按出边顺序自动分配是/否分支
---
## 七、完整示例:跨条件节点传递 Agent 输出
### 7.1 流程结构
```text
开始 → Agent(生成初始值)→ 条件 → Agent(加工)→ 输出
↘ 否 → 输出
```
### 7.2 节点配置
**Agent 1(第一个 Agent**
| 字段 | 值 |
|------|-----|
| 节点指令 | 只输出 `123333333` |
| 输出变量名 | `agent_result1` |
**条件**
| 字段 | 值 |
|------|-----|
| 条件表达式 | `{{outputs.agent_result1}} != ""` |
**Agent 2(第二个 Agent**
| 字段 | 值 |
|------|-----|
| 输入来源 | `{{outputs.agent_result1}}` |
| 节点指令 | 在输入基础上加 100,然后输出 |
| 输出变量名 | `agent_result` |
**输出**
| 字段 | 值 |
|------|-----|
| 输出变量名 | `result` |
| 变量来源 | `{{outputs.agent_result}}` |
### 7.3 常见错误
| 错误配置 | 原因 |
|----------|------|
| Agent 2 输入来源写 `{{previous.output}}` | `previous` 指向条件节点,得到的是 `true`/`false`,不是 Agent 1 的文本 |
| 未给 Agent 1 填输出变量名 | `outputs.agent_result1` 不存在,下游取到空值 |
| 条件表达式写 `{{previous.output}}` | 判断的是开始节点或上一节点的输出,而非 Agent 1 的命名变量 |
---
## 八、绑定角色并运行
### 8.1 在角色管理中绑定
1. 进入 **角色管理**,编辑或新建角色
2. 选择 **工作流 / 图编排** 绑定的流程 ID
3. 策略设为 `auto`(默认:有 `workflow_id` 时自动执行)
4. 保存角色
也可在角色 YAML 中直接配置:
```yaml
name: 工作流测试
workflow_id: "1233"
workflow_version: latest
workflow_policy: auto
```
### 8.2 运行效果
用户选择该角色并发送消息后:
1. 引擎加载对应 `graph_json` 并按图执行
2. 对话页可看到 `workflow_start``workflow_node_start`、Agent 推理等进度事件
3. 流程结束后返回摘要,列出 `outputs` 中所有命名输出
若未配置输出节点或条件未命中,`outputs` 可能为空,摘要会提示检查输出节点与分支。
---
## 九、保存前校验规则
保存时系统会自动检查:
| 规则 | 说明 |
|------|------|
| 必须有开始节点 | 至少 1 个 `start` |
| 必须有输出节点 | 至少 1 个 `output`,且填写输出变量名 |
| 连线合法 | 源/目标节点存在,不能自环 |
| 开始节点无入边 | 开始节点不能被指向 |
| 输出节点无出边 | 输出节点后不应再连线 |
| 工具节点 | 必须选择 MCP 工具 |
| 条件节点 | 必须填写表达式;建议 1~2 条出边(是/否) |
---
## 十、排错指南
| 现象 | 可能原因 | 处理建议 |
|------|----------|----------|
| 下游拿到空值 | 上游未配置输出变量名 | 给上游 Agent/工具填 **输出变量名**,下游用 `{{outputs.xxx}}` |
| 下游拿到 `true`/`false` | 误用 `{{previous.output}}`,上一步是条件节点 | 改用 `{{outputs.xxx}}` |
| 条件总走「否」 | 表达式与真实输出格式不一致 | 检查 Agent 输出是否带引号、换行;用 `!= ""` 先验证 |
| 流程无最终输出 | 未命中输出节点所在分支 | 检查条件分支连线;确保至少一条路径到达 **输出** 节点 |
| 角色对话未跑流程 | 角色未绑定或未启用 | 确认 `workflow_id``workflow_policy: auto`、流程 `enabled: true` |
| 工具节点失败 | 参数 JSON 不合法或工具未启用 | 检查参数模板;在 MCP 中启用对应工具 |
---
## 十一、最佳实践
1. **命名规范**:为每个需要被引用的节点设置有意义的输出变量名,如 `scan_result``parsed_targets`,避免都叫 `agent_result`
2. **跨节点传参优先用 `outputs`**:只要中间可能插入条件、工具、审批节点,就应用命名变量。
3. **`previous` 仅用于直连**:A → B 且无中间节点时,`{{previous.output}}` 最简洁。
4. **条件判断引用源数据**:判断 Agent 输出时用 `{{outputs.xxx}}`,不要用 `{{previous.output}}`(除非条件紧跟在目标 Agent 之后)。
5. **每条路径都要有出口**:确保「是」「否」分支最终都能到达 **输出** 节点(或你期望的终点)。
6. **保存前跑一遍**:用简单指令(如固定字符串输出)验证数据传递,再替换为真实业务逻辑。
---
## 十二、相关代码位置(开发者参考)
| 模块 | 路径 |
|------|------|
| 执行引擎 | `internal/workflow/runner.go` |
| 画布前端 | `web/static/js/workflows.js` |
| 流程 API | `internal/handler/workflow.go` |
| 角色绑定 | `internal/config/config.go``workflow_id` 字段) |
+403
View File
@@ -0,0 +1,403 @@
# CyberStrikeAI Graph Orchestration Guide
[中文](workflow-graph.md)
This document explains how to use **Graph Orchestration**: building workflows on the canvas, configuring node types, passing data between nodes, and binding a graph to a role for automatic execution.
---
## 1. Where to find Graph Orchestration
1. Log in to the CyberStrikeAI web UI.
2. Open **Graph Orchestration** in the left sidebar.
3. Select an existing workflow from the list, or create a new one.
4. Drag nodes, draw edges, and configure properties on the canvas.
5. Fill in **ID**, **Name**, and **Description**, then click **Save**.
Saved workflows can be bound to a role under **Role Management**. When `workflow_policy` is `auto`, chatting with that role runs the bound graph automatically.
---
## 2. Canvas basics
| Action | Description |
|--------|-------------|
| Add node | Click a node type button above the canvas (Start, Tool, Agent, Condition, HITL, Output, End) |
| Connect | Click **Connect**, then click source and target nodes; click **Connect** again to exit connect mode |
| Select | Click a node or edge; properties appear in the right panel |
| Delete selected | Remove the current node or edge |
| Auto layout | Rearrange node positions |
| Delete workflow | Remove the entire workflow definition |
**Requirements:** Every workflow needs at least **one Start node** and **one Output node**. Start nodes must not have incoming edges; Output nodes must not have outgoing edges.
---
## 3. Execution model (read this before configuring)
The engine executes the workflow as a **directed graph**, starting from the **Start** node and following edges to downstream nodes.
During a run, the engine keeps internal state. Template expressions `{{...}}` read from that state:
| Internal state | Template prefix | Meaning |
|----------------|-----------------|---------|
| `inputs` | `{{inputs.xxx}}` | Workflow inputs at start (user message, conversation ID, etc.) |
| `lastOutput` | `{{previous.xxx}}` | Output of the **most recently executed** node |
| `outputs` | `{{outputs.xxx}}` | Global **named variable pool** (written by nodes with an output key) |
| `nodeOutputs` | `{{nodeId.xxx}}` | Full output object of a specific node ID |
### 3.1 What is `previous`?
`{{previous.output}}` is the `output` field of the **immediately preceding executed node**.
- After every node finishes, the engine updates `lastOutput`.
- It is **not** “the node drawn upstream on the canvas”; it is **the previous step in actual execution order**.
Example:
```text
Start → Agent A → Agent B
```
For Agent B, `{{previous.output}}` = Agent As output.
With a condition in between:
```text
Start → Agent A → Condition → Agent B
```
For Agent B, `{{previous.output}}` = the **condition node** output (`true` / `false`), **not** Agent As result.
### 3.2 What is `outputs`?
`outputs` is a **named variable registry** maintained by the engine during execution.
When an Agent, Tool, or Output node sets an **Output variable name** (`output_key`), the result is stored as:
```text
outputs["your_variable_name"] = node_output
```
Any downstream node can then reference it via `{{outputs.variable_name}}`, even if other nodes sit in between.
Example:
- Agent A **Output variable name**: `agent_result1`
- Agent B **Input source**: `{{outputs.agent_result1}}`
Agent B still receives Agent As output even when a condition node lies between them.
### 3.3 When to use `previous` vs `outputs`
| Scenario | Recommended |
|----------|-------------|
| Two nodes are **directly connected**; you only need the last step | `{{previous.output}}` |
| Other nodes sit in between (condition, tool, HITL, etc.) | `{{outputs.variable_name}}` |
| Reference output from an **earlier** node | `{{outputs.variable_name}}` or `{{nodeId.output}}` |
| Condition should test an Agents output | `{{outputs.variable_name}} != ""` |
| Read the original user input | `{{inputs.message}}` |
**Rule of thumb:**
- `previous` = last step (chained, adjacent)
- `outputs` = by name (cross-node, look back)
---
## 4. Template syntax
### 4.1 Basic format
```text
{{path.to.value}}
```
Allowed characters in paths: letters, digits, underscore, dot, hyphen. Examples:
```text
{{previous.output}}
{{outputs.agent_result1}}
{{inputs.message}}
{{inputs.conversationId}}
{{previous.matched}}
{{node-abc123.output}}
```
### 4.2 Available paths
| Path | Description |
|------|-------------|
| `{{inputs.message}}` | User message (Start node input) |
| `{{inputs.conversationId}}` | Conversation ID |
| `{{inputs.projectId}}` | Project ID |
| `{{previous.output}}` | Primary output of the previous node |
| `{{previous.matched}}` | Match result of the previous condition node (`true` / `false`) |
| `{{outputs.variable_name}}` | Named output registered by a node |
| `{{nodeId.output}}` | `output` field of the node with that ID |
### 4.3 Condition expressions
Condition nodes and edge conditions support simple comparisons:
```text
{{outputs.agent_result1}} != ""
{{previous.output}} == "ok"
{{outputs.count}} == "100"
```
Rules:
- Use `==` or `!=` for string comparison (leading/trailing spaces and quotes are trimmed)
- Without a comparator, non-empty values that are not `false`, `0`, or `null` are treated as true
---
## 5. Node types and configuration
### 5.1 Start
Workflow entry point; injects user input into `inputs`.
| Field | Description | Default |
|-------|-------------|---------|
| Input keys | Comma-separated input key names | `message, conversationId, projectId` |
Start node output includes: `output`, `message`, `conversationId`, `projectId`.
### 5.2 Agent
Runs an LLM Agent task. Supports multiple modes.
| Field | Description | Default |
|-------|-------------|---------|
| Agent mode | `eino_single` / `deep` / `plan_execute` / `supervisor` | `eino_single` |
| Input source | Template for upstream data | `{{previous.output}}` |
| Node instruction | Task description for this node | empty |
| Output variable name | Key written into `outputs` | `agent_result` |
**Message assembly:**
- Instruction only → send instruction to the Agent
- Input source only → “Continue based on upstream output: …”
- Both → combined “upstream input + node instruction”
After execution:
- `previous.output` becomes this nodes response text
- If **Output variable name** is set, the value is also stored in `outputs[variable_name]`
### 5.3 Tool
Calls an enabled MCP tool.
| Field | Description | Default |
|-------|-------------|---------|
| MCP tool | Tool name (required) | — |
| Argument template | JSON with `{{...}}` templates | `{}` |
| Timeout (seconds) | Optional | empty |
Example argument template:
```json
{"target": "{{inputs.message}}", "port": "443"}
```
If an output variable name is configured, the tool result is written to `outputs`.
### 5.4 Condition
Evaluates an expression and outputs `matched` (`true` / `false`).
| Field | Description | Default |
|-------|-------------|---------|
| Expression | Supports `{{...}}` and `==` / `!=` | `{{previous.output}} != ""` |
**Branching rules:**
- The **first outgoing edge** defaults to the **“yes”** branch (`matched == true`)
- The **second outgoing edge** defaults to the **“no”** branch (`matched == false`)
- Edge labels such as `是` / `否` (or `yes` / `no`, `true` / `false`) help identify branches
- A third or later edge needs a custom **edge condition**
Edge condition examples (select an edge, configure in the right panel):
```text
{{previous.matched}} == "true"
{{previous.matched}} == "false"
```
### 5.5 HITL (human-in-the-loop)
Human approval checkpoint (currently record-only; marks `approved: true` and continues).
| Field | Description | Default |
|-------|-------------|---------|
| Prompt | Supports templates | `Please approve before continuing` |
| Reviewer | `human` / `audit_agent` | `human` |
### 5.6 Output
Writes the final workflow result into `outputs` for summary and chat display.
| Field | Description | Default |
|-------|-------------|---------|
| Output variable name | Required key for the final result | `result` |
| Variable source | Template deciding what to write | `{{previous.output}}` |
**Note:** Output nodes are workflow exits and must not have outgoing edges.
### 5.7 End
Optional node for an end summary template (less common in role-bound flows).
| Field | Description | Default |
|-------|-------------|---------|
| Result template | Supports `{{outputs.xxx}}` | `{{outputs.result}}` |
---
## 6. Edge configuration
Select an **edge** to configure its **condition** in the right panel.
| Scenario | Example |
|----------|---------|
| Filter after a normal node | `{{previous.output}} == "ok"` |
| “Yes” branch from a condition | `{{previous.matched}} == "true"` |
| “No” branch from a condition | `{{previous.matched}} == "false"` |
If no edge condition is set:
- Non-condition nodes: edge is always allowed
- Condition nodes: yes/no branches are assigned by edge order automatically
---
## 7. Full example: passing Agent output across a condition
### 7.1 Graph structure
```text
Start → Agent (initial value) → Condition → Agent (transform) → Output
↘ no → Output
```
### 7.2 Node configuration
**Agent 1**
| Field | Value |
|-------|-------|
| Node instruction | Output only `123333333` |
| Output variable name | `agent_result1` |
**Condition**
| Field | Value |
|-------|-------|
| Expression | `{{outputs.agent_result1}} != ""` |
**Agent 2**
| Field | Value |
|-------|-------|
| Input source | `{{outputs.agent_result1}}` |
| Node instruction | Add 100 to the input, then output |
| Output variable name | `agent_result` |
**Output**
| Field | Value |
|-------|-------|
| Output variable name | `result` |
| Variable source | `{{outputs.agent_result}}` |
### 7.3 Common mistakes
| Wrong config | Why it fails |
|--------------|--------------|
| Agent 2 input source = `{{previous.output}}` | `previous` points to the condition node → `true`/`false`, not Agent 1s text |
| Agent 1 has no output variable name | `outputs.agent_result1` does not exist → empty downstream |
| Condition uses `{{previous.output}}` | Tests the wrong upstream value instead of Agent 1s named output |
---
## 8. Bind to a role and run
### 8.1 Bind in Role Management
1. Open **Role Management**, edit or create a role.
2. Select the workflow / graph ID to bind.
3. Set policy to `auto` (default when `workflow_id` is set).
4. Save the role.
You can also configure this in role YAML:
```yaml
name: workflow-test
workflow_id: "1233"
workflow_version: latest
workflow_policy: auto
```
### 8.2 Runtime behavior
When a user chats with that role:
1. The engine loads `graph_json` and executes the graph.
2. The chat UI shows progress events (`workflow_start`, `workflow_node_start`, Agent reasoning, etc.).
3. When finished, a summary lists all named entries in `outputs`.
If no Output node is reached or no branch matches, `outputs` may be empty and the summary will suggest checking the Output node and branches.
---
## 9. Validation before save
On save, the system checks:
| Rule | Description |
|------|-------------|
| Start node required | At least one `start` node |
| Output node required | At least one `output` node with an output variable name |
| Valid edges | Source and target exist; no self-loops |
| Start has no incoming edges | Start must not be targeted |
| Output has no outgoing edges | Nothing after Output |
| Tool nodes | MCP tool must be selected |
| Condition nodes | Expression required; ideally 12 outgoing edges (yes/no) |
---
## 10. Troubleshooting
| Symptom | Likely cause | Fix |
|---------|--------------|-----|
| Downstream gets empty value | Upstream has no output variable name | Set **Output variable name** on upstream; use `{{outputs.xxx}}` downstream |
| Downstream gets `true`/`false` | Used `{{previous.output}}` while previous node is a condition | Use `{{outputs.xxx}}` instead |
| Condition always takes “no” | Expression does not match actual output format | Check Agent output for quotes/newlines; try `!= ""` first |
| No final output | Output node branch not reached | Verify condition wiring; ensure every path reaches an **Output** node |
| Role chat does not run workflow | Role not bound or disabled | Check `workflow_id`, `workflow_policy: auto`, workflow `enabled: true` |
| Tool node fails | Invalid JSON in arguments or tool disabled | Fix argument template; enable the tool in MCP settings |
---
## 11. Best practices
1. **Meaningful names**: Use descriptive output variable names (`scan_result`, `parsed_targets`) instead of reusing `agent_result` everywhere.
2. **Prefer `outputs` for cross-node data**: If a condition, tool, or HITL node might sit in between, use named variables.
3. **Use `previous` only for direct links**: `A → B` with nothing in between is the ideal case for `{{previous.output}}`.
4. **Conditions should reference source data**: When testing Agent output, use `{{outputs.xxx}}` unless the condition immediately follows that Agent.
5. **Every path needs an exit**: Ensure both yes and no branches eventually reach an **Output** node (or your intended end).
6. **Validate with a simple run**: Use fixed-string outputs to verify data flow before swapping in real business logic.
---
## 12. Code references (for developers)
| Module | Path |
|--------|------|
| Execution engine | `internal/workflow/runner.go` |
| Canvas UI | `web/static/js/workflows.js` |
| Workflow API | `internal/handler/workflow.go` |
| Role binding | `internal/config/config.go` (`workflow_id` field) |
+18
View File
@@ -356,6 +356,9 @@ func New(cfg *config.Config, log *logger.Logger, configPath string) (*App, error
attackChainHandler := handler.NewAttackChainHandler(db, &cfg.OpenAI, log.Logger)
vulnerabilityHandler := handler.NewVulnerabilityHandler(db, log.Logger)
projectHandler := handler.NewProjectHandler(db, log.Logger)
workflowHandler := handler.NewWorkflowHandler(db, log.Logger)
workflowHandler.SetAudit(auditSvc)
workflowHandler.SetRuntime(agent, cfg)
vulnerabilityHandler.SetAudit(auditSvc)
webshellHandler := handler.NewWebShellHandler(log.Logger, db)
webshellHandler.SetAudit(auditSvc)
@@ -367,6 +370,7 @@ func New(cfg *config.Config, log *logger.Logger, configPath string) (*App, error
configHandler.SetAudit(auditSvc)
agentHandler.SetHitlToolWhitelistSaver(configHandler)
agentHandler.SetHitlAuditStrategySaver(configHandler)
agentHandler.SetHitlDefaultReviewerSaver(configHandler)
externalMCPHandler := handler.NewExternalMCPHandler(externalMCPMgr, cfg, configPath, log.Logger)
externalMCPHandler.SetAudit(auditSvc)
roleHandler := handler.NewRoleHandler(cfg, configPath, log.Logger)
@@ -517,6 +521,7 @@ func New(cfg *config.Config, log *logger.Logger, configPath string) (*App, error
app, // 传递 App 实例以便动态获取 knowledgeHandler
vulnerabilityHandler,
projectHandler,
workflowHandler,
webshellHandler,
chatUploadsHandler,
roleHandler,
@@ -763,6 +768,7 @@ func setupRoutes(
app *App, // 传递 App 实例以便动态获取 knowledgeHandler
vulnerabilityHandler *handler.VulnerabilityHandler,
projectHandler *handler.ProjectHandler,
workflowHandler *handler.WorkflowHandler,
webshellHandler *handler.WebShellHandler,
chatUploadsHandler *handler.ChatUploadsHandler,
roleHandler *handler.RoleHandler,
@@ -826,6 +832,8 @@ func setupRoutes(
protected.GET("/hitl/tool-whitelist", agentHandler.GetHITLGlobalToolWhitelist)
protected.PUT("/hitl/tool-whitelist", agentHandler.SetHITLGlobalToolWhitelist)
protected.POST("/hitl/tool-whitelist", agentHandler.MergeHITLGlobalToolWhitelist)
protected.GET("/hitl/default-reviewer", agentHandler.GetHITLDefaultReviewer)
protected.PUT("/hitl/default-reviewer", agentHandler.UpdateHITLDefaultReviewer)
protected.GET("/hitl/audit-strategy", agentHandler.GetHITLAuditStrategy)
protected.PUT("/hitl/audit-strategy", agentHandler.UpdateHITLAuditStrategy)
// Agent Loop 取消与任务列表
@@ -1189,6 +1197,16 @@ func setupRoutes(
protected.PUT("/roles/:name", roleHandler.UpdateRole)
protected.DELETE("/roles/:name", roleHandler.DeleteRole)
// 图编排 / 工作流定义(图结构固定,业务字段保存在 graph_json 中)
protected.GET("/workflows/runs/pending", workflowHandler.ListPendingRuns)
protected.GET("/workflows/runs/:runId", workflowHandler.GetRun)
protected.POST("/workflows/runs/:runId/resume", workflowHandler.ResumeRun)
protected.GET("/workflows", workflowHandler.List)
protected.GET("/workflows/:id", workflowHandler.Get)
protected.POST("/workflows", workflowHandler.Create)
protected.PUT("/workflows/:id", workflowHandler.Update)
protected.DELETE("/workflows/:id", workflowHandler.Delete)
// Skills管理(具体路径需注册在 /skills/:name 之前)
protected.GET("/skills", skillsHandler.GetSkills)
protected.GET("/skills/stats", skillsHandler.GetSkillStats)
+72 -17
View File
@@ -120,9 +120,19 @@ func formatVulnerabilityDetail(v *database.Vulnerability) string {
b.WriteString(v.Description)
b.WriteString("\n")
}
if v.Proof != "" {
b.WriteString("\n--- 证明(POC ---\n")
b.WriteString(v.Proof)
if v.Preconditions != "" {
b.WriteString("\n--- 前置条件 ---\n")
b.WriteString(v.Preconditions)
b.WriteString("\n")
}
if v.ReproSteps != "" {
b.WriteString("\n--- 复现步骤 ---\n")
b.WriteString(v.ReproSteps)
b.WriteString("\n")
}
if v.Evidence != "" {
b.WriteString("\n--- 证据 / POC ---\n")
b.WriteString(v.Evidence)
b.WriteString("\n")
}
if v.Impact != "" {
@@ -135,9 +145,36 @@ func formatVulnerabilityDetail(v *database.Vulnerability) string {
b.WriteString(v.Recommendation)
b.WriteString("\n")
}
if v.RetestNotes != "" {
b.WriteString("\n--- 复测方式 ---\n")
b.WriteString(v.RetestNotes)
b.WriteString("\n")
}
return b.String()
}
func missingVulnerabilityReproFields(args map[string]interface{}) []string {
required := []struct {
key string
label string
}{
{"target", "target(受影响的 URL/IP/服务/接口)"},
{"vulnerability_type", "vulnerability_type(漏洞类型)"},
{"description", "description(漏洞摘要与触发点)"},
{"reproduction_steps", "reproduction_steps(可逐步执行的复现步骤)"},
{"evidence", "evidencePOC、原始请求/响应、命令输出或截图/日志证据)"},
{"impact", "impact(确认后的实际影响)"},
{"recommendation", "recommendation(修复建议)"},
}
missing := make([]string, 0)
for _, item := range required {
if strings.TrimSpace(strArg(args, item.key)) == "" {
missing = append(missing, item.label)
}
}
return missing
}
func truncateRunes(s string, max int) string {
r := []rune(s)
if len(r) <= max {
@@ -163,18 +200,18 @@ func registerVulnerabilityTools(mcpServer *mcp.Server, db *database.DB, logger *
func registerRecordVulnerabilityTool(mcpServer *mcp.Server, db *database.DB, logger *zap.Logger) {
tool := mcp.Tool{
Name: builtin.ToolRecordVulnerability,
Description: "记录发现的漏洞详情到漏洞管理系统。边渗透边记录:每验证出一条可复现漏洞(含 POC/影响)后立即调用,勿等会话结束。包括标题、描述、严重程度、类型、目标、证明、影响和建议等。记录前可先 list_vulnerabilities 避免重复。",
ShortDescription: "记录现的漏洞详情到漏洞管理系统",
Description: "记录发现的漏洞详情到漏洞管理系统。必须按“仅看本记录即可复现”的标准填写:目标、触发点、前置条件、复现步骤、证据/POC、实际影响、修复建议和复测方式。边渗透边记录:每验证出一条可复现漏洞后立即调用,勿等会话结束。记录前可先 list_vulnerabilities 避免重复。",
ShortDescription: "记录可复现的漏洞详情到漏洞管理系统",
InputSchema: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"title": map[string]interface{}{
"type": "string",
"description": "漏洞标题(必需)",
"description": "漏洞标题(必需)。建议格式:<资产/接口> 存在 <漏洞类型>,例如“/api/login 存在 SQL 注入”。",
},
"description": map[string]interface{}{
"type": "string",
"description": "漏洞详细描述",
"description": "漏洞摘要与触发点(必需):说明哪个功能/参数/入口存在问题、为什么可被利用。不要只写结论。",
},
"severity": map[string]interface{}{
"type": "string",
@@ -183,26 +220,38 @@ func registerRecordVulnerabilityTool(mcpServer *mcp.Server, db *database.DB, log
},
"vulnerability_type": map[string]interface{}{
"type": "string",
"description": "漏洞类型,如:SQL注入、XSS、CSRF、命令注入等",
"description": "漏洞类型,如:SQL注入、XSS、CSRF、命令注入等(必需)",
},
"target": map[string]interface{}{
"type": "string",
"description": "受影响的目标(URL、IP地址、服务等)",
"description": "受影响的目标(必需):尽量精确到 URL、IP:端口、服务名、接口路径和参数名。",
},
"proof": map[string]interface{}{
"preconditions": map[string]interface{}{
"type": "string",
"description": "漏洞证明(POC、截图、请求/响应等)",
"description": "前置条件:登录状态、权限、账号、Header/Cookie、特定数据、网络位置、环境/版本等;无前置条件写“无”。",
},
"reproduction_steps": map[string]interface{}{
"type": "string",
"description": "复现步骤(必需):按 1/2/3 编号,写清入口、参数、payload、执行命令、观察点。应让未参与对话的人照做即可复现。",
},
"evidence": map[string]interface{}{
"type": "string",
"description": "证据 / POC(必需):原始 HTTP 请求/响应、curl/工具命令、截图文字说明、日志、DNSLog/回连记录、数据库结果、文件路径、时间戳等。优先放最小可验证证据。",
},
"impact": map[string]interface{}{
"type": "string",
"description": "漏洞影响说明",
"description": "漏洞影响说明(必需):结合已验证事实说明可造成什么后果,避免泛泛而谈。",
},
"recommendation": map[string]interface{}{
"type": "string",
"description": "修复建议",
"description": "修复建议(必需):给出针对该触发点/参数/组件的具体修复和复测建议。",
},
"retest_notes": map[string]interface{}{
"type": "string",
"description": "复测方式:修复后如何验证漏洞已关闭,包括应返回的状态码、错误信息或访问控制结果。",
},
},
"required": []string{"title", "severity"},
"required": []string{"title", "description", "severity", "vulnerability_type", "target", "reproduction_steps", "evidence", "impact", "recommendation"},
},
}
@@ -231,6 +280,9 @@ func registerRecordVulnerabilityTool(mcpServer *mcp.Server, db *database.DB, log
if !validSeverities[severity] {
return textResult(fmt.Sprintf("错误: severity 必须是 critical、high、medium、low 或 info 之一,当前值: %s", severity), true), nil
}
if missing := missingVulnerabilityReproFields(args); len(missing) > 0 {
return textResult("错误: 漏洞记录缺少复现所需信息,请补充后再记录:\n- "+strings.Join(missing, "\n- ")+"\n\n最佳实践:漏洞管理中的单条记录应独立包含目标、前置条件、复现步骤、证据/POC、影响和修复/复测方式。", true), nil
}
projectID := ""
if pid, perr := db.GetConversationProjectID(conversationID); perr == nil {
@@ -246,9 +298,12 @@ func registerRecordVulnerabilityTool(mcpServer *mcp.Server, db *database.DB, log
Status: "open",
Type: strArg(args, "vulnerability_type"),
Target: strArg(args, "target"),
Proof: strArg(args, "proof"),
Preconditions: strArg(args, "preconditions"),
ReproSteps: strArg(args, "reproduction_steps"),
Evidence: strArg(args, "evidence"),
Impact: strArg(args, "impact"),
Recommendation: strArg(args, "recommendation"),
RetestNotes: strArg(args, "retest_notes"),
}
created, err := db.CreateVulnerability(vuln)
@@ -275,8 +330,8 @@ func registerRecordVulnerabilityTool(mcpServer *mcp.Server, db *database.DB, log
func registerListVulnerabilitiesTool(mcpServer *mcp.Server, db *database.DB, logger *zap.Logger) {
tool := mcp.Tool{
Name: builtin.ToolListVulnerabilities,
Description: "列出当前授权范围内的漏洞(摘要)。默认:对话已绑定项目时列出该项目下全部漏洞;未绑项目时仅列出当前会话漏洞。可用 scope=conversation 仅看本会话。记录新漏洞前建议先调用以避免重复。",
Name: builtin.ToolListVulnerabilities,
Description: "列出当前授权范围内的漏洞(摘要)。默认:对话已绑定项目时列出该项目下全部漏洞;未绑项目时仅列出当前会话漏洞。可用 scope=conversation 仅看本会话。记录新漏洞前建议先调用以避免重复。",
ShortDescription: "列出漏洞(默认当前项目)",
InputSchema: map[string]interface{}{
"type": "object",
+74 -59
View File
@@ -30,7 +30,7 @@ type Config struct {
Monitor MonitorConfig `yaml:"monitor,omitempty" json:"monitor,omitempty"`
ExternalMCP ExternalMCPConfig `yaml:"external_mcp,omitempty"`
Knowledge KnowledgeConfig `yaml:"knowledge,omitempty"`
C2 C2Config `yaml:"c2,omitempty" json:"c2,omitempty"` // 内置 C2 总开关;未配置时默认启用
C2 C2Config `yaml:"c2,omitempty" json:"c2,omitempty"` // 内置 C2 总开关;未配置时默认启用
Robots RobotsConfig `yaml:"robots,omitempty" json:"robots,omitempty"` // 企业微信/钉钉/飞书等机器人配置
RolesDir string `yaml:"roles_dir,omitempty" json:"roles_dir,omitempty"` // 角色配置文件目录(新方式)
Roles map[string]RoleConfig `yaml:"roles,omitempty" json:"roles,omitempty"` // 向后兼容:支持在主配置文件中定义角色
@@ -79,7 +79,7 @@ func (c ProjectConfig) FactSummaryMaxRunesEffective() int {
type MultiAgentConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
RobotDefaultAgentMode string `yaml:"robot_default_agent_mode,omitempty" json:"robot_default_agent_mode,omitempty"` // eino_single | deep | plan_execute | supervisor
BatchUseMultiAgent bool `yaml:"batch_use_multi_agent" json:"batch_use_multi_agent"` // 为 true 时批量任务队列中每子任务走 Eino 多代理
BatchUseMultiAgent bool `yaml:"batch_use_multi_agent" json:"batch_use_multi_agent"` // 为 true 时批量任务队列中每子任务走 Eino 多代理
// Orchestration 已弃用:保留仅兼容旧版 config.yaml;编排由聊天/WebShell 请求体 orchestration 决定,未传时按 deep。
Orchestration string `yaml:"orchestration,omitempty" json:"orchestration,omitempty"`
// MaxIteration 已废弃:统一使用 agent.max_iterationsYAML 中保留字段仅为兼容旧配置,运行时不读取)。
@@ -87,10 +87,10 @@ type MultiAgentConfig struct {
// PlanExecuteLoopMaxIterations plan_execute 模式下 execute↔replan 外层循环上限;0 表示用 Eino 默认 10。
PlanExecuteLoopMaxIterations int `yaml:"plan_execute_loop_max_iterations,omitempty" json:"plan_execute_loop_max_iterations,omitempty"`
// SubAgentMaxIterations 已废弃:子代理与主代理均使用 agent.max_iterationsMarkdown max_iterations>0 可覆盖)。
SubAgentMaxIterations int `yaml:"sub_agent_max_iterations,omitempty" json:"sub_agent_max_iterations,omitempty"`
WithoutGeneralSubAgent bool `yaml:"without_general_sub_agent" json:"without_general_sub_agent"`
WithoutWriteTodos bool `yaml:"without_write_todos" json:"without_write_todos"`
OrchestratorInstruction string `yaml:"orchestrator_instruction" json:"orchestrator_instruction"`
SubAgentMaxIterations int `yaml:"sub_agent_max_iterations,omitempty" json:"sub_agent_max_iterations,omitempty"`
WithoutGeneralSubAgent bool `yaml:"without_general_sub_agent" json:"without_general_sub_agent"`
WithoutWriteTodos bool `yaml:"without_write_todos" json:"without_write_todos"`
OrchestratorInstruction string `yaml:"orchestrator_instruction" json:"orchestrator_instruction"`
// OrchestratorInstructionPlanExecute plan_execute 主代理(规划侧)系统提示;非空且 agents/orchestrator-plan-execute.md 正文为空或未存在时生效。不与 Deep 的 orchestrator_instruction 混用。
OrchestratorInstructionPlanExecute string `yaml:"orchestrator_instruction_plan_execute,omitempty" json:"orchestrator_instruction_plan_execute,omitempty"`
// OrchestratorInstructionSupervisor supervisor 主代理系统提示(transfer/exit 说明仍由运行追加);非空且 agents/orchestrator-supervisor.md 正文为空或未存在时生效。
@@ -130,11 +130,11 @@ type MultiAgentEinoCallbacksConfig struct {
// MultiAgentEinoCallbacksOtelConfig OpenTelemetry for Eino callback spans (W3C trace in collector / stdout).
type MultiAgentEinoCallbacksOtelConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
ServiceName string `yaml:"service_name,omitempty" json:"service_name,omitempty"`
Exporter string `yaml:"exporter,omitempty" json:"exporter,omitempty"` // none | stdout | otlphttp
OTLPEndpoint string `yaml:"otlp_endpoint,omitempty" json:"otlp_endpoint,omitempty"` // host:port, e.g. localhost:4318 (path /v1/traces)
SampleRatio float64 `yaml:"sample_ratio,omitempty" json:"sample_ratio,omitempty"` // 01, default 1.0
Enabled bool `yaml:"enabled" json:"enabled"`
ServiceName string `yaml:"service_name,omitempty" json:"service_name,omitempty"`
Exporter string `yaml:"exporter,omitempty" json:"exporter,omitempty"` // none | stdout | otlphttp
OTLPEndpoint string `yaml:"otlp_endpoint,omitempty" json:"otlp_endpoint,omitempty"` // host:port, e.g. localhost:4318 (path /v1/traces)
SampleRatio float64 `yaml:"sample_ratio,omitempty" json:"sample_ratio,omitempty"` // 01, default 1.0
}
// EinoCallbacksModeEffective returns off | log_only | sse | full.
@@ -245,12 +245,12 @@ type MultiAgentEinoMiddlewareConfig struct {
// PlantaskRelDir relative to skills_dir for per-conversation task boards (default .eino/plantask).
PlantaskRelDir string `yaml:"plantask_rel_dir,omitempty" json:"plantask_rel_dir,omitempty"`
// Reduction truncates/offloads large tool outputs (requires eino local backend for Write).
ReductionEnable bool `yaml:"reduction_enable,omitempty" json:"reduction_enable,omitempty"`
ReductionRootDir string `yaml:"reduction_root_dir,omitempty" json:"reduction_root_dir,omitempty"` // 非空:落盘根目录(默认 tmp/reduction);其下按 projects/{id} 或 conversations/{id} 隔离
ReductionMaxLengthForTrunc int `yaml:"reduction_max_length_for_trunc,omitempty" json:"reduction_max_length_for_trunc,omitempty"` // default 12000
ReductionMaxTokensForClear int `yaml:"reduction_max_tokens_for_clear,omitempty" json:"reduction_max_tokens_for_clear,omitempty"` // default 50000
ReductionClearExclude []string `yaml:"reduction_clear_exclude,omitempty" json:"reduction_clear_exclude,omitempty"`
ReductionSubAgents bool `yaml:"reduction_sub_agents,omitempty" json:"reduction_sub_agents,omitempty"` // also attach to sub-agents
ReductionEnable bool `yaml:"reduction_enable,omitempty" json:"reduction_enable,omitempty"`
ReductionRootDir string `yaml:"reduction_root_dir,omitempty" json:"reduction_root_dir,omitempty"` // 非空:落盘根目录(默认 tmp/reduction);其下按 projects/{id} 或 conversations/{id} 隔离
ReductionMaxLengthForTrunc int `yaml:"reduction_max_length_for_trunc,omitempty" json:"reduction_max_length_for_trunc,omitempty"` // default 12000
ReductionMaxTokensForClear int `yaml:"reduction_max_tokens_for_clear,omitempty" json:"reduction_max_tokens_for_clear,omitempty"` // default 50000
ReductionClearExclude []string `yaml:"reduction_clear_exclude,omitempty" json:"reduction_clear_exclude,omitempty"`
ReductionSubAgents bool `yaml:"reduction_sub_agents,omitempty" json:"reduction_sub_agents,omitempty"` // also attach to sub-agents
// SummarizationTriggerRatio controls summarization trigger threshold as max_total_tokens * ratio (default 0.8).
SummarizationTriggerRatio float64 `yaml:"summarization_trigger_ratio,omitempty" json:"summarization_trigger_ratio,omitempty"`
// SummarizationEmitInternalEvents controls middleware internal event emission (default true).
@@ -398,13 +398,13 @@ type MultiAgentSubConfig struct {
// MultiAgentPublic 返回给前端的精简信息(不含子代理指令全文)。
type MultiAgentPublic struct {
Enabled bool `json:"enabled"`
RobotDefaultAgentMode string `json:"robot_default_agent_mode,omitempty"`
BatchUseMultiAgent bool `json:"batch_use_multi_agent"`
SubAgentCount int `json:"sub_agent_count"`
Orchestration string `json:"orchestration,omitempty"`
PlanExecuteLoopMaxIterations int `json:"plan_execute_loop_max_iterations"`
ToolSearchAlwaysVisibleTools []string `json:"tool_search_always_visible_tools,omitempty"`
Enabled bool `json:"enabled"`
RobotDefaultAgentMode string `json:"robot_default_agent_mode,omitempty"`
BatchUseMultiAgent bool `json:"batch_use_multi_agent"`
SubAgentCount int `json:"sub_agent_count"`
Orchestration string `json:"orchestration,omitempty"`
PlanExecuteLoopMaxIterations int `json:"plan_execute_loop_max_iterations"`
ToolSearchAlwaysVisibleTools []string `json:"tool_search_always_visible_tools,omitempty"`
ToolSearchAlwaysVisibleEffectiveTools []string `json:"tool_search_always_visible_effective_tools,omitempty"`
}
@@ -445,10 +445,10 @@ func NormalizeMultiAgentOrchestration(s string) string {
// MultiAgentAPIUpdate 设置页/API 仅更新多代理标量字段;写入 YAML 时不覆盖 sub_agents 等块。
type MultiAgentAPIUpdate struct {
Enabled bool `json:"enabled"`
RobotDefaultAgentMode string `json:"robot_default_agent_mode,omitempty"`
BatchUseMultiAgent bool `json:"batch_use_multi_agent"`
PlanExecuteLoopMaxIterations *int `json:"plan_execute_loop_max_iterations,omitempty"`
Enabled bool `json:"enabled"`
RobotDefaultAgentMode string `json:"robot_default_agent_mode,omitempty"`
BatchUseMultiAgent bool `json:"batch_use_multi_agent"`
PlanExecuteLoopMaxIterations *int `json:"plan_execute_loop_max_iterations,omitempty"`
// 指针区分「JSON 未传该字段」与「传空数组要清空」;省略时不应覆盖 YAML 中的常驻工具白名单。
ToolSearchAlwaysVisibleTools *[]string `json:"tool_search_always_visible_tools,omitempty"`
}
@@ -464,14 +464,14 @@ type RobotsConfig struct {
// RobotWechatConfig 微信 iLink 机器人配置(个人微信 ClawBot / iLink 协议)
type RobotWechatConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
BotToken string `yaml:"bot_token,omitempty" json:"bot_token,omitempty"`
ILinkBotID string `yaml:"ilink_bot_id,omitempty" json:"ilink_bot_id,omitempty"`
ILinkUserID string `yaml:"ilink_user_id,omitempty" json:"ilink_user_id,omitempty"`
BaseURL string `yaml:"base_url,omitempty" json:"base_url,omitempty"` // 默认 https://ilinkai.weixin.qq.com
BotType string `yaml:"bot_type,omitempty" json:"bot_type,omitempty"` // get_bot_qrcode 参数,默认 3
BotAgent string `yaml:"bot_agent,omitempty" json:"bot_agent,omitempty"` // base_info.bot_agent
GetUpdatesBuf string `yaml:"get_updates_buf,omitempty" json:"get_updates_buf,omitempty"` // 长轮询游标(运行时)
Enabled bool `yaml:"enabled" json:"enabled"`
BotToken string `yaml:"bot_token,omitempty" json:"bot_token,omitempty"`
ILinkBotID string `yaml:"ilink_bot_id,omitempty" json:"ilink_bot_id,omitempty"`
ILinkUserID string `yaml:"ilink_user_id,omitempty" json:"ilink_user_id,omitempty"`
BaseURL string `yaml:"base_url,omitempty" json:"base_url,omitempty"` // 默认 https://ilinkai.weixin.qq.com
BotType string `yaml:"bot_type,omitempty" json:"bot_type,omitempty"` // get_bot_qrcode 参数,默认 3
BotAgent string `yaml:"bot_agent,omitempty" json:"bot_agent,omitempty"` // base_info.bot_agent
GetUpdatesBuf string `yaml:"get_updates_buf,omitempty" json:"get_updates_buf,omitempty"` // 长轮询游标(运行时)
}
// RobotSessionConfig 机器人会话隔离策略
@@ -510,19 +510,19 @@ func ValidateWecomConfig(w RobotWecomConfig) error {
// RobotDingtalkConfig 钉钉机器人配置
type RobotDingtalkConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
ClientID string `yaml:"client_id" json:"client_id"` // 应用 Key (AppKey)
ClientSecret string `yaml:"client_secret" json:"client_secret"` // 应用 Secret
Enabled bool `yaml:"enabled" json:"enabled"`
ClientID string `yaml:"client_id" json:"client_id"` // 应用 Key (AppKey)
ClientSecret string `yaml:"client_secret" json:"client_secret"` // 应用 Secret
AllowConversationIDFallback bool `yaml:"allow_conversation_id_fallback" json:"allow_conversation_id_fallback"` // sender_id 缺失时是否允许回退到会话 ID
}
// RobotLarkConfig 飞书机器人配置
type RobotLarkConfig struct {
Enabled bool `yaml:"enabled" json:"enabled"`
AppID string `yaml:"app_id" json:"app_id"` // 应用 App ID
AppSecret string `yaml:"app_secret" json:"app_secret"` // 应用 App Secret
VerifyToken string `yaml:"verify_token" json:"verify_token"` // 事件订阅 Verification Token(可选)
AllowChatIDFallback bool `yaml:"allow_chat_id_fallback" json:"allow_chat_id_fallback"` // 用户 ID 缺失时是否允许回退到 chat_id
Enabled bool `yaml:"enabled" json:"enabled"`
AppID string `yaml:"app_id" json:"app_id"` // 应用 App ID
AppSecret string `yaml:"app_secret" json:"app_secret"` // 应用 App Secret
VerifyToken string `yaml:"verify_token" json:"verify_token"` // 事件订阅 Verification Token(可选)
AllowChatIDFallback bool `yaml:"allow_chat_id_fallback" json:"allow_chat_id_fallback"` // 用户 ID 缺失时是否允许回退到 chat_id
}
type ServerConfig struct {
@@ -621,8 +621,8 @@ type DatabaseConfig struct {
}
type AgentConfig struct {
MaxIterations int `yaml:"max_iterations" json:"max_iterations"`
ToolTimeoutMinutes int `yaml:"tool_timeout_minutes" json:"tool_timeout_minutes"` // 单次工具执行最大时长(分钟),超时自动终止,防止长时间挂起;0 表示不限制(不推荐)
MaxIterations int `yaml:"max_iterations" json:"max_iterations"`
ToolTimeoutMinutes int `yaml:"tool_timeout_minutes" json:"tool_timeout_minutes"` // 单次工具执行最大时长(分钟),超时自动终止,防止长时间挂起;0 表示不限制(不推荐)
// ShellNoOutputTimeoutSeconds execute/exec 无任何 stdout/stderr 时的空闲终止秒数(通用防挂死,不维护命令黑名单);0=默认 300(5 分钟);-1=关闭。
ShellNoOutputTimeoutSeconds int `yaml:"shell_no_output_timeout_seconds" json:"shell_no_output_timeout_seconds"`
// WorkspaceRootDir 会话工作目录根路径(curl/wget 下载、read_file/glob/grep 本地分析);空=tmp/workspace,其下按 projects/{id} 或 conversations/{id} 隔离。
@@ -643,6 +643,18 @@ type HitlConfig struct {
AuditAgentPromptReviewEdit string `yaml:"audit_agent_prompt_review_edit,omitempty" json:"audit_agent_prompt_review_edit,omitempty"`
// RetentionDays 已决策审计日志(hitl_interrupts 非 pending)保留天数;省略时默认 90;0 表示不自动清理。
RetentionDays *int `yaml:"retention_days,omitempty" json:"retention_days,omitempty"`
// DefaultReviewer 全局默认审批方(human | audit_agent);未选会话时切换会写入 config.yaml;新建会话无独立配置时沿用。
DefaultReviewer string `yaml:"default_reviewer,omitempty" json:"default_reviewer,omitempty"`
}
// EffectiveDefaultReviewer returns human or audit_agent; omitted or unknown values default to human.
func (h HitlConfig) EffectiveDefaultReviewer() string {
switch strings.ToLower(strings.TrimSpace(h.DefaultReviewer)) {
case "audit_agent", "agent", "ai":
return "audit_agent"
default:
return "human"
}
}
// RetentionDaysEffective returns retention; 0 means keep forever; omitted defaults to 90.
@@ -756,9 +768,9 @@ func (m MonitorConfig) RetentionDaysEffective() int {
// AuditConfig platform operation audit log settings (not chat/tool execution bodies).
type AuditConfig struct {
// Enabled nil or true enables persistence; explicit false disables.
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
RetentionDays int `yaml:"retention_days,omitempty" json:"retention_days,omitempty"`
MaxDetailBytes int `yaml:"max_detail_bytes,omitempty" json:"max_detail_bytes,omitempty"`
Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
RetentionDays int `yaml:"retention_days,omitempty" json:"retention_days,omitempty"`
MaxDetailBytes int `yaml:"max_detail_bytes,omitempty" json:"max_detail_bytes,omitempty"`
// AuthFailureCooldownSeconds: per-IP cooldown for auth login/change_password failure audit rows; -1 disables; 0 uses default 60.
AuthFailureCooldownSeconds int `yaml:"auth_failure_cooldown_seconds,omitempty" json:"auth_failure_cooldown_seconds,omitempty"`
}
@@ -1436,8 +1448,8 @@ func Default() *Config {
},
Agent: AgentConfig{
MaxIterations: 30, // 默认最大迭代次数
ToolTimeoutMinutes: 10, // 单次工具执行默认最多 10 分钟,避免异常长时间占用
ShellNoOutputTimeoutSeconds: 300, // execute/exec 无新输出空闲终止(秒);-1 关闭
ToolTimeoutMinutes: 10, // 单次工具执行默认最多 10 分钟,避免异常长时间占用
ShellNoOutputTimeoutSeconds: 300, // execute/exec 无新输出空闲终止(秒);-1 关闭
},
Security: SecurityConfig{
Tools: []ToolConfig{}, // 工具配置应该从 config.yaml 或 tools/ 目录加载
@@ -1638,7 +1650,7 @@ type RetrievalConfig struct {
TopK int `yaml:"top_k" json:"top_k"` // 检索Top-K
SimilarityThreshold float64 `yaml:"similarity_threshold" json:"similarity_threshold"` // 余弦相似度阈值
// SubIndexFilter 非空时仅保留 sub_indexes 含该标签(逗号分隔之一)的行;sub_indexes 为空的旧行仍返回。
SubIndexFilter string `yaml:"sub_index_filter,omitempty" json:"sub_index_filter,omitempty"`
SubIndexFilter string `yaml:"sub_index_filter,omitempty" json:"sub_index_filter,omitempty"`
MultiQuery MultiQueryConfig `yaml:"multi_query" json:"multi_query"`
Rerank RerankConfig `yaml:"rerank" json:"rerank"`
// PostRetrieve 检索后处理(去重、预算截断);精排在 MultiQuery 融合后执行。
@@ -1653,11 +1665,14 @@ type RolesConfig struct {
// RoleConfig 单个角色配置
type RoleConfig struct {
Name string `yaml:"name" json:"name"` // 角色名称
Description string `yaml:"description" json:"description"` // 角色描述
UserPrompt string `yaml:"user_prompt" json:"user_prompt"` // 用户提示词(追加到用户消息前)
Icon string `yaml:"icon,omitempty" json:"icon,omitempty"` // 角色图标(可选)
Tools []string `yaml:"tools,omitempty" json:"tools,omitempty"` // 关联的工具列表(toolKey格式,如 "toolName" 或 "mcpName::toolName"
MCPs []string `yaml:"mcps,omitempty" json:"mcps,omitempty"` // 向后兼容:关联的MCP服务器列表(已废弃,使用tools替代)
Enabled bool `yaml:"enabled" json:"enabled"` // 是否启用
Name string `yaml:"name" json:"name"` // 角色名称
Description string `yaml:"description" json:"description"` // 角色描述
UserPrompt string `yaml:"user_prompt" json:"user_prompt"` // 用户提示词(追加到用户消息前)
Icon string `yaml:"icon,omitempty" json:"icon,omitempty"` // 角色图标(可选)
Tools []string `yaml:"tools,omitempty" json:"tools,omitempty"` // 关联的工具列表(toolKey格式,如 "toolName" 或 "mcpName::toolName"
MCPs []string `yaml:"mcps,omitempty" json:"mcps,omitempty"` // 向后兼容:关联的MCP服务器列表(已废弃,使用tools替代)
WorkflowID string `yaml:"workflow_id,omitempty" json:"workflow_id,omitempty"` // 可选:绑定图编排流程 ID
WorkflowVersion string `yaml:"workflow_version,omitempty" json:"workflow_version,omitempty"` // latest 或具体版本号;空等同 latest
WorkflowPolicy string `yaml:"workflow_policy,omitempty" json:"workflow_policy,omitempty"` // auto | off;空且 workflow_id 非空时按 auto
Enabled bool `yaml:"enabled" json:"enabled"` // 是否启用
}
+84 -5
View File
@@ -5,8 +5,8 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"strings"
"sync"
"time"
_ "github.com/mattn/go-sqlite3"
@@ -388,9 +388,12 @@ func (db *DB) initTables() error {
status TEXT NOT NULL DEFAULT 'open',
vulnerability_type TEXT,
target TEXT,
proof TEXT,
preconditions TEXT,
reproduction_steps TEXT,
evidence TEXT,
impact TEXT,
recommendation TEXT,
retest_notes TEXT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
project_id TEXT,
@@ -584,6 +587,53 @@ func (db *DB) initTables() error {
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);`
createWorkflowDefinitionsTable := `
CREATE TABLE IF NOT EXISTS workflow_definitions (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
version INTEGER NOT NULL DEFAULT 1,
graph_json TEXT NOT NULL,
enabled INTEGER NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
);`
createWorkflowRunsTable := `
CREATE TABLE IF NOT EXISTS workflow_runs (
id TEXT PRIMARY KEY,
workflow_id TEXT NOT NULL,
workflow_version INTEGER NOT NULL DEFAULT 1,
conversation_id TEXT,
project_id TEXT,
role_id TEXT,
status TEXT NOT NULL,
input_json TEXT,
output_json TEXT,
error TEXT,
pending_hitl_node_id TEXT,
pending_hitl_json TEXT,
started_at DATETIME NOT NULL,
finished_at DATETIME,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE SET NULL
);`
createWorkflowNodeRunsTable := `
CREATE TABLE IF NOT EXISTS workflow_node_runs (
id TEXT PRIMARY KEY,
run_id TEXT NOT NULL,
node_id TEXT NOT NULL,
status TEXT NOT NULL,
input_json TEXT,
output_json TEXT,
error TEXT,
started_at DATETIME NOT NULL,
finished_at DATETIME,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (run_id) REFERENCES workflow_runs(id) ON DELETE CASCADE
);`
// 创建索引
createIndexes := `
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages(conversation_id);
@@ -642,6 +692,12 @@ func (db *DB) initTables() error {
CREATE INDEX IF NOT EXISTS idx_audit_logs_category ON audit_logs(category);
CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON audit_logs(action);
CREATE INDEX IF NOT EXISTS idx_audit_logs_result ON audit_logs(result);
CREATE INDEX IF NOT EXISTS idx_workflow_definitions_updated_at ON workflow_definitions(updated_at);
CREATE INDEX IF NOT EXISTS idx_workflow_definitions_enabled ON workflow_definitions(enabled);
CREATE INDEX IF NOT EXISTS idx_workflow_runs_workflow ON workflow_runs(workflow_id);
CREATE INDEX IF NOT EXISTS idx_workflow_runs_conversation ON workflow_runs(conversation_id);
CREATE INDEX IF NOT EXISTS idx_workflow_runs_status ON workflow_runs(status);
CREATE INDEX IF NOT EXISTS idx_workflow_node_runs_run ON workflow_node_runs(run_id);
`
if _, err := db.Exec(createConversationsTable); err != nil {
@@ -727,6 +783,16 @@ func (db *DB) initTables() error {
return fmt.Errorf("创建audit_logs表失败: %w", err)
}
for tableName, ddl := range map[string]string{
"workflow_definitions": createWorkflowDefinitionsTable,
"workflow_runs": createWorkflowRunsTable,
"workflow_node_runs": createWorkflowNodeRunsTable,
} {
if _, err := db.Exec(ddl); err != nil {
return fmt.Errorf("创建%s表失败: %w", tableName, err)
}
}
for tableName, ddl := range map[string]string{
"c2_listeners": createC2ListenersTable,
"c2_sessions": createC2SessionsTable,
@@ -784,6 +850,9 @@ func (db *DB) initTables() error {
db.logger.Warn("迁移webshell_connections表失败", zap.Error(err))
// 不返回错误,允许继续运行
}
if err := db.migrateWorkflowRunsTable(); err != nil {
db.logger.Warn("迁移workflow_runs表失败", zap.Error(err))
}
if _, err := db.Exec(createIndexes); err != nil {
return fmt.Errorf("创建索引失败: %w", err)
@@ -1224,9 +1293,12 @@ func (db *DB) migrateVulnerabilitiesConversationFK() error {
status TEXT NOT NULL DEFAULT 'open',
vulnerability_type TEXT,
target TEXT,
proof TEXT,
preconditions TEXT,
reproduction_steps TEXT,
evidence TEXT,
impact TEXT,
recommendation TEXT,
retest_notes TEXT,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
project_id TEXT,
@@ -1239,12 +1311,15 @@ func (db *DB) migrateVulnerabilitiesConversationFK() error {
const copyRows = `
INSERT INTO vulnerabilities_new (
id, conversation_id, conversation_tag, task_tag, title, description,
severity, status, vulnerability_type, target, proof, impact, recommendation,
severity, status, vulnerability_type, target, preconditions, reproduction_steps,
evidence, impact, recommendation, retest_notes,
created_at, updated_at, project_id
)
SELECT
id, conversation_id, conversation_tag, task_tag, title, description,
severity, status, vulnerability_type, target, proof, impact, recommendation,
severity, status, vulnerability_type, target,
COALESCE(preconditions, ''), COALESCE(reproduction_steps, ''),
COALESCE(evidence, ''), impact, recommendation, COALESCE(retest_notes, ''),
created_at, updated_at, project_id
FROM vulnerabilities;`
if _, err := tx.Exec(copyRows); err != nil {
@@ -1315,6 +1390,10 @@ func (db *DB) migrateVulnerabilitiesTable() error {
{name: "conversation_tag", stmt: "ALTER TABLE vulnerabilities ADD COLUMN conversation_tag TEXT"},
{name: "task_tag", stmt: "ALTER TABLE vulnerabilities ADD COLUMN task_tag TEXT"},
{name: "project_id", stmt: "ALTER TABLE vulnerabilities ADD COLUMN project_id TEXT"},
{name: "preconditions", stmt: "ALTER TABLE vulnerabilities ADD COLUMN preconditions TEXT"},
{name: "reproduction_steps", stmt: "ALTER TABLE vulnerabilities ADD COLUMN reproduction_steps TEXT"},
{name: "evidence", stmt: "ALTER TABLE vulnerabilities ADD COLUMN evidence TEXT"},
{name: "retest_notes", stmt: "ALTER TABLE vulnerabilities ADD COLUMN retest_notes TEXT"},
}
for _, col := range columns {
+24 -14
View File
@@ -72,14 +72,17 @@ func (f VulnerabilityListFilter) appendWhere(query string, args []interface{}) (
LOWER(COALESCE(description, '')) LIKE LOWER(?) OR
LOWER(COALESCE(vulnerability_type, '')) LIKE LOWER(?) OR
LOWER(COALESCE(target, '')) LIKE LOWER(?) OR
LOWER(COALESCE(proof, '')) LIKE LOWER(?) OR
LOWER(COALESCE(preconditions, '')) LIKE LOWER(?) OR
LOWER(COALESCE(reproduction_steps, '')) LIKE LOWER(?) OR
LOWER(COALESCE(evidence, '')) LIKE LOWER(?) OR
LOWER(COALESCE(impact, '')) LIKE LOWER(?) OR
LOWER(COALESCE(recommendation, '')) LIKE LOWER(?) OR
LOWER(COALESCE(retest_notes, '')) LIKE LOWER(?) OR
LOWER(COALESCE(conversation_id, '')) LIKE LOWER(?) OR
LOWER(COALESCE(conversation_tag, '')) LIKE LOWER(?) OR
LOWER(COALESCE(task_tag, '')) LIKE LOWER(?)
)`
for i := 0; i < 11; i++ {
for i := 0; i < 14; i++ {
args = append(args, pattern)
}
}
@@ -101,9 +104,12 @@ type Vulnerability struct {
Status string `json:"status"` // open, confirmed, fixed, false_positive, ignored
Type string `json:"type"`
Target string `json:"target"`
Proof string `json:"proof"`
Preconditions string `json:"preconditions"`
ReproSteps string `json:"reproduction_steps"`
Evidence string `json:"evidence"`
Impact string `json:"impact"`
Recommendation string `json:"recommendation"`
RetestNotes string `json:"retest_notes"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
@@ -131,16 +137,16 @@ func (db *DB) CreateVulnerability(vuln *Vulnerability) (*Vulnerability, error) {
query := `
INSERT INTO vulnerabilities (
id, conversation_id, project_id, conversation_tag, task_tag, title, description, severity, status,
vulnerability_type, target, proof, impact, recommendation,
vulnerability_type, target, preconditions, reproduction_steps, evidence, impact, recommendation, retest_notes,
created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
_, err := db.Exec(
query,
vuln.ID, nullIfEmpty(vuln.ConversationID), nullIfEmpty(vuln.ProjectID), vuln.ConversationTag, vuln.TaskTag, vuln.Title, vuln.Description,
vuln.Severity, vuln.Status, vuln.Type, vuln.Target,
vuln.Proof, vuln.Impact, vuln.Recommendation,
vuln.Preconditions, vuln.ReproSteps, vuln.Evidence, vuln.Impact, vuln.Recommendation, vuln.RetestNotes,
vuln.CreatedAt, vuln.UpdatedAt,
)
if err != nil {
@@ -155,7 +161,9 @@ func (db *DB) GetVulnerability(id string) (*Vulnerability, error) {
var vuln Vulnerability
query := `
SELECT id, COALESCE(conversation_id,''), COALESCE(project_id,''), title, description, severity, status,
conversation_tag, task_tag, vulnerability_type, target, proof, impact, recommendation,
conversation_tag, task_tag, vulnerability_type, target,
COALESCE(preconditions,''), COALESCE(reproduction_steps,''), COALESCE(evidence,''),
impact, recommendation, COALESCE(retest_notes,''),
COALESCE((SELECT bt.id FROM batch_tasks bt WHERE bt.conversation_id = vulnerabilities.conversation_id LIMIT 1), '') AS task_id,
COALESCE((SELECT bt.queue_id FROM batch_tasks bt WHERE bt.conversation_id = vulnerabilities.conversation_id LIMIT 1), '') AS task_queue_id,
created_at, updated_at
@@ -166,7 +174,7 @@ func (db *DB) GetVulnerability(id string) (*Vulnerability, error) {
err := db.QueryRow(query, id).Scan(
&vuln.ID, &vuln.ConversationID, &vuln.ProjectID, &vuln.Title, &vuln.Description,
&vuln.Severity, &vuln.Status, &vuln.ConversationTag, &vuln.TaskTag, &vuln.Type, &vuln.Target,
&vuln.Proof, &vuln.Impact, &vuln.Recommendation,
&vuln.Preconditions, &vuln.ReproSteps, &vuln.Evidence, &vuln.Impact, &vuln.Recommendation, &vuln.RetestNotes,
&vuln.TaskID, &vuln.TaskQueueID,
&vuln.CreatedAt, &vuln.UpdatedAt,
)
@@ -184,7 +192,9 @@ func (db *DB) GetVulnerability(id string) (*Vulnerability, error) {
func (db *DB) ListVulnerabilities(limit, offset int, filter VulnerabilityListFilter) ([]*Vulnerability, error) {
query := `
SELECT id, COALESCE(conversation_id,''), COALESCE(project_id,''), title, description, severity, status, conversation_tag, task_tag,
vulnerability_type, target, proof, impact, recommendation,
vulnerability_type, target,
COALESCE(preconditions,''), COALESCE(reproduction_steps,''), COALESCE(evidence,''),
impact, recommendation, COALESCE(retest_notes,''),
COALESCE((SELECT bt.id FROM batch_tasks bt WHERE bt.conversation_id = vulnerabilities.conversation_id LIMIT 1), '') AS task_id,
COALESCE((SELECT bt.queue_id FROM batch_tasks bt WHERE bt.conversation_id = vulnerabilities.conversation_id LIMIT 1), '') AS task_queue_id,
created_at, updated_at
@@ -209,7 +219,7 @@ func (db *DB) ListVulnerabilities(limit, offset int, filter VulnerabilityListFil
err := rows.Scan(
&vuln.ID, &vuln.ConversationID, &vuln.ProjectID, &vuln.Title, &vuln.Description,
&vuln.Severity, &vuln.Status, &vuln.ConversationTag, &vuln.TaskTag, &vuln.Type, &vuln.Target,
&vuln.Proof, &vuln.Impact, &vuln.Recommendation,
&vuln.Preconditions, &vuln.ReproSteps, &vuln.Evidence, &vuln.Impact, &vuln.Recommendation, &vuln.RetestNotes,
&vuln.TaskID, &vuln.TaskQueueID,
&vuln.CreatedAt, &vuln.UpdatedAt,
)
@@ -245,16 +255,16 @@ func (db *DB) UpdateVulnerability(id string, vuln *Vulnerability) error {
query := `
UPDATE vulnerabilities
SET project_id = ?, conversation_tag = ?, task_tag = ?, title = ?, description = ?, severity = ?, status = ?,
vulnerability_type = ?, target = ?, proof = ?, impact = ?,
recommendation = ?, updated_at = ?
vulnerability_type = ?, target = ?, preconditions = ?, reproduction_steps = ?, evidence = ?, impact = ?,
recommendation = ?, retest_notes = ?, updated_at = ?
WHERE id = ?
`
_, err := db.Exec(
query,
nullIfEmpty(vuln.ProjectID), vuln.ConversationTag, vuln.TaskTag, vuln.Title, vuln.Description, vuln.Severity, vuln.Status,
vuln.Type, vuln.Target, vuln.Proof, vuln.Impact,
vuln.Recommendation, vuln.UpdatedAt, id,
vuln.Type, vuln.Target, vuln.Preconditions, vuln.ReproSteps, vuln.Evidence, vuln.Impact,
vuln.Recommendation, vuln.RetestNotes, vuln.UpdatedAt, id,
)
if err != nil {
return fmt.Errorf("更新漏洞失败: %w", err)
+424
View File
@@ -0,0 +1,424 @@
package database
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
"time"
)
// WorkflowDefinition is a persisted user-defined graph/workflow template.
// graph_json intentionally remains opaque so users can define their own fields.
type WorkflowDefinition struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Version int `json:"version"`
GraphJSON string `json:"graph_json"`
Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type WorkflowRun struct {
ID string `json:"id"`
WorkflowID string `json:"workflow_id"`
WorkflowVersion int `json:"workflow_version"`
ConversationID string `json:"conversation_id,omitempty"`
ProjectID string `json:"project_id,omitempty"`
RoleID string `json:"role_id,omitempty"`
Status string `json:"status"`
InputJSON string `json:"input_json,omitempty"`
OutputJSON string `json:"output_json,omitempty"`
Error string `json:"error,omitempty"`
PendingHITLNodeID string `json:"pending_hitl_node_id,omitempty"`
PendingHITLJSON string `json:"pending_hitl_json,omitempty"`
StartedAt time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
}
type WorkflowNodeRun struct {
ID string `json:"id"`
RunID string `json:"run_id"`
NodeID string `json:"node_id"`
Status string `json:"status"`
InputJSON string `json:"input_json,omitempty"`
OutputJSON string `json:"output_json,omitempty"`
Error string `json:"error,omitempty"`
StartedAt time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at,omitempty"`
}
func scanWorkflowDefinition(scanner interface {
Scan(dest ...interface{}) error
}) (*WorkflowDefinition, error) {
var row WorkflowDefinition
var desc sql.NullString
var enabled int
if err := scanner.Scan(&row.ID, &row.Name, &desc, &row.Version, &row.GraphJSON, &enabled, &row.CreatedAt, &row.UpdatedAt); err != nil {
return nil, err
}
row.Description = desc.String
row.Enabled = enabled != 0
return &row, nil
}
const workflowDefinitionColumns = `id, name, description, version, graph_json, enabled, created_at, updated_at`
func (db *DB) ListWorkflowDefinitions(includeDisabled bool) ([]*WorkflowDefinition, error) {
query := "SELECT " + workflowDefinitionColumns + " FROM workflow_definitions"
if !includeDisabled {
query += " WHERE enabled = 1"
}
query += " ORDER BY updated_at DESC"
rows, err := db.Query(query)
if err != nil {
return nil, fmt.Errorf("查询工作流列表失败: %w", err)
}
defer rows.Close()
var out []*WorkflowDefinition
for rows.Next() {
wf, err := scanWorkflowDefinition(rows)
if err != nil {
return nil, fmt.Errorf("扫描工作流失败: %w", err)
}
out = append(out, wf)
}
return out, rows.Err()
}
func (db *DB) GetWorkflowDefinition(id string) (*WorkflowDefinition, error) {
id = strings.TrimSpace(id)
if id == "" {
return nil, nil
}
wf, err := scanWorkflowDefinition(db.QueryRow("SELECT "+workflowDefinitionColumns+" FROM workflow_definitions WHERE id = ?", id))
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("查询工作流失败: %w", err)
}
return wf, nil
}
func (db *DB) UpsertWorkflowDefinition(wf *WorkflowDefinition) error {
if wf == nil {
return fmt.Errorf("工作流为空")
}
wf.ID = strings.TrimSpace(wf.ID)
wf.Name = strings.TrimSpace(wf.Name)
if wf.ID == "" || wf.Name == "" {
return fmt.Errorf("工作流 id 和 name 不能为空")
}
if strings.TrimSpace(wf.GraphJSON) == "" {
wf.GraphJSON = `{"nodes":[],"edges":[],"config":{}}`
}
if wf.Version <= 0 {
wf.Version = 1
}
now := time.Now()
existing, err := db.GetWorkflowDefinition(wf.ID)
if err != nil {
return err
}
if existing == nil {
_, err = db.Exec(
`INSERT INTO workflow_definitions (id, name, description, version, graph_json, enabled, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
wf.ID, wf.Name, wf.Description, wf.Version, wf.GraphJSON, boolToInt(wf.Enabled), now, now,
)
} else {
nextVersion := existing.Version + 1
if wf.Version > existing.Version {
nextVersion = wf.Version
}
_, err = db.Exec(
`UPDATE workflow_definitions
SET name = ?, description = ?, version = ?, graph_json = ?, enabled = ?, updated_at = ?
WHERE id = ?`,
wf.Name, wf.Description, nextVersion, wf.GraphJSON, boolToInt(wf.Enabled), now, wf.ID,
)
}
if err != nil {
return fmt.Errorf("保存工作流失败: %w", err)
}
return nil
}
func (db *DB) DeleteWorkflowDefinition(id string) error {
id = strings.TrimSpace(id)
if id == "" {
return fmt.Errorf("工作流 id 不能为空")
}
if _, err := db.Exec("DELETE FROM workflow_definitions WHERE id = ?", id); err != nil {
return fmt.Errorf("删除工作流失败: %w", err)
}
return nil
}
func (db *DB) CreateWorkflowRun(run *WorkflowRun) error {
if run == nil {
return fmt.Errorf("工作流运行为空")
}
if strings.TrimSpace(run.ID) == "" || strings.TrimSpace(run.WorkflowID) == "" {
return fmt.Errorf("工作流运行 id 和 workflow_id 不能为空")
}
if run.WorkflowVersion <= 0 {
run.WorkflowVersion = 1
}
if strings.TrimSpace(run.Status) == "" {
run.Status = "running"
}
if run.StartedAt.IsZero() {
run.StartedAt = time.Now()
}
_, err := db.Exec(
`INSERT INTO workflow_runs (id, workflow_id, workflow_version, conversation_id, project_id, role_id, status, input_json, started_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
run.ID, run.WorkflowID, run.WorkflowVersion, nullString(run.ConversationID), nullString(run.ProjectID), nullString(run.RoleID), run.Status, run.InputJSON, run.StartedAt,
)
if err != nil {
return fmt.Errorf("创建工作流运行失败: %w", err)
}
return nil
}
func (db *DB) FinishWorkflowRun(runID, status, outputJSON, errText string) error {
runID = strings.TrimSpace(runID)
if runID == "" {
return fmt.Errorf("工作流运行 id 不能为空")
}
if strings.TrimSpace(status) == "" {
status = "completed"
}
now := time.Now()
_, err := db.Exec(
`UPDATE workflow_runs SET status = ?, output_json = ?, error = ?, finished_at = ? WHERE id = ?`,
status, outputJSON, errText, now, runID,
)
if err != nil {
return fmt.Errorf("更新工作流运行失败: %w", err)
}
return nil
}
func (db *DB) CreateWorkflowNodeRun(n *WorkflowNodeRun) error {
if n == nil {
return fmt.Errorf("工作流节点运行为空")
}
if strings.TrimSpace(n.ID) == "" || strings.TrimSpace(n.RunID) == "" || strings.TrimSpace(n.NodeID) == "" {
return fmt.Errorf("节点运行 id、run_id 和 node_id 不能为空")
}
if strings.TrimSpace(n.Status) == "" {
n.Status = "running"
}
if n.StartedAt.IsZero() {
n.StartedAt = time.Now()
}
_, err := db.Exec(
`INSERT INTO workflow_node_runs (id, run_id, node_id, status, input_json, started_at)
VALUES (?, ?, ?, ?, ?, ?)`,
n.ID, n.RunID, n.NodeID, n.Status, n.InputJSON, n.StartedAt,
)
if err != nil {
return fmt.Errorf("创建工作流节点运行失败: %w", err)
}
return nil
}
func (db *DB) FinishWorkflowNodeRun(nodeRunID, status, outputJSON, errText string) error {
nodeRunID = strings.TrimSpace(nodeRunID)
if nodeRunID == "" {
return fmt.Errorf("节点运行 id 不能为空")
}
if strings.TrimSpace(status) == "" {
status = "completed"
}
now := time.Now()
_, err := db.Exec(
`UPDATE workflow_node_runs SET status = ?, output_json = ?, error = ?, finished_at = ? WHERE id = ?`,
status, outputJSON, errText, now, nodeRunID,
)
if err != nil {
return fmt.Errorf("更新工作流节点运行失败: %w", err)
}
return nil
}
func scanWorkflowRun(scanner interface {
Scan(dest ...interface{}) error
}) (*WorkflowRun, error) {
var row WorkflowRun
var convID, projectID, roleID, inputJSON, outputJSON, errText, pendingNode, pendingJSON sql.NullString
var finishedAt sql.NullTime
if err := scanner.Scan(
&row.ID, &row.WorkflowID, &row.WorkflowVersion,
&convID, &projectID, &roleID, &row.Status,
&inputJSON, &outputJSON, &errText,
&pendingNode, &pendingJSON,
&row.StartedAt, &finishedAt,
); err != nil {
return nil, err
}
row.ConversationID = convID.String
row.ProjectID = projectID.String
row.RoleID = roleID.String
row.InputJSON = inputJSON.String
row.OutputJSON = outputJSON.String
row.Error = errText.String
row.PendingHITLNodeID = pendingNode.String
row.PendingHITLJSON = pendingJSON.String
if finishedAt.Valid {
t := finishedAt.Time
row.FinishedAt = &t
}
return &row, nil
}
const workflowRunColumns = `id, workflow_id, workflow_version, conversation_id, project_id, role_id, status, input_json, output_json, error, pending_hitl_node_id, pending_hitl_json, started_at, finished_at`
func (db *DB) GetWorkflowRun(runID string) (*WorkflowRun, error) {
runID = strings.TrimSpace(runID)
if runID == "" {
return nil, nil
}
row, err := scanWorkflowRun(db.QueryRow("SELECT "+workflowRunColumns+" FROM workflow_runs WHERE id = ?", runID))
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("查询工作流运行失败: %w", err)
}
return row, nil
}
func (db *DB) SetWorkflowRunStatus(runID, status string) error {
runID = strings.TrimSpace(runID)
if runID == "" {
return fmt.Errorf("工作流运行 id 不能为空")
}
_, err := db.Exec(`UPDATE workflow_runs SET status = ? WHERE id = ?`, strings.TrimSpace(status), runID)
if err != nil {
return fmt.Errorf("更新工作流运行状态失败: %w", err)
}
return nil
}
func (db *DB) SetWorkflowRunAwaitingHITL(runID, nodeID, pendingJSON string) error {
runID = strings.TrimSpace(runID)
if runID == "" {
return fmt.Errorf("工作流运行 id 不能为空")
}
_, err := db.Exec(
`UPDATE workflow_runs SET status = 'awaiting_hitl', pending_hitl_node_id = ?, pending_hitl_json = ?, finished_at = NULL WHERE id = ?`,
strings.TrimSpace(nodeID), pendingJSON, runID,
)
if err != nil {
return fmt.Errorf("更新工作流 HITL 等待状态失败: %w", err)
}
return nil
}
// RecordWorkflowRunHITLDecision stores a human decision on a paused workflow run.
func (db *DB) RecordWorkflowRunHITLDecision(runID string, approved bool, comment string) error {
runID = strings.TrimSpace(runID)
if runID == "" {
return fmt.Errorf("工作流运行 id 不能为空")
}
run, err := db.GetWorkflowRun(runID)
if err != nil {
return err
}
if run == nil {
return fmt.Errorf("工作流运行不存在")
}
pending := map[string]interface{}{}
if strings.TrimSpace(run.PendingHITLJSON) != "" {
_ = json.Unmarshal([]byte(run.PendingHITLJSON), &pending)
}
if approved {
pending["decision"] = "approved"
} else {
pending["decision"] = "rejected"
}
pending["comment"] = strings.TrimSpace(comment)
raw, _ := json.Marshal(pending)
_, err = db.Exec(
`UPDATE workflow_runs SET pending_hitl_json = ? WHERE id = ? AND status = 'awaiting_hitl'`,
string(raw), runID,
)
if err != nil {
return fmt.Errorf("记录工作流审批决定失败: %w", err)
}
return nil
}
func (db *DB) ListWorkflowRunsAwaitingHITL(limit int) ([]*WorkflowRun, error) {
return db.ListWorkflowRunsAwaitingHITLFiltered("", limit)
}
// ListWorkflowRunsAwaitingHITLFiltered returns awaiting_hitl runs, optionally scoped to a conversation.
func (db *DB) ListWorkflowRunsAwaitingHITLFiltered(conversationID string, limit int) ([]*WorkflowRun, error) {
if limit <= 0 {
limit = 50
}
conversationID = strings.TrimSpace(conversationID)
var rows *sql.Rows
var err error
if conversationID != "" {
rows, err = db.Query(
`SELECT `+workflowRunColumns+` FROM workflow_runs WHERE status = 'awaiting_hitl' AND conversation_id = ? ORDER BY started_at DESC LIMIT ?`,
conversationID, limit,
)
} else {
rows, err = db.Query(
`SELECT `+workflowRunColumns+` FROM workflow_runs WHERE status = 'awaiting_hitl' ORDER BY started_at DESC LIMIT ?`,
limit,
)
}
if err != nil {
return nil, fmt.Errorf("查询等待审批的工作流运行失败: %w", err)
}
defer rows.Close()
var out []*WorkflowRun
for rows.Next() {
row, err := scanWorkflowRun(rows)
if err != nil {
return nil, err
}
out = append(out, row)
}
return out, rows.Err()
}
func (db *DB) migrateWorkflowRunsTable() error {
cols := []struct{ name, ddl string }{
{"pending_hitl_node_id", "ALTER TABLE workflow_runs ADD COLUMN pending_hitl_node_id TEXT"},
{"pending_hitl_json", "ALTER TABLE workflow_runs ADD COLUMN pending_hitl_json TEXT"},
}
for _, col := range cols {
var count int
err := db.QueryRow("SELECT COUNT(*) FROM pragma_table_info('workflow_runs') WHERE name=?", col.name).Scan(&count)
if err != nil || count > 0 {
continue
}
if _, err := db.Exec(col.ddl); err != nil {
errMsg := strings.ToLower(err.Error())
if !strings.Contains(errMsg, "duplicate column") && !strings.Contains(errMsg, "already exists") {
return err
}
}
}
return nil
}
func nullString(v string) interface{} {
v = strings.TrimSpace(v)
if v == "" {
return nil
}
return v
}
+20 -2
View File
@@ -185,8 +185,9 @@ type AgentHandler struct {
agentsMarkdownDir string // 多代理:Markdown 子 Agent 目录(绝对路径,空则不从磁盘合并)
batchCronParser cron.Parser
// hitlWhitelistSaver 侧栏「应用」HITL 时将会话增量白名单合并写入 config.yaml(可选)
hitlWhitelistSaver HitlToolWhitelistSaver
hitlStrategySaver HitlAuditStrategySaver
hitlWhitelistSaver HitlToolWhitelistSaver
hitlStrategySaver HitlAuditStrategySaver
hitlDefaultReviewerSaver HitlDefaultReviewerSaver
auditLLM *openai.Client
audit *audit.Service
}
@@ -288,6 +289,23 @@ func (h *AgentHandler) SetHitlToolWhitelistSaver(s HitlToolWhitelistSaver) {
h.hitlWhitelistSaver = s
}
// HitlDefaultReviewerSaver 持久化全局默认审批方到 config.yaml。
type HitlDefaultReviewerSaver interface {
UpdateHitlDefaultReviewer(reviewer string) error
}
// SetHitlDefaultReviewerSaver 设置 HITL 默认审批方落盘。
func (h *AgentHandler) SetHitlDefaultReviewerSaver(s HitlDefaultReviewerSaver) {
h.hitlDefaultReviewerSaver = s
}
func (h *AgentHandler) hitlEffectiveDefaultReviewer() string {
if h != nil && h.config != nil {
return normalizeHitlReviewer(h.config.Hitl.EffectiveDefaultReviewer())
}
return "human"
}
// HITLNeedsToolApproval 供 C2 危险任务门控:与会话侧人机协同及免审批白名单判定一致。
func (h *AgentHandler) HITLNeedsToolApproval(conversationID, toolName string) bool {
if h == nil || h.hitlManager == nil {
+13
View File
@@ -1802,10 +1802,23 @@ func updateHitlConfig(doc *yaml.Node, cfg config.HitlConfig) {
hitlNode := ensureMap(root, "hitl")
// flow 样式 [a, b, c] 单行展示,工具多时比块序列省行数
setFlowStringSliceInMap(hitlNode, "tool_whitelist", cfg.ToolWhitelist)
setStringInMap(hitlNode, "default_reviewer", cfg.EffectiveDefaultReviewer())
setStringInMap(hitlNode, "audit_agent_prompt", cfg.AuditAgentPrompt)
setStringInMap(hitlNode, "audit_agent_prompt_review_edit", cfg.AuditAgentPromptReviewEdit)
}
// UpdateHitlDefaultReviewer 更新全局默认审批方并写入 config.yaml。
func (h *ConfigHandler) UpdateHitlDefaultReviewer(reviewer string) error {
h.mu.Lock()
defer h.mu.Unlock()
h.config.Hitl.DefaultReviewer = config.HitlConfig{DefaultReviewer: reviewer}.EffectiveDefaultReviewer()
if err := h.saveConfig(); err != nil {
return err
}
h.logger.Info("HITL 全局默认审批方已写入配置文件", zap.String("default_reviewer", h.config.Hitl.DefaultReviewer))
return nil
}
// UpdateHitlAuditAgentStrategy 更新审批/审查编辑两套审计 Agent 提示词并写入 config.yaml。
func (h *ConfigHandler) UpdateHitlAuditAgentStrategy(approvalPrompt, reviewEditPrompt string) error {
h.mu.Lock()
+6
View File
@@ -116,6 +116,9 @@ func (h *AgentHandler) EinoSingleAgentLoopStream(c *gin.Context) {
"userMessageId": prep.UserMessageID,
})
}
if h.runRoleWorkflowStreamIfBound(&req, prep, sendEvent) {
return
}
var cancelWithCause context.CancelCauseFunc
curFinalMessage := prep.FinalMessage
@@ -385,6 +388,9 @@ func (h *AgentHandler) EinoSingleAgentLoop(c *gin.Context) {
if h.hitlManager != nil {
defer h.hitlManager.DeactivateConversation(prep.ConversationID)
}
if h.runRoleWorkflowJSONIfBound(c, &req, prep) {
return
}
var progressBuf strings.Builder
progressCallbackRaw := func(eventType, message string, data interface{}) {
+78 -3
View File
@@ -389,6 +389,18 @@ func (m *HITLManager) LoadConversationConfig(conversationID string) (*HITLReques
}, nil
}
func (m *HITLManager) HasConversationConfig(conversationID string) (bool, error) {
if strings.TrimSpace(conversationID) == "" {
return false, nil
}
var one int
err := m.db.QueryRow(`SELECT 1 FROM hitl_conversation_configs WHERE conversation_id = ? LIMIT 1`, conversationID).Scan(&one)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return err == nil, err
}
func (m *HITLManager) waitDecision(ctx context.Context, p *pendingInterrupt, timeout time.Duration) (hitlDecision, error) {
defer func() {
m.mu.Lock()
@@ -427,14 +439,32 @@ func (h *AgentHandler) activateHITLForConversation(conversationID string, req *H
return
}
if req == nil {
cfg, err := h.hitlManager.LoadConversationConfig(conversationID)
cfg, err := h.loadHITLConversationConfig(conversationID)
if err == nil {
req = cfg
}
}
if req != nil && strings.TrimSpace(req.Reviewer) == "" {
req.Reviewer = h.hitlEffectiveDefaultReviewer()
}
h.hitlManager.ActivateConversation(conversationID, h.hitlRequestWithMergedConfigWhitelist(req))
}
func (h *AgentHandler) loadHITLConversationConfig(conversationID string) (*HITLRequest, error) {
cfg, err := h.hitlManager.LoadConversationConfig(conversationID)
if err != nil {
return nil, err
}
has, err := h.hitlManager.HasConversationConfig(conversationID)
if err != nil {
return nil, err
}
if !has {
cfg.Reviewer = h.hitlEffectiveDefaultReviewer()
}
return cfg, nil
}
func (h *AgentHandler) waitHITLApproval(runCtx context.Context, cancelRun context.CancelCauseFunc, conversationID, assistantMessageID, toolName, toolCallID string, payload map[string]interface{}, sendEventFunc func(eventType, message string, data interface{})) (*hitlDecision, error) {
cfg, need := h.hitlManager.shouldInterrupt(conversationID, toolName)
if !need {
@@ -710,7 +740,7 @@ func (h *AgentHandler) GetHITLConversationConfig(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "conversationId is required"})
return
}
cfg, err := h.hitlManager.LoadConversationConfig(conversationID)
cfg, err := h.loadHITLConversationConfig(conversationID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -729,6 +759,7 @@ func (h *AgentHandler) GetHITLConversationConfig(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"conversationId": conversationID,
"hitl": cfg,
"defaultReviewer": h.hitlEffectiveDefaultReviewer(),
"hitlGlobalToolWhitelist": h.hitlConfigGlobalToolWhitelist(),
})
}
@@ -741,6 +772,9 @@ func (h *AgentHandler) UpsertHITLConversationConfig(c *gin.Context) {
}
req.Mode = normalizeHitlMode(req.Mode)
req.Reviewer = normalizeHitlReviewer(req.Reviewer)
if strings.TrimSpace(req.Reviewer) == "" {
req.Reviewer = h.hitlEffectiveDefaultReviewer()
}
if err := h.hitlManager.SaveConversationConfig(req.ConversationID, &req.HITLRequest); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -769,7 +803,48 @@ type setHitlGlobalWhitelistReq struct {
// GetHITLGlobalToolWhitelist 返回 config.yaml 中的全局免审批工具白名单。
func (h *AgentHandler) GetHITLGlobalToolWhitelist(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"toolWhitelist": h.hitlConfigGlobalToolWhitelist(),
"toolWhitelist": h.hitlConfigGlobalToolWhitelist(),
"defaultReviewer": h.hitlEffectiveDefaultReviewer(),
})
}
type setHitlDefaultReviewerReq struct {
Reviewer string `json:"reviewer"`
}
// GetHITLDefaultReviewer 返回 config.yaml 中的全局默认审批方。
func (h *AgentHandler) GetHITLDefaultReviewer(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"defaultReviewer": h.hitlEffectiveDefaultReviewer(),
})
}
// UpdateHITLDefaultReviewer 将全局默认审批方写入 config.yaml(未选会话时切换审批方)。
func (h *AgentHandler) UpdateHITLDefaultReviewer(c *gin.Context) {
if h.hitlDefaultReviewerSaver == nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "HITL 配置持久化不可用"})
return
}
var req setHitlDefaultReviewerReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
reviewer := normalizeHitlReviewer(req.Reviewer)
if err := h.hitlDefaultReviewerSaver.UpdateHitlDefaultReviewer(reviewer); err != nil {
h.logger.Warn("写入 HITL 默认审批方到 config.yaml 失败", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if h.config != nil {
h.config.Hitl.DefaultReviewer = reviewer
}
if h.audit != nil {
h.audit.RecordOK(c, "hitl", "default_reviewer_update", "HITL 全局默认审批方更新", "hitl_config", "default_reviewer", nil)
}
c.JSON(http.StatusOK, gin.H{
"ok": true,
"defaultReviewer": reviewer,
})
}
+6
View File
@@ -133,6 +133,9 @@ func (h *AgentHandler) MultiAgentLoopStream(c *gin.Context) {
"userMessageId": prep.UserMessageID,
})
}
if h.runRoleWorkflowStreamIfBound(&req, prep, sendEvent) {
return
}
var cancelWithCause context.CancelCauseFunc
curFinalMessage := prep.FinalMessage
@@ -407,6 +410,9 @@ func (h *AgentHandler) MultiAgentLoop(c *gin.Context) {
if h.hitlManager != nil {
defer h.hitlManager.DeactivateConversation(prep.ConversationID)
}
if h.runRoleWorkflowJSONIfBound(c, &req, prep) {
return
}
baseCtx, cancelWithCause := context.WithCancelCause(c.Request.Context())
defer cancelWithCause(nil)
+23 -23
View File
@@ -506,7 +506,7 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
},
"CreateVulnerabilityRequest": map[string]interface{}{
"type": "object",
"required": []string{"conversation_id", "title", "severity"},
"required": []string{"conversation_id", "title", "description", "severity", "type", "target", "reproduction_steps", "evidence", "impact", "recommendation"},
"properties": map[string]interface{}{
"conversation_id": map[string]interface{}{
"type": "string",
@@ -538,10 +538,9 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
"type": "string",
"description": "受影响的目标",
},
"proof": map[string]interface{}{
"type": "string",
"description": "漏洞证明",
},
"preconditions": map[string]interface{}{"type": "string", "description": "前置条件"},
"reproduction_steps": map[string]interface{}{"type": "string", "description": "复现步骤"},
"evidence": map[string]interface{}{"type": "string", "description": "证据/POC,包含请求响应、命令输出、截图说明、日志等"},
"impact": map[string]interface{}{
"type": "string",
"description": "影响",
@@ -550,6 +549,7 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
"type": "string",
"description": "修复建议",
},
"retest_notes": map[string]interface{}{"type": "string", "description": "复测方式"},
},
},
"UpdateVulnerabilityRequest": map[string]interface{}{
@@ -581,10 +581,9 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
"type": "string",
"description": "受影响的目标",
},
"proof": map[string]interface{}{
"type": "string",
"description": "漏洞证明",
},
"preconditions": map[string]interface{}{"type": "string", "description": "前置条件"},
"reproduction_steps": map[string]interface{}{"type": "string", "description": "复现步骤"},
"evidence": map[string]interface{}{"type": "string", "description": "证据/POC,包含请求响应、命令输出、截图说明、日志等"},
"impact": map[string]interface{}{
"type": "string",
"description": "影响",
@@ -593,6 +592,7 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
"type": "string",
"description": "修复建议",
},
"retest_notes": map[string]interface{}{"type": "string", "description": "复测方式"},
},
},
"ListVulnerabilitiesResponse": map[string]interface{}{
@@ -805,18 +805,18 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
"type": "object",
"description": "视觉分析(analyze_image MCP 工具);enabled 且 model 非空时注册工具",
"properties": map[string]interface{}{
"enabled": map[string]interface{}{"type": "boolean", "description": "是否启用 analyze_image"},
"model": map[string]interface{}{"type": "string", "description": "视觉模型名(必填)", "example": "qwen-vl-max"},
"api_key": map[string]interface{}{"type": "string", "description": "API Key;留空复用 openai.api_key"},
"base_url": map[string]interface{}{"type": "string", "description": "Base URL;留空复用 openai.base_url"},
"provider": map[string]interface{}{"type": "string", "description": "提供商;留空复用 openai.provider"},
"timeout_seconds": map[string]interface{}{"type": "integer", "description": "VL 调用超时(秒)"},
"max_image_bytes": map[string]interface{}{"type": "integer", "description": "原始文件大小上限(字节)"},
"max_dimension": map[string]interface{}{"type": "integer", "description": "长边缩放像素"},
"jpeg_quality": map[string]interface{}{"type": "integer", "description": "JPEG 质量 60-100"},
"max_payload_bytes": map[string]interface{}{"type": "integer", "description": "送 API 体积上限(字节)"},
"skip_preprocess_below_bytes": map[string]interface{}{"type": "integer", "description": "低于该字节且尺寸合规时可原图直传;0=始终压缩"},
"detail": map[string]interface{}{"type": "string", "enum": []string{"low", "high", "auto"}, "description": "OpenAI 兼容 image detail"},
"enabled": map[string]interface{}{"type": "boolean", "description": "是否启用 analyze_image"},
"model": map[string]interface{}{"type": "string", "description": "视觉模型名(必填)", "example": "qwen-vl-max"},
"api_key": map[string]interface{}{"type": "string", "description": "API Key;留空复用 openai.api_key"},
"base_url": map[string]interface{}{"type": "string", "description": "Base URL;留空复用 openai.base_url"},
"provider": map[string]interface{}{"type": "string", "description": "提供商;留空复用 openai.provider"},
"timeout_seconds": map[string]interface{}{"type": "integer", "description": "VL 调用超时(秒)"},
"max_image_bytes": map[string]interface{}{"type": "integer", "description": "原始文件大小上限(字节)"},
"max_dimension": map[string]interface{}{"type": "integer", "description": "长边缩放像素"},
"jpeg_quality": map[string]interface{}{"type": "integer", "description": "JPEG 质量 60-100"},
"max_payload_bytes": map[string]interface{}{"type": "integer", "description": "送 API 体积上限(字节)"},
"skip_preprocess_below_bytes": map[string]interface{}{"type": "integer", "description": "低于该字节且尺寸合规时可原图直传;0=始终压缩"},
"detail": map[string]interface{}{"type": "string", "enum": []string{"low", "high", "auto"}, "description": "OpenAI 兼容 image detail"},
},
},
"AnalyzeImageToolCall": map[string]interface{}{
@@ -1432,7 +1432,7 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
{
"name": "id", "in": "path", "required": true,
"description": "对话ID",
"schema": map[string]interface{}{"type": "string"},
"schema": map[string]interface{}{"type": "string"},
},
},
"requestBody": map[string]interface{}{
@@ -2570,7 +2570,7 @@ func (h *OpenAPIHandler) GetOpenAPISpec(c *gin.Context) {
"content": map[string]interface{}{
"application/json": map[string]interface{}{
"schema": map[string]interface{}{
"type": "object",
"type": "object",
"required": []string{"source_fact_key", "target_fact_key", "edge_type"},
"properties": map[string]interface{}{
"source_fact_key": map[string]interface{}{"type": "string"},
+72 -39
View File
@@ -45,9 +45,12 @@ type CreateVulnerabilityRequest struct {
Status string `json:"status"`
Type string `json:"type"`
Target string `json:"target"`
Proof string `json:"proof"`
Preconditions string `json:"preconditions"`
ReproSteps string `json:"reproduction_steps"`
Evidence string `json:"evidence"`
Impact string `json:"impact"`
Recommendation string `json:"recommendation"`
RetestNotes string `json:"retest_notes"`
}
// CreateVulnerability 创建漏洞
@@ -69,9 +72,12 @@ func (h *VulnerabilityHandler) CreateVulnerability(c *gin.Context) {
Status: req.Status,
Type: req.Type,
Target: req.Target,
Proof: req.Proof,
Preconditions: req.Preconditions,
ReproSteps: req.ReproSteps,
Evidence: req.Evidence,
Impact: req.Impact,
Recommendation: req.Recommendation,
RetestNotes: req.RetestNotes,
}
created, err := h.db.CreateVulnerability(vuln)
@@ -118,7 +124,7 @@ func parseVulnerabilityListFilter(c *gin.Context) database.VulnerabilityListFilt
q = strings.TrimSpace(c.Query("search"))
}
return database.VulnerabilityListFilter{
ProjectID: c.Query("project_id"),
ProjectID: c.Query("project_id"),
ID: c.Query("id"),
Search: q,
ConversationID: c.Query("conversation_id"),
@@ -197,17 +203,20 @@ func (h *VulnerabilityHandler) ListVulnerabilities(c *gin.Context) {
// UpdateVulnerabilityRequest 更新漏洞请求
type UpdateVulnerabilityRequest struct {
ProjectID *string `json:"project_id"`
ConversationTag string `json:"conversation_tag"`
TaskTag string `json:"task_tag"`
Title string `json:"title"`
Description string `json:"description"`
Severity string `json:"severity"`
Status string `json:"status"`
Type string `json:"type"`
Target string `json:"target"`
Proof string `json:"proof"`
Impact string `json:"impact"`
Recommendation string `json:"recommendation"`
ConversationTag *string `json:"conversation_tag"`
TaskTag *string `json:"task_tag"`
Title *string `json:"title"`
Description *string `json:"description"`
Severity *string `json:"severity"`
Status *string `json:"status"`
Type *string `json:"type"`
Target *string `json:"target"`
Preconditions *string `json:"preconditions"`
ReproSteps *string `json:"reproduction_steps"`
Evidence *string `json:"evidence"`
Impact *string `json:"impact"`
Recommendation *string `json:"recommendation"`
RetestNotes *string `json:"retest_notes"`
}
// UpdateVulnerability 更新漏洞
@@ -231,38 +240,47 @@ func (h *VulnerabilityHandler) UpdateVulnerability(c *gin.Context) {
if req.ProjectID != nil {
existing.ProjectID = strings.TrimSpace(*req.ProjectID)
}
if req.ConversationTag != "" {
existing.ConversationTag = req.ConversationTag
if req.ConversationTag != nil {
existing.ConversationTag = *req.ConversationTag
}
if req.TaskTag != "" {
existing.TaskTag = req.TaskTag
if req.TaskTag != nil {
existing.TaskTag = *req.TaskTag
}
if req.Title != "" {
existing.Title = req.Title
if req.Title != nil {
existing.Title = *req.Title
}
if req.Description != "" {
existing.Description = req.Description
if req.Description != nil {
existing.Description = *req.Description
}
if req.Severity != "" {
existing.Severity = req.Severity
if req.Severity != nil {
existing.Severity = *req.Severity
}
if req.Status != "" {
existing.Status = req.Status
if req.Status != nil {
existing.Status = *req.Status
}
if req.Type != "" {
existing.Type = req.Type
if req.Type != nil {
existing.Type = *req.Type
}
if req.Target != "" {
existing.Target = req.Target
if req.Target != nil {
existing.Target = *req.Target
}
if req.Proof != "" {
existing.Proof = req.Proof
if req.Preconditions != nil {
existing.Preconditions = *req.Preconditions
}
if req.Impact != "" {
existing.Impact = req.Impact
if req.ReproSteps != nil {
existing.ReproSteps = *req.ReproSteps
}
if req.Recommendation != "" {
existing.Recommendation = req.Recommendation
if req.Evidence != nil {
existing.Evidence = *req.Evidence
}
if req.Impact != nil {
existing.Impact = *req.Impact
}
if req.Recommendation != nil {
existing.Recommendation = *req.Recommendation
}
if req.RetestNotes != nil {
existing.RetestNotes = *req.RetestNotes
}
if err := h.db.UpdateVulnerability(id, existing); err != nil {
@@ -495,9 +513,19 @@ func appendVulnerabilityMarkdown(b *strings.Builder, v *database.Vulnerability,
b.WriteString(v.Description)
b.WriteString("\n")
}
if v.Proof != "" {
b.WriteString("\n#### 证明(POC\n\n```\n")
b.WriteString(v.Proof)
if v.Preconditions != "" {
b.WriteString("\n#### 前置条件\n\n")
b.WriteString(v.Preconditions)
b.WriteString("\n")
}
if v.ReproSteps != "" {
b.WriteString("\n#### 复现步骤\n\n")
b.WriteString(v.ReproSteps)
b.WriteString("\n")
}
if v.Evidence != "" {
b.WriteString("\n#### 证据 / POC\n\n```\n")
b.WriteString(v.Evidence)
b.WriteString("\n```\n")
}
if v.Impact != "" {
@@ -510,6 +538,11 @@ func appendVulnerabilityMarkdown(b *strings.Builder, v *database.Vulnerability,
b.WriteString(v.Recommendation)
b.WriteString("\n")
}
if v.RetestNotes != "" {
b.WriteString("\n#### 复测方式\n\n")
b.WriteString(v.RetestNotes)
b.WriteString("\n")
}
b.WriteString("\n")
}
+153
View File
@@ -0,0 +1,153 @@
package handler
import (
"encoding/json"
"net/http"
"strings"
"cyberstrike-ai/internal/agent"
"cyberstrike-ai/internal/audit"
"cyberstrike-ai/internal/config"
"cyberstrike-ai/internal/database"
workflowrunner "cyberstrike-ai/internal/workflow"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type WorkflowHandler struct {
db *database.DB
logger *zap.Logger
audit *audit.Service
agent *agent.Agent
cfg *config.Config
}
func NewWorkflowHandler(db *database.DB, logger *zap.Logger) *WorkflowHandler {
return &WorkflowHandler{db: db, logger: logger}
}
func (h *WorkflowHandler) SetAudit(s *audit.Service) {
h.audit = s
}
type workflowSaveRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Version int `json:"version,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Graph json.RawMessage `json:"graph,omitempty"`
GraphJSON json.RawMessage `json:"graph_json,omitempty"`
}
func (h *WorkflowHandler) List(c *gin.Context) {
includeDisabled := strings.EqualFold(c.Query("includeDisabled"), "true") || c.Query("include_disabled") == "1"
items, err := h.db.ListWorkflowDefinitions(includeDisabled)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"workflows": items})
}
func (h *WorkflowHandler) Get(c *gin.Context) {
id := strings.TrimSpace(c.Param("id"))
wf, err := h.db.GetWorkflowDefinition(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if wf == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "工作流不存在"})
return
}
c.JSON(http.StatusOK, gin.H{"workflow": wf})
}
func (h *WorkflowHandler) Create(c *gin.Context) {
h.save(c, "")
}
func (h *WorkflowHandler) Update(c *gin.Context) {
h.save(c, c.Param("id"))
}
func (h *WorkflowHandler) save(c *gin.Context, pathID string) {
var req workflowSaveRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求参数: " + err.Error()})
return
}
id := strings.TrimSpace(req.ID)
if strings.TrimSpace(pathID) != "" {
id = strings.TrimSpace(pathID)
}
name := strings.TrimSpace(req.Name)
if id == "" || name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "工作流 id 和 name 不能为空"})
return
}
graph := req.Graph
if len(graph) == 0 {
graph = req.GraphJSON
}
if len(graph) == 0 {
graph = []byte(`{"nodes":[],"edges":[],"config":{}}`)
}
if !json.Valid(graph) {
c.JSON(http.StatusBadRequest, gin.H{"error": "graph 必须是合法 JSON"})
return
}
if err := workflowrunner.ValidateGraphJSON(c.Request.Context(), string(graph)); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "工作流图无法编译: " + err.Error()})
return
}
var probe interface{}
if err := json.Unmarshal(graph, &probe); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "graph JSON 解析失败: " + err.Error()})
return
}
enabled := true
if req.Enabled != nil {
enabled = *req.Enabled
}
wf := &database.WorkflowDefinition{
ID: id,
Name: name,
Description: strings.TrimSpace(req.Description),
Version: req.Version,
GraphJSON: string(graph),
Enabled: enabled,
}
if err := h.db.UpsertWorkflowDefinition(wf); err != nil {
if h.logger != nil {
h.logger.Warn("保存工作流失败", zap.String("id", id), zap.Error(err))
}
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
saved, _ := h.db.GetWorkflowDefinition(id)
workflowrunner.InvalidateCompiledCache(id)
if h.audit != nil {
h.audit.RecordOK(c, "workflow", "save", "保存图编排流程", "workflow", id, map[string]interface{}{"name": name})
}
c.JSON(http.StatusOK, gin.H{"message": "工作流已保存", "workflow": saved})
}
func (h *WorkflowHandler) Delete(c *gin.Context) {
id := strings.TrimSpace(c.Param("id"))
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "工作流 id 不能为空"})
return
}
if err := h.db.DeleteWorkflowDefinition(id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
workflowrunner.InvalidateCompiledCache(id)
if h.audit != nil {
h.audit.RecordOK(c, "workflow", "delete", "删除图编排流程", "workflow", id, nil)
}
c.JSON(http.StatusOK, gin.H{"message": "工作流已删除"})
}
+263
View File
@@ -0,0 +1,263 @@
package handler
import (
"context"
"errors"
"net/http"
"strings"
"time"
"cyberstrike-ai/internal/config"
workflowrunner "cyberstrike-ai/internal/workflow"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
func (h *AgentHandler) roleForWorkflow(req *ChatRequest) (config.RoleConfig, bool) {
if h == nil || h.config == nil || h.config.Roles == nil || req == nil {
return config.RoleConfig{}, false
}
roleName := strings.TrimSpace(req.Role)
if roleName == "" {
return config.RoleConfig{}, false
}
role, ok := h.config.Roles[roleName]
if !ok || !role.Enabled {
return config.RoleConfig{}, false
}
if role.Name == "" {
role.Name = roleName
}
if !workflowrunner.ShouldAutoRunRoleWorkflow(role) {
return config.RoleConfig{}, false
}
return role, true
}
func (h *AgentHandler) runRoleWorkflowStreamIfBound(
req *ChatRequest,
prep *multiAgentPrepared,
sendEvent func(eventType, message string, data interface{}),
) bool {
role, ok := h.roleForWorkflow(req)
if !ok || prep == nil {
return false
}
conversationID := prep.ConversationID
assistantMessageID := prep.AssistantMessageID
userMessage := ""
if req != nil {
userMessage = req.Message
}
taskStatus := "completed"
taskOwned := false
defer func() {
if taskOwned {
h.tasks.FinishTask(conversationID, taskStatus)
}
}()
baseCtx, cancelWithCause := context.WithCancelCause(context.Background())
defer cancelWithCause(nil)
taskCtx, timeoutCancel := context.WithTimeout(baseCtx, 600*time.Minute)
defer timeoutCancel()
if _, err := h.tasks.StartTask(conversationID, userMessage, cancelWithCause); err != nil {
var errorMsg string
if errors.Is(err, ErrTaskAlreadyRunning) {
errorMsg = "⚠️ 当前会话已有任务正在执行中,请等待当前任务完成或点击「停止任务」后再尝试。"
sendEvent("error", errorMsg, map[string]interface{}{
"conversationId": conversationID,
"errorType": "task_already_running",
})
} else {
errorMsg = "❌ 无法启动任务: " + err.Error()
sendEvent("error", errorMsg, nil)
}
if assistantMessageID != "" {
_, _ = h.db.Exec("UPDATE messages SET content = ?, updated_at = ? WHERE id = ?", errorMsg, time.Now(), assistantMessageID)
}
sendEvent("done", "", map[string]interface{}{"conversationId": conversationID})
return true
}
taskOwned = true
progress := h.createProgressCallback(taskCtx, cancelWithCause, conversationID, assistantMessageID, sendEvent)
result, err := workflowrunner.RunRoleBoundWorkflow(taskCtx, workflowrunner.RunArgs{
DB: h.db,
Logger: h.logger,
Role: role,
AppCfg: h.config,
Agent: h.agent,
ConversationID: conversationID,
ProjectID: h.conversationProjectID(conversationID),
UserMessage: prep.FinalMessage,
History: prep.History,
RoleTools: prep.RoleTools,
AgentsMarkdownDir: h.agentsMarkdownDir,
SystemPromptExtra: h.agentSessionContextBlock(conversationID),
AssistantMessageID: assistantMessageID,
Progress: progress,
})
if err != nil {
cause := context.Cause(baseCtx)
if errors.Is(cause, ErrTaskCancelled) {
taskStatus = "cancelled"
h.tasks.UpdateTaskStatus(conversationID, taskStatus)
cancelMsg := "任务已被用户取消,后续操作已停止。"
if assistantMessageID != "" {
if err := h.appendAssistantMessageNotice(assistantMessageID, cancelMsg); err != nil {
h.logger.Warn("更新取消后的助手消息失败", zap.Error(err))
}
_ = h.db.AddProcessDetail(assistantMessageID, conversationID, "cancelled", cancelMsg, nil)
}
sendEvent("cancelled", cancelMsg, map[string]interface{}{
"conversationId": conversationID,
"messageId": assistantMessageID,
})
sendEvent("done", "", map[string]interface{}{"conversationId": conversationID})
return true
}
if errors.Is(err, context.DeadlineExceeded) || errors.Is(context.Cause(taskCtx), context.DeadlineExceeded) {
taskStatus = "timeout"
h.tasks.UpdateTaskStatus(conversationID, taskStatus)
timeoutMsg := "任务执行超时,已自动终止。"
if assistantMessageID != "" {
_, _ = h.db.Exec("UPDATE messages SET content = ?, updated_at = ? WHERE id = ?", timeoutMsg, time.Now(), assistantMessageID)
_ = h.db.AddProcessDetail(assistantMessageID, conversationID, "timeout", timeoutMsg, nil)
}
sendEvent("error", timeoutMsg, map[string]interface{}{
"conversationId": conversationID,
"messageId": assistantMessageID,
"errorType": "timeout",
})
sendEvent("done", "", map[string]interface{}{"conversationId": conversationID})
return true
}
errMsg := "执行角色绑定流程失败: " + err.Error()
taskStatus = "failed"
h.tasks.UpdateTaskStatus(conversationID, taskStatus)
if assistantMessageID != "" {
_, _ = h.db.Exec("UPDATE messages SET content = ?, updated_at = ? WHERE id = ?", errMsg, time.Now(), assistantMessageID)
_ = h.db.AddProcessDetail(assistantMessageID, conversationID, "error", errMsg, nil)
}
sendEvent("error", errMsg, map[string]interface{}{"conversationId": conversationID})
sendEvent("done", "", map[string]interface{}{"conversationId": conversationID})
return true
}
if prep.AssistantMessageID != "" {
_ = h.db.UpdateAssistantMessageFinalize(prep.AssistantMessageID, result.Response, nil, "")
}
payload := map[string]interface{}{
"conversationId": prep.ConversationID,
"messageId": prep.AssistantMessageID,
"agentMode": "workflow",
"workflowRunId": result.RunID,
}
if result.AwaitingHITL {
payload["workflowStatus"] = "awaiting_hitl"
payload["awaitingHitl"] = true
}
sendEvent("response", result.Response, payload)
sendEvent("done", "", map[string]interface{}{"conversationId": prep.ConversationID})
return true
}
func (h *AgentHandler) runRoleWorkflowJSONIfBound(c *gin.Context, req *ChatRequest, prep *multiAgentPrepared) bool {
role, ok := h.roleForWorkflow(req)
if !ok || prep == nil {
return false
}
conversationID := prep.ConversationID
assistantMessageID := prep.AssistantMessageID
userMessage := ""
if req != nil {
userMessage = req.Message
}
taskStatus := "completed"
taskOwned := false
defer func() {
if taskOwned {
h.tasks.FinishTask(conversationID, taskStatus)
}
}()
baseCtx, cancelWithCause := context.WithCancelCause(c.Request.Context())
defer cancelWithCause(nil)
taskCtx, timeoutCancel := context.WithTimeout(baseCtx, 600*time.Minute)
defer timeoutCancel()
if _, err := h.tasks.StartTask(conversationID, userMessage, cancelWithCause); err != nil {
if errors.Is(err, ErrTaskAlreadyRunning) {
c.JSON(http.StatusConflict, gin.H{
"error": "⚠️ 当前会话已有任务正在执行中,请等待当前任务完成或点击「停止任务」后再尝试。",
"conversationId": conversationID,
"errorType": "task_already_running",
})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "❌ 无法启动任务: " + err.Error()})
}
return true
}
taskOwned = true
progress := h.createProgressCallback(taskCtx, cancelWithCause, conversationID, assistantMessageID, nil)
result, err := workflowrunner.RunRoleBoundWorkflow(taskCtx, workflowrunner.RunArgs{
DB: h.db,
Logger: h.logger,
Role: role,
AppCfg: h.config,
Agent: h.agent,
ConversationID: conversationID,
ProjectID: h.conversationProjectID(conversationID),
UserMessage: prep.FinalMessage,
History: prep.History,
RoleTools: prep.RoleTools,
AgentsMarkdownDir: h.agentsMarkdownDir,
SystemPromptExtra: h.agentSessionContextBlock(conversationID),
AssistantMessageID: assistantMessageID,
Progress: progress,
})
if err != nil {
cause := context.Cause(baseCtx)
if errors.Is(cause, ErrTaskCancelled) {
taskStatus = "cancelled"
cancelMsg := "任务已被用户取消,后续操作已停止。"
if assistantMessageID != "" {
_ = h.appendAssistantMessageNotice(assistantMessageID, cancelMsg)
_ = h.db.AddProcessDetail(assistantMessageID, conversationID, "cancelled", cancelMsg, nil)
}
c.JSON(http.StatusOK, gin.H{
"status": "cancelled",
"message": cancelMsg,
"conversationId": conversationID,
})
return true
}
errMsg := "执行角色绑定流程失败: " + err.Error()
taskStatus = "failed"
if assistantMessageID != "" {
_, _ = h.db.Exec("UPDATE messages SET content = ?, updated_at = ? WHERE id = ?", errMsg, time.Now(), assistantMessageID)
}
c.JSON(http.StatusInternalServerError, gin.H{"error": errMsg, "conversationId": conversationID})
return true
}
if prep.AssistantMessageID != "" {
_ = h.db.UpdateAssistantMessageFinalize(prep.AssistantMessageID, result.Response, nil, "")
}
c.JSON(http.StatusOK, gin.H{
"response": result.Response,
"conversationId": prep.ConversationID,
"assistantMessageId": prep.AssistantMessageID,
"agentMode": "workflow",
"workflowRunId": result.RunID,
"workflowStatus": result.Status,
"awaitingHitl": result.AwaitingHITL,
})
return true
}
+128
View File
@@ -0,0 +1,128 @@
package handler
import (
"net/http"
"strings"
"time"
"cyberstrike-ai/internal/agent"
"cyberstrike-ai/internal/config"
workflowrunner "cyberstrike-ai/internal/workflow"
"github.com/gin-gonic/gin"
)
func (h *WorkflowHandler) SetRuntime(agent *agent.Agent, cfg *config.Config) {
h.agent = agent
h.cfg = cfg
}
func (h *WorkflowHandler) GetRun(c *gin.Context) {
runID := strings.TrimSpace(c.Param("runId"))
run, err := h.db.GetWorkflowRun(runID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if run == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "工作流运行不存在"})
return
}
c.JSON(http.StatusOK, gin.H{"run": run})
}
func (h *WorkflowHandler) ListPendingRuns(c *gin.Context) {
conversationID := strings.TrimSpace(c.Query("conversationId"))
runs, err := h.db.ListWorkflowRunsAwaitingHITLFiltered(conversationID, 50)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"runs": runs})
}
type workflowResumeRequest struct {
Approved bool `json:"approved"`
Comment string `json:"comment,omitempty"`
}
func (h *WorkflowHandler) ResumeRun(c *gin.Context) {
if h.agent == nil || h.cfg == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "工作流运行时未初始化"})
return
}
runID := strings.TrimSpace(c.Param("runId"))
var req workflowResumeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的请求参数: " + err.Error()})
return
}
run, err := h.db.GetWorkflowRun(runID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if run == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "工作流运行不存在"})
return
}
role := config.RoleConfig{Name: strings.TrimSpace(run.RoleID)}
if role.Name != "" && h.cfg.Roles != nil {
if r, ok := h.cfg.Roles[role.Name]; ok {
role = r
if role.Name == "" {
role.Name = run.RoleID
}
}
}
if run.Status != "awaiting_hitl" {
c.JSON(http.StatusBadRequest, gin.H{"error": "工作流运行不在等待审批状态: " + run.Status})
return
}
if err := h.db.RecordWorkflowRunHITLDecision(runID, req.Approved, req.Comment); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
decision := workflowrunner.HITLDecision{
Approved: req.Approved,
Comment: strings.TrimSpace(req.Comment),
}
delegated := workflowrunner.NotifyHITLDecision(runID, decision)
if !delegated {
for i := 0; i < 10; i++ {
time.Sleep(50 * time.Millisecond)
if workflowrunner.NotifyHITLDecision(runID, decision) {
delegated = true
break
}
}
}
if delegated {
c.JSON(http.StatusOK, gin.H{
"workflowRunId": runID,
"status": "delegated",
"streamResuming": true,
"approved": req.Approved,
})
return
}
result, err := workflowrunner.ResumeWorkflowRun(c.Request.Context(), workflowrunner.RunArgs{
DB: h.db,
Logger: h.logger,
Role: role,
AppCfg: h.cfg,
Agent: h.agent,
ConversationID: run.ConversationID,
ProjectID: run.ProjectID,
}, runID, req.Approved, req.Comment)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{
"response": result.Response,
"workflowRunId": result.RunID,
"status": result.Status,
"awaitingHitl": result.AwaitingHITL,
})
}
+24
View File
@@ -0,0 +1,24 @@
package workflow
import (
"context"
"github.com/cloudwego/eino/compose"
)
// compileAgentSubgraph wraps an Agent canvas node as an Eino subgraph (AddGraphNode best practice).
func compileAgentSubgraph(_ context.Context, node graphNode) (compose.AnyGraph, error) {
n := node
innerID := n.ID + "__agent"
g := compose.NewGraph[WorkflowNodeOutput, WorkflowNodeOutput]()
_ = g.AddLambdaNode(innerID, compose.InvokableLambda(func(runCtx context.Context, _ WorkflowNodeOutput) (WorkflowNodeOutput, error) {
return runWorkflowNodeLambda(runCtx, n)
}))
if err := g.AddEdge(compose.START, innerID); err != nil {
return nil, err
}
if err := g.AddEdge(innerID, compose.END); err != nil {
return nil, err
}
return g, nil
}
+141
View File
@@ -0,0 +1,141 @@
package workflow
import (
"encoding/json"
"fmt"
"strings"
)
// FieldBinding selects a value from workflow state (replaces {{...}} templates).
type FieldBinding struct {
From string `json:"from"` // inputs | previous | <nodeId>
Field string `json:"field"` // e.g. output, message
}
func parseFieldBinding(cfg map[string]any, keys ...string) (FieldBinding, bool) {
for _, key := range keys {
if cfg == nil {
continue
}
raw, ok := cfg[key]
if !ok || raw == nil {
continue
}
switch v := raw.(type) {
case map[string]any:
return FieldBinding{
From: strings.TrimSpace(fmt.Sprint(v["from"])),
Field: strings.TrimSpace(fmt.Sprint(v["field"])),
}, true
case string:
s := strings.TrimSpace(v)
if s == "" {
continue
}
var b FieldBinding
if err := json.Unmarshal([]byte(s), &b); err == nil && (b.From != "" || b.Field != "") {
return b, true
}
}
}
return FieldBinding{}, false
}
func defaultBinding(from, field string) FieldBinding {
return FieldBinding{From: from, Field: field}
}
func resolveBinding(b FieldBinding, state *WorkflowLocalState) any {
from := strings.TrimSpace(b.From)
field := strings.TrimSpace(b.Field)
if field == "" {
field = "output"
}
if from == "" || from == "previous" || from == "prev" {
if field == "output" && state.LastOutput != nil {
return state.LastOutput["output"]
}
return valueFromPath("previous."+field, state)
}
if from == "inputs" || from == "input" {
if field == "" {
return state.Inputs
}
return valueFromPath("inputs."+field, state)
}
if from == "outputs" {
return valueFromPath("outputs."+field, state)
}
return valueFromPath(from+"."+field, state)
}
func resolveBindingString(b FieldBinding, state *WorkflowLocalState) string {
return strings.TrimSpace(fmt.Sprint(resolveBinding(b, state)))
}
func resolveNodeInputBinding(cfg map[string]any, state *WorkflowLocalState) string {
if b, ok := parseFieldBinding(cfg, "input_binding"); ok {
return resolveBindingString(b, state)
}
// legacy template field removed — default previous.output
return resolveBindingString(defaultBinding("previous", "output"), state)
}
func resolveOutputSourceBinding(cfg map[string]any, state *WorkflowLocalState) any {
if b, ok := parseFieldBinding(cfg, "source_binding"); ok {
return resolveBinding(b, state)
}
return resolveBinding(defaultBinding("previous", "output"), state)
}
func resolveHITLPromptBinding(cfg map[string]any, state *WorkflowLocalState) string {
if b, ok := parseFieldBinding(cfg, "prompt_binding"); ok {
return resolveBindingString(b, state)
}
if s := cfgString(cfg, "prompt"); s != "" {
return s
}
return resolveBindingString(defaultBinding("previous", "output"), state)
}
func toolArgumentBindings(cfg map[string]any) map[string]FieldBinding {
raw, ok := cfg["argument_bindings"].(map[string]any)
if !ok || len(raw) == 0 {
return nil
}
out := make(map[string]FieldBinding, len(raw))
for argName, v := range raw {
m, ok := v.(map[string]any)
if !ok {
continue
}
out[argName] = FieldBinding{
From: strings.TrimSpace(fmt.Sprint(m["from"])),
Field: strings.TrimSpace(fmt.Sprint(m["field"])),
}
}
return out
}
func resolveToolArguments(cfg map[string]any, state *WorkflowLocalState) (map[string]interface{}, error) {
bindings := toolArgumentBindings(cfg)
if len(bindings) > 0 {
args := make(map[string]interface{}, len(bindings))
for k, b := range bindings {
args[k] = resolveBinding(b, state)
}
return args, nil
}
raw := cfgString(cfg, "arguments")
if raw == "" {
return map[string]interface{}{}, nil
}
var args map[string]interface{}
if err := json.Unmarshal([]byte(raw), &args); err != nil {
return nil, err
}
if args == nil {
args = map[string]interface{}{}
}
return args, nil
}
+69
View File
@@ -0,0 +1,69 @@
package workflow
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
)
// fileCheckPointStore persists Eino workflow checkpoints on disk (per run id).
type fileCheckPointStore struct {
dir string
mu sync.RWMutex
}
func newFileCheckPointStore(dir string) (*fileCheckPointStore, error) {
dir = strings.TrimSpace(dir)
if dir == "" {
dir = filepath.Join("data", "workflow-checkpoints")
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, fmt.Errorf("create workflow checkpoint dir: %w", err)
}
return &fileCheckPointStore{dir: dir}, nil
}
func (s *fileCheckPointStore) path(id string) (string, error) {
id = strings.TrimSpace(id)
if id == "" {
return "", fmt.Errorf("checkpoint id is empty")
}
if strings.Contains(id, "..") || strings.ContainsAny(id, `/\`) {
return "", fmt.Errorf("invalid checkpoint id")
}
return filepath.Join(s.dir, id+".ckpt"), nil
}
func (s *fileCheckPointStore) Get(_ context.Context, checkPointID string) ([]byte, bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
p, err := s.path(checkPointID)
if err != nil {
return nil, false, err
}
data, err := os.ReadFile(p)
if err != nil {
if os.IsNotExist(err) {
return nil, false, nil
}
return nil, false, err
}
return data, true, nil
}
func (s *fileCheckPointStore) Set(_ context.Context, checkPointID string, checkPoint []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
p, err := s.path(checkPointID)
if err != nil {
return err
}
tmp := p + ".tmp"
if err := os.WriteFile(tmp, checkPoint, 0o600); err != nil {
return err
}
return os.Rename(tmp, p)
}
+107
View File
@@ -0,0 +1,107 @@
package workflow
import (
"context"
"fmt"
"github.com/cloudwego/eino/compose"
)
func hasConditionalOutgoingEdges(idx *graphIndex, nodeID string) bool {
for _, edge := range idx.outgoing[nodeID] {
cond := firstNonEmpty(cfgString(edge.Config, "condition"), cfgString(edge.Config, "expression"))
if cond != "" {
return true
}
}
return false
}
func wireConditionBranch(
wf *compose.Workflow[WorkflowInput, WorkflowOutput],
nodeRefs map[string]*compose.WorkflowNode,
idx *graphIndex,
condID string,
condNode graphNode,
) error {
edges := idx.outgoing[condID]
if len(edges) == 0 {
return nil
}
branchID := branchNodeID(condID)
wf.AddPassthroughNode(branchID).AddInput(condID)
endNodes := map[string]bool{compose.END: true}
for _, edge := range edges {
endNodes[edge.Target] = true
}
sortedEdges := append([]graphEdge(nil), edges...)
sortEdgesByCanvas(sortedEdges, idx.nodes)
branch := compose.NewGraphBranch(func(runCtx context.Context, _ map[string]any) (string, error) {
rt := workflowRuntimeFrom(runCtx)
if rt == nil {
return compose.END, fmt.Errorf("workflow runtime missing in context")
}
emitConditionBranchProgress(rt.args, rt.runID, condNode, sortedEdges, idx.nodes, rt.state)
for edgeIdx, edge := range sortedEdges {
if conditionBranchAllowed(edge, edgeIdx, rt.state) {
return edge.Target, nil
}
}
return compose.END, nil
}, endNodes)
wf.AddBranch(branchID, branch)
for _, edge := range edges {
if target, ok := nodeRefs[edge.Target]; ok {
target.AddInput(branchID)
}
}
return nil
}
func wireEdgeConditionBranch(
wf *compose.Workflow[WorkflowInput, WorkflowOutput],
nodeRefs map[string]*compose.WorkflowNode,
idx *graphIndex,
sourceID string,
sourceNode graphNode,
) error {
edges := idx.outgoing[sourceID]
if len(edges) == 0 {
return nil
}
branchID := edgeBranchNodeID(sourceID)
wf.AddPassthroughNode(branchID).AddInput(sourceID)
endNodes := map[string]bool{compose.END: true}
for _, edge := range edges {
endNodes[edge.Target] = true
}
sortedEdges := append([]graphEdge(nil), edges...)
sortEdgesByCanvas(sortedEdges, idx.nodes)
branch := compose.NewGraphBranch(func(runCtx context.Context, _ map[string]any) (string, error) {
rt := workflowRuntimeFrom(runCtx)
if rt == nil {
return compose.END, fmt.Errorf("workflow runtime missing in context")
}
for edgeIdx, edge := range sortedEdges {
if edgeAllowed(edge, sourceNode, edgeIdx, rt.state) {
return edge.Target, nil
}
}
return compose.END, nil
}, endNodes)
wf.AddBranch(branchID, branch)
for _, edge := range edges {
if target, ok := nodeRefs[edge.Target]; ok {
target.AddInput(branchID)
}
}
return nil
}
+22
View File
@@ -0,0 +1,22 @@
package workflow
import (
"context"
"cyberstrike-ai/internal/config"
"cyberstrike-ai/internal/einoobserve"
)
func attachWorkflowCallbacks(ctx context.Context, cfg *config.Config, args RunArgs, workflowName string) context.Context {
if cfg == nil {
return ctx
}
cbCfg := &cfg.MultiAgent.EinoCallbacks
return einoobserve.AttachAgentRunCallbacks(ctx, cbCfg, einoobserve.Params{
Logger: args.Logger,
Progress: args.Progress,
ConversationID: args.ConversationID,
OrchMode: "workflow",
OrchestratorName: workflowName,
})
}
+214
View File
@@ -0,0 +1,214 @@
package workflow
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/cloudwego/eino/compose"
)
func executeEinoGraph(ctx context.Context, args RunArgs, runID string, workflowID string, version int, g *graphDef, state *WorkflowLocalState) error {
_, err := invokeEinoGraph(ctx, args, runID, workflowID, version, g, state, false)
return err
}
func invokeEinoGraph(ctx context.Context, args RunArgs, runID string, workflowID string, version int, g *graphDef, state *WorkflowLocalState, resume bool) (bool, error) {
wfInput := workflowInputFromMap(state.Inputs)
if resume {
wfInput = WorkflowInput{}
}
rt := &workflowRuntime{
args: args,
runID: runID,
idx: indexGraph(g),
state: state,
}
art, err := defaultEngine.getOrCompile(ctx, workflowID, version, g)
if err != nil {
return false, fmt.Errorf("编译 Eino Workflow 失败: %w", err)
}
rt.idx = art.idx
runCtx := withWorkflowRuntime(ctx, rt)
runCtx = attachWorkflowCallbacks(runCtx, args.AppCfg, args, workflowID)
invokeOpts := []compose.Option{compose.WithCheckPointID(runID)}
for {
_, err = art.runnable.Invoke(runCtx, wfInput, invokeOpts...)
if err == nil {
return false, nil
}
if hitlErr := extractAwaitingHITL(err, art, runID, args, state); hitlErr != nil {
return true, hitlErr
}
return false, err
}
}
func extractAwaitingHITL(err error, art *compiledArtifact, runID string, args RunArgs, state *WorkflowLocalState) error {
info, ok := compose.ExtractInterruptInfo(err)
if !ok || len(art.hitlIDs) == 0 {
return nil
}
nodeID := nextHITLNodeID(info, art.hitlIDs)
node := art.idx.nodes[nodeID]
if nodeID == "" {
return nil
}
prompt := resolveHITLPromptBinding(node.Config, state)
label := firstNonEmpty(node.Label, nodeID)
if args.DB != nil {
pending := map[string]any{
"nodeId": nodeID,
"label": label,
"prompt": prompt,
"reviewer": cfgString(node.Config, "reviewer"),
}
pendingJSON, _ := json.Marshal(pending)
_ = args.DB.SetWorkflowRunAwaitingHITL(runID, nodeID, string(pendingJSON))
}
if args.Progress != nil {
args.Progress("workflow_hitl_waiting", fmt.Sprintf("等待人工确认:%s", label), map[string]any{
"workflowRunId": runID,
"nodeId": nodeID,
"label": label,
"prompt": prompt,
"reviewer": cfgString(node.Config, "reviewer"),
"mode": "interactive",
"resumeApi": fmt.Sprintf("/api/workflows/runs/%s/resume", runID),
})
}
return &AwaitingHITLError{
RunID: runID,
NodeID: nodeID,
NodeLabel: label,
Prompt: prompt,
Reviewer: cfgString(node.Config, "reviewer"),
}
}
func nextHITLNodeID(info *compose.InterruptInfo, hitlIDs []string) string {
if info != nil && len(info.BeforeNodes) > 0 {
for _, id := range info.BeforeNodes {
for _, hitl := range hitlIDs {
if id == hitl {
return id
}
}
}
return info.BeforeNodes[0]
}
if len(hitlIDs) == 0 {
return ""
}
return hitlIDs[0]
}
// ResumeWorkflowRun continues a run paused at HITL after human decision.
func ResumeWorkflowRun(ctx context.Context, args RunArgs, runID string, approved bool, comment string) (*RunResult, error) {
run, err := args.DB.GetWorkflowRun(runID)
if err != nil {
return nil, err
}
if run == nil {
return nil, fmt.Errorf("工作流运行不存在")
}
if run.Status != "awaiting_hitl" {
return nil, fmt.Errorf("工作流运行不在等待审批状态: %s", run.Status)
}
wf, err := args.DB.GetWorkflowDefinition(run.WorkflowID)
if err != nil || wf == nil {
return nil, fmt.Errorf("工作流定义不存在")
}
graph, err := parseGraph(wf.GraphJSON)
if err != nil {
return nil, err
}
var input map[string]interface{}
_ = json.Unmarshal([]byte(run.InputJSON), &input)
state := newWorkflowLocalState(input, runID)
if state.Inputs == nil {
state.Inputs = map[string]any{}
}
state.Inputs["_hitl_approved"] = approved
state.Inputs["_hitl_comment"] = strings.TrimSpace(comment)
state.Inputs["_hitl_node_id"] = run.PendingHITLNodeID
if !approved {
errText := strings.TrimSpace(comment)
if errText == "" {
errText = "人工审批拒绝"
}
_ = args.DB.FinishWorkflowRun(runID, "rejected", "", errText)
if args.Progress != nil {
args.Progress("workflow_hitl_rejected", fmt.Sprintf("工作流已在审批节点「%s」被拒绝。", run.PendingHITLNodeID), map[string]interface{}{
"workflowRunId": runID,
"nodeId": run.PendingHITLNodeID,
"comment": errText,
})
}
return &RunResult{
RunID: runID,
Response: fmt.Sprintf("工作流已在审批节点「%s」被拒绝。", run.PendingHITLNodeID),
Status: "rejected",
}, nil
}
if args.Progress != nil {
args.Progress("workflow_hitl_resumed", "人工审批已通过,继续执行", map[string]interface{}{
"workflowRunId": runID,
"nodeId": run.PendingHITLNodeID,
"comment": strings.TrimSpace(comment),
})
}
_ = args.DB.SetWorkflowRunStatus(runID, "running")
resumeArgs := args
if strings.TrimSpace(resumeArgs.ConversationID) == "" {
resumeArgs.ConversationID = run.ConversationID
}
awaiting, err := invokeEinoGraph(ctx, resumeArgs, runID, wf.ID, run.WorkflowVersion, graph, state, true)
if err != nil {
if IsAwaitingHITL(err) {
return &RunResult{
RunID: runID,
Status: "awaiting_hitl",
Response: fmt.Sprintf("工作流在节点「%s」等待下一次人工确认。", err.(*AwaitingHITLError).NodeID),
AwaitingHITL: true,
}, nil
}
_ = args.DB.FinishWorkflowRun(runID, "failed", "", err.Error())
return nil, err
}
_ = awaiting
output := map[string]interface{}{
"workflowId": wf.ID,
"workflowName": wf.Name,
"workflowVersion": wf.Version,
"workflowRunId": runID,
"status": "completed",
"outputs": state.Outputs,
"executedNodes": state.Executed,
"skippedNodes": state.Skipped,
"engine": "eino_workflow",
}
outputJSON, _ := json.Marshal(output)
response := renderWorkflowResponse(args.Role.Name, wf.Name, wf.Version, runID, state)
_ = args.DB.FinishWorkflowRun(runID, "completed", string(outputJSON), "")
if args.Progress != nil {
args.Progress("workflow_done", fmt.Sprintf("流程「%s」运行完成", wf.Name), map[string]interface{}{
"workflowRunId": runID,
"workflowId": wf.ID,
"outputs": state.Outputs,
"response": response,
"engine": "eino_workflow",
})
}
return &RunResult{Response: response, RunID: runID, Status: "completed"}, nil
}
+195
View File
@@ -0,0 +1,195 @@
package workflow
import (
"context"
"path/filepath"
"testing"
"cyberstrike-ai/internal/config"
"cyberstrike-ai/internal/database"
"go.uber.org/zap"
)
func testWorkflowDB(t *testing.T) *database.DB {
t.Helper()
dir := t.TempDir()
db, err := database.NewDB(filepath.Join(dir, "workflow.db"), zap.NewNop())
if err != nil {
t.Fatalf("NewDB: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
return db
}
func linearStartOutputGraph() string {
return `{
"nodes": [
{"id": "start-1", "type": "start", "label": "开始", "position": {"x": 0, "y": 0}, "config": {}},
{"id": "out-1", "type": "output", "label": "输出", "position": {"x": 0, "y": 120}, "config": {"output_key": "result", "source_binding": {"from": "inputs", "field": "message"}}}
],
"edges": [
{"id": "e1", "source": "start-1", "target": "out-1"}
],
"config": {"schema_version": 1}
}`
}
func conditionBranchGraph() string {
return `{
"nodes": [
{"id": "start-1", "type": "start", "label": "开始", "position": {"x": 0, "y": 0}, "config": {}},
{"id": "cond-1", "type": "condition", "label": "判断", "position": {"x": 0, "y": 80}, "config": {"expression": "{{inputs.message}} == yes"}},
{"id": "out-yes", "type": "output", "label": "是", "position": {"x": -80, "y": 160}, "config": {"output_key": "branch", "static_value": "yes"}},
{"id": "out-no", "type": "output", "label": "否", "position": {"x": 80, "y": 160}, "config": {"output_key": "branch", "static_value": "no"}}
],
"edges": [
{"id": "e1", "source": "start-1", "target": "cond-1"},
{"id": "e2", "source": "cond-1", "target": "out-yes", "label": "是"},
{"id": "e3", "source": "cond-1", "target": "out-no", "label": "否"}
],
"config": {"schema_version": 1}
}`
}
func TestValidateGraphJSON_linear(t *testing.T) {
if err := ValidateGraphJSON(context.Background(), linearStartOutputGraph()); err != nil {
t.Fatalf("validate: %v", err)
}
}
func TestCompileEngine_linear(t *testing.T) {
ctx := context.Background()
SetCheckpointDir(t.TempDir())
g, err := parseGraph(linearStartOutputGraph())
if err != nil {
t.Fatal(err)
}
if _, err := defaultEngine.compile(ctx, g); err != nil {
t.Fatalf("compile: %v", err)
}
}
func createTestWorkflowRun(t *testing.T, db *database.DB, runID string) {
t.Helper()
if err := db.CreateWorkflowRun(&database.WorkflowRun{
ID: runID,
WorkflowID: "test-wf",
Status: "running",
}); err != nil {
t.Fatalf("CreateWorkflowRun: %v", err)
}
}
func TestExecuteEinoGraph_linearStartOutput(t *testing.T) {
ctx := context.Background()
SetCheckpointDir(t.TempDir())
db := testWorkflowDB(t)
createTestWorkflowRun(t, db, "run-linear")
g, err := parseGraph(linearStartOutputGraph())
if err != nil {
t.Fatal(err)
}
state := newWorkflowLocalState(map[string]interface{}{"message": "ping"}, "run-linear")
args := RunArgs{DB: db}
if err := executeEinoGraph(ctx, args, "run-linear", "test-wf", 1, g, state); err != nil {
t.Fatalf("execute: %v", err)
}
if got := state.Outputs["result"]; got != "ping" {
t.Fatalf("outputs[result] = %v, want ping", got)
}
if len(state.Executed) != 2 {
t.Fatalf("executed nodes = %d, want 2", len(state.Executed))
}
}
func TestExecuteEinoGraph_conditionBranch(t *testing.T) {
ctx := context.Background()
SetCheckpointDir(t.TempDir())
db := testWorkflowDB(t)
createTestWorkflowRun(t, db, "run-yes")
createTestWorkflowRun(t, db, "run-no")
g, err := parseGraph(conditionBranchGraph())
if err != nil {
t.Fatal(err)
}
stateYes := newWorkflowLocalState(map[string]interface{}{"message": "yes"}, "run-yes")
if err := executeEinoGraph(ctx, RunArgs{DB: db}, "run-yes", "test-wf-branch", 1, g, stateYes); err != nil {
t.Fatalf("execute yes: %v", err)
}
if got := stateYes.Outputs["branch"]; got != "yes" {
t.Fatalf("yes branch output = %v", got)
}
stateNo := newWorkflowLocalState(map[string]interface{}{"message": "no"}, "run-no")
if err := executeEinoGraph(ctx, RunArgs{DB: db}, "run-no", "test-wf-branch", 1, g, stateNo); err != nil {
t.Fatalf("execute no: %v", err)
}
if got := stateNo.Outputs["branch"]; got != "no" {
t.Fatalf("no branch output = %v", got)
}
}
func TestRunRoleBoundWorkflow_integration(t *testing.T) {
ctx := context.Background()
SetCheckpointDir(t.TempDir())
db := testWorkflowDB(t)
graph := linearStartOutputGraph()
if err := db.UpsertWorkflowDefinition(&database.WorkflowDefinition{
ID: "wf-linear",
Name: "线性流程",
Version: 1,
GraphJSON: graph,
Enabled: true,
}); err != nil {
t.Fatal(err)
}
role := config.RoleConfig{
Name: "tester",
Enabled: true,
WorkflowID: "wf-linear",
WorkflowPolicy: "auto",
}
result, err := RunRoleBoundWorkflow(ctx, RunArgs{
DB: db,
Logger: zap.NewNop(),
Role: role,
UserMessage: "from-role",
})
if err != nil {
t.Fatalf("RunRoleBoundWorkflow: %v", err)
}
if result == nil || result.RunID == "" {
t.Fatal("expected run result")
}
}
func TestCompiledCache_reuse(t *testing.T) {
ctx := context.Background()
SetCheckpointDir(t.TempDir())
InvalidateCompiledCache("cache-wf")
g, err := parseGraph(linearStartOutputGraph())
if err != nil {
t.Fatal(err)
}
a1, err := defaultEngine.getOrCompile(ctx, "cache-wf", 1, g)
if err != nil {
t.Fatal(err)
}
a2, err := defaultEngine.getOrCompile(ctx, "cache-wf", 1, g)
if err != nil {
t.Fatal(err)
}
if a1 != a2 {
t.Fatal("expected cached artifact pointer reuse")
}
InvalidateCompiledCache("cache-wf")
a3, err := defaultEngine.getOrCompile(ctx, "cache-wf", 1, g)
if err != nil {
t.Fatal(err)
}
if a1 == a3 {
t.Fatal("expected new artifact after invalidation")
}
}
+64
View File
@@ -0,0 +1,64 @@
package workflow
import (
"context"
"cyberstrike-ai/internal/agent"
"cyberstrike-ai/internal/config"
"cyberstrike-ai/internal/database"
"go.uber.org/zap"
)
type workflowRuntimeCtxKey struct{}
// workflowRuntime carries per-run execution context into Eino Workflow local state.
type workflowRuntime struct {
args RunArgs
runID string
idx *graphIndex
state *WorkflowLocalState
}
func withWorkflowRuntime(ctx context.Context, rt *workflowRuntime) context.Context {
return context.WithValue(ctx, workflowRuntimeCtxKey{}, rt)
}
func workflowRuntimeFrom(ctx context.Context) *workflowRuntime {
rt, _ := ctx.Value(workflowRuntimeCtxKey{}).(*workflowRuntime)
return rt
}
func newWorkflowRuntime(args RunArgs, runID string, idx *graphIndex, inputs map[string]interface{}) *workflowRuntime {
return &workflowRuntime{
args: args,
runID: runID,
idx: idx,
state: newWorkflowLocalState(inputs, runID),
}
}
// RunArgs is the execution context for a role-bound workflow run.
type RunArgs struct {
DB *database.DB
Logger *zap.Logger
Role config.RoleConfig
AppCfg *config.Config
Agent *agent.Agent
ConversationID string
ProjectID string
UserMessage string
History []agent.ChatMessage
RoleTools []string
AgentsMarkdownDir string
SystemPromptExtra string
AssistantMessageID string
Progress agent.ProgressCallback
}
type RunResult struct {
Response string
RunID string
Status string
AwaitingHITL bool
}
+236
View File
@@ -0,0 +1,236 @@
package workflow
import (
"context"
"fmt"
"strings"
"sync"
"github.com/cloudwego/eino/compose"
)
type compiledArtifact struct {
runnable compose.Runnable[WorkflowInput, WorkflowOutput]
idx *graphIndex
hitlIDs []string
}
// Engine compiles and caches Eino Workflow artifacts.
type Engine struct {
mu sync.RWMutex
cache map[string]*compiledArtifact
cpStore compose.CheckPointStore
cpStoreMu sync.Once
cpStoreErr error
checkpointDir string
}
var defaultEngine = &Engine{
cache: make(map[string]*compiledArtifact),
checkpointDir: "data/workflow-checkpoints",
}
// SetCheckpointDir overrides the workflow checkpoint root (mainly for tests).
func SetCheckpointDir(dir string) {
defaultEngine.mu.Lock()
defer defaultEngine.mu.Unlock()
defaultEngine.checkpointDir = strings.TrimSpace(dir)
defaultEngine.cpStore = nil
defaultEngine.cpStoreErr = nil
defaultEngine.cpStoreMu = sync.Once{}
}
func (e *Engine) checkpointStore() (compose.CheckPointStore, error) {
e.cpStoreMu.Do(func() {
e.cpStore, e.cpStoreErr = newFileCheckPointStore(e.checkpointDir)
})
return e.cpStore, e.cpStoreErr
}
// InvalidateCompiledCache drops cached compilations for a workflow id.
func InvalidateCompiledCache(workflowID string) {
workflowID = strings.TrimSpace(workflowID)
if workflowID == "" {
return
}
defaultEngine.mu.Lock()
defer defaultEngine.mu.Unlock()
for key := range defaultEngine.cache {
if strings.HasPrefix(key, workflowID+":") {
delete(defaultEngine.cache, key)
}
}
}
// ValidateGraphJSON parses and trial-compiles a canvas graph (save-time gate).
func ValidateGraphJSON(ctx context.Context, graphJSON string) error {
g, err := parseGraph(graphJSON)
if err != nil {
return err
}
idx := indexGraph(g)
if len(findStartNodeIDs(idx)) == 0 {
return fmt.Errorf("工作流缺少可执行的起点节点")
}
if !hasTerminalNode(idx) {
return fmt.Errorf("工作流至少需要一个无出边的终点或 output/end 节点")
}
_, err = defaultEngine.compile(ctx, g)
return err
}
func hasTerminalNode(idx *graphIndex) bool {
for id, node := range idx.nodes {
if len(idx.outgoing[id]) == 0 {
return true
}
if strings.EqualFold(node.Type, "end") || strings.EqualFold(node.Type, "output") {
return true
}
}
return false
}
func (e *Engine) getOrCompile(ctx context.Context, workflowID string, version int, g *graphDef) (*compiledArtifact, error) {
key := cacheKey(workflowID, version)
e.mu.RLock()
if art, ok := e.cache[key]; ok {
e.mu.RUnlock()
return art, nil
}
e.mu.RUnlock()
art, err := e.compile(ctx, g)
if err != nil {
return nil, err
}
e.mu.Lock()
if existing, ok := e.cache[key]; ok {
e.mu.Unlock()
return existing, nil
}
e.cache[key] = art
e.mu.Unlock()
return art, nil
}
func (e *Engine) compile(ctx context.Context, g *graphDef) (*compiledArtifact, error) {
cpStore, err := e.checkpointStore()
if err != nil {
return nil, err
}
idx := indexGraph(g)
hitlIDs := collectHITLNodeIDs(idx)
compileOpts := []compose.GraphCompileOption{
compose.WithGraphName("CyberStrikeWorkflow"),
compose.WithCheckPointStore(cpStore),
}
if len(hitlIDs) > 0 {
compileOpts = append(compileOpts, compose.WithInterruptBeforeNodes(hitlIDs))
}
wf := compose.NewWorkflow[WorkflowInput, WorkflowOutput](
compose.WithGenLocalState(func(runCtx context.Context) *WorkflowLocalState {
if rt := workflowRuntimeFrom(runCtx); rt != nil && rt.state != nil {
return rt.state
}
return &WorkflowLocalState{
Outputs: make(map[string]any),
NodeOutputs: make(map[string]map[string]any),
NodeProceed: make(map[string]bool),
}
}),
)
nodeRefs := make(map[string]*compose.WorkflowNode, len(idx.nodes))
for id, node := range idx.nodes {
n := node
if strings.EqualFold(n.Type, "agent") {
sub, err := compileAgentSubgraph(ctx, n)
if err != nil {
return nil, fmt.Errorf("编译 Agent 子图 %s 失败: %w", id, err)
}
nodeRefs[id] = wf.AddGraphNode(id, sub)
continue
}
if strings.EqualFold(n.Type, "start") {
nodeRefs[id] = wf.AddLambdaNode(id, compose.InvokableLambda(func(runCtx context.Context, _ WorkflowInput) (WorkflowNodeOutput, error) {
return runWorkflowNodeLambda(runCtx, n)
}))
continue
}
nodeRefs[id] = wf.AddLambdaNode(id, compose.InvokableLambda(func(runCtx context.Context, _ WorkflowNodeOutput) (WorkflowNodeOutput, error) {
return runWorkflowNodeLambda(runCtx, n)
}))
}
for id, node := range idx.nodes {
if strings.EqualFold(node.Type, "condition") {
if err := wireConditionBranch(wf, nodeRefs, idx, id, node); err != nil {
return nil, err
}
continue
}
if hasConditionalOutgoingEdges(idx, id) {
if err := wireEdgeConditionBranch(wf, nodeRefs, idx, id, node); err != nil {
return nil, err
}
continue
}
for _, edge := range idx.outgoing[id] {
if target, ok := nodeRefs[edge.Target]; ok {
target.AddInput(id)
}
}
}
for _, startID := range findStartNodeIDs(idx) {
if ref, ok := nodeRefs[startID]; ok {
ref.AddInput(compose.START)
}
}
endNode := wf.End()
for id, node := range idx.nodes {
if len(idx.outgoing[id]) == 0 || strings.EqualFold(node.Type, "end") {
endNode.AddInput(id, compose.ToField(id))
}
}
runnable, err := wf.Compile(ctx, compileOpts...)
if err != nil {
return nil, err
}
return &compiledArtifact{runnable: runnable, idx: idx, hitlIDs: hitlIDs}, nil
}
func collectHITLNodeIDs(idx *graphIndex) []string {
var ids []string
for id, node := range idx.nodes {
if strings.EqualFold(node.Type, "hitl") {
ids = append(ids, id)
}
}
return ids
}
func runWorkflowNodeLambda(runCtx context.Context, n graphNode) (WorkflowNodeOutput, error) {
localRT := workflowRuntimeFrom(runCtx)
if localRT == nil {
return nil, fmt.Errorf("workflow runtime missing in context")
}
result, proceed, err := executeNode(runCtx, localRT.args, localRT.runID, n, localRT.state)
if err != nil {
return nil, err
}
localRT.state.NodeOutputs[n.ID] = result
localRT.state.LastOutput = result
if !proceed && !strings.EqualFold(n.Type, "end") {
label := firstNonEmpty(n.Label, n.ID)
if errText := cfgString(result, "error"); errText != "" {
return result, fmt.Errorf("节点「%s」失败: %s", label, errText)
}
return result, fmt.Errorf("节点「%s」未继续执行", label)
}
return result, nil
}
+24
View File
@@ -0,0 +1,24 @@
package workflow
import "errors"
// AwaitingHITLError indicates the workflow paused before a HITL node for human approval.
type AwaitingHITLError struct {
RunID string
NodeID string
NodeLabel string
Prompt string
Reviewer string
}
func (e *AwaitingHITLError) Error() string {
if e == nil {
return "workflow awaiting human approval"
}
return "workflow awaiting human approval at node " + e.NodeID
}
func IsAwaitingHITL(err error) bool {
var target *AwaitingHITLError
return errors.As(err, &target)
}
+153
View File
@@ -0,0 +1,153 @@
package workflow
import (
"encoding/json"
"fmt"
"sort"
"strings"
)
type graphDef struct {
Nodes []graphNode `json:"nodes"`
Edges []graphEdge `json:"edges"`
Config map[string]any `json:"config"`
}
type graphNode struct {
ID string `json:"id"`
Type string `json:"type"`
Label string `json:"label"`
Position graphPosition `json:"position"`
Config map[string]any `json:"config"`
}
type graphEdge struct {
ID string `json:"id"`
Source string `json:"source"`
Target string `json:"target"`
Label string `json:"label"`
Config map[string]any `json:"config"`
}
type graphPosition struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}
type graphIndex struct {
nodes map[string]graphNode
outgoing map[string][]graphEdge
incoming map[string][]graphEdge
}
func parseGraph(raw string) (*graphDef, error) {
var g graphDef
if err := json.Unmarshal([]byte(strings.TrimSpace(raw)), &g); err != nil {
return nil, fmt.Errorf("解析工作流图失败: %w", err)
}
if len(g.Nodes) == 0 {
return nil, fmt.Errorf("工作流没有节点")
}
if g.Config == nil {
g.Config = make(map[string]any)
}
return &g, nil
}
func indexGraph(g *graphDef) *graphIndex {
idx := &graphIndex{
nodes: make(map[string]graphNode, len(g.Nodes)),
outgoing: make(map[string][]graphEdge),
incoming: make(map[string][]graphEdge),
}
for _, node := range g.Nodes {
node.ID = strings.TrimSpace(node.ID)
if node.ID == "" {
continue
}
if strings.TrimSpace(node.Type) == "" {
node.Type = "tool"
}
if node.Config == nil {
node.Config = make(map[string]any)
}
idx.nodes[node.ID] = node
}
for _, edge := range g.Edges {
if _, ok := idx.nodes[edge.Source]; !ok {
continue
}
if _, ok := idx.nodes[edge.Target]; !ok {
continue
}
idx.outgoing[edge.Source] = append(idx.outgoing[edge.Source], edge)
idx.incoming[edge.Target] = append(idx.incoming[edge.Target], edge)
}
for source := range idx.outgoing {
sortEdgesByCanvas(idx.outgoing[source], idx.nodes)
}
return idx
}
func sortEdgesByCanvas(edges []graphEdge, nodes map[string]graphNode) {
sort.SliceStable(edges, func(i, j int) bool {
a := nodes[edges[i].Target]
b := nodes[edges[j].Target]
if a.Position.Y != b.Position.Y {
return a.Position.Y < b.Position.Y
}
if a.Position.X != b.Position.X {
return a.Position.X < b.Position.X
}
return edges[i].Target < edges[j].Target
})
}
func sortNodeIDsByCanvas(ids []string, nodes map[string]graphNode) {
sort.SliceStable(ids, func(i, j int) bool {
a := nodes[ids[i]]
b := nodes[ids[j]]
if a.Position.Y != b.Position.Y {
return a.Position.Y < b.Position.Y
}
if a.Position.X != b.Position.X {
return a.Position.X < b.Position.X
}
return ids[i] < ids[j]
})
}
func findStartNodeIDs(idx *graphIndex) []string {
var queue []string
for id, node := range idx.nodes {
if strings.EqualFold(node.Type, "start") {
queue = append(queue, id)
}
}
if len(queue) == 0 {
inDegree := make(map[string]int, len(idx.nodes))
for id := range idx.nodes {
inDegree[id] = 0
}
for _, edges := range idx.outgoing {
for _, edge := range edges {
inDegree[edge.Target]++
}
}
for id, deg := range inDegree {
if deg == 0 {
queue = append(queue, id)
}
}
}
sortNodeIDsByCanvas(queue, idx.nodes)
return queue
}
func branchNodeID(nodeID string) string {
return nodeID + "__eino_branch"
}
func edgeBranchNodeID(nodeID string) string {
return nodeID + "__eino_edge_branch"
}
+119
View File
@@ -0,0 +1,119 @@
package workflow
import (
"context"
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"cyberstrike-ai/internal/database"
)
// HITLDecision is a human decision on a workflow approval node.
type HITLDecision struct {
Approved bool
Comment string
}
var hitlWaiters sync.Map // runID -> chan HITLDecision
func registerHITLWaiter(runID string) chan HITLDecision {
ch := make(chan HITLDecision, 1)
hitlWaiters.Store(runID, ch)
return ch
}
func unregisterHITLWaiter(runID string, ch chan HITLDecision) {
hitlWaiters.CompareAndDelete(runID, ch)
}
// NotifyHITLDecision wakes a streaming workflow run waiting at a HITL node.
// Returns true when an active waiter was signaled.
func NotifyHITLDecision(runID string, decision HITLDecision) bool {
v, ok := hitlWaiters.Load(runID)
if !ok {
return false
}
ch, ok := v.(chan HITLDecision)
if !ok {
return false
}
select {
case ch <- decision:
return true
default:
return true
}
}
func readHITLDecisionFromDB(db *database.DB, runID string) (HITLDecision, bool, error) {
if db == nil {
return HITLDecision{}, false, nil
}
run, err := db.GetWorkflowRun(runID)
if err != nil {
return HITLDecision{}, false, err
}
if run == nil || strings.TrimSpace(run.PendingHITLJSON) == "" {
return HITLDecision{}, false, nil
}
var pending map[string]interface{}
if err := json.Unmarshal([]byte(run.PendingHITLJSON), &pending); err != nil {
return HITLDecision{}, false, nil
}
raw, ok := pending["decision"]
if !ok {
return HITLDecision{}, false, nil
}
decision := strings.ToLower(strings.TrimSpace(fmt.Sprint(raw)))
switch decision {
case "approved", "approve":
comment := ""
if v, ok := pending["comment"]; ok {
comment = strings.TrimSpace(fmt.Sprint(v))
}
return HITLDecision{Approved: true, Comment: comment}, true, nil
case "rejected", "reject":
comment := ""
if v, ok := pending["comment"]; ok {
comment = strings.TrimSpace(fmt.Sprint(v))
}
return HITLDecision{Approved: false, Comment: comment}, true, nil
default:
return HITLDecision{}, false, nil
}
}
func waitWorkflowHITLDecision(ctx context.Context, db *database.DB, runID string) (HITLDecision, error) {
ch := registerHITLWaiter(runID)
defer unregisterHITLWaiter(runID, ch)
return waitWorkflowHITLDecisionWithChannel(ctx, db, runID, ch)
}
func waitWorkflowHITLDecisionWithChannel(ctx context.Context, db *database.DB, runID string, ch chan HITLDecision) (HITLDecision, error) {
if d, ok, err := readHITLDecisionFromDB(db, runID); err != nil {
return HITLDecision{}, err
} else if ok {
return d, nil
}
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return HITLDecision{}, ctx.Err()
case d := <-ch:
return d, nil
case <-ticker.C:
if d, ok, err := readHITLDecisionFromDB(db, runID); err != nil {
return HITLDecision{}, err
} else if ok {
return d, nil
}
}
}
}
+131
View File
@@ -0,0 +1,131 @@
package workflow
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"cyberstrike-ai/internal/database"
"github.com/google/uuid"
)
func executeNode(ctx context.Context, args RunArgs, runID string, node graphNode, state *WorkflowLocalState) (map[string]any, bool, error) {
label := node.Label
if strings.TrimSpace(label) == "" {
label = node.ID
}
nodeRunID := uuid.NewString()
input := map[string]any{
"nodeId": node.ID,
"nodeType": node.Type,
"label": label,
"inputs": state.Inputs,
"previous": state.LastOutput,
}
inputJSON, _ := json.Marshal(input)
if err := args.DB.CreateWorkflowNodeRun(&database.WorkflowNodeRun{
ID: nodeRunID,
RunID: runID,
NodeID: node.ID,
Status: "running",
InputJSON: string(inputJSON),
StartedAt: time.Now(),
}); err != nil {
return nil, false, err
}
if args.Progress != nil {
args.Progress("workflow_node_start", fmt.Sprintf("开始节点:%s", label), map[string]any{
"workflowRunId": runID,
"nodeRunId": nodeRunID,
"nodeId": node.ID,
"nodeType": node.Type,
"label": label,
})
}
result, proceed, status, errText := runBuiltinNode(ctx, args, node, state)
outputJSON, _ := json.Marshal(result)
if err := args.DB.FinishWorkflowNodeRun(nodeRunID, status, string(outputJSON), errText); err != nil {
return nil, false, err
}
if status == "skipped" {
state.Skipped = append(state.Skipped, label)
} else {
state.Executed = append(state.Executed, label)
}
if args.Progress != nil {
progressData := map[string]any{
"workflowRunId": runID,
"nodeRunId": nodeRunID,
"nodeId": node.ID,
"nodeType": node.Type,
"label": label,
"status": status,
"output": result,
}
progressMsg := fmt.Sprintf("节点完成:%s%s", label, status)
if strings.EqualFold(node.Type, "condition") {
matched := false
if v, ok := result["matched"].(bool); ok {
matched = v
}
expr := cfgString(node.Config, "expression")
if matched {
progressMsg = fmt.Sprintf("条件判断:%s → 是", label)
} else {
progressMsg = fmt.Sprintf("条件判断:%s → 否", label)
}
progressData["expression"] = expr
progressData["matched"] = matched
}
args.Progress("workflow_node_result", progressMsg, progressData)
}
state.NodeProceed[node.ID] = proceed
return result, proceed, nil
}
func emitConditionBranchProgress(args RunArgs, runID string, node graphNode, edges []graphEdge, nodes map[string]graphNode, state *WorkflowLocalState) {
if args.Progress == nil || len(edges) == 0 {
return
}
for edgeIdx, edge := range edges {
allowed := edgeAllowed(edge, node, edgeIdx, state)
target := nodes[edge.Target]
targetLabel := strings.TrimSpace(target.Label)
if targetLabel == "" {
targetLabel = edge.Target
}
branchLabel := strings.TrimSpace(edge.Label)
if branchLabel == "" {
switch edgeIdx {
case 0:
branchLabel = "是"
case 1:
branchLabel = "否"
default:
branchLabel = fmt.Sprintf("分支 %d", edgeIdx+1)
}
}
cond := firstNonEmpty(cfgString(edge.Config, "condition"), cfgString(edge.Config, "expression"))
eventType := "workflow_branch_skipped"
msg := fmt.Sprintf("跳过分支「%s」→ %s", branchLabel, targetLabel)
if allowed {
eventType = "workflow_branch_taken"
msg = fmt.Sprintf("执行分支「%s」→ %s", branchLabel, targetLabel)
}
args.Progress(eventType, msg, map[string]any{
"workflowRunId": runID,
"nodeId": node.ID,
"nodeType": node.Type,
"label": node.Label,
"branchLabel": branchLabel,
"targetId": edge.Target,
"targetLabel": targetLabel,
"edgeCondition": cond,
"matched": conditionMatched(state),
})
}
}
+323
View File
@@ -0,0 +1,323 @@
package workflow
import (
"context"
"fmt"
"strings"
"cyberstrike-ai/internal/agent"
"cyberstrike-ai/internal/multiagent"
)
func runBuiltinNode(ctx context.Context, args RunArgs, node graphNode, state *WorkflowLocalState) (map[string]any, bool, string, string) {
cfg := node.Config
switch strings.ToLower(strings.TrimSpace(node.Type)) {
case "start":
out := map[string]any{
"output": state.Inputs["message"],
"message": state.Inputs["message"],
"conversationId": state.Inputs["conversationId"],
"projectId": state.Inputs["projectId"],
}
return out, true, "completed", ""
case "condition":
expr := cfgString(cfg, "expression")
ok := evalCondition(expr, state)
out := map[string]any{"output": ok, "condition": expr, "matched": ok}
return out, true, "completed", ""
case "output":
key := cfgString(cfg, "output_key")
if key == "" {
key = "result"
}
var value any
if v := cfgString(cfg, "static_value"); v != "" {
value = v
} else {
value = resolveOutputSourceBinding(cfg, state)
}
state.Outputs[key] = value
return map[string]any{"output": value, "outputs": map[string]any{key: value}}, true, "completed", ""
case "end":
value := resolveOutputSourceBinding(cfg, state)
if b, ok := parseFieldBinding(cfg, "result_binding"); ok {
value = resolveBinding(b, state)
}
return map[string]any{"output": value}, false, "completed", ""
case "tool":
return runToolNode(ctx, args, node, state)
case "agent":
return runAgentNode(ctx, args, node, state)
case "hitl":
return runHITLNode(args, node, state)
default:
reason := "未知节点类型"
return map[string]any{"output": "", "skipped": true, "reason": reason, "node_type": node.Type}, true, "skipped", reason
}
}
func runToolNode(ctx context.Context, args RunArgs, node graphNode, state *WorkflowLocalState) (map[string]any, bool, string, string) {
toolName := cfgString(node.Config, "tool_name")
if toolName == "" {
errText := "工具节点未选择 MCP 工具"
return map[string]any{"output": "", "error": errText}, false, "failed", errText
}
if args.Agent == nil {
errText := "工具节点执行失败:Agent 为空"
return map[string]any{"output": "", "tool_name": toolName, "error": errText}, false, "failed", errText
}
toolArgs, err := resolveToolArguments(node.Config, state)
if err != nil {
errText := fmt.Sprintf("工具参数不是合法 JSON%v", err)
return map[string]any{"output": "", "tool_name": toolName, "error": errText}, false, "failed", errText
}
if args.Progress != nil {
args.Progress("workflow_tool_start", fmt.Sprintf("调用工具:%s", toolName), map[string]any{
"nodeId": node.ID,
"tool": toolName,
"args": toolArgs,
})
}
result, err := args.Agent.ExecuteMCPToolForConversation(ctx, args.ConversationID, toolName, toolArgs)
if err != nil {
errText := err.Error()
return map[string]any{"output": "", "tool_name": toolName, "arguments": toolArgs, "error": errText}, false, "failed", errText
}
output := ""
executionID := ""
isError := false
if result != nil {
output = result.Result
executionID = result.ExecutionID
isError = result.IsError
}
out := map[string]any{
"output": output,
"tool_name": toolName,
"arguments": toolArgs,
"execution_id": executionID,
"is_error": isError,
}
if key := cfgString(node.Config, "output_key"); key != "" {
state.Outputs[key] = output
}
if isError {
errText := strings.TrimSpace(output)
if errText == "" {
errText = "工具返回错误"
}
return out, false, "failed", errText
}
return out, true, "completed", ""
}
func runAgentNode(ctx context.Context, args RunArgs, node graphNode, state *WorkflowLocalState) (map[string]any, bool, string, string) {
if args.AppCfg == nil || args.Agent == nil {
errText := "Agent 节点执行失败:应用配置或 Agent 为空"
return map[string]any{"output": "", "error": errText}, false, "failed", errText
}
mode := strings.ToLower(cfgString(node.Config, "agent_mode"))
if mode == "" {
mode = "eino_single"
}
inputSource := resolveNodeInputBinding(node.Config, state)
message := buildAgentNodeMessage(node, state, inputSource)
var result *multiagent.RunResult
var err error
state.SegmentMaxIteration = 0
agentProgress := workflowAgentProgress(args.Progress, state, node)
switch mode {
case "eino_single", "single", "chat":
result, err = multiagent.RunEinoSingleChatModelAgent(
ctx,
args.AppCfg,
&args.AppCfg.MultiAgent,
args.Agent,
args.DB,
args.Logger,
args.ConversationID,
args.ProjectID,
message,
args.History,
args.RoleTools,
agentProgress,
nil,
args.SystemPromptExtra,
)
default:
result, err = multiagent.RunDeepAgent(
ctx,
args.AppCfg,
&args.AppCfg.MultiAgent,
args.Agent,
args.DB,
args.Logger,
args.ConversationID,
args.ProjectID,
message,
args.History,
args.RoleTools,
agentProgress,
args.AgentsMarkdownDir,
mode,
nil,
args.SystemPromptExtra,
)
}
if err != nil {
errText := err.Error()
state.MainIterationOffset += state.SegmentMaxIteration
return map[string]any{"output": "", "mode": mode, "error": errText}, false, "failed", errText
}
state.MainIterationOffset += state.SegmentMaxIteration
response := ""
mcpIDs := []string{}
if result != nil {
response = result.Response
mcpIDs = result.MCPExecutionIDs
}
if args.Progress != nil {
args.Progress("workflow_agent_output", response, map[string]any{
"nodeId": node.ID,
"label": firstNonEmpty(node.Label, node.ID),
"mode": mode,
"inputSource": inputSource,
"inputPreview": truncateWorkflowPreview(inputSource, 500),
"mcpExecutionIds": mcpIDs,
})
}
if key := cfgString(node.Config, "output_key"); key != "" {
state.Outputs[key] = response
}
return map[string]any{
"output": response,
"mode": mode,
"mcp_execution_ids": mcpIDs,
}, true, "completed", ""
}
func buildAgentNodeMessage(node graphNode, state *WorkflowLocalState, upstreamInput string) string {
instruction := strings.TrimSpace(cfgString(node.Config, "instruction"))
upstreamInput = strings.TrimSpace(upstreamInput)
if instruction == "" {
if upstreamInput != "" {
return fmt.Sprintf("请基于上游节点输出继续处理:\n%s", upstreamInput)
}
return fmt.Sprintf("请基于上游节点输出继续处理:\n%v", state.LastOutput["output"])
}
if upstreamInput == "" {
return instruction
}
return strings.TrimSpace(fmt.Sprintf("上游输入:\n%s\n\n节点指令:\n%s", upstreamInput, instruction))
}
func workflowAgentProgress(progress agent.ProgressCallback, state *WorkflowLocalState, node graphNode) agent.ProgressCallback {
if progress == nil {
return nil
}
return func(eventType, message string, data interface{}) {
switch eventType {
case "response_start", "response_delta", "response", "done":
return
default:
enrichWorkflowAgentEventData(data, state, node)
if eventType == "iteration" {
applyWorkflowMainIterationOffset(data, state)
}
progress(eventType, message, data)
}
}
}
func enrichWorkflowAgentEventData(data interface{}, state *WorkflowLocalState, node graphNode) {
m, ok := data.(map[string]interface{})
if !ok || m == nil {
return
}
if node.ID != "" {
m["workflowNodeId"] = node.ID
}
if state != nil && strings.TrimSpace(state.WorkflowRunID) != "" {
m["workflowRunId"] = state.WorkflowRunID
}
}
func applyWorkflowMainIterationOffset(data interface{}, state *WorkflowLocalState) {
if state == nil {
return
}
m, ok := data.(map[string]interface{})
if !ok || m == nil {
return
}
scope, _ := m["einoScope"].(string)
if strings.TrimSpace(scope) != "main" {
return
}
raw := iterationNumberFromProgressData(m)
if raw <= 0 {
return
}
if raw > state.SegmentMaxIteration {
state.SegmentMaxIteration = raw
}
m["iteration"] = raw + state.MainIterationOffset
}
func iterationNumberFromProgressData(m map[string]interface{}) int {
switch v := m["iteration"].(type) {
case int:
return v
case int32:
return int(v)
case int64:
return int(v)
case float64:
return int(v)
case float32:
return int(v)
default:
return 0
}
}
func runHITLNode(args RunArgs, node graphNode, state *WorkflowLocalState) (map[string]any, bool, string, string) {
prompt := resolveHITLPromptBinding(node.Config, state)
reviewer := cfgString(node.Config, "reviewer")
if reviewer == "" {
reviewer = "human"
}
approved := true
if state != nil && state.Inputs != nil {
if v, ok := state.Inputs["_hitl_approved"]; ok {
approved = fmt.Sprint(v) == "true"
}
}
if !approved {
reason := "人工审批已拒绝"
if state != nil && state.Inputs != nil {
if v, ok := state.Inputs["_hitl_comment"]; ok {
if s := strings.TrimSpace(fmt.Sprint(v)); s != "" {
reason = s
}
}
}
return map[string]any{"output": "", "prompt": prompt, "approved": false, "mode": "interactive"}, false, "failed", reason
}
if args.Progress != nil {
args.Progress("workflow_hitl_checkpoint", "人工确认节点已通过", map[string]any{
"nodeId": node.ID,
"prompt": prompt,
"reviewer": reviewer,
"mode": "interactive",
"approved": true,
})
}
return map[string]any{
"output": prompt,
"prompt": prompt,
"reviewer": reviewer,
"approved": true,
"mode": "interactive",
}, true, "completed", ""
}
+221
View File
@@ -0,0 +1,221 @@
package workflow
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"cyberstrike-ai/internal/config"
"cyberstrike-ai/internal/database"
"github.com/google/uuid"
"go.uber.org/zap"
)
// ShouldAutoRunRoleWorkflow returns true when a role explicitly binds a workflow
// and does not turn it off. Empty policy defaults to auto to keep role UX simple.
func ShouldAutoRunRoleWorkflow(role config.RoleConfig) bool {
if strings.TrimSpace(role.WorkflowID) == "" {
return false
}
policy := strings.ToLower(strings.TrimSpace(role.WorkflowPolicy))
return policy == "" || policy == "auto"
}
// RunRoleBoundWorkflow executes the persisted role-bound workflow via cached Eino Workflow.
func RunRoleBoundWorkflow(ctx context.Context, args RunArgs) (*RunResult, error) {
if args.DB == nil {
return nil, fmt.Errorf("workflow db is nil")
}
workflowID := strings.TrimSpace(args.Role.WorkflowID)
if workflowID == "" {
return nil, fmt.Errorf("角色未绑定工作流")
}
wf, err := args.DB.GetWorkflowDefinition(workflowID)
if err != nil {
return nil, err
}
if wf == nil {
return nil, fmt.Errorf("角色绑定的工作流不存在: %s", workflowID)
}
if !wf.Enabled {
return nil, fmt.Errorf("角色绑定的工作流已禁用: %s", workflowID)
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
runID := uuid.NewString()
input := map[string]interface{}{
"message": args.UserMessage,
"conversationId": args.ConversationID,
"projectId": args.ProjectID,
"role": args.Role.Name,
"workflowId": wf.ID,
"workflowVersion": wf.Version,
}
inputJSON, _ := json.Marshal(input)
run := &database.WorkflowRun{
ID: runID,
WorkflowID: wf.ID,
WorkflowVersion: wf.Version,
ConversationID: args.ConversationID,
ProjectID: args.ProjectID,
RoleID: args.Role.Name,
Status: "running",
InputJSON: string(inputJSON),
StartedAt: time.Now(),
}
if err := args.DB.CreateWorkflowRun(run); err != nil {
return nil, err
}
if args.Progress != nil {
args.Progress("workflow_start", fmt.Sprintf("开始运行流程「%s」", wf.Name), map[string]interface{}{
"workflowId": wf.ID,
"workflowName": wf.Name,
"workflowVersion": wf.Version,
"workflowRunId": runID,
"conversationId": args.ConversationID,
"engine": "eino_workflow",
})
}
graph, err := parseGraph(wf.GraphJSON)
if err != nil {
_ = args.DB.FinishWorkflowRun(runID, "failed", "", err.Error())
return nil, err
}
state := newWorkflowLocalState(input, runID)
streaming := args.Progress != nil
resuming := false
for {
_, err := invokeEinoGraph(ctx, args, runID, wf.ID, wf.Version, graph, state, resuming)
if err == nil {
break
}
if !IsAwaitingHITL(err) {
_ = args.DB.FinishWorkflowRun(runID, "failed", "", err.Error())
return nil, err
}
hitl := err.(*AwaitingHITLError)
partial := map[string]interface{}{
"workflowId": wf.ID,
"workflowName": wf.Name,
"workflowVersion": wf.Version,
"workflowRunId": runID,
"status": "awaiting_hitl",
"outputs": state.Outputs,
"executedNodes": state.Executed,
"skippedNodes": state.Skipped,
"pendingHitl": map[string]interface{}{
"nodeId": hitl.NodeID,
"label": hitl.NodeLabel,
"prompt": hitl.Prompt,
},
"engine": "eino_workflow",
}
partialJSON, _ := json.Marshal(partial)
_ = args.DB.SetWorkflowRunAwaitingHITL(runID, hitl.NodeID, string(partialJSON))
response := fmt.Sprintf("工作流「%s」已在节点「%s」暂停,等待人工审批。\n运行 ID:%s", wf.Name, firstNonEmpty(hitl.NodeLabel, hitl.NodeID), runID)
if args.Progress != nil {
args.Progress("workflow_paused", response, map[string]interface{}{
"workflowRunId": runID,
"status": "awaiting_hitl",
"nodeId": hitl.NodeID,
"resumeApi": fmt.Sprintf("/api/workflows/runs/%s/resume", runID),
})
}
if !streaming {
return &RunResult{
Response: response,
RunID: runID,
Status: "awaiting_hitl",
AwaitingHITL: true,
}, nil
}
ch := registerHITLWaiter(runID)
decision, waitErr := waitWorkflowHITLDecisionWithChannel(ctx, args.DB, runID, ch)
unregisterHITLWaiter(runID, ch)
if waitErr != nil {
_ = args.DB.FinishWorkflowRun(runID, "cancelled", "", waitErr.Error())
return nil, waitErr
}
if !decision.Approved {
errText := strings.TrimSpace(decision.Comment)
if errText == "" {
errText = "人工审批拒绝"
}
_ = args.DB.FinishWorkflowRun(runID, "rejected", "", errText)
rejectResponse := fmt.Sprintf("工作流已在审批节点「%s」被拒绝。", firstNonEmpty(hitl.NodeLabel, hitl.NodeID))
if args.Progress != nil {
args.Progress("workflow_hitl_rejected", rejectResponse, map[string]interface{}{
"workflowRunId": runID,
"nodeId": hitl.NodeID,
"comment": errText,
})
}
return &RunResult{
Response: rejectResponse,
RunID: runID,
Status: "rejected",
}, nil
}
if args.Progress != nil {
args.Progress("workflow_hitl_resumed", "人工审批已通过,继续执行", map[string]interface{}{
"workflowRunId": runID,
"nodeId": hitl.NodeID,
"comment": decision.Comment,
})
}
if state.Inputs == nil {
state.Inputs = map[string]any{}
}
state.Inputs["_hitl_approved"] = true
state.Inputs["_hitl_comment"] = decision.Comment
state.Inputs["_hitl_node_id"] = hitl.NodeID
_ = args.DB.SetWorkflowRunStatus(runID, "running")
resuming = true
}
output := map[string]interface{}{
"workflowId": wf.ID,
"workflowName": wf.Name,
"workflowVersion": wf.Version,
"workflowRunId": runID,
"status": "completed",
"outputs": state.Outputs,
"executedNodes": state.Executed,
"skippedNodes": state.Skipped,
"engine": "eino_workflow",
}
outputJSON, _ := json.Marshal(output)
response := renderWorkflowResponse(args.Role.Name, wf.Name, wf.Version, runID, state)
if err := args.DB.FinishWorkflowRun(runID, "completed", string(outputJSON), ""); err != nil {
return nil, err
}
if args.Progress != nil {
args.Progress("workflow_done", fmt.Sprintf("流程「%s」运行完成", wf.Name), map[string]interface{}{
"workflowRunId": runID,
"workflowId": wf.ID,
"outputs": state.Outputs,
"response": response,
"engine": "eino_workflow",
})
}
if args.Logger != nil {
args.Logger.Info("role-bound workflow completed",
zap.String("workflow_id", wf.ID),
zap.String("workflow_run_id", runID),
zap.String("conversation_id", args.ConversationID),
zap.String("role", args.Role.Name),
zap.String("engine", "eino_workflow"),
)
}
return &RunResult{Response: response, RunID: runID, Status: "completed"}, nil
}
+224
View File
@@ -0,0 +1,224 @@
package workflow
import (
"fmt"
"regexp"
"sort"
"strings"
"github.com/cloudwego/eino/schema"
)
func init() {
schema.RegisterName[*WorkflowLocalState]("_cyberstrike_workflow_local_state")
}
// WorkflowLocalState is the Eino WithGenLocalState payload (checkpoint-serializable).
type WorkflowLocalState struct {
Inputs map[string]any `json:"inputs,omitempty"`
Outputs map[string]any `json:"outputs,omitempty"`
NodeOutputs map[string]map[string]any `json:"nodeOutputs,omitempty"`
NodeProceed map[string]bool `json:"nodeProceed,omitempty"`
LastOutput map[string]any `json:"lastOutput,omitempty"`
Executed []string `json:"executed,omitempty"`
Skipped []string `json:"skipped,omitempty"`
WorkflowRunID string `json:"workflowRunId,omitempty"`
MainIterationOffset int `json:"mainIterationOffset,omitempty"`
SegmentMaxIteration int `json:"segmentMaxIteration,omitempty"`
}
func newWorkflowLocalState(inputs map[string]interface{}, runID string) *WorkflowLocalState {
in := make(map[string]any, len(inputs))
for k, v := range inputs {
in[k] = v
}
return &WorkflowLocalState{
Inputs: in,
Outputs: make(map[string]any),
NodeOutputs: make(map[string]map[string]any),
NodeProceed: make(map[string]bool),
WorkflowRunID: runID,
}
}
var templateVarRe = regexp.MustCompile(`\{\{\s*([a-zA-Z0-9_.-]+)\s*\}\}`)
func resolveTemplate(s string, state *WorkflowLocalState) string {
if strings.TrimSpace(s) == "" {
return fmt.Sprint(valueFromPath("previous.output", state))
}
return templateVarRe.ReplaceAllStringFunc(s, func(match string) string {
m := templateVarRe.FindStringSubmatch(match)
if len(m) != 2 {
return match
}
return fmt.Sprint(valueFromPath(m[1], state))
})
}
func valueFromPath(path string, state *WorkflowLocalState) any {
parts := strings.Split(path, ".")
if len(parts) == 0 {
return ""
}
var cur any
switch parts[0] {
case "inputs", "input":
cur = state.Inputs
case "previous", "prev":
cur = state.LastOutput
case "outputs":
cur = state.Outputs
default:
if v, ok := state.Inputs[parts[0]]; ok {
cur = v
} else if v, ok := state.NodeOutputs[parts[0]]; ok {
cur = v
} else {
return ""
}
}
for _, p := range parts[1:] {
m, ok := cur.(map[string]any)
if !ok {
return ""
}
cur = m[p]
}
if cur == nil {
return ""
}
return cur
}
func evalCondition(expr string, state *WorkflowLocalState) bool {
expr = strings.TrimSpace(expr)
if expr == "" {
return true
}
resolved := strings.TrimSpace(resolveTemplate(expr, state))
switch {
case strings.Contains(resolved, "!="):
parts := strings.SplitN(resolved, "!=", 2)
return cleanComparable(parts[0]) != cleanComparable(parts[1])
case strings.Contains(resolved, "=="):
parts := strings.SplitN(resolved, "==", 2)
return cleanComparable(parts[0]) == cleanComparable(parts[1])
default:
v := strings.ToLower(cleanComparable(resolved))
return v != "" && v != "false" && v != "0" && v != "null"
}
}
func cleanComparable(s string) string {
s = strings.TrimSpace(s)
s = strings.Trim(s, `"'`)
return s
}
func edgeAllowed(edge graphEdge, sourceNode graphNode, edgeIndex int, state *WorkflowLocalState) bool {
cond := firstNonEmpty(cfgString(edge.Config, "condition"), cfgString(edge.Config, "expression"))
if cond != "" {
return evalCondition(cond, state)
}
if strings.EqualFold(strings.TrimSpace(sourceNode.Type), "condition") {
return conditionBranchAllowed(edge, edgeIndex, state)
}
return true
}
func conditionBranchAllowed(edge graphEdge, edgeIndex int, state *WorkflowLocalState) bool {
matched := conditionMatched(state)
if branch := conditionBranchHint(edge); branch != "" {
return (branch == "true" && matched) || (branch == "false" && !matched)
}
switch edgeIndex {
case 0:
return matched
case 1:
return !matched
default:
return false
}
}
func conditionMatched(state *WorkflowLocalState) bool {
v := strings.ToLower(cleanComparable(fmt.Sprint(valueFromPath("previous.matched", state))))
return v == "true" || v == "1"
}
func conditionBranchHint(edge graphEdge) string {
if edge.Config != nil {
switch strings.ToLower(strings.TrimSpace(cfgString(edge.Config, "branch"))) {
case "true", "yes", "y", "是":
return "true"
case "false", "no", "n", "否":
return "false"
}
}
switch strings.ToLower(strings.TrimSpace(edge.Label)) {
case "true", "yes", "y", "是":
return "true"
case "false", "no", "n", "否":
return "false"
}
return ""
}
func cfgString(cfg map[string]any, key string) string {
if cfg == nil {
return ""
}
if v, ok := cfg[key]; ok {
return strings.TrimSpace(fmt.Sprint(v))
}
return ""
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if s := strings.TrimSpace(value); s != "" {
return s
}
}
return ""
}
func truncateWorkflowPreview(s string, limit int) string {
s = strings.TrimSpace(s)
if limit <= 0 || len([]rune(s)) <= limit {
return s
}
runes := []rune(s)
return string(runes[:limit]) + "..."
}
func renderWorkflowResponse(roleName, workflowName string, version int, runID string, state *WorkflowLocalState) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("角色「%s」已完成工作流「%s」(版本 %d)。\n\n", roleName, workflowName, version))
sb.WriteString(fmt.Sprintf("运行 ID%s\n", runID))
sb.WriteString(fmt.Sprintf("已执行节点:%d", len(state.Executed)))
if len(state.Skipped) > 0 {
sb.WriteString(fmt.Sprintf(",跳过节点:%d", len(state.Skipped)))
}
sb.WriteString("\n\n")
if len(state.Outputs) > 0 {
sb.WriteString("输出:\n")
keys := make([]string, 0, len(state.Outputs))
for k := range state.Outputs {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
sb.WriteString(fmt.Sprintf("- %s%v\n", k, state.Outputs[k]))
}
} else {
sb.WriteString("暂无输出。请检查是否配置了输出节点,或条件分支是否命中。\n")
}
if len(state.Skipped) > 0 {
sb.WriteString("\n未执行的节点类型仍会保留运行记录:")
sb.WriteString(strings.Join(state.Skipped, "、"))
sb.WriteString("。")
}
return strings.TrimSpace(sb.String())
}
+74
View File
@@ -0,0 +1,74 @@
package workflow
import (
"fmt"
"strconv"
)
// WorkflowInput is the typed entry for Eino compose.Workflow[I,O].
type WorkflowInput struct {
Message string `json:"message"`
ConversationID string `json:"conversationId"`
ProjectID string `json:"projectId"`
Role string `json:"role"`
WorkflowID string `json:"workflowId"`
WorkflowVersion int `json:"workflowVersion"`
}
// WorkflowOutput aggregates terminal node payloads keyed by canvas node id.
type WorkflowOutput map[string]any
// WorkflowNodeOutput is the per-node lambda payload (alias for Eino edge type alignment).
type WorkflowNodeOutput = map[string]interface{}
func workflowInputFromMap(m map[string]interface{}) WorkflowInput {
in := WorkflowInput{}
if m == nil {
return in
}
if v, ok := m["message"].(string); ok {
in.Message = v
} else if m["message"] != nil {
in.Message = fmt.Sprint(m["message"])
}
if v, ok := m["conversationId"].(string); ok {
in.ConversationID = v
}
if v, ok := m["projectId"].(string); ok {
in.ProjectID = v
}
if v, ok := m["role"].(string); ok {
in.Role = v
}
if v, ok := m["workflowId"].(string); ok {
in.WorkflowID = v
}
switch v := m["workflowVersion"].(type) {
case int:
in.WorkflowVersion = v
case int64:
in.WorkflowVersion = int(v)
case float64:
in.WorkflowVersion = int(v)
case string:
if n, err := strconv.Atoi(v); err == nil {
in.WorkflowVersion = n
}
}
return in
}
func (in WorkflowInput) toStateInputs() map[string]any {
return map[string]any{
"message": in.Message,
"conversationId": in.ConversationID,
"projectId": in.ProjectID,
"role": in.Role,
"workflowId": in.WorkflowID,
"workflowVersion": in.WorkflowVersion,
}
}
func cacheKey(workflowID string, version int) string {
return workflowID + ":" + strconv.Itoa(version)
}
+855 -15
View File
File diff suppressed because it is too large Load Diff
+142 -14
View File
@@ -85,6 +85,7 @@
"agentsManagement": "Agent management",
"roles": "Roles",
"rolesManagement": "Roles Management",
"workflows": "Graph Orchestration",
"settings": "System settings",
"hitl": "Human-in-the-loop",
"c2": "C2",
@@ -683,7 +684,7 @@
"hitl": {
"pageTitle": "HITL approvals",
"pageReviewerLabel": "Current reviewer",
"pageReviewerHint": "Applies to the selected conversation. Without a conversation, saved locally for new chats. Takes effect immediately.",
"pageReviewerHint": "Applies to the selected conversation. Without a conversation, saved to config.yaml as the global default for new chats. Takes effect immediately.",
"pageReviewerSaved": "Reviewer saved.",
"whitelistLabel": "Tool whitelist (no approval)",
"whitelistHint": "One per line or comma-separated. Saved to config.yaml global whitelist and takes effect immediately (synced with chat sidebar).",
@@ -1915,7 +1916,7 @@
},
"chatFilesPage": {
"title": "File Management",
"intro": "Files uploaded in chat appear here. Click “Copy path” to copy the server absolute path and paste it into a conversation so the model can reference the file.",
"intro": "Files uploaded in chat appear here. Drag files into the list below, or click Upload to pick files (multiple allowed). Click “Copy path” to copy the server absolute path and paste it into a conversation so the model can reference the file.",
"upload": "Upload",
"conversationFilter": "Conversation ID",
"conversationPlaceholder": "Leave empty for all",
@@ -2027,7 +2028,7 @@
"exportNoResults": "No vulnerabilities match the current filters",
"exportStarted": "Started downloading {{count}} file(s)",
"exportFailed": "Export failed",
"saveRequiredFields": "Please fill in conversation ID, title, and severity",
"saveRequiredFields": "Please fill in conversation ID, title, description, severity, type, target, reproduction steps, evidence/POC, impact, and remediation",
"saveFailed": "Save failed",
"fetchFailed": "Failed to fetch vulnerability",
"deleteFailed": "Delete failed",
@@ -2046,9 +2047,12 @@
"detailTaskQueueId": "Task queue ID",
"detailConversationTag": "Conversation tag",
"detailTaskTag": "Task tag",
"detailProof": "Proof",
"detailPreconditions": "Preconditions",
"detailReproductionSteps": "Reproduction steps",
"detailEvidence": "Evidence / POC",
"detailImpact": "Impact",
"detailRecommendation": "Remediation",
"detailRetestNotes": "Retest method",
"downloadOkTitle": "Downloaded",
"exportFailedMessage": "Export failed",
"downloadFailed": "Download failed"
@@ -2800,9 +2804,9 @@
"taskTag": "Task tag",
"taskTagPlaceholder": "e.g. batch scan Q2, retest",
"title": "Title",
"titlePlaceholder": "Vulnerability title",
"titlePlaceholder": "/api/login is vulnerable to SQL injection",
"description": "Description",
"descriptionPlaceholder": "Detailed description",
"descriptionPlaceholder": "Describe the summary, trigger point, observed abnormal behavior, and why it is exploitable.",
"severity": "Severity",
"pleaseSelect": "Please select",
"severityCritical": "Critical",
@@ -2819,13 +2823,19 @@
"type": "Vulnerability type",
"typePlaceholder": "e.g. SQL injection, XSS, CSRF",
"target": "Target",
"targetPlaceholder": "Affected target (URL, IP, etc.)",
"proof": "Proof (POC)",
"proofPlaceholder": "Proof: request/response, screenshots, etc.",
"targetPlaceholder": "Be specific: URL, IP:port, endpoint path, and parameter name.",
"preconditions": "Preconditions",
"preconditionsPlaceholder": "Login state, permissions, account, headers/cookies, required data, environment/version; write none if not needed.",
"reproductionSteps": "Reproduction steps",
"reproductionStepsPlaceholder": "Number the steps and include entry point, parameter, payload, command, and observation point.",
"evidence": "Evidence / POC",
"evidencePlaceholder": "Raw request/response, curl/tool command, screenshot notes, logs, DNSLog/callback records, database results, file paths, timestamps, etc.",
"impact": "Impact",
"impactPlaceholder": "Impact description",
"impactPlaceholder": "Describe the verified real-world impact, such as which data can be read or changed.",
"recommendation": "Recommendation",
"recommendationPlaceholder": "Remediation"
"recommendationPlaceholder": "Write the concrete fix and retest criteria.",
"retestNotes": "Retest method",
"retestNotesPlaceholder": "How to verify the fix, including expected status code, error message, or access-control result."
},
"vulnerabilityMd": {
"headingBasic": "Basic information",
@@ -2842,9 +2852,12 @@
"labelCreated": "Created at",
"labelUpdated": "Updated at",
"headingDescription": "Description",
"headingProof": "Proof (POC)",
"headingPreconditions": "Preconditions",
"headingReproductionSteps": "Reproduction steps",
"headingEvidence": "Evidence / POC",
"headingImpact": "Impact",
"headingRecommendation": "Remediation"
"headingRecommendation": "Remediation",
"headingRetestNotes": "Retest method"
},
"roleModal": {
"addRole": "Add role",
@@ -2903,7 +2916,122 @@
"mcpDisabledBadgeTitle": "Off in MCP Management; check only expresses role linkage—turn on in MCP to run",
"roleFilterOnBanner": "These tools are checked and linked to this role (independent of MCP-wide enable).",
"roleFilterOffBanner": "These tools are unchecked and not linked to this role.",
"checkboxLinkTitle": "Check to link this tool to this role"
"checkboxLinkTitle": "Check to link this tool to this role",
"bindWorkflow": "Bind graph workflow",
"bindWorkflowHint": "When a workflow is selected, conversations with this role automatically run the bound graph; workflow fields are configured freely in the graph JSON.",
"workflowPolicy": "Workflow trigger policy",
"workflowPolicyAuto": "Auto trigger",
"workflowPolicyOff": "Off",
"noWorkflowBind": "No workflow",
"workflowDisabledSuffix": " (disabled)"
},
"workflows": {
"title": "Graph Orchestration",
"newGraph": "New graph",
"processLibrary": "Process library",
"nodeLibrary": "Node library",
"emptyList": "No graph workflows yet",
"statusEnabled": "Enabled",
"statusDisabled": "Disabled",
"metaId": "ID",
"metaName": "Name",
"metaDescription": "Description",
"metaEnabled": "Enabled",
"namePlaceholder": "Basic Web scan",
"descriptionPlaceholder": "Optional",
"connect": "Connect",
"connecting": "Connecting",
"deleteSelected": "Delete selected",
"autoLayout": "Auto layout",
"canvasEmpty": "Drag nodes from the left onto the canvas, or click node buttons to add quickly",
"properties": "Properties",
"nodeProperties": "Node properties",
"edgeProperties": "Edge properties",
"deleteNode": "Delete node",
"deleteEdge": "Delete edge",
"propertyEmpty": "Select a node or edge to edit properties",
"propLabel": "Name",
"propType": "Type",
"customFields": "Custom fields",
"addField": "Add field",
"noCustomFields": "No custom fields",
"nodes": {
"start": "Start",
"tool": "Tool",
"agent": "Agent",
"condition": "Condition",
"hitl": "Approval",
"output": "Output",
"end": "End",
"default": "Node"
},
"edges": {
"yes": "Yes",
"no": "No"
},
"config": {
"inputKeys": "Input variables",
"mcpTool": "MCP tool",
"selectTool": "Select a tool",
"toolDisabled": " (disabled)",
"argumentsTemplate": "Arguments template",
"argumentsStatic": "Tool arguments (JSON)",
"timeoutSeconds": "Timeout (seconds)",
"optional": "Optional",
"agentMode": "Agent mode",
"inputSource": "Input source",
"inputBinding": "Input field binding",
"inputBindingHint": "from = data source, field = field name (e.g. output, message)",
"nodeInstruction": "Node instruction",
"instructionPlaceholder": "Describe what this node should accomplish",
"outputKey": "Output variable name",
"conditionExpression": "Condition expression",
"conditionHint": "The node computes matched (true/false); outgoing edges define branches: first edge is \"Yes\", second is \"No\". You can also write <code>{{previous.matched}} == \"true\"</code> on the edge.",
"edgeCondition": "Edge condition",
"edgeConditionHintCondition": "{{previous.matched}} == \"true\" (Yes) or == \"false\" (No)",
"edgeConditionHintExample": "e.g. {{previous.output}} == \"ok\"",
"edgeBranchHint": "The first edge from a condition node defaults to the \"Yes\" branch, the second to \"No\"; you can customize conditions here.",
"hitlPrompt": "Approval prompt",
"hitlPromptPlaceholder": "Approve to continue",
"hitlReviewer": "Reviewer",
"hitlInteractiveHint": "The run pauses at this node; approve or reject via API or the monitor panel to continue.",
"promptBinding": "Prompt field binding",
"promptBindingHint": "When prompt text is empty, read approval text from the bound field",
"outputSource": "Variable source",
"sourceBinding": "Output field binding",
"sourceBindingHint": "Write the bound field value to the output variable; static value below overrides when set",
"staticValue": "Static output value",
"resultBinding": "End summary binding",
"resultBindingHint": "Field shown in the end node summary",
"endTemplate": "End summary template"
},
"defaultHitlPrompt": "Please approve whether this step should continue",
"nodeFallback": "Node {{n}}",
"loadFailed": "Failed to load workflows",
"saveFailed": "Failed to save workflow",
"deleteFailed": "Failed to delete workflow",
"saved": "Workflow saved",
"deleted": "Workflow deleted",
"idNameRequired": "Workflow ID and name are required",
"selectToDelete": "Select a workflow to delete",
"confirmDelete": "Delete workflow {{id}}?",
"duplicateEdge": "An edge already exists between these two nodes",
"connectModeOn": "Connect mode: click source node then target node",
"connectModeOff": "Exited connect mode",
"validation": {
"needStart": "At least one Start node is required",
"needOutput": "At least one Output node is required",
"edgeSelfLoop": "Edge {{id}} cannot point to itself",
"edgeSourceMissing": "Edge {{id}} source node does not exist",
"edgeTargetMissing": "Edge {{id}} target node does not exist",
"startIncoming": "Start node {{label}} must not have incoming edges",
"outputOutgoing": "Output node {{label}} must not have outgoing edges",
"toolNeedsMcp": "Tool node {{label}} requires an MCP tool",
"conditionNeedsExpr": "Condition node {{label}} requires a condition expression",
"conditionNeedsOutEdge": "Condition node {{label}} needs at least one outgoing edge (Yes/No branch)",
"conditionTooManyEdges": "Condition node {{label}} should have at most two outgoing edges (Yes/No); configure edge conditions for a third and beyond",
"outputNeedsKey": "Output node {{label}} requires an output variable name"
}
},
"c2": {
"clipboardCopied": "Copied to clipboard",
+142 -14
View File
@@ -85,6 +85,7 @@
"agentsManagement": "Agent管理",
"roles": "角色",
"rolesManagement": "角色管理",
"workflows": "图编排",
"settings": "系统设置",
"hitl": "人机协同",
"c2": "C2",
@@ -671,7 +672,7 @@
"hitl": {
"pageTitle": "人机协同审批",
"pageReviewerLabel": "当前审批方",
"pageReviewerHint": "作用于当前选中会话;未选会话时保存到本机,新建会话时沿用。切换后立即生效。",
"pageReviewerHint": "作用于当前选中会话;未选会话时写入 config.yaml 作为全局默认,新建会话时沿用。切换后立即生效。",
"pageReviewerSaved": "审批方已保存。",
"whitelistLabel": "免审批工具白名单",
"whitelistHint": "每行一个或逗号分隔;保存后写入 config.yaml 全局白名单并立即生效(与聊天侧栏同步展示)。",
@@ -1903,7 +1904,7 @@
},
"chatFilesPage": {
"title": "文件管理",
"intro": "管理在对话中上传的文件。需要让 AI 引用某文件时,在列表中点击「复制路径」,到对话里粘贴即可(路径为服务器上的绝对路径,与对话附件保存位置一致)。",
"intro": "管理在对话中上传的文件。可将文件拖拽到下方列表区域,或点击「上传文件」选择文件(支持多选)。需要让 AI 引用某文件时,在列表中点击「复制路径」,到对话里粘贴即可(路径为服务器上的绝对路径,与对话附件保存位置一致)。",
"upload": "上传文件",
"conversationFilter": "会话 ID",
"conversationPlaceholder": "留空表示全部",
@@ -2015,7 +2016,7 @@
"exportNoResults": "当前筛选条件下无可导出漏洞",
"exportStarted": "已开始下载 {{count}} 份报告",
"exportFailed": "导出失败",
"saveRequiredFields": "请填写必填字段:会话ID、标题和严重程度",
"saveRequiredFields": "请填写必填字段:会话ID、标题、描述、严重程度、漏洞类型、目标、复现步骤、证据/POC、影响和修复建议",
"saveFailed": "保存失败",
"fetchFailed": "获取漏洞失败",
"deleteFailed": "删除失败",
@@ -2034,9 +2035,12 @@
"detailTaskQueueId": "任务队列ID",
"detailConversationTag": "对话标签",
"detailTaskTag": "任务标签",
"detailProof": "证明",
"detailPreconditions": "前置条件",
"detailReproductionSteps": "复现步骤",
"detailEvidence": "证据 / POC",
"detailImpact": "影响",
"detailRecommendation": "修复建议",
"detailRetestNotes": "复测方式",
"downloadOkTitle": "下载成功",
"exportFailedMessage": "导出失败",
"downloadFailed": "下载失败"
@@ -2788,9 +2792,9 @@
"taskTag": "任务标签",
"taskTagPlaceholder": "如:批量扫描Q2、专项复测",
"title": "标题",
"titlePlaceholder": "漏洞标题",
"titlePlaceholder": "/api/login 存在 SQL 注入",
"description": "描述",
"descriptionPlaceholder": "漏洞详细描述",
"descriptionPlaceholder": "说明漏洞摘要、触发点、异常现象和为什么可被利用。",
"severity": "严重程度",
"pleaseSelect": "请选择",
"severityCritical": "严重",
@@ -2807,13 +2811,19 @@
"type": "漏洞类型",
"typePlaceholder": "如:SQL注入、XSS、CSRF等",
"target": "目标",
"targetPlaceholder": "受影响的目标(URLIP地址等)",
"proof": "证明(POC",
"proofPlaceholder": "漏洞证明,如请求/响应、截图等",
"targetPlaceholder": "精确到 URL/IP:端口/接口路径/参数名",
"preconditions": "前置条件",
"preconditionsPlaceholder": "登录状态、权限、账号、Header/Cookie、特定数据、环境/版本;无则写无。",
"reproductionSteps": "复现步骤",
"reproductionStepsPlaceholder": "按 1/2/3 编号,写清入口、参数、payload、执行命令、观察点。",
"evidence": "证据 / POC",
"evidencePlaceholder": "原始请求/响应、curl/工具命令、截图说明、日志、DNSLog/回连记录、数据库结果、文件路径、时间戳等。",
"impact": "影响",
"impactPlaceholder": "漏洞影响说明",
"impactPlaceholder": "结合已验证事实说明实际影响,例如越权读取哪些数据。",
"recommendation": "修复建议",
"recommendationPlaceholder": "修复建议"
"recommendationPlaceholder": "写具体修复点和复测标准。",
"retestNotes": "复测方式",
"retestNotesPlaceholder": "修复后如何验证漏洞已关闭,包括应返回的状态码、错误信息或访问控制结果。"
},
"vulnerabilityMd": {
"headingBasic": "基本信息",
@@ -2830,9 +2840,12 @@
"labelCreated": "创建时间",
"labelUpdated": "更新时间",
"headingDescription": "描述",
"headingProof": "证明(POC",
"headingPreconditions": "前置条件",
"headingReproductionSteps": "复现步骤",
"headingEvidence": "证据 / POC",
"headingImpact": "影响",
"headingRecommendation": "修复建议"
"headingRecommendation": "修复建议",
"headingRetestNotes": "复测方式"
},
"roleModal": {
"addRole": "添加角色",
@@ -2891,7 +2904,122 @@
"mcpDisabledBadgeTitle": "MCP 管理里该工具为关闭;勾选只表示想关联到本角色,实际调用需先在 MCP 中打开",
"roleFilterOnBanner": "以下为「已勾选、关联到本角色」的工具(与 MCP 管理里全局开/关无关)。",
"roleFilterOffBanner": "以下为「未勾选、未关联到本角色」的工具。",
"checkboxLinkTitle": "勾选表示本角色关联使用该工具"
"checkboxLinkTitle": "勾选表示本角色关联使用该工具",
"bindWorkflow": "绑定图编排流程",
"bindWorkflowHint": "选中流程后,对话页使用该角色会自动触发绑定图;流程字段由图定义 JSON 自由配置。",
"workflowPolicy": "流程触发策略",
"workflowPolicyAuto": "自动触发",
"workflowPolicyOff": "关闭",
"noWorkflowBind": "不绑定流程",
"workflowDisabledSuffix": "(已禁用)"
},
"workflows": {
"title": "图编排",
"newGraph": "新建图",
"processLibrary": "流程库",
"nodeLibrary": "节点库",
"emptyList": "暂无图编排流程",
"statusEnabled": "启用",
"statusDisabled": "禁用",
"metaId": "ID",
"metaName": "名称",
"metaDescription": "描述",
"metaEnabled": "启用",
"namePlaceholder": "基础 Web 扫描",
"descriptionPlaceholder": "可选",
"connect": "连线",
"connecting": "连线中",
"deleteSelected": "删除选中",
"autoLayout": "自动布局",
"canvasEmpty": "从左侧拖拽节点到画布,或点击节点按钮快速添加",
"properties": "属性",
"nodeProperties": "节点属性",
"edgeProperties": "连线属性",
"deleteNode": "删除节点",
"deleteEdge": "删除连线",
"propertyEmpty": "选择一个节点或连线后编辑属性",
"propLabel": "名称",
"propType": "类型",
"customFields": "自定义字段",
"addField": "添加字段",
"noCustomFields": "暂无自定义字段",
"nodes": {
"start": "开始",
"tool": "工具",
"agent": "Agent",
"condition": "条件",
"hitl": "审批",
"output": "输出",
"end": "结束",
"default": "节点"
},
"edges": {
"yes": "是",
"no": "否"
},
"config": {
"inputKeys": "输入变量",
"mcpTool": "MCP 工具",
"selectTool": "请选择工具",
"toolDisabled": "(未启用)",
"argumentsTemplate": "参数模板",
"argumentsStatic": "工具参数(JSON",
"timeoutSeconds": "超时秒数",
"optional": "可选",
"agentMode": "Agent 模式",
"inputSource": "输入来源",
"inputBinding": "输入字段绑定",
"inputBindingHint": "from 选数据来源,field 为字段名(如 output、message",
"nodeInstruction": "节点指令",
"instructionPlaceholder": "描述该节点要完成的任务",
"outputKey": "输出变量名",
"conditionExpression": "条件表达式",
"conditionHint": "节点会计算 matchedtrue/false),由出边决定分支:第一条线为「是」,第二条为「否」;也可在连线上写 <code>{{previous.matched}} == \"true\"</code>。",
"edgeCondition": "连线条件",
"edgeConditionHintCondition": "{{previous.matched}} == \"true\"(是)或 == \"false\"(否)",
"edgeConditionHintExample": "例如: {{previous.output}} == \"ok\"",
"edgeBranchHint": "从条件节点连出的第一条线默认为「是」分支,第二条为「否」分支;也可在此自定义条件。",
"hitlPrompt": "审批提示",
"hitlPromptPlaceholder": "请审批是否继续",
"hitlReviewer": "审批方",
"hitlInteractiveHint": "运行时在审批节点暂停,需通过 API 或监控面板人工通过/拒绝后继续。",
"promptBinding": "提示字段绑定",
"promptBindingHint": "留空提示文案时,从绑定字段读取审批说明",
"outputSource": "变量来源",
"sourceBinding": "输出字段绑定",
"sourceBindingHint": "将绑定字段的值写入输出变量;也可填写下方固定值覆盖",
"staticValue": "固定输出值",
"resultBinding": "结束摘要绑定",
"resultBindingHint": "结束节点展示的摘要字段",
"endTemplate": "结束摘要模板"
},
"defaultHitlPrompt": "请审批该步骤是否继续执行",
"nodeFallback": "节点 {{n}}",
"loadFailed": "加载工作流失败",
"saveFailed": "保存工作流失败",
"deleteFailed": "删除工作流失败",
"saved": "工作流已保存",
"deleted": "工作流已删除",
"idNameRequired": "工作流 ID 和名称不能为空",
"selectToDelete": "请选择要删除的工作流",
"confirmDelete": "确定删除工作流 {{id}}",
"duplicateEdge": "这两个节点之间已经有连线",
"connectModeOn": "连线模式:依次点击源节点和目标节点",
"connectModeOff": "已退出连线模式",
"validation": {
"needStart": "至少需要一个开始节点",
"needOutput": "至少需要一个输出节点",
"edgeSelfLoop": "连线 {{id}} 不能指向自身",
"edgeSourceMissing": "连线 {{id}} 的源节点不存在",
"edgeTargetMissing": "连线 {{id}} 的目标节点不存在",
"startIncoming": "开始节点 {{label}} 不应有入边",
"outputOutgoing": "输出节点 {{label}} 不应有出边",
"toolNeedsMcp": "工具节点 {{label}} 需要选择 MCP 工具",
"conditionNeedsExpr": "条件节点 {{label}} 需要条件表达式",
"conditionNeedsOutEdge": "条件节点 {{label}} 至少需要一条出边(是/否分支)",
"conditionTooManyEdges": "条件节点 {{label}} 建议最多两条出边(是/否);第三条及以后需配置连线条件",
"outputNeedsKey": "输出节点 {{label}} 需要输出变量名"
}
},
"c2": {
"clipboardCopied": "已复制到剪贴板",
+83 -12
View File
@@ -84,6 +84,7 @@ function initChatFilesPage() {
/* ignore */
}
}
setupChatFilesDragDrop();
loadChatFilesPage();
}
@@ -1226,21 +1227,31 @@ function chatFilesUploadToFolderClick(ev, btn) {
if (inp) inp.click();
}
async function onChatFilesUploadPick(ev) {
const input = ev.target;
const file = input && input.files && input.files[0];
if (!file) return;
const form = new FormData();
form.append('file', file);
function chatFilesResolveUploadTarget() {
const pendingDir = chatFilesPendingUploadDir;
chatFilesPendingUploadDir = '';
if (pendingDir) {
form.append('relativeDir', pendingDir);
} else {
const conv = document.getElementById('chat-files-filter-conv');
if (conv && conv.value.trim()) {
form.append('conversationId', conv.value.trim());
}
return { relativeDir: pendingDir };
}
if (chatFilesGetGroupByMode() === 'folder') {
const dir = chatFilesBrowsePath.join('/');
return dir ? { relativeDir: dir } : {};
}
const conv = document.getElementById('chat-files-filter-conv');
if (conv && conv.value.trim()) {
return { conversationId: conv.value.trim() };
}
return {};
}
async function chatFilesUploadFile(file, target) {
if (!file || chatFilesXHRUploadBusy) return false;
const form = new FormData();
form.append('file', file);
if (target && target.relativeDir) {
form.append('relativeDir', target.relativeDir);
} else if (target && target.conversationId) {
form.append('conversationId', target.conversationId);
}
chatFilesSetUploadBusy(true);
chatFilesSetUploadProgressUI(true, 0, file.name);
@@ -1265,15 +1276,75 @@ async function onChatFilesUploadPick(ev) {
: '上传成功。在列表中点击「复制路径」即可粘贴到对话中引用。';
chatFilesShowToast(msg);
}
return true;
} catch (e) {
alert((e && e.message) ? e.message : String(e));
return false;
} finally {
chatFilesSetUploadBusy(false);
chatFilesSetUploadProgressUI(false);
}
}
async function chatFilesUploadFiles(fileList) {
if (!fileList || !fileList.length || chatFilesXHRUploadBusy) return;
const files = Array.from(fileList).filter(function (f) {
return f && (f.name || f.size > 0);
});
if (!files.length) return;
const target = chatFilesResolveUploadTarget();
for (let i = 0; i < files.length; i++) {
const ok = await chatFilesUploadFile(files[i], target);
if (!ok) break;
}
}
async function onChatFilesUploadPick(ev) {
const input = ev.target;
const files = input && input.files;
if (!files || !files.length) return;
try {
await chatFilesUploadFiles(files);
} finally {
input.value = '';
}
}
let chatFilesDragDropBound = false;
function setupChatFilesDragDrop() {
if (chatFilesDragDropBound) return;
const wrap = document.getElementById('chat-files-list-wrap');
if (!wrap) return;
chatFilesDragDropBound = true;
wrap.addEventListener('dragover', function (e) {
e.preventDefault();
e.stopPropagation();
if (chatFilesXHRUploadBusy) return;
this.classList.add('drag-over');
});
wrap.addEventListener('dragleave', function (e) {
e.preventDefault();
e.stopPropagation();
if (!this.contains(e.relatedTarget)) {
this.classList.remove('drag-over');
}
});
wrap.addEventListener('drop', function (e) {
e.preventDefault();
e.stopPropagation();
this.classList.remove('drag-over');
if (chatFilesXHRUploadBusy) return;
const files = e.dataTransfer && e.dataTransfer.files;
if (files && files.length) {
chatFilesUploadFiles(files).catch(function (err) {
if (err) alert((err && err.message) ? err.message : String(err));
});
}
});
}
// 语言切换后重新渲染列表:表头与「更多」菜单由 JS 拼接,无 data-i18n,需用当前语言的 t() 再生成一遍
document.addEventListener('languagechange', function () {
if (typeof window.currentPage !== 'function') return;
+86 -21
View File
@@ -137,9 +137,12 @@ function normalizeHitlMode(mode) {
}
function defaultHitlConfig() {
const serverReviewer = (typeof window !== 'undefined' && window.csaiHitlDefaultReviewer)
? window.csaiHitlDefaultReviewer
: 'human';
return {
mode: HITL_MODE_OFF,
reviewer: 'human',
reviewer: normalizeHitlReviewer(serverReviewer),
sensitiveTools: '',
updatedAt: ''
};
@@ -315,16 +318,18 @@ async function onHitlReviewerChanged(reviewer) {
const cfg = readHitlConfigFromForm();
const cid = typeof currentConversationId === 'string' ? currentConversationId.trim() : '';
saveHitlConfigForConversation(cid, cfg, { syncGlobalLast: true });
if (cid && typeof window.saveHitlConversationConfig === 'function') {
try {
try {
if (cid && typeof window.saveHitlConversationConfig === 'function') {
await window.saveHitlConversationConfig(cid, cfg);
const ok = typeof window.t === 'function' ? window.t('hitl.pageReviewerSaved') : '审批方已保存。';
showChatToast(ok, 'success');
} catch (e) {
console.warn('onHitlReviewerChanged', e);
const prefix = typeof window.t === 'function' ? window.t('chat.hitlApplyFail') : '同步到服务器失败';
showChatToast(prefix, 'error');
} else if (typeof window.putHitlDefaultReviewer === 'function') {
await window.putHitlDefaultReviewer(cfg.reviewer);
}
const ok = typeof window.t === 'function' ? window.t('hitl.pageReviewerSaved') : '审批方已保存。';
showChatToast(ok, 'success');
} catch (e) {
console.warn('onHitlReviewerChanged', e);
const prefix = typeof window.t === 'function' ? window.t('chat.hitlApplyFail') : '同步到服务器失败';
showChatToast(prefix, 'error');
}
}
@@ -507,6 +512,7 @@ function chatAgentModeNormalizeStored(stored, cfg) {
if (typeof window !== 'undefined') {
window.csaiHitlGlobalToolWhitelist = window.csaiHitlGlobalToolWhitelist || [];
window.csaiHitlDefaultReviewer = window.csaiHitlDefaultReviewer || 'human';
window.csaiChatAgentMode = {
EINO_MODES: CHAT_AGENT_EINO_MODES,
EINO_SINGLE: CHAT_AGENT_MODE_EINO_SINGLE,
@@ -518,6 +524,7 @@ if (typeof window !== 'undefined') {
window.applyHitlSidebarConfig = applyHitlSidebarConfig;
window.readHitlConfigFromForm = readHitlConfigFromForm;
window.applyHitlConfigToUI = applyHitlConfigToUI;
window.refreshHitlConfigByCurrentConversation = refreshHitlConfigByCurrentConversation;
window.saveHitlConfigForConversation = saveHitlConfigForConversation;
window.getHitlConfigForConversation = getHitlConfigForConversation;
bindHitlSidebarModeListener();
@@ -2020,6 +2027,19 @@ function refreshSystemReadyMessageBubbles() {
});
}
function createMessageAvatar(role) {
const avatar = document.createElement('div');
avatar.className = 'message-avatar';
if (role === 'user') {
avatar.innerHTML = '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><circle cx="12" cy="7" r="4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
} else if (role === 'assistant') {
avatar.innerHTML = '<img src="/static/logo.png" alt="" class="message-avatar-img">';
} else {
avatar.textContent = 'S';
}
return avatar;
}
// 添加消息(options.systemReadyMessage 为 true 时,语言切换会刷新该条文案)
function addMessage(role, content, mcpExecutionIds = null, progressId = null, createdAt = null, options = null) {
const messagesDiv = document.getElementById('chat-messages');
@@ -2030,16 +2050,7 @@ function addMessage(role, content, mcpExecutionIds = null, progressId = null, cr
messageDiv.className = 'message ' + role;
// 创建头像
const avatar = document.createElement('div');
avatar.className = 'message-avatar';
if (role === 'user') {
avatar.textContent = 'U';
} else if (role === 'assistant') {
avatar.textContent = 'A';
} else {
avatar.textContent = 'S';
}
messageDiv.appendChild(avatar);
messageDiv.appendChild(createMessageAvatar(role));
// 创建消息内容容器
const contentWrapper = document.createElement('div');
@@ -2400,6 +2411,15 @@ function processDetailRowFingerprint(d) {
return et + '\0' + msg + '\0' + dataKey;
}
function compactWorkflowProcessDetails(details) {
if (!Array.isArray(details) || details.length === 0) return details || [];
return details.filter((detail) => {
const eventType = detail && detail.eventType ? String(detail.eventType) : '';
// workflow_node_start 已经表达了节点进入;这些事件只用于实时状态,落到详情里会让 Agent 节点看起来重复启动。
return eventType !== 'workflow_agent_start';
});
}
// 渲染过程详情
// options.append=true 时分页追加;options.markLoaded=false 时保留 lazy 标记(分页加载中)
function renderProcessDetails(messageId, processDetails, options) {
@@ -2491,6 +2511,7 @@ function renderProcessDetails(messageId, processDetails, options) {
if (typeof window.coalesceProcessDetailsToolPairs === 'function') {
processDetails = window.coalesceProcessDetailsToolPairs(processDetails);
}
processDetails = compactWorkflowProcessDetails(processDetails);
// 如果没有processDetails或为空,显示空状态
if (!processDetails || processDetails.length === 0) {
if (!appendMode) {
@@ -2518,7 +2539,44 @@ function renderProcessDetails(messageId, processDetails, options) {
const agPx = processDetailAgentPrefix(data);
let itemTitle = title;
if (eventType === 'iteration') {
if (eventType === 'workflow_start') {
const name = data.workflowName || data.workflowId || '';
itemTitle = '🧭 工作流开始' + (name ? (' · ' + name) : '');
} else if (eventType === 'workflow_done') {
const name = data.workflowName || data.workflowId || '';
itemTitle = '✅ 工作流完成' + (name ? (' · ' + name) : '');
} else if (eventType === 'workflow_node_start') {
const label = data.label || title || data.nodeId || '';
itemTitle = '▶ 节点开始' + (label ? (' · ' + label) : '');
} else if (eventType === 'workflow_node_result') {
const label = data.label || data.nodeId || '';
const status = data.status || '';
const nodeType = data.nodeType != null ? String(data.nodeType).toLowerCase() : '';
if (nodeType === 'condition') {
const matched = data.matched === true || data.matched === 'true' || (data.output && (data.output.matched === true || data.output.matched === 'true'));
itemTitle = (matched ? '✅' : '🔀') + ' 条件判断' + (label ? (' · ' + label) : '') + ' → ' + (matched ? '是' : '否');
} else {
const icon = status === 'failed' ? '❌' : (status === 'skipped' ? '⏭️' : '✅');
itemTitle = icon + ' 节点完成' + (label ? (' · ' + label) : '') + (status ? ('' + status + '') : '');
}
} else if (eventType === 'workflow_branch_taken' || eventType === 'workflow_branch_skipped') {
const branch = data.branchLabel || '';
const target = data.targetLabel || data.targetId || '';
const taken = eventType === 'workflow_branch_taken';
itemTitle = (taken ? '➡️' : '⏭️') + (taken ? ' 执行分支' : ' 跳过分支') + (branch ? (' · ' + branch) : '') + (target ? (' → ' + target) : '');
} else if (eventType === 'workflow_tool_start') {
const tool = data.tool || data.toolName || '';
itemTitle = '🔧 工具节点' + (tool ? (' · ' + tool) : '');
} else if (eventType === 'workflow_agent_output') {
const label = data.label || data.nodeId || '';
itemTitle = '🤖 Agent 输出' + (label ? (' · ' + label) : '');
} else if (eventType === 'workflow_hitl_checkpoint') {
itemTitle = '🧑‍⚖️ 人工确认检查点';
} else if (eventType === 'workflow_hitl_waiting') {
itemTitle = '🧑‍⚖️ 工作流等待审批';
} else if (eventType === 'workflow_paused') {
itemTitle = '⏸️ 工作流已暂停';
} else if (eventType === 'iteration') {
const n = data.iteration || 1;
if (data.orchestration === 'plan_execute' && data.einoScope === 'main') {
const phase = typeof window.translatePlanExecuteAgentName === 'function'
@@ -2649,16 +2707,23 @@ function finishProcessDetailsRender(messageElement, processDetails, isLazyNotLoa
}
const hasPendingHitlInDetails = processDetails.some(d => d && d.eventType === 'hitl_interrupt');
const hasPendingWorkflowHitl = processDetails.some(d => d && d.eventType === 'workflow_hitl_waiting');
const hasErrorOrCancelled = processDetails.some(d =>
d.eventType === 'error' || d.eventType === 'cancelled'
);
if (hasErrorOrCancelled && !hasPendingHitlInDetails) {
if (hasErrorOrCancelled && !hasPendingHitlInDetails && !hasPendingWorkflowHitl) {
timeline.classList.remove('expanded');
const processDetailBtn = messageElement.querySelector('.process-detail-btn');
if (processDetailBtn) {
processDetailBtn.innerHTML = '<span>' + (typeof window.t === 'function' ? window.t('chat.expandDetail') : '展开详情') + '</span>';
}
}
if (hasPendingWorkflowHitl && messageElement && messageElement.id) {
const convId = typeof window.currentConversationId === 'string' ? window.currentConversationId : '';
if (convId && typeof window.restoreWorkflowHitlInlineForConversation === 'function') {
window.restoreWorkflowHitlInlineForConversation(convId);
}
}
}
/** 懒加载折叠态:后台拉摘要,提示迭代规模而不加载全量详情 */
+175 -10
View File
@@ -231,10 +231,66 @@ async function fetchHitlConversationConfig(conversationId) {
if (!data || !data.hitl) return null;
return {
hitl: data.hitl,
defaultReviewer: hitlReviewerNormalize(data.defaultReviewer || 'human'),
hitlGlobalToolWhitelist: Array.isArray(data.hitlGlobalToolWhitelist) ? data.hitlGlobalToolWhitelist : []
};
}
function applyHitlDefaultReviewerFromServer(reviewer) {
const v = hitlReviewerNormalize(reviewer);
if (typeof window !== 'undefined') {
window.csaiHitlDefaultReviewer = v;
}
if (typeof window.saveHitlLastGlobalConfig === 'function' && typeof window.getHitlLastGlobalConfig === 'function') {
const gl = window.getHitlLastGlobalConfig();
const base = gl && typeof gl === 'object'
? gl
: { mode: 'off', sensitiveTools: '', updatedAt: '' };
window.saveHitlLastGlobalConfig(Object.assign({}, base, {
reviewer: v,
updatedAt: new Date().toISOString()
}));
}
return v;
}
async function fetchHitlDefaultReviewer() {
const resp = await hitlApiFetch('/api/hitl/default-reviewer', { credentials: 'same-origin' });
if (!resp.ok) {
return applyHitlDefaultReviewerFromServer('human');
}
const data = await resp.json();
return applyHitlDefaultReviewerFromServer(data && data.defaultReviewer);
}
async function putHitlDefaultReviewer(reviewer) {
const normalized = hitlReviewerNormalize(reviewer);
const resp = await hitlApiFetch('/api/hitl/default-reviewer', {
method: 'PUT',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ reviewer: normalized })
});
if (!resp.ok) {
const msg = await readHitlApiError(resp);
throw new Error(msg || ('HTTP ' + resp.status));
}
const data = await resp.json();
return applyHitlDefaultReviewerFromServer(data && data.defaultReviewer);
}
async function initHitlDefaultReviewerFromServer() {
try {
await fetchHitlDefaultReviewer();
if (!getCurrentConversationIdForHitl() && typeof window.refreshHitlConfigByCurrentConversation === 'function') {
window.refreshHitlConfigByCurrentConversation();
}
refreshHitlPageReviewerBar();
} catch (e) {
console.warn('initHitlDefaultReviewerFromServer', e);
}
}
/** 无会话时:将免审批工具合并进服务端 config.yaml,返回更新后的全局白名单数组 */
async function mergeHitlGlobalToolWhitelist(sensitiveTools) {
const list = Array.isArray(sensitiveTools) ? sensitiveTools : [];
@@ -462,6 +518,9 @@ async function syncHitlConfigFromServer(conversationId) {
const pack = await fetchHitlConversationConfig(conversationId);
if (!pack || !pack.hitl) return;
const cfg = pack.hitl;
if (pack.defaultReviewer) {
applyHitlDefaultReviewerFromServer(pack.defaultReviewer);
}
const globalWL = pack.hitlGlobalToolWhitelist || [];
if (typeof window !== 'undefined') {
window.csaiHitlGlobalToolWhitelist = globalWL;
@@ -619,14 +678,9 @@ async function followAgentRunAfterHitlDecision(conversationId) {
}
function renderHitlPendingList(items) {
const container = document.getElementById('hitl-pending-list');
if (!container) return;
const list = Array.isArray(items) ? items : [];
if (!list.length) {
container.innerHTML = '<div class="empty-state">' + escapeHtml(hitlT('emptyState', 'No pending approvals')) + '</div>';
return;
}
container.innerHTML = list.map(function (item) {
if (!list.length) return '';
return list.map(function (item) {
const payloadObj = hitlParsePayloadObject(item.payload || '');
const payload = String(item.payload || '');
const contextHtml = hitlRenderContextBlocks(payloadObj);
@@ -663,6 +717,86 @@ function renderHitlPendingList(items) {
}).join('');
}
function hitlWorkflowPendingLabel(run) {
const pending = hitlParsePayloadObject(run.pending_hitl_json || run.pendingHitlJson || '');
const pendingHitl = pending.pendingHitl && typeof pending.pendingHitl === 'object' ? pending.pendingHitl : pending;
return pendingHitl.label || pendingHitl.nodeId || run.pending_hitl_node_id || run.pendingHitlNodeId || run.workflow_id || run.workflowId || run.id || '-';
}
function renderWorkflowHitlPendingList(runs) {
const list = Array.isArray(runs) ? runs : [];
if (!list.length) return '';
return list.map(function (run) {
const runId = String(run.id || '').trim();
const pending = hitlParsePayloadObject(run.pending_hitl_json || run.pendingHitlJson || '');
const pendingHitl = pending.pendingHitl && typeof pending.pendingHitl === 'object' ? pending.pendingHitl : pending;
const label = hitlWorkflowPendingLabel(run);
const prompt = String(pendingHitl.prompt || '').trim();
const convId = String(run.conversation_id || run.conversationId || '').trim();
const qRun = JSON.stringify(runId).replace(/"/g, '&quot;');
const qConv = JSON.stringify(convId).replace(/"/g, '&quot;');
const workflowLabel = hitlT('workflowPendingTitle', 'Workflow approval');
const openChatLabel = hitlT('openConversation', 'Open conversation');
return (
'<div class="hitl-pending-item hitl-pending-item--workflow">' +
'<div class="hitl-pending-item-header">' +
'<div class="hitl-pending-item-title">' +
'<span class="hitl-tool-badge">' + escapeHtml(workflowLabel) + '</span>' +
'<span class="hitl-mode-tag hitl-mode-tag--approval">' + escapeHtml(label) + '</span>' +
'</div>' +
'</div>' +
'<div class="hitl-pending-meta">' + escapeHtml(hitlT('conversationLabel', 'Conversation:')) + ' ' + escapeHtml(convId || '-') + '</div>' +
(prompt ? ('<div class="hitl-input-help">' + escapeHtml(prompt) + '</div>') : '') +
'<div class="hitl-input-help">' + escapeHtml(hitlT('commentHelp', 'Comment (optional): briefly note the approval reason.')) + '</div>' +
'<input id="workflow-hitl-comment-' + escapeHtml(runId) + '" class="hitl-config-input hitl-inline-comment" type="text" placeholder="' + escapeHtml(hitlT('commentPlaceholder', 'e.g. allow read-only command')) + '">' +
'<div class="hitl-pending-actions">' +
(convId ? ('<button class="btn-secondary" onclick="openHitlConversation(' + qConv + ')">' + escapeHtml(openChatLabel) + '</button>') : '') +
'<button class="btn-secondary" onclick="submitWorkflowHitlDecisionFromPage(' + qRun + ', false, ' + qConv + ')">' + escapeHtml(hitlT('reject', 'Reject')) + '</button>' +
'<button class="btn-primary" onclick="submitWorkflowHitlDecisionFromPage(' + qRun + ', true, ' + qConv + ')">' + escapeHtml(hitlT('approve', 'Approve')) + '</button>' +
'</div>' +
'</div>'
);
}).join('');
}
async function submitWorkflowHitlDecisionFromPage(runId, approved, conversationId) {
const rid = String(runId || '').trim();
if (!rid) return;
const commentEl = document.getElementById('workflow-hitl-comment-' + rid);
const comment = commentEl ? String(commentEl.value || '').trim() : '';
try {
if (typeof window.submitWorkflowHitlDecision === 'function') {
await window.submitWorkflowHitlDecision(rid, approved, comment);
} else {
const resp = await hitlApiFetch('/api/workflows/runs/' + encodeURIComponent(rid) + '/resume', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'same-origin',
body: JSON.stringify({ approved: !!approved, comment: comment })
});
const body = await resp.json().catch(function () { return {}; });
if (!resp.ok) throw new Error((body && body.error) ? body.error : 'submit failed');
}
if (conversationId && typeof followAgentRunAfterHitlDecision === 'function') {
await followAgentRunAfterHitlDecision(conversationId);
}
await refreshHitlPending();
} catch (e) {
alert((e && e.message) ? e.message : hitlT('submitFailed', 'Submit failed'));
}
}
function openHitlConversation(conversationId) {
const cid = String(conversationId || '').trim();
if (!cid) return;
if (typeof switchPage === 'function') {
switchPage('chat');
}
if (typeof loadConversation === 'function') {
loadConversation(cid);
}
}
async function refreshHitlPending() {
const container = document.getElementById('hitl-pending-list');
if (!container) return;
@@ -680,7 +814,27 @@ async function refreshHitlPending() {
}
const data = await resp.json();
const items = Array.isArray(data.items) ? data.items : [];
hitlPendingTotal = typeof data.total === 'number' ? data.total : items.length;
let workflowRuns = [];
try {
const wfResp = await hitlApiFetch('/api/workflows/runs/pending', { credentials: 'same-origin' });
if (wfResp.ok) {
const wfData = await wfResp.json().catch(function () { return {}; });
workflowRuns = Array.isArray(wfData.runs) ? wfData.runs : [];
}
} catch (wfErr) {
console.warn('fetch workflow pending runs failed', wfErr);
}
const searchQ = q && q.value.trim() ? q.value.trim().toLowerCase() : '';
if (searchQ) {
workflowRuns = workflowRuns.filter(function (run) {
const conv = String(run.conversation_id || run.conversationId || '').toLowerCase();
const wfId = String(run.workflow_id || run.workflowId || '').toLowerCase();
const runId = String(run.id || '').toLowerCase();
const label = hitlWorkflowPendingLabel(run).toLowerCase();
return conv.indexOf(searchQ) >= 0 || wfId.indexOf(searchQ) >= 0 || runId.indexOf(searchQ) >= 0 || label.indexOf(searchQ) >= 0;
});
}
hitlPendingTotal = (typeof data.total === 'number' ? data.total : items.length) + workflowRuns.length;
const maxPage = Math.max(1, Math.ceil(hitlPendingTotal / hitlPendingPageSize));
if (hitlPendingPage > maxPage) {
hitlPendingPage = maxPage;
@@ -694,7 +848,13 @@ async function refreshHitlPending() {
}
hitlPendingCache = items;
hitlPendingLoaded = true;
renderHitlPendingList(items);
const workflowHtml = renderWorkflowHitlPendingList(workflowRuns);
const toolHtml = items.length ? renderHitlPendingList(items) : '';
if (!workflowHtml && !toolHtml) {
container.innerHTML = '<div class="empty-state">' + escapeHtml(hitlT('emptyState', 'No pending approvals')) + '</div>';
} else {
container.innerHTML = workflowHtml + (workflowHtml && toolHtml ? '<div class="hitl-pending-section-divider"></div>' : '') + (toolHtml || '');
}
renderHitlPendingPagination();
} catch (e) {
hitlPendingLoaded = false;
@@ -1266,7 +1426,7 @@ function refreshHitlLogsI18n() {
function refreshHitlPendingI18n() {
if (!document.getElementById('hitl-pending-list') || !hitlPendingLoaded) return;
renderHitlPendingList(hitlPendingCache);
refreshHitlPending();
}
function refreshHitlI18n() {
@@ -1442,6 +1602,8 @@ window.onHitlLogsPageSizeChange = onHitlLogsPageSizeChange;
window.onHitlPendingPageSizeChange = onHitlPendingPageSizeChange;
window.submitHitlDecision = submitHitlDecision;
window.submitHitlDecisionWithPayload = submitHitlDecisionWithPayload;
window.submitWorkflowHitlDecisionFromPage = submitWorkflowHitlDecisionFromPage;
window.openHitlConversation = openHitlConversation;
window.dismissHitlItem = dismissHitlItem;
window.followAgentRunAfterHitlDecision = followAgentRunAfterHitlDecision;
@@ -1460,6 +1622,7 @@ document.addEventListener('DOMContentLoaded', function () {
if (typeof window.bindHitlReviewerToggleListeners === 'function') {
window.bindHitlReviewerToggleListeners();
}
initHitlDefaultReviewerFromServer();
setTimeout(reconcileHitlUiState, 0);
});
@@ -1478,3 +1641,5 @@ window.mergeHitlGlobalToolWhitelist = mergeHitlGlobalToolWhitelist;
// 由 chat.js 在 loadConversation 内 await 调用;挂到 window 供其它入口显式触发
window.syncHitlConfigFromServer = syncHitlConfigFromServer;
window.fetchHitlDefaultReviewer = fetchHitlDefaultReviewer;
window.putHitlDefaultReviewer = putHitlDefaultReviewer;
+11 -11
View File
@@ -338,8 +338,8 @@ function showFofaParseModal(nlText, parsed) {
const explanation = parsed?.explanation != null ? String(parsed.explanation) : '';
const warningsHtml = warnings.length
? `<ul style="margin: 8px 0 0 18px;">${warnings.map(w => `<li>${escapeHtml(w)}</li>`).join('')}</ul>`
: '<div class="muted" style="margin-top: 8px;">' + _t('infoCollect.none') + '</div>';
? `<ul class="info-collect-parse-warnings-list">${warnings.map(w => `<li>${escapeHtml(w)}</li>`).join('')}</ul>`
: '<div class="muted info-collect-parse-warnings-empty">' + _t('infoCollect.none') + '</div>';
const modal = document.createElement('div');
modal.id = 'fofa-parse-modal';
@@ -348,37 +348,37 @@ function showFofaParseModal(nlText, parsed) {
openAppModal(modal, { focus: false });
deferModalContent(function () {
modal.innerHTML = `
<div class="modal-content" style="max-width: 900px;">
<div class="modal-content info-collect-parse-modal-content" style="max-width: 900px;">
<div class="modal-header">
<h2>${_t('infoCollect.parseResultTitle')}</h2>
<span class="modal-close" id="fofa-parse-modal-close" title="${_t('common.close')}">&times;</span>
</div>
<div style="padding: 18px 28px; overflow: auto;">
<div class="info-collect-parse-modal-body">
<div class="form-group">
<label>${_t('infoCollect.naturalLanguageLabel')}</label>
<div class="muted" style="margin-top: 6px; white-space: pre-wrap;">${safeNL || '-'}</div>
<div class="muted info-collect-parse-nl-text">${safeNL || '-'}</div>
</div>
<div class="form-group" style="margin-top: 14px;">
<div class="form-group info-collect-parse-form-group">
<label for="fofa-parse-query">${_t('infoCollect.fofaQueryEditable')}</label>
<textarea id="fofa-parse-query" class="info-collect-query-input" rows="2" placeholder="${_t('infoCollect.queryPlaceholder')}"></textarea>
<small class="form-hint">${_t('infoCollect.confirmBeforeQuery')}</small>
</div>
<div class="form-group" style="margin-top: 14px;">
<div class="form-group info-collect-parse-form-group">
<label>${_t('infoCollect.reminder')}</label>
<div style="background: #fff8e1; border: 1px solid #ffe8a3; border-radius: 10px; padding: 10px 12px;">
<div class="info-collect-parse-warnings">
${warningsHtml}
</div>
</div>
${explanation ? `
<div class="form-group" style="margin-top: 14px;">
<div class="form-group info-collect-parse-form-group">
<label>${_t('infoCollect.explanation')}</label>
<pre style="margin-top: 8px; white-space: pre-wrap; background: var(--bg-tertiary); border: 1px solid var(--border-color); border-radius: 10px; padding: 10px 12px; font-size: 13px;">${escapeHtml(explanation)}</pre>
<pre class="info-collect-parse-explanation">${escapeHtml(explanation)}</pre>
</div>` : ''}
</div>
<div class="modal-footer" style="padding: 18px 28px;">
<div class="modal-footer info-collect-parse-modal-footer">
<button class="btn-secondary" type="button" id="fofa-parse-cancel">${_t('infoCollect.parseModalCancel')}</button>
<button class="btn-secondary" type="button" id="fofa-parse-apply">${_t('infoCollect.parseModalApply')}</button>
<button class="btn-primary" type="button" id="fofa-parse-apply-run">${_t('infoCollect.parseModalApplyRun')}</button>
+3
View File
@@ -34,6 +34,9 @@
if (el.classList.contains('info-collect-cell-modal')) {
return 'flex';
}
if (el.classList.contains('chat-files-form-modal')) {
return 'flex';
}
if (FLEX_MODAL_IDS.has(el.id)) {
return 'flex';
}
+449 -8
View File
@@ -333,6 +333,19 @@ const responseStreamStateByProgressId = new Map();
// 主通道当前迭代轮次缓存:progressId -> { iteration, orchestration }
const mainIterationStateByProgressId = new Map();
/** 图编排多 Agent 节点切换时清空流式聚合,避免推理/输出条目覆盖上一节点内容 */
function clearTimelineStreamStates(progressId) {
responseStreamStateByProgressId.delete(progressId);
thinkingStreamStateByProgressId.delete(progressId);
einoAgentReplyStreamStateByProgressId.delete(progressId);
const prefix = String(progressId) + '::';
for (const key of Array.from(toolResultStreamStateByKey.keys())) {
if (String(key).startsWith(prefix)) {
toolResultStreamStateByKey.delete(key);
}
}
}
/** 同一段主通道流式输出(Eino 可能重复 response_start */
function sameMainResponseStreamMeta(a, b) {
if (!a || !b) return false;
@@ -341,7 +354,10 @@ function sameMainResponseStreamMeta(a, b) {
if (!agentA || agentA !== agentB) return false;
const orchA = String(a.orchestration != null ? a.orchestration : '').trim();
const orchB = String(b.orchestration != null ? b.orchestration : '').trim();
return orchA === orchB;
if (orchA !== orchB) return false;
const nodeA = String(a.workflowNodeId != null ? a.workflowNodeId : '').trim();
const nodeB = String(b.workflowNodeId != null ? b.workflowNodeId : '').trim();
return nodeA === nodeB;
}
function resolveMainIterationTag(progressId, responseData) {
@@ -366,7 +382,8 @@ function buildMainResponseStreamIdentity(progressId, responseData) {
const agent = String(d.einoAgent != null ? d.einoAgent : '').trim();
const orch = String(d.orchestration != null ? d.orchestration : '').trim();
const iterTag = resolveMainIterationTag(progressId, d);
return agent + '|' + orch + '|iter=' + iterTag;
const nodeId = String(d.workflowNodeId != null ? d.workflowNodeId : '').trim();
return agent + '|' + orch + '|iter=' + iterTag + '|wfNode=' + nodeId;
}
function extractIterationTagFromStreamIdentity(identity) {
@@ -1747,13 +1764,18 @@ function handleStreamEvent(event, progressElement, progressId,
if (scope !== 'sub') {
const prevMainIter = mainIterationStateByProgressId.get(String(progressId));
const prevN = prevMainIter && prevMainIter.iteration != null ? prevMainIter.iteration : null;
const prevNode = prevMainIter && prevMainIter.workflowNodeId != null
? String(prevMainIter.workflowNodeId).trim()
: '';
const curNode = d.workflowNodeId != null ? String(d.workflowNodeId).trim() : '';
mainIterationStateByProgressId.set(String(progressId), {
iteration: n,
orchestration: d.orchestration != null ? d.orchestration : ''
orchestration: d.orchestration != null ? d.orchestration : '',
workflowNodeId: curNode
});
// 主通道进入新轮次后不复用上一轮的「执行输出」时间线条目
if (prevN != null && prevN !== n) {
responseStreamStateByProgressId.delete(progressId);
// 主通道进入新轮次或图编排切换到新 Agent 节点后,不复用上一段的流式时间线条目
if (prevN != null && (n < prevN || prevN !== n || (curNode && prevNode && curNode !== prevNode))) {
clearTimelineStreamStates(progressId);
}
}
let iterTitle;
@@ -1785,6 +1807,147 @@ function handleStreamEvent(event, progressElement, progressId,
break;
}
case 'workflow_start': {
const d = event.data || {};
const name = d.workflowName || d.workflowId || '';
addTimelineItem(timeline, 'workflow_start', {
title: '🧭 工作流开始' + (name ? (' · ' + name) : ''),
message: event.message || '',
data: d
});
break;
}
case 'workflow_done': {
const d = event.data || {};
const name = d.workflowName || d.workflowId || '';
addTimelineItem(timeline, 'workflow_done', {
title: '✅ 工作流完成' + (name ? (' · ' + name) : ''),
message: event.message || '',
data: d
});
break;
}
case 'workflow_node_start': {
const d = event.data || {};
const label = d.label || d.nodeId || '';
const nodeType = d.nodeType != null ? String(d.nodeType).toLowerCase() : '';
if (nodeType === 'agent') {
clearTimelineStreamStates(progressId);
}
addTimelineItem(timeline, 'workflow_node_start', {
title: '▶ 节点开始' + (label ? (' · ' + label) : ''),
message: event.message || '',
data: d
});
break;
}
case 'workflow_node_result': {
const d = event.data || {};
const label = d.label || d.nodeId || '';
const status = d.status || '';
const nodeType = d.nodeType != null ? String(d.nodeType).toLowerCase() : '';
let title;
if (nodeType === 'condition') {
const matched = d.matched === true || d.matched === 'true' || (d.output && (d.output.matched === true || d.output.matched === 'true'));
title = (matched ? '✅' : '🔀') + ' 条件判断' + (label ? (' · ' + label) : '') + ' → ' + (matched ? '是' : '否');
} else {
const icon = status === 'failed' ? '❌' : (status === 'skipped' ? '⏭️' : '✅');
title = icon + ' 节点完成' + (label ? (' · ' + label) : '') + (status ? ('' + status + '') : '');
}
addTimelineItem(timeline, 'workflow_node_result', {
title: title,
message: event.message || '',
data: d
});
break;
}
case 'workflow_branch_taken':
case 'workflow_branch_skipped': {
const d = event.data || {};
const branch = d.branchLabel || '';
const target = d.targetLabel || d.targetId || '';
const taken = event.type === 'workflow_branch_taken';
addTimelineItem(timeline, event.type, {
title: (taken ? '➡️' : '⏭️') + (taken ? ' 执行分支' : ' 跳过分支') + (branch ? (' · ' + branch) : '') + (target ? (' → ' + target) : ''),
message: event.message || '',
data: d
});
break;
}
case 'workflow_tool_start': {
const d = event.data || {};
const tool = d.tool || d.toolName || '';
addTimelineItem(timeline, 'workflow_tool_start', {
title: '🔧 工具节点' + (tool ? (' · ' + tool) : ''),
message: event.message || '',
data: d
});
break;
}
case 'workflow_agent_output': {
const d = event.data || {};
const label = d.label || d.nodeId || '';
addTimelineItem(timeline, 'workflow_agent_output', {
title: '🤖 Agent 输出' + (label ? (' · ' + label) : ''),
message: event.message || '',
data: d
});
break;
}
case 'workflow_hitl_checkpoint': {
addTimelineItem(timeline, 'workflow_hitl_checkpoint', {
title: '🧑‍⚖️ 人工确认检查点',
message: event.message || '',
data: event.data || {}
});
break;
}
case 'workflow_hitl_waiting': {
const d = event.data || {};
const hitlItemId = addTimelineItem(timeline, 'workflow_hitl_waiting', {
title: '🧑‍⚖️ 工作流等待审批',
message: event.message || '',
data: d
});
renderInlineWorkflowHitlApproval(hitlItemId, d);
break;
}
case 'workflow_hitl_resumed': {
addTimelineItem(timeline, 'workflow_hitl_resumed', {
title: '✅ 审批已通过',
message: event.message || '人工审批已通过,继续执行',
data: event.data || {}
});
break;
}
case 'workflow_hitl_rejected': {
addTimelineItem(timeline, 'workflow_hitl_rejected', {
title: '❌ 审批已拒绝',
message: event.message || '',
data: event.data || {}
});
break;
}
case 'workflow_paused': {
addTimelineItem(timeline, 'workflow_paused', {
title: '⏸️ 工作流已暂停',
message: event.message || '',
data: event.data || {}
});
break;
}
case 'eino_trace_run':
case 'eino_trace_start':
case 'eino_trace_end':
@@ -2542,6 +2705,16 @@ function handleStreamEvent(event, progressElement, progressId,
break;
case 'done':
if (event.data && event.data.workflowStatus === 'awaiting_hitl') {
const waitingTitle = document.querySelector(`#${progressId} .progress-title`);
if (waitingTitle) {
waitingTitle.textContent = '⏸️ ' + (typeof window.t === 'function' ? window.t('chat.workflowAwaitingApproval') : '工作流等待审批');
}
if (progressTaskState.has(progressId)) {
finalizeProgressTask(progressId, typeof window.t === 'function' ? window.t('chat.workflowAwaitingApproval') : '等待审批');
}
break;
}
// 清理流式输出状态
responseStreamStateByProgressId.delete(progressId);
mainIterationStateByProgressId.delete(String(progressId));
@@ -2709,6 +2882,210 @@ function renderInlineHitlApproval(itemId, data) {
rejectBtn.onclick = function () { submit('reject'); };
}
function renderInlineWorkflowHitlApproval(itemId, data) {
const item = document.getElementById(itemId);
if (!item || !data) return;
const runId = data.workflowRunId || data.workflow_run_id;
if (!runId) return;
let contentEl = item.querySelector('.timeline-item-content');
if (!contentEl) {
contentEl = document.createElement('div');
contentEl.className = 'timeline-item-content';
item.appendChild(contentEl);
}
const existingPanel = contentEl.querySelector('.workflow-hitl-inline-approval');
if (existingPanel) existingPanel.remove();
const label = data.label || data.nodeId || runId;
const prompt = data.prompt || '';
const panel = document.createElement('div');
panel.className = 'workflow-hitl-inline-approval hitl-inline-approval';
panel.innerHTML = `
<div class="hitl-input-help"><strong>${escapeHtml(label)}</strong> </div>
${prompt ? `<div class="hitl-input-help">${escapeHtml(prompt)}</div>` : ''}
<div class="hitl-input-help">备注可选</div>
<input class="hitl-config-input workflow-hitl-inline-comment" type="text" placeholder="审批意见">
<div class="hitl-pending-actions">
<button class="btn-secondary workflow-hitl-inline-reject">拒绝</button>
<button class="btn-primary workflow-hitl-inline-approve">通过</button>
</div>
<div class="hitl-input-help workflow-hitl-inline-status"></div>
`;
contentEl.appendChild(panel);
const approveBtn = panel.querySelector('.workflow-hitl-inline-approve');
const rejectBtn = panel.querySelector('.workflow-hitl-inline-reject');
const commentInput = panel.querySelector('.workflow-hitl-inline-comment');
const statusEl = panel.querySelector('.workflow-hitl-inline-status');
const setBusy = function (busy) {
approveBtn.disabled = busy;
rejectBtn.disabled = busy;
};
const submit = async function (approved) {
setBusy(true);
const comment = String(commentInput.value || '').trim();
try {
const fetchFn = typeof apiFetch === 'function' ? apiFetch : fetch;
const response = await fetchFn(`/api/workflows/runs/${encodeURIComponent(runId)}/resume`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ approved: approved, comment: comment })
});
const body = response && typeof response.json === 'function' ? await response.json() : null;
if (!response || !response.ok) {
statusEl.textContent = (body && body.error) ? body.error : '提交失败,请重试';
setBusy(false);
return;
}
if (body && body.streamResuming) {
statusEl.textContent = approved ? '已通过,工作流继续执行中…' : '已拒绝';
panel.classList.add('hitl-inline-done');
return;
}
statusEl.textContent = approved ? '已通过,工作流继续执行' : '已拒绝';
panel.classList.add('hitl-inline-done');
} catch (e) {
statusEl.textContent = '提交失败:' + (e && e.message ? e.message : 'unknown error');
setBusy(false);
}
};
approveBtn.onclick = function () { submit(true); };
rejectBtn.onclick = function () { submit(false); };
}
function parseWorkflowHitlPendingJSON(raw) {
if (!raw) return {};
if (typeof raw === 'object') return raw;
try {
const o = JSON.parse(String(raw));
return o && typeof o === 'object' ? o : {};
} catch (e) {
return {};
}
}
function workflowHitlDataFromRun(run) {
if (!run) return null;
const runId = run.id || run.workflowRunId || run.workflow_run_id;
if (!runId) return null;
const pending = parseWorkflowHitlPendingJSON(run.pending_hitl_json || run.pendingHitlJson || run.pendingHitlJSON);
const pendingHitl = pending.pendingHitl && typeof pending.pendingHitl === 'object' ? pending.pendingHitl : pending;
return {
workflowRunId: String(runId),
nodeId: pendingHitl.nodeId || run.pending_hitl_node_id || run.pendingHitlNodeId || '',
label: pendingHitl.label || pendingHitl.nodeId || run.pending_hitl_node_id || run.pendingHitlNodeId || runId,
prompt: pendingHitl.prompt || '',
conversationId: run.conversation_id || run.conversationId || ''
};
}
function findWorkflowHitlTimelineItem(detailsContainer, runId) {
if (!detailsContainer || !runId) return null;
const rid = String(runId).trim();
const byRun = detailsContainer.querySelector('[data-workflow-run-id="' + hitlEscapeAttrSelector(rid) + '"]');
if (byRun) return byRun;
const items = detailsContainer.querySelectorAll('.timeline-item-workflow_hitl_waiting');
for (let i = items.length - 1; i >= 0; i--) {
const el = items[i];
if (!el.querySelector('.workflow-hitl-inline-approval.hitl-inline-done')) {
return el;
}
}
return items.length ? items[items.length - 1] : null;
}
/**
* 刷新或切换会话后根据 workflow_runs(awaiting_hitl) 恢复工作流内联审批入口
*/
async function restoreWorkflowHitlInlineForConversation(conversationId) {
if (!conversationId || typeof apiFetch !== 'function') return;
if (typeof window.currentConversationId === 'string' && window.currentConversationId !== conversationId) {
return;
}
try {
const resp = await apiFetch('/api/workflows/runs/pending?conversationId=' + encodeURIComponent(conversationId));
if (!resp.ok) return;
const data = await resp.json().catch(function () { return {}; });
const runs = Array.isArray(data.runs) ? data.runs : [];
if (!runs.length) return;
let msgEl = document.querySelector('#chat-messages [data-backend-message-id]');
const nodes = document.querySelectorAll('#chat-messages .message.assistant');
for (let i = nodes.length - 1; i >= 0; i--) {
if (nodes[i] && nodes[i].dataset && nodes[i].dataset.backendMessageId) {
msgEl = nodes[i];
break;
}
}
if (!msgEl || !msgEl.id) return;
const clientMsgId = msgEl.id;
const backendMsgId = msgEl.dataset.backendMessageId;
const detailsContainer = document.getElementById('process-details-' + clientMsgId);
if (!detailsContainer) return;
if (detailsContainer.dataset.lazyNotLoaded === '1' && detailsContainer.dataset.loaded !== '1') {
try {
detailsContainer.dataset.loading = '1';
if (typeof loadProcessDetailsPaginated === 'function') {
await loadProcessDetailsPaginated(clientMsgId, backendMsgId);
} else if (typeof apiFetch === 'function' && backendMsgId) {
const res = await apiFetch('/api/messages/' + encodeURIComponent(backendMsgId) + '/process-details');
const j = await res.json().catch(function () { return {}; });
if (res.ok && typeof renderProcessDetails === 'function') {
renderProcessDetails(clientMsgId, (j && Array.isArray(j.processDetails)) ? j.processDetails : []);
}
}
} catch (e) {
console.error('加载过程详情失败(工作流 HITL 恢复):', e);
} finally {
detailsContainer.dataset.loading = '0';
}
}
expandProcessDetailsTimeline(clientMsgId);
for (let i = 0; i < runs.length; i++) {
const hitlData = workflowHitlDataFromRun(runs[i]);
if (!hitlData) continue;
let hitlItemEl = findWorkflowHitlTimelineItem(detailsContainer, hitlData.workflowRunId);
if (!hitlItemEl) {
const timeline = detailsContainer.querySelector('.progress-timeline');
if (timeline && typeof addTimelineItem === 'function') {
const itemId = addTimelineItem(timeline, 'workflow_hitl_waiting', {
title: '🧑‍⚖️ 工作流等待审批',
message: hitlData.label || '',
data: hitlData
});
hitlItemEl = document.getElementById(itemId);
}
}
if (hitlItemEl && hitlItemEl.id) {
renderInlineWorkflowHitlApproval(hitlItemEl.id, hitlData);
}
}
} catch (e) {
console.error('restoreWorkflowHitlInlineForConversation failed', e);
}
}
window.restoreWorkflowHitlInlineForConversation = restoreWorkflowHitlInlineForConversation;
window.submitWorkflowHitlDecision = async function submitWorkflowHitlDecision(runId, approved, comment) {
const fetchFn = typeof apiFetch === 'function' ? apiFetch : fetch;
const response = await fetchFn('/api/workflows/runs/' + encodeURIComponent(String(runId)) + '/resume', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ approved: !!approved, comment: comment || '' })
});
const body = response && typeof response.json === 'function' ? await response.json() : null;
if (!response || !response.ok) {
throw new Error((body && body.error) ? body.error : '提交失败');
}
return body;
};
function hitlEscapeAttrSelector(val) {
const s = String(val);
if (typeof CSS !== 'undefined' && typeof CSS.escape === 'function') {
@@ -2832,6 +3209,9 @@ async function restoreHitlInlineForConversation(conversationId) {
if (!hitlItemEl) continue;
renderInlineHitlApproval(hitlItemEl.id, hitlData);
}
if (typeof restoreWorkflowHitlInlineForConversation === 'function') {
await restoreWorkflowHitlInlineForConversation(conversationId);
}
} catch (e) {
console.error('restoreHitlInlineForConversation failed', e);
}
@@ -3262,6 +3642,34 @@ function updateToolCallStatus(progressId, toolCallId, status) {
}
// 添加时间线项目
function buildWorkflowConditionResultHtml(data) {
const output = (data && data.output) || {};
const expr = (data && data.expression) || output.condition || '';
const matched = (data && (data.matched === true || data.matched === 'true'))
|| output.matched === true || output.matched === 'true';
const branchText = matched ? '是(true' : '否(false';
const branchClass = matched ? 'is-true' : 'is-false';
return `<div class="timeline-item-content workflow-condition-result">
<div class="workflow-condition-row">
<span class="workflow-agent-io-label">表达式</span>
<code>${escapeHtml(String(expr || '(空)'))}</code>
</div>
<div class="workflow-condition-row">
<span class="workflow-agent-io-label">结果</span>
<span class="workflow-condition-branch ${branchClass}">${escapeHtml(branchText)}</span>
</div>
</div>`;
}
function buildWorkflowBranchDetailHtml(data) {
const cond = (data && data.edgeCondition) || '';
if (!cond) return '';
return `<div class="timeline-item-content workflow-branch-detail">
<span class="workflow-agent-io-label">连线条件</span>
<code>${escapeHtml(cond)}</code>
</div>`;
}
function addTimelineItem(timeline, type, options) {
const item = document.createElement('div');
// 生成唯一ID
@@ -3300,6 +3708,12 @@ function addTimelineItem(timeline, type, options) {
if (type === 'hitl_interrupt' && options.data && options.data.interruptId != null && String(options.data.interruptId).trim() !== '') {
item.dataset.hitlInterruptId = String(options.data.interruptId).trim();
}
if (type === 'workflow_hitl_waiting' && options.data) {
const runId = options.data.workflowRunId || options.data.workflow_run_id;
if (runId != null && String(runId).trim() !== '') {
item.dataset.workflowRunId = String(runId).trim();
}
}
if (type === 'tool_result' && options.data) {
const d = options.data;
item.dataset.toolName = (d.toolName != null && d.toolName !== '') ? String(d.toolName) : '';
@@ -3382,8 +3796,35 @@ function addTimelineItem(timeline, type, options) {
</div>
</div>
`;
} else if (type === 'eino_agent_reply' && options.message) {
content += `<div class="timeline-item-content">${formatMarkdown(options.message, timelineMarkdownOpts)}</div>`;
} else if ((type === 'eino_agent_reply' || type === 'workflow_agent_output') && options.message) {
let prefix = '';
if (type === 'workflow_agent_output' && options.data) {
const source = options.data.inputSource || '';
const preview = options.data.inputPreview || '';
if (source || preview) {
const previewText = String(preview || '').trim();
const summaryPreview = previewText.length > 80 ? (previewText.slice(0, 80) + '...') : previewText;
prefix = `<details class="workflow-agent-input">
<summary>
<span class="workflow-agent-io-label">输入</span>
${source ? `<code>${escapeHtml(source)}</code>` : ''}
${summaryPreview ? `<span class="workflow-agent-input-summary">${escapeHtml(summaryPreview)}</span>` : ''}
</summary>
${previewText ? `<pre>${escapeHtml(previewText)}</pre>` : '<div class="workflow-agent-empty">暂无输入预览</div>'}
</details>`;
}
}
const body = type === 'workflow_agent_output'
? `<div class="workflow-agent-output">
<div class="workflow-agent-io-label">输出</div>
<div class="workflow-agent-output-body">${formatMarkdown(options.message, timelineMarkdownOpts)}</div>
</div>`
: formatMarkdown(options.message, timelineMarkdownOpts);
content += `<div class="timeline-item-content workflow-agent-io">${prefix}${body}</div>`;
} else if (type === 'workflow_node_result' && options.data && String(options.data.nodeType || '').toLowerCase() === 'condition') {
content += buildWorkflowConditionResultHtml(options.data);
} else if ((type === 'workflow_branch_taken' || type === 'workflow_branch_skipped') && options.data) {
content += buildWorkflowBranchDetailHtml(options.data);
} else if (type === 'tool_result' && options.data) {
const data = options.data;
const isError = data.isError || !data.success;
+22 -1
View File
@@ -1058,6 +1058,13 @@ async function showAddRoleModal() {
document.getElementById('role-icon').value = '';
document.getElementById('role-user-prompt').value = '';
document.getElementById('role-enabled').checked = true;
if (typeof loadWorkflowOptionsForRoleModal === 'function') {
await loadWorkflowOptionsForRoleModal('');
}
const workflowPolicy = document.getElementById('role-workflow-policy');
if (workflowPolicy) {
workflowPolicy.value = 'auto';
}
// 添加角色时:显示工具选择界面,隐藏默认角色提示
const toolsSection = document.getElementById('role-tools-section');
@@ -1144,6 +1151,13 @@ async function editRole(roleName) {
document.getElementById('role-icon').value = iconValue;
document.getElementById('role-user-prompt').value = role.user_prompt || '';
document.getElementById('role-enabled').checked = role.enabled !== false;
if (typeof loadWorkflowOptionsForRoleModal === 'function') {
await loadWorkflowOptionsForRoleModal(role.workflow_id || '');
}
const workflowPolicy = document.getElementById('role-workflow-policy');
if (workflowPolicy) {
workflowPolicy.value = role.workflow_policy || 'auto';
}
// 检查是否为默认角色
const isDefaultRole = roleName === '默认';
@@ -1398,6 +1412,10 @@ async function saveRole() {
}
const userPrompt = document.getElementById('role-user-prompt').value.trim();
const enabled = document.getElementById('role-enabled').checked;
const workflowIdEl = document.getElementById('role-workflow-id');
const workflowPolicyEl = document.getElementById('role-workflow-policy');
const workflowId = workflowIdEl ? workflowIdEl.value.trim() : '';
const workflowPolicy = workflowPolicyEl ? workflowPolicyEl.value.trim() : 'auto';
const isEdit = document.getElementById('role-name').disabled;
@@ -1504,7 +1522,10 @@ async function saveRole() {
icon: icon || undefined, // 如果为空字符串,则不发送该字段
user_prompt: userPrompt,
tools: tools, // 默认角色为空数组,表示使用所有工具
enabled: enabled
enabled: enabled,
workflow_id: workflowId || undefined,
workflow_version: workflowId ? 'latest' : undefined,
workflow_policy: workflowId ? (workflowPolicy || 'auto') : undefined
};
const url = isEdit ? `/api/roles/${encodeURIComponent(name)}` : '/api/roles';
const method = isEdit ? 'PUT' : 'POST';
+7 -3
View File
@@ -58,7 +58,7 @@ function initRouter() {
const hashParts = hash.split('?');
let pageId = hashParts[0];
if (pageId === 'c2') pageId = 'c2-listeners';
if (pageId && ['dashboard', 'chat', 'hitl', 'info-collect', 'projects', 'vulnerabilities', 'webshell', 'chat-files', 'mcp-monitor', 'mcp-management', 'knowledge-management', 'knowledge-retrieval-logs', 'roles-management', 'skills-monitor', 'skills-management', 'agents-management', 'settings', 'tasks', 'c2-listeners', 'c2-sessions', 'c2-tasks', 'c2-payloads', 'c2-events', 'c2-profiles'].includes(pageId)) {
if (pageId && ['dashboard', 'chat', 'hitl', 'info-collect', 'projects', 'vulnerabilities', 'webshell', 'chat-files', 'mcp-monitor', 'mcp-management', 'knowledge-management', 'knowledge-retrieval-logs', 'roles-management', 'workflows', 'skills-monitor', 'skills-management', 'agents-management', 'settings', 'tasks', 'c2-listeners', 'c2-sessions', 'c2-tasks', 'c2-payloads', 'c2-events', 'c2-profiles'].includes(pageId)) {
switchPage(pageId);
if (pageId === 'chat') {
scheduleChatConversationFromHash(500);
@@ -449,6 +449,11 @@ async function initPage(pageId) {
});
}
break;
case 'workflows':
if (typeof refreshWorkflows === 'function') {
refreshWorkflows();
}
break;
case 'skills-monitor':
// 初始化Skills状态监控页面
if (typeof loadSkillsMonitor === 'function') {
@@ -510,7 +515,7 @@ document.addEventListener('DOMContentLoaded', function() {
let pageId = hashParts[0];
if (pageId === 'c2') pageId = 'c2-listeners';
if (pageId && ['dashboard', 'chat', 'hitl', 'info-collect', 'projects', 'tasks', 'vulnerabilities', 'webshell', 'chat-files', 'mcp-monitor', 'mcp-management', 'knowledge-management', 'knowledge-retrieval-logs', 'roles-management', 'skills-monitor', 'skills-management', 'agents-management', 'settings', 'c2-listeners', 'c2-sessions', 'c2-tasks', 'c2-payloads', 'c2-events', 'c2-profiles'].includes(pageId)) {
if (pageId && ['dashboard', 'chat', 'hitl', 'info-collect', 'projects', 'tasks', 'workflows', 'vulnerabilities', 'webshell', 'chat-files', 'mcp-monitor', 'mcp-management', 'knowledge-management', 'knowledge-retrieval-logs', 'roles-management', 'skills-monitor', 'skills-management', 'agents-management', 'settings', 'c2-listeners', 'c2-sessions', 'c2-tasks', 'c2-payloads', 'c2-events', 'c2-profiles'].includes(pageId)) {
switchPage(pageId);
if (pageId === 'chat') {
scheduleChatConversationFromHash(200);
@@ -569,4 +574,3 @@ function initConversationSidebarState() {
// 导出函数供其他脚本使用(与上方尽早绑定保持一致,便于外部脚本探测)
window.currentPage = function() { return currentPage; };
+62 -16
View File
@@ -1303,9 +1303,14 @@ function renderVulnerabilities(vulnerabilities, renderOptions) {
${vuln.conversation_tag ? vulnDetailField(vulnT('vulnerabilityPage.detailConversationTag'), vuln.conversation_tag, false) : ''}
${vuln.task_tag ? vulnDetailField(vulnT('vulnerabilityPage.detailTaskTag'), vuln.task_tag, false) : ''}
</div>
${vuln.proof ? `<div class="vulnerability-proof"><strong>${escapeHtml(vulnT('vulnerabilityPage.detailProof'))}:</strong><pre>${escapeHtml(vuln.proof)}</pre></div>` : ''}
${vuln.impact ? `<div class="vulnerability-impact"><strong>${escapeHtml(vulnT('vulnerabilityPage.detailImpact'))}:</strong> ${escapeHtml(vuln.impact)}</div>` : ''}
${vuln.recommendation ? `<div class="vulnerability-recommendation"><strong>${escapeHtml(vulnT('vulnerabilityPage.detailRecommendation'))}:</strong> ${escapeHtml(vuln.recommendation)}</div>` : ''}
<div class="vulnerability-repro">
${vulnNarrativeSection(vulnT('vulnerabilityPage.detailPreconditions'), vuln.preconditions)}
${vulnNarrativeSection(vulnT('vulnerabilityPage.detailReproductionSteps'), vuln.reproduction_steps)}
${vulnNarrativeSection(vulnT('vulnerabilityPage.detailEvidence'), vuln.evidence, { code: true })}
${vulnNarrativeSection(vulnT('vulnerabilityPage.detailImpact'), vuln.impact)}
${vulnNarrativeSection(vulnT('vulnerabilityPage.detailRecommendation'), vuln.recommendation)}
${vulnNarrativeSection(vulnT('vulnerabilityPage.detailRetestNotes'), vuln.retest_notes)}
</div>
<div class="vulnerability-related-facts" id="vuln-related-facts-${vuln.id}" data-project-id="${escapeHtml(vuln.project_id || '')}" data-vuln-id="${escapeHtml(vuln.id)}" hidden></div>
</div>
</div>
@@ -1467,9 +1472,12 @@ async function showAddVulnerabilityModal() {
document.getElementById('vulnerability-status').value = 'open';
document.getElementById('vulnerability-type').value = '';
document.getElementById('vulnerability-target').value = '';
document.getElementById('vulnerability-proof').value = '';
document.getElementById('vulnerability-preconditions').value = '';
document.getElementById('vulnerability-reproduction-steps').value = '';
document.getElementById('vulnerability-evidence').value = '';
document.getElementById('vulnerability-impact').value = '';
document.getElementById('vulnerability-recommendation').value = '';
document.getElementById('vulnerability-retest-notes').value = '';
openAppModal('vulnerability-modal');
}
@@ -1493,9 +1501,12 @@ async function editVulnerability(id) {
document.getElementById('vulnerability-status').value = vuln.status || 'open';
document.getElementById('vulnerability-type').value = vuln.type || '';
document.getElementById('vulnerability-target').value = vuln.target || '';
document.getElementById('vulnerability-proof').value = vuln.proof || '';
document.getElementById('vulnerability-preconditions').value = vuln.preconditions || '';
document.getElementById('vulnerability-reproduction-steps').value = vuln.reproduction_steps || '';
document.getElementById('vulnerability-evidence').value = vuln.evidence || '';
document.getElementById('vulnerability-impact').value = vuln.impact || '';
document.getElementById('vulnerability-recommendation').value = vuln.recommendation || '';
document.getElementById('vulnerability-retest-notes').value = vuln.retest_notes || '';
await populateVulnerabilityModalProjectSelect(vuln.project_id || '');
document.getElementById('vulnerability-title')?.focus();
});
@@ -1510,9 +1521,16 @@ async function editVulnerability(id) {
async function saveVulnerability() {
const conversationId = document.getElementById('vulnerability-conversation-id').value.trim();
const title = document.getElementById('vulnerability-title').value.trim();
const description = document.getElementById('vulnerability-description').value.trim();
const severity = document.getElementById('vulnerability-severity').value;
const type = document.getElementById('vulnerability-type').value.trim();
const target = document.getElementById('vulnerability-target').value.trim();
const reproductionSteps = document.getElementById('vulnerability-reproduction-steps').value.trim();
const evidence = document.getElementById('vulnerability-evidence').value.trim();
const impact = document.getElementById('vulnerability-impact').value.trim();
const recommendation = document.getElementById('vulnerability-recommendation').value.trim();
if (!conversationId || !title || !severity) {
if (!conversationId || !title || !description || !severity || !type || !target || !reproductionSteps || !evidence || !impact || !recommendation) {
alert(vulnT('vulnerabilityPage.saveRequiredFields'));
return;
}
@@ -1525,14 +1543,17 @@ async function saveVulnerability() {
conversation_tag: document.getElementById('vulnerability-conversation-tag').value.trim(),
task_tag: document.getElementById('vulnerability-task-tag').value.trim(),
title: title,
description: document.getElementById('vulnerability-description').value.trim(),
description: description,
severity: severity,
status: document.getElementById('vulnerability-status').value,
type: document.getElementById('vulnerability-type').value.trim(),
target: document.getElementById('vulnerability-target').value.trim(),
proof: document.getElementById('vulnerability-proof').value.trim(),
impact: document.getElementById('vulnerability-impact').value.trim(),
recommendation: document.getElementById('vulnerability-recommendation').value.trim()
type: type,
target: target,
preconditions: document.getElementById('vulnerability-preconditions').value.trim(),
reproduction_steps: reproductionSteps,
evidence: evidence,
impact: impact,
recommendation: recommendation,
retest_notes: document.getElementById('vulnerability-retest-notes').value.trim()
};
try {
@@ -1553,9 +1574,12 @@ async function saveVulnerability() {
status: data.status,
type: data.type,
target: data.target,
proof: data.proof,
preconditions: data.preconditions,
reproduction_steps: data.reproduction_steps,
evidence: data.evidence,
impact: data.impact,
recommendation: data.recommendation,
retest_notes: data.retest_notes,
};
}
@@ -1864,6 +1888,17 @@ function vulnDetailField(label, value, asCode) {
</div>`;
}
function vulnNarrativeSection(label, value, options) {
if (value === undefined || value === null || String(value).trim() === '') return '';
const opts = options || {};
const tag = opts.code ? 'pre' : 'div';
const cls = opts.code ? 'vulnerability-section-body vulnerability-section-body--code' : 'vulnerability-section-body';
return `<section class="vulnerability-section">
<strong>${escapeHtml(label)}</strong>
<${tag} class="${cls}">${escapeHtml(String(value))}</${tag}>
</section>`;
}
// 将漏洞格式化为Markdown(章节标题随界面语言)
function formatVulnerabilityAsMarkdown(vuln) {
const severityText = vulnSeverityLabel(vuln.severity);
@@ -1905,8 +1940,16 @@ function formatVulnerabilityAsMarkdown(vuln) {
markdown += `## ${L('headingDescription')}\n\n${vuln.description}\n\n`;
}
if (vuln.proof) {
markdown += `## ${L('headingProof')}\n\n\`\`\`\n${vuln.proof}\n\`\`\`\n\n`;
if (vuln.preconditions) {
markdown += `## ${L('headingPreconditions')}\n\n${vuln.preconditions}\n\n`;
}
if (vuln.reproduction_steps) {
markdown += `## ${L('headingReproductionSteps')}\n\n${vuln.reproduction_steps}\n\n`;
}
if (vuln.evidence) {
markdown += `## ${L('headingEvidence')}\n\n\`\`\`\n${vuln.evidence}\n\`\`\`\n\n`;
}
if (vuln.impact) {
@@ -1917,6 +1960,10 @@ function formatVulnerabilityAsMarkdown(vuln) {
markdown += `## ${L('headingRecommendation')}\n\n${vuln.recommendation}\n\n`;
}
if (vuln.retest_notes) {
markdown += `## ${L('headingRetestNotes')}\n\n${vuln.retest_notes}\n\n`;
}
return markdown;
}
@@ -2194,4 +2241,3 @@ window.setVulnerabilityIdFilter = setVulnerabilityIdFilter;
window.bindVulnerabilityProject = bindVulnerabilityProject;
window.buildVulnerabilityProjectOptionsHtml = buildVulnerabilityProjectOptionsHtml;
window.changeVulnerabilityStatus = changeVulnerabilityStatus;
File diff suppressed because it is too large Load Diff
+147 -18
View File
@@ -201,6 +201,16 @@
<span data-i18n="nav.tasks">任务管理</span>
</div>
</div>
<div class="nav-item" data-page="workflows">
<div class="nav-item-content" data-title="图编排" onclick="switchPage('workflows')" data-i18n="nav.workflows" data-i18n-attr="data-title" data-i18n-skip-text="true">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="3" y="3" width="8" height="8" rx="2"/>
<path d="M7 11v4a2 2 0 0 0 2 2h4"/>
<rect x="13" y="13" width="8" height="8" rx="2"/>
</svg>
<span data-i18n="nav.workflows">图编排</span>
</div>
</div>
<div class="nav-item" data-page="projects">
<div class="nav-item-content" data-title="项目管理" onclick="switchPage('projects')" data-i18n="nav.projects" data-i18n-attr="data-title" data-i18n-skip-text="true">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
@@ -1537,10 +1547,13 @@
</div>
</div>
</label>
<div class="search-box">
<input type="text" id="knowledge-search" data-i18n="knowledgePage.searchPlaceholder" data-i18n-attr="placeholder" placeholder="搜索知识..." oninput="handleKnowledgeSearchInput()" onkeydown="if(event.key==='Enter') searchKnowledgeItems()" />
<button class="btn-search" onclick="searchKnowledgeItems()" data-i18n="common.search" data-i18n-attr="title" title="搜索">🔍</button>
</div>
<label class="knowledge-search-field">
<span data-i18n="common.search">搜索</span>
<div class="search-box">
<input type="text" id="knowledge-search" data-i18n="knowledgePage.searchPlaceholder" data-i18n-attr="placeholder" placeholder="搜索知识..." oninput="handleKnowledgeSearchInput()" onkeydown="if(event.key==='Enter') searchKnowledgeItems()" />
<button class="btn-search" onclick="searchKnowledgeItems()" data-i18n="common.search" data-i18n-attr="title" title="搜索">🔍</button>
</div>
</label>
</div>
</div>
<div id="knowledge-items-list" class="knowledge-items-list">
@@ -2287,12 +2300,12 @@
<h2 data-i18n="chatFilesPage.title">文件管理</h2>
<div class="page-header-actions">
<button type="button" class="btn-primary" id="chat-files-header-upload-btn" onclick="chatFilesOpenUploadPicker()" data-i18n="chatFilesPage.upload">上传文件</button>
<input type="file" id="chat-files-upload-input" style="display:none" onchange="onChatFilesUploadPick(event)" />
<input type="file" id="chat-files-upload-input" style="display:none" multiple onchange="onChatFilesUploadPick(event)" />
<button type="button" class="btn-secondary" id="chat-files-refresh-btn" onclick="loadChatFilesPage()" data-i18n="common.refresh">刷新</button>
</div>
</div>
<div class="page-content">
<p class="chat-files-intro" data-i18n="chatFilesPage.intro">管理在对话中上传的文件。需要让 AI 引用某文件时,在列表中点击「复制路径」,到对话里粘贴即可。</p>
<p class="chat-files-intro" data-i18n="chatFilesPage.intro">管理在对话中上传的文件。可将文件拖拽到下方列表区域,或点击「上传文件」选择文件(支持多选)。需要让 AI 引用某文件时,在列表中点击「复制路径」,到对话里粘贴即可。</p>
<div class="tasks-filters chat-files-filters">
<label>
<span data-i18n="chatFilesPage.conversationFilter">会话 ID</span>
@@ -2534,6 +2547,95 @@
</div>
</div>
<!-- 图编排页面 -->
<div id="page-workflows" class="page">
<div class="page-header">
<h2 data-i18n="workflows.title">图编排</h2>
<div class="page-header-actions">
<button class="btn-secondary" onclick="refreshWorkflows()" data-i18n="common.refresh">刷新</button>
<button class="btn-primary" onclick="newWorkflowDraft()" data-i18n="workflows.newGraph">新建图</button>
</div>
</div>
<div class="page-content workflow-page-content">
<aside class="workflow-sidebar">
<div class="workflow-panel">
<div class="workflow-panel-header">
<h3 data-i18n="workflows.processLibrary">流程库</h3>
</div>
<div id="workflow-list" class="workflow-list">
<div class="loading-spinner" data-i18n="common.loading">加载中...</div>
</div>
</div>
<div class="workflow-panel">
<div class="workflow-panel-header">
<h3 data-i18n="workflows.nodeLibrary">节点库</h3>
</div>
<div class="workflow-node-palette">
<button type="button" draggable="true" data-node-type="start" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('start')" data-i18n="workflows.nodes.start">开始</button>
<button type="button" draggable="true" data-node-type="tool" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('tool')" data-i18n="workflows.nodes.tool">工具</button>
<button type="button" draggable="true" data-node-type="agent" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('agent')" data-i18n="workflows.nodes.agent">Agent</button>
<button type="button" draggable="true" data-node-type="condition" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('condition')" data-i18n="workflows.nodes.condition">条件</button>
<button type="button" draggable="true" data-node-type="hitl" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('hitl')" data-i18n="workflows.nodes.hitl">审批</button>
<button type="button" draggable="true" data-node-type="output" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('output')" data-i18n="workflows.nodes.output">输出</button>
<button type="button" draggable="true" data-node-type="end" ondragstart="workflowPaletteDragStart(event)" onclick="addWorkflowNodeFromPalette('end')" data-i18n="workflows.nodes.end">结束</button>
</div>
</div>
</aside>
<main class="workflow-main">
<section class="workflow-meta-bar">
<div class="workflow-meta-fields">
<label><span data-i18n="workflows.metaId">ID</span> <input type="text" id="workflow-id" placeholder="web-scan-basic" autocomplete="off"></label>
<label><span data-i18n="workflows.metaName">名称</span> <input type="text" id="workflow-name" data-i18n="workflows.namePlaceholder" data-i18n-attr="placeholder" placeholder="基础 Web 扫描" autocomplete="off"></label>
<label><span data-i18n="workflows.metaDescription">描述</span> <input type="text" id="workflow-description" data-i18n="workflows.descriptionPlaceholder" data-i18n-attr="placeholder" placeholder="可选" autocomplete="off"></label>
<label class="workflow-enabled-toggle"><input type="checkbox" id="workflow-enabled" checked> <span data-i18n="workflows.metaEnabled">启用</span></label>
</div>
<div class="workflow-toolbar">
<button class="btn-secondary btn-small" type="button" onclick="toggleWorkflowConnectMode()" id="workflow-connect-btn" data-i18n="workflows.connect">连线</button>
<button class="btn-secondary btn-small" type="button" onclick="deleteWorkflowSelection()" data-i18n="workflows.deleteSelected">删除选中</button>
<button class="btn-secondary btn-small" type="button" onclick="layoutWorkflowGraph()" data-i18n="workflows.autoLayout">自动布局</button>
<button class="btn-secondary btn-small" onclick="deleteCurrentWorkflow()" data-i18n="common.delete">删除</button>
<button class="btn-primary btn-small" onclick="saveWorkflowDraft()" data-i18n="common.save">保存</button>
</div>
</section>
<section class="workflow-canvas-wrap" ondragover="workflowCanvasDragOver(event)" ondrop="workflowCanvasDrop(event)">
<div id="workflow-canvas"></div>
<div id="workflow-canvas-empty" class="workflow-canvas-empty" data-i18n="workflows.canvasEmpty">从左侧拖拽节点到画布,或点击节点按钮快速添加</div>
</section>
</main>
<aside class="workflow-properties">
<div class="workflow-panel-header">
<h3 id="workflow-property-title" data-i18n="workflows.properties">属性</h3>
<button type="button" id="workflow-property-delete-btn" class="btn-secondary btn-small" onclick="deleteWorkflowSelection()" hidden data-i18n="common.delete">删除</button>
</div>
<div id="workflow-property-empty" class="workflow-property-empty" data-i18n="workflows.propertyEmpty">选择一个节点或连线后编辑属性</div>
<div id="workflow-property-form" class="workflow-property-form" hidden>
<div class="form-group">
<label for="workflow-prop-label" data-i18n="workflows.propLabel">名称</label>
<input type="text" id="workflow-prop-label" class="form-input" oninput="updateWorkflowSelectedProperty()">
</div>
<div class="form-group" id="workflow-prop-type-wrap">
<label for="workflow-prop-type" data-i18n="workflows.propType">类型</label>
<select id="workflow-prop-type" onchange="updateWorkflowSelectedProperty()">
<option value="start" data-i18n="workflows.nodes.start">开始</option>
<option value="tool" data-i18n="workflows.nodes.tool">工具</option>
<option value="agent" data-i18n="workflows.nodes.agent">Agent</option>
<option value="condition" data-i18n="workflows.nodes.condition">条件</option>
<option value="hitl" data-i18n="workflows.nodes.hitl">审批</option>
<option value="output" data-i18n="workflows.nodes.output">输出</option>
<option value="end" data-i18n="workflows.nodes.end">结束</option>
</select>
</div>
<div id="workflow-typed-config" class="workflow-typed-config"></div>
<div class="workflow-custom-fields-head">
<span data-i18n="workflows.customFields">自定义字段</span>
<button type="button" class="btn-secondary btn-small" onclick="addWorkflowCustomField()" data-i18n="workflows.addField">添加字段</button>
</div>
<div id="workflow-custom-fields" class="workflow-custom-fields"></div>
</div>
</aside>
</div>
</div>
<!-- 角色管理页面 -->
<div id="page-roles-management" class="page">
<div class="page-header">
@@ -4382,11 +4484,11 @@
</div>
<div class="form-group">
<label for="vulnerability-title"><span data-i18n="vulnerabilityModal.title">标题</span> <span style="color: red;">*</span></label>
<input type="text" id="vulnerability-title" data-i18n="vulnerabilityModal.titlePlaceholder" data-i18n-attr="placeholder" placeholder="漏洞标题" required />
<input type="text" id="vulnerability-title" data-i18n="vulnerabilityModal.titlePlaceholder" data-i18n-attr="placeholder" placeholder="/api/login 存在 SQL 注入" required />
</div>
<div class="form-group">
<label for="vulnerability-description" data-i18n="vulnerabilityModal.description">描述</label>
<textarea id="vulnerability-description" rows="5" data-i18n="vulnerabilityModal.descriptionPlaceholder" data-i18n-attr="placeholder" placeholder="漏洞详细描述"></textarea>
<label for="vulnerability-description"><span data-i18n="vulnerabilityModal.description">描述</span> <span style="color: red;">*</span></label>
<textarea id="vulnerability-description" rows="7" data-i18n="vulnerabilityModal.descriptionPlaceholder" data-i18n-attr="placeholder" placeholder="建议包含:摘要、测试环境与范围、前置条件、复现步骤、预期结果、实际结果。"></textarea>
</div>
<div class="form-group">
<label for="vulnerability-severity"><span data-i18n="vulnerabilityModal.severity">严重程度</span> <span style="color: red;">*</span></label>
@@ -4410,24 +4512,36 @@
</select>
</div>
<div class="form-group">
<label for="vulnerability-type" data-i18n="vulnerabilityModal.type">漏洞类型</label>
<label for="vulnerability-type"><span data-i18n="vulnerabilityModal.type">漏洞类型</span> <span style="color: red;">*</span></label>
<input type="text" id="vulnerability-type" data-i18n="vulnerabilityModal.typePlaceholder" data-i18n-attr="placeholder" placeholder="如:SQL注入、XSS、CSRF等" />
</div>
<div class="form-group">
<label for="vulnerability-target" data-i18n="vulnerabilityModal.target">目标</label>
<input type="text" id="vulnerability-target" data-i18n="vulnerabilityModal.targetPlaceholder" data-i18n-attr="placeholder" placeholder="受影响的目标(URLIP地址等)" />
<label for="vulnerability-target"><span data-i18n="vulnerabilityModal.target">目标</span> <span style="color: red;">*</span></label>
<input type="text" id="vulnerability-target" data-i18n="vulnerabilityModal.targetPlaceholder" data-i18n-attr="placeholder" placeholder="精确到 URL/IP:端口/接口路径/参数名" />
</div>
<div class="form-group">
<label for="vulnerability-proof" data-i18n="vulnerabilityModal.proof">证明(POC</label>
<textarea id="vulnerability-proof" rows="5" data-i18n="vulnerabilityModal.proofPlaceholder" data-i18n-attr="placeholder" placeholder="漏洞证明,如请求/响应、截图等"></textarea>
<label for="vulnerability-preconditions" data-i18n="vulnerabilityModal.preconditions">前置条件</label>
<textarea id="vulnerability-preconditions" rows="3" data-i18n="vulnerabilityModal.preconditionsPlaceholder" data-i18n-attr="placeholder" placeholder="登录状态、权限、账号、Header/Cookie、特定数据、环境/版本;无则写无。"></textarea>
</div>
<div class="form-group">
<label for="vulnerability-impact" data-i18n="vulnerabilityModal.impact">影响</label>
<textarea id="vulnerability-impact" rows="3" data-i18n="vulnerabilityModal.impactPlaceholder" data-i18n-attr="placeholder" placeholder="漏洞影响说明"></textarea>
<label for="vulnerability-reproduction-steps"><span data-i18n="vulnerabilityModal.reproductionSteps">复现步骤</span> <span style="color: red;">*</span></label>
<textarea id="vulnerability-reproduction-steps" rows="6" data-i18n="vulnerabilityModal.reproductionStepsPlaceholder" data-i18n-attr="placeholder" placeholder="按 1/2/3 编号,写清入口、参数、payload、执行命令、观察点。"></textarea>
</div>
<div class="form-group">
<label for="vulnerability-recommendation" data-i18n="vulnerabilityModal.recommendation">修复建议</label>
<textarea id="vulnerability-recommendation" rows="3" data-i18n="vulnerabilityModal.recommendationPlaceholder" data-i18n-attr="placeholder" placeholder="修复建议"></textarea>
<label for="vulnerability-evidence"><span data-i18n="vulnerabilityModal.evidence">证据 / POC</span> <span style="color: red;">*</span></label>
<textarea id="vulnerability-evidence" rows="8" data-i18n="vulnerabilityModal.evidencePlaceholder" data-i18n-attr="placeholder" placeholder="原始请求/响应、curl/工具命令、截图说明、日志、DNSLog/回连记录、数据库结果、文件路径、时间戳等。"></textarea>
</div>
<div class="form-group">
<label for="vulnerability-impact"><span data-i18n="vulnerabilityModal.impact">影响</span> <span style="color: red;">*</span></label>
<textarea id="vulnerability-impact" rows="3" data-i18n="vulnerabilityModal.impactPlaceholder" data-i18n-attr="placeholder" placeholder="结合已验证事实说明实际影响,例如越权读取哪些数据。"></textarea>
</div>
<div class="form-group">
<label for="vulnerability-recommendation"><span data-i18n="vulnerabilityModal.recommendation">修复建议</span> <span style="color: red;">*</span></label>
<textarea id="vulnerability-recommendation" rows="3" data-i18n="vulnerabilityModal.recommendationPlaceholder" data-i18n-attr="placeholder" placeholder="写具体修复点和复测标准。"></textarea>
</div>
<div class="form-group">
<label for="vulnerability-retest-notes" data-i18n="vulnerabilityModal.retestNotes">复测方式</label>
<textarea id="vulnerability-retest-notes" rows="3" data-i18n="vulnerabilityModal.retestNotesPlaceholder" data-i18n-attr="placeholder" placeholder="修复后如何验证漏洞已关闭,包括应返回的状态码、错误信息或访问控制结果。"></textarea>
</div>
</div>
<div class="modal-footer">
@@ -4546,6 +4660,20 @@
<textarea id="role-user-prompt" rows="10" data-i18n="roleModal.userPromptPlaceholder" data-i18n-attr="placeholder" placeholder="输入用户提示词,会在用户消息前追加此提示词..."></textarea>
<small class="form-hint" data-i18n="roleModal.userPromptHint">此提示词会追加到用户消息前,用于指导AI的行为。注意:这不会修改系统提示词。</small>
</div>
<div class="form-group">
<label for="role-workflow-id" data-i18n="roleModal.bindWorkflow">绑定图编排流程</label>
<select id="role-workflow-id">
<option value="" data-i18n="roleModal.noWorkflowBind">不绑定流程</option>
</select>
<small class="form-hint" data-i18n="roleModal.bindWorkflowHint">选中流程后,对话页使用该角色会自动触发绑定图;流程字段由图定义 JSON 自由配置。</small>
</div>
<div class="form-group">
<label for="role-workflow-policy" data-i18n="roleModal.workflowPolicy">流程触发策略</label>
<select id="role-workflow-policy">
<option value="auto" data-i18n="roleModal.workflowPolicyAuto">自动触发</option>
<option value="off" data-i18n="roleModal.workflowPolicyOff">关闭</option>
</select>
</div>
<div class="form-group" id="role-tools-section">
<label data-i18n="roleModal.relatedTools">关联的工具(可选)</label>
<div id="role-tools-default-hint" class="role-tools-default-hint" style="display: none;">
@@ -4764,6 +4892,7 @@
<script src="/static/js/webshell.js"></script>
<script src="/static/js/chat-files.js"></script>
<script src="/static/js/tasks.js"></script>
<script src="/static/js/workflows.js"></script>
<script src="/static/js/roles.js"></script>
<script src="/static/js/c2.js"></script>
</body>