koishi-plugin-dify-ai 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ export declare class DifyClient {
11
11
  private timeout;
12
12
  constructor(http: Context['http'], app: DifyClientApp, timeout?: number);
13
13
  private buildUrl;
14
+ private authHeaders;
14
15
  private buildJsonHeaders;
15
16
  private errorFrom;
16
17
  /** 非 2xx 时抛出带状态码与 Dify 错误码的 DifyApiError */
package/lib/config.d.ts CHANGED
@@ -71,13 +71,6 @@ export interface RuleEntry {
71
71
  scope: ScopeRow[];
72
72
  variables: VariableEntry[];
73
73
  }
74
- /** 引用触发消息的条件分支形态:关闭时空分支,开启时按耗时阈值决定是否引用 */
75
- export type QuoteEntry = {
76
- mode: 'off';
77
- } | {
78
- mode: 'on';
79
- minElapsedSeconds: number;
80
- };
81
74
  /** 触发后冷却的条件分支形态 */
82
75
  export type CooldownEntry = {
83
76
  mode: 'off';
@@ -89,33 +82,29 @@ export type CooldownEntry = {
89
82
  minSeconds?: number;
90
83
  maxSeconds?: number;
91
84
  };
92
- /** 上传消息文件的条件分支形态:关闭时空分支,开启时限制数量与单文件大小 */
93
- export type UploadEntry = {
94
- mode: 'off';
95
- } | {
96
- mode: 'on';
97
- maxCount?: number;
98
- maxSizeMb?: number;
99
- };
100
85
  export interface Config {
101
86
  apps: AppConfigEntry[];
102
87
  rules: RuleEntry[];
103
88
  parseElements: boolean;
104
- quote: QuoteEntry;
89
+ quote: boolean;
90
+ minElapsedSeconds: number;
105
91
  maxSegmentLength: number;
106
- /** boolean 是 0.1.x 的遗留形态,仅迁移读取(true → 开启不限,false → 关闭) */
107
- uploadFiles: UploadEntry | boolean;
108
- /** 0.1.x 的布尔开关,仅用于迁移读取(true → 总是引用,false → 关闭) */
109
- quoteTrigger?: boolean;
92
+ uploadFiles: boolean;
93
+ maxCount: number;
94
+ maxSizeMb: number;
110
95
  requestTimeout: number;
111
96
  historyLimit: number;
112
97
  sendErrors: boolean;
113
98
  cooldown: CooldownEntry;
114
99
  }
100
+ /**
101
+ * 上传消息文件:布尔开关驱动分支(开=可配数量与单文件大小上限,关=空分支),官方 union-tagged 写法。
102
+ * 上限默认 -1 规避 0 被表单默认值机制吞掉(-1 与 0 同为不限,仍受 Dify 各类型上限约束)。
103
+ */
115
104
  export declare const Config: Schema<Config>;
116
105
  /**
117
- * 上传消息文件配置(含历史形态迁移:0.1.x 布尔、0.1.7 mode)。
118
- * 数量与单文件大小 0 与 -1 均为不限,大小仍与 Dify 各类型上限取较小值。
106
+ * 上传消息文件配置(平铺开关,默认关闭:显式开启才上传)。数量与单文件大小
107
+ * 0 与 -1 均为不限,大小仍与 Dify 各类型上限取较小值。
119
108
  */
120
109
  export declare function resolveUploadConfig(config: Config): {
121
110
  enabled: boolean;
@@ -123,9 +112,8 @@ export declare function resolveUploadConfig(config: Config): {
123
112
  maxMb: number;
124
113
  };
125
114
  /**
126
- * 引用触发消息配置(含历史形态迁移:0.1.x 布尔与 quoteTrigger 透传、0.1.7 mode、布尔开关新形态)。
127
- * 迁移判据沿用:旧布尔被透传保留且配置未被用户改动(默认形态)时以旧布尔为准。
128
- * 阈值 -1 与 0 同为总是引用(比较 elapsed >= 阈值 自然兼容)。
115
+ * 引用触发消息配置(平铺开关,含 0.1.x quoteTrigger 透传迁移:
116
+ * 旧布尔被保留且开关与阈值均未被改动时以旧布尔为准)。阈值 -1 与 0 同为总是引用。
129
117
  */
130
118
  export declare function resolveQuoteConfig(config: Config): {
131
119
  enabled: boolean;
package/lib/history.d.ts CHANGED
@@ -7,11 +7,12 @@ export interface QuotedMessage {
7
7
  content: string;
8
8
  }
9
9
  export interface HistoryEntry {
10
- role: 'user' | 'assistant';
10
+ /** event 为群事件条目(戳一戳/入退群等,content 即事件文案,不进历史变量) */
11
+ role: 'user' | 'assistant' | 'event';
11
12
  userId: string;
12
13
  username: string;
13
14
  messageId?: string;
14
- /** 原貌(含 @bot 渲染与图片占位):统一格式行(query/历史变量)用 */
15
+ /** 原貌(含 @bot 渲染与图片占位):统一格式行(query/历史变量)用;event 为事件文案 */
15
16
  content: string;
16
17
  /** 剔除 @bot 的原貌:content 变量与精简行用 */
17
18
  contentText: string;
@@ -19,12 +20,11 @@ export interface HistoryEntry {
19
20
  imageUrls?: string[];
20
21
  timestamp: number;
21
22
  quote?: QuotedMessage;
23
+ /** 即时触发已消费过该消息(密度计数排除,渲染行尾注记) */
24
+ replied?: boolean;
25
+ /** 该消息已被撤回(渲染行尾注记) */
26
+ deleted?: boolean;
22
27
  }
23
- /**
24
- * 统一消息格式(历史消息变量 / 定时摘要 / 密度触发共用):
25
- * `[时间] 昵称 (用户ID) [msg:消息ID](引用了 [时间] 昵称 (用户ID) 说:内容)说:内容`
26
- * 引用段与外层同构但不展示消息 ID;无引用则整段省略。
27
- */
28
28
  export declare function formatHistoryEntry(entry: HistoryEntry): string;
29
29
  /** 多条消息逐行拼接 */
30
30
  export declare function formatHistoryEntries(entries: HistoryEntry[]): string;
package/lib/index.js CHANGED
@@ -147,26 +147,39 @@ var variableSchema = import_koishi.Schema.union([
147
147
  template: import_koishi.Schema.string().role("textarea").required().description("JSON 对象")
148
148
  }).default({ source: "json", template: "" }).description("JSON 对象")
149
149
  ]);
150
- var quoteSchema = import_koishi.Schema.union([
151
- import_koishi.Schema.intersect([
150
+ var legacySwitch = import_koishi.Schema.transform(
151
+ import_koishi.Schema.any(),
152
+ (value) => typeof value === "boolean" ? value : value?.mode !== "off"
153
+ );
154
+ function switchSchema(defaultValue) {
155
+ return import_koishi.Schema.union([import_koishi.Schema.boolean().default(defaultValue), legacySwitch]).default(defaultValue);
156
+ }
157
+ __name(switchSchema, "switchSchema");
158
+ var quoteSwitchSchema = import_koishi.Schema.intersect([
159
+ import_koishi.Schema.object({
160
+ quote: switchSchema(true).description("引用触发消息")
161
+ }),
162
+ import_koishi.Schema.union([
152
163
  import_koishi.Schema.object({
153
- enabled: import_koishi.Schema.boolean().default(true).description("开启")
164
+ quote: import_koishi.Schema.const(true),
165
+ minElapsedSeconds: import_koishi.Schema.number().min(-1).default(-1).description("回复耗时多少秒才引用,0 总是引用")
154
166
  }),
155
- import_koishi.Schema.union([
156
- import_koishi.Schema.object({
157
- enabled: import_koishi.Schema.const(true).required(),
158
- minElapsedSeconds: import_koishi.Schema.number().min(-1).default(-1).description("回复耗时多少秒才引用,0 总是引用")
159
- }),
160
- import_koishi.Schema.object({})
161
- ])
162
- ]),
163
- import_koishi.Schema.boolean(),
164
- import_koishi.Schema.object({ mode: import_koishi.Schema.const("off").required() }),
167
+ import_koishi.Schema.object({})
168
+ ])
169
+ ]);
170
+ var uploadSwitchSchema = import_koishi.Schema.intersect([
165
171
  import_koishi.Schema.object({
166
- mode: import_koishi.Schema.const("on").required(),
167
- minElapsedSeconds: import_koishi.Schema.number().min(-1).default(-1)
168
- })
169
- ]).default({ enabled: true, minElapsedSeconds: -1 });
172
+ uploadFiles: switchSchema(false).description("上传消息中的文件")
173
+ }),
174
+ import_koishi.Schema.union([
175
+ import_koishi.Schema.object({
176
+ uploadFiles: import_koishi.Schema.const(true).required(),
177
+ maxCount: import_koishi.Schema.number().min(-1).default(-1).description("数量上限,0 不限"),
178
+ maxSizeMb: import_koishi.Schema.number().min(-1).default(-1).description("单文件大小上限 MB,0 不限")
179
+ }),
180
+ import_koishi.Schema.object({})
181
+ ])
182
+ ]);
170
183
  var cooldownSchema = import_koishi.Schema.union([
171
184
  import_koishi.Schema.object({
172
185
  mode: import_koishi.Schema.const("off").required()
@@ -181,28 +194,6 @@ var cooldownSchema = import_koishi.Schema.union([
181
194
  maxSeconds: import_koishi.Schema.natural().default(120).min(1).description("最大冷却秒数")
182
195
  }).default({ mode: "sliding", minSeconds: 30, maxSeconds: 120 }).description("滑动窗口")
183
196
  ]);
184
- var uploadSchema = import_koishi.Schema.union([
185
- import_koishi.Schema.intersect([
186
- import_koishi.Schema.object({
187
- enabled: import_koishi.Schema.boolean().default(true).description("开启")
188
- }),
189
- import_koishi.Schema.union([
190
- import_koishi.Schema.object({
191
- enabled: import_koishi.Schema.const(true).required(),
192
- maxCount: import_koishi.Schema.number().min(-1).default(-1).description("数量上限,0 不限"),
193
- maxSizeMb: import_koishi.Schema.number().min(-1).default(-1).description("单文件大小上限 MB,0 不限")
194
- }),
195
- import_koishi.Schema.object({})
196
- ])
197
- ]),
198
- import_koishi.Schema.boolean(),
199
- import_koishi.Schema.object({ mode: import_koishi.Schema.const("off").required() }),
200
- import_koishi.Schema.object({
201
- mode: import_koishi.Schema.const("on").required(),
202
- maxCount: import_koishi.Schema.number().min(-1).default(-1),
203
- maxSizeMb: import_koishi.Schema.number().min(-1).default(-1)
204
- })
205
- ]).default({ enabled: true, maxCount: -1, maxSizeMb: -1 });
206
197
  var Config = import_koishi.Schema.intersect([
207
198
  import_koishi.Schema.object({
208
199
  apps: import_koishi.Schema.array(
@@ -249,10 +240,10 @@ var Config = import_koishi.Schema.intersect([
249
240
  }).description("触发规则"),
250
241
  import_koishi.Schema.object({
251
242
  parseElements: import_koishi.Schema.boolean().default(false).description("解析回复中的消息元素"),
252
- quote: quoteSchema.description("引用触发消息"),
253
- maxSegmentLength: import_koishi.Schema.number().min(-1).default(-1).description("分段长度,0 不分段"),
254
- uploadFiles: uploadSchema.description("上传消息中的文件")
243
+ maxSegmentLength: import_koishi.Schema.number().min(-1).default(-1).description("分段长度,0 不分段")
255
244
  }).description("回复行为"),
245
+ quoteSwitchSchema,
246
+ uploadSwitchSchema,
256
247
  import_koishi.Schema.object({
257
248
  requestTimeout: import_koishi.Schema.natural().role("ms").default(3e5).description("请求超时"),
258
249
  historyLimit: import_koishi.Schema.natural().default(20).min(1).description("历史消息条数"),
@@ -374,30 +365,22 @@ function toVariable(entry) {
374
365
  }
375
366
  __name(toVariable, "toVariable");
376
367
  function resolveUploadConfig(config) {
377
- const upload = config.uploadFiles;
378
- if (typeof upload === "boolean") {
379
- return upload ? { enabled: true, maxCount: 0, maxMb: 0 } : { enabled: false, maxCount: 0, maxMb: 0 };
380
- }
381
- if (upload?.mode === "off" || upload?.enabled === false) {
382
- return { enabled: false, maxCount: 0, maxMb: 0 };
383
- }
368
+ if (config.uploadFiles !== true) return { enabled: false, maxCount: 0, maxMb: 0 };
384
369
  return {
385
370
  enabled: true,
386
- maxCount: Number(upload?.maxCount) || 0,
387
- maxMb: Number(upload?.maxSizeMb) || 0
371
+ maxCount: Number(config.maxCount) || 0,
372
+ maxMb: Number(config.maxSizeMb) || 0
388
373
  };
389
374
  }
390
375
  __name(resolveUploadConfig, "resolveUploadConfig");
391
376
  function resolveQuoteConfig(config) {
392
- const quote = config.quote;
393
- if (typeof quote === "boolean") return { enabled: quote, minElapsedSeconds: 0 };
394
- if (quote?.mode === "off") return { enabled: false, minElapsedSeconds: 0 };
395
- const threshold = Number(quote?.minElapsedSeconds) || 0;
396
- const untouched = !(threshold > 0) && quote?.enabled !== false;
397
- if (typeof config.quoteTrigger === "boolean" && untouched) {
398
- return { enabled: config.quoteTrigger, minElapsedSeconds: 0 };
377
+ const threshold = Number(config.minElapsedSeconds) || 0;
378
+ const untouched = config.quote !== false && !(threshold > 0);
379
+ const legacy = config.quoteTrigger;
380
+ if (typeof legacy === "boolean" && untouched) {
381
+ return { enabled: legacy, minElapsedSeconds: 0 };
399
382
  }
400
- return { enabled: quote?.mode === "on" ? true : quote?.enabled !== false, minElapsedSeconds: threshold };
383
+ return { enabled: config.quote !== false, minElapsedSeconds: threshold };
401
384
  }
402
385
  __name(resolveQuoteConfig, "resolveQuoteConfig");
403
386
  function toCooldown(entry) {
@@ -413,6 +396,7 @@ function toCooldown(entry) {
413
396
  }
414
397
  __name(toCooldown, "toCooldown");
415
398
  function normalizeConfig(config) {
399
+ config ??= {};
416
400
  const apps = /* @__PURE__ */ new Map();
417
401
  for (const app of config.apps ?? []) {
418
402
  if (app.name?.trim() && !apps.has(app.name)) apps.set(app.name, app);
@@ -470,11 +454,11 @@ var DifyClient = class {
470
454
  buildUrl(path) {
471
455
  return this.app.apiURL.replace(/\/+$/, "") + path;
472
456
  }
457
+ authHeaders() {
458
+ return { Authorization: `Bearer ${this.app.apiKey}` };
459
+ }
473
460
  buildJsonHeaders() {
474
- return {
475
- Authorization: `Bearer ${this.app.apiKey}`,
476
- "Content-Type": "application/json"
477
- };
461
+ return { ...this.authHeaders(), "Content-Type": "application/json" };
478
462
  }
479
463
  errorFrom(status, data, statusText = "") {
480
464
  const isObject = typeof data === "object" && data !== null;
@@ -524,7 +508,7 @@ var DifyClient = class {
524
508
  );
525
509
  form.set("user", user);
526
510
  const res = await this.http("POST", this.buildUrl("/files/upload"), {
527
- headers: { Authorization: `Bearer ${this.app.apiKey}` },
511
+ headers: this.authHeaders(),
528
512
  data: form,
529
513
  timeout: this.timeout,
530
514
  validateStatus: /* @__PURE__ */ __name(() => true, "validateStatus")
@@ -580,6 +564,14 @@ function annotateDroppedPlaceholders(text, droppedUrls) {
580
564
  return result;
581
565
  }
582
566
  __name(annotateDroppedPlaceholders, "annotateDroppedPlaceholders");
567
+ function isImageElement(element) {
568
+ return element.type === "img" || element.type === "image";
569
+ }
570
+ __name(isImageElement, "isImageElement");
571
+ function imageSourceOf(element) {
572
+ return element.attrs?.["url"] ?? element.attrs?.["src"];
573
+ }
574
+ __name(imageSourceOf, "imageSourceOf");
583
575
  function elementsOf(session) {
584
576
  if (session.elements?.length) return session.elements;
585
577
  try {
@@ -599,8 +591,8 @@ function extractTextWithPlaceholders(session, options = {}) {
599
591
  if (options.excludeBotAt && element.attrs?.["id"] === session.selfId) continue;
600
592
  const name2 = element.attrs?.["name"] ?? element.attrs?.["id"];
601
593
  if (name2) parts.push(`@${name2}`);
602
- } else if (element.type === "img" || element.type === "image") {
603
- parts.push(imagePlaceholder(element.attrs?.["url"] ?? element.attrs?.["src"]));
594
+ } else if (isImageElement(element)) {
595
+ parts.push(imagePlaceholder(imageSourceOf(element)));
604
596
  }
605
597
  }
606
598
  return parts.join(" ").replace(/\s+/g, " ").trim();
@@ -609,8 +601,8 @@ __name(extractTextWithPlaceholders, "extractTextWithPlaceholders");
609
601
  function extractImageUrls(session) {
610
602
  const urls = [];
611
603
  for (const element of elementsOf(session)) {
612
- if (element.type !== "img" && element.type !== "image") continue;
613
- const source = element.attrs?.["url"] ?? element.attrs?.["src"];
604
+ if (!isImageElement(element)) continue;
605
+ const source = imageSourceOf(element);
614
606
  if (typeof source === "string" && source) urls.push(source);
615
607
  }
616
608
  return urls;
@@ -626,10 +618,18 @@ function formatTimestamp(timestamp) {
626
618
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
627
619
  }
628
620
  __name(formatTimestamp, "formatTimestamp");
621
+ function entryAnnotations(entry) {
622
+ return `${entry.replied ? "(已回复)" : ""}${entry.deleted ? "(已撤回)" : ""}`;
623
+ }
624
+ __name(entryAnnotations, "entryAnnotations");
629
625
  function formatHistoryEntry(entry) {
626
+ if (entry.role === "event") {
627
+ return `[${formatTimestamp(entry.timestamp)}] ${entry.content}${entryAnnotations(entry)}`;
628
+ }
630
629
  const quote = entry.quote ? `(引用了 [${formatTimestamp(entry.quote.timestamp)}] ${entry.quote.username} (${entry.quote.userId}) 说:${entry.quote.content})` : "";
631
630
  const messageId = entry.messageId ? ` [msg:${entry.messageId}]` : "";
632
- return `[${formatTimestamp(entry.timestamp)}] ${entry.username} (${entry.userId})${messageId}${quote}${quote ? "" : " "}说:${entry.content}`;
631
+ const separator = quote ? "" : " ";
632
+ return `[${formatTimestamp(entry.timestamp)}] ${entry.username} (${entry.userId})${messageId}${quote}${separator}说:${entry.content}${entryAnnotations(entry)}`;
633
633
  }
634
634
  __name(formatHistoryEntry, "formatHistoryEntry");
635
635
  function formatHistoryEntries(entries) {
@@ -637,7 +637,10 @@ function formatHistoryEntries(entries) {
637
637
  }
638
638
  __name(formatHistoryEntries, "formatHistoryEntries");
639
639
  function formatHistoryBrief(entries) {
640
- return entries.map((entry) => entry.messageId ? `[msg:${entry.messageId}] ${entry.contentText}` : entry.contentText).join("\n");
640
+ return entries.map((entry) => {
641
+ const line = entry.messageId ? `[msg:${entry.messageId}] ${entry.contentText}` : entry.contentText;
642
+ return `${line}${entryAnnotations(entry)}`;
643
+ }).join("\n");
641
644
  }
642
645
  __name(formatHistoryBrief, "formatHistoryBrief");
643
646
  function buildQuote(session) {
@@ -700,7 +703,10 @@ function hasAt(session, id) {
700
703
  return session.elements?.some((element) => element.type === "at" && element.attrs?.["id"] === id) ?? false;
701
704
  }
702
705
  __name(hasAt, "hasAt");
703
- var NON_TEXT_PREFIX = new RegExp(`^(?:@\\S+|\\[image:[0-9a-f]{${IMAGE_HASH_LENGTH}}\\]|\\[图片\\]|\\s)*`);
706
+ var escapeRegExp = /* @__PURE__ */ __name((text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "escapeRegExp");
707
+ var NON_TEXT_PREFIX = new RegExp(
708
+ `^(?:@\\S+|\\[image:[0-9a-f]{${IMAGE_HASH_LENGTH}}\\]|${escapeRegExp(IMAGE_PLACEHOLDER)}|\\s)*`
709
+ );
704
710
  function stripPrefixKeyword(verbatim, keyword) {
705
711
  const prefix = NON_TEXT_PREFIX.exec(verbatim)?.[0] ?? "";
706
712
  const rest = verbatim.slice(prefix.length);
@@ -760,8 +766,7 @@ function matchScope(scope, session) {
760
766
  ];
761
767
  if (dimensions.some(([allowed, value]) => allowed?.length && !allowed.includes(value))) return false;
762
768
  if (scope.excludeUsers?.length && scope.excludeUsers.includes(session.userId)) return false;
763
- if (scope.isDirect === true && !session.isDirect) return false;
764
- if (scope.isDirect === false && session.isDirect) return false;
769
+ if (scope.isDirect != null && scope.isDirect !== !!session.isDirect) return false;
765
770
  return true;
766
771
  }
767
772
  __name(matchScope, "matchScope");
@@ -817,11 +822,11 @@ var MessagePool = class {
817
822
  pool.entries.splice(0, pool.entries.length - POOL_CAPACITY);
818
823
  }
819
824
  }
820
- /** 时效内且通过规则作用域的真人条数(bot 行不推高密度,防自激励) */
825
+ /** 时效内且通过规则作用域的真人条数(bot 行不推高密度防自激励;已即时回复过的消息不重复计数防双触发) */
821
826
  countQualified(pool, rule, condition, now) {
822
827
  const windowMs = condition.densityWindowSeconds * 1e3;
823
828
  return pool.entries.filter(
824
- (item) => item.role === "user" && now - item.timestamp <= windowMs && matchScope(rule.scope, scopeSessionOf(pool, item))
829
+ (item) => item.role === "user" && !item.replied && now - item.timestamp <= windowMs && matchScope(rule.scope, scopeSessionOf(pool, item))
825
830
  ).length;
826
831
  }
827
832
  /**
@@ -841,12 +846,23 @@ var MessagePool = class {
841
846
  if (skip?.(rule, condition, index)) continue;
842
847
  if (this.countQualified(pool, rule, condition, now) >= condition.densityCount) {
843
848
  this.channels.delete(key);
844
- return [{ rule, condition, conditionIndex: index, entries: pool.entries }];
849
+ return [{ rule, conditionIndex: index, entries: pool.entries }];
845
850
  }
846
851
  }
847
852
  }
848
853
  return [];
849
854
  }
855
+ /** 给池内已有条目标注已撤回(按消息 ID 定位,找不到忽略——不新建条目) */
856
+ markDeleted(channelKey, messageId) {
857
+ const pool = this.channels.get(channelKey);
858
+ if (!pool) return;
859
+ for (let i = pool.entries.length - 1; i >= 0; i--) {
860
+ if (pool.entries[i].messageId === messageId) {
861
+ pool.entries[i].deleted = true;
862
+ return;
863
+ }
864
+ }
865
+ }
850
866
  /** 冷却到期复查:按条件参数统计该频道池(now 为复查时刻),达标即取走整池,未达标返回 null */
851
867
  takeDensityChannel(rule, condition, channelKey, now) {
852
868
  const pool = this.channels.get(channelKey);
@@ -868,6 +884,91 @@ var MessagePool = class {
868
884
  }
869
885
  };
870
886
 
887
+ // src/trigger/pool-events.ts
888
+ function actorOf(session) {
889
+ return `${session.username || session.userId} (${session.userId})`;
890
+ }
891
+ __name(actorOf, "actorOf");
892
+ function eventEntry(session, content) {
893
+ return {
894
+ role: "event",
895
+ userId: session.userId ?? "",
896
+ username: session.username || session.userId || "",
897
+ content,
898
+ contentText: content,
899
+ timestamp: session.timestamp ?? Date.now()
900
+ };
901
+ }
902
+ __name(eventEntry, "eventEntry");
903
+ function buildNoticeEntry(session) {
904
+ const actor = actorOf(session);
905
+ switch (session.subtype) {
906
+ case "poke": {
907
+ const target = session.targetId;
908
+ if (!target) return null;
909
+ return eventEntry(session, `${actor} 戳了 ${target === session.selfId ? "bot" : target}`);
910
+ }
911
+ case "lucky-king":
912
+ return eventEntry(session, `${actor} 成为红包运气王`);
913
+ case "honor":
914
+ return eventEntry(session, `${actor} 获得群荣誉`);
915
+ default:
916
+ return null;
917
+ }
918
+ }
919
+ __name(buildNoticeEntry, "buildNoticeEntry");
920
+ function buildMemberEntry(session) {
921
+ const actor = actorOf(session);
922
+ const payload = session.onebot ?? {};
923
+ switch (session.subtype) {
924
+ // set/remove 被 adapter 覆盖为 role,原始值只在载荷
925
+ case "role":
926
+ return eventEntry(session, `${actor} ${payload.sub_type === "remove" ? "被取消管理员" : "被设为管理员"}`);
927
+ case "ban": {
928
+ if (payload.sub_type === "lift_ban") return eventEntry(session, `${actor} 被解除禁言`);
929
+ const seconds = Number(payload.duration) || 0;
930
+ return eventEntry(session, `${actor} 被禁言${seconds > 0 ? ` ${seconds} 秒` : ""}`);
931
+ }
932
+ case "nickname":
933
+ return eventEntry(session, `${actor} 修改了群名片`);
934
+ default:
935
+ return null;
936
+ }
937
+ }
938
+ __name(buildMemberEntry, "buildMemberEntry");
939
+ function buildJoinEntry(session) {
940
+ const invitedBy = session.subtype === "passive" && session.operatorId;
941
+ return eventEntry(session, `${actorOf(session)} 加入群聊${invitedBy ? `(由 ${session.operatorId} 邀请)` : ""}`);
942
+ }
943
+ __name(buildJoinEntry, "buildJoinEntry");
944
+ function buildLeaveEntry(session) {
945
+ if (session.subtype === "passive" && session.operatorId) {
946
+ return eventEntry(session, `${actorOf(session)} 被 ${session.operatorId} 移出群聊`);
947
+ }
948
+ return eventEntry(session, `${actorOf(session)} 离开群聊`);
949
+ }
950
+ __name(buildLeaveEntry, "buildLeaveEntry");
951
+ function recordable(session) {
952
+ return !!session.channelId && !!session.userId && session.userId !== session.selfId;
953
+ }
954
+ __name(recordable, "recordable");
955
+ function registerPoolEvents(ctx, pool) {
956
+ const collect = /* @__PURE__ */ __name((build) => (session) => {
957
+ if (!recordable(session)) return;
958
+ const entry = build(session);
959
+ if (entry) pool.record(session, entry);
960
+ }, "collect");
961
+ ctx.on("notice", collect(buildNoticeEntry));
962
+ ctx.on("guild-member", collect(buildMemberEntry));
963
+ ctx.on("guild-member-added", collect(buildJoinEntry));
964
+ ctx.on("guild-member-removed", collect(buildLeaveEntry));
965
+ ctx.on("message-deleted", (session) => {
966
+ if (!session.channelId || !session.messageId) return;
967
+ pool.markDeleted(channelKeyOf(session.platform, session.channelId), session.messageId);
968
+ });
969
+ }
970
+ __name(registerPoolEvents, "registerPoolEvents");
971
+
871
972
  // src/trigger/scheduler.ts
872
973
  var import_cron_parser = require("cron-parser");
873
974
  var MS_PER_MINUTE = 6e4;
@@ -932,13 +1033,13 @@ var RuleScheduler = class {
932
1033
  // src/trigger/cooldown.ts
933
1034
  var BUFFER_CAPACITY = 1024;
934
1035
  var DENSITY_LANE_PREFIX = "density:";
1036
+ var LANE_KEY_SEPARATOR = "\n";
935
1037
  function densityLane(index) {
936
1038
  return `${DENSITY_LANE_PREFIX}${index}`;
937
1039
  }
938
1040
  __name(densityLane, "densityLane");
939
1041
  function laneKeyOf(ruleName, lane) {
940
- return `${ruleName}
941
- ${lane}`;
1042
+ return `${ruleName}${LANE_KEY_SEPARATOR}${lane}`;
942
1043
  }
943
1044
  __name(laneKeyOf, "laneKeyOf");
944
1045
  var TriggerCooldown = class {
@@ -974,23 +1075,23 @@ var TriggerCooldown = class {
974
1075
  state.fireAt + setting.maxMs
975
1076
  );
976
1077
  }
977
- /** 滑动窗口顺延:已到期的通道不复活 */
1078
+ /** 滑动窗口顺延:sliding 模式且通道未到期才执行(已到期的不复活) */
1079
+ slideIfActive(state, setting, now) {
1080
+ if (setting?.mode !== "sliding" || !state || now >= state.deadline) return;
1081
+ this.slide(state, setting, now);
1082
+ }
1083
+ /** 滑动窗口顺延 */
978
1084
  extend(channelKey, ruleName, lane, now) {
979
- const setting = this.setting;
980
- if (setting?.mode !== "sliding") return;
981
1085
  const state = this.channels.get(channelKey)?.get(laneKeyOf(ruleName, lane));
982
- if (!state || now >= state.deadline) return;
983
- this.slide(state, setting, now);
1086
+ this.slideIfActive(state, this.setting, now);
984
1087
  }
985
- /** 该频道所有密度通道由一条过路消息顺延(密度冷却期内的消息积攒驱动窗口滑动);已到期的不复活 */
1088
+ /** 该频道所有密度通道由一条过路消息顺延(密度冷却期内的消息积攒驱动窗口滑动) */
986
1089
  extendDensityLanes(channelKey, now) {
987
- const setting = this.setting;
988
- if (setting?.mode !== "sliding") return;
989
1090
  const lanes = this.channels.get(channelKey);
990
1091
  if (!lanes) return;
991
1092
  for (const [key, state] of lanes) {
992
- if (!key.includes("\ndensity:") || now >= state.deadline) continue;
993
- this.slide(state, setting, now);
1093
+ if (!key.includes(LANE_KEY_SEPARATOR + DENSITY_LANE_PREFIX)) continue;
1094
+ this.slideIfActive(state, this.setting, now);
994
1095
  }
995
1096
  }
996
1097
  /** 冷却期内缓存一条命中消息(仅消息通道) */
@@ -1010,7 +1111,7 @@ var TriggerCooldown = class {
1010
1111
  if (!lanes) continue;
1011
1112
  for (const [laneKey, state] of lanes) {
1012
1113
  if (now < state.deadline) continue;
1013
- const separator = laneKey.indexOf("\n");
1114
+ const separator = laneKey.indexOf(LANE_KEY_SEPARATOR);
1014
1115
  lanes.delete(laneKey);
1015
1116
  expired.push({
1016
1117
  channelKey: key,
@@ -1035,10 +1136,19 @@ var TriggerCooldown = class {
1035
1136
 
1036
1137
  // src/trigger/engine.ts
1037
1138
  var SCHEDULER_TICK_MS = 1e3;
1139
+ var joinNonEmpty = /* @__PURE__ */ __name((parts) => parts.filter(Boolean).join("\n"), "joinNonEmpty");
1038
1140
  function collectImageUrls(entries) {
1039
1141
  return entries.flatMap((entry) => entry?.role === "user" ? entry.imageUrls ?? [] : []);
1040
1142
  }
1041
1143
  __name(collectImageUrls, "collectImageUrls");
1144
+ function windowFieldsOf(entries) {
1145
+ return {
1146
+ queryText: formatHistoryEntries(entries),
1147
+ contentText: formatHistoryBrief(entries),
1148
+ imageUrls: collectImageUrls(entries)
1149
+ };
1150
+ }
1151
+ __name(windowFieldsOf, "windowFieldsOf");
1042
1152
  function sessionIdentityOf(session) {
1043
1153
  return {
1044
1154
  platform: session.platform,
@@ -1087,12 +1197,12 @@ var TriggerEngine = class {
1087
1197
  this.history.recordEntry(key, entry);
1088
1198
  this.pool.record(session, entry);
1089
1199
  });
1200
+ registerPoolEvents(this.ctx, this.pool);
1090
1201
  this.scheduler.sync(this.rules);
1091
1202
  this.ctx.logger.info("已加载 %d 条启用规则", this.rules.length);
1092
1203
  }
1093
1204
  handleMessage(session, next) {
1094
- if (this.ctx.bots[session.uid]) return next();
1095
- if (!session.channelId) return next();
1205
+ if (this.ctx.bots[session.uid] || !session.channelId) return next();
1096
1206
  const channelKey = channelKeyOf(session.platform, session.channelId);
1097
1207
  this.tickCooldown(channelKey);
1098
1208
  const entry = buildHistoryEntry(session);
@@ -1145,6 +1255,7 @@ var TriggerEngine = class {
1145
1255
  this.cooldown.begin(channelKey, ruleName, lane, Date.now());
1146
1256
  }
1147
1257
  fireMessage(rule, session, condition, entry) {
1258
+ if (entry) entry.replied = true;
1148
1259
  this.beginCooldown(channelKeyOf(session.platform, session.channelId), rule.name, "message");
1149
1260
  this.run({
1150
1261
  session,
@@ -1163,16 +1274,17 @@ var TriggerEngine = class {
1163
1274
  ...sessionIdentityOf(session),
1164
1275
  triggerType: "density",
1165
1276
  rule: fire.rule,
1166
- queryText: formatHistoryEntries(fire.entries),
1167
- contentText: formatHistoryBrief(fire.entries),
1168
- imageUrls: collectImageUrls(fire.entries)
1277
+ ...windowFieldsOf(fire.entries)
1169
1278
  });
1170
1279
  }
1171
1280
  /** 冷却到期:把缓冲内的触发消息合并为一次触发,session 取首条(供投递与文件上传) */
1172
1281
  fireMerged(rule, buffer) {
1282
+ for (const item of buffer) {
1283
+ if (item.entry) item.entry.replied = true;
1284
+ }
1173
1285
  const first = buffer[0];
1174
- const queryText = buffer.map((item) => item.entry ? formatHistoryEntry(item.entry) : "").filter(Boolean).join("\n");
1175
- const contentText = buffer.map((item) => item.contentText).filter(Boolean).join("\n");
1286
+ const queryText = joinNonEmpty(buffer.map((item) => item.entry ? formatHistoryEntry(item.entry) : ""));
1287
+ const contentText = joinNonEmpty(buffer.map((item) => item.contentText));
1176
1288
  this.beginCooldown(channelKeyOf(first.session.platform, first.session.channelId), rule.name, "message");
1177
1289
  this.run({
1178
1290
  session: first.session,
@@ -1186,10 +1298,9 @@ var TriggerEngine = class {
1186
1298
  });
1187
1299
  }
1188
1300
  /** 密度冷却到期复查达标:无当前消息,用池身份构造上下文(投递同定时路径) */
1189
- fireDensityExpiry(rule, conditionIndex, channel, now) {
1301
+ fireDensityExpiry(rule, condition, conditionIndex, channel, now) {
1190
1302
  this.beginCooldown(channelKeyOf(channel.platform, channel.channelId), rule.name, densityLane(conditionIndex));
1191
- const condition = rule.conditions[conditionIndex];
1192
- const windowMs = condition.type === "density" ? condition.densityWindowSeconds * 1e3 : 0;
1303
+ const windowMs = condition.densityWindowSeconds * 1e3;
1193
1304
  let userId;
1194
1305
  for (let i = channel.entries.length - 1; i >= 0; i--) {
1195
1306
  const item = channel.entries[i];
@@ -1203,9 +1314,7 @@ var TriggerEngine = class {
1203
1314
  userId,
1204
1315
  triggerType: "density",
1205
1316
  rule,
1206
- queryText: formatHistoryEntries(channel.entries),
1207
- contentText: formatHistoryBrief(channel.entries),
1208
- imageUrls: collectImageUrls(channel.entries),
1317
+ ...windowFieldsOf(channel.entries),
1209
1318
  isDirect: channel.isDirect
1210
1319
  });
1211
1320
  }
@@ -1219,9 +1328,7 @@ var TriggerEngine = class {
1219
1328
  ...poolIdentityOf(channel),
1220
1329
  triggerType: "schedule",
1221
1330
  rule,
1222
- queryText: formatHistoryEntries(channel.entries),
1223
- contentText: formatHistoryBrief(channel.entries),
1224
- imageUrls: collectImageUrls(channel.entries),
1331
+ ...windowFieldsOf(channel.entries),
1225
1332
  isDirect: channel.isDirect
1226
1333
  });
1227
1334
  }
@@ -1245,7 +1352,7 @@ var TriggerEngine = class {
1245
1352
  const condition = rule.conditions[index];
1246
1353
  if (condition?.type !== "density") return;
1247
1354
  const channel = this.pool.takeDensityChannel(rule, condition, expired.channelKey, now);
1248
- if (channel) this.fireDensityExpiry(rule, index, channel, now);
1355
+ if (channel) this.fireDensityExpiry(rule, condition, index, channel, now);
1249
1356
  }
1250
1357
  };
1251
1358
 
@@ -1304,6 +1411,10 @@ function guessExtension(mimeType) {
1304
1411
  return found ? found[0] : "bin";
1305
1412
  }
1306
1413
  __name(guessExtension, "guessExtension");
1414
+ function fallbackFileName(mimeType) {
1415
+ return `file.${guessExtension(mimeType)}`;
1416
+ }
1417
+ __name(fallbackFileName, "fallbackFileName");
1307
1418
  function mimeToFileType(mimeType) {
1308
1419
  if (!mimeType) return "custom";
1309
1420
  if (mimeType.startsWith("image/")) return "image";
@@ -1317,15 +1428,11 @@ function mimeToFileType(mimeType) {
1317
1428
  __name(mimeToFileType, "mimeToFileType");
1318
1429
  function extractFileCandidates(session) {
1319
1430
  const candidates = [];
1320
- const seen = /* @__PURE__ */ new Set();
1321
1431
  for (const element of session.elements ?? []) {
1322
1432
  const difyType = ELEMENT_TYPE[element.type];
1323
1433
  if (!difyType) continue;
1324
- const source = element.attrs?.["url"] ?? element.attrs?.["src"];
1434
+ const source = imageSourceOf(element);
1325
1435
  if (typeof source !== "string" || !source) continue;
1326
- const key = `${difyType}:${source}`;
1327
- if (seen.has(key)) continue;
1328
- seen.add(key);
1329
1436
  candidates.push({ source, type: difyType });
1330
1437
  }
1331
1438
  return candidates;
@@ -1351,7 +1458,7 @@ function parseDataUrl(source) {
1351
1458
  const mimeType = match[1] || "application/octet-stream";
1352
1459
  const isBase64 = /(?:^|;)base64/.test(match[2] ?? "");
1353
1460
  const buffer = isBase64 ? Buffer.from(match[3], "base64") : percentDecodeBytes(match[3]);
1354
- return { buffer, fileName: `file.${guessExtension(mimeType)}`, mimeType };
1461
+ return { buffer, fileName: fallbackFileName(mimeType), mimeType };
1355
1462
  }
1356
1463
  __name(parseDataUrl, "parseDataUrl");
1357
1464
  async function readLocalFile(ctx, source) {
@@ -1362,7 +1469,7 @@ async function readLocalFile(ctx, source) {
1362
1469
  }
1363
1470
  const buffer = await (0, import_promises.readFile)(filePath);
1364
1471
  const mimeType = guessMime((0, import_path.extname)(filePath));
1365
- return { buffer, fileName: (0, import_path.basename)(filePath) || `file.${guessExtension(mimeType)}`, mimeType };
1472
+ return { buffer, fileName: (0, import_path.basename)(filePath) || fallbackFileName(mimeType), mimeType };
1366
1473
  }
1367
1474
  __name(readLocalFile, "readLocalFile");
1368
1475
  async function fetchRemoteFile(ctx, source) {
@@ -1378,7 +1485,7 @@ async function fetchRemoteFile(ctx, source) {
1378
1485
  } catch {
1379
1486
  }
1380
1487
  const mimeType = guessMime((0, import_path.extname)(fileName));
1381
- return { buffer, fileName: fileName || `file.${guessExtension(mimeType)}`, mimeType };
1488
+ return { buffer, fileName: fileName || fallbackFileName(mimeType), mimeType };
1382
1489
  } catch (error) {
1383
1490
  ctx.logger.warn("下载远程文件失败:%s (%s)", source, error);
1384
1491
  return null;
@@ -1493,19 +1600,21 @@ function resolveMetaField(key, fireCtx, now) {
1493
1600
  }
1494
1601
  }
1495
1602
  __name(resolveMetaField, "resolveMetaField");
1603
+ function historyVariableOf(fireCtx, history, limit) {
1604
+ const channelKey = channelKeyOf(fireCtx.platform, fireCtx.channelId);
1605
+ return history.toVariable(history.get(channelKey, limit ?? history.limit));
1606
+ }
1607
+ __name(historyVariableOf, "historyVariableOf");
1496
1608
  function resolveSource(source, fireCtx, history, now) {
1497
- switch (source?.type) {
1609
+ switch (source.type) {
1498
1610
  case "session":
1499
1611
  return resolveSessionField(source.key, fireCtx, now);
1500
1612
  case "meta":
1501
1613
  return resolveMetaField(source.key, fireCtx, now);
1502
1614
  case "constant":
1503
1615
  return source.value;
1504
- case "history": {
1505
- const channelKey = channelKeyOf(fireCtx.platform, fireCtx.channelId);
1506
- const limit = source.limit ?? history.limit;
1507
- return history.toVariable(history.get(channelKey, limit));
1508
- }
1616
+ case "history":
1617
+ return historyVariableOf(fireCtx, history, source.limit);
1509
1618
  default:
1510
1619
  return void 0;
1511
1620
  }
@@ -1514,28 +1623,26 @@ __name(resolveSource, "resolveSource");
1514
1623
  var META_PLACEHOLDERS = /* @__PURE__ */ new Set(["triggerType", "ruleName", "triggerTime"]);
1515
1624
  function resolvePlaceholderValue(name2, limit, fireCtx, history, now) {
1516
1625
  if (name2 === "history") {
1517
- const channelKey = channelKeyOf(fireCtx.platform, fireCtx.channelId);
1518
- return history.toVariable(history.get(channelKey, limit ?? history.limit));
1626
+ return historyVariableOf(fireCtx, history, limit);
1519
1627
  }
1520
1628
  if (name2 === "isDirect") {
1521
- return fireCtx.session?.isDirect ?? fireCtx.isDirect ?? false;
1629
+ return resolveSessionField("isDirect", fireCtx, now);
1522
1630
  }
1523
1631
  if (META_PLACEHOLDERS.has(name2)) {
1524
- return String(resolveMetaField(name2, fireCtx, now) ?? "");
1632
+ return resolveMetaField(name2, fireCtx, now);
1525
1633
  }
1526
- const value = resolveSessionField(name2, fireCtx, now);
1527
- return value === void 0 || value === null ? "" : String(value);
1634
+ return String(resolveSessionField(name2, fireCtx, now));
1528
1635
  }
1529
1636
  __name(resolvePlaceholderValue, "resolvePlaceholderValue");
1530
- var BARE_PLACEHOLDER = /"\{\{\s*(\w+)(?::(\d+))?\s*\}\}"/g;
1637
+ var BARE_PLACEHOLDER = new RegExp(`"${JSON_PLACEHOLDER.source}"`, "g");
1531
1638
  var escapeJsonText = /* @__PURE__ */ __name((value) => JSON.stringify(value).slice(1, -1), "escapeJsonText");
1532
1639
  function interpolateTemplate(template, fireCtx, history, now) {
1533
1640
  const resolve = /* @__PURE__ */ __name((name2, limit) => resolvePlaceholderValue(name2, limit ? Number(limit) : void 0, fireCtx, history, now), "resolve");
1534
- const native = template.replace(
1641
+ const bareReplaced = template.replace(
1535
1642
  BARE_PLACEHOLDER,
1536
1643
  (_match, name2, limit) => JSON.stringify(resolve(name2, limit))
1537
1644
  );
1538
- return native.replace(
1645
+ return bareReplaced.replace(
1539
1646
  JSON_PLACEHOLDER,
1540
1647
  (_match, name2, limit) => escapeJsonText(String(resolve(name2, limit)))
1541
1648
  );
@@ -1554,12 +1661,12 @@ function resolveVariables(rule, fireCtx, history) {
1554
1661
  const inputs = {};
1555
1662
  const now = (/* @__PURE__ */ new Date()).toISOString();
1556
1663
  for (const binding of rule.variables) {
1557
- if (binding?.source?.type !== "json") continue;
1664
+ if (binding.source.type !== "json") continue;
1558
1665
  const object = resolveJsonObject(binding.source.template, fireCtx, history, now);
1559
1666
  if (object) Object.assign(inputs, object);
1560
1667
  }
1561
1668
  for (const binding of rule.variables) {
1562
- if (!binding?.field || binding.source?.type === "json") continue;
1669
+ if (binding.source.type === "json") continue;
1563
1670
  const value = resolveSource(binding.source, fireCtx, history, now);
1564
1671
  if (value !== void 0) inputs[binding.field] = value;
1565
1672
  }
@@ -1575,12 +1682,13 @@ function splitText(text, maxLength) {
1575
1682
  const lines = text.split("\n");
1576
1683
  const segments = [];
1577
1684
  let current = "";
1685
+ const flush = /* @__PURE__ */ __name(() => {
1686
+ if (current) segments.push(current);
1687
+ }, "flush");
1578
1688
  for (const line of lines) {
1579
1689
  if (line.length > maxLength) {
1580
- if (current) {
1581
- segments.push(current);
1582
- current = "";
1583
- }
1690
+ flush();
1691
+ current = "";
1584
1692
  for (let i = 0; i < line.length; i += maxLength) {
1585
1693
  segments.push(line.slice(i, i + maxLength));
1586
1694
  }
@@ -1594,7 +1702,7 @@ function splitText(text, maxLength) {
1594
1702
  ${line}` : line;
1595
1703
  }
1596
1704
  }
1597
- if (current) segments.push(current);
1705
+ flush();
1598
1706
  return segments;
1599
1707
  }
1600
1708
  __name(splitText, "splitText");
@@ -1698,22 +1806,7 @@ var RequestRunner = class {
1698
1806
  inputs: resolveVariables(rule, fireCtx, this.history),
1699
1807
  uploadedFiles: []
1700
1808
  };
1701
- const upload = resolveUploadConfig(this.config);
1702
- if (upload.enabled) {
1703
- const sessionCandidates = fireCtx.session ? extractFileCandidates(fireCtx.session) : [];
1704
- const urlCandidates = (fireCtx.imageUrls ?? []).filter((url) => !sessionCandidates.some((candidate) => candidate.source === url)).map((url) => ({ source: url, type: "image" }));
1705
- const { files, dropped } = await uploadCandidates(
1706
- this.ctx,
1707
- client,
1708
- [...sessionCandidates, ...urlCandidates],
1709
- requestCtx.difyUser,
1710
- { maxCount: upload.maxCount, maxSizeMb: upload.maxMb }
1711
- );
1712
- requestCtx.uploadedFiles = files;
1713
- if (dropped.length) {
1714
- fireCtx.queryText = annotateDroppedPlaceholders(fireCtx.queryText, dropped);
1715
- }
1716
- }
1809
+ await this.uploadTriggerFiles(fireCtx, client, requestCtx);
1717
1810
  let reply;
1718
1811
  try {
1719
1812
  reply = await this.dispatch(app, client, fireCtx, requestCtx);
@@ -1737,6 +1830,30 @@ var RequestRunner = class {
1737
1830
  });
1738
1831
  await this.deliver(fireCtx, messages);
1739
1832
  }
1833
+ /**
1834
+ * 文件上传管线:消息触发取会话元素候选(含音视频文件),窗口类触发(合并/密度/定时)
1835
+ * 经 fireCtx.imageUrls 补齐相关消息的图片;同 URL 重复由上传入口去重;
1836
+ * 被丢弃图片在 query 的占位符上注明已丢弃,模型可感知缺图原因。
1837
+ * 注意:workflow 的「文件型输入变量」(inputs 内嵌文件对象)不在本管线范围内,
1838
+ * 顶层 files 已覆盖视觉模型组合输入的主场景。
1839
+ */
1840
+ async uploadTriggerFiles(fireCtx, client, requestCtx) {
1841
+ const upload = resolveUploadConfig(this.config);
1842
+ if (!upload.enabled) return;
1843
+ const sessionCandidates = fireCtx.session ? extractFileCandidates(fireCtx.session) : [];
1844
+ const urlCandidates = (fireCtx.imageUrls ?? []).map((url) => ({ source: url, type: "image" }));
1845
+ const { files, dropped } = await uploadCandidates(
1846
+ this.ctx,
1847
+ client,
1848
+ [...sessionCandidates, ...urlCandidates],
1849
+ requestCtx.difyUser,
1850
+ { maxCount: upload.maxCount, maxSizeMb: upload.maxMb }
1851
+ );
1852
+ requestCtx.uploadedFiles = files;
1853
+ if (dropped.length) {
1854
+ fireCtx.queryText = annotateDroppedPlaceholders(fireCtx.queryText, dropped);
1855
+ }
1856
+ }
1740
1857
  /** 引用判定:开关开启、回复耗时达到阈值、且由消息触发(定时与密度没有单一触发消息) */
1741
1858
  shouldQuoteReply(fireCtx, startedAt) {
1742
1859
  const quote = resolveQuoteConfig(this.config);
@@ -1772,12 +1889,16 @@ var RequestRunner = class {
1772
1889
  }
1773
1890
  return { text: response.answer, files: [] };
1774
1891
  }
1892
+ /** 逐条发送,单条失败仅告警不中断 */
1893
+ async sendAll(messages, send) {
1894
+ for (const message of messages) {
1895
+ await send(message).catch((error) => this.ctx.logger.warn(error));
1896
+ }
1897
+ }
1775
1898
  async deliver(fireCtx, messages) {
1776
1899
  if (!messages.length) return;
1777
1900
  if (fireCtx.session) {
1778
- for (const message of messages) {
1779
- await fireCtx.session.send(message).catch((error) => this.ctx.logger.warn(error));
1780
- }
1901
+ await this.sendAll(messages, (message) => fireCtx.session.send(message));
1781
1902
  return;
1782
1903
  }
1783
1904
  const bots = Object.values(this.ctx.bots);
@@ -1786,9 +1907,7 @@ var RequestRunner = class {
1786
1907
  this.ctx.logger.warn("平台 %s 上没有可用 bot,无法发送主动消息", fireCtx.platform);
1787
1908
  return;
1788
1909
  }
1789
- for (const message of messages) {
1790
- await bot.sendMessage(fireCtx.channelId, message).catch((error) => this.ctx.logger.warn(error));
1791
- }
1910
+ await this.sendAll(messages, (message) => bot.sendMessage(fireCtx.channelId, message));
1792
1911
  }
1793
1912
  };
1794
1913
 
@@ -1856,23 +1975,34 @@ var ConversationStore = class {
1856
1975
  async saveConversationId(bindingKey, difyConversationId) {
1857
1976
  await this.ctx.database.upsert("dify_conversation", [{ bindingKey, difyConversationId }]);
1858
1977
  }
1859
- /** 解析 bindingKey 的构成(重置时反查应用凭据与 difyUser 用) */
1978
+ /** 解析 bindingKey 的构成(重置时反查应用凭据与 difyUser 用)。
1979
+ * 段位定位假设 appId / platform / selfId 不含冒号(平台名与应用标识按约定不含;
1980
+ * Matrix 的 bot id 含冒号属键格式改造范畴,当前不区分) */
1860
1981
  parseBindingKey(bindingKey) {
1861
- const [appId, platform, , scope, ...ids] = bindingKey.split(":");
1982
+ const segments = bindingKey.split(":");
1983
+ if (segments.length === 2 && segments[1] === "global") {
1984
+ return { appId: segments[0], platform: "", scope: "global", ids: [] };
1985
+ }
1986
+ const [appId, platform, , scope, ...ids] = segments;
1862
1987
  return { appId, platform, scope, ids };
1863
1988
  }
1989
+ /** scope 之后的 id 片段拼回整体(键内 id 可能内嵌冒号,消费方按语义整体比较) */
1990
+ joinedIdsOf(bindingKey) {
1991
+ return this.parseBindingKey(bindingKey).ids.join(":");
1992
+ }
1864
1993
  /** 由 bindingKey 还原删除 Dify 会话所需的 difyUser(与 computeDifyUser 对齐) */
1865
1994
  difyUserOfBindingKey(bindingKey) {
1866
- const { platform, scope, ids } = this.parseBindingKey(bindingKey);
1995
+ const { platform, scope } = this.parseBindingKey(bindingKey);
1996
+ const ids = this.joinedIdsOf(bindingKey);
1867
1997
  switch (scope) {
1868
1998
  case "global":
1869
1999
  return "koishi";
1870
2000
  case "private":
1871
- return `koishi:${platform}:private:${ids.join(":")}`;
2001
+ return `koishi:${platform}:private:${ids}`;
1872
2002
  case "guild":
1873
2003
  case "channel":
1874
2004
  case "user":
1875
- return `koishi:${platform}:${ids.join(":")}`;
2005
+ return `koishi:${platform}:${ids}`;
1876
2006
  default:
1877
2007
  return `koishi:${platform}`;
1878
2008
  }
@@ -1893,12 +2023,13 @@ var ConversationStore = class {
1893
2023
  const [channelPlatform, channelId] = splitKey(filter.channelKey ?? "");
1894
2024
  const matched = rows.filter((row) => {
1895
2025
  if (filter.bindingKey) return row.bindingKey === filter.bindingKey;
1896
- const { platform, scope, ids } = this.parseBindingKey(row.bindingKey);
2026
+ const { platform, scope } = this.parseBindingKey(row.bindingKey);
1897
2027
  if (platform !== guildPlatform) return false;
1898
- if (scope === "guild") return ids[0] === guildId;
1899
- if (scope === "user") return ids.length === 2 ? ids[0] === guildId : false;
1900
- if (scope === "channel") return platform === channelPlatform && ids[0] === channelId;
1901
- if (scope === "private") return filter.isDirect === true && ids[0] === filter.userId;
2028
+ const joinedIds = this.joinedIdsOf(row.bindingKey);
2029
+ if (scope === "guild") return joinedIds === guildId;
2030
+ if (scope === "user") return joinedIds.startsWith(`${guildId}:`);
2031
+ if (scope === "channel") return platform === channelPlatform && joinedIds === channelId;
2032
+ if (scope === "private") return filter.isDirect === true && joinedIds === filter.userId;
1902
2033
  return false;
1903
2034
  });
1904
2035
  let reset = 0;
@@ -1941,14 +2072,14 @@ __name(defineTables, "defineTables");
1941
2072
  // src/commands/misc.ts
1942
2073
  function registerMiscCommands(parent, conversationStore) {
1943
2074
  parent.subcommand(".conv.reset", { authority: 3 }).action(async ({ session }) => {
1944
- const reset = await conversationStore.resetWhere({
2075
+ const resetCount = await conversationStore.resetWhere({
1945
2076
  guildKey: guildKeyOf(session.platform, session.guildId, session.channelId),
1946
2077
  channelKey: channelKeyOf(session.platform, session.channelId),
1947
2078
  isDirect: session.isDirect,
1948
2079
  userId: session.userId
1949
2080
  });
1950
- if (!reset) return session.text("dify-ai.commands.conv-empty");
1951
- return session.text("dify-ai.commands.conv-reset", [reset]);
2081
+ if (!resetCount) return session.text("dify-ai.commands.conv-empty");
2082
+ return session.text("dify-ai.commands.conv-reset", [resetCount]);
1952
2083
  });
1953
2084
  }
1954
2085
  __name(registerMiscCommands, "registerMiscCommands");
@@ -13,7 +13,7 @@ interface FilePayload {
13
13
  }
14
14
  /** mime → Dify 文件类型 */
15
15
  export declare function mimeToFileType(mimeType?: string): DifyFileType;
16
- /** 从消息元素中提取可上传的文件候选(图片/语音/视频/附件),按来源去重 */
16
+ /** 从消息元素中提取可上传的文件候选(图片/语音/视频/附件);同源去重由上传入口统一处理 */
17
17
  export declare function extractFileCandidates(session: Session): FileCandidate[];
18
18
  /** 三源归一:data URL / 远程 URL / 本地路径 → {buffer, fileName, mimeType}(冒烟测试直测) */
19
19
  export declare function resolveFilePayload(ctx: Context, candidate: FileCandidate): Promise<FilePayload | null>;
@@ -27,6 +27,6 @@ export interface UploadResult {
27
27
  files: DifyInputFileObject[];
28
28
  dropped: string[];
29
29
  }
30
- /** 上传一批候选(入口按 type:source 去重,再按数量上限截断、逐个上传) */
30
+ /** 上传一批候选(入口按来源 URL 去重,再按数量上限截断、逐个上传) */
31
31
  export declare function uploadCandidates(ctx: Context, client: DifyClient, candidates: FileCandidate[], difyUser: string, limit: UploadLimit): Promise<UploadResult>;
32
32
  export {};
@@ -28,10 +28,20 @@ export declare class RequestRunner {
28
28
  /** 应用类型:schema 显式指定优先,auto 时用探测结果(未探测到回退 chat) */
29
29
  private appTypeOf;
30
30
  run(fireCtx: FireContext): Promise<void>;
31
+ /**
32
+ * 文件上传管线:消息触发取会话元素候选(含音视频文件),窗口类触发(合并/密度/定时)
33
+ * 经 fireCtx.imageUrls 补齐相关消息的图片;同 URL 重复由上传入口去重;
34
+ * 被丢弃图片在 query 的占位符上注明已丢弃,模型可感知缺图原因。
35
+ * 注意:workflow 的「文件型输入变量」(inputs 内嵌文件对象)不在本管线范围内,
36
+ * 顶层 files 已覆盖视觉模型组合输入的主场景。
37
+ */
38
+ private uploadTriggerFiles;
31
39
  /** 引用判定:开关开启、回复耗时达到阈值、且由消息触发(定时与密度没有单一触发消息) */
32
40
  private shouldQuoteReply;
33
41
  /** 按应用类型分流:workflow/completion 无会话,chat 系续接 conversation_id */
34
42
  private dispatch;
43
+ /** 逐条发送,单条失败仅告警不中断 */
44
+ private sendAll;
35
45
  private deliver;
36
46
  }
37
47
  export {};
@@ -27,8 +27,12 @@ export declare class ConversationStore {
27
27
  computeDifyUser(rule: DifyRule, fireCtx: FireContext): string;
28
28
  getConversationId(bindingKey: string): Promise<string | undefined>;
29
29
  saveConversationId(bindingKey: string, difyConversationId: string): Promise<void>;
30
- /** 解析 bindingKey 的构成(重置时反查应用凭据与 difyUser 用) */
30
+ /** 解析 bindingKey 的构成(重置时反查应用凭据与 difyUser 用)。
31
+ * 段位定位假设 appId / platform / selfId 不含冒号(平台名与应用标识按约定不含;
32
+ * Matrix 的 bot id 含冒号属键格式改造范畴,当前不区分) */
31
33
  private parseBindingKey;
34
+ /** scope 之后的 id 片段拼回整体(键内 id 可能内嵌冒号,消费方按语义整体比较) */
35
+ private joinedIdsOf;
32
36
  /** 由 bindingKey 还原删除 Dify 会话所需的 difyUser(与 computeDifyUser 对齐) */
33
37
  private difyUserOfBindingKey;
34
38
  /**
@@ -38,9 +38,11 @@ export declare class TriggerCooldown {
38
38
  isCooling(channelKey: string, ruleName: string, lane: CooldownLane, now: number): boolean;
39
39
  /** 滑动窗口顺延:截止推到「now + 最小窗口」,不超过「触发时刻 + 最大窗口」 */
40
40
  private slide;
41
- /** 滑动窗口顺延:已到期的通道不复活 */
41
+ /** 滑动窗口顺延:sliding 模式且通道未到期才执行(已到期的不复活) */
42
+ private slideIfActive;
43
+ /** 滑动窗口顺延 */
42
44
  extend(channelKey: string, ruleName: string, lane: CooldownLane, now: number): void;
43
- /** 该频道所有密度通道由一条过路消息顺延(密度冷却期内的消息积攒驱动窗口滑动);已到期的不复活 */
45
+ /** 该频道所有密度通道由一条过路消息顺延(密度冷却期内的消息积攒驱动窗口滑动) */
44
46
  extendDensityLanes(channelKey: string, now: number): void;
45
47
  /** 冷却期内缓存一条命中消息(仅消息通道) */
46
48
  bufferMessage(channelKey: string, ruleName: string, message: PendingMessage): void;
@@ -0,0 +1,3 @@
1
+ import { Context } from 'koishi';
2
+ import { MessagePool } from './pool';
3
+ export declare function registerPoolEvents(ctx: Context, pool: MessagePool): void;
@@ -13,8 +13,7 @@ export interface ChannelPool {
13
13
  }
14
14
  export interface DensityFire {
15
15
  rule: DifyRule;
16
- /** 命中的密度条件与其在规则内的下标(密度冷却通道键) */
17
- condition: DensityCondition;
16
+ /** 命中条件在规则内的下标(密度冷却通道键) */
18
17
  conditionIndex: number;
19
18
  /** 触发时取走的该频道全池消息(含 bot 行与时效外条目) */
20
19
  entries: HistoryEntry[];
@@ -28,7 +27,7 @@ export declare class MessagePool {
28
27
  private channels;
29
28
  /** 一条消息(含 bot 自己的发言)入池,全量记录不做规则过滤 */
30
29
  record(session: Session, entry: HistoryEntry): void;
31
- /** 时效内且通过规则作用域的真人条数(bot 行不推高密度,防自激励) */
30
+ /** 时效内且通过规则作用域的真人条数(bot 行不推高密度防自激励;已即时回复过的消息不重复计数防双触发) */
32
31
  private countQualified;
33
32
  /**
34
33
  * 新消息入池后的密度评估:时效内(相对触发消息)且通过规则作用域的真人条数达到阈值。
@@ -36,6 +35,8 @@ export declare class MessagePool {
36
35
  * skip 返回 true 的条件(冷却中的密度条件)不参与判定。
37
36
  */
38
37
  checkDensity(rules: DifyRule[], session: Session, skip?: (rule: DifyRule, condition: DensityCondition, index: number) => boolean): DensityFire[];
38
+ /** 给池内已有条目标注已撤回(按消息 ID 定位,找不到忽略——不新建条目) */
39
+ markDeleted(channelKey: string, messageId: string): void;
39
40
  /** 冷却到期复查:按条件参数统计该频道池(now 为复查时刻),达标即取走整池,未达标返回 null */
40
41
  takeDensityChannel(rule: DifyRule, condition: DensityCondition, channelKey: string, now: number): ChannelPool | null;
41
42
  /** 定时到点:取走该规则作用域内有消息的所有频道(整池清空,含未过作用域的条目) */
@@ -1,4 +1,4 @@
1
- import { Session } from 'koishi';
1
+ import { h, Session } from 'koishi';
2
2
  /** 图片占位符携带的 URL 摘要长度(hex 位) */
3
3
  export declare const IMAGE_HASH_LENGTH = 8;
4
4
  /**
@@ -20,6 +20,8 @@ interface ExtractVerbatimOptions {
20
20
  /** 跳过 @ 机器人自身的渲染(content 变量语义:@bot 是触发结构而非消息内容) */
21
21
  excludeBotAt?: boolean;
22
22
  }
23
+ /** 媒体元素的源地址(url 或 src 属性,未定形态原样返回由调用方守卫) */
24
+ export declare function imageSourceOf(element: h): unknown;
23
25
  /**
24
26
  * 消息原貌文本:text 元素按序拼接,@ 元素渲染为 @名字(缺名字用 ID),
25
27
  * img/image 元素渲染为 [image:URL摘要]——统一格式行(query/池/历史)用默认形态,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-dify-ai",
3
3
  "description": "Invoke Dify apps (chatflow / workflow / completion) in Koishi with triggers and conversation bindings",
4
- "version": "0.1.9",
4
+ "version": "0.1.11",
5
5
  "author": "PinkElysia",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/PinkElysiaDev/dify-ai-plugin-for-Koishi#readme",