feat(deep-research): 强化任务链路可靠性并接入 agentfx 报告助手种子
功能说明:\n- 重构 Deep Research 调度与执行链,补齐 dispatcher、executor、live hub 与依赖注入入口,提升任务下发、执行、回调的稳定性\n- 增强运行时持久化与恢复能力,接入业务映射、任务来源与任务事件日志模型,覆盖持久化与执行可靠性关键路径测试\n- 新增 agentfx 报告助手 seed 与任务报告导入脚本配置,支持离线环境快速初始化与任务流模板回填\n- 完善 deep-research 与 thread_runs 路由参数编排,兼容不同业务映射与运行入口的前后端联动\n- 补充前端 agentfx deep-link 解析,保证 deep-research 场景下的登录跳转与任务上下文一致
This commit is contained in:
1 parent
438772f0b7
commit
5c75e1af16
36 files changed
+3163
-312
No files matched your search
Binary file not shown.
@@ -0,0 +1,95 @@
|
||||
---
|
||||
name: add-audience
|
||||
description: 添加受众目标入库:校验受众JSON载荷,登录任务系统后按完整链路(查重→删除同名受众及其关系表→创建受众目标→创建账号关联)调用接口,并如实回报每个接口的入参与调用结果。
|
||||
allowed-tools:
|
||||
- read_file
|
||||
- bash
|
||||
---
|
||||
|
||||
# 添加受众目标(完整链路入库)
|
||||
|
||||
当且仅当需要把分析产出的受众目标(含关联账号)写入任务系统时使用本 Skill。脚本会自动完成:登录鉴权 → 查重 → 删除同名受众(连带删除 `task-action-pathway-account` 关系表与脆弱点关联)→ 创建受众目标 → 创建账号关联。
|
||||
|
||||
## 前置条件
|
||||
|
||||
先用 `write_file` 把载荷写到 `/mnt/user-data/outputs/add-audience-payload.json`,结构如下(`targetAudience` 也可写作 `groupName`):
|
||||
|
||||
```json
|
||||
{
|
||||
"taskId": 101,
|
||||
"actionId": 201,
|
||||
"audienceList": [
|
||||
{
|
||||
"targetAudience": "18-30岁智能穿戴尝鲜用户",
|
||||
"recReason": "对新形态设备接受度高……",
|
||||
"userList": [
|
||||
{
|
||||
"id": "mock_fa_0001",
|
||||
"name": "极客评测室GeekBench",
|
||||
"platformType": 1,
|
||||
"platform": "facebook",
|
||||
"dataSource": "target",
|
||||
"score": 0.93,
|
||||
"metadata": { "...": "完整user对象会整体存入 frofileInfo" }
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
- `taskId` / `actionId` 必须是整数,等于当前任务与行动 ID。
|
||||
- `userList` 每项是完整 user 对象:`id`、`name` 必填;`platformType` 写入 accountPlat;`accountSource` 优先取 `dataSource`,缺失时回退 `score`;整个对象 JSON 序列化后存入 `frofileInfo`。
|
||||
- `recReason` 中的 `<n>` 引用标记会被自动清洗,无需预处理。
|
||||
|
||||
完整字段定义、账号对象字段表与生成规则,见:
|
||||
|
||||
```text
|
||||
/mnt/skills/public/add-audience/docs/受众JSON生成规范.md
|
||||
```
|
||||
|
||||
(custom 安装时把路径中的 `public` 换成 `custom`;需要生成载荷时,先读该规范再写 JSON。)
|
||||
|
||||
## 接口地址与鉴权配置
|
||||
|
||||
部署时编辑以下文件(安装到 custom 时把路径中的 `public` 换成 `custom`):
|
||||
|
||||
```text
|
||||
/mnt/skills/public/add-audience/config/audience_api.json
|
||||
```
|
||||
|
||||
| 配置项 | 说明 |
|
||||
| --- | --- |
|
||||
| `base_url` | 任务系统地址(到端口,不带 `/api`),如 `http://192.168.1.11:3912` |
|
||||
| `username` / `password` | 登录账号(环境变量 `AUDIENCE_API_USERNAME` / `AUDIENCE_API_PASSWORD` 可覆盖) |
|
||||
| `request.verify_ssl` | **默认 false,已关闭 HTTPS 证书校验**,适配自签名证书内网环境;公网环境建议改回 true |
|
||||
| `request.timeout_seconds` | 每个请求的超时秒数,默认 30 |
|
||||
|
||||
鉴权流程(脚本自动完成):`POST {base_url}/api/auth/login`(body `{username, password}`)取 `access_token`,后续所有请求附加请求头 `Authorization: Bearer {access_token}`。
|
||||
|
||||
## 执行方式
|
||||
|
||||
```bash
|
||||
python /mnt/skills/public/add-audience/scripts/add_audiences.py \
|
||||
--json /mnt/user-data/outputs/add-audience-payload.json
|
||||
```
|
||||
|
||||
可加 `--dry-run` 只校验载荷并打印调用计划,不实际发送。环境变量覆盖地址:`AUDIENCE_API_BASE_URL`。
|
||||
|
||||
## 结果处理(必须如实转述)
|
||||
|
||||
脚本 stdout 是一段 JSON,其中:
|
||||
|
||||
- `入参摘要`:本次传入的 taskId/actionId 与每个受众组(名称、清洗后理由、账号数)——**必须告知用户**。
|
||||
- `calls`:每个接口的 method、url、请求体(密码与 token 已脱敏)、HTTP 状态与响应——**必须汇总告知用户**,特别是删除了哪些同名旧受众及其关系表记录、新建的 designId。
|
||||
- `groups` / `summary` / `人读摘要`:分组结果与统计。
|
||||
|
||||
规则:
|
||||
|
||||
- `success: true`:按 `人读摘要` + `groups` 如实汇报新增/删除数量。
|
||||
- `success: false`:如实说明失败步骤与错误(如登录 401 → 提示检查 config 账号密码),**不要假装调用成功**,也不要静默吞掉失败。
|
||||
- 不要自己用 curl / requests 逐条调接口替代本 Skill;不要在未执行脚本的情况下宣称"已添加"。
|
||||
|
||||
## 同名受众处理策略
|
||||
|
||||
与前端行为一致:同名(`targetAudience` 相同)受众已存在时**先删后建**,删除链为:关系表 `task-action-pathway-account`(按 pathwayId 逐条)→ `task-action-design/deleteDesignVulns?designId=` → `task-action-design/{id}`。如用户只想要"跳过已存在"而非覆盖,需在执行前明确说明并由用户决策。
|
||||
@@ -0,0 +1,31 @@
|
||||
{
|
||||
"_note": "添加受众技能接口配置。base_url 填任务系统完整服务地址(到端口即可,不带 /api);username/password 为登录账号(默认取登录页写死的 admin/ch@user);verify_ssl=false 时关闭 HTTPS 证书校验(自签名证书内网环境用)。环境变量可覆盖:AUDIENCE_API_BASE_URL / AUDIENCE_API_USERNAME / AUDIENCE_API_PASSWORD / AUDIENCE_API_VERIFY_SSL。",
|
||||
"base_url": "http://127.0.0.1:4913",
|
||||
"username": "admin",
|
||||
"password": "ch@user",
|
||||
|
||||
"auth": {
|
||||
"login_path": "/api/auth/login",
|
||||
"token_field": "access_token",
|
||||
"header_name": "Authorization",
|
||||
"header_template": "Bearer {token}"
|
||||
},
|
||||
|
||||
"endpoints": {
|
||||
"login": "/api/auth/login",
|
||||
"design_list": "/api/task/task-action-design",
|
||||
"design_create": "/api/task/task-action-design",
|
||||
"design_delete": "/api/task/task-action-design",
|
||||
"delete_design_vulns": "/api/task/task-action-design/deleteDesignVulns",
|
||||
"pathway_relation_list": "/api/task/task-action-design-pathway-relation",
|
||||
"pathway_account_delete": "/api/task/task-action-pathway-account",
|
||||
"design_vulns_list": "/api/task/task-action-design-vulns",
|
||||
"design_account_create": "/api/task/task-action-design-account"
|
||||
},
|
||||
|
||||
"request": {
|
||||
"timeout_seconds": 30,
|
||||
"verify_ssl": false,
|
||||
"extra_headers": {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
# 受众群体 JSON 生成规范(add-audience 载荷)
|
||||
|
||||
本规范规定大模型生成「受众群体入库载荷」时的 JSON 格式。生成后写入文件,交由 `scripts/add_audiences.py --json <文件>` 执行入库。
|
||||
|
||||
## 一、整体结构
|
||||
|
||||
```json
|
||||
{
|
||||
"taskId": 101,
|
||||
"actionId": 201,
|
||||
"audienceList": [
|
||||
{
|
||||
"targetAudience": "18-30岁智能穿戴尝鲜用户",
|
||||
"recReason": "对新形态设备接受度高<2>,预热期讨论参与意愿强,是首发销量的核心贡献人群",
|
||||
"userList": [
|
||||
{
|
||||
"id": "mock_fa_0001",
|
||||
"name": "极客评测室GeekBench",
|
||||
"platformType": 1,
|
||||
"platform": "facebook",
|
||||
"followers_count": 285200,
|
||||
"interactions": 12713,
|
||||
"issue": "真无线耳机;手机周边;数码开箱;",
|
||||
"dataSource": "target",
|
||||
"score": 0.9276,
|
||||
"page_content": "数码科技领域创作者「极客评测室GeekBench」……",
|
||||
"metadata": {
|
||||
"screen_name": "极客评测室GeekBench",
|
||||
"user_id": "mock_fa_0001",
|
||||
"平台": "facebook",
|
||||
"发文": [],
|
||||
"用户画像": { "粉丝数": 285200, "内容垂类": "数码科技" }
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## 二、字段定义
|
||||
|
||||
### 顶层
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `taskId` | integer | ✅ | 当前任务 ID。**必须取自用户上下文/前端传入,禁止编造**;未知时先向用户询问 |
|
||||
| `actionId` | integer | ✅ | 当前行动 ID。同上 |
|
||||
| `audienceList` | array | ✅ | 受众组数组,**至少 1 项**;按推荐优先级排序(S 级在前) |
|
||||
|
||||
### audienceList 每项
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `targetAudience` | string | ✅ | 受众群体名称。去掉首尾空格后非空;**同一载荷内不得重名**;简洁(建议 ≤ 20 字),如「中高端降噪耳机通勤党」。不要写 `groupName`,统一用本字段 |
|
||||
| `recReason` | string | 推荐 | 入选理由,一两句话。文本中的 `<数字>` 引用标记、顿号、「引用」、空括号会被脚本自动清洗,无需预处理;但也不要故意堆这些符号 |
|
||||
| `userList` | array | ✅ | 该群体关联的账号数组,**必须为数组**(允许空数组 `[]`,表示只建群体不挂账号) |
|
||||
|
||||
### userList 每项(账号对象)
|
||||
|
||||
| 字段 | 类型 | 必填 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| `id` | string\|number | ✅ | 账号唯一标识 → 写入接口字段 `accountId` |
|
||||
| `name` | string | ✅ | 账号名 → 写入 `accountName` |
|
||||
| `platformType` | integer | 建议 | 平台编码 → 写入 `accountPlat`(facebook=1、x=2、instagram=3、threads=5、youtube=6、tiktok=7) |
|
||||
| `platform` | string | 建议 | 平台名(供人读) |
|
||||
| `followers_count` | integer | 建议 | 粉丝数 |
|
||||
| `interactions` | integer | 可选 | 近 30 天均篇互动 |
|
||||
| `issue` | string | 可选 | 关注话题串,如 `"真无线耳机;数码评测;"` |
|
||||
| `dataSource` | string | 建议 | 数据来源(`target` / `social` / `AI_LLM`)→ 优先写入 `accountSource` |
|
||||
| `score` | number 0-1 | 可选 | 相关度分数;**仅当无 `dataSource` 时**作为 `accountSource` 的回退值 |
|
||||
| `page_content` | string | 建议 | 账号描述正文 |
|
||||
| `metadata` | object | 强烈建议 | 完整画像对象(`用户画像`、`发文` 等)。**整个 user 对象会原样 JSON 序列化存入 `frofileInfo`**,字段越全,后续页面回显与二次分析越完整 |
|
||||
|
||||
## 三、生成规则(生成侧模型必须遵守)
|
||||
|
||||
1. **账号必须来自真实数据源**:`userList` 的账号对象必须从账号池(如 `product-account-matching` 技能的 `accounts.json`,用 `query_accounts.py --full` 取完整档案)或对话引用中**原样复制**,禁止虚构 id/name/粉丝数等任何字段。
|
||||
2. **ID 不编造**:`taskId` / `actionId` 只能取自当前对话上下文;用户没给就先问,不要猜。
|
||||
3. **同名覆盖语义要告知**:目标系统里已存在同名 `targetAudience` 时,执行脚本会**先删除旧受众(连带其传播节点关系表与脆弱点关联)再新建**。生成前如已知会覆盖,需在回复中向用户说明。
|
||||
4. **群体划分有依据**:`targetAudience` 的划分角度(垂类/场景/人群)与 `recReason` 的理由需与账号池数据(内容垂类、关注话题、发文、受众画像)对得上,理由中可引用具体账号名。
|
||||
5. **输出为纯 JSON**:写入 `.json` 文件时最外层就是对象,不要包裹 markdown 代码块、注释或多余文本。
|
||||
6. **数量适中**:单次建议 2-6 个受众组,每组 3-15 个账号;超出时按 `score` 与推荐优先级裁剪。
|
||||
|
||||
## 四、校验失败常见原因
|
||||
|
||||
脚本会前置校验载荷,以下错误会直接退出(不做任何写入):
|
||||
|
||||
- `taskId` / `actionId` 不是整数(如传了 `"101"` 字符串)
|
||||
- `audienceList` 为空或缺失
|
||||
- 某组 `targetAudience`(或 `groupName`)为空/纯空格
|
||||
- 某组 `userList` 不是数组(如传了对象或字符串)
|
||||
@@ -0,0 +1,82 @@
|
||||
# add-audience 技能 · 安装与调用说明
|
||||
|
||||
把「添加受众目标」的完整接口链路封装给大模型调用:模型写好受众 JSON → 脚本登录任务系统 → 自动执行查重、删除同名(含关系表)、创建受众与账号关联 → 把每个接口的入参与调用结果如实回报给用户。
|
||||
|
||||
## 一、安装
|
||||
|
||||
**方式 A(推荐)**:在技能管理界面上传 `add-audience.skill`(本质是 zip,内含 `add-audience/` 目录:SKILL.md + config + scripts)。
|
||||
|
||||
**方式 B(手工)**:把 `add-audience/` 目录解压到 DeerFlow 后端的 `skills/custom/` 下,然后调用一次 `POST /skills/reconcile-custom` 收编注册。
|
||||
|
||||
安装后技能名:`add-audience`。若安装到 public,文档中的 `/mnt/skills/custom/...` 路径对应换成 `/mnt/skills/public/...`。
|
||||
|
||||
## 二、配置(config/audience_api.json)
|
||||
|
||||
| 配置项 | 默认值 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `base_url` | `http://127.0.0.1:4913` | 任务系统地址(到端口,不带 `/api`);按实际环境修改,如 `http://192.168.1.11:3912` |
|
||||
| `username` / `password` | `admin` / `ch@user` | 登录账号,取自前端登录页写死的默认值,可自行修改 |
|
||||
| `auth.*` | `/api/auth/login` + `Authorization: Bearer {token}` | 登录路径、token 字段与请求头拼装规则,一般不用动 |
|
||||
| `endpoints.*` | `/api/task/task-action-design` 等 | 完整链路涉及的全部接口路径 |
|
||||
| `request.verify_ssl` | `false` | **已关闭 HTTPS 证书校验**,适配自签证书内网;公网环境建议改 `true` |
|
||||
| `request.timeout_seconds` | `30` | 单请求超时 |
|
||||
|
||||
环境变量覆盖:`AUDIENCE_API_BASE_URL`、`AUDIENCE_API_USERNAME`、`AUDIENCE_API_PASSWORD`、`AUDIENCE_API_VERIFY_SSL`。
|
||||
|
||||
## 三、鉴权与请求头
|
||||
|
||||
1. 脚本先 `POST {base_url}/api/auth/login`,请求体 `{"username": "...", "password": "..."}`;
|
||||
2. 从响应取 `access_token`;
|
||||
3. 后续所有业务请求附加请求头 `Authorization: Bearer {access_token}`(即 `auth.header_name` + `auth.header_template`)。
|
||||
|
||||
证书校验通过 Python ssl 上下文关闭(`verify_ssl=false` 时),HTTPS 自签名证书可直接访问。
|
||||
|
||||
## 四、完整调用链
|
||||
|
||||
对载荷中的每个受众组依次执行:
|
||||
|
||||
| 步骤 | 接口 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| 1 | `POST /api/auth/login` | 登录换 token |
|
||||
| 2 | `GET /api/task/task-action-design?filter=taskId&filter=actionId` | 查当前行动下已有受众(查重) |
|
||||
| 3a | `GET /api/task/task-action-design-pathway-relation?filter=actionId` | 查关系表,找同名受众的 pathwayId |
|
||||
| 3b | `DELETE /api/task/task-action-pathway-account/{pathwayId}` | **逐条删除关系表记录**(同名时) |
|
||||
| 3c | `DELETE /api/task/task-action-design/deleteDesignVulns?designId={id}` | 删除受众关联脆弱点(同名时) |
|
||||
| 3d | `DELETE /api/task/task-action-design/{id}` | 删除同名旧受众本身 |
|
||||
| 4 | `POST /api/task/task-action-design` | 创建受众目标,`parentTargetAudience` 固定 `大模型生成`,`recReason` 自动清洗 `<n>` 引用标记 |
|
||||
| 5 | `POST /api/task/task-action-design-account`(每账号一次) | 字段映射:`accountId←user.id`、`accountName←user.name`、`accountPlat←user.platformType`、`accountSource←user.dataSource`(缺失回退 `user.score`)、`frofileInfo←整个user对象JSON`、`designId←步骤4返回id` |
|
||||
|
||||
## 五、载荷格式
|
||||
|
||||
```json
|
||||
{
|
||||
"taskId": 101,
|
||||
"actionId": 201,
|
||||
"audienceList": [
|
||||
{
|
||||
"targetAudience": "18-30岁智能穿戴尝鲜用户",
|
||||
"recReason": "对新形态设备接受度高……",
|
||||
"userList": [ { "id": "...", "name": "...", "platformType": 1, "dataSource": "target", "...": "完整user对象" } ]
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
`targetAudience` 也可写作 `groupName`(对齐前端对话链路字段名)。
|
||||
|
||||
## 六、输出与验证
|
||||
|
||||
脚本 stdout 输出单段 JSON:
|
||||
|
||||
- `入参摘要`:taskId/actionId 与各受众组(名称、清洗后理由、账号数)
|
||||
- `calls`:每个接口的 method/url/请求体(密码、token 脱敏)/HTTP 状态/响应
|
||||
- `groups`:每组结果(designId、新增账号数、删除的旧受众及关系表记录数)
|
||||
- `summary` / `人读摘要`:统计与人读结论
|
||||
|
||||
验证是否真正入库:`GET {base_url}/api/task/task-action-design?filter=taskId||$eq||{taskId}` 与 `.../task-action-design-account?...` 应能看到新建记录。
|
||||
|
||||
## 七、注意事项
|
||||
|
||||
- 密码明文存于 config,部署环境请控制文件访问权限,或改用环境变量注入。
|
||||
- 同名受众默认「先删后建」(与前端对话链路一致);需要跳过语义时应先改造脚本再加策略参数。
|
||||
- 业务失败(如创建受众返回非 201)时脚本继续处理其余受众组,整体 `success=false`,退出码 2;登录失败立即退出,退出码 1。
|
||||
+335
@@ -0,0 +1,335 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
添加受众目标入库脚本(完整链路)。
|
||||
|
||||
链路(与前端 block-chat-audience.taskAddActionDesign / audience-content.taskActionDesignDel 一致):
|
||||
1. POST {login} 登录换取 access_token(Authorization: Bearer)
|
||||
2. GET {design_list}?filter=taskId&filter=actionId 查询当前行动下已有受众(查重)
|
||||
3. 同名受众存在时执行删除链:
|
||||
GET {pathway_relation_list}?filter=actionId 找出该受众的 pathwayId(关系表)
|
||||
DELETE {pathway_account_delete}/{pathwayId} 删关系表 task-action-pathway-account
|
||||
DELETE {delete_design_vulns}?designId={id} 删受众关联脆弱点
|
||||
DELETE {design_delete}/{id} 删受众目标本身
|
||||
4. POST {design_create} 创建受众目标(parentTargetAudience=大模型生成)
|
||||
5. POST {design_account_create} ×N 逐个创建受众-账号关联
|
||||
|
||||
用法:
|
||||
python add_audiences.py --json payload.json [--dry-run] [--config ../config/audience_api.json]
|
||||
|
||||
payload.json 结构:
|
||||
{
|
||||
"taskId": 101,
|
||||
"actionId": 201,
|
||||
"audienceList": [
|
||||
{ "targetAudience": "群体名", // 也可用 groupName
|
||||
"recReason": "推荐理由", // 会自动清洗 <n> 引用标记
|
||||
"userList": [ {完整user对象} ] } // 取 id/name/platformType/dataSource/score,整个对象进 frofileInfo
|
||||
]
|
||||
}
|
||||
|
||||
输出:一段 JSON(stdout),包含每个接口的入参与调用结果(密码与 token 已脱敏),
|
||||
以及分组结果与汇总,供模型如实转述给用户。
|
||||
"""
|
||||
import argparse
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import ssl
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
DEFAULT_CONFIG = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "config", "audience_api.json")
|
||||
|
||||
# 环境变量覆盖名
|
||||
ENV_OVERRIDES = {
|
||||
"base_url": "AUDIENCE_API_BASE_URL",
|
||||
"username": "AUDIENCE_API_USERNAME",
|
||||
"password": "AUDIENCE_API_PASSWORD",
|
||||
"verify_ssl": "AUDIENCE_API_VERIFY_SSL",
|
||||
}
|
||||
|
||||
|
||||
def load_config(path: str) -> dict:
|
||||
cfg = json.loads(open(path, encoding="utf-8").read())
|
||||
for key, env in ENV_OVERRIDES.items():
|
||||
if os.environ.get(env):
|
||||
raw = os.environ[env]
|
||||
if key == "verify_ssl":
|
||||
cfg.setdefault("request", {})
|
||||
cfg["request"]["verify_ssl"] = raw.lower() in ("1", "true", "yes")
|
||||
else:
|
||||
cfg[key] = raw
|
||||
# 兼容 "$ENV_VAR" 占位值写法
|
||||
def resolve(v):
|
||||
if isinstance(v, str) and v.startswith("$") and re.fullmatch(r"\$[A-Z0-9_]+", v):
|
||||
return os.environ.get(v[1:], "")
|
||||
return v
|
||||
|
||||
def walk(o):
|
||||
if isinstance(o, dict):
|
||||
return {k: walk(v) for k, v in o.items()}
|
||||
if isinstance(o, list):
|
||||
return [walk(v) for v in o]
|
||||
return resolve(o)
|
||||
|
||||
return walk(cfg)
|
||||
|
||||
|
||||
def clean_reason(text) -> str:
|
||||
"""复刻前端 recReason 清洗:<n> 引用标记、顿号、「引用」、空括号。"""
|
||||
if not isinstance(text, str):
|
||||
return ""
|
||||
text = re.sub(r"<(\d+)>", "", text)
|
||||
return text.replace("、", "").replace("引用", "").replace("()", "")
|
||||
|
||||
|
||||
class ApiClient:
|
||||
def __init__(self, cfg: dict, dry_run: bool = False):
|
||||
self.base = cfg["base_url"].rstrip("/")
|
||||
self.cfg = cfg
|
||||
self.dry_run = dry_run
|
||||
self.headers = {"Content-Type": "application/json"}
|
||||
self.headers.update(cfg.get("request", {}).get("extra_headers") or {})
|
||||
self.timeout = cfg.get("request", {}).get("timeout_seconds", 30)
|
||||
verify = cfg.get("request", {}).get("verify_ssl", True)
|
||||
self.ctx = ssl.create_default_context() if verify else ssl._create_unverified_context()
|
||||
self.calls = []
|
||||
|
||||
def _log(self, step, method, url, request, status, response):
|
||||
self.calls.append({
|
||||
"step": step, "method": method, "url": url,
|
||||
"request": request, "response": {"status": status, "body": response},
|
||||
})
|
||||
|
||||
def request(self, step, method, path, query=None, body=None, auth=True):
|
||||
"""执行一次 HTTP 请求并记录。返回 (status, 解析后的响应)。"""
|
||||
url = self.base + path
|
||||
if query:
|
||||
url += "?" + urllib.parse.urlencode(query, doseq=True)
|
||||
payload = json.dumps(body, ensure_ascii=False).encode("utf-8") if body is not None else None
|
||||
display_body = self._redact(copy.deepcopy(body))
|
||||
req_headers = dict(self.headers)
|
||||
if auth:
|
||||
req_headers[self.cfg["auth"]["header_name"]] = "***已附带鉴权头***"
|
||||
|
||||
if self.dry_run:
|
||||
self._log(step, method, url, {"query": query, "body": display_body}, 0, "[dry-run] 未实际发送")
|
||||
return 0, None
|
||||
|
||||
req = urllib.request.Request(url, data=payload, method=method)
|
||||
for k, v in req_headers.items():
|
||||
if v == "***已附带鉴权头***":
|
||||
req.add_header(k, self._auth_header_value())
|
||||
else:
|
||||
req.add_header(k, v)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=self.timeout, context=self.ctx) as resp:
|
||||
status, text = resp.status, resp.read().decode("utf-8", "replace")
|
||||
except urllib.error.HTTPError as e:
|
||||
status, text = e.code, e.read().decode("utf-8", "replace")
|
||||
except Exception as e: # 网络层错误
|
||||
self._log(step, method, url, {"query": query, "body": display_body}, -1, str(e))
|
||||
raise
|
||||
|
||||
try:
|
||||
parsed = json.loads(text) if text else None
|
||||
except ValueError:
|
||||
parsed = text[:300]
|
||||
self._log(step, method, url, {"query": query, "body": display_body}, status, parsed if not isinstance(parsed, str) else parsed)
|
||||
return status, parsed
|
||||
|
||||
def _auth_header_value(self) -> str:
|
||||
return self.cfg["auth"]["header_template"].format(token=getattr(self, "token", ""))
|
||||
|
||||
@staticmethod
|
||||
def _redact(body):
|
||||
if isinstance(body, dict):
|
||||
for k in list(body.keys()):
|
||||
if k in ("password",):
|
||||
body[k] = "***"
|
||||
return body
|
||||
|
||||
|
||||
def ep(cfg, key) -> str:
|
||||
return cfg["endpoints"][key]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="添加受众目标(完整链路:登录→查重→删同名及关系表→建受众→建账号关联)")
|
||||
parser.add_argument("--json", required=True, help="载荷 JSON 文件路径")
|
||||
parser.add_argument("--config", default=DEFAULT_CONFIG, help="接口配置文件路径")
|
||||
parser.add_argument("--dry-run", action="store_true", help="只校验与打印调用计划,不实际发送")
|
||||
args = parser.parse_args()
|
||||
|
||||
result = {
|
||||
"success": False, "dry_run": args.dry_run,
|
||||
"配置": {}, "入参摘要": {}, "登录": None, "groups": [], "summary": {}, "error": None,
|
||||
}
|
||||
|
||||
try:
|
||||
cfg = load_config(args.config)
|
||||
except Exception as e:
|
||||
result["error"] = f"配置加载失败:{e}"
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
# ---- 载荷校验 ----
|
||||
try:
|
||||
payload = json.loads(open(args.json, encoding="utf-8").read())
|
||||
except Exception as e:
|
||||
result["error"] = f"载荷 JSON 读取失败:{e}"
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
task_id = payload.get("taskId")
|
||||
action_id = payload.get("actionId")
|
||||
groups_in = payload.get("audienceList") or []
|
||||
errors = []
|
||||
if not isinstance(task_id, int) or not isinstance(action_id, int):
|
||||
errors.append("taskId / actionId 必须为整数")
|
||||
if not groups_in:
|
||||
errors.append("audienceList 不能为空")
|
||||
for i, g in enumerate(groups_in):
|
||||
name = (g.get("targetAudience") or g.get("groupName") or "").strip()
|
||||
if not name:
|
||||
errors.append(f"audienceList[{i}] 缺少群体名(targetAudience/groupName)")
|
||||
if not isinstance(g.get("userList"), list):
|
||||
errors.append(f"audienceList[{i}] 缺少 userList 数组")
|
||||
if errors:
|
||||
result["error"] = "载荷校验失败:" + ";".join(errors)
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
result["配置"] = {
|
||||
"base_url": cfg["base_url"], "username": cfg["username"],
|
||||
"verify_ssl": cfg.get("request", {}).get("verify_ssl", True),
|
||||
"timeout_seconds": cfg.get("request", {}).get("timeout_seconds", 30),
|
||||
}
|
||||
result["入参摘要"] = {
|
||||
"taskId": task_id, "actionId": action_id,
|
||||
"audienceList": [
|
||||
{"targetAudience": (g.get("targetAudience") or g.get("groupName", "")).strip(),
|
||||
"recReason清洗后": clean_reason(g.get("recReason", "")),
|
||||
"账号数": len(g.get("userList") or [])}
|
||||
for g in groups_in
|
||||
],
|
||||
}
|
||||
|
||||
client = ApiClient(cfg, dry_run=args.dry_run)
|
||||
|
||||
# ---- 1. 登录 ----
|
||||
login_status, login_resp = client.request("登录", "POST", ep(cfg, "login"),
|
||||
body={"username": cfg["username"], "password": cfg["password"]}, auth=False)
|
||||
if login_status not in (200, 201):
|
||||
result["登录"] = f"失败(HTTP {login_status})"
|
||||
result["error"] = f"登录失败:HTTP {login_status},请检查 config/audience_api.json 的 base_url 与账号密码"
|
||||
result["calls"] = client.calls
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
token = (login_resp or {}).get(cfg["auth"]["token_field"]) if isinstance(login_resp, dict) else None
|
||||
if not token:
|
||||
result["登录"] = "失败(响应中无 " + cfg["auth"]["token_field"] + ")"
|
||||
result["error"] = f"登录响应缺少 {cfg['auth']['token_field']} 字段:{login_resp}"
|
||||
result["calls"] = client.calls
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
client.token = token
|
||||
result["登录"] = f"成功(已获取 {cfg['auth']['token_field']},后续请求头 {cfg['auth']['header_name']}: {cfg['auth']['header_template']})"
|
||||
|
||||
# ---- 2. 查询已有受众(查重) ----
|
||||
q = {"filter": [f"taskId||$eq||{task_id}", f"actionId||$eq||{action_id}"], "limit": 99999}
|
||||
status, designs_resp = client.request("查询已有受众", "GET", ep(cfg, "design_list"), query=q)
|
||||
designs = (designs_resp or {}).get("data") or [] if isinstance(designs_resp, dict) else []
|
||||
if status != 200:
|
||||
result["error"] = f"查询已有受众失败:HTTP {status}"
|
||||
result["calls"] = client.calls
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
# 关系表(一次查出整个行动的 pathway 关联)
|
||||
rel_status, rel_resp = client.request("查询受众-传播节点关系表", "GET", ep(cfg, "pathway_relation_list"),
|
||||
query={"filter": [f"actionId||$eq||{action_id}"], "limit": 99999})
|
||||
relations = (rel_resp or {}).get("data") or [] if isinstance(rel_resp, dict) else []
|
||||
|
||||
summary = {"新增受众": 0, "新增账号关联": 0, "删除同名旧受众": 0, "删除关系表记录": 0, "删除脆弱点关联": 0}
|
||||
all_ok = True
|
||||
|
||||
for g in groups_in:
|
||||
name = (g.get("targetAudience") or g.get("groupName")).strip()
|
||||
group = {"targetAudience": name, "designId": None, "新增账号": 0, "deletedOld": [], "ok": False, "error": None}
|
||||
|
||||
# ---- 3. 同名删除链 ----
|
||||
for old in [d for d in designs if str(d.get("targetAudience", "")).strip() == name]:
|
||||
old_id = old.get("id")
|
||||
pathway_ids = [r.get("pathwayId") for r in relations if r.get("designId") == old_id and r.get("pathwayId") is not None]
|
||||
for pid in pathway_ids:
|
||||
s, _ = client.request("删除关系表记录", "DELETE", f"{ep(cfg, 'pathway_account_delete')}/{pid}")
|
||||
if s in (200, 204):
|
||||
summary["删除关系表记录"] += 1
|
||||
s, _ = client.request("删除受众关联脆弱点", "DELETE", ep(cfg, "delete_design_vulns"),
|
||||
query={"designId": old_id})
|
||||
if s in (200, 204):
|
||||
summary["删除脆弱点关联"] += 1
|
||||
s, _ = client.request("删除同名旧受众", "DELETE", f"{ep(cfg, 'design_delete')}/{old_id}")
|
||||
if s in (200, 204):
|
||||
summary["删除同名旧受众"] += 1
|
||||
group["deletedOld"].append({"id": old_id, "删除关系表记录数": len(pathway_ids)})
|
||||
|
||||
# ---- 4. 创建受众目标 ----
|
||||
design_body = {
|
||||
"taskId": task_id, "actionId": action_id,
|
||||
"parentTargetAudience": "大模型生成",
|
||||
"targetAudience": name,
|
||||
"recReason": clean_reason(g.get("recReason", "")),
|
||||
}
|
||||
s, created = client.request("创建受众目标", "POST", ep(cfg, "design_create"), body=design_body)
|
||||
if s not in (200, 201) or not isinstance(created, dict) or "id" not in created:
|
||||
group["error"] = f"创建受众目标失败:HTTP {s},响应:{created}"
|
||||
result["groups"].append(group)
|
||||
all_ok = False
|
||||
continue
|
||||
design_id = created["id"]
|
||||
group["designId"] = design_id
|
||||
summary["新增受众"] += 1
|
||||
|
||||
# ---- 5. 创建账号关联 ----
|
||||
for user in g.get("userList") or []:
|
||||
account_source = user.get("dataSource") if user.get("dataSource") is not None else user.get("score")
|
||||
account_body = {
|
||||
"taskId": task_id, "actionId": action_id,
|
||||
"accountId": user.get("id"),
|
||||
"accountName": user.get("name"),
|
||||
"accountPlat": user.get("platformType"),
|
||||
"accountSource": account_source,
|
||||
"frofileInfo": json.dumps(user, ensure_ascii=False),
|
||||
"designId": design_id,
|
||||
}
|
||||
s, resp = client.request("创建受众-账号关联", "POST", ep(cfg, "design_account_create"), body=account_body)
|
||||
if s in (200, 201):
|
||||
group["新增账号"] += 1
|
||||
summary["新增账号关联"] += 1
|
||||
else:
|
||||
group.setdefault("账号失败", []).append({"accountName": user.get("name"), "status": s, "response": resp})
|
||||
all_ok = False
|
||||
|
||||
group["ok"] = "账号失败" not in group
|
||||
result["groups"].append(group)
|
||||
|
||||
result["summary"] = summary
|
||||
result["success"] = all_ok
|
||||
result["calls"] = client.calls
|
||||
result["人读摘要"] = (
|
||||
f"{'[演练]' if args.dry_run else ''}共处理 {len(groups_in)} 个受众组:"
|
||||
f"新增受众 {summary['新增受众']} 个、账号关联 {summary['新增账号关联']} 条;"
|
||||
f"删除同名旧受众 {summary['删除同名旧受众']} 个(连带关系表记录 {summary['删除关系表记录']} 条、脆弱点关联 {summary['删除脆弱点关联']} 处)。"
|
||||
+ ("全部成功。" if all_ok else "存在失败项,详见 groups/error 字段。")
|
||||
)
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(0 if all_ok else 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user