koishi-plugin-dify-ai 0.1.12 → 0.1.14
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 +4 -4
- package/lib/index.d.ts +2 -0
- package/lib/index.js +75 -38
- package/lib/requester/runner.d.ts +2 -0
- package/lib/requester/variables.d.ts +2 -2
- package/lib/state/conversation.d.ts +0 -2
- package/lib/state/tables.d.ts +7 -0
- 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
|
@@ -2,9 +2,8 @@ import { Schema } from 'koishi';
|
|
|
2
2
|
import { CooldownSetting, DifyRule } from './types';
|
|
3
3
|
export type AppTypeSetting = 'auto' | 'chat' | 'agent-chat' | 'advanced-chat' | 'workflow' | 'completion';
|
|
4
4
|
export interface AppConfigEntry {
|
|
5
|
+
/** 应用标识:填 Dify 的应用 ID(沿用 name 字段名兼容既有绑定键与规则引用) */
|
|
5
6
|
name: string;
|
|
6
|
-
/** 会话绑定用的稳定标识;留空回退应用名 */
|
|
7
|
-
appId: string;
|
|
8
7
|
apiURL: string;
|
|
9
8
|
apiKey: string;
|
|
10
9
|
appType: AppTypeSetting;
|
|
@@ -27,11 +26,11 @@ export type ConditionEntry = {
|
|
|
27
26
|
type: 'quote-user';
|
|
28
27
|
userIds: string;
|
|
29
28
|
} | {
|
|
30
|
-
type: '
|
|
29
|
+
type: 'cron';
|
|
31
30
|
cron?: string;
|
|
32
31
|
intervalMinutes?: number;
|
|
33
32
|
} | {
|
|
34
|
-
type: '
|
|
33
|
+
type: 'frequency';
|
|
35
34
|
densityCount?: number;
|
|
36
35
|
densityWindowSeconds?: number;
|
|
37
36
|
};
|
|
@@ -54,6 +53,7 @@ export type VariableEntry = {
|
|
|
54
53
|
value: string;
|
|
55
54
|
} | {
|
|
56
55
|
source: 'json';
|
|
56
|
+
field: string;
|
|
57
57
|
template: string;
|
|
58
58
|
};
|
|
59
59
|
/** 作用域的一行:维度 + 值 */
|
package/lib/index.d.ts
CHANGED
|
@@ -5,4 +5,6 @@ export declare const inject: {
|
|
|
5
5
|
required: string[];
|
|
6
6
|
};
|
|
7
7
|
export { Config };
|
|
8
|
+
/** 控制台插件配置页顶部说明:JSON 对象变量的占位符速查 */
|
|
9
|
+
export declare const usage = "\n## JSON \u5BF9\u8C61\u53D8\u91CF\u7684\u5360\u4F4D\u7B26\n\n\u300CJSON \u5BF9\u8C61\u300D\u5206\u652F\u7684\u6A21\u677F\u5185\u53EF\u7528\u4EE5\u4E0B\u5360\u4F4D\u7B26\uFF08\u6574\u4E2A\u5BF9\u8C61\u6CE8\u5165\u5B57\u6BB5\u540D\u5BF9\u5E94\u7684\u5355\u4E00\u53D8\u91CF\uFF09\uFF1A\n\n| \u5360\u4F4D\u7B26 | \u7C7B\u578B | \u8BF4\u660E |\n| --- | --- | --- |\n| `{{userId}}` `{{botId}}` `{{guildId}}` `{{channelId}}` `{{platform}}` | \u5B57\u7B26\u4E32 | \u4F1A\u8BDD\u8EAB\u4EFD |\n| `{{messageId}}` `{{username}}` `{{timestamp}}` | \u5B57\u7B26\u4E32 | \u6D88\u606F\u6807\u8BC6\u4E0E\u4F5C\u8005 |\n| `{{content}}` | \u5B57\u7B26\u4E32 | \u6D88\u606F\u539F\u8C8C |\n| `{{quoteContent}}` `{{quoteAuthorId}}` | \u5B57\u7B26\u4E32 | \u88AB\u5F15\u7528\u6D88\u606F\uFF08\u5F15\u7528\u6BB5 / \u4F5C\u8005\uFF09 |\n| `{{triggerType}}` `{{ruleName}}` `{{triggerTime}}` | \u5B57\u7B26\u4E32 | \u89E6\u53D1\u5143\u4FE1\u606F |\n| `{{history}}` / `{{history:N}}` | \u5B57\u7B26\u4E32 | \u5386\u53F2\u6D88\u606F\u6587\u672C\uFF0CN \u6307\u5B9A\u6761\u6570\uFF08\u7F3A\u7701\u7528\u5168\u5C40\u914D\u7F6E\uFF09 |\n| `{{isDirect}}` | \u5E03\u5C14 | \u662F\u5426\u79C1\u804A |\n\n- \u503C\u6070\u597D\u4E3A\u4E00\u4E2A\u5360\u4F4D\u7B26\uFF08\u5982 `\"is_private\": \"{{isDirect}}\"`\uFF09\u65F6\u4FDD\u7559\u539F\u751F\u7C7B\u578B\n- \u5360\u4F4D\u7B26\u6DF7\u5165\u5176\u4ED6\u6587\u5B57\uFF08\u5982 `\"\u4F60\u597D {{username}}\"`\uFF09\u6309\u5B57\u7B26\u4E32\u62FC\u63A5\uFF0C\u5F15\u53F7\u6362\u884C\u81EA\u52A8\u8F6C\u4E49\n- \u672A\u77E5\u53D8\u91CF\u5F97\u7A7A\u4E32\n";
|
|
8
10
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -23,7 +23,8 @@ __export(src_exports, {
|
|
|
23
23
|
Config: () => Config,
|
|
24
24
|
apply: () => apply,
|
|
25
25
|
inject: () => inject,
|
|
26
|
-
name: () => name
|
|
26
|
+
name: () => name,
|
|
27
|
+
usage: () => usage
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(src_exports);
|
|
29
30
|
|
|
@@ -52,7 +53,7 @@ var import_koishi = require("koishi");
|
|
|
52
53
|
|
|
53
54
|
// src/types.ts
|
|
54
55
|
function isMessageTrigger(type) {
|
|
55
|
-
return type !== "
|
|
56
|
+
return type !== "cron" && type !== "frequency";
|
|
56
57
|
}
|
|
57
58
|
__name(isMessageTrigger, "isMessageTrigger");
|
|
58
59
|
var JSON_PLACEHOLDER = /\{\{\s*(\w+)(?::(\d+))?\s*\}\}/g;
|
|
@@ -66,6 +67,7 @@ var appTypeSchema = import_koishi.Schema.union([
|
|
|
66
67
|
import_koishi.Schema.const("workflow").description("Workflow"),
|
|
67
68
|
import_koishi.Schema.const("completion").description("文本生成")
|
|
68
69
|
]);
|
|
70
|
+
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
71
|
var conditionSchema = import_koishi.Schema.union([
|
|
70
72
|
import_koishi.Schema.object({
|
|
71
73
|
type: import_koishi.Schema.const("keyword-prefix").required(),
|
|
@@ -90,15 +92,15 @@ var conditionSchema = import_koishi.Schema.union([
|
|
|
90
92
|
userIds: import_koishi.Schema.string().role("textarea").required().description("用户 ID,每行一个")
|
|
91
93
|
}).default({ type: "quote-user", userIds: "" }).description("引用指定用户"),
|
|
92
94
|
import_koishi.Schema.object({
|
|
93
|
-
type:
|
|
95
|
+
type: renamedType("cron", "schedule").required(),
|
|
94
96
|
cron: import_koishi.Schema.string().default("").description("Cron 表达式"),
|
|
95
97
|
intervalMinutes: import_koishi.Schema.number().default(-1).min(-1).description("间隔分钟")
|
|
96
|
-
}).default({ type: "
|
|
98
|
+
}).default({ type: "cron", cron: "", intervalMinutes: 0 }).description("定时"),
|
|
97
99
|
import_koishi.Schema.object({
|
|
98
|
-
type:
|
|
100
|
+
type: renamedType("frequency", "density").required(),
|
|
99
101
|
densityCount: import_koishi.Schema.natural().default(5).description("条数阈值"),
|
|
100
102
|
densityWindowSeconds: import_koishi.Schema.natural().default(60).description("窗口秒数")
|
|
101
|
-
}).default({ type: "
|
|
103
|
+
}).default({ type: "frequency", densityCount: 5, densityWindowSeconds: 60 }).description("消息频率")
|
|
102
104
|
]);
|
|
103
105
|
var sessionKeySchema = import_koishi.Schema.union([
|
|
104
106
|
import_koishi.Schema.const("none").description("无"),
|
|
@@ -144,8 +146,9 @@ var variableSchema = import_koishi.Schema.union([
|
|
|
144
146
|
}).default({ source: "constant", field: "", value: "" }).description("常量文本"),
|
|
145
147
|
import_koishi.Schema.object({
|
|
146
148
|
source: import_koishi.Schema.const("json").required(),
|
|
149
|
+
field: import_koishi.Schema.string().required().description("字段名"),
|
|
147
150
|
template: import_koishi.Schema.string().role("textarea").required().description("JSON 对象")
|
|
148
|
-
}).default({ source: "json", template: "" }).description("JSON 对象")
|
|
151
|
+
}).default({ source: "json", field: "", template: "" }).description("JSON 对象")
|
|
149
152
|
]);
|
|
150
153
|
var legacySwitch = import_koishi.Schema.transform(
|
|
151
154
|
import_koishi.Schema.any(),
|
|
@@ -198,8 +201,7 @@ var Config = import_koishi.Schema.intersect([
|
|
|
198
201
|
import_koishi.Schema.object({
|
|
199
202
|
apps: import_koishi.Schema.array(
|
|
200
203
|
import_koishi.Schema.object({
|
|
201
|
-
name: import_koishi.Schema.string().required().description("
|
|
202
|
-
appId: import_koishi.Schema.string().default("").description("应用 ID,会话绑定用,留空取应用名"),
|
|
204
|
+
name: import_koishi.Schema.string().required().description("应用 ID"),
|
|
203
205
|
apiURL: import_koishi.Schema.string().required().role("link").description("API 地址"),
|
|
204
206
|
apiKey: import_koishi.Schema.string().required().role("secret").description("App Key"),
|
|
205
207
|
appType: appTypeSchema.default("auto").description("应用类型")
|
|
@@ -212,7 +214,7 @@ var Config = import_koishi.Schema.intersect([
|
|
|
212
214
|
name: import_koishi.Schema.string().required().description("规则名"),
|
|
213
215
|
enabled: import_koishi.Schema.boolean().default(true).description("启用"),
|
|
214
216
|
priority: import_koishi.Schema.number().default(-1).description("优先级,大者先匹配"),
|
|
215
|
-
app: import_koishi.Schema.string().required().description("
|
|
217
|
+
app: import_koishi.Schema.string().required().description("应用 ID"),
|
|
216
218
|
conversationScope: import_koishi.Schema.union([
|
|
217
219
|
import_koishi.Schema.const("per-guild").description("按群聊"),
|
|
218
220
|
import_koishi.Schema.const("per-user").description("按用户"),
|
|
@@ -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;
|
|
@@ -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 !== "
|
|
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
|
-
/** 计算规则下一次触发时刻;无
|
|
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 !== "
|
|
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: "
|
|
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: "
|
|
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: "
|
|
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 !== "
|
|
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)
|
|
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;
|
|
@@ -1792,6 +1795,8 @@ var RequestRunner = class {
|
|
|
1792
1795
|
static {
|
|
1793
1796
|
__name(this, "RequestRunner");
|
|
1794
1797
|
}
|
|
1798
|
+
/** 启动期类型探测任务:auto 应用首次请求在探测完成前等待它(settle 即放行,失败仍回退 chat) */
|
|
1799
|
+
probeTask;
|
|
1795
1800
|
/** 应用类型:schema 显式指定优先,auto 时用探测结果(未探测到回退 chat) */
|
|
1796
1801
|
appTypeOf(app) {
|
|
1797
1802
|
return app.appType !== "auto" ? app.appType : this.appTypes.get(app.name) ?? FALLBACK_APP_TYPE;
|
|
@@ -1807,7 +1812,7 @@ var RequestRunner = class {
|
|
|
1807
1812
|
const client = createDifyClient(this.ctx, app, this.config.requestTimeout);
|
|
1808
1813
|
if (!client) return;
|
|
1809
1814
|
const requestCtx = {
|
|
1810
|
-
bindingKey: this.conversationStore.computeBindingKey(app.
|
|
1815
|
+
bindingKey: this.conversationStore.computeBindingKey(app.name, rule, fireCtx),
|
|
1811
1816
|
difyUser: this.conversationStore.computeDifyUser(rule, fireCtx),
|
|
1812
1817
|
inputs: resolveVariables(rule, fireCtx, this.history),
|
|
1813
1818
|
uploadedFiles: []
|
|
@@ -1868,6 +1873,10 @@ var RequestRunner = class {
|
|
|
1868
1873
|
}
|
|
1869
1874
|
/** 按应用类型分流:workflow/completion 无会话,chat 系续接 conversation_id */
|
|
1870
1875
|
async dispatch(app, client, fireCtx, requestCtx) {
|
|
1876
|
+
if (app.appType === "auto" && !this.appTypes.has(app.name) && this.probeTask) {
|
|
1877
|
+
await this.probeTask.catch(() => {
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1871
1880
|
const base = {
|
|
1872
1881
|
inputs: requestCtx.inputs,
|
|
1873
1882
|
response_mode: "blocking",
|
|
@@ -2042,8 +2051,7 @@ var ConversationStore = class {
|
|
|
2042
2051
|
for (const row of matched) {
|
|
2043
2052
|
if (row.difyConversationId) {
|
|
2044
2053
|
const { appId } = this.parseBindingKey(row.bindingKey);
|
|
2045
|
-
const
|
|
2046
|
-
const client = app && createDifyClient(this.ctx, app);
|
|
2054
|
+
const client = appId && this.apps.has(appId) ? createDifyClient(this.ctx, this.apps.get(appId)) : void 0;
|
|
2047
2055
|
if (client) {
|
|
2048
2056
|
await client.deleteConversation(row.difyConversationId, this.difyUserOfBindingKey(row.bindingKey)).catch((error) => this.ctx.logger.warn("删除 Dify 会话失败:%s", error));
|
|
2049
2057
|
}
|
|
@@ -2053,13 +2061,6 @@ var ConversationStore = class {
|
|
|
2053
2061
|
}
|
|
2054
2062
|
return reset;
|
|
2055
2063
|
}
|
|
2056
|
-
/** 按应用 ID(apps 配置的 appId 字段)反查应用 */
|
|
2057
|
-
findByAppId(appId) {
|
|
2058
|
-
for (const app of this.apps.values()) {
|
|
2059
|
-
if (app.appId && app.appId === appId) return app;
|
|
2060
|
-
}
|
|
2061
|
-
return void 0;
|
|
2062
|
-
}
|
|
2063
2064
|
};
|
|
2064
2065
|
|
|
2065
2066
|
// src/state/tables.ts
|
|
@@ -2070,8 +2071,17 @@ function defineConversationTable(ctx) {
|
|
|
2070
2071
|
}, { primary: "bindingKey" });
|
|
2071
2072
|
}
|
|
2072
2073
|
__name(defineConversationTable, "defineConversationTable");
|
|
2074
|
+
function defineProbeTable(ctx) {
|
|
2075
|
+
ctx.model.extend("dify_app_probe", {
|
|
2076
|
+
name: "string(255)",
|
|
2077
|
+
mode: "string(32)",
|
|
2078
|
+
updatedAt: "timestamp"
|
|
2079
|
+
}, { primary: "name" });
|
|
2080
|
+
}
|
|
2081
|
+
__name(defineProbeTable, "defineProbeTable");
|
|
2073
2082
|
function defineTables(ctx) {
|
|
2074
2083
|
defineConversationTable(ctx);
|
|
2084
|
+
defineProbeTable(ctx);
|
|
2075
2085
|
}
|
|
2076
2086
|
__name(defineTables, "defineTables");
|
|
2077
2087
|
|
|
@@ -2093,9 +2103,29 @@ __name(registerMiscCommands, "registerMiscCommands");
|
|
|
2093
2103
|
// src/index.ts
|
|
2094
2104
|
var name = "dify-ai";
|
|
2095
2105
|
var inject = { required: ["database"] };
|
|
2106
|
+
var usage = `
|
|
2107
|
+
## JSON 对象变量的占位符
|
|
2108
|
+
|
|
2109
|
+
「JSON 对象」分支的模板内可用以下占位符(整个对象注入字段名对应的单一变量):
|
|
2110
|
+
|
|
2111
|
+
| 占位符 | 类型 | 说明 |
|
|
2112
|
+
| --- | --- | --- |
|
|
2113
|
+
| \`{{userId}}\` \`{{botId}}\` \`{{guildId}}\` \`{{channelId}}\` \`{{platform}}\` | 字符串 | 会话身份 |
|
|
2114
|
+
| \`{{messageId}}\` \`{{username}}\` \`{{timestamp}}\` | 字符串 | 消息标识与作者 |
|
|
2115
|
+
| \`{{content}}\` | 字符串 | 消息原貌 |
|
|
2116
|
+
| \`{{quoteContent}}\` \`{{quoteAuthorId}}\` | 字符串 | 被引用消息(引用段 / 作者) |
|
|
2117
|
+
| \`{{triggerType}}\` \`{{ruleName}}\` \`{{triggerTime}}\` | 字符串 | 触发元信息 |
|
|
2118
|
+
| \`{{history}}\` / \`{{history:N}}\` | 字符串 | 历史消息文本,N 指定条数(缺省用全局配置) |
|
|
2119
|
+
| \`{{isDirect}}\` | 布尔 | 是否私聊 |
|
|
2120
|
+
|
|
2121
|
+
- 值恰好为一个占位符(如 \`"is_private": "{{isDirect}}"\`)时保留原生类型
|
|
2122
|
+
- 占位符混入其他文字(如 \`"你好 {{username}}"\`)按字符串拼接,引号换行自动转义
|
|
2123
|
+
- 未知变量得空串
|
|
2124
|
+
`;
|
|
2096
2125
|
var PROBE_TIMEOUT_MS = 1e4;
|
|
2097
2126
|
function apply(ctx, config) {
|
|
2098
2127
|
const startedAt = Date.now();
|
|
2128
|
+
ctx.logger.info("apply 进入");
|
|
2099
2129
|
ctx.i18n.define("zh-CN", zh_CN_default);
|
|
2100
2130
|
ctx.i18n.define("en-US", en_US_default);
|
|
2101
2131
|
defineTables(ctx);
|
|
@@ -2105,11 +2135,16 @@ function apply(ctx, config) {
|
|
|
2105
2135
|
const conversationStore = new ConversationStore(ctx, apps);
|
|
2106
2136
|
const runner = new RequestRunner(ctx, config, conversationStore, history, apps, appTypes);
|
|
2107
2137
|
const engine = new TriggerEngine(ctx, runner, history, rules, cooldown);
|
|
2108
|
-
ctx.on("ready", () => {
|
|
2138
|
+
ctx.on("ready", async () => {
|
|
2139
|
+
const cached = await ctx.database.get("dify_app_probe", {}).catch(() => []);
|
|
2140
|
+
for (const row of cached) {
|
|
2141
|
+
if (apps.has(row.name)) appTypes.set(row.name, row.mode);
|
|
2142
|
+
}
|
|
2109
2143
|
engine.start();
|
|
2110
2144
|
ctx.logger.info("插件装配完成,耗时 %dms", Date.now() - startedAt);
|
|
2111
2145
|
const probeStart = Date.now();
|
|
2112
|
-
|
|
2146
|
+
runner.probeTask = probeAppTypes(ctx, apps, appTypes);
|
|
2147
|
+
void runner.probeTask.then(() => {
|
|
2113
2148
|
ctx.logger.info("应用类型探测完成,耗时 %dms", Date.now() - probeStart);
|
|
2114
2149
|
});
|
|
2115
2150
|
});
|
|
@@ -2127,6 +2162,7 @@ async function probeAppTypes(ctx, apps, appTypes) {
|
|
|
2127
2162
|
try {
|
|
2128
2163
|
const info = await client.getAppInfo();
|
|
2129
2164
|
appTypes.set(app.name, info.mode);
|
|
2165
|
+
await ctx.database.upsert("dify_app_probe", [{ name: app.name, mode: info.mode, updatedAt: /* @__PURE__ */ new Date() }]).catch((error) => ctx.logger.warn("探测结果写库失败:%s", error));
|
|
2130
2166
|
ctx.logger.info("应用 %s 类型探测:%s", app.name, info.mode);
|
|
2131
2167
|
} catch (error) {
|
|
2132
2168
|
ctx.logger.warn("应用 %s 类型探测失败(按 chat 处理):%s", app.name, error);
|
|
@@ -2140,5 +2176,6 @@ __name(probeAppTypes, "probeAppTypes");
|
|
|
2140
2176
|
Config,
|
|
2141
2177
|
apply,
|
|
2142
2178
|
inject,
|
|
2143
|
-
name
|
|
2179
|
+
name,
|
|
2180
|
+
usage
|
|
2144
2181
|
});
|
|
@@ -25,6 +25,8 @@ export declare class RequestRunner {
|
|
|
25
25
|
apps: Map<string, AppConfigEntry>,
|
|
26
26
|
/** 启动探测结果(name → Dify mode),由 index 在 ready 后填充 */
|
|
27
27
|
appTypes: Map<string, string>);
|
|
28
|
+
/** 启动期类型探测任务:auto 应用首次请求在探测完成前等待它(settle 即放行,失败仍回退 chat) */
|
|
29
|
+
probeTask?: Promise<void>;
|
|
28
30
|
/** 应用类型:schema 显式指定优先,auto 时用探测结果(未探测到回退 chat) */
|
|
29
31
|
private appTypeOf;
|
|
30
32
|
run(fireCtx: FireContext): Promise<void>;
|
|
@@ -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/state/tables.d.ts
CHANGED
|
@@ -5,9 +5,16 @@ export interface DifyConversationRow {
|
|
|
5
5
|
/** Dify 侧 conversation_id;workflow/completion 类应用恒为空 */
|
|
6
6
|
difyConversationId: string;
|
|
7
7
|
}
|
|
8
|
+
/** 应用类型探测的持久化缓存:重载后先读缓存分流,后台探测刷新 */
|
|
9
|
+
export interface DifyAppProbeRow {
|
|
10
|
+
name: string;
|
|
11
|
+
mode: string;
|
|
12
|
+
updatedAt: Date;
|
|
13
|
+
}
|
|
8
14
|
declare module 'koishi' {
|
|
9
15
|
interface Tables {
|
|
10
16
|
dify_conversation: DifyConversationRow;
|
|
17
|
+
dify_app_probe: DifyAppProbeRow;
|
|
11
18
|
}
|
|
12
19
|
}
|
|
13
20
|
export declare function defineTables(ctx: Context): void;
|
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.14",
|
|
5
5
|
"author": "PinkElysia",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"homepage": "https://github.com/PinkElysiaDev/dify-ai-plugin-for-Koishi#readme",
|