Files
memind/wechat/ack/ack-builders.mjs
john 9b4a25799f Add smart ACK provider for WeChat MP replies
Replace fixed ackText with a rule-based AckProvider that picks
response templates by message type and intent (translate, summary,
rewrite, poster, ppt, mindmap, code, search, schedule). Pure sync,
zero I/O, auto-falls back to config.ackText on any error.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-26 15:19:03 +08:00

63 lines
1.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { TEMPLATES } from './ack-templates.mjs';
function pickRandom(arr, randomEnabled) {
if (!randomEnabled || arr.length === 1) return arr[0];
return arr[Math.floor(Math.random() * arr.length)];
}
function withNickname(text, nickname, nicknameEnabled) {
if (!nicknameEnabled || !nickname) return text;
return `${nickname}${text}`;
}
function buildText(pool, ctx) {
const text = pickRandom(pool, ctx.randomEnabled);
return withNickname(text, ctx.nickname, ctx.nicknameEnabled);
}
const ImageBuilder = {
priority: 80,
support: (ctx) => ctx.msgType === 'image',
build: (ctx) => buildText(TEMPLATES.image, ctx),
};
const VoiceBuilder = {
priority: 80,
support: (ctx) => ctx.msgType === 'voice',
build: (ctx) => buildText(TEMPLATES.voice, ctx),
};
const LocationBuilder = {
priority: 80,
support: (ctx) => ctx.msgType === 'location',
build: (ctx) => buildText(TEMPLATES.location, ctx),
};
const LinkBuilder = {
priority: 80,
support: (ctx) => ctx.msgType === 'link',
build: (ctx) => buildText(TEMPLATES.link, ctx),
};
const IntentBuilder = {
priority: 50,
support: (ctx) => {
const task = ctx.intent?.task;
return task && task !== 'unknown' && TEMPLATES.intent[task];
},
build: (ctx) => buildText(TEMPLATES.intent[ctx.intent.task], ctx),
};
const DefaultBuilder = {
priority: 0,
support: () => true,
build: (ctx) => buildText(TEMPLATES.default, ctx),
};
const BUILDERS = [ImageBuilder, VoiceBuilder, LocationBuilder, LinkBuilder, IntentBuilder, DefaultBuilder]
.sort((a, b) => b.priority - a.priority);
export function selectBuilder(ctx) {
return BUILDERS.find((b) => b.support(ctx)) ?? DefaultBuilder;
}