mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-11-30 16:02:58 +08:00
feat: 精确会话自动化规则
This commit is contained in:
1 parent
95384a4a92
commit
069d836dea
25 files changed
+1710
-122
No files matched your search
@@ -50,6 +50,17 @@ export interface AutomationRule {
|
||||
actions: AutomationAction[]
|
||||
/** 同一规则在同一会话内的最小触发间隔(秒)。防刷屏。 */
|
||||
cooldownSeconds: number
|
||||
/**
|
||||
* 命中后**等多久再发「回复确认」**(秒)。0 = 立刻回复。
|
||||
*
|
||||
* 与 `cooldownSeconds` 同口径(秒):界面上是秒、落盘也是秒,
|
||||
* 只有 `AutomationActionRunner` 在等待前换算成毫秒。别在这里存毫秒,
|
||||
* 否则「触发间隔」和「回复等待」两个字段一个秒一个毫秒,迟早有人读错。
|
||||
*
|
||||
* **旧版 rules.json 里没有这个字段**:读取方一律按
|
||||
* `normalizeReplyDelaySeconds()` 处理 ⇒ 缺省即默认 2 秒,不是「不等待」。
|
||||
*/
|
||||
replyDelaySeconds: number
|
||||
createdAt: number
|
||||
updatedAt: number
|
||||
}
|
||||
@@ -57,6 +68,7 @@ export interface AutomationRule {
|
||||
/** 执行步骤状态。`skipped` 用于「上一步失败所以这一步不做」。 */
|
||||
export type AutomationStepStatus = 'pending' | 'running' | 'success' | 'failed' | 'skipped'
|
||||
|
||||
/** 执行步骤键。只有**真正进入执行**的规则才会有步骤。 */
|
||||
export type AutomationStepKey = 'received' | 'matched' | 'reply' | 'report' | 'send'
|
||||
|
||||
export interface AutomationStep {
|
||||
@@ -69,8 +81,37 @@ export interface AutomationStep {
|
||||
durationMs?: number
|
||||
/** 用户可读的错误信息。**禁止**含 wxid / 文件路径 / 原始 payload。 */
|
||||
error?: string
|
||||
/**
|
||||
* 被跳过时的原因(给用户看的一句话)。
|
||||
*
|
||||
* 与 `error` 分开:`skipped` 不是错误,硬塞进 `error` 会让界面把它当失败渲染。
|
||||
* **禁止**含 localId / serverId / dedup key 这类工程术语。
|
||||
*/
|
||||
skipReason?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次 execution 的最终状态。
|
||||
*
|
||||
* **规则(不要改口径)**
|
||||
*
|
||||
* | 情况 | status |
|
||||
* | --- | --- |
|
||||
* | 所有**启用的必需 action** 成功 | `success` |
|
||||
* | 某个必需 action `failed` | `failed` |
|
||||
* | 后续 action 因前置失败未执行(step 记 `skipped`) | `failed`(整个规则没跑完整) |
|
||||
* | 规则本来就没启用某个 action | 该 step `skipped`,**不影响**整体结果 |
|
||||
* | 消息**没有命中**规则 | **不创建 execution** |
|
||||
* | 落在 rule conversation gate 的阻塞窗口内 | **不创建 execution** |
|
||||
*
|
||||
* ⚠️ **阻塞窗口内的消息不留任何痕迹**:不匹配、不执行、不回复、**不写 execution**。
|
||||
* 从用户产品视角,那段时间这条规则就是「没处理这些消息」—— 不是「跳过」。
|
||||
* 门语义见 `automation-service.ts` 的 `ConversationRuleGate`。
|
||||
*
|
||||
* ⇒ 所以 execution 级**没有** `skipped`:一次 execution 只要被创建出来,就说明它真的跑过,
|
||||
* 结果非成功即失败。**步骤级**的 `skipped`(`AutomationStepStatus`)是另一回事,仍然保留 ——
|
||||
* 那是「这次执行里某一步没做(没配 / 前置失败)」。
|
||||
*/
|
||||
export type AutomationExecutionStatus = 'running' | 'success' | 'failed'
|
||||
|
||||
export interface AutomationExecution {
|
||||
@@ -108,6 +149,39 @@ export const AUTOMATION_SEND_PURPOSE = {
|
||||
/** 审计日志里的来源标识(`WechatActionOrigin`)。 */
|
||||
export const AUTOMATION_SEND_ORIGIN = 'automation'
|
||||
|
||||
/**
|
||||
* 「回复确认」前的默认等待(秒)。
|
||||
*
|
||||
* 规则一命中就立刻回复,看起来就是个机器人 —— 消息刚到、回复就到了。
|
||||
* 这个等待把回复推到一个更像人的时间点上,所以它是**规则自己的一项执行参数**
|
||||
* (和 `cooldownSeconds` 同口径:秒),在「编辑自动化 → 3 · 触发后执行」里配。
|
||||
*
|
||||
* **只作用于「回复确认」这一步**:日报生成 + 图片发送本来就以秒计,
|
||||
* 不再叠加等待,否则整条链路会慢得让人以为卡住了。
|
||||
*/
|
||||
export const AUTOMATION_REPLY_DELAY_DEFAULT_SECONDS = 2
|
||||
|
||||
/** 等待上限。再长用户会以为功能坏了,所以封顶而不是无限放开。 */
|
||||
export const AUTOMATION_REPLY_DELAY_MAX_SECONDS = 60
|
||||
|
||||
/**
|
||||
* 把任意输入收敛成合法的等待秒数。
|
||||
*
|
||||
* 口径(三条都是刻意的):
|
||||
* - **空值 / 非法值 → 默认值**,而不是 0 —— 填错了不该静默变成「不等待」;
|
||||
* - **0 是合法值**(显式表示立刻回复),负数与它同归 0;
|
||||
* - 超过上限按上限截断,避免有人填 10 分钟。
|
||||
*/
|
||||
export function normalizeReplyDelaySeconds(value: unknown): number {
|
||||
if (value === null || value === undefined || value === '') {
|
||||
return AUTOMATION_REPLY_DELAY_DEFAULT_SECONDS
|
||||
}
|
||||
const parsed = typeof value === 'number' ? value : Number(String(value).trim())
|
||||
if (!Number.isFinite(parsed)) return AUTOMATION_REPLY_DELAY_DEFAULT_SECONDS
|
||||
if (parsed <= 0) return 0
|
||||
return Math.min(AUTOMATION_REPLY_DELAY_MAX_SECONDS, Math.round(parsed))
|
||||
}
|
||||
|
||||
/** 所有自动化行为的 `sourceId` / `executionId` 口径:一次执行一个 id。 */
|
||||
export function automationIdempotencyKey(
|
||||
kind: keyof typeof AUTOMATION_SEND_PURPOSE,
|
||||
@@ -125,6 +199,8 @@ export interface AutomationRuleDraft {
|
||||
conditions: AutomationConditions
|
||||
actions: AutomationAction[]
|
||||
cooldownSeconds: number
|
||||
/** 命中后等多久再回复确认(秒)。0 = 立刻回复。 */
|
||||
replyDelaySeconds: number
|
||||
}
|
||||
|
||||
/** 顶部状态条数据(全部来自 main 侧真实能力,UI 不做平台判断)。 */
|
||||
@@ -197,6 +273,18 @@ export const AUTOMATION_STEP_LABELS: Record<AutomationStepKey, string> = {
|
||||
send: '发送日报图片'
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行结果的展示文案。
|
||||
*
|
||||
* `skipped` 是**中性**状态(命中了但被规则自身的冷却 / 去重拦下),
|
||||
* 不是失败 —— 界面不能用红色渲染它。
|
||||
*/
|
||||
export const AUTOMATION_EXECUTION_STATUS_LABELS: Record<AutomationExecutionStatus, string> = {
|
||||
running: '执行中',
|
||||
success: '成功',
|
||||
failed: '失败'
|
||||
}
|
||||
|
||||
export const KEYWORD_MATCH_MODE_LABELS: Record<KeywordMatchMode, string> = {
|
||||
contains: '包含',
|
||||
exact: '完全匹配',
|
||||
@@ -281,6 +369,7 @@ export function createDefaultDailyReportRule(now: number): AutomationRule {
|
||||
{ type: 'sendReportImage', enabled: true }
|
||||
],
|
||||
cooldownSeconds: 60,
|
||||
replyDelaySeconds: AUTOMATION_REPLY_DELAY_DEFAULT_SECONDS,
|
||||
createdAt: now,
|
||||
updatedAt: now
|
||||
}
|
||||
@@ -323,6 +412,7 @@ export function normalizeRuleDraft(input: unknown, fallbackName = '未命名自
|
||||
ignoreSelf: conditions.ignoreSelf !== false
|
||||
},
|
||||
actions: normalizedActions,
|
||||
cooldownSeconds: Number.isFinite(cooldown) ? Math.max(0, Math.floor(cooldown)) : 60
|
||||
cooldownSeconds: Number.isFinite(cooldown) ? Math.max(0, Math.floor(cooldown)) : 60,
|
||||
replyDelaySeconds: normalizeReplyDelaySeconds(raw.replyDelaySeconds)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,14 @@ export type WechatActionOrigin =
|
||||
| 'member_monitor'
|
||||
| 'scheduled_report'
|
||||
| 'user_tts'
|
||||
/**
|
||||
* 用户在界面上**手动**触发的发送(发送日报图片、给群发统计文本等)。
|
||||
*
|
||||
* 立项规则:**所有发送都必须经过 `WechatActionGateway`**。手动发送在改造前是
|
||||
* 从 IPC 直连 `PersonalWechatSendService` 的,因此没有审计记录、没有幂等、
|
||||
* 也不进 Send Log —— 排查「消息到底发没发出去」时会缺一份证据。
|
||||
*/
|
||||
| 'user_manual'
|
||||
/** Automation v1(@我生成日报)。与 `shared/automation.ts` 的 AUTOMATION_SEND_ORIGIN 一致。 */
|
||||
| 'automation'
|
||||
| 'unknown'
|
||||
@@ -11,6 +19,10 @@ export type WechatActionPurpose =
|
||||
| 'member_left_notification'
|
||||
| 'scheduled_report'
|
||||
| 'tts_voice'
|
||||
/** 用户手动发送图片(日报图片 / 群成员统计图)。 */
|
||||
| 'manual_image'
|
||||
/** 用户手动发送文本。 */
|
||||
| 'manual_text'
|
||||
/**
|
||||
* Automation v1 的两个用途。
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user