koishi-plugin-dify-ai 0.1.11 → 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 +3 -2
- package/lib/history.d.ts +4 -0
- package/lib/index.js +33 -23
- package/lib/requester/variables.d.ts +2 -2
- package/lib/trigger/scheduler.d.ts +1 -1
- package/lib/types.d.ts +7 -7
- package/package.json +1 -1
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: '
|
|
30
|
+
type: 'cron';
|
|
31
31
|
cron?: string;
|
|
32
32
|
intervalMinutes?: number;
|
|
33
33
|
} | {
|
|
34
|
-
type: '
|
|
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/history.d.ts
CHANGED
|
@@ -25,11 +25,15 @@ export interface HistoryEntry {
|
|
|
25
25
|
/** 该消息已被撤回(渲染行尾注记) */
|
|
26
26
|
deleted?: boolean;
|
|
27
27
|
}
|
|
28
|
+
/** 引用段本体:[时间] 昵称 (用户ID) 说:内容(quoteContent 变量与统一格式行同源,不带消息 ID) */
|
|
29
|
+
export declare function formatQuotedMessage(quote: QuotedMessage): string;
|
|
28
30
|
export declare function formatHistoryEntry(entry: HistoryEntry): string;
|
|
29
31
|
/** 多条消息逐行拼接 */
|
|
30
32
|
export declare function formatHistoryEntries(entries: HistoryEntry[]): string;
|
|
31
33
|
/** 精简行:只记消息 ID 与剔除 @bot 的原貌内容,不带时间与作者前缀——窗口触发的 content 变量用 */
|
|
32
34
|
export declare function formatHistoryBrief(entries: HistoryEntry[]): string;
|
|
35
|
+
/** 从会话的引用对象构造引用段数据(quoteContent 变量与消息条目共用) */
|
|
36
|
+
export declare function buildQuote(session: Session): QuotedMessage | undefined;
|
|
33
37
|
/** 从会话消息构造记录条目(历史缓冲 / 消息池共用);assistant 行标注为机器人自己的发言 */
|
|
34
38
|
export declare function buildHistoryEntry(session: Session, role?: 'user' | 'assistant'): HistoryEntry | null;
|
|
35
39
|
/**
|
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 !== "
|
|
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:
|
|
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: "
|
|
97
|
+
}).default({ type: "cron", cron: "", intervalMinutes: 0 }).description("定时"),
|
|
97
98
|
import_koishi.Schema.object({
|
|
98
|
-
type:
|
|
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: "
|
|
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 "
|
|
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: "
|
|
281
|
+
type: "cron",
|
|
280
282
|
...cron ? { cron } : {},
|
|
281
283
|
...intervalMinutes > 0 ? { intervalMinutes } : {}
|
|
282
284
|
};
|
|
283
285
|
}
|
|
284
|
-
case "
|
|
286
|
+
case "frequency":
|
|
285
287
|
return {
|
|
286
|
-
type: "
|
|
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:
|
|
350
|
+
return { field: field2, source: { type: "json", template } };
|
|
348
351
|
}
|
|
349
352
|
const field = entry.field?.trim();
|
|
350
353
|
if (!field) return null;
|
|
@@ -622,11 +625,15 @@ function entryAnnotations(entry) {
|
|
|
622
625
|
return `${entry.replied ? "(已回复)" : ""}${entry.deleted ? "(已撤回)" : ""}`;
|
|
623
626
|
}
|
|
624
627
|
__name(entryAnnotations, "entryAnnotations");
|
|
628
|
+
function formatQuotedMessage(quote) {
|
|
629
|
+
return `[${formatTimestamp(quote.timestamp)}] ${quote.username} (${quote.userId}) 说:${quote.content}`;
|
|
630
|
+
}
|
|
631
|
+
__name(formatQuotedMessage, "formatQuotedMessage");
|
|
625
632
|
function formatHistoryEntry(entry) {
|
|
626
633
|
if (entry.role === "event") {
|
|
627
634
|
return `[${formatTimestamp(entry.timestamp)}] ${entry.content}${entryAnnotations(entry)}`;
|
|
628
635
|
}
|
|
629
|
-
const quote = entry.quote ? `(引用了
|
|
636
|
+
const quote = entry.quote ? `(引用了 ${formatQuotedMessage(entry.quote)})` : "";
|
|
630
637
|
const messageId = entry.messageId ? ` [msg:${entry.messageId}]` : "";
|
|
631
638
|
const separator = quote ? "" : " ";
|
|
632
639
|
return `[${formatTimestamp(entry.timestamp)}] ${entry.username} (${entry.userId})${messageId}${quote}${separator}说:${entry.content}${entryAnnotations(entry)}`;
|
|
@@ -842,7 +849,7 @@ var MessagePool = class {
|
|
|
842
849
|
for (const rule of rules) {
|
|
843
850
|
for (let index = 0; index < rule.conditions.length; index++) {
|
|
844
851
|
const condition = rule.conditions[index];
|
|
845
|
-
if (condition.type !== "
|
|
852
|
+
if (condition.type !== "frequency") continue;
|
|
846
853
|
if (skip?.(rule, condition, index)) continue;
|
|
847
854
|
if (this.countQualified(pool, rule, condition, now) >= condition.densityCount) {
|
|
848
855
|
this.channels.delete(key);
|
|
@@ -1017,12 +1024,12 @@ var RuleScheduler = class {
|
|
|
1017
1024
|
}
|
|
1018
1025
|
return null;
|
|
1019
1026
|
}
|
|
1020
|
-
/** 计算规则下一次触发时刻;无
|
|
1027
|
+
/** 计算规则下一次触发时刻;无 cron 条件返回 null */
|
|
1021
1028
|
computeNextRun(rule) {
|
|
1022
1029
|
const now = Date.now();
|
|
1023
1030
|
let next = null;
|
|
1024
1031
|
for (const condition of rule.conditions) {
|
|
1025
|
-
if (condition.type !== "
|
|
1032
|
+
if (condition.type !== "cron") continue;
|
|
1026
1033
|
const candidate = this.nextRunOf(condition, now);
|
|
1027
1034
|
if (candidate != null && (next == null || candidate < next)) next = candidate;
|
|
1028
1035
|
}
|
|
@@ -1272,7 +1279,7 @@ var TriggerEngine = class {
|
|
|
1272
1279
|
this.run({
|
|
1273
1280
|
session,
|
|
1274
1281
|
...sessionIdentityOf(session),
|
|
1275
|
-
triggerType: "
|
|
1282
|
+
triggerType: "frequency",
|
|
1276
1283
|
rule: fire.rule,
|
|
1277
1284
|
...windowFieldsOf(fire.entries)
|
|
1278
1285
|
});
|
|
@@ -1312,7 +1319,7 @@ var TriggerEngine = class {
|
|
|
1312
1319
|
this.run({
|
|
1313
1320
|
...poolIdentityOf(channel),
|
|
1314
1321
|
userId,
|
|
1315
|
-
triggerType: "
|
|
1322
|
+
triggerType: "frequency",
|
|
1316
1323
|
rule,
|
|
1317
1324
|
...windowFieldsOf(channel.entries),
|
|
1318
1325
|
isDirect: channel.isDirect
|
|
@@ -1326,7 +1333,7 @@ var TriggerEngine = class {
|
|
|
1326
1333
|
for (const channel of this.pool.takeSchedule(rule)) {
|
|
1327
1334
|
this.run({
|
|
1328
1335
|
...poolIdentityOf(channel),
|
|
1329
|
-
triggerType: "
|
|
1336
|
+
triggerType: "cron",
|
|
1330
1337
|
rule,
|
|
1331
1338
|
...windowFieldsOf(channel.entries),
|
|
1332
1339
|
isDirect: channel.isDirect
|
|
@@ -1350,7 +1357,7 @@ var TriggerEngine = class {
|
|
|
1350
1357
|
}
|
|
1351
1358
|
const index = Number(expired.lane.slice(DENSITY_LANE_PREFIX.length));
|
|
1352
1359
|
const condition = rule.conditions[index];
|
|
1353
|
-
if (condition?.type !== "
|
|
1360
|
+
if (condition?.type !== "frequency") return;
|
|
1354
1361
|
const channel = this.pool.takeDensityChannel(rule, condition, expired.channelKey, now);
|
|
1355
1362
|
if (channel) this.fireDensityExpiry(rule, condition, index, channel, now);
|
|
1356
1363
|
}
|
|
@@ -1572,8 +1579,10 @@ function resolveSessionField(key, fireCtx, now) {
|
|
|
1572
1579
|
return session?.messageId ?? "";
|
|
1573
1580
|
case "content":
|
|
1574
1581
|
return fireCtx.contentText;
|
|
1575
|
-
case "quoteContent":
|
|
1576
|
-
|
|
1582
|
+
case "quoteContent": {
|
|
1583
|
+
const quote = session ? buildQuote(session) : void 0;
|
|
1584
|
+
return quote ? formatQuotedMessage(quote) : "";
|
|
1585
|
+
}
|
|
1577
1586
|
case "quoteAuthorId":
|
|
1578
1587
|
return session?.quote?.user?.id ?? "";
|
|
1579
1588
|
case "username":
|
|
@@ -1663,7 +1672,7 @@ function resolveVariables(rule, fireCtx, history) {
|
|
|
1663
1672
|
for (const binding of rule.variables) {
|
|
1664
1673
|
if (binding.source.type !== "json") continue;
|
|
1665
1674
|
const object = resolveJsonObject(binding.source.template, fireCtx, history, now);
|
|
1666
|
-
if (object)
|
|
1675
|
+
if (object) inputs[binding.field] = object;
|
|
1667
1676
|
}
|
|
1668
1677
|
for (const binding of rule.variables) {
|
|
1669
1678
|
if (binding.source.type === "json") continue;
|
|
@@ -2090,6 +2099,7 @@ var inject = { required: ["database"] };
|
|
|
2090
2099
|
var PROBE_TIMEOUT_MS = 1e4;
|
|
2091
2100
|
function apply(ctx, config) {
|
|
2092
2101
|
const startedAt = Date.now();
|
|
2102
|
+
ctx.logger.info("apply 进入");
|
|
2093
2103
|
ctx.i18n.define("zh-CN", zh_CN_default);
|
|
2094
2104
|
ctx.i18n.define("en-US", en_US_default);
|
|
2095
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
|
|
7
|
-
*
|
|
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>;
|
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 = '
|
|
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
|
-
/**
|
|
27
|
+
/** 定时触发(cron):cron(服务器本地时区)与固定间隔分钟数二选一 */
|
|
28
28
|
export interface ScheduleCondition {
|
|
29
|
-
type: '
|
|
29
|
+
type: 'cron';
|
|
30
30
|
cron?: string;
|
|
31
31
|
intervalMinutes?: number;
|
|
32
32
|
}
|
|
33
|
-
/**
|
|
33
|
+
/** 消息频率触发:窗口内消息条数达到阈值即触发 */
|
|
34
34
|
export interface DensityCondition {
|
|
35
|
-
type: '
|
|
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.
|
|
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",
|