koishi-plugin-dify-ai 0.1.12 → 0.1.13

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.
package/lib/config.d.ts CHANGED
@@ -27,11 +27,11 @@ export type ConditionEntry = {
27
27
  type: 'quote-user';
28
28
  userIds: string;
29
29
  } | {
30
- type: 'schedule';
30
+ type: 'cron';
31
31
  cron?: string;
32
32
  intervalMinutes?: number;
33
33
  } | {
34
- type: 'density';
34
+ type: 'frequency';
35
35
  densityCount?: number;
36
36
  densityWindowSeconds?: number;
37
37
  };
@@ -54,6 +54,7 @@ export type VariableEntry = {
54
54
  value: string;
55
55
  } | {
56
56
  source: 'json';
57
+ field: string;
57
58
  template: string;
58
59
  };
59
60
  /** 作用域的一行:维度 + 值 */
package/lib/index.js CHANGED
@@ -52,7 +52,7 @@ var import_koishi = require("koishi");
52
52
 
53
53
  // src/types.ts
54
54
  function isMessageTrigger(type) {
55
- return type !== "schedule" && type !== "density";
55
+ return type !== "cron" && type !== "frequency";
56
56
  }
57
57
  __name(isMessageTrigger, "isMessageTrigger");
58
58
  var JSON_PLACEHOLDER = /\{\{\s*(\w+)(?::(\d+))?\s*\}\}/g;
@@ -66,6 +66,7 @@ var appTypeSchema = import_koishi.Schema.union([
66
66
  import_koishi.Schema.const("workflow").description("Workflow"),
67
67
  import_koishi.Schema.const("completion").description("文本生成")
68
68
  ]);
69
+ var renamedType = /* @__PURE__ */ __name((name2, legacy) => import_koishi.Schema.union([import_koishi.Schema.const(name2), import_koishi.Schema.transform(import_koishi.Schema.const(legacy), () => name2)]), "renamedType");
69
70
  var conditionSchema = import_koishi.Schema.union([
70
71
  import_koishi.Schema.object({
71
72
  type: import_koishi.Schema.const("keyword-prefix").required(),
@@ -90,15 +91,15 @@ var conditionSchema = import_koishi.Schema.union([
90
91
  userIds: import_koishi.Schema.string().role("textarea").required().description("用户 ID,每行一个")
91
92
  }).default({ type: "quote-user", userIds: "" }).description("引用指定用户"),
92
93
  import_koishi.Schema.object({
93
- type: import_koishi.Schema.const("schedule").required(),
94
+ type: renamedType("cron", "schedule").required(),
94
95
  cron: import_koishi.Schema.string().default("").description("Cron 表达式"),
95
96
  intervalMinutes: import_koishi.Schema.number().default(-1).min(-1).description("间隔分钟")
96
- }).default({ type: "schedule", cron: "", intervalMinutes: 0 }).description("定时"),
97
+ }).default({ type: "cron", cron: "", intervalMinutes: 0 }).description("定时"),
97
98
  import_koishi.Schema.object({
98
- type: import_koishi.Schema.const("density").required(),
99
+ type: renamedType("frequency", "density").required(),
99
100
  densityCount: import_koishi.Schema.natural().default(5).description("条数阈值"),
100
101
  densityWindowSeconds: import_koishi.Schema.natural().default(60).description("窗口秒数")
101
- }).default({ type: "density", densityCount: 5, densityWindowSeconds: 60 }).description("消息密度")
102
+ }).default({ type: "frequency", densityCount: 5, densityWindowSeconds: 60 }).description("消息频率")
102
103
  ]);
103
104
  var sessionKeySchema = import_koishi.Schema.union([
104
105
  import_koishi.Schema.const("none").description("无"),
@@ -144,8 +145,9 @@ var variableSchema = import_koishi.Schema.union([
144
145
  }).default({ source: "constant", field: "", value: "" }).description("常量文本"),
145
146
  import_koishi.Schema.object({
146
147
  source: import_koishi.Schema.const("json").required(),
148
+ field: import_koishi.Schema.string().required().description("字段名"),
147
149
  template: import_koishi.Schema.string().role("textarea").required().description("JSON 对象")
148
- }).default({ source: "json", template: "" }).description("JSON 对象")
150
+ }).default({ source: "json", field: "", template: "" }).description("JSON 对象")
149
151
  ]);
150
152
  var legacySwitch = import_koishi.Schema.transform(
151
153
  import_koishi.Schema.any(),
@@ -271,19 +273,19 @@ function toCondition(entry) {
271
273
  case "at-bot":
272
274
  case "quote-bot":
273
275
  return { type: entry.type };
274
- case "schedule": {
276
+ case "cron": {
275
277
  const cron = entry.cron?.trim();
276
278
  const intervalMinutes = Number(entry.intervalMinutes);
277
279
  if (!cron && !(intervalMinutes > 0)) return null;
278
280
  return {
279
- type: "schedule",
281
+ type: "cron",
280
282
  ...cron ? { cron } : {},
281
283
  ...intervalMinutes > 0 ? { intervalMinutes } : {}
282
284
  };
283
285
  }
284
- case "density":
286
+ case "frequency":
285
287
  return {
286
- type: "density",
288
+ type: "frequency",
287
289
  densityCount: Math.max(1, Number(entry.densityCount) || 1),
288
290
  densityWindowSeconds: Math.max(1, Number(entry.densityWindowSeconds) || 1)
289
291
  };
@@ -336,15 +338,16 @@ function toScope(rows) {
336
338
  __name(toScope, "toScope");
337
339
  function toVariable(entry) {
338
340
  if (entry.source === "json") {
341
+ const field2 = entry.field?.trim();
339
342
  const template = entry.template ?? "";
340
- if (!template.trim()) return null;
343
+ if (!field2 || !template.trim()) return null;
341
344
  try {
342
345
  const probe = JSON.parse(template.replace(JSON_PLACEHOLDER, "0"));
343
346
  if (typeof probe !== "object" || probe === null || Array.isArray(probe)) return null;
344
347
  } catch {
345
348
  return null;
346
349
  }
347
- return { field: "", source: { type: "json", template } };
350
+ return { field: field2, source: { type: "json", template } };
348
351
  }
349
352
  const field = entry.field?.trim();
350
353
  if (!field) return null;
@@ -846,7 +849,7 @@ var MessagePool = class {
846
849
  for (const rule of rules) {
847
850
  for (let index = 0; index < rule.conditions.length; index++) {
848
851
  const condition = rule.conditions[index];
849
- if (condition.type !== "density") continue;
852
+ if (condition.type !== "frequency") continue;
850
853
  if (skip?.(rule, condition, index)) continue;
851
854
  if (this.countQualified(pool, rule, condition, now) >= condition.densityCount) {
852
855
  this.channels.delete(key);
@@ -1021,12 +1024,12 @@ var RuleScheduler = class {
1021
1024
  }
1022
1025
  return null;
1023
1026
  }
1024
- /** 计算规则下一次触发时刻;无 schedule 条件返回 null */
1027
+ /** 计算规则下一次触发时刻;无 cron 条件返回 null */
1025
1028
  computeNextRun(rule) {
1026
1029
  const now = Date.now();
1027
1030
  let next = null;
1028
1031
  for (const condition of rule.conditions) {
1029
- if (condition.type !== "schedule") continue;
1032
+ if (condition.type !== "cron") continue;
1030
1033
  const candidate = this.nextRunOf(condition, now);
1031
1034
  if (candidate != null && (next == null || candidate < next)) next = candidate;
1032
1035
  }
@@ -1276,7 +1279,7 @@ var TriggerEngine = class {
1276
1279
  this.run({
1277
1280
  session,
1278
1281
  ...sessionIdentityOf(session),
1279
- triggerType: "density",
1282
+ triggerType: "frequency",
1280
1283
  rule: fire.rule,
1281
1284
  ...windowFieldsOf(fire.entries)
1282
1285
  });
@@ -1316,7 +1319,7 @@ var TriggerEngine = class {
1316
1319
  this.run({
1317
1320
  ...poolIdentityOf(channel),
1318
1321
  userId,
1319
- triggerType: "density",
1322
+ triggerType: "frequency",
1320
1323
  rule,
1321
1324
  ...windowFieldsOf(channel.entries),
1322
1325
  isDirect: channel.isDirect
@@ -1330,7 +1333,7 @@ var TriggerEngine = class {
1330
1333
  for (const channel of this.pool.takeSchedule(rule)) {
1331
1334
  this.run({
1332
1335
  ...poolIdentityOf(channel),
1333
- triggerType: "schedule",
1336
+ triggerType: "cron",
1334
1337
  rule,
1335
1338
  ...windowFieldsOf(channel.entries),
1336
1339
  isDirect: channel.isDirect
@@ -1354,7 +1357,7 @@ var TriggerEngine = class {
1354
1357
  }
1355
1358
  const index = Number(expired.lane.slice(DENSITY_LANE_PREFIX.length));
1356
1359
  const condition = rule.conditions[index];
1357
- if (condition?.type !== "density") return;
1360
+ if (condition?.type !== "frequency") return;
1358
1361
  const channel = this.pool.takeDensityChannel(rule, condition, expired.channelKey, now);
1359
1362
  if (channel) this.fireDensityExpiry(rule, condition, index, channel, now);
1360
1363
  }
@@ -1577,7 +1580,7 @@ function resolveSessionField(key, fireCtx, now) {
1577
1580
  case "content":
1578
1581
  return fireCtx.contentText;
1579
1582
  case "quoteContent": {
1580
- const quote = buildQuote(session);
1583
+ const quote = session ? buildQuote(session) : void 0;
1581
1584
  return quote ? formatQuotedMessage(quote) : "";
1582
1585
  }
1583
1586
  case "quoteAuthorId":
@@ -1669,7 +1672,7 @@ function resolveVariables(rule, fireCtx, history) {
1669
1672
  for (const binding of rule.variables) {
1670
1673
  if (binding.source.type !== "json") continue;
1671
1674
  const object = resolveJsonObject(binding.source.template, fireCtx, history, now);
1672
- if (object) Object.assign(inputs, object);
1675
+ if (object) inputs[binding.field] = object;
1673
1676
  }
1674
1677
  for (const binding of rule.variables) {
1675
1678
  if (binding.source.type === "json") continue;
@@ -2096,6 +2099,7 @@ var inject = { required: ["database"] };
2096
2099
  var PROBE_TIMEOUT_MS = 1e4;
2097
2100
  function apply(ctx, config) {
2098
2101
  const startedAt = Date.now();
2102
+ ctx.logger.info("apply 进入");
2099
2103
  ctx.i18n.define("zh-CN", zh_CN_default);
2100
2104
  ctx.i18n.define("en-US", en_US_default);
2101
2105
  defineTables(ctx);
@@ -3,7 +3,7 @@ import { MessageHistory } from '../history';
3
3
  import { DifyRule } from '../types';
4
4
  /**
5
5
  * 按规则的变量映射表构造 Dify inputs。
6
- * JSON 对象条目先注入(顶层键展开合并进 inputs),单条映射后注入覆盖同名键——
7
- * 现有单条配置行为不变,JSON 作为批量默认值。
6
+ * JSON 对象条目作为单一变量整体注入 inputs[字段名](适配 Dify json_object 特性,
7
+ * 字段名即 Dify 侧该 object 变量的名字);单条映射后注入覆盖同名键。
8
8
  */
9
9
  export declare function resolveVariables(rule: DifyRule, fireCtx: FireContext, history: MessageHistory): Record<string, any>;
@@ -14,6 +14,6 @@ export declare class RuleScheduler {
14
14
  tick(): void;
15
15
  /** 单个 schedule 条件的下次触发时刻(cron 或固定间隔);无效 cron 记 warn 跳过 */
16
16
  private nextRunOf;
17
- /** 计算规则下一次触发时刻;无 schedule 条件返回 null */
17
+ /** 计算规则下一次触发时刻;无 cron 条件返回 null */
18
18
  private computeNextRun;
19
19
  }
package/lib/types.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import type { Session } from 'koishi';
2
2
  /** 消息驱动的触发原因(merged = 冷却期内多条触发消息合并为一次触发) */
3
3
  export type MessageTriggerType = 'keyword-prefix' | 'keyword-contains' | 'at-bot' | 'at-user' | 'quote-bot' | 'quote-user' | 'merged';
4
- /** 非消息驱动的触发原因 */
5
- export type ProactiveTriggerType = 'schedule' | 'density';
4
+ /** 非消息驱动的触发原因(triggerType 值 0.1.12 起 schedule→cron、density→frequency) */
5
+ export type ProactiveTriggerType = 'cron' | 'frequency';
6
6
  export type TriggerType = MessageTriggerType | ProactiveTriggerType;
7
- /** 是否由消息触发(合并触发也算):定时与密度触发没有单一触发消息 */
7
+ /** 是否由消息触发(合并触发也算):定时与消息频率触发没有单一触发消息 */
8
8
  export declare function isMessageTrigger(type: TriggerType): boolean;
9
9
  /** 关键词条件(prefix 触发时剥离关键词作为 query,contains 保留全文) */
10
10
  export interface KeywordCondition {
@@ -24,15 +24,15 @@ export interface AtBotCondition {
24
24
  export interface QuoteBotCondition {
25
25
  type: 'quote-bot';
26
26
  }
27
- /** 定时触发:cron(服务器本地时区)与固定间隔分钟数二选一 */
27
+ /** 定时触发(cron):cron(服务器本地时区)与固定间隔分钟数二选一 */
28
28
  export interface ScheduleCondition {
29
- type: 'schedule';
29
+ type: 'cron';
30
30
  cron?: string;
31
31
  intervalMinutes?: number;
32
32
  }
33
- /** 消息密度触发:窗口内消息条数达到阈值即触发 */
33
+ /** 消息频率触发:窗口内消息条数达到阈值即触发 */
34
34
  export interface DensityCondition {
35
- type: 'density';
35
+ type: 'frequency';
36
36
  densityCount: number;
37
37
  densityWindowSeconds: number;
38
38
  }
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.12",
4
+ "version": "0.1.13",
5
5
  "author": "PinkElysia",
6
6
  "license": "MIT",
7
7
  "homepage": "https://github.com/PinkElysiaDev/dify-ai-plugin-for-Koishi#readme",